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