Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/cmp/cmp_server.c

raw
1 /*
2 * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
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
12 /* general CMP server functions */
13
14 #include "cmp_local.h"
15
16 /* the context for the generic CMP server */
17 struct ossl_cmp_srv_ctx_st {
18 OSSL_CMP_CTX *ctx; /* CMP client context reused for transactionID etc. */
19 void *custom_ctx; /* application-specific server context */
20 int certReqId; /* of ir/cr/kur, OSSL_CMP_CERTREQID_NONE for p10cr */
21 int polling; /* current transaction is in polling mode */
22
23 OSSL_CMP_SRV_cert_request_cb_t process_cert_request;
24 OSSL_CMP_SRV_rr_cb_t process_rr;
25 OSSL_CMP_SRV_genm_cb_t process_genm;
26 OSSL_CMP_SRV_error_cb_t process_error;
27 OSSL_CMP_SRV_certConf_cb_t process_certConf;
28 OSSL_CMP_SRV_pollReq_cb_t process_pollReq;
29 OSSL_CMP_SRV_delayed_delivery_cb_t delayed_delivery;
30 OSSL_CMP_SRV_clean_transaction_cb_t clean_transaction;
31
32 int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */
33 int acceptUnprotected; /* Accept requests with no/invalid prot. */
34 int acceptRAVerified; /* Accept ir/cr/kur with POPO RAVerified */
35 int grantImplicitConfirm; /* Grant implicit confirmation if requested */
36
37 }; /* OSSL_CMP_SRV_CTX */
38
39 void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
40 {
41 if (srv_ctx == NULL)
42 return;
43
44 OSSL_CMP_CTX_free(srv_ctx->ctx);
45 OPENSSL_free(srv_ctx);
46 }
47
48 OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
49 {
50 OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
51
52 if (ctx == NULL)
53 goto err;
54
55 if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
56 goto err;
57 ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
58 ctx->polling = 0;
59
60 /* all other elements are initialized to 0 or NULL, respectively */
61 return ctx;
62 err:
63 OSSL_CMP_SRV_CTX_free(ctx);
64 return NULL;
65 }
66
67 int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx,
68 OSSL_CMP_SRV_cert_request_cb_t process_cert_request,
69 OSSL_CMP_SRV_rr_cb_t process_rr,
70 OSSL_CMP_SRV_genm_cb_t process_genm,
71 OSSL_CMP_SRV_error_cb_t process_error,
72 OSSL_CMP_SRV_certConf_cb_t process_certConf,
73 OSSL_CMP_SRV_pollReq_cb_t process_pollReq)
74 {
75 if (srv_ctx == NULL) {
76 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
77 return 0;
78 }
79 srv_ctx->custom_ctx = custom_ctx;
80 srv_ctx->process_cert_request = process_cert_request;
81 srv_ctx->process_rr = process_rr;
82 srv_ctx->process_genm = process_genm;
83 srv_ctx->process_error = process_error;
84 srv_ctx->process_certConf = process_certConf;
85 srv_ctx->process_pollReq = process_pollReq;
86 return 1;
87 }
88
89 int OSSL_CMP_SRV_CTX_init_trans(OSSL_CMP_SRV_CTX *srv_ctx,
90 OSSL_CMP_SRV_delayed_delivery_cb_t delay,
91 OSSL_CMP_SRV_clean_transaction_cb_t clean)
92 {
93 if (srv_ctx == NULL) {
94 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
95 return 0;
96 }
97 srv_ctx->delayed_delivery = delay;
98 srv_ctx->clean_transaction = clean;
99 return 1;
100 }
101
102 OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
103 {
104 if (srv_ctx == NULL) {
105 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
106 return NULL;
107 }
108 return srv_ctx->ctx;
109 }
110
111 void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
112 {
113 if (srv_ctx == NULL) {
114 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
115 return NULL;
116 }
117 return srv_ctx->custom_ctx;
118 }
119
120 int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
121 int val)
122 {
123 if (srv_ctx == NULL) {
124 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
125 return 0;
126 }
127 srv_ctx->sendUnprotectedErrors = val != 0;
128 return 1;
129 }
130
131 int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
132 {
133 if (srv_ctx == NULL) {
134 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
135 return 0;
136 }
137 srv_ctx->acceptUnprotected = val != 0;
138 return 1;
139 }
140
141 int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
142 {
143 if (srv_ctx == NULL) {
144 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
145 return 0;
146 }
147 srv_ctx->acceptRAVerified = val != 0;
148 return 1;
149 }
150
151 int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
152 int val)
153 {
154 if (srv_ctx == NULL) {
155 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
156 return 0;
157 }
158 srv_ctx->grantImplicitConfirm = val != 0;
159 return 1;
160 }
161
162 /* return error msg with waiting status if polling is initiated, else NULL */
163 static OSSL_CMP_MSG *delayed_delivery(OSSL_CMP_SRV_CTX *srv_ctx,
164 const OSSL_CMP_MSG *req)
165 {
166 int ret;
167 unsigned long err;
168 int status = OSSL_CMP_PKISTATUS_waiting,
169 fail_info = 0, errorCode = 0;
170 const char *txt = NULL, *details = NULL;
171 OSSL_CMP_PKISI *si;
172 OSSL_CMP_MSG *msg;
173
174 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
175 && srv_ctx->delayed_delivery != NULL))
176 return NULL;
177
178 ret = srv_ctx->delayed_delivery(srv_ctx, req);
179 if (ret == 0)
180 return NULL;
181 if (ret == 1) {
182 srv_ctx->polling = 1;
183 } else {
184 status = OSSL_CMP_PKISTATUS_rejection;
185 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_systemFailure;
186 txt = "server application error";
187 err = ERR_peek_error();
188 errorCode = ERR_GET_REASON(err);
189 details = ERR_reason_error_string(err);
190 }
191
192 si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt);
193 if (si == NULL)
194 return NULL;
195
196 msg = ossl_cmp_error_new(srv_ctx->ctx, si, errorCode, details,
197 srv_ctx->sendUnprotectedErrors);
198 OSSL_CMP_PKISI_free(si);
199 return msg;
200 }
201
202 /*
203 * Processes an ir/cr/p10cr/kur and returns a certification response.
204 * Only handles the first certification request contained in req
205 * returns an ip/cp/kup on success and NULL on error
206 */
207 static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
208 const OSSL_CMP_MSG *req)
209 {
210 OSSL_CMP_MSG *msg = NULL;
211 OSSL_CMP_PKISI *si = NULL;
212 X509 *certOut = NULL;
213 EVP_PKEY *keyOut = NULL;
214 STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
215 const OSSL_CRMF_MSG *crm = NULL;
216 const X509_REQ *p10cr = NULL;
217 int bodytype;
218 int certReqId, central_keygen;
219
220 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
221 return NULL;
222
223 switch (OSSL_CMP_MSG_get_bodytype(req)) {
224 case OSSL_CMP_PKIBODY_P10CR:
225 case OSSL_CMP_PKIBODY_CR:
226 bodytype = OSSL_CMP_PKIBODY_CP;
227 break;
228 case OSSL_CMP_PKIBODY_IR:
229 bodytype = OSSL_CMP_PKIBODY_IP;
230 break;
231 case OSSL_CMP_PKIBODY_KUR:
232 bodytype = OSSL_CMP_PKIBODY_KUP;
233 break;
234 default:
235 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
236 return NULL;
237 }
238
239 if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
240 certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */
241 p10cr = req->body->value.p10cr;
242 } else {
243 OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
244
245 if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
246 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
247 return NULL;
248 }
249 if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) {
250 ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND);
251 return NULL;
252 }
253 certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
254 if (certReqId != OSSL_CMP_CERTREQID) { /* so far, only possible value */
255 ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
256 return NULL;
257 }
258 }
259 srv_ctx->certReqId = certReqId;
260
261 central_keygen = OSSL_CRMF_MSG_centralkeygen_requested(crm, p10cr);
262 if (central_keygen < 0)
263 return NULL;
264 if (central_keygen == 0
265 && !ossl_cmp_verify_popo(srv_ctx->ctx, req, srv_ctx->acceptRAVerified)) {
266 /* Proof of possession could not be verified */
267 si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
268 1 << OSSL_CMP_PKIFAILUREINFO_badPOP,
269 ERR_reason_error_string(ERR_peek_error()));
270 if (si == NULL)
271 return NULL;
272 } else {
273 OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
274
275 si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
276 &certOut, &chainOut, &caPubs);
277 if (si == NULL)
278 goto err;
279 if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting)
280 srv_ctx->polling = 1;
281 /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */
282 if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx,
283 OSSL_CMP_OPT_IMPLICIT_CONFIRM,
284 ossl_cmp_hdr_has_implicitConfirm(hdr)
285 && srv_ctx->grantImplicitConfirm
286 /* do not set if polling starts: */
287 && certOut != NULL))
288 goto err;
289 if (central_keygen == 1
290 && srv_ctx->ctx->newPkey_priv && srv_ctx->ctx->newPkey != NULL)
291 keyOut = srv_ctx->ctx->newPkey;
292 }
293
294 msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si,
295 certOut, keyOut, NULL /* enc */, chainOut, caPubs,
296 srv_ctx->sendUnprotectedErrors);
297 /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */
298 if (msg == NULL)
299 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
300
301 err:
302 OSSL_CMP_PKISI_free(si);
303 X509_free(certOut);
304 OSSL_CMP_CTX_set0_newPkey(srv_ctx->ctx, 0, NULL);
305 OSSL_STACK_OF_X509_free(chainOut);
306 OSSL_STACK_OF_X509_free(caPubs);
307 return msg;
308 }
309
310 static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
311 const OSSL_CMP_MSG *req)
312 {
313 OSSL_CMP_MSG *msg = NULL;
314 OSSL_CMP_REVDETAILS *details;
315 OSSL_CRMF_CERTID *certId = NULL;
316 OSSL_CRMF_CERTTEMPLATE *tmpl;
317 const X509_NAME *issuer;
318 const ASN1_INTEGER *serial;
319 OSSL_CMP_PKISI *si;
320
321 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
322 return NULL;
323
324 if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
325 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
326 return NULL;
327 }
328 details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr, 0);
329 if (details == NULL) {
330 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
331 return NULL;
332 }
333
334 tmpl = details->certDetails;
335 issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
336 serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
337 if (issuer != NULL && serial != NULL
338 && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
339 return NULL;
340 if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
341 goto err;
342
343 if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
344 srv_ctx->sendUnprotectedErrors))
345 == NULL)
346 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
347
348 err:
349 OSSL_CRMF_CERTID_free(certId);
350 OSSL_CMP_PKISI_free(si);
351 return msg;
352 }
353
354 /*
355 * Processes genm and creates a genp message mirroring the contents of the
356 * incoming message
357 */
358 static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
359 const OSSL_CMP_MSG *req)
360 {
361 OSSL_CMP_GENMSGCONTENT *itavs;
362 OSSL_CMP_MSG *msg;
363
364 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
365 return NULL;
366
367 if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
368 return NULL;
369
370 msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
371 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
372 return msg;
373 }
374
375 static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
376 const OSSL_CMP_MSG *req)
377 {
378 OSSL_CMP_ERRORMSGCONTENT *errorContent;
379 OSSL_CMP_MSG *msg;
380
381 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
382 return NULL;
383 errorContent = req->body->value.error;
384 srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
385 errorContent->errorCode, errorContent->errorDetails);
386
387 if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
388 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
389 return msg;
390 }
391
392 static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
393 const OSSL_CMP_MSG *req)
394 {
395 OSSL_CMP_CTX *ctx;
396 OSSL_CMP_CERTCONFIRMCONTENT *ccc;
397 int num;
398 OSSL_CMP_MSG *msg = NULL;
399 OSSL_CMP_CERTSTATUS *status = NULL;
400
401 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
402 return NULL;
403
404 ctx = srv_ctx->ctx;
405 ccc = req->body->value.certConf;
406 num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
407
408 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
409 || ctx->status != OSSL_CMP_PKISTATUS_trans) {
410 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
411 return NULL;
412 }
413
414 if (num == 0) {
415 ossl_cmp_err(ctx, "certificate rejected by client");
416 } else {
417 if (num > 1)
418 ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
419 status = sk_OSSL_CMP_CERTSTATUS_value(ccc, 0);
420 }
421
422 if (status != NULL) {
423 int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
424 ASN1_OCTET_STRING *certHash = status->certHash;
425 OSSL_CMP_PKISI *si = status->statusInfo;
426
427 if (certReqId != srv_ctx->certReqId) {
428 ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
429 return NULL;
430 }
431 if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
432 return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
433
434 if (si != NULL
435 && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) {
436 int pki_status = ossl_cmp_pkisi_get_status(si);
437 const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
438
439 ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
440 str == NULL ? "without" : "with",
441 str == NULL ? "PKIStatus" : str);
442 }
443 }
444
445 if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
446 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
447 return msg;
448 }
449
450 /* pollReq is handled separately, to avoid recursive call */
451 static OSSL_CMP_MSG *process_non_polling_request(OSSL_CMP_SRV_CTX *srv_ctx,
452 const OSSL_CMP_MSG *req)
453 {
454 OSSL_CMP_MSG *rsp = NULL;
455
456 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
457 && req->body != NULL))
458 return NULL;
459
460 switch (OSSL_CMP_MSG_get_bodytype(req)) {
461 case OSSL_CMP_PKIBODY_IR:
462 case OSSL_CMP_PKIBODY_CR:
463 case OSSL_CMP_PKIBODY_P10CR:
464 case OSSL_CMP_PKIBODY_KUR:
465 if (srv_ctx->process_cert_request == NULL)
466 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
467 else
468 rsp = process_cert_request(srv_ctx, req);
469 break;
470 case OSSL_CMP_PKIBODY_RR:
471 if (srv_ctx->process_rr == NULL)
472 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
473 else
474 rsp = process_rr(srv_ctx, req);
475 break;
476 case OSSL_CMP_PKIBODY_GENM:
477 if (srv_ctx->process_genm == NULL)
478 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
479 else
480 rsp = process_genm(srv_ctx, req);
481 break;
482 case OSSL_CMP_PKIBODY_ERROR:
483 if (srv_ctx->process_error == NULL)
484 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
485 else
486 rsp = process_error(srv_ctx, req);
487 break;
488 case OSSL_CMP_PKIBODY_CERTCONF:
489 if (srv_ctx->process_certConf == NULL)
490 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
491 else
492 rsp = process_certConf(srv_ctx, req);
493 break;
494
495 case OSSL_CMP_PKIBODY_POLLREQ:
496 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
497 break;
498 default:
499 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
500 break;
501 }
502
503 return rsp;
504 }
505
506 static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
507 const OSSL_CMP_MSG *req)
508 {
509 OSSL_CMP_POLLREQCONTENT *prc;
510 OSSL_CMP_POLLREQ *pr;
511 int certReqId;
512 OSSL_CMP_MSG *orig_req;
513 int64_t check_after = 0;
514 OSSL_CMP_MSG *msg = NULL;
515
516 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
517 return NULL;
518
519 if (!srv_ctx->polling) {
520 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
521 return NULL;
522 }
523
524 prc = req->body->value.pollReq;
525 if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) {
526 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
527 return NULL;
528 }
529
530 pr = sk_OSSL_CMP_POLLREQ_value(prc, 0);
531 certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
532 if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
533 &orig_req, &check_after))
534 return NULL;
535
536 if (orig_req != NULL) {
537 srv_ctx->polling = 0;
538 msg = process_non_polling_request(srv_ctx, orig_req);
539 OSSL_CMP_MSG_free(orig_req);
540 } else {
541 if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
542 check_after))
543 == NULL)
544 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
545 }
546 return msg;
547 }
548
549 /*
550 * Determine whether missing/invalid protection of request message is allowed.
551 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
552 */
553 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
554 const OSSL_CMP_MSG *req,
555 int invalid_protection,
556 int accept_unprotected_requests)
557 {
558 if (!ossl_assert(ctx != NULL && req != NULL))
559 return -1;
560
561 if (accept_unprotected_requests) {
562 ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
563 invalid_protection ? "invalid" : "missing");
564 return 1;
565 }
566 if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
567 && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
568 ossl_cmp_warn(ctx, "ignoring missing protection of error message");
569 return 1;
570 }
571 return 0;
572 }
573
574 /*
575 * returns created message and NULL on internal error
576 */
577 OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
578 const OSSL_CMP_MSG *req)
579 {
580 OSSL_CMP_CTX *ctx;
581 ASN1_OCTET_STRING *backup_secret;
582 OSSL_CMP_PKIHEADER *hdr;
583 int req_type, rsp_type;
584 int req_verified = 0;
585 OSSL_CMP_MSG *rsp = NULL;
586
587 if (srv_ctx == NULL || srv_ctx->ctx == NULL
588 || req == NULL || req->body == NULL
589 || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
590 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
591 return 0;
592 }
593 ctx = srv_ctx->ctx;
594 backup_secret = ctx->secretValue;
595 req_type = OSSL_CMP_MSG_get_bodytype(req);
596 ossl_cmp_log1(DEBUG, ctx,
597 "received %s", ossl_cmp_bodytype_to_string(req_type));
598
599 /*
600 * Some things need to be done already before validating the message in
601 * order to be able to send an error message as far as needed and possible.
602 */
603 if (hdr->sender->type != GEN_DIRNAME) {
604 ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
605 goto err;
606 }
607 if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
608 goto err;
609
610 if (srv_ctx->polling && req_type != OSSL_CMP_PKIBODY_POLLREQ
611 && req_type != OSSL_CMP_PKIBODY_ERROR) {
612 ERR_raise(ERR_LIB_CMP, CMP_R_EXPECTED_POLLREQ);
613 goto err;
614 }
615
616 switch (req_type) {
617 case OSSL_CMP_PKIBODY_IR:
618 case OSSL_CMP_PKIBODY_CR:
619 case OSSL_CMP_PKIBODY_P10CR:
620 case OSSL_CMP_PKIBODY_KUR:
621 case OSSL_CMP_PKIBODY_RR:
622 case OSSL_CMP_PKIBODY_GENM:
623 case OSSL_CMP_PKIBODY_ERROR:
624 if (ctx->transactionID != NULL) {
625 char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
626
627 if (tid != NULL)
628 ossl_cmp_log1(WARN, ctx,
629 "Assuming that last transaction with ID=%s got aborted",
630 tid);
631 OPENSSL_free(tid);
632 }
633 /* start of a new transaction, reset transactionID and senderNonce */
634 if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
635 || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
636 goto err;
637
638 if (srv_ctx->clean_transaction != NULL
639 && !srv_ctx->clean_transaction(srv_ctx, NULL)) {
640 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
641 goto err;
642 }
643
644 break;
645 default:
646 /* transactionID should be already initialized */
647 if (ctx->transactionID == NULL) {
648 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
649 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
650 goto err;
651 #endif
652 }
653 }
654
655 req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
656 srv_ctx->acceptUnprotected);
657 if (ctx->secretValue != NULL && ctx->pkey != NULL
658 && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
659 ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
660 if (!req_verified)
661 goto err;
662
663 if (req_type == OSSL_CMP_PKIBODY_POLLREQ) {
664 if (srv_ctx->process_pollReq == NULL)
665 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
666 else
667 rsp = process_pollReq(srv_ctx, req);
668 } else {
669 if (srv_ctx->delayed_delivery != NULL
670 && (rsp = delayed_delivery(srv_ctx, req)) != NULL) {
671 goto err;
672 }
673 rsp = process_non_polling_request(srv_ctx, req);
674 }
675
676 err:
677 if (rsp == NULL) {
678 /* on error, try to respond with CMP error message to client */
679 const char *data = NULL, *reason = NULL;
680 int flags = 0;
681 unsigned long err = ERR_peek_error_data(&data, &flags);
682 int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
683 /* fail_info is not very specific */
684 OSSL_CMP_PKISI *si = NULL;
685
686 if (!req_verified) {
687 /*
688 * Above ossl_cmp_msg_check_update() was not successfully executed,
689 * which normally would set ctx->transactionID and ctx->recipNonce.
690 * So anyway try to provide the right transactionID and recipNonce,
691 * while ignoring any (extra) error in next two function calls.
692 */
693 if (ctx->transactionID == NULL)
694 (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
695 (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
696 }
697
698 if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
699 data = NULL;
700 reason = ERR_reason_error_string(err);
701 if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
702 fail_info, reason))
703 != NULL) {
704 rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
705 data, srv_ctx->sendUnprotectedErrors);
706 OSSL_CMP_PKISI_free(si);
707 }
708 }
709 OSSL_CMP_CTX_print_errors(ctx);
710 ctx->secretValue = backup_secret;
711
712 rsp_type = rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
713 if (rsp != NULL)
714 ossl_cmp_log1(DEBUG, ctx,
715 "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
716 else
717 ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
718
719 /* determine whether to keep the transaction open or not */
720 ctx->status = OSSL_CMP_PKISTATUS_trans;
721 switch (rsp_type) {
722 case OSSL_CMP_PKIBODY_IP:
723 case OSSL_CMP_PKIBODY_CP:
724 case OSSL_CMP_PKIBODY_KUP:
725 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
726 break;
727 /* fall through */
728
729 case OSSL_CMP_PKIBODY_ERROR:
730 if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp))
731 break;
732 /* fall through */
733
734 case OSSL_CMP_PKIBODY_RP:
735 case OSSL_CMP_PKIBODY_PKICONF:
736 case OSSL_CMP_PKIBODY_GENP:
737 /* Other terminating response message types are not supported */
738 srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
739 /* Prepare for next transaction, ignoring any errors here: */
740 if (srv_ctx->clean_transaction != NULL)
741 (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID);
742 (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
743 (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
744 ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
745
746 default: /* not closing transaction in other cases */
747 break;
748 }
749 return rsp;
750 }
751
752 /*
753 * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
754 * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
755 * returns received message on success, else NULL and pushes an element on the
756 * error stack.
757 */
758 OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
759 const OSSL_CMP_MSG *req)
760 {
761 OSSL_CMP_SRV_CTX *srv_ctx = NULL;
762
763 if (client_ctx == NULL || req == NULL) {
764 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
765 return NULL;
766 }
767
768 if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
769 ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
770 return NULL;
771 }
772
773 return OSSL_CMP_SRV_process_request(srv_ctx, req);
774 }