No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

263 líneas
8.6 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. #include <map>
  58. #include <string>
  59. #include <vector>
  60. #include <openssl/bio.h>
  61. #include <openssl/crypto.h>
  62. #include <openssl/digest.h>
  63. #include <openssl/err.h>
  64. #include <openssl/evp.h>
  65. #include <openssl/pem.h>
  66. #include "../test/file_test.h"
  67. #include "../test/scoped_types.h"
  68. #include "../test/stl_compat.h"
  69. // evp_test dispatches between multiple test types. HMAC tests test the legacy
  70. // EVP_PKEY_HMAC API. PrivateKey tests take a key name parameter and single
  71. // block, decode it as a PEM private key, and save it under that key name.
  72. // Decrypt, Sign, and Verify tests take a previously imported key name as
  73. // parameter and test their respective operations.
  74. static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
  75. if (name == "MD5") {
  76. return EVP_md5();
  77. } else if (name == "SHA1") {
  78. return EVP_sha1();
  79. } else if (name == "SHA224") {
  80. return EVP_sha224();
  81. } else if (name == "SHA256") {
  82. return EVP_sha256();
  83. } else if (name == "SHA384") {
  84. return EVP_sha384();
  85. } else if (name == "SHA512") {
  86. return EVP_sha512();
  87. }
  88. t->PrintLine("Unknown digest: '%s'", name.c_str());
  89. return nullptr;
  90. }
  91. using KeyMap = std::map<std::string, EVP_PKEY*>;
  92. // ImportPrivateKey evaluates a PrivateKey test in |t| and writes the resulting
  93. // private key to |key_map|.
  94. static bool ImportPrivateKey(FileTest *t, KeyMap *key_map) {
  95. const std::string &key_name = t->GetParameter();
  96. if (key_map->count(key_name) > 0) {
  97. t->PrintLine("Duplicate key '%s'.", key_name.c_str());
  98. return false;
  99. }
  100. const std::string &block = t->GetBlock();
  101. ScopedBIO bio(BIO_new_mem_buf(const_cast<char*>(block.data()), block.size()));
  102. if (!bio) {
  103. return false;
  104. }
  105. ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), nullptr, 0, nullptr));
  106. if (!pkey) {
  107. t->PrintLine("Error reading private key.");
  108. return false;
  109. }
  110. (*key_map)[key_name] = pkey.release();
  111. return true;
  112. }
  113. static bool TestHMAC(FileTest *t) {
  114. std::string digest_str;
  115. if (!t->GetAttribute(&digest_str, "HMAC")) {
  116. return false;
  117. }
  118. const EVP_MD *digest = GetDigest(t, digest_str);
  119. if (digest == nullptr) {
  120. return false;
  121. }
  122. std::vector<uint8_t> key, input, output;
  123. if (!t->GetBytes(&key, "Key") ||
  124. !t->GetBytes(&input, "Input") ||
  125. !t->GetBytes(&output, "Output")) {
  126. return false;
  127. }
  128. ScopedEVP_PKEY pkey(EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, nullptr,
  129. bssl::vector_data(&key),
  130. key.size()));
  131. ScopedEVP_MD_CTX mctx;
  132. if (!pkey ||
  133. !EVP_DigestSignInit(mctx.get(), nullptr, digest, nullptr, pkey.get()) ||
  134. !EVP_DigestSignUpdate(mctx.get(), bssl::vector_data(&input),
  135. input.size())) {
  136. return false;
  137. }
  138. size_t len;
  139. std::vector<uint8_t> actual;
  140. if (!EVP_DigestSignFinal(mctx.get(), nullptr, &len)) {
  141. return false;
  142. }
  143. actual.resize(len);
  144. if (!EVP_DigestSignFinal(mctx.get(), bssl::vector_data(&actual), &len)) {
  145. return false;
  146. }
  147. actual.resize(len);
  148. return t->ExpectBytesEqual(bssl::vector_data(&output), output.size(),
  149. bssl::vector_data(&actual), actual.size());
  150. }
  151. static bool TestEVP(FileTest *t, void *arg) {
  152. KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
  153. if (t->GetType() == "PrivateKey") {
  154. return ImportPrivateKey(t, key_map);
  155. } else if (t->GetType() == "HMAC") {
  156. return TestHMAC(t);
  157. }
  158. int (*key_op_init)(EVP_PKEY_CTX *ctx);
  159. int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  160. const uint8_t *in, size_t in_len);
  161. if (t->GetType() == "Decrypt") {
  162. key_op_init = EVP_PKEY_decrypt_init;
  163. key_op = EVP_PKEY_decrypt;
  164. } else if (t->GetType() == "Sign") {
  165. key_op_init = EVP_PKEY_sign_init;
  166. key_op = EVP_PKEY_sign;
  167. } else if (t->GetType() == "Verify") {
  168. key_op_init = EVP_PKEY_verify_init;
  169. key_op = nullptr; // EVP_PKEY_verify is handled differently.
  170. } else {
  171. t->PrintLine("Unknown test '%s'", t->GetType().c_str());
  172. return false;
  173. }
  174. // Load the key.
  175. const std::string &key_name = t->GetParameter();
  176. if (key_map->count(key_name) == 0) {
  177. t->PrintLine("Could not find key '%s'.", key_name.c_str());
  178. return false;
  179. }
  180. EVP_PKEY *key = (*key_map)[key_name];
  181. std::vector<uint8_t> input, output;
  182. if (!t->GetBytes(&input, "Input") ||
  183. !t->GetBytes(&output, "Output")) {
  184. return false;
  185. }
  186. // Set up the EVP_PKEY_CTX.
  187. ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(key, nullptr));
  188. if (!ctx || !key_op_init(ctx.get())) {
  189. return false;
  190. }
  191. if (t->HasAttribute("Digest")) {
  192. const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
  193. if (digest == nullptr ||
  194. !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) {
  195. return false;
  196. }
  197. }
  198. if (t->GetType() == "Verify") {
  199. if (!EVP_PKEY_verify(ctx.get(), bssl::vector_data(&output), output.size(),
  200. bssl::vector_data(&input), input.size())) {
  201. // ECDSA sometimes doesn't push an error code. Push one on the error queue
  202. // so it's distinguishable from other errors.
  203. ERR_put_error(ERR_LIB_USER, 0, ERR_R_EVP_LIB, __FILE__, __LINE__);
  204. return false;
  205. }
  206. return true;
  207. }
  208. size_t len;
  209. std::vector<uint8_t> actual;
  210. if (!key_op(ctx.get(), nullptr, &len, bssl::vector_data(&input),
  211. input.size())) {
  212. return false;
  213. }
  214. actual.resize(len);
  215. if (!key_op(ctx.get(), bssl::vector_data(&actual), &len,
  216. bssl::vector_data(&input), input.size())) {
  217. return false;
  218. }
  219. actual.resize(len);
  220. if (!t->ExpectBytesEqual(bssl::vector_data(&output), output.size(),
  221. bssl::vector_data(&actual), len)) {
  222. return false;
  223. }
  224. return true;
  225. }
  226. int main(int argc, char **argv) {
  227. CRYPTO_library_init();
  228. if (argc != 2) {
  229. fprintf(stderr, "%s <test file.txt>\n", argv[0]);
  230. return 1;
  231. }
  232. KeyMap map;
  233. int ret = FileTestMain(TestEVP, &map, argv[1]);
  234. // TODO(davidben): When we can rely on a move-aware std::map, make KeyMap a
  235. // map of ScopedEVP_PKEY instead.
  236. for (const auto &pair : map) {
  237. EVP_PKEY_free(pair.second);
  238. }
  239. return ret;
  240. }