You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

223 lines
7.2 KiB

  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include <stdio.h>
  54. #include <stdint.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #if defined(_MSC_VER)
  58. #pragma warning(push)
  59. #pragma warning(disable: 4702)
  60. #endif
  61. #include <map>
  62. #include <string>
  63. #include <vector>
  64. #if defined(_MSC_VER)
  65. #pragma warning(pop)
  66. #endif
  67. #include <openssl/bio.h>
  68. #include <openssl/crypto.h>
  69. #include <openssl/digest.h>
  70. #include <openssl/err.h>
  71. #include <openssl/evp.h>
  72. #include <openssl/pem.h>
  73. #include "../test/file_test.h"
  74. #include "../test/scoped_types.h"
  75. // evp_test dispatches between multiple test types. PrivateKey tests take a key
  76. // name parameter and single block, decode it as a PEM private key, and save it
  77. // under that key name. Decrypt, Sign, and Verify tests take a previously
  78. // imported key name as parameter and test their respective operations.
  79. static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
  80. if (name == "MD5") {
  81. return EVP_md5();
  82. } else if (name == "SHA1") {
  83. return EVP_sha1();
  84. } else if (name == "SHA224") {
  85. return EVP_sha224();
  86. } else if (name == "SHA256") {
  87. return EVP_sha256();
  88. } else if (name == "SHA384") {
  89. return EVP_sha384();
  90. } else if (name == "SHA512") {
  91. return EVP_sha512();
  92. }
  93. t->PrintLine("Unknown digest: '%s'", name.c_str());
  94. return nullptr;
  95. }
  96. using KeyMap = std::map<std::string, EVP_PKEY*>;
  97. // ImportPrivateKey evaluates a PrivateKey test in |t| and writes the resulting
  98. // private key to |key_map|.
  99. static bool ImportPrivateKey(FileTest *t, KeyMap *key_map) {
  100. const std::string &key_name = t->GetParameter();
  101. if (key_map->count(key_name) > 0) {
  102. t->PrintLine("Duplicate key '%s'.", key_name.c_str());
  103. return false;
  104. }
  105. const std::string &block = t->GetBlock();
  106. ScopedBIO bio(BIO_new_mem_buf(const_cast<char*>(block.data()), block.size()));
  107. if (!bio) {
  108. return false;
  109. }
  110. ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), nullptr, 0, nullptr));
  111. if (!pkey) {
  112. t->PrintLine("Error reading private key.");
  113. return false;
  114. }
  115. (*key_map)[key_name] = pkey.release();
  116. return true;
  117. }
  118. static bool TestEVP(FileTest *t, void *arg) {
  119. KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
  120. if (t->GetType() == "PrivateKey") {
  121. return ImportPrivateKey(t, key_map);
  122. }
  123. int (*key_op_init)(EVP_PKEY_CTX *ctx);
  124. int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  125. const uint8_t *in, size_t in_len);
  126. if (t->GetType() == "Decrypt") {
  127. key_op_init = EVP_PKEY_decrypt_init;
  128. key_op = EVP_PKEY_decrypt;
  129. } else if (t->GetType() == "Sign") {
  130. key_op_init = EVP_PKEY_sign_init;
  131. key_op = EVP_PKEY_sign;
  132. } else if (t->GetType() == "Verify") {
  133. key_op_init = EVP_PKEY_verify_init;
  134. key_op = nullptr; // EVP_PKEY_verify is handled differently.
  135. } else {
  136. t->PrintLine("Unknown test '%s'", t->GetType().c_str());
  137. return false;
  138. }
  139. // Load the key.
  140. const std::string &key_name = t->GetParameter();
  141. if (key_map->count(key_name) == 0) {
  142. t->PrintLine("Could not find key '%s'.", key_name.c_str());
  143. return false;
  144. }
  145. EVP_PKEY *key = (*key_map)[key_name];
  146. std::vector<uint8_t> input, output;
  147. if (!t->GetBytes(&input, "Input") ||
  148. !t->GetBytes(&output, "Output")) {
  149. return false;
  150. }
  151. // Set up the EVP_PKEY_CTX.
  152. ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(key, nullptr));
  153. if (!ctx || !key_op_init(ctx.get())) {
  154. return false;
  155. }
  156. if (t->HasAttribute("Digest")) {
  157. const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
  158. if (digest == nullptr ||
  159. !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) {
  160. return false;
  161. }
  162. }
  163. if (t->GetType() == "Verify") {
  164. if (!EVP_PKEY_verify(ctx.get(), output.data(), output.size(), input.data(),
  165. input.size())) {
  166. // ECDSA sometimes doesn't push an error code. Push one on the error queue
  167. // so it's distinguishable from other errors.
  168. OPENSSL_PUT_ERROR(USER, ERR_R_EVP_LIB);
  169. return false;
  170. }
  171. return true;
  172. }
  173. size_t len;
  174. std::vector<uint8_t> actual;
  175. if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
  176. return false;
  177. }
  178. actual.resize(len);
  179. if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size())) {
  180. return false;
  181. }
  182. actual.resize(len);
  183. if (!t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) {
  184. return false;
  185. }
  186. return true;
  187. }
  188. int main(int argc, char **argv) {
  189. CRYPTO_library_init();
  190. if (argc != 2) {
  191. fprintf(stderr, "%s <test file.txt>\n", argv[0]);
  192. return 1;
  193. }
  194. KeyMap map;
  195. int ret = FileTestMain(TestEVP, &map, argv[1]);
  196. // TODO(davidben): When we can rely on a move-aware std::map, make KeyMap a
  197. // map of ScopedEVP_PKEY instead.
  198. for (const auto &pair : map) {
  199. EVP_PKEY_free(pair.second);
  200. }
  201. return ret;
  202. }