use of org.jruby.RubyInteger in project jruby-openssl by jruby.
the class BN method exp.
@JRubyMethod(name = "**")
public BN exp(final ThreadContext context, IRubyObject other) {
// somewhat strangely, BigInteger takes int rather than BigInteger
// as the argument to pow. so we'll have to narrow the value, and
// raise an exception if data would be lost. (on the other hand, an
// exponent even approaching Integer.MAX_VALUE would be silly big, and
// the value would take a very, very long time to calculate.)
// we'll check for values < 0 (illegal) while we're at it
int exp = -1;
if (other instanceof RubyInteger) {
long val = ((RubyInteger) other).getLongValue();
if (val >= 0 && val <= Integer.MAX_VALUE) {
exp = (int) val;
} else if (other instanceof RubyBignum) {
// inherently too big
throw newBNError(context.runtime, "invalid exponent");
}
}
if (exp == -1) {
if (!(other instanceof BN)) {
throw context.runtime.newTypeError("Cannot convert into " + other.getMetaClass().getName());
}
BigInteger val = ((BN) other).value;
if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) {
throw newBNError(context.runtime, "invalid exponent");
}
exp = val.intValue();
}
try {
return newBN(context.runtime, value.pow(exp));
} catch (ArithmeticException e) {
// shouldn't happen, we've already checked for < 0
throw newBNError(context.runtime, "invalid exponent");
}
}
use of org.jruby.RubyInteger in project jruby-openssl by jruby.
the class ASN1 method decodeObject.
// ObjectId
static IRubyObject decodeObject(final ThreadContext context, final RubyModule ASN1, final org.bouncycastle.asn1.ASN1Encodable obj) throws IOException, IllegalArgumentException {
final Ruby runtime = context.runtime;
if (obj instanceof ASN1Integer) {
final BN val = BN.newBN(runtime, ((ASN1Integer) obj).getValue());
return ASN1.getClass("Integer").callMethod(context, "new", val);
}
if (obj instanceof DERInteger) {
final BN val = BN.newBN(runtime, ((DERInteger) obj).getValue());
return ASN1.getClass("Integer").callMethod(context, "new", val);
}
if (obj instanceof DERBitString) {
final DERBitString derObj = (DERBitString) obj;
RubyString str = runtime.newString(new ByteList(derObj.getBytes(), false));
IRubyObject bitString = ASN1.getClass("BitString").callMethod(context, "new", str);
bitString.callMethod(context, "unused_bits=", runtime.newFixnum(derObj.getPadBits()));
return bitString;
}
if (obj instanceof ASN1String) {
final Integer typeId = typeId(obj.getClass());
String type = typeId == null ? null : (String) (ASN1_INFO[typeId][2]);
final ByteList bytes;
if (obj instanceof DERUTF8String) {
if (type == null)
type = "UTF8String";
bytes = new ByteList(((DERUTF8String) obj).getString().getBytes("UTF-8"), false);
} else {
if (type == null) {
if (obj instanceof DERNumericString) {
type = "NumericString";
} else if (obj instanceof DERPrintableString) {
type = "PrintableString";
} else if (obj instanceof DERIA5String) {
type = "IA5String";
} else if (obj instanceof DERT61String) {
type = "T61String";
} else if (obj instanceof DERGeneralString) {
type = "GeneralString";
} else if (obj instanceof DERUniversalString) {
type = "UniversalString";
} else if (obj instanceof DERBMPString) {
type = "BMPString";
} else {
// NOTE "VideotexString", "GraphicString", "ISO64String" not-handled in BC !
throw new IllegalArgumentException("could not handle ASN1 string type: " + obj + " (" + obj.getClass().getName() + ")");
}
}
bytes = ByteList.create(((ASN1String) obj).getString());
}
return ASN1.getClass(type).callMethod(context, "new", runtime.newString(bytes));
}
if (obj instanceof ASN1OctetString) {
final ByteList octets = new ByteList(((ASN1OctetString) obj).getOctets(), false);
// final ByteList octets = new ByteList(((ASN1OctetString) obj).getEncoded(ASN1Encoding.DER), false);
return ASN1.getClass("OctetString").callMethod(context, "new", runtime.newString(octets));
}
if (obj instanceof ASN1Null) {
return ASN1.getClass("Null").callMethod(context, "new", runtime.getNil());
}
if (obj instanceof ASN1Boolean) {
final boolean val = ((ASN1Boolean) obj).isTrue();
return ASN1.getClass("Boolean").callMethod(context, "new", runtime.newBoolean(val));
}
// DERBoolean extends ASN1Boolean only since 1.51 (<= 1.50 the other way around)
if (obj instanceof DERBoolean) {
final boolean val = ((DERBoolean) obj).isTrue();
return ASN1.getClass("Boolean").callMethod(context, "new", runtime.newBoolean(val));
}
if (obj instanceof ASN1UTCTime) {
final Date adjustedTime;
try {
adjustedTime = ((ASN1UTCTime) obj).getAdjustedDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, adjustedTime.getTime());
return ASN1.getClass("UTCTime").callMethod(context, "new", time);
}
// NOTE: keep for BC versions compatibility ... extends ASN1UTCTime (since BC 1.51)
if (obj instanceof DERUTCTime) {
final Date adjustedTime;
try {
adjustedTime = ((DERUTCTime) obj).getAdjustedDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, adjustedTime.getTime());
return ASN1.getClass("UTCTime").callMethod(context, "new", time);
}
if (obj instanceof ASN1GeneralizedTime) {
final Date generalTime;
try {
generalTime = ((ASN1GeneralizedTime) obj).getDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, generalTime.getTime());
return ASN1.getClass("GeneralizedTime").callMethod(context, "new", time);
}
// NOTE: keep for BC versions compatibility ... extends ASN1GeneralizedTime (since BC 1.51)
if (obj instanceof DERGeneralizedTime) {
final Date generalTime;
try {
generalTime = ((DERGeneralizedTime) obj).getDate();
} catch (ParseException e) {
throw new IOException(e);
}
final RubyTime time = RubyTime.newTime(runtime, generalTime.getTime());
return ASN1.getClass("GeneralizedTime").callMethod(context, "new", time);
}
if (obj instanceof ASN1ObjectIdentifier) {
final String objId = ((ASN1ObjectIdentifier) obj).getId();
return ASN1.getClass("ObjectId").callMethod(context, "new", runtime.newString(objId));
}
// DERObjectIdentifier extends ASN1ObjectIdentifier = 1.51
if (obj instanceof DERObjectIdentifier) {
final String objId = ((DERObjectIdentifier) obj).getId();
return ASN1.getClass("ObjectId").callMethod(context, "new", runtime.newString(objId));
}
if (obj instanceof ASN1TaggedObject) {
final ASN1TaggedObject taggedObj = (ASN1TaggedObject) obj;
IRubyObject val = decodeObject(context, ASN1, taggedObj.getObject());
IRubyObject tag = runtime.newFixnum(taggedObj.getTagNo());
IRubyObject tag_class = runtime.newSymbol("CONTEXT_SPECIFIC");
final RubyArray valArr = runtime.newArray(val);
return ASN1.getClass("ASN1Data").callMethod(context, "new", new IRubyObject[] { valArr, tag, tag_class });
}
if (obj instanceof DERApplicationSpecific) {
final DERApplicationSpecific appSpecific = (DERApplicationSpecific) obj;
IRubyObject tag = runtime.newFixnum(appSpecific.getApplicationTag());
IRubyObject tag_class = runtime.newSymbol("APPLICATION");
final ASN1Sequence sequence = (ASN1Sequence) appSpecific.getObject(SEQUENCE);
@SuppressWarnings("unchecked") final RubyArray valArr = decodeObjects(context, ASN1, sequence.getObjects());
return ASN1.getClass("ASN1Data").callMethod(context, "new", new IRubyObject[] { valArr, tag, tag_class });
}
if (obj instanceof ASN1Sequence) {
@SuppressWarnings("unchecked") RubyArray arr = decodeObjects(context, ASN1, ((ASN1Sequence) obj).getObjects());
return ASN1.getClass("Sequence").callMethod(context, "new", arr);
}
if (obj instanceof ASN1Set) {
@SuppressWarnings("unchecked") RubyArray arr = decodeObjects(context, ASN1, ((ASN1Set) obj).getObjects());
return ASN1.getClass("Set").callMethod(context, "new", arr);
}
if (obj instanceof ASN1Enumerated) {
final RubyInteger value = RubyBignum.bignorm(runtime, ((ASN1Enumerated) obj).getValue());
return ASN1.getClass("Enumerated").callMethod(context, "new", value);
}
throw new IllegalArgumentException("unable to decode object: " + obj + " (" + (obj == null ? "" : obj.getClass().getName()) + ")");
}
use of org.jruby.RubyInteger in project jruby-openssl by jruby.
the class BN method coerce.
@JRubyMethod(name = "coerce")
public // FIXME: is this right? don't see how it would be useful...
IRubyObject coerce(IRubyObject other) {
final Ruby runtime = getRuntime();
IRubyObject self;
if (other instanceof RubyString) {
self = runtime.newString(value.toString());
} else if (other instanceof RubyInteger) {
self = to_i();
} else if (other instanceof BN) {
self = this;
} else {
throw runtime.newTypeError("don't know how to coerce to " + other.getMetaClass().getName());
}
return runtime.newArray(other, self);
}
use of org.jruby.RubyInteger 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 });
}
use of org.jruby.RubyInteger in project jruby-openssl by jruby.
the class X509Name method to_a.
@Override
@JRubyMethod
public RubyArray to_a() {
final Ruby runtime = getRuntime();
final RubyArray entries = runtime.newArray(oids.size());
final Iterator<ASN1ObjectIdentifier> oidsIter = oids.iterator();
@SuppressWarnings("unchecked") final Iterator<Object> valuesIter = (Iterator) values.iterator();
final Iterator<RubyInteger> typesIter = types.iterator();
while (oidsIter.hasNext()) {
final ASN1ObjectIdentifier oid = oidsIter.next();
String oName = name(runtime, oid);
if (oName == null)
oName = oid.toString();
final String value = valuesIter.next().toString();
final IRubyObject type = typesIter.next();
final IRubyObject[] entry = new IRubyObject[] { runtime.newString(oName), runtime.newString(value), type };
entries.append(runtime.newArrayNoCopy(entry));
}
return entries;
}
Aggregations