Search in sources :

Example 1 with BIO

use of org.jruby.ext.openssl.impl.BIO in project jruby-openssl by jruby.

the class PKCS7 method add_data.

@JRubyMethod(name = { "add_data", "data=" })
public IRubyObject add_data(IRubyObject obj) {
    if (p7.isSigned()) {
        try {
            p7.contentNew(ASN1Registry.NID_pkcs7_data);
        } catch (PKCS7Exception pkcs7e) {
            throw newPKCS7Error(getRuntime(), pkcs7e);
        }
    }
    BIO in = obj2bio(obj);
    BIO out = null;
    try {
        out = p7.dataInit(null);
    } catch (PKCS7Exception pkcs7e) {
        throw newPKCS7Error(getRuntime(), pkcs7e);
    }
    byte[] buf = new byte[4096];
    for (; ; ) {
        try {
            int i = in.read(buf, 0, buf.length);
            if (i <= 0) {
                break;
            }
            if (out != null) {
                out.write(buf, 0, i);
            }
        } catch (IOException e) {
            throw getRuntime().newIOErrorFromException(e);
        }
    }
    try {
        p7.dataFinal(out);
    } catch (PKCS7Exception pkcs7e) {
        throw newPKCS7Error(getRuntime(), pkcs7e);
    }
    setData(getRuntime().getNil());
    return obj;
}
Also used : 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) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with BIO

use of org.jruby.ext.openssl.impl.BIO in project jruby-openssl by jruby.

the class PKCS7 method sign.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject sign(IRubyObject self, IRubyObject[] args) {
    final Ruby runtime = self.getRuntime();
    final X509Cert cert;
    final PKey key;
    final IRubyObject data;
    IRubyObject certs = runtime.getNil();
    IRubyObject flags = runtime.getNil();
    switch(Arity.checkArgumentCount(runtime, args, 3, 5)) {
        case 5:
            flags = args[4];
        case 4:
            certs = args[3];
        default:
            cert = (X509Cert) args[0];
            key = (PKey) args[1];
            data = args[2];
    }
    X509AuxCertificate auxCert = cert.getAuxCert();
    PrivateKey privKey = key.getPrivateKey();
    final int flg = flags.isNil() ? 0 : RubyNumeric.fix2int(flags);
    final BIO dataBIO = obj2bio(data);
    List<X509AuxCertificate> auxCerts = certs.isNil() ? null : getAuxCerts(certs);
    org.jruby.ext.openssl.impl.PKCS7 pkcs7Impl;
    try {
        pkcs7Impl = org.jruby.ext.openssl.impl.PKCS7.sign(auxCert, privKey, auxCerts, dataBIO, flg);
    } catch (PKCS7Exception e) {
        throw newPKCS7Error(runtime, e);
    }
    final PKCS7 pkcs7 = wrap(runtime, pkcs7Impl);
    pkcs7.setData(data);
    return pkcs7;
}
Also used : ThreadContext(org.jruby.runtime.ThreadContext) PrivateKey(java.security.PrivateKey) MemBIO(org.jruby.ext.openssl.impl.MemBIO) BIO(org.jruby.ext.openssl.impl.BIO) IRubyObject(org.jruby.runtime.builtin.IRubyObject) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) PKCS7Exception(org.jruby.ext.openssl.impl.PKCS7Exception) NotVerifiedPKCS7Exception(org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with BIO

use of org.jruby.ext.openssl.impl.BIO in project jruby-openssl by jruby.

the class PKCS7 method verify.

@JRubyMethod(rest = true)
public IRubyObject verify(IRubyObject[] args) {
    final Ruby runtime = getRuntime();
    IRubyObject certs;
    X509Store store;
    IRubyObject indata = runtime.getNil();
    IRubyObject vflags = runtime.getNil();
    switch(Arity.checkArgumentCount(runtime, args, 2, 4)) {
        case 4:
            vflags = args[3];
        case 3:
            indata = args[2];
        default:
            store = (X509Store) args[1];
            certs = args[0];
    }
    final int flg = vflags.isNil() ? 0 : RubyNumeric.fix2int(vflags);
    if (indata.isNil())
        indata = getData();
    final BIO in = indata.isNil() ? null : obj2bio(indata);
    List<X509AuxCertificate> x509s = certs.isNil() ? null : getAuxCerts(certs);
    final Store storeStr = store.getStore();
    final BIO out = BIO.mem();
    boolean result = false;
    try {
        p7.verify(x509s, storeStr, in, out, flg);
        result = true;
    } catch (NotVerifiedPKCS7Exception e) {
    // result = false;
    } catch (PKCS7Exception pkcs7e) {
        if (isDebug(runtime)) {
            // runtime.getOut().println(pkcs7e);
            pkcs7e.printStackTrace(runtime.getOut());
        }
    // result = false;
    }
    IRubyObject data = membio2str(getRuntime(), out);
    setData(data);
    return result ? runtime.getTrue() : runtime.getFalse();
}
Also used : MemBIO(org.jruby.ext.openssl.impl.MemBIO) BIO(org.jruby.ext.openssl.impl.BIO) Store(org.jruby.ext.openssl.x509store.Store) NotVerifiedPKCS7Exception(org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception) IRubyObject(org.jruby.runtime.builtin.IRubyObject) X509AuxCertificate(org.jruby.ext.openssl.x509store.X509AuxCertificate) PKCS7Exception(org.jruby.ext.openssl.impl.PKCS7Exception) NotVerifiedPKCS7Exception(org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with BIO

use of org.jruby.ext.openssl.impl.BIO in project jruby-openssl by jruby.

the class PKCS7 method read_smime.

@JRubyMethod(meta = true)
public static IRubyObject read_smime(IRubyObject self, IRubyObject arg) {
    final Ruby runtime = self.getRuntime();
    final BIO in = obj2bio(arg);
    final BIO[] out = new BIO[] { null };
    org.jruby.ext.openssl.impl.PKCS7 pkcs7Impl = null;
    try {
        pkcs7Impl = new SMIME(Mime.DEFAULT).readPKCS7(in, out);
    } catch (IOException ioe) {
        throw newPKCS7Error(runtime, ioe.getMessage());
    } catch (PKCS7Exception pkcs7e) {
        throw newPKCS7Error(runtime, pkcs7e);
    }
    if (pkcs7Impl == null) {
        throw newPKCS7Error(runtime, (String) null);
    }
    IRubyObject data = out[0] != null ? membio2str(runtime, out[0]) : runtime.getNil();
    final PKCS7 pkcs7 = wrap(runtime, pkcs7Impl);
    pkcs7.setData(data);
    return pkcs7;
}
Also used : ThreadContext(org.jruby.runtime.ThreadContext) SMIME(org.jruby.ext.openssl.impl.SMIME) 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) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 5 with BIO

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

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)6 BIO (org.jruby.ext.openssl.impl.BIO)6 MemBIO (org.jruby.ext.openssl.impl.MemBIO)6 NotVerifiedPKCS7Exception (org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception)6 PKCS7Exception (org.jruby.ext.openssl.impl.PKCS7Exception)6 IRubyObject (org.jruby.runtime.builtin.IRubyObject)5 IOException (java.io.IOException)3 Ruby (org.jruby.Ruby)3 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)3 ThreadContext (org.jruby.runtime.ThreadContext)3 PrivateKey (java.security.PrivateKey)2 SMIME (org.jruby.ext.openssl.impl.SMIME)1 Store (org.jruby.ext.openssl.x509store.Store)1