xref: /freebsd/crypto/openssl/crypto/pkcs12/p12_init.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*
2  * Copyright 1999-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 "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include "crypto/pkcs7.h"
14 #include "p12_local.h"
15 
16 /* Initialise a PKCS12 structure to take data */
17 
18 PKCS12 *PKCS12_init_ex(int mode, OSSL_LIB_CTX *ctx, const char *propq)
19 {
20     PKCS12 *pkcs12;
21 
22     if ((pkcs12 = PKCS12_new()) == NULL) {
23         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
24         return NULL;
25     }
26     if (!ASN1_INTEGER_set(pkcs12->version, 3))
27         goto err;
28     pkcs12->authsafes->type = OBJ_nid2obj(mode);
29 
30     ossl_pkcs7_set0_libctx(pkcs12->authsafes, ctx);
31     if (!ossl_pkcs7_set1_propq(pkcs12->authsafes, propq)) {
32         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
33         goto err;
34     }
35 
36     switch (mode) {
37     case NID_pkcs7_data:
38         if ((pkcs12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL) {
39             ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
40             goto err;
41         }
42         break;
43     default:
44         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
45         goto err;
46     }
47     return pkcs12;
48 
49  err:
50     PKCS12_free(pkcs12);
51     return NULL;
52 }
53 
54 PKCS12 *PKCS12_init(int mode)
55 {
56     return PKCS12_init_ex(mode, NULL, NULL);
57 }
58 
59