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