Search in sources :

Example 1 with PKCS10Request

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

the class X509Request 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 (Arity.checkArgumentCount(runtime, args, 0, 1) == 0)
        return this;
    try {
        request = new PKCS10Request(StringHelper.readX509PEM(context, args[0]));
    } catch (RuntimeException e) {
        debugStackTrace(runtime, e);
        throw newRequestError(runtime, "invalid certificate request data", e);
    }
    final String algorithm;
    final byte[] encoded;
    try {
        final PublicKey pkey = request.generatePublicKey();
        algorithm = pkey.getAlgorithm();
        encoded = pkey.getEncoded();
    } catch (IOException e) {
        throw newRequestError(runtime, e);
    } catch (GeneralSecurityException e) {
        throw newRequestError(runtime, e);
    }
    final RubyString enc = RubyString.newString(runtime, encoded);
    if ("RSA".equalsIgnoreCase(algorithm)) {
        this.public_key = newPKeyImplInstance(context, "RSA", enc);
    } else if ("DSA".equalsIgnoreCase(algorithm)) {
        this.public_key = newPKeyImplInstance(context, "DSA", enc);
    } else {
        throw runtime.newNotImplementedError("public key algorithm: " + algorithm);
    }
    this.subject = newName(context, request.getSubject());
    final Attribute[] attrs = request.getAttributes();
    try {
        // final RubyModule _ASN1 = _ASN1(runtime);
        if (attrs != null) {
            for (final Attribute attr : attrs) {
                final ASN1ObjectIdentifier type = attr.getAttrType();
                final ASN1Set values = attr.getAttrValues();
                attributes.add(newAttribute(context, type, values));
            }
        }
    } catch (IOException e) {
        throw newRequestError(runtime, e);
    }
    return this;
}
Also used : PKCS10Request(org.jruby.ext.openssl.impl.PKCS10Request) Attribute(org.bouncycastle.asn1.pkcs.Attribute) PublicKey(java.security.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) RubyString(org.jruby.RubyString) RubyString(org.jruby.RubyString) IOException(java.io.IOException) ASN1Set(org.bouncycastle.asn1.ASN1Set) Ruby(org.jruby.Ruby) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with PKCS10Request

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

the class X509Request method getRequest.

private PKCS10Request getRequest() {
    if (request != null)
        return request;
    PublicKey publicKey = null;
    if (public_key != null && !public_key.isNil()) {
        publicKey = public_key.getPublicKey();
    }
    X500Name subjectName = subject != null ? getX500Name(subject) : null;
    final ThreadContext context = getRuntime().getCurrentContext();
    return request = new PKCS10Request(subjectName, publicKey, newAttributesImpl(context));
}
Also used : PKCS10Request(org.jruby.ext.openssl.impl.PKCS10Request) PublicKey(java.security.PublicKey) ThreadContext(org.jruby.runtime.ThreadContext) X500Name(org.bouncycastle.asn1.x500.X500Name)

Aggregations

PublicKey (java.security.PublicKey)2 PKCS10Request (org.jruby.ext.openssl.impl.PKCS10Request)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 ASN1Set (org.bouncycastle.asn1.ASN1Set)1 Attribute (org.bouncycastle.asn1.pkcs.Attribute)1 X500Name (org.bouncycastle.asn1.x500.X500Name)1 Ruby (org.jruby.Ruby)1 RubyString (org.jruby.RubyString)1 JRubyMethod (org.jruby.anno.JRubyMethod)1 ThreadContext (org.jruby.runtime.ThreadContext)1