java.security.cert.X509Certificate.getSubjectAlternativeNames()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中java.security.cert.X509Certificate.getSubjectAlternativeNames()方法的一些代码示例,展示了X509Certificate.getSubjectAlternativeNames()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。X509Certificate.getSubjectAlternativeNames()方法的具体详情如下:
包路径:java.security.cert.X509Certificate
类名称:X509Certificate
方法名:getSubjectAlternativeNames

X509Certificate.getSubjectAlternativeNames介绍

[英]Returns a read-only list of the subject alternative names from the SubjectAltName extension.

The ASN.1 definition of SubjectAltName:

SubjectAltName ::= GeneralNames 
GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName 
GeneralName ::= CHOICE { 
otherName                       [0]     AnotherName, 
rfc822Name                      [1]     IA5String, 
dNSName                         [2]     IA5String, 
x400Address                     [3]     ORAddress, 
directoryName                   [4]     Name, 
ediPartyName                    [5]     EDIPartyName, 
uniformResourceIdentifier       [6]     IA5String, 
iPAddress                       [7]     OCTET STRING, 
registeredID                    [8]     OBJECT IDENTIFIER }

[中]从SubjectAltName扩展名返回主题备选名称的只读列表。
ASN。1主题名称的定义:

SubjectAltName ::= GeneralNames 
GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName 
GeneralName ::= CHOICE { 
otherName                       [0]     AnotherName, 
rfc822Name                      [1]     IA5String, 
dNSName                         [2]     IA5String, 
x400Address                     [3]     ORAddress, 
directoryName                   [4]     Name, 
ediPartyName                    [5]     EDIPartyName, 
uniformResourceIdentifier       [6]     IA5String, 
iPAddress                       [7]     OCTET STRING, 
registeredID                    [8]     OBJECT IDENTIFIER }

代码示例

代码示例来源:origin: apache/kafka

CertificateEntries(X509Certificate cert) throws GeneralSecurityException {
  this.subjectPrincipal = cert.getSubjectX500Principal();
  Collection<List<?>> altNames = cert.getSubjectAlternativeNames();
  // use a set for comparison
  this.subjectAltNames = altNames != null ? new HashSet<>(altNames) : Collections.emptySet();
}

代码示例来源:origin: square/okhttp

private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
 List<String> result = new ArrayList<>();
 try {
  Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
  if (subjectAltNames == null) {
   return Collections.emptyList();
  }
  for (Object subjectAltName : subjectAltNames) {
   List<?> entry = (List<?>) subjectAltName;
   if (entry == null || entry.size() < 2) {
    continue;
   }
   Integer altNameType = (Integer) entry.get(0);
   if (altNameType == null) {
    continue;
   }
   if (altNameType == type) {
    String altName = (String) entry.get(1);
    if (altName != null) {
     result.add(altName);
    }
   }
  }
  return result;
 } catch (CertificateParsingException e) {
  return Collections.emptyList();
 }
}

代码示例来源:origin: redisson/redisson

@Override
public Collection<List<?>> getSubjectAlternativeNames() throws CertificateParsingException {
  return unwrap().getSubjectAlternativeNames();
}

代码示例来源:origin: redisson/redisson

@Override
public Collection<List<?>> getIssuerAlternativeNames() throws CertificateParsingException {
  return unwrap().getSubjectAlternativeNames();
}

代码示例来源:origin: prestodb/presto

private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
 List<String> result = new ArrayList<>();
 try {
  Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
  if (subjectAltNames == null) {
   return Collections.emptyList();
  }
  for (Object subjectAltName : subjectAltNames) {
   List<?> entry = (List<?>) subjectAltName;
   if (entry == null || entry.size() < 2) {
    continue;
   }
   Integer altNameType = (Integer) entry.get(0);
   if (altNameType == null) {
    continue;
   }
   if (altNameType == type) {
    String altName = (String) entry.get(1);
    if (altName != null) {
     result.add(altName);
    }
   }
  }
  return result;
 } catch (CertificateParsingException e) {
  return Collections.emptyList();
 }
}

代码示例来源:origin: apache/zookeeper

private static List<SubjectName> getSubjectAltNames(final X509Certificate cert) {
  try {
    final Collection<List<?>> entries = cert.getSubjectAlternativeNames();
    if (entries == null) {
      return Collections.emptyList();
    }
    final List<SubjectName> result = new ArrayList<SubjectName>();
    for (List<?> entry: entries) {
      final Integer type = entry.size() >= 2 ? (Integer) entry.get(0) : null;
      if (type != null) {
        final String s = (String) entry.get(1);
        result.add(new SubjectName(s, type));
      }
    }
    return result;
  } catch (final CertificateParsingException ignore) {
    return Collections.emptyList();
  }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
 List<String> result = new ArrayList<>();
 try {
  Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
  if (subjectAltNames == null) {
   return Collections.emptyList();
  }
  for (Object subjectAltName : subjectAltNames) {
   List<?> entry = (List<?>) subjectAltName;
   if (entry == null || entry.size() < 2) {
    continue;
   }
   Integer altNameType = (Integer) entry.get(0);
   if (altNameType == null) {
    continue;
   }
   if (altNameType == type) {
    String altName = (String) entry.get(1);
    if (altName != null) {
     result.add(altName);
    }
   }
  }
  return result;
 } catch (CertificateParsingException e) {
  return Collections.emptyList();
 }
}

代码示例来源:origin: apache/nifi

/**
 * Returns a list of subject alternative names. Any name that is represented as a String by X509Certificate.getSubjectAlternativeNames() is converted to lowercase and returned.
 *
 * @param certificate a certificate
 * @return a list of subject alternative names; list is never null
 * @throws CertificateParsingException if parsing the certificate failed
 */
public static List<String> getSubjectAlternativeNames(final X509Certificate certificate) throws CertificateParsingException {
  final Collection<List<?>> altNames = certificate.getSubjectAlternativeNames();
  if (altNames == null) {
    return new ArrayList<>();
  }
  final List<String> result = new ArrayList<>();
  for (final List<?> generalName : altNames) {
    /**
     * generalName has the name type as the first element a String or byte array for the second element. We return any general names that are String types.
     *
     * We don't inspect the numeric name type because some certificates incorrectly put IPs and DNS names under the wrong name types.
     */
    final Object value = generalName.get(1);
    if (value instanceof String) {
      result.add(((String) value).toLowerCase());
    }
  }
  return result;
}

代码示例来源:origin: robovm/robovm

private List<String> getSubjectAltNames(X509Certificate certificate, int type) {
  List<String> result = new ArrayList<String>();
  try {
    Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
    if (subjectAltNames == null) {
      return Collections.emptyList();
    }
    for (Object subjectAltName : subjectAltNames) {
      List<?> entry = (List<?>) subjectAltName;
      if (entry == null || entry.size() < 2) {
        continue;
      }
      Integer altNameType = (Integer) entry.get(0);
      if (altNameType == null) {
        continue;
      }
      if (altNameType == type) {
        String altName = (String) entry.get(1);
        if (altName != null) {
          result.add(altName);
        }
      }
    }
    return result;
  } catch (CertificateParsingException e) {
    return Collections.emptyList();
  }
}

代码示例来源:origin: TakahikoKawasaki/nv-websocket-client

private static List<String> getSubjectAltNames(X509Certificate certificate, int type) {
 List<String> result = new ArrayList<String>();
 try {
  Collection<?> subjectAltNames = certificate.getSubjectAlternativeNames();
  if (subjectAltNames == null) {
   return Collections.emptyList();
  }
  for (Object subjectAltName : subjectAltNames) {
   List<?> entry = (List<?>) subjectAltName;
   if (entry == null || entry.size() < 2) {
    continue;
   }
   Integer altNameType = (Integer) entry.get(0);
   if (altNameType == null) {
    continue;
   }
   if (altNameType == type) {
    String altName = (String) entry.get(1);
    if (altName != null) {
     result.add(altName);
    }
   }
  }
  return result;
 } catch (CertificateParsingException e) {
  return Collections.emptyList();
 }
}

代码示例来源:origin: robovm/robovm

Collection<List<?>> c = null;
try {
  c = cert.getSubjectAlternativeNames();

代码示例来源:origin: eclipse-vertx/vert.x

Collection<List<?>> ans = x509Cert.getSubjectAlternativeNames();
List<String> domains = new ArrayList<>();
if (ans != null) {

代码示例来源:origin: igniterealtime/Smack

Collection<List<?>> subjectAlternativeNames = cert.getSubjectAlternativeNames();
if (subjectAlternativeNames == null) {
  throw new CertificateException("No subject alternative names present");

代码示例来源:origin: org.apache.hadoop/hadoop-common

Collection<List<?>> c = null;
try {
  c = cert.getSubjectAlternativeNames();

代码示例来源:origin: igniterealtime/Openfire

try
  Collection<List<?>> altNames = certificate.getSubjectAlternativeNames();

代码示例来源:origin: wildfly/wildfly

public static boolean matchGeneralNames(List<GeneralName> generalNames, X509Certificate cert) {
  X500Principal certSubjectName = cert.getSubjectX500Principal();
  try {
    if (matchGeneralNames(generalNames, convertToGeneralNames(cert.getSubjectAlternativeNames()))) {
      return true;
    }
  } catch (CertificateParsingException e) {
    // Ignore unless the subject name is empty
    if (certSubjectName == null) {
      throw saslEntity.unableToDetermineSubjectName(e);
    }
  }
  List<GeneralName> certNames;
  if (certSubjectName != null) {
    certNames = new ArrayList<GeneralName>(1);
    certNames.add(new DirectoryName(certSubjectName.getName(X500Principal.CANONICAL)));
    if (matchGeneralNames(generalNames, certNames)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: floragunncom/search-guard

final Collection<List<?>> ianList = peerCerts[0].getSubjectAlternativeNames();
if (ianList != null) {
  final StringBuilder sb = new StringBuilder();

代码示例来源:origin: igniterealtime/Smack

Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
if (subjAltNames != null) {
  List<String> nonMatchingDnsAltnames = new LinkedList<>();

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

public SSLEngine clientSslEngineFor(HttpRequest httpRequest, SSLSession serverSslSession) {
  try {
    X509Certificate upstreamCert = getCertificateFromSession(serverSslSession);
    // TODO store the upstream cert by commonName to review it later
    // A reasons to not use the common name and the alternative names
    // from upstream certificate from serverSslSession to create the
    // dynamic certificate:
    //
    // It's not necessary. The host name is accepted by the browser.
    //
    String commonName = getCommonName(upstreamCert);
    SubjectAlternativeNameHolder san = new SubjectAlternativeNameHolder();
    san.addAll(upstreamCert.getSubjectAlternativeNames());
    LOG.debug("Subject Alternative Names: {}", san);
    return sslEngineSource.createCertForHost(commonName, san);
  } catch (Exception e) {
    throw new FakeCertificateException(
        "Creation dynamic certificate failed", e);
  }
}

代码示例来源:origin: igniterealtime/Openfire

final Collection<List<?>> certSans = cert.getSubjectAlternativeNames();
if ( certSans != null )

相关文章

微信公众号

最新文章

更多