use of org.bouncycastle.asn1.DERPrintableString in project xipki by xipki.
the class X509Ca method incSerialNumber.
// method removeEmptyRdns
private static Object[] incSerialNumber(IdentifiedX509Certprofile profile, X500Name origName, String latestSn) throws BadFormatException {
RDN[] rdns = origName.getRDNs();
int commonNameIndex = -1;
int serialNumberIndex = -1;
for (int i = 0; i < rdns.length; i++) {
RDN rdn = rdns[i];
ASN1ObjectIdentifier type = rdn.getFirst().getType();
if (ObjectIdentifiers.DN_CN.equals(type)) {
commonNameIndex = i;
} else if (ObjectIdentifiers.DN_SERIALNUMBER.equals(type)) {
serialNumberIndex = i;
}
}
String newSerialNumber = profile.incSerialNumber(latestSn);
RDN serialNumberRdn = new RDN(ObjectIdentifiers.DN_SERIALNUMBER, new DERPrintableString(newSerialNumber));
X500Name newName;
if (serialNumberIndex != -1) {
rdns[serialNumberIndex] = serialNumberRdn;
newName = new X500Name(rdns);
} else {
List<RDN> newRdns = new ArrayList<>(rdns.length + 1);
if (commonNameIndex == -1) {
newRdns.add(serialNumberRdn);
}
for (int i = 0; i < rdns.length; i++) {
newRdns.add(rdns[i]);
if (i == commonNameIndex) {
newRdns.add(serialNumberRdn);
}
}
newName = new X500Name(newRdns.toArray(new RDN[0]));
}
return new Object[] { newName, newSerialNumber };
}
use of org.bouncycastle.asn1.DERPrintableString in project xipki by xipki.
the class ExtensionsChecker method checkDirectoryString.
private void checkDirectoryString(ASN1ObjectIdentifier extType, QaDirectoryString conf, StringBuilder failureMsg, byte[] extensionValue, Extensions requestedExtensions, ExtensionControl extControl) {
if (conf == null) {
byte[] expected = getExpectedExtValue(extType, requestedExtensions, extControl);
if (!Arrays.equals(expected, extensionValue)) {
addViolation(failureMsg, "extension values", hex(extensionValue), (expected == null) ? "not present" : hex(expected));
}
return;
}
ASN1Primitive asn1;
try {
asn1 = ASN1Primitive.fromByteArray(extensionValue);
} catch (IOException ex) {
failureMsg.append("invalid syntax of extension value; ");
return;
}
boolean correctStringType;
switch(conf.getType()) {
case bmpString:
correctStringType = (asn1 instanceof DERBMPString);
break;
case printableString:
correctStringType = (asn1 instanceof DERPrintableString);
break;
case teletexString:
correctStringType = (asn1 instanceof DERT61String);
break;
case utf8String:
correctStringType = (asn1 instanceof DERUTF8String);
break;
default:
throw new RuntimeException("should not reach here, unknown DirectoryStringType " + conf.getType());
}
if (!correctStringType) {
failureMsg.append("extension value is not of type DirectoryString.").append(conf.getText()).append("; ");
return;
}
String extTextValue = ((ASN1String) asn1).getString();
if (!conf.getText().equals(extTextValue)) {
addViolation(failureMsg, "content", extTextValue, conf.getText());
}
}
use of org.bouncycastle.asn1.DERPrintableString in project xipki by xipki.
the class ScepUtil method generateRequest.
public static PKCS10CertificationRequest generateRequest(PrivateKey privatekey, SubjectPublicKeyInfo subjectPublicKeyInfo, X500Name subjectDn, String challengePassword, List<Extension> extensions) throws OperatorCreationException {
requireNonNull("privatekey", privatekey);
requireNonNull("subjectPublicKeyInfo", subjectPublicKeyInfo);
requireNonNull("subjectDn", subjectDn);
Map<ASN1ObjectIdentifier, ASN1Encodable> attributes = new HashMap<ASN1ObjectIdentifier, ASN1Encodable>();
if (challengePassword != null && !challengePassword.isEmpty()) {
DERPrintableString asn1Pwd = new DERPrintableString(challengePassword);
attributes.put(PKCSObjectIdentifiers.pkcs_9_at_challengePassword, asn1Pwd);
}
if (extensions != null && !extensions.isEmpty()) {
Extensions asn1Extensions = new Extensions(extensions.toArray(new Extension[0]));
attributes.put(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, asn1Extensions);
}
return generateRequest(privatekey, subjectPublicKeyInfo, subjectDn, attributes);
}
use of org.bouncycastle.asn1.DERPrintableString in project keepass2android by PhilippC.
the class ASN1Dump method _dumpAsString.
/**
* dump a DER object as a formatted string with indentation
*
* @param obj the DERObject to be dumped out.
*/
static void _dumpAsString(String indent, boolean verbose, DERObject obj, StringBuffer buf) {
String nl = System.getProperty("line.separator");
if (obj instanceof ASN1Sequence) {
Enumeration e = ((ASN1Sequence) obj).getObjects();
String tab = indent + TAB;
buf.append(indent);
if (obj instanceof BERConstructedSequence) {
buf.append("BER ConstructedSequence");
} else if (obj instanceof DERConstructedSequence) {
buf.append("DER ConstructedSequence");
} else if (obj instanceof BERSequence) {
buf.append("BER Sequence");
} else if (obj instanceof DERSequence) {
buf.append("DER Sequence");
} else {
buf.append("Sequence");
}
buf.append(nl);
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o == null || o.equals(new DERNull())) {
buf.append(tab);
buf.append("NULL");
buf.append(nl);
} else if (o instanceof DERObject) {
_dumpAsString(tab, verbose, (DERObject) o, buf);
} else {
_dumpAsString(tab, verbose, ((DEREncodable) o).getDERObject(), buf);
}
}
} else if (obj instanceof DERTaggedObject) {
String tab = indent + TAB;
buf.append(indent);
if (obj instanceof BERTaggedObject) {
buf.append("BER Tagged [");
} else {
buf.append("Tagged [");
}
DERTaggedObject o = (DERTaggedObject) obj;
buf.append(Integer.toString(o.getTagNo()));
buf.append(']');
if (!o.isExplicit()) {
buf.append(" IMPLICIT ");
}
buf.append(nl);
if (o.isEmpty()) {
buf.append(tab);
buf.append("EMPTY");
buf.append(nl);
} else {
_dumpAsString(tab, verbose, o.getObject(), buf);
}
} else if (obj instanceof DERConstructedSet) {
Enumeration e = ((ASN1Set) obj).getObjects();
String tab = indent + TAB;
buf.append(indent);
buf.append("ConstructedSet");
buf.append(nl);
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o == null) {
buf.append(tab);
buf.append("NULL");
buf.append(nl);
} else if (o instanceof DERObject) {
_dumpAsString(tab, verbose, (DERObject) o, buf);
} else {
_dumpAsString(tab, verbose, ((DEREncodable) o).getDERObject(), buf);
}
}
} else if (obj instanceof BERSet) {
Enumeration e = ((ASN1Set) obj).getObjects();
String tab = indent + TAB;
buf.append(indent);
buf.append("BER Set");
buf.append(nl);
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o == null) {
buf.append(tab);
buf.append("NULL");
buf.append(nl);
} else if (o instanceof DERObject) {
_dumpAsString(tab, verbose, (DERObject) o, buf);
} else {
_dumpAsString(tab, verbose, ((DEREncodable) o).getDERObject(), buf);
}
}
} else if (obj instanceof DERSet) {
Enumeration e = ((ASN1Set) obj).getObjects();
String tab = indent + TAB;
buf.append(indent);
buf.append("DER Set");
buf.append(nl);
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o == null) {
buf.append(tab);
buf.append("NULL");
buf.append(nl);
} else if (o instanceof DERObject) {
_dumpAsString(tab, verbose, (DERObject) o, buf);
} else {
_dumpAsString(tab, verbose, ((DEREncodable) o).getDERObject(), buf);
}
}
} else if (obj instanceof DERObjectIdentifier) {
buf.append(indent + "ObjectIdentifier(" + ((DERObjectIdentifier) obj).getId() + ")" + nl);
} else if (obj instanceof DERBoolean) {
buf.append(indent + "Boolean(" + ((DERBoolean) obj).isTrue() + ")" + nl);
} else if (obj instanceof DERInteger) {
buf.append(indent + "Integer(" + ((DERInteger) obj).getValue() + ")" + nl);
} else if (obj instanceof BERConstructedOctetString) {
ASN1OctetString oct = (ASN1OctetString) obj;
buf.append(indent + "BER Constructed Octet String" + "[" + oct.getOctets().length + "] ");
if (verbose) {
buf.append(dumpBinaryDataAsString(indent, oct.getOctets()));
} else {
buf.append(nl);
}
} else if (obj instanceof DEROctetString) {
ASN1OctetString oct = (ASN1OctetString) obj;
buf.append(indent + "DER Octet String" + "[" + oct.getOctets().length + "] ");
if (verbose) {
buf.append(dumpBinaryDataAsString(indent, oct.getOctets()));
} else {
buf.append(nl);
}
} else if (obj instanceof DERBitString) {
DERBitString bt = (DERBitString) obj;
buf.append(indent + "DER Bit String" + "[" + bt.getBytes().length + ", " + bt.getPadBits() + "] ");
if (verbose) {
buf.append(dumpBinaryDataAsString(indent, bt.getBytes()));
} else {
buf.append(nl);
}
} else if (obj instanceof DERIA5String) {
buf.append(indent + "IA5String(" + ((DERIA5String) obj).getString() + ") " + nl);
} else if (obj instanceof DERUTF8String) {
buf.append(indent + "UTF8String(" + ((DERUTF8String) obj).getString() + ") " + nl);
} else if (obj instanceof DERPrintableString) {
buf.append(indent + "PrintableString(" + ((DERPrintableString) obj).getString() + ") " + nl);
} else if (obj instanceof DERVisibleString) {
buf.append(indent + "VisibleString(" + ((DERVisibleString) obj).getString() + ") " + nl);
} else if (obj instanceof DERBMPString) {
buf.append(indent + "BMPString(" + ((DERBMPString) obj).getString() + ") " + nl);
} else if (obj instanceof DERT61String) {
buf.append(indent + "T61String(" + ((DERT61String) obj).getString() + ") " + nl);
} else if (obj instanceof DERUTCTime) {
buf.append(indent + "UTCTime(" + ((DERUTCTime) obj).getTime() + ") " + nl);
} else if (obj instanceof DERGeneralizedTime) {
buf.append(indent + "GeneralizedTime(" + ((DERGeneralizedTime) obj).getTime() + ") " + nl);
} else if (obj instanceof DERUnknownTag) {
buf.append(indent + "Unknown " + Integer.toString(((DERUnknownTag) obj).getTag(), 16) + " " + new String(Hex.encode(((DERUnknownTag) obj).getData())) + nl);
} else if (obj instanceof BERApplicationSpecific) {
buf.append(outputApplicationSpecific("BER", indent, verbose, obj, nl));
} else if (obj instanceof DERApplicationSpecific) {
buf.append(outputApplicationSpecific("DER", indent, verbose, obj, nl));
} else if (obj instanceof DEREnumerated) {
DEREnumerated en = (DEREnumerated) obj;
buf.append(indent + "DER Enumerated(" + en.getValue() + ")" + nl);
} else if (obj instanceof DERExternal) {
DERExternal ext = (DERExternal) obj;
buf.append(indent + "External " + nl);
String tab = indent + TAB;
if (ext.getDirectReference() != null) {
buf.append(tab + "Direct Reference: " + ext.getDirectReference().getId() + nl);
}
if (ext.getIndirectReference() != null) {
buf.append(tab + "Indirect Reference: " + ext.getIndirectReference().toString() + nl);
}
if (ext.getDataValueDescriptor() != null) {
_dumpAsString(tab, verbose, ext.getDataValueDescriptor(), buf);
}
buf.append(tab + "Encoding: " + ext.getEncoding() + nl);
_dumpAsString(tab, verbose, ext.getExternalContent(), buf);
} else {
buf.append(indent + obj.toString() + nl);
}
}
use of org.bouncycastle.asn1.DERPrintableString in project xipki by xipki.
the class CertStoreQueryExecutor method getLatestSerialNumber.
// method isHealthy
String getLatestSerialNumber(X500Name nameWithSn) throws OperationException {
RDN[] rdns1 = nameWithSn.getRDNs();
RDN[] rdns2 = new RDN[rdns1.length];
for (int i = 0; i < rdns1.length; i++) {
RDN rdn = rdns1[i];
rdns2[i] = rdn.getFirst().getType().equals(ObjectIdentifiers.DN_SERIALNUMBER) ? new RDN(ObjectIdentifiers.DN_SERIALNUMBER, new DERPrintableString("%")) : rdn;
}
String namePattern = X509Util.getRfc4519Name(new X500Name(rdns2));
final String sql = sqls.sqlLatestSerialForSubjectLike;
ResultSet rs = null;
PreparedStatement ps;
try {
ps = borrowPreparedStatement(sql);
} catch (DataAccessException ex) {
throw new OperationException(ErrorCode.DATABASE_FAILURE, ex.getMessage());
}
String subjectStr;
try {
ps.setString(1, namePattern);
rs = ps.executeQuery();
if (!rs.next()) {
return null;
}
subjectStr = rs.getString("SUBJECT");
} catch (SQLException ex) {
throw new OperationException(ErrorCode.DATABASE_FAILURE, ex.getMessage());
} finally {
releaseDbResources(ps, rs);
}
X500Name lastName = new X500Name(subjectStr);
RDN[] rdns = lastName.getRDNs(ObjectIdentifiers.DN_SERIALNUMBER);
if (rdns == null || rdns.length == 0) {
return null;
}
return X509Util.rdnValueToString(rdns[0].getFirst().getValue());
}
Aggregations