cmp_mock_srv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 2018-2020 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. DEFINE_STACK_OF(X509)
  16. DEFINE_STACK_OF(OSSL_CMP_ITAV)
  17. DEFINE_STACK_OF(ASN1_UTF8STRING)
  18. /* the context for the CMP mock server */
  19. typedef struct
  20. {
  21. X509 *certOut; /* certificate to be returned in cp/ip/kup msg */
  22. STACK_OF(X509) *chainOut; /* chain of certOut to add to extraCerts field */
  23. STACK_OF(X509) *caPubsOut; /* certs to return in caPubs field of ip msg */
  24. OSSL_CMP_PKISI *statusOut; /* status for ip/cp/kup/rp msg unless polling */
  25. int sendError; /* send error response also on valid requests */
  26. OSSL_CMP_MSG *certReq; /* ir/cr/p10cr/kur remembered while polling */
  27. int certReqId; /* id of last ir/cr/kur, used for polling */
  28. int pollCount; /* number of polls before actual cert response */
  29. int checkAfterTime; /* time the client should wait between polling */
  30. } mock_srv_ctx;
  31. static void mock_srv_ctx_free(mock_srv_ctx *ctx)
  32. {
  33. if (ctx == NULL)
  34. return;
  35. OSSL_CMP_PKISI_free(ctx->statusOut);
  36. X509_free(ctx->certOut);
  37. sk_X509_pop_free(ctx->chainOut, X509_free);
  38. sk_X509_pop_free(ctx->caPubsOut, X509_free);
  39. OSSL_CMP_MSG_free(ctx->certReq);
  40. OPENSSL_free(ctx);
  41. }
  42. static mock_srv_ctx *mock_srv_ctx_new(void)
  43. {
  44. mock_srv_ctx *ctx = OPENSSL_zalloc(sizeof(mock_srv_ctx));
  45. if (ctx == NULL)
  46. goto err;
  47. if ((ctx->statusOut = OSSL_CMP_PKISI_new()) == NULL)
  48. goto err;
  49. ctx->certReqId = -1;
  50. /* all other elements are initialized to 0 or NULL, respectively */
  51. return ctx;
  52. err:
  53. mock_srv_ctx_free(ctx);
  54. return NULL;
  55. }
  56. int ossl_cmp_mock_srv_set1_certOut(OSSL_CMP_SRV_CTX *srv_ctx, X509 *cert)
  57. {
  58. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  59. if (ctx == NULL) {
  60. CMPerr(0, CMP_R_NULL_ARGUMENT);
  61. return 0;
  62. }
  63. if (cert == NULL || X509_up_ref(cert)) {
  64. X509_free(ctx->certOut);
  65. ctx->certOut = cert;
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. int ossl_cmp_mock_srv_set1_chainOut(OSSL_CMP_SRV_CTX *srv_ctx,
  71. STACK_OF(X509) *chain)
  72. {
  73. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  74. STACK_OF(X509) *chain_copy = NULL;
  75. if (ctx == NULL) {
  76. CMPerr(0, CMP_R_NULL_ARGUMENT);
  77. return 0;
  78. }
  79. if (chain != NULL && (chain_copy = X509_chain_up_ref(chain)) == NULL)
  80. return 0;
  81. sk_X509_pop_free(ctx->chainOut, X509_free);
  82. ctx->chainOut = chain_copy;
  83. return 1;
  84. }
  85. int ossl_cmp_mock_srv_set1_caPubsOut(OSSL_CMP_SRV_CTX *srv_ctx,
  86. STACK_OF(X509) *caPubs)
  87. {
  88. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  89. STACK_OF(X509) *caPubs_copy = NULL;
  90. if (ctx == NULL) {
  91. CMPerr(0, CMP_R_NULL_ARGUMENT);
  92. return 0;
  93. }
  94. if (caPubs != NULL && (caPubs_copy = X509_chain_up_ref(caPubs)) == NULL)
  95. return 0;
  96. sk_X509_pop_free(ctx->caPubsOut, X509_free);
  97. ctx->caPubsOut = caPubs_copy;
  98. return 1;
  99. }
  100. int ossl_cmp_mock_srv_set_statusInfo(OSSL_CMP_SRV_CTX *srv_ctx, int status,
  101. int fail_info, const char *text)
  102. {
  103. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  104. OSSL_CMP_PKISI *si;
  105. if (ctx == NULL) {
  106. CMPerr(0, CMP_R_NULL_ARGUMENT);
  107. return 0;
  108. }
  109. if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, text)) == NULL)
  110. return 0;
  111. OSSL_CMP_PKISI_free(ctx->statusOut);
  112. ctx->statusOut = si;
  113. return 1;
  114. }
  115. int ossl_cmp_mock_srv_set_send_error(OSSL_CMP_SRV_CTX *srv_ctx, int val)
  116. {
  117. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  118. if (ctx == NULL) {
  119. CMPerr(0, CMP_R_NULL_ARGUMENT);
  120. return 0;
  121. }
  122. ctx->sendError = val != 0;
  123. return 1;
  124. }
  125. int ossl_cmp_mock_srv_set_pollCount(OSSL_CMP_SRV_CTX *srv_ctx, int count)
  126. {
  127. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  128. if (ctx == NULL) {
  129. CMPerr(0, CMP_R_NULL_ARGUMENT);
  130. return 0;
  131. }
  132. if (count < 0) {
  133. CMPerr(0, CMP_R_INVALID_ARGS);
  134. return 0;
  135. }
  136. ctx->pollCount = count;
  137. return 1;
  138. }
  139. int ossl_cmp_mock_srv_set_checkAfterTime(OSSL_CMP_SRV_CTX *srv_ctx, int sec)
  140. {
  141. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  142. if (ctx == NULL) {
  143. CMPerr(0, CMP_R_NULL_ARGUMENT);
  144. return 0;
  145. }
  146. ctx->checkAfterTime = sec;
  147. return 1;
  148. }
  149. static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
  150. const OSSL_CMP_MSG *cert_req,
  151. int certReqId,
  152. const OSSL_CRMF_MSG *crm,
  153. const X509_REQ *p10cr,
  154. X509 **certOut,
  155. STACK_OF(X509) **chainOut,
  156. STACK_OF(X509) **caPubs)
  157. {
  158. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  159. OSSL_CMP_PKISI *si = NULL;
  160. if (ctx == NULL || cert_req == NULL
  161. || certOut == NULL || chainOut == NULL || caPubs == NULL) {
  162. CMPerr(0, CMP_R_NULL_ARGUMENT);
  163. return NULL;
  164. }
  165. if (ctx->sendError) {
  166. CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
  167. return NULL;
  168. }
  169. *certOut = NULL;
  170. *chainOut = NULL;
  171. *caPubs = NULL;
  172. ctx->certReqId = certReqId;
  173. if (ctx->pollCount > 0) {
  174. ctx->pollCount--;
  175. OSSL_CMP_MSG_free(ctx->certReq);
  176. if ((ctx->certReq = OSSL_CMP_MSG_dup(cert_req)) == NULL)
  177. return NULL;
  178. return OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_waiting, 0, NULL);
  179. }
  180. if (ctx->certOut != NULL
  181. && (*certOut = X509_dup(ctx->certOut)) == NULL)
  182. /* TODO better return a cert produced from data in request template */
  183. goto err;
  184. if (ctx->chainOut != NULL
  185. && (*chainOut = X509_chain_up_ref(ctx->chainOut)) == NULL)
  186. goto err;
  187. if (ctx->caPubsOut != NULL
  188. && (*caPubs = X509_chain_up_ref(ctx->caPubsOut)) == NULL)
  189. goto err;
  190. if (ctx->statusOut != NULL
  191. && (si = OSSL_CMP_PKISI_dup(ctx->statusOut)) == NULL)
  192. goto err;
  193. return si;
  194. err:
  195. X509_free(*certOut);
  196. *certOut = NULL;
  197. sk_X509_pop_free(*chainOut, X509_free);
  198. *chainOut = NULL;
  199. sk_X509_pop_free(*caPubs, X509_free);
  200. *caPubs = NULL;
  201. return NULL;
  202. }
  203. static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
  204. const OSSL_CMP_MSG *rr,
  205. const X509_NAME *issuer,
  206. const ASN1_INTEGER *serial)
  207. {
  208. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  209. if (ctx == NULL || rr == NULL || issuer == NULL || serial == NULL) {
  210. CMPerr(0, CMP_R_NULL_ARGUMENT);
  211. return NULL;
  212. }
  213. if (ctx->sendError || ctx->certOut == NULL) {
  214. CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
  215. return NULL;
  216. }
  217. /* accept revocation only for the certificate we sent in ir/cr/kur */
  218. if (X509_NAME_cmp(issuer, X509_get_issuer_name(ctx->certOut)) != 0
  219. || ASN1_INTEGER_cmp(serial,
  220. X509_get0_serialNumber(ctx->certOut)) != 0) {
  221. CMPerr(0, CMP_R_REQUEST_NOT_ACCEPTED);
  222. return NULL;
  223. }
  224. return OSSL_CMP_PKISI_dup(ctx->statusOut);
  225. }
  226. static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
  227. const OSSL_CMP_MSG *genm,
  228. const STACK_OF(OSSL_CMP_ITAV) *in,
  229. STACK_OF(OSSL_CMP_ITAV) **out)
  230. {
  231. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  232. if (ctx == NULL || genm == NULL || in == NULL || out == NULL) {
  233. CMPerr(0, CMP_R_NULL_ARGUMENT);
  234. return 0;
  235. }
  236. if (ctx->sendError) {
  237. CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
  238. return 0;
  239. }
  240. *out = sk_OSSL_CMP_ITAV_deep_copy(in, OSSL_CMP_ITAV_dup,
  241. OSSL_CMP_ITAV_free);
  242. return *out != NULL;
  243. }
  244. static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
  245. const OSSL_CMP_PKISI *statusInfo,
  246. const ASN1_INTEGER *errorCode,
  247. const OSSL_CMP_PKIFREETEXT *errorDetails)
  248. {
  249. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  250. char buf[OSSL_CMP_PKISI_BUFLEN];
  251. char *sibuf;
  252. int i;
  253. if (ctx == NULL || error == NULL) {
  254. CMPerr(0, CMP_R_NULL_ARGUMENT);
  255. return;
  256. }
  257. BIO_printf(bio_err, "mock server received error:\n");
  258. if (statusInfo == NULL) {
  259. BIO_printf(bio_err, "pkiStatusInfo absent\n");
  260. } else {
  261. sibuf = OSSL_CMP_snprint_PKIStatusInfo(statusInfo, buf, sizeof(buf));
  262. BIO_printf(bio_err, "pkiStatusInfo: %s\n",
  263. sibuf != NULL ? sibuf: "<invalid>");
  264. }
  265. if (errorCode == NULL)
  266. BIO_printf(bio_err, "errorCode absent\n");
  267. else
  268. BIO_printf(bio_err, "errorCode: %ld\n", ASN1_INTEGER_get(errorCode));
  269. if (sk_ASN1_UTF8STRING_num(errorDetails) <= 0) {
  270. BIO_printf(bio_err, "errorDetails absent\n");
  271. } else {
  272. /* TODO could use sk_ASN1_UTF8STRING2text() if exported */
  273. BIO_printf(bio_err, "errorDetails: ");
  274. for (i = 0; i < sk_ASN1_UTF8STRING_num(errorDetails); i++) {
  275. if (i > 0)
  276. BIO_printf(bio_err, ", ");
  277. BIO_printf(bio_err, "\"");
  278. ASN1_STRING_print(bio_err,
  279. sk_ASN1_UTF8STRING_value(errorDetails, i));
  280. BIO_printf(bio_err, "\"");
  281. }
  282. BIO_printf(bio_err, "\n");
  283. }
  284. }
  285. static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
  286. const OSSL_CMP_MSG *certConf, int certReqId,
  287. const ASN1_OCTET_STRING *certHash,
  288. const OSSL_CMP_PKISI *si)
  289. {
  290. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  291. ASN1_OCTET_STRING *digest;
  292. if (ctx == NULL || certConf == NULL || certHash == NULL) {
  293. CMPerr(0, CMP_R_NULL_ARGUMENT);
  294. return 0;
  295. }
  296. if (ctx->sendError || ctx->certOut == NULL) {
  297. CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
  298. return 0;
  299. }
  300. if (certReqId != ctx->certReqId) {
  301. /* in case of error, invalid reqId -1 */
  302. CMPerr(0, CMP_R_BAD_REQUEST_ID);
  303. return 0;
  304. }
  305. if ((digest = X509_digest_sig(ctx->certOut)) == NULL)
  306. return 0;
  307. if (ASN1_OCTET_STRING_cmp(certHash, digest) != 0) {
  308. ASN1_OCTET_STRING_free(digest);
  309. CMPerr(0, CMP_R_CERTHASH_UNMATCHED);
  310. return 0;
  311. }
  312. ASN1_OCTET_STRING_free(digest);
  313. return 1;
  314. }
  315. static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
  316. const OSSL_CMP_MSG *pollReq, int certReqId,
  317. OSSL_CMP_MSG **certReq, int64_t *check_after)
  318. {
  319. mock_srv_ctx *ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx);
  320. if (ctx == NULL || pollReq == NULL
  321. || certReq == NULL || check_after == NULL) {
  322. CMPerr(0, CMP_R_NULL_ARGUMENT);
  323. return 0;
  324. }
  325. if (ctx->sendError || ctx->certReq == NULL) {
  326. *certReq = NULL;
  327. CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
  328. return 0;
  329. }
  330. if (ctx->pollCount == 0) {
  331. *certReq = ctx->certReq;
  332. ctx->certReq = NULL;
  333. *check_after = 0;
  334. } else {
  335. ctx->pollCount--;
  336. *certReq = NULL;
  337. *check_after = ctx->checkAfterTime;
  338. }
  339. return 1;
  340. }
  341. OSSL_CMP_SRV_CTX *ossl_cmp_mock_srv_new(void)
  342. {
  343. OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new();
  344. mock_srv_ctx *ctx = mock_srv_ctx_new();
  345. if (srv_ctx != NULL && ctx != NULL
  346. && OSSL_CMP_SRV_CTX_init(srv_ctx, ctx, process_cert_request,
  347. process_rr, process_genm, process_error,
  348. process_certConf, process_pollReq))
  349. return srv_ctx;
  350. mock_srv_ctx_free(ctx);
  351. OSSL_CMP_SRV_CTX_free(srv_ctx);
  352. return NULL;
  353. }
  354. void ossl_cmp_mock_srv_free(OSSL_CMP_SRV_CTX *srv_ctx)
  355. {
  356. if (srv_ctx != NULL)
  357. mock_srv_ctx_free(OSSL_CMP_SRV_CTX_get0_custom_ctx(srv_ctx));
  358. OSSL_CMP_SRV_CTX_free(srv_ctx);
  359. }