cmp_server.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */
  209. if (msg == NULL)
  210. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
  211. err:
  212. OSSL_CMP_PKISI_free(si);
  213. X509_free(certOut);
  214. OSSL_STACK_OF_X509_free(chainOut);
  215. OSSL_STACK_OF_X509_free(caPubs);
  216. return msg;
  217. }
  218. static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
  219. const OSSL_CMP_MSG *req)
  220. {
  221. OSSL_CMP_MSG *msg = NULL;
  222. OSSL_CMP_REVDETAILS *details;
  223. OSSL_CRMF_CERTID *certId = NULL;
  224. OSSL_CRMF_CERTTEMPLATE *tmpl;
  225. const X509_NAME *issuer;
  226. const ASN1_INTEGER *serial;
  227. OSSL_CMP_PKISI *si;
  228. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  229. return NULL;
  230. if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
  231. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
  232. return NULL;
  233. }
  234. if ((details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr,
  235. OSSL_CMP_REVREQSID)) == NULL) {
  236. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  237. return NULL;
  238. }
  239. tmpl = details->certDetails;
  240. issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
  241. serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
  242. if (issuer != NULL && serial != NULL
  243. && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
  244. return NULL;
  245. if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
  246. goto err;
  247. if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
  248. srv_ctx->sendUnprotectedErrors)) == NULL)
  249. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
  250. err:
  251. OSSL_CRMF_CERTID_free(certId);
  252. OSSL_CMP_PKISI_free(si);
  253. return msg;
  254. }
  255. /*
  256. * Processes genm and creates a genp message mirroring the contents of the
  257. * incoming message
  258. */
  259. static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
  260. const OSSL_CMP_MSG *req)
  261. {
  262. OSSL_CMP_GENMSGCONTENT *itavs;
  263. OSSL_CMP_MSG *msg;
  264. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  265. return NULL;
  266. if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
  267. return NULL;
  268. msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
  269. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  270. return msg;
  271. }
  272. static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
  273. const OSSL_CMP_MSG *req)
  274. {
  275. OSSL_CMP_ERRORMSGCONTENT *errorContent;
  276. OSSL_CMP_MSG *msg;
  277. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  278. return NULL;
  279. errorContent = req->body->value.error;
  280. srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
  281. errorContent->errorCode, errorContent->errorDetails);
  282. if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
  283. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  284. return msg;
  285. }
  286. static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
  287. const OSSL_CMP_MSG *req)
  288. {
  289. OSSL_CMP_CTX *ctx;
  290. OSSL_CMP_CERTCONFIRMCONTENT *ccc;
  291. int num;
  292. OSSL_CMP_MSG *msg = NULL;
  293. OSSL_CMP_CERTSTATUS *status = NULL;
  294. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  295. return NULL;
  296. ctx = srv_ctx->ctx;
  297. ccc = req->body->value.certConf;
  298. num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
  299. if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
  300. || ctx->status != OSSL_CMP_PKISTATUS_trans) {
  301. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
  302. return NULL;
  303. }
  304. if (num == 0) {
  305. ossl_cmp_err(ctx, "certificate rejected by client");
  306. } else {
  307. if (num > 1)
  308. ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
  309. status = sk_OSSL_CMP_CERTSTATUS_value(ccc, OSSL_CMP_CERTREQID);
  310. }
  311. if (status != NULL) {
  312. int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
  313. ASN1_OCTET_STRING *certHash = status->certHash;
  314. OSSL_CMP_PKISI *si = status->statusInfo;
  315. if (certReqId != srv_ctx->certReqId) {
  316. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  317. return NULL;
  318. }
  319. if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
  320. return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
  321. if (si != NULL
  322. && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) {
  323. int pki_status = ossl_cmp_pkisi_get_status(si);
  324. const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
  325. ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
  326. str == NULL ? "without" : "with",
  327. str == NULL ? "PKIStatus" : str);
  328. }
  329. }
  330. if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
  331. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
  332. return msg;
  333. }
  334. static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
  335. const OSSL_CMP_MSG *req)
  336. {
  337. OSSL_CMP_POLLREQCONTENT *prc;
  338. OSSL_CMP_POLLREQ *pr;
  339. int certReqId;
  340. OSSL_CMP_MSG *certReq;
  341. int64_t check_after = 0;
  342. OSSL_CMP_MSG *msg = NULL;
  343. if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
  344. return NULL;
  345. prc = req->body->value.pollReq;
  346. if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) {
  347. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
  348. return NULL;
  349. }
  350. pr = sk_OSSL_CMP_POLLREQ_value(prc, OSSL_CMP_CERTREQID);
  351. certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
  352. if (certReqId != srv_ctx->certReqId) {
  353. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  354. return NULL;
  355. }
  356. if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
  357. &certReq, &check_after))
  358. return NULL;
  359. if (certReq != NULL) {
  360. msg = process_cert_request(srv_ctx, certReq);
  361. OSSL_CMP_MSG_free(certReq);
  362. } else {
  363. if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
  364. check_after)) == NULL)
  365. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
  366. }
  367. return msg;
  368. }
  369. /*
  370. * Determine whether missing/invalid protection of request message is allowed.
  371. * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
  372. */
  373. static int unprotected_exception(const OSSL_CMP_CTX *ctx,
  374. const OSSL_CMP_MSG *req,
  375. int invalid_protection,
  376. int accept_unprotected_requests)
  377. {
  378. if (!ossl_assert(ctx != NULL && req != NULL))
  379. return -1;
  380. if (accept_unprotected_requests) {
  381. ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
  382. invalid_protection ? "invalid" : "missing");
  383. return 1;
  384. }
  385. if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
  386. && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
  387. ossl_cmp_warn(ctx, "ignoring missing protection of error message");
  388. return 1;
  389. }
  390. return 0;
  391. }
  392. /*
  393. * returns created message and NULL on internal error
  394. */
  395. OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
  396. const OSSL_CMP_MSG *req)
  397. {
  398. OSSL_CMP_CTX *ctx;
  399. ASN1_OCTET_STRING *backup_secret;
  400. OSSL_CMP_PKIHEADER *hdr;
  401. int req_type, rsp_type;
  402. int req_verified = 0;
  403. OSSL_CMP_MSG *rsp = NULL;
  404. if (srv_ctx == NULL || srv_ctx->ctx == NULL
  405. || req == NULL || req->body == NULL
  406. || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
  407. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  408. return 0;
  409. }
  410. ctx = srv_ctx->ctx;
  411. backup_secret = ctx->secretValue;
  412. req_type = OSSL_CMP_MSG_get_bodytype(req);
  413. ossl_cmp_log1(DEBUG, ctx,
  414. "received %s", ossl_cmp_bodytype_to_string(req_type));
  415. /*
  416. * Some things need to be done already before validating the message in
  417. * order to be able to send an error message as far as needed and possible.
  418. */
  419. if (hdr->sender->type != GEN_DIRNAME) {
  420. ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
  421. goto err;
  422. }
  423. if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
  424. goto err;
  425. switch (req_type) {
  426. case OSSL_CMP_PKIBODY_IR:
  427. case OSSL_CMP_PKIBODY_CR:
  428. case OSSL_CMP_PKIBODY_P10CR:
  429. case OSSL_CMP_PKIBODY_KUR:
  430. case OSSL_CMP_PKIBODY_RR:
  431. case OSSL_CMP_PKIBODY_GENM:
  432. case OSSL_CMP_PKIBODY_ERROR:
  433. if (ctx->transactionID != NULL) {
  434. char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
  435. if (tid != NULL)
  436. ossl_cmp_log1(WARN, ctx,
  437. "Assuming that last transaction with ID=%s got aborted",
  438. tid);
  439. OPENSSL_free(tid);
  440. }
  441. /* start of a new transaction, reset transactionID and senderNonce */
  442. if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
  443. || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
  444. goto err;
  445. break;
  446. default:
  447. /* transactionID should be already initialized */
  448. if (ctx->transactionID == NULL) {
  449. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  450. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  451. goto err;
  452. #endif
  453. }
  454. }
  455. req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
  456. srv_ctx->acceptUnprotected);
  457. if (ctx->secretValue != NULL && ctx->pkey != NULL
  458. && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
  459. ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
  460. if (!req_verified)
  461. goto err;
  462. switch (req_type) {
  463. case OSSL_CMP_PKIBODY_IR:
  464. case OSSL_CMP_PKIBODY_CR:
  465. case OSSL_CMP_PKIBODY_P10CR:
  466. case OSSL_CMP_PKIBODY_KUR:
  467. if (srv_ctx->process_cert_request == NULL)
  468. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  469. else
  470. rsp = process_cert_request(srv_ctx, req);
  471. break;
  472. case OSSL_CMP_PKIBODY_RR:
  473. if (srv_ctx->process_rr == NULL)
  474. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  475. else
  476. rsp = process_rr(srv_ctx, req);
  477. break;
  478. case OSSL_CMP_PKIBODY_GENM:
  479. if (srv_ctx->process_genm == NULL)
  480. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  481. else
  482. rsp = process_genm(srv_ctx, req);
  483. break;
  484. case OSSL_CMP_PKIBODY_ERROR:
  485. if (srv_ctx->process_error == NULL)
  486. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  487. else
  488. rsp = process_error(srv_ctx, req);
  489. break;
  490. case OSSL_CMP_PKIBODY_CERTCONF:
  491. if (srv_ctx->process_certConf == NULL)
  492. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  493. else
  494. rsp = process_certConf(srv_ctx, req);
  495. break;
  496. case OSSL_CMP_PKIBODY_POLLREQ:
  497. if (srv_ctx->process_pollReq == NULL)
  498. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
  499. else
  500. rsp = process_pollReq(srv_ctx, req);
  501. break;
  502. default:
  503. /* Other request message types are not supported */
  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. /* fail_info is not very specific */
  515. OSSL_CMP_PKISI *si = NULL;
  516. if (!req_verified) {
  517. /*
  518. * Above ossl_cmp_msg_check_update() was not successfully executed,
  519. * which normally would set ctx->transactionID and ctx->recipNonce.
  520. * So anyway try to provide the right transactionID and recipNonce,
  521. * while ignoring any (extra) error in next two function calls.
  522. */
  523. if (ctx->transactionID == NULL)
  524. (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
  525. (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
  526. }
  527. if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
  528. data = NULL;
  529. reason = ERR_reason_error_string(err);
  530. if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
  531. fail_info, reason)) != NULL) {
  532. rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
  533. data, srv_ctx->sendUnprotectedErrors);
  534. OSSL_CMP_PKISI_free(si);
  535. }
  536. }
  537. OSSL_CMP_CTX_print_errors(ctx);
  538. ctx->secretValue = backup_secret;
  539. rsp_type =
  540. rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
  541. if (rsp != NULL)
  542. ossl_cmp_log1(DEBUG, ctx,
  543. "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
  544. else
  545. ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
  546. /* determine whether to keep the transaction open or not */
  547. ctx->status = OSSL_CMP_PKISTATUS_trans;
  548. switch (rsp_type) {
  549. case OSSL_CMP_PKIBODY_IP:
  550. case OSSL_CMP_PKIBODY_CP:
  551. case OSSL_CMP_PKIBODY_KUP:
  552. if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
  553. break;
  554. /* fall through */
  555. case OSSL_CMP_PKIBODY_RP:
  556. case OSSL_CMP_PKIBODY_PKICONF:
  557. case OSSL_CMP_PKIBODY_GENP:
  558. case OSSL_CMP_PKIBODY_ERROR:
  559. /* Other terminating response message types are not supported */
  560. /* Prepare for next transaction, ignoring any errors here: */
  561. (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
  562. (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
  563. ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
  564. default: /* not closing transaction in other cases */
  565. break;
  566. }
  567. return rsp;
  568. }
  569. /*
  570. * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
  571. * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
  572. * returns received message on success, else NULL and pushes an element on the
  573. * error stack.
  574. */
  575. OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
  576. const OSSL_CMP_MSG *req)
  577. {
  578. OSSL_CMP_SRV_CTX *srv_ctx = NULL;
  579. if (client_ctx == NULL || req == NULL) {
  580. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  581. return NULL;
  582. }
  583. if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
  584. ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
  585. return NULL;
  586. }
  587. return OSSL_CMP_SRV_process_request(srv_ctx, req);
  588. }