OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #include "chrome/renderer/extensions/enterprise_platform_keys_natives.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/values.h" | |
10 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
11 #include "content/public/renderer/v8_value_converter.h" | |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | |
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
14 #include "third_party/WebKit/public/platform/WebString.h" | |
15 #include "third_party/WebKit/public/web/WebCryptoNormalize.h" | |
16 | |
17 namespace extensions { | |
18 | |
19 namespace { | |
20 | |
21 std::string AlgorithmIdToName(blink::WebCryptoAlgorithmId id) { | |
eroman
2014/06/05 23:38:28
This is a duplication of algorithmIdToName(blink::
pneubeck (no reviews)
2014/06/06 12:39:45
Done.
| |
22 switch (id) { | |
23 case blink::WebCryptoAlgorithmIdHmac: | |
24 return "HMAC"; | |
25 case blink::WebCryptoAlgorithmIdSha1: | |
26 return "SHA-1"; | |
27 case blink::WebCryptoAlgorithmIdAesKw: | |
28 return "AES-KW"; | |
29 case blink::WebCryptoAlgorithmIdSha512: | |
30 return "SHA-512"; | |
31 case blink::WebCryptoAlgorithmIdSha384: | |
32 return "SHA-384"; | |
33 case blink::WebCryptoAlgorithmIdSha256: | |
34 return "SHA-256"; | |
35 case blink::WebCryptoAlgorithmIdAesCbc: | |
36 return "AES-CBC"; | |
37 case blink::WebCryptoAlgorithmIdAesGcm: | |
38 return "AES-GCM"; | |
39 case blink::WebCryptoAlgorithmIdAesCtr: | |
40 return "AES-CTR"; | |
41 case blink::WebCryptoAlgorithmIdRsaOaep: | |
42 return "RSA-OAEP"; | |
43 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | |
44 return "RSASSA-PKCS1-V1_5"; | |
eroman
2014/06/05 23:38:28
Note that the canonicalized name used by the spec
pneubeck (no reviews)
2014/06/06 12:39:45
Done.
| |
45 } | |
46 NOTREACHED(); | |
47 return ""; | |
48 } | |
49 | |
50 bool StringToWebCryptoOperation(const std::string& str, | |
51 blink::WebCryptoOperation* op) { | |
52 if (str == "GenerateKey") { | |
53 *op = blink::WebCryptoOperationGenerateKey; | |
54 return true; | |
55 } | |
56 if (str == "Sign") { | |
57 *op = blink::WebCryptoOperationSign; | |
58 return true; | |
59 } | |
60 if (str == "Verify") { | |
61 *op = blink::WebCryptoOperationVerify; | |
62 return true; | |
63 } | |
64 return false; | |
65 } | |
66 | |
67 scoped_ptr<base::DictionaryValue> WebCryptoAlgorithmToBaseValue( | |
68 const blink::WebCryptoAlgorithm& algorithm) { | |
69 DCHECK(!algorithm.isNull()); | |
70 | |
71 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
72 dict->SetStringWithoutPathExpansion("name", | |
73 AlgorithmIdToName(algorithm.id())); | |
74 const blink::WebCryptoRsaHashedKeyGenParams* rsaHashedKeyGen = | |
75 algorithm.rsaHashedKeyGenParams(); | |
76 if (rsaHashedKeyGen) { | |
77 dict->SetIntegerWithoutPathExpansion("modulusLength", | |
78 rsaHashedKeyGen->modulusLengthBits()); | |
eroman
2014/06/05 23:38:27
What about the hash property?
I am not sure I und
pneubeck (no reviews)
2014/06/06 12:39:45
Like pulic exponent, I'd put that into a follow up
| |
79 return dict.Pass(); | |
80 } | |
81 // Otherwise, |algorithm| is missing support here or no parameters were | |
82 // required. | |
83 return dict.Pass(); | |
84 } | |
85 | |
86 } // namespace | |
87 | |
88 EnterprisePlatformKeysNatives::EnterprisePlatformKeysNatives( | |
89 ScriptContext* context) | |
90 : ObjectBackedNativeHandler(context) { | |
91 RouteFunction("NormalizeAlgorithm", | |
92 base::Bind(&EnterprisePlatformKeysNatives::NormalizeAlgorithm, | |
93 base::Unretained(this))); | |
94 } | |
95 | |
96 void EnterprisePlatformKeysNatives::NormalizeAlgorithm( | |
97 const v8::FunctionCallbackInfo<v8::Value>& call_info) { | |
98 DCHECK_EQ(call_info.Length(), 2); | |
99 DCHECK(call_info[0]->IsObject()); | |
100 DCHECK(call_info[1]->IsString()); | |
101 | |
102 blink::WebCryptoOperation operation; | |
103 if (!StringToWebCryptoOperation(*v8::String::Utf8Value(call_info[1]), | |
104 &operation)) { | |
105 return; | |
106 } | |
107 | |
108 blink::WebString error_details; | |
eroman
2014/06/05 23:38:27
The exception information is being disregarded, is
pneubeck (no reviews)
2014/06/06 12:39:45
I wanted to do another pass to fix error handling
| |
109 int exception_code = 0; | |
110 | |
111 blink::WebCryptoAlgorithm algorithm = | |
112 blink::normalizeCryptoAlgorithm(call_info[0]->ToObject(), | |
113 operation, | |
114 &exception_code, | |
115 &error_details, | |
116 call_info.GetIsolate()); | |
117 | |
118 scoped_ptr<base::DictionaryValue> algorithm_dict; | |
119 if (!algorithm.isNull()) | |
120 algorithm_dict = WebCryptoAlgorithmToBaseValue(algorithm); | |
121 | |
122 if (!algorithm_dict) | |
123 return; | |
124 | |
125 scoped_ptr<content::V8ValueConverter> converter( | |
126 content::V8ValueConverter::create()); | |
127 call_info.GetReturnValue().Set( | |
128 converter->ToV8Value(algorithm_dict.get(), context()->v8_context())); | |
129 } | |
130 | |
131 } // namespace extensions | |
OLD | NEW |