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)) 351 == NULL) 352 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR); 353 354 err: 355 OSSL_CRMF_CERTID_free(certId); 356 OSSL_CMP_PKISI_free(si); 357 return msg; 358 } 359 360 /* 361 * Processes genm and creates a genp message mirroring the contents of the 362 * incoming message 363 */ 364 static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx, 365 const OSSL_CMP_MSG *req) 366 { 367 OSSL_CMP_GENMSGCONTENT *itavs; 368 OSSL_CMP_MSG *msg; 369 370 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) 371 return NULL; 372 373 if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs)) 374 return NULL; 375 376 msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs); 377 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free); 378 return msg; 379 } 380 381 static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx, 382 const OSSL_CMP_MSG *req) 383 { 384 OSSL_CMP_ERRORMSGCONTENT *errorContent; 385 OSSL_CMP_MSG *msg; 386 387 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) 388 return NULL; 389 errorContent = req->body->value.error; 390 srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo, 391 errorContent->errorCode, errorContent->errorDetails); 392 393 if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL) 394 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF); 395 return msg; 396 } 397 398 static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx, 399 const OSSL_CMP_MSG *req) 400 { 401 OSSL_CMP_CTX *ctx; 402 OSSL_CMP_CERTCONFIRMCONTENT *ccc; 403 int num; 404 OSSL_CMP_MSG *msg = NULL; 405 OSSL_CMP_CERTSTATUS *status = NULL; 406 407 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) 408 return NULL; 409 410 ctx = srv_ctx->ctx; 411 ccc = req->body->value.certConf; 412 num = sk_OSSL_CMP_CERTSTATUS_num(ccc); 413 414 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1 415 || ctx->status != OSSL_CMP_PKISTATUS_trans) { 416 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF); 417 return NULL; 418 } 419 420 if (num == 0) { 421 ossl_cmp_err(ctx, "certificate rejected by client"); 422 } else { 423 if (num > 1) 424 ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored"); 425 status = sk_OSSL_CMP_CERTSTATUS_value(ccc, 0); 426 } 427 428 if (status != NULL) { 429 int certReqId = ossl_cmp_asn1_get_int(status->certReqId); 430 ASN1_OCTET_STRING *certHash = status->certHash; 431 OSSL_CMP_PKISI *si = status->statusInfo; 432 433 if (certReqId != srv_ctx->certReqId) { 434 ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID); 435 return NULL; 436 } 437 if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si)) 438 return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */ 439 440 if (si != NULL 441 && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) { 442 int pki_status = ossl_cmp_pkisi_get_status(si); 443 const char *str = ossl_cmp_PKIStatus_to_string(pki_status); 444 445 ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s", 446 str == NULL ? "without" : "with", 447 str == NULL ? "PKIStatus" : str); 448 } 449 } 450 451 if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL) 452 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF); 453 return msg; 454 } 455 456 /* pollReq is handled separately, to avoid recursive call */ 457 static OSSL_CMP_MSG *process_non_polling_request(OSSL_CMP_SRV_CTX *srv_ctx, 458 const OSSL_CMP_MSG *req) 459 { 460 OSSL_CMP_MSG *rsp = NULL; 461 462 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL 463 && req->body != NULL)) 464 return NULL; 465 466 switch (OSSL_CMP_MSG_get_bodytype(req)) { 467 case OSSL_CMP_PKIBODY_IR: 468 case OSSL_CMP_PKIBODY_CR: 469 case OSSL_CMP_PKIBODY_P10CR: 470 case OSSL_CMP_PKIBODY_KUR: 471 if (srv_ctx->process_cert_request == NULL) 472 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 473 else 474 rsp = process_cert_request(srv_ctx, req); 475 break; 476 case OSSL_CMP_PKIBODY_RR: 477 if (srv_ctx->process_rr == NULL) 478 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 479 else 480 rsp = process_rr(srv_ctx, req); 481 break; 482 case OSSL_CMP_PKIBODY_GENM: 483 if (srv_ctx->process_genm == NULL) 484 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 485 else 486 rsp = process_genm(srv_ctx, req); 487 break; 488 case OSSL_CMP_PKIBODY_ERROR: 489 if (srv_ctx->process_error == NULL) 490 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 491 else 492 rsp = process_error(srv_ctx, req); 493 break; 494 case OSSL_CMP_PKIBODY_CERTCONF: 495 if (srv_ctx->process_certConf == NULL) 496 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 497 else 498 rsp = process_certConf(srv_ctx, req); 499 break; 500 501 case OSSL_CMP_PKIBODY_POLLREQ: 502 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); 503 break; 504 default: 505 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 506 break; 507 } 508 509 return rsp; 510 } 511 512 static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx, 513 const OSSL_CMP_MSG *req) 514 { 515 OSSL_CMP_POLLREQCONTENT *prc; 516 OSSL_CMP_POLLREQ *pr; 517 int certReqId; 518 OSSL_CMP_MSG *orig_req; 519 int64_t check_after = 0; 520 OSSL_CMP_MSG *msg = NULL; 521 522 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) 523 return NULL; 524 525 if (!srv_ctx->polling) { 526 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); 527 return NULL; 528 } 529 530 prc = req->body->value.pollReq; 531 if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) { 532 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED); 533 return NULL; 534 } 535 536 pr = sk_OSSL_CMP_POLLREQ_value(prc, 0); 537 certReqId = ossl_cmp_asn1_get_int(pr->certReqId); 538 if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId, 539 &orig_req, &check_after)) 540 return NULL; 541 542 if (orig_req != NULL) { 543 srv_ctx->polling = 0; 544 msg = process_non_polling_request(srv_ctx, orig_req); 545 OSSL_CMP_MSG_free(orig_req); 546 } else { 547 if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId, 548 check_after)) 549 == NULL) 550 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP); 551 } 552 return msg; 553 } 554 555 /* 556 * Determine whether missing/invalid protection of request message is allowed. 557 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error. 558 */ 559 static int unprotected_exception(const OSSL_CMP_CTX *ctx, 560 const OSSL_CMP_MSG *req, 561 int invalid_protection, 562 int accept_unprotected_requests) 563 { 564 if (!ossl_assert(ctx != NULL && req != NULL)) 565 return -1; 566 567 if (accept_unprotected_requests) { 568 ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message", 569 invalid_protection ? "invalid" : "missing"); 570 return 1; 571 } 572 if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR 573 && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) { 574 ossl_cmp_warn(ctx, "ignoring missing protection of error message"); 575 return 1; 576 } 577 return 0; 578 } 579 580 /* 581 * returns created message and NULL on internal error 582 */ 583 OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx, 584 const OSSL_CMP_MSG *req) 585 { 586 OSSL_CMP_CTX *ctx; 587 ASN1_OCTET_STRING *backup_secret; 588 OSSL_CMP_PKIHEADER *hdr; 589 int req_type, rsp_type; 590 int req_verified = 0; 591 OSSL_CMP_MSG *rsp = NULL; 592 593 if (srv_ctx == NULL || srv_ctx->ctx == NULL 594 || req == NULL || req->body == NULL 595 || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) { 596 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); 597 return 0; 598 } 599 ctx = srv_ctx->ctx; 600 backup_secret = ctx->secretValue; 601 req_type = OSSL_CMP_MSG_get_bodytype(req); 602 ossl_cmp_log1(DEBUG, ctx, 603 "received %s", ossl_cmp_bodytype_to_string(req_type)); 604 605 /* 606 * Some things need to be done already before validating the message in 607 * order to be able to send an error message as far as needed and possible. 608 */ 609 if (hdr->sender->type != GEN_DIRNAME) { 610 ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED); 611 goto err; 612 } 613 if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName)) 614 goto err; 615 616 if (srv_ctx->polling && req_type != OSSL_CMP_PKIBODY_POLLREQ 617 && req_type != OSSL_CMP_PKIBODY_ERROR) { 618 ERR_raise(ERR_LIB_CMP, CMP_R_EXPECTED_POLLREQ); 619 goto err; 620 } 621 622 switch (req_type) { 623 case OSSL_CMP_PKIBODY_IR: 624 case OSSL_CMP_PKIBODY_CR: 625 case OSSL_CMP_PKIBODY_P10CR: 626 case OSSL_CMP_PKIBODY_KUR: 627 case OSSL_CMP_PKIBODY_RR: 628 case OSSL_CMP_PKIBODY_GENM: 629 case OSSL_CMP_PKIBODY_ERROR: 630 if (ctx->transactionID != NULL) { 631 char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID); 632 633 if (tid != NULL) 634 ossl_cmp_log1(WARN, ctx, 635 "Assuming that last transaction with ID=%s got aborted", 636 tid); 637 OPENSSL_free(tid); 638 } 639 /* start of a new transaction, reset transactionID and senderNonce */ 640 if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL) 641 || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)) 642 goto err; 643 644 if (srv_ctx->clean_transaction != NULL 645 && !srv_ctx->clean_transaction(srv_ctx, NULL)) { 646 ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE); 647 goto err; 648 } 649 650 break; 651 default: 652 /* transactionID should be already initialized */ 653 if (ctx->transactionID == NULL) { 654 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 655 ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); 656 goto err; 657 #endif 658 } 659 } 660 661 req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception, 662 srv_ctx->acceptUnprotected); 663 if (ctx->secretValue != NULL && ctx->pkey != NULL 664 && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC) 665 ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */ 666 if (!req_verified) 667 goto err; 668 669 if (req_type == OSSL_CMP_PKIBODY_POLLREQ) { 670 if (srv_ctx->process_pollReq == NULL) 671 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); 672 else 673 rsp = process_pollReq(srv_ctx, req); 674 } else { 675 if (srv_ctx->delayed_delivery != NULL 676 && (rsp = delayed_delivery(srv_ctx, req)) != NULL) { 677 goto err; 678 } 679 rsp = process_non_polling_request(srv_ctx, req); 680 } 681 682 err: 683 if (rsp == NULL) { 684 /* on error, try to respond with CMP error message to client */ 685 const char *data = NULL, *reason = NULL; 686 int flags = 0; 687 unsigned long err = ERR_peek_error_data(&data, &flags); 688 int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest; 689 /* fail_info is not very specific */ 690 OSSL_CMP_PKISI *si = NULL; 691 692 if (!req_verified) { 693 /* 694 * Above ossl_cmp_msg_check_update() was not successfully executed, 695 * which normally would set ctx->transactionID and ctx->recipNonce. 696 * So anyway try to provide the right transactionID and recipNonce, 697 * while ignoring any (extra) error in next two function calls. 698 */ 699 if (ctx->transactionID == NULL) 700 (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID); 701 (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce); 702 } 703 704 if ((flags & ERR_TXT_STRING) == 0 || *data == '\0') 705 data = NULL; 706 reason = ERR_reason_error_string(err); 707 if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, 708 fail_info, reason)) 709 != NULL) { 710 rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err, 711 data, srv_ctx->sendUnprotectedErrors); 712 OSSL_CMP_PKISI_free(si); 713 } 714 } 715 OSSL_CMP_CTX_print_errors(ctx); 716 ctx->secretValue = backup_secret; 717 718 rsp_type = rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR; 719 if (rsp != NULL) 720 ossl_cmp_log1(DEBUG, ctx, 721 "sending %s", ossl_cmp_bodytype_to_string(rsp_type)); 722 else 723 ossl_cmp_log(ERR, ctx, "cannot send proper CMP response"); 724 725 /* determine whether to keep the transaction open or not */ 726 ctx->status = OSSL_CMP_PKISTATUS_trans; 727 switch (rsp_type) { 728 case OSSL_CMP_PKIBODY_IP: 729 case OSSL_CMP_PKIBODY_CP: 730 case OSSL_CMP_PKIBODY_KUP: 731 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0) 732 break; 733 /* fall through */ 734 735 case OSSL_CMP_PKIBODY_ERROR: 736 if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp)) 737 break; 738 /* fall through */ 739 740 case OSSL_CMP_PKIBODY_RP: 741 case OSSL_CMP_PKIBODY_PKICONF: 742 case OSSL_CMP_PKIBODY_GENP: 743 /* Other terminating response message types are not supported */ 744 srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID; 745 /* Prepare for next transaction, ignoring any errors here: */ 746 if (srv_ctx->clean_transaction != NULL) 747 (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID); 748 (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL); 749 (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL); 750 ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */ 751 752 default: /* not closing transaction in other cases */ 753 break; 754 } 755 return rsp; 756 } 757 758 /* 759 * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client. 760 * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg. 761 * returns received message on success, else NULL and pushes an element on the 762 * error stack. 763 */ 764 OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx, 765 const OSSL_CMP_MSG *req) 766 { 767 OSSL_CMP_SRV_CTX *srv_ctx = NULL; 768 769 if (client_ctx == NULL || req == NULL) { 770 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); 771 return NULL; 772 } 773 774 if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) { 775 ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR); 776 return NULL; 777 } 778 779 return OSSL_CMP_SRV_process_request(srv_ctx, req); 780 } 781