Search in sources :

Example 41 with RubyString

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 });
}
Also used : RubyInteger(org.jruby.RubyInteger) RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 42 with RubyString

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

Example 43 with RubyString

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();
}
Also used : RubyModule(org.jruby.RubyModule) RubyString(org.jruby.RubyString)

Example 44 with RubyString

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;
}
Also used : RubyString(org.jruby.RubyString) StringReader(java.io.StringReader) BigInteger(java.math.BigInteger) DHParameterSpec(javax.crypto.spec.DHParameterSpec) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 45 with RubyString

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;
}
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

RubyString (org.jruby.RubyString)49 JRubyMethod (org.jruby.anno.JRubyMethod)32 Ruby (org.jruby.Ruby)28 IRubyObject (org.jruby.runtime.builtin.IRubyObject)18 IOException (java.io.IOException)15 ByteList (org.jruby.util.ByteList)12 StringReader (java.io.StringReader)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 GeneralSecurityException (java.security.GeneralSecurityException)5 RaiseException (org.jruby.exceptions.RaiseException)5 BigInteger (java.math.BigInteger)4 PublicKey (java.security.PublicKey)4 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 RubyArray (org.jruby.RubyArray)4 Charset (java.nio.charset.Charset)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 RubyInteger (org.jruby.RubyInteger)3 ThreadContext (org.jruby.runtime.ThreadContext)3 InputSource (org.xml.sax.InputSource)3