xref: /freebsd-src/crypto/openssl/test/helpers/pkcs12.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <stdlib.h>
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include "internal/nelem.h"
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert #include <openssl/pkcs12.h>
17*e0c4386eSCy Schubert #include <openssl/x509.h>
18*e0c4386eSCy Schubert #include <openssl/x509v3.h>
19*e0c4386eSCy Schubert #include <openssl/pem.h>
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert #include "../testutil.h"
22*e0c4386eSCy Schubert 
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
25*e0c4386eSCy Schubert  * PKCS#12 Test structures
26*e0c4386eSCy Schubert  */
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert /* Holds a set of Attributes */
29*e0c4386eSCy Schubert typedef struct pkcs12_attr {
30*e0c4386eSCy Schubert     char *oid;
31*e0c4386eSCy Schubert     char *value;
32*e0c4386eSCy Schubert } PKCS12_ATTR;
33*e0c4386eSCy Schubert 
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert /* Holds encryption parameters */
36*e0c4386eSCy Schubert typedef struct pkcs12_enc {
37*e0c4386eSCy Schubert     int         nid;
38*e0c4386eSCy Schubert     const char *pass;
39*e0c4386eSCy Schubert     int         iter;
40*e0c4386eSCy Schubert } PKCS12_ENC;
41*e0c4386eSCy Schubert 
42*e0c4386eSCy Schubert /* Set of variables required for constructing the PKCS#12 structure */
43*e0c4386eSCy Schubert typedef struct pkcs12_builder {
44*e0c4386eSCy Schubert     const char *filename;
45*e0c4386eSCy Schubert     int success;
46*e0c4386eSCy Schubert     BIO *p12bio;
47*e0c4386eSCy Schubert     STACK_OF(PKCS7) *safes;
48*e0c4386eSCy Schubert     int safe_idx;
49*e0c4386eSCy Schubert     STACK_OF(PKCS12_SAFEBAG) *bags;
50*e0c4386eSCy Schubert     int bag_idx;
51*e0c4386eSCy Schubert } PKCS12_BUILDER;
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert 
54*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
55*e0c4386eSCy Schubert  * PKCS#12 Test function declarations
56*e0c4386eSCy Schubert  */
57*e0c4386eSCy Schubert 
58*e0c4386eSCy Schubert /* Global settings */
59*e0c4386eSCy Schubert void PKCS12_helper_set_write_files(int enable);
60*e0c4386eSCy Schubert void PKCS12_helper_set_legacy(int enable);
61*e0c4386eSCy Schubert void PKCS12_helper_set_libctx(OSSL_LIB_CTX *libctx);
62*e0c4386eSCy Schubert void PKCS12_helper_set_propq(const char *propq);
63*e0c4386eSCy Schubert 
64*e0c4386eSCy Schubert /* Allocate and initialise a PKCS#12 builder object */
65*e0c4386eSCy Schubert PKCS12_BUILDER *new_pkcs12_builder(const char *filename);
66*e0c4386eSCy Schubert 
67*e0c4386eSCy Schubert /* Finalise and free the PKCS#12 builder object, returning the success/fail flag */
68*e0c4386eSCy Schubert int end_pkcs12_builder(PKCS12_BUILDER *pb);
69*e0c4386eSCy Schubert 
70*e0c4386eSCy Schubert /* Encode/build functions */
71*e0c4386eSCy Schubert void start_pkcs12(PKCS12_BUILDER *pb);
72*e0c4386eSCy Schubert void end_pkcs12(PKCS12_BUILDER *pb);
73*e0c4386eSCy Schubert void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
74*e0c4386eSCy Schubert 
75*e0c4386eSCy Schubert void start_contentinfo(PKCS12_BUILDER *pb);
76*e0c4386eSCy Schubert void end_contentinfo(PKCS12_BUILDER *pb);
77*e0c4386eSCy Schubert void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc);
78*e0c4386eSCy Schubert 
79*e0c4386eSCy Schubert void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
80*e0c4386eSCy Schubert                  const PKCS12_ATTR *attrs);
81*e0c4386eSCy Schubert void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
82*e0c4386eSCy Schubert                 const PKCS12_ATTR *attrs, const PKCS12_ENC *enc);
83*e0c4386eSCy Schubert void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
84*e0c4386eSCy Schubert                    const PKCS12_ATTR *attrs);
85*e0c4386eSCy Schubert 
86*e0c4386eSCy Schubert /* Decode/check functions */
87*e0c4386eSCy Schubert void start_check_pkcs12(PKCS12_BUILDER *pb);
88*e0c4386eSCy Schubert void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
89*e0c4386eSCy Schubert void start_check_pkcs12_file(PKCS12_BUILDER *pb);
90*e0c4386eSCy Schubert void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
91*e0c4386eSCy Schubert void end_check_pkcs12(PKCS12_BUILDER *pb);
92*e0c4386eSCy Schubert 
93*e0c4386eSCy Schubert void start_check_contentinfo(PKCS12_BUILDER *pb);
94*e0c4386eSCy Schubert void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc);
95*e0c4386eSCy Schubert void end_check_contentinfo(PKCS12_BUILDER *pb);
96*e0c4386eSCy Schubert 
97*e0c4386eSCy Schubert void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
98*e0c4386eSCy Schubert                    const PKCS12_ATTR *attrs);
99*e0c4386eSCy Schubert void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
100*e0c4386eSCy Schubert                   const PKCS12_ATTR *attrs, const PKCS12_ENC *enc);
101*e0c4386eSCy Schubert void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
102*e0c4386eSCy Schubert                      const PKCS12_ATTR *attrs);
103*e0c4386eSCy Schubert 
104