namemap_internal_test.c 4.3 KB

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