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