Ruby  2.4.2p198(2017-09-14revision59899)
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 SafeGetPKCS12(obj, p12) do { \
21  OSSL_Check_Kind((obj), cPKCS12); \
22  GetPKCS12((obj), (p12)); \
23 } while (0)
24 
25 #define ossl_pkcs12_set_key(o,v) rb_iv_set((o), "@key", (v))
26 #define ossl_pkcs12_set_cert(o,v) rb_iv_set((o), "@certificate", (v))
27 #define ossl_pkcs12_set_ca_certs(o,v) rb_iv_set((o), "@ca_certs", (v))
28 #define ossl_pkcs12_get_key(o) rb_iv_get((o), "@key")
29 #define ossl_pkcs12_get_cert(o) rb_iv_get((o), "@certificate")
30 #define ossl_pkcs12_get_ca_certs(o) rb_iv_get((o), "@ca_certs")
31 
32 /*
33  * Classes
34  */
37 
38 /*
39  * Private
40  */
41 static void
42 ossl_pkcs12_free(void *ptr)
43 {
44  PKCS12_free(ptr);
45 }
46 
48  "OpenSSL/PKCS12",
49  {
51  },
53 };
54 
55 static VALUE
57 {
58  PKCS12 *p12;
59  VALUE obj;
60 
61  obj = NewPKCS12(klass);
62  if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL);
63  SetPKCS12(obj, p12);
64 
65  return obj;
66 }
67 
68 static VALUE
70 {
71  PKCS12 *p12, *p12_old, *p12_new;
72 
73  rb_check_frozen(self);
74  GetPKCS12(self, p12_old);
75  SafeGetPKCS12(other, p12);
76 
77  p12_new = ASN1_dup((i2d_of_void *)i2d_PKCS12, (d2i_of_void *)d2i_PKCS12, (char *)p12);
78  if (!p12_new)
79  ossl_raise(ePKCS12Error, "ASN1_dup");
80 
81  SetPKCS12(self, p12_new);
82  PKCS12_free(p12_old);
83 
84  return self;
85 }
86 
87 /*
88  * call-seq:
89  * PKCS12.create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]])
90  *
91  * === Parameters
92  * * +pass+ - string
93  * * +name+ - A string describing the key.
94  * * +key+ - Any PKey.
95  * * +cert+ - A X509::Certificate.
96  * * The public_key portion of the certificate must contain a valid public key.
97  * * The not_before and not_after fields must be filled in.
98  * * +ca+ - An optional array of X509::Certificate's.
99  * * +key_pbe+ - string
100  * * +cert_pbe+ - string
101  * * +key_iter+ - integer
102  * * +mac_iter+ - integer
103  * * +keytype+ - An integer representing an MSIE specific extension.
104  *
105  * Any optional arguments may be supplied as nil to preserve the OpenSSL defaults.
106  *
107  * See the OpenSSL documentation for PKCS12_create().
108  */
109 static VALUE
111 {
112  VALUE pass, name, pkey, cert, ca, key_nid, cert_nid, key_iter, mac_iter, keytype;
113  VALUE obj;
114  char *passphrase, *friendlyname;
115  EVP_PKEY *key;
116  X509 *x509;
117  STACK_OF(X509) *x509s;
118  int nkey = 0, ncert = 0, kiter = 0, miter = 0, ktype = 0;
119  PKCS12 *p12;
120 
121  rb_scan_args(argc, argv, "46", &pass, &name, &pkey, &cert, &ca, &key_nid, &cert_nid, &key_iter, &mac_iter, &keytype);
122  passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
123  friendlyname = NIL_P(name) ? NULL : StringValueCStr(name);
124  key = GetPKeyPtr(pkey);
125  x509 = GetX509CertPtr(cert);
126 /* TODO: make a VALUE to nid function */
127  if (!NIL_P(key_nid)) {
128  if ((nkey = OBJ_txt2nid(StringValueCStr(key_nid))) == NID_undef)
129  ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, key_nid);
130  }
131  if (!NIL_P(cert_nid)) {
132  if ((ncert = OBJ_txt2nid(StringValueCStr(cert_nid))) == NID_undef)
133  ossl_raise(rb_eArgError, "Unknown PBE algorithm %"PRIsVALUE, cert_nid);
134  }
135  if (!NIL_P(key_iter))
136  kiter = NUM2INT(key_iter);
137  if (!NIL_P(mac_iter))
138  miter = NUM2INT(mac_iter);
139  if (!NIL_P(keytype))
140  ktype = NUM2INT(keytype);
141 
142  obj = NewPKCS12(cPKCS12);
143  x509s = NIL_P(ca) ? NULL : ossl_x509_ary2sk(ca);
144  p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s,
145  nkey, ncert, kiter, miter, ktype);
146  sk_X509_pop_free(x509s, X509_free);
147  if(!p12) ossl_raise(ePKCS12Error, NULL);
148  SetPKCS12(obj, p12);
149 
150  ossl_pkcs12_set_key(obj, pkey);
151  ossl_pkcs12_set_cert(obj, cert);
152  ossl_pkcs12_set_ca_certs(obj, ca);
153 
154  return obj;
155 }
156 
157 /*
158  * call-seq:
159  * PKCS12.new -> pkcs12
160  * PKCS12.new(str) -> pkcs12
161  * PKCS12.new(str, pass) -> pkcs12
162  *
163  * === Parameters
164  * * +str+ - Must be a DER encoded PKCS12 string.
165  * * +pass+ - string
166  */
167 static VALUE
169 {
170  BIO *in;
171  VALUE arg, pass, pkey, cert, ca;
172  char *passphrase;
173  EVP_PKEY *key;
174  X509 *x509;
175  STACK_OF(X509) *x509s = NULL;
176  int st = 0;
177  PKCS12 *pkcs = DATA_PTR(self);
178 
179  if(rb_scan_args(argc, argv, "02", &arg, &pass) == 0) return self;
180  passphrase = NIL_P(pass) ? NULL : StringValueCStr(pass);
181  in = ossl_obj2bio(&arg);
182  d2i_PKCS12_bio(in, &pkcs);
183  DATA_PTR(self) = pkcs;
184  BIO_free(in);
185 
186  pkey = cert = ca = Qnil;
187  /* OpenSSL's bug; PKCS12_parse() puts errors even if it succeeds.
188  * Fixed in OpenSSL 1.0.0t, 1.0.1p, 1.0.2d */
189  ERR_set_mark();
190  if(!PKCS12_parse(pkcs, passphrase, &key, &x509, &x509s))
191  ossl_raise(ePKCS12Error, "PKCS12_parse");
192  ERR_pop_to_mark();
193  if (key) {
194  pkey = rb_protect((VALUE (*)(VALUE))ossl_pkey_new, (VALUE)key, &st);
195  if (st) goto err;
196  }
197  if (x509) {
198  cert = rb_protect((VALUE (*)(VALUE))ossl_x509_new, (VALUE)x509, &st);
199  if (st) goto err;
200  }
201  if (x509s) {
202  ca = rb_protect((VALUE (*)(VALUE))ossl_x509_sk2ary, (VALUE)x509s, &st);
203  if (st) goto err;
204  }
205 
206  err:
207  X509_free(x509);
208  sk_X509_pop_free(x509s, X509_free);
209  ossl_pkcs12_set_key(self, pkey);
210  ossl_pkcs12_set_cert(self, cert);
211  ossl_pkcs12_set_ca_certs(self, ca);
212  if(st) rb_jump_tag(st);
213 
214  return self;
215 }
216 
217 static VALUE
219 {
220  PKCS12 *p12;
221  VALUE str;
222  long len;
223  unsigned char *p;
224 
225  GetPKCS12(self, p12);
226  if((len = i2d_PKCS12(p12, NULL)) <= 0)
228  str = rb_str_new(0, len);
229  p = (unsigned char *)RSTRING_PTR(str);
230  if(i2d_PKCS12(p12, &p) <= 0)
232  ossl_str_adjust(str, p);
233 
234  return str;
235 }
236 
237 void
239 {
240 #if 0
241  mOSSL = rb_define_module("OpenSSL");
243 #endif
244 
245  /*
246  * Defines a file format commonly used to store private keys with
247  * accompanying public key certificates, protected with a password-based
248  * symmetric key.
249  */
253 
256  rb_attr(cPKCS12, rb_intern("key"), 1, 0, Qfalse);
257  rb_attr(cPKCS12, rb_intern("certificate"), 1, 0, Qfalse);
258  rb_attr(cPKCS12, rb_intern("ca_certs"), 1, 0, Qfalse);
259  rb_define_method(cPKCS12, "initialize", ossl_pkcs12_initialize, -1);
261 }
static const rb_data_type_t ossl_pkcs12_type
Definition: ossl_pkcs12.c:47
VALUE rb_eStandardError
Definition: error.c:760
VALUE mOSSL
Definition: ossl.c:213
int *VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs)
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1145
#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
static VALUE ossl_pkcs12_initialize_copy(VALUE self, VALUE other)
Definition: ossl_pkcs12.c:69
#define ossl_str_adjust(str, p)
Definition: ossl.h:82
static VALUE ossl_pkcs12_initialize(int argc, VALUE *argv, VALUE self)
Definition: ossl_pkcs12.c:168
#define NewPKCS12(klass)
Definition: ossl_pkcs12.c:7
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:891
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 SafeGetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:20
STACK_OF(X509) *ossl_x509_ary2sk0(VALUE)
#define DATA_PTR(dta)
Definition: ruby.h:1113
VALUE ossl_pkey_new(EVP_PKEY *pkey)
Definition: ossl_pkey.c:107
VALUE cPKCS12
Definition: ossl_pkcs12.c:35
#define rb_define_copy_func(klass, func)
Definition: ruby_missing.h:13
X509 * GetX509CertPtr(VALUE)
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1138
#define GetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:15
VALUE ePKCS12Error
Definition: ossl_pkcs12.c:36
#define NIL_P(v)
Definition: ruby.h:451
VALUE eOSSLError
Definition: ossl.c:218
int argc
Definition: ruby.c:183
#define Qfalse
Definition: ruby.h:436
VALUE ossl_x509_new(X509 *)
Definition: ossl_x509cert.c:55
#define ossl_pkcs12_set_cert(o, v)
Definition: ossl_pkcs12.c:26
int err
Definition: win32.c:135
static VALUE ossl_pkcs12_s_allocate(VALUE klass)
Definition: ossl_pkcs12.c:56
void Init_ossl_pkcs12(void)
Definition: ossl_pkcs12.c:238
#define ossl_pkcs12_set_key(o, v)
Definition: ossl_pkcs12.c:25
#define ossl_pkcs12_set_ca_certs(o, v)
Definition: ossl_pkcs12.c:27
static void ossl_pkcs12_free(void *ptr)
Definition: ossl_pkcs12.c:42
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
#define PRIsVALUE
Definition: ruby.h:135
#define Qnil
Definition: ruby.h:438
unsigned long VALUE
Definition: ruby.h:85
#define SetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:10
void rb_jump_tag(int tag)
Definition: eval.c:788
register unsigned int len
Definition: zonetab.h:51
#define StringValueCStr(v)
Definition: ruby.h:571
#define RSTRING_PTR(str)
Definition: ruby.h:982
static VALUE ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
Definition: ossl_pkcs12.c:110
static VALUE ossl_pkcs12_to_der(VALUE self)
Definition: ossl_pkcs12.c:218
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:278
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:276
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
VALUE rb_eArgError
Definition: error.c:763
char ** argv
Definition: ruby.c:184
VALUE rb_str_new(const char *, long)
Definition: string.c:736