summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/publish/publishers/LdapCertSubjPublisher.java
blob: 0c596f3b0e7839d1f269cac6d1c74a93c362f452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cms.publish.publishers;


import java.io.IOException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.Vector;

import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPModification;
import netscape.ldap.LDAPModificationSet;
import netscape.ldap.LDAPSearchResults;
import netscape.ldap.LDAPv2;
import netscape.security.x509.X500Name;
import netscape.security.x509.X509CertImpl;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.ldap.ELdapException;
import com.netscape.certsrv.ldap.ELdapServerDownException;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.publish.ILdapPublisher;


/** 
 * Interface for mapping a X509 certificate to a LDAP entry 
 * Publishes a certificate as binary and its subject name.
 * there is one subject name value for each certificate. 
 *
 * @version $Revision$, $Date$
 */
public class LdapCertSubjPublisher implements ILdapPublisher {
    public static final String LDAP_CERTSUBJNAME_ATTR = "certSubjectName";
    protected String mCertAttr = LdapUserCertPublisher.LDAP_USERCERT_ATTR;
    protected String mSubjNameAttr = LDAP_CERTSUBJNAME_ATTR;

    private ILogger mLogger = CMS.getLogger();
    private boolean mInited = false;
    protected IConfigStore mConfig = null;

    /**
     * constructor using default certificate subject name and attribute for
     * publishing subject name.
     */
    public LdapCertSubjPublisher() {
    }

    public String getImplName() {
        return "LdapCertSubjPublisher";
    }

    public String getDescription() {
        return "LdapCertSubjPublisher";
    }

    public Vector getInstanceParams() {
        Vector v = new Vector();

        v.addElement("certAttr=" + mCertAttr);
        v.addElement("subjectNameAttr=" + mSubjNameAttr);
        return v;
    }

    public Vector getDefaultParams() {
        Vector v = new Vector();

        v.addElement("certAttr=" + mCertAttr);
        v.addElement("subjectNameAttr=" + mSubjNameAttr);
        return v;
    }

    public IConfigStore getConfigStore() {
        return mConfig;
    }

    public void init(IConfigStore config)
        throws EBaseException {
        if (mInited)
            return;
        mConfig = config;
        mCertAttr = mConfig.getString("certAttr", 
                    LdapUserCertPublisher.LDAP_USERCERT_ATTR);
        mSubjNameAttr = mConfig.getString("certSubjectName", 
                    LDAP_CERTSUBJNAME_ATTR);
        mInited = true;
    }

    /**
     * constrcutor using specified certificate attribute and 
     * certificate subject name attribute.
     */
    public LdapCertSubjPublisher(String certAttr, String subjNameAttr) {
        mCertAttr = certAttr;
        mSubjNameAttr = subjNameAttr;
    }

    public String getCertAttr() {
        return mCertAttr;
    }

    public String getSubjNameAttr() {
        return mSubjNameAttr;
    }

    public void setSubjNameAttr(String subjNameAttr) {
        mSubjNameAttr = subjNameAttr;
    }

    public void setCertAttr(String certAttr) {
        mCertAttr = certAttr;
    }

    /**
     * publish a user certificate
     * Adds the cert to the multi-valued certificate attribute as a
     * DER encoded binary blob. Does not check if cert already exists.
     * Then adds the subject name of the cert to the subject name attribute.
     * @param conn the LDAP connection
     * @param dn dn of the entry to publish the certificate
     * @param certObj the certificate  object.
     * @exception ELdapException if cert or subject name already exists, 
     * 				if cert encoding fails, if getting cert subject name fails.
     *			Use ELdapException.getException() to find underlying exception.
     */
    public void publish(LDAPConnection conn, String dn, Object certObj)
        throws ELdapException {
        if (conn == null) {
            log(ILogger.LL_INFO, "LdapCertSubjPublisher: no LDAP connection");
            return;
        }

        if (!(certObj instanceof X509Certificate))
            throw new IllegalArgumentException("Illegal arg to publish");

        X509Certificate cert = (X509Certificate) certObj;

        try {
            boolean hasCert = false, hasSubjname = false;
            byte[] certEnc = cert.getEncoded();
            String subjName = ((X500Name) cert.getSubjectDN()).toLdapDNString();

            LDAPSearchResults res = 
                conn.search(dn, LDAPv2.SCOPE_BASE, "(objectclass=*)", 
                    new String[] { mCertAttr, mSubjNameAttr }, false);

            LDAPEntry entry = res.next();
            LDAPAttribute certs = entry.getAttribute(mCertAttr);
            LDAPAttribute subjnames = entry.getAttribute(mSubjNameAttr);

            // check if has cert already.
            if (certs != null) {
                hasCert = LdapUserCertPublisher.ByteValueExists(certs, certEnc);
            }

            // check if has subject name already.
            if (subjnames != null) {
                hasSubjname = 
                        LdapUserCertPublisher.StringValueExists(subjnames, subjName);
            }

            // if has both, done.
            if (hasCert && hasSubjname) {
                log(ILogger.LL_INFO, 
                    "publish: " + subjName + " already has cert & subject name");
                return;
            }

            // add cert if not already there.
            LDAPModificationSet modSet = new LDAPModificationSet();

            if (!hasCert) {
                log(ILogger.LL_INFO, "publish: adding cert to " + subjName);
                modSet.add(LDAPModification.ADD, 
                    new LDAPAttribute(mCertAttr, certEnc)); 
            }
            // add subject name if not already there.
            if (!hasSubjname) {
                log(ILogger.LL_INFO, "publish: adding " + subjName + " to " + dn);
                modSet.add(LDAPModification.ADD, 
                    new LDAPAttribute(mSubjNameAttr, subjName)); 
            }
            conn.modify(dn, modSet);
        } catch (CertificateEncodingException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_PUBLISH_ERROR", e.toString()));
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_GET_DER_ENCODED_CERT_FAILED", e.toString()));
        } catch (LDAPException e) {
            if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE) {
                // need to intercept this because message from LDAP is
                // "DSA is unavailable" which confuses with DSA PKI.
                log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("PUBLISH_NO_LDAP_SERVER"));
                throw new ELdapServerDownException(CMS.getUserMessage("CMS_LDAP_SERVER_UNAVAILABLE", conn.getHost(), "" + conn.getPort()));
            } else {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_PUBLISHER_EXCEPTION", "", e.toString()));
                throw new ELdapException(CMS.getUserMessage("CMS_LDAP_PUBLISH_USERCERT_ERROR", e.toString()));
            }
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_PUBLISH_ERROR", e.toString()));
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_PUBLISH_USERCERT_ERROR", e.toString()));
        }
    }

    /**
     * deletes the certificate from the list of certificates.
     * does not check if certificate is already there.
     * also takes out the subject name if no other certificate remain
     * with the same subject name.
     */
    public void unpublish(LDAPConnection conn, String dn, Object certObj)
        throws ELdapException {
        if (!(certObj instanceof X509Certificate))
            throw new IllegalArgumentException("Illegal arg to publish");

        try {
            boolean hasCert = false, hasSubjname = false;
            boolean hasAnotherCert = false;
            X509Certificate cert = (X509Certificate) certObj;
            String subjName = ((X500Name) cert.getSubjectDN()).toLdapDNString();

            byte[] certEnc = cert.getEncoded();

            LDAPSearchResults res = 
                conn.search(dn, LDAPv2.SCOPE_BASE, "(objectclass=*)", 
                    new String[] { mCertAttr, mSubjNameAttr }, false);

            LDAPEntry entry = res.next();
            LDAPAttribute certs = entry.getAttribute(mCertAttr);
            LDAPAttribute subjnames = entry.getAttribute(mSubjNameAttr);

            // check for cert and other certs with same subject name.
            if (certs != null) {
                hasCert = LdapUserCertPublisher.ByteValueExists(certs, certEnc);
                // check for other certs with the same subject name
                Enumeration vals = certs.getByteValues();
                byte[] val = null;

                while (vals.hasMoreElements()) {
                    val = (byte[]) vals.nextElement();
                    if (Utils.byteArraysAreEqual(certEnc, val)) {
                        hasCert = true;
                        continue;
                    }
                    try {
                        X509CertImpl certval = new X509CertImpl(val);
                        // XXX use some sort of X500name equals function here.
                        String subjnam = 
                            ((X500Name) certval.getSubjectDN()).toLdapDNString();

                        if (subjnam.equalsIgnoreCase(subjName)) {
                            hasAnotherCert = true;
                        }
                    } catch (CertificateEncodingException e) {
                        // ignore this certificate.
                        CMS.debug(
                            "LdapCertSubjPublisher: unpublish: an invalid cert in dn entry encountered");
                    } catch (CertificateException e) {
                        // ignore this certificate.
                        CMS.debug(
                            "LdapCertSubjPublisher: unpublish: an invalid cert in dn entry encountered");
                    }
                }
            }

            // check if doesn't have subject name already.
            if (subjnames != null) {
                hasSubjname = 
                        LdapUserCertPublisher.StringValueExists(subjnames, subjName);
            }

            // if doesn't have both, done.
            if (!hasCert && !hasSubjname) {
                log(ILogger.LL_INFO, 
                    "unpublish: " + subjName + " already has not cert & subjname");
                return;
            }

            // delete cert if there. 
            LDAPModificationSet modSet = new LDAPModificationSet();

            if (hasCert) {
                log(ILogger.LL_INFO, 
                    "unpublish: deleting cert " + subjName + " from " + dn);
                modSet.add(LDAPModification.DELETE,
                    new LDAPAttribute(mCertAttr, certEnc));
            }
            // delete subject name if no other cert has the same name.
            if (hasSubjname && !hasAnotherCert) {
                log(ILogger.LL_INFO, 
                    "unpublish: deleting subject name " + subjName + " from " + dn);
                modSet.add(LDAPModification.DELETE,
                    new LDAPAttribute(mSubjNameAttr, subjName));
            }
            conn.modify(dn, modSet); 
        } catch (CertificateEncodingException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_UNPUBLISH_ERROR", e.toString()));
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_GET_DER_ENCODED_CERT_FAILED", e.toString()));
        } catch (CertificateException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_UNPUBLISH_ERROR", e.toString()));
            throw new ELdapException(
                    CMS.getUserMessage("CMS_LDAP_DECODING_CERT_FAILED", e.toString()));
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_UNPUBLISH_ERROR", e.toString()));
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_GET_LDAP_DN_STRING_FAILED", e.toString()));
        } catch (LDAPException e) {
            if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE) {
                // need to intercept this because message from LDAP is
                // "DSA is unavailable" which confuses with DSA PKI.
                log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("PUBLISH_NO_LDAP_SERVER"));
                throw new ELdapServerDownException(CMS.getUserMessage("CMS_LDAP_SERVER_UNAVAILABLE", conn.getHost(), "" + conn.getPort()));
            } else {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_UNPUBLISH_ERROR", e.toString()));
                throw new ELdapException(CMS.getUserMessage("CMS_LDAP_UNPUBLISH_USERCERT_ERROR", e.toString()));
            }
        }
        return;
    }

    private void log(int level, String msg) {
        mLogger.log(ILogger.EV_SYSTEM, ILogger.S_LDAP, level,
            "LdapCertSubjPublisher: " + msg);
    }

}