use of org.jruby.RubyString in project jruby-openssl by jruby.
the class X509ExtensionFactory method create_ext_from_string.
// "oid = critical, value"
@JRubyMethod
public IRubyObject create_ext_from_string(final ThreadContext context, final IRubyObject arg) {
final RubyString str = (RubyString) arg;
final Ruby runtime = context.runtime;
RubyInteger i = str.index19(context, StringHelper.newString(runtime, new byte[] { '=' })).convertToInteger("to_i");
final int ind = (int) i.getLongValue();
RubyString oid = (RubyString) str.substr19(runtime, 0, ind);
oid.strip_bang19(context);
final int len = (int) str.length19().getLongValue() - ind;
RubyString value = (RubyString) str.substr19(runtime, ind + 1, len);
value.lstrip_bang19(context);
IRubyObject critical = context.nil;
if (value.start_with_p(context, StringHelper.newString(runtime, critical__)).isTrue()) {
// value[ 0, 'critical, '.length ] = ''
critical = runtime.newBoolean(true);
value.op_aset19(context, runtime.newFixnum(0), runtime.newFixnum(critical__.length), RubyString.newEmptyString(runtime));
}
value.strip_bang19(context);
return create_ext(context, new IRubyObject[] { oid, value, critical });
}
use of org.jruby.RubyString in project jruby-openssl by jruby.
the class X509Name method add_entry.
@JRubyMethod
public IRubyObject add_entry(final ThreadContext context, final IRubyObject oid, final IRubyObject value, IRubyObject type) {
final Ruby runtime = context.runtime;
final RubyString oidStr = oid.asString();
if (type == null || type.isNil())
type = getDefaultType(context, oidStr);
final ASN1ObjectIdentifier objectId;
try {
objectId = ASN1.getObjectID(runtime, oidStr.toString());
} catch (IllegalArgumentException e) {
throw newNameError(runtime, "invalid field name: " + oidStr, e);
}
// NOTE: won't reach here :
if (objectId == null)
throw newNameError(runtime, "invalid field name");
try {
addEntry(objectId, value.asString(), (RubyInteger) type);
} catch (IOException e) {
throw newNameError(runtime, "invalid value", e);
}
return this;
}
use of org.jruby.RubyString in project jruby-openssl by jruby.
the class PKey method passwordPrompt.
protected static char[] passwordPrompt(final ThreadContext context, final String prompt) {
final RubyModule Kernel = context.runtime.getKernel();
// NOTE: just a fast and simple print && gets - hopefully better than nothing!
Kernel.callMethod("print", context.runtime.newString(prompt));
final RubyString gets = Kernel.callMethod(context, "gets").convertToString();
gets.chomp_bang(context);
return gets.toString().toCharArray();
}
use of org.jruby.RubyString in project jruby-openssl by jruby.
the class PKeyDH method initialize.
@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public synchronized IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;
if (this.dh_p != null || this.dh_g != null || this.dh_y != null || this.dh_x != null) {
throw newDHError(runtime, "illegal initialization");
}
final int argc = Arity.checkArgumentCount(runtime, args, 0, 2);
if (argc > 0) {
IRubyObject arg0 = args[0];
if (argc == 1 && arg0 instanceof RubyString) {
try {
DHParameterSpec spec = PEMInputOutput.readDHParameters(new StringReader(arg0.toString()));
if (spec == null) {
spec = org.jruby.ext.openssl.impl.PKey.readDHParameter(arg0.asString().getByteList().bytes());
}
if (spec == null) {
throw runtime.newArgumentError("invalid DH PARAMETERS");
}
this.dh_p = spec.getP();
this.dh_g = spec.getG();
} catch (NoClassDefFoundError e) {
throw newDHError(runtime, bcExceptionMessage(e));
} catch (IOException e) {
throw runtime.newIOErrorFromException(e);
}
} else {
int bits = RubyNumeric.fix2int(arg0);
// g defaults to 2
int gval = argc == 2 ? RubyNumeric.fix2int(args[1]) : 2;
BigInteger p;
try {
p = generateP(bits, gval);
} catch (IllegalArgumentException e) {
throw runtime.newArgumentError(e.getMessage());
}
BigInteger g = BigInteger.valueOf(gval);
BigInteger x = generateX(p);
BigInteger y = generateY(p, g, x);
this.dh_p = p;
this.dh_g = g;
// private key
this.dh_x = x;
// public key
this.dh_y = y;
}
}
return this;
}
use of org.jruby.RubyString in project jruby-openssl by jruby.
the class PKeyRSA method initialize.
@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;
if (Arity.checkArgumentCount(runtime, args, 0, 2) == 0) {
privateKey = null;
publicKey = null;
return this;
}
IRubyObject arg = args[0];
IRubyObject pass = null;
if (args.length > 1)
pass = args[1];
if (arg instanceof RubyFixnum) {
int keySize = RubyNumeric.fix2int((RubyFixnum) arg);
BigInteger exp = RSAKeyGenParameterSpec.F4;
if (pass != null && !pass.isNil()) {
exp = BigInteger.valueOf(RubyNumeric.num2long(pass));
}
return rsaGenerate(runtime, this, keySize, exp);
}
final char[] passwd = password(pass);
final RubyString str = readInitArg(context, arg);
final String strJava = str.toString();
Object key = null;
final KeyFactory rsaFactory;
try {
rsaFactory = SecurityHelper.getKeyFactory("RSA");
} catch (NoSuchAlgorithmException e) {
throw runtime.newRuntimeError("unsupported key algorithm (RSA)");
} catch (RuntimeException e) {
throw runtime.newRuntimeError("unsupported key algorithm (RSA) " + e);
}
// TODO: ugly NoClassDefFoundError catching for no BC env. How can we remove this?
boolean noClassDef = false;
if (key == null && !noClassDef) {
// PEM_read_bio_RSAPrivateKey
try {
key = readPrivateKey(strJava, passwd);
} catch (NoClassDefFoundError e) {
noClassDef = true;
debugStackTrace(runtime, e);
} catch (PEMInputOutput.PasswordRequiredException retry) {
if (ttySTDIN(context)) {
try {
key = readPrivateKey(strJava, passwordPrompt(context));
} catch (Exception e) {
debugStackTrace(runtime, e);
}
}
} catch (Exception e) {
debugStackTrace(runtime, e);
}
}
if (key == null && !noClassDef) {
// PEM_read_bio_RSAPublicKey
try {
key = PEMInputOutput.readRSAPublicKey(new StringReader(strJava), passwd);
} catch (NoClassDefFoundError e) {
noClassDef = true;
debugStackTrace(runtime, e);
} catch (Exception e) {
debugStackTrace(runtime, e);
}
}
if (key == null && !noClassDef) {
// PEM_read_bio_RSA_PUBKEY
try {
key = PEMInputOutput.readRSAPubKey(new StringReader(strJava));
} catch (NoClassDefFoundError e) {
noClassDef = true;
debugStackTrace(runtime, e);
} catch (Exception e) {
debugStackTrace(runtime, e);
}
}
if (key == null && !noClassDef) {
// d2i_RSAPrivateKey_bio
try {
key = readRSAPrivateKey(rsaFactory, str.getBytes());
} catch (NoClassDefFoundError e) {
noClassDef = true;
debugStackTrace(runtime, e);
} catch (InvalidKeySpecException e) {
debug(runtime, "PKeyRSA could not read private key", e);
} catch (IOException e) {
debug(runtime, "PKeyRSA could not read private key", e);
} catch (RuntimeException e) {
if (isKeyGenerationFailure(e))
debug(runtime, "PKeyRSA could not read private key", e);
else
debugStackTrace(runtime, e);
}
}
if (key == null && !noClassDef) {
// d2i_RSAPublicKey_bio
try {
key = readRSAPublicKey(rsaFactory, str.getBytes());
} catch (NoClassDefFoundError e) {
noClassDef = true;
debugStackTrace(runtime, e);
} catch (InvalidKeySpecException e) {
debug(runtime, "PKeyRSA could not read public key", e);
} catch (IOException e) {
debug(runtime, "PKeyRSA could not read public key", e);
} catch (RuntimeException e) {
if (isKeyGenerationFailure(e))
debug(runtime, "PKeyRSA could not read public key", e);
else
debugStackTrace(runtime, e);
}
}
if (key == null)
key = tryPKCS8EncodedKey(runtime, rsaFactory, str.getBytes());
if (key == null)
key = tryX509EncodedKey(runtime, rsaFactory, str.getBytes());
if (key == null)
throw newRSAError(runtime, "Neither PUB key nor PRIV key:");
if (key instanceof KeyPair) {
PublicKey publicKey = ((KeyPair) key).getPublic();
PrivateKey privateKey = ((KeyPair) key).getPrivate();
if (!(privateKey instanceof RSAPrivateCrtKey)) {
if (privateKey == null) {
throw newRSAError(runtime, "Neither PUB key nor PRIV key: (private key is null)");
}
throw newRSAError(runtime, "Neither PUB key nor PRIV key: (invalid key type " + privateKey.getClass().getName() + ")");
}
this.privateKey = (RSAPrivateCrtKey) privateKey;
this.publicKey = (RSAPublicKey) publicKey;
} else if (key instanceof RSAPrivateCrtKey) {
this.privateKey = (RSAPrivateCrtKey) key;
try {
this.publicKey = (RSAPublicKey) rsaFactory.generatePublic(new RSAPublicKeySpec(privateKey.getModulus(), privateKey.getPublicExponent()));
} catch (GeneralSecurityException e) {
throw newRSAError(runtime, e.getMessage());
} catch (RuntimeException e) {
debugStackTrace(runtime, e);
throw newRSAError(runtime, e.toString());
}
} else if (key instanceof RSAPublicKey) {
this.publicKey = (RSAPublicKey) key;
this.privateKey = null;
} else {
throw newRSAError(runtime, "Neither PUB key nor PRIV key: " + key.getClass().getName());
}
return this;
}
Aggregations