cmp_server_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_true(OSSL_CMP_SRV_CTX_init_trans(ctx, NULL, NULL))
  68. || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx))
  69. || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0))
  70. goto end;
  71. if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0))
  72. || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0))
  73. || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1))
  74. || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1)))
  75. goto end;
  76. if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx))
  77. || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx,
  78. (unsigned char *)"server", 6)
  79. || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx,
  80. (unsigned char *)"1234", 4))
  81. goto end;
  82. if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req))
  83. || !TEST_int_eq(OSSL_CMP_MSG_get_bodytype(rsp),
  84. OSSL_CMP_PKIBODY_ERROR)
  85. || !TEST_ptr(errorContent = rsp->body->value.error)
  86. || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode),
  87. ERR_PACK(ERR_LIB_CMP, 0, dummy_errorCode)))
  88. goto end;
  89. res = 1;
  90. end:
  91. OSSL_CMP_MSG_free(rsp);
  92. OSSL_CMP_CTX_free(client_ctx);
  93. return res;
  94. }
  95. static int test_handle_request(void)
  96. {
  97. SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up);
  98. fixture->req = request;
  99. fixture->expected = 1;
  100. EXECUTE_TEST(execute_test_handle_request, tear_down);
  101. return result;
  102. }
  103. void cleanup_tests(void)
  104. {
  105. OSSL_CMP_MSG_free(request);
  106. OSSL_PROVIDER_unload(default_null_provider);
  107. OSSL_PROVIDER_unload(provider);
  108. OSSL_LIB_CTX_free(libctx);
  109. return;
  110. }
  111. #define USAGE \
  112. "CR_protected_PBM_1234.der module_name [module_conf_file]\n"
  113. OPT_TEST_DECLARE_USAGE(USAGE)
  114. int setup_tests(void)
  115. {
  116. const char *request_f;
  117. if (!test_skip_common_options()) {
  118. TEST_error("Error parsing test options\n");
  119. return 0;
  120. }
  121. if (!TEST_ptr(request_f = test_get_argument(0))) {
  122. TEST_error("usage: cmp_server_test %s", USAGE);
  123. return 0;
  124. }
  125. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 1, USAGE))
  126. return 0;
  127. if (!TEST_ptr(request = load_pkimsg(request_f, libctx))) {
  128. cleanup_tests();
  129. return 0;
  130. }
  131. /*
  132. * this (indirectly) calls
  133. * OSSL_CMP_SRV_CTX_new(),
  134. * OSSL_CMP_SRV_CTX_free(),
  135. * OSSL_CMP_CTX_server_perform(),
  136. * OSSL_CMP_SRV_process_request(),
  137. * OSSL_CMP_SRV_CTX_init(),
  138. * OSSL_CMP_SRV_CTX_get0_cmp_ctx(),
  139. * OSSL_CMP_SRV_CTX_get0_custom_ctx(),
  140. * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(),
  141. * OSSL_CMP_SRV_CTX_set_accept_unprotected(),
  142. * OSSL_CMP_SRV_CTX_set_accept_raverified(), and
  143. * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm()
  144. */
  145. ADD_TEST(test_handle_request);
  146. return 1;
  147. }