Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

342 řádky
11 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 <gtest/gtest.h>
  66. #include <openssl/bytestring.h>
  67. #include <openssl/crypto.h>
  68. #include <openssl/digest.h>
  69. #include <openssl/err.h>
  70. #include <openssl/rsa.h>
  71. #include "../test/file_test.h"
  72. #include "../test/test_util.h"
  73. // evp_test dispatches between multiple test types. PrivateKey tests take a key
  74. // name parameter and single block, decode it as a PEM private key, and save it
  75. // under that key name. Decrypt, Sign, and Verify tests take a previously
  76. // imported key name as parameter and test their respective operations.
  77. static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
  78. if (name == "MD5") {
  79. return EVP_md5();
  80. } else if (name == "SHA1") {
  81. return EVP_sha1();
  82. } else if (name == "SHA224") {
  83. return EVP_sha224();
  84. } else if (name == "SHA256") {
  85. return EVP_sha256();
  86. } else if (name == "SHA384") {
  87. return EVP_sha384();
  88. } else if (name == "SHA512") {
  89. return EVP_sha512();
  90. }
  91. ADD_FAILURE() << "Unknown digest: " << name;
  92. return nullptr;
  93. }
  94. static int GetKeyType(FileTest *t, const std::string &name) {
  95. if (name == "RSA") {
  96. return EVP_PKEY_RSA;
  97. }
  98. if (name == "EC") {
  99. return EVP_PKEY_EC;
  100. }
  101. if (name == "DSA") {
  102. return EVP_PKEY_DSA;
  103. }
  104. if (name == "Ed25519") {
  105. return EVP_PKEY_ED25519;
  106. }
  107. ADD_FAILURE() << "Unknown key type: " << name;
  108. return EVP_PKEY_NONE;
  109. }
  110. static int GetRSAPadding(FileTest *t, int *out, const std::string &name) {
  111. if (name == "PKCS1") {
  112. *out = RSA_PKCS1_PADDING;
  113. return true;
  114. }
  115. if (name == "PSS") {
  116. *out = RSA_PKCS1_PSS_PADDING;
  117. return true;
  118. }
  119. if (name == "OAEP") {
  120. *out = RSA_PKCS1_OAEP_PADDING;
  121. return true;
  122. }
  123. ADD_FAILURE() << "Unknown RSA padding mode: " << name;
  124. return false;
  125. }
  126. using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>;
  127. static bool ImportKey(FileTest *t, KeyMap *key_map,
  128. EVP_PKEY *(*parse_func)(CBS *cbs),
  129. int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) {
  130. std::vector<uint8_t> input;
  131. if (!t->GetBytes(&input, "Input")) {
  132. return false;
  133. }
  134. CBS cbs;
  135. CBS_init(&cbs, input.data(), input.size());
  136. bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs));
  137. if (!pkey) {
  138. return false;
  139. }
  140. std::string key_type;
  141. if (!t->GetAttribute(&key_type, "Type")) {
  142. return false;
  143. }
  144. EXPECT_EQ(GetKeyType(t, key_type), EVP_PKEY_id(pkey.get()));
  145. // The key must re-encode correctly.
  146. bssl::ScopedCBB cbb;
  147. uint8_t *der;
  148. size_t der_len;
  149. if (!CBB_init(cbb.get(), 0) ||
  150. !marshal_func(cbb.get(), pkey.get()) ||
  151. !CBB_finish(cbb.get(), &der, &der_len)) {
  152. return false;
  153. }
  154. bssl::UniquePtr<uint8_t> free_der(der);
  155. std::vector<uint8_t> output = input;
  156. if (t->HasAttribute("Output") &&
  157. !t->GetBytes(&output, "Output")) {
  158. return false;
  159. }
  160. EXPECT_EQ(Bytes(output), Bytes(der, der_len)) << "Re-encoding the key did not match.";
  161. // Save the key for future tests.
  162. const std::string &key_name = t->GetParameter();
  163. EXPECT_EQ(0u, key_map->count(key_name)) << "Duplicate key: " << key_name;
  164. (*key_map)[key_name] = std::move(pkey);
  165. return true;
  166. }
  167. // SetupContext configures |ctx| based on attributes in |t|, with the exception
  168. // of the signing digest which must be configured externally.
  169. static bool SetupContext(FileTest *t, EVP_PKEY_CTX *ctx) {
  170. if (t->HasAttribute("RSAPadding")) {
  171. int padding;
  172. if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) ||
  173. !EVP_PKEY_CTX_set_rsa_padding(ctx, padding)) {
  174. return false;
  175. }
  176. }
  177. if (t->HasAttribute("PSSSaltLength") &&
  178. !EVP_PKEY_CTX_set_rsa_pss_saltlen(
  179. ctx, atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) {
  180. return false;
  181. }
  182. if (t->HasAttribute("MGF1Digest")) {
  183. const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest"));
  184. if (digest == nullptr || !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, digest)) {
  185. return false;
  186. }
  187. }
  188. return true;
  189. }
  190. static bool TestEVP(FileTest *t, KeyMap *key_map) {
  191. if (t->GetType() == "PrivateKey") {
  192. return ImportKey(t, key_map, EVP_parse_private_key,
  193. EVP_marshal_private_key);
  194. }
  195. if (t->GetType() == "PublicKey") {
  196. return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key);
  197. }
  198. int (*key_op_init)(EVP_PKEY_CTX *ctx) = nullptr;
  199. int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
  200. const uint8_t *in, size_t in_len) = nullptr;
  201. int (*md_op_init)(EVP_MD_CTX * ctx, EVP_PKEY_CTX * *pctx, const EVP_MD *type,
  202. ENGINE *e, EVP_PKEY *pkey) = nullptr;
  203. bool is_verify = false;
  204. if (t->GetType() == "Decrypt") {
  205. key_op_init = EVP_PKEY_decrypt_init;
  206. key_op = EVP_PKEY_decrypt;
  207. } else if (t->GetType() == "Sign") {
  208. key_op_init = EVP_PKEY_sign_init;
  209. key_op = EVP_PKEY_sign;
  210. } else if (t->GetType() == "Verify") {
  211. key_op_init = EVP_PKEY_verify_init;
  212. is_verify = true;
  213. } else if (t->GetType() == "SignMessage") {
  214. md_op_init = EVP_DigestSignInit;
  215. } else if (t->GetType() == "VerifyMessage") {
  216. md_op_init = EVP_DigestVerifyInit;
  217. is_verify = true;
  218. } else {
  219. ADD_FAILURE() << "Unknown test " << t->GetType();
  220. return false;
  221. }
  222. // Load the key.
  223. const std::string &key_name = t->GetParameter();
  224. if (key_map->count(key_name) == 0) {
  225. ADD_FAILURE() << "Could not find key " << key_name;
  226. return false;
  227. }
  228. EVP_PKEY *key = (*key_map)[key_name].get();
  229. const EVP_MD *digest = nullptr;
  230. if (t->HasAttribute("Digest")) {
  231. digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
  232. if (digest == nullptr) {
  233. return false;
  234. }
  235. }
  236. // For verify tests, the "output" is the signature. Read it now so that, for
  237. // tests which expect a failure in SetupContext, the attribute is still
  238. // consumed.
  239. std::vector<uint8_t> input, actual, output;
  240. if (!t->GetBytes(&input, "Input") ||
  241. (is_verify && !t->GetBytes(&output, "Output"))) {
  242. return false;
  243. }
  244. if (md_op_init) {
  245. bssl::ScopedEVP_MD_CTX ctx;
  246. EVP_PKEY_CTX *pctx;
  247. if (!md_op_init(ctx.get(), &pctx, digest, nullptr, key) ||
  248. !SetupContext(t, pctx)) {
  249. return false;
  250. }
  251. if (is_verify) {
  252. return !!EVP_DigestVerify(ctx.get(), output.data(), output.size(),
  253. input.data(), input.size());
  254. }
  255. size_t len;
  256. if (!EVP_DigestSign(ctx.get(), nullptr, &len, input.data(), input.size())) {
  257. return false;
  258. }
  259. actual.resize(len);
  260. if (!EVP_DigestSign(ctx.get(), actual.data(), &len, input.data(),
  261. input.size()) ||
  262. !t->GetBytes(&output, "Output")) {
  263. return false;
  264. }
  265. actual.resize(len);
  266. EXPECT_EQ(Bytes(output), Bytes(actual));
  267. return true;
  268. }
  269. bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr));
  270. if (!ctx ||
  271. !key_op_init(ctx.get()) ||
  272. (digest != nullptr &&
  273. !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) ||
  274. !SetupContext(t, ctx.get())) {
  275. return false;
  276. }
  277. if (is_verify) {
  278. return !!EVP_PKEY_verify(ctx.get(), output.data(), output.size(),
  279. input.data(), input.size());
  280. }
  281. size_t len;
  282. if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
  283. return false;
  284. }
  285. actual.resize(len);
  286. if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size()) ||
  287. !t->GetBytes(&output, "Output")) {
  288. return false;
  289. }
  290. actual.resize(len);
  291. EXPECT_EQ(Bytes(output), Bytes(actual));
  292. return true;
  293. }
  294. TEST(EVPTest, TestVectors) {
  295. KeyMap key_map;
  296. FileTestGTest("crypto/evp/evp_tests.txt", [&](FileTest *t) {
  297. bool result = TestEVP(t, &key_map);
  298. if (t->HasAttribute("Error")) {
  299. ASSERT_FALSE(result) << "Operation unexpectedly succeeded.";
  300. uint32_t err = ERR_peek_error();
  301. EXPECT_EQ(t->GetAttributeOrDie("Error"), ERR_reason_error_string(err));
  302. } else if (!result) {
  303. ADD_FAILURE() << "Operation unexpectedly failed.";
  304. ERR_print_errors_fp(stdout);
  305. }
  306. });
  307. }