Search in sources :

Example 61 with RubyArray

use of org.jruby.RubyArray 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)

Example 62 with RubyArray

use of org.jruby.RubyArray in project jruby-openssl by jruby.

the class X509StoreContext method initialize.

@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
    X509Store store;
    IRubyObject cert, chain;
    cert = chain = context.nil;
    store = (X509Store) args[0];
    if (Arity.checkArgumentCount(context.runtime, args, 1, 3) > 1) {
        cert = args[1];
        if (args.length > 2)
            chain = args[2];
    }
    final X509AuxCertificate _cert;
    if (cert.isNil()) {
        _cert = null;
    } else {
        if (!(cert instanceof X509Cert)) {
            throw context.runtime.newTypeError(cert, "OpenSSL::X509::Certificate");
        }
        _cert = ((X509Cert) cert).getAuxCert();
    }
    final List<X509AuxCertificate> _chain;
    if (!chain.isNil()) {
        @SuppressWarnings("unchecked") final RubyArray certs = (RubyArray) chain;
        _chain = new ArrayList<X509AuxCertificate>(certs.size());
        for (int i = 0; i < certs.size(); i++) {
            // NOTE: if we use the normal java syntax for iterating over this
            // RubyArray, the `toJava` method of the X509Cert class will be
            // implicitly called, and that will return the BC certificate object
            // rather than the JRuby one.
            X509Cert c = (X509Cert) certs.eltOk(i);
            _chain.add(c.getAuxCert());
        }
    } else {
        _chain = new ArrayList<X509AuxCertificate>(4);
    }
    this.storeContext = new StoreContext(store.getStore());
    if (storeContext.init(_cert, _chain) != 1) {
        throw newStoreError(context.runtime, null);
    }
    IRubyObject time = store.getInstanceVariables().getInstanceVariable("@time");
    if (!time.isNil())
        set_time(time);
    this.setInstanceVariable("@verify_callback", store.verify_callback());
    this.setInstanceVariable("@cert", cert);
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) IRubyObject(org.jruby.runtime.builtin.IRubyObject) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) StoreContext(org.jruby.ext.openssl.x509store.StoreContext) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 63 with RubyArray

use of org.jruby.RubyArray in project jruby-openssl by jruby.

the class X509StoreContext method chain.

@JRubyMethod
public IRubyObject chain(final ThreadContext context) {
    final Ruby runtime = context.runtime;
    final List<X509AuxCertificate> chain = storeContext.getChain();
    if (chain == null)
        return runtime.getNil();
    final RubyArray result = runtime.newArray(chain.size());
    final RubyClass _Certificate = _Certificate(runtime);
    try {
        for (X509AuxCertificate x509 : chain) {
            RubyString encoded = StringHelper.newString(runtime, x509.getEncoded());
            result.append(_Certificate.callMethod(context, "new", encoded));
        }
    } catch (CertificateEncodingException e) {
        throw newStoreError(runtime, e.getMessage());
    }
    return result;
}
Also used : RubyArray(org.jruby.RubyArray) RubyString(org.jruby.RubyString) RubyClass(org.jruby.RubyClass) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 64 with RubyArray

use of org.jruby.RubyArray in project nokogiri by sparklemotion.

the class XmlSchema method validate_document_or_file.

IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
    RubyArray<?> errors = (RubyArray) this.getInstanceVariable("@errors");
    ErrorHandler errorHandler = new SchemaErrorHandler(context.runtime, errors);
    setErrorHandler(errorHandler);
    try {
        validate(xmlDocument.getDocument());
    } catch (SAXException ex) {
        XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
        xmlSyntaxError.setException(ex);
        errors.append(xmlSyntaxError);
    } catch (IOException ex) {
        throw context.runtime.newIOError(ex.getMessage());
    }
    return errors;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler) RubyArray(org.jruby.RubyArray) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 65 with RubyArray

use of org.jruby.RubyArray in project nokogiri by sparklemotion.

the class XsltStylesheet method createDocumentFromString.

private IRubyObject createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult) {
    IRubyObject[] args = new IRubyObject[4];
    args[0] = stringOrBlank(runtime, stringResult);
    // url
    args[1] = runtime.getNil();
    // encoding
    args[2] = runtime.getNil();
    RubyClass parse_options = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
    if (htmlish) {
        args[3] = parse_options.getConstant("DEFAULT_HTML");
        RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML4::Document");
        return Helpers.invoke(context, htmlDocumentClass, "parse", args);
    } else {
        args[3] = parse_options.getConstant("DEFAULT_XML");
        RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        XmlDocument xmlDocument = (XmlDocument) Helpers.invoke(context, xmlDocumentClass, "parse", args);
        if (((Document) xmlDocument.getNode()).getDocumentElement() == null) {
            RubyArray<?> errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
            Helpers.invoke(context, errors, "<<", args[0]);
        }
        return xmlDocument;
    }
}
Also used : RubyArray(org.jruby.RubyArray) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Aggregations

RubyArray (org.jruby.RubyArray)65 JRubyMethod (org.jruby.anno.JRubyMethod)34 Ruby (org.jruby.Ruby)26 IRubyObject (org.jruby.runtime.builtin.IRubyObject)26 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)13 RubyString (org.jruby.RubyString)13 IOException (java.io.IOException)11 RaiseException (org.jruby.exceptions.RaiseException)10 ArrayList (java.util.ArrayList)8 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)8 RubyClass (org.jruby.RubyClass)6 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)5 RubyFixnum (org.jruby.RubyFixnum)5 ThreadContext (org.jruby.runtime.ThreadContext)5 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ASN1String (org.bouncycastle.asn1.ASN1String)4 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)4 RubyModule (org.jruby.RubyModule)4