Search in sources :

Example 1 with ThreadContext

use of org.jruby.runtime.ThreadContext in project gocd by gocd.

the class NokogiriHandler method call.

private void call(String methodName) {
    ThreadContext context = ruby.getCurrentContext();
    RuntimeHelpers.invoke(context, document(context), methodName);
}
Also used : ThreadContext(org.jruby.runtime.ThreadContext)

Example 2 with ThreadContext

use of org.jruby.runtime.ThreadContext in project jruby-openssl by jruby.

the class ASN1 method createASN1.

public static void createASN1(final Ruby runtime, final RubyModule OpenSSL) {
    final RubyModule ASN1 = OpenSSL.defineModuleUnder("ASN1");
    final RubyClass OpenSSLError = OpenSSL.getClass("OpenSSLError");
    ASN1.defineClassUnder("ASN1Error", OpenSSLError, OpenSSLError.getAllocator());
    ASN1.defineAnnotatedMethods(ASN1.class);
    final RubyArray UNIVERSAL_TAG_NAME = runtime.newArray(ASN1_INFO.length);
    for (int i = 0; i < ASN1_INFO.length; i++) {
        final String name = (String) ASN1_INFO[i][0];
        if (name.charAt(0) != '[') {
            UNIVERSAL_TAG_NAME.append(runtime.newString(name));
            ASN1.setConstant(name, runtime.newFixnum(i));
        } else {
            UNIVERSAL_TAG_NAME.append(runtime.getNil());
        }
    }
    ASN1.setConstant("UNIVERSAL_TAG_NAME", UNIVERSAL_TAG_NAME);
    final ThreadContext context = runtime.getCurrentContext();
    final ObjectAllocator asn1DataAllocator = ASN1Data.ALLOCATOR;
    RubyClass _ASN1Data = ASN1.defineClassUnder("ASN1Data", runtime.getObject(), asn1DataAllocator);
    _ASN1Data.addReadWriteAttribute(context, "value");
    _ASN1Data.addReadWriteAttribute(context, "tag");
    _ASN1Data.addReadWriteAttribute(context, "tag_class");
    _ASN1Data.defineAnnotatedMethods(ASN1Data.class);
    final ObjectAllocator primitiveAllocator = Primitive.ALLOCATOR;
    RubyClass Primitive = ASN1.defineClassUnder("Primitive", _ASN1Data, primitiveAllocator);
    Primitive.addReadWriteAttribute(context, "tagging");
    Primitive.addReadAttribute(context, "infinite_length");
    Primitive.defineAnnotatedMethods(Primitive.class);
    final ObjectAllocator constructiveAllocator = Constructive.ALLOCATOR;
    RubyClass Constructive = ASN1.defineClassUnder("Constructive", _ASN1Data, constructiveAllocator);
    Constructive.includeModule(runtime.getModule("Enumerable"));
    Constructive.addReadWriteAttribute(context, "tagging");
    Constructive.addReadWriteAttribute(context, "infinite_length");
    Constructive.defineAnnotatedMethods(Constructive.class);
    // OpenSSL::ASN1::Boolean <=> value is a Boolean
    ASN1.defineClassUnder("Boolean", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::Integer <=> value is a Number
    ASN1.defineClassUnder("Integer", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::Null <=> value is always nil
    ASN1.defineClassUnder("Null", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::Object <=> value is a String
    ASN1.defineClassUnder("Object", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::Enumerated <=> value is a Number
    ASN1.defineClassUnder("Enumerated", Primitive, primitiveAllocator);
    RubyClass BitString = ASN1.defineClassUnder("BitString", Primitive, primitiveAllocator);
    BitString.addReadWriteAttribute(context, "unused_bits");
    ASN1.defineClassUnder("OctetString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("UTF8String", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("NumericString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("PrintableString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("T61String", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("VideotexString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("IA5String", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("GraphicString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("ISO64String", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("GeneralString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("UniversalString", Primitive, primitiveAllocator);
    ASN1.defineClassUnder("BMPString", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::UTCTime <=> value is a Time
    ASN1.defineClassUnder("UTCTime", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::GeneralizedTime <=> value is a Time
    ASN1.defineClassUnder("GeneralizedTime", Primitive, primitiveAllocator);
    // OpenSSL::ASN1::EndOfContent <=> value is always nil
    ASN1.defineClassUnder("EndOfContent", Primitive, primitiveAllocator);
    RubyClass ObjectId = ASN1.defineClassUnder("ObjectId", Primitive, primitiveAllocator);
    ObjectId.defineAnnotatedMethods(ObjectId.class);
    ASN1.defineClassUnder("Sequence", Constructive, Constructive.getAllocator());
    ASN1.defineClassUnder("Set", Constructive, Constructive.getAllocator());
}
Also used : RubyModule(org.jruby.RubyModule) RubyArray(org.jruby.RubyArray) ThreadContext(org.jruby.runtime.ThreadContext) RubyClass(org.jruby.RubyClass) 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) ObjectAllocator(org.jruby.runtime.ObjectAllocator)

Example 3 with ThreadContext

use of org.jruby.runtime.ThreadContext in project jruby-openssl by jruby.

the class SSLContext method convertToAuxCerts.

private static List<X509AuxCertificate> convertToAuxCerts(final ThreadContext context, IRubyObject value) {
    final RubyModule SSLContext = _SSLContext(context.runtime);
    final RubyModule Certificate = _Certificate(context.runtime);
    if (value instanceof RubyArray) {
        final RubyArray val = (RubyArray) value;
        final int size = val.size();
        final ArrayList<X509AuxCertificate> result = new ArrayList<X509AuxCertificate>(size);
        for (int i = 0; i < size; i++) result.add(assureCertificate(context, Certificate, val.eltInternal(i)).getAuxCert());
        return result;
    }
    if (value instanceof List) {
        final List<X509Cert> val = (List) value;
        final int size = val.size();
        final ArrayList<X509AuxCertificate> result = new ArrayList<X509AuxCertificate>(size);
        for (int i = 0; i < size; i++) result.add(assureCertificate(context, Certificate, val.get(i)).getAuxCert());
        return result;
    }
    // else :
    final ArrayList<X509AuxCertificate> result = new ArrayList<X509AuxCertificate>();
    Utils.invoke(context, value, "each", CallBlock.newCallClosure(value, SSLContext, Arity.NO_ARGUMENTS, new BlockCallback() {

        public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block) {
            result.add(assureCertificate(context, Certificate, args[0]).getAuxCert());
            return context.nil;
        }
    }, context));
    return result;
}
Also used : RubyModule(org.jruby.RubyModule) RubyArray(org.jruby.RubyArray) ArrayList(java.util.ArrayList) BlockCallback(org.jruby.runtime.BlockCallback) ThreadContext(org.jruby.runtime.ThreadContext) Block(org.jruby.runtime.Block) CallBlock(org.jruby.runtime.CallBlock) ByteList(org.jruby.util.ByteList) List(java.util.List) ArrayList(java.util.ArrayList) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate)

Example 4 with ThreadContext

use of org.jruby.runtime.ThreadContext in project jruby-openssl by jruby.

the class X509Extension method getValue.

private IRubyObject getValue(final Ruby runtime) throws IOException {
    if (value instanceof RubyString) {
        // explicitly set value
        return (RubyString) value;
    }
    final ThreadContext context = runtime.getCurrentContext();
    final byte[] enc = getRealValueEncoded();
    IRubyObject extValue = runtime.newString(new ByteList(enc, false));
    extValue = ASN1.decodeImpl(context, _ASN1(runtime), extValue);
    return extValue.callMethod(context, "value");
}
Also used : ByteList(org.jruby.util.ByteList) RubyString(org.jruby.RubyString) ThreadContext(org.jruby.runtime.ThreadContext) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 5 with ThreadContext

use of org.jruby.runtime.ThreadContext in project jruby-openssl by jruby.

the class OCSPRequest method verify.

@JRubyMethod(name = "verify", rest = true)
public IRubyObject verify(IRubyObject[] args) {
    Ruby runtime = getRuntime();
    ThreadContext context = runtime.getCurrentContext();
    int flags = 0;
    boolean ret = false;
    if (Arity.checkArgumentCount(runtime, args, 2, 3) == 3) {
        flags = RubyFixnum.fix2int((RubyFixnum) args[2]);
    }
    IRubyObject certificates = args[0];
    IRubyObject store = args[1];
    OCSPReq bcOCSPReq = getBCOCSPReq();
    if (bcOCSPReq == null) {
        throw newOCSPError(runtime, new NullPointerException("Missing BC asn1bcReq. Missing certIDs or signature?"));
    }
    if (!bcOCSPReq.isSigned()) {
        return RubyBoolean.newBoolean(runtime, ret);
    }
    GeneralName genName = bcOCSPReq.getRequestorName();
    if (genName.getTagNo() != 4) {
        return RubyBoolean.newBoolean(runtime, ret);
    }
    X500Name genX500Name = X500Name.getInstance(genName.getName());
    X509StoreContext storeContext = null;
    JcaContentVerifierProviderBuilder jcacvpb = new JcaContentVerifierProviderBuilder();
    jcacvpb.setProvider("BC");
    try {
        java.security.cert.Certificate signer = findCertByName(genX500Name, certificates, flags);
        if (signer == null)
            return RubyBoolean.newBoolean(runtime, ret);
        if ((flags & RubyFixnum.fix2int(_OCSP(runtime).getConstant(OCSP_NOINTERN))) > 0 && ((flags & RubyFixnum.fix2int(_OCSP(runtime).getConstant(OCSP_TRUSTOTHER))) > 0))
            flags |= RubyFixnum.fix2int(_OCSP(runtime).getConstant(OCSP_NOVERIFY));
        if ((flags & RubyFixnum.fix2int(_OCSP(runtime).getConstant(OCSP_NOSIGS))) == 0) {
            PublicKey signerPubKey = signer.getPublicKey();
            ContentVerifierProvider cvp = jcacvpb.build(signerPubKey);
            ret = bcOCSPReq.isSignatureValid(cvp);
            if (!ret) {
                return RubyBoolean.newBoolean(runtime, ret);
            }
        }
        if ((flags & RubyFixnum.fix2int(_OCSP(runtime).getConstant(OCSP_NOVERIFY))) == 0) {
            if ((flags & RubyFixnum.fix2int(_OCSP(runtime).getConstant(OCSP_NOCHAIN))) > 0) {
                storeContext = X509StoreContext.newStoreContext(context, (X509Store) store, X509Cert.wrap(runtime, signer), context.nil);
            } else {
                RubyArray certs = RubyArray.newEmptyArray(runtime);
                ASN1Sequence bcCerts = asn1bcReq.getOptionalSignature().getCerts();
                if (bcCerts != null) {
                    Iterator<ASN1Encodable> it = bcCerts.iterator();
                    while (it.hasNext()) {
                        Certificate cert = Certificate.getInstance(it.next());
                        certs.add(X509Cert.wrap(runtime, new X509AuxCertificate(cert)));
                    }
                }
                storeContext = X509StoreContext.newStoreContext(context, (X509Store) store, X509Cert.wrap(runtime, signer), certs);
            }
            storeContext.set_purpose(context, _X509(runtime).getConstant("PURPOSE_OCSP_HELPER"));
            storeContext.set_trust(context, _X509(runtime).getConstant("TRUST_OCSP_REQUEST"));
            ret = storeContext.verify(context).isTrue();
            if (!ret)
                return RubyBoolean.newBoolean(runtime, false);
        }
    } catch (Exception e) {
        debugStackTrace(e);
        throw newOCSPError(runtime, e);
    }
    return RubyBoolean.newBoolean(getRuntime(), ret);
}
Also used : RubyArray(org.jruby.RubyArray) X500Name(org.bouncycastle.asn1.x500.X500Name) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) Ruby(org.jruby.Ruby) ContentVerifierProvider(org.bouncycastle.operator.ContentVerifierProvider) PublicKey(java.security.PublicKey) ThreadContext(org.jruby.runtime.ThreadContext) RubyFixnum(org.jruby.RubyFixnum) RaiseException(org.jruby.exceptions.RaiseException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) JcaContentVerifierProviderBuilder(org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) OCSPReq(org.bouncycastle.cert.ocsp.OCSPReq) GeneralName(org.bouncycastle.asn1.x509.GeneralName) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) X509Certificate(java.security.cert.X509Certificate) Certificate(org.bouncycastle.asn1.x509.Certificate) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ThreadContext (org.jruby.runtime.ThreadContext)25 IRubyObject (org.jruby.runtime.builtin.IRubyObject)11 Ruby (org.jruby.Ruby)7 RubyArray (org.jruby.RubyArray)5 RubyClass (org.jruby.RubyClass)4 RubyString (org.jruby.RubyString)4 IOException (java.io.IOException)3 XPathFunctionException (javax.xml.xpath.XPathFunctionException)3 ByteList (org.jruby.util.ByteList)3 PublicKey (java.security.PublicKey)2 ArrayList (java.util.ArrayList)2 X500Name (org.bouncycastle.asn1.x500.X500Name)2 RubyHash (org.jruby.RubyHash)2 RubyModule (org.jruby.RubyModule)2 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 List (java.util.List)1