当前位置: 首页>>代码示例>>Java>>正文


Java X509Certificate.getSubjectAlternativeNames方法代码示例

本文整理汇总了Java中java.security.cert.X509Certificate.getSubjectAlternativeNames方法的典型用法代码示例。如果您正苦于以下问题:Java X509Certificate.getSubjectAlternativeNames方法的具体用法?Java X509Certificate.getSubjectAlternativeNames怎么用?Java X509Certificate.getSubjectAlternativeNames使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.cert.X509Certificate的用法示例。


在下文中一共展示了X509Certificate.getSubjectAlternativeNames方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: buildCertAppliesToString

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
public static String buildCertAppliesToString(X509Certificate cert) {
    List<String> elements = new ArrayList<>();
    try {
        Collection<List<?>> altNames = cert.getSubjectAlternativeNames();
        if (altNames != null) {
            for (List<?> altName : altNames) {
                Integer altNameType = (Integer) altName.get(0);
                if (altNameType != 2 && altNameType != 7) // dns or ip
                    continue;
                elements.add((String) altName.get(1));
            }
        }
    } catch (CertificateParsingException ignored) {
    }

    if (elements.size() == 0)
        return "none";
    return TextUtils.join(",", elements.toArray());
}
 
开发者ID:MCMrARM,项目名称:revolution-irc,代码行数:20,代码来源:ServerCertificateManager.java

示例2: resolvePrincipalInternal

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
/**
 * Retrieves Subject Alternative Name UPN extension as a principal id String.
 *
 * @param certificate X.509 certificate credential.
 *
 * @return Resolved principal ID or null if no SAN UPN extension is available in provided certificate.
 *
 * @see AbstractX509PrincipalResolver#resolvePrincipalInternal(java.security.cert.X509Certificate)
 * @see java.security.cert.X509Certificate#getSubjectAlternativeNames()
 */
@Override
protected String resolvePrincipalInternal(final X509Certificate certificate) {
    logger.debug("Resolving principal from Subject Alternative Name UPN for {}", certificate);
    try {
        final Collection<List<?>> subjectAltNames = certificate.getSubjectAlternativeNames();
        if (subjectAltNames != null) {
            for (final List<?> sanItem : subjectAltNames) {
                final ASN1Sequence seq = getAltnameSequence(sanItem);
                final String upnString = getUPNStringFromSequence(seq);
                if (upnString != null) {
                    return upnString;
                }
            }
        }
    } catch (final CertificateParsingException e) {
        logger.error("Error is encountered while trying to retrieve subject alternative names collection from certificate", e);
        logger.debug("Returning null principal id...");
        return null;
    }
    logger.debug("Returning null principal id...");
    return null;
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:33,代码来源:X509SubjectAlternativeNameUPNPrincipalResolver.java

示例3: getSubjectAlternativeNames

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:apache,项目名称:nifi-registry,代码行数:31,代码来源:CertificateUtils.java

示例4: getSubjectAltName

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
private static Object getSubjectAltName(X509Certificate cert, int type) {
    Collection<List<?>> subjectAltNames;

    try {
        subjectAltNames = cert.getSubjectAlternativeNames();
    } catch (CertificateParsingException cpe) {
        if (debug != null && Debug.isOn("handshake")) {
            System.out.println(
                    "Attempt to obtain subjectAltNames extension failed!");
        }
        return null;
    }

    if (subjectAltNames != null) {
        for (List<?> subjectAltName : subjectAltNames) {
            int subjectAltNameType = (Integer)subjectAltName.get(0);
            if (subjectAltNameType == type) {
                return subjectAltName.get(1);
            }
        }
    }

    return null;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:25,代码来源:ClientHandshaker.java

示例5: getSubjectAltNames

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
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();
        }
        Iterator it = subjectAltNames.iterator();
        while (it.hasNext()) {
            List<?> entry = (List) it.next();
            if (entry != null && entry.size() >= 2) {
                Integer altNameType = (Integer) entry.get(0);
                if (altNameType != null && altNameType.intValue() == type) {
                    String altName = (String) entry.get(1);
                    if (altName != null) {
                        result.add(altName);
                    }
                }
            }
        }
        return result;
    } catch (CertificateParsingException e) {
        return Collections.emptyList();
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:26,代码来源:OkHostnameVerifier.java

示例6: getSubjectAltNames

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
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();
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:29,代码来源:OkHostnameVerifier.java

示例7: getCN

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
public static Set<String> getCN(X509Certificate cert) {
  Set<String> names = new HashSet<>();

  // 读取CN
  String subjectDN = cert.getSubjectX500Principal().getName();
  String[] pairs = subjectDN.split(",");
  for (String p : pairs) {
    String[] kv = p.split("=");
    if (kv.length == 2 && kv[0].equals("CN")) {
      names.add(kv[1]);
    }
  }

  // 读取SubjectAlternativeNames
  try {
    Collection<List<?>> collection = cert.getSubjectAlternativeNames();
    if (collection != null) {
      for (List<?> list : collection) {
        if (list.size() == 2) {
          Object key = list.get(0);
          Object value = list.get(1);
          if (key instanceof Integer && value instanceof String) {
            int intKey = ((Integer) key).intValue();
            String strValue = (String) value;
            if (intKey == SUBALTNAME_DNSNAME || intKey == SUBALTNAME_IPADDRESS) {
              names.add(strValue);
            }
          }
        }
      }
    }
  } catch (CertificateParsingException e) {
    throw new IllegalArgumentException("can not read AlternativeNames.");
  }

  return names;
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:38,代码来源:CertificateUtil.java

示例8: getDNSSubjectAlts

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
/**
 * Extract all alternative names from a certificate.
 * @param cert
 * @return
 */
private static String[] getDNSSubjectAlts(X509Certificate cert) {
	LinkedList subjectAltList = new LinkedList();
	Collection c = null;
	try {
		c = cert.getSubjectAlternativeNames();
	} catch (CertificateParsingException cpe) {
		// Should probably log.debug() this?
		cpe.printStackTrace();
	}
	if (c != null) {
		Iterator it = c.iterator();
		while (it.hasNext()) {
			List list = (List) it.next();
			int type = ((Integer) list.get(0)).intValue();
			// If type is 2, then we've got a dNSName
			if (type == 2) {
				String s = (String) list.get(1);
				subjectAltList.add(s);
			}
		}
	}
	if (!subjectAltList.isEmpty()) {
		String[] subjectAlts = new String[subjectAltList.size()];
		subjectAltList.toArray(subjectAlts);
		return subjectAlts;
	} else {
		return new String[0];
	}
        
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:36,代码来源:SSLProtocolSocketFactory.java

示例9: getDNSSubjectAlts

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
/**
 * Extracts the array of SubjectAlt DNS names from an X509Certificate.
 * Returns null if there aren't any.
 * <p/>
 * Note:  Java doesn't appear able to extract international characters
 * from the SubjectAlts.  It can only extract international characters
 * from the CN field.
 * <p/>
 * (Or maybe the version of OpenSSL I'm using to test isn't storing the
 * international characters correctly in the SubjectAlts?).
 *
 * @param cert X509Certificate
 * @return Array of SubjectALT DNS names stored in the certificate.
 */
public static String[] getDNSSubjectAlts(X509Certificate cert) {
    final List<String> subjectAltList = new LinkedList<String>();
    Collection<List<?>> c = null;
    try {
        c = cert.getSubjectAlternativeNames();
    }
    catch (CertificateParsingException cpe) {
        // Should probably log.debug() this?
        cpe.printStackTrace();
    }
    if (c != null) {
        Iterator<List<?>> it = c.iterator();
        while (it.hasNext()) {
            List<?> list = it.next();
            int type = ((Integer) list.get(0)).intValue();
            // If type is 2, then we've got a dNSName
            if (type == 2) {
                String s = (String) list.get(1);
                subjectAltList.add(s);
            }
        }
    }
    if (!subjectAltList.isEmpty()) {
        String[] subjectAlts = new String[subjectAltList.size()];
        subjectAltList.toArray(subjectAlts);
        return subjectAlts;
    } else {
        return null;
    }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:45,代码来源:SSLHostnameVerifier.java

示例10: getDisplayNameFromCertificate

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
/**
 * Returns either a string that "sums up" the certificate for humans, in a similar manner to what you might see
 * in a web browser, or null if one cannot be extracted. This will typically be the common name (CN) field, but
 * can also be the org (O) field, org+location+country if withLocation is set, or the email
 * address for S/MIME certificates.
 */
@Nullable
public static String getDisplayNameFromCertificate(@Nonnull X509Certificate certificate, boolean withLocation) throws CertificateParsingException {
    X500Name name = new X500Name(certificate.getSubjectX500Principal().getName());
    String commonName = null, org = null, location = null, country = null;
    for (RDN rdn : name.getRDNs()) {
        AttributeTypeAndValue pair = rdn.getFirst();
        String val = ((ASN1String) pair.getValue()).getString();
        ASN1ObjectIdentifier type = pair.getType();
        if (type.equals(RFC4519Style.cn))
            commonName = val;
        else if (type.equals(RFC4519Style.o))
            org = val;
        else if (type.equals(RFC4519Style.l))
            location = val;
        else if (type.equals(RFC4519Style.c))
            country = val;
    }
    final Collection<List<?>> subjectAlternativeNames = certificate.getSubjectAlternativeNames();
    String altName = null;
    if (subjectAlternativeNames != null)
        for (final List<?> subjectAlternativeName : subjectAlternativeNames)
            if ((Integer) subjectAlternativeName.get(0) == 1) // rfc822name
                altName = (String) subjectAlternativeName.get(1);

    if (org != null) {
        return withLocation ? Joiner.on(", ").skipNulls().join(org, location, country) : org;
    } else if (commonName != null) {
        return commonName;
    } else {
        return altName;
    }
}
 
开发者ID:guodroid,项目名称:okwallet,代码行数:39,代码来源:X509Utils.java

示例11: getSubjectAlts

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
/**
 * Extracts the array of SubjectAlt DNS or IP names from an X509Certificate.
 * Returns null if there aren't any.
 *
 * @param cert X509Certificate
 * @param hostname
 * @return Array of SubjectALT DNS or IP names stored in the certificate.
 */
private static String[] getSubjectAlts(
        final X509Certificate cert, final String hostname) {
    int subjectType;
    if (isIPAddress(hostname)) {
        subjectType = 7;
    } else {
        subjectType = 2;
    }

    LinkedList<String> subjectAltList = new LinkedList<String>();
    Collection<List<?>> c = null;
    try {
        c = cert.getSubjectAlternativeNames();
    }
    catch(CertificateParsingException cpe) {
        Logger.getLogger(AbstractVerifier.class.getName())
                .log(Level.FINE, "Error parsing certificate.", cpe);
    }
    if(c != null) {
        for (List<?> aC : c) {
            List<?> list = aC;
            int type = ((Integer) list.get(0)).intValue();
            if (type == subjectType) {
                String s = (String) list.get(1);
                subjectAltList.add(s);
            }
        }
    }
    if(!subjectAltList.isEmpty()) {
        String[] subjectAlts = new String[subjectAltList.size()];
        subjectAltList.toArray(subjectAlts);
        return subjectAlts;
    } else {
        return null;
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:45,代码来源:AbstractVerifier.java

示例12: getSubjectAltNames

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
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();
  }
}
 
开发者ID:aabognah,项目名称:LoRaWAN-Smart-Parking,代码行数:29,代码来源:OkHostnameVerifier.java

示例13: getSubjectAltNames

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
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();
  }
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:29,代码来源:OkHostnameVerifier.java

示例14: main

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    ByteArrayInputStream is = new ByteArrayInputStream(certStr.getBytes());
    X509Certificate cert = (X509Certificate) cf.generateCertificate(is);

    if (cert.getSubjectAlternativeNames() == null) {
        throw new Exception("Failed to parse Subject Alternative Name");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:X400Address.java

示例15: printCertificateDetails

import java.security.cert.X509Certificate; //导入方法依赖的package包/类
private void printCertificateDetails() throws CertificateParsingException {

      try {
        System.err.println("* Cipher Suite       : " + cipherSuite);

        for (Certificate cert : serverCertificates) {
          System.err.println("* Cert Type          : " + cert.getType());

          if (cert instanceof X509Certificate) {
            X509Certificate x509Cert = (X509Certificate) cert;

            //                  *      Type          : "
            System.err.println("*      Issuer        : " + x509Cert.getIssuerDN());
            System.err.println("*      Subject       : " + x509Cert.getSubjectDN());

            //                  *      Type      : "
            System.err.println("*      Issuer ID     : " + x509Cert.getIssuerUniqueID());
            System.err.println("*      Sig Algorithm : " + x509Cert.getSigAlgName());
            System.err.println("*      Basic Const   : " + x509Cert.getBasicConstraints());
            System.err.println("*      Ext Key Usage : " + x509Cert.getExtendedKeyUsage());
            System.err.println("*      Not Before    : " + x509Cert.getNotBefore());
            System.err.println("*      Not After     : " + x509Cert.getNotAfter());
            System.err.println("*      Subject ID    : " + x509Cert.getSubjectUniqueID());

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

            if (altNames != null) {
              for (List<?> nameList : altNames) {
                for (Object name : nameList) {
                  System.err.println("*      Alt Name     : " + name);
                }
              }
            }
          }

          System.err.println("*      Hash Code     : " + cert.hashCode());
          System.err.println("*      PubKey Algo   : " + cert.getPublicKey().getAlgorithm());
          System.err.println("*      PubKey Format : " + cert.getPublicKey().getFormat());
          System.err.println("\n");
        }
      } catch (IllegalStateException ignored) {}
    }
 
开发者ID:symphonyoss,项目名称:JCurl,代码行数:43,代码来源:JCurl.java


注:本文中的java.security.cert.X509Certificate.getSubjectAlternativeNames方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。