use of org.jruby.RubyFixnum in project jruby-openssl by jruby.
the class OCSPSingleResponse method revocation_reason.
@JRubyMethod(name = "revocation_reason")
public IRubyObject revocation_reason() {
Ruby runtime = getRuntime();
RubyFixnum revoked = (RubyFixnum) _OCSP(runtime).getConstant("V_CERTSTATUS_REVOKED");
if (bcSingleResponse.getCertStatus().getTagNo() == (int) revoked.getLongValue()) {
try {
RevokedInfo revokedInfo = RevokedInfo.getInstance(DERTaggedObject.fromByteArray(bcSingleResponse.getCertStatus().getStatus().toASN1Primitive().getEncoded()));
return RubyFixnum.newFixnum(runtime, revokedInfo.getRevocationReason().getValue().intValue());
} catch (IOException e) {
throw newOCSPError(runtime, e);
}
}
return runtime.getNil();
}
use of org.jruby.RubyFixnum in project jruby-openssl by jruby.
the class OCSPSingleResponse method revocation_time.
@JRubyMethod(name = "revocation_time")
public IRubyObject revocation_time() {
Ruby runtime = getRuntime();
RubyFixnum revoked = (RubyFixnum) _OCSP(runtime).getConstant("V_CERTSTATUS_REVOKED");
if (bcSingleResponse.getCertStatus().getTagNo() == (int) revoked.getLongValue()) {
try {
RevokedInfo revokedInfo = RevokedInfo.getInstance(DERTaggedObject.fromByteArray(bcSingleResponse.getCertStatus().getStatus().toASN1Primitive().getEncoded()));
return RubyTime.newTime(runtime, revokedInfo.getRevocationTime().getDate().getTime());
} catch (Exception e) {
throw newOCSPError(runtime, e);
}
}
return runtime.getNil();
}
use of org.jruby.RubyFixnum in project jruby-openssl by jruby.
the class OCSPSingleResponse method check_validity.
@JRubyMethod(name = "check_validity", rest = true)
public IRubyObject check_validity(IRubyObject[] args) {
Ruby runtime = getRuntime();
int nsec, maxsec;
Date thisUpdate, nextUpdate;
if (Arity.checkArgumentCount(runtime, args, 0, 2) == 0) {
nsec = 0;
maxsec = -1;
} else if (Arity.checkArgumentCount(runtime, args, 0, 2) == 1) {
RubyFixnum rNsec = (RubyFixnum) args[0];
nsec = (int) rNsec.getLongValue();
maxsec = -1;
} else {
RubyFixnum rNsec = (RubyFixnum) args[0];
RubyFixnum rMaxsec = (RubyFixnum) args[1];
nsec = (int) rNsec.getLongValue();
maxsec = (int) rMaxsec.getLongValue();
}
try {
ASN1GeneralizedTime bcThisUpdate = bcSingleResponse.getThisUpdate();
if (bcThisUpdate == null) {
thisUpdate = null;
} else {
thisUpdate = bcThisUpdate.getDate();
}
ASN1GeneralizedTime bcNextUpdate = bcSingleResponse.getNextUpdate();
if (bcNextUpdate == null) {
nextUpdate = null;
} else {
nextUpdate = bcNextUpdate.getDate();
}
} catch (ParseException e) {
throw newOCSPError(runtime, e);
}
return RubyBoolean.newBoolean(runtime, checkValidityImpl(thisUpdate, nextUpdate, nsec, maxsec));
}
use of org.jruby.RubyFixnum in project jruby-openssl by jruby.
the class X509Name method createX509Name.
public static void createX509Name(final Ruby runtime, final RubyModule _X509) {
RubyClass _Name = _X509.defineClassUnder("Name", runtime.getObject(), X509NAME_ALLOCATOR);
RubyClass _OpenSSLError = runtime.getModule("OpenSSL").getClass("OpenSSLError");
_X509.defineClassUnder("NameError", _OpenSSLError, _OpenSSLError.getAllocator());
_Name.defineAnnotatedMethods(X509Name.class);
_Name.includeModule(runtime.getComparable());
_Name.setConstant("COMPAT", runtime.newFixnum(COMPAT));
_Name.setConstant("RFC2253", runtime.newFixnum(RFC2253));
_Name.setConstant("ONELINE", runtime.newFixnum(ONELINE));
_Name.setConstant("MULTILINE", runtime.newFixnum(MULTILINE));
final RubyFixnum UTF8_STRING = runtime.newFixnum(BERTags.UTF8_STRING);
_Name.setConstant("DEFAULT_OBJECT_TYPE", UTF8_STRING);
final RubyFixnum PRINTABLE_STRING = runtime.newFixnum(BERTags.PRINTABLE_STRING);
final RubyFixnum IA5_STRING = runtime.newFixnum(BERTags.IA5_STRING);
final ThreadContext context = runtime.getCurrentContext();
final RubyHash hash = new RubyHash(runtime, UTF8_STRING);
hash.op_aset(context, newString(runtime, new byte[] { 'C' }), PRINTABLE_STRING);
final byte[] countryName = { 'c', 'o', 'u', 'n', 't', 'r', 'y', 'N', 'a', 'm', 'e' };
hash.op_aset(context, newString(runtime, countryName), PRINTABLE_STRING);
final byte[] serialNumber = { 's', 'e', 'r', 'i', 'a', 'l', 'N', 'u', 'm', 'b', 'e', 'r' };
hash.op_aset(context, newString(runtime, serialNumber), PRINTABLE_STRING);
final byte[] dnQualifier = { 'd', 'n', 'Q', 'u', 'a', 'l', 'i', 'f', 'i', 'e', 'r' };
hash.op_aset(context, newString(runtime, dnQualifier), PRINTABLE_STRING);
hash.op_aset(context, newString(runtime, new byte[] { 'D', 'C' }), IA5_STRING);
final byte[] domainComponent = { 'd', 'o', 'm', 'a', 'i', 'n', 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n', 't' };
hash.op_aset(context, newString(runtime, domainComponent), IA5_STRING);
final byte[] emailAddress = { 'e', 'm', 'a', 'i', 'l', 'A', 'd', 'd', 'r', 'e', 's', 's' };
hash.op_aset(context, newString(runtime, emailAddress), IA5_STRING);
_Name.setConstant("OBJECT_TYPE_TEMPLATE", hash);
}
use of org.jruby.RubyFixnum 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