Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(891)

Side by Side Diff: chrome/renderer/extensions/enterprise_certificates_natives.cc

Issue 214863002: Extension API enterprise.platformKeys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changed to OO-style API with Token object. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_certificates_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/Platform.h"
13 #include "third_party/WebKit/public/platform/WebCrypto.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
15 #include "third_party/WebKit/public/web/WebScriptBindings.h"
16 #include "v8/include/v8.h"
17
18 namespace extensions {
19
20 namespace {
21
22 void ThrowException(const std::string& error_message, v8::Isolate* isolate) {
23 isolate->ThrowException(v8::Exception::Error(
24 v8::String::NewFromUtf8(isolate, error_message.c_str())));
25 }
26
27 } // namespace
28
29 EnterpriseCertificatesNatives::EnterpriseCertificatesNatives(
30 RequestSender* request_sender,
31 ChromeV8Context* context)
32 : ObjectBackedNativeHandler(context), request_sender_(request_sender) {
33 RouteFunction("ImportNative",
34 base::Bind(&EnterpriseCertificatesNatives::ImportNative,
35 base::Unretained(this)));
36 }
37
38 void EnterpriseCertificatesNatives::ImportNative(
39 const v8::FunctionCallbackInfo<v8::Value>& info) {
40 v8::Isolate* isolate = info.GetIsolate();
41
42 v8::Local<v8::Array> extension_args = info[1].As<v8::Array>();
43 blink::WebCryptoKey key = blink::WebScriptBindings::toWebCryptoKey(
44 extension_args->Get(0)->ToObject(), isolate);
45
46 if (key.isNull()) {
47 ThrowException("Key is not a valid WebCrypto key.", isolate);
48 return;
49 }
50 if (key.type() != blink::WebCryptoKeyTypePrivate) {
51 ThrowException("Key is not a private key.", isolate);
52 return;
53 }
54 if (!(key.usages() & blink::WebCryptoKeyUsageSign)) {
55 ThrowException("Key usage prohibits signing.", isolate);
56 return;
57 }
58
59 blink::WebVector<unsigned char> key_data;
60 if (!blink::Platform::current()->crypto()->serializeKeyForClone(key,
Ryan Sleevi 2014/04/15 00:15:04 You'll want to add comments about why this is OK ;
61 key_data)) {
62 ThrowException("Key cannot be accessed.", isolate);
63 }
64
65 scoped_ptr<content::V8ValueConverter> converter(
66 content::V8ValueConverter::create());
67 converter->SetFunctionAllowed(false);
68 converter->SetStripNullFromObjects(true);
69
70 base::ListValue internal_args;
71 internal_args.Append(base::BinaryValue::CreateWithCopiedBuffer(
72 (char*)key_data.data(), key_data.size()));
Ryan Sleevi 2014/04/15 00:15:04 C++ casts
73 // Copy over the remaining arguments: certificate, token and callback.
74 for (size_t i = 1; i < extension_args->Length(); ++i) {
75 base::Value* arg =
76 converter->FromV8Value(extension_args->Get(i), context()->v8_context());
77 if (arg)
78 internal_args.Append(arg);
79 else
80 internal_args.Append(base::Value::CreateNullValue());
81 }
82
83 const std::string name =
84 "enterprise.certificatesInternal.importClientCertificateAndRawKey";
85 int request_id = info[2]->Int32Value();
86 bool has_callback = info[3]->BooleanValue();
87
88 LOG(ERROR) << "StartRequest";
89 request_sender_->StartRequest(context(),
90 name,
91 request_id,
92 has_callback,
93 false /* for_io_thread */,
94 &internal_args);
95 }
96
97 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698