cmp_server_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2007-2020 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 "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_CMP_MSG *request = NULL;
  19. static void tear_down(CMP_SRV_TEST_FIXTURE *fixture)
  20. {
  21. OSSL_CMP_SRV_CTX_free(fixture->srv_ctx);
  22. OPENSSL_free(fixture);
  23. }
  24. static CMP_SRV_TEST_FIXTURE *set_up(const char *const test_case_name)
  25. {
  26. CMP_SRV_TEST_FIXTURE *fixture;
  27. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  28. return NULL;
  29. fixture->test_case_name = test_case_name;
  30. if (!TEST_ptr(fixture->srv_ctx = OSSL_CMP_SRV_CTX_new()))
  31. goto err;
  32. return fixture;
  33. err:
  34. tear_down(fixture);
  35. return NULL;
  36. }
  37. static int dummy_errorCode = CMP_R_MULTIPLE_SAN_SOURCES; /* any reason code */
  38. static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
  39. const OSSL_CMP_MSG *cert_req,
  40. int certReqId,
  41. const OSSL_CRMF_MSG *crm,
  42. const X509_REQ *p10cr,
  43. X509 **certOut,
  44. STACK_OF(X509) **chainOut,
  45. STACK_OF(X509) **caPubs)
  46. {
  47. CMPerr(0, dummy_errorCode);
  48. return NULL;
  49. }
  50. static int execute_test_handle_request(CMP_SRV_TEST_FIXTURE *fixture)
  51. {
  52. OSSL_CMP_SRV_CTX *ctx = fixture->srv_ctx;
  53. OSSL_CMP_CTX *client_ctx;
  54. OSSL_CMP_CTX *cmp_ctx;
  55. char *dummy_custom_ctx = "@test_dummy", *custom_ctx;
  56. OSSL_CMP_MSG *rsp = NULL;
  57. OSSL_CMP_ERRORMSGCONTENT *errorContent;
  58. int res = 0;
  59. if (!TEST_ptr(client_ctx = OSSL_CMP_CTX_new())
  60. || !TEST_true(OSSL_CMP_CTX_set_transfer_cb_arg(client_ctx, ctx)))
  61. goto end;
  62. if (!TEST_true(OSSL_CMP_SRV_CTX_init(ctx, dummy_custom_ctx,
  63. process_cert_request, NULL, NULL,
  64. NULL, NULL, NULL))
  65. || !TEST_ptr(custom_ctx = OSSL_CMP_SRV_CTX_get0_custom_ctx(ctx))
  66. || !TEST_int_eq(strcmp(custom_ctx, dummy_custom_ctx), 0))
  67. goto end;
  68. if (!TEST_true(OSSL_CMP_SRV_CTX_set_send_unprotected_errors(ctx, 0))
  69. || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_unprotected(ctx, 0))
  70. || !TEST_true(OSSL_CMP_SRV_CTX_set_accept_raverified(ctx, 1))
  71. || !TEST_true(OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(ctx, 1)))
  72. goto end;
  73. if (!TEST_ptr(cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(ctx))
  74. || !OSSL_CMP_CTX_set1_referenceValue(cmp_ctx,
  75. (unsigned char *)"server", 6)
  76. || !OSSL_CMP_CTX_set1_secretValue(cmp_ctx,
  77. (unsigned char *)"1234", 4))
  78. goto end;
  79. if (!TEST_ptr(rsp = OSSL_CMP_CTX_server_perform(client_ctx, fixture->req))
  80. || !TEST_int_eq(ossl_cmp_msg_get_bodytype(rsp),
  81. OSSL_CMP_PKIBODY_ERROR)
  82. || !TEST_ptr(errorContent = rsp->body->value.error)
  83. || !TEST_int_eq(ASN1_INTEGER_get(errorContent->errorCode),
  84. dummy_errorCode))
  85. goto end;
  86. res = 1;
  87. end:
  88. OSSL_CMP_MSG_free(rsp);
  89. OSSL_CMP_CTX_free(client_ctx);
  90. return res;
  91. }
  92. static int test_handle_request(void)
  93. {
  94. SETUP_TEST_FIXTURE(CMP_SRV_TEST_FIXTURE, set_up);
  95. fixture->req = request;
  96. fixture->expected = 1;
  97. EXECUTE_TEST(execute_test_handle_request, tear_down);
  98. return result;
  99. }
  100. void cleanup_tests(void)
  101. {
  102. OSSL_CMP_MSG_free(request);
  103. return;
  104. }
  105. int setup_tests(void)
  106. {
  107. const char *request_f;
  108. if (!test_skip_common_options()) {
  109. TEST_error("Error parsing test options\n");
  110. return 0;
  111. }
  112. if (!TEST_ptr(request_f = test_get_argument(0))) {
  113. TEST_error("usage: cmp_server_test CR_protected_PBM_1234.der\n");
  114. return 0;
  115. }
  116. if (!TEST_ptr(request = load_pkimsg(request_f))) {
  117. cleanup_tests();
  118. return 0;
  119. }
  120. /*
  121. * this (indirectly) calls
  122. * OSSL_CMP_SRV_CTX_new(),
  123. * OSSL_CMP_SRV_CTX_free(),
  124. * OSSL_CMP_CTX_server_perform(),
  125. * OSSL_CMP_SRV_process_request(),
  126. * OSSL_CMP_SRV_CTX_init(),
  127. * OSSL_CMP_SRV_CTX_get0_cmp_ctx(),
  128. * OSSL_CMP_SRV_CTX_get0_custom_ctx(),
  129. * OSSL_CMP_SRV_CTX_set_send_unprotected_errors(),
  130. * OSSL_CMP_SRV_CTX_set_accept_unprotected(),
  131. * OSSL_CMP_SRV_CTX_set_accept_raverified(), and
  132. * OSSL_CMP_SRV_CTX_set_grant_implicit_confirm()
  133. */
  134. ADD_TEST(test_handle_request);
  135. return 1;
  136. }