Search in sources :

Example 1 with CipherSpec

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

the class PKCS7 method encrypt.

/**
 * ossl_pkcs7_s_encrypt
 */
@JRubyMethod(meta = true, rest = true)
public static IRubyObject encrypt(IRubyObject self, IRubyObject[] args) {
    final Ruby runtime = self.getRuntime();
    IRubyObject certs, data, cipher = runtime.getNil(), flags = runtime.getNil();
    switch(Arity.checkArgumentCount(self.getRuntime(), args, 2, 4)) {
        case 4:
            flags = args[3];
        case 3:
            cipher = args[2];
    }
    data = args[1];
    certs = args[0];
    CipherSpec cipherSpec = null;
    if (cipher.isNil()) {
        try {
            javax.crypto.Cipher c = SecurityHelper.getCipher("RC2/CBC/PKCS5Padding");
            cipherSpec = new CipherSpec(c, Cipher.Algorithm.javaToOssl("RC2/CBC/PKCS5Padding", 40), 40);
        } catch (GeneralSecurityException e) {
            throw newPKCS7Error(runtime, e);
        }
    } else {
        final Cipher c = (Cipher) cipher;
        cipherSpec = new CipherSpec(c.getCipherInstance(), c.getName(), c.getGenerateKeyLength() * 8);
    }
    final int flg = flags.isNil() ? 0 : RubyNumeric.fix2int(flags);
    final List<X509AuxCertificate> auxCerts = getAuxCerts(certs);
    final byte[] dataBytes = data.asString().getBytes();
    org.jruby.ext.openssl.impl.PKCS7 pkcs7Impl;
    try {
        pkcs7Impl = org.jruby.ext.openssl.impl.PKCS7.encrypt(auxCerts, dataBytes, cipherSpec, flg);
    } catch (PKCS7Exception pkcs7e) {
        throw newPKCS7Error(self.getRuntime(), pkcs7e);
    }
    final PKCS7 pkcs7 = wrap(runtime, pkcs7Impl);
    pkcs7.setData(data);
    return pkcs7;
}
Also used : ThreadContext(org.jruby.runtime.ThreadContext) GeneralSecurityException(java.security.GeneralSecurityException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) CipherSpec(org.jruby.ext.openssl.impl.CipherSpec) 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 2 with CipherSpec

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

the class PKeyEC method to_pem.

@Override
@JRubyMethod(name = "to_pem", alias = "export", rest = true)
public RubyString to_pem(final IRubyObject[] args) {
    Arity.checkArgumentCount(getRuntime(), args, 0, 2);
    CipherSpec spec = null;
    char[] passwd = null;
    if (args.length > 0) {
        spec = cipherSpec(args[0]);
        if (args.length > 1)
            passwd = password(args[1]);
    }
    try {
        final StringWriter writer = new StringWriter();
        if (privateKey != null) {
            PEMInputOutput.writeECPrivateKey(writer, (ECPrivateKey) privateKey, spec, passwd);
        } else {
            PEMInputOutput.writeECPublicKey(writer, publicKey);
        }
        return RubyString.newString(getRuntime(), writer.getBuffer());
    } catch (IOException ex) {
        throw newECError(getRuntime(), ex.getMessage());
    }
}
Also used : StringWriter(java.io.StringWriter) CipherSpec(org.jruby.ext.openssl.impl.CipherSpec) IOException(java.io.IOException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with CipherSpec

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

the class PKeyDSA method to_pem.

@Override
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
public RubyString to_pem(final IRubyObject[] args) {
    Arity.checkArgumentCount(getRuntime(), args, 0, 2);
    CipherSpec spec = null;
    char[] passwd = null;
    if (args.length > 0) {
        spec = cipherSpec(args[0]);
        if (args.length > 1)
            passwd = password(args[1]);
    }
    try {
        final StringWriter writer = new StringWriter();
        if (privateKey != null) {
            PEMInputOutput.writeDSAPrivateKey(writer, privateKey, spec, passwd);
        } else {
            PEMInputOutput.writeDSAPublicKey(writer, publicKey);
        }
        return RubyString.newString(getRuntime(), writer.getBuffer());
    } catch (NoClassDefFoundError ncdfe) {
        throw newDSAError(getRuntime(), bcExceptionMessage(ncdfe));
    } catch (IOException e) {
        throw newDSAError(getRuntime(), e.getMessage(), e);
    }
}
Also used : StringWriter(java.io.StringWriter) CipherSpec(org.jruby.ext.openssl.impl.CipherSpec) IOException(java.io.IOException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with CipherSpec

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

the class PKeyRSA method to_pem.

@Override
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
public RubyString to_pem(final IRubyObject[] args) {
    Arity.checkArgumentCount(getRuntime(), args, 0, 2);
    CipherSpec spec = null;
    char[] passwd = null;
    if (args.length > 0) {
        spec = cipherSpec(args[0]);
        if (args.length > 1)
            passwd = password(args[1]);
    }
    try {
        final StringWriter writer = new StringWriter();
        if (privateKey != null) {
            PEMInputOutput.writeRSAPrivateKey(writer, privateKey, spec, passwd);
        } else {
            PEMInputOutput.writeRSAPublicKey(writer, publicKey);
        }
        return RubyString.newString(getRuntime(), writer.getBuffer());
    } catch (NoClassDefFoundError ncdfe) {
        throw newRSAError(getRuntime(), bcExceptionMessage(ncdfe));
    } catch (IOException ioe) {
        throw newRSAError(getRuntime(), ioe.getMessage());
    }
}
Also used : StringWriter(java.io.StringWriter) CipherSpec(org.jruby.ext.openssl.impl.CipherSpec) IOException(java.io.IOException) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)4 CipherSpec (org.jruby.ext.openssl.impl.CipherSpec)4 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 GeneralSecurityException (java.security.GeneralSecurityException)1 Ruby (org.jruby.Ruby)1 NotVerifiedPKCS7Exception (org.jruby.ext.openssl.impl.NotVerifiedPKCS7Exception)1 PKCS7Exception (org.jruby.ext.openssl.impl.PKCS7Exception)1 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)1 ThreadContext (org.jruby.runtime.ThreadContext)1 IRubyObject (org.jruby.runtime.builtin.IRubyObject)1