| 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; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // returns true. | 44 // returns true. |
| 45 function catchInvalidTokenError(reject) { | 45 function catchInvalidTokenError(reject) { |
| 46 if (chrome.runtime.lastError && | 46 if (chrome.runtime.lastError && |
| 47 chrome.runtime.lastError.message == errorInvalidToken) { | 47 chrome.runtime.lastError.message == errorInvalidToken) { |
| 48 reject(chrome.runtime.lastError); | 48 reject(chrome.runtime.lastError); |
| 49 return true; | 49 return true; |
| 50 } | 50 } |
| 51 return false; | 51 return false; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Returns true if |array| is a BigInteger describing the standard public |
| 55 // exponent 65537. In particular, it ignores leading zeros as required by the |
| 56 // BigInteger definition in WebCrypto. |
| 57 function equalsStandardPublicExponent(array) { |
| 58 var expected = [0x01, 0x00, 0x01]; |
| 59 if (array.length < expected.length) |
| 60 return false; |
| 61 for (var i = 0; i < array.length; i++) { |
| 62 var expectedDigit = 0; |
| 63 if (i < expected.length) { |
| 64 // |expected| is symmetric, endianness doesn't matter. |
| 65 expectedDigit = expected[i]; |
| 66 } |
| 67 if (array[array.length - 1 - i] !== expectedDigit) |
| 68 return false; |
| 69 } |
| 70 return true; |
| 71 } |
| 72 |
| 54 /** | 73 /** |
| 55 * Implementation of WebCrypto.SubtleCrypto used in enterprise.platformKeys. | 74 * Implementation of WebCrypto.SubtleCrypto used in enterprise.platformKeys. |
| 56 * @param {string} tokenId The id of the backing Token. | 75 * @param {string} tokenId The id of the backing Token. |
| 57 * @constructor | 76 * @constructor |
| 58 */ | 77 */ |
| 59 var SubtleCryptoImpl = function(tokenId) { | 78 var SubtleCryptoImpl = function(tokenId) { |
| 60 this.tokenId = tokenId; | 79 this.tokenId = tokenId; |
| 61 }; | 80 }; |
| 62 | 81 |
| 63 SubtleCryptoImpl.prototype.generateKey = | 82 SubtleCryptoImpl.prototype.generateKey = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 76 throw CreateDataError(); | 95 throw CreateDataError(); |
| 77 } | 96 } |
| 78 var normalizedAlgorithmParameters = | 97 var normalizedAlgorithmParameters = |
| 79 normalizeAlgorithm(algorithm, 'GenerateKey'); | 98 normalizeAlgorithm(algorithm, 'GenerateKey'); |
| 80 if (!normalizedAlgorithmParameters) { | 99 if (!normalizedAlgorithmParameters) { |
| 81 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to | 100 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to |
| 82 // throw here. | 101 // throw here. |
| 83 throw CreateSyntaxError(); | 102 throw CreateSyntaxError(); |
| 84 } | 103 } |
| 85 | 104 |
| 86 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-v1_5') { | 105 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-v1_5' || |
| 106 !equalsStandardPublicExponent( |
| 107 normalizedAlgorithmParameters.publicExponent)) { |
| 87 // Note: This deviates from WebCrypto.SubtleCrypto. | 108 // Note: This deviates from WebCrypto.SubtleCrypto. |
| 88 throw CreateNotSupportedError(); | 109 throw CreateNotSupportedError(); |
| 89 } | 110 } |
| 90 | 111 |
| 91 internalAPI.generateKey(subtleCrypto.tokenId, | 112 internalAPI.generateKey(subtleCrypto.tokenId, |
| 92 normalizedAlgorithmParameters.modulusLength, | 113 normalizedAlgorithmParameters.modulusLength, |
| 93 function(spki) { | 114 function(spki) { |
| 94 if (catchInvalidTokenError(reject)) | 115 if (catchInvalidTokenError(reject)) |
| 95 return; | 116 return; |
| 96 if (chrome.runtime.lastError) { | 117 if (chrome.runtime.lastError) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // TODO(pneubeck): It should be possible to export to format 'jwk'. | 168 // TODO(pneubeck): It should be possible to export to format 'jwk'. |
| 148 throw CreateNotSupportedError(); | 169 throw CreateNotSupportedError(); |
| 149 } | 170 } |
| 150 }); | 171 }); |
| 151 }; | 172 }; |
| 152 | 173 |
| 153 exports.SubtleCrypto = | 174 exports.SubtleCrypto = |
| 154 utils.expose('SubtleCrypto', | 175 utils.expose('SubtleCrypto', |
| 155 SubtleCryptoImpl, | 176 SubtleCryptoImpl, |
| 156 {functions:['generateKey', 'sign', 'exportKey']}); | 177 {functions:['generateKey', 'sign', 'exportKey']}); |
| OLD | NEW |