Class: OpenSSL::PKCS12 (Ruby 2.3.2)
Description: Class : OpenSSL::PKCS12 - Ruby 2.3.2

- Home
- Core 2.3.2
- Std-lib 2.3.2
- Downloads

Support for the Ruby 2.3 series has ended. See here for reference.

### Home Classes Methods

### In Files

- openssl/ossl.c

### Parent

Object

### Namespace

- CLASS OpenSSL::PKCS12::PKCS12Error

### Methods

- ::create
- ::new
- #to_der

### Class/Module Index

Quicksearch

- Integer
- OpenSSL::ASN1
- OpenSSL::ASN1::ASN1Data
- OpenSSL::ASN1::ASN1Error
- OpenSSL::ASN1::Constructive
- OpenSSL::ASN1::ObjectId
- OpenSSL::ASN1::Primitive
- OpenSSL::BN
- OpenSSL::BNError
- OpenSSL::Buffering
- OpenSSL::Cipher
- OpenSSL::Cipher::Cipher
- OpenSSL::Cipher::CipherError
- OpenSSL::Config
- OpenSSL::ConfigError
- OpenSSL::Digest
- OpenSSL::Digest::DigestError
- OpenSSL::Engine
- OpenSSL::Engine::EngineError
- OpenSSL::ExtConfig
- OpenSSL::HMAC
- OpenSSL::HMACError
- OpenSSL::Netscape
- OpenSSL::Netscape::SPKI
- OpenSSL::Netscape::SPKIError
- OpenSSL::OCSP
- OpenSSL::OCSP::BasicResponse
- OpenSSL::OCSP::CertificateId
- OpenSSL::OCSP::OCSPError
- OpenSSL::OCSP::Request
- OpenSSL::OCSP::Response
- OpenSSL::OpenSSLError
- OpenSSL::PKCS12
- OpenSSL::PKCS12::PKCS12Error
- OpenSSL::PKCS5
- OpenSSL::PKCS5::PKCS5Error
- OpenSSL::PKCS7
- OpenSSL::PKCS7::PKCS7Error
- OpenSSL::PKCS7::RecipientInfo
- OpenSSL::PKCS7::SignerInfo
- OpenSSL::PKey
- OpenSSL::PKey::DH
- OpenSSL::PKey::DHError
- OpenSSL::PKey::DSA
- OpenSSL::PKey::DSAError
- OpenSSL::PKey::EC
- OpenSSL::PKey::EC::Group
- OpenSSL::PKey::EC::Group::Error
- OpenSSL::PKey::EC::Point
- OpenSSL::PKey::EC::Point::Error
- OpenSSL::PKey::ECError
- OpenSSL::PKey::PKey
- OpenSSL::PKey::PKeyError
- OpenSSL::PKey::RSA
- OpenSSL::PKey::RSAError
- OpenSSL::Random
- OpenSSL::Random::RandomError
- OpenSSL::SSL
- OpenSSL::SSL::SSLContext
- OpenSSL::SSL::SSLContext::ExtConfig
- OpenSSL::SSL::SSLError
- OpenSSL::SSL::SSLErrorWaitReadable
- OpenSSL::SSL::SSLErrorWaitWritable
- OpenSSL::SSL::SSLServer
- OpenSSL::SSL::SSLSocket
- OpenSSL::SSL::SSLSocket::ExtConfig
- OpenSSL::SSL::Session
- OpenSSL::SSL::Session::SessionError
- OpenSSL::SSL::SocketForwarder
- OpenSSL::X509
- OpenSSL::X509::Attribute
- OpenSSL::X509::AttributeError
- OpenSSL::X509::CRL
- OpenSSL::X509::CRLError
- OpenSSL::X509::Certificate
- OpenSSL::X509::CertificateError
- OpenSSL::X509::Extension
- OpenSSL::X509::ExtensionError
- OpenSSL::X509::ExtensionFactory
- OpenSSL::X509::Name
- OpenSSL::X509::Name::RFC2253DN
- OpenSSL::X509::NameError
- OpenSSL::X509::Request
- OpenSSL::X509::RequestError
- OpenSSL::X509::Revoked
- OpenSSL::X509::RevokedError
- OpenSSL::X509::Store
- OpenSSL::X509::StoreContext
- OpenSSL::X509::StoreError
- unknown

No matching classes.

# OpenSSL::PKCS12

Defines a file format commonly used to store private keys with accompanying public key certificates, protected with a password-based symmetric key.

### Attributes

ca_certs[R]
certificate[R]
key[R]

### Public Class Methods

create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]]) click to toggle source

### Parameters¶ ↑

- `pass` - string

- `name` - A string describing the key.

- `key` - Any PKey.

- `cert` - A X509::Certificate.

- - The public_key portion of the certificate must contain a valid public key.

- - The not_before and not_after fields must be filled in.

- `ca` - An optional array of X509::Certificate's.

- `key_pbe` - string

- `cert_pbe` - string

- `key_iter` - integer

- `mac_iter` - integer

- `keytype` - An integer representing an MSIE specific extension.

Any optional arguments may be supplied as nil to preserve the OpenSSL defaults.

See the OpenSSL documentation for PKCS12_create().

                   static VALUE
    ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
    {
        VALUE pass, name, pkey, cert, ca, key_nid, cert_nid, key_iter, mac_iter, keytype;
        VALUE obj;
        char *passphrase, *friendlyname;
        EVP_PKEY *key;
        X509 *x509;
        STACK_OF(X509) *x509s;
        int nkey = 0, ncert = 0, kiter = 0, miter = 0, ktype = 0;
        PKCS12 *p12;

        rb_scan_args(argc, argv, "46", &pass, &name, &pkey, &cert, &ca, &key_nid, &cert_nid, &key_iter, &mac_iter, &keytype);
        passphrase = NIL_P(pass) ? NULL : StringValuePtr(pass);
        friendlyname = NIL_P(name) ? NULL : StringValuePtr(name);
        key = GetPKeyPtr(pkey);
        x509 = GetX509CertPtr(cert);
    /* TODO: make a VALUE to nid function */
        if (!NIL_P(key_nid)) {
            if ((nkey = OBJ_txt2nid(StringValuePtr(key_nid))) == NID_undef)
                ossl_raise(rb_eArgError, "Unknown PBE algorithm %s", StringValuePtr(key_nid));
        }
        if (!NIL_P(cert_nid)) {
            if ((ncert = OBJ_txt2nid(StringValuePtr(cert_nid))) == NID_undef)
                ossl_raise(rb_eArgError, "Unknown PBE algorithm %s", StringValuePtr(cert_nid));
        }
        if (!NIL_P(key_iter))
            kiter = NUM2INT(key_iter);
        if (!NIL_P(mac_iter))
            miter = NUM2INT(mac_iter);
        if (!NIL_P(keytype))
            ktype = NUM2INT(keytype);

        obj = NewPKCS12(cPKCS12);
        x509s = NIL_P(ca) ? NULL : ossl_x509_ary2sk(ca);
        p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s,
                            nkey, ncert, kiter, miter, ktype);
        sk_X509_pop_free(x509s, X509_free);
        if(!p12) ossl_raise(ePKCS12Error, NULL);
        SetPKCS12(obj, p12);

        ossl_pkcs12_set_key(obj, pkey);
        ossl_pkcs12_set_cert(obj, cert);
        ossl_pkcs12_set_ca_certs(obj, ca);

        return obj;
    }

new → pkcs12 click to toggle source
new(str) → pkcs12
new(str, pass) → pkcs12

### Parameters¶ ↑

- `str` - Must be a DER encoded PKCS12 string.

- `pass` - string

                   static VALUE
    ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
    {
        BIO *in;
        VALUE arg, pass, pkey, cert, ca;
        char *passphrase;
        EVP_PKEY *key;
        X509 *x509;
        STACK_OF(X509) *x509s = NULL;
        int st = 0;
        PKCS12 *pkcs = DATA_PTR(self);

        if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) return self;
        passphrase = NIL_P(pass) ? NULL : StringValuePtr(pass);
        in = ossl_obj2bio(arg);
        d2i_PKCS12_bio(in, &pkcs);
        DATA_PTR(self) = pkcs;
        BIO_free(in);

        pkey = cert = ca = Qnil;
        /* OpenSSL's bug; PKCS12_parse() puts errors even if it succeeds.
         * Fixed in OpenSSL 1.0.0t, 1.0.1p, 1.0.2d */
        ERR_set_mark();
        if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s))
            ossl_raise(ePKCS12Error, "PKCS12_parse");
        ERR_pop_to_mark();
        pkey = rb_protect((VALUE(*)_((VALUE)))ossl_pkey_new, (VALUE)key,
                          &st); /* NO DUP */
        if(st) goto err;
        cert = rb_protect((VALUE(*)_((VALUE)))ossl_x509_new, (VALUE)x509, &st);
        if(st) goto err;
        if(x509s){
            ca =
                rb_protect((VALUE(*)_((VALUE)))ossl_x509_sk2ary, (VALUE)x509s, &st);
            if(st) goto err;
        }

      err:
        X509_free(x509);
        sk_X509_pop_free(x509s, X509_free);
        ossl_pkcs12_set_key(self, pkey);
        ossl_pkcs12_set_cert(self, cert);
        ossl_pkcs12_set_ca_certs(self, ca);
        if(st) rb_jump_tag(st);

        return self;
    }

### Public Instance Methods

to_der() click to toggle source

                   static VALUE
    ossl_pkcs12_to_der(VALUE self)
    {
        PKCS12 *p12;
        VALUE str;
        long len;
        unsigned char *p;

        GetPKCS12(self, p12);
        if((len = i2d_PKCS12(p12, NULL)) <= 0)
            ossl_raise(ePKCS12Error, NULL);
        str = rb_str_new(0, len);
        p = (unsigned char *)RSTRING_PTR(str);
        if(i2d_PKCS12(p12, &p) <= 0)
            ossl_raise(ePKCS12Error, NULL);
        ossl_str_adjust(str, p);

        return str;
    }