/src/openssl30/crypto/pkcs12/p12_sbag.c
Line | Count | Source |
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 "p12_local.h" |
14 | | |
15 | | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
16 | | ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid) |
17 | 0 | { |
18 | 0 | return PKCS12_get_attr_gen(bag->attrib, attr_nid); |
19 | 0 | } |
20 | | #endif |
21 | | |
22 | | const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag, |
23 | | int attr_nid) |
24 | 0 | { |
25 | 0 | return PKCS12_get_attr_gen(bag->attrib, attr_nid); |
26 | 0 | } |
27 | | |
28 | | ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid) |
29 | 0 | { |
30 | 0 | return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid); |
31 | 0 | } |
32 | | |
33 | | const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag) |
34 | 0 | { |
35 | 0 | if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag) |
36 | 0 | return NULL; |
37 | 0 | return bag->value.keybag; |
38 | 0 | } |
39 | | |
40 | | const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag) |
41 | 0 | { |
42 | 0 | if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag) |
43 | 0 | return NULL; |
44 | 0 | return bag->value.shkeybag; |
45 | 0 | } |
46 | | |
47 | | const STACK_OF(PKCS12_SAFEBAG) * |
48 | | PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag) |
49 | 0 | { |
50 | 0 | if (OBJ_obj2nid(bag->type) != NID_safeContentsBag) |
51 | 0 | return NULL; |
52 | 0 | return bag->value.safes; |
53 | 0 | } |
54 | | |
55 | | const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag) |
56 | 0 | { |
57 | 0 | return bag->type; |
58 | 0 | } |
59 | | |
60 | | int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag) |
61 | 0 | { |
62 | 0 | return OBJ_obj2nid(bag->type); |
63 | 0 | } |
64 | | |
65 | | int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag) |
66 | 0 | { |
67 | 0 | int btype = PKCS12_SAFEBAG_get_nid(bag); |
68 | |
|
69 | 0 | if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag) |
70 | 0 | return -1; |
71 | 0 | return OBJ_obj2nid(bag->value.bag->type); |
72 | 0 | } |
73 | | |
74 | | const ASN1_OBJECT *PKCS12_SAFEBAG_get0_bag_type(const PKCS12_SAFEBAG *bag) |
75 | 0 | { |
76 | 0 | int btype = PKCS12_SAFEBAG_get_nid(bag); |
77 | |
|
78 | 0 | if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag) |
79 | 0 | return NULL; |
80 | 0 | return bag->value.bag->type; |
81 | 0 | } |
82 | | |
83 | | const ASN1_TYPE *PKCS12_SAFEBAG_get0_bag_obj(const PKCS12_SAFEBAG *bag) |
84 | 0 | { |
85 | 0 | int vtype = PKCS12_SAFEBAG_get_bag_nid(bag); |
86 | |
|
87 | 0 | if (vtype == -1 || vtype == NID_x509Certificate || vtype == NID_x509Crl |
88 | 0 | || vtype == NID_sdsiCertificate) |
89 | 0 | return NULL; |
90 | 0 | return bag->value.bag->value.other; |
91 | 0 | } |
92 | | |
93 | | X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag) |
94 | 0 | { |
95 | 0 | if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag) |
96 | 0 | return NULL; |
97 | 0 | if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate) |
98 | 0 | return NULL; |
99 | 0 | return ASN1_item_unpack(bag->value.bag->value.octet, |
100 | 0 | ASN1_ITEM_rptr(X509)); |
101 | 0 | } |
102 | | |
103 | | X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag) |
104 | 0 | { |
105 | 0 | if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag) |
106 | 0 | return NULL; |
107 | 0 | if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl) |
108 | 0 | return NULL; |
109 | 0 | return ASN1_item_unpack(bag->value.bag->value.octet, |
110 | 0 | ASN1_ITEM_rptr(X509_CRL)); |
111 | 0 | } |
112 | | |
113 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509) |
114 | 0 | { |
115 | 0 | return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509), |
116 | 0 | NID_x509Certificate, NID_certBag); |
117 | 0 | } |
118 | | |
119 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl) |
120 | 0 | { |
121 | 0 | return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL), |
122 | 0 | NID_x509Crl, NID_crlBag); |
123 | 0 | } |
124 | | |
125 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_secret(int type, int vtype, const unsigned char *value, int len) |
126 | 0 | { |
127 | 0 | PKCS12_BAGS *bag; |
128 | 0 | PKCS12_SAFEBAG *safebag; |
129 | |
|
130 | 0 | if ((bag = PKCS12_BAGS_new()) == NULL) { |
131 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
132 | 0 | return NULL; |
133 | 0 | } |
134 | 0 | bag->type = OBJ_nid2obj(type); |
135 | |
|
136 | 0 | switch(vtype) { |
137 | 0 | case V_ASN1_OCTET_STRING: |
138 | 0 | { |
139 | 0 | ASN1_OCTET_STRING *strtmp = ASN1_OCTET_STRING_new(); |
140 | |
|
141 | 0 | if (strtmp == NULL) { |
142 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
143 | 0 | goto err; |
144 | 0 | } |
145 | | /* Pack data into an octet string */ |
146 | 0 | if (!ASN1_OCTET_STRING_set(strtmp, value, len)) { |
147 | 0 | ASN1_OCTET_STRING_free(strtmp); |
148 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR); |
149 | 0 | goto err; |
150 | 0 | } |
151 | 0 | bag->value.other = ASN1_TYPE_new(); |
152 | 0 | if (bag->value.other == NULL) { |
153 | 0 | ASN1_OCTET_STRING_free(strtmp); |
154 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
155 | 0 | goto err; |
156 | 0 | } |
157 | 0 | ASN1_TYPE_set(bag->value.other, vtype, strtmp); |
158 | 0 | } |
159 | 0 | break; |
160 | | |
161 | 0 | default: |
162 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_TYPE); |
163 | 0 | goto err; |
164 | 0 | } |
165 | | |
166 | 0 | if ((safebag = PKCS12_SAFEBAG_new()) == NULL) { |
167 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
168 | 0 | goto err; |
169 | 0 | } |
170 | 0 | safebag->value.bag = bag; |
171 | 0 | safebag->type = OBJ_nid2obj(NID_secretBag); |
172 | 0 | return safebag; |
173 | | |
174 | 0 | err: |
175 | 0 | PKCS12_BAGS_free(bag); |
176 | 0 | return NULL; |
177 | 0 | } |
178 | | |
179 | | /* Turn PKCS8 object into a keybag */ |
180 | | |
181 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8) |
182 | 0 | { |
183 | 0 | PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new(); |
184 | |
|
185 | 0 | if (bag == NULL) { |
186 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
187 | 0 | return NULL; |
188 | 0 | } |
189 | 0 | bag->type = OBJ_nid2obj(NID_keyBag); |
190 | 0 | bag->value.keybag = p8; |
191 | 0 | return bag; |
192 | 0 | } |
193 | | |
194 | | /* Turn PKCS8 object into a shrouded keybag */ |
195 | | |
196 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8) |
197 | 0 | { |
198 | 0 | PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new(); |
199 | | |
200 | | /* Set up the safe bag */ |
201 | 0 | if (bag == NULL) { |
202 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); |
203 | 0 | return NULL; |
204 | 0 | } |
205 | 0 | bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag); |
206 | 0 | bag->value.shkeybag = p8; |
207 | 0 | return bag; |
208 | 0 | } |
209 | | |
210 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(int pbe_nid, |
211 | | const char *pass, |
212 | | int passlen, |
213 | | unsigned char *salt, |
214 | | int saltlen, int iter, |
215 | | PKCS8_PRIV_KEY_INFO *p8inf, |
216 | | OSSL_LIB_CTX *ctx, |
217 | | const char *propq) |
218 | 0 | { |
219 | 0 | PKCS12_SAFEBAG *bag = NULL; |
220 | 0 | const EVP_CIPHER *pbe_ciph = NULL; |
221 | 0 | EVP_CIPHER *pbe_ciph_fetch = NULL; |
222 | 0 | X509_SIG *p8; |
223 | |
|
224 | 0 | ERR_set_mark(); |
225 | 0 | pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq); |
226 | 0 | if (pbe_ciph == NULL) |
227 | 0 | pbe_ciph = EVP_get_cipherbynid(pbe_nid); |
228 | 0 | ERR_pop_to_mark(); |
229 | |
|
230 | 0 | if (pbe_ciph != NULL) |
231 | 0 | pbe_nid = -1; |
232 | |
|
233 | 0 | p8 = PKCS8_encrypt_ex(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter, |
234 | 0 | p8inf, ctx, propq); |
235 | 0 | if (p8 == NULL) |
236 | 0 | goto err; |
237 | | |
238 | 0 | bag = PKCS12_SAFEBAG_create0_pkcs8(p8); |
239 | 0 | if (bag == NULL) |
240 | 0 | X509_SIG_free(p8); |
241 | |
|
242 | 0 | err: |
243 | 0 | EVP_CIPHER_free(pbe_ciph_fetch); |
244 | 0 | return bag; |
245 | 0 | } |
246 | | |
247 | | PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid, |
248 | | const char *pass, |
249 | | int passlen, |
250 | | unsigned char *salt, |
251 | | int saltlen, int iter, |
252 | | PKCS8_PRIV_KEY_INFO *p8inf) |
253 | 0 | { |
254 | 0 | return PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(pbe_nid, pass, passlen, |
255 | 0 | salt, saltlen, iter, p8inf, |
256 | 0 | NULL, NULL); |
257 | 0 | } |