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