cmp_server.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright 2007-2021 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. /* general CMP server functions */
  12. #include <openssl/asn1t.h>
  13. #include "cmp_local.h"
  14. /* explicit #includes not strictly needed since implied by the above: */
  15. #include <openssl/cmp.h>
  16. #include <openssl/err.h>
  17. /* the context for the generic CMP server */
  18. struct ossl_cmp_srv_ctx_st
  19. {
  20. OSSL_CMP_CTX *ctx; /* Client CMP context, partly reused for srv */
  21. void *custom_ctx; /* pointer to specific server context */
  22. OSSL_CMP_SRV_cert_request_cb_t process_cert_request;
  23. OSSL_CMP_SRV_rr_cb_t process_rr;
  24. OSSL_CMP_SRV_genm_cb_t process_genm;
  25. OSSL_CMP_SRV_error_cb_t process_error;
  26. OSSL_CMP_SRV_certConf_cb_t process_certConf;
  27. OSSL_CMP_SRV_pollReq_cb_t process_pollReq;
  28. int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */
  29. int acceptUnprotected; /* Accept requests with no/invalid prot. */
  30. int acceptRAVerified; /* Accept ir/cr/kur with POPO RAVerified */
  31. int grantImplicitConfirm; /* Grant implicit confirmation if requested */
  32. }; /* OSSL_CMP_SRV_CTX */
  33. void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
  34. {
  35. if (srv_ctx == NULL)
  36. return;
  37. OSSL_CMP_CTX_free(srv_ctx->ctx);
  38. OPENSSL_free(srv_ctx);
  39. }
  40. OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
  41. {
  42. OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
  43. if (ctx == NULL)
  44. goto err;
  45. if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
  46. goto err;
  47. /* all other elements are initialized to 0 or NULL, respectively */
  48. return ctx;
  49. err:
  50. OSSL_CMP_SRV_CTX_free(ctx);
  51. return NULL;
  52. }
  53. int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx,
  54. OSSL_CMP_SRV_cert_request_cb_t process_cert_request,
  55. OSSL_CMP_SRV_rr_cb_t process_rr,
  56. OSSL_CMP_SRV_genm_cb_t process_genm,
  57. OSSL_CMP_SRV_error_cb_t process_error,
  58. OSSL_CMP_SRV_certConf_cb_t process_certConf,
  59. OSSL_CMP_SRV_pollReq_cb_t process_pollReq)
  60. {
  61. if (srv_ctx == NULL) {
  62. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  63. return 0;
  64. }
  65. srv_ctx->custom_ctx = custom_ctx;
  66. srv_ctx->process_cert_request = process_cert_request;
  67. srv_ctx->process_rr = process_rr;
  68. srv_ctx->process_genm = process_genm;
  69. srv_ctx->process_error = process_error;
  70. srv_ctx->process_certConf = process_certConf;
  71. srv_ctx->process_pollReq = process_pollReq;
  72. return 1;
  73. }
  74. OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
  75. {
  76. if (srv_ctx == NULL) {
  77. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  78. return NULL;
  79. }
  80. return srv_ctx->ctx;
  81. }
  82. void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
  83. {
  84. if (srv_ctx == NULL) {
  85. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  86. return NULL;
  87. }
  88. return srv_ctx->custom_ctx;
  89. }
  90. int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
  91. int val)
  92. {
  93. if (srv_ctx == NULL) {
  94. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  95. return 0;
  96. }
  97. srv_ctx->sendUnprotectedErrors = val != 0;
  98. return 1;
  99. }
  100. int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
  101. {
  102. if (srv_ctx == NULL) {
  103. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  104. return 0;
  105. }
  106. srv_ctx->acceptUnprotected = val != 0;
  107. return 1;
  108. }
  109. int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
  110. {
  111. if (srv_ctx == NULL) {
  112. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  113. return 0;
  114. }
  115. srv_ctx->acceptRAVerified = val != 0;
  116. return 1;
  117. }
  118. int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
  119. int val)
  120. {
  121. if (srv_ctx == NULL) {
  122. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  123. return 0;
  124. }
  125. srv_ctx->grantImplicitConfirm = val != 0;
  126. return 1;
  127. }
  128. /*
  129. * Processes an ir/cr/p10cr/kur and returns a certification response.
  130. * Only handles the first certification request contained in req
  131. * returns an ip/cp/kup on success and NULL on error
  132. */
  133. static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
  134. const OSSL_CMP_MSG *req)
  135. {
  136. OSSL_CMP_MSG *msg = NULL;
  137. OSSL_CMP_PKISI *si = NULL;
  138. X509 *certOut = NULL;
  139. STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
  140. const OSSL_CRMF_MSG *crm = NULL;
  141. const X509_REQ *p10cr = NULL;
  142. int bodytype;
  143. int certReqId;
  144. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  145. return NULL;
  146. switch (OSSL_CMP_MSG_get_bodytype(req)) {
  147. case OSSL_CMP_PKIBODY_P10CR:
  148. case OSSL_CMP_PKIBODY_CR:
  149. bodytype = OSSL_CMP_PKIBODY_CP;
  150. break;
  151. case OSSL_CMP_PKIBODY_IR:
  152. bodytype = OSSL_CMP_PKIBODY_IP;
  153. break;
  154. case OSSL_CMP_PKIBODY_KUR:
  155. bodytype = OSSL_CMP_PKIBODY_KUP;
  156. break;
  157. default:
  158. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  159. return NULL;
  160. }
  161. if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
  162. certReqId = OSSL_CMP_CERTREQID;
  163. p10cr = req->body->value.p10cr;
  164. } else {
  165. OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
  166. if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
  167. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
  168. return NULL;
  169. }
  170. if ((crm = sk_OSSL_CRMF_MSG_value(reqs, OSSL_CMP_CERTREQID)) == NULL) {
  171. ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND);
  172. return NULL;
  173. }
  174. certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
  175. }
  176. if (!ossl_cmp_verify_popo(srv_ctx->ctx, req, srv_ctx->acceptRAVerified)) {
  177. /* Proof of possession could not be verified */
  178. si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
  179. 1 << OSSL_CMP_PKIFAILUREINFO_badPOP,
  180. ERR_reason_error_string(ERR_peek_error()));
  181. if (si == NULL)
  182. return NULL;
  183. } else {
  184. OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
  185. si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
  186. &certOut, &chainOut, &caPubs);
  187. if (si == NULL)
  188. goto err;
  189. /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */
  190. if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx,
  191. OSSL_CMP_OPT_IMPLICIT_CONFIRM,
  192. ossl_cmp_hdr_has_implicitConfirm(hdr)
  193. && srv_ctx->grantImplicitConfirm
  194. /* do not set if polling starts: */
  195. && certOut != NULL))
  196. goto err;
  197. }
  198. msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si,
  199. certOut, NULL /* enc */, chainOut, caPubs,
  200. srv_ctx->sendUnprotectedErrors);
  201. /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */
  202. if (msg == NULL)
  203. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
  204. err:
  205. OSSL_CMP_PKISI_free(si);
  206. X509_free(certOut);
  207. OSSL_STACK_OF_X509_free(chainOut);
  208. OSSL_STACK_OF_X509_free(caPubs);
  209. return msg;
  210. }
  211. static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
  212. const OSSL_CMP_MSG *req)
  213. {
  214. OSSL_CMP_MSG *msg = NULL;
  215. OSSL_CMP_REVDETAILS *details;
  216. OSSL_CRMF_CERTID *certId = NULL;
  217. OSSL_CRMF_CERTTEMPLATE *tmpl;
  218. const X509_NAME *issuer;
  219. const ASN1_INTEGER *serial;
  220. OSSL_CMP_PKISI *si;
  221. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  222. return NULL;
  223. if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
  224. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
  225. return NULL;
  226. }
  227. if ((details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr,
  228. OSSL_CMP_REVREQSID)) == NULL) {
  229. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  230. return NULL;
  231. }
  232. tmpl = details->certDetails;
  233. issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
  234. serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
  235. if (issuer != NULL && serial != NULL
  236. && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
  237. return NULL;
  238. if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
  239. goto err;
  240. if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
  241. srv_ctx->sendUnprotectedErrors)) == NULL)
  242. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
  243. err:
  244. OSSL_CRMF_CERTID_free(certId);
  245. OSSL_CMP_PKISI_free(si);
  246. return msg;
  247. }
  248. /*
  249. * Processes genm and creates a genp message mirroring the contents of the
  250. * incoming message
  251. */
  252. static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
  253. const OSSL_CMP_MSG *req)
  254. {
  255. OSSL_CMP_GENMSGCONTENT *itavs;
  256. OSSL_CMP_MSG *msg;
  257. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  258. return NULL;
  259. if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
  260. return NULL;
  261. msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
  262. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  263. return msg;
  264. }
  265. static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
  266. const OSSL_CMP_MSG *req)
  267. {
  268. OSSL_CMP_ERRORMSGCONTENT *errorContent;
  269. OSSL_CMP_MSG *msg;
  270. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  271. return NULL;
  272. errorContent = req->body->value.error;
  273. srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
  274. errorContent->errorCode, errorContent->errorDetails);
  275. if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
  276. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  277. return msg;
  278. }
  279. static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
  280. const OSSL_CMP_MSG *req)
  281. {
  282. OSSL_CMP_CTX *ctx;
  283. OSSL_CMP_CERTCONFIRMCONTENT *ccc;
  284. int num;
  285. OSSL_CMP_MSG *msg = NULL;
  286. OSSL_CMP_CERTSTATUS *status = NULL;
  287. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  288. return NULL;
  289. ctx = srv_ctx->ctx;
  290. ccc = req->body->value.certConf;
  291. num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
  292. if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
  293. || ctx->status != OSSL_CMP_PKISTATUS_trans) {
  294. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
  295. return NULL;
  296. }
  297. if (num == 0) {
  298. ossl_cmp_err(ctx, "certificate rejected by client");
  299. } else {
  300. if (num > 1)
  301. ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
  302. status = sk_OSSL_CMP_CERTSTATUS_value(ccc, OSSL_CMP_CERTREQID);
  303. }
  304. if (status != NULL) {
  305. int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
  306. ASN1_OCTET_STRING *certHash = status->certHash;
  307. OSSL_CMP_PKISI *si = status->statusInfo;
  308. if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
  309. return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
  310. if (si != NULL
  311. && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) {
  312. int pki_status = ossl_cmp_pkisi_get_status(si);
  313. const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
  314. ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
  315. str == NULL ? "without" : "with",
  316. str == NULL ? "PKIStatus" : str);
  317. }
  318. }
  319. if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
  320. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  321. return msg;
  322. }
  323. static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
  324. const OSSL_CMP_MSG *req)
  325. {
  326. OSSL_CMP_POLLREQCONTENT *prc;
  327. OSSL_CMP_POLLREQ *pr;
  328. int certReqId;
  329. OSSL_CMP_MSG *certReq;
  330. int64_t check_after = 0;
  331. OSSL_CMP_MSG *msg = NULL;
  332. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  333. return NULL;
  334. prc = req->body->value.pollReq;
  335. if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) {
  336. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
  337. return NULL;
  338. }
  339. pr = sk_OSSL_CMP_POLLREQ_value(prc, 0);
  340. certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
  341. if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
  342. &certReq, &check_after))
  343. return NULL;
  344. if (certReq != NULL) {
  345. msg = process_cert_request(srv_ctx, certReq);
  346. OSSL_CMP_MSG_free(certReq);
  347. } else {
  348. if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
  349. check_after)) == NULL)
  350. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
  351. }
  352. return msg;
  353. }
  354. /*
  355. * Determine whether missing/invalid protection of request message is allowed.
  356. * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
  357. */
  358. static int unprotected_exception(const OSSL_CMP_CTX *ctx,
  359. const OSSL_CMP_MSG *req,
  360. int invalid_protection,
  361. int accept_unprotected_requests)
  362. {
  363. if (!ossl_assert(ctx != NULL && req != NULL))
  364. return -1;
  365. if (accept_unprotected_requests) {
  366. ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
  367. invalid_protection ? "invalid" : "missing");
  368. return 1;
  369. }
  370. if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
  371. && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
  372. ossl_cmp_warn(ctx, "ignoring missing protection of error message");
  373. return 1;
  374. }
  375. return 0;
  376. }
  377. /*
  378. * returns created message and NULL on internal error
  379. */
  380. OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
  381. const OSSL_CMP_MSG *req)
  382. {
  383. OSSL_CMP_CTX *ctx;
  384. ASN1_OCTET_STRING *backup_secret;
  385. OSSL_CMP_PKIHEADER *hdr;
  386. int req_type, rsp_type;
  387. int res;
  388. OSSL_CMP_MSG *rsp = NULL;
  389. if (srv_ctx == NULL || srv_ctx->ctx == NULL
  390. || req == NULL || req->body == NULL
  391. || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
  392. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  393. return 0;
  394. }
  395. ctx = srv_ctx->ctx;
  396. backup_secret = ctx->secretValue;
  397. req_type = OSSL_CMP_MSG_get_bodytype(req);
  398. ossl_cmp_log1(DEBUG, ctx,
  399. "received %s", ossl_cmp_bodytype_to_string(req_type));
  400. /*
  401. * Some things need to be done already before validating the message in
  402. * order to be able to send an error message as far as needed and possible.
  403. */
  404. if (hdr->sender->type != GEN_DIRNAME) {
  405. ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
  406. goto err;
  407. }
  408. if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
  409. goto err;
  410. switch (req_type) {
  411. case OSSL_CMP_PKIBODY_IR:
  412. case OSSL_CMP_PKIBODY_CR:
  413. case OSSL_CMP_PKIBODY_P10CR:
  414. case OSSL_CMP_PKIBODY_KUR:
  415. case OSSL_CMP_PKIBODY_RR:
  416. case OSSL_CMP_PKIBODY_GENM:
  417. case OSSL_CMP_PKIBODY_ERROR:
  418. if (ctx->transactionID != NULL) {
  419. char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
  420. if (tid != NULL)
  421. ossl_cmp_log1(WARN, ctx,
  422. "Assuming that last transaction with ID=%s got aborted",
  423. tid);
  424. OPENSSL_free(tid);
  425. }
  426. /* start of a new transaction, reset transactionID and senderNonce */
  427. if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
  428. || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
  429. goto err;
  430. break;
  431. default:
  432. /* transactionID should be already initialized */
  433. if (ctx->transactionID == NULL) {
  434. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  435. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  436. goto err;
  437. #endif
  438. }
  439. }
  440. res = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
  441. srv_ctx->acceptUnprotected);
  442. if (ctx->secretValue != NULL && ctx->pkey != NULL
  443. && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
  444. ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
  445. if (!res)
  446. goto err;
  447. switch (req_type) {
  448. case OSSL_CMP_PKIBODY_IR:
  449. case OSSL_CMP_PKIBODY_CR:
  450. case OSSL_CMP_PKIBODY_P10CR:
  451. case OSSL_CMP_PKIBODY_KUR:
  452. if (srv_ctx->process_cert_request == NULL)
  453. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  454. else
  455. rsp = process_cert_request(srv_ctx, req);
  456. break;
  457. case OSSL_CMP_PKIBODY_RR:
  458. if (srv_ctx->process_rr == NULL)
  459. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  460. else
  461. rsp = process_rr(srv_ctx, req);
  462. break;
  463. case OSSL_CMP_PKIBODY_GENM:
  464. if (srv_ctx->process_genm == NULL)
  465. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  466. else
  467. rsp = process_genm(srv_ctx, req);
  468. break;
  469. case OSSL_CMP_PKIBODY_ERROR:
  470. if (srv_ctx->process_error == NULL)
  471. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  472. else
  473. rsp = process_error(srv_ctx, req);
  474. break;
  475. case OSSL_CMP_PKIBODY_CERTCONF:
  476. if (srv_ctx->process_certConf == NULL)
  477. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  478. else
  479. rsp = process_certConf(srv_ctx, req);
  480. break;
  481. case OSSL_CMP_PKIBODY_POLLREQ:
  482. if (srv_ctx->process_pollReq == NULL)
  483. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  484. else
  485. rsp = process_pollReq(srv_ctx, req);
  486. break;
  487. default:
  488. /* Other request message types are not supported */
  489. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  490. break;
  491. }
  492. err:
  493. if (rsp == NULL) {
  494. /* on error, try to respond with CMP error message to client */
  495. const char *data = NULL, *reason = NULL;
  496. int flags = 0;
  497. unsigned long err = ERR_peek_error_data(&data, &flags);
  498. int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
  499. /* fail_info is not very specific */
  500. OSSL_CMP_PKISI *si = NULL;
  501. if (ctx->transactionID == NULL) {
  502. /* ignore any (extra) error in next two function calls: */
  503. (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
  504. (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
  505. }
  506. if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
  507. data = NULL;
  508. reason = ERR_reason_error_string(err);
  509. if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
  510. fail_info, reason)) != NULL) {
  511. rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
  512. data, srv_ctx->sendUnprotectedErrors);
  513. OSSL_CMP_PKISI_free(si);
  514. }
  515. }
  516. OSSL_CMP_CTX_print_errors(ctx);
  517. ctx->secretValue = backup_secret;
  518. rsp_type =
  519. rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
  520. if (rsp != NULL)
  521. ossl_cmp_log1(DEBUG, ctx,
  522. "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
  523. else
  524. ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
  525. /* determine whether to keep the transaction open or not */
  526. ctx->status = OSSL_CMP_PKISTATUS_trans;
  527. switch (rsp_type) {
  528. case OSSL_CMP_PKIBODY_IP:
  529. case OSSL_CMP_PKIBODY_CP:
  530. case OSSL_CMP_PKIBODY_KUP:
  531. if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
  532. break;
  533. /* fall through */
  534. case OSSL_CMP_PKIBODY_RP:
  535. case OSSL_CMP_PKIBODY_PKICONF:
  536. case OSSL_CMP_PKIBODY_GENP:
  537. case OSSL_CMP_PKIBODY_ERROR:
  538. /* Other terminating response message types are not supported */
  539. /* Prepare for next transaction, ignoring any errors here: */
  540. (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
  541. (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
  542. ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
  543. default: /* not closing transaction in other cases */
  544. break;
  545. }
  546. return rsp;
  547. }
  548. /*
  549. * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
  550. * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
  551. * returns received message on success, else NULL and pushes an element on the
  552. * error stack.
  553. */
  554. OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
  555. const OSSL_CMP_MSG *req)
  556. {
  557. OSSL_CMP_SRV_CTX *srv_ctx = NULL;
  558. if (client_ctx == NULL || req == NULL) {
  559. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  560. return NULL;
  561. }
  562. if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
  563. ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
  564. return NULL;
  565. }
  566. return OSSL_CMP_SRV_process_request(srv_ctx, req);
  567. }