use of org.gudy.bouncycastle.asn1.ASN1Encodable in project keystore-explorer by kaikramer.
the class CryptoFileUtil method detectKeyStoreType.
/**
* Detect the KeyStore type contained in the supplied file.
*
* @param is
* Input stream to detect type for
* @return KeyStore type or null if none matched
* @throws IOException
* If an I/O problem occurred
*/
public static KeyStoreType detectKeyStoreType(InputStream is) throws IOException {
byte[] contents = ReadUtil.readFully(is);
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(contents))) {
// If less than 4 bytes are available it isn't a KeyStore
if (dis.available() < 4) {
return null;
}
// Read first integer (4 bytes)
int i1 = dis.readInt();
// Test for JKS - starts with appropriate magic number
if (i1 == JKS_MAGIC_NUMBER) {
return JKS;
}
// Test for JCEKS - starts with appropriate magic number
if (i1 == JCEKS_MAGIC_NUMBER) {
return JCEKS;
}
// Both start with a version number of 0, 1 or 2
if ((i1 == 0) || (i1 == 1) || (i1 == 2)) {
if (contents.length < 26) {
// Insufficient bytes to be BKS or UBER
return null;
}
// Skip to 21st from last byte (file length minus 21 and the 4 bytes already read)
dis.skip(contents.length - 25);
// Read what may be the null byte
if (dis.readByte() == 0) {
// Found null byte - BKS/BKS-V1
if (i1 == 1) {
return BKS_V1;
} else {
return BKS;
}
} else {
// No null byte - UBER
return UBER;
}
}
}
// @formatter:off
/*
* Test for PKCS #12. ASN.1 should look like this:
*
* PFX ::= ASN1Sequence { version ASN1Integer {v3(3)}(v3,...), authSafe
* ContentInfo, macData MacData OPTIONAL
*/
// @formatter:on
ASN1Primitive pfx = null;
try {
pfx = ASN1Primitive.fromByteArray(contents);
} catch (IOException e) {
// if it cannot be parsed as ASN1, it is certainly not a pfx key store
return null;
}
// Is a sequence...
if ((pfx != null) && (pfx instanceof ASN1Sequence)) {
// Has two or three components...
ASN1Sequence sequence = (ASN1Sequence) pfx;
if ((sequence.size() == 2) || (sequence.size() == 3)) {
// ...the first of which is a version of 3
ASN1Encodable firstComponent = sequence.getObjectAt(0);
if (firstComponent instanceof ASN1Integer) {
ASN1Integer version = (ASN1Integer) firstComponent;
if (version.getValue().intValue() == 3) {
return PKCS12;
}
}
}
}
// KeyStore type not recognised
return null;
}
use of org.gudy.bouncycastle.asn1.ASN1Encodable in project keystore-explorer by kaikramer.
the class GeneralNameUtil method toString.
/**
* Get string representation for all General Names.
*
* @param generalName
* General name
* @return String representation of general name
* @throws IOException
* If general name is invalid
*/
public static String toString(GeneralName generalName) throws IOException {
if (generalName == null) {
return "";
}
switch(generalName.getTagNo()) {
case GeneralName.ediPartyName:
/* EDIPartyName ::= SEQUENCE {
* nameAssigner [0] DirectoryString OPTIONAL,
* partyName [1] DirectoryString }
*/
ASN1Sequence ediPartyName = (ASN1Sequence) generalName.getName();
DirectoryString nameAssigner = DirectoryString.getInstance(ediPartyName.getObjectAt(0));
DirectoryString partyName = DirectoryString.getInstance(ediPartyName.getObjectAt(1));
String nameAssignerStr = null;
if (nameAssigner != null) {
// Optional
nameAssignerStr = nameAssigner.getString();
}
String partyNameStr = partyName.getString();
if (nameAssignerStr != null) {
return MessageFormat.format(res.getString("GeneralNameUtil.EdiPartyGeneralName"), nameAssignerStr, partyNameStr);
} else {
return MessageFormat.format(res.getString("GeneralNameUtil.EdiPartyGeneralNameNoAssigner"), partyNameStr);
}
case GeneralName.otherName:
return parseUPN(generalName);
case GeneralName.x400Address:
/*
* No support for this at the moment - just get a hex dump
* The Oracle CertificateFactory blows up if a certificate extension contains this anyway
*/
ASN1Encodable x400Address = generalName.getName();
return MessageFormat.format(res.getString("GeneralNameUtil.X400AddressGeneralName"), HexUtil.getHexString(x400Address.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
default:
return safeToString(generalName, true);
}
}
use of org.gudy.bouncycastle.asn1.ASN1Encodable in project keystore-explorer by kaikramer.
the class GeneralNameUtil method parseUPN.
/**
* Parse UPN/otherName
*
* @param generalName otherName object
* @return UPN as string
*/
public static String parseUPN(GeneralName generalName) {
// OtherName ::= SEQUENCE {
// type-id OBJECT IDENTIFIER,
// value [0] EXPLICIT ANY DEFINED BY type-id }
ASN1Sequence otherName = (ASN1Sequence) generalName.getName();
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) otherName.getObjectAt(0);
if (UPN_OID.equals(oid.getId())) {
DERTaggedObject derTaggedObject = (DERTaggedObject) otherName.getObjectAt(1);
DERUTF8String upn = DERUTF8String.getInstance(derTaggedObject.getObject());
return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), "UPN", upn.getString());
}
// fallback to generic handling
ASN1Encodable value = otherName.getObjectAt(1);
try {
return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), ObjectIdUtil.toString(oid), HexUtil.getHexString(value.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
} catch (IOException e) {
return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), ObjectIdUtil.toString(oid), "");
}
}
use of org.gudy.bouncycastle.asn1.ASN1Encodable in project xipki by xipki.
the class BaseX509Certprofile method createPostalAddressRdn.
private static RDN createPostalAddressRdn(ASN1ObjectIdentifier type, ASN1Encodable rdnValue, RdnControl control, int index) throws BadCertTemplateException {
ParamUtil.requireNonNull("type", type);
if (!(rdnValue instanceof ASN1Sequence)) {
throw new BadCertTemplateException("rdnValue of RDN postalAddress has incorrect syntax");
}
ASN1Sequence seq = (ASN1Sequence) rdnValue;
final int size = seq.size();
if (size < 1 || size > 6) {
throw new BadCertTemplateException("Sequence size of RDN postalAddress is not within [1, 6]: " + size);
}
ASN1EncodableVector vec = new ASN1EncodableVector();
for (int i = 0; i < size; i++) {
ASN1Encodable line = seq.getObjectAt(i);
String text;
if (line instanceof ASN1String && !(line instanceof DERUniversalString)) {
text = ((ASN1String) line).getString();
} else {
throw new BadCertTemplateException(String.format("postalAddress[%d] has incorrect syntax", i));
}
ASN1Encodable asn1Line = createRdnValue(text, type, control, index);
vec.add(asn1Line);
}
return new RDN(type, new DERSequence(vec));
}
use of org.gudy.bouncycastle.asn1.ASN1Encodable in project xipki by xipki.
the class BaseX509Certprofile method createDateOfBirthRdn.
private static RDN createDateOfBirthRdn(ASN1ObjectIdentifier type, ASN1Encodable rdnValue) throws BadCertTemplateException {
ParamUtil.requireNonNull("type", type);
String text;
ASN1Encodable newRdnValue = null;
if (rdnValue instanceof ASN1GeneralizedTime) {
text = ((ASN1GeneralizedTime) rdnValue).getTimeString();
newRdnValue = rdnValue;
} else if (rdnValue instanceof ASN1String && !(rdnValue instanceof DERUniversalString)) {
text = ((ASN1String) rdnValue).getString();
} else {
throw new BadCertTemplateException("Value of RDN dateOfBirth has incorrect syntax");
}
if (!SubjectDnSpec.PATTERN_DATE_OF_BIRTH.matcher(text).matches()) {
throw new BadCertTemplateException("Value of RDN dateOfBirth does not have format YYYMMDD000000Z");
}
if (newRdnValue == null) {
newRdnValue = new DERGeneralizedTime(text);
}
return new RDN(type, newRdnValue);
}
Aggregations