Search in sources :

Example 36 with ByteList

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);
    }
}
Also used : ByteList(org.jruby.util.ByteList) IOException(java.io.IOException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 37 with ByteList

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;
}
Also used : X509CRLEntry(java.security.cert.X509CRLEntry) ByteList(org.jruby.util.ByteList) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 38 with ByteList

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();
}
Also used : ByteList(org.jruby.util.ByteList) RubyArray(org.jruby.RubyArray) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby)

Example 39 with ByteList

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

Example 40 with ByteList

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");
    }
}
Also used : ByteList(org.jruby.util.ByteList) Ruby(org.jruby.Ruby) 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