2003-12-11 07:29:08 -05:00
|
|
|
/*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-12-11 07:29:08 -05:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
#define NewPKCS12(klass) \
|
|
|
|
TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, 0)
|
|
|
|
|
|
|
|
#define SetPKCS12(obj, p12) do { \
|
2011-03-07 03:44:45 -05:00
|
|
|
if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
|
2015-05-29 01:55:02 -04:00
|
|
|
RTYPEDDATA_DATA(obj) = (p12); \
|
2003-12-11 07:29:08 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GetPKCS12(obj, p12) do { \
|
2014-12-12 17:37:53 -05:00
|
|
|
TypedData_Get_Struct((obj), PKCS12, &ossl_pkcs12_type, (p12)); \
|
2011-03-07 03:44:45 -05:00
|
|
|
if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
|
2003-12-11 07:29:08 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define SafeGetPKCS12(obj, p12) do { \
|
2011-03-07 03:44:45 -05:00
|
|
|
OSSL_Check_Kind((obj), cPKCS12); \
|
|
|
|
GetPKCS12((obj), (p12)); \
|
2003-12-11 07:29:08 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ossl_pkcs12_set_key(o,v) rb_iv_set((o), "@key", (v))
|
|
|
|
#define ossl_pkcs12_set_cert(o,v) rb_iv_set((o), "@certificate", (v))
|
|
|
|
#define ossl_pkcs12_set_ca_certs(o,v) rb_iv_set((o), "@ca_certs", (v))
|
|
|
|
#define ossl_pkcs12_get_key(o) rb_iv_get((o), "@key")
|
|
|
|
#define ossl_pkcs12_get_cert(o) rb_iv_get((o), "@certificate")
|
|
|
|
#define ossl_pkcs12_get_ca_certs(o) rb_iv_get((o), "@ca_certs")
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cPKCS12;
|
|
|
|
VALUE ePKCS12Error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
2014-12-12 17:37:53 -05:00
|
|
|
static void
|
|
|
|
ossl_pkcs12_free(void *ptr)
|
|
|
|
{
|
|
|
|
PKCS12_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t ossl_pkcs12_type = {
|
|
|
|
"OpenSSL/PKCS12",
|
|
|
|
{
|
|
|
|
0, ossl_pkcs12_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2003-12-11 07:29:08 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_pkcs12_s_allocate(VALUE klass)
|
|
|
|
{
|
|
|
|
PKCS12 *p12;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewPKCS12(klass);
|
2003-12-11 07:29:08 -05:00
|
|
|
if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL);
|
2015-05-29 01:55:02 -04:00
|
|
|
SetPKCS12(obj, p12);
|
2003-12-11 07:29:08 -05:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_pkcs12_initialize_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
PKCS12 *p12, *p12_old, *p12_new;
|
|
|
|
|
|
|
|
rb_check_frozen(self);
|
|
|
|
GetPKCS12(self, p12_old);
|
|
|
|
SafeGetPKCS12(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;
|
|
|
|
}
|
|
|
|
|
2007-04-05 01:59:22 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* PKCS12.create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]])
|
|
|
|
*
|
|
|
|
* === Parameters
|
|
|
|
* * +pass+ - string
|
|
|
|
* * +name+ - A string describing the key.
|
|
|
|
* * +key+ - Any PKey.
|
|
|
|
* * +cert+ - A X509::Certificate.
|
2016-08-29 01:47:09 -04:00
|
|
|
* * The public_key portion of the certificate must contain a valid public key.
|
|
|
|
* * The not_before and not_after fields must be filled in.
|
2007-04-05 01:59:22 -04:00
|
|
|
* * +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().
|
|
|
|
*/
|
2003-12-11 07:29:08 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2007-04-05 01:59:22 -04:00
|
|
|
VALUE pass, name, pkey, cert, ca, key_nid, cert_nid, key_iter, mac_iter, keytype;
|
2003-12-11 07:29:08 -05:00
|
|
|
VALUE obj;
|
|
|
|
char *passphrase, *friendlyname;
|
|
|
|
EVP_PKEY *key;
|
|
|
|
X509 *x509;
|
|
|
|
STACK_OF(X509) *x509s;
|
2007-04-05 01:59:22 -04:00
|
|
|
int nkey = 0, ncert = 0, kiter = 0, miter = 0, ktype = 0;
|
2003-12-11 07:29:08 -05:00
|
|
|
PKCS12 *p12;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2007-04-05 01:59:22 -04:00
|
|
|
rb_scan_args(argc, argv, "46", &pass, &name, &pkey, &cert, &ca, &key_nid, &cert_nid, &key_iter, &mac_iter, &keytype);
|
2016-05-23 07:40:07 -04:00
|
|
|
passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
|
|
|
|
friendlyname = NIL_P(name) ? NULL : StringValueCStr(name);
|
2003-12-11 07:29:08 -05:00
|
|
|
key = GetPKeyPtr(pkey);
|
|
|
|
x509 = GetX509CertPtr(cert);
|
2007-04-05 01:59:22 -04:00
|
|
|
/* TODO: make a VALUE to nid function */
|
|
|
|
if (!NIL_P(key_nid)) {
|
2016-05-23 07:40:07 -04:00
|
|
|
if ((nkey = OBJ_txt2nid(StringValueCStr(key_nid))) == NID_undef)
|
|
|
|
ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, key_nid);
|
2007-04-05 01:59:22 -04:00
|
|
|
}
|
|
|
|
if (!NIL_P(cert_nid)) {
|
2016-05-23 07:40:07 -04:00
|
|
|
if ((ncert = OBJ_txt2nid(StringValueCStr(cert_nid))) == NID_undef)
|
|
|
|
ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, cert_nid);
|
2007-04-05 01:59:22 -04:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewPKCS12(cPKCS12);
|
2016-08-29 01:47:09 -04:00
|
|
|
x509s = NIL_P(ca) ? NULL : ossl_x509_ary2sk(ca);
|
2003-12-11 07:29:08 -05:00
|
|
|
p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s,
|
2007-04-05 01:59:22 -04:00
|
|
|
nkey, ncert, kiter, miter, ktype);
|
2003-12-11 07:29:08 -05:00
|
|
|
sk_X509_pop_free(x509s, X509_free);
|
|
|
|
if(!p12) ossl_raise(ePKCS12Error, NULL);
|
2015-05-29 01:55:02 -04:00
|
|
|
SetPKCS12(obj, p12);
|
2003-12-11 07:29:08 -05:00
|
|
|
|
2007-04-05 01:59:22 -04:00
|
|
|
ossl_pkcs12_set_key(obj, pkey);
|
|
|
|
ossl_pkcs12_set_cert(obj, cert);
|
|
|
|
ossl_pkcs12_set_ca_certs(obj, ca);
|
|
|
|
|
2003-12-11 07:29:08 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2007-04-05 01:59:22 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* PKCS12.new -> pkcs12
|
|
|
|
* PKCS12.new(str) -> pkcs12
|
|
|
|
* PKCS12.new(str, pass) -> pkcs12
|
|
|
|
*
|
|
|
|
* === Parameters
|
|
|
|
* * +str+ - Must be a DER encoded PKCS12 string.
|
|
|
|
* * +pass+ - string
|
|
|
|
*/
|
2003-12-11 07:29:08 -05:00
|
|
|
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;
|
2008-07-22 11:34:23 -04:00
|
|
|
PKCS12 *pkcs = DATA_PTR(self);
|
2003-12-11 07:29:08 -05:00
|
|
|
|
|
|
|
if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) return self;
|
2016-05-23 07:40:07 -04:00
|
|
|
passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
|
2003-12-11 07:29:08 -05:00
|
|
|
in = ossl_obj2bio(arg);
|
2008-07-22 11:34:23 -04:00
|
|
|
d2i_PKCS12_bio(in, &pkcs);
|
|
|
|
DATA_PTR(self) = pkcs;
|
2003-12-11 07:29:08 -05:00
|
|
|
BIO_free(in);
|
|
|
|
|
|
|
|
pkey = cert = ca = Qnil;
|
2016-05-18 03:59:09 -04:00
|
|
|
/* 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();
|
2008-07-22 11:34:23 -04:00
|
|
|
if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s))
|
2007-04-05 01:59:22 -04:00
|
|
|
ossl_raise(ePKCS12Error, "PKCS12_parse");
|
2016-05-18 03:59:09 -04:00
|
|
|
ERR_pop_to_mark();
|
2016-09-07 21:09:19 -04:00
|
|
|
if (key) {
|
|
|
|
pkey = rb_protect((VALUE (*)(VALUE))ossl_pkey_new, (VALUE)key, &st);
|
|
|
|
if (st) goto err;
|
|
|
|
}
|
|
|
|
if (x509) {
|
|
|
|
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;
|
2003-12-11 07:29:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2008-07-22 11:34:23 -04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(str);
|
2003-12-11 07:29:08 -05:00
|
|
|
if(i2d_PKCS12(p12, &p) <= 0)
|
|
|
|
ossl_raise(ePKCS12Error, NULL);
|
|
|
|
ossl_str_adjust(str, p);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_pkcs12(void)
|
2003-12-11 07:29:08 -05:00
|
|
|
{
|
2016-08-29 01:47:09 -04:00
|
|
|
#if 0
|
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 01:59:22 -04:00
|
|
|
/*
|
|
|
|
* Defines a file format commonly used to store private keys with
|
|
|
|
* accompanying public key certificates, protected with a password-based
|
|
|
|
* symmetric key.
|
|
|
|
*/
|
|
|
|
cPKCS12 = rb_define_class_under(mOSSL, "PKCS12", rb_cObject);
|
|
|
|
ePKCS12Error = rb_define_class_under(cPKCS12, "PKCS12Error", eOSSLError);
|
|
|
|
rb_define_singleton_method(cPKCS12, "create", ossl_pkcs12_s_create, -1);
|
2003-12-11 07:29:08 -05:00
|
|
|
|
|
|
|
rb_define_alloc_func(cPKCS12, ossl_pkcs12_s_allocate);
|
2016-08-29 01:47:09 -04:00
|
|
|
rb_define_copy_func(cPKCS12, ossl_pkcs12_initialize_copy);
|
2003-12-11 07:29:08 -05:00
|
|
|
rb_attr(cPKCS12, rb_intern("key"), 1, 0, Qfalse);
|
|
|
|
rb_attr(cPKCS12, rb_intern("certificate"), 1, 0, Qfalse);
|
|
|
|
rb_attr(cPKCS12, rb_intern("ca_certs"), 1, 0, Qfalse);
|
|
|
|
rb_define_method(cPKCS12, "initialize", ossl_pkcs12_initialize, -1);
|
|
|
|
rb_define_method(cPKCS12, "to_der", ossl_pkcs12_to_der, 0);
|
|
|
|
}
|