Search in sources :

Example 6 with PKCS7Exception

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

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

the class PKCS7 method add_signer.

@JRubyMethod
public IRubyObject add_signer(IRubyObject obj) {
    SignerInfoWithPkey signedInfo = ((SignerInfo) obj).getSignerInfo().dup();
    try {
        p7.addSigner(signedInfo);
    } catch (PKCS7Exception e) {
        throw newPKCS7Error(getRuntime(), e);
    }
    if (p7.isSigned()) {
        ASN1Encodable objectId = org.jruby.ext.openssl.impl.PKCS7.OID_pkcs7_data;
        signedInfo.addSignedAttribute(ASN1Registry.NID_pkcs9_contentType, objectId);
    }
    return this;
}
Also used : SignerInfoWithPkey(org.jruby.ext.openssl.impl.SignerInfoWithPkey) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) PKCS7Exception(org.jruby.ext.openssl.impl.PKCS7Exception) NotVerifiedPKCS7Exception(org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 8 with PKCS7Exception

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

the class PKCS7 method write_smime.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject write_smime(IRubyObject self, IRubyObject[] args) {
    final Ruby runtime = self.getRuntime();
    final PKCS7 pkcs7;
    IRubyObject data = runtime.getNil();
    IRubyObject flags = runtime.getNil();
    switch(Arity.checkArgumentCount(runtime, args, 1, 3)) {
        case 3:
            flags = args[2];
        case 2:
            data = args[1];
        default:
            pkcs7 = (PKCS7) args[0];
    }
    if (data.isNil())
        data = pkcs7.getData();
    final int flg = flags.isNil() ? 0 : RubyNumeric.fix2int(flags);
    String smime = "";
    try {
        smime = new SMIME().writePKCS7(pkcs7.p7, data.asJavaString(), flg);
    } catch (PKCS7Exception e) {
        throw newPKCS7Error(runtime, e);
    } catch (IOException e) {
        throw newPKCS7Error(runtime, e.getMessage());
    }
    return RubyString.newString(runtime, smime);
}
Also used : SMIME(org.jruby.ext.openssl.impl.SMIME) RubyString(org.jruby.RubyString) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) PKCS7Exception(org.jruby.ext.openssl.impl.PKCS7Exception) NotVerifiedPKCS7Exception(org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 9 with PKCS7Exception

use of org.jruby.ext.openssl.impl.PKCS7Exception 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 10 with PKCS7Exception

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

the class PKCS7 method decrypt.

@JRubyMethod(rest = true)
public IRubyObject decrypt(IRubyObject[] args) {
    IRubyObject dflags;
    if (Arity.checkArgumentCount(getRuntime(), args, 2, 3) == 3) {
        dflags = args[2];
    } else {
        dflags = getRuntime().getNil();
    }
    PKey pkey = (PKey) args[0];
    X509Cert cert = (X509Cert) args[1];
    final PrivateKey privKey = pkey.getPrivateKey();
    final X509AuxCertificate auxCert = cert.getAuxCert();
    final int flg = dflags.isNil() ? 0 : RubyNumeric.fix2int(dflags);
    final BIO out = BIO.mem();
    try {
        p7.decrypt(privKey, auxCert, out, flg);
    } catch (PKCS7Exception pkcs7e) {
        throw newPKCS7Error(getRuntime(), pkcs7e);
    }
    return membio2str(getRuntime(), out);
}
Also used : 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) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)10 NotVerifiedPKCS7Exception (org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception)10 PKCS7Exception (org.jruby.ext.openssl.impl.PKCS7Exception)10 IRubyObject (org.jruby.runtime.builtin.IRubyObject)7 BIO (org.jruby.ext.openssl.impl.BIO)6 MemBIO (org.jruby.ext.openssl.impl.MemBIO)6 Ruby (org.jruby.Ruby)5 IOException (java.io.IOException)4 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)4 ThreadContext (org.jruby.runtime.ThreadContext)4 PrivateKey (java.security.PrivateKey)2 RubyString (org.jruby.RubyString)2 SMIME (org.jruby.ext.openssl.impl.SMIME)2 GeneralSecurityException (java.security.GeneralSecurityException)1 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)1 CipherSpec (org.jruby.ext.openssl.impl.CipherSpec)1 SignerInfoWithPkey (org.jruby.ext.openssl.impl.SignerInfoWithPkey)1 Store (org.jruby.ext.openssl.x509store.Store)1