cmp_server.c 25 KB

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