Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
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 #undef rb_intern
236 #if 0
237  mOSSL = rb_define_module("OpenSSL");
239 #endif
240 
241  /*
242  * Defines a file format commonly used to store private keys with
243  * accompanying public key certificates, protected with a password-based
244  * symmetric key.
245  */
248  rb_define_singleton_method(cPKCS12, "create", ossl_pkcs12_s_create, -1);
249 
250  rb_define_alloc_func(cPKCS12, ossl_pkcs12_s_allocate);
251  rb_define_method(cPKCS12, "initialize_copy", ossl_pkcs12_initialize_copy, 1);
252  rb_attr(cPKCS12, rb_intern("key"), 1, 0, Qfalse);
253  rb_attr(cPKCS12, rb_intern("certificate"), 1, 0, Qfalse);
254  rb_attr(cPKCS12, rb_intern("ca_certs"), 1, 0, Qfalse);
255  rb_define_method(cPKCS12, "initialize", ossl_pkcs12_initialize, -1);
256  rb_define_method(cPKCS12, "to_der", ossl_pkcs12_to_der, 0);
257 }
GetPKeyPtr
EVP_PKEY * GetPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:432
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:670
ePKCS12Error
VALUE ePKCS12Error
Definition: ossl_pkcs12.c:31
rb_attr
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1519
rb_intern
ID rb_intern(const char *)
Definition: symbol.c:784
PRIsVALUE
#define PRIsVALUE
Definition: inttypes.h:77
ossl_obj2bio
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1094
ossl_x509_new
VALUE ossl_x509_new(X509 *)
Definition: ossl_x509cert.c:51
ossl_x509_sk2ary
int *VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs)
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:887
ossl.h
argv
char ** argv
Definition: ruby.c:243
ptr
struct RIMemo * ptr
Definition: debug.c:87
NUM2INT
#define NUM2INT
Definition: int.h:44
ossl_str_adjust
#define ossl_str_adjust(str, p)
Definition: ossl.h:88
Init_ossl_pkcs12
void Init_ossl_pkcs12(void)
Definition: ossl_pkcs12.c:233
rb_protect
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1109
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FREE_IMMEDIATELY
Definition: rtypeddata.h:62
NIL_P
#define NIL_P
Definition: special_consts.h:46
SetPKCS12
#define SetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:10
mOSSL
VALUE mOSSL
Definition: ossl.c:237
NewPKCS12
#define NewPKCS12(klass)
Definition: ossl_pkcs12.c:7
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
ossl_pkcs12_set_cert
#define ossl_pkcs12_set_cert(o, v)
Definition: ossl_pkcs12.c:21
GetX509CertPtr
X509 * GetX509CertPtr(VALUE)
Definition: ossl_x509cert.c:71
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:50
Qfalse
#define Qfalse
Definition: special_consts.h:50
rb_jump_tag
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:925
len
uint8_t len
Definition: escape.c:17
Qnil
#define Qnil
Definition: special_consts.h:51
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:299
NULL
#define NULL
Definition: regenc.h:69
ossl_pkcs12_set_ca_certs
#define ossl_pkcs12_set_ca_certs(o, v)
Definition: ossl_pkcs12.c:22
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2347
cPKCS12
VALUE cPKCS12
Definition: ossl_pkcs12.c:30
key
key
Definition: openssl_missing.h:145
VALUE
unsigned long VALUE
Definition: value.h:38
STACK_OF
STACK_OF(X509) *ossl_x509_ary2sk(VALUE)
ossl_pkcs12_set_key
#define ossl_pkcs12_set_key(o, v)
Definition: ossl_pkcs12.c:20
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
rb_check_frozen
#define rb_check_frozen
Definition: error.h:72
argc
int argc
Definition: ruby.c:242
err
int err
Definition: win32.c:143
rb_data_type_struct
Definition: rtypeddata.h:70
eOSSLError
VALUE eOSSLError
Definition: ossl.c:242
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:809
DATA_PTR
#define DATA_PTR(obj)
Definition: rdata.h:55
StringValueCStr
#define StringValueCStr(v)
Definition: rstring.h:52
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:655
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:1090
GetPKCS12
#define GetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:15
ossl_pkey_new
VALUE ossl_pkey_new(EVP_PKEY *pkey)
Definition: ossl_pkey.c:67
name
const char * name
Definition: nkf.c:208