Search in sources :

Example 16 with ThreadContext

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

the class X509Name method createX509Name.

public static void createX509Name(final Ruby runtime, final RubyModule _X509) {
    RubyClass _Name = _X509.defineClassUnder("Name", runtime.getObject(), X509NAME_ALLOCATOR);
    RubyClass _OpenSSLError = runtime.getModule("OpenSSL").getClass("OpenSSLError");
    _X509.defineClassUnder("NameError", _OpenSSLError, _OpenSSLError.getAllocator());
    _Name.defineAnnotatedMethods(X509Name.class);
    _Name.includeModule(runtime.getComparable());
    _Name.setConstant("COMPAT", runtime.newFixnum(COMPAT));
    _Name.setConstant("RFC2253", runtime.newFixnum(RFC2253));
    _Name.setConstant("ONELINE", runtime.newFixnum(ONELINE));
    _Name.setConstant("MULTILINE", runtime.newFixnum(MULTILINE));
    final RubyFixnum UTF8_STRING = runtime.newFixnum(BERTags.UTF8_STRING);
    _Name.setConstant("DEFAULT_OBJECT_TYPE", UTF8_STRING);
    final RubyFixnum PRINTABLE_STRING = runtime.newFixnum(BERTags.PRINTABLE_STRING);
    final RubyFixnum IA5_STRING = runtime.newFixnum(BERTags.IA5_STRING);
    final ThreadContext context = runtime.getCurrentContext();
    final RubyHash hash = new RubyHash(runtime, UTF8_STRING);
    hash.op_aset(context, newString(runtime, new byte[] { 'C' }), PRINTABLE_STRING);
    final byte[] countryName = { 'c', 'o', 'u', 'n', 't', 'r', 'y', 'N', 'a', 'm', 'e' };
    hash.op_aset(context, newString(runtime, countryName), PRINTABLE_STRING);
    final byte[] serialNumber = { 's', 'e', 'r', 'i', 'a', 'l', 'N', 'u', 'm', 'b', 'e', 'r' };
    hash.op_aset(context, newString(runtime, serialNumber), PRINTABLE_STRING);
    final byte[] dnQualifier = { 'd', 'n', 'Q', 'u', 'a', 'l', 'i', 'f', 'i', 'e', 'r' };
    hash.op_aset(context, newString(runtime, dnQualifier), PRINTABLE_STRING);
    hash.op_aset(context, newString(runtime, new byte[] { 'D', 'C' }), IA5_STRING);
    final byte[] domainComponent = { 'd', 'o', 'm', 'a', 'i', 'n', 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n', 't' };
    hash.op_aset(context, newString(runtime, domainComponent), IA5_STRING);
    final byte[] emailAddress = { 'e', 'm', 'a', 'i', 'l', 'A', 'd', 'd', 'r', 'e', 's', 's' };
    hash.op_aset(context, newString(runtime, emailAddress), IA5_STRING);
    _Name.setConstant("OBJECT_TYPE_TEMPLATE", hash);
}
Also used : RubyHash(org.jruby.RubyHash) ThreadContext(org.jruby.runtime.ThreadContext) RubyClass(org.jruby.RubyClass) RubyFixnum(org.jruby.RubyFixnum)

Example 17 with ThreadContext

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

the class X509Request method getRequest.

private PKCS10Request getRequest() {
    if (request != null)
        return request;
    PublicKey publicKey = null;
    if (public_key != null && !public_key.isNil()) {
        publicKey = public_key.getPublicKey();
    }
    X500Name subjectName = subject != null ? getX500Name(subject) : null;
    final ThreadContext context = getRuntime().getCurrentContext();
    return request = new PKCS10Request(subjectName, publicKey, newAttributesImpl(context));
}
Also used : PKCS10Request(org.jruby.ext.openssl.impl.PKCS10Request) PublicKey(java.security.PublicKey) ThreadContext(org.jruby.runtime.ThreadContext) X500Name(org.bouncycastle.asn1.x500.X500Name)

Example 18 with ThreadContext

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

the class X509Store method createX509Store.

public static void createX509Store(final Ruby runtime, final RubyModule X509) {
    RubyClass Store = X509.defineClassUnder("Store", runtime.getObject(), X509STORE_ALLOCATOR);
    RubyClass OpenSSLError = runtime.getModule("OpenSSL").getClass("OpenSSLError");
    X509.defineClassUnder("StoreError", OpenSSLError, OpenSSLError.getAllocator());
    final ThreadContext context = runtime.getCurrentContext();
    Store.addReadWriteAttribute(context, "error");
    Store.addReadWriteAttribute(context, "error_string");
    Store.addReadWriteAttribute(context, "chain");
    Store.defineAnnotatedMethods(X509Store.class);
    Store.undefineMethod("dup");
    X509StoreContext.createX509StoreContext(runtime, X509);
}
Also used : ThreadContext(org.jruby.runtime.ThreadContext) RubyClass(org.jruby.RubyClass)

Example 19 with ThreadContext

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

the class PKCS7 method initialize.

@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, IRubyObject[] args) {
    if (Arity.checkArgumentCount(getRuntime(), args, 0, 1) == 0) {
        p7 = new org.jruby.ext.openssl.impl.PKCS7();
        try {
            p7.setType(ASN1Registry.NID_undef);
        } catch (PKCS7Exception e) {
            throw newPKCS7Error(getRuntime(), e);
        }
        return this;
    }
    IRubyObject arg = to_der_if_possible(context, args[0]);
    BIO input = obj2bio(arg);
    try {
        p7 = org.jruby.ext.openssl.impl.PKCS7.readPEM(input);
        if (p7 == null) {
            input.reset();
            p7 = org.jruby.ext.openssl.impl.PKCS7.fromASN1(input);
        }
    } catch (IllegalArgumentException e) {
        throw getRuntime().newArgumentError(e.getMessage());
    } catch (IOException ioe) {
        throw newPKCS7Error(getRuntime(), ioe.getMessage());
    } catch (PKCS7Exception pkcs7e) {
        throw newPKCS7Error(getRuntime(), pkcs7e);
    }
    setData(getRuntime().getNil());
    return this;
}
Also used : ThreadContext(org.jruby.runtime.ThreadContext) MemBIO(org.jruby.ext.openssl.impl.MemBIO) BIO(org.jruby.ext.openssl.impl.BIO) IOException(java.io.IOException) PKCS7Exception(org.jruby.ext.openssl.impl.PKCS7Exception) NotVerifiedPKCS7Exception(org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 20 with ThreadContext

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

the class SSLContext method createSSLContext.

public static void createSSLContext(final Ruby runtime, final RubyModule SSL) {
    // OpenSSL::SSL
    RubyClass SSLContext = SSL.defineClassUnder("SSLContext", runtime.getObject(), SSLCONTEXT_ALLOCATOR);
    final ThreadContext context = runtime.getCurrentContext();
    SSLContext.addReadWriteAttribute(context, "cert");
    SSLContext.addReadWriteAttribute(context, "key");
    SSLContext.addReadWriteAttribute(context, "client_ca");
    SSLContext.addReadWriteAttribute(context, "ca_file");
    SSLContext.addReadWriteAttribute(context, "ca_path");
    SSLContext.addReadWriteAttribute(context, "timeout");
    SSLContext.addReadWriteAttribute(context, "verify_mode");
    SSLContext.addReadWriteAttribute(context, "verify_depth");
    SSLContext.addReadWriteAttribute(context, "verify_callback");
    SSLContext.addReadWriteAttribute(context, "options");
    SSLContext.addReadWriteAttribute(context, "cert_store");
    SSLContext.addReadWriteAttribute(context, "extra_chain_cert");
    SSLContext.addReadWriteAttribute(context, "client_cert_cb");
    SSLContext.addReadWriteAttribute(context, "session_id_context");
    SSLContext.addReadWriteAttribute(context, "tmp_dh_callback");
    SSLContext.addReadWriteAttribute(context, "servername_cb");
    SSLContext.addReadWriteAttribute(context, "renegotiation_cb");
    SSLContext.defineAlias("ssl_timeout", "timeout");
    SSLContext.defineAlias("ssl_timeout=", "timeout=");
    SSLContext.defineAnnotatedMethods(SSLContext.class);
    final Set<String> methodKeys = SSL_VERSION_OSSL2JSSE.keySet();
    final RubyArray methods = runtime.newArray(methodKeys.size());
    for (final String method : methodKeys) {
        if (method.equals("SSLv2") || method.startsWith("SSLv2_")) {
            // do not report SSLv2, SSLv2_server, SSLv2_client
            continue;
        }
        if (method.indexOf('.') == -1) {
            // do not "officially" report TLSv1.1 and TLSv1.2
            methods.append(runtime.newSymbol(method));
        }
    }
    SSLContext.defineConstant("METHODS", methods);
    // in 1.8.7 as well as 1.9.3 :
    // [:TLSv1, :TLSv1_server, :TLSv1_client, :SSLv3, :SSLv3_server, :SSLv3_client, :SSLv23, :SSLv23_server, :SSLv23_client]
    // in 2.0.0 :
    // [:TLSv1, :TLSv1_server, :TLSv1_client, :TLSv1_2, :TLSv1_2_server, :TLSv1_2_client, :TLSv1_1, :TLSv1_1_server,
    // :TLSv1_1_client, :SSLv3, :SSLv3_server, :SSLv3_client, :SSLv23, :SSLv23_server, :SSLv23_client]
    SSLContext.setConstant("SESSION_CACHE_OFF", runtime.newFixnum(SESSION_CACHE_OFF));
    SSLContext.setConstant("SESSION_CACHE_CLIENT", runtime.newFixnum(SESSION_CACHE_CLIENT));
    SSLContext.setConstant("SESSION_CACHE_SERVER", runtime.newFixnum(SESSION_CACHE_SERVER));
    SSLContext.setConstant("SESSION_CACHE_BOTH", runtime.newFixnum(SESSION_CACHE_BOTH));
    SSLContext.setConstant("SESSION_CACHE_NO_AUTO_CLEAR", runtime.newFixnum(SESSION_CACHE_NO_AUTO_CLEAR));
    SSLContext.setConstant("SESSION_CACHE_NO_INTERNAL_LOOKUP", runtime.newFixnum(SESSION_CACHE_NO_INTERNAL_LOOKUP));
    SSLContext.setConstant("SESSION_CACHE_NO_INTERNAL_STORE", runtime.newFixnum(SESSION_CACHE_NO_INTERNAL_STORE));
    SSLContext.setConstant("SESSION_CACHE_NO_INTERNAL", runtime.newFixnum(SESSION_CACHE_NO_INTERNAL));
    // DEFAULT_CERT_STORE = OpenSSL::X509::Store.new
    // DEFAULT_CERT_STORE.set_default_paths
    // if defined?(OpenSSL::X509::V_FLAG_CRL_CHECK_ALL)
    // DEFAULT_CERT_STORE.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
    // end
    final X509Store DEFAULT_CERT_STORE = X509Store.newStore(runtime);
    DEFAULT_CERT_STORE.set_default_paths(context);
    final IRubyObject V_FLAG_CRL_CHECK_ALL = _X509(runtime).getConstantAt("V_FLAG_CRL_CHECK_ALL");
    if (V_FLAG_CRL_CHECK_ALL != null)
        DEFAULT_CERT_STORE.set_flags(V_FLAG_CRL_CHECK_ALL);
    SSLContext.setConstant("DEFAULT_CERT_STORE", DEFAULT_CERT_STORE);
    // DEFAULT_PARAMS = {
    // :ssl_version => "SSLv23",
    // :verify_mode => OpenSSL::SSL::VERIFY_PEER,
    // :ciphers => "ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW",
    // :options => OpenSSL::SSL::OP_ALL,
    // }
    // on MRI 2.1 (should not matter for us) :
    // :options => defined?(OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS) ?
    // OpenSSL::SSL::OP_ALL & ~OpenSSL::SSL::OP_DONT_INSERT_EMPTY_FRAGMENTS :
    // OpenSSL::SSL::OP_ALL
    final RubyHash DEFAULT_PARAMS = new RubyHash(runtime);
    IRubyObject ssl_version = StringHelper.newString(runtime, new byte[] { 'S', 'S', 'L', 'v', '2', '3' });
    DEFAULT_PARAMS.op_aset(context, runtime.newSymbol("ssl_version"), ssl_version);
    IRubyObject verify_mode = runtime.newFixnum(VERIFY_PEER);
    DEFAULT_PARAMS.op_aset(context, runtime.newSymbol("verify_mode"), verify_mode);
    IRubyObject ciphers = StringHelper.newString(runtime, new byte[] { 'A', 'L', 'L', ':', '!', 'A', 'D', 'H', ':', '!', 'E', 'X', 'P', 'O', 'R', 'T', ':', '!', 'S', 'S', 'L', 'v', '2', ':', 'R', 'C', '4', '+', 'R', 'S', 'A', ':', '+', 'H', 'I', 'G', 'H', ':', '+', 'M', 'E', 'D', 'I', 'U', 'M', ':', '+', 'L', 'O', 'W' });
    DEFAULT_PARAMS.op_aset(context, runtime.newSymbol("ciphers"), ciphers);
    IRubyObject options = runtime.newFixnum(OP_ALL);
    DEFAULT_PARAMS.op_aset(context, runtime.newSymbol("options"), options);
    SSLContext.setConstant("DEFAULT_PARAMS", DEFAULT_PARAMS);
}
Also used : RubyArray(org.jruby.RubyArray) RubyHash(org.jruby.RubyHash) ThreadContext(org.jruby.runtime.ThreadContext) RubyClass(org.jruby.RubyClass) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

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