use of org.jruby.util.ByteList in project jruby-openssl by jruby.
the class NetscapeSPKI method to_pem.
@JRubyMethod(name = { "to_pem", "to_s" })
public IRubyObject to_pem() {
try {
byte[] source = toDER();
// no Base64.DO_BREAK_LINES option needed for NSPKI :
source = Base64.encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS);
return getRuntime().newString(new ByteList(source, false));
} catch (IOException ioe) {
throw newSPKIError(ioe);
}
}
use of org.jruby.util.ByteList in project jruby-openssl by jruby.
the class X509CRL method initialize.
@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args, final Block block) {
final Ruby runtime = context.runtime;
this.extensions = runtime.newArray(8);
if (Arity.checkArgumentCount(runtime, args, 0, 1) == 0)
return this;
final ByteList strList = args[0].asString().getByteList();
final byte[] bytes = strList.unsafeBytes();
final int offset = strList.getBegin();
final int length = strList.getRealSize();
try {
if (avoidJavaSecurity) {
this.crlHolder = parseCRLHolder(bytes, offset, length);
} else {
this.crl = generateCRL(bytes, offset, length);
}
} catch (IOException e) {
debugStackTrace(runtime, e);
throw newCRLError(runtime, e);
} catch (GeneralSecurityException e) {
debugStackTrace(runtime, e);
throw newCRLError(runtime, e);
}
set_last_update(context, RubyTime.newTime(runtime, crl.getThisUpdate().getTime()));
set_next_update(context, RubyTime.newTime(runtime, crl.getNextUpdate().getTime()));
set_issuer(X509Name.newName(runtime, crl.getIssuerX500Principal()));
final int version = crl.getVersion();
this.version = runtime.newFixnum(version > 0 ? version - 1 : 2);
extractExtensions(context);
Set<? extends X509CRLEntry> revokedCRLs = crl.getRevokedCertificates();
if (revokedCRLs != null && !revokedCRLs.isEmpty()) {
final X509CRLEntry[] revokedSorted = revokedCRLs.toArray(new X509CRLEntry[revokedCRLs.size()]);
Arrays.sort(revokedSorted, 0, revokedSorted.length, new Comparator<X509CRLEntry>() {
public int compare(X509CRLEntry o1, X509CRLEntry o2) {
return o1.getRevocationDate().compareTo(o2.getRevocationDate());
}
});
for (X509CRLEntry entry : revokedSorted) {
revoked().append(X509Revoked.newInstance(context, entry));
}
}
this.changed = false;
return this;
}
use of org.jruby.util.ByteList in project jruby-openssl by jruby.
the class X509Extension method rawValueAsString.
private RubyString rawValueAsString(final ThreadContext context) throws IOException {
final Ruby runtime = context.runtime;
// e.g. [ ASN1::UTF8String, ... ]
final IRubyObject value = getValue(runtime);
if (value instanceof RubyArray) {
final RubyArray arr = (RubyArray) value;
final ByteList strVal = new ByteList(64);
final int len = arr.size();
for (int i = 0; i < len; i++) {
IRubyObject entry = arr.eltInternal(i);
if (entry.respondsTo("value")) {
entry = entry.callMethod(context, "value");
}
strVal.append(entry.asString().getByteList());
if (i < len - 1)
strVal.append(',').append(' ');
}
return runtime.newString(strVal);
}
return value.asString();
}
use of org.jruby.util.ByteList in project jruby-openssl by jruby.
the class X509ExtensionFactory method parseSubjectKeyIdentifier.
private DEROctetString parseSubjectKeyIdentifier(final ThreadContext context, final String oid, final String valuex) {
if ("hash".equalsIgnoreCase(valuex)) {
return new DEROctetString(publicKeyIdentifier(context));
}
if (valuex.length() == 20 || !isHex(valuex)) {
return new DEROctetString(ByteList.plain(valuex));
}
final int len = valuex.length();
final ByteList hex = new ByteList(len / 2 + 1);
for (int i = 0; i < len; i += 2) {
if (i + 1 >= len) {
throw newExtensionError(context.runtime, oid + " = " + valuex + ": odd number of digits");
}
final int c1 = upHex(valuex.charAt(i));
final int c2 = upHex(valuex.charAt(i + 1));
if (c1 != -1 && c2 != -1) {
hex.append(((c1 << 4) & 0xF0) | (c2 & 0xF));
} else {
throw newExtensionError(context.runtime, oid + " = " + valuex + ": illegal hex digit");
}
while ((i + 2) < len && valuex.charAt(i + 2) == ':') {
i++;
}
}
final byte[] hexBytes = new byte[hex.getRealSize()];
System.arraycopy(hex.getUnsafeBytes(), hex.getBegin(), hexBytes, 0, hexBytes.length);
return new DEROctetString(hexBytes);
}
use of org.jruby.util.ByteList in project jruby-openssl by jruby.
the class PKeyDSA method sysverify.
// ossl_dsa_verify
@JRubyMethod
public IRubyObject sysverify(IRubyObject data, IRubyObject sign) {
final Ruby runtime = getRuntime();
ByteList sigBytes = convertToString(runtime, sign, "OpenSSL::PKey::DSAError", "invalid signature").getByteList();
ByteList dataBytes = convertToString(runtime, data, "OpenSSL::PKey::DSAError", "invalid data").getByteList();
try {
return runtime.newBoolean(verify("NONEwithDSA", getPublicKey(), dataBytes, sigBytes));
} catch (NoSuchAlgorithmException e) {
throw newDSAError(runtime, e.getMessage());
} catch (SignatureException e) {
throw newDSAError(runtime, "invalid signature");
} catch (InvalidKeyException e) {
throw newDSAError(runtime, "invalid key");
}
}
Aggregations