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());
}
}
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));
}
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());
}
}
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());
}
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;
}
Aggregations