cmp_server_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2020
  4. * Copyright Siemens AG 2015-2020
  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. #include "helpers/cmp_testlib.h"
  12. typedef struct test_fixture {
  13. const char *test_case_name;
  14. int expected;
  15. OSSL_CMP_SRV_CTX *srv_ctx;
  16. OSSL_CMP_MSG *req;
  17. } CMP_SRV_TEST_FIXTURE;
  18. static OSSL_LIB_CTX *libctx = NULL;
  19. static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
  20. static OSSL_CMP_MSG *request = NULL;
  21. static void tear_down(CMP_SRV_TEST_FIXTURE *fixture)
  22. {
  23. OSSL_CMP_SRV_CTX_free(fixture->srv_ctx);
  24. OPENSSL_free(fixture);
  25. }
  26. static CMP_SRV_TEST_FIXTURE *set_up(const char *const test_case_name)
  27. {
  28. CMP_SRV_TEST_FIXTURE *fixture;
  29. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  30. return NULL;
  31. fixture->test_case_name = test_case_name;
  32. if (!TEST_ptr(fixture->srv_ctx = OSSL_CMP_SRV_CTX_new(libctx, NULL)))
  33. goto err;
  34. return fixture;
  35. err:
  36. tear_down(fixture);
  37. return NULL;
  38. }
  39. static int dummy_errorCode = CMP_R_MULTIPLE_SAN_SOURCES; /* any reason code */
  40. static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
  41. const OSSL_CMP_MSG *cert_req,
  42. int certReqId,
  43. const OSSL_CRMF_MSG *crm,
  44. const X509_REQ *p10cr,
  45. X509 **certOut,
  46. STACK_OF(X509) **chainOut,
  47. STACK_OF(X509) **caPubs)
  48. {
  49. ERR_raise(ERR_LIB_CMP, dummy_errorCode);
  50. return NULL;
  51. }
  52. static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE *fixture)
  53. {
  54. OSSL_CMP_SRV_CTX *ctx = fixture->srv_ctx;
  55. OSSL_CMP_CTX *client_ctx;
  56. OSSL_CMP_CTX *cmp_ctx;
  57. char *dummy_custom_ctx = "@test_dummy", *custom_ctx;
  58. OSSL_CMP_MSG *rsp = NULL;
  59. OSSL_CMP_ERRORMSGCONTENT *errorContent;
  60. int res = 0;
  61. if (!TEST_ptr(client_ctx = OSSL_CMP_CTX_new(libctx, NULL))
  62. || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx, ctx)))
  63. goto end;
  64. if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx, dummy_custom_ctx,
  65. process_cert_request, NULL, NULL,
  66. NULL, NULL, NULL))
  67. || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx))
  68. || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0))
  69. goto end;
  70. if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0))
  71. || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0))
  72. || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1))
  73. || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1)))
  74. goto end;
  75. if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx))
  76. || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx,
  77. (unsigned char *)"server", 6)
  78. || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx,
  79. (unsigned char *)"1234", 4))
  80. goto end;
  81. if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req))
  82. || !TEST_int_eq(OSSL_CMP_MSG_get_bodytype(rsp),
  83. OSSL_CMP_PKIBODY_ERROR)
  84. || !TEST_ptr(errorContent = rsp->body->value.error)
  85. || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode),
  86. ERR_PACK(ERR_LIB_CMP, 0, dummy_errorCode)))
  87. goto end;
  88. res = 1;
  89. end:
  90. OSSL_CMP_MSG_free(rsp);
  91. OSSL_CMP_CTX_free(client_ctx);
  92. return res;
  93. }
  94. static int test_handle_request(void)
  95. {
  96. SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up);
  97. fixture->req = request;
  98. fixture->expected = 1;
  99. EXECUTE_TEST(execute_test_handle_request, tear_down);
  100. return result;
  101. }
  102. void cleanup_tests(void)
  103. {
  104. OSSL_CMP_MSG_free(request);
  105. OSSL_PROVIDER_unload(default_null_provider);
  106. OSSL_PROVIDER_unload(provider);
  107. OSSL_LIB_CTX_free(libctx);
  108. return;
  109. }
  110. #define USAGE \
  111. "CR_protected_PBM_1234.der module_name [module_conf_file]\n"
  112. OPT_TEST_DECLARE_USAGE(USAGE)
  113. int setup_tests(void)
  114. {
  115. const char *request_f;
  116. if (!test_skip_common_options()) {
  117. TEST_error("Error parsing test options\n");
  118. return 0;
  119. }
  120. if (!TEST_ptr(request_f = test_get_argument(0))) {
  121. TEST_error("usage: cmp_server_test %s", USAGE);
  122. return 0;
  123. }
  124. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
  125. return 0;
  126. if (!TEST_ptr(request = load_pkimsg(request_f, libctx))) {
  127. cleanup_tests();
  128. return 0;
  129. }
  130. /*
  131. * this (indirectly) calls
  132. * OSSL_CMP_SRV_CTX_new(),
  133. * OSSL_CMP_SRV_CTX_free(),
  134. * OSSL_CMP_CTX_server_perform(),
  135. * OSSL_CMP_SRV_process_request(),
  136. * OSSL_CMP_SRV_CTX_init(),
  137. * OSSL_CMP_SRV_CTX_get0_cmp_ctx(),
  138. * OSSL_CMP_SRV_CTX_get0_custom_ctx(),
  139. * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(),
  140. * OSSL_CMP_SRV_CTX_set_accept_unprotected(),
  141. * OSSL_CMP_SRV_CTX_set_accept_raverified(), and
  142. * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm()
  143. */
  144. ADD_TEST(test_handle_request);
  145. return 1;
  146. }