cmp_server.c 22 KB

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