Search in sources :

Example 16 with X509AuxCertificate

use of org.jruby.ext.openssl.x509store.X509AuxCertificate 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 17 with X509AuxCertificate

use of org.jruby.ext.openssl.x509store.X509AuxCertificate 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)

Aggregations

X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)17 Ruby (org.jruby.Ruby)8 JRubyMethod (org.jruby.anno.JRubyMethod)8 IRubyObject (org.jruby.runtime.builtin.IRubyObject)7 ArrayList (java.util.ArrayList)6 RubyArray (org.jruby.RubyArray)6 IOException (java.io.IOException)5 NotVerifiedPKCS7Exception (org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception)4 PKCS7Exception (org.jruby.ext.openssl.impl.PKCS7Exception)4 ThreadContext (org.jruby.runtime.ThreadContext)4 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)3 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)3 BIO (org.jruby.ext.openssl.impl.BIO)3 MemBIO (org.jruby.ext.openssl.impl.MemBIO)3 GeneralSecurityException (java.security.GeneralSecurityException)2 PrivateKey (java.security.PrivateKey)2 CertificateException (java.security.cert.CertificateException)2 X509Certificate (java.security.cert.X509Certificate)2 Certificate (org.bouncycastle.asn1.x509.Certificate)2 Store (org.jruby.ext.openssl.x509store.Store)2