System.out.println("issuer " + cert.getIssuerDN().toString() + ", " + cert.getClass().getName());
    private KeyStore generateToken(Admin administrator, String username, String password, int keylength, boolean createJKS, boolean loadkeys, boolean savekeys)       throws Exception{         KeyPair rsaKeys = null;          if(loadkeys){           // used saved keys.           IKeyRecoverySessionRemote keyrecoverysession = keyrecoveryhome.create();           rsaKeys = ((KeyRecoveryData) keyrecoverysession.keyRecovery(administrator, username)).getKeyPair();                       }         else{                // generate new keys.             rsaKeys = KeyTools.genKeys(keylength);         }                  ISignSessionRemote signsession = signsessionhome.create();         X509Certificate cert = (X509Certificate)signsession.createCertificate(administrator, username, password, rsaKeys.getPublic());         System.out.println("issuer " + cert.getIssuerDN().toString() + ", " + cert.getClass().getName());                 // Make a certificate chain from the certificate and the CA-certificate        CertificateFactory cf = CertificateFactory.getInstance("X.509");        Certificate[] cachain = signsession.getCertificateChain(administrator);        // Verify CA-certificate        if (CertTools.isSelfSigned((X509Certificate)cachain[cachain.length-1])) {            try {                cachain[cachain.length-1].verify(cachain[cachain.length-1].getPublicKey());            } catch (GeneralSecurityException se) {                throw new Exception("RootCA certificate does not verify");            }        }        else            throw new Exception("RootCA certificate not self-signed");        // Verify that the user-certificate is signed by our CA        try {            cert.verify(cachain[0].getPublicKey());        } catch (GeneralSecurityException se) {            throw new Exception("Generated certificate does not verify using CA-certificate.");        }        if(savekeys){          // Save generated keys to database.              IKeyRecoverySessionRemote keyrecoverysession = keyrecoveryhome.create();           keyrecoverysession.addKeyRecoveryData(administrator, cert, username, rsaKeys);                      }                        // Use CommonName as alias in the keystore        //String alias = CertTools.getPartFromDN(cert.getSubjectDN().toString(), "CN");        // Use username as alias in the keystore        String alias = username;        // Store keys and certificates in keystore.        KeyStore ks = null;        if (createJKS)            ks = KeyTools.createJKS(alias, rsaKeys.getPrivate(), password, cert, cachain);        else            ks = KeyTools.createP12(alias, rsaKeys.getPrivate(), cert, cachain);        return ks;    }