@Test public void testReadKeyPair() throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { KeyPair keyPair = CertUtils.readKeyPairFromPemFile("src/test/resources/internal.key"); assertNotNull(keyPair); assertNotNull(keyPair.getPrivate()); assertNotNull(keyPair.getPublic()); }
public static KeyPair readKeyPairFromPemFile(String filename) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { File pkFile = new File(filename); try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) { Object o = pemParser.readObject(); if (o == null || !(o instanceof PrivateKeyInfo)) { throw new CodedException(X_INTERNAL_ERROR, "Could not read key from '%s'", filename); } PrivateKeyInfo pki = (PrivateKeyInfo) o; KeyFactory kf = KeyFactory.getInstance("RSA"); final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded()); final PrivateKey privateKey = kf.generatePrivate(ks); final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey()); final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(), rpk.getPublicExponent())); KeyPair kp = new KeyPair(publicKey, privateKey); return kp; } }
CertUtils { public static KeyPair readKeyPairFromPemFile(String filename) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { File pkFile = new File(filename); try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) { Object o = pemParser.readObject(); if (o == null || !(o instanceof PrivateKeyInfo)) { throw new CodedException(X_INTERNAL_ERROR, "Could not read key from '%s'", filename); } PrivateKeyInfo pki = (PrivateKeyInfo) o; KeyFactory kf = KeyFactory.getInstance("RSA"); final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded()); final PrivateKey privateKey = kf.generatePrivate(ks); final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey()); final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(), rpk.getPublicExponent())); KeyPair kp = new KeyPair(publicKey, privateKey); return kp; } } }
CertUtils { public static KeyPair readKeyPairFromPemFile(String filename) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { File pkFile = new File(filename); try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) { Object o = pemParser.readObject(); if (o == null || !(o instanceof PrivateKeyInfo)) { throw new CodedException(X_INTERNAL_ERROR, "Could not read key from '%s'", filename); } PrivateKeyInfo pki = (PrivateKeyInfo) o; KeyFactory kf = KeyFactory.getInstance("RSA"); final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded()); final PrivateKey privateKey = kf.generatePrivate(ks); final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey()); final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(), rpk.getPublicExponent())); KeyPair kp = new KeyPair(publicKey, privateKey); return kp; } } private  CertUtils(); }
CertUtils { public static KeyPair readKeyPairFromPemFile(String filename) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { File pkFile = new File(filename); try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) { Object o = pemParser.readObject(); if (o == null || !(o instanceof PrivateKeyInfo)) { throw new CodedException(X_INTERNAL_ERROR, "Could not read key from '%s'", filename); } PrivateKeyInfo pki = (PrivateKeyInfo) o; KeyFactory kf = KeyFactory.getInstance("RSA"); final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded()); final PrivateKey privateKey = kf.generatePrivate(ks); final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey()); final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(), rpk.getPublicExponent())); KeyPair kp = new KeyPair(publicKey, privateKey); return kp; } } private  CertUtils(); static String getSubjectCommonName(X509Certificate cert); static String getSubjectSerialNumber(X509Certificate cert); static ClientId getSubjectClientId(X509Certificate cert); static boolean isAuthCert(X509Certificate cert); static boolean isSigningCert(X509Certificate cert); static boolean isValid(X509Certificate cert); static boolean isSelfSigned(X509Certificate cert); static String getOcspResponderUriFromCert(X509Certificate subject); static String[] getCertHashes(List<X509Certificate> certs); static String getRDNValue(X500Name name, ASN1ObjectIdentifier id); static byte[] generateCertRequest(PrivateKey privateKey, PublicKey publicKey, String
            principal); static KeyPair readKeyPairFromPemFile(String filename); static void writePemToFile(byte[] certBytes, String filename); static X509Certificate[] readCertificateChain(String filename); static void createPkcs12(String filenameKey, String filenamePem, String filenameP12); static String identify(X509Certificate certificate); }
CertUtils { public static KeyPair readKeyPairFromPemFile(String filename) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { File pkFile = new File(filename); try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) { Object o = pemParser.readObject(); if (o == null || !(o instanceof PrivateKeyInfo)) { throw new CodedException(X_INTERNAL_ERROR, "Could not read key from '%s'", filename); } PrivateKeyInfo pki = (PrivateKeyInfo) o; KeyFactory kf = KeyFactory.getInstance("RSA"); final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded()); final PrivateKey privateKey = kf.generatePrivate(ks); final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey()); final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(), rpk.getPublicExponent())); KeyPair kp = new KeyPair(publicKey, privateKey); return kp; } } private  CertUtils(); static String getSubjectCommonName(X509Certificate cert); static String getSubjectSerialNumber(X509Certificate cert); static ClientId getSubjectClientId(X509Certificate cert); static boolean isAuthCert(X509Certificate cert); static boolean isSigningCert(X509Certificate cert); static boolean isValid(X509Certificate cert); static boolean isSelfSigned(X509Certificate cert); static String getOcspResponderUriFromCert(X509Certificate subject); static String[] getCertHashes(List<X509Certificate> certs); static String getRDNValue(X500Name name, ASN1ObjectIdentifier id); static byte[] generateCertRequest(PrivateKey privateKey, PublicKey publicKey, String
            principal); static KeyPair readKeyPairFromPemFile(String filename); static void writePemToFile(byte[] certBytes, String filename); static X509Certificate[] readCertificateChain(String filename); static void createPkcs12(String filenameKey, String filenamePem, String filenameP12); static String identify(X509Certificate certificate);  }