generator.initialize( KEY_SIZE );
KeyPair keypair = generator.genKeyPair();
// Generate the private key attributes
PrivateKey privateKey = keypair.getPrivate();
modifications.add( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultClientAttribute(
PRIVATE_KEY_AT, privateKey.getEncoded() ) ) );
modifications.add( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultClientAttribute(
PRIVATE_KEY_FORMAT_AT, privateKey.getFormat() ) ) );
// Generate public key
PublicKey publicKey = keypair.getPublic();
modifications.add( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultClientAttribute(
PUBLIC_KEY_AT, publicKey.getEncoded() ) ) );
modifications.add( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultClientAttribute(
PUBLIC_KEY_FORMAT_AT, publicKey.getFormat() ) ) );
// Generate the self-signed certificate
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 );
X509Certificate cert = certGen.generate( privateKey, "BC" );
modifications.add( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE, new DefaultClientAttribute(
USER_CERTIFICATE_AT, cert.getEncoded() ) ) );
// Write the modifications
ldapServer.getDirectoryService().getAdminSession().modify( dn, modifications );