| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 const JwkToWebCryptoUsage kJwkWebCryptoUsageMap[] = { | 54 const JwkToWebCryptoUsage kJwkWebCryptoUsageMap[] = { |
| 55 {"encrypt", blink::WebCryptoKeyUsageEncrypt}, | 55 {"encrypt", blink::WebCryptoKeyUsageEncrypt}, |
| 56 {"decrypt", blink::WebCryptoKeyUsageDecrypt}, | 56 {"decrypt", blink::WebCryptoKeyUsageDecrypt}, |
| 57 {"sign", blink::WebCryptoKeyUsageSign}, | 57 {"sign", blink::WebCryptoKeyUsageSign}, |
| 58 {"verify", blink::WebCryptoKeyUsageVerify}, | 58 {"verify", blink::WebCryptoKeyUsageVerify}, |
| 59 {"deriveKey", blink::WebCryptoKeyUsageDeriveKey}, | 59 {"deriveKey", blink::WebCryptoKeyUsageDeriveKey}, |
| 60 {"deriveBits", blink::WebCryptoKeyUsageDeriveBits}, | 60 {"deriveBits", blink::WebCryptoKeyUsageDeriveBits}, |
| 61 {"wrapKey", blink::WebCryptoKeyUsageWrapKey}, | 61 {"wrapKey", blink::WebCryptoKeyUsageWrapKey}, |
| 62 {"unwrapKey", blink::WebCryptoKeyUsageUnwrapKey}}; | 62 {"unwrapKey", blink::WebCryptoKeyUsageUnwrapKey}}; |
| 63 | 63 |
| 64 // Modifies the input usage_mask by according to the key_op value. | 64 // Modifies the input usages by according to the key_op value. |
| 65 bool JwkKeyOpToWebCryptoUsage(const std::string& key_op, | 65 bool JwkKeyOpToWebCryptoUsage(const std::string& key_op, |
| 66 blink::WebCryptoKeyUsageMask* usage_mask) { | 66 blink::WebCryptoKeyUsageMask* usages) { |
| 67 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { | 67 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { |
| 68 if (kJwkWebCryptoUsageMap[i].jwk_key_op == key_op) { | 68 if (kJwkWebCryptoUsageMap[i].jwk_key_op == key_op) { |
| 69 *usage_mask |= kJwkWebCryptoUsageMap[i].webcrypto_usage; | 69 *usages |= kJwkWebCryptoUsageMap[i].webcrypto_usage; |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 return false; | 73 return false; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Composes a Web Crypto usage mask from an array of JWK key_ops values. | 76 // Composes a Web Crypto usage mask from an array of JWK key_ops values. |
| 77 Status GetWebCryptoUsagesFromJwkKeyOps( | 77 Status GetWebCryptoUsagesFromJwkKeyOps(const base::ListValue* jwk_key_ops_value, |
| 78 const base::ListValue* jwk_key_ops_value, | 78 blink::WebCryptoKeyUsageMask* usages) { |
| 79 blink::WebCryptoKeyUsageMask* usage_mask) { | 79 *usages = 0; |
| 80 *usage_mask = 0; | |
| 81 for (size_t i = 0; i < jwk_key_ops_value->GetSize(); ++i) { | 80 for (size_t i = 0; i < jwk_key_ops_value->GetSize(); ++i) { |
| 82 std::string key_op; | 81 std::string key_op; |
| 83 if (!jwk_key_ops_value->GetString(i, &key_op)) { | 82 if (!jwk_key_ops_value->GetString(i, &key_op)) { |
| 84 return Status::ErrorJwkPropertyWrongType( | 83 return Status::ErrorJwkPropertyWrongType( |
| 85 base::StringPrintf("key_ops[%d]", static_cast<int>(i)), "string"); | 84 base::StringPrintf("key_ops[%d]", static_cast<int>(i)), "string"); |
| 86 } | 85 } |
| 87 // Unrecognized key_ops are silently skipped. | 86 // Unrecognized key_ops are silently skipped. |
| 88 ignore_result(JwkKeyOpToWebCryptoUsage(key_op, usage_mask)); | 87 ignore_result(JwkKeyOpToWebCryptoUsage(key_op, usages)); |
| 89 } | 88 } |
| 90 return Status::Success(); | 89 return Status::Success(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 // Composes a JWK key_ops List from a Web Crypto usage mask. | 92 // Composes a JWK key_ops List from a Web Crypto usage mask. |
| 94 // Note: Caller must assume ownership of returned instance. | 93 // Note: Caller must assume ownership of returned instance. |
| 95 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( | 94 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( |
| 96 blink::WebCryptoKeyUsageMask usage_mask) { | 95 blink::WebCryptoKeyUsageMask usages) { |
| 97 base::ListValue* jwk_key_ops = new base::ListValue(); | 96 base::ListValue* jwk_key_ops = new base::ListValue(); |
| 98 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { | 97 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { |
| 99 if (usage_mask & kJwkWebCryptoUsageMap[i].webcrypto_usage) | 98 if (usages & kJwkWebCryptoUsageMap[i].webcrypto_usage) |
| 100 jwk_key_ops->AppendString(kJwkWebCryptoUsageMap[i].jwk_key_op); | 99 jwk_key_ops->AppendString(kJwkWebCryptoUsageMap[i].jwk_key_op); |
| 101 } | 100 } |
| 102 return jwk_key_ops; | 101 return jwk_key_ops; |
| 103 } | 102 } |
| 104 | 103 |
| 105 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { | 104 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
| 106 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | 105 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
| 107 } | 106 } |
| 108 | 107 |
| 109 blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( | 108 blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 // avoid feeding OpenSSL data that will hang use a whitelist. | 234 // avoid feeding OpenSSL data that will hang use a whitelist. |
| 236 if (*public_exponent != 3 && *public_exponent != 65537) | 235 if (*public_exponent != 3 && *public_exponent != 65537) |
| 237 return Status::ErrorGenerateKeyPublicExponent(); | 236 return Status::ErrorGenerateKeyPublicExponent(); |
| 238 | 237 |
| 239 return Status::Success(); | 238 return Status::Success(); |
| 240 } | 239 } |
| 241 | 240 |
| 242 } // namespace webcrypto | 241 } // namespace webcrypto |
| 243 | 242 |
| 244 } // namespace content | 243 } // namespace content |
| OLD | NEW |