{
generator = KeyPairGenerator.getInstance( keyAlgo );
}
catch ( NoSuchAlgorithmException e )
{
LdapException ne = new LdapException( I18n.err( I18n.ERR_291 ) );
ne.initCause( e );
throw ne;
}
generator.initialize( KEY_SIZE );
KeyPair keypair = generator.genKeyPair();
entry.put( KEY_ALGORITHM_AT, keyAlgo );
// Generate the private key attributes
PrivateKey privateKey = keypair.getPrivate();
entry.put( PRIVATE_KEY_AT, privateKey.getEncoded() );
entry.put( PRIVATE_KEY_FORMAT_AT, privateKey.getFormat() );
LOG.debug( "PrivateKey: {}", privateKey );
PublicKey publicKey = keypair.getPublic();
entry.put( PUBLIC_KEY_AT, publicKey.getEncoded() );
entry.put( PUBLIC_KEY_FORMAT_AT, publicKey.getFormat() );
LOG.debug( "PublicKey: {}", publicKey );
// Generate the self-signed certificate
Date startDate = new Date();
Date expiryDate = new Date( System.currentTimeMillis() + YEAR_MILLIS );
BigInteger serialNumber = BigInteger.valueOf( System.currentTimeMillis() );
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
X500Principal issuerName = new X500Principal( issuerDN );
X500Principal subjectName = new X500Principal( subjectDN );
certGen.setSerialNumber( serialNumber );
certGen.setIssuerDN( issuerName );
certGen.setNotBefore( startDate );
certGen.setNotAfter( expiryDate );
certGen.setSubjectDN( subjectName );
certGen.setPublicKey( publicKey );
certGen.setSignatureAlgorithm( "SHA1With" + keyAlgo );
try
{
X509Certificate cert = certGen.generate( privateKey, "BC" );
entry.put( USER_CERTIFICATE_AT, cert.getEncoded() );
LOG.debug( "X509 Certificate: {}", cert );
}
catch ( Exception e )
{
LdapException ne = new LdapException( I18n.err( I18n.ERR_292 ) );
ne.initCause( e );
throw ne;
}
LOG.info( "Keys and self signed certificate successfully generated." );
}