cmp_mock_srv.c 22 KB

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