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.
 
 
 
 
 
 

269 lines
8.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 <utility>
  64. #include <vector>
  65. #if defined(_MSC_VER)
  66. #pragma warning(pop)
  67. #endif
  68. #include <openssl/bytestring.h>
  69. #include <openssl/crypto.h>
  70. #include <openssl/digest.h>
  71. #include <openssl/err.h>
  72. #include <openssl/evp.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. static int GetKeyType(FileTest *t, const std::string &name) {
  97. if (name == "RSA") {
  98. return EVP_PKEY_RSA;
  99. }
  100. if (name == "EC") {
  101. return EVP_PKEY_EC;
  102. }
  103. if (name == "DSA") {
  104. return EVP_PKEY_DSA;
  105. }
  106. t->PrintLine("Unknown key type: '%s'", name.c_str());
  107. return EVP_PKEY_NONE;
  108. }
  109. using KeyMap = std::map<std::string, ScopedEVP_PKEY>;
  110. static bool ImportKey(FileTest *t, KeyMap *key_map,
  111. EVP_PKEY *(*parse_func)(CBS *cbs),
  112. int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) {
  113. std::vector<uint8_t> input;
  114. if (!t->GetBytes(&input, "Input")) {
  115. return false;
  116. }
  117. CBS cbs;
  118. CBS_init(&cbs, input.data(), input.size());
  119. ScopedEVP_PKEY pkey(parse_func(&cbs));
  120. if (!pkey) {
  121. return false;
  122. }
  123. std::string key_type;
  124. if (!t->GetAttribute(&key_type, "Type")) {
  125. return false;
  126. }
  127. if (EVP_PKEY_id(pkey.get()) != GetKeyType(t, key_type)) {
  128. t->PrintLine("Bad key type.");
  129. return false;
  130. }
  131. // The key must re-encode correctly.
  132. ScopedCBB cbb;
  133. uint8_t *der;
  134. size_t der_len;
  135. if (!CBB_init(cbb.get(), 0) ||
  136. !marshal_func(cbb.get(), pkey.get()) ||
  137. !CBB_finish(cbb.get(), &der, &der_len)) {
  138. return false;
  139. }
  140. ScopedOpenSSLBytes free_der(der);
  141. std::vector<uint8_t> output = input;
  142. if (t->HasAttribute("Output") &&
  143. !t->GetBytes(&output, "Output")) {
  144. return false;
  145. }
  146. if (!t->ExpectBytesEqual(output.data(), output.size(), der, der_len)) {
  147. t->PrintLine("Re-encoding the key did not match.");
  148. return false;
  149. }
  150. // Save the key for future tests.
  151. const std::string &key_name = t->GetParameter();
  152. if (key_map->count(key_name) > 0) {
  153. t->PrintLine("Duplicate key '%s'.", key_name.c_str());
  154. return false;
  155. }
  156. (*key_map)[key_name] = std::move(pkey);
  157. return true;
  158. }
  159. static bool TestEVP(FileTest *t, void *arg) {
  160. KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
  161. if (t->GetType() == "PrivateKey") {
  162. return ImportKey(t, key_map, EVP_parse_private_key,
  163. EVP_marshal_private_key);
  164. }
  165. if (t->GetType() == "PublicKey") {
  166. return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key);
  167. }
  168. int (*key_op_init)(EVP_PKEY_CTX *ctx);
  169. int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  170. const uint8_t *in, size_t in_len);
  171. if (t->GetType() == "Decrypt") {
  172. key_op_init = EVP_PKEY_decrypt_init;
  173. key_op = EVP_PKEY_decrypt;
  174. } else if (t->GetType() == "Sign") {
  175. key_op_init = EVP_PKEY_sign_init;
  176. key_op = EVP_PKEY_sign;
  177. } else if (t->GetType() == "Verify") {
  178. key_op_init = EVP_PKEY_verify_init;
  179. key_op = nullptr; // EVP_PKEY_verify is handled differently.
  180. } else {
  181. t->PrintLine("Unknown test '%s'", t->GetType().c_str());
  182. return false;
  183. }
  184. // Load the key.
  185. const std::string &key_name = t->GetParameter();
  186. if (key_map->count(key_name) == 0) {
  187. t->PrintLine("Could not find key '%s'.", key_name.c_str());
  188. return false;
  189. }
  190. EVP_PKEY *key = (*key_map)[key_name].get();
  191. std::vector<uint8_t> input, output;
  192. if (!t->GetBytes(&input, "Input") ||
  193. !t->GetBytes(&output, "Output")) {
  194. return false;
  195. }
  196. // Set up the EVP_PKEY_CTX.
  197. ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(key, nullptr));
  198. if (!ctx || !key_op_init(ctx.get())) {
  199. return false;
  200. }
  201. if (t->HasAttribute("Digest")) {
  202. const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
  203. if (digest == nullptr ||
  204. !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) {
  205. return false;
  206. }
  207. }
  208. if (t->GetType() == "Verify") {
  209. if (!EVP_PKEY_verify(ctx.get(), output.data(), output.size(), input.data(),
  210. input.size())) {
  211. // ECDSA sometimes doesn't push an error code. Push one on the error queue
  212. // so it's distinguishable from other errors.
  213. OPENSSL_PUT_ERROR(USER, ERR_R_EVP_LIB);
  214. return false;
  215. }
  216. return true;
  217. }
  218. size_t len;
  219. std::vector<uint8_t> actual;
  220. if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
  221. return false;
  222. }
  223. actual.resize(len);
  224. if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size())) {
  225. return false;
  226. }
  227. actual.resize(len);
  228. if (!t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) {
  229. return false;
  230. }
  231. return true;
  232. }
  233. int main(int argc, char **argv) {
  234. CRYPTO_library_init();
  235. if (argc != 2) {
  236. fprintf(stderr, "%s <test file.txt>\n", argv[0]);
  237. return 1;
  238. }
  239. KeyMap map;
  240. return FileTestMain(TestEVP, &map, argv[1]);
  241. }