public synchronized X509Certificate getMappedCertificate ( final X509Certificate cert ) throws CertificateEncodingException , InvalidKeyException , CertificateException , CertificateNotYetValidException , NoSuchAlgorithmException , NoSuchProviderException , SignatureException , KeyStoreException , UnrecoverableKeyException { String thumbprint = ThumbprintUtil . getThumbprint ( cert ) ; String mappedCertThumbprint = _certMap . get ( thumbprint ) ; if ( mappedCertThumbprint == null ) { // Check if we've already mapped this public key from a KeyValue PublicKey mappedPk = getMappedPublicKey ( cert . getPublicKey ( ) ) ; PrivateKey privKey ; if ( mappedPk == null ) { PublicKey pk = cert . getPublicKey ( ) ; String algo = pk . getAlgorithm ( ) ; KeyPair kp ; if ( algo . equals ( "RSA" ) ) { kp = getRSAKeyPair ( ) ; } else if ( algo . equals ( "DSA" ) ) { kp = getDSAKeyPair ( ) ; } else { throw new InvalidKeyException ( "Key algorithm " + algo + " not supported." ) ; } mappedPk = kp . getPublic ( ) ; privKey = kp . getPrivate ( ) ; mapPublicKeys ( cert . getPublicKey ( ) , mappedPk ) ; } else { privKey = getPrivateKey ( mappedPk ) ; } X509Certificate replacementCert = CertificateCreator . mitmDuplicateCertificate ( cert , mappedPk , getSigningCert ( ) , getSigningPrivateKey ( ) ) ; addCertAndPrivateKey ( null , replacementCert , privKey ) ; mappedCertThumbprint = ThumbprintUtil . getThumbprint ( replacementCert ) ; _certMap . put ( thumbprint , mappedCertThumbprint ) ; _certMap . put ( mappedCertThumbprint , thumbprint ) ; _subjectMap . put ( replacementCert . getSubjectX500Principal ( ) . getName ( ) , thumbprint ) ; if ( persistImmediately ) { persist ( ) ; } return replacementCert ; } return getCertificateByAlias ( mappedCertThumbprint ) ; }
This method returns the duplicated certificate mapped to the passed in cert or creates and returns one if no mapping has yet been performed . If a naked public key has already been mapped that matches the key in the cert the already mapped keypair will be reused for the mapped cert .