Search in sources :

Example 1 with RubyFixnum

use of org.jruby.RubyFixnum in project gocd by gocd.

the class XmlNode method isErrorIncreased.

private boolean isErrorIncreased(RubyArray baseErrors, RubyArray createdErrors) {
    RubyFixnum length = ((RubyArray) createdErrors.op_diff(baseErrors)).length();
    int diff_in_length = (Integer) length.toJava(Integer.class);
    return diff_in_length > 0;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) RubyFixnum(org.jruby.RubyFixnum)

Example 2 with RubyFixnum

use of org.jruby.RubyFixnum in project propane by ruby-processing.

the class Vec3 method toVertexUV.

/**
 * Sends this Vec3D as a processing vertex uv
 *
 * @param context ThreadContext
 * @param args IRubyObject[]
 */
@JRubyMethod(name = "to_vertex_uv", rest = true)
public void toVertexUV(ThreadContext context, IRubyObject... args) {
    int count = args.length;
    double u = 0;
    double v = 0;
    if (count == 3) {
        u = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
        v = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
    }
    if (count == 2) {
        Vec2 texture = (Vec2) args[1].toJava(Vec2.class);
        u = texture.javax();
        v = texture.javay();
    }
    JRender renderer = (JRender) args[0].toJava(JRender.class);
    renderer.vertex(jx, jy, jz, u, v);
}
Also used : Vec2(monkstone.vecmath.vec2.Vec2) RubyFloat(org.jruby.RubyFloat) JRender(monkstone.vecmath.JRender) RubyFixnum(org.jruby.RubyFixnum) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with RubyFixnum

use of org.jruby.RubyFixnum in project jruby-openssl by jruby.

the class PKeyDSA 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) {
        this.privateKey = null;
        this.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);
        return dsaGenerate(context.runtime, this, keySize);
    }
    final char[] passwd = password(pass);
    final RubyString str = readInitArg(context, arg);
    final String strJava = str.toString();
    Object key = null;
    final KeyFactory dsaFactory;
    try {
        dsaFactory = SecurityHelper.getKeyFactory("DSA");
    } catch (NoSuchAlgorithmException e) {
        throw runtime.newRuntimeError("unsupported key algorithm (DSA)");
    } catch (RuntimeException e) {
        throw runtime.newRuntimeError("unsupported key algorithm (DSA) " + e);
    }
    // TODO: ugly NoClassDefFoundError catching for no BC env. How can we remove this?
    boolean noClassDef = false;
    if (key == null && !noClassDef) {
        // PEM_read_bio_DSAPrivateKey
        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_DSAPublicKey
        try {
            key = PEMInputOutput.readDSAPublicKey(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_DSA_PUBKEY
        try {
            key = PEMInputOutput.readDSAPubKey(new StringReader(strJava));
        } catch (NoClassDefFoundError e) {
            noClassDef = true;
            debugStackTrace(runtime, e);
        } catch (Exception e) {
            debugStackTrace(runtime, e);
        }
    }
    if (key == null && !noClassDef) {
        // d2i_DSAPrivateKey_bio
        try {
            key = readDSAPrivateKey(dsaFactory, str.getBytes());
        } catch (NoClassDefFoundError e) {
            noClassDef = true;
            debugStackTrace(runtime, e);
        } catch (InvalidKeySpecException e) {
            debug(runtime, "PKeyDSA could not read private key", e);
        } catch (IOException e) {
            debug(runtime, "PKeyDSA could not read private key", e);
        } catch (RuntimeException e) {
            if (isKeyGenerationFailure(e))
                debug(runtime, "PKeyDSA could not read private key", e);
            else
                debugStackTrace(runtime, e);
        }
    }
    if (key == null && !noClassDef) {
        // d2i_DSA_PUBKEY_bio
        try {
            key = readDSAPublicKey(dsaFactory, str.getBytes());
        } catch (NoClassDefFoundError e) {
            noClassDef = true;
            debugStackTrace(runtime, e);
        } catch (InvalidKeySpecException e) {
            debug(runtime, "PKeyDSA could not read public key", e);
        } catch (IOException e) {
            debug(runtime, "PKeyDSA could not read public key", e);
        } catch (RuntimeException e) {
            if (isKeyGenerationFailure(e))
                debug(runtime, "PKeyDSA could not read public key", e);
            else
                debugStackTrace(runtime, e);
        }
    }
    if (key == null)
        key = tryPKCS8EncodedKey(runtime, dsaFactory, str.getBytes());
    if (key == null)
        key = tryX509EncodedKey(runtime, dsaFactory, str.getBytes());
    if (key == null)
        throw newDSAError(runtime, "Neither PUB key nor PRIV key:");
    if (key instanceof KeyPair) {
        final PublicKey pubKey = ((KeyPair) key).getPublic();
        final PrivateKey privKey = ((KeyPair) key).getPrivate();
        if (!(privKey instanceof DSAPrivateKey)) {
            if (privKey == null) {
                throw newDSAError(runtime, "Neither PUB key nor PRIV key: (private key is null)");
            }
            throw newDSAError(runtime, "Neither PUB key nor PRIV key: (invalid key type " + privKey.getClass().getName() + ")");
        }
        this.privateKey = (DSAPrivateKey) privKey;
        this.publicKey = (DSAPublicKey) pubKey;
    } else if (key instanceof DSAPrivateKey) {
        this.privateKey = (DSAPrivateKey) key;
    } else if (key instanceof DSAPublicKey) {
        this.publicKey = (DSAPublicKey) key;
        this.privateKey = null;
    } else {
        throw newDSAError(runtime, "Neither PUB key nor PRIV key: " + key.getClass().getName());
    }
    return this;
}
Also used : PKey.readDSAPrivateKey(org.jruby.ext.openssl.impl.PKey.readDSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PEMInputOutput(org.jruby.ext.openssl.x509store.PEMInputOutput) PKey.readDSAPublicKey(org.jruby.ext.openssl.impl.PKey.readDSAPublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RubyString(org.jruby.RubyString) RubyString(org.jruby.RubyString) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RubyFixnum(org.jruby.RubyFixnum) RaiseException(org.jruby.exceptions.RaiseException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) PKey.readDSAPublicKey(org.jruby.ext.openssl.impl.PKey.readDSAPublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) StringReader(java.io.StringReader) PKey.readDSAPrivateKey(org.jruby.ext.openssl.impl.PKey.readDSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) IRubyObject(org.jruby.runtime.builtin.IRubyObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with RubyFixnum

use of org.jruby.RubyFixnum in project jruby-openssl by jruby.

the class PKeyRSA method generate.

@JRubyMethod(name = "generate", meta = true, rest = true)
public static IRubyObject generate(IRubyObject self, IRubyObject[] args) {
    final Ruby runtime = self.getRuntime();
    BigInteger exp = RSAKeyGenParameterSpec.F4;
    if (Arity.checkArgumentCount(runtime, args, 1, 2) == 2) {
        if (args[1] instanceof RubyFixnum) {
            exp = BigInteger.valueOf(RubyNumeric.num2long(args[1]));
        } else {
            exp = ((RubyBignum) args[1]).getValue();
        }
    }
    final int keySize = RubyNumeric.fix2int(args[0]);
    return rsaGenerate(runtime, new PKeyRSA(runtime, (RubyClass) self), keySize, exp);
}
Also used : BigInteger(java.math.BigInteger) RubyClass(org.jruby.RubyClass) Ruby(org.jruby.Ruby) RubyFixnum(org.jruby.RubyFixnum) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 5 with RubyFixnum

use of org.jruby.RubyFixnum in project jruby-openssl by jruby.

the class X509 method createX509.

public static void createX509(final Ruby runtime, final RubyModule _OpenSSL) {
    final RubyModule _X509 = _OpenSSL.defineModuleUnder("X509");
    X509Name.createX509Name(runtime, _X509);
    X509Cert.createX509Cert(runtime, _X509);
    X509Extension.createX509Extension(runtime, _X509);
    X509CRL.createX509CRL(runtime, _X509);
    X509Revoked.createX509Revoked(runtime, _X509);
    X509Store.createX509Store(runtime, _X509);
    X509Request.createRequest(runtime, _X509);
    X509Attribute.createAttribute(runtime, _X509);
    final RubyFixnum _1 = runtime.newFixnum(1);
    final RubyFixnum _2 = runtime.newFixnum(2);
    final RubyFixnum _3 = runtime.newFixnum(3);
    final RubyFixnum _4 = runtime.newFixnum(4);
    final RubyFixnum _5 = runtime.newFixnum(5);
    final RubyFixnum _6 = runtime.newFixnum(6);
    final RubyFixnum _7 = runtime.newFixnum(7);
    final RubyFixnum _8 = runtime.newFixnum(8);
    _X509.setConstant("V_OK", runtime.newFixnum(0));
    _X509.setConstant("V_ERR_UNABLE_TO_GET_ISSUER_CERT", _2);
    _X509.setConstant("V_ERR_UNABLE_TO_GET_CRL", _3);
    _X509.setConstant("V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE", _4);
    _X509.setConstant("V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE", _5);
    _X509.setConstant("V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY", _6);
    _X509.setConstant("V_ERR_CERT_SIGNATURE_FAILURE", _7);
    _X509.setConstant("V_ERR_CRL_SIGNATURE_FAILURE", _8);
    _X509.setConstant("V_ERR_CERT_NOT_YET_VALID", runtime.newFixnum(9));
    _X509.setConstant("V_ERR_CERT_HAS_EXPIRED", runtime.newFixnum(10));
    _X509.setConstant("V_ERR_CRL_NOT_YET_VALID", runtime.newFixnum(11));
    _X509.setConstant("V_ERR_CRL_HAS_EXPIRED", runtime.newFixnum(12));
    _X509.setConstant("V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD", runtime.newFixnum(13));
    _X509.setConstant("V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD", runtime.newFixnum(14));
    _X509.setConstant("V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD", runtime.newFixnum(15));
    _X509.setConstant("V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD", runtime.newFixnum(16));
    _X509.setConstant("V_ERR_OUT_OF_MEM", runtime.newFixnum(17));
    _X509.setConstant("V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT", runtime.newFixnum(18));
    _X509.setConstant("V_ERR_SELF_SIGNED_CERT_IN_CHAIN", runtime.newFixnum(19));
    _X509.setConstant("V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY", runtime.newFixnum(20));
    _X509.setConstant("V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE", runtime.newFixnum(21));
    _X509.setConstant("V_ERR_CERT_CHAIN_TOO_LONG", runtime.newFixnum(22));
    _X509.setConstant("V_ERR_CERT_REVOKED", runtime.newFixnum(23));
    _X509.setConstant("V_ERR_INVALID_CA", runtime.newFixnum(24));
    _X509.setConstant("V_ERR_PATH_LENGTH_EXCEEDED", runtime.newFixnum(25));
    _X509.setConstant("V_ERR_INVALID_PURPOSE", runtime.newFixnum(26));
    _X509.setConstant("V_ERR_CERT_UNTRUSTED", runtime.newFixnum(27));
    _X509.setConstant("V_ERR_CERT_REJECTED", runtime.newFixnum(28));
    _X509.setConstant("V_ERR_SUBJECT_ISSUER_MISMATCH", runtime.newFixnum(29));
    _X509.setConstant("V_ERR_AKID_SKID_MISMATCH", runtime.newFixnum(30));
    _X509.setConstant("V_ERR_AKID_ISSUER_SERIAL_MISMATCH", runtime.newFixnum(31));
    _X509.setConstant("V_ERR_KEYUSAGE_NO_CERTSIGN", runtime.newFixnum(32));
    _X509.setConstant("V_ERR_APPLICATION_VERIFICATION", runtime.newFixnum(50));
    _X509.setConstant("V_FLAG_CRL_CHECK", _4);
    _X509.setConstant("V_FLAG_CRL_CHECK_ALL", _8);
    _X509.setConstant("PURPOSE_SSL_CLIENT", _1);
    _X509.setConstant("PURPOSE_SSL_SERVER", _2);
    _X509.setConstant("PURPOSE_NS_SSL_SERVER", _3);
    _X509.setConstant("PURPOSE_SMIME_SIGN", _4);
    _X509.setConstant("PURPOSE_SMIME_ENCRYPT", _5);
    _X509.setConstant("PURPOSE_CRL_SIGN", _6);
    _X509.setConstant("PURPOSE_ANY", _7);
    _X509.setConstant("PURPOSE_OCSP_HELPER", _8);
    _X509.setConstant("TRUST_COMPAT", _1);
    _X509.setConstant("TRUST_SSL_CLIENT", _2);
    _X509.setConstant("TRUST_SSL_SERVER", _3);
    _X509.setConstant("TRUST_EMAIL", _4);
    _X509.setConstant("TRUST_OBJECT_SIGN", _5);
    _X509.setConstant("TRUST_OCSP_SIGN", _6);
    _X509.setConstant("TRUST_OCSP_REQUEST", _7);
    // These should eventually point to correct things.
    _X509.setConstant("DEFAULT_CERT_AREA", runtime.newString(X509Utils.X509_CERT_AREA));
    _X509.setConstant("DEFAULT_CERT_DIR", runtime.newString(X509Utils.X509_CERT_DIR));
    _X509.setConstant("DEFAULT_CERT_FILE", runtime.newString(X509Utils.X509_CERT_FILE));
    _X509.setConstant("DEFAULT_CERT_DIR_ENV", runtime.newString(X509Utils.X509_CERT_DIR_EVP));
    _X509.setConstant("DEFAULT_CERT_FILE_ENV", runtime.newString(X509Utils.X509_CERT_FILE_EVP));
    _X509.setConstant("DEFAULT_PRIVATE_DIR", runtime.newString(X509Utils.X509_PRIVATE_DIR));
}
Also used : RubyModule(org.jruby.RubyModule) RubyFixnum(org.jruby.RubyFixnum)

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