类 OpenSSL::PKCS12

定义了一种常用的文件格式,用于存储私钥及其配套的公钥证书,并使用基于密码的对称密钥进行保护。

属性

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

公共类方法

create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]]) 点击切换源代码

参数

  • pass - 字符串

  • name - 描述密钥的字符串。

  • key - 任何 PKey

  • cert - 一个 X509::Certificate

    • 证书的公钥部分必须包含有效的公钥。

    • not_before 和 not_after 字段必须填写。

  • ca - 一个可选的 X509::Certificate 数组。

  • key_pbe - 字符串

  • cert_pbe - 字符串

  • key_iter - 整数

  • mac_iter - 整数

  • keytype - 表示 MSIE 特定扩展的整数。

任何可选参数都可以作为 nil 提供,以保留 OpenSSL 默认值。

请参阅 OpenSSL 文档以了解 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 : StringValueCStr(pass);
    friendlyname = NIL_P(name) ? NULL : StringValueCStr(name);
    key = GetPKeyPtr(pkey);
    x509 = GetX509CertPtr(cert);
/* TODO: make a VALUE to nid function */
    if (!NIL_P(key_nid)) {
        if ((nkey = OBJ_txt2nid(StringValueCStr(key_nid))) == NID_undef)
            ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, key_nid);
    }
    if (!NIL_P(cert_nid)) {
        if ((ncert = OBJ_txt2nid(StringValueCStr(cert_nid))) == NID_undef)
            ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, 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 点击切换源代码
new(str) → pkcs12
new(str, pass) → pkcs12

参数

  • str - 必须是 DER 编码的 PKCS12 字符串。

  • pass - 字符串

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 : StringValueCStr(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();
    if (key) {
        pkey = rb_protect(ossl_pkey_new_i, (VALUE)key, &st);
        if (st) goto err;
    }
    if (x509) {
        cert = rb_protect(ossl_x509_new_i, (VALUE)x509, &st);
        if (st) goto err;
    }
    if (x509s) {
        ca = rb_protect(ossl_x509_sk2ary_i, (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;
}

公共实例方法

initialize_copy(p1) 点击切换源代码
static VALUE
ossl_pkcs12_initialize_copy(VALUE self, VALUE other)
{
    PKCS12 *p12, *p12_old, *p12_new;

    rb_check_frozen(self);
    GetPKCS12(self, p12_old);
    GetPKCS12(other, p12);

    p12_new = ASN1_dup((i2d_of_void *)i2d_PKCS12, (d2i_of_void *)d2i_PKCS12, (char *)p12);
    if (!p12_new)
        ossl_raise(ePKCS12Error, "ASN1_dup");

    SetPKCS12(self, p12_new);
    PKCS12_free(p12_old);

    return self;
}
to_der() 点击切换源代码
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;
}