Search in sources :

Example 11 with RubyFixnum

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();
}
Also used : IOException(java.io.IOException) RevokedInfo(org.bouncycastle.asn1.ocsp.RevokedInfo) Ruby(org.jruby.Ruby) RubyFixnum(org.jruby.RubyFixnum) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 12 with RubyFixnum

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();
}
Also used : RevokedInfo(org.bouncycastle.asn1.ocsp.RevokedInfo) Ruby(org.jruby.Ruby) RubyFixnum(org.jruby.RubyFixnum) RaiseException(org.jruby.exceptions.RaiseException) ParseException(java.text.ParseException) IOException(java.io.IOException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 13 with RubyFixnum

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));
}
Also used : ASN1GeneralizedTime(org.bouncycastle.asn1.ASN1GeneralizedTime) ParseException(java.text.ParseException) Ruby(org.jruby.Ruby) Date(java.util.Date) RubyFixnum(org.jruby.RubyFixnum) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 14 with RubyFixnum

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);
}
Also used : RubyHash(org.jruby.RubyHash) ThreadContext(org.jruby.runtime.ThreadContext) RubyClass(org.jruby.RubyClass) RubyFixnum(org.jruby.RubyFixnum)

Example 15 with RubyFixnum

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;
}
Also used : PKey.readRSAPrivateKey(org.jruby.ext.openssl.impl.PKey.readRSAPrivateKey) PrivateKey(java.security.PrivateKey) RubyString(org.jruby.RubyString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RSAPublicKey(java.security.interfaces.RSAPublicKey) PKey.readRSAPublicKey(org.jruby.ext.openssl.impl.PKey.readRSAPublicKey) StringReader(java.io.StringReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) Ruby(org.jruby.Ruby) KeyFactory(java.security.KeyFactory) KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PEMInputOutput(org.jruby.ext.openssl.x509store.PEMInputOutput) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) PKey.readRSAPublicKey(org.jruby.ext.openssl.impl.PKey.readRSAPublicKey) RubyString(org.jruby.RubyString) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) RubyFixnum(org.jruby.RubyFixnum) RaiseException(org.jruby.exceptions.RaiseException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BigInteger(java.math.BigInteger) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RubyFixnum (org.jruby.RubyFixnum)15 JRubyMethod (org.jruby.anno.JRubyMethod)12 Ruby (org.jruby.Ruby)11 IOException (java.io.IOException)8 RaiseException (org.jruby.exceptions.RaiseException)7 IRubyObject (org.jruby.runtime.builtin.IRubyObject)6 RubyArray (org.jruby.RubyArray)5 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)4 RubyString (org.jruby.RubyString)4 PublicKey (java.security.PublicKey)3 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)3 StringReader (java.io.StringReader)2 BigInteger (java.math.BigInteger)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 CertificateException (java.security.cert.CertificateException)2 CertificateParsingException (java.security.cert.CertificateParsingException)2 X509Certificate (java.security.cert.X509Certificate)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2