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