Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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 */
36static void
37ossl_pkcs12_free(void *ptr)
38{
39 PKCS12_free(ptr);
40}
41
42static const rb_data_type_t ossl_pkcs12_type = {
43 "OpenSSL/PKCS12",
44 {
45 0, ossl_pkcs12_free,
46 },
48};
49
50static VALUE
51ossl_pkcs12_s_allocate(VALUE klass)
52{
53 PKCS12 *p12;
54 VALUE obj;
55
57 if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL);
58 SetPKCS12(obj, p12);
59
60 return obj;
61}
62
63static VALUE
64ossl_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 */
104static VALUE
105ossl_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
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
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 */
162static VALUE
163ossl_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
212static VALUE
213ossl_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)
228
229 return str;
230}
231
232void
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}
struct RIMemo * ptr
Definition: debug.c:65
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
Definition: class.c:711
VALUE rb_define_module(const char *)
Definition: class.c:785
VALUE rb_cObject
Object class.
Definition: ruby.h:2012
VALUE rb_eStandardError
Definition: error.c:921
VALUE rb_protect(VALUE(*)(VALUE), VALUE, int *)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1072
VALUE rb_eArgError
Definition: error.c:925
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:884
const char * name
Definition: nkf.c:208
VALUE mOSSL
Definition: ossl.c:231
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
VALUE eOSSLError
Definition: ossl.c:236
#define ossl_str_adjust(str, p)
Definition: ossl.h:87
int *VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs)
STACK_OF(X509) *ossl_x509_ary2sk(VALUE)
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
VALUE cPKCS12
Definition: ossl_pkcs12.c:30
#define GetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:15
#define ossl_pkcs12_set_ca_certs(o, v)
Definition: ossl_pkcs12.c:22
VALUE ePKCS12Error
Definition: ossl_pkcs12.c:31
#define ossl_pkcs12_set_cert(o, v)
Definition: ossl_pkcs12.c:21
void Init_ossl_pkcs12(void)
Definition: ossl_pkcs12.c:233
#define NewPKCS12(klass)
Definition: ossl_pkcs12.c:7
#define SetPKCS12(obj, p12)
Definition: ossl_pkcs12.c:10
#define ossl_pkcs12_set_key(o, v)
Definition: ossl_pkcs12.c:20
EVP_PKEY * GetPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:229
VALUE ossl_pkey_new(EVP_PKEY *pkey)
Definition: ossl_pkey.c:129
VALUE ossl_x509_new(X509 *)
Definition: ossl_x509cert.c:51
X509 * GetX509CertPtr(VALUE)
Definition: ossl_x509cert.c:71
#define NULL
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1180
enum ruby_tag_type st
const VALUE VALUE obj
#define rb_check_frozen(obj)
#define RSTRING_PTR(str)
#define rb_str_new(str, len)
#define NIL_P(v)
unsigned long VALUE
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
__inline__ const void *__restrict__ size_t len
#define NUM2INT(x)
void rb_define_singleton_method(VALUE, const char *, VALUE(*)(), int)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define PRIsVALUE
#define rb_scan_args(argc, argvp, fmt,...)
#define rb_intern(str)
#define Qnil
#define Qfalse
#define DATA_PTR(dta)
const VALUE * argv
void rb_define_method(VALUE, const char *, VALUE(*)(), int)
#define StringValueCStr(v)