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.
 
 
 
 
 
 

302 wiersze
9.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 <openssl/rsa.h>
  70. #include "../test/file_test.h"
  71. // evp_test dispatches between multiple test types. PrivateKey tests take a key
  72. // name parameter and single block, decode it as a PEM private key, and save it
  73. // under that key name. Decrypt, Sign, and Verify tests take a previously
  74. // imported key name as parameter and test their respective operations.
  75. static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
  76. if (name == "MD5") {
  77. return EVP_md5();
  78. } else if (name == "SHA1") {
  79. return EVP_sha1();
  80. } else if (name == "SHA224") {
  81. return EVP_sha224();
  82. } else if (name == "SHA256") {
  83. return EVP_sha256();
  84. } else if (name == "SHA384") {
  85. return EVP_sha384();
  86. } else if (name == "SHA512") {
  87. return EVP_sha512();
  88. }
  89. t->PrintLine("Unknown digest: '%s'", name.c_str());
  90. return nullptr;
  91. }
  92. static int GetKeyType(FileTest *t, const std::string &name) {
  93. if (name == "RSA") {
  94. return EVP_PKEY_RSA;
  95. }
  96. if (name == "EC") {
  97. return EVP_PKEY_EC;
  98. }
  99. if (name == "DSA") {
  100. return EVP_PKEY_DSA;
  101. }
  102. t->PrintLine("Unknown key type: '%s'", name.c_str());
  103. return EVP_PKEY_NONE;
  104. }
  105. static int GetRSAPadding(FileTest *t, int *out, const std::string &name) {
  106. if (name == "PKCS1") {
  107. *out = RSA_PKCS1_PADDING;
  108. return true;
  109. }
  110. if (name == "PSS") {
  111. *out = RSA_PKCS1_PSS_PADDING;
  112. return true;
  113. }
  114. if (name == "OAEP") {
  115. *out = RSA_PKCS1_OAEP_PADDING;
  116. return true;
  117. }
  118. t->PrintLine("Unknown RSA padding mode: '%s'", name.c_str());
  119. return false;
  120. }
  121. using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>;
  122. static bool ImportKey(FileTest *t, KeyMap *key_map,
  123. EVP_PKEY *(*parse_func)(CBS *cbs),
  124. int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) {
  125. std::vector<uint8_t> input;
  126. if (!t->GetBytes(&input, "Input")) {
  127. return false;
  128. }
  129. CBS cbs;
  130. CBS_init(&cbs, input.data(), input.size());
  131. bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs));
  132. if (!pkey) {
  133. return false;
  134. }
  135. std::string key_type;
  136. if (!t->GetAttribute(&key_type, "Type")) {
  137. return false;
  138. }
  139. if (EVP_PKEY_id(pkey.get()) != GetKeyType(t, key_type)) {
  140. t->PrintLine("Bad key type.");
  141. return false;
  142. }
  143. // The key must re-encode correctly.
  144. bssl::ScopedCBB cbb;
  145. uint8_t *der;
  146. size_t der_len;
  147. if (!CBB_init(cbb.get(), 0) ||
  148. !marshal_func(cbb.get(), pkey.get()) ||
  149. !CBB_finish(cbb.get(), &der, &der_len)) {
  150. return false;
  151. }
  152. bssl::UniquePtr<uint8_t> free_der(der);
  153. std::vector<uint8_t> output = input;
  154. if (t->HasAttribute("Output") &&
  155. !t->GetBytes(&output, "Output")) {
  156. return false;
  157. }
  158. if (!t->ExpectBytesEqual(output.data(), output.size(), der, der_len)) {
  159. t->PrintLine("Re-encoding the key did not match.");
  160. return false;
  161. }
  162. // Save the key for future tests.
  163. const std::string &key_name = t->GetParameter();
  164. if (key_map->count(key_name) > 0) {
  165. t->PrintLine("Duplicate key '%s'.", key_name.c_str());
  166. return false;
  167. }
  168. (*key_map)[key_name] = std::move(pkey);
  169. return true;
  170. }
  171. static bool TestEVP(FileTest *t, void *arg) {
  172. KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
  173. if (t->GetType() == "PrivateKey") {
  174. return ImportKey(t, key_map, EVP_parse_private_key,
  175. EVP_marshal_private_key);
  176. }
  177. if (t->GetType() == "PublicKey") {
  178. return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key);
  179. }
  180. int (*key_op_init)(EVP_PKEY_CTX *ctx);
  181. int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  182. const uint8_t *in, size_t in_len);
  183. if (t->GetType() == "Decrypt") {
  184. key_op_init = EVP_PKEY_decrypt_init;
  185. key_op = EVP_PKEY_decrypt;
  186. } else if (t->GetType() == "Sign") {
  187. key_op_init = EVP_PKEY_sign_init;
  188. key_op = EVP_PKEY_sign;
  189. } else if (t->GetType() == "Verify") {
  190. key_op_init = EVP_PKEY_verify_init;
  191. key_op = nullptr; // EVP_PKEY_verify is handled differently.
  192. } else {
  193. t->PrintLine("Unknown test '%s'", t->GetType().c_str());
  194. return false;
  195. }
  196. // Load the key.
  197. const std::string &key_name = t->GetParameter();
  198. if (key_map->count(key_name) == 0) {
  199. t->PrintLine("Could not find key '%s'.", key_name.c_str());
  200. return false;
  201. }
  202. EVP_PKEY *key = (*key_map)[key_name].get();
  203. std::vector<uint8_t> input, output;
  204. if (!t->GetBytes(&input, "Input") ||
  205. !t->GetBytes(&output, "Output")) {
  206. return false;
  207. }
  208. // Set up the EVP_PKEY_CTX.
  209. bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr));
  210. if (!ctx || !key_op_init(ctx.get())) {
  211. return false;
  212. }
  213. if (t->HasAttribute("Digest")) {
  214. const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
  215. if (digest == nullptr ||
  216. !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) {
  217. return false;
  218. }
  219. }
  220. if (t->HasAttribute("RSAPadding")) {
  221. int padding;
  222. if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) ||
  223. !EVP_PKEY_CTX_set_rsa_padding(ctx.get(), padding)) {
  224. return false;
  225. }
  226. }
  227. if (t->HasAttribute("PSSSaltLength") &&
  228. !EVP_PKEY_CTX_set_rsa_pss_saltlen(
  229. ctx.get(), atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) {
  230. return false;
  231. }
  232. if (t->HasAttribute("MGF1Digest")) {
  233. const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest"));
  234. if (digest == nullptr ||
  235. !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), digest)) {
  236. return false;
  237. }
  238. }
  239. if (t->GetType() == "Verify") {
  240. if (!EVP_PKEY_verify(ctx.get(), output.data(), output.size(), input.data(),
  241. input.size())) {
  242. // ECDSA sometimes doesn't push an error code. Push one on the error queue
  243. // so it's distinguishable from other errors.
  244. OPENSSL_PUT_ERROR(USER, ERR_R_EVP_LIB);
  245. return false;
  246. }
  247. return true;
  248. }
  249. size_t len;
  250. std::vector<uint8_t> actual;
  251. if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
  252. return false;
  253. }
  254. actual.resize(len);
  255. if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size())) {
  256. return false;
  257. }
  258. actual.resize(len);
  259. if (!t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) {
  260. return false;
  261. }
  262. return true;
  263. }
  264. int main(int argc, char *argv[]) {
  265. CRYPTO_library_init();
  266. if (argc != 2) {
  267. fprintf(stderr, "%s <test file.txt>\n", argv[0]);
  268. return 1;
  269. }
  270. KeyMap map;
  271. return FileTestMain(TestEVP, &map, argv[1]);
  272. }