upcallstest.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/objects.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/provider.h>
  12. #include "testutil.h"
  13. static const OSSL_ALGORITHM *obj_query(void *provctx, int operation_id,
  14. int *no_cache)
  15. {
  16. *no_cache = 0;
  17. return NULL;
  18. }
  19. static const OSSL_DISPATCH obj_dispatch_table[] = {
  20. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))obj_query },
  21. { 0, NULL }
  22. };
  23. static OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid = NULL;
  24. static OSSL_FUNC_core_obj_create_fn *c_obj_create = NULL;
  25. /* test signature ids requiring digest */
  26. #define SIG_OID "1.3.6.1.4.1.16604.998877.1"
  27. #define SIG_SN "my-sig"
  28. #define SIG_LN "my-sig-long"
  29. #define DIGEST_OID "1.3.6.1.4.1.16604.998877.2"
  30. #define DIGEST_SN "my-digest"
  31. #define DIGEST_LN "my-digest-long"
  32. #define SIGALG_OID "1.3.6.1.4.1.16604.998877.3"
  33. #define SIGALG_SN "my-sigalg"
  34. #define SIGALG_LN "my-sigalg-long"
  35. /* test signature ids requiring no digest */
  36. #define NODIG_SIG_OID "1.3.6.1.4.1.16604.998877.4"
  37. #define NODIG_SIG_SN "my-nodig-sig"
  38. #define NODIG_SIG_LN "my-nodig-sig-long"
  39. #define NODIG_SIGALG_OID "1.3.6.1.4.1.16604.998877.5"
  40. #define NODIG_SIGALG_SN "my-nodig-sigalg"
  41. #define NODIG_SIGALG_LN "my-nodig-sigalg-long"
  42. static int obj_provider_init(const OSSL_CORE_HANDLE *handle,
  43. const OSSL_DISPATCH *in,
  44. const OSSL_DISPATCH **out,
  45. void **provctx)
  46. {
  47. *provctx = (void *)handle;
  48. *out = obj_dispatch_table;
  49. for (; in->function_id != 0; in++) {
  50. switch (in->function_id) {
  51. case OSSL_FUNC_CORE_OBJ_ADD_SIGID:
  52. c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in);
  53. break;
  54. case OSSL_FUNC_CORE_OBJ_CREATE:
  55. c_obj_create = OSSL_FUNC_core_obj_create(in);
  56. break;
  57. break;
  58. default:
  59. /* Just ignore anything we don't understand */
  60. break;
  61. }
  62. }
  63. if (!c_obj_create(handle, DIGEST_OID, DIGEST_SN, DIGEST_LN)
  64. || !c_obj_create(handle, SIG_OID, SIG_SN, SIG_LN)
  65. || !c_obj_create(handle, SIGALG_OID, SIGALG_SN, SIGALG_LN))
  66. return 0;
  67. if (!c_obj_create(handle, NODIG_SIG_OID, NODIG_SIG_SN, NODIG_SIG_LN)
  68. || !c_obj_create(handle, NODIG_SIGALG_OID, NODIG_SIGALG_SN, NODIG_SIGALG_LN))
  69. return 0;
  70. if (!c_obj_add_sigid(handle, SIGALG_OID, DIGEST_SN, SIG_LN))
  71. return 0;
  72. /* additional tests checking empty digest algs are accepted, too */
  73. if (!c_obj_add_sigid(handle, NODIG_SIGALG_OID, "", NODIG_SIG_LN))
  74. return 0;
  75. /* checking wrong digest alg name is rejected: */
  76. if (c_obj_add_sigid(handle, NODIG_SIGALG_OID, "NonsenseAlg", NODIG_SIG_LN))
  77. return 0;
  78. return 1;
  79. }
  80. static int obj_create_test(void)
  81. {
  82. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  83. OSSL_PROVIDER *objprov = NULL;
  84. int sigalgnid, digestnid, signid, foundsid;
  85. int testresult = 0;
  86. if (!TEST_ptr(libctx))
  87. goto err;
  88. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "obj-prov",
  89. obj_provider_init))
  90. || !TEST_ptr(objprov = OSSL_PROVIDER_load(libctx, "obj-prov")))
  91. goto err;
  92. /* Check that the provider created the OIDs/NIDs we expected */
  93. sigalgnid = OBJ_txt2nid(SIGALG_OID);
  94. if (!TEST_int_ne(sigalgnid, NID_undef)
  95. || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
  96. || !TEST_int_ne(digestnid, NID_undef)
  97. || !TEST_int_ne(signid, NID_undef)
  98. || !TEST_int_eq(digestnid, OBJ_sn2nid(DIGEST_SN))
  99. || !TEST_int_eq(signid, OBJ_ln2nid(SIG_LN)))
  100. goto err;
  101. /* Check empty digest alg storage capability */
  102. sigalgnid = OBJ_txt2nid(NODIG_SIGALG_OID);
  103. if (!TEST_int_ne(sigalgnid, NID_undef)
  104. || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
  105. || !TEST_int_eq(digestnid, NID_undef)
  106. || !TEST_int_ne(signid, NID_undef))
  107. goto err;
  108. /* Testing OBJ_find_sigid_by_algs */
  109. /* First check exact sig/digest recall: */
  110. sigalgnid = OBJ_sn2nid(SIGALG_SN);
  111. digestnid = OBJ_sn2nid(DIGEST_SN);
  112. signid = OBJ_ln2nid(SIG_LN);
  113. if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) ||
  114. (foundsid != sigalgnid))
  115. return 0;
  116. /* Check wrong signature/digest combination is rejected */
  117. if ((OBJ_find_sigid_by_algs(&foundsid, OBJ_sn2nid("SHA512"), signid)) &&
  118. (foundsid == sigalgnid))
  119. return 0;
  120. /* Now also check signature not needing digest is found */
  121. /* a) when some digest is given */
  122. sigalgnid = OBJ_sn2nid(NODIG_SIGALG_SN);
  123. digestnid = OBJ_sn2nid("SHA512");
  124. signid = OBJ_ln2nid(NODIG_SIG_LN);
  125. if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) ||
  126. (foundsid != sigalgnid))
  127. return 0;
  128. /* b) when NID_undef is passed */
  129. digestnid = NID_undef;
  130. if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) ||
  131. (foundsid != sigalgnid))
  132. return 0;
  133. testresult = 1;
  134. err:
  135. OSSL_PROVIDER_unload(objprov);
  136. OSSL_LIB_CTX_free(libctx);
  137. return testresult;
  138. }
  139. int setup_tests(void)
  140. {
  141. ADD_TEST(obj_create_test);
  142. return 1;
  143. }