/src/openssl31/crypto/crmf/crmf_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*- |
2 | | * Copyright 2007-2022 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 | | |
35 | | /* explicit #includes not strictly needed since implied by the above: */ |
36 | | #include <openssl/crmf.h> |
37 | | #include <openssl/err.h> |
38 | | #include <openssl/evp.h> |
39 | | |
40 | | /*- |
41 | | * atyp = Attribute Type |
42 | | * valt = Value Type |
43 | | * ctrlinf = "regCtrl" or "regInfo" |
44 | | */ |
45 | | #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \ |
46 | 0 | valt *OSSL_CRMF_MSG_get0_##ctrlinf##_##atyp(const OSSL_CRMF_MSG *msg) \ |
47 | 0 | { \ |
48 | 0 | int i; \ |
49 | 0 | STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *controls; \ |
50 | 0 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \ |
51 | 0 | \ |
52 | 0 | if (msg == NULL || msg->certReq == NULL) \ |
53 | 0 | return NULL; \ |
54 | 0 | controls = msg->certReq->controls; \ |
55 | 0 | for (i = 0; i < sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_num(controls); i++) { \ |
56 | 0 | atav = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_value(controls, i); \ |
57 | 0 | if (OBJ_obj2nid(atav->type) == NID_id_##ctrlinf##_##atyp) \ |
58 | 0 | return atav->value.atyp; \ |
59 | 0 | } \ |
60 | 0 | return NULL; \ |
61 | 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 |
62 | | \ |
63 | 56 | int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, const valt *in) \ |
64 | 56 | { \ |
65 | 56 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \ |
66 | 56 | \ |
67 | 56 | if (msg == NULL || in == NULL) \ |
68 | 56 | goto err; \ |
69 | 56 | if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \ |
70 | 56 | goto err; \ |
71 | 56 | if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \ |
72 | 56 | goto err; \ |
73 | 56 | if ((atav->value.atyp = valt##_dup(in)) == NULL) \ |
74 | 56 | goto err; \ |
75 | 56 | if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \ |
76 | 56 | goto err; \ |
77 | 56 | return 1; \ |
78 | 56 | err: \ |
79 | 0 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \ |
80 | 0 | return 0; \ |
81 | 56 | } 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 | 63 | 56 | int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, const valt *in) \ | 64 | 56 | { \ | 65 | 56 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \ | 66 | 56 | \ | 67 | 56 | if (msg == NULL || in == NULL) \ | 68 | 56 | goto err; \ | 69 | 56 | if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \ | 70 | 56 | goto err; \ | 71 | 56 | if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \ | 72 | 56 | goto err; \ | 73 | 56 | if ((atav->value.atyp = valt##_dup(in)) == NULL) \ | 74 | 56 | goto err; \ | 75 | 56 | if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \ | 76 | 56 | goto err; \ | 77 | 56 | return 1; \ | 78 | 56 | err: \ | 79 | 0 | OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \ | 80 | 0 | return 0; \ | 81 | 56 | } |
Unexecuted instantiation: OSSL_CRMF_MSG_set1_regCtrl_protocolEncrKey Unexecuted instantiation: OSSL_CRMF_MSG_set1_regInfo_utf8Pairs Unexecuted instantiation: OSSL_CRMF_MSG_set1_regInfo_certReq |
82 | | |
83 | | |
84 | | /*- |
85 | | * Pushes the given control attribute into the controls stack of a CertRequest |
86 | | * (section 6) |
87 | | * returns 1 on success, 0 on error |
88 | | */ |
89 | | static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm, |
90 | | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl) |
91 | 56 | { |
92 | 56 | int new = 0; |
93 | | |
94 | 56 | if (crm == NULL || crm->certReq == NULL || ctrl == NULL) { |
95 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 56 | if (crm->certReq->controls == NULL) { |
100 | 56 | crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null(); |
101 | 56 | if (crm->certReq->controls == NULL) |
102 | 0 | goto err; |
103 | 56 | new = 1; |
104 | 56 | } |
105 | 56 | if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl)) |
106 | 0 | goto err; |
107 | | |
108 | 56 | return 1; |
109 | 0 | err: |
110 | 0 | if (new != 0) { |
111 | 0 | sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls); |
112 | 0 | crm->certReq->controls = NULL; |
113 | 0 | } |
114 | 0 | return 0; |
115 | 56 | } |
116 | | |
117 | | /* id-regCtrl-regToken Control (section 6.1) */ |
118 | | IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl) |
119 | | |
120 | | /* id-regCtrl-authenticator Control (section 6.2) */ |
121 | 0 | #define ASN1_UTF8STRING_dup ASN1_STRING_dup |
122 | | IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl) |
123 | | |
124 | | int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi, |
125 | | int method, GENERAL_NAME *nm) |
126 | 0 | { |
127 | 0 | if (spi == NULL |
128 | 0 | || method < OSSL_CRMF_PUB_METHOD_DONTCARE |
129 | 0 | || method > OSSL_CRMF_PUB_METHOD_LDAP) { |
130 | 0 | ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT); |
131 | 0 | return 0; |
132 | 0 | } |
133 | | |
134 | 0 | if (!ASN1_INTEGER_set(spi->pubMethod, method)) |
135 | 0 | return 0; |
136 | 0 | GENERAL_NAME_free(spi->pubLocation); |
137 | 0 | spi->pubLocation = nm; |
138 | 0 | return 1; |
139 | 0 | } |
140 | | |
141 | | int |
142 | | OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi, |
143 | | OSSL_CRMF_SINGLEPUBINFO *spi) |
144 | 0 | { |
145 | 0 | if (pi == NULL || spi == NULL) { |
146 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | if (pi->pubInfos == NULL) |
150 | 0 | pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null(); |
151 | 0 | if (pi->pubInfos == NULL) |
152 | 0 | return 0; |
153 | | |
154 | 0 | return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi); |
155 | 0 | } |
156 | | |
157 | | int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi, |
158 | | int action) |
159 | 0 | { |
160 | 0 | if (pi == NULL |
161 | 0 | || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH |
162 | 0 | || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) { |
163 | 0 | ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT); |
164 | 0 | return 0; |
165 | 0 | } |
166 | | |
167 | 0 | return ASN1_INTEGER_set(pi->action, action); |
168 | 0 | } |
169 | | |
170 | | /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */ |
171 | | IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO, |
172 | | regCtrl) |
173 | | |
174 | | /* id-regCtrl-oldCertID Control (section 6.5) from the given */ |
175 | | IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl) |
176 | | |
177 | | OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer, |
178 | | const ASN1_INTEGER *serial) |
179 | 62 | { |
180 | 62 | OSSL_CRMF_CERTID *cid = NULL; |
181 | | |
182 | 62 | if (issuer == NULL || serial == NULL) { |
183 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
184 | 0 | return NULL; |
185 | 0 | } |
186 | | |
187 | 62 | if ((cid = OSSL_CRMF_CERTID_new()) == NULL) |
188 | 0 | goto err; |
189 | | |
190 | 62 | if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer)) |
191 | 0 | goto err; |
192 | 62 | cid->issuer->type = GEN_DIRNAME; |
193 | | |
194 | 62 | ASN1_INTEGER_free(cid->serialNumber); |
195 | 62 | if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL) |
196 | 0 | goto err; |
197 | | |
198 | 62 | return cid; |
199 | | |
200 | 0 | err: |
201 | 0 | OSSL_CRMF_CERTID_free(cid); |
202 | 0 | return NULL; |
203 | 62 | } |
204 | | |
205 | | /* |
206 | | * id-regCtrl-protocolEncrKey Control (section 6.6) |
207 | | */ |
208 | | IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl) |
209 | | |
210 | | /*- |
211 | | * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack. |
212 | | * (section 7) |
213 | | * returns 1 on success, 0 on error |
214 | | */ |
215 | | static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm, |
216 | | OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri) |
217 | 0 | { |
218 | 0 | STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL; |
219 | |
|
220 | 0 | if (crm == NULL || ri == NULL) { |
221 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | 0 | if (crm->regInfo == NULL) |
226 | 0 | crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null(); |
227 | 0 | if (crm->regInfo == NULL) |
228 | 0 | goto err; |
229 | 0 | if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri)) |
230 | 0 | goto err; |
231 | 0 | return 1; |
232 | | |
233 | 0 | err: |
234 | 0 | if (info != NULL) |
235 | 0 | crm->regInfo = NULL; |
236 | 0 | sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info); |
237 | 0 | return 0; |
238 | 0 | } |
239 | | |
240 | | /* id-regInfo-utf8Pairs to regInfo (section 7.1) */ |
241 | | IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo) |
242 | | |
243 | | /* id-regInfo-certReq to regInfo (section 7.2) */ |
244 | | IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo) |
245 | | |
246 | | |
247 | | /* retrieves the certificate template of crm */ |
248 | | OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm) |
249 | 2.12k | { |
250 | 2.12k | if (crm == NULL || crm->certReq == NULL) { |
251 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
252 | 0 | return NULL; |
253 | 0 | } |
254 | 2.12k | return crm->certReq->certTemplate; |
255 | 2.12k | } |
256 | | |
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 | | |
278 | | int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid) |
279 | 762 | { |
280 | 762 | if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) { |
281 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
282 | 0 | return 0; |
283 | 0 | } |
284 | | |
285 | 762 | return ASN1_INTEGER_set(crm->certReq->certReqId, rid); |
286 | 762 | } |
287 | | |
288 | | /* get ASN.1 encoded integer, return -1 on error */ |
289 | | static int crmf_asn1_get_int(const ASN1_INTEGER *a) |
290 | 1.40k | { |
291 | 1.40k | int64_t res; |
292 | | |
293 | 1.40k | if (!ASN1_INTEGER_get_int64(&res, a)) { |
294 | 7 | ERR_raise(ERR_LIB_CRMF, ASN1_R_INVALID_NUMBER); |
295 | 7 | return -1; |
296 | 7 | } |
297 | 1.40k | if (res < INT_MIN) { |
298 | 144 | ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_SMALL); |
299 | 144 | return -1; |
300 | 144 | } |
301 | 1.25k | if (res > INT_MAX) { |
302 | 20 | ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_LARGE); |
303 | 20 | return -1; |
304 | 20 | } |
305 | 1.23k | return (int)res; |
306 | 1.25k | } |
307 | | |
308 | | int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm) |
309 | 1.40k | { |
310 | 1.40k | if (crm == NULL || /* not really needed: */ crm->certReq == NULL) { |
311 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
312 | 0 | return -1; |
313 | 0 | } |
314 | 1.40k | return crmf_asn1_get_int(crm->certReq->certReqId); |
315 | 1.40k | } |
316 | | |
317 | | |
318 | | int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm, |
319 | | X509_EXTENSIONS *exts) |
320 | 762 | { |
321 | 762 | OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
322 | | |
323 | 762 | if (tmpl == NULL) { /* also crm == NULL implies this */ |
324 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
325 | 0 | return 0; |
326 | 0 | } |
327 | | |
328 | 762 | if (sk_X509_EXTENSION_num(exts) == 0) { |
329 | 0 | sk_X509_EXTENSION_free(exts); |
330 | 0 | exts = NULL; /* do not include empty extensions list */ |
331 | 0 | } |
332 | | |
333 | 762 | sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free); |
334 | 762 | tmpl->extensions = exts; |
335 | 762 | return 1; |
336 | 762 | } |
337 | | |
338 | | |
339 | | int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm, |
340 | | X509_EXTENSION *ext) |
341 | 0 | { |
342 | 0 | int new = 0; |
343 | 0 | OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
344 | |
|
345 | 0 | if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */ |
346 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
347 | 0 | return 0; |
348 | 0 | } |
349 | | |
350 | 0 | if (tmpl->extensions == NULL) { |
351 | 0 | if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL) |
352 | 0 | goto err; |
353 | 0 | new = 1; |
354 | 0 | } |
355 | | |
356 | 0 | if (!sk_X509_EXTENSION_push(tmpl->extensions, ext)) |
357 | 0 | goto err; |
358 | 0 | return 1; |
359 | 0 | err: |
360 | 0 | if (new != 0) { |
361 | 0 | sk_X509_EXTENSION_free(tmpl->extensions); |
362 | 0 | tmpl->extensions = NULL; |
363 | 0 | } |
364 | 0 | return 0; |
365 | 0 | } |
366 | | |
367 | | static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps, |
368 | | const OSSL_CRMF_CERTREQUEST *cr, |
369 | | EVP_PKEY *pkey, const EVP_MD *digest, |
370 | | OSSL_LIB_CTX *libctx, const char *propq) |
371 | 0 | { |
372 | 0 | char name[80] = ""; |
373 | |
|
374 | 0 | if (ps == NULL || cr == NULL || pkey == NULL) { |
375 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
376 | 0 | return 0; |
377 | 0 | } |
378 | 0 | if (ps->poposkInput != NULL) { |
379 | | /* We do not support cases 1+2 defined in RFC 4211, section 4.1 */ |
380 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPOSKINPUT_NOT_SUPPORTED); |
381 | 0 | return 0; |
382 | 0 | } |
383 | | |
384 | 0 | if (EVP_PKEY_get_default_digest_name(pkey, name, sizeof(name)) > 0 |
385 | 0 | && strcmp(name, "UNDEF") == 0) /* at least for Ed25519, Ed448 */ |
386 | 0 | digest = NULL; |
387 | |
|
388 | 0 | return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST), |
389 | 0 | ps->algorithmIdentifier, NULL, ps->signature, cr, |
390 | 0 | NULL, pkey, digest, libctx, propq); |
391 | 0 | } |
392 | | |
393 | | |
394 | | int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm, |
395 | | EVP_PKEY *pkey, const EVP_MD *digest, |
396 | | OSSL_LIB_CTX *libctx, const char *propq) |
397 | 762 | { |
398 | 762 | OSSL_CRMF_POPO *pp = NULL; |
399 | 762 | ASN1_INTEGER *tag = NULL; |
400 | | |
401 | 762 | if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) { |
402 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
403 | 0 | return 0; |
404 | 0 | } |
405 | | |
406 | 762 | if (meth == OSSL_CRMF_POPO_NONE) |
407 | 762 | goto end; |
408 | 0 | if ((pp = OSSL_CRMF_POPO_new()) == NULL) |
409 | 0 | goto err; |
410 | 0 | pp->type = meth; |
411 | |
|
412 | 0 | switch (meth) { |
413 | 0 | case OSSL_CRMF_POPO_RAVERIFIED: |
414 | 0 | if ((pp->value.raVerified = ASN1_NULL_new()) == NULL) |
415 | 0 | goto err; |
416 | 0 | break; |
417 | | |
418 | 0 | case OSSL_CRMF_POPO_SIGNATURE: |
419 | 0 | { |
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 | } |
431 | 0 | break; |
432 | | |
433 | 0 | case OSSL_CRMF_POPO_KEYENC: |
434 | 0 | if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL) |
435 | 0 | goto err; |
436 | 0 | tag = ASN1_INTEGER_new(); |
437 | 0 | pp->value.keyEncipherment->type = |
438 | 0 | OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE; |
439 | 0 | pp->value.keyEncipherment->value.subsequentMessage = tag; |
440 | 0 | if (tag == NULL |
441 | 0 | || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT)) |
442 | 0 | goto err; |
443 | 0 | break; |
444 | | |
445 | 0 | default: |
446 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO); |
447 | 0 | goto err; |
448 | 0 | } |
449 | | |
450 | 762 | end: |
451 | 762 | OSSL_CRMF_POPO_free(crm->popo); |
452 | 762 | crm->popo = pp; |
453 | | |
454 | 762 | return 1; |
455 | 0 | err: |
456 | 0 | OSSL_CRMF_POPO_free(pp); |
457 | 0 | return 0; |
458 | 0 | } |
459 | | |
460 | | /* verifies the Proof-of-Possession of the request with the given rid in reqs */ |
461 | | int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs, |
462 | | int rid, int acceptRAVerified, |
463 | | OSSL_LIB_CTX *libctx, const char *propq) |
464 | 924 | { |
465 | 924 | OSSL_CRMF_MSG *req = NULL; |
466 | 924 | X509_PUBKEY *pubkey = NULL; |
467 | 924 | OSSL_CRMF_POPOSIGNINGKEY *sig = NULL; |
468 | 924 | const ASN1_ITEM *it; |
469 | 924 | void *asn; |
470 | | |
471 | 924 | if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) { |
472 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
473 | 0 | return 0; |
474 | 0 | } |
475 | | |
476 | 924 | if (req->popo == NULL) { |
477 | 8 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING); |
478 | 8 | return 0; |
479 | 8 | } |
480 | | |
481 | 916 | switch (req->popo->type) { |
482 | 2 | case OSSL_CRMF_POPO_RAVERIFIED: |
483 | 2 | if (!acceptRAVerified) { |
484 | 2 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED); |
485 | 2 | return 0; |
486 | 2 | } |
487 | 0 | break; |
488 | 913 | case OSSL_CRMF_POPO_SIGNATURE: |
489 | 913 | pubkey = req->certReq->certTemplate->publicKey; |
490 | 913 | if (pubkey == NULL) { |
491 | 3 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY); |
492 | 3 | return 0; |
493 | 3 | } |
494 | 910 | sig = req->popo->value.signature; |
495 | 910 | if (sig->poposkInput != NULL) { |
496 | | /* |
497 | | * According to RFC 4211: publicKey contains a copy of |
498 | | * the public key from the certificate template. This MUST be |
499 | | * exactly the same value as contained in the certificate template. |
500 | | */ |
501 | 0 | if (sig->poposkInput->publicKey == NULL) { |
502 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY); |
503 | 0 | return 0; |
504 | 0 | } |
505 | 0 | if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) { |
506 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY); |
507 | 0 | return 0; |
508 | 0 | } |
509 | 0 | it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT); |
510 | 0 | asn = sig->poposkInput; |
511 | 910 | } else { |
512 | 910 | if (req->certReq->certTemplate->subject == NULL) { |
513 | 5 | ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_SUBJECT); |
514 | 5 | return 0; |
515 | 5 | } |
516 | 905 | it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST); |
517 | 905 | asn = req->certReq; |
518 | 905 | } |
519 | 905 | if (ASN1_item_verify_ex(it, sig->algorithmIdentifier, sig->signature, |
520 | 905 | asn, NULL, X509_PUBKEY_get0(pubkey), libctx, |
521 | 905 | propq) < 1) |
522 | 853 | return 0; |
523 | 52 | break; |
524 | 52 | case OSSL_CRMF_POPO_KEYENC: |
525 | 1 | case OSSL_CRMF_POPO_KEYAGREE: |
526 | 1 | default: |
527 | 1 | ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_POPO_METHOD); |
528 | 1 | return 0; |
529 | 916 | } |
530 | 52 | return 1; |
531 | 916 | } |
532 | | |
533 | | /* retrieves the serialNumber of the given cert template or NULL on error */ |
534 | | const ASN1_INTEGER |
535 | | *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
536 | 15 | { |
537 | 15 | return tmpl != NULL ? tmpl->serialNumber : NULL; |
538 | 15 | } |
539 | | |
540 | | const X509_NAME |
541 | | *OSSL_CRMF_CERTTEMPLATE_get0_subject(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
542 | 0 | { |
543 | 0 | return tmpl != NULL ? tmpl->subject : NULL; |
544 | 0 | } |
545 | | |
546 | | /* retrieves the issuer name of the given cert template or NULL on error */ |
547 | | const X509_NAME |
548 | | *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
549 | 15 | { |
550 | 15 | return tmpl != NULL ? tmpl->issuer : NULL; |
551 | 15 | } |
552 | | |
553 | | X509_EXTENSIONS |
554 | | *OSSL_CRMF_CERTTEMPLATE_get0_extensions(const OSSL_CRMF_CERTTEMPLATE *tmpl) |
555 | 0 | { |
556 | 0 | return tmpl != NULL ? tmpl->extensions : NULL; |
557 | 0 | } |
558 | | |
559 | | /* retrieves the issuer name of the given CertId or NULL on error */ |
560 | | const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid) |
561 | 0 | { |
562 | 0 | return cid != NULL && cid->issuer->type == GEN_DIRNAME ? |
563 | 0 | cid->issuer->d.directoryName : NULL; |
564 | 0 | } |
565 | | |
566 | | /* retrieves the serialNumber of the given CertId or NULL on error */ |
567 | | const ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID *cid) |
568 | 0 | { |
569 | 0 | return cid != NULL ? cid->serialNumber : NULL; |
570 | 0 | } |
571 | | |
572 | | /*- |
573 | | * fill in certificate template. |
574 | | * Any value argument that is NULL will leave the respective field unchanged. |
575 | | */ |
576 | | int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl, |
577 | | EVP_PKEY *pubkey, |
578 | | const X509_NAME *subject, |
579 | | const X509_NAME *issuer, |
580 | | const ASN1_INTEGER *serial) |
581 | 966 | { |
582 | 966 | if (tmpl == NULL) { |
583 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
584 | 0 | return 0; |
585 | 0 | } |
586 | 966 | if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject)) |
587 | 0 | return 0; |
588 | 966 | if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer)) |
589 | 0 | return 0; |
590 | 966 | if (serial != NULL) { |
591 | 204 | ASN1_INTEGER_free(tmpl->serialNumber); |
592 | 204 | if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL) |
593 | 0 | return 0; |
594 | 204 | } |
595 | 966 | if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey)) |
596 | 0 | return 0; |
597 | 966 | return 1; |
598 | 966 | } |
599 | | |
600 | | |
601 | | /*- |
602 | | * Decrypts the certificate in the given encryptedValue using private key pkey. |
603 | | * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2. |
604 | | * |
605 | | * returns a pointer to the decrypted certificate |
606 | | * returns NULL on error or if no certificate available |
607 | | */ |
608 | | X509 |
609 | | *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert, |
610 | | OSSL_LIB_CTX *libctx, const char *propq, |
611 | | EVP_PKEY *pkey) |
612 | 0 | { |
613 | 0 | X509 *cert = NULL; /* decrypted certificate */ |
614 | 0 | EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */ |
615 | 0 | unsigned char *ek = NULL; /* decrypted symmetric encryption key */ |
616 | 0 | size_t eksize = 0; /* size of decrypted symmetric encryption key */ |
617 | 0 | EVP_CIPHER *cipher = NULL; /* used cipher */ |
618 | 0 | int cikeysize = 0; /* key size from cipher */ |
619 | 0 | unsigned char *iv = NULL; /* initial vector for symmetric encryption */ |
620 | 0 | unsigned char *outbuf = NULL; /* decryption output buffer */ |
621 | 0 | const unsigned char *p = NULL; /* needed for decoding ASN1 */ |
622 | 0 | int n, outlen = 0; |
623 | 0 | EVP_PKEY_CTX *pkctx = NULL; /* private key context */ |
624 | 0 | char name[OSSL_MAX_NAME_SIZE]; |
625 | |
|
626 | 0 | if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL |
627 | 0 | || ecert->encValue == NULL || pkey == NULL) { |
628 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT); |
629 | 0 | return NULL; |
630 | 0 | } |
631 | | |
632 | | /* select symmetric cipher based on algorithm given in message */ |
633 | 0 | OBJ_obj2txt(name, sizeof(name), ecert->symmAlg->algorithm, 0); |
634 | |
|
635 | 0 | (void)ERR_set_mark(); |
636 | 0 | cipher = EVP_CIPHER_fetch(NULL, name, NULL); |
637 | |
|
638 | 0 | if (cipher == NULL) |
639 | 0 | cipher = (EVP_CIPHER *)EVP_get_cipherbyname(name); |
640 | |
|
641 | 0 | if (cipher == NULL) { |
642 | 0 | (void)ERR_clear_last_mark(); |
643 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_CIPHER); |
644 | 0 | goto end; |
645 | 0 | } |
646 | 0 | (void)ERR_pop_to_mark(); |
647 | |
|
648 | 0 | cikeysize = EVP_CIPHER_get_key_length(cipher); |
649 | | /* first the symmetric key needs to be decrypted */ |
650 | 0 | pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
651 | 0 | if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx) > 0) { |
652 | 0 | ASN1_BIT_STRING *encKey = ecert->encSymmKey; |
653 | 0 | size_t failure; |
654 | 0 | int retval; |
655 | |
|
656 | 0 | if (EVP_PKEY_decrypt(pkctx, NULL, &eksize, |
657 | 0 | encKey->data, encKey->length) <= 0 |
658 | 0 | || (ek = OPENSSL_malloc(eksize)) == NULL) |
659 | 0 | goto end; |
660 | 0 | retval = EVP_PKEY_decrypt(pkctx, ek, &eksize, |
661 | 0 | encKey->data, encKey->length); |
662 | 0 | ERR_clear_error(); /* error state may have sensitive information */ |
663 | 0 | failure = ~constant_time_is_zero_s(constant_time_msb(retval) |
664 | 0 | | constant_time_is_zero(retval)); |
665 | 0 | failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize); |
666 | 0 | if (failure) { |
667 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY); |
668 | 0 | goto end; |
669 | 0 | } |
670 | 0 | } else { |
671 | 0 | goto end; |
672 | 0 | } |
673 | 0 | if ((iv = OPENSSL_malloc(EVP_CIPHER_get_iv_length(cipher))) == NULL) |
674 | 0 | goto end; |
675 | 0 | if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv, |
676 | 0 | EVP_CIPHER_get_iv_length(cipher)) |
677 | 0 | != EVP_CIPHER_get_iv_length(cipher)) { |
678 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_MALFORMED_IV); |
679 | 0 | goto end; |
680 | 0 | } |
681 | | |
682 | | /* |
683 | | * d2i_X509 changes the given pointer, so use p for decoding the message and |
684 | | * keep the original pointer in outbuf so the memory can be freed later |
685 | | */ |
686 | 0 | if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length + |
687 | 0 | EVP_CIPHER_get_block_size(cipher))) == NULL |
688 | 0 | || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL) |
689 | 0 | goto end; |
690 | 0 | EVP_CIPHER_CTX_set_padding(evp_ctx, 0); |
691 | |
|
692 | 0 | if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv) |
693 | 0 | || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen, |
694 | 0 | ecert->encValue->data, |
695 | 0 | ecert->encValue->length) |
696 | 0 | || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) { |
697 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_CERTIFICATE); |
698 | 0 | goto end; |
699 | 0 | } |
700 | 0 | outlen += n; |
701 | | |
702 | | /* convert decrypted certificate from DER to internal ASN.1 structure */ |
703 | 0 | if ((cert = X509_new_ex(libctx, propq)) == NULL) |
704 | 0 | goto end; |
705 | 0 | if (d2i_X509(&cert, &p, outlen) == NULL) |
706 | 0 | ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE); |
707 | 0 | end: |
708 | 0 | EVP_PKEY_CTX_free(pkctx); |
709 | 0 | OPENSSL_free(outbuf); |
710 | 0 | EVP_CIPHER_CTX_free(evp_ctx); |
711 | 0 | EVP_CIPHER_free(cipher); |
712 | 0 | OPENSSL_clear_free(ek, eksize); |
713 | 0 | OPENSSL_free(iv); |
714 | 0 | return cert; |
715 | 0 | } |