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 var utils = require('utils'); |
| 6 var internalAPI = require('enterprise.platformKeysInternal').binding; |
| 7 var intersect = require('enterprise.platformKeys.utils').intersect; |
| 8 var KeyPair = require('enterprise.platformKeys.KeyPair').KeyPair; |
| 9 var keyModule = require('enterprise.platformKeys.Key'); |
| 10 var Key = keyModule.Key; |
| 11 var getSpki = keyModule.getSpki; |
| 12 var KeyUsage = keyModule.KeyUsage; |
| 13 |
| 14 /** |
| 15 * Implementation of WebCrypto.SubtleCrypto used in enterprise.platformKeys. |
| 16 * @param {string} tokenId The id of the backing Token. |
| 17 * @constructor |
| 18 */ |
| 19 var SubtleCryptoImpl = function(tokenId) { |
| 20 this.tokenId = tokenId; |
| 21 }; |
| 22 |
| 23 SubtleCryptoImpl.prototype.generateKey = |
| 24 function(algorithm, extractable, keyUsages) { |
| 25 var subtleCrypto = this; |
| 26 return new Promise(function(resolve, reject) { |
| 27 if (extractable) |
| 28 throw new Error('Extractable keys are not supported.'); |
| 29 if (intersect(keyUsages, [KeyUsage.sign, KeyUsage.verify]).length != |
| 30 keyUsages.length) { |
| 31 throw new Error( |
| 32 'Unsupported keyUsages. Only "sign" and "verify" supported.'); |
| 33 } |
| 34 if (algorithm.name != 'RSASSA-PKCS1-v1_5' || !algorithm.modulusLength) |
| 35 throw new Error('The algorithm is not supported'); |
| 36 |
| 37 internalAPI.generateKey( |
| 38 subtleCrypto.tokenId, algorithm.modulusLength, function(spki) { |
| 39 if (chrome.runtime.lastError) { |
| 40 reject(chrome.runtime.lastError); |
| 41 return; |
| 42 } |
| 43 resolve(new KeyPair(spki, algorithm, keyUsages)); |
| 44 }); |
| 45 }); |
| 46 }; |
| 47 |
| 48 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { |
| 49 var subtleCrypto = this; |
| 50 return new Promise(function(resolve, reject) { |
| 51 if (key.type != 'private') |
| 52 throw new Error('Key type not supported.'); |
| 53 |
| 54 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer |
| 55 // might contain more data than dataView. |
| 56 var data = dataView.buffer.slice(dataView.byteOffset, |
| 57 dataView.byteOffset + dataView.byteLength); |
| 58 internalAPI.sign( |
| 59 subtleCrypto.tokenId, getSpki(key), data, function(signature) { |
| 60 if (chrome.runtime.lastError) { |
| 61 reject(chrome.runtime.lastError); |
| 62 return; |
| 63 } |
| 64 resolve(signature); |
| 65 }); |
| 66 }); |
| 67 }; |
| 68 |
| 69 SubtleCryptoImpl.prototype.exportKey = function(format, key) { |
| 70 return new Promise(function(resolve, reject) { |
| 71 if (format != 'spki') |
| 72 throw new Error('Format not supported.'); |
| 73 if (key.type != 'public') |
| 74 throw new Error('Key type not supported.'); |
| 75 |
| 76 resolve(getSpki(key)); |
| 77 }); |
| 78 }; |
| 79 |
| 80 exports.SubtleCrypto = |
| 81 utils.expose('SubtleCrypto', |
| 82 SubtleCryptoImpl, |
| 83 {functions:['generateKey', 'sign', 'exportKey']}); |
OLD | NEW |