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