OLD | NEW |
1 // Copyright 2014 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 utils = require('utils'); | 5 var utils = require('utils'); |
6 var internalAPI = require('enterprise.platformKeys.internalAPI'); | 6 var internalAPI = require('platformKeys.internalAPI'); |
7 var intersect = require('enterprise.platformKeys.utils').intersect; | 7 var keyModule = require('platformKeys.Key'); |
8 var KeyPair = require('enterprise.platformKeys.KeyPair').KeyPair; | |
9 var keyModule = require('enterprise.platformKeys.Key'); | |
10 var getSpki = keyModule.getSpki; | 8 var getSpki = keyModule.getSpki; |
11 var KeyUsage = keyModule.KeyUsage; | 9 var KeyUsage = keyModule.KeyUsage; |
12 | 10 |
13 var normalizeAlgorithm = | 11 var normalizeAlgorithm = |
14 requireNative('enterprise_platform_keys_natives').NormalizeAlgorithm; | 12 requireNative('platform_keys_natives').NormalizeAlgorithm; |
15 | 13 |
16 // This error is thrown by the internal and public API's token functions and | 14 // This error is thrown by the internal and public API's token functions and |
17 // must be rethrown by this custom binding. Keep this in sync with the C++ part | 15 // must be rethrown by this custom binding. Keep this in sync with the C++ part |
18 // of this API. | 16 // of this API. |
19 var errorInvalidToken = "The token is not valid."; | 17 var errorInvalidToken = "The token is not valid."; |
20 | 18 |
21 // The following errors are specified in WebCrypto. | 19 // The following errors are specified in WebCrypto. |
22 // TODO(pneubeck): These should be DOMExceptions. | 20 // TODO(pneubeck): These should be DOMExceptions. |
23 function CreateNotSupportedError() { | 21 function CreateNotSupportedError() { |
24 return new Error('The algorithm is not supported'); | 22 return new Error('The algorithm is not supported'); |
(...skipping 19 matching lines...) Expand all Loading... |
44 // returns true. | 42 // returns true. |
45 function catchInvalidTokenError(reject) { | 43 function catchInvalidTokenError(reject) { |
46 if (chrome.runtime.lastError && | 44 if (chrome.runtime.lastError && |
47 chrome.runtime.lastError.message == errorInvalidToken) { | 45 chrome.runtime.lastError.message == errorInvalidToken) { |
48 reject(chrome.runtime.lastError); | 46 reject(chrome.runtime.lastError); |
49 return true; | 47 return true; |
50 } | 48 } |
51 return false; | 49 return false; |
52 } | 50 } |
53 | 51 |
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 | |
73 /** | 52 /** |
74 * Implementation of WebCrypto.SubtleCrypto used in enterprise.platformKeys. | 53 * Implementation of WebCrypto.SubtleCrypto used in platformKeys and |
| 54 * enterprise.platformKeys. |
75 * @param {string} tokenId The id of the backing Token. | 55 * @param {string} tokenId The id of the backing Token. |
76 * @constructor | 56 * @constructor |
77 */ | 57 */ |
78 var SubtleCryptoImpl = function(tokenId) { | 58 var SubtleCryptoImpl = function(tokenId) { |
79 this.tokenId = tokenId; | 59 this.tokenId = tokenId; |
80 }; | 60 }; |
81 | 61 |
82 SubtleCryptoImpl.prototype.generateKey = | |
83 function(algorithm, extractable, keyUsages) { | |
84 var subtleCrypto = this; | |
85 return new Promise(function(resolve, reject) { | |
86 // TODO(pneubeck): Apply the algorithm normalization of the WebCrypto | |
87 // implementation. | |
88 | |
89 if (extractable) { | |
90 // Note: This deviates from WebCrypto.SubtleCrypto. | |
91 throw CreateNotSupportedError(); | |
92 } | |
93 if (intersect(keyUsages, [KeyUsage.sign, KeyUsage.verify]).length != | |
94 keyUsages.length) { | |
95 throw CreateDataError(); | |
96 } | |
97 var normalizedAlgorithmParameters = | |
98 normalizeAlgorithm(algorithm, 'GenerateKey'); | |
99 if (!normalizedAlgorithmParameters) { | |
100 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to | |
101 // throw here. | |
102 throw CreateSyntaxError(); | |
103 } | |
104 | |
105 // normalizeAlgorithm returns an array, but publicExponent should be a | |
106 // Uint8Array. | |
107 normalizedAlgorithmParameters.publicExponent = | |
108 new Uint8Array(normalizedAlgorithmParameters.publicExponent); | |
109 | |
110 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-v1_5' || | |
111 !equalsStandardPublicExponent( | |
112 normalizedAlgorithmParameters.publicExponent)) { | |
113 // Note: This deviates from WebCrypto.SubtleCrypto. | |
114 throw CreateNotSupportedError(); | |
115 } | |
116 | |
117 internalAPI.generateKey(subtleCrypto.tokenId, | |
118 normalizedAlgorithmParameters.modulusLength, | |
119 function(spki) { | |
120 if (catchInvalidTokenError(reject)) | |
121 return; | |
122 if (chrome.runtime.lastError) { | |
123 reject(CreateOperationError()); | |
124 return; | |
125 } | |
126 resolve(new KeyPair(spki, normalizedAlgorithmParameters, keyUsages)); | |
127 }); | |
128 }); | |
129 }; | |
130 | |
131 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { | 62 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { |
132 var subtleCrypto = this; | 63 var subtleCrypto = this; |
133 return new Promise(function(resolve, reject) { | 64 return new Promise(function(resolve, reject) { |
134 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) | 65 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) |
135 throw CreateInvalidAccessError(); | 66 throw CreateInvalidAccessError(); |
136 | 67 |
137 var normalizedAlgorithmParameters = | 68 var normalizedAlgorithmParameters = |
138 normalizeAlgorithm(algorithm, 'Sign'); | 69 normalizeAlgorithm(algorithm, 'Sign'); |
139 if (!normalizedAlgorithmParameters) { | 70 if (!normalizedAlgorithmParameters) { |
140 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to | 71 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 if (key.type != 'public') | 103 if (key.type != 'public') |
173 throw CreateInvalidAccessError(); | 104 throw CreateInvalidAccessError(); |
174 resolve(getSpki(key)); | 105 resolve(getSpki(key)); |
175 } else { | 106 } else { |
176 // TODO(pneubeck): It should be possible to export to format 'jwk'. | 107 // TODO(pneubeck): It should be possible to export to format 'jwk'. |
177 throw CreateNotSupportedError(); | 108 throw CreateNotSupportedError(); |
178 } | 109 } |
179 }); | 110 }); |
180 }; | 111 }; |
181 | 112 |
| 113 // Required for subclassing. |
| 114 exports.SubtleCryptoImpl = SubtleCryptoImpl |
| 115 |
182 exports.SubtleCrypto = | 116 exports.SubtleCrypto = |
183 utils.expose('SubtleCrypto', | 117 utils.expose('SubtleCrypto', |
184 SubtleCryptoImpl, | 118 SubtleCryptoImpl, |
185 {functions:['generateKey', 'sign', 'exportKey']}); | 119 {functions:['sign', 'exportKey']}); |
OLD | NEW |