OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/webcrypto/webcrypto_util.h" | 5 #include "content/child/webcrypto/webcrypto_util.h" |
6 | 6 |
| 7 #include <set> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
9 #include "content/child/webcrypto/status.h" | 11 #include "content/child/webcrypto/status.h" |
10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 | 17 |
16 namespace webcrypto { | 18 namespace webcrypto { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 const JwkToWebCryptoUsage kJwkWebCryptoUsageMap[] = { | 56 const JwkToWebCryptoUsage kJwkWebCryptoUsageMap[] = { |
55 {"encrypt", blink::WebCryptoKeyUsageEncrypt}, | 57 {"encrypt", blink::WebCryptoKeyUsageEncrypt}, |
56 {"decrypt", blink::WebCryptoKeyUsageDecrypt}, | 58 {"decrypt", blink::WebCryptoKeyUsageDecrypt}, |
57 {"sign", blink::WebCryptoKeyUsageSign}, | 59 {"sign", blink::WebCryptoKeyUsageSign}, |
58 {"verify", blink::WebCryptoKeyUsageVerify}, | 60 {"verify", blink::WebCryptoKeyUsageVerify}, |
59 {"deriveKey", blink::WebCryptoKeyUsageDeriveKey}, | 61 {"deriveKey", blink::WebCryptoKeyUsageDeriveKey}, |
60 {"deriveBits", blink::WebCryptoKeyUsageDeriveBits}, | 62 {"deriveBits", blink::WebCryptoKeyUsageDeriveBits}, |
61 {"wrapKey", blink::WebCryptoKeyUsageWrapKey}, | 63 {"wrapKey", blink::WebCryptoKeyUsageWrapKey}, |
62 {"unwrapKey", blink::WebCryptoKeyUsageUnwrapKey}}; | 64 {"unwrapKey", blink::WebCryptoKeyUsageUnwrapKey}}; |
63 | 65 |
64 // Modifies the input usages by according to the key_op value. | |
65 bool JwkKeyOpToWebCryptoUsage(const std::string& key_op, | 66 bool JwkKeyOpToWebCryptoUsage(const std::string& key_op, |
66 blink::WebCryptoKeyUsageMask* usages) { | 67 blink::WebCryptoKeyUsage* usage) { |
67 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { | 68 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { |
68 if (kJwkWebCryptoUsageMap[i].jwk_key_op == key_op) { | 69 if (kJwkWebCryptoUsageMap[i].jwk_key_op == key_op) { |
69 *usages |= kJwkWebCryptoUsageMap[i].webcrypto_usage; | 70 *usage = kJwkWebCryptoUsageMap[i].webcrypto_usage; |
70 return true; | 71 return true; |
71 } | 72 } |
72 } | 73 } |
73 return false; | 74 return false; |
74 } | 75 } |
75 | 76 |
76 // Composes a Web Crypto usage mask from an array of JWK key_ops values. | 77 // Composes a Web Crypto usage mask from an array of JWK key_ops values. |
77 Status GetWebCryptoUsagesFromJwkKeyOps(const base::ListValue* jwk_key_ops_value, | 78 Status GetWebCryptoUsagesFromJwkKeyOps(const base::ListValue* key_ops, |
78 blink::WebCryptoKeyUsageMask* usages) { | 79 blink::WebCryptoKeyUsageMask* usages) { |
| 80 // This set keeps track of all unrecognized key_ops values. |
| 81 std::set<std::string> unrecognized_usages; |
| 82 |
79 *usages = 0; | 83 *usages = 0; |
80 for (size_t i = 0; i < jwk_key_ops_value->GetSize(); ++i) { | 84 for (size_t i = 0; i < key_ops->GetSize(); ++i) { |
81 std::string key_op; | 85 std::string key_op; |
82 if (!jwk_key_ops_value->GetString(i, &key_op)) { | 86 if (!key_ops->GetString(i, &key_op)) { |
83 return Status::ErrorJwkPropertyWrongType( | 87 return Status::ErrorJwkPropertyWrongType( |
84 base::StringPrintf("key_ops[%d]", static_cast<int>(i)), "string"); | 88 base::StringPrintf("key_ops[%d]", static_cast<int>(i)), "string"); |
85 } | 89 } |
86 // Unrecognized key_ops are silently skipped. | 90 |
87 ignore_result(JwkKeyOpToWebCryptoUsage(key_op, usages)); | 91 blink::WebCryptoKeyUsage usage; |
| 92 if (JwkKeyOpToWebCryptoUsage(key_op, &usage)) { |
| 93 // Ensure there are no duplicate usages. |
| 94 if (*usages & usage) |
| 95 return Status::ErrorJwkDuplicateKeyOps(); |
| 96 *usages |= usage; |
| 97 } |
| 98 |
| 99 // Reaching here means the usage was unrecognized. Such usages are skipped |
| 100 // over, however they are kept track of in a set to ensure there were no |
| 101 // duplicates. |
| 102 if (!unrecognized_usages.insert(key_op).second) |
| 103 return Status::ErrorJwkDuplicateKeyOps(); |
88 } | 104 } |
89 return Status::Success(); | 105 return Status::Success(); |
90 } | 106 } |
91 | 107 |
92 // Composes a JWK key_ops List from a Web Crypto usage mask. | 108 // Composes a JWK key_ops List from a Web Crypto usage mask. |
93 // Note: Caller must assume ownership of returned instance. | 109 // Note: Caller must assume ownership of returned instance. |
94 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( | 110 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( |
95 blink::WebCryptoKeyUsageMask usages) { | 111 blink::WebCryptoKeyUsageMask usages) { |
96 base::ListValue* jwk_key_ops = new base::ListValue(); | 112 base::ListValue* jwk_key_ops = new base::ListValue(); |
97 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { | 113 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // avoid feeding OpenSSL data that will hang use a whitelist. | 250 // avoid feeding OpenSSL data that will hang use a whitelist. |
235 if (*public_exponent != 3 && *public_exponent != 65537) | 251 if (*public_exponent != 3 && *public_exponent != 65537) |
236 return Status::ErrorGenerateKeyPublicExponent(); | 252 return Status::ErrorGenerateKeyPublicExponent(); |
237 | 253 |
238 return Status::Success(); | 254 return Status::Success(); |
239 } | 255 } |
240 | 256 |
241 } // namespace webcrypto | 257 } // namespace webcrypto |
242 | 258 |
243 } // namespace content | 259 } // namespace content |
OLD | NEW |