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