Ruby  2.5.0dev(2017-10-22revision60238)
ossl_pkcs12.c
Go to the documentation of this file.
1 /*
2  * This program is licensed under the same licence as Ruby.
3  * (See the file 'LICENCE'.)
4  */
5 #include "ossl.h"
6 
7 #define NewPKCS12(klass) \
8  TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, 0)
9 
10 #define SetPKCS12(obj, p12) do { \
11  if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
12  RTYPEDDATA_DATA(obj) = (p12); \
13 } while (0)
14 
15 #define GetPKCS12(obj, p12) do { \
16  TypedData_Get_Struct((obj), PKCS12, &ossl_pkcs12_type, (p12)); \
17  if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
18 } while (0)
19 
20 #define ossl_pkcs12_set_key(o,v) rb_iv_set((o), "@key", (v))
21 #define ossl_pkcs12_set_cert(o,v) rb_iv_set((o), "@certificate", (v))
22 #define ossl_pkcs12_set_ca_certs(o,v) rb_iv_set((o), "@ca_certs", (v))
23 #define ossl_pkcs12_get_key(o) rb_iv_get((o), "@key")
24 #define ossl_pkcs12_get_cert(o) rb_iv_get((o), "@certificate")
25 #define ossl_pkcs12_get_ca_certs(o) rb_iv_get((o), "@ca_certs")
26 
27 /*
28  * Classes
29  */
32 
33 /*
34  * Private
35  */
36 static void
37 ossl_pkcs12_free(void *ptr)
38 {
39  PKCS12_free(ptr);
40 }
41 
42 static const rb_data_type_t ossl_pkcs12_type = {
43  "OpenSSL/PKCS12",
44  {
45  0, ossl_pkcs12_free,
46  },
48 };
49 
50 static VALUE
51 ossl_pkcs12_s_allocate(VALUE klass)
52 {
53  PKCS12 *p12;
54  VALUE obj;
55 
56  obj = NewPKCS12(klass);
57  if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL);
58  SetPKCS12(obj, p12);
59 
60  return obj;
61 }
62 
63 static VALUE
64 ossl_pkcs12_initialize_copy(VALUE self, VALUE other)
65 {
66  PKCS12 *p12, *p12_old, *p12_new;
67 
68  rb_check_frozen(self);
69  GetPKCS12(self, p12_old);
70  GetPKCS12(other, p12);
71 
72  p12_new = ASN1_dup((i2d_of_void *)i2d_PKCS12, (d2i_of_void *)d2i_PKCS12, (char *)p12);
73  if (!p12_new)
74  ossl_raise(ePKCS12Error, "ASN1_dup");
75 
76  SetPKCS12(self, p12_new);
77  PKCS12_free(p12_old);
78 
79  return self;
80 }
81 
82 /*
83  * call-seq:
84  * PKCS12.create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]])
85  *
86  * === Parameters
87  * * _pass_ - string
88  * * _name_ - A string describing the key.
89  * * _key_ - Any PKey.
90  * * _cert_ - A X509::Certificate.
91  * * The public_key portion of the certificate must contain a valid public key.
92  * * The not_before and not_after fields must be filled in.
93  * * _ca_ - An optional array of X509::Certificate's.
94  * * _key_pbe_ - string
95  * * _cert_pbe_ - string
96  * * _key_iter_ - integer
97  * * _mac_iter_ - integer
98  * * _keytype_ - An integer representing an MSIE specific extension.
99  *
100  * Any optional arguments may be supplied as +nil+ to preserve the OpenSSL defaults.
101  *
102  * See the OpenSSL documentation for PKCS12_create().
103  */
104 static VALUE
105 ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
106 {
107  VALUE pass, name, pkey, cert, ca, key_nid, cert_nid, key_iter, mac_iter, keytype;
108  VALUE obj;
109  char *passphrase, *friendlyname;
110  EVP_PKEY *key;
111  X509 *x509;
112  STACK_OF(X509) *x509s;
113  int nkey = 0, ncert = 0, kiter = 0, miter = 0, ktype = 0;
114  PKCS12 *p12;
115 
116  rb_scan_args(argc, argv, "46", &pass, &name, &pkey, &cert, &ca, &key_nid, &cert_nid, &key_iter, &mac_iter, &keytype);
117  passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
118  friendlyname = NIL_P(name) ? NULL : StringValueCStr(name);
119  key = GetPKeyPtr(pkey);
120  x509 = GetX509CertPtr(cert);
121 /* TODO: make a VALUE to nid function */
122  if (!NIL_P(key_nid)) {
123  if ((nkey = OBJ_txt2nid(StringValueCStr(key_nid))) == NID_undef)
124  ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, key_nid);
125  }
126  if (!NIL_P(cert_nid)) {
127  if ((ncert = OBJ_txt2nid(StringValueCStr(cert_nid))) == NID_undef)
128  ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, cert_nid);
129  }
130  if (!NIL_P(key_iter))
131  kiter = NUM2INT(key_iter);
132  if (!NIL_P(mac_iter))
133  miter = NUM2INT(mac_iter);
134  if (!NIL_P(keytype))
135  ktype = NUM2INT(keytype);
136 
137  obj = NewPKCS12(cPKCS12);
138  x509s = NIL_P(ca) ? NULL : ossl_x509_ary2sk(ca);
139  p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s,
140  nkey, ncert, kiter, miter, ktype);
141  sk_X509_pop_free(x509s, X509_free);
142  if(!p12) ossl_raise(ePKCS12Error, NULL);
143  SetPKCS12(obj, p12);
144 
145  ossl_pkcs12_set_key(obj, pkey);
146  ossl_pkcs12_set_cert(obj, cert);
147  ossl_pkcs12_set_ca_certs(obj, ca);
148 
149  return obj;
150 }
151 
152 /*
153  * call-seq:
154  * PKCS12.new -> pkcs12
155  * PKCS12.new(str) -> pkcs12
156  * PKCS12.new(str, pass) -> pkcs12
157  *
158  * === Parameters
159  * * _str_ - Must be a DER encoded PKCS12 string.
160  * * _pass_ - string
161  */
162 static VALUE
163 ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
164 {
165  BIO *in;
166  VALUE arg, pass, pkey, cert, ca;
167  char *passphrase;
168  EVP_PKEY *key;
169  X509 *x509;
170  STACK_OF(X509) *x509s = NULL;
171  int st = 0;
172  PKCS12 *pkcs = DATA_PTR(self);
173 
174  if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) return self;
175  passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
176  in = ossl_obj2bio(&arg);
177  d2i_PKCS12_bio(in, &pkcs);
178  DATA_PTR(self) = pkcs;
179  BIO_free(in);
180 
181  pkey = cert = ca = Qnil;
182  /* OpenSSL's bug; PKCS12_parse() puts errors even if it succeeds.
183  * Fixed in OpenSSL 1.0.0t, 1.0.1p, 1.0.2d */
184  ERR_set_mark();
185  if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s))
186  ossl_raise(ePKCS12Error, "PKCS12_parse");
187  ERR_pop_to_mark();
188  if (key) {
189  pkey = rb_protect((VALUE (*)(VALUE))ossl_pkey_new, (VALUE)key, &st);
190  if (st) goto err;
191  }
192  if (x509) {
193  cert = rb_protect((VALUE (*)(VALUE))ossl_x509_new, (VALUE)x509, &st);
194  if (st) goto err;
195  }
196  if (x509s) {
197  ca = rb_protect((VALUE (*)(VALUE))ossl_x509_sk2ary, (VALUE)x509s, &st);
198  if (st) goto err;
199  }
200 
201  err:
202  X509_free(x509);
203  sk_X509_pop_free(x509s, X509_free);
204  ossl_pkcs12_set_key(self, pkey);
205  ossl_pkcs12_set_cert(self, cert);
206  ossl_pkcs12_set_ca_certs(self, ca);
207  if(st) rb_jump_tag(st);
208 
209  return self;
210 }
211 
212 static VALUE
213 ossl_pkcs12_to_der(VALUE self)
214 {
215  PKCS12 *p12;
216  VALUE str;
217  long len;
218  unsigned char *p;
219 
220  GetPKCS12(self, p12);
221  if((len = i2d_PKCS12(p12, NULL)) <= 0)
223  str = rb_str_new(0, len);
224  p = (unsigned char *)RSTRING_PTR(str);
225  if(i2d_PKCS12(p12, &p) <= 0)
227  ossl_str_adjust(str, p);
228 
229  return str;
230 }
231 
232 void
234 {
235 #if 0
236  mOSSL = rb_define_module("OpenSSL");
238 #endif
239 
240  /*
241  * Defines a file format commonly used to store private keys with
242  * accompanying public key certificates, protected with a password-based
243  * symmetric key.
244  */
247  rb_define_singleton_method(cPKCS12, "create", ossl_pkcs12_s_create, -1);
248 
249  rb_define_alloc_func(cPKCS12, ossl_pkcs12_s_allocate);
250  rb_define_method(cPKCS12, "initialize_copy", ossl_pkcs12_initialize_copy, 1);
251  rb_attr(cPKCS12, rb_intern("key"), 1, 0, Qfalse);
252  rb_attr(cPKCS12, rb_intern("certificate"), 1, 0, Qfalse);
253  rb_attr(cPKCS12, rb_intern("ca_certs"), 1, 0, Qfalse);
254  rb_define_method(cPKCS12, "initialize", ossl_pkcs12_initialize, -1);
255  rb_define_method(cPKCS12, "to_der", ossl_pkcs12_to_der, 0);
256 }
VALUE mOSSL
Definition: ossl.c:231
int *VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs)
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:992
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1138
#define NUM2INT(x)
Definition: ruby.h:684
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:821
#define ossl_str_adjust(str, p)
Definition: ossl.h:82
#define NewPKCS12(klass)
Definition: ossl_pkcs12.c:7
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
#define DATA_PTR(dta)
Definition: ruby.h:1106
VALUE ossl_pkey_new(EVP_PKEY *pkey)
Definition: ossl_pkey.c:107
VALUE rb_eArgError
Definition: error.c:802
VALUE cPKCS12
Definition: ossl_pkcs12.c:30
X509 * GetX509CertPtr(VALUE)
Definition: ossl_x509cert.c:71
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
STACK_OF(X509) *ossl_x509_ary2sk(VALUE)
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1137
#define GetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:15
VALUE ePKCS12Error
Definition: ossl_pkcs12.c:31
#define NIL_P(v)
Definition: ruby.h:451
VALUE eOSSLError
Definition: ossl.c:236
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
VALUE ossl_x509_new(X509 *)
Definition: ossl_x509cert.c:51
#define ossl_pkcs12_set_cert(o, v)
Definition: ossl_pkcs12.c:21
int err
Definition: win32.c:135
void Init_ossl_pkcs12(void)
Definition: ossl_pkcs12.c:233
#define ossl_pkcs12_set_key(o, v)
Definition: ossl_pkcs12.c:20
#define ossl_pkcs12_set_ca_certs(o, v)
Definition: ossl_pkcs12.c:22
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
#define PRIsVALUE
Definition: ruby.h:135
#define Qnil
Definition: ruby.h:438
VALUE rb_eStandardError
Definition: error.c:799
unsigned long VALUE
Definition: ruby.h:85
#define SetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:10
register unsigned int len
Definition: zonetab.h:51
#define StringValueCStr(v)
Definition: ruby.h:571
#define RSTRING_PTR(str)
Definition: ruby.h:975
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
EVP_PKEY * GetPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:206
const char * name
Definition: nkf.c:208
#define rb_check_frozen(obj)
Definition: intern.h:271
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define rb_intern(str)
#define NULL
Definition: _sdbm.c:102
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
char ** argv
Definition: ruby.c:188
VALUE rb_str_new(const char *, long)
Definition: string.c:737