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