Search in sources :

Example 16 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class PKeyDSA method syssign.

// ossl_dsa_sign
@JRubyMethod
public IRubyObject syssign(IRubyObject data) {
    final Ruby runtime = getRuntime();
    DSAPrivateKey privateKey;
    if ((privateKey = this.privateKey) == null) {
        throw newDSAError(runtime, "Private DSA key needed!");
    }
    try {
        // DSS1
        ByteList sign = sign("NONEwithDSA", privateKey, data.convertToString().getByteList());
        return RubyString.newString(runtime, sign);
    } catch (GeneralSecurityException ex) {
        throw newDSAError(runtime, ex.getMessage());
    }
}
Also used : ByteList(org.jruby.util.ByteList) PKey.readDSAPrivateKey(org.jruby.ext.openssl.impl.PKey.readDSAPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 17 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class Random method generate.

private static RubyString generate(final ThreadContext context, final IRubyObject self, final int len, final boolean secure) {
    final Holder holder = retrieveHolder((RubyModule) self);
    final byte[] bytes = new byte[len];
    (secure ? holder.getSecureRandom(context) : holder.getPlainRandom()).nextBytes(bytes);
    return RubyString.newString(context.runtime, new ByteList(bytes, false));
}
Also used : ByteList(org.jruby.util.ByteList)

Example 18 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class SSLSocket method sysreadImpl.

private IRubyObject sysreadImpl(final ThreadContext context, IRubyObject len, IRubyObject buff, final boolean blocking, final boolean exception) {
    final Ruby runtime = context.runtime;
    final int length = RubyNumeric.fix2int(len);
    final RubyString buffStr;
    if (buff != null && !buff.isNil()) {
        buffStr = buff.asString();
    } else {
        // fine since we're setValue
        buffStr = RubyString.newEmptyString(runtime);
    }
    if (length == 0) {
        buffStr.clear();
        return buffStr;
    }
    if (length < 0) {
        throw runtime.newArgumentError("negative string size (or size too big)");
    }
    try {
        // So we need to make sure to only block when there is no data left to process
        if (engine == null || !(peerAppData.hasRemaining() || peerNetData.position() > 0)) {
            final Object ex = waitSelect(SelectionKey.OP_READ, blocking, exception);
            // :wait_readable
            if (ex instanceof IRubyObject)
                return (IRubyObject) ex;
        }
        final ByteBuffer dst = ByteBuffer.allocate(length);
        int read = -1;
        // ensure >0 bytes read; sysread is blocking read.
        while (read <= 0) {
            if (engine == null) {
                read = socketChannelImpl().read(dst);
            } else {
                read = read(dst, blocking);
            }
            if (read == -1) {
                if (exception)
                    throw runtime.newEOFError();
                return runtime.getNil();
            }
            if (read == 0 && status == SSLEngineResult.Status.BUFFER_UNDERFLOW) {
                // If we didn't get any data back because we only read in a partial TLS record,
                // instead of spinning until the rest comes in, call waitSelect to either block
                // until the rest is available, or throw a "read would block" error if we are in
                // non-blocking mode.
                final Object ex = waitSelect(SelectionKey.OP_READ, blocking, exception);
                // :wait_readable
                if (ex instanceof IRubyObject)
                    return (IRubyObject) ex;
            }
        }
        final byte[] bytesRead = dst.array();
        final int offset = dst.position() - read;
        buffStr.setValue(new ByteList(bytesRead, offset, read, false));
        return buffStr;
    } catch (IOException ioe) {
        throw runtime.newIOError(ioe.getMessage());
    }
}
Also used : ByteList(org.jruby.util.ByteList) RubyString(org.jruby.RubyString) RubyObject(org.jruby.RubyObject) IRubyObject(org.jruby.runtime.builtin.IRubyObject) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) ByteBuffer(java.nio.ByteBuffer)

Example 19 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class StringHelper method readX509PEM.

static byte[] readX509PEM(final ThreadContext context, IRubyObject arg) {
    final RubyString str = StringHelper.readPossibleDERInput(context, arg);
    final ByteList bytes = str.getByteList();
    return readX509PEM(bytes.unsafeBytes(), bytes.getBegin(), bytes.getRealSize());
}
Also used : ByteList(org.jruby.util.ByteList) RubyString(org.jruby.RubyString)

Example 20 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class X509Cert method initialize.

@JRubyMethod(name = "initialize", optional = 1, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args, final Block unusedBlock) {
    if (args.length == 0) {
        this.subject = X509Name.newName(context.runtime);
        this.issuer = X509Name.newName(context.runtime);
        return this;
    }
    final RubyString str = StringHelper.readPossibleDERInput(context, args[0]);
    final ByteList bytes = str.getByteList();
    initialize(context, bytes.unsafeBytes(), bytes.getBegin(), bytes.getRealSize());
    return this;
}
Also used : ByteList(org.jruby.util.ByteList) RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ByteList (org.jruby.util.ByteList)43 Ruby (org.jruby.Ruby)21 JRubyMethod (org.jruby.anno.JRubyMethod)19 RubyString (org.jruby.RubyString)18 IRubyObject (org.jruby.runtime.builtin.IRubyObject)10 GeneralSecurityException (java.security.GeneralSecurityException)8 IOException (java.io.IOException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 ByteBuffer (java.nio.ByteBuffer)5 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BigInteger (java.math.BigInteger)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)3 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)3 InputSource (org.xml.sax.InputSource)3 StringReader (java.io.StringReader)2