Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2433)

Unified Diff: content/child/webcrypto/test/rsa_ssa_unittest.cc

Issue 745443002: Check that usage isn't empty when generateKey() is called (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more tests Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/child/webcrypto/test/rsa_ssa_unittest.cc
diff --git a/content/child/webcrypto/test/rsa_ssa_unittest.cc b/content/child/webcrypto/test/rsa_ssa_unittest.cc
index b4ba71660eb91cec92a023d45fade141c0531681..8acecc21f0f86c9c9ff2d073b37c2b615f02cbe6 100644
--- a/content/child/webcrypto/test/rsa_ssa_unittest.cc
+++ b/content/child/webcrypto/test/rsa_ssa_unittest.cc
@@ -345,14 +345,20 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
public_exponent);
bool extractable = true;
const blink::WebCryptoKeyUsageMask usages = 0;
+ const blink::WebCryptoKeyUsageMask public_usages =
+ blink::WebCryptoKeyUsageVerify;
+ const blink::WebCryptoKeyUsageMask private_usages =
+ blink::WebCryptoKeyUsageSign;
blink::WebCryptoKey public_key;
blink::WebCryptoKey private_key;
EXPECT_EQ(Status::Success(),
GenerateKeyPair(
- algorithm, extractable, usages, &public_key, &private_key));
- EXPECT_FALSE(public_key.isNull());
- EXPECT_FALSE(private_key.isNull());
+ algorithm, extractable,
+ public_usages | private_usages,
+ &public_key, &private_key));
+ ASSERT_FALSE(public_key.isNull());
+ ASSERT_FALSE(private_key.isNull());
EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
EXPECT_EQ(modulus_length,
@@ -365,8 +371,8 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
private_key.algorithm().rsaHashedParams()->hash().id());
EXPECT_TRUE(public_key.extractable());
EXPECT_EQ(extractable, private_key.extractable());
- EXPECT_EQ(usages, public_key.usages());
- EXPECT_EQ(usages, private_key.usages());
+ EXPECT_EQ(public_usages, public_key.usages());
+ EXPECT_EQ(private_usages, private_key.usages());
// Try exporting the generated key pair, and then re-importing to verify that
// the exported data was valid.
@@ -377,7 +383,7 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
if (SupportsRsaPrivateKeyImport()) {
public_key = blink::WebCryptoKey::createNull();
- EXPECT_EQ(Status::Success(),
+ ASSERT_EQ(Status::Success(),
ImportKey(blink::WebCryptoKeyFormatSpki,
CryptoData(public_key_spki),
CreateRsaHashedImportAlgorithm(
@@ -395,14 +401,14 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
ExportKey(
blink::WebCryptoKeyFormatPkcs8, private_key, &private_key_pkcs8));
private_key = blink::WebCryptoKey::createNull();
- EXPECT_EQ(Status::Success(),
+ ASSERT_EQ(Status::Success(),
ImportKey(blink::WebCryptoKeyFormatPkcs8,
CryptoData(private_key_pkcs8),
CreateRsaHashedImportAlgorithm(
blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
blink::WebCryptoAlgorithmIdSha256),
true,
- usages,
+ private_usages,
&private_key));
EXPECT_EQ(modulus_length,
private_key.algorithm().rsaHashedParams()->modulusLengthBits());
@@ -462,16 +468,17 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
modulus_length,
exponent_with_leading_zeros);
EXPECT_EQ(Status::Success(),
- GenerateKeyPair(
- algorithm, extractable, usages, &public_key, &private_key));
+ GenerateKeyPair(algorithm, extractable,
+ public_usages | private_usages, &public_key,
+ &private_key));
EXPECT_FALSE(public_key.isNull());
EXPECT_FALSE(private_key.isNull());
EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
EXPECT_TRUE(public_key.extractable());
EXPECT_EQ(extractable, private_key.extractable());
- EXPECT_EQ(usages, public_key.usages());
- EXPECT_EQ(usages, private_key.usages());
+ EXPECT_EQ(public_usages, public_key.usages());
+ EXPECT_EQ(private_usages, private_key.usages());
// Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha1)
algorithm =
@@ -481,7 +488,8 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
public_exponent);
EXPECT_EQ(
Status::Success(),
- GenerateKeyPair(algorithm, false, usages, &public_key, &private_key));
+ GenerateKeyPair(algorithm, false, public_usages | private_usages,
+ &public_key, &private_key));
EXPECT_FALSE(public_key.isNull());
EXPECT_FALSE(private_key.isNull());
EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
@@ -498,8 +506,8 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
// extractable.
EXPECT_TRUE(public_key.extractable());
EXPECT_FALSE(private_key.extractable());
- EXPECT_EQ(usages, public_key.usages());
- EXPECT_EQ(usages, private_key.usages());
+ EXPECT_EQ(public_usages, public_key.usages());
+ EXPECT_EQ(private_usages, private_key.usages());
// Exporting a private key as SPKI format doesn't make sense. However this
// will first fail because the key is not extractable.
@@ -511,7 +519,8 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairRsa) {
// This should fail since spki is for public keys.
EXPECT_EQ(
Status::Success(),
- GenerateKeyPair(algorithm, true, usages, &public_key, &private_key));
+ GenerateKeyPair(algorithm, true, public_usages | private_usages,
+ &public_key, &private_key));
EXPECT_EQ(Status::ErrorUnexpectedKeyType(),
ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output));
}
@@ -892,6 +901,23 @@ TEST(WebCryptoRsaSsaTest, GenerateKeyPairIntersectUsages) {
EXPECT_EQ(blink::WebCryptoKeyUsageSign, private_key.usages());
}
+TEST(WebCryptoRsaSsaTest, GenerateKeyPairEmptyUsages) {
+ const unsigned int modulus_length = 256;
+ const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
+
+ blink::WebCryptoKey public_key;
+ blink::WebCryptoKey private_key;
+
+ ASSERT_EQ(Status::ErrorCreateKeyBadUsages(),
+ GenerateKeyPair(
+ CreateRsaHashedKeyGenAlgorithm(
+ blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
+ blink::WebCryptoAlgorithmIdSha256,
+ modulus_length,
+ public_exponent),
+ true, 0, &public_key, &private_key));
+}
+
TEST(WebCryptoRsaSsaTest, ImportExportJwkRsaPublicKey) {
struct TestCase {
const blink::WebCryptoAlgorithmId hash;
@@ -1051,12 +1077,16 @@ TEST(WebCryptoRsaSsaTest, ImportInvalidKeyData) {
std::string test_error;
ASSERT_TRUE(test->GetString("error", &test_error));
+ blink::WebCryptoKeyUsageMask usages = blink::WebCryptoKeyUsageSign;
+ if (key_format == blink::WebCryptoKeyFormatSpki) {
eroman 2014/11/20 23:40:10 Omit the curlies.
nharper 2014/11/21 22:12:01 Done.
+ usages = blink::WebCryptoKeyUsageVerify;
+ }
blink::WebCryptoKey key;
Status status = ImportKey(key_format, CryptoData(key_data),
CreateRsaHashedImportAlgorithm(
blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
blink::WebCryptoAlgorithmIdSha256),
- true, 0, &key);
+ true, usages, &key);
EXPECT_EQ(test_error, StatusToString(status));
}
}

Powered by Google App Engine
This is Rietveld 408576698