Crypto { public static List<String> extractX509CertURIs(X509Certificate x509Cert) { return extractX509CertSANField(x509Cert, GeneralName.uniformResourceIdentifier); }  static String hmac(String message, String sharedSecret); static String sign(String message, PrivateKey key, String digestAlgorithm); static byte[] sign(byte[] message, PrivateKey key, String digestAlgorithm); static String sign(String message, PrivateKey key); static boolean verify(String message, PublicKey key, String signature,
                                 String digestAlgorithm); static boolean verify(String message, PublicKey key, String signature); static boolean verify(byte[] message, PublicKey key, byte[] signature,
                                 String digestAlgorithm); static byte [] sha256(byte [] data); static byte [] sha256(String text); static String ybase64(byte [] data); static byte [] ybase64Decode(String b64); static String ybase64DecodeString(String b64); static String ybase64EncodeString(String str); static String x509CertificatesToPEM(X509Certificate[] x509Certs); static X509Certificate[] loadX509Certificates(final String certsFile); static X509Certificate loadX509Certificate(File certFile); static X509Certificate loadX509Certificate(String pemEncoded); static X509Certificate loadX509Certificate(Reader reader); static PublicKey loadPublicKey(String pemEncoded); static PublicKey loadPublicKey(Reader r); static PublicKey loadPublicKey(File f); static PublicKey extractPublicKey(PrivateKey privateKey); static PrivateKey loadPrivateKey(String pemEncoded); static PrivateKey loadPrivateKey(Reader reader); static PrivateKey loadPrivateKey(File file); static PrivateKey loadPrivateKey(File file, String pwd); static PrivateKey loadPrivateKey(String pemEncoded, String pwd); static PrivateKey loadPrivateKey(Reader reader, String pwd); static PrivateKey generateRSAPrivateKey(int bits); static String randomSalt(); static String encodedFile(File f); static String encodedFile(FileInputStream is); static PKCS10CertificationRequest getPKCS10CertRequest(String csr); static String extractX509CSRSubjectField(PKCS10CertificationRequest certReq, ASN1ObjectIdentifier id); static String extractX509CSRCommonName(PKCS10CertificationRequest certReq); static String extractX509CSRSubjectOField(PKCS10CertificationRequest certReq); static String extractX509CSRSubjectOUField(PKCS10CertificationRequest certReq); static String extractX509CSREmail(PKCS10CertificationRequest certReq); static List<String> extractX509CSREmails(PKCS10CertificationRequest certReq); static List<String> extractX509CSRDnsNames(PKCS10CertificationRequest certReq); static List<String> extractX509CSRURIs(PKCS10CertificationRequest certReq); static List<String> extractX509CSRIPAddresses(PKCS10CertificationRequest certReq); static String extractX509CSRPublicKey(PKCS10CertificationRequest certReq); static String generateX509CSR(PrivateKey privateKey, String x500Principal,
                                         GeneralName[] sanArray); static String generateX509CSR(PrivateKey privateKey, PublicKey publicKey,
                                         String x500Principal, GeneralName[] sanArray); static String extractX509CertSubjectField(X509Certificate x509Cert, ASN1ObjectIdentifier id); static long extractX509CertIssueTime(X509Certificate x509Cert); static String extractX509CertCommonName(X509Certificate x509Cert); static String extractX509CertSubjectOUField(X509Certificate x509Cert); static String extractX509CertSubjectOField(X509Certificate x509Cert); static boolean isRestrictedCertificate(X509Certificate x509Cert, GlobStringsMatcher globStringsMatcher); static List<String> extractX509CertDnsNames(X509Certificate x509Cert); static List<String> extractX509CertEmails(X509Certificate x509Cert); static List<String> extractX509CertIPAddresses(X509Certificate x509Cert); static List<String> extractX509CertURIs(X509Certificate x509Cert); static String extractX509CertPublicKey(X509Certificate x509Cert); static X509Certificate generateX509Certificate(PKCS10CertificationRequest certReq,
            PrivateKey caPrivateKey, X509Certificate caCertificate, int validityTimeout,
            boolean basicConstraints); static X509Certificate generateX509Certificate(PKCS10CertificationRequest certReq,
            PrivateKey caPrivateKey, X500Name issuer, int validityTimeout,
            boolean basicConstraints); static boolean validatePKCS7Signature(String data, String signature, PublicKey publicKey); static String convertToPEMFormat(Object obj); static void main(String[] args); static final String SHA1; static final String SHA256; }
@Test public void testExtractX509CertURIsNull() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/valid_cn_x509.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); List<String> uris = Crypto.extractX509CertURIs(cert); assertTrue(uris.isEmpty()); } try (InputStream inStream = new FileInputStream("src/test/resources/x509_altnames_noip.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); List<String> uris = Crypto.extractX509CertURIs(cert); assertTrue(uris.isEmpty()); } }
@Test public void testExtractX509CertURIsNullSingle() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/x509_altnames_singleuri.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); List<String> uris = Crypto.extractX509CertURIs(cert); assertEquals(1, uris.size()); assertEquals(uris.get(0), "spiffe: } }
@Test public void testExtractX509CertURIsNullDouble() throws Exception { try (InputStream inStream = new FileInputStream("src/test/resources/x509_altnames_doubleuri.cert")) { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); List<String> uris = Crypto.extractX509CertURIs(cert); assertEquals(2, uris.size()); assertEquals(uris.get(0), "spiffe: assertEquals(uris.get(1), "spiffe: } }