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.
5 #include "chrome/renderer/extensions/platform_keys_natives.h"
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/platform/WebVector.h"
16 #include "third_party/WebKit/public/web/WebCryptoNormalize.h"
18 namespace extensions
{
22 bool StringToWebCryptoOperation(const std::string
& str
,
23 blink::WebCryptoOperation
* op
) {
24 if (str
== "GenerateKey") {
25 *op
= blink::WebCryptoOperationGenerateKey
;
29 *op
= blink::WebCryptoOperationSign
;
32 if (str
== "Verify") {
33 *op
= blink::WebCryptoOperationVerify
;
39 scoped_ptr
<base::DictionaryValue
> WebCryptoAlgorithmToBaseValue(
40 const blink::WebCryptoAlgorithm
& algorithm
) {
41 DCHECK(!algorithm
.isNull());
43 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
44 const blink::WebCryptoAlgorithmInfo
* info
=
45 blink::WebCryptoAlgorithm::lookupAlgorithmInfo(algorithm
.id());
46 dict
->SetStringWithoutPathExpansion("name", info
->name
);
47 const blink::WebCryptoRsaHashedKeyGenParams
* rsaHashedKeyGen
=
48 algorithm
.rsaHashedKeyGenParams();
49 if (rsaHashedKeyGen
) {
50 dict
->SetIntegerWithoutPathExpansion("modulusLength",
51 rsaHashedKeyGen
->modulusLengthBits());
52 const blink::WebVector
<unsigned char>& public_exponent
=
53 rsaHashedKeyGen
->publicExponent();
54 dict
->SetWithoutPathExpansion(
56 base::BinaryValue::CreateWithCopiedBuffer(
57 reinterpret_cast<const char*>(public_exponent
.data()),
58 public_exponent
.size()));
60 const blink::WebCryptoAlgorithm
& hash
= rsaHashedKeyGen
->hash();
61 DCHECK(!hash
.isNull());
62 const blink::WebCryptoAlgorithmInfo
* hash_info
=
63 blink::WebCryptoAlgorithm::lookupAlgorithmInfo(hash
.id());
65 scoped_ptr
<base::DictionaryValue
> hash_dict(new base::DictionaryValue
);
66 hash_dict
->SetStringWithoutPathExpansion("name", hash_info
->name
);
67 dict
->SetWithoutPathExpansion("hash", hash_dict
.release());
69 // Otherwise, |algorithm| is missing support here or no parameters were
76 PlatformKeysNatives::PlatformKeysNatives(ScriptContext
* context
)
77 : ObjectBackedNativeHandler(context
) {
78 RouteFunction("NormalizeAlgorithm",
79 base::Bind(&PlatformKeysNatives::NormalizeAlgorithm
,
80 base::Unretained(this)));
83 void PlatformKeysNatives::NormalizeAlgorithm(
84 const v8::FunctionCallbackInfo
<v8::Value
>& call_info
) {
85 DCHECK_EQ(call_info
.Length(), 2);
86 DCHECK(call_info
[0]->IsObject());
87 DCHECK(call_info
[1]->IsString());
89 blink::WebCryptoOperation operation
;
90 if (!StringToWebCryptoOperation(*v8::String::Utf8Value(call_info
[1]),
95 blink::WebString error_details
;
96 int exception_code
= 0;
98 blink::WebCryptoAlgorithm algorithm
= blink::normalizeCryptoAlgorithm(
99 v8::Local
<v8::Object
>::Cast(call_info
[0]), operation
, &exception_code
,
100 &error_details
, call_info
.GetIsolate());
102 scoped_ptr
<base::DictionaryValue
> algorithm_dict
;
103 if (!algorithm
.isNull())
104 algorithm_dict
= WebCryptoAlgorithmToBaseValue(algorithm
);
109 scoped_ptr
<content::V8ValueConverter
> converter(
110 content::V8ValueConverter::create());
111 call_info
.GetReturnValue().Set(
112 converter
->ToV8Value(algorithm_dict
.get(), context()->v8_context()));
115 } // namespace extensions