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

Side by Side Diff: chrome/renderer/resources/extensions/enterprise_platform_keys_custom_bindings.js

Issue 214863002: Extension API enterprise.platformKeys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changed to WebCrypto like API. Created 6 years, 7 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 // Custom binding for the enterprise.platformKeys API.
6
not at google - send to devlin 2014/05/02 18:31:56 could you explain what is going on here? there is
pneubeck (no reviews) 2014/05/05 20:09:35 Done.
7 var binding = require('binding').Binding.create('enterprise.platformKeys');
8 var internalAPI = require('binding')
9 .Binding.create('enterprise.platformKeysInternal')
10 .generate();
11
12 function intersect(a, b) {
13 var result = [];
14 for (var i = 0; i < a.length; i++) {
15 if (b.indexOf(a[i]) >= 0)
16 result.push(a[i]);
17 }
18 return result;
19 };
20
21 // Key class definition.
22 var Key = function(type, publicKeySpki, algorithm, keyUsages) {
23 privates(this).type_ = type;
not at google - send to devlin 2014/05/02 18:31:56 a nice idiom for privates(..) is to have a "KeyImp
pneubeck (no reviews) 2014/05/05 20:09:35 Done.
24 privates(this).spki_ = publicKeySpki;
25 privates(this).algorithm_ = algorithm;
26 privates(this).keyUsages_ = keyUsages;
27 };
28
29 Object.defineProperty(Key.prototype, 'type', {
30 get: function() {
31 return privates(this).type_;
32 },
33 enumerable : true
34 });
35
36 // PublicKey class definition.
37 var PublicKey = function(publicKeySpki, algorithm, keyUsages) {
38 Key.call(this, "public", publicKeySpki, algorithm, keyUsages);
39 };
40 PublicKey.prototype.__proto__ = Key.prototype;
41
42 Object.defineProperty(PublicKey.prototype, 'extractable', {
43 get: function() {
44 return true;
45 },
46 enumerable : true
47 });
48
49 // PrivateKey class definition.
50 var PrivateKey = function(publicKeySpki, algorithm, keyUsages) {
51 Key.call(this, "private", publicKeySpki, algorithm, keyUsages);
52 };
53 PrivateKey.prototype.__proto__ = Key.prototype;
54
55 Object.defineProperty(PrivateKey.prototype, 'extractable', {
56 get: function() {
57 return false;
58 },
59 enumerable : true
60 });
61
62 // KeyPair class definition.
63 var KeyPair = function(publicKeySpki, algorithm, keyUsages) {
64 privates(this).publicKey_ =
65 new PublicKey(publicKeySpki, algorithm, intersect(["verify"], keyUsages));
66 privates(this).privateKey_ =
67 new PrivateKey(publicKeySpki, algorithm, intersect(["sign"], keyUsages));
68 };
69
70 Object.defineProperty(KeyPair.prototype, 'publicKey', {
71 get: function() {
72 return privates(this).publicKey_;
73 },
74 enumerable : true
75 });
76
77 Object.defineProperty(KeyPair.prototype, 'privateKey', {
78 get: function() {
79 return privates(this).privateKey_;
80 },
81 enumerable : true
82 });
83
84 // SublteCrypto implementation.
85 var SubtleCrypto = function(tokenId) {
86 privates(this).tokenId_ = tokenId;
87 };
88
89 function addCryptoMethod(methodName, func) {
90 SubtleCrypto.prototype[methodName] = function() {
91 var subtleCrypto = this;
92 var args = arguments;
93 return new Promise(function(resolve, reject) {
94 try {
95 var tokenId = privates(subtleCrypto).tokenId_;
96 if (!tokenId)
97 throw new Error('Invalid token object');
98
99 var newArgs = [resolve, reject, tokenId];
100 for (var i = 0; i < args.length; i++) {
101 newArgs.push(args[i]);
102 }
103
104 func.apply(null, newArgs);
105 }
106 catch (error) {
not at google - send to devlin 2014/05/02 18:31:56 are exceptions not automatically propagated to fai
pneubeck (no reviews) 2014/05/05 20:09:35 Indeed they are.
107 reject(error);
108 }
109 });
110 }
111 }
112
113 // Token class definition.
114 var Token = function(tokenId) {
115 privates(this).tokenId_ = tokenId;
116 privates(this).subtleCrypto_ = new SubtleCrypto(tokenId);
117 };
118
119 Object.defineProperty(Token.prototype, 'id', {
120 get: function() {
121 return privates(this).tokenId_;
122 },
123 enumerable : true
124 });
125
126 Object.defineProperty(Token.prototype, 'subtleCrypto', {
127 get: function() {
128 return privates(this).subtleCrypto_;
129 },
130 enumerable : true
131 });
132
133 addCryptoMethod(
134 'generateKey',
135 function(resolve, reject, tokenId, algorithm, extractable, keyUsages) {
136 if (extractable)
137 throw new Error('Extractable keys are not supported.');
138 if (intersect(keyUsages, ["sign", "verify"]).length != keyUsages.length) {
139 throw new Error(
140 'Unsupported keyUsages. Only "sign" and "verify" supported.');
141 }
142 if (algorithm.name != 'RSASSA-PKCS1-v1_5')
143 throw new Error('The algorithm is not supported');
144
145 internalAPI.generateKey(tokenId, function(spki) {
146 if (chrome.runtime.lastError) {
147 reject(chrome.runtime.lastError.message);
148 return;
149 }
150 resolve(new KeyPair(spki, algorithm, keyUsages));
151 });
152 });
153
154 addCryptoMethod('sign',
155 function(resolve, reject, tokenId, algorithm, key, dataView) {
156 if (!(key instanceof Key))
157 throw new Error('Invalid key.');
158 if (key.type != "private")
159 throw new Error('Key type not supported.');
160
161 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer
162 // might contain more data than dataView.
163 var data = dataView.buffer.slice(dataView.byteOffset,
164 dataView.byteOffset + dataView.byteLength);
165 internalAPI.sign(tokenId, privates(key).spki_, data, function(signature) {
166 if (chrome.runtime.lastError) {
167 reject(chrome.runtime.lastError.message);
168 return;
169 }
170 resolve(signature);
171 });
172 });
173
174 addCryptoMethod('exportKey', function(resolve, reject, tokenId, format, key) {
175 if (format != 'spki')
176 throw new Error('Format not supported.');
177 if (!(key instanceof Key))
178 throw new Error('Invalid key.');
179 if (key.type != "public")
180 throw new Error('Key type not supported.');
181
182 resolve(privates(key).spki_);
183 });
184
185 binding.registerCustomHook(function(api) {
186 var apiFunctions = api.apiFunctions;
187
188 var ret = apiFunctions.setHandleRequest('getTokens', function(callback) {
189 internalAPI.getTokens(function(tokenIds) {
190 callback($Array.map(tokenIds,
191 function(tokenId) { return new Token(tokenId); }));
192 });
193 });
194 });
195
196 exports.binding = binding.generate();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698