Search in sources :

Example 6 with ASN1Boolean

use of org.bouncycastle.asn1.ASN1Boolean 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()) + ")");
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) RubyTime(org.jruby.RubyTime) RubyArray(org.jruby.RubyArray) DERApplicationSpecific(org.bouncycastle.asn1.DERApplicationSpecific) RubyInteger(org.jruby.RubyInteger) ASN1TaggedObject(org.bouncycastle.asn1.ASN1TaggedObject) ASN1UTCTime(org.bouncycastle.asn1.ASN1UTCTime) ASN1GeneralizedTime(org.bouncycastle.asn1.ASN1GeneralizedTime) DERBitString(org.bouncycastle.asn1.DERBitString) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERBMPString(org.bouncycastle.asn1.DERBMPString) DERGeneralString(org.bouncycastle.asn1.DERGeneralString) RubyString(org.jruby.RubyString) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) DERNumericString(org.bouncycastle.asn1.DERNumericString) DEROctetString(org.bouncycastle.asn1.DEROctetString) BEROctetString(org.bouncycastle.asn1.BEROctetString) DERIA5String(org.bouncycastle.asn1.DERIA5String) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERT61String(org.bouncycastle.asn1.DERT61String) DERVisibleString(org.bouncycastle.asn1.DERVisibleString) ASN1String(org.bouncycastle.asn1.ASN1String) DERUniversalString(org.bouncycastle.asn1.DERUniversalString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) DERInteger(org.bouncycastle.asn1.DERInteger) DERIA5String(org.bouncycastle.asn1.DERIA5String) DERGeneralizedTime(org.bouncycastle.asn1.DERGeneralizedTime) DERUTCTime(org.bouncycastle.asn1.DERUTCTime) ASN1Enumerated(org.bouncycastle.asn1.ASN1Enumerated) DERGeneralString(org.bouncycastle.asn1.DERGeneralString) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) Ruby(org.jruby.Ruby) DERBoolean(org.bouncycastle.asn1.DERBoolean) ByteList(org.jruby.util.ByteList) DERBMPString(org.bouncycastle.asn1.DERBMPString) RubyString(org.jruby.RubyString) DERUniversalString(org.bouncycastle.asn1.DERUniversalString) DERBitString(org.bouncycastle.asn1.DERBitString) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException) DERObjectIdentifier(org.bouncycastle.asn1.DERObjectIdentifier) Date(java.util.Date) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) BigInteger(java.math.BigInteger) RubyInteger(org.jruby.RubyInteger) DERInteger(org.bouncycastle.asn1.DERInteger) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Set(org.bouncycastle.asn1.ASN1Set) DERNumericString(org.bouncycastle.asn1.DERNumericString) DERT61String(org.bouncycastle.asn1.DERT61String) ASN1String(org.bouncycastle.asn1.ASN1String) ASN1Boolean(org.bouncycastle.asn1.ASN1Boolean) ParseException(java.text.ParseException) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) ASN1Null(org.bouncycastle.asn1.ASN1Null)

Example 7 with ASN1Boolean

use of org.bouncycastle.asn1.ASN1Boolean in project jruby-openssl by jruby.

the class X509Extension method initialize.

@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
    if (args.length == 1) {
        final byte[] bytes = to_der_if_possible(context, args[0]).asString().getBytes();
        try {
            ASN1Sequence seq = (ASN1Sequence) ASN1.readObject(bytes);
            setRealObjectID((ASN1ObjectIdentifier) seq.getObjectAt(0));
            final ASN1Encodable criticalOrValue = seq.getObjectAt(1);
            if (criticalOrValue instanceof ASN1Boolean) {
                setRealCritical(((ASN1Boolean) criticalOrValue).isTrue());
                // byte[]
                this.value = ((DEROctetString) seq.getObjectAt(2)).getOctets();
            } else if (criticalOrValue instanceof DERBoolean) {
                // NOTE: keep it due BC <= 1.50
                setRealCritical(((DERBoolean) criticalOrValue).isTrue());
                // byte[]
                this.value = ((DEROctetString) seq.getObjectAt(2)).getOctets();
            } else {
                // byte[]
                this.value = ((DEROctetString) criticalOrValue).getOctets();
            }
        } catch (IOException e) {
            throw newExtensionError(context.runtime, e);
        }
    } else if (args.length > 1) {
        setRealObjectID(ASN1.getObjectID(context.runtime, args[0].toString()));
        // a RubyString
        this.value = args[1];
    } else {
        // args.length < 1
        throw context.runtime.newArgumentError("wrong number of arguments (0 for 1..3)");
    }
    if (args.length > 2)
        setRealCritical(args[2].isTrue());
    return this;
}
Also used : ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Boolean(org.bouncycastle.asn1.ASN1Boolean) IOException(java.io.IOException) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERBoolean(org.bouncycastle.asn1.DERBoolean) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ASN1Boolean (org.bouncycastle.asn1.ASN1Boolean)5 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)5 IOException (java.io.IOException)3 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 DERIA5String (org.bouncycastle.asn1.DERIA5String)3 DEROctetString (org.bouncycastle.asn1.DEROctetString)3 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)3 BigInteger (java.math.BigInteger)2 ASN1GeneralizedTime (org.bouncycastle.asn1.ASN1GeneralizedTime)2 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)2 ASN1String (org.bouncycastle.asn1.ASN1String)2 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)2 DERBMPString (org.bouncycastle.asn1.DERBMPString)2 DERBitString (org.bouncycastle.asn1.DERBitString)2 DERBoolean (org.bouncycastle.asn1.DERBoolean)2 DERGeneralString (org.bouncycastle.asn1.DERGeneralString)2 DERUniversalString (org.bouncycastle.asn1.DERUniversalString)2