use of org.bouncycastle.asn1.DERPrintableString in project signer by demoiselle.
the class OIDGeneric method getInstance.
/**
* Instance for OIDGeneric.
*
* @param data
* Set of bytes with the contents of the certificate.
* @return Object GenericOID
* @throws IOException exception of input/output
* @throws Exception general exception
*/
public static OIDGeneric getInstance(byte[] data) throws IOException, Exception {
is = new ASN1InputStream(data);
DLSequence sequence = (DLSequence) is.readObject();
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) sequence.getObjectAt(0);
DERTaggedObject taggedObject = (DERTaggedObject) sequence.getObjectAt(1);
DERTaggedObject taggedObject2 = (DERTaggedObject) taggedObject.getObject();
DEROctetString octet = null;
DERPrintableString print = null;
DERUTF8String utf8 = null;
DERIA5String ia5 = null;
try {
octet = (DEROctetString) taggedObject2.getObject();
} catch (Exception e) {
try {
print = (DERPrintableString) taggedObject2.getObject();
} catch (Exception e1) {
try {
utf8 = (DERUTF8String) taggedObject2.getObject();
} catch (Exception e2) {
ia5 = (DERIA5String) taggedObject2.getObject();
}
}
}
String className = getPackageName() + oid.getId().replaceAll("[.]", "_");
OIDGeneric oidGenerico;
try {
oidGenerico = (OIDGeneric) Class.forName(className).newInstance();
} catch (InstantiationException e) {
throw new Exception(coreMessagesBundle.getString("error.class.instance", className), e);
} catch (IllegalAccessException e) {
throw new Exception(coreMessagesBundle.getString("error.class.illegal.access", className), e);
} catch (ClassNotFoundException e) {
oidGenerico = new OIDGeneric();
}
oidGenerico.oid = oid.getId();
if (octet != null) {
oidGenerico.data = new String(octet.getOctets());
} else {
if (print != null) {
oidGenerico.data = print.getString();
} else {
if (utf8 != null) {
oidGenerico.data = utf8.getString();
} else {
oidGenerico.data = ia5.getString();
}
}
}
oidGenerico.initialize();
return oidGenerico;
}
use of org.bouncycastle.asn1.DERPrintableString in project signer by demoiselle.
the class PolicyIssuerName method parse.
@Override
public void parse(ASN1Primitive primitive) {
if (primitive instanceof DLSequence) {
DLSequence sequence = (DLSequence) primitive;
ASN1Encodable asn1Encodable = sequence.getObjectAt(0);
if (asn1Encodable instanceof DERTaggedObject) {
DERTaggedObject derTaggedObject = (DERTaggedObject) asn1Encodable;
ASN1Primitive object = derTaggedObject.getObject();
if (object instanceof DEROctetString) {
OctetString octetString = new OctetString();
octetString.parse(object);
this.issuerName = octetString.getValueUTF8();
} else if (object instanceof DERSequence) {
DERSequence sequence2 = (DERSequence) object;
for (int i = 0; i < sequence2.size(); i++) {
ASN1Encodable obj = sequence2.getObjectAt(i);
if (obj instanceof DERSet) {
DERSet set = (DERSet) obj;
ASN1Encodable object2 = set.getObjectAt(0);
if (object2 instanceof DERSequence) {
DERSequence sequence3 = (DERSequence) object2;
ObjectIdentifier objectIdendifier = new ObjectIdentifier();
objectIdendifier.parse(sequence3.getObjectAt(0).toASN1Primitive());
String name = null;
ASN1Encodable object3 = sequence3.getObjectAt(1);
if (object3 instanceof DERPrintableString) {
name = ((DERPrintableString) object3).getString();
} else if (object3 instanceof DERUTF8String) {
name = ((DERUTF8String) object3).getString();
} else {
System.out.println(policyMessagesBundle.getString("error.not.recognized.object", object3.getClass(), object3.toString()));
}
if (this.issuerNames == null) {
this.issuerNames = new HashMap<ObjectIdentifier, String>();
}
this.issuerNames.put(objectIdendifier, name);
}
}
}
}
}
}
}
use of org.bouncycastle.asn1.DERPrintableString in project keystore-explorer by kaikramer.
the class DialogHelper method populateTextField.
private static void populateTextField(Attribute[] attrs, JTextField textField, ASN1ObjectIdentifier pkcs9Attr) {
if (attrs != null) {
for (Attribute attribute : attrs) {
ASN1ObjectIdentifier attributeOid = attribute.getAttrType();
if (attributeOid.equals(pkcs9Attr)) {
ASN1Encodable challenge = attribute.getAttributeValues()[0];
// data type can be one of IA5String or UTF8String
if (challenge instanceof DERPrintableString) {
textField.setText(((DERPrintableString) challenge).getString());
} else if (challenge instanceof DERUTF8String) {
textField.setText(((DERUTF8String) challenge).getString());
}
textField.setCaretPosition(0);
}
}
}
}
use of org.bouncycastle.asn1.DERPrintableString in project jruby-openssl by jruby.
the class ASN1 method decodeObject.
// ObjectId
static IRubyObject decodeObject(final ThreadContext context, final RubyModule ASN1, final org.bouncycastle.asn1.ASN1Encodable obj) throws IOException, IllegalArgumentException {
final Ruby runtime = context.runtime;
if (obj instanceof ASN1Integer) {
final BN val = BN.newBN(runtime, ((ASN1Integer) obj).getValue());
return ASN1.getClass("Integer").callMethod(context, "new", val);
}
if (obj instanceof DERInteger) {
final BN val = BN.newBN(runtime, ((DERInteger) obj).getValue());
return ASN1.getClass("Integer").callMethod(context, "new", val);
}
if (obj instanceof DERBitString) {
final DERBitString derObj = (DERBitString) obj;
RubyString str = runtime.newString(new ByteList(derObj.getBytes(), false));
IRubyObject bitString = ASN1.getClass("BitString").callMethod(context, "new", str);
bitString.callMethod(context, "unused_bits=", runtime.newFixnum(derObj.getPadBits()));
return bitString;
}
if (obj instanceof ASN1String) {
final Integer typeId = typeId(obj.getClass());
String type = typeId == null ? null : (String) (ASN1_INFO[typeId][2]);
final ByteList bytes;
if (obj instanceof DERUTF8String) {
if (type == null)
type = "UTF8String";
bytes = new ByteList(((DERUTF8String) obj).getString().getBytes("UTF-8"), false);
} else {
if (type == null) {
if (obj instanceof DERNumericString) {
type = "NumericString";
} else if (obj instanceof DERPrintableString) {
type = "PrintableString";
} else if (obj instanceof DERIA5String) {
type = "IA5String";
} else if (obj instanceof DERT61String) {
type = "T61String";
} else if (obj instanceof DERGeneralString) {
type = "GeneralString";
} else if (obj instanceof DERUniversalString) {
type = "UniversalString";
} else if (obj instanceof DERBMPString) {
type = "BMPString";
} else {
// NOTE "VideotexString", "GraphicString", "ISO64String" not-handled in BC !
throw new IllegalArgumentException("could not handle ASN1 string type: " + obj + " (" + obj.getClass().getName() + ")");
}
}
bytes = ByteList.create(((ASN1String) obj).getString());
}
return ASN1.getClass(type).callMethod(context, "new", runtime.newString(bytes));
}
if (obj instanceof ASN1OctetString) {
final ByteList octets = new ByteList(((ASN1OctetString) obj).getOctets(), false);
// final ByteList octets = new ByteList(((ASN1OctetString) obj).getEncoded(ASN1Encoding.DER), false);
return ASN1.getClass("OctetString").callMethod(context, "new", runtime.newString(octets));
}
if (obj instanceof ASN1Null) {
return ASN1.getClass("Null").callMethod(context, "new", runtime.getNil());
}
if (obj instanceof ASN1Boolean) {
final boolean val = ((ASN1Boolean) obj).isTrue();
return ASN1.getClass("Boolean").callMethod(context, "new", runtime.newBoolean(val));
}
// DERBoolean extends ASN1Boolean only since 1.51 (<= 1.50 the other way around)
if (obj instanceof DERBoolean) {
final boolean val = ((DERBoolean) obj).isTrue();
return ASN1.getClass("Boolean").callMethod(context, "new", runtime.newBoolean(val));
}
if (obj instanceof ASN1UTCTime) {
final Date adjustedTime;
try {
adjustedTime = ((ASN1UTCTime) obj).getAdjustedDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, adjustedTime.getTime());
return ASN1.getClass("UTCTime").callMethod(context, "new", time);
}
// NOTE: keep for BC versions compatibility ... extends ASN1UTCTime (since BC 1.51)
if (obj instanceof DERUTCTime) {
final Date adjustedTime;
try {
adjustedTime = ((DERUTCTime) obj).getAdjustedDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, adjustedTime.getTime());
return ASN1.getClass("UTCTime").callMethod(context, "new", time);
}
if (obj instanceof ASN1GeneralizedTime) {
final Date generalTime;
try {
generalTime = ((ASN1GeneralizedTime) obj).getDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, generalTime.getTime());
return ASN1.getClass("GeneralizedTime").callMethod(context, "new", time);
}
// NOTE: keep for BC versions compatibility ... extends ASN1GeneralizedTime (since BC 1.51)
if (obj instanceof DERGeneralizedTime) {
final Date generalTime;
try {
generalTime = ((DERGeneralizedTime) obj).getDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, generalTime.getTime());
return ASN1.getClass("GeneralizedTime").callMethod(context, "new", time);
}
if (obj instanceof ASN1ObjectIdentifier) {
final String objId = ((ASN1ObjectIdentifier) obj).getId();
return ASN1.getClass("ObjectId").callMethod(context, "new", runtime.newString(objId));
}
// DERObjectIdentifier extends ASN1ObjectIdentifier = 1.51
if (obj instanceof DERObjectIdentifier) {
final String objId = ((DERObjectIdentifier) obj).getId();
return ASN1.getClass("ObjectId").callMethod(context, "new", runtime.newString(objId));
}
if (obj instanceof ASN1TaggedObject) {
final ASN1TaggedObject taggedObj = (ASN1TaggedObject) obj;
IRubyObject val = decodeObject(context, ASN1, taggedObj.getObject());
IRubyObject tag = runtime.newFixnum(taggedObj.getTagNo());
IRubyObject tag_class = runtime.newSymbol("CONTEXT_SPECIFIC");
final RubyArray valArr = runtime.newArray(val);
return ASN1.getClass("ASN1Data").callMethod(context, "new", new IRubyObject[] { valArr, tag, tag_class });
}
if (obj instanceof DERApplicationSpecific) {
final DERApplicationSpecific appSpecific = (DERApplicationSpecific) obj;
IRubyObject tag = runtime.newFixnum(appSpecific.getApplicationTag());
IRubyObject tag_class = runtime.newSymbol("APPLICATION");
final ASN1Sequence sequence = (ASN1Sequence) appSpecific.getObject(SEQUENCE);
@SuppressWarnings("unchecked") final RubyArray valArr = decodeObjects(context, ASN1, sequence.getObjects());
return ASN1.getClass("ASN1Data").callMethod(context, "new", new IRubyObject[] { valArr, tag, tag_class });
}
if (obj instanceof ASN1Sequence) {
@SuppressWarnings("unchecked") RubyArray arr = decodeObjects(context, ASN1, ((ASN1Sequence) obj).getObjects());
return ASN1.getClass("Sequence").callMethod(context, "new", arr);
}
if (obj instanceof ASN1Set) {
@SuppressWarnings("unchecked") RubyArray arr = decodeObjects(context, ASN1, ((ASN1Set) obj).getObjects());
return ASN1.getClass("Set").callMethod(context, "new", arr);
}
if (obj instanceof ASN1Enumerated) {
final RubyInteger value = RubyBignum.bignorm(runtime, ((ASN1Enumerated) obj).getValue());
return ASN1.getClass("Enumerated").callMethod(context, "new", value);
}
throw new IllegalArgumentException("unable to decode object: " + obj + " (" + (obj == null ? "" : obj.getClass().getName()) + ")");
}
Aggregations