/src/openssl35/crypto/crmf/crmf_lib.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright Nokia 2007-2018 |
4 | | * Copyright Siemens AG 2015-2019 |
5 | | * |
6 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
7 | | * this file except in compliance with the License. You can obtain a copy |
8 | | * in the file LICENSE in the source distribution or at |
9 | | * https://www.openssl.org/source/license.html |
10 | | * |
11 | | * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb. |
12 | | */ |
13 | | |
14 | | /* |
15 | | * This file contains the functions that handle the individual items inside |
16 | | * the CRMF structures |
17 | | */ |
18 | | |
19 | | /* |
20 | | * NAMING |
21 | | * |
22 | | * The 0 functions use the supplied structure pointer directly in the parent and |
23 | | * it will be freed up when the parent is freed. |
24 | | * |
25 | | * The 1 functions use a copy of the supplied structure pointer (or in some |
26 | | * cases increases its link count) in the parent and so both should be freed up. |
27 | | */ |
28 | | |
29 | | #include <openssl/asn1t.h> |
30 | | |
31 | | #include "crmf_local.h" |
32 | | #include "internal/constant_time.h" |
33 | | #include "internal/sizes.h" |
34 | | #include "crypto/evp.h" |
35 | | #include "crypto/x509.h" |
36 | | |
37 | | /* explicit #includes not strictly needed since implied by the above: */ |
38 | | #include <openssl/crmf.h> |
39 | | #include <openssl/err.h> |
40 | | #include <openssl/evp.h> |
41 | | #include <openssl/cms.h> |
42 | | |
43 | | /*- |
44 | | * atyp = Attribute Type |
45 | | * valt = Value Type |
46 | | * ctrlinf = "regCtrl" or "regInfo" |
47 | | */ |
48 | | #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \ |
49 | | valt *OSSL_CRMF_MSG_get0_##ctrlinf##_##atyp(const OSSL_CRMF_MSG *msg) \ |
50 | 0 | { \ |
51 | 0 | int i; \ |
52 | 0 | STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *controls; \ |
53 | 0 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \ |
54 | 0 | \ |
55 | 0 | if (msg == NULL || msg->certReq == NULL) \ |
56 | 0 | return NULL; \ |
57 | 0 | controls = msg->certReq->controls; \ |
58 | 0 | for (i = 0; i < sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_num(controls); i++) { \ |
59 | 0 | atav = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_value(controls, i); \ |
60 | 0 | if (OBJ_obj2nid(atav->type) == NID_id_##ctrlinf##_##atyp) \ |
61 | 0 | return atav->value.atyp; \ |
62 | 0 | } \ |
63 | 0 | return NULL; \ |
64 | 0 | } \ |
65 | | \ |
66 | | int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, const valt *in) \ |
67 | 90 | { \ |
68 | 90 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \ |
69 | 90 | \ |
70 | 90 | if (msg == NULL || in == NULL) \ |
71 | 90 | goto err; \ |
72 | 90 | if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \ |
73 | 90 | goto err; \ |
74 | 90 | if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \ |
75 | 90 | goto err; \ |
76 | 90 | if ((atav->value.atyp = valt##_dup(in)) == NULL) \ |
77 | 90 | goto err; \ |
78 | 90 | if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \ |
79 | 90 | goto err; \ |
80 | 90 | return 1; \ |
81 | 90 | err: \ |
82 | 0 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \ |
83 | 0 | return 0; \ |
84 | 90 | } |
85 | | |
86 | | /*- |
87 | | * Pushes the given control attribute into the controls stack of a CertRequest |
88 | | * (section 6) |
89 | | * returns 1 on success, 0 on error |
90 | | */ |
91 | | static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm, |
92 | | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl) |
93 | 90 | { |
94 | 90 | int new = 0; |
95 | | |
96 | 90 | if (crm == NULL || crm->certReq == NULL || ctrl == NULL) { |
97 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | 90 | if (crm->certReq->controls == NULL) { |
102 | 90 | crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null(); |
103 | 90 | if (crm->certReq->controls == NULL) |
104 | 0 | goto err; |
105 | 90 | new = 1; |
106 | 90 | } |
107 | 90 | if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl)) |
108 | 0 | goto err; |
109 | | |
110 | 90 | return 1; |
111 | 0 | err: |
112 | 0 | if (new != 0) { |
113 | 0 | sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls); |
114 | 0 | crm->certReq->controls = NULL; |
115 | 0 | } |
116 | 0 | return 0; |
117 | 90 | } |
118 | | |
119 | | /* id-regCtrl-regToken Control (section 6.1) */ |
120 | 0 | IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl) Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_regToken Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_regToken |
121 | | |
122 | | /* id-regCtrl-authenticator Control (section 6.2) */ |
123 | 0 | #define ASN1_UTF8STRING_dup ASN1_STRING_dup |
124 | 0 | IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl) Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_authenticator Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_authenticator |
125 | | |
126 | | int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi, |
127 | | int method, GENERAL_NAME *nm) |
128 | 0 | { |
129 | 0 | if (spi == NULL |
130 | 0 | || method < OSSL_CRMF_PUB_METHOD_DONTCARE |
131 | 0 | || method > OSSL_CRMF_PUB_METHOD_LDAP) { |
132 | 0 | ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT); |
133 | 0 | return 0; |
134 | 0 | } |
135 | | |
136 | 0 | if (!ASN1_INTEGER_set(spi->pubMethod, method)) |
137 | 0 | return 0; |
138 | 0 | GENERAL_NAME_free(spi->pubLocation); |
139 | 0 | spi->pubLocation = nm; |
140 | 0 | return 1; |
141 | 0 | } |
142 | | |
143 | | int OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi, |
144 | | OSSL_CRMF_SINGLEPUBINFO *spi) |
145 | 0 | { |
146 | 0 | if (pi == NULL || spi == NULL) { |
147 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
148 | 0 | return 0; |
149 | 0 | } |
150 | 0 | if (pi->pubInfos == NULL) |
151 | 0 | pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null(); |
152 | 0 | if (pi->pubInfos == NULL) |
153 | 0 | return 0; |
154 | | |
155 | 0 | return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi); |
156 | 0 | } |
157 | | |
158 | | int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi, |
159 | | int action) |
160 | 0 | { |
161 | 0 | if (pi == NULL |
162 | 0 | || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH |
163 | 0 | || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) { |
164 | 0 | ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT); |
165 | 0 | return 0; |
166 | 0 | } |
167 | | |
168 | 0 | return ASN1_INTEGER_set(pi->action, action); |
169 | 0 | } |
170 | | |
171 | | /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */ |
172 | 0 | IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO, Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_pkiPublicationInfo Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_pkiPublicationInfo |
173 | | regCtrl) |
174 | | |
175 | | /* id-regCtrl-oldCertID Control (section 6.5) from the given */ |
176 | 90 | IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl) Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_oldCertID OSSL_CRMF_MSG_set1_regCtrl_oldCertID Line | Count | Source | 176 | | IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl) |
|
177 | | |
178 | | OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer, |
179 | | const ASN1_INTEGER *serial) |
180 | 140 | { |
181 | 140 | OSSL_CRMF_CERTID *cid = NULL; |
182 | | |
183 | 140 | if (issuer == NULL || serial == NULL) { |
184 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
185 | 0 | return NULL; |
186 | 0 | } |
187 | | |
188 | 140 | if ((cid = OSSL_CRMF_CERTID_new()) == NULL) |
189 | 0 | goto err; |
190 | | |
191 | 140 | if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer)) |
192 | 0 | goto err; |
193 | 140 | cid->issuer->type = GEN_DIRNAME; |
194 | | |
195 | 140 | ASN1_INTEGER_free(cid->serialNumber); |
196 | 140 | if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL) |
197 | 0 | goto err; |
198 | | |
199 | 140 | return cid; |
200 | | |
201 | 0 | err: |
202 | 0 | OSSL_CRMF_CERTID_free(cid); |
203 | 0 | return NULL; |
204 | 140 | } |
205 | | |
206 | | /* |
207 | | * id-regCtrl-protocolEncrKey Control (section 6.6) |
208 | | */ |
209 | 0 | IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl) Unexecuted instantiation: OSSL_CRMF_MSG_get0_regCtrl_protocolEncrKey Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_protocolEncrKey |
210 | | |
211 | | /*- |
212 | | * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack. |
213 | | * (section 7) |
214 | | * returns 1 on success, 0 on error |
215 | | */ |
216 | | static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm, |
217 | | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri) |
218 | 0 | { |
219 | 0 | STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL; |
220 | |
|
221 | 0 | if (crm == NULL || ri == NULL) { |
222 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
223 | 0 | return 0; |
224 | 0 | } |
225 | | |
226 | 0 | if (crm->regInfo == NULL) |
227 | 0 | crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null(); |
228 | 0 | if (crm->regInfo == NULL) |
229 | 0 | goto err; |
230 | 0 | if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri)) |
231 | 0 | goto err; |
232 | 0 | return 1; |
233 | | |
234 | 0 | err: |
235 | 0 | if (info != NULL) |
236 | 0 | crm->regInfo = NULL; |
237 | 0 | sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info); |
238 | 0 | return 0; |
239 | 0 | } |
240 | | |
241 | | /* id-regInfo-utf8Pairs to regInfo (section 7.1) */ |
242 | 0 | IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo) Unexecuted instantiation: OSSL_CRMF_MSG_get0_regInfo_utf8Pairs Unexecuted instantiation: OSSL_CRMF_MSG_set1_regInfo_utf8Pairs |
243 | | |
244 | | /* id-regInfo-certReq to regInfo (section 7.2) */ |
245 | 0 | IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo) Unexecuted instantiation: OSSL_CRMF_MSG_get0_regInfo_certReq Unexecuted instantiation: OSSL_CRMF_MSG_set1_regInfo_certReq |
246 | | |
247 | | /* retrieves the certificate template of crm */ |
248 | | OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm) |
249 | 4.42k | { |
250 | 4.42k | if (crm == NULL || crm->certReq == NULL) { |
251 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
252 | 0 | return NULL; |
253 | 0 | } |
254 | 4.42k | return crm->certReq->certTemplate; |
255 | 4.42k | } |
256 | | |
257 | | int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm, |
258 | | ASN1_TIME *notBefore, ASN1_TIME *notAfter) |
259 | 0 | { |
260 | 0 | OSSL_CRMF_OPTIONALVALIDITY *vld; |
261 | 0 | OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
262 | |
|
263 | 0 | if (tmpl == NULL) { /* also crm == NULL implies this */ |
264 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | 0 | if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL) |
269 | 0 | return 0; |
270 | 0 | vld->notBefore = notBefore; |
271 | 0 | vld->notAfter = notAfter; |
272 | 0 | tmpl->validity = vld; |
273 | 0 | return 1; |
274 | 0 | } |
275 | | |
276 | | int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid) |
277 | 1.17k | { |
278 | 1.17k | if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) { |
279 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
280 | 0 | return 0; |
281 | 0 | } |
282 | | |
283 | 1.17k | return ASN1_INTEGER_set(crm->certReq->certReqId, rid); |
284 | 1.17k | } |
285 | | |
286 | | /* get ASN.1 encoded integer, return -1 on error */ |
287 | | static int crmf_asn1_get_int(const ASN1_INTEGER *a) |
288 | 3.32k | { |
289 | 3.32k | int64_t res; |
290 | | |
291 | 3.32k | if (!ASN1_INTEGER_get_int64(&res, a)) { |
292 | 7 | ERR_raise(ERR_LIB_CRMF, ASN1_R_INVALID_NUMBER); |
293 | 7 | return -1; |
294 | 7 | } |
295 | 3.32k | if (res < INT_MIN) { |
296 | 65 | ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_SMALL); |
297 | 65 | return -1; |
298 | 65 | } |
299 | 3.25k | if (res > INT_MAX) { |
300 | 8 | ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_LARGE); |
301 | 8 | return -1; |
302 | 8 | } |
303 | 3.24k | return (int)res; |
304 | 3.25k | } |
305 | | |
306 | | int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm) |
307 | 3.32k | { |
308 | 3.32k | if (crm == NULL || /* not really needed: */ crm->certReq == NULL) { |
309 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
310 | 0 | return -1; |
311 | 0 | } |
312 | 3.32k | return crmf_asn1_get_int(crm->certReq->certReqId); |
313 | 3.32k | } |
314 | | |
315 | | int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm, |
316 | | X509_EXTENSIONS *exts) |
317 | 1.17k | { |
318 | 1.17k | OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
319 | | |
320 | 1.17k | if (tmpl == NULL) { /* also crm == NULL implies this */ |
321 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
322 | 0 | return 0; |
323 | 0 | } |
324 | | |
325 | 1.17k | if (sk_X509_EXTENSION_num(exts) == 0) { |
326 | 0 | sk_X509_EXTENSION_free(exts); |
327 | 0 | exts = NULL; /* do not include empty extensions list */ |
328 | 0 | } |
329 | | |
330 | 1.17k | sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free); |
331 | 1.17k | tmpl->extensions = exts; |
332 | 1.17k | return 1; |
333 | 1.17k | } |
334 | | |
335 | | int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm, |
336 | | X509_EXTENSION *ext) |
337 | 0 | { |
338 | 0 | int new = 0; |
339 | 0 | OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
340 | |
|
341 | 0 | if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */ |
342 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
343 | 0 | return 0; |
344 | 0 | } |
345 | | |
346 | 0 | if (tmpl->extensions == NULL) { |
347 | 0 | if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL) |
348 | 0 | goto err; |
349 | 0 | new = 1; |
350 | 0 | } |
351 | | |
352 | 0 | if (!sk_X509_EXTENSION_push(tmpl->extensions, ext)) |
353 | 0 | goto err; |
354 | 0 | return 1; |
355 | 0 | err: |
356 | 0 | if (new != 0) { |
357 | 0 | sk_X509_EXTENSION_free(tmpl->extensions); |
358 | 0 | tmpl->extensions = NULL; |
359 | 0 | } |
360 | 0 | return 0; |
361 | 0 | } |
362 | | |
363 | | static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps, |
364 | | const OSSL_CRMF_CERTREQUEST *cr, |
365 | | EVP_PKEY *pkey, const EVP_MD *digest, |
366 | | OSSL_LIB_CTX *libctx, const char *propq) |
367 | 0 | { |
368 | 0 | char name[80] = ""; |
369 | 0 | EVP_PKEY *pub; |
370 | |
|
371 | 0 | if (ps == NULL || cr == NULL || pkey == NULL) { |
372 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
373 | 0 | return 0; |
374 | 0 | } |
375 | 0 | pub = X509_PUBKEY_get0(cr->certTemplate->publicKey); |
376 | 0 | if (!ossl_x509_check_private_key(pub, pkey)) |
377 | 0 | return 0; |
378 | | |
379 | 0 | if (ps->poposkInput != NULL) { |
380 | | /* We do not support cases 1+2 defined in RFC 4211, section 4.1 */ |
381 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPOSKINPUT_NOT_SUPPORTED); |
382 | 0 | return 0; |
383 | 0 | } |
384 | | |
385 | 0 | if (EVP_PKEY_get_default_digest_name(pkey, name, sizeof(name)) > 0 |
386 | 0 | && strcmp(name, "UNDEF") == 0) /* at least for Ed25519, Ed448 */ |
387 | 0 | digest = NULL; |
388 | |
|
389 | 0 | return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST), |
390 | 0 | ps->algorithmIdentifier, /* sets this X509_ALGOR */ |
391 | 0 | NULL, ps->signature, /* sets the ASN1_BIT_STRING */ |
392 | 0 | cr, NULL, pkey, digest, libctx, propq); |
393 | 0 | } |
394 | | |
395 | | int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm, |
396 | | EVP_PKEY *pkey, const EVP_MD *digest, |
397 | | OSSL_LIB_CTX *libctx, const char *propq) |
398 | 1.17k | { |
399 | 1.17k | OSSL_CRMF_POPO *pp = NULL; |
400 | 1.17k | ASN1_INTEGER *tag = NULL; |
401 | | |
402 | 1.17k | if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) { |
403 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
404 | 0 | return 0; |
405 | 0 | } |
406 | | |
407 | 1.17k | if (meth == OSSL_CRMF_POPO_NONE) |
408 | 1.17k | goto end; |
409 | 0 | if ((pp = OSSL_CRMF_POPO_new()) == NULL) |
410 | 0 | goto err; |
411 | 0 | pp->type = meth; |
412 | |
|
413 | 0 | switch (meth) { |
414 | 0 | case OSSL_CRMF_POPO_RAVERIFIED: |
415 | 0 | if ((pp->value.raVerified = ASN1_NULL_new()) == NULL) |
416 | 0 | goto err; |
417 | 0 | break; |
418 | | |
419 | 0 | case OSSL_CRMF_POPO_SIGNATURE: { |
420 | 0 | OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new(); |
421 | |
|
422 | 0 | if (ps == NULL) |
423 | 0 | goto err; |
424 | 0 | if (!create_popo_signature(ps, crm->certReq, pkey, digest, |
425 | 0 | libctx, propq)) { |
426 | 0 | OSSL_CRMF_POPOSIGNINGKEY_free(ps); |
427 | 0 | goto err; |
428 | 0 | } |
429 | 0 | pp->value.signature = ps; |
430 | 0 | } break; |
431 | | |
432 | 0 | case OSSL_CRMF_POPO_KEYENC: |
433 | 0 | if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL) |
434 | 0 | goto err; |
435 | 0 | tag = ASN1_INTEGER_new(); |
436 | 0 | pp->value.keyEncipherment->type = OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE; |
437 | 0 | pp->value.keyEncipherment->value.subsequentMessage = tag; |
438 | 0 | if (tag == NULL |
439 | 0 | || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT)) |
440 | 0 | goto err; |
441 | 0 | break; |
442 | | |
443 | 0 | default: |
444 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO); |
445 | 0 | goto err; |
446 | 0 | } |
447 | | |
448 | 1.17k | end: |
449 | 1.17k | OSSL_CRMF_POPO_free(crm->popo); |
450 | 1.17k | crm->popo = pp; |
451 | | |
452 | 1.17k | return 1; |
453 | 0 | err: |
454 | 0 | OSSL_CRMF_POPO_free(pp); |
455 | 0 | return 0; |
456 | 0 | } |
457 | | |
458 | | /* verifies the Proof-of-Possession of the request with the given rid in reqs */ |
459 | | int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs, |
460 | | int rid, int acceptRAVerified, |
461 | | OSSL_LIB_CTX *libctx, const char *propq) |
462 | 2.85k | { |
463 | 2.85k | OSSL_CRMF_MSG *req = NULL; |
464 | 2.85k | X509_PUBKEY *pubkey = NULL; |
465 | 2.85k | OSSL_CRMF_POPOSIGNINGKEY *sig = NULL; |
466 | 2.85k | const ASN1_ITEM *it; |
467 | 2.85k | void *asn; |
468 | | |
469 | 2.85k | if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) { |
470 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
471 | 0 | return 0; |
472 | 0 | } |
473 | | |
474 | 2.85k | if (req->popo == NULL) { |
475 | 3 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING); |
476 | 3 | return 0; |
477 | 3 | } |
478 | | |
479 | 2.85k | switch (req->popo->type) { |
480 | 0 | case OSSL_CRMF_POPO_RAVERIFIED: |
481 | 0 | if (!acceptRAVerified) { |
482 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED); |
483 | 0 | return 0; |
484 | 0 | } |
485 | 0 | break; |
486 | 2.85k | case OSSL_CRMF_POPO_SIGNATURE: |
487 | 2.85k | pubkey = req->certReq->certTemplate->publicKey; |
488 | 2.85k | if (pubkey == NULL) { |
489 | 3 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY); |
490 | 3 | return 0; |
491 | 3 | } |
492 | 2.85k | sig = req->popo->value.signature; |
493 | 2.85k | if (sig->poposkInput != NULL) { |
494 | | /* |
495 | | * According to RFC 4211: publicKey contains a copy of |
496 | | * the public key from the certificate template. This MUST be |
497 | | * exactly the same value as contained in the certificate template. |
498 | | */ |
499 | 0 | if (sig->poposkInput->publicKey == NULL) { |
500 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY); |
501 | 0 | return 0; |
502 | 0 | } |
503 | 0 | if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) { |
504 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY); |
505 | 0 | return 0; |
506 | 0 | } |
507 | | |
508 | | /* |
509 | | * Should check at this point the contents of the authInfo sub-field |
510 | | * as requested in FR #19807 according to RFC 4211 section 4.1. |
511 | | */ |
512 | | |
513 | 0 | it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT); |
514 | 0 | asn = sig->poposkInput; |
515 | 2.85k | } else { |
516 | 2.85k | if (req->certReq->certTemplate->subject == NULL) { |
517 | 13 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_SUBJECT); |
518 | 13 | return 0; |
519 | 13 | } |
520 | 2.84k | it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST); |
521 | 2.84k | asn = req->certReq; |
522 | 2.84k | } |
523 | 2.84k | if (ASN1_item_verify_ex(it, sig->algorithmIdentifier, sig->signature, |
524 | 2.84k | asn, NULL, X509_PUBKEY_get0(pubkey), libctx, |
525 | 2.84k | propq) |
526 | 2.84k | < 1) |
527 | 2.73k | return 0; |
528 | 106 | break; |
529 | 106 | case OSSL_CRMF_POPO_KEYENC: |
530 | | /* |
531 | | * When OSSL_CMP_certrep_new() supports encrypted certs, |
532 | | * should return 1 if the type of req->popo->value.keyEncipherment |
533 | | * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and |
534 | | * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT |
535 | | */ |
536 | 0 | case OSSL_CRMF_POPO_KEYAGREE: |
537 | 0 | default: |
538 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_POPO_METHOD); |
539 | 0 | return 0; |
540 | 2.85k | } |
541 | 106 | return 1; |
542 | 2.85k | } |
543 | | |
544 | | int OSSL_CRMF_MSG_centralkeygen_requested(const OSSL_CRMF_MSG *crm, const X509_REQ *p10cr) |
545 | 3.44k | { |
546 | 3.44k | X509_PUBKEY *pubkey = NULL; |
547 | 3.44k | const unsigned char *pk = NULL; |
548 | 3.44k | int pklen, ret = 0; |
549 | | |
550 | 3.44k | if (crm == NULL && p10cr == NULL) { |
551 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
552 | 0 | return -1; |
553 | 0 | } |
554 | | |
555 | 3.44k | if (crm != NULL) |
556 | 1.54k | pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(OSSL_CRMF_MSG_get0_tmpl(crm)); |
557 | 1.89k | else |
558 | 1.89k | pubkey = p10cr->req_info.pubkey; |
559 | | |
560 | 3.44k | if (pubkey == NULL |
561 | 3.44k | || (X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey) |
562 | 3.44k | && pklen == 0)) |
563 | 6 | ret = 1; |
564 | | |
565 | | /* |
566 | | * In case of CRMF, POPO MUST be absent if central key generation |
567 | | * is requested, otherwise MUST be present |
568 | | */ |
569 | 3.44k | if (crm != NULL && ret != (crm->popo == NULL)) { |
570 | 3 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_CENTRAL_KEYGEN); |
571 | 3 | return -2; |
572 | 3 | } |
573 | 3.44k | return ret; |
574 | 3.44k | } |
575 | | |
576 | | X509_PUBKEY |
577 | | *OSSL_CRMF_CERTTEMPLATE_get0_publicKey(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
578 | 1.54k | { |
579 | 1.54k | return tmpl != NULL ? tmpl->publicKey : NULL; |
580 | 1.54k | } |
581 | | |
582 | | const ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
583 | 73 | { |
584 | 73 | return tmpl != NULL ? tmpl->serialNumber : NULL; |
585 | 73 | } |
586 | | |
587 | | const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_subject(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
588 | 0 | { |
589 | 0 | return tmpl != NULL ? tmpl->subject : NULL; |
590 | 0 | } |
591 | | |
592 | | const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
593 | 73 | { |
594 | 73 | return tmpl != NULL ? tmpl->issuer : NULL; |
595 | 73 | } |
596 | | |
597 | | X509_EXTENSIONS |
598 | | *OSSL_CRMF_CERTTEMPLATE_get0_extensions(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
599 | 0 | { |
600 | 0 | return tmpl != NULL ? tmpl->extensions : NULL; |
601 | 0 | } |
602 | | |
603 | | const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid) |
604 | 0 | { |
605 | 0 | return cid != NULL && cid->issuer->type == GEN_DIRNAME ? cid->issuer->d.directoryName : NULL; |
606 | 0 | } |
607 | | |
608 | | const ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID |
609 | | *cid) |
610 | 0 | { |
611 | 0 | return cid != NULL ? cid->serialNumber : NULL; |
612 | 0 | } |
613 | | |
614 | | /*- |
615 | | * Fill in the certificate template |tmpl|. |
616 | | * Any other NULL argument will leave the respective field unchanged. |
617 | | */ |
618 | | int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl, |
619 | | EVP_PKEY *pubkey, |
620 | | const X509_NAME *subject, |
621 | | const X509_NAME *issuer, |
622 | | const ASN1_INTEGER *serial) |
623 | 1.35k | { |
624 | 1.35k | if (tmpl == NULL) { |
625 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
626 | 0 | return 0; |
627 | 0 | } |
628 | 1.35k | if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject)) |
629 | 0 | return 0; |
630 | 1.35k | if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer)) |
631 | 0 | return 0; |
632 | 1.35k | if (serial != NULL) { |
633 | 182 | ASN1_INTEGER_free(tmpl->serialNumber); |
634 | 182 | if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL) |
635 | 0 | return 0; |
636 | 182 | } |
637 | 1.35k | if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey)) |
638 | 0 | return 0; |
639 | 1.35k | return 1; |
640 | 1.35k | } |
641 | | |
642 | | #ifndef OPENSSL_NO_CMS |
643 | | DECLARE_ASN1_ITEM(CMS_SignedData) /* copied from cms_local.h */ |
644 | | |
645 | | /* check for KGA authorization implied by CA flag or by explicit EKU cmKGA */ |
646 | | static int check_cmKGA(ossl_unused const X509_PURPOSE *purpose, const X509 *x, int ca) |
647 | 0 | { |
648 | 0 | STACK_OF(ASN1_OBJECT) *ekus; |
649 | 0 | int i, ret = 1; |
650 | |
|
651 | 0 | if (ca) |
652 | 0 | return ret; |
653 | 0 | ekus = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL); |
654 | 0 | for (i = 0; i < sk_ASN1_OBJECT_num(ekus); i++) { |
655 | 0 | if (OBJ_obj2nid(sk_ASN1_OBJECT_value(ekus, i)) == NID_cmKGA) |
656 | 0 | goto end; |
657 | 0 | } |
658 | 0 | ret = 0; |
659 | |
|
660 | 0 | end: |
661 | 0 | sk_ASN1_OBJECT_pop_free(ekus, ASN1_OBJECT_free); |
662 | 0 | return ret; |
663 | 0 | } |
664 | | #endif /* OPENSSL_NO_CMS */ |
665 | | |
666 | | EVP_PKEY *OSSL_CRMF_ENCRYPTEDKEY_get1_pkey(const OSSL_CRMF_ENCRYPTEDKEY *encryptedKey, |
667 | | X509_STORE *ts, STACK_OF(X509) *extra, EVP_PKEY *pkey, |
668 | | X509 *cert, ASN1_OCTET_STRING *secret, |
669 | | OSSL_LIB_CTX *libctx, const char *propq) |
670 | 0 | { |
671 | 0 | #ifndef OPENSSL_NO_CMS |
672 | 0 | BIO *bio = NULL; |
673 | 0 | CMS_SignedData *sd = NULL; |
674 | 0 | BIO *pkey_bio = NULL; |
675 | 0 | int purpose_id, bak_purpose_id; |
676 | 0 | X509_VERIFY_PARAM *vpm; |
677 | 0 | #endif |
678 | 0 | EVP_PKEY *ret = NULL; |
679 | |
|
680 | 0 | if (encryptedKey == NULL) { |
681 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
682 | 0 | return NULL; |
683 | 0 | } |
684 | 0 | if (encryptedKey->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA) { |
685 | 0 | unsigned char *p; |
686 | 0 | const unsigned char *p_copy; |
687 | 0 | int len; |
688 | |
|
689 | 0 | p = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(encryptedKey->value.encryptedValue, |
690 | 0 | libctx, propq, pkey, &len); |
691 | 0 | if ((p_copy = p) != NULL) |
692 | 0 | ret = d2i_AutoPrivateKey_ex(NULL, &p_copy, len, libctx, propq); |
693 | 0 | OPENSSL_free(p); |
694 | 0 | return ret; |
695 | 0 | } |
696 | | |
697 | 0 | #ifndef OPENSSL_NO_CMS |
698 | 0 | if (ts == NULL) { |
699 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
700 | 0 | return NULL; |
701 | 0 | } |
702 | 0 | if ((bio = CMS_EnvelopedData_decrypt(encryptedKey->value.envelopedData, |
703 | 0 | NULL, pkey, cert, secret, 0, |
704 | 0 | libctx, propq)) |
705 | 0 | == NULL) { |
706 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDKEY); |
707 | 0 | goto end; |
708 | 0 | } |
709 | 0 | sd = ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_SignedData), bio, NULL); |
710 | 0 | if (sd == NULL) |
711 | 0 | goto end; |
712 | | |
713 | 0 | if ((purpose_id = X509_PURPOSE_get_by_sname(SN_cmKGA)) < 0) { |
714 | 0 | purpose_id = X509_PURPOSE_get_unused_id(libctx); |
715 | 0 | if (!X509_PURPOSE_add(purpose_id, X509_TRUST_COMPAT, 0, check_cmKGA, |
716 | 0 | LN_cmKGA, SN_cmKGA, NULL)) |
717 | 0 | goto end; |
718 | 0 | } |
719 | 0 | if ((vpm = X509_STORE_get0_param(ts)) == NULL) |
720 | 0 | goto end; |
721 | | |
722 | | /* temporarily override X509_PURPOSE_SMIME_SIGN: */ |
723 | 0 | bak_purpose_id = X509_VERIFY_PARAM_get_purpose(vpm); |
724 | 0 | if (!X509_STORE_set_purpose(ts, purpose_id)) { |
725 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE); |
726 | 0 | goto end; |
727 | 0 | } |
728 | | |
729 | 0 | pkey_bio = CMS_SignedData_verify(sd, NULL, NULL /* scerts */, ts, |
730 | 0 | extra, NULL, 0, libctx, propq); |
731 | |
|
732 | 0 | if (!X509_STORE_set_purpose(ts, bak_purpose_id)) { |
733 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE); |
734 | 0 | goto end; |
735 | 0 | } |
736 | | |
737 | 0 | if (pkey_bio == NULL) { |
738 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_VERIFYING_ENCRYPTEDKEY); |
739 | 0 | goto end; |
740 | 0 | } |
741 | | |
742 | | /* unpack AsymmetricKeyPackage */ |
743 | 0 | if ((ret = d2i_PrivateKey_ex_bio(pkey_bio, NULL, libctx, propq)) == NULL) |
744 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_ENCRYPTEDKEY); |
745 | |
|
746 | 0 | end: |
747 | 0 | CMS_SignedData_free(sd); |
748 | 0 | BIO_free(bio); |
749 | 0 | BIO_free(pkey_bio); |
750 | 0 | return ret; |
751 | | #else |
752 | | /* prevent warning on unused parameters: */ |
753 | | ((void)ts, (void)extra, (void)cert, (void)secret); |
754 | | ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED); |
755 | | return NULL; |
756 | | #endif /* OPENSSL_NO_CMS */ |
757 | 0 | } |
758 | | |
759 | | unsigned char *OSSL_CRMF_ENCRYPTEDVALUE_decrypt(const OSSL_CRMF_ENCRYPTEDVALUE *enc, |
760 | | OSSL_LIB_CTX *libctx, const char *propq, |
761 | | EVP_PKEY *pkey, int *outlen) |
762 | 0 | { |
763 | 0 | EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */ |
764 | 0 | unsigned char *ek = NULL; /* decrypted symmetric encryption key */ |
765 | 0 | size_t eksize = 0; /* size of decrypted symmetric encryption key */ |
766 | 0 | EVP_CIPHER *cipher = NULL; /* used cipher */ |
767 | 0 | int cikeysize = 0; /* key size from cipher */ |
768 | 0 | unsigned char *iv = NULL; /* initial vector for symmetric encryption */ |
769 | 0 | unsigned char *out = NULL; /* decryption output buffer */ |
770 | 0 | int n, ret = 0; |
771 | 0 | EVP_PKEY_CTX *pkctx = NULL; /* private key context */ |
772 | 0 | char name[OSSL_MAX_NAME_SIZE]; |
773 | |
|
774 | 0 | if (outlen == NULL) { |
775 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
776 | 0 | return NULL; |
777 | 0 | } |
778 | 0 | *outlen = 0; |
779 | 0 | if (enc == NULL || enc->symmAlg == NULL || enc->encSymmKey == NULL |
780 | 0 | || enc->encValue == NULL || pkey == NULL) { |
781 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
782 | 0 | return NULL; |
783 | 0 | } |
784 | | |
785 | | /* select symmetric cipher based on algorithm given in message */ |
786 | 0 | OBJ_obj2txt(name, sizeof(name), enc->symmAlg->algorithm, 0); |
787 | 0 | (void)ERR_set_mark(); |
788 | 0 | cipher = EVP_CIPHER_fetch(libctx, name, propq); |
789 | 0 | if (cipher == NULL) |
790 | 0 | cipher = (EVP_CIPHER *)EVP_get_cipherbyobj(enc->symmAlg->algorithm); |
791 | 0 | if (cipher == NULL) { |
792 | 0 | (void)ERR_clear_last_mark(); |
793 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_CIPHER); |
794 | 0 | goto end; |
795 | 0 | } |
796 | 0 | (void)ERR_pop_to_mark(); |
797 | |
|
798 | 0 | cikeysize = EVP_CIPHER_get_key_length(cipher); |
799 | | /* first the symmetric key needs to be decrypted */ |
800 | 0 | pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
801 | 0 | if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx) > 0) { |
802 | 0 | ASN1_BIT_STRING *encKey = enc->encSymmKey; |
803 | 0 | size_t failure; |
804 | 0 | int retval; |
805 | |
|
806 | 0 | if (EVP_PKEY_decrypt(pkctx, NULL, &eksize, |
807 | 0 | encKey->data, encKey->length) |
808 | 0 | <= 0 |
809 | 0 | || (ek = OPENSSL_malloc(eksize)) == NULL) |
810 | 0 | goto end; |
811 | 0 | retval = EVP_PKEY_decrypt(pkctx, ek, &eksize, encKey->data, encKey->length); |
812 | 0 | failure = ~constant_time_is_zero_s(constant_time_msb(retval) |
813 | 0 | | constant_time_is_zero(retval)); |
814 | 0 | failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize); |
815 | 0 | if (failure) { |
816 | 0 | ERR_clear_error(); /* error state may have sensitive information */ |
817 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY); |
818 | 0 | goto end; |
819 | 0 | } |
820 | 0 | } else { |
821 | 0 | goto end; |
822 | 0 | } |
823 | 0 | if ((iv = OPENSSL_malloc(EVP_CIPHER_get_iv_length(cipher))) == NULL) |
824 | 0 | goto end; |
825 | 0 | if (ASN1_TYPE_get_octetstring(enc->symmAlg->parameter, iv, |
826 | 0 | EVP_CIPHER_get_iv_length(cipher)) |
827 | 0 | != EVP_CIPHER_get_iv_length(cipher)) { |
828 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_MALFORMED_IV); |
829 | 0 | goto end; |
830 | 0 | } |
831 | | |
832 | 0 | if ((out = OPENSSL_malloc(enc->encValue->length + EVP_CIPHER_get_block_size(cipher))) == NULL |
833 | 0 | || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL) |
834 | 0 | goto end; |
835 | 0 | EVP_CIPHER_CTX_set_padding(evp_ctx, 0); |
836 | |
|
837 | 0 | if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv) |
838 | 0 | || !EVP_DecryptUpdate(evp_ctx, out, outlen, |
839 | 0 | enc->encValue->data, |
840 | 0 | enc->encValue->length) |
841 | 0 | || !EVP_DecryptFinal(evp_ctx, out + *outlen, &n)) { |
842 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDVALUE); |
843 | 0 | goto end; |
844 | 0 | } |
845 | 0 | *outlen += n; |
846 | 0 | ret = 1; |
847 | |
|
848 | 0 | end: |
849 | 0 | EVP_PKEY_CTX_free(pkctx); |
850 | 0 | EVP_CIPHER_CTX_free(evp_ctx); |
851 | 0 | EVP_CIPHER_free(cipher); |
852 | 0 | OPENSSL_clear_free(ek, eksize); |
853 | 0 | OPENSSL_free(iv); |
854 | 0 | if (ret) |
855 | 0 | return out; |
856 | 0 | OPENSSL_free(out); |
857 | 0 | return NULL; |
858 | 0 | } |
859 | | |
860 | | /* |
861 | | * Decrypts the certificate in the given encryptedValue using private key pkey. |
862 | | * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2. |
863 | | * |
864 | | * returns a pointer to the decrypted certificate |
865 | | * returns NULL on error or if no certificate available |
866 | | */ |
867 | | X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert, |
868 | | OSSL_LIB_CTX *libctx, const char *propq, |
869 | | EVP_PKEY *pkey) |
870 | 0 | { |
871 | 0 | unsigned char *buf = NULL; |
872 | 0 | const unsigned char *p; |
873 | 0 | int len; |
874 | 0 | X509 *cert = NULL; |
875 | |
|
876 | 0 | buf = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(ecert, libctx, propq, pkey, &len); |
877 | 0 | if ((p = buf) == NULL || (cert = X509_new_ex(libctx, propq)) == NULL) |
878 | 0 | goto end; |
879 | | |
880 | 0 | if (d2i_X509(&cert, &p, len) == NULL) { |
881 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE); |
882 | 0 | X509_free(cert); |
883 | 0 | cert = NULL; |
884 | 0 | } |
885 | |
|
886 | 0 | end: |
887 | 0 | OPENSSL_free(buf); |
888 | 0 | return cert; |
889 | 0 | } |
890 | | /*- |
891 | | * Decrypts the certificate in the given encryptedKey using private key pkey. |
892 | | * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2. |
893 | | * |
894 | | * returns a pointer to the decrypted certificate |
895 | | * returns NULL on error or if no certificate available |
896 | | */ |
897 | | X509 *OSSL_CRMF_ENCRYPTEDKEY_get1_encCert(const OSSL_CRMF_ENCRYPTEDKEY *ecert, |
898 | | OSSL_LIB_CTX *libctx, const char *propq, |
899 | | EVP_PKEY *pkey, unsigned int flags) |
900 | 0 | { |
901 | 0 | #ifndef OPENSSL_NO_CMS |
902 | 0 | BIO *bio; |
903 | 0 | X509 *cert = NULL; |
904 | 0 | #endif |
905 | |
|
906 | 0 | if (ecert->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA) |
907 | 0 | return OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(ecert->value.encryptedValue, |
908 | 0 | libctx, propq, pkey); |
909 | 0 | #ifndef OPENSSL_NO_CMS |
910 | 0 | bio = CMS_EnvelopedData_decrypt(ecert->value.envelopedData, NULL, |
911 | 0 | pkey, NULL /* cert */, NULL, flags, |
912 | 0 | libctx, propq); |
913 | 0 | if (bio == NULL) |
914 | 0 | return NULL; |
915 | 0 | cert = d2i_X509_bio(bio, NULL); |
916 | 0 | if (cert == NULL) |
917 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE); |
918 | 0 | BIO_free(bio); |
919 | 0 | return cert; |
920 | | #else |
921 | | (void)flags; /* prevent warning on unused parameter */ |
922 | | ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED); |
923 | | return NULL; |
924 | | #endif /* OPENSSL_NO_CMS */ |
925 | 0 | } |
926 | | |
927 | | #ifndef OPENSSL_NO_CMS |
928 | | OSSL_CRMF_ENCRYPTEDKEY *OSSL_CRMF_ENCRYPTEDKEY_init_envdata(CMS_EnvelopedData *envdata) |
929 | 0 | { |
930 | 0 | OSSL_CRMF_ENCRYPTEDKEY *ek = OSSL_CRMF_ENCRYPTEDKEY_new(); |
931 | |
|
932 | 0 | if (ek == NULL) |
933 | 0 | return NULL; |
934 | 0 | ek->type = OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA; |
935 | 0 | ek->value.envelopedData = envdata; |
936 | 0 | return ek; |
937 | 0 | } |
938 | | #endif |