String sn = userX509Certificate.getSubjectDN().toString(); int cnPos = sn.toUpperCase().indexOf("CN=");
String sn = userX509Certificate.getSubjectDN().toString();
    public void createPEM() throws KeyStoreException,         FileNotFoundException, IOException, NoSuchProviderException,         NoSuchAlgorithmException, CertificateEncodingException, CertificateException,        UnrecoverableKeyException {        KeyStore ks = ks = KeyStore.getInstance("PKCS12", "BC");        InputStream in = new FileInputStream(p12File);        ks.load(in, password.toCharArray());        in.close();                // Fid the key private key entry in the keystore        Enumeration e = ks.aliases();        Object o = null;        PrivateKey serverPrivKey = null;        while (e.hasMoreElements()) {            o = e.nextElement();            if(o instanceof String) {                if ( (ks.isKeyEntry((String) o)) && ((serverPrivKey = (PrivateKey)ks.getKey((String) o, password.toCharArray())) != null) ) {                    cat.debug("Aliases " + o + " is KeyEntry.");                    break;                }            }                    }                cat.debug("Private key encode: " + serverPrivKey == null ? null : serverPrivKey.getFormat());        byte privKeyEncoded[] = "".getBytes();        if (serverPrivKey != null)            privKeyEncoded = serverPrivKey.getEncoded();                //Certificate chain[] = ks.getCertificateChain((String) o);        Certificate chain[] = KeyTools.getCertChain(ks, (String) o);        cat.debug("Loaded certificate chain with length "+chain.length+" from keystore.");        X509Certificate userX509Certificate = (X509Certificate) chain[0];                byte output[] = userX509Certificate.getEncoded();        String sn = userX509Certificate.getSubjectDN().toString();        int cnPos = sn.toUpperCase().indexOf("CN=");                String userFile = CertTools.getPartFromDN(sn, "CN");        String filetype = ".pem";                File path = new File(exportpath);        path.mkdir();        File tmpFile = new File(path, userFile + filetype);        if(!overwrite)            if(tmpFile.exists()) {                cat.error("File '"+tmpFile+"' already exists, don't overwrite.");                return;            }        OutputStream out = new FileOutputStream(tmpFile);        out.write(beginCertificate);        out.write(NL);        byte userCertB64[] = Base64.encode(output);        out.write(userCertB64);        out.write(NL);        out.write(endCertificate);        out.close();                tmpFile = new File(path, userFile + "-Key" + filetype);        if(!overwrite)            if(tmpFile.exists()) {                cat.error("File '"+tmpFile+"' already exists, don't overwrite.");                return;            }        out = new FileOutputStream(tmpFile);        out.write(beginPrivateKey);        out.write(NL);        byte privKey[] = Base64.encode(privKeyEncoded);        out.write(privKey);        out.write(NL);        out.write(endPrivateKey);        out.close();                tmpFile = new File(path, userFile + "-CA" + filetype);        if(!overwrite)            if(tmpFile.exists()) {                cat.error("File '"+tmpFile+"' already exists, don't overwrite.");                return;            }        if (CertTools.isSelfSigned(userX509Certificate)) {            cat.info("User certificate is selfsigned, this is a RootCA, no CA certificates written.");        } else {            out = new FileOutputStream(tmpFile);            for(int num = 1;num < chain.length;num++) {                X509Certificate tmpX509Cert = (X509Certificate) chain[num];                byte tmpOutput[] = tmpX509Cert.getEncoded();                out.write(beginCertificate);                out.write(NL);                byte tmpCACertB64[] = Base64.encode(tmpOutput);                out.write(tmpCACertB64);                out.write(NL);                out.write(endCertificate);                out.write(NL);            }            out.close();        }    } // createPEM