JSSPKIUtility extends ProviderBasedPKIUtility { public static AuthorityKeyIdentifierExtension buildAuthorityKeyIdentifier(RSAPublicKey key) throws IOException { try { MessageDigest d = MessageDigest.getInstance("SHA-1"); byte[] encodedKey = key.getEncoded(); DerInputStream s = new DerValue(encodedKey).toDerInputStream(); s.skipSequence(0); BitArray b = s.getUnalignedBitString(); byte[] digest = d.digest(b.toByteArray()); KeyIdentifier ki = new KeyIdentifier(digest); return new AuthorityKeyIdentifierExtension(ki, null, null); } catch (NoSuchAlgorithmException e) { throw new IOException("Could not find SHA1 implementation", e); } } @Inject  JSSPKIUtility(CertificateReader reader, SubjectKeyIdentifierWriter writer, Configuration config); @Override X509Certificate createX509Certificate(String dn, Set<X509ExtensionWrapper> extensions,
        Set<X509ByteExtensionWrapper> byteExtensions, Date startDate, Date endDate, KeyPair clientKeyPair,
        BigInteger serialNumber, String alternateName); @Override X509CRL createX509CRL(List<X509CRLEntryWrapper> entries, BigInteger crlNumber); static AuthorityKeyIdentifierExtension buildAuthorityKeyIdentifier(RSAPublicKey key); static AuthorityKeyIdentifierExtension buildAuthorityKeyIdentifier(X509Certificate caCert); @Override byte[] getPemEncoded(X509Certificate cert); @Override byte[] getPemEncoded(RSAPrivateKey key); @Override byte[] getPemEncoded(X509CRL crl); @Override void writePemEncoded(X509Certificate cert, OutputStream out); @Override void writePemEncoded(RSAPrivateKey key, OutputStream out); @Override void writePemEncoded(X509CRL crl, OutputStream out); static final byte[] LINE_SEPARATOR; static final String SIGNING_ALG_ID; static final String CRL_PEM_NAME; static final String CERTIFICATE_PEM_NAME; static final String PRIVATE_KEY_PEM_NAME; }
@Test public void testCalculateAuthorityKeyIdentifier() throws Exception { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); RSAPublicKey key = (RSAPublicKey) gen.generateKeyPair().getPublic(); AuthorityKeyIdentifier expectedAki = new JcaX509ExtensionUtils().createAuthorityKeyIdentifier(key); AuthorityKeyIdentifierExtension actualAki = JSSPKIUtility.buildAuthorityKeyIdentifier(key); byte[] expectedKeyIdentifier = expectedAki.getKeyIdentifier(); byte[] actualKeyIdentifier = ((KeyIdentifier) actualAki.get(AuthorityKeyIdentifierExtension.KEY_ID)) .getIdentifier(); assertArrayEquals(expectedKeyIdentifier, actualKeyIdentifier); }