PKCS12.xs

filter: Statementstmt 78.8% 562 / 713 Branchbran 58.3% 280 / 480 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.5% 842 / 1193 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Crypt/OpenSSL/PKCS12.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_newCONSTSUB
6 #include "ppport.h"
7  
8 #include <openssl/crypto.h>
9 #include <openssl/err.h>
10 #include <openssl/pem.h>
11 #include <openssl/pkcs12.h>
12 #include <openssl/x509.h>
13 #include <openssl/ssl.h>
14 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
15 #include <openssl/provider.h>
16 OSSL_PROVIDER *legacy = NULL;
17 OSSL_PROVIDER *deflt = NULL;
18 #endif
19 #define NOKEYS 0x1
20 #define NOCERTS 0x2
21 #define INFO 0x4
22 #define CLCERTS 0x8
23 #define CACERTS 0x10
24  
25 #if OPENSSL_VERSION_NUMBER < 0x10100000L
26 #define PKCS12_SAFEBAG_get0_p8inf(o) ((o)->value.keybag)
27 #define PKCS12_SAFEBAG_get0_attr PKCS12_get_attr
28 #define PKCS12_SAFEBAG_get_bag_nid M_PKCS12_cert_bag_type
29 #define PKCS12_SAFEBAG_get_nid M_PKCS12_bag_type
30 #define PKCS12_SAFEBAG_get1_cert PKCS12_certbag2x509
31 #define PKCS12_SAFEBAG_get0_safes(o) ((o)->value.safes)
32 #define PKCS12_SAFEBAG_get0_type(o) (o)->type
33 #define PKCS8_pkey_get0_attrs(o) ((o)->attributes)
34 #define PKCS12_SAFEBAG_get0_attrs(o) ((o)->attrib)
35 #define CONST_PKCS8_PRIV_KEY_INFO PKCS8_PRIV_KEY_INFO
36 #define CONST_X509_ALGOR X509_ALGOR
37 #define CONST_X509_SIG X509_SIG
38 #define CONST_ASN1_TYPE ASN1_TYPE
39 #define CONST_STACK_OF(o) STACK_OF(o)
40 #define CONST_X509_NAME X509_NAME
41 #define CONST_ASN1_INTEGER ASN1_INTEGER
42 #define CONST_ASN1_OBJECT ASN1_OBJECT
43 #define CONST_ASN1_OCTET_STRING ASN1_OCTET_STRING
44 #define CONST_VOID void
45 #define ASN1_STRING_get0_data(x) ((const unsigned char *)ASN1_STRING_data(x))
46 #else
47 #define CONST_PKCS8_PRIV_KEY_INFO const PKCS8_PRIV_KEY_INFO
48 #define CONST_X509_ALGOR const X509_ALGOR
49 #define CONST_X509_SIG const X509_SIG
50 #define CONST_ASN1_TYPE const ASN1_TYPE
51 #define CONST_STACK_OF(o) const STACK_OF(o)
52 #define CONST_X509_NAME const X509_NAME
53 #define CONST_ASN1_INTEGER const ASN1_INTEGER
54 #define CONST_ASN1_OBJECT const ASN1_OBJECT
55 #define CONST_ASN1_OCTET_STRING const ASN1_OCTET_STRING
56 #define CONST_VOID const void
57 #endif
58  
59 const EVP_CIPHER *enc;
60 int dump_certs_pkeys_bags(pTHX_ BIO *out, CONST_STACK_OF(PKCS12_SAFEBAG) *bags,
61 const char *pass, int passlen, int options,
62 char *pempass, const EVP_CIPHER *enc, HV* hash);
63 static int alg_print(pTHX_ BIO *bio, CONST_X509_ALGOR *alg, HV* hash);
64 void print_attribute(pTHX_ BIO *out, CONST_ASN1_TYPE *av, char **value);
65 int print_attribs(pTHX_ BIO *out, CONST_STACK_OF(X509_ATTRIBUTE) *attrlst, const char *name, HV *hash);
66 void hex_prin(BIO *out, const unsigned char *buf, int len);
67 void dump_cert_text(BIO *out, X509 *x);
68 SV * get_cert_subject_name(pTHX_ X509 *x);
69 SV * get_cert_issuer_name(pTHX_ X509 *x);
70  
71 #define CHECK_OPEN_SSL(p_result) if (!(p_result)) croakSSL(__FILE__, __LINE__);
72 #define PACKAGE_CROAK(p_message) croak("%s", (p_message))
73 #define CHECK_NEW(p_var, p_size, p_type) \
74 if (New(0, p_var, p_size, p_type) == NULL) \
75 { PACKAGE_CROAK("unable to alloc buffer"); }
76  
77 /* fake our package name */
78 typedef PKCS12* Crypt__OpenSSL__PKCS12;
79  
80 0 void croakSSL(char* p_file, int p_line) {
81  
82 const char* errorReason;
83  
84 /* Just return the top error on the stack */
85 0 errorReason = ERR_reason_error_string(ERR_get_error());
86  
87 0 ERR_clear_error();
88  
89 0 croak("%s:%d: OpenSSL error: %s", p_file, p_line, errorReason);
90 }
91  
92 4 EVP_PKEY* _load_pkey(char* keyString, EVP_PKEY*(*p_loader)(BIO*, EVP_PKEY**, pem_password_cb*, void*)) {
93  
94 EVP_PKEY* pkey;
95 BIO* stringBIO;
96  
97 4 ▶ if (!strncmp(keyString, "----", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strncmp(keyString, "----", 4)) { 0 4 4
98  
99 0 ▶ CHECK_OPEN_SSL(stringBIO = BIO_new_mem_buf(keyString, strlen(keyString)));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(stringBIO = BIO_new_mem_buf(keyString, strlen(keyString))); 0 0 0
100  
101 } else {
102  
103 4 ▶ CHECK_OPEN_SSL(stringBIO = BIO_new_file(keyString, "r"));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(stringBIO = BIO_new_file(keyString, "r")); 0 4 4
104 }
105  
106 4 pkey = p_loader(stringBIO, NULL, NULL, NULL);
107  
108 4 (void)BIO_set_close(stringBIO, BIO_CLOSE);
109 4 BIO_free_all(stringBIO);
110  
111 4 ▶ CHECK_OPEN_SSL(pkey);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(pkey); 0 4 4
112 4 return pkey;
113 }
114  
115 4 STACK_OF(X509)* _load_cert_chain(char* keyString, STACK_OF(X509_INFO)*(*p_loader)(BIO*, STACK_OF(X509_INFO)*, pem_password_cb*, void*)) {
116 int i;
117 4 STACK_OF(X509_INFO) *xis = NULL;
118 4 X509_INFO *xi = NULL;
119 BIO* stringBIO;
120 4 STACK_OF(X509) *stack = sk_X509_new_null();
121  
122 4 ▶ if (!strncmp(keyString, "----", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strncmp(keyString, "----", 4)) { 0 4 4
123 0 ▶ CHECK_OPEN_SSL(stringBIO = BIO_new_mem_buf(keyString, strlen(keyString)));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(stringBIO = BIO_new_mem_buf(keyString, strlen(keyString))); 0 0 0
124 } else {
125 4 ▶ CHECK_OPEN_SSL(stringBIO = BIO_new_file(keyString, "r"));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(stringBIO = BIO_new_file(keyString, "r")); 0 4 4
126 }
127  
128 4 xis = p_loader(stringBIO, NULL, NULL, NULL);
129 8 ▶ for (i = 0; i < sk_X509_INFO_num(xis); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sk_X509_INFO_num(xis); i++) { 4 4 8
130 4 xi = sk_X509_INFO_value(xis, i);
131 4 ▶ if (xi->x509 != NULL && stack != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (xi->x509 != NULL && stack != NULL) { 4 0 4
if (xi->x509 != NULL && stack != NULL) { 4 0 4
132 4 ▶ CHECK_OPEN_SSL(xi->x509);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(xi->x509); 0 4 4
133 4 ▶ if (!sk_X509_push(stack, xi->x509))
Branch: 1 branch
BranchTrueFalseTotal
if (!sk_X509_push(stack, xi->x509)) 0 4 4
134 0 goto end;
135 4 xi->x509 = NULL;
136 }
137 }
138  
139 4 end:
140 4 sk_X509_INFO_pop_free(xis, X509_INFO_free);
141 4 (void)BIO_set_close(stringBIO, BIO_CLOSE);
142 4 BIO_free_all(stringBIO);
143  
144 4 return stack;
145 }
146  
147 0 static void sv_bio_error(pTHX_ BIO *bio) {
148  
149 0 SV* sv = (SV *)BIO_get_callback_arg(bio);
150 0 ▶ if (sv) sv_free(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (sv) sv_free(sv); 0 0 0
151  
152 0 BIO_free_all (bio);
153 0 }
154  
155 462 SV* extractBioString(pTHX_ BIO* p_stringBio)
156 {
157 SV* sv;
158 char *datap;
159 462 long datasize = 0;
160
161 462 ▶ CHECK_OPEN_SSL(BIO_flush(p_stringBio) == 1);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(BIO_flush(p_stringBio) == 1); 0 462 462
162  
163 462 datasize = BIO_get_mem_data(p_stringBio, &datap);
164 462 sv = newSVpv(datap, datasize);
165  
166 462 ▶ CHECK_OPEN_SSL(BIO_set_close(p_stringBio, BIO_CLOSE) == 1);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(BIO_set_close(p_stringBio, BIO_CLOSE) == 1); 0 462 462
167 462 BIO_free(p_stringBio);
168 462 return sv;
169 }
170  
171 7 static const char *ssl_error(pTHX) {
172 BIO *bio;
173 SV *sv;
174 STRLEN l;
175  
176 7 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 7 7
177 7 ERR_print_errors(bio);
178 7 sv = extractBioString(aTHX_ bio);
179 7 ERR_clear_error();
180  
181 7 ▶ if (!sv || !SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) { 7 0 7
if (!sv || !SvOK(sv)) { 1 6 7
182 1 return ""; // Return an empty C string if no actual error messages were found
183 }
184 6 return SvPV(sv, l);
185 }
186  
187 /* these are trimmed from their openssl/apps/pkcs12.c counterparts */
188 174 int dump_certs_pkeys_bag (pTHX_ BIO *bio, PKCS12_SAFEBAG *bag, const char *pass, int passlen, int options, char *pempass, const EVP_CIPHER *enc, HV * bag_hv) {
189  
190 EVP_PKEY *pkey;
191 X509 *x509;
192 174 CONST_PKCS8_PRIV_KEY_INFO *p8 = NULL;
193 CONST_STACK_OF(X509_ATTRIBUTE) *bag_attrs;
194 CONST_STACK_OF(X509_ATTRIBUTE) *key_attrs;
195  
196 174 bag_attrs = PKCS12_SAFEBAG_get0_attrs(bag);
197  
198 /* FIXME: Change NULL to default_enc and allow the encryption to ne overridden like no-des */
199 #ifndef OPENSSL_NO_DES
200 174 EVP_CIPHER *default_enc = (EVP_CIPHER *)EVP_des_ede3_cbc();
201 174 enc = NULL; /*default_enc; */
202 #else
203 EVP_CIPHER *default_enc = (EVP_CIPHER *)EVP_aes_256_cbc();
204 enc = default_enc;
205 #endif
206  
207 174 switch (PKCS12_SAFEBAG_get_nid(bag)) {
208  
209 2 case NID_keyBag: ;
210  
211 2 ▶ if (options & NOKEYS) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (options & NOKEYS) return 1; 0 2 2
212  
213 2 p8 = PKCS12_SAFEBAG_get0_p8inf(bag);
214  
215 2 ▶ if (!(pkey = EVP_PKCS82PKEY (p8))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(pkey = EVP_PKCS82PKEY (p8))) return 0; 0 2 2
216  
217 2 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 2 0 2
218 2 key_attrs = PKCS8_pkey_get0_attrs(p8);
219 2 ▶ if (bag_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (bag_hv) { 1 1 2
220 1 SV * value = newSVpvn("key_bag", strlen("key_bag"));
221 1 ▶ if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL) 0 1 1
222 0 croak("unable to add certificate_bag to the bag_hv");
223  
224 /* Assign the output to a temporary BIO and free after it is saved to key_sv */
225 BIO *keybio;
226 1 ▶ CHECK_OPEN_SSL(keybio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(keybio = BIO_new(BIO_s_mem())); 0 1 1
227 1 PEM_write_bio_PrivateKey (keybio, pkey, enc, NULL, 0, NULL, pempass);
228 1 SV * key_sv = extractBioString(aTHX_ keybio);
229  
230 1 ▶ if((hv_store(bag_hv, "key", strlen("key"), key_sv, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "key", strlen("key"), key_sv, 0)) == NULL) 0 1 1
231 0 croak("unable to add certificate_bag to the bag_hv");
232 1 print_attribs(aTHX_ bio, bag_attrs, "bag_attributes", bag_hv);
233 1 print_attribs(aTHX_ bio, key_attrs, "key_attributes", bag_hv);
234 } else {
235 1 BIO_printf(bio, "Key bag\n");
236 1 print_attribs(aTHX_ bio, bag_attrs, "Bag Attributes", NULL);
237 1 print_attribs(aTHX_ bio, key_attrs, "Key Attributes", NULL);
238 1 PEM_write_bio_PrivateKey (bio, pkey, enc, NULL, 0, NULL, pempass);
239 }
240 } else {
241 0 PEM_write_bio_PrivateKey (bio, pkey, enc, NULL, 0, NULL, pempass);
242 }
243  
244 2 EVP_PKEY_free(pkey);
245  
246 2 break;
247  
248 18 case NID_pkcs8ShroudedKeyBag: ;
249  
250 18 ▶ if (options & NOKEYS) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (options & NOKEYS) return 1; 5 13 18
251  
252 13 ▶ if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL) 0 13 13
253 0 return 0;
254 13 ▶ if ((pkey = EVP_PKCS82PKEY (p8)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((pkey = EVP_PKCS82PKEY (p8)) == NULL) { 0 13 13
255 0 PKCS8_PRIV_KEY_INFO_free((PKCS8_PRIV_KEY_INFO *) p8);
256 0 return 0;
257 }
258 13 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 10 3 13
259 CONST_X509_ALGOR *tp8alg;
260 #if OPENSSL_VERSION_NUMBER > 0x10100000L
261 CONST_X509_SIG *tp8;
262  
263 10 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
264 10 X509_SIG_get0(tp8, &tp8alg, NULL);
265 #else
266 tp8alg = bag->value.shkeybag->algor;
267 #endif
268 10 key_attrs = PKCS8_pkey_get0_attrs(p8);
269 10 ▶ if (bag_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (bag_hv) { 5 5 10
270 5 SV *str_bag = newSVpvn("shrouded_bag", strlen("shrouded_bag"));
271 5 HV * parameters_hv = newHV();;
272 5 ▶ if((hv_store(bag_hv, "type", strlen("type"), str_bag, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "type", strlen("type"), str_bag, 0)) == NULL) 0 5 5
273 0 croak("unable to add type to the bag_hv");
274 #if OPENSSL_VERSION_NUMBER > 0x10000000L
275 5 alg_print(aTHX_ bio, tp8alg, parameters_hv);
276 #endif
277 5 print_attribs(aTHX_ bio, bag_attrs, "bag_attributes", bag_hv);
278 5 ▶ if((hv_store(bag_hv, "parameters", strlen("parameters"), newRV_inc((SV *) parameters_hv), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "parameters", strlen("parameters"), newRV_inc((SV *) parameters_hv), 0)) == NULL) 0 5 5
279 0 croak("unable to add bag_attributes to the bag_hv");
280  
281 5 SV *str_key_bag = newSVpvn("shrouded_keybag", strlen("shrouded_keybag"));
282 5 ▶ if((hv_store(bag_hv, "type", strlen("type"), str_key_bag, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "type", strlen("type"), str_key_bag, 0)) == NULL) 0 5 5
283 0 croak("unable to add type to the bag_hv");
284  
285 5 print_attribs(aTHX_ bio, key_attrs, "key_attributes", bag_hv);
286  
287 /* Assign the output to a temporary BIO and free after it is saved to key_sv */
288 BIO *keybio;
289 5 ▶ CHECK_OPEN_SSL(keybio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(keybio = BIO_new(BIO_s_mem())); 0 5 5
290 5 PEM_write_bio_PrivateKey (keybio, pkey, enc, NULL, 0, NULL, pempass);
291 5 SV * key_sv = extractBioString(aTHX_ keybio);
292  
293 5 ▶ if((hv_store(bag_hv, "key", strlen("key"), key_sv, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "key", strlen("key"), key_sv, 0)) == NULL) 0 5 5
294 0 croak("unable to add certificate_bag to the bag_hv");
295 } else {
296 5 BIO_printf(bio, "Shrouded Keybag: ");
297 5 alg_print(aTHX_ bio, tp8alg, NULL);
298 5 print_attribs(aTHX_ bio, bag_attrs, "Bag Attributes", NULL);
299  
300 5 print_attribs(aTHX_ bio, key_attrs, "Key Attributes", NULL);
301 5 PEM_write_bio_PrivateKey (bio, pkey, enc, NULL, 0, NULL, pempass);
302 }
303 } else {
304 3 PEM_write_bio_PrivateKey (bio, pkey, enc, NULL, 0, NULL, pempass);
305 }
306  
307 13 PKCS8_PRIV_KEY_INFO_free((PKCS8_PRIV_KEY_INFO *)p8);
308  
309 13 EVP_PKEY_free(pkey);
310  
311 13 break;
312  
313 99 case NID_certBag:
314  
315 99 ▶ if (options & NOCERTS) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (options & NOCERTS) return 1; 6 93 99
316  
317 93 ▶ if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) { 21 72 93
318  
319 21 ▶ if (options & CACERTS) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (options & CACERTS) return 1; 1 20 21
320  
321 72 ▶ } else if (options & CLCERTS) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (options & CLCERTS) { 3 69 72
322  
323 3 return 1;
324 }
325  
326 89 ▶ if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate) return 1; 0 89 89
327  
328 89 ▶ if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL) return 0; 0 89 89
329 89 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 84 5 89
330 84 ▶ if (bag_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (bag_hv) { 68 16 84
331 BIO *keybio;
332 68 ▶ CHECK_OPEN_SSL(keybio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(keybio = BIO_new(BIO_s_mem())); 0 68 68
333 68 SV * value = newSVpvn("certificate_bag", strlen("certificate_bag"));
334 68 print_attribs(aTHX_ keybio, bag_attrs, "bag_attributes", bag_hv);
335 68 ▶ if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL) 0 68 68
336 0 croak("unable to add type to the bag_hv");
337 68 ▶ if((hv_store(bag_hv, "subject", strlen("subject"), get_cert_subject_name(aTHX_ x509), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "subject", strlen("subject"), get_cert_subject_name(aTHX_ x509), 0)) == NULL) 0 68 68
338 0 croak("unable to add subject to the bag_hv");
339 68 ▶ if((hv_store(bag_hv, "issuer", strlen("issuer"), get_cert_issuer_name(aTHX_ x509), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "issuer", strlen("issuer"), get_cert_issuer_name(aTHX_ x509), 0)) == NULL) 0 68 68
340 0 croak("unable to add issuer to the bag_hv");
341 68 PEM_write_bio_X509 (keybio, x509);
342 68 SV * key_sv = extractBioString(aTHX_ keybio);
343 68 ▶ if((hv_store(bag_hv, "cert", strlen("cert"), key_sv, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "cert", strlen("cert"), key_sv, 0)) == NULL) 0 68 68
344 0 croak("unable to add certificate_bag to the bag_hv");
345 } else
346 {
347 16 BIO_printf(bio, "Certificate bag\n");
348 16 print_attribs(aTHX_ bio, bag_attrs, "Bag Attributes", NULL);
349 16 dump_cert_text(bio, x509);
350 16 PEM_write_bio_X509 (bio, x509);
351 }
352 } else {
353 5 PEM_write_bio_X509 (bio, x509);
354 }
355  
356 89 X509_free(x509);
357  
358 89 break;
359  
360 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
361 53 case NID_secretBag:
362 /*FIXME: Not sure how to test this */
363 53 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 53 0 53
364 53 char * attribute_value = NULL;
365 53 ▶ if (bag_hv){
Branch: 1 branch
BranchTrueFalseTotal
if (bag_hv){ 52 1 53
366 52 SV * value = newSVpvn("secret_bag", strlen("secret_bag"));
367 52 ▶ if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL) 0 52 52
368 0 croak("unable to add type to the bag_hv");
369 52 print_attribs(aTHX_ bio, bag_attrs, "bag_attributes", bag_hv);
370 52 Newx(attribute_value, 0, char);
371 52 print_attribute(aTHX_ bio, PKCS12_SAFEBAG_get0_bag_obj(bag), &attribute_value);
372 52 ▶ if(hv_store(bag_hv, "bag_value", strlen("bag_value"), newSVpvn(attribute_value, strlen(attribute_value)), 0) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(hv_store(bag_hv, "bag_value", strlen("bag_value"), newSVpvn(attribute_value, strlen(attribute_value)), 0) == NULL) 0 52 52
373 0 croak("unable to add MAC to the bag_hv");
374 }
375 else {
376 1 BIO_printf(bio, "Secret bag\n");
377 1 print_attribs(aTHX_ bio, bag_attrs, "Bag Attributes", NULL);
378 1 BIO_printf(bio, "Bag Type: ");
379 1 i2a_ASN1_OBJECT(bio, PKCS12_SAFEBAG_get0_bag_type(bag));
380 1 BIO_printf(bio, "\nBag Value: ");
381 1 print_attribute(aTHX_ bio, PKCS12_SAFEBAG_get0_bag_obj(bag), &attribute_value);
382 }
383 53 Safefree(attribute_value);
384 } else {
385 0 ▶ if (!bag_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (!bag_hv) 0 0 0
386 0 i2a_ASN1_OBJECT(bio, PKCS12_SAFEBAG_get0_bag_type(bag));
387 }
388 53 break;
389 #endif
390 2 case NID_safeContentsBag:
391 2 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 2 0 2
392 2 ▶ if(bag_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if(bag_hv) { 1 1 2
393 1 SV * value = newSVpvn("safe_contents_bag", strlen("safe_contents_bag"));
394 1 ▶ if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "type", strlen("type"), value, 0)) == NULL) 0 1 1
395 0 croak("unable to add type to the bag_hv");
396  
397 1 print_attribs(aTHX_ bio, bag_attrs, "bag_attributes", bag_hv);
398  
399 1 dump_certs_pkeys_bags(aTHX_ bio, PKCS12_SAFEBAG_get0_safes(bag),
400 pass, passlen, options, pempass, enc, bag_hv);
401 } else {
402 1 BIO_printf(bio, "Safe Contents bag\n");
403 1 print_attribs(aTHX_ bio, bag_attrs, "Bag Attributes", NULL);
404 1 dump_certs_pkeys_bags(aTHX_ bio, PKCS12_SAFEBAG_get0_safes(bag),
405 pass, passlen, options, pempass, enc, NULL);
406 }
407 }
408 2 break;
409 0 default:
410 0 ▶ if(bag_hv)
Branch: 1 branch
BranchTrueFalseTotal
if(bag_hv) 0 0 0
411 0 return 1;
412 0 BIO_printf(bio, "Warning unsupported bag type: ");
413 0 i2a_ASN1_OBJECT(bio, PKCS12_SAFEBAG_get0_type(bag));
414 0 BIO_printf(bio, "\n");
415 }
416  
417 159 return 1;
418 }
419  
420 153 int dump_certs_pkeys_bags(pTHX_ BIO *bio, CONST_STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass, int passlen, int options, char *pempass, const EVP_CIPHER *enc, HV * hash) {
421  
422 int i;
423  
424 153 AV * bags_av = NULL;
425 153 HV * bag_hv = NULL;
426 153 ▶ if(hash) {
Branch: 1 branch
BranchTrueFalseTotal
if(hash) { 120 33 153
427 120 bags_av = newAV();
428 }
429 327 ▶ for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { 174 153 327
430 174 ▶ if(hash)
Branch: 1 branch
BranchTrueFalseTotal
if(hash) 127 47 174
431 127 bag_hv = newHV();
432 174 ▶ if (!dump_certs_pkeys_bag (aTHX_ bio, sk_PKCS12_SAFEBAG_value (bags, i), pass, passlen, options, pempass, enc, bag_hv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dump_certs_pkeys_bag (aTHX_ bio, sk_PKCS12_SAFEBAG_value (bags, i), pass, passlen, options, pempass, enc, bag_hv)) { 0 174 174
433 0 return 0;
434 }
435 174 ▶ if(hash)
Branch: 1 branch
BranchTrueFalseTotal
if(hash) 127 47 174
436 127 av_push(bags_av, newRV_inc((SV *) bag_hv));
437 }
438  
439 153 ▶ if (hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash) { 120 33 153
440 120 SV **svp = NULL;
441 char *type;
442 120 ▶ if (hv_exists(bag_hv, "type", strlen("type"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(bag_hv, "type", strlen("type"))) { 120 0 120
443 120 svp = hv_fetch(bag_hv, "type", strlen("type"), 0);
444 120 ▶ if (svp != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (svp != NULL) 120 0 120
445 240 type = SvPVbyte_nolen(*svp);
446 }
447 120 ▶ if (svp != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp != NULL) { 120 0 120
448 120 ▶ if (strcmp(type, "safe_contents_bag") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(type, "safe_contents_bag") == 0 ) { 1 119 120
449 1 ▶ if((hv_store(hash, "safe_contents_bag", strlen("safe_contents_bag"), newRV_inc((SV *) bags_av), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(hash, "safe_contents_bag", strlen("safe_contents_bag"), newRV_inc((SV *) bags_av), 0)) == NULL) 0 1 1
450 0 croak("unable to add bags to the hash");
451 } else {
452 119 ▶ if((hv_store(hash, "bags", strlen("bags"), newRV_inc((SV *) bags_av), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(hash, "bags", strlen("bags"), newRV_inc((SV *) bags_av), 0)) == NULL) 0 119 119
453 0 croak("unable to add bags to the hash");
454 }
455 }
456 }
457 153 return 1;
458 }
459  
460 129 int dump_certs_keys_p12(pTHX_ BIO *bio, PKCS12 *p12, const char *pass, int passlen, int options, char *pempass, HV * orig_hash) {
461  
462 STACK_OF(PKCS7) *asafes;
463 STACK_OF(PKCS12_SAFEBAG) *bags;
464  
465 int i, bagnid;
466 PKCS7 *p7;
467 129 HV * bag_hv = NULL;
468 129 HV * parameters_hv = NULL;
469 129 AV * pkcs7_bags_av = newAV();
470 129 AV * pkcs7_enc_bags_av = newAV();
471  
472 129 ▶ if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) { 0 129 129
473 0 croak("Unable to PKCS12_unpack_authsafes");
474 return 0;
475 }
476  
477 280 ▶ for (i = 0; i < sk_PKCS7_num(asafes); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sk_PKCS7_num(asafes); i++) { 151 129 280
478  
479 151 ▶ if (orig_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig_hash) { 119 32 151
480 119 parameters_hv = newHV();
481 119 bag_hv = newHV();
482 }
483  
484 151 p7 = sk_PKCS7_value(asafes, i);
485  
486 151 bagnid = OBJ_obj2nid(p7->type);
487  
488 151 ▶ if (bagnid == NID_pkcs7_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (bagnid == NID_pkcs7_data) { 78 73 151
489  
490 78 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 70 8 78
491 70 ▶ if (orig_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig_hash) { 61 9 70
492 61 bag_hv = newHV();
493 61 av_push(pkcs7_bags_av, newRV_inc((SV *) bag_hv));
494 }
495 else
496 9 BIO_printf(bio, "PKCS7 Data\n");
497 }
498 78 bags = PKCS12_unpack_p7data(p7);
499  
500 73 ▶ } else if (bagnid == NID_pkcs7_encrypted) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (bagnid == NID_pkcs7_encrypted) { 73 0 73
501 73 ▶ if (options & INFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (options & INFO) { 65 8 73
502 65 ▶ if (orig_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig_hash) { 58 7 65
503 58 bag_hv = newHV();
504  
505 58 ▶ if (p7->d.encrypted != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p7->d.encrypted != NULL) 58 0 58
506 58 alg_print(aTHX_ bio, p7->d.encrypted->enc_data->algorithm, parameters_hv);
507 58 ▶ if((hv_store(bag_hv, "parameters", strlen("parameters"), newRV_inc((SV *) parameters_hv), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, "parameters", strlen("parameters"), newRV_inc((SV *) parameters_hv), 0)) == NULL) 0 58 58
508 0 croak("unable to add parameters to the hash");
509 58 ▶ if((hv_store(orig_hash, "pkcs7_encrypted_data", strlen("pkcs7_encrypted_data"), newRV_inc((SV *) bag_hv), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(orig_hash, "pkcs7_encrypted_data", strlen("pkcs7_encrypted_data"), newRV_inc((SV *) bag_hv), 0)) == NULL) 0 58 58
510 0 croak("unable to add pkcs7_encrypted_data to the orig_hash");
511 58 av_push(pkcs7_enc_bags_av, newRV_inc((SV *) bag_hv));
512 } else {
513 7 BIO_printf(bio, "PKCS7 Encrypted data: ");
514 7 ▶ if (p7->d.encrypted == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p7->d.encrypted == NULL) { 0 7 7
515 0 BIO_printf(bio, "<no data>\n");
516 } else {
517 7 alg_print(aTHX_ bio, p7->d.encrypted->enc_data->algorithm, NULL);
518 }
519 }
520 }
521 73 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
522  
523 } else {
524 0 continue;
525 }
526  
527 151 ▶ if (bags == NULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (bags == NULL) return 0; 0 151 151
528  
529 151 ▶ if (!dump_certs_pkeys_bags(aTHX_ bio, bags, pass, passlen, options, pempass, enc, bag_hv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dump_certs_pkeys_bags(aTHX_ bio, bags, pass, passlen, options, pempass, enc, bag_hv)) { 0 151 151
530 0 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
531 0 return 0;
532 }
533  
534 151 ▶ if (orig_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig_hash) { 119 32 151
535 119 ▶ if((hv_store(orig_hash, "pkcs7_data", strlen("pkcs7_data"), newRV_inc((SV *) pkcs7_bags_av), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(orig_hash, "pkcs7_data", strlen("pkcs7_data"), newRV_inc((SV *) pkcs7_bags_av), 0)) == NULL) 0 119 119
536 0 croak("unable to add bags to the hash");
537 119 ▶ if((hv_store(orig_hash, "pkcs7_encrypted_data", strlen("pkcs7_encrypted_data"), newRV_inc((SV *) pkcs7_enc_bags_av), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(orig_hash, "pkcs7_encrypted_data", strlen("pkcs7_encrypted_data"), newRV_inc((SV *) pkcs7_enc_bags_av), 0)) == NULL) 0 119 119
538 0 croak("unable to add bags to the hash");
539 }
540 151 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
541 }
542  
543 129 sk_PKCS7_pop_free(asafes, PKCS7_free);
544  
545 129 return 1;
546 }
547 #ifdef TIM
548 # define B_FORMAT_TEXT 0x8000
549 # define FORMAT_TEXT (1 | B_FORMAT_TEXT) /* Generic text */
550 int FMT_istext(int format)
551 {
552 return (format & B_FORMAT_TEXT) == B_FORMAT_TEXT;
553 }
554  
555 BIO *dup_bio_err(int format)
556 {
557 BIO *b = BIO_new_fp(stderr,
558 BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0));
559  
560 return b;
561 }
562 #endif
563 static unsigned long nmflag = 0;
564 static char nmflag_set = 0;
565 # define XN_FLAG_SPC_EQ (1 << 23)/* Put spaces round '=' */
566  
567 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
568  
569 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
570 ASN1_STRFLGS_ESC_QUOTE | \
571 ASN1_STRFLGS_ESC_CTRL | \
572 ASN1_STRFLGS_ESC_MSB)
573  
574 168 unsigned long get_nameopt(void)
575 {
576 return
577 168 ▶ nmflag_set ? nmflag : ESC_FLAGS | XN_FLAG_SEP_CPLUS_SPC | ASN1_STRFLGS_UTF8_CONVERT | XN_FLAG_SPC_EQ;
Branch: 1 branch
BranchTrueFalseTotal
nmflag_set ? nmflag : ESC_FLAGS | XN_FLAG_SEP_CPLUS_SPC | ASN1_STRFLGS_UTF8_CONVERT | XN_FLAG_SPC_EQ; 0 168 168
578 }
579  
580 168 void print_name(BIO *out, const char *title, CONST_X509_NAME *nm)
581 {
582 char *buf;
583 168 char mline = 0;
584 168 int indent = 0;
585 168 unsigned long lflags = get_nameopt();
586  
587 168 ▶ if (out == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out == NULL) 0 168 168
588 0 return;
589 168 ▶ if (title != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (title != NULL) 168 0 168
590 168 BIO_puts(out, title);
591 168 ▶ if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { 0 168 168
592 0 mline = 1;
593 0 indent = 4;
594 }
595 168 ▶ if (lflags == XN_FLAG_COMPAT) {
Branch: 1 branch
BranchTrueFalseTotal
if (lflags == XN_FLAG_COMPAT) { 0 168 168
596 0 buf = X509_NAME_oneline(nm, 0, 0);
597 0 BIO_puts(out, buf);
598 0 BIO_puts(out, "\n");
599 0 OPENSSL_free(buf);
600 } else {
601 168 ▶ if (mline)
Branch: 1 branch
BranchTrueFalseTotal
if (mline) 0 168 168
602 0 BIO_puts(out, "\n");
603 168 X509_NAME_print_ex(out, nm, indent, lflags);
604  
605 168 BIO_puts(out, "\n");
606 }
607 }
608  
609 16 void dump_cert_text(BIO *out, X509 *x)
610 {
611 16 print_name(out, "subject=", X509_get_subject_name(x));
612 16 print_name(out, "issuer=", X509_get_issuer_name(x));
613 16 }
614  
615 68 SV * get_cert_subject_name(pTHX_ X509 *x)
616 {
617 BIO *bio;
618 68 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 68 68
619  
620 68 print_name(bio, "", X509_get_subject_name(x));
621 68 SV *sv = extractBioString(aTHX_ bio);
622 68 return sv;
623 }
624  
625 68 SV * get_cert_issuer_name(pTHX_ X509 *x)
626 {
627 BIO *bio;
628 68 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 68 68
629  
630 68 print_name(bio, "", X509_get_issuer_name(x));
631 68 SV *sv = extractBioString(aTHX_ bio);
632 68 return sv;
633 }
634  
635 121 void get_hex(char *out, const unsigned char *buf, int len)
636 {
637 int i;
638 1728 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 1607 121 1728
639 1607 ▶ if (i == (len-1))
Branch: 1 branch
BranchTrueFalseTotal
if (i == (len-1)) 119 1488 1607
640 119 out += sprintf(out, "%02X", buf[i]);
641 else
642 1488 out += sprintf(out, "%02X ", buf[i]);
643 }
644 121 *out = '\0';
645 121 }
646  
647 17 void hex_prin(BIO *out, const unsigned char *buf, int len)
648 {
649 int i;
650 247 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 230 17 247
651 230 BIO_printf(out, "%02X ", buf[i]);
652 17 }
653  
654 /* Generalised x509 attribute value print */
655  
656 319 void print_attribute(pTHX_ BIO *out, CONST_ASN1_TYPE *av, char **attribute)
657 {
658 char *value;
659 319 int length = 0;
660 /*
661 const char *ln;
662 char objbuf[80];
663 */
664 319 switch (av->type) {
665 124 case V_ASN1_BMPSTRING:
666 124 length = ASN1_STRING_length(av->value.bmpstring);
667 124 ▶ if (length < 0 || length > (INT_MAX - 1))
Branch: 2 branches
BranchTrueFalseTotal
if (length < 0 || length > (INT_MAX - 1)) 124 0 124
if (length < 0 || length > (INT_MAX - 1)) 0 124 124
668 0 croak("BMPSTRING attribute length out of range (got %d)", length);
669 124 value = OPENSSL_uni2asc(ASN1_STRING_get0_data(av->value.bmpstring), length);
670 /* Defensive: OPENSSL_uni2asc returns NULL on allocation failure, and on
671 an odd-length BMPSTRING. Guard before either branch dereferences it:
672 the else branch below passes value to BIO_printf. */
673 124 ▶ if (value == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (value == NULL) 0 124 124
674 0 croak("BMPSTRING attribute could not be decoded");
675 124 ▶ if(*attribute != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(*attribute != NULL) { 113 11 124
676 /* uni2asc returns a NUL-terminated C string whose length can be shorter
677 than `length` (each UTF-16 unit -> one ASCII byte), and is 0 for an
678 empty BMPSTRING. Size the buffer on strlen(value) + 1 and write an
679 explicit terminator. Sizing on `length` degenerated for an empty
680 BMPSTRING: Renew(*attribute, 0, char) frees the buffer and returns
681 NULL, and downstream callers run strlen()/newSVpvn() on it -> NULL
682 pointer dereference. */
683 113 size_t vlen = strlen(value);
684 113 Renew(*attribute, vlen + 1, char);
685 113 memcpy(*attribute, value, vlen);
686 113 (*attribute)[vlen] = '\0';
687 } else {
688 11 BIO_printf(out, "%s\n", value);
689 }
690 124 OPENSSL_free(value);
691 124 break;
692  
693 53 case V_ASN1_UTF8STRING:
694 53 length = ASN1_STRING_length(av->value.utf8string);
695 53 ▶ if(*attribute != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(*attribute != NULL) { 52 1 53
696 52 ▶ if (length < 0 || length > (INT_MAX - 1))
Branch: 2 branches
BranchTrueFalseTotal
if (length < 0 || length > (INT_MAX - 1)) 52 0 52
if (length < 0 || length > (INT_MAX - 1)) 0 52 52
697 0 croak("UTF8STRING attribute length out of range (got %d)", length);
698 52 Renew(*attribute, (size_t)length + 1, char);
699 52 ▶ if (length)
Branch: 1 branch
BranchTrueFalseTotal
if (length) 52 0 52
700 52 memcpy(*attribute, ASN1_STRING_get0_data(av->value.utf8string), (size_t)length);
701 52 (*attribute)[length] = '\0';
702 } else {
703 1 BIO_printf(out, "%.*s\n", length, (const char *)ASN1_STRING_get0_data(av->value.utf8string));
704 }
705 53 break;
706  
707 135 case V_ASN1_OCTET_STRING:
708 135 length = ASN1_STRING_length(av->value.octet_string);
709 135 ▶ if(*attribute != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(*attribute != NULL) { 119 16 135
710 119 ▶ if (length < 0 || length > INT_MAX / 4)
Branch: 2 branches
BranchTrueFalseTotal
if (length < 0 || length > INT_MAX / 4) 119 0 119
if (length < 0 || length > INT_MAX / 4) 0 119 119
711 0 croak("OCTET STRING attribute length out of range (got %d)", length);
712 119 Renew(*attribute, (size_t)length * 4 + 1, char);
713 119 get_hex(*attribute, ASN1_STRING_get0_data(av->value.octet_string), length);
714 } else {
715 16 hex_prin(out, ASN1_STRING_get0_data(av->value.octet_string), length);
716 16 BIO_printf(out, "\n");
717 }
718 135 break;
719  
720 3 case V_ASN1_BIT_STRING:
721 3 length = ASN1_STRING_length(av->value.bit_string);
722 3 ▶ if(*attribute != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(*attribute != NULL) { 2 1 3
723 2 ▶ if (length < 0 || length > INT_MAX / 4)
Branch: 2 branches
BranchTrueFalseTotal
if (length < 0 || length > INT_MAX / 4) 2 0 2
if (length < 0 || length > INT_MAX / 4) 0 2 2
724 0 croak("BIT STRING attribute length out of range (got %d)", length);
725 2 Renew(*attribute, (size_t)length * 4 + 1, char);
726 2 get_hex(*attribute, ASN1_STRING_get0_data(av->value.bit_string), length);
727 } else {
728 1 hex_prin(out, ASN1_STRING_get0_data(av->value.bit_string), length);
729 1 BIO_printf(out, "\n");
730 }
731 3 break;
732  
733 /* case V_ASN1_OBJECT:
734 ln = OBJ_nid2ln(OBJ_obj2nid(av->value.object));
735 if (!ln)
736 ln = "";
737 OBJ_obj2txt(objbuf, sizeof(objbuf), av->value.object, 1);
738 if(*attribute != NULL) {
739 Renew(*attribute, strlen(ln), char);
740 strncpy(*attribute, ln, strlen(*attribute));
741 } else {
742 BIO_printf(out, "%s (%s)", ln, objbuf);
743 BIO_printf(out, "\n");
744 }
745 break;
746 */
747 4 default:
748 4 ▶ if(*attribute != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(*attribute != NULL) { 2 2 4
749 2 Renew(*attribute, strlen("<Unsupported tag >") + 11 + 2, char); /* +2 for \n\0 */
750 2 snprintf(*attribute, strlen("<Unsupported tag >") + 11 + 2,
751 2 "<Unsupported tag %i>\n", av->type);
752 }
753 else {
754 2 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
755 }
756 4 break;
757 }
758 319 }
759  
760 /* Generalised attribute print: handle PKCS#8 and bag attributes */
761 163 int print_attribs(pTHX_ BIO *out, CONST_STACK_OF(X509_ATTRIBUTE) *attrlst,
762 const char *name, HV * hash)
763 {
764 X509_ATTRIBUTE *attr;
765 CONST_ASN1_TYPE *av;
766 int i, j, attr_nid;
767 163 AV * bags_av = newAV();
768 163 ▶ if (!attrlst) {
Branch: 1 branch
BranchTrueFalseTotal
if (!attrlst) { 24 139 163
769 24 ▶ if(hash) {
Branch: 1 branch
BranchTrueFalseTotal
if(hash) { 12 12 24
770 /* FIXME: May need to change attribute storage for empty attributes */
771 /*SV * value = newSVpvn("<No Attributes>", strlen("<No Attributes>")); */
772 /*if((hv_store(hash, "attributes", strlen("attributes"), newRV_inc((SV *) bags_av), 0)) == NULL) */
773 /* croak("unable to add attributes to the hash"); */
774 } else
775 12 BIO_printf(out, "%s: <No Attributes>\n", name);
776 24 return 1;
777 }
778 139 ▶ if (!sk_X509_ATTRIBUTE_num(attrlst)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sk_X509_ATTRIBUTE_num(attrlst)) { 0 139 139
779 0 ▶ if(hash) {
Branch: 1 branch
BranchTrueFalseTotal
if(hash) { 0 0 0
780 /* FIXME: May need to change attribute storage for empty attributes */
781 /*if((hv_store(hash, "attributes", strlen("attributes"), newRV_inc((SV *) bags_av), 0)) == NULL) */
782 /* croak("unable to add attributes to the hash"); */
783 } else
784 0 BIO_printf(out, "%s: <Empty Attributes>\n", name);
785 0 return 1;
786 }
787 139 ▶ if(!hash)
Branch: 1 branch
BranchTrueFalseTotal
if(!hash) 18 121 139
788 18 BIO_printf(out, "%s\n", name);
789  
790 139 HV * bag_hv = newHV();
791 405 ▶ for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) { 266 139 405
792 CONST_ASN1_OBJECT *attr_obj;
793 266 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
794 266 attr_obj = X509_ATTRIBUTE_get0_object(attr);
795 266 attr_nid = OBJ_obj2nid(attr_obj);
796 266 ▶ if (!hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hash) { 30 236 266
797 30 ▶ if (attr_nid == NID_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (attr_nid == NID_undef) { 3 27 30
798 3 BIO_printf(out, " ");
799 3 i2a_ASN1_OBJECT(out, attr_obj);
800 3 BIO_printf(out, ": ");
801 }
802 else {
803 27 ▶ if (!hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hash) { 27 0 27
804 /* Print the name of the attributes */
805 27 BIO_printf(out, " ");
806 27 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
807 }
808 }
809 }
810  
811 266 ▶ if (X509_ATTRIBUTE_count(attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (X509_ATTRIBUTE_count(attr)) { 266 0 266
812 532 ▶ for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < X509_ATTRIBUTE_count(attr); j++) 266 266 532
813 {
814 266 av = X509_ATTRIBUTE_get0_type(attr, j);
815 266 char *attribute_value = NULL;
816 266 ▶ if(hash) {
Branch: 1 branch
BranchTrueFalseTotal
if(hash) { 236 30 266
817 236 const char *attribute_id = NULL;
818 236 Newx(attribute_value, 0, char);
819 236 print_attribute(aTHX_ out, av, &attribute_value);
820 236 ▶ if (attr_nid != NID_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (attr_nid != NID_undef) { 129 107 236
821 /* Save the attribute name and value to the hash */
822 129 attribute_id = OBJ_nid2ln(attr_nid);
823 129 ▶ if (attribute_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (attribute_id) { 129 0 129
824 129 ▶ if((hv_store(bag_hv, attribute_id, strlen(attribute_id), newSVpvn(attribute_value, strlen(attribute_value)), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, attribute_id, strlen(attribute_id), newSVpvn(attribute_value, strlen(attribute_value)), 0)) == NULL) 0 129 129
825 0 croak("unable to store attribute in hash");
826 }
827 } else {
828 BIO *attr_bio;
829 107 char *attr_key = NULL;
830 long attr_key_len;
831  
832 107 ▶ CHECK_OPEN_SSL(attr_bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(attr_bio = BIO_new(BIO_s_mem())); 0 107 107
833 107 i2a_ASN1_OBJECT(attr_bio, attr_obj);
834  
835 107 ▶ CHECK_OPEN_SSL(BIO_flush(attr_bio) == 1);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(BIO_flush(attr_bio) == 1); 0 107 107
836 107 attr_key_len = BIO_get_mem_data(attr_bio, &attr_key);
837  
838 107 ▶ if (attr_key_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (attr_key_len > 0) { 107 0 107
839 107 ▶ if((hv_store(bag_hv, attr_key, (I32)attr_key_len, newSVpvn(attribute_value, strlen(attribute_value)), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(bag_hv, attr_key, (I32)attr_key_len, newSVpvn(attribute_value, strlen(attribute_value)), 0)) == NULL) 0 107 107
840 0 croak("unable to store attribute in hash");
841 }
842  
843 107 (void)BIO_set_close(attr_bio, BIO_CLOSE);
844 107 BIO_free(attr_bio);
845 }
846 236 Safefree(attribute_value);
847 } else {
848 30 print_attribute(aTHX_ out, av, &attribute_value);
849 }
850 266 av_push(bags_av, newRV_inc((SV *) bag_hv));
851 }
852 } else {
853 0 BIO_printf(out, "<No Values>\n");
854 }
855 }
856 139 ▶ if (hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash) { 121 18 139
857 121 ▶ if((hv_store(hash, name, strlen(name), newRV_inc((SV *) bag_hv), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(hash, name, strlen(name), newRV_inc((SV *) bag_hv), 0)) == NULL) 0 121 121
858 0 croak("unable to add bags to the hash");
859 }
860 139 return 1;
861 }
862  
863 75 static int alg_print(pTHX_ BIO *bio, CONST_X509_ALGOR *alg, HV * parameters_hash)
864 {
865 int pbenid, aparamtype;
866 CONST_ASN1_OBJECT *aoid;
867 CONST_VOID *aparam;
868 75 PBEPARAM *pbe = NULL;
869  
870 75 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
871 75 pbenid = OBJ_obj2nid(aoid);
872 75 ▶ if (parameters_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (parameters_hash) { 63 12 75
873 63 SV * nid_long_name = newSVpvn(OBJ_nid2ln(pbenid), strlen(OBJ_nid2ln(pbenid)));
874 63 ▶ if((hv_store(parameters_hash, "nid_long_name", strlen("nid_long_name"), nid_long_name, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "nid_long_name", strlen("nid_long_name"), nid_long_name, 0)) == NULL) 0 63 63
875 0 croak("unable to add MAC to the parameters_hash");
876 63 SV * nid_short_name = newSVpvn(OBJ_nid2sn(pbenid), strlen(OBJ_nid2sn(pbenid)));
877 63 ▶ if((hv_store(parameters_hash, "nid_short_name", strlen("nid_short_name"), nid_short_name, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "nid_short_name", strlen("nid_short_name"), nid_short_name, 0)) == NULL) 0 63 63
878 0 croak("unable to add MAC to the parameters_hash");
879 } else {
880 12 BIO_printf(bio, "%s", OBJ_nid2ln(pbenid));
881 }
882 /*
883 * If PBE algorithm is PBES2 decode algorithm parameters
884 * for additional details.
885 */
886 75 ▶ if (pbenid == NID_pbes2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pbenid == NID_pbes2) { 14 61 75
887 14 PBE2PARAM *pbe2 = NULL;
888 int encnid;
889 14 ▶ if (aparamtype == V_ASN1_SEQUENCE)
Branch: 1 branch
BranchTrueFalseTotal
if (aparamtype == V_ASN1_SEQUENCE) 14 0 14
890 14 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
891 14 ▶ if (pbe2 == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pbe2 == NULL) { 0 14 14
892 0 BIO_puts(bio, ", <unsupported parameters>");
893 0 goto done;
894 }
895 14 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
896 14 pbenid = OBJ_obj2nid(aoid);
897  
898 14 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
899 14 encnid = OBJ_obj2nid(aoid);
900 14 ▶ if (parameters_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (parameters_hash) { 7 7 14
901 7 SV * nid_long_name = newSVpvn(OBJ_nid2ln(pbenid), strlen(OBJ_nid2ln(pbenid)));
902 7 ▶ if((hv_store(parameters_hash, "nid_long_name", strlen("nid_long_name"), nid_long_name, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "nid_long_name", strlen("nid_long_name"), nid_long_name, 0)) == NULL) 0 7 7
903 0 croak("unable to add MAC to the parameters_hash");
904 7 SV * nid_short_name = newSVpvn(OBJ_nid2sn(pbenid), strlen(OBJ_nid2sn(pbenid)));
905 7 ▶ if((hv_store(parameters_hash, "nid_short_name", strlen("nid_short_name"), nid_short_name, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "nid_short_name", strlen("nid_short_name"), nid_short_name, 0)) == NULL) 0 7 7
906 0 croak("unable to add MAC to the parameters_hash");
907 } else
908 7 BIO_printf(bio, ", %s, %s", OBJ_nid2ln(pbenid),
909 OBJ_nid2sn(encnid));
910 /* If KDF is PBKDF2 decode parameters */
911 14 ▶ if (pbenid == NID_id_pbkdf2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pbenid == NID_id_pbkdf2) { 14 0 14
912 14 PBKDF2PARAM *kdf = NULL;
913 int prfnid;
914 14 ▶ if (aparamtype == V_ASN1_SEQUENCE)
Branch: 1 branch
BranchTrueFalseTotal
if (aparamtype == V_ASN1_SEQUENCE) 14 0 14
915 14 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
916 14 ▶ if (kdf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (kdf == NULL) { 0 14 14
917 0 BIO_puts(bio, ", <unsupported parameters>");
918 0 goto done;
919 }
920  
921 14 ▶ if (kdf->prf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (kdf->prf == NULL) { 0 14 14
922 0 prfnid = NID_hmacWithSHA1;
923 } else {
924 14 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
925 14 prfnid = OBJ_obj2nid(aoid);
926 }
927 14 ▶ if (parameters_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (parameters_hash) { 7 7 14
928 7 SV * iteration = newSViv(ASN1_INTEGER_get(kdf->iter));
929 7 ▶ if((hv_store(parameters_hash, "iteration", strlen("iteration"), iteration, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "iteration", strlen("iteration"), iteration, 0)) == NULL) 0 7 7
930 0 croak("unable to add iteration to the parameters_hash");
931 } else
932 14 BIO_printf(bio, ", Iteration %ld, PRF %s",
933 7 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
934 14 PBKDF2PARAM_free(kdf);
935 #if OPENSSL_VERSION_NUMBER > 0x10100000L
936 #ifndef LIBRESSL_VERSION_NUMBER
937 #ifndef OPENSSL_NO_SCRYPT
938 0 ▶ } else if (pbenid == NID_id_scrypt) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pbenid == NID_id_scrypt) { 0 0 0
939 0 SCRYPT_PARAMS *kdf = NULL;
940  
941 0 ▶ if (aparamtype == V_ASN1_SEQUENCE)
Branch: 1 branch
BranchTrueFalseTotal
if (aparamtype == V_ASN1_SEQUENCE) 0 0 0
942 0 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
943 0 ▶ if (kdf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (kdf == NULL) { 0 0 0
944 0 BIO_puts(bio, ", <unsupported parameters>");
945 0 goto done;
946 }
947 0 ▶ if (parameters_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (parameters_hash) { 0 0 0
948 0 ▶ if((hv_store(parameters_hash, "salt_length", strlen("salt_length"), newSViv(ASN1_STRING_length(kdf->salt)), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "salt_length", strlen("salt_length"), newSViv(ASN1_STRING_length(kdf->salt)), 0)) == NULL) 0 0 0
949 0 croak("unable to add salt_length to the parameters_hash");
950 0 ▶ if((hv_store(parameters_hash, "cost", strlen("cost"), newSViv(ASN1_INTEGER_get(kdf->costParameter)), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "cost", strlen("cost"), newSViv(ASN1_INTEGER_get(kdf->costParameter)), 0)) == NULL) 0 0 0
951 0 croak("unable to add cost to the parameters_hash");
952 0 ▶ if((hv_store(parameters_hash, "block_size", strlen("block_size"), newSViv(ASN1_INTEGER_get(kdf->blockSize)), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "block_size", strlen("block_size"), newSViv(ASN1_INTEGER_get(kdf->blockSize)), 0)) == NULL) 0 0 0
953 0 croak("unable to add block_size to the parameters_hash");
954 0 ▶ if((hv_store(parameters_hash, "parallelism", strlen("parallelism"), newSViv(ASN1_INTEGER_get(kdf->parallelizationParameter)), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "parallelism", strlen("parallelism"), newSViv(ASN1_INTEGER_get(kdf->parallelizationParameter)), 0)) == NULL) 0 0 0
955 0 croak("unable to add parallelism to the parameters_hash");
956 } else
957 0 BIO_printf(bio, ", Salt length: %d, Cost(N): %ld, "
958 "Block size(r): %ld, Parallelism(p): %ld",
959 0 ASN1_STRING_length(kdf->salt),
960 0 ASN1_INTEGER_get(kdf->costParameter),
961 0 ASN1_INTEGER_get(kdf->blockSize),
962 0 ASN1_INTEGER_get(kdf->parallelizationParameter));
963 0 SCRYPT_PARAMS_free(kdf);
964 #endif
965 #endif
966 #endif
967 }
968 14 PBE2PARAM_free(pbe2);
969 } else {
970 61 ▶ if (aparamtype == V_ASN1_SEQUENCE)
Branch: 1 branch
BranchTrueFalseTotal
if (aparamtype == V_ASN1_SEQUENCE) 61 0 61
971 61 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
972 61 ▶ if (pbe == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pbe == NULL) { 0 61 61
973 0 BIO_puts(bio, ", <unsupported parameters>");
974 0 goto done;
975 }
976 61 ▶ if (parameters_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (parameters_hash) { 56 5 61
977 56 long int val = ASN1_INTEGER_get(pbe->iter);
978 56 SV * iteration = newSViv(val);
979 56 ▶ if((hv_store(parameters_hash, "iteration", strlen("iteration"), iteration, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(parameters_hash, "iteration", strlen("iteration"), iteration, 0)) == NULL) 0 56 56
980 0 croak("unable to add MAC to the parameters_hash");
981 } else
982 5 BIO_printf(bio, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
983 61 PBEPARAM_free(pbe);
984 }
985 75 done:
986 75 ▶ if(!parameters_hash)
Branch: 1 branch
BranchTrueFalseTotal
if(!parameters_hash) 12 63 75
987 12 BIO_puts(bio, "\n");
988 75 return 1;
989 }
990  
991 MODULE = Crypt::OpenSSL::PKCS12 PACKAGE = Crypt::OpenSSL::PKCS12
992  
993 PROTOTYPES: DISABLE
994  
995 BOOT:
996 {
997 HV *stash;
998 char *name;
999 int i;
1000  
1001 18 struct { char *n; I32 v; } Crypt__OpenSSL__PKCS12__const[] = {
1002 {"NOKEYS", NOKEYS},
1003 {"NOCERTS", NOCERTS},
1004 {"INFO", INFO},
1005 {"CLCERTS", CLCERTS},
1006 {"CACERTS", CACERTS},
1007 {Nullch,0}
1008 };
1009  
1010 18 OpenSSL_add_all_algorithms();
1011  
1012 18 stash = gv_stashpvn("Crypt::OpenSSL::PKCS12", 22, TRUE);
1013  
1014 108 ▶ for (i = 0; (name = Crypt__OpenSSL__PKCS12__const[i].n); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; (name = Crypt__OpenSSL__PKCS12__const[i].n); i++) { 90 18 108
1015 90 newCONSTSUB(stash, name, newSViv(Crypt__OpenSSL__PKCS12__const[i].v));
1016 }
1017 }
1018  
1019 Crypt::OpenSSL::PKCS12
1020 new(class)
1021 SV *class
1022  
1023 CODE:
1024  
1025 0 ▶ if ((RETVAL = PKCS12_new()) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((RETVAL = PKCS12_new()) == NULL) { 0 0 0
1026 0 croak("Couldn't create PKCS12_new() for class %" SVf "\n", SVfARG(class));
1027 }
1028  
1029 OUTPUT:
1030 RETVAL
1031  
1032 IV legacy_support(class)
1033 SV *class;
1034  
1035 CODE:
1036 1 RETVAL = 1;
1037 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1038 1 ▶ if (legacy)
Branch: 1 branch
BranchTrueFalseTotal
if (legacy) 1 0 1
1039 1 RETVAL = 1;
1040 else
1041 0 RETVAL = 0;
1042 #endif
1043 OUTPUT:
1044 RETVAL
1045  
1046 Crypt::OpenSSL::PKCS12
1047 new_from_string(class, string)
1048 SV *class
1049 SV *string
1050  
1051 ALIAS:
1052 new_from_file = 1
1053  
1054 PREINIT:
1055 BIO *bio;
1056 STRLEN str_len;
1057 char *str_ptr;
1058 CODE:
1059  
1060 38 ▶ SvGETMAGIC(string);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(string); 0 38 38
SvGETMAGIC(string); 0 0 0
1061 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1062 /* FIXME: There should likely be an option for whether to load the legacy provider */
1063 38 legacy = OSSL_PROVIDER_load(NULL, "legacy");
1064 38 ▶ if (legacy == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (legacy == NULL) { 0 38 38
1065 0 warn("Failed to load Legacy provider\n");
1066 }
1067 38 deflt = OSSL_PROVIDER_load(NULL, "default");
1068 38 ▶ if (deflt == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (deflt == NULL) { 0 38 38
1069 0 OSSL_PROVIDER_unload(legacy);
1070 0 croak("Failed to load Default provider\n");
1071 }
1072 #endif
1073  
1074 38 ▶ if (SvPOKp(string) || SvNOKp(string) || SvIOKp(string)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvPOKp(string) || SvNOKp(string) || SvIOKp(string)) { 12 26 38
if (SvPOKp(string) || SvNOKp(string) || SvIOKp(string)) { 10 2 12
if (SvPOKp(string) || SvNOKp(string) || SvIOKp(string)) { 2 8 10
1075 30 ▶ if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 1) { 17 13 30
1076 /* We are not looking up the SV's UTF8 bit because BIO_new_file() accepts
1077 * filename like syscall fopen() which mainly may accept octet sequences
1078 * for UTF-8 in C char*. That's what we get from using SvPV(). Also,
1079 * using SvPV() is not a bug if ASCII input is only allowed. */
1080 17 str_ptr = SvPV(string, str_len);
1081 } else {
1082 /* To avoid encoding mess, caller is not allowed to provide octets from
1083 * UTF-8 encoded strings. BIO_new_mem_buf() needs octet input only. */
1084 13 ▶ if (SvUTF8(string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(string)) { 4 9 13
1085 4 croak("PKCS12_new_from: Source string must not be UTF-8 encoded (please use octets)");
1086 }
1087 9 str_ptr = SvPV(string, str_len);
1088 }
1089 } else {
1090 8 croak("PKCS12_new_from: Invalid Perl type for string or file was passed (0x%x).", (unsigned int)SvFLAGS(string));
1091 }
1092  
1093 26 ▶ if (!str_ptr || !str_len) croak("PKCS12_new_from: No string or file was passed.");
Branch: 2 branches
BranchTrueFalseTotal
if (!str_ptr || !str_len) croak("PKCS12_new_from: No string or file was passed."); 26 0 26
if (!str_ptr || !str_len) croak("PKCS12_new_from: No string or file was passed."); 0 26 26
1094  
1095 26 ▶ if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 1) { 17 9 26
1096 17 bio = BIO_new_file(str_ptr, "rb");
1097 } else {
1098 9 bio = BIO_new_mem_buf(str_ptr, str_len);
1099 }
1100  
1101 26 ▶ if (!bio) croak("Failed to create BIO");
Branch: 1 branch
BranchTrueFalseTotal
if (!bio) croak("Failed to create BIO"); 0 26 26
1102  
1103 /* this can come in any number of ways */
1104 26 ▶ if ((RETVAL = d2i_PKCS12_bio(bio, 0)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((RETVAL = d2i_PKCS12_bio(bio, 0)) == NULL) { 6 20 26
1105 6 BIO_free_all(bio);
1106 6 croak("%" SVf ": Couldn't create PKCS12 from d2i_PKCS12_BIO(): %s", SVfARG(class), ssl_error(aTHX));
1107 }
1108  
1109 20 BIO_free_all(bio);
1110  
1111 OUTPUT:
1112 RETVAL
1113  
1114 # This is called at per-object destruction time.
1115 void
1116 DESTROY(pkcs12)
1117 Crypt::OpenSSL::PKCS12 pkcs12;
1118  
1119 CODE:
1120 20 ▶ if (pkcs12) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkcs12) { 20 0 20
1121 20 PKCS12_free(pkcs12);
1122 }
1123  
1124 # This is called via an END block in the Perl module to clean up initialization that happened in BOOT.
1125 void
1126 __PKCS12_cleanup(void)
1127 CODE:
1128  
1129 18 CRYPTO_cleanup_all_ex_data();
1130 18 ERR_free_strings();
1131 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1132 ERR_remove_state(0);
1133 #endif
1134 18 EVP_cleanup();
1135  
1136 SV*
1137 as_string(pkcs12)
1138 Crypt::OpenSSL::PKCS12 pkcs12;
1139  
1140 PREINIT:
1141 BIO *bio;
1142  
1143 CODE:
1144  
1145 3 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 3 3
1146  
1147 3 ▶ if (!(i2d_PKCS12_bio(bio, pkcs12))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(i2d_PKCS12_bio(bio, pkcs12))) { 0 3 3
1148 0 sv_bio_error(aTHX_ bio);
1149 0 croak("i2d_PKCS12_bio: %s", ssl_error(aTHX));
1150 }
1151  
1152 3 RETVAL = extractBioString(aTHX_ bio);
1153  
1154 OUTPUT:
1155 RETVAL
1156  
1157 SV*
1158 mac_ok(pkcs12, pwd = &PL_sv_undef)
1159 Crypt::OpenSSL::PKCS12 pkcs12
1160 SV *pwd
1161  
1162 PREINIT:
1163 9 STRLEN pwd_len = 0;
1164 9 ▶ const char *pwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *pwd_str = ""; 9 0 9
1165  
1166 CODE:
1167  
1168 9 ▶ if (SvOK(pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pwd)) { 9 0 9
1169 9 pwd_str = SvPV(pwd, pwd_len);
1170 9 ▶ if (pwd_len > (STRLEN)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pwd_len > (STRLEN)INT_MAX) 0 9 9
1171 0 croak("password length exceeds INT_MAX");
1172 }
1173  
1174 9 ▶ if (!(PKCS12_verify_mac(pkcs12, pwd_str, (int)pwd_len)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(PKCS12_verify_mac(pkcs12, pwd_str, (int)pwd_len))) 1 8 9
1175 1 croak("PKCS12_verify_mac: \n%s", ssl_error(aTHX));
1176  
1177 8 RETVAL = &PL_sv_yes;
1178  
1179 OUTPUT:
1180 RETVAL
1181  
1182 SV*
1183 changepass(pkcs12, oldpwd = &PL_sv_undef, newpwd = &PL_sv_undef)
1184 Crypt::OpenSSL::PKCS12 pkcs12
1185 SV *oldpwd
1186 SV *newpwd
1187  
1188 PREINIT:
1189 0 STRLEN oldpwd_len = 0, newpwd_len = 0;
1190 0 const char *oldpwd_str = "";
1191 0 ▶ const char *newpwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *newpwd_str = ""; 0 0 0
1192  
1193 CODE:
1194  
1195 0 ▶ if (SvOK(oldpwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(oldpwd)) { 0 0 0
1196 0 oldpwd_str = SvPV(oldpwd, oldpwd_len);
1197 0 ▶ if (memchr(oldpwd_str, '\0', oldpwd_len) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(oldpwd_str, '\0', oldpwd_len) != NULL) 0 0 0
1198 0 croak("old PKCS12 password contains embedded NUL byte; "
1199 "PKCS12_newpass() uses strlen() and would silently truncate it");
1200 }
1201 0 ▶ if (SvOK(newpwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(newpwd)) { 0 0 0
1202 0 newpwd_str = SvPV(newpwd, newpwd_len);
1203 0 ▶ if (memchr(newpwd_str, '\0', newpwd_len) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(newpwd_str, '\0', newpwd_len) != NULL) 0 0 0
1204 0 croak("new PKCS12 password contains embedded NUL byte; "
1205 "PKCS12_newpass() uses strlen() and would silently truncate it");
1206 }
1207  
1208 0 ▶ if (!(PKCS12_newpass(pkcs12, oldpwd_str, newpwd_str))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(PKCS12_newpass(pkcs12, oldpwd_str, newpwd_str))) { 0 0 0
1209 0 warn("PKCS12_newpass failed: %s\n"
1210 "Note: PKCS12_newpass() cannot change the password on PBES2-encrypted "
1211 "PKCS12 files, which OpenSSL 3.x/4.x use by default for newly created "
1212 "files. This is a known upstream OpenSSL limitation, not specific to "
1213 "this module: https://github.com/openssl/openssl/issues/19092\n", ssl_error(aTHX));
1214 0 RETVAL = &PL_sv_no;
1215 } else {
1216 0 RETVAL = &PL_sv_yes;
1217 }
1218  
1219 OUTPUT:
1220 RETVAL
1221  
1222 SV*
1223 create(pkcs12, cert_chain_pem = "", pk = "", pass = &PL_sv_undef, file = 0, name = "PKCS12 Certificate")
1224 char *cert_chain_pem
1225 char *pk
1226 SV *pass
1227 char *file
1228 char *name
1229  
1230 PREINIT:
1231 FILE *fp;
1232 EVP_PKEY* pkey;
1233 PKCS12 *p12;
1234 4 STACK_OF(X509) *cert_chain = NULL;
1235 4 STRLEN pass_len = 0;
1236 4 ▶ const char *pass_str = NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *pass_str = NULL; 0 4 4
1237  
1238 CODE:
1239  
1240 4 ▶ if (SvOK(pass)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pass)) { 4 0 4
1241 4 pass_str = SvPV(pass, pass_len);
1242 4 ▶ if (memchr(pass_str, '\0', pass_len) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(pass_str, '\0', pass_len) != NULL) 1 3 4
1243 1 croak("PKCS12 password contains embedded NUL byte; "
1244 "PKCS12_create() uses strlen() and would silently truncate it");
1245 }
1246  
1247 3 pkey = _load_pkey(pk, PEM_read_bio_PrivateKey);
1248 3 cert_chain = _load_cert_chain(cert_chain_pem, PEM_X509_INFO_read_bio);
1249 3 p12 = PKCS12_create(pass_str, name, pkey, sk_X509_shift(cert_chain), cert_chain, 0, 0, 0, 0, 0);
1250  
1251 3 ▶ if (!p12) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p12) { 0 3 3
1252 0 ERR_print_errors_fp(stderr);
1253 0 croak("Error creating PKCS#12 structure\n");
1254 }
1255  
1256 3 ▶ if (!(fp = fopen(file, "wb"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(fp = fopen(file, "wb"))) { 0 3 3
1257 0 ERR_print_errors_fp(stderr);
1258 0 croak("Error opening file %s\n", file);
1259 }
1260  
1261 3 i2d_PKCS12_fp(fp, p12);
1262 3 PKCS12_free(p12);
1263 3 fclose(fp);
1264  
1265 3 RETVAL = &PL_sv_yes;
1266  
1267 OUTPUT:
1268 RETVAL
1269  
1270  
1271 SV*
1272 create_as_string(pkcs12, cert_chain_pem = "", pk = "", pass = &PL_sv_undef, name = "PKCS12 Certificate")
1273 char *cert_chain_pem
1274 char *pk
1275 SV *pass
1276 char *name
1277  
1278 PREINIT:
1279 BIO *bio;
1280 EVP_PKEY* pkey;
1281 PKCS12 *p12;
1282 2 STACK_OF(X509) *cert_chain = NULL;
1283 2 STRLEN pass_len = 0;
1284 2 ▶ const char *pass_str = NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *pass_str = NULL; 0 2 2
1285  
1286 CODE:
1287  
1288 2 ▶ if (SvOK(pass)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pass)) { 2 0 2
1289 2 pass_str = SvPV(pass, pass_len);
1290 2 ▶ if (memchr(pass_str, '\0', pass_len) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(pass_str, '\0', pass_len) != NULL) 1 1 2
1291 1 croak("PKCS12 password contains embedded NUL byte; "
1292 "PKCS12_create() uses strlen() and would silently truncate it");
1293 }
1294  
1295 1 pkey = _load_pkey(pk, PEM_read_bio_PrivateKey);
1296 1 cert_chain = _load_cert_chain(cert_chain_pem, PEM_X509_INFO_read_bio);
1297 1 p12 = PKCS12_create(pass_str, name, pkey, sk_X509_shift(cert_chain), cert_chain, 0, 0, 0, 0, 0);
1298  
1299 1 ▶ if (!p12) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p12) { 0 1 1
1300 0 ERR_print_errors_fp(stderr);
1301 0 croak("Error creating PKCS#12 structure\n");
1302 }
1303  
1304 1 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 1 1
1305 1 i2d_PKCS12_bio(bio, p12);
1306  
1307 1 RETVAL = extractBioString(aTHX_ bio);
1308 1 PKCS12_free(p12);
1309  
1310 OUTPUT:
1311 RETVAL
1312  
1313 SV*
1314 certificate(pkcs12, pwd = &PL_sv_undef)
1315 Crypt::OpenSSL::PKCS12 pkcs12
1316 SV *pwd
1317  
1318 PREINIT:
1319 BIO *bio;
1320 4 STACK_OF(PKCS7) *asafes = NULL;
1321 4 STRLEN pwd_len = 0;
1322 4 ▶ const char *pwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *pwd_str = ""; 4 0 4
1323  
1324 CODE:
1325  
1326 4 ▶ if (SvOK(pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pwd)) { 4 0 4
1327 4 pwd_str = SvPV(pwd, pwd_len);
1328 4 ▶ if (pwd_len > (STRLEN)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pwd_len > (STRLEN)INT_MAX) 0 4 4
1329 0 croak("password length exceeds INT_MAX");
1330 }
1331  
1332 4 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 4 4
1333  
1334 4 ▶ if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL) 0 4 4
1335 0 RETVAL = newSVpvn("",0);
1336  
1337 4 dump_certs_keys_p12(aTHX_ bio, pkcs12, pwd_str, (int)pwd_len, CLCERTS|NOKEYS, NULL, NULL);
1338  
1339 4 RETVAL = extractBioString(aTHX_ bio);
1340  
1341 OUTPUT:
1342 RETVAL
1343  
1344 SV*
1345 ca_certificate(pkcs12, pwd = &PL_sv_undef)
1346 Crypt::OpenSSL::PKCS12 pkcs12
1347 SV *pwd
1348  
1349 PREINIT:
1350 BIO *bio;
1351 1 STACK_OF(PKCS7) *asafes = NULL;
1352 1 STRLEN pwd_len = 0;
1353 1 ▶ const char *pwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *pwd_str = ""; 1 0 1
1354  
1355 CODE:
1356  
1357 1 ▶ if (SvOK(pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pwd)) { 1 0 1
1358 1 pwd_str = SvPV(pwd, pwd_len);
1359 1 ▶ if (pwd_len > (STRLEN)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pwd_len > (STRLEN)INT_MAX) 0 1 1
1360 0 croak("password length exceeds INT_MAX");
1361 }
1362  
1363 1 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 1 1
1364  
1365 1 ▶ if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL) 0 1 1
1366 0 RETVAL = newSVpvn("",0);
1367  
1368 1 dump_certs_keys_p12(aTHX_ bio, pkcs12, pwd_str, (int)pwd_len, CACERTS|NOKEYS, NULL, NULL);
1369  
1370 1 RETVAL = extractBioString(aTHX_ bio);
1371  
1372 OUTPUT:
1373 RETVAL
1374  
1375 SV*
1376 private_key(pkcs12, pwd = &PL_sv_undef)
1377 Crypt::OpenSSL::PKCS12 pkcs12
1378 SV *pwd
1379  
1380 PREINIT:
1381 BIO *bio;
1382 3 STRLEN pwd_len = 0;
1383 3 ▶ const char *pwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *pwd_str = ""; 3 0 3
1384  
1385 CODE:
1386  
1387 3 ▶ if (SvOK(pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pwd)) { 3 0 3
1388 3 pwd_str = SvPV(pwd, pwd_len);
1389 3 ▶ if (pwd_len > (STRLEN)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pwd_len > (STRLEN)INT_MAX) 0 3 3
1390 0 croak("password length exceeds INT_MAX");
1391 }
1392  
1393 3 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 3 3
1394  
1395 3 PKCS12_unpack_authsafes(pkcs12);
1396  
1397 3 dump_certs_keys_p12(aTHX_ bio, pkcs12, pwd_str, (int)pwd_len, NOCERTS, NULL, NULL);
1398  
1399 3 RETVAL = extractBioString(aTHX_ bio);
1400  
1401 OUTPUT:
1402 RETVAL
1403  
1404 HV* info_as_hash(pkcs12, pwd = &PL_sv_undef)
1405 Crypt::OpenSSL::PKCS12 pkcs12
1406 SV *pwd
1407  
1408 PREINIT:
1409 BIO *bio;
1410 112 STACK_OF(PKCS7) *asafes = NULL;
1411 112 STRLEN pwd_len = 0;
1412 112 ▶ const char *pwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *pwd_str = ""; 112 0 112
1413  
1414 CONST_ASN1_INTEGER *tmaciter;
1415 #if OPENSSL_VERSION_NUMBER > 0x10100000L
1416 SV *value;
1417 CONST_X509_ALGOR *macalgid;
1418 CONST_ASN1_OBJECT *macobj;
1419 CONST_ASN1_OCTET_STRING *tmac;
1420 CONST_ASN1_OCTET_STRING *tsalt;
1421 #endif
1422  
1423 CODE:
1424 112 ▶ if (SvOK(pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pwd)) { 112 0 112
1425 112 pwd_str = SvPV(pwd, pwd_len);
1426 112 ▶ if (pwd_len > (STRLEN)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pwd_len > (STRLEN)INT_MAX) 0 112 112
1427 0 croak("password length exceeds INT_MAX");
1428 }
1429 112 RETVAL = newHV();
1430  
1431 112 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 112 112
1432  
1433 112 ▶ if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL) 0 112 112
1434 0 RETVAL = newHV();
1435 112 HV * mac = newHV();
1436 #if OPENSSL_VERSION_NUMBER > 0x10100000L
1437 112 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, pkcs12);
1438 /* current hash algorithms do not use parameters so extract just name,
1439 in future alg_print() may be needed */
1440 112 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
1441 112 i2a_ASN1_OBJECT(bio, macobj);
1442 112 value = extractBioString(aTHX_ bio);
1443 112 ▶ if((hv_store(mac, "digest", strlen("digest"), value, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(mac, "digest", strlen("digest"), value, 0)) == NULL) 0 112 112
1444 0 croak("unable to add digest to the hash");
1445 #else
1446 tmaciter = pkcs12->mac->iter;
1447 #endif
1448 112 ▶ SV * mac_iteration = newSViv (tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
Branch: 1 branch
BranchTrueFalseTotal
SV * mac_iteration = newSViv (tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L); 112 0 112
1449  
1450 112 ▶ if((hv_store(mac, "iteration", strlen("iteration"), mac_iteration, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(mac, "iteration", strlen("iteration"), mac_iteration, 0)) == NULL) 0 112 112
1451 0 croak("unable to add iteration to the hash");
1452 112 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 112 112
1453 /* BIO_printf(bio, "MAC length: %ld, salt length: %ld", */
1454 #if OPENSSL_VERSION_NUMBER > 0x10100000L
1455 112 ▶ SV * mac_len = newSViv(tmac != NULL ? ASN1_STRING_length(tmac) : 0L);
Branch: 1 branch
BranchTrueFalseTotal
SV * mac_len = newSViv(tmac != NULL ? ASN1_STRING_length(tmac) : 0L); 112 0 112
1456 112 ▶ SV * salt_len = newSViv(tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
Branch: 1 branch
BranchTrueFalseTotal
SV * salt_len = newSViv(tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L); 112 0 112
1457  
1458 112 ▶ if((hv_store(mac, "length", 6, mac_len, 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(mac, "length", 6, mac_len, 0)) == NULL) 0 112 112
1459 0 croak("unable to add length to the hash");
1460 #else
1461 SV * salt_len = newSViv((pkcs12)->mac ? pkcs12->mac->salt->length : 0L);
1462 #endif
1463  
1464 112 ▶ if((hv_store(mac, "salt_length", strlen("salt_length"), INT2PTR(SV*, salt_len), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(mac, "salt_length", strlen("salt_length"), INT2PTR(SV*, salt_len), 0)) == NULL) 0 112 112
1465 0 croak("unable to add salt_length to the hash");
1466  
1467 112 ▶ if((hv_store(RETVAL, "mac", strlen("mac"), newRV_inc((SV *) mac), 0)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if((hv_store(RETVAL, "mac", strlen("mac"), newRV_inc((SV *) mac), 0)) == NULL) 0 112 112
1468 0 croak("unable to add MAC to the hash");
1469 112 dump_certs_keys_p12(aTHX_ bio, pkcs12, pwd_str, (int)pwd_len, INFO, NULL, RETVAL);
1470  
1471 112 SV * end = extractBioString(aTHX_ bio);
1472  
1473 112 ▶ if (SvPOK(end)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(end)) { 0 112 112
1474 0 ▶ if (SvCUR(end) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(end) != 0) 0 0 0
1475 0 printf("BIO %s\n", SvPVbyte_nolen(end));
1476 0 warn("bio from info_as_hash should be zero length - report issue");
1477 }
1478  
1479 112 sv_2mortal((SV*)RETVAL);
1480  
1481 OUTPUT:
1482 RETVAL
1483  
1484 SV*
1485 info(pkcs12, pwd = &PL_sv_undef)
1486 Crypt::OpenSSL::PKCS12 pkcs12
1487 SV *pwd
1488  
1489 PREINIT:
1490 BIO *bio;
1491 9 STACK_OF(PKCS7) *asafes = NULL;
1492 9 STRLEN pwd_len = 0;
1493 9 ▶ const char *pwd_str = "";
Branch: 1 branch
BranchTrueFalseTotal
const char *pwd_str = ""; 9 0 9
1494  
1495 CONST_ASN1_INTEGER *tmaciter;
1496 #if OPENSSL_VERSION_NUMBER > 0x10100000L
1497 CONST_X509_ALGOR *macalgid;
1498 CONST_ASN1_OBJECT *macobj;
1499 CONST_ASN1_OCTET_STRING *tmac;
1500 CONST_ASN1_OCTET_STRING *tsalt;
1501 #endif
1502 CODE:
1503  
1504 9 ▶ if (SvOK(pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pwd)) { 9 0 9
1505 9 pwd_str = SvPV(pwd, pwd_len);
1506 9 ▶ if (pwd_len > (STRLEN)INT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pwd_len > (STRLEN)INT_MAX) 0 9 9
1507 0 croak("password length exceeds INT_MAX");
1508 }
1509  
1510 9 ▶ CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem()));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_OPEN_SSL(bio = BIO_new(BIO_s_mem())); 0 9 9
1511  
1512 9 ▶ if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((asafes = PKCS12_unpack_authsafes(pkcs12)) == NULL) 0 9 9
1513 0 RETVAL = newSVpvn("",0);
1514 #if OPENSSL_VERSION_NUMBER > 0x10100000L
1515 9 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, pkcs12);
1516 /* current hash algorithms do not use parameters so extract just name,
1517 in future alg_print() may be needed */
1518 9 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
1519 9 BIO_puts(bio, "MAC: ");
1520 9 i2a_ASN1_OBJECT(bio, macobj);
1521 /* current hash algorithms do not use parameters so extract just name,
1522 in future alg_print() may be needed */
1523 9 BIO_printf(bio, ", Iteration %ld\n",
1524 9 ▶ tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
Branch: 1 branch
BranchTrueFalseTotal
tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L); 9 0 9
1525 18 BIO_printf(bio, "MAC length: %ld, salt length: %ld\n",
1526 9 ▶ tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
Branch: 1 branch
BranchTrueFalseTotal
tmac != NULL ? ASN1_STRING_length(tmac) : 0L, 9 0 9
1527 9 ▶ tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
Branch: 1 branch
BranchTrueFalseTotal
tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L); 9 0 9
1528 #else
1529 tmaciter = pkcs12->mac->iter;
1530 BIO_printf(bio, "MAC Iteration %ld\n",
1531 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
1532 /* If we enter empty password try no password first */
1533 if (!PKCS12_verify_mac(pkcs12, pwd_str, (int)pwd_len)) {
1534 BIO_printf(bio, "Mac verify error: invalid password?\n");
1535 ERR_print_errors(bio);
1536 goto end;
1537 }
1538 BIO_printf(bio, "MAC verified OK\n");
1539 end:
1540 #endif
1541 9 dump_certs_keys_p12(aTHX_ bio, pkcs12, pwd_str, (int)pwd_len, INFO, NULL, NULL);
1542  
1543 9 RETVAL = extractBioString(aTHX_ bio);
1544  
1545 OUTPUT:
1546 RETVAL
↑ Summary blib/lib/Crypt/OpenSSL/PKCS12.pm »