rsa_oaep_unittest.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include "base/base64url.h"
  7. #include "base/containers/span.h"
  8. #include "components/webcrypto/algorithm_dispatch.h"
  9. #include "components/webcrypto/algorithms/test_helpers.h"
  10. #include "components/webcrypto/jwk.h"
  11. #include "components/webcrypto/status.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
  14. #include "third_party/blink/public/platform/web_crypto_key_algorithm.h"
  15. namespace webcrypto {
  16. namespace {
  17. // Creates an RSA-OAEP algorithm
  18. blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
  19. const std::vector<uint8_t>& label) {
  20. return blink::WebCryptoAlgorithm::AdoptParamsAndCreate(
  21. blink::kWebCryptoAlgorithmIdRsaOaep,
  22. new blink::WebCryptoRsaOaepParams(!label.empty(), label));
  23. }
  24. std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) {
  25. // The JSON web signature spec says that padding is omitted.
  26. // https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-36#section-2
  27. std::string base64url_encoded;
  28. base::Base64UrlEncode(
  29. base::StringPiece(reinterpret_cast<const char*>(input.data()),
  30. input.size()),
  31. base::Base64UrlEncodePolicy::OMIT_PADDING, &base64url_encoded);
  32. return base64url_encoded;
  33. }
  34. base::Value::Dict CreatePublicKeyJwkDict(
  35. base::flat_map<std::string, std::string> extra_properties) {
  36. base::Value::Dict jwk;
  37. jwk.Set("kty", "RSA");
  38. jwk.Set("n", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex)));
  39. jwk.Set("e", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex)));
  40. for (const auto& prop : extra_properties) {
  41. jwk.Set(prop.first, prop.second);
  42. }
  43. return jwk;
  44. }
  45. class WebCryptoRsaOaepTest : public WebCryptoTestBase {};
  46. // Import a PKCS#8 private key that uses RSAPrivateKey with the
  47. // id-rsaEncryption OID.
  48. TEST_F(WebCryptoRsaOaepTest, ImportPkcs8WithRsaEncryption) {
  49. blink::WebCryptoKey private_key;
  50. ASSERT_EQ(Status::Success(),
  51. ImportKey(blink::kWebCryptoKeyFormatPkcs8,
  52. HexStringToBytes(kPrivateKeyPkcs8DerHex),
  53. CreateRsaHashedImportAlgorithm(
  54. blink::kWebCryptoAlgorithmIdRsaOaep,
  55. blink::kWebCryptoAlgorithmIdSha1),
  56. true, blink::kWebCryptoKeyUsageDecrypt, &private_key));
  57. }
  58. TEST_F(WebCryptoRsaOaepTest, ImportPublicJwkWithNoAlg) {
  59. blink::WebCryptoKey public_key;
  60. ASSERT_EQ(
  61. Status::Success(),
  62. ImportKeyJwkFromDict(
  63. CreatePublicKeyJwkDict({}),
  64. CreateRsaHashedImportAlgorithm(blink::kWebCryptoAlgorithmIdRsaOaep,
  65. blink::kWebCryptoAlgorithmIdSha1),
  66. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  67. }
  68. TEST_F(WebCryptoRsaOaepTest, ImportPublicJwkWithMatchingAlg) {
  69. blink::WebCryptoKey public_key;
  70. ASSERT_EQ(
  71. Status::Success(),
  72. ImportKeyJwkFromDict(
  73. CreatePublicKeyJwkDict({{"alg", "RSA-OAEP"}}),
  74. CreateRsaHashedImportAlgorithm(blink::kWebCryptoAlgorithmIdRsaOaep,
  75. blink::kWebCryptoAlgorithmIdSha1),
  76. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  77. }
  78. TEST_F(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedAlgFails) {
  79. blink::WebCryptoKey public_key;
  80. ASSERT_EQ(
  81. Status::ErrorJwkAlgorithmInconsistent(),
  82. ImportKeyJwkFromDict(
  83. CreatePublicKeyJwkDict({{"alg", "RSA-OAEP-512"}}),
  84. CreateRsaHashedImportAlgorithm(blink::kWebCryptoAlgorithmIdRsaOaep,
  85. blink::kWebCryptoAlgorithmIdSha1),
  86. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  87. }
  88. TEST_F(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedTypeFails) {
  89. blink::WebCryptoKey public_key;
  90. ASSERT_EQ(
  91. Status::ErrorJwkUnexpectedKty("RSA"),
  92. ImportKeyJwkFromDict(
  93. CreatePublicKeyJwkDict({{"kty", "oct"}, {"alg", "RSA-OAEP"}}),
  94. CreateRsaHashedImportAlgorithm(blink::kWebCryptoAlgorithmIdRsaOaep,
  95. blink::kWebCryptoAlgorithmIdSha1),
  96. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  97. }
  98. TEST_F(WebCryptoRsaOaepTest, ExportPublicJwk) {
  99. struct TestData {
  100. blink::WebCryptoAlgorithmId hash_alg;
  101. const char* expected_jwk_alg;
  102. } kTestData[] = {{blink::kWebCryptoAlgorithmIdSha1, "RSA-OAEP"},
  103. {blink::kWebCryptoAlgorithmIdSha256, "RSA-OAEP-256"},
  104. {blink::kWebCryptoAlgorithmIdSha384, "RSA-OAEP-384"},
  105. {blink::kWebCryptoAlgorithmIdSha512, "RSA-OAEP-512"}};
  106. for (const auto& test_data : kTestData) {
  107. SCOPED_TRACE(test_data.expected_jwk_alg);
  108. // Import the key in a known-good format
  109. blink::WebCryptoKey public_key;
  110. ASSERT_EQ(Status::Success(),
  111. ImportKeyJwkFromDict(
  112. CreatePublicKeyJwkDict({{"alg", test_data.expected_jwk_alg}}),
  113. CreateRsaHashedImportAlgorithm(
  114. blink::kWebCryptoAlgorithmIdRsaOaep, test_data.hash_alg),
  115. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  116. // Now export the key as JWK and verify its contents
  117. std::vector<uint8_t> jwk_data;
  118. ASSERT_EQ(Status::Success(),
  119. ExportKey(blink::kWebCryptoKeyFormatJwk, public_key, &jwk_data));
  120. EXPECT_TRUE(VerifyPublicJwk(jwk_data, test_data.expected_jwk_alg,
  121. kPublicKeyModulusHex, kPublicKeyExponentHex,
  122. blink::kWebCryptoKeyUsageEncrypt));
  123. }
  124. }
  125. struct RsaOaepKnownAnswer {
  126. const char* pubkey;
  127. const char* privkey;
  128. blink::WebCryptoAlgorithmId hash;
  129. const char* label;
  130. const char* ciphertext;
  131. const char* plaintext;
  132. };
  133. const RsaOaepKnownAnswer kRsaOaepKnownAnswers[] = {
  134. // Tests for RSA-OAEP Encrypt/Decrypt without a label (the empty string).
  135. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  136. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  137. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  138. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  139. "0d0ce8cffb2249bd9a21370203010001",
  140. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  141. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  142. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  143. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  144. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  145. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  146. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  147. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  148. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  149. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  150. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  151. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  152. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  153. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  154. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  155. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  156. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  157. "0acf832b847f13a3d24a79f4d",
  158. blink::kWebCryptoAlgorithmIdSha1, "",
  159. "443FA1354F1DBC5C007A019CACCF18A3E6F986D7513D787F13DED463239F1833D6E33BFA8"
  160. "AF034C198B0D903F202F543FEF38FAF54018EB7794B4FE638CA4D87C564B845C0695D7E70"
  161. "C5D4464BEFF05225E747A6CA096A5E5E634002836D3CCE35713F082B20DCE7BB51324E4C8"
  162. "9E202E3568E9F403ED864DE751BBE63E09680",
  163. "666F6F64"},
  164. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  165. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  166. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  167. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  168. "0d0ce8cffb2249bd9a21370203010001",
  169. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  170. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  171. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  172. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  173. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  174. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  175. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  176. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  177. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  178. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  179. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  180. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  181. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  182. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  183. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  184. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  185. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  186. "0acf832b847f13a3d24a79f4d",
  187. blink::kWebCryptoAlgorithmIdSha256, "",
  188. "4EBEBCA7297864FDEA44B3BF0E4D5AC26A8478E63642CED06182597DF247EC9883E72DFDC"
  189. "4507EE5058C52F6B929FBC34CE1FCF7C4A914D5B0C5C94638BDB0C70618E3F4FE79D29158"
  190. "5DFD3197C4C16B57213337D12E59FC7A38617A1BE3DD2B09DA518E568712D2E6523F079C1"
  191. "6EFCE540CAA829C5E39D62D64902A4E9D0BB3",
  192. "666F6F64"},
  193. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  194. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  195. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  196. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  197. "0d0ce8cffb2249bd9a21370203010001",
  198. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  199. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  200. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  201. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  202. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  203. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  204. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  205. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  206. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  207. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  208. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  209. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  210. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  211. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  212. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  213. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  214. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  215. "0acf832b847f13a3d24a79f4d",
  216. blink::kWebCryptoAlgorithmIdSha384, "",
  217. "67B83119BA513947000F3D7F144408FCBD6B22F93C01671C1E40607972BB991716EAA79B9"
  218. "6FF8DEF95D8E673697E279096D035BA9ED79B9FCAC933A26FB003941CC8E8749511CEDFFA"
  219. "CA653769AB5209E414B80D30A0DC44BE72E49A7FE732819C0DA4142FA0810FFF19E56EFB1"
  220. "4EAA5649398E4C7D920B254CAE54F8019B25A",
  221. "666F6F64"},
  222. // Tests for RSA-OAEP with an optional label specified.
  223. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  224. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  225. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  226. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  227. "0d0ce8cffb2249bd9a21370203010001",
  228. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  229. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  230. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  231. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  232. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  233. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  234. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  235. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  236. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  237. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  238. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  239. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  240. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  241. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  242. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  243. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  244. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  245. "0acf832b847f13a3d24a79f4d",
  246. blink::kWebCryptoAlgorithmIdSha1, "66656564206D65207365796D6F7572",
  247. "A27427C7CB3CD5B7D3C432B3F4BE98577B1FFBF302EDEFF0B219CABAB3E42DF7E8E24F9A8"
  248. "9B387D314B199219F91B7F2CDE8E8D6F461A9495C3B5A398E0F670919EF62CCFF96CCF0FC"
  249. "AF178E5D898C332AAD342F4C42B3209B10CE04B0D004A3ED2514A707D617A321206C22508"
  250. "4C1446BF17208C6D278B0EA3F5968D1D44590",
  251. "666F6F64"},
  252. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  253. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  254. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  255. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  256. "0d0ce8cffb2249bd9a21370203010001",
  257. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  258. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  259. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  260. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  261. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  262. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  263. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  264. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  265. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  266. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  267. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  268. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  269. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  270. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  271. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  272. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  273. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  274. "0acf832b847f13a3d24a79f4d",
  275. blink::kWebCryptoAlgorithmIdSha256, "66656564206D65207365796D6F7572",
  276. "333777A27C14C6186D2E90024507D7BD01D2A23FD462E9DBA6E9E96759A7025F29ABAA40D"
  277. "F6B3355648AE8FD90CD08D9D92529CAD337A6BED806D19B998E472EC0704205A6B83BDB4D"
  278. "4F5DB03FFE8BC68B44CA7F723A032161BD5B6D4517D58FAD8F1859676BC15A3BE9D20D78B"
  279. "11D73F79B23372CF4793F6301F612E820D67A",
  280. "666F6F64"},
  281. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  282. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  283. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  284. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  285. "0d0ce8cffb2249bd9a21370203010001",
  286. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  287. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  288. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  289. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  290. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  291. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  292. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  293. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  294. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  295. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  296. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  297. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  298. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  299. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  300. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  301. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  302. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  303. "0acf832b847f13a3d24a79f4d",
  304. blink::kWebCryptoAlgorithmIdSha384, "66656564206D65207365796D6F7572",
  305. "686D4B27EBD14F4745FB947459DE0D349E53C115F466245268E5C6B8C48FFFDAFB9C77373"
  306. "60D850A6865883ADD516E4AFE71AACE2A73083A4D991293D59199639CF27F6DC0799F7856"
  307. "53C3B1E4213EC277A6FA121F6CD2F81E29F1A35A4604B473983CFEDACB00AA9786E92999D"
  308. "C9FA9AFDBDBB3F0EB3F49B301C5BFE14974BE",
  309. "666F6F64"},
  310. // With RSA-OAEP, an empty message is valid.
  311. {"30819f300d06092a864886f70d010101050003818d0030818902818100a56e4a0e7010175"
  312. "89a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283"
  313. "a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c"
  314. "0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f2"
  315. "0d0ce8cffb2249bd9a21370203010001",
  316. "30820275020100300d06092a864886f70d01010105000482025f3082025b0201000281810"
  317. "0a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056"
  318. "ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8"
  319. "b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b"
  320. "6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d4f545"
  321. "1ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c02"
  322. "66c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a3957"
  323. "4501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6"
  324. "dcee883547c6a3b325024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8"
  325. "6296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda"
  326. "8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48d"
  327. "e8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd024028"
  328. "fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c0777"
  329. "90427bbdd8dd3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa7120"
  330. "49898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e16"
  331. "78255827580a8e4e8e14151d1510a82a3f2e729024027156aba4126d24a81f3a528cbfb27"
  332. "f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb219"
  333. "0acf832b847f13a3d24a79f4d",
  334. blink::kWebCryptoAlgorithmIdSha1, "",
  335. "5E35C080E4A0EBBB5AC8EE44888A6DB6B8C4D05A6427BE0ECBF245A23BCFC4A4A7D9E5466"
  336. "123511399F5D01A3CF910DD6FEBCA969B23C753A0574163D847E70E7EFBFBBF6CC0154556"
  337. "0D620F00CFA33AE318AF6090B76E4ADC5FE1686165786F8B7E559156E8AF690D6EF050133"
  338. "AB5620FAF91B17CF52FED0B57FAC0924F2CC4",
  339. ""},
  340. };
  341. TEST_F(WebCryptoRsaOaepTest, EncryptDecryptKnownAnswerTest) {
  342. for (const auto& test : kRsaOaepKnownAnswers) {
  343. SCOPED_TRACE(&test - &kRsaOaepKnownAnswers[0]);
  344. std::vector<uint8_t> public_key_der = HexStringToBytes(test.pubkey);
  345. std::vector<uint8_t> private_key_der = HexStringToBytes(test.privkey);
  346. std::vector<uint8_t> ciphertext = HexStringToBytes(test.ciphertext);
  347. std::vector<uint8_t> plaintext = HexStringToBytes(test.plaintext);
  348. std::vector<uint8_t> label = HexStringToBytes(test.label);
  349. blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
  350. blink::kWebCryptoAlgorithmIdRsaOaep, test.hash);
  351. blink::WebCryptoKey public_key;
  352. blink::WebCryptoKey private_key;
  353. ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
  354. public_key_der, private_key_der, import_algorithm, false,
  355. blink::kWebCryptoKeyUsageEncrypt, blink::kWebCryptoKeyUsageDecrypt,
  356. &public_key, &private_key));
  357. blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
  358. std::vector<uint8_t> decrypted_data;
  359. ASSERT_EQ(Status::Success(),
  360. Decrypt(op_algorithm, private_key, ciphertext, &decrypted_data));
  361. EXPECT_BYTES_EQ(plaintext, decrypted_data);
  362. std::vector<uint8_t> encrypted_data;
  363. ASSERT_EQ(Status::Success(),
  364. Encrypt(op_algorithm, public_key, plaintext, &encrypted_data));
  365. std::vector<uint8_t> redecrypted_data;
  366. ASSERT_EQ(Status::Success(), Decrypt(op_algorithm, private_key,
  367. encrypted_data, &redecrypted_data));
  368. EXPECT_BYTES_EQ(plaintext, redecrypted_data);
  369. }
  370. }
  371. TEST_F(WebCryptoRsaOaepTest, EncryptWithLargeMessageFails) {
  372. const blink::WebCryptoAlgorithmId kHash = blink::kWebCryptoAlgorithmIdSha1;
  373. const size_t kHashSize = 20;
  374. blink::WebCryptoKey public_key;
  375. ASSERT_EQ(Status::Success(),
  376. ImportKeyJwkFromDict(
  377. CreatePublicKeyJwkDict({}),
  378. CreateRsaHashedImportAlgorithm(
  379. blink::kWebCryptoAlgorithmIdRsaOaep, kHash),
  380. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  381. // The maximum size of an encrypted message is:
  382. // modulus length
  383. // - 1 (leading octet)
  384. // - hash size (maskedSeed)
  385. // - hash size (lHash portion of maskedDB)
  386. // - 1 (at least one octet for the padding string)
  387. size_t kMaxMessageSize = (kModulusLengthBits / 8) - 2 - (2 * kHashSize);
  388. // The label has no influence on the maximum message size. For simplicity,
  389. // use the empty string.
  390. std::vector<uint8_t> label;
  391. blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
  392. // Test that a message just before the boundary succeeds.
  393. std::vector<uint8_t> large_message;
  394. large_message.resize(kMaxMessageSize - 1, 'A');
  395. std::vector<uint8_t> ciphertext;
  396. ASSERT_EQ(Status::Success(),
  397. Encrypt(op_algorithm, public_key, large_message, &ciphertext));
  398. // Test that a message at the boundary succeeds.
  399. large_message.resize(kMaxMessageSize, 'A');
  400. ciphertext.clear();
  401. ASSERT_EQ(Status::Success(),
  402. Encrypt(op_algorithm, public_key, large_message, &ciphertext));
  403. // Test that a message greater than the largest size fails.
  404. large_message.resize(kMaxMessageSize + 1, 'A');
  405. ciphertext.clear();
  406. ASSERT_EQ(Status::OperationError(),
  407. Encrypt(op_algorithm, public_key, large_message, &ciphertext));
  408. }
  409. // Ensures that if the selected hash algorithm for the RSA-OAEP message is too
  410. // large, then it is rejected, independent of the actual message to be
  411. // encrypted.
  412. // For example, a 1024-bit RSA key is too small to accomodate a message that
  413. // uses OAEP with SHA-512, since it requires 1040 bits to encode
  414. // (2 * hash size + 2 padding bytes).
  415. TEST_F(WebCryptoRsaOaepTest, EncryptWithLargeDigestFails) {
  416. const blink::WebCryptoAlgorithmId kHash = blink::kWebCryptoAlgorithmIdSha512;
  417. blink::WebCryptoKey public_key;
  418. ASSERT_EQ(Status::Success(),
  419. ImportKeyJwkFromDict(
  420. CreatePublicKeyJwkDict({}),
  421. CreateRsaHashedImportAlgorithm(
  422. blink::kWebCryptoAlgorithmIdRsaOaep, kHash),
  423. true, blink::kWebCryptoKeyUsageEncrypt, &public_key));
  424. // The label has no influence on the maximum message size. For simplicity,
  425. // use the empty string.
  426. std::vector<uint8_t> label;
  427. blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
  428. std::vector<uint8_t> small_message = {'A'};
  429. std::vector<uint8_t> ciphertext;
  430. // This is an operation error, as the internal consistency checking of the
  431. // algorithm parameters is up to the implementation.
  432. ASSERT_EQ(Status::OperationError(),
  433. Encrypt(op_algorithm, public_key, small_message, &ciphertext));
  434. }
  435. TEST_F(WebCryptoRsaOaepTest, DecryptWithLargeMessageFails) {
  436. blink::WebCryptoKey private_key;
  437. ASSERT_EQ(Status::Success(),
  438. ImportKey(blink::kWebCryptoKeyFormatPkcs8,
  439. HexStringToBytes(kPrivateKeyPkcs8DerHex),
  440. CreateRsaHashedImportAlgorithm(
  441. blink::kWebCryptoAlgorithmIdRsaOaep,
  442. blink::kWebCryptoAlgorithmIdSha1),
  443. true, blink::kWebCryptoKeyUsageDecrypt, &private_key));
  444. // The label has no influence on the maximum message size. For simplicity,
  445. // use the empty string.
  446. std::vector<uint8_t> label;
  447. blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
  448. std::vector<uint8_t> large_dummy_message(kModulusLengthBits / 8, 'A');
  449. std::vector<uint8_t> plaintext;
  450. ASSERT_EQ(Status::OperationError(), Decrypt(op_algorithm, private_key,
  451. large_dummy_message, &plaintext));
  452. }
  453. TEST_F(WebCryptoRsaOaepTest, WrapUnwrapRawKey) {
  454. blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
  455. blink::kWebCryptoAlgorithmIdRsaOaep, blink::kWebCryptoAlgorithmIdSha1);
  456. blink::WebCryptoKey public_key;
  457. blink::WebCryptoKey private_key;
  458. ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
  459. HexStringToBytes(kPublicKeySpkiDerHex),
  460. HexStringToBytes(kPrivateKeyPkcs8DerHex), import_algorithm, false,
  461. blink::kWebCryptoKeyUsageEncrypt | blink::kWebCryptoKeyUsageWrapKey,
  462. blink::kWebCryptoKeyUsageDecrypt | blink::kWebCryptoKeyUsageUnwrapKey,
  463. &public_key, &private_key));
  464. std::vector<uint8_t> label;
  465. blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
  466. const std::string key_hex = "000102030405060708090A0B0C0D0E0F";
  467. const blink::WebCryptoAlgorithm key_algorithm =
  468. CreateAlgorithm(blink::kWebCryptoAlgorithmIdAesCbc);
  469. blink::WebCryptoKey key =
  470. ImportSecretKeyFromRaw(HexStringToBytes(key_hex), key_algorithm,
  471. blink::kWebCryptoKeyUsageEncrypt);
  472. ASSERT_FALSE(key.IsNull());
  473. std::vector<uint8_t> wrapped_key;
  474. ASSERT_EQ(Status::Success(),
  475. WrapKey(blink::kWebCryptoKeyFormatRaw, key, public_key,
  476. wrapping_algorithm, &wrapped_key));
  477. // Verify that |wrapped_key| can be decrypted and yields the key data.
  478. // Because |private_key| supports both decrypt and unwrap, this is valid.
  479. std::vector<uint8_t> decrypted_key;
  480. ASSERT_EQ(Status::Success(), Decrypt(wrapping_algorithm, private_key,
  481. wrapped_key, &decrypted_key));
  482. EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key);
  483. // Now attempt to unwrap the key, which should also decrypt the data.
  484. blink::WebCryptoKey unwrapped_key;
  485. ASSERT_EQ(Status::Success(),
  486. UnwrapKey(blink::kWebCryptoKeyFormatRaw, wrapped_key, private_key,
  487. wrapping_algorithm, key_algorithm, true,
  488. blink::kWebCryptoKeyUsageEncrypt, &unwrapped_key));
  489. ASSERT_FALSE(unwrapped_key.IsNull());
  490. std::vector<uint8_t> raw_key;
  491. ASSERT_EQ(Status::Success(),
  492. ExportKey(blink::kWebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
  493. EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
  494. }
  495. TEST_F(WebCryptoRsaOaepTest, WrapUnwrapJwkSymKey) {
  496. // The public and private portions of a 2048-bit RSA key with the
  497. // id-rsaEncryption OID
  498. const char kPublicKey2048SpkiDerHex[] =
  499. "30820122300d06092a864886f70d01010105000382010f003082010a0282010100c5d8ce"
  500. "137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b300c6a6c9764"
  501. "f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448e7183a3a68"
  502. "e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872458d1b1e2f"
  503. "7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34ba17bc5d08"
  504. "a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea9893652d02fc606"
  505. "36f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d733711c89ca"
  506. "749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b557c16615d"
  507. "5d0203010001";
  508. const char kPrivateKey2048Pkcs8DerHex[] =
  509. "308204bd020100300d06092a864886f70d0101010500048204a7308204a3020100028201"
  510. "0100c5d8ce137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b30"
  511. "0c6a6c9764f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448"
  512. "e7183a3a68e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872"
  513. "458d1b1e2f7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34"
  514. "ba17bc5d08a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea98936"
  515. "52d02fc60636f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d7"
  516. "33711c89ca749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b"
  517. "557c16615d5d02030100010282010074b70feb41a0b0fcbc207670400556c9450042ede3"
  518. "d4383fb1ce8f3558a6d4641d26dd4c333fa4db842d2b9cf9d2354d3e16ad027a9f682d8c"
  519. "f4145a1ad97b9edcd8a41c402bd9d8db10f62f43df854cdccbbb2100834f083f53ed6d42"
  520. "b1b729a59072b004a4e945fc027db15e9c121d1251464d320d4774d5732df6b3dbf751f4"
  521. "9b19c9db201e19989c883bbaad5333db47f64f6f7a95b8d4936b10d945aa3f794cfaab62"
  522. "e7d47686129358914f3b8085f03698a650ab5b8c7e45813f2b0515ec05b6e5195b6a7c2a"
  523. "0d36969745f431ded4fd059f6aa361a4649541016d356297362b778e90f077d48815b339"
  524. "ec6f43aba345df93e67fcb6c2cb5b4544e9be902818100e9c90abe5f9f32468c5b6d630c"
  525. "54a4d7d75e29a72cf792f21e242aac78fd7995c42dfd4ae871d2619ff7096cb05baa78e3"
  526. "23ecab338401a8059adf7a0d8be3b21edc9a9c82c5605634a2ec81ec053271721351868a"
  527. "4c2e50c689d7cef94e31ff23658af5843366e2b289c5bf81d72756a7b93487dd8770d69c"
  528. "1f4e089d6d89f302818100d8a58a727c4e209132afd9933b98c89aca862a01cc0be74133"
  529. "bee517909e5c379e526895ac4af11780c1fe91194c777c9670b6423f0f5a32fd7691a622"
  530. "113eef4bed2ef863363a335fd55b0e75088c582437237d7f3ed3f0a643950237bc6e6277"
  531. "ccd0d0a1b4170aa1047aa7ffa7c8c54be10e8c7327ae2e0885663963817f6f02818100e5"
  532. "aed9ba4d71b7502e6748a1ce247ecb7bd10c352d6d9256031cdf3c11a65e44b0b7ca2945"
  533. "134671195af84c6b3bb3d10ebf65ae916f38bd5dbc59a0ad1c69b8beaf57cb3a8335f19b"
  534. "c7117b576987b48331cd9fd3d1a293436b7bb5e1a35c6560de4b5688ea834367cb0997eb"
  535. "b578f59ed4cb724c47dba94d3b484c1876dcd70281807f15bc7d2406007cac2b138a96af"
  536. "2d1e00276b84da593132c253fcb73212732dfd25824c2a615bc3d9b7f2c8d2fa542d3562"
  537. "b0c7738e61eeff580a6056239fb367ea9e5efe73d4f846033602e90c36a78db6fa8ea792"
  538. "0769675ec58e237bd994d189c8045a96f5dd3a4f12547257ce224e3c9af830a4da3c0eab"
  539. "9227a0035ae9028180067caea877e0b23090fc689322b71fbcce63d6596e66ab5fcdbaa0"
  540. "0d49e93aba8effb4518c2da637f209028401a68f344865b4956b032c69acde51d29177ca"
  541. "3db99fdbf5e74848ed4fa7bdfc2ebb60e2aaa5354770a763e1399ab7a2099762d525fea0"
  542. "37f3e1972c45a477e66db95c9609bb27f862700ef93379930786cf751b";
  543. blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
  544. blink::kWebCryptoAlgorithmIdRsaOaep, blink::kWebCryptoAlgorithmIdSha1);
  545. blink::WebCryptoKey public_key;
  546. blink::WebCryptoKey private_key;
  547. ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
  548. HexStringToBytes(kPublicKey2048SpkiDerHex),
  549. HexStringToBytes(kPrivateKey2048Pkcs8DerHex), import_algorithm, false,
  550. blink::kWebCryptoKeyUsageEncrypt | blink::kWebCryptoKeyUsageWrapKey,
  551. blink::kWebCryptoKeyUsageDecrypt | blink::kWebCryptoKeyUsageUnwrapKey,
  552. &public_key, &private_key));
  553. std::vector<uint8_t> label;
  554. blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
  555. const std::string key_hex = "000102030405060708090a0b0c0d0e0f";
  556. const blink::WebCryptoAlgorithm key_algorithm =
  557. CreateAlgorithm(blink::kWebCryptoAlgorithmIdAesCbc);
  558. blink::WebCryptoKey key =
  559. ImportSecretKeyFromRaw(HexStringToBytes(key_hex), key_algorithm,
  560. blink::kWebCryptoKeyUsageEncrypt);
  561. ASSERT_FALSE(key.IsNull());
  562. std::vector<uint8_t> wrapped_key;
  563. ASSERT_EQ(Status::Success(),
  564. WrapKey(blink::kWebCryptoKeyFormatJwk, key, public_key,
  565. wrapping_algorithm, &wrapped_key));
  566. // Verify that |wrapped_key| can be decrypted and yields a valid JWK object.
  567. // Because |private_key| supports both decrypt and unwrap, this is valid.
  568. std::vector<uint8_t> decrypted_jwk;
  569. ASSERT_EQ(Status::Success(), Decrypt(wrapping_algorithm, private_key,
  570. wrapped_key, &decrypted_jwk));
  571. EXPECT_TRUE(VerifySecretJwk(decrypted_jwk, "A128CBC", key_hex,
  572. blink::kWebCryptoKeyUsageEncrypt));
  573. // Now attempt to unwrap the key, which should also decrypt the data.
  574. blink::WebCryptoKey unwrapped_key;
  575. ASSERT_EQ(Status::Success(),
  576. UnwrapKey(blink::kWebCryptoKeyFormatJwk, wrapped_key, private_key,
  577. wrapping_algorithm, key_algorithm, true,
  578. blink::kWebCryptoKeyUsageEncrypt, &unwrapped_key));
  579. ASSERT_FALSE(unwrapped_key.IsNull());
  580. std::vector<uint8_t> raw_key;
  581. ASSERT_EQ(Status::Success(),
  582. ExportKey(blink::kWebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
  583. EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
  584. }
  585. TEST_F(WebCryptoRsaOaepTest, ImportExportJwkRsaPublicKey) {
  586. struct TestCase {
  587. const blink::WebCryptoAlgorithmId hash;
  588. const blink::WebCryptoKeyUsageMask usage;
  589. const char* const jwk_alg;
  590. };
  591. const TestCase kTests[] = {
  592. {blink::kWebCryptoAlgorithmIdSha1, blink::kWebCryptoKeyUsageEncrypt,
  593. "RSA-OAEP"},
  594. {blink::kWebCryptoAlgorithmIdSha256, blink::kWebCryptoKeyUsageEncrypt,
  595. "RSA-OAEP-256"},
  596. {blink::kWebCryptoAlgorithmIdSha384, blink::kWebCryptoKeyUsageEncrypt,
  597. "RSA-OAEP-384"},
  598. {blink::kWebCryptoAlgorithmIdSha512, blink::kWebCryptoKeyUsageEncrypt,
  599. "RSA-OAEP-512"}};
  600. for (const auto& test : kTests) {
  601. SCOPED_TRACE(&test - &kTests[0]);
  602. const blink::WebCryptoAlgorithm import_algorithm =
  603. CreateRsaHashedImportAlgorithm(blink::kWebCryptoAlgorithmIdRsaOaep,
  604. test.hash);
  605. // Import the spki to create a public key
  606. blink::WebCryptoKey public_key;
  607. ASSERT_EQ(Status::Success(),
  608. ImportKey(blink::kWebCryptoKeyFormatSpki,
  609. HexStringToBytes(kPublicKeySpkiDerHex),
  610. import_algorithm, true, test.usage, &public_key));
  611. // Export the public key as JWK and verify its contents
  612. std::vector<uint8_t> jwk;
  613. ASSERT_EQ(Status::Success(),
  614. ExportKey(blink::kWebCryptoKeyFormatJwk, public_key, &jwk));
  615. EXPECT_TRUE(VerifyPublicJwk(jwk, test.jwk_alg, kPublicKeyModulusHex,
  616. kPublicKeyExponentHex, test.usage));
  617. // Import the JWK back in to create a new key
  618. blink::WebCryptoKey public_key2;
  619. ASSERT_EQ(Status::Success(),
  620. ImportKey(blink::kWebCryptoKeyFormatJwk, jwk, import_algorithm,
  621. true, test.usage, &public_key2));
  622. ASSERT_TRUE(public_key2.Handle());
  623. EXPECT_EQ(blink::kWebCryptoKeyTypePublic, public_key2.GetType());
  624. EXPECT_TRUE(public_key2.Extractable());
  625. EXPECT_EQ(import_algorithm.Id(), public_key2.Algorithm().Id());
  626. // TODO(eroman): Export the SPKI and verify matches.
  627. }
  628. }
  629. } // namespace
  630. } // namespace webcrypto