use of org.xipki.ca.api.BadFormatException in project xipki by xipki.
the class X509Ca method adaptGrantedSubejct.
// method generateCertificate0
private void adaptGrantedSubejct(GrantedCertTemplate gct) throws OperationException {
boolean duplicateSubjectPermitted = caInfo.isDuplicateSubjectPermitted();
if (duplicateSubjectPermitted && !gct.certprofile.isDuplicateSubjectPermitted()) {
duplicateSubjectPermitted = false;
}
if (duplicateSubjectPermitted) {
return;
}
long fpSubject = X509Util.fpCanonicalizedName(gct.grantedSubject);
String grantedSubjectText = X509Util.getRfc4519Name(gct.grantedSubject);
final boolean incSerial = gct.certprofile.incSerialNumberIfSubjectExists();
final boolean certIssued = certstore.isCertForSubjectIssued(caIdent, fpSubject);
if (certIssued && !incSerial) {
throw new OperationException(ErrorCode.ALREADY_ISSUED, "certificate for the given subject " + grantedSubjectText + " already issued");
}
if (!certIssued) {
return;
}
X500Name subject = gct.grantedSubject;
String latestSn;
try {
Object[] objs = incSerialNumber(gct.certprofile, subject, null);
latestSn = certstore.getLatestSerialNumber((X500Name) objs[0]);
} catch (BadFormatException ex) {
throw new OperationException(ErrorCode.SYSTEM_FAILURE, ex);
}
boolean foundUniqueSubject = false;
// maximal 100 tries
for (int i = 0; i < 100; i++) {
try {
Object[] objs = incSerialNumber(gct.certprofile, subject, latestSn);
subject = (X500Name) objs[0];
if (CompareUtil.equalsObject(latestSn, objs[1])) {
break;
}
latestSn = (String) objs[1];
} catch (BadFormatException ex) {
throw new OperationException(ErrorCode.SYSTEM_FAILURE, ex);
}
foundUniqueSubject = !certstore.isCertForSubjectIssued(caIdent, X509Util.fpCanonicalizedName(subject));
if (foundUniqueSubject) {
break;
}
}
if (!foundUniqueSubject) {
throw new OperationException(ErrorCode.ALREADY_ISSUED, "certificate for the given subject " + grantedSubjectText + " and profile " + gct.certprofile.getIdent() + " already issued, and could not create new unique serial number");
}
gct.setGrantedSubject(subject);
}
Aggregations