cmp_mock_srv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Siemens AG 2018-2020
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or atf
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "apps.h"
  11. #include "cmp_mock_srv.h"
  12. #include <openssl/cmp.h>
  13. #include <openssl/err.h>
  14. #include <openssl/cmperr.h>
  15. /* the context for the CMP mock server */
  16. typedef struct
  17. {
  18. X509 *refCert; /* cert to expect for oldCertID in kur/rr msg */
  19. X509 *certOut; /* certificate to be returned in cp/ip/kup msg */
  20. STACK_OF(X509) *chainOut; /* chain of certOut to add to extraCerts field */
  21. STACK_OF(X509) *caPubsOut; /* used in caPubs of ip and in caCerts of genp */
  22. X509 *newWithNew; /* to return in newWithNew of rootKeyUpdate */
  23. X509 *newWithOld; /* to return in newWithOld of rootKeyUpdate */
  24. X509 *oldWithNew; /* to return in oldWithNew of rootKeyUpdate */
  25. OSSL_CMP_PKISI *statusOut; /* status for ip/cp/kup/rp msg unless polling */
  26. int sendError; /* send error response on given request type */
  27. OSSL_CMP_MSG *req; /* original request message during polling */
  28. int pollCount; /* number of polls before actual cert response */
  29. int curr_pollCount; /* number of polls so far for current request */
  30. int checkAfterTime; /* time the client should wait between polling */
  31. } mock_srv_ctx;
  32. static void mock_srv_ctx_free(mock_srv_ctx *ctx)
  33. {
  34. if (ctx == NULL)
  35. return;
  36. OSSL_CMP_PKISI_free(ctx->statusOut);
  37. X509_free(ctx->refCert);
  38. X509_free(ctx->certOut);
  39. OSSL_STACK_OF_X509_free(ctx->chainOut);
  40. OSSL_STACK_OF_X509_free(ctx->caPubsOut);
  41. OSSL_CMP_MSG_free(ctx->req);
  42. OPENSSL_free(ctx);
  43. }
  44. static mock_srv_ctx *mock_srv_ctx_new(void)
  45. {
  46. mock_srv_ctx *ctx = OPENSSL_zalloc(sizeof(mock_srv_ctx));
  47. if (ctx == NULL)
  48. goto err;
  49. if ((ctx->statusOut = OSSL_CMP_PKISI_new()) == NULL)
  50. goto err;
  51. ctx->sendError = -1;
  52. /* all other elements are initialized to 0 or NULL, respectively */
  53. return ctx;
  54. err:
  55. mock_srv_ctx_free(ctx);
  56. return NULL;
  57. }
  58. #define DEFINE_OSSL_SET1_CERT(FIELD) \
  59. int ossl_cmp_mock_srv_set1_##FIELD(OSSL_CMP_SRV_CTX *srv_ctx, \
  60. X509 *cert) \
  61. { \
  62. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx); \
  63. \
  64. if (ctx == NULL) { \
  65. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
  66. return 0; \
  67. } \
  68. if (cert == NULL || X509_up_ref(cert)) { \
  69. X509_free(ctx->FIELD); \
  70. ctx->FIELD = cert; \
  71. return 1; \
  72. } \
  73. return 0; \
  74. }
  75. DEFINE_OSSL_SET1_CERT(refCert)
  76. DEFINE_OSSL_SET1_CERT(certOut)
  77. int ossl_cmp_mock_srv_set1_chainOut(OSSL_CMP_SRV_CTX *srv_ctx,
  78. STACK_OF(X509) *chain)
  79. {
  80. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  81. STACK_OF(X509) *chain_copy = NULL;
  82. if (ctx == NULL) {
  83. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  84. return 0;
  85. }
  86. if (chain != NULL && (chain_copy = X509_chain_up_ref(chain)) == NULL)
  87. return 0;
  88. OSSL_STACK_OF_X509_free(ctx->chainOut);
  89. ctx->chainOut = chain_copy;
  90. return 1;
  91. }
  92. int ossl_cmp_mock_srv_set1_caPubsOut(OSSL_CMP_SRV_CTX *srv_ctx,
  93. STACK_OF(X509) *caPubs)
  94. {
  95. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  96. STACK_OF(X509) *caPubs_copy = NULL;
  97. if (ctx == NULL) {
  98. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  99. return 0;
  100. }
  101. if (caPubs != NULL && (caPubs_copy = X509_chain_up_ref(caPubs)) == NULL)
  102. return 0;
  103. OSSL_STACK_OF_X509_free(ctx->caPubsOut);
  104. ctx->caPubsOut = caPubs_copy;
  105. return 1;
  106. }
  107. DEFINE_OSSL_SET1_CERT(newWithNew)
  108. DEFINE_OSSL_SET1_CERT(newWithOld)
  109. DEFINE_OSSL_SET1_CERT(oldWithNew)
  110. int ossl_cmp_mock_srv_set_statusInfo(OSSL_CMP_SRV_CTX *srv_ctx, int status,
  111. int fail_info, const char *text)
  112. {
  113. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  114. OSSL_CMP_PKISI *si;
  115. if (ctx == NULL) {
  116. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  117. return 0;
  118. }
  119. if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, text)) == NULL)
  120. return 0;
  121. OSSL_CMP_PKISI_free(ctx->statusOut);
  122. ctx->statusOut = si;
  123. return 1;
  124. }
  125. int ossl_cmp_mock_srv_set_sendError(OSSL_CMP_SRV_CTX *srv_ctx, int bodytype)
  126. {
  127. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  128. if (ctx == NULL) {
  129. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  130. return 0;
  131. }
  132. /* might check bodytype, but this would require exporting all body types */
  133. ctx->sendError = bodytype;
  134. return 1;
  135. }
  136. int ossl_cmp_mock_srv_set_pollCount(OSSL_CMP_SRV_CTX *srv_ctx, int count)
  137. {
  138. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  139. if (ctx == NULL) {
  140. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  141. return 0;
  142. }
  143. if (count < 0) {
  144. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  145. return 0;
  146. }
  147. ctx->pollCount = count;
  148. return 1;
  149. }
  150. int ossl_cmp_mock_srv_set_checkAfterTime(OSSL_CMP_SRV_CTX *srv_ctx, int sec)
  151. {
  152. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  153. if (ctx == NULL) {
  154. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  155. return 0;
  156. }
  157. ctx->checkAfterTime = sec;
  158. return 1;
  159. }
  160. /* determine whether to delay response to (non-polling) request */
  161. static int delayed_delivery(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *req)
  162. {
  163. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  164. int req_type = OSSL_CMP_MSG_get_bodytype(req);
  165. if (ctx == NULL || req == NULL) {
  166. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  167. return -1;
  168. }
  169. /*
  170. * For ir/cr/p10cr/kur delayed delivery is handled separately in
  171. * process_cert_request
  172. */
  173. if (req_type == OSSL_CMP_IR
  174. || req_type == OSSL_CMP_CR
  175. || req_type == OSSL_CMP_P10CR
  176. || req_type == OSSL_CMP_KUR
  177. /* Client may use error to abort the ongoing polling */
  178. || req_type == OSSL_CMP_ERROR)
  179. return 0;
  180. if (ctx->pollCount > 0 && ctx->curr_pollCount == 0) {
  181. /* start polling */
  182. if ((ctx->req = OSSL_CMP_MSG_dup(req)) == NULL)
  183. return -1;
  184. return 1;
  185. }
  186. return 0;
  187. }
  188. /* check for matching reference cert components, as far as given */
  189. static int refcert_cmp(const X509 *refcert,
  190. const X509_NAME *issuer, const ASN1_INTEGER *serial)
  191. {
  192. const X509_NAME *ref_issuer;
  193. const ASN1_INTEGER *ref_serial;
  194. if (refcert == NULL)
  195. return 1;
  196. ref_issuer = X509_get_issuer_name(refcert);
  197. ref_serial = X509_get0_serialNumber(refcert);
  198. return (ref_issuer == NULL || X509_NAME_cmp(issuer, ref_issuer) == 0)
  199. && (ref_serial == NULL || ASN1_INTEGER_cmp(serial, ref_serial) == 0);
  200. }
  201. /* reset the state that belongs to a transaction */
  202. static int clean_transaction(OSSL_CMP_SRV_CTX *srv_ctx,
  203. ossl_unused const ASN1_OCTET_STRING *id)
  204. {
  205. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  206. if (ctx == NULL) {
  207. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  208. return 0;
  209. }
  210. ctx->curr_pollCount = 0;
  211. OSSL_CMP_MSG_free(ctx->req);
  212. ctx->req = NULL;
  213. return 1;
  214. }
  215. static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
  216. const OSSL_CMP_MSG *cert_req,
  217. ossl_unused int certReqId,
  218. const OSSL_CRMF_MSG *crm,
  219. const X509_REQ *p10cr,
  220. X509 **certOut,
  221. STACK_OF(X509) **chainOut,
  222. STACK_OF(X509) **caPubs)
  223. {
  224. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  225. int bodytype;
  226. OSSL_CMP_PKISI *si = NULL;
  227. if (ctx == NULL || cert_req == NULL
  228. || certOut == NULL || chainOut == NULL || caPubs == NULL) {
  229. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  230. return NULL;
  231. }
  232. bodytype = OSSL_CMP_MSG_get_bodytype(cert_req);
  233. if (ctx->sendError == 1 || ctx->sendError == bodytype) {
  234. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  235. return NULL;
  236. }
  237. *certOut = NULL;
  238. *chainOut = NULL;
  239. *caPubs = NULL;
  240. if (ctx->pollCount > 0 && ctx->curr_pollCount == 0) {
  241. /* start polling */
  242. if ((ctx->req = OSSL_CMP_MSG_dup(cert_req)) == NULL)
  243. return NULL;
  244. return OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_waiting, 0, NULL);
  245. }
  246. if (ctx->curr_pollCount >= ctx->pollCount)
  247. /* give final response after polling */
  248. ctx->curr_pollCount = 0;
  249. /* accept cert profile for cr messages only with the configured name */
  250. if (OSSL_CMP_MSG_get_bodytype(cert_req) == OSSL_CMP_CR) {
  251. STACK_OF(OSSL_CMP_ITAV) *itavs =
  252. OSSL_CMP_HDR_get0_geninfo_ITAVs(OSSL_CMP_MSG_get0_header(cert_req));
  253. int i;
  254. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  255. OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_value(itavs, i);
  256. ASN1_OBJECT *obj = OSSL_CMP_ITAV_get0_type(itav);
  257. STACK_OF(ASN1_UTF8STRING) *strs;
  258. ASN1_UTF8STRING *str;
  259. const char *data;
  260. if (OBJ_obj2nid(obj) == NID_id_it_certProfile) {
  261. if (!OSSL_CMP_ITAV_get0_certProfile(itav, &strs))
  262. return NULL;
  263. if (sk_ASN1_UTF8STRING_num(strs) < 1) {
  264. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_CERTPROFILE);
  265. return NULL;
  266. }
  267. str = sk_ASN1_UTF8STRING_value(strs, 0);
  268. if (str == NULL
  269. || (data =
  270. (const char *)ASN1_STRING_get0_data(str)) == NULL) {
  271. ERR_raise(ERR_LIB_CMP, ERR_R_PASSED_INVALID_ARGUMENT);
  272. return NULL;
  273. }
  274. if (strcmp(data, "profile1") != 0) {
  275. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_CERTPROFILE);
  276. return NULL;
  277. }
  278. break;
  279. }
  280. }
  281. }
  282. /* accept cert update request only for the reference cert, if given */
  283. if (bodytype == OSSL_CMP_KUR
  284. && crm != NULL /* thus not p10cr */ && ctx->refCert != NULL) {
  285. const OSSL_CRMF_CERTID *cid = OSSL_CRMF_MSG_get0_regCtrl_oldCertID(crm);
  286. if (cid == NULL) {
  287. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_CERTID);
  288. return NULL;
  289. }
  290. if (!refcert_cmp(ctx->refCert,
  291. OSSL_CRMF_CERTID_get0_issuer(cid),
  292. OSSL_CRMF_CERTID_get0_serialNumber(cid))) {
  293. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_CERTID);
  294. return NULL;
  295. }
  296. }
  297. if (ctx->certOut != NULL
  298. && (*certOut = X509_dup(ctx->certOut)) == NULL)
  299. /* Should return a cert produced from request template, see FR #16054 */
  300. goto err;
  301. if (ctx->chainOut != NULL
  302. && (*chainOut = X509_chain_up_ref(ctx->chainOut)) == NULL)
  303. goto err;
  304. if (ctx->caPubsOut != NULL /* OSSL_CMP_PKIBODY_IP not visible here */
  305. && (*caPubs = X509_chain_up_ref(ctx->caPubsOut)) == NULL)
  306. goto err;
  307. if (ctx->statusOut != NULL
  308. && (si = OSSL_CMP_PKISI_dup(ctx->statusOut)) == NULL)
  309. goto err;
  310. return si;
  311. err:
  312. X509_free(*certOut);
  313. *certOut = NULL;
  314. OSSL_STACK_OF_X509_free(*chainOut);
  315. *chainOut = NULL;
  316. OSSL_STACK_OF_X509_free(*caPubs);
  317. *caPubs = NULL;
  318. return NULL;
  319. }
  320. static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
  321. const OSSL_CMP_MSG *rr,
  322. const X509_NAME *issuer,
  323. const ASN1_INTEGER *serial)
  324. {
  325. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  326. if (ctx == NULL || rr == NULL) {
  327. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  328. return NULL;
  329. }
  330. if (ctx->sendError == 1
  331. || ctx->sendError == OSSL_CMP_MSG_get_bodytype(rr)) {
  332. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  333. return NULL;
  334. }
  335. /* allow any RR derived from CSR which does not include issuer and serial */
  336. if ((issuer != NULL || serial != NULL)
  337. /* accept revocation only for the reference cert, if given */
  338. && !refcert_cmp(ctx->refCert, issuer, serial)) {
  339. ERR_raise_data(ERR_LIB_CMP, CMP_R_REQUEST_NOT_ACCEPTED,
  340. "wrong certificate to revoke");
  341. return NULL;
  342. }
  343. return OSSL_CMP_PKISI_dup(ctx->statusOut);
  344. }
  345. static OSSL_CMP_ITAV *process_genm_itav(mock_srv_ctx *ctx, int req_nid,
  346. const OSSL_CMP_ITAV *req)
  347. {
  348. OSSL_CMP_ITAV *rsp;
  349. switch (req_nid) {
  350. case NID_id_it_caCerts:
  351. rsp = OSSL_CMP_ITAV_new_caCerts(ctx->caPubsOut);
  352. break;
  353. case NID_id_it_rootCaCert:
  354. rsp = OSSL_CMP_ITAV_new_rootCaKeyUpdate(ctx->newWithNew,
  355. ctx->newWithOld,
  356. ctx->oldWithNew);
  357. break;
  358. default:
  359. rsp = OSSL_CMP_ITAV_dup(req);
  360. }
  361. return rsp;
  362. }
  363. static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
  364. const OSSL_CMP_MSG *genm,
  365. const STACK_OF(OSSL_CMP_ITAV) *in,
  366. STACK_OF(OSSL_CMP_ITAV) **out)
  367. {
  368. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  369. if (ctx == NULL || genm == NULL || in == NULL || out == NULL) {
  370. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  371. return 0;
  372. }
  373. if (ctx->sendError == 1
  374. || ctx->sendError == OSSL_CMP_MSG_get_bodytype(genm)
  375. || sk_OSSL_CMP_ITAV_num(in) > 1) {
  376. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  377. return 0;
  378. }
  379. if (sk_OSSL_CMP_ITAV_num(in) == 1) {
  380. OSSL_CMP_ITAV *req = sk_OSSL_CMP_ITAV_value(in, 0), *rsp;
  381. ASN1_OBJECT *obj = OSSL_CMP_ITAV_get0_type(req);
  382. if ((*out = sk_OSSL_CMP_ITAV_new_reserve(NULL, 1)) == NULL)
  383. return 0;
  384. rsp = process_genm_itav(ctx, OBJ_obj2nid(obj), req);
  385. if (rsp != NULL && sk_OSSL_CMP_ITAV_push(*out, rsp))
  386. return 1;
  387. sk_OSSL_CMP_ITAV_free(*out);
  388. return 0;
  389. }
  390. *out = sk_OSSL_CMP_ITAV_deep_copy(in, OSSL_CMP_ITAV_dup,
  391. OSSL_CMP_ITAV_free);
  392. return *out != NULL;
  393. }
  394. static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
  395. const OSSL_CMP_PKISI *statusInfo,
  396. const ASN1_INTEGER *errorCode,
  397. const OSSL_CMP_PKIFREETEXT *errorDetails)
  398. {
  399. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  400. char buf[OSSL_CMP_PKISI_BUFLEN];
  401. char *sibuf;
  402. int i;
  403. if (ctx == NULL || error == NULL) {
  404. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  405. return;
  406. }
  407. BIO_printf(bio_err, "mock server received error:\n");
  408. if (statusInfo == NULL) {
  409. BIO_printf(bio_err, "pkiStatusInfo absent\n");
  410. } else {
  411. sibuf = OSSL_CMP_snprint_PKIStatusInfo(statusInfo, buf, sizeof(buf));
  412. BIO_printf(bio_err, "pkiStatusInfo: %s\n",
  413. sibuf != NULL ? sibuf: "<invalid>");
  414. }
  415. if (errorCode == NULL)
  416. BIO_printf(bio_err, "errorCode absent\n");
  417. else
  418. BIO_printf(bio_err, "errorCode: %ld\n", ASN1_INTEGER_get(errorCode));
  419. if (sk_ASN1_UTF8STRING_num(errorDetails) <= 0) {
  420. BIO_printf(bio_err, "errorDetails absent\n");
  421. } else {
  422. BIO_printf(bio_err, "errorDetails: ");
  423. for (i = 0; i < sk_ASN1_UTF8STRING_num(errorDetails); i++) {
  424. if (i > 0)
  425. BIO_printf(bio_err, ", ");
  426. ASN1_STRING_print_ex(bio_err,
  427. sk_ASN1_UTF8STRING_value(errorDetails, i),
  428. ASN1_STRFLGS_ESC_QUOTE);
  429. }
  430. BIO_printf(bio_err, "\n");
  431. }
  432. }
  433. static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
  434. const OSSL_CMP_MSG *certConf,
  435. ossl_unused int certReqId,
  436. const ASN1_OCTET_STRING *certHash,
  437. const OSSL_CMP_PKISI *si)
  438. {
  439. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  440. ASN1_OCTET_STRING *digest;
  441. if (ctx == NULL || certConf == NULL || certHash == NULL) {
  442. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  443. return 0;
  444. }
  445. if (ctx->sendError == 1
  446. || ctx->sendError == OSSL_CMP_MSG_get_bodytype(certConf)
  447. || ctx->certOut == NULL) {
  448. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  449. return 0;
  450. }
  451. if ((digest = X509_digest_sig(ctx->certOut, NULL, NULL)) == NULL)
  452. return 0;
  453. if (ASN1_OCTET_STRING_cmp(certHash, digest) != 0) {
  454. ASN1_OCTET_STRING_free(digest);
  455. ERR_raise(ERR_LIB_CMP, CMP_R_CERTHASH_UNMATCHED);
  456. return 0;
  457. }
  458. ASN1_OCTET_STRING_free(digest);
  459. return 1;
  460. }
  461. /* return 0 on failure, 1 on success, setting *req or otherwise *check_after */
  462. static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
  463. const OSSL_CMP_MSG *pollReq,
  464. ossl_unused int certReqId,
  465. OSSL_CMP_MSG **req, int64_t *check_after)
  466. {
  467. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  468. if (req != NULL)
  469. *req = NULL;
  470. if (ctx == NULL || pollReq == NULL
  471. || req == NULL || check_after == NULL) {
  472. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  473. return 0;
  474. }
  475. if (ctx->sendError == 1
  476. || ctx->sendError == OSSL_CMP_MSG_get_bodytype(pollReq)) {
  477. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  478. return 0;
  479. }
  480. if (ctx->req == NULL) { /* not currently in polling mode */
  481. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_POLLREQ);
  482. return 0;
  483. }
  484. if (++ctx->curr_pollCount >= ctx->pollCount) {
  485. /* end polling */
  486. *req = ctx->req;
  487. ctx->req = NULL;
  488. *check_after = 0;
  489. } else {
  490. *check_after = ctx->checkAfterTime;
  491. }
  492. return 1;
  493. }
  494. OSSL_CMP_SRV_CTX *ossl_cmp_mock_srv_new(OSSL_LIB_CTX *libctx, const char *propq)
  495. {
  496. OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new(libctx, propq);
  497. mock_srv_ctx *ctx = mock_srv_ctx_new();
  498. if (srv_ctx != NULL && ctx != NULL
  499. && OSSL_CMP_SRV_CTX_init(srv_ctx, ctx, process_cert_request,
  500. process_rr, process_genm, process_error,
  501. process_certConf, process_pollReq)
  502. && OSSL_CMP_SRV_CTX_init_trans(srv_ctx,
  503. delayed_delivery, clean_transaction))
  504. return srv_ctx;
  505. mock_srv_ctx_free(ctx);
  506. OSSL_CMP_SRV_CTX_free(srv_ctx);
  507. return NULL;
  508. }
  509. void ossl_cmp_mock_srv_free(OSSL_CMP_SRV_CTX *srv_ctx)
  510. {
  511. if (srv_ctx != NULL)
  512. mock_srv_ctx_free(OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx));
  513. OSSL_CMP_SRV_CTX_free(srv_ctx);
  514. }