Chromium Code Reviews| 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 // Lazily initialized since it is unlikely to be needed. | |
| 82 scoped_ptr<std::set<std::string>> unrecognized_usages; | |
|
Ryan Sleevi
2014/10/27 22:19:36
This seems to be premature optimization. Are you s
eroman
2014/10/27 23:29:04
Removed in favor of stack allocation.
Agreed. I d
| |
| 83 | |
| 79 *usages = 0; | 84 *usages = 0; |
| 80 for (size_t i = 0; i < jwk_key_ops_value->GetSize(); ++i) { | 85 for (size_t i = 0; i < key_ops->GetSize(); ++i) { |
| 81 std::string key_op; | 86 std::string key_op; |
| 82 if (!jwk_key_ops_value->GetString(i, &key_op)) { | 87 if (!key_ops->GetString(i, &key_op)) { |
| 83 return Status::ErrorJwkPropertyWrongType( | 88 return Status::ErrorJwkPropertyWrongType( |
| 84 base::StringPrintf("key_ops[%d]", static_cast<int>(i)), "string"); | 89 base::StringPrintf("key_ops[%d]", static_cast<int>(i)), "string"); |
| 85 } | 90 } |
| 86 // Unrecognized key_ops are silently skipped. | 91 |
| 87 ignore_result(JwkKeyOpToWebCryptoUsage(key_op, usages)); | 92 blink::WebCryptoKeyUsage usage; |
| 93 if (JwkKeyOpToWebCryptoUsage(key_op, &usage)) { | |
| 94 // Ensure there are no duplicate usages. | |
| 95 if (*usages & usage) | |
| 96 return Status::ErrorJwkDuplicateKeyOps(); | |
| 97 *usages |= usage; | |
| 98 } | |
| 99 | |
| 100 // Reaching here means the usage was unrecognized. Such usages are skipped | |
| 101 // over, however they are kept track of in a set to ensure there were no | |
| 102 // duplicates. | |
| 103 if (!unrecognized_usages.get()) | |
| 104 unrecognized_usages.reset(new std::set<std::string>); | |
| 105 if (!unrecognized_usages->insert(key_op).second) | |
| 106 return Status::ErrorJwkDuplicateKeyOps(); | |
| 88 } | 107 } |
| 89 return Status::Success(); | 108 return Status::Success(); |
| 90 } | 109 } |
| 91 | 110 |
| 92 // Composes a JWK key_ops List from a Web Crypto usage mask. | 111 // Composes a JWK key_ops List from a Web Crypto usage mask. |
| 93 // Note: Caller must assume ownership of returned instance. | 112 // Note: Caller must assume ownership of returned instance. |
| 94 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( | 113 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( |
| 95 blink::WebCryptoKeyUsageMask usages) { | 114 blink::WebCryptoKeyUsageMask usages) { |
| 96 base::ListValue* jwk_key_ops = new base::ListValue(); | 115 base::ListValue* jwk_key_ops = new base::ListValue(); |
| 97 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { | 116 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. | 253 // avoid feeding OpenSSL data that will hang use a whitelist. |
| 235 if (*public_exponent != 3 && *public_exponent != 65537) | 254 if (*public_exponent != 3 && *public_exponent != 65537) |
| 236 return Status::ErrorGenerateKeyPublicExponent(); | 255 return Status::ErrorGenerateKeyPublicExponent(); |
| 237 | 256 |
| 238 return Status::Success(); | 257 return Status::Success(); |
| 239 } | 258 } |
| 240 | 259 |
| 241 } // namespace webcrypto | 260 } // namespace webcrypto |
| 242 | 261 |
| 243 } // namespace content | 262 } // namespace content |
| OLD | NEW |