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;
}
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());
}
}
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);
}
}
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());
}
}
Aggregations