List of usage examples for org.bouncycastle.asn1.x509 Extension subjectDirectoryAttributes
ASN1ObjectIdentifier subjectDirectoryAttributes
To view the source code for org.bouncycastle.asn1.x509 Extension subjectDirectoryAttributes.
Click Source Link
From source file:org.xipki.pki.ca.qa.ExtensionsChecker.java
License:Open Source License
private void checkExtensionSubjectDirAttrs(final StringBuilder failureMsg, final byte[] extensionValue, final Extensions requestedExtensions, final ExtensionControl extControl) { SubjectDirectoryAttributesControl conf = certProfile.getSubjectDirAttrsControl(); if (conf == null) { failureMsg.append("extension is present but not expected; "); return;/* w ww .ja v a2s . c o m*/ } ASN1Encodable extInRequest = null; if (requestedExtensions != null) { extInRequest = requestedExtensions.getExtensionParsedValue(Extension.subjectDirectoryAttributes); } if (extInRequest == null) { failureMsg.append("extension is present but not expected; "); return; } SubjectDirectoryAttributes requested = SubjectDirectoryAttributes.getInstance(extInRequest); Vector<?> reqSubDirAttrs = requested.getAttributes(); ASN1GeneralizedTime expDateOfBirth = null; String expPlaceOfBirth = null; String expGender = null; Set<String> expCountryOfCitizenshipList = new HashSet<>(); Set<String> expCountryOfResidenceList = new HashSet<>(); Map<ASN1ObjectIdentifier, Set<ASN1Encodable>> expOtherAttrs = new HashMap<>(); final int expN = reqSubDirAttrs.size(); for (int i = 0; i < expN; i++) { Attribute attr = Attribute.getInstance(reqSubDirAttrs.get(i)); ASN1ObjectIdentifier attrType = attr.getAttrType(); ASN1Encodable attrVal = attr.getAttributeValues()[0]; if (ObjectIdentifiers.DN_DATE_OF_BIRTH.equals(attrType)) { expDateOfBirth = ASN1GeneralizedTime.getInstance(attrVal); } else if (ObjectIdentifiers.DN_PLACE_OF_BIRTH.equals(attrType)) { expPlaceOfBirth = DirectoryString.getInstance(attrVal).getString(); } else if (ObjectIdentifiers.DN_GENDER.equals(attrType)) { expGender = DERPrintableString.getInstance(attrVal).getString(); } else if (ObjectIdentifiers.DN_COUNTRY_OF_CITIZENSHIP.equals(attrType)) { String country = DERPrintableString.getInstance(attrVal).getString(); expCountryOfCitizenshipList.add(country); } else if (ObjectIdentifiers.DN_COUNTRY_OF_RESIDENCE.equals(attrType)) { String country = DERPrintableString.getInstance(attrVal).getString(); expCountryOfResidenceList.add(country); } else { Set<ASN1Encodable> otherAttrVals = expOtherAttrs.get(attrType); if (otherAttrVals == null) { otherAttrVals = new HashSet<>(); expOtherAttrs.put(attrType, otherAttrVals); } otherAttrVals.add(attrVal); } } SubjectDirectoryAttributes ext = SubjectDirectoryAttributes.getInstance(extensionValue); Vector<?> subDirAttrs = ext.getAttributes(); ASN1GeneralizedTime dateOfBirth = null; String placeOfBirth = null; String gender = null; Set<String> countryOfCitizenshipList = new HashSet<>(); Set<String> countryOfResidenceList = new HashSet<>(); Map<ASN1ObjectIdentifier, Set<ASN1Encodable>> otherAttrs = new HashMap<>(); List<ASN1ObjectIdentifier> attrTypes = new LinkedList<>(conf.getTypes()); final int n = subDirAttrs.size(); for (int i = 0; i < n; i++) { Attribute attr = Attribute.getInstance(subDirAttrs.get(i)); ASN1ObjectIdentifier attrType = attr.getAttrType(); if (!attrTypes.contains(attrType)) { failureMsg.append("attribute of type " + attrType.getId() + " is present but not expected; "); continue; } ASN1Encodable[] attrs = attr.getAttributeValues(); if (attrs.length != 1) { failureMsg.append("attribute of type " + attrType.getId() + " does not single-value value: " + attrs.length + "; "); continue; } ASN1Encodable attrVal = attrs[0]; if (ObjectIdentifiers.DN_DATE_OF_BIRTH.equals(attrType)) { dateOfBirth = ASN1GeneralizedTime.getInstance(attrVal); } else if (ObjectIdentifiers.DN_PLACE_OF_BIRTH.equals(attrType)) { placeOfBirth = DirectoryString.getInstance(attrVal).getString(); } else if (ObjectIdentifiers.DN_GENDER.equals(attrType)) { gender = DERPrintableString.getInstance(attrVal).getString(); } else if (ObjectIdentifiers.DN_COUNTRY_OF_CITIZENSHIP.equals(attrType)) { String country = DERPrintableString.getInstance(attrVal).getString(); countryOfCitizenshipList.add(country); } else if (ObjectIdentifiers.DN_COUNTRY_OF_RESIDENCE.equals(attrType)) { String country = DERPrintableString.getInstance(attrVal).getString(); countryOfResidenceList.add(country); } else { Set<ASN1Encodable> otherAttrVals = otherAttrs.get(attrType); if (otherAttrVals == null) { otherAttrVals = new HashSet<>(); otherAttrs.put(attrType, otherAttrVals); } otherAttrVals.add(attrVal); } } if (dateOfBirth != null) { attrTypes.remove(ObjectIdentifiers.DN_DATE_OF_BIRTH); } if (placeOfBirth != null) { attrTypes.remove(ObjectIdentifiers.DN_PLACE_OF_BIRTH); } if (gender != null) { attrTypes.remove(ObjectIdentifiers.DN_GENDER); } if (!countryOfCitizenshipList.isEmpty()) { attrTypes.remove(ObjectIdentifiers.DN_COUNTRY_OF_CITIZENSHIP); } if (!countryOfResidenceList.isEmpty()) { attrTypes.remove(ObjectIdentifiers.DN_COUNTRY_OF_RESIDENCE); } attrTypes.removeAll(otherAttrs.keySet()); if (!attrTypes.isEmpty()) { List<String> attrTypeTexts = new LinkedList<>(); for (ASN1ObjectIdentifier oid : attrTypes) { attrTypeTexts.add(oid.getId()); } failureMsg.append("required attributes of types " + attrTypeTexts + " are not present; "); } if (dateOfBirth != null) { String timeStirng = dateOfBirth.getTimeString(); if (!SubjectDnSpec.PATTERN_DATE_OF_BIRTH.matcher(timeStirng).matches()) { failureMsg.append("invalid dateOfBirth: " + timeStirng + "; "); } String exp = (expDateOfBirth == null) ? null : expDateOfBirth.getTimeString(); if (!timeStirng.equalsIgnoreCase(exp)) { addViolation(failureMsg, "dateOfBirth", timeStirng, exp); } } if (gender != null) { if (!(gender.equalsIgnoreCase("F") || gender.equalsIgnoreCase("M"))) { failureMsg.append("invalid gender: " + gender + "; "); } if (!gender.equalsIgnoreCase(expGender)) { addViolation(failureMsg, "gender", gender, expGender); } } if (placeOfBirth != null) { if (!placeOfBirth.equals(expPlaceOfBirth)) { addViolation(failureMsg, "placeOfBirth", placeOfBirth, expPlaceOfBirth); } } if (!countryOfCitizenshipList.isEmpty()) { Set<String> diffs = strInBnotInA(expCountryOfCitizenshipList, countryOfCitizenshipList); if (CollectionUtil.isNonEmpty(diffs)) { failureMsg.append("countryOfCitizenship ").append(diffs.toString()); failureMsg.append(" are present but not expected; "); } diffs = strInBnotInA(countryOfCitizenshipList, expCountryOfCitizenshipList); if (CollectionUtil.isNonEmpty(diffs)) { failureMsg.append("countryOfCitizenship ").append(diffs.toString()); failureMsg.append(" are absent but are required; "); } } if (!countryOfResidenceList.isEmpty()) { Set<String> diffs = strInBnotInA(expCountryOfResidenceList, countryOfResidenceList); if (CollectionUtil.isNonEmpty(diffs)) { failureMsg.append("countryOfResidence ").append(diffs.toString()); failureMsg.append(" are present but not expected; "); } diffs = strInBnotInA(countryOfResidenceList, expCountryOfResidenceList); if (CollectionUtil.isNonEmpty(diffs)) { failureMsg.append("countryOfResidence ").append(diffs.toString()); failureMsg.append(" are absent but are required; "); } } if (!otherAttrs.isEmpty()) { for (ASN1ObjectIdentifier attrType : otherAttrs.keySet()) { Set<ASN1Encodable> expAttrValues = expOtherAttrs.get(attrType); if (expAttrValues == null) { failureMsg.append("attribute of type " + attrType.getId() + " is present but not requested; "); continue; } Set<ASN1Encodable> attrValues = otherAttrs.get(attrType); if (!attrValues.equals(expAttrValues)) { failureMsg .append("attribute of type " + attrType.getId() + " differs from the requested one; "); continue; } } } }