cmp_server.c 21 KB

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