/src/openssl/crypto/pkcs12/p12_init.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2023 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 | | /* Refer to https://datatracker.ietf.org/doc/html/rfc7292#section-4 */ |
17 | 0 | #define PKCS12_VERSION 3 |
18 | | |
19 | | /* Initialise a PKCS12 structure to take data */ |
20 | | PKCS12 *PKCS12_init_ex(int mode, OSSL_LIB_CTX *ctx, const char *propq) |
21 | 0 | { |
22 | 0 | PKCS12 *pkcs12; |
23 | |
|
24 | 0 | if ((pkcs12 = PKCS12_new()) == NULL) { |
25 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
26 | 0 | return NULL; |
27 | 0 | } |
28 | 0 | if (!ASN1_INTEGER_set(pkcs12->version, PKCS12_VERSION)) |
29 | 0 | goto err; |
30 | 0 | pkcs12->authsafes->type = OBJ_nid2obj(mode); |
31 | |
|
32 | 0 | ossl_pkcs7_set0_libctx(pkcs12->authsafes, ctx); |
33 | 0 | if (!ossl_pkcs7_set1_propq(pkcs12->authsafes, propq)) { |
34 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_PKCS7_LIB); |
35 | 0 | goto err; |
36 | 0 | } |
37 | | |
38 | 0 | switch (mode) { |
39 | 0 | case NID_pkcs7_data: |
40 | 0 | if ((pkcs12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL) { |
41 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
42 | 0 | goto err; |
43 | 0 | } |
44 | 0 | break; |
45 | 0 | default: |
46 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE); |
47 | 0 | goto err; |
48 | 0 | } |
49 | 0 | return pkcs12; |
50 | | |
51 | 0 | err: |
52 | 0 | PKCS12_free(pkcs12); |
53 | 0 | return NULL; |
54 | 0 | } |
55 | | |
56 | | int PKCS12_check_version(const PKCS12 *p12) |
57 | 0 | { |
58 | 0 | if (p12 == NULL) |
59 | 0 | return 0; |
60 | 0 | return ASN1_INTEGER_get(p12->version) == PKCS12_VERSION; |
61 | 0 | } |
62 | | |
63 | | PKCS12 *PKCS12_init(int mode) |
64 | 0 | { |
65 | 0 | return PKCS12_init_ex(mode, NULL, NULL); |
66 | 0 | } |
67 | | |
68 | | const PKCS7_CTX *ossl_pkcs12_get0_pkcs7ctx(const PKCS12 *p12) |
69 | 0 | { |
70 | 0 | if (p12 == NULL || p12->authsafes == NULL) |
71 | 0 | return NULL; |
72 | 0 | return &p12->authsafes->ctx; |
73 | 0 | } |