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