Search in sources :

Example 1 with NetscapeCertRequest

use of org.jruby.ext.openssl.impl.NetscapeCertRequest in project jruby-openssl by jruby.

the class NetscapeSPKI method verify.

@JRubyMethod
public IRubyObject verify(final IRubyObject pkey) {
    final NetscapeCertRequest cert = (NetscapeCertRequest) this.cert;
    cert.setPublicKey(((PKey) pkey).getPublicKey());
    try {
        boolean result = cert.verify(challenge.toString());
        return getRuntime().newBoolean(result);
    } catch (NoSuchAlgorithmException e) {
        debugStackTrace(getRuntime(), e);
        throw newSPKIError(e);
    } catch (GeneralSecurityException e) {
        throw newSPKIError(e);
    }
}
Also used : NetscapeCertRequest(org.jruby.ext.openssl.impl.NetscapeCertRequest) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with NetscapeCertRequest

use of org.jruby.ext.openssl.impl.NetscapeCertRequest in project jruby-openssl by jruby.

the class NetscapeSPKI method sign.

@JRubyMethod
public IRubyObject sign(final IRubyObject key, final IRubyObject digest) {
    final String keyAlg = ((PKey) key).getAlgorithm();
    final String digAlg = ((Digest) digest).getShortAlgorithm();
    final String symKey = keyAlg.toLowerCase() + '-' + digAlg.toLowerCase();
    try {
        final ASN1ObjectIdentifier alg = ASN1.sym2Oid(getRuntime(), symKey);
        final PublicKey publicKey = ((PKey) this.public_key).getPublicKey();
        final String challengeStr = challenge.toString();
        final NetscapeCertRequest cert;
        this.cert = cert = new NetscapeCertRequest(challengeStr, new AlgorithmIdentifier(alg), publicKey);
        cert.sign(((PKey) key).getPrivateKey());
    } catch (NoSuchAlgorithmException e) {
        debugStackTrace(getRuntime(), e);
        throw newSPKIError(e);
    } catch (GeneralSecurityException e) {
        throw newSPKIError(e);
    }
    return this;
}
Also used : NetscapeCertRequest(org.jruby.ext.openssl.impl.NetscapeCertRequest) PublicKey(java.security.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) DERBitString(org.bouncycastle.asn1.DERBitString) DERIA5String(org.bouncycastle.asn1.DERIA5String) RubyString(org.jruby.RubyString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with NetscapeCertRequest

use of org.jruby.ext.openssl.impl.NetscapeCertRequest in project jruby-openssl by jruby.

the class NetscapeSPKI method initialize.

@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
    final Ruby runtime = context.runtime;
    if (args.length > 0) {
        byte[] request = args[0].convertToString().getBytes();
        request = tryBase64Decode(request);
        final NetscapeCertRequest cert;
        try {
            this.cert = cert = new NetscapeCertRequest(request);
            challenge = runtime.newString(cert.getChallenge());
        } catch (GeneralSecurityException e) {
            throw newSPKIError(e);
        } catch (IllegalArgumentException e) {
            throw newSPKIError(e);
        }
        final PublicKey publicKey = cert.getPublicKey();
        final String algorithm = publicKey.getAlgorithm();
        final RubyString pub_key = RubyString.newString(runtime, publicKey.getEncoded());
        if ("RSA".equalsIgnoreCase(algorithm)) {
            this.public_key = _RSA(runtime).callMethod(context, "new", pub_key);
        } else if ("DSA".equalsIgnoreCase(algorithm)) {
            this.public_key = _DSA(runtime).callMethod(context, "new", pub_key);
        } else {
            throw runtime.newLoadError("not implemented algo for public key: " + algorithm);
        }
    }
    return this;
}
Also used : NetscapeCertRequest(org.jruby.ext.openssl.impl.NetscapeCertRequest) PublicKey(java.security.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) RubyString(org.jruby.RubyString) DERBitString(org.bouncycastle.asn1.DERBitString) DERIA5String(org.bouncycastle.asn1.DERIA5String) RubyString(org.jruby.RubyString) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

GeneralSecurityException (java.security.GeneralSecurityException)3 JRubyMethod (org.jruby.anno.JRubyMethod)3 NetscapeCertRequest (org.jruby.ext.openssl.impl.NetscapeCertRequest)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 PublicKey (java.security.PublicKey)2 DERBitString (org.bouncycastle.asn1.DERBitString)2 DERIA5String (org.bouncycastle.asn1.DERIA5String)2 RubyString (org.jruby.RubyString)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1 Ruby (org.jruby.Ruby)1