Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/crmf/crmf_lib.c

raw
1 /*-
2 * Copyright 2007-2026 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 "crmf_local.h"
30 #include <openssl/asn1t.h>
31 #include "internal/constant_time.h"
32 #include "internal/sizes.h" /* for OSSL_MAX_NAME_SIZE */
33 #include "crypto/x509.h" /* for ossl_x509_check_private_key() */
34
35 /*-
36 * atyp = Attribute Type
37 * valt = Value Type
38 * ctrlinf = "regCtrl" or "regInfo"
39 */
40 #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf) \
41 valt *OSSL_CRMF_MSG_get0_##ctrlinf##_##atyp(const OSSL_CRMF_MSG *msg) \
42 { \
43 int i; \
44 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *controls; \
45 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
46 \
47 if (msg == NULL || msg->certReq == NULL) \
48 return NULL; \
49 controls = msg->certReq->controls; \
50 for (i = 0; i < sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_num(controls); i++) { \
51 atav = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_value(controls, i); \
52 if (OBJ_obj2nid(atav->type) == NID_id_##ctrlinf##_##atyp) \
53 return atav->value.atyp; \
54 } \
55 return NULL; \
56 } \
57 \
58 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg, const valt *in) \
59 { \
60 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL; \
61 \
62 if (msg == NULL || in == NULL) \
63 goto err; \
64 if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL) \
65 goto err; \
66 if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL) \
67 goto err; \
68 if ((atav->value.atyp = valt##_dup(in)) == NULL) \
69 goto err; \
70 if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav)) \
71 goto err; \
72 return 1; \
73 err: \
74 OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav); \
75 return 0; \
76 }
77
78 /*-
79 * Pushes the given control attribute into the controls stack of a CertRequest
80 * (section 6)
81 * returns 1 on success, 0 on error
82 */
83 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm,
84 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl)
85 {
86 int new = 0;
87
88 if (crm == NULL || crm->certReq == NULL || ctrl == NULL) {
89 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
90 return 0;
91 }
92
93 if (crm->certReq->controls == NULL) {
94 crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
95 if (crm->certReq->controls == NULL)
96 goto err;
97 new = 1;
98 }
99 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
100 goto err;
101
102 return 1;
103 err:
104 if (new != 0) {
105 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
106 crm->certReq->controls = NULL;
107 }
108 return 0;
109 }
110
111 /* id-regCtrl-regToken Control (section 6.1) */
112 IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
113
114 /* id-regCtrl-authenticator Control (section 6.2) */
115 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
116 IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl)
117
118 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi,
119 int method, GENERAL_NAME *nm)
120 {
121 if (spi == NULL
122 || method < OSSL_CRMF_PUB_METHOD_DONTCARE
123 || method > OSSL_CRMF_PUB_METHOD_LDAP) {
124 ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT);
125 return 0;
126 }
127
128 if (!ASN1_INTEGER_set(spi->pubMethod, method))
129 return 0;
130 GENERAL_NAME_free(spi->pubLocation);
131 spi->pubLocation = nm;
132 return 1;
133 }
134
135 int OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
136 OSSL_CRMF_SINGLEPUBINFO *spi)
137 {
138 if (pi == NULL || spi == NULL) {
139 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
140 return 0;
141 }
142 if (pi->pubInfos == NULL)
143 pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
144 if (pi->pubInfos == NULL)
145 return 0;
146
147 return sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi);
148 }
149
150 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(OSSL_CRMF_PKIPUBLICATIONINFO *pi,
151 int action)
152 {
153 if (pi == NULL
154 || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
155 || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
156 ERR_raise(ERR_LIB_CRMF, ERR_R_PASSED_INVALID_ARGUMENT);
157 return 0;
158 }
159
160 return ASN1_INTEGER_set(pi->action, action);
161 }
162
163 /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
164 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
165 regCtrl)
166
167 /* id-regCtrl-oldCertID Control (section 6.5) from the given */
168 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
169
170 OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
171 const ASN1_INTEGER *serial)
172 {
173 OSSL_CRMF_CERTID *cid = NULL;
174
175 if (issuer == NULL || serial == NULL) {
176 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
177 return NULL;
178 }
179
180 if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
181 goto err;
182
183 if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
184 goto err;
185 cid->issuer->type = GEN_DIRNAME;
186
187 ASN1_INTEGER_free(cid->serialNumber);
188 if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
189 goto err;
190
191 return cid;
192
193 err:
194 OSSL_CRMF_CERTID_free(cid);
195 return NULL;
196 }
197
198 /*
199 * id-regCtrl-protocolEncrKey Control (section 6.6)
200 */
201 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
202
203 /*-
204 * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
205 * (section 7)
206 * returns 1 on success, 0 on error
207 */
208 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
209 OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
210 {
211 STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
212
213 if (crm == NULL || ri == NULL) {
214 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
215 return 0;
216 }
217
218 if (crm->regInfo == NULL)
219 crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
220 if (crm->regInfo == NULL)
221 goto err;
222 if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
223 goto err;
224 return 1;
225
226 err:
227 if (info != NULL)
228 crm->regInfo = NULL;
229 sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
230 return 0;
231 }
232
233 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
234 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
235
236 /* id-regInfo-certReq to regInfo (section 7.2) */
237 IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
238
239 /* retrieves the certificate template of crm */
240 OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
241 {
242 if (crm == NULL || crm->certReq == NULL) {
243 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
244 return NULL;
245 }
246 return crm->certReq->certTemplate;
247 }
248
249 int OSSL_CRMF_MSG_set0_validity(OSSL_CRMF_MSG *crm,
250 ASN1_TIME *notBefore, ASN1_TIME *notAfter)
251 {
252 OSSL_CRMF_OPTIONALVALIDITY *vld;
253 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
254
255 if (tmpl == NULL) { /* also crm == NULL implies this */
256 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
257 return 0;
258 }
259
260 if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
261 return 0;
262 vld->notBefore = notBefore;
263 vld->notAfter = notAfter;
264 tmpl->validity = vld;
265 return 1;
266 }
267
268 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
269 {
270 if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
271 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
272 return 0;
273 }
274
275 return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
276 }
277
278 /* get ASN.1 encoded integer, return -1 on error */
279 static int crmf_asn1_get_int(const ASN1_INTEGER *a)
280 {
281 int64_t res;
282
283 if (!ASN1_INTEGER_get_int64(&res, a)) {
284 ERR_raise(ERR_LIB_CRMF, ASN1_R_INVALID_NUMBER);
285 return -1;
286 }
287 if (res < INT_MIN) {
288 ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_SMALL);
289 return -1;
290 }
291 if (res > INT_MAX) {
292 ERR_raise(ERR_LIB_CRMF, ASN1_R_TOO_LARGE);
293 return -1;
294 }
295 return (int)res;
296 }
297
298 int OSSL_CRMF_MSG_get_certReqId(const OSSL_CRMF_MSG *crm)
299 {
300 if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
301 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
302 return -1;
303 }
304 return crmf_asn1_get_int(crm->certReq->certReqId);
305 }
306
307 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
308 X509_EXTENSIONS *exts)
309 {
310 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
311
312 if (tmpl == NULL) { /* also crm == NULL implies this */
313 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
314 return 0;
315 }
316
317 if (sk_X509_EXTENSION_num(exts) == 0) {
318 sk_X509_EXTENSION_free(exts);
319 exts = NULL; /* do not include empty extensions list */
320 }
321
322 sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
323 tmpl->extensions = exts;
324 return 1;
325 }
326
327 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
328 X509_EXTENSION *ext)
329 {
330 int new = 0;
331 OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
332
333 if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
334 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
335 return 0;
336 }
337
338 if (tmpl->extensions == NULL) {
339 if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
340 goto err;
341 new = 1;
342 }
343
344 if (!sk_X509_EXTENSION_push(tmpl->extensions, ext))
345 goto err;
346 return 1;
347 err:
348 if (new != 0) {
349 sk_X509_EXTENSION_free(tmpl->extensions);
350 tmpl->extensions = NULL;
351 }
352 return 0;
353 }
354
355 static int create_popo_signature(OSSL_CRMF_POPOSIGNINGKEY *ps,
356 const OSSL_CRMF_CERTREQUEST *cr,
357 EVP_PKEY *pkey, const EVP_MD *digest,
358 OSSL_LIB_CTX *libctx, const char *propq)
359 {
360 char name[80] = "";
361 EVP_PKEY *pub;
362
363 if (ps == NULL || cr == NULL || pkey == NULL) {
364 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
365 return 0;
366 }
367 pub = X509_PUBKEY_get0(cr->certTemplate->publicKey);
368 if (!ossl_x509_check_private_key(pub, pkey))
369 return 0;
370
371 if (ps->poposkInput != NULL) {
372 /* We do not support cases 1+2 defined in RFC 4211, section 4.1 */
373 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPOSKINPUT_NOT_SUPPORTED);
374 return 0;
375 }
376
377 if (EVP_PKEY_get_default_digest_name(pkey, name, sizeof(name)) > 0
378 && strcmp(name, "UNDEF") == 0) /* at least for Ed25519, Ed448 */
379 digest = NULL;
380
381 return ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
382 ps->algorithmIdentifier, /* sets this X509_ALGOR */
383 NULL, ps->signature, /* sets the ASN1_BIT_STRING */
384 cr, NULL, pkey, digest, libctx, propq);
385 }
386
387 int OSSL_CRMF_MSG_create_popo(int meth, OSSL_CRMF_MSG *crm,
388 EVP_PKEY *pkey, const EVP_MD *digest,
389 OSSL_LIB_CTX *libctx, const char *propq)
390 {
391 OSSL_CRMF_POPO *pp = NULL;
392 ASN1_INTEGER *tag = NULL;
393
394 if (crm == NULL || (meth == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
395 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
396 return 0;
397 }
398
399 if (meth == OSSL_CRMF_POPO_NONE)
400 goto end;
401 if ((pp = OSSL_CRMF_POPO_new()) == NULL)
402 goto err;
403 pp->type = meth;
404
405 switch (meth) {
406 case OSSL_CRMF_POPO_RAVERIFIED:
407 if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
408 goto err;
409 break;
410
411 case OSSL_CRMF_POPO_SIGNATURE: {
412 OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
413
414 if (ps == NULL)
415 goto err;
416 if (!create_popo_signature(ps, crm->certReq, pkey, digest,
417 libctx, propq)) {
418 OSSL_CRMF_POPOSIGNINGKEY_free(ps);
419 goto err;
420 }
421 pp->value.signature = ps;
422 } break;
423
424 case OSSL_CRMF_POPO_KEYENC:
425 if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
426 goto err;
427 tag = ASN1_INTEGER_new();
428 pp->value.keyEncipherment->type = OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
429 pp->value.keyEncipherment->value.subsequentMessage = tag;
430 if (tag == NULL
431 || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
432 goto err;
433 break;
434
435 default:
436 ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
437 goto err;
438 }
439
440 end:
441 OSSL_CRMF_POPO_free(crm->popo);
442 crm->popo = pp;
443
444 return 1;
445 err:
446 OSSL_CRMF_POPO_free(pp);
447 return 0;
448 }
449
450 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
451 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
452 int rid, int acceptRAVerified,
453 OSSL_LIB_CTX *libctx, const char *propq)
454 {
455 OSSL_CRMF_MSG *req = NULL;
456 X509_PUBKEY *pubkey = NULL;
457 OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
458 const ASN1_ITEM *it;
459 void *asn;
460
461 if (reqs == NULL || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL) {
462 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
463 return 0;
464 }
465
466 if (req->popo == NULL) {
467 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING);
468 return 0;
469 }
470
471 switch (req->popo->type) {
472 case OSSL_CRMF_POPO_RAVERIFIED:
473 if (!acceptRAVerified) {
474 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_RAVERIFIED_NOT_ACCEPTED);
475 return 0;
476 }
477 break;
478 case OSSL_CRMF_POPO_SIGNATURE:
479 pubkey = req->certReq->certTemplate->publicKey;
480 if (pubkey == NULL) {
481 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
482 return 0;
483 }
484 sig = req->popo->value.signature;
485 if (sig->poposkInput != NULL) {
486 /*
487 * According to RFC 4211: publicKey contains a copy of
488 * the public key from the certificate template. This MUST be
489 * exactly the same value as contained in the certificate template.
490 */
491 if (sig->poposkInput->publicKey == NULL) {
492 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_PUBLIC_KEY);
493 return 0;
494 }
495 if (X509_PUBKEY_eq(pubkey, sig->poposkInput->publicKey) != 1) {
496 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_PUBLIC_KEY);
497 return 0;
498 }
499
500 /*
501 * Should check at this point the contents of the authInfo sub-field
502 * as requested in FR #19807 according to RFC 4211 section 4.1.
503 */
504
505 it = ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT);
506 asn = sig->poposkInput;
507 } else {
508 if (req->certReq->certTemplate->subject == NULL) {
509 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_MISSING_SUBJECT);
510 return 0;
511 }
512 it = ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST);
513 asn = req->certReq;
514 }
515 if (ASN1_item_verify_ex(it, sig->algorithmIdentifier, sig->signature,
516 asn, NULL, X509_PUBKEY_get0(pubkey), libctx,
517 propq)
518 < 1)
519 return 0;
520 break;
521 case OSSL_CRMF_POPO_KEYENC:
522 /*
523 * When OSSL_CMP_certrep_new() supports encrypted certs,
524 * should return 1 if the type of req->popo->value.keyEncipherment
525 * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
526 * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
527 */
528 case OSSL_CRMF_POPO_KEYAGREE:
529 default:
530 ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_POPO_METHOD);
531 return 0;
532 }
533 return 1;
534 }
535
536 int OSSL_CRMF_MSG_centralkeygen_requested(const OSSL_CRMF_MSG *crm, const X509_REQ *p10cr)
537 {
538 X509_PUBKEY *pubkey = NULL;
539 const unsigned char *pk = NULL;
540 int pklen, ret = 0;
541
542 if (crm == NULL && p10cr == NULL) {
543 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
544 return -1;
545 }
546
547 if (crm != NULL)
548 pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(OSSL_CRMF_MSG_get0_tmpl(crm));
549 else
550 pubkey = p10cr->req_info.pubkey;
551
552 if (pubkey == NULL
553 || (X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey)
554 && pklen == 0))
555 ret = 1;
556
557 /*
558 * In case of CRMF, POPO MUST be absent if central key generation
559 * is requested, otherwise MUST be present
560 */
561 if (crm != NULL && ret != (crm->popo == NULL)) {
562 ERR_raise(ERR_LIB_CRMF, CRMF_R_POPO_INCONSISTENT_CENTRAL_KEYGEN);
563 return -2;
564 }
565 return ret;
566 }
567
568 X509_PUBKEY
569 *OSSL_CRMF_CERTTEMPLATE_get0_publicKey(const OSSL_CRMF_CERTTEMPLATE *tmpl)
570 {
571 return tmpl != NULL ? tmpl->publicKey : NULL;
572 }
573
574 const ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(const OSSL_CRMF_CERTTEMPLATE *tmpl)
575 {
576 return tmpl != NULL ? tmpl->serialNumber : NULL;
577 }
578
579 const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_subject(const OSSL_CRMF_CERTTEMPLATE *tmpl)
580 {
581 return tmpl != NULL ? tmpl->subject : NULL;
582 }
583
584 const X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(const OSSL_CRMF_CERTTEMPLATE *tmpl)
585 {
586 return tmpl != NULL ? tmpl->issuer : NULL;
587 }
588
589 X509_EXTENSIONS
590 *OSSL_CRMF_CERTTEMPLATE_get0_extensions(const OSSL_CRMF_CERTTEMPLATE *tmpl)
591 {
592 return tmpl != NULL ? tmpl->extensions : NULL;
593 }
594
595 const X509_NAME *OSSL_CRMF_CERTID_get0_issuer(const OSSL_CRMF_CERTID *cid)
596 {
597 return cid != NULL && cid->issuer->type == GEN_DIRNAME ? cid->issuer->d.directoryName : NULL;
598 }
599
600 const ASN1_INTEGER *OSSL_CRMF_CERTID_get0_serialNumber(const OSSL_CRMF_CERTID
601 *cid)
602 {
603 return cid != NULL ? cid->serialNumber : NULL;
604 }
605
606 /*-
607 * Fill in the certificate template |tmpl|.
608 * Any other NULL argument will leave the respective field unchanged.
609 */
610 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
611 EVP_PKEY *pubkey,
612 const X509_NAME *subject,
613 const X509_NAME *issuer,
614 const ASN1_INTEGER *serial)
615 {
616 if (tmpl == NULL) {
617 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
618 return 0;
619 }
620 if (subject != NULL && !X509_NAME_set((X509_NAME **)&tmpl->subject, subject))
621 return 0;
622 if (issuer != NULL && !X509_NAME_set((X509_NAME **)&tmpl->issuer, issuer))
623 return 0;
624 if (serial != NULL) {
625 ASN1_INTEGER_free(tmpl->serialNumber);
626 if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
627 return 0;
628 }
629 if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
630 return 0;
631 return 1;
632 }
633
634 #ifndef OPENSSL_NO_CMS
635 DECLARE_ASN1_ITEM(CMS_SignedData) /* copied from cms_local.h */
636
637 /* check for KGA authorization implied by CA flag or by explicit EKU cmKGA */
638 static int check_cmKGA(ossl_unused const X509_PURPOSE *purpose, const X509 *x, int ca)
639 {
640 STACK_OF(ASN1_OBJECT) *ekus;
641 int i, ret = 1;
642
643 if (ca)
644 return ret;
645 ekus = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL);
646 for (i = 0; i < sk_ASN1_OBJECT_num(ekus); i++) {
647 if (OBJ_obj2nid(sk_ASN1_OBJECT_value(ekus, i)) == NID_cmKGA)
648 goto end;
649 }
650 ret = 0;
651
652 end:
653 sk_ASN1_OBJECT_pop_free(ekus, ASN1_OBJECT_free);
654 return ret;
655 }
656 #endif /* OPENSSL_NO_CMS */
657
658 EVP_PKEY *OSSL_CRMF_ENCRYPTEDKEY_get1_pkey(const OSSL_CRMF_ENCRYPTEDKEY *encryptedKey,
659 X509_STORE *ts, STACK_OF(X509) *extra, EVP_PKEY *pkey,
660 X509 *cert, ASN1_OCTET_STRING *secret,
661 OSSL_LIB_CTX *libctx, const char *propq)
662 {
663 #ifndef OPENSSL_NO_CMS
664 BIO *bio = NULL;
665 CMS_SignedData *sd = NULL;
666 BIO *pkey_bio = NULL;
667 int purpose_id, bak_purpose_id;
668 X509_VERIFY_PARAM *vpm;
669 #endif
670 EVP_PKEY *ret = NULL;
671
672 if (encryptedKey == NULL) {
673 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
674 return NULL;
675 }
676 if (encryptedKey->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA) {
677 unsigned char *p;
678 const unsigned char *p_copy;
679 int len;
680
681 p = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(encryptedKey->value.encryptedValue,
682 libctx, propq, pkey, &len);
683 if ((p_copy = p) != NULL)
684 ret = d2i_AutoPrivateKey_ex(NULL, &p_copy, len, libctx, propq);
685 OPENSSL_free(p);
686 return ret;
687 }
688
689 #ifndef OPENSSL_NO_CMS
690 if (ts == NULL) {
691 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
692 return NULL;
693 }
694 if ((bio = CMS_EnvelopedData_decrypt(encryptedKey->value.envelopedData,
695 NULL, pkey, cert, secret, 0,
696 libctx, propq))
697 == NULL) {
698 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDKEY);
699 goto end;
700 }
701 sd = ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_SignedData), bio, NULL);
702 if (sd == NULL)
703 goto end;
704
705 if ((purpose_id = X509_PURPOSE_get_by_sname(SN_cmKGA)) < 0) {
706 purpose_id = X509_PURPOSE_get_unused_id(libctx);
707 if (!X509_PURPOSE_add(purpose_id, X509_TRUST_COMPAT, 0, check_cmKGA,
708 LN_cmKGA, SN_cmKGA, NULL))
709 goto end;
710 }
711 if ((vpm = X509_STORE_get0_param(ts)) == NULL)
712 goto end;
713
714 /* temporarily override X509_PURPOSE_SMIME_SIGN: */
715 bak_purpose_id = X509_VERIFY_PARAM_get_purpose(vpm);
716 if (!X509_STORE_set_purpose(ts, purpose_id)) {
717 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE);
718 goto end;
719 }
720
721 pkey_bio = CMS_SignedData_verify(sd, NULL, NULL /* scerts */, ts,
722 extra, NULL, 0, libctx, propq);
723
724 if (!X509_STORE_set_purpose(ts, bak_purpose_id)) {
725 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_SETTING_PURPOSE);
726 goto end;
727 }
728
729 if (pkey_bio == NULL) {
730 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_VERIFYING_ENCRYPTEDKEY);
731 goto end;
732 }
733
734 /* unpack AsymmetricKeyPackage */
735 if ((ret = d2i_PrivateKey_ex_bio(pkey_bio, NULL, libctx, propq)) == NULL)
736 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_ENCRYPTEDKEY);
737
738 end:
739 CMS_SignedData_free(sd);
740 BIO_free(bio);
741 BIO_free(pkey_bio);
742 return ret;
743 #else
744 /* prevent warning on unused parameters: */
745 ((void)ts, (void)extra, (void)cert, (void)secret);
746 ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED);
747 return NULL;
748 #endif /* OPENSSL_NO_CMS */
749 }
750
751 unsigned char *OSSL_CRMF_ENCRYPTEDVALUE_decrypt(const OSSL_CRMF_ENCRYPTEDVALUE *enc,
752 OSSL_LIB_CTX *libctx, const char *propq,
753 EVP_PKEY *pkey, int *outlen)
754 {
755 EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
756 unsigned char *ek = NULL; /* decrypted symmetric encryption key */
757 size_t eksize = 0; /* size of decrypted symmetric encryption key */
758 EVP_CIPHER *cipher = NULL; /* used cipher */
759 int cikeysize = 0; /* key size from cipher */
760 unsigned char *iv = NULL; /* initial vector for symmetric encryption */
761 int iv_len; /* iv length */
762 unsigned char *out = NULL; /* decryption output buffer */
763 int n, ret = 0;
764 EVP_PKEY_CTX *pkctx = NULL; /* private key context */
765 char name[OSSL_MAX_NAME_SIZE];
766
767 if (outlen == NULL) {
768 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
769 return NULL;
770 }
771 *outlen = 0;
772 if (enc == NULL || enc->symmAlg == NULL || enc->encSymmKey == NULL
773 || enc->encValue == NULL || pkey == NULL) {
774 ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
775 return NULL;
776 }
777
778 /* select symmetric cipher based on algorithm given in message */
779 OBJ_obj2txt(name, sizeof(name), enc->symmAlg->algorithm, 0);
780 (void)ERR_set_mark();
781 cipher = EVP_CIPHER_fetch(libctx, name, propq);
782 if (cipher == NULL)
783 cipher = (EVP_CIPHER *)EVP_get_cipherbyobj(enc->symmAlg->algorithm);
784 if (cipher == NULL) {
785 (void)ERR_clear_last_mark();
786 ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_CIPHER);
787 goto end;
788 }
789 (void)ERR_pop_to_mark();
790
791 cikeysize = EVP_CIPHER_get_key_length(cipher);
792 /* first the symmetric key needs to be decrypted */
793 pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
794 if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx) > 0) {
795 ASN1_BIT_STRING *encKey = enc->encSymmKey;
796 size_t failure;
797 int retval;
798
799 if (EVP_PKEY_decrypt(pkctx, NULL, &eksize,
800 encKey->data, encKey->length)
801 <= 0
802 || (ek = OPENSSL_malloc(eksize)) == NULL)
803 goto end;
804 retval = EVP_PKEY_decrypt(pkctx, ek, &eksize, encKey->data, encKey->length);
805 failure = ~constant_time_is_zero_s(constant_time_msb(retval)
806 | constant_time_is_zero(retval));
807 failure |= ~constant_time_eq_s(eksize, (size_t)cikeysize);
808 if (failure) {
809 ERR_clear_error(); /* error state may have sensitive information */
810 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
811 goto end;
812 }
813 } else {
814 goto end;
815 }
816 iv_len = EVP_CIPHER_get_iv_length(cipher);
817 if ((iv = OPENSSL_malloc(iv_len)) == NULL)
818 goto end;
819 if (enc->symmAlg->parameter == NULL
820 || ASN1_TYPE_get_octetstring(enc->symmAlg->parameter, iv, iv_len)
821 != iv_len) {
822 ERR_raise(ERR_LIB_CRMF, CRMF_R_MALFORMED_IV);
823 goto end;
824 }
825
826 if ((out = OPENSSL_malloc(enc->encValue->length + EVP_CIPHER_get_block_size(cipher))) == NULL
827 || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
828 goto end;
829 EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
830
831 if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
832 || !EVP_DecryptUpdate(evp_ctx, out, outlen,
833 enc->encValue->data,
834 enc->encValue->length)
835 || !EVP_DecryptFinal(evp_ctx, out + *outlen, &n)) {
836 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECRYPTING_ENCRYPTEDVALUE);
837 goto end;
838 }
839 *outlen += n;
840 ret = 1;
841
842 end:
843 EVP_PKEY_CTX_free(pkctx);
844 EVP_CIPHER_CTX_free(evp_ctx);
845 EVP_CIPHER_free(cipher);
846 OPENSSL_clear_free(ek, eksize);
847 OPENSSL_free(iv);
848 if (ret)
849 return out;
850 OPENSSL_free(out);
851 return NULL;
852 }
853
854 /*
855 * Decrypts the certificate in the given encryptedValue using private key pkey.
856 * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2.
857 *
858 * returns a pointer to the decrypted certificate
859 * returns NULL on error or if no certificate available
860 */
861 X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(const OSSL_CRMF_ENCRYPTEDVALUE *ecert,
862 OSSL_LIB_CTX *libctx, const char *propq,
863 EVP_PKEY *pkey)
864 {
865 unsigned char *buf = NULL;
866 const unsigned char *p;
867 int len;
868 X509 *cert = NULL;
869
870 buf = OSSL_CRMF_ENCRYPTEDVALUE_decrypt(ecert, libctx, propq, pkey, &len);
871 if ((p = buf) == NULL || (cert = X509_new_ex(libctx, propq)) == NULL)
872 goto end;
873
874 if (d2i_X509(&cert, &p, len) == NULL) {
875 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE);
876 X509_free(cert);
877 cert = NULL;
878 }
879
880 end:
881 OPENSSL_free(buf);
882 return cert;
883 }
884 /*-
885 * Decrypts the certificate in the given encryptedKey using private key pkey.
886 * This is needed for the indirect PoP method as in RFC 9810 section 5.2.8.3.2.
887 *
888 * returns a pointer to the decrypted certificate
889 * returns NULL on error or if no certificate available
890 */
891 X509 *OSSL_CRMF_ENCRYPTEDKEY_get1_encCert(const OSSL_CRMF_ENCRYPTEDKEY *ecert,
892 OSSL_LIB_CTX *libctx, const char *propq,
893 EVP_PKEY *pkey, unsigned int flags)
894 {
895 #ifndef OPENSSL_NO_CMS
896 BIO *bio;
897 X509 *cert = NULL;
898 #endif
899
900 if (ecert->type != OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA)
901 return OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(ecert->value.encryptedValue,
902 libctx, propq, pkey);
903 #ifndef OPENSSL_NO_CMS
904 bio = CMS_EnvelopedData_decrypt(ecert->value.envelopedData, NULL,
905 pkey, NULL /* cert */, NULL, flags,
906 libctx, propq);
907 if (bio == NULL)
908 return NULL;
909 cert = d2i_X509_bio(bio, NULL);
910 if (cert == NULL)
911 ERR_raise(ERR_LIB_CRMF, CRMF_R_ERROR_DECODING_CERTIFICATE);
912 BIO_free(bio);
913 return cert;
914 #else
915 (void)flags; /* prevent warning on unused parameter */
916 ERR_raise(ERR_LIB_CRMF, CRMF_R_CMS_NOT_SUPPORTED);
917 return NULL;
918 #endif /* OPENSSL_NO_CMS */
919 }
920
921 #ifndef OPENSSL_NO_CMS
922 OSSL_CRMF_ENCRYPTEDKEY *OSSL_CRMF_ENCRYPTEDKEY_init_envdata(CMS_EnvelopedData *envdata)
923 {
924 OSSL_CRMF_ENCRYPTEDKEY *ek = OSSL_CRMF_ENCRYPTEDKEY_new();
925
926 if (ek == NULL)
927 return NULL;
928 ek->type = OSSL_CRMF_ENCRYPTEDKEY_ENVELOPEDDATA;
929 ek->value.envelopedData = envdata;
930 return ek;
931 }
932 #endif