]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/crmf/crmf_lib.c
fix various formatting nits in CMP contribution chunks 1-6 found by the new util...
[thirdparty/openssl.git] / crypto / crmf / crmf_lib.c
1 /*-
2 * Copyright 2007-2019 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
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 \
50 if (msg == NULL || in == NULL) \
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) {
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)
85 goto err;
86 new = 1;
87 }
88 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
89 goto err;
90
91 return 1;
92 err:
93 if (new != 0) {
94 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
95 crm->certReq->controls = NULL;
96 }
97 return 0;
98 }
99
100 /* id-regCtrl-regToken Control (section 6.1) */
101 IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
102
103 /* id-regCtrl-authenticator Control (section 6.2) */
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) {
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
125 int
126 OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
127 OSSL_CRMF_SINGLEPUBINFO *spi)
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)
137 return 0;
138
139 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
140 }
141
142 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
143 int action)
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
156 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
157 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
158 regCtrl)
159
160 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
161 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
162
163 OSSL_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)
174 goto err;
175
176 if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
177 goto err;
178 cid->issuer->type = GEN_DIRNAME;
179
180 ASN1_INTEGER_free(cid->serialNumber);
181 if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
182 goto err;
183
184 return cid;
185
186 err:
187 OSSL_CRMF_CERTID_free(cid);
188 return NULL;
189 }
190
191 /*
192 * id-regCtrl-protocolEncrKey Control (section 6.6)
193 */
194 IMPLEMENT_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 */
201 static 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)
214 goto err;
215 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
216 goto err;
217 return 1;
218
219 err:
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) */
227 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
228
229 /* id-regInfo-certReq to regInfo (section 7.2) */
230 IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
231
232
233 /* retrieves the certificate template of crm */
234 OSSL_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
244 int 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))
257 goto err;
258 if (to != 0 && ((to_asn = ASN1_TIME_set(NULL, to)) == NULL))
259 goto err;
260 if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
261 goto err;
262
263 vld->notBefore = from_asn;
264 vld->notAfter = to_asn;
265
266 tmpl->validity = vld;
267
268 return 1;
269 err:
270 ASN1_TIME_free(from_asn);
271 ASN1_TIME_free(to_asn);
272 return 0;
273 }
274
275
276 int 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 */
287 static int crmf_asn1_get_int(const ASN1_INTEGER *a)
288 {
289 int64_t res;
290
291 if (!ASN1_INTEGER_get_int64(&res, a)) {
292 CRMFerr(0, ASN1_R_INVALID_NUMBER);
293 return -1;
294 }
295 if (res < INT_MIN) {
296 CRMFerr(0, ASN1_R_TOO_SMALL);
297 return -1;
298 }
299 if (res > INT_MAX) {
300 CRMFerr(0, ASN1_R_TOO_LARGE);
301 return -1;
302 }
303 return (int)res;
304 }
305
306 int OSSL_CRMF_MSG_get_certReqId(OSSL_CRMF_MSG *crm)
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 }
312 return crmf_asn1_get_int(crm->certReq->certReqId);
313 }
314
315
316 int 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
337 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
338 X509_EXTENSION *ext)
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)
350 goto err;
351 new = 1;
352 }
353
354 if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
355 goto err;
356 return 1;
357 err:
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. */
366 static int CRMF_poposigningkey_init(OSSL_CRMF_POPOSIGNINGKEY *ps,
367 OSSL_CRMF_CERTREQUEST *cr,
368 EVP_PKEY *pkey, int dgst)
369 {
370 int len;
371 size_t crlen;
372 size_t siglen;
373 unsigned char *crder = NULL, *sig = NULL;
374 int alg_nid = 0;
375 int md_nid = 0;
376 const EVP_MD *alg = NULL;
377 EVP_MD_CTX *ctx = NULL;
378 int ret = 0;
379
380 if (ps == NULL || cr == NULL || pkey == NULL) {
381 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_NULL_ARGUMENT);
382 return 0;
383 }
384
385 /* OpenSSL defaults all bit strings to be encoded as ASN.1 NamedBitList */
386 ps->signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
387 ps->signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
388
389 len = i2d_OSSL_CRMF_CERTREQUEST(cr, &crder);
390 if (len < 0 || crder == NULL) {
391 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR);
392 goto err;
393 }
394 crlen = (size_t)len;
395
396 if (!OBJ_find_sigid_by_algs(&alg_nid, dgst, EVP_PKEY_id(pkey))) {
397 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT,
398 CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY);
399 goto err;
400 }
401 if (!OBJ_find_sigid_algs(alg_nid, &md_nid, NULL)
402 || (alg = EVP_get_digestbynid(md_nid)) == NULL) {
403 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT,
404 CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY);
405 goto err;
406 }
407 if (!X509_ALGOR_set0(ps->algorithmIdentifier, OBJ_nid2obj(alg_nid),
408 V_ASN1_NULL, NULL)
409 || (ctx = EVP_MD_CTX_new()) == NULL
410 || EVP_DigestSignInit(ctx, NULL, alg, NULL, pkey) <= 0
411 || EVP_DigestSignUpdate(ctx, crder, crlen) <= 0
412 || EVP_DigestSignFinal(ctx, NULL, &siglen) <= 0) {
413 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR);
414 goto err;
415 }
416 if ((sig = OPENSSL_malloc(siglen)) == NULL)
417 goto err;
418 if (EVP_DigestSignFinal(ctx, sig, &siglen) <= 0
419 || !ASN1_BIT_STRING_set(ps->signature, sig, siglen)) {
420 CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR);
421 goto err;
422 }
423 ret = 1;
424
425 err:
426 OPENSSL_free(crder);
427 EVP_MD_CTX_free(ctx);
428 OPENSSL_free(sig);
429 return ret;
430 }
431
432
433 int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, EVP_PKEY *pkey,
434 int dgst, int ppmtd)
435 {
436 OSSL_CRMF_POPO *pp = NULL;
437 ASN1_INTEGER *tag = NULL;
438
439 if (crm == NULL || (ppmtd == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
440 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, CRMF_R_NULL_ARGUMENT);
441 return 0;
442 }
443
444 if (ppmtd == OSSL_CRMF_POPO_NONE)
445 goto end;
446 if ((pp = OSSL_CRMF_POPO_new()) == NULL)
447 goto err;
448 pp->type = ppmtd;
449
450 switch (ppmtd) {
451 case OSSL_CRMF_POPO_RAVERIFIED:
452 if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
453 goto err;
454 break;
455
456 case OSSL_CRMF_POPO_SIGNATURE:
457 {
458 OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
459 if (ps == NULL
460 || !CRMF_poposigningkey_init(ps, crm->certReq, pkey, dgst)) {
461 OSSL_CRMF_POPOSIGNINGKEY_free(ps);
462 goto err;
463 }
464 pp->value.signature = ps;
465 }
466 break;
467
468 case OSSL_CRMF_POPO_KEYENC:
469 if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
470 goto err;
471 tag = ASN1_INTEGER_new();
472 pp->value.keyEncipherment->type =
473 OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
474 pp->value.keyEncipherment->value.subsequentMessage = tag;
475 if (tag == NULL
476 || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
477 goto err;
478 break;
479
480 default:
481 CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO,
482 CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
483 goto err;
484 }
485
486 end:
487 OSSL_CRMF_POPO_free(crm->popo);
488 crm->popo = pp;
489
490 return 1;
491 err:
492 OSSL_CRMF_POPO_free(pp);
493 return 0;
494 }
495
496 /* returns 0 for equal, -1 for a < b or error on a, 1 for a > b or error on b */
497 static int X509_PUBKEY_cmp(X509_PUBKEY *a, X509_PUBKEY *b)
498 {
499 X509_ALGOR *algA = NULL, *algB = NULL;
500 int res = 0;
501
502 if (a == b)
503 return 0;
504 if (a == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a)
505 || algA == NULL)
506 return -1;
507 if (b == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b)
508 || algB == NULL)
509 return 1;
510 if ((res = X509_ALGOR_cmp(algA, algB)) != 0)
511 return res;
512 return EVP_PKEY_cmp(X509_PUBKEY_get0(a), X509_PUBKEY_get0(b));
513 }
514
515 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
516 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
517 int rid, int acceptRAVerified)
518 {
519 OSSL_CRMF_MSG *req = NULL;
520 X509_PUBKEY *pubkey = NULL;
521 OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
522
523 if (reqs == NULL
524 || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL
525 || req->popo == NULL) {
526 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
527 CRMF_R_NULL_ARGUMENT);
528 return 0;
529 }
530
531 switch (req->popo->type) {
532 case OSSL_CRMF_POPO_RAVERIFIED:
533 if (acceptRAVerified)
534 return 1;
535 break;
536 case OSSL_CRMF_POPO_SIGNATURE:
537 pubkey = req->certReq->certTemplate->publicKey;
538 sig = req->popo->value.signature;
539 if (sig->poposkInput != NULL) {
540 /*
541 * According to RFC 4211: publicKey contains a copy of
542 * the public key from the certificate template. This MUST be
543 * exactly the same value as contained in the certificate template.
544 */
545 const ASN1_ITEM *rptr = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
546
547 if (pubkey == NULL
548 || sig->poposkInput->publicKey == NULL
549 || X509_PUBKEY_cmp(pubkey, sig->poposkInput->publicKey)
550 || ASN1_item_verify(rptr, sig->algorithmIdentifier,
551 sig->signature, sig->poposkInput,
552 X509_PUBKEY_get0(pubkey)) < 1)
553 break;
554 } else {
555 if (pubkey == NULL
556 || req->certReq->certTemplate->subject == NULL
557 || ASN1_item_verify(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
558 sig->algorithmIdentifier,
559 sig->signature,
560 req->certReq,
561 X509_PUBKEY_get0(pubkey)) < 1)
562 break;
563 }
564 return 1;
565 case OSSL_CRMF_POPO_KEYENC:
566 /*
567 * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
568 * return 1 if the type of req->popo->value.keyEncipherment
569 * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
570 * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
571 */
572 case OSSL_CRMF_POPO_KEYAGREE:
573 default:
574 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
575 CRMF_R_UNSUPPORTED_POPO_METHOD);
576 return 0;
577 }
578 CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
579 CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED);
580 return 0;
581 }
582
583 /* retrieves the serialNumber of the given cert template or NULL on error */
584 ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(OSSL_CRMF_CERTTEMPLATE *tmpl)
585 {
586 return tmpl != NULL ? tmpl->serialNumber : NULL;
587 }
588
589 /* retrieves the issuer name of the given cert template or NULL on error */
590 X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(OSSL_CRMF_CERTTEMPLATE *tmpl)
591 {
592 return tmpl != NULL ? tmpl->issuer : NULL;
593 }
594
595 /* retrieves the issuer name of the given CertId or NULL on error */
596 X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
597 {
598 return cid != NULL && cid->issuer->type == GEN_DIRNAME ?
599 cid->issuer->d.directoryName : NULL;
600 }
601
602 /* retrieves the serialNumber of the given CertId or NULL on error */
603 ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID *cid)
604 {
605 return cid != NULL ? cid->serialNumber : NULL;
606 }
607
608 /*-
609 * fill in certificate template.
610 * Any value argument that is NULL will leave the respective field unchanged.
611 */
612 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
613 EVP_PKEY *pubkey,
614 const X509_NAME *subject,
615 const X509_NAME *issuer,
616 const ASN1_INTEGER *serial)
617 {
618 if (tmpl == NULL) {
619 CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, CRMF_R_NULL_ARGUMENT);
620 return 0;
621 }
622 if (subject != NULL && !X509_NAME_set(&tmpl->subject, subject))
623 return 0;
624 if (issuer != NULL && !X509_NAME_set(&tmpl->issuer, issuer))
625 return 0;
626 if (serial != NULL) {
627 ASN1_INTEGER_free(tmpl->serialNumber);
628 if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
629 return 0;
630 }
631 if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
632 return 0;
633 return 1;
634 }
635
636
637 /*-
638 * Decrypts the certificate in the given encryptedValue using private key pkey.
639 * This is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2.
640 *
641 * returns a pointer to the decrypted certificate
642 * returns NULL on error or if no certificate available
643 */
644 X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(OSSL_CRMF_ENCRYPTEDVALUE *ecert,
645 EVP_PKEY *pkey)
646 {
647 X509 *cert = NULL; /* decrypted certificate */
648 EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
649 unsigned char *ek = NULL; /* decrypted symmetric encryption key */
650 size_t eksize = 0; /* size of decrypted symmetric encryption key */
651 const EVP_CIPHER *cipher = NULL; /* used cipher */
652 int cikeysize = 0; /* key size from cipher */
653 unsigned char *iv = NULL; /* initial vector for symmetric encryption */
654 unsigned char *outbuf = NULL; /* decryption output buffer */
655 const unsigned char *p = NULL; /* needed for decoding ASN1 */
656 int symmAlg = 0; /* NIDs for symmetric algorithm */
657 int n, outlen = 0;
658 EVP_PKEY_CTX *pkctx = NULL; /* private key context */
659
660 if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL
661 || ecert->encValue == NULL || pkey == NULL) {
662 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
663 CRMF_R_NULL_ARGUMENT);
664 return NULL;
665 }
666 if ((symmAlg = OBJ_obj2nid(ecert->symmAlg->algorithm)) == 0) {
667 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
668 CRMF_R_UNSUPPORTED_CIPHER);
669 return NULL;
670 }
671 /* select symmetric cipher based on algorithm given in message */
672 if ((cipher = EVP_get_cipherbynid(symmAlg)) == NULL) {
673 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
674 CRMF_R_UNSUPPORTED_CIPHER);
675 goto end;
676 }
677 cikeysize = EVP_CIPHER_key_length(cipher);
678 /* first the symmetric key needs to be decrypted */
679 pkctx = EVP_PKEY_CTX_new(pkey, NULL);
680 if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) {
681 ASN1_BIT_STRING *encKey = ecert->encSymmKey;
682 size_t failure;
683 int retval;
684
685 if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
686 encKey->data, encKey->length) <= 0
687 || (ek = OPENSSL_malloc(eksize)) == NULL)
688 goto end;
689 retval = EVP_PKEY_decrypt(pkctx, ek, &eksize,
690 encKey->data, encKey->length);
691 ERR_clear_error(); /* error state may have sensitive information */
692 failure = ~constant_time_is_zero_s(constant_time_msb(retval)
693 | constant_time_is_zero(retval));
694 failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
695 if (failure) {
696 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
697 CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
698 goto end;
699 }
700 } else {
701 goto end;
702 }
703 if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL)
704 goto end;
705 if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv,
706 EVP_CIPHER_iv_length(cipher))
707 != EVP_CIPHER_iv_length(cipher)) {
708 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
709 CRMF_R_MALFORMED_IV);
710 goto end;
711 }
712
713 /*
714 * d2i_X509 changes the given pointer, so use p for decoding the message and
715 * keep the original pointer in outbuf so the memory can be freed later
716 */
717 if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length +
718 EVP_CIPHER_block_size(cipher))) == NULL
719 || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
720 goto end;
721 EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
722
723 if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
724 || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen,
725 ecert->encValue->data,
726 ecert->encValue->length)
727 || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) {
728 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
729 CRMF_R_ERROR_DECRYPTING_CERTIFICATE);
730 goto end;
731 }
732 outlen += n;
733
734 /* convert decrypted certificate from DER to internal ASN.1 structure */
735 if ((cert = d2i_X509(NULL, &p, outlen)) == NULL) {
736 CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
737 CRMF_R_ERROR_DECODING_CERTIFICATE);
738 }
739 end:
740 EVP_PKEY_CTX_free(pkctx);
741 OPENSSL_free(outbuf);
742 EVP_CIPHER_CTX_free(evp_ctx);
743 OPENSSL_clear_free(ek, eksize);
744 OPENSSL_free(iv);
745 return cert;
746 }