2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include "internal/nelem.h"
16 #include <openssl/pkcs12.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pem.h>
22 #include "pkcs12_helper.h"
25 DEFINE_STACK_OF(PKCS7)
26 DEFINE_STACK_OF(PKCS12_SAFEBAG)
27 DEFINE_STACK_OF(X509_ATTRIBUTE)
29 /* Set this to > 0 write test data to file */
32 /* -------------------------------------------------------------------------
33 * Local function declarations
36 static X509 *load_cert(const unsigned char *bytes, int len);
37 static EVP_PKEY *load_pkey(const unsigned char *bytes, int len);
39 static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs);
41 static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
42 static int write_p12(PKCS12 *p12, const char *outfile);
44 static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac);
45 static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac);
46 static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac);
47 static int check_asn1_string(const ASN1_TYPE *av, const char *txt);
48 static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs);
51 /* --------------------------------------------------------------------------
52 * Test data load functions
55 static X509 *load_cert(const unsigned char *bytes, int len)
59 cert = d2i_X509(NULL, &bytes, len);
66 static EVP_PKEY *load_pkey(const unsigned char *bytes, int len)
68 EVP_PKEY *pkey = NULL;
70 pkey = d2i_AutoPrivateKey(NULL, &bytes, len);
78 /* -------------------------------------------------------------------------
82 PKCS12_BUILDER *new_pkcs12_builder(const char *filename)
84 PKCS12_BUILDER *pb = OPENSSL_malloc(sizeof(PKCS12_BUILDER));
88 pb->filename = filename;
93 int end_pkcs12_builder(PKCS12_BUILDER *pb)
95 int result = pb->success;
102 void start_pkcs12(PKCS12_BUILDER *pb)
108 void end_pkcs12(PKCS12_BUILDER *pb)
112 generate_p12(pb, NULL);
116 void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
120 generate_p12(pb, mac);
124 /* Generate the PKCS12 encoding and write to memory bio */
125 static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
132 pb->p12bio = BIO_new(BIO_s_mem());
133 if (!TEST_ptr(pb->p12bio)) {
137 p12 = PKCS12_add_safes(pb->safes, 0);
138 if (!TEST_ptr(p12)) {
142 sk_PKCS7_pop_free(pb->safes, PKCS7_free);
145 if (!TEST_true(PKCS12_set_mac(p12, mac->pass, strlen(mac->pass),
146 NULL, 0, mac->iter, EVP_get_digestbynid(mac->nid)))) {
151 i2d_PKCS12_bio(pb->p12bio, p12);
153 /* Can write to file here for debug */
155 write_p12(p12, pb->filename);
161 static int write_p12(PKCS12 *p12, const char *outfile)
164 BIO *out = BIO_new_file(outfile, "w");
169 if (!TEST_int_eq(i2d_PKCS12_bio(out, p12), 1))
177 static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac)
181 p12 = d2i_PKCS12_bio(bio, NULL);
186 if (!TEST_false(PKCS12_mac_present(p12)))
189 if (!check_p12_mac(p12, mac))
199 /* For use with existing files */
200 static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac)
203 BIO *in = BIO_new_file(infile, "r");
207 p12 = d2i_PKCS12_bio(in, NULL);
212 if (!TEST_false(PKCS12_mac_present(p12)))
215 if (!check_p12_mac(p12, mac))
224 static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac)
226 return TEST_true(PKCS12_mac_present(p12))
227 && TEST_true(PKCS12_verify_mac(p12, mac->pass, strlen(mac->pass)));
231 /* -------------------------------------------------------------------------
232 * PKCS7 content info builder
235 void start_contentinfo(PKCS12_BUILDER *pb)
241 void end_contentinfo(PKCS12_BUILDER *pb)
244 if (pb->bags && !TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, -1, 0, NULL))) {
249 sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
254 void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
258 && !TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, enc->nid, enc->iter, enc->pass))) {
263 sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
268 static STACK_OF(PKCS12_SAFEBAG) *decode_contentinfo(STACK_OF(PKCS7) *safes, int idx, const PKCS12_ENC *enc)
270 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
271 PKCS7 *p7 = sk_PKCS7_value(safes, idx);
272 int bagnid = OBJ_obj2nid(p7->type);
275 if (!TEST_int_eq(bagnid, NID_pkcs7_encrypted))
277 /* TODO: Check algorithm (iterations?) against what we originally set */
278 bags = PKCS12_unpack_p7encdata(p7, enc->pass, strlen(enc->pass));
280 if (!TEST_int_eq(bagnid, NID_pkcs7_data))
282 bags = PKCS12_unpack_p7data(p7);
293 /* -------------------------------------------------------------------------
294 * PKCS12 safeBag/attribute builder
297 static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs)
301 const PKCS12_ATTR *p_attr = attrs;
306 while (p_attr->oid != NULL) {
307 TEST_info("Adding attribute %s = %s", p_attr->oid, p_attr->value);
308 attr_nid = OBJ_txt2nid(p_attr->oid);
310 if (attr_nid == NID_friendlyName) {
311 if (!TEST_true(PKCS12_add_friendlyname(bag, p_attr->value, -1)))
313 } else if (attr_nid == NID_localKeyID) {
314 if (!TEST_true(PKCS12_add_localkeyid(bag, (unsigned char *)p_attr->value,
315 strlen(p_attr->value))))
318 /* Custom attribute values limited to ASCII in these tests */
319 if (!TEST_true(PKCS12_add1_attr_by_txt(bag, p_attr->oid, MBSTRING_ASC,
320 (unsigned char *)p_attr->value,
321 strlen(p_attr->value))))
331 void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
332 const PKCS12_ATTR *attrs)
334 PKCS12_SAFEBAG *bag = NULL;
341 cert = load_cert(bytes, len);
342 if (!TEST_ptr(cert)) {
347 name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
348 TEST_info("Adding certificate <%s>", name);
351 bag = PKCS12_add_cert(&pb->bags, cert);
352 if (!TEST_ptr(bag)) {
357 if (!TEST_true(add_attributes(bag, attrs))) {
365 void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
366 const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
368 PKCS12_SAFEBAG *bag = NULL;
369 EVP_PKEY *pkey = NULL;
374 TEST_info("Adding key");
376 pkey = load_pkey(bytes, len);
377 if (!TEST_ptr(pkey)) {
382 bag = PKCS12_add_key(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass);
383 if (!TEST_ptr(bag)) {
387 if (!add_attributes(bag, attrs))
393 void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
394 const PKCS12_ATTR *attrs)
396 PKCS12_SAFEBAG *bag = NULL;
401 TEST_info("Adding secret <%s>", secret);
403 bag = PKCS12_add_secret(&pb->bags, secret_nid, (const unsigned char *)secret, strlen(secret));
404 if (!TEST_ptr(bag)) {
408 if (!add_attributes(bag, attrs))
413 /* -------------------------------------------------------------------------
414 * PKCS12 structure checking
417 static int check_asn1_string(const ASN1_TYPE *av, const char *txt)
426 case V_ASN1_BMPSTRING:
427 value = OPENSSL_uni2asc(av->value.bmpstring->data,
428 av->value.bmpstring->length);
429 if (!TEST_str_eq(txt, (char *)value))
433 case V_ASN1_UTF8STRING:
434 if (!TEST_str_eq(txt, (char *)av->value.utf8string->data))
438 case V_ASN1_OCTET_STRING:
439 if (!TEST_str_eq(txt, (char *)av->value.octet_string->data))
444 /* Tests do not support other attribute types currently */
453 static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs)
456 X509_ATTRIBUTE *attr;
461 for (i = 0; i < sk_X509_ATTRIBUTE_num(bag_attrs); i++) {
462 const PKCS12_ATTR *p_attr = attrs;
463 ASN1_OBJECT *attr_obj;
465 attr = sk_X509_ATTRIBUTE_value(bag_attrs, i);
466 attr_obj = X509_ATTRIBUTE_get0_object(attr);
467 OBJ_obj2txt(attr_txt, 100, attr_obj, 0);
469 while(p_attr->oid != NULL) {
470 /* Find a matching attribute type */
471 if (strcmp(p_attr->oid, attr_txt) == 0) {
473 /* TODO: Handle multi-value attributes */
474 if (!TEST_int_eq(X509_ATTRIBUTE_count(attr), 1))
477 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
479 av = X509_ATTRIBUTE_get0_type(attr, j);
480 if (!TEST_true(check_asn1_string(av, p_attr->value)))
493 void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
494 const PKCS12_ATTR *attrs)
497 X509 *ref_x509 = NULL;
498 const PKCS12_SAFEBAG *bag;
503 bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
504 if (!TEST_ptr(bag)) {
508 if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
509 || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_certBag)
510 || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), NID_x509Certificate)) {
514 x509 = PKCS12_SAFEBAG_get1_cert(bag);
515 if (!TEST_ptr(x509)) {
519 ref_x509 = load_cert(bytes, len);
520 if (!TEST_false(X509_cmp(x509, ref_x509)))
527 void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
528 const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
530 EVP_PKEY *pkey = NULL;
531 EVP_PKEY *ref_pkey = NULL;
532 PKCS8_PRIV_KEY_INFO *p8;
533 const PKCS8_PRIV_KEY_INFO *p8c;
534 const PKCS12_SAFEBAG *bag;
539 bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
540 if (!TEST_ptr(bag)) {
545 if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)) {
550 switch (PKCS12_SAFEBAG_get_nid(bag)) {
552 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
553 if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8c))) {
557 /* TODO: handle key attributes */
558 /* PKCS8_pkey_get0_attrs(p8c); */
561 case NID_pkcs8ShroudedKeyBag:
562 if (!TEST_ptr(p8 = PKCS12_decrypt_skey(bag, enc->pass, strlen(enc->pass)))) {
566 if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8))) {
567 PKCS8_PRIV_KEY_INFO_free(p8);
571 /* TODO: handle key attributes */
572 /* PKCS8_pkey_get0_attrs(p8); */
573 PKCS8_PRIV_KEY_INFO_free(p8);
581 /* PKEY compare returns 1 for match */
582 ref_pkey = load_pkey(bytes, len);
583 if (!TEST_true(EVP_PKEY_eq(pkey, ref_pkey)))
587 EVP_PKEY_free(ref_pkey);
590 void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs)
592 const PKCS12_SAFEBAG *bag;
597 bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
598 if (!TEST_ptr(bag)) {
603 if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
604 || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_secretBag)
605 || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), secret_nid)
606 || !TEST_true(check_asn1_string(PKCS12_SAFEBAG_get0_bag_obj(bag), secret)))
611 void start_check_pkcs12(PKCS12_BUILDER *pb)
613 PKCS12 *p12 = from_bio_p12(pb->p12bio, NULL);
614 if (!TEST_ptr(p12)) {
618 pb->safes = PKCS12_unpack_authsafes(p12);
619 if (!TEST_ptr(pb->safes))
626 void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
628 PKCS12 *p12 = from_bio_p12(pb->p12bio, mac);
629 if (!TEST_ptr(p12)) {
633 pb->safes = PKCS12_unpack_authsafes(p12);
634 if (!TEST_ptr(pb->safes))
641 void start_check_pkcs12_file(PKCS12_BUILDER *pb)
643 PKCS12 *p12 = read_p12(pb->filename, NULL);
644 if (!TEST_ptr(p12)) {
648 pb->safes = PKCS12_unpack_authsafes(p12);
649 if (!TEST_ptr(pb->safes))
656 void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
658 PKCS12 *p12 = read_p12(pb->filename, mac);
659 if (!TEST_ptr(p12)) {
663 pb->safes = PKCS12_unpack_authsafes(p12);
664 if (!TEST_ptr(pb->safes))
671 void end_check_pkcs12(PKCS12_BUILDER *pb)
673 sk_PKCS7_pop_free(pb->safes, PKCS7_free);
677 void start_check_contentinfo(PKCS12_BUILDER *pb)
680 pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, NULL);
681 if (!TEST_ptr(pb->bags)) {
685 TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
688 void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
691 pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, enc);
692 if (!TEST_ptr(pb->bags)) {
696 TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
700 void end_check_contentinfo(PKCS12_BUILDER *pb)
702 if (!TEST_int_eq(sk_PKCS12_SAFEBAG_num(pb->bags), pb->bag_idx))
704 sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);