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"
24 DEFINE_STACK_OF(PKCS12_SAFEBAG)
26 /* Set this to > 0 write test data to file */
29 /* -------------------------------------------------------------------------
30 * Local function declarations
33 static X509 *load_cert(const unsigned char *bytes, int len);
34 static EVP_PKEY *load_pkey(const unsigned char *bytes, int len);
36 static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs);
38 static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
39 static int write_p12(PKCS12 *p12, const char *outfile);
41 static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac);
42 static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac);
43 static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac);
44 static int check_asn1_string(const ASN1_TYPE *av, const char *txt);
45 static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs);
48 /* --------------------------------------------------------------------------
49 * Test data load functions
52 static X509 *load_cert(const unsigned char *bytes, int len)
56 cert = d2i_X509(NULL, &bytes, len);
63 static EVP_PKEY *load_pkey(const unsigned char *bytes, int len)
65 EVP_PKEY *pkey = NULL;
67 pkey = d2i_AutoPrivateKey(NULL, &bytes, len);
75 /* -------------------------------------------------------------------------
79 PKCS12_BUILDER *new_pkcs12_builder(const char *filename)
81 PKCS12_BUILDER *pb = OPENSSL_malloc(sizeof(PKCS12_BUILDER));
85 pb->filename = filename;
90 int end_pkcs12_builder(PKCS12_BUILDER *pb)
92 int result = pb->success;
99 void start_pkcs12(PKCS12_BUILDER *pb)
105 void end_pkcs12(PKCS12_BUILDER *pb)
109 generate_p12(pb, NULL);
113 void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
117 generate_p12(pb, mac);
121 /* Generate the PKCS12 encoding and write to memory bio */
122 static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
129 pb->p12bio = BIO_new(BIO_s_mem());
130 if (!TEST_ptr(pb->p12bio)) {
134 p12 = PKCS12_add_safes(pb->safes, 0);
135 if (!TEST_ptr(p12)) {
139 sk_PKCS7_pop_free(pb->safes, PKCS7_free);
142 if (!TEST_true(PKCS12_set_mac(p12, mac->pass, strlen(mac->pass),
143 NULL, 0, mac->iter, EVP_get_digestbynid(mac->nid)))) {
148 i2d_PKCS12_bio(pb->p12bio, p12);
150 /* Can write to file here for debug */
152 write_p12(p12, pb->filename);
158 static int write_p12(PKCS12 *p12, const char *outfile)
161 BIO *out = BIO_new_file(outfile, "w");
166 if (!TEST_int_eq(i2d_PKCS12_bio(out, p12), 1))
174 static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac)
178 p12 = d2i_PKCS12_bio(bio, NULL);
183 if (!TEST_false(PKCS12_mac_present(p12)))
186 if (!check_p12_mac(p12, mac))
196 /* For use with existing files */
197 static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac)
200 BIO *in = BIO_new_file(infile, "r");
204 p12 = d2i_PKCS12_bio(in, NULL);
209 if (!TEST_false(PKCS12_mac_present(p12)))
212 if (!check_p12_mac(p12, mac))
221 static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac)
223 return TEST_true(PKCS12_mac_present(p12))
224 && TEST_true(PKCS12_verify_mac(p12, mac->pass, strlen(mac->pass)));
228 /* -------------------------------------------------------------------------
229 * PKCS7 content info builder
232 void start_contentinfo(PKCS12_BUILDER *pb)
238 void end_contentinfo(PKCS12_BUILDER *pb)
241 if (pb->bags && !TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, -1, 0, NULL))) {
246 sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
251 void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
255 && !TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, enc->nid, enc->iter, enc->pass))) {
260 sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
265 static STACK_OF(PKCS12_SAFEBAG) *decode_contentinfo(STACK_OF(PKCS7) *safes, int idx, const PKCS12_ENC *enc)
267 STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
268 PKCS7 *p7 = sk_PKCS7_value(safes, idx);
269 int bagnid = OBJ_obj2nid(p7->type);
272 if (!TEST_int_eq(bagnid, NID_pkcs7_encrypted))
274 /* TODO: Check algorithm (iterations?) against what we originally set */
275 bags = PKCS12_unpack_p7encdata(p7, enc->pass, strlen(enc->pass));
277 if (!TEST_int_eq(bagnid, NID_pkcs7_data))
279 bags = PKCS12_unpack_p7data(p7);
290 /* -------------------------------------------------------------------------
291 * PKCS12 safeBag/attribute builder
294 static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs)
298 const PKCS12_ATTR *p_attr = attrs;
303 while (p_attr->oid != NULL) {
304 TEST_info("Adding attribute %s = %s", p_attr->oid, p_attr->value);
305 attr_nid = OBJ_txt2nid(p_attr->oid);
307 if (attr_nid == NID_friendlyName) {
308 if (!TEST_true(PKCS12_add_friendlyname(bag, p_attr->value, -1)))
310 } else if (attr_nid == NID_localKeyID) {
311 if (!TEST_true(PKCS12_add_localkeyid(bag, (unsigned char *)p_attr->value,
312 strlen(p_attr->value))))
315 /* Custom attribute values limited to ASCII in these tests */
316 if (!TEST_true(PKCS12_add1_attr_by_txt(bag, p_attr->oid, MBSTRING_ASC,
317 (unsigned char *)p_attr->value,
318 strlen(p_attr->value))))
328 void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
329 const PKCS12_ATTR *attrs)
331 PKCS12_SAFEBAG *bag = NULL;
338 cert = load_cert(bytes, len);
339 if (!TEST_ptr(cert)) {
344 name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
345 TEST_info("Adding certificate <%s>", name);
348 bag = PKCS12_add_cert(&pb->bags, cert);
349 if (!TEST_ptr(bag)) {
354 if (!TEST_true(add_attributes(bag, attrs))) {
362 void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
363 const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
365 PKCS12_SAFEBAG *bag = NULL;
366 EVP_PKEY *pkey = NULL;
371 TEST_info("Adding key");
373 pkey = load_pkey(bytes, len);
374 if (!TEST_ptr(pkey)) {
379 bag = PKCS12_add_key(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass);
380 if (!TEST_ptr(bag)) {
384 if (!add_attributes(bag, attrs))
390 void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
391 const PKCS12_ATTR *attrs)
393 PKCS12_SAFEBAG *bag = NULL;
398 TEST_info("Adding secret <%s>", secret);
400 bag = PKCS12_add_secret(&pb->bags, secret_nid, (const unsigned char *)secret, strlen(secret));
401 if (!TEST_ptr(bag)) {
405 if (!add_attributes(bag, attrs))
410 /* -------------------------------------------------------------------------
411 * PKCS12 structure checking
414 static int check_asn1_string(const ASN1_TYPE *av, const char *txt)
423 case V_ASN1_BMPSTRING:
424 value = OPENSSL_uni2asc(av->value.bmpstring->data,
425 av->value.bmpstring->length);
426 if (!TEST_str_eq(txt, (char *)value))
430 case V_ASN1_UTF8STRING:
431 if (!TEST_str_eq(txt, (char *)av->value.utf8string->data))
435 case V_ASN1_OCTET_STRING:
436 if (!TEST_str_eq(txt, (char *)av->value.octet_string->data))
441 /* Tests do not support other attribute types currently */
450 static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs)
453 X509_ATTRIBUTE *attr;
458 for (i = 0; i < sk_X509_ATTRIBUTE_num(bag_attrs); i++) {
459 const PKCS12_ATTR *p_attr = attrs;
460 ASN1_OBJECT *attr_obj;
462 attr = sk_X509_ATTRIBUTE_value(bag_attrs, i);
463 attr_obj = X509_ATTRIBUTE_get0_object(attr);
464 OBJ_obj2txt(attr_txt, 100, attr_obj, 0);
466 while(p_attr->oid != NULL) {
467 /* Find a matching attribute type */
468 if (strcmp(p_attr->oid, attr_txt) == 0) {
470 /* TODO: Handle multi-value attributes */
471 if (!TEST_int_eq(X509_ATTRIBUTE_count(attr), 1))
474 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
476 av = X509_ATTRIBUTE_get0_type(attr, j);
477 if (!TEST_true(check_asn1_string(av, p_attr->value)))
490 void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
491 const PKCS12_ATTR *attrs)
494 X509 *ref_x509 = NULL;
495 const PKCS12_SAFEBAG *bag;
500 bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
501 if (!TEST_ptr(bag)) {
505 if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
506 || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_certBag)
507 || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), NID_x509Certificate)) {
511 x509 = PKCS12_SAFEBAG_get1_cert(bag);
512 if (!TEST_ptr(x509)) {
516 ref_x509 = load_cert(bytes, len);
517 if (!TEST_false(X509_cmp(x509, ref_x509)))
524 void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
525 const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
527 EVP_PKEY *pkey = NULL;
528 EVP_PKEY *ref_pkey = NULL;
529 PKCS8_PRIV_KEY_INFO *p8;
530 const PKCS8_PRIV_KEY_INFO *p8c;
531 const PKCS12_SAFEBAG *bag;
536 bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
537 if (!TEST_ptr(bag)) {
542 if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)) {
547 switch (PKCS12_SAFEBAG_get_nid(bag)) {
549 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
550 if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8c))) {
554 /* TODO: handle key attributes */
555 /* PKCS8_pkey_get0_attrs(p8c); */
558 case NID_pkcs8ShroudedKeyBag:
559 if (!TEST_ptr(p8 = PKCS12_decrypt_skey(bag, enc->pass, strlen(enc->pass)))) {
563 if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8))) {
564 PKCS8_PRIV_KEY_INFO_free(p8);
568 /* TODO: handle key attributes */
569 /* PKCS8_pkey_get0_attrs(p8); */
570 PKCS8_PRIV_KEY_INFO_free(p8);
578 /* PKEY compare returns 1 for match */
579 ref_pkey = load_pkey(bytes, len);
580 if (!TEST_true(EVP_PKEY_eq(pkey, ref_pkey)))
584 EVP_PKEY_free(ref_pkey);
587 void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs)
589 const PKCS12_SAFEBAG *bag;
594 bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
595 if (!TEST_ptr(bag)) {
600 if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
601 || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_secretBag)
602 || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), secret_nid)
603 || !TEST_true(check_asn1_string(PKCS12_SAFEBAG_get0_bag_obj(bag), secret)))
608 void start_check_pkcs12(PKCS12_BUILDER *pb)
610 PKCS12 *p12 = from_bio_p12(pb->p12bio, NULL);
611 if (!TEST_ptr(p12)) {
615 pb->safes = PKCS12_unpack_authsafes(p12);
616 if (!TEST_ptr(pb->safes))
623 void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
625 PKCS12 *p12 = from_bio_p12(pb->p12bio, mac);
626 if (!TEST_ptr(p12)) {
630 pb->safes = PKCS12_unpack_authsafes(p12);
631 if (!TEST_ptr(pb->safes))
638 void start_check_pkcs12_file(PKCS12_BUILDER *pb)
640 PKCS12 *p12 = read_p12(pb->filename, NULL);
641 if (!TEST_ptr(p12)) {
645 pb->safes = PKCS12_unpack_authsafes(p12);
646 if (!TEST_ptr(pb->safes))
653 void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
655 PKCS12 *p12 = read_p12(pb->filename, mac);
656 if (!TEST_ptr(p12)) {
660 pb->safes = PKCS12_unpack_authsafes(p12);
661 if (!TEST_ptr(pb->safes))
668 void end_check_pkcs12(PKCS12_BUILDER *pb)
670 sk_PKCS7_pop_free(pb->safes, PKCS7_free);
674 void start_check_contentinfo(PKCS12_BUILDER *pb)
677 pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, NULL);
678 if (!TEST_ptr(pb->bags)) {
682 TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
685 void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
688 pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, enc);
689 if (!TEST_ptr(pb->bags)) {
693 TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
697 void end_check_contentinfo(PKCS12_BUILDER *pb)
699 if (!TEST_int_eq(sk_PKCS12_SAFEBAG_num(pb->bags), pb->bag_idx))
701 sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);