CertificateUtil { public static List<Object> getSubjectAltNameField(X509Certificate certificate, int field) { try { List<Object> values = new ArrayList<>(); Collection<List<?>> subjectAltNames = certificate.getSubjectAlternativeNames(); if (subjectAltNames == null) subjectAltNames = Collections.emptyList(); for (List<?> idAndValue : subjectAltNames) { if (idAndValue != null && idAndValue.size() == 2) { if (idAndValue.get(0).equals(field)) { Object value = idAndValue.get(1); if (value != null) values.add(value); } } } return values; } catch (CertificateParsingException e) { return Collections.emptyList(); } }  static X509Certificate decodeCertificate(byte[] certificateBytes); static X509Certificate decodeCertificate(InputStream inputStream); static List<X509Certificate> decodeCertificates(byte[] certificateBytes); static List<X509Certificate> decodeCertificates(InputStream inputStream); static PKCS10CertificationRequest generateCsr(
        KeyPair keyPair,
        X509Certificate certificate
    ); static PKCS10CertificationRequest generateCsr(
        KeyPair keyPair,
        String subjectName,
        String sanUri,
        List<String> sanDnsNames,
        List<String> sanIpAddresses,
        String signatureAlgorithm
    ); static PKCS10CertificationRequest generateCsr(
        KeyPair keyPair,
        X500Name subject,
        String sanUri,
        List<String> sanDnsNames,
        List<String> sanIpAddresses,
        String signatureAlgorithm
    ); static String getCsrPem(PKCS10CertificationRequest csr); static List<Object> getSubjectAltNameField(X509Certificate certificate, int field); static Optional<String> getSanUri(X509Certificate certificate); static List<String> getSanDnsNames(X509Certificate certificate); static List<String> getSanIpAddresses(X509Certificate certificate); static ByteString thumbprint(X509Certificate certificate); static ByteString getCertificateChainBytes(List<X509Certificate> certificateChain); static final int SUBJECT_ALT_NAME_DNS_NAME; static final int SUBJECT_ALT_NAME_IP_ADDRESS; static final int SUBJECT_ALT_NAME_URI; }
@Test public void testGetSubjectAltNameField() { Object uri = CertificateUtil .getSubjectAltNameField(certificate, CertificateUtil.SUBJECT_ALT_NAME_URI) .get(0); Object dnsName = CertificateUtil .getSubjectAltNameField(certificate, CertificateUtil.SUBJECT_ALT_NAME_DNS_NAME) .get(0); Object ipAddress = CertificateUtil .getSubjectAltNameField(certificate, CertificateUtil.SUBJECT_ALT_NAME_IP_ADDRESS) .get(0); assertEquals(uri, "urn:eclipse:milo:test"); assertEquals(dnsName, "localhost"); assertEquals(ipAddress, "127.0.0.1"); }