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 } |
23 | 26 |
24 function CreateInvalidAccessError() { | 27 function CreateInvalidAccessError() { |
25 return new Error('The requested operation is not valid for the provided key'); | 28 return new Error('The requested operation is not valid for the provided key'); |
26 } | 29 } |
27 | 30 |
28 function CreateDataError() { | 31 function CreateDataError() { |
29 return new Error('Data provided to an operation does not meet requirements'); | 32 return new Error('Data provided to an operation does not meet requirements'); |
30 } | 33 } |
31 | 34 |
32 function CreateSyntaxError() { | 35 function CreateSyntaxError() { |
33 return new Error('A required parameter was missing our out-of-range'); | 36 return new Error('A required parameter was missing or out-of-range'); |
34 } | 37 } |
35 | 38 |
36 function CreateOperationError() { | 39 function CreateOperationError() { |
37 return new Error('The operation failed for an operation-specific reason'); | 40 return new Error('The operation failed for an operation-specific reason'); |
38 } | 41 } |
39 | 42 |
40 // Catches an |internalErrorInvalidToken|. If so, forwards it to |reject| and | 43 // Catches an |internalErrorInvalidToken|. If so, forwards it to |reject| and |
41 // returns true. | 44 // returns true. |
42 function catchInvalidTokenError(reject) { | 45 function catchInvalidTokenError(reject) { |
43 if (chrome.runtime.lastError && | 46 if (chrome.runtime.lastError && |
(...skipping 21 matching lines...) Expand all 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 = |
| 79 normalizeAlgorithm(algorithm, 'GenerateKey'); |
| 80 if (!normalizedAlgorithmParameters) { |
76 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to | 81 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to |
77 // throw here. | 82 // throw here. |
78 throw CreateSyntaxError(); | 83 throw CreateSyntaxError(); |
79 } | 84 } |
80 | 85 |
81 if (algorithm.name.toUpperCase() !== 'RSASSA-PKCS1-V1_5') { | 86 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-v1_5') { |
82 // Note: This deviates from WebCrypto.SubtleCrypto. | 87 // Note: This deviates from WebCrypto.SubtleCrypto. |
83 throw CreateNotSupportedError(); | 88 throw CreateNotSupportedError(); |
84 } | 89 } |
85 if (!algorithm.modulusLength || !algorithm.publicExponent) | |
86 throw CreateSyntaxError(); | |
87 | 90 |
88 internalAPI.generateKey( | 91 internalAPI.generateKey(subtleCrypto.tokenId, |
89 subtleCrypto.tokenId, algorithm.modulusLength, function(spki) { | 92 normalizedAlgorithmParameters.modulusLength, |
90 if (catchInvalidTokenError(reject)) | 93 function(spki) { |
91 return; | 94 if (catchInvalidTokenError(reject)) |
92 if (chrome.runtime.lastError) { | 95 return; |
93 reject(CreateOperationError()); | 96 if (chrome.runtime.lastError) { |
94 return; | 97 reject(CreateOperationError()); |
95 } | 98 return; |
96 resolve(new KeyPair(spki, algorithm, keyUsages)); | 99 } |
97 }); | 100 resolve(new KeyPair(spki, algorithm, keyUsages)); |
| 101 }); |
98 }); | 102 }); |
99 }; | 103 }; |
100 | 104 |
101 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { | 105 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { |
102 var subtleCrypto = this; | 106 var subtleCrypto = this; |
103 return new Promise(function(resolve, reject) { | 107 return new Promise(function(resolve, reject) { |
104 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) | 108 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) |
105 throw CreateInvalidAccessError(); | 109 throw CreateInvalidAccessError(); |
106 | 110 |
| 111 var normalizedAlgorithmParameters = |
| 112 normalizeAlgorithm(algorithm, 'Sign'); |
| 113 if (!normalizedAlgorithmParameters) { |
| 114 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to |
| 115 // throw here. |
| 116 throw CreateSyntaxError(); |
| 117 } |
| 118 |
107 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer | 119 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer |
108 // might contain more data than dataView. | 120 // might contain more data than dataView. |
109 var data = dataView.buffer.slice(dataView.byteOffset, | 121 var data = dataView.buffer.slice(dataView.byteOffset, |
110 dataView.byteOffset + dataView.byteLength); | 122 dataView.byteOffset + dataView.byteLength); |
111 internalAPI.sign( | 123 internalAPI.sign( |
112 subtleCrypto.tokenId, getSpki(key), data, function(signature) { | 124 subtleCrypto.tokenId, getSpki(key), data, function(signature) { |
113 if (catchInvalidTokenError(reject)) | 125 if (catchInvalidTokenError(reject)) |
114 return; | 126 return; |
115 if (chrome.runtime.lastError) { | 127 if (chrome.runtime.lastError) { |
116 reject(CreateOperationError()); | 128 reject(CreateOperationError()); |
(...skipping 18 matching lines...) Expand all Loading... |
135 // TODO(pneubeck): It should be possible to export to format 'jwk'. | 147 // TODO(pneubeck): It should be possible to export to format 'jwk'. |
136 throw CreateNotSupportedError(); | 148 throw CreateNotSupportedError(); |
137 } | 149 } |
138 }); | 150 }); |
139 }; | 151 }; |
140 | 152 |
141 exports.SubtleCrypto = | 153 exports.SubtleCrypto = |
142 utils.expose('SubtleCrypto', | 154 utils.expose('SubtleCrypto', |
143 SubtleCryptoImpl, | 155 SubtleCryptoImpl, |
144 {functions:['generateKey', 'sign', 'exportKey']}); | 156 {functions:['generateKey', 'sign', 'exportKey']}); |
OLD | NEW |