public static void updateWithSelfSignedServerCertificate ( KeyStore pKeyStore ) throws NoSuchProviderException , NoSuchAlgorithmException , IOException , InvalidKeyException , CertificateException , SignatureException , KeyStoreException { final Object x500Name ; final Object [ ] certAttributes = { "Jolokia Agent " + Version . getAgentVersion ( ) , // CN "JVM" , // OU "jolokia.org" , // O "Pegnitz" , // L "Franconia" , // ST "DE" } ; if ( ClassUtil . checkForClass ( X500_NAME_SUN ) ) { x500Name = ClassUtil . newInstance ( X500_NAME_SUN , certAttributes ) ; } else if ( ClassUtil . checkForClass ( X500_NAME_IBM ) ) { x500Name = ClassUtil . newInstance ( X500_NAME_IBM , certAttributes ) ; } else { throw new IllegalStateException ( "Neither Sun- nor IBM-style JVM found." ) ; } // Need to do it via reflection because Java8 moved class to a different package Object keypair = createKeyPair ( ) ; PrivateKey privKey = getPrivateKey ( keypair ) ; X509Certificate [ ] chain = new X509Certificate [ 1 ] ; chain [ 0 ] = getSelfCertificate ( keypair , x500Name , new Date ( ) , ( long ) 3650 * 24 * 60 * 60 ) ; pKeyStore . setKeyEntry ( "jolokia-agent" , privKey , new char [ 0 ] , chain ) ; }
Update the given keystore with a self signed server certificate . This can be used if no server certificate is provided from the outside and no SSL verification is used by the client .