| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 internalAPI = require('platformKeys.internalAPI'); | 5 var internalAPI = require('platformKeys.internalAPI'); |
| 6 | 6 |
| 7 var normalizeAlgorithm = | 7 var normalizeAlgorithm = |
| 8 requireNative('platform_keys_natives').NormalizeAlgorithm; | 8 requireNative('platform_keys_natives').NormalizeAlgorithm; |
| 9 | 9 |
| 10 function combineAlgorithms(algorithm, importParams) { | 10 // Returns the normalized parameters of |importParams|. |
| 11 if (importParams.name === undefined) { | 11 // Any unknown parameters will be ignored. |
| 12 importParams.name = algorithm.name; | 12 function normalizeImportParams(importParams) { |
| 13 if (!importParams.name || |
| 14 Object.prototype.toString.call(importParams.name) != '[object String]') { |
| 15 throw new Error('Algorithm: name: Missing or not a String'); |
| 13 } | 16 } |
| 14 | 17 |
| 15 // Verify whether importParams.hash equals | 18 var filteredParams = { name: importParams.name }; |
| 16 // { name: 'none' } | 19 |
| 17 if (importParams.hash && | 20 var hashIsNone = false; |
| 18 importParams.hash.name.toLowerCase() === 'none') { | 21 if (importParams.hash) { |
| 19 if (Object.keys(importParams.hash).length != 1 || | 22 if (importParams.hash.name.toLowerCase() === 'none') { |
| 20 Object.keys(importParams).length != 2) { | 23 hashIsNone = true; |
| 21 // 'name' must be the only hash property in this case. | 24 // Temporarily replace |hash| by a valid WebCrypto Hash for normalization. |
| 22 throw new Error('A required parameter was missing or out-of-range'); | 25 // This will be reverted to 'none' after normalization. |
| 23 } | 26 filteredParams.hash = { name: 'SHA-1' }; |
| 24 importParams.hash.name = 'none'; | 27 } else { |
| 25 } else { | 28 filteredParams.hash = { name: importParams.hash.name } |
| 26 // Otherwise apply WebCrypto's algorithm normalization. | |
| 27 importParams = normalizeAlgorithm(importParams, 'ImportKey'); | |
| 28 if (!importParams) { | |
| 29 // throw CreateSyntaxError(); | |
| 30 throw new Error('A required parameter was missing or out-of-range'); | |
| 31 } | 29 } |
| 32 } | 30 } |
| 33 | 31 |
| 32 // Apply WebCrypto's algorithm normalization. |
| 33 var resultParams = normalizeAlgorithm(filteredParams, 'ImportKey'); |
| 34 if (!resultParams ) { |
| 35 throw new Error('A required parameter was missing or out-of-range'); |
| 36 } |
| 37 if (hashIsNone) { |
| 38 resultParams.hash = { name: 'none' }; |
| 39 } |
| 40 return resultParams; |
| 41 } |
| 42 |
| 43 function combineAlgorithms(algorithm, importParams) { |
| 34 // internalAPI.getPublicKey returns publicExponent as ArrayBuffer, but it | 44 // internalAPI.getPublicKey returns publicExponent as ArrayBuffer, but it |
| 35 // should be a Uint8Array. | 45 // should be a Uint8Array. |
| 36 if (algorithm.publicExponent) { | 46 if (algorithm.publicExponent) { |
| 37 algorithm.publicExponent = new Uint8Array(algorithm.publicExponent); | 47 algorithm.publicExponent = new Uint8Array(algorithm.publicExponent); |
| 38 } | 48 } |
| 39 | 49 |
| 40 for (var key in importParams) { | 50 algorithm.hash = importParams.hash; |
| 41 algorithm[key] = importParams[key]; | |
| 42 } | |
| 43 | |
| 44 return algorithm; | 51 return algorithm; |
| 45 } | 52 } |
| 46 | 53 |
| 47 function getPublicKey(cert, importParams, callback) { | 54 function getPublicKey(cert, importParams, callback) { |
| 55 importParams = normalizeImportParams(importParams); |
| 56 // TODO(pneubeck): pass importParams.name to the internal getPublicKey and |
| 57 // verify on the C++ side that the requested algorithm is compatible with the |
| 58 // given SubjectPublicKeyInfo of the certificate. |
| 59 // https://crbug.com/466584 |
| 48 internalAPI.getPublicKey(cert, function(publicKey, algorithm) { | 60 internalAPI.getPublicKey(cert, function(publicKey, algorithm) { |
| 49 var combinedAlgorithm = combineAlgorithms(algorithm, importParams); | 61 var combinedAlgorithm = combineAlgorithms(algorithm, importParams); |
| 50 callback(publicKey, combinedAlgorithm); | 62 callback(publicKey, combinedAlgorithm); |
| 51 }); | 63 }); |
| 52 } | 64 } |
| 53 | 65 |
| 54 exports.getPublicKey = getPublicKey; | 66 exports.getPublicKey = getPublicKey; |
| OLD | NEW |