@Test public void generateCredentialsTest() throws NoSuchAlgorithmException, IOException, CertificateException, NoSuchProviderException, OadrSecurityException, InvalidKeyException, SignatureException, OperatorCreationException { KeyPair ca = OadrPKISecurity.generateRsaKeyPair(); SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(ca.getPublic().getEncoded()); AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256withRSA"); AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); BigInteger sn = new BigInteger(64, new SecureRandom()); X509v3CertificateBuilder certificateGenerator = new X509v3CertificateBuilder( new X500Name("cn=TestCa"), sn, Date.from(LocalDateTime.now().minusDays(1).toInstant(ZoneOffset.UTC)), Date.from(LocalDateTime.now().plusDays(365).toInstant(ZoneOffset.UTC)), new X500Name("cn=TestSubject"), subPubKeyInfo); ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId) .build(PrivateKeyFactory.createKey(ca.getPrivate().getEncoded())); X509CertificateHolder holder = certificateGenerator.build(sigGen); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC"); X509Certificate caCert = (X509Certificate) certificateFactory .generateCertificate(new ByteArrayInputStream(holder.toASN1Structure().getEncoded())); String commonName = "test.oadr.com"; OadrUserX509Credential generateCredentials = OadrPKISecurity.generateCredentials(ca, caCert, commonName, OadrPKIAlgorithm.SHA256_RSA); testGeneratedCredential(generateCredentials, "SHA256withRSA"); generateCredentials = OadrPKISecurity.generateCredentials(ca, caCert, commonName, OadrPKIAlgorithm.SHA256_DSA); testGeneratedCredential(generateCredentials, "SHA256withDSA"); }
public static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert, String commonName, OadrPKIAlgorithm algo) throws OadrSecurityException { try { long now = System.currentTimeMillis(); String venCN = commonName; BigInteger serialNumber = BigInteger.valueOf(now); KeyPair venCred = null; String csrAlgo = ""; if (OadrPKIAlgorithm.SHA256_DSA.equals(algo)) { venCred = OadrPKISecurity.generateEccKeyPair(); csrAlgo = "SHA256withDSA"; } else if (OadrPKIAlgorithm.SHA256_RSA.equals(algo)) { venCred = OadrPKISecurity.generateRsaKeyPair(); csrAlgo = "SHA256withRSA"; } String x509PrincipalName = "C=FR, ST=Paris, L=Paris, O=Avob, OU=Avob, CN=" + venCN; PKCS10CertificationRequest csr = OadrPKISecurity.generateCsr(venCred, x509PrincipalName, csrAlgo); X509Certificate crt = OadrPKISecurity.signCsr(csr, caKeyPair, caCert, serialNumber); String fingerprint = OadrFingerprintSecurity.getOadr20bFingerprint(crt); File crtFile = writeToFile(venCN, "crt", OadrPKISecurity.writeCrtToString(crt)); File caCrtFile = writeToFile("ca", "crt", OadrPKISecurity.writeCrtToString(caCert)); File keyFile = writeToFile(venCN, "key", OadrPKISecurity.writeKeyToString(venCred)); File fingerprintFile = writeToFile(venCN, "fingerprint", fingerprint); return new OadrUserX509Credential(fingerprint, caCrtFile, crtFile, keyFile, fingerprintFile); } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | NoSuchProviderException | IOException e) { throw new OadrSecurityException(e); } }
OadrPKISecurity { public static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert, String commonName, OadrPKIAlgorithm algo) throws OadrSecurityException { try { long now = System.currentTimeMillis(); String venCN = commonName; BigInteger serialNumber = BigInteger.valueOf(now); KeyPair venCred = null; String csrAlgo = ""; if (OadrPKIAlgorithm.SHA256_DSA.equals(algo)) { venCred = OadrPKISecurity.generateEccKeyPair(); csrAlgo = "SHA256withDSA"; } else if (OadrPKIAlgorithm.SHA256_RSA.equals(algo)) { venCred = OadrPKISecurity.generateRsaKeyPair(); csrAlgo = "SHA256withRSA"; } String x509PrincipalName = "C=FR, ST=Paris, L=Paris, O=Avob, OU=Avob, CN=" + venCN; PKCS10CertificationRequest csr = OadrPKISecurity.generateCsr(venCred, x509PrincipalName, csrAlgo); X509Certificate crt = OadrPKISecurity.signCsr(csr, caKeyPair, caCert, serialNumber); String fingerprint = OadrFingerprintSecurity.getOadr20bFingerprint(crt); File crtFile = writeToFile(venCN, "crt", OadrPKISecurity.writeCrtToString(crt)); File caCrtFile = writeToFile("ca", "crt", OadrPKISecurity.writeCrtToString(caCert)); File keyFile = writeToFile(venCN, "key", OadrPKISecurity.writeKeyToString(venCred)); File fingerprintFile = writeToFile(venCN, "fingerprint", fingerprint); return new OadrUserX509Credential(fingerprint, caCrtFile, crtFile, keyFile, fingerprintFile); } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | NoSuchProviderException | IOException e) { throw new OadrSecurityException(e); } } }
OadrPKISecurity { public static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert, String commonName, OadrPKIAlgorithm algo) throws OadrSecurityException { try { long now = System.currentTimeMillis(); String venCN = commonName; BigInteger serialNumber = BigInteger.valueOf(now); KeyPair venCred = null; String csrAlgo = ""; if (OadrPKIAlgorithm.SHA256_DSA.equals(algo)) { venCred = OadrPKISecurity.generateEccKeyPair(); csrAlgo = "SHA256withDSA"; } else if (OadrPKIAlgorithm.SHA256_RSA.equals(algo)) { venCred = OadrPKISecurity.generateRsaKeyPair(); csrAlgo = "SHA256withRSA"; } String x509PrincipalName = "C=FR, ST=Paris, L=Paris, O=Avob, OU=Avob, CN=" + venCN; PKCS10CertificationRequest csr = OadrPKISecurity.generateCsr(venCred, x509PrincipalName, csrAlgo); X509Certificate crt = OadrPKISecurity.signCsr(csr, caKeyPair, caCert, serialNumber); String fingerprint = OadrFingerprintSecurity.getOadr20bFingerprint(crt); File crtFile = writeToFile(venCN, "crt", OadrPKISecurity.writeCrtToString(crt)); File caCrtFile = writeToFile("ca", "crt", OadrPKISecurity.writeCrtToString(caCert)); File keyFile = writeToFile(venCN, "key", OadrPKISecurity.writeKeyToString(venCred)); File fingerprintFile = writeToFile(venCN, "fingerprint", fingerprint); return new OadrUserX509Credential(fingerprint, caCrtFile, crtFile, keyFile, fingerprintFile); } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | NoSuchProviderException | IOException e) { throw new OadrSecurityException(e); } } private  OadrPKISecurity(); }
OadrPKISecurity { public static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert, String commonName, OadrPKIAlgorithm algo) throws OadrSecurityException { try { long now = System.currentTimeMillis(); String venCN = commonName; BigInteger serialNumber = BigInteger.valueOf(now); KeyPair venCred = null; String csrAlgo = ""; if (OadrPKIAlgorithm.SHA256_DSA.equals(algo)) { venCred = OadrPKISecurity.generateEccKeyPair(); csrAlgo = "SHA256withDSA"; } else if (OadrPKIAlgorithm.SHA256_RSA.equals(algo)) { venCred = OadrPKISecurity.generateRsaKeyPair(); csrAlgo = "SHA256withRSA"; } String x509PrincipalName = "C=FR, ST=Paris, L=Paris, O=Avob, OU=Avob, CN=" + venCN; PKCS10CertificationRequest csr = OadrPKISecurity.generateCsr(venCred, x509PrincipalName, csrAlgo); X509Certificate crt = OadrPKISecurity.signCsr(csr, caKeyPair, caCert, serialNumber); String fingerprint = OadrFingerprintSecurity.getOadr20bFingerprint(crt); File crtFile = writeToFile(venCN, "crt", OadrPKISecurity.writeCrtToString(crt)); File caCrtFile = writeToFile("ca", "crt", OadrPKISecurity.writeCrtToString(caCert)); File keyFile = writeToFile(venCN, "key", OadrPKISecurity.writeKeyToString(venCred)); File fingerprintFile = writeToFile(venCN, "fingerprint", fingerprint); return new OadrUserX509Credential(fingerprint, caCrtFile, crtFile, keyFile, fingerprintFile); } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | NoSuchProviderException | IOException e) { throw new OadrSecurityException(e); } } private  OadrPKISecurity(); static PrivateKey parsePrivateKey(String privateKeyFilePath); static PrivateKey parsePrivateKey(FileReader fileReader); static X509Certificate parseCertificate(String certificateFilePath); static X509Certificate parseCertificate(FileReader fileReader); static KeyStore createKeyStore(String privateKeyFilePath, String clientCertificatefilePath, String password); static KeyManagerFactory createKeyManagerFactory(String clientPrivateKeyPemFilePath,
			String clientCertificatePemFilePath, String password); static KeyStore createTrustStore(List<String> certificates); static TrustManagerFactory createTrustManagerFactory(List<String> trustedCertificateFilePath); static SSLContext createSSLContext(String clientPrivateKeyPemFilePath, String clientCertificatePemFilePath,
			List<String> trustCertificates, String password); static String md5Hex(String data); static KeyPair generateRsaKeyPair(); static KeyPair generateEccKeyPair(); static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert,
			String commonName, OadrPKIAlgorithm algo); }
OadrPKISecurity { public static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert, String commonName, OadrPKIAlgorithm algo) throws OadrSecurityException { try { long now = System.currentTimeMillis(); String venCN = commonName; BigInteger serialNumber = BigInteger.valueOf(now); KeyPair venCred = null; String csrAlgo = ""; if (OadrPKIAlgorithm.SHA256_DSA.equals(algo)) { venCred = OadrPKISecurity.generateEccKeyPair(); csrAlgo = "SHA256withDSA"; } else if (OadrPKIAlgorithm.SHA256_RSA.equals(algo)) { venCred = OadrPKISecurity.generateRsaKeyPair(); csrAlgo = "SHA256withRSA"; } String x509PrincipalName = "C=FR, ST=Paris, L=Paris, O=Avob, OU=Avob, CN=" + venCN; PKCS10CertificationRequest csr = OadrPKISecurity.generateCsr(venCred, x509PrincipalName, csrAlgo); X509Certificate crt = OadrPKISecurity.signCsr(csr, caKeyPair, caCert, serialNumber); String fingerprint = OadrFingerprintSecurity.getOadr20bFingerprint(crt); File crtFile = writeToFile(venCN, "crt", OadrPKISecurity.writeCrtToString(crt)); File caCrtFile = writeToFile("ca", "crt", OadrPKISecurity.writeCrtToString(caCert)); File keyFile = writeToFile(venCN, "key", OadrPKISecurity.writeKeyToString(venCred)); File fingerprintFile = writeToFile(venCN, "fingerprint", fingerprint); return new OadrUserX509Credential(fingerprint, caCrtFile, crtFile, keyFile, fingerprintFile); } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | NoSuchProviderException | IOException e) { throw new OadrSecurityException(e); } } private  OadrPKISecurity(); static PrivateKey parsePrivateKey(String privateKeyFilePath); static PrivateKey parsePrivateKey(FileReader fileReader); static X509Certificate parseCertificate(String certificateFilePath); static X509Certificate parseCertificate(FileReader fileReader); static KeyStore createKeyStore(String privateKeyFilePath, String clientCertificatefilePath, String password); static KeyManagerFactory createKeyManagerFactory(String clientPrivateKeyPemFilePath,
			String clientCertificatePemFilePath, String password); static KeyStore createTrustStore(List<String> certificates); static TrustManagerFactory createTrustManagerFactory(List<String> trustedCertificateFilePath); static SSLContext createSSLContext(String clientPrivateKeyPemFilePath, String clientCertificatePemFilePath,
			List<String> trustCertificates, String password); static String md5Hex(String data); static KeyPair generateRsaKeyPair(); static KeyPair generateEccKeyPair(); static OadrUserX509Credential generateCredentials(KeyPair caKeyPair, X509Certificate caCert,
			String commonName, OadrPKIAlgorithm algo);  }