/** * Gets a list of all subject names defined for the given certificate. The list includes the first common name (CN) * specified in the subject distinguished name (if defined) and all subject alternative names. * * @param cert X.509 certificate to examine. * * @return List of subject names. * * @throws EncodingException on cert field extraction. */ public static List<String> subjectNames(final X509Certificate cert) throws EncodingException { final List<String> names = new ArrayList<>(); final String cn = subjectCN(cert); if (cn != null) { names.add(cn); } final GeneralNames altNames = subjectAltNames(cert); if (altNames == null) { return names; } for (GeneralName name : altNames.getNames()) { names.add(name.getName().toString()); } return names; }
/** * Gets a list of subject names defined for the given certificate. The list includes the first common name (CN) * specified in the subject distinguished name (if defined) and all subject alternative names of the given type. * * @param cert X.509 certificate to examine. * @param types One or more subject alternative name types to fetch. * * @return List of subject names. * * @throws EncodingException on cert field extraction. */ public static List<String> subjectNames(final X509Certificate cert, final GeneralNameType... types) throws EncodingException { final List<String> names = new ArrayList<>(); final String cn = subjectCN(cert); if (cn != null) { names.add(cn); } final GeneralNames altNames = subjectAltNames(cert, types); if (altNames == null) { return names; } for (GeneralName name : altNames.getNames()) { names.add(name.getName().toString()); } return names; }
/** * Gets all subject alternative names of the given type(s) on the given cert. * * @param cert X.509 certificate to examine. * @param types One or more subject alternative name types to fetch. * * @return List of subject alternative names of the matching type(s) or null if none found. * * @throws EncodingException on cert field extraction. */ public static GeneralNames subjectAltNames(final X509Certificate cert, final GeneralNameType... types) throws EncodingException { final GeneralNamesBuilder builder = new GeneralNamesBuilder(); final GeneralNames altNames = subjectAltNames(cert); if (altNames != null) { for (GeneralName name : altNames.getNames()) { for (GeneralNameType type : types) { if (type.ordinal() == name.getTagNo()) { builder.addName(name); } } } } final GeneralNames names = builder.build(); if (names.getNames().length == 0) { return null; } return names; }