| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var utils = require('utils'); | 5 var utils = require('utils'); |
| 6 var internalAPI = require('enterprise.platformKeys.internalAPI'); | 6 var internalAPI = require('enterprise.platformKeys.internalAPI'); |
| 7 var intersect = require('enterprise.platformKeys.utils').intersect; | 7 var intersect = require('enterprise.platformKeys.utils').intersect; |
| 8 var KeyPair = require('enterprise.platformKeys.KeyPair').KeyPair; | 8 var KeyPair = require('enterprise.platformKeys.KeyPair').KeyPair; |
| 9 var keyModule = require('enterprise.platformKeys.Key'); | 9 var keyModule = require('enterprise.platformKeys.Key'); |
| 10 var getSpki = keyModule.getSpki; | 10 var getSpki = keyModule.getSpki; |
| 11 var KeyUsage = keyModule.KeyUsage; | 11 var KeyUsage = keyModule.KeyUsage; |
| 12 | 12 |
| 13 var normalizeAlgorithm = |
| 14 requireNative('enterprise_platform_keys_natives').NormalizeAlgorithm; |
| 15 |
| 13 // This error is thrown by the internal and public API's token functions and | 16 // This error is thrown by the internal and public API's token functions and |
| 14 // must be rethrown by this custom binding. Keep this in sync with the C++ part | 17 // must be rethrown by this custom binding. Keep this in sync with the C++ part |
| 15 // of this API. | 18 // of this API. |
| 16 var errorInvalidToken = "The token is not valid."; | 19 var errorInvalidToken = "The token is not valid."; |
| 17 | 20 |
| 18 // The following errors are specified in WebCrypto. | 21 // The following errors are specified in WebCrypto. |
| 19 // TODO(pneubeck): These should be DOMExceptions. | 22 // TODO(pneubeck): These should be DOMExceptions. |
| 20 function CreateNotSupportedError() { | 23 function CreateNotSupportedError() { |
| 21 return new Error('The algorithm is not supported'); | 24 return new Error('The algorithm is not supported'); |
| 22 } | 25 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // implementation. | 68 // implementation. |
| 66 | 69 |
| 67 if (extractable) { | 70 if (extractable) { |
| 68 // Note: This deviates from WebCrypto.SubtleCrypto. | 71 // Note: This deviates from WebCrypto.SubtleCrypto. |
| 69 throw CreateNotSupportedError(); | 72 throw CreateNotSupportedError(); |
| 70 } | 73 } |
| 71 if (intersect(keyUsages, [KeyUsage.sign, KeyUsage.verify]).length != | 74 if (intersect(keyUsages, [KeyUsage.sign, KeyUsage.verify]).length != |
| 72 keyUsages.length) { | 75 keyUsages.length) { |
| 73 throw CreateDataError(); | 76 throw CreateDataError(); |
| 74 } | 77 } |
| 75 if (!algorithm.name) { | 78 var normalizedAlgorithmParameters = normalizeAlgorithm(algorithm, ''); |
| 79 if (!normalizedAlgorithmParameters) { |
| 76 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to | 80 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to |
| 77 // throw here. | 81 // throw here. |
| 78 throw CreateSyntaxError(); | 82 throw CreateSyntaxError(); |
| 79 } | 83 } |
| 80 | 84 |
| 81 if (algorithm.name.toUpperCase() !== 'RSASSA-PKCS1-V1_5') { | 85 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-V1_5') { |
| 82 // Note: This deviates from WebCrypto.SubtleCrypto. | 86 // Note: This deviates from WebCrypto.SubtleCrypto. |
| 83 throw CreateNotSupportedError(); | 87 throw CreateNotSupportedError(); |
| 84 } | 88 } |
| 85 if (!algorithm.modulusLength || !algorithm.publicExponent) | |
| 86 throw CreateSyntaxError(); | |
| 87 | 89 |
| 88 internalAPI.generateKey( | 90 internalAPI.generateKey(subtleCrypto.tokenId, |
| 89 subtleCrypto.tokenId, algorithm.modulusLength, function(spki) { | 91 normalizedAlgorithmParameters.modulusLength, |
| 90 if (catchInvalidTokenError(reject)) | 92 function(spki) { |
| 91 return; | 93 if (catchInvalidTokenError(reject)) |
| 92 if (chrome.runtime.lastError) { | 94 return; |
| 93 reject(CreateOperationError()); | 95 if (chrome.runtime.lastError) { |
| 94 return; | 96 reject(CreateOperationError()); |
| 95 } | 97 return; |
| 96 resolve(new KeyPair(spki, algorithm, keyUsages)); | 98 } |
| 97 }); | 99 resolve(new KeyPair(spki, algorithm, keyUsages)); |
| 100 }); |
| 98 }); | 101 }); |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { | 104 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { |
| 102 var subtleCrypto = this; | 105 var subtleCrypto = this; |
| 103 return new Promise(function(resolve, reject) { | 106 return new Promise(function(resolve, reject) { |
| 104 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) | 107 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) |
| 105 throw CreateInvalidAccessError(); | 108 throw CreateInvalidAccessError(); |
| 106 | 109 |
| 107 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer | 110 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer |
| (...skipping 27 matching lines...) Expand all Loading... |
| 135 // TODO(pneubeck): It should be possible to export to format 'jwk'. | 138 // TODO(pneubeck): It should be possible to export to format 'jwk'. |
| 136 throw CreateNotSupportedError(); | 139 throw CreateNotSupportedError(); |
| 137 } | 140 } |
| 138 }); | 141 }); |
| 139 }; | 142 }; |
| 140 | 143 |
| 141 exports.SubtleCrypto = | 144 exports.SubtleCrypto = |
| 142 utils.expose('SubtleCrypto', | 145 utils.expose('SubtleCrypto', |
| 143 SubtleCryptoImpl, | 146 SubtleCryptoImpl, |
| 144 {functions:['generateKey', 'sign', 'exportKey']}); | 147 {functions:['generateKey', 'sign', 'exportKey']}); |
| OLD | NEW |