namemap_internal_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2019-2021 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/evp.h>
  10. #include "internal/namemap.h"
  11. #include "testutil.h"
  12. #define NAME1 "name1"
  13. #define NAME2 "name2"
  14. #define ALIAS1 "alias1"
  15. #define ALIAS1_UC "ALIAS1"
  16. static int test_namemap_empty(void)
  17. {
  18. OSSL_NAMEMAP *nm = NULL;
  19. int ok;
  20. ok = TEST_int_eq(ossl_namemap_empty(NULL), 1)
  21. && TEST_ptr(nm = ossl_namemap_new())
  22. && TEST_int_eq(ossl_namemap_empty(nm), 1)
  23. && TEST_int_ne(ossl_namemap_add_name(nm, 0, NAME1), 0)
  24. && TEST_int_eq(ossl_namemap_empty(nm), 0);
  25. ossl_namemap_free(nm);
  26. return ok;
  27. }
  28. static int test_namemap(OSSL_NAMEMAP *nm)
  29. {
  30. int num1 = ossl_namemap_add_name(nm, 0, NAME1);
  31. int num2 = ossl_namemap_add_name(nm, 0, NAME2);
  32. int num3 = ossl_namemap_add_name(nm, num1, ALIAS1);
  33. int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC);
  34. int check1 = ossl_namemap_name2num(nm, NAME1);
  35. int check2 = ossl_namemap_name2num(nm, NAME2);
  36. int check3 = ossl_namemap_name2num(nm, ALIAS1);
  37. int check4 = ossl_namemap_name2num(nm, ALIAS1_UC);
  38. int false1 = ossl_namemap_name2num(nm, "cookie");
  39. return TEST_int_ne(num1, 0)
  40. && TEST_int_ne(num2, 0)
  41. && TEST_int_eq(num1, num3)
  42. && TEST_int_eq(num3, num4)
  43. && TEST_int_eq(num1, check1)
  44. && TEST_int_eq(num2, check2)
  45. && TEST_int_eq(num3, check3)
  46. && TEST_int_eq(num4, check4)
  47. && TEST_int_eq(false1, 0);
  48. }
  49. static int test_namemap_independent(void)
  50. {
  51. OSSL_NAMEMAP *nm = ossl_namemap_new();
  52. int ok = TEST_ptr(nm) && test_namemap(nm);
  53. ossl_namemap_free(nm);
  54. return ok;
  55. }
  56. static int test_namemap_stored(void)
  57. {
  58. OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
  59. return TEST_ptr(nm)
  60. && test_namemap(nm);
  61. }
  62. /*
  63. * Test that EVP_get_digestbyname() will use the namemap when it can't find
  64. * entries in the legacy method database.
  65. */
  66. static int test_digestbyname(void)
  67. {
  68. int id;
  69. OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
  70. const EVP_MD *sha256, *foo;
  71. if (!TEST_ptr(nm))
  72. return 0;
  73. id = ossl_namemap_add_name(nm, 0, "SHA256");
  74. if (!TEST_int_ne(id, 0))
  75. return 0;
  76. if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id))
  77. return 0;
  78. sha256 = EVP_get_digestbyname("SHA256");
  79. if (!TEST_ptr(sha256))
  80. return 0;
  81. foo = EVP_get_digestbyname("foo");
  82. if (!TEST_ptr_eq(sha256, foo))
  83. return 0;
  84. return 1;
  85. }
  86. /*
  87. * Test that EVP_get_cipherbyname() will use the namemap when it can't find
  88. * entries in the legacy method database.
  89. */
  90. static int test_cipherbyname(void)
  91. {
  92. int id;
  93. OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
  94. const EVP_CIPHER *aes128, *bar;
  95. if (!TEST_ptr(nm))
  96. return 0;
  97. id = ossl_namemap_add_name(nm, 0, "AES-128-CBC");
  98. if (!TEST_int_ne(id, 0))
  99. return 0;
  100. if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id))
  101. return 0;
  102. aes128 = EVP_get_cipherbyname("AES-128-CBC");
  103. if (!TEST_ptr(aes128))
  104. return 0;
  105. bar = EVP_get_cipherbyname("bar");
  106. if (!TEST_ptr_eq(aes128, bar))
  107. return 0;
  108. return 1;
  109. }
  110. /*
  111. * Test that EVP_CIPHER_is_a() responds appropriately, even for ciphers that
  112. * are entirely legacy.
  113. */
  114. static int test_cipher_is_a(void)
  115. {
  116. EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL);
  117. int rv = 1;
  118. if (!TEST_ptr(fetched))
  119. return 0;
  120. if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM"))
  121. || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM")))
  122. rv = 0;
  123. if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM"))
  124. || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM")))
  125. rv = 0;
  126. EVP_CIPHER_free(fetched);
  127. return rv;
  128. }
  129. /*
  130. * Test that EVP_MD_is_a() responds appropriately, even for MDs that are
  131. * entirely legacy.
  132. */
  133. static int test_digest_is_a(void)
  134. {
  135. EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL);
  136. int rv = 1;
  137. if (!TEST_ptr(fetched))
  138. return 0;
  139. if (!TEST_true(EVP_MD_is_a(fetched, "SHA512"))
  140. || !TEST_false(EVP_MD_is_a(fetched, "SHA1")))
  141. rv = 0;
  142. if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256"))
  143. || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256")))
  144. rv = 0;
  145. EVP_MD_free(fetched);
  146. return rv;
  147. }
  148. int setup_tests(void)
  149. {
  150. ADD_TEST(test_namemap_empty);
  151. ADD_TEST(test_namemap_independent);
  152. ADD_TEST(test_namemap_stored);
  153. ADD_TEST(test_digestbyname);
  154. ADD_TEST(test_cipherbyname);
  155. ADD_TEST(test_digest_is_a);
  156. ADD_TEST(test_cipher_is_a);
  157. return 1;
  158. }