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