Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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