Index: chrome/renderer/resources/extensions/enterprise_platform_keys/enterprise_platform_keys_subtle_crypto.js |
diff --git a/chrome/renderer/resources/extensions/enterprise_platform_keys/enterprise_platform_keys_subtle_crypto.js b/chrome/renderer/resources/extensions/enterprise_platform_keys/enterprise_platform_keys_subtle_crypto.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..be7dd9bd62121bf2acd94834c3e8dc19253557c9 |
--- /dev/null |
+++ b/chrome/renderer/resources/extensions/enterprise_platform_keys/enterprise_platform_keys_subtle_crypto.js |
@@ -0,0 +1,88 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+var utils = require('utils'); |
+var internalAPI = require('enterprise.platformKeysInternal').binding; |
+var intersect = require('enterprise.platformKeys.utils').intersect; |
+var keyModule = require('enterprise.platformKeys.Key'); |
+var Key = keyModule.Key; |
+var KeyImpl = keyModule.KeyImpl; |
+var KeyPair = keyModule.KeyPair; |
+var KeyUsage = keyModule.KeyUsage; |
+ |
+/** |
+ * Implementation of WebCrypto.SubtleCrypto used in enterprise.platformKeys. |
+ * @param {string} tokenId The id of the backing Token. |
+ * @constructor |
+ */ |
+var SubtleCryptoImpl = function(tokenId) { |
+ this.tokenId = tokenId; |
+}; |
+ |
+SubtleCryptoImpl.prototype.generateKey = |
+ function(algorithm, extractable, keyUsages) { |
+ var subtleCrypto = this; |
+ return new Promise(function(resolve, reject) { |
+ if (extractable) |
+ throw new Error('Extractable keys are not supported.'); |
+ if (intersect(keyUsages, [KeyUsage.sign, KeyUsage.verify]).length != |
+ keyUsages.length) { |
+ throw new Error( |
+ 'Unsupported keyUsages. Only "sign" and "verify" supported.'); |
+ } |
not at google - send to devlin
2014/05/05 21:09:24
all of this validation could happen before returni
pneubeck (no reviews)
2014/05/05 21:28:23
As far as I understand the Promise thing, reject s
|
+ if (algorithm.name != 'RSASSA-PKCS1-v1_5') |
+ throw new Error('The algorithm is not supported'); |
not at google - send to devlin
2014/05/05 21:09:24
maybe include the name of the algorithm in this fa
pneubeck (no reviews)
2014/05/06 14:07:21
The errors are specified by SubtleCrypto (using DO
|
+ |
+ internalAPI.generateKey(subtleCrypto.tokenId, function(spki) { |
+ if (chrome.runtime.lastError) { |
+ reject(chrome.runtime.lastError); |
+ return; |
+ } |
+ resolve(new KeyPair(spki, algorithm, keyUsages)); |
+ }); |
+ }); |
+}; |
+ |
+SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { |
+ var subtleCrypto = this; |
+ return new Promise(function(resolve, reject) { |
+ var keyImpl = privates(key).impl; |
not at google - send to devlin
2014/05/05 21:09:24
same comments as above
pneubeck (no reviews)
2014/05/06 14:07:21
Done.
|
+ if (!(keyImpl instanceof KeyImpl)) |
not at google - send to devlin
2014/05/05 21:09:24
instanceof is dangerous in bindings files because
pneubeck (no reviews)
2014/05/05 21:28:23
The only reason why I added this was, that I wante
pneubeck (no reviews)
2014/05/06 14:07:21
I removed the dependence on the class KeyImpl from
|
+ throw new Error('Invalid key object.'); |
+ if (key.type != 'private') |
+ throw new Error('Key type not supported.'); |
+ |
+ // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer |
+ // might contain more data than dataView. |
+ var data = dataView.buffer.slice(dataView.byteOffset, |
+ dataView.byteOffset + dataView.byteLength); |
+ internalAPI.sign( |
+ subtleCrypto.tokenId, keyImpl.spki, data, function(signature) { |
+ if (chrome.runtime.lastError) { |
+ reject(chrome.runtime.lastError); |
+ return; |
+ } |
+ resolve(signature); |
+ }); |
+ }); |
+}; |
+ |
+SubtleCryptoImpl.prototype.exportKey = function(format, key) { |
not at google - send to devlin
2014/05/05 21:09:24
same comments here
pneubeck (no reviews)
2014/05/06 14:07:21
Done.
|
+ return new Promise(function(resolve, reject) { |
+ if (format != 'spki') |
+ throw new Error('Format not supported.'); |
+ var keyImpl = privates(key).impl; |
+ if (!(keyImpl instanceof KeyImpl)) |
+ throw new Error('Invalid key object.'); |
+ if (key.type != 'public') |
+ throw new Error('Key type not supported.'); |
+ |
+ resolve(keyImpl.spki); |
+ }); |
+}; |
+ |
+exports.SubtleCrypto = |
+ utils.expose('SubtleCrypto', |
+ SubtleCryptoImpl, |
+ {functions:['generateKey', 'sign', 'exportKey']}); |