attributeSet = getAttributeSet(cAObjectclass, dn, false);
attributeSet = getAttributeSet(cAObjectclass, dn, true, false);
    public boolean storeCertificate(Admin admin, Certificate incert, String username, String cafp, int status, int type) {        debug(">storeCertificate(username="+username+")");        int ldapVersion = LDAPConnection.LDAP_V3;        LDAPConnection lc = new LDAPConnection();        String dn = null;        try {            // Extract the users DN from the cert.            dn = CertTools.getSubjectDN((X509Certificate) incert);        } catch (Exception e) {            error("Error decoding input certificate: ", e);                         logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null, (X509Certificate) incert,                    LogEntry.EVENT_ERROR_STORECERTIFICATE, "Error decoding input certificate.");            return false;        }        // Extract the users email from the cert.        // First see if we have subjectAltNames extension        String email = null;        byte[] subjAltNameValue = ((X509Certificate) incert).getExtensionValue("2.5.29.17");        // If not, see if we have old styld email-in-DN        if (subjAltNameValue == null) {            email = CertTools.getPartFromDN(dn, "EmailAddress");        } else {            try {                // Get extension value                ByteArrayInputStream bIn = new ByteArrayInputStream(subjAltNameValue);                DEROctetString asn1 = (DEROctetString) new DERInputStream(bIn).readObject();                ByteArrayInputStream bIn1 = new ByteArrayInputStream(asn1.getOctets());                ASN1Sequence san = (ASN1Sequence) new DERInputStream(bIn1).readObject();                for (int i = 0; i < san.size(); i++) {                    DERTaggedObject gn = (DERTaggedObject) san.getObjectAt(i);                    if (gn.getTagNo() == 1) {                        // This is rfc822Name!                        DERIA5String str;                        if (gn.getObject() instanceof DERIA5String) {                            str = (DERIA5String) gn.getObject();                        } else {                            str = new DERIA5String(((DEROctetString) gn.getObject()).getOctets());                        }                        email = str.getString();                    }                }            } catch (IOException e) {                error("IOException when getting subjectAltNames extension.");                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                    (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                    "IOException when getting subjectAltNames extension.");            }        }        if (checkContainerName(dn) == false) {            info("DN not part of containername, aborting store operation.");                                                                     logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                    (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                    "DN not part of containername, aborting store operation.");                        return false;        }        // Check if the entry is already present, we will update it with the new certificate.        LDAPEntry oldEntry = null;        try {            // connect to the server            lc.connect(ldapHost, ldapPort);            // authenticate to the server            lc.bind(ldapVersion, loginDN, loginPassword);            // try to read the old object            oldEntry = lc.read(dn);            // disconnect with the server            lc.disconnect();        } catch (LDAPException e) {            if (e.getLDAPResultCode() == LDAPException.NO_SUCH_OBJECT) {                debug("No old entry exist for '" + dn + "'.");                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                        (X509Certificate) incert, LogEntry.EVENT_INFO_STORECERTIFICATE,                        "No old entry exist for '" + dn + "'.");                            } else {                error("Error binding to and reading from LDAP server: ", e);                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                       (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                       "Error binding to and reading from LDAP server.");                                return false;            }        }        LDAPEntry newEntry = null;        LDAPModificationSet modSet = null;        LDAPAttributeSet attributeSet = null;        String attribute = null;        String objectclass = null;        if (type == SecConst.CERTTYPE_ENDENTITY) {            debug("Publishing end user certificate to " + ldapHost);            if (oldEntry != null) {                // TODO: Are we the correct type objectclass?                modSet = getModificationSet(oldEntry, dn, true);            } else {                objectclass = userObjectclass;            }            attributeSet = getAttributeSet(userObjectclass, dn, true);            if (email != null) {                LDAPAttribute mailAttr = new LDAPAttribute("mail", email);                if (oldEntry != null) {                    modSet.add(LDAPModification.REPLACE, mailAttr);                } else {                    attributeSet.add(mailAttr);                }            }            try {                attribute = userCertAttribute;                LDAPAttribute certAttr = new LDAPAttribute(userCertAttribute, incert.getEncoded());                if (oldEntry != null) {                    modSet.add(LDAPModification.REPLACE, certAttr);                } else {                    attributeSet.add(certAttr);                }            } catch (CertificateEncodingException e) {                error("Error encoding certificate when storing in LDAP: ", e);                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                       (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                       "Error encoding certificate when storing in LDAP.");                return false;            }        } else if ((type == SecConst.CERTTYPE_SUBCA) || (type == SecConst.CERTTYPE_ROOTCA)) {            debug("Publishing CA certificate to " + ldapHost);            if (oldEntry != null) {                modSet = getModificationSet(oldEntry, dn, false);            } else {                objectclass = cAObjectclass;            }            attributeSet = getAttributeSet(cAObjectclass, dn, false);            try {                attribute = cACertAttribute;                LDAPAttribute certAttr = new LDAPAttribute(cACertAttribute, incert.getEncoded());                if (oldEntry != null) {                    modSet.add(LDAPModification.REPLACE, certAttr);                } else {                    attributeSet.add(certAttr);                    // Also create using the crlattribute, it may be required                    LDAPAttribute crlAttr = new LDAPAttribute(cRLAttribute, "");                    attributeSet.add(crlAttr);                    debug("Added (fake) attribute for CRL.");                }            } catch (CertificateEncodingException e) {                error("Error encoding certificate when storing in LDAP: ", e);                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                        (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                        "Error encoding certificate when storing in LDAP.");                return false;            }        } else {            info("Certificate of type '" + type + "' will not be published.");                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                    (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                    "Certificate of type '" + type + "' will not be published.");                      return false;        }        try {            // connect to the server            lc.connect(ldapHost, ldapPort);            // authenticate to the server            lc.bind(ldapVersion, loginDN, loginPassword);            // Add or modify the entry            if (oldEntry != null) {                lc.modify(dn, modSet);                debug("\nModified object: " + dn + " successfully.");                  logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                        (X509Certificate) incert, LogEntry.EVENT_INFO_STORECERTIFICATE,                        "Modified object: " + dn + " successfully in LDAP.");            } else {                if (oldEntry == null) {                    newEntry = new LDAPEntry(dn, attributeSet);                }                lc.add(newEntry);                debug("\nAdded object: " + dn + " successfully.");                logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                        (X509Certificate) incert, LogEntry.EVENT_INFO_STORECERTIFICATE,                        "Added object: " + dn + " successfully in LDAP.");            }            // disconnect with the server            lc.disconnect();        } catch (LDAPException e) {            error("Error storing certificate (" + attribute + ") in LDAP (" + objectclass + "): ", e);               logsession.log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), null,                    (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,                    "Error storing certificate (" + attribute + ") in LDAP (" + objectclass + ").");            return false;        }        debug("<storeCertificate()");        return true;    } // storeCertificate