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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 throw CreateDataError(); | 95 throw CreateDataError(); |
96 } | 96 } |
97 var normalizedAlgorithmParameters = | 97 var normalizedAlgorithmParameters = |
98 normalizeAlgorithm(algorithm, 'GenerateKey'); | 98 normalizeAlgorithm(algorithm, 'GenerateKey'); |
99 if (!normalizedAlgorithmParameters) { | 99 if (!normalizedAlgorithmParameters) { |
100 // 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 |
101 // throw here. | 101 // throw here. |
102 throw CreateSyntaxError(); | 102 throw CreateSyntaxError(); |
103 } | 103 } |
104 | 104 |
| 105 // normalizeAlgorithm returns an array, but publicExponent should be a |
| 106 // Uint8Array. |
| 107 normalizedAlgorithmParameters.publicExponent = |
| 108 new Uint8Array(normalizedAlgorithmParameters.publicExponent); |
| 109 |
105 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-v1_5' || | 110 if (normalizedAlgorithmParameters.name !== 'RSASSA-PKCS1-v1_5' || |
106 !equalsStandardPublicExponent( | 111 !equalsStandardPublicExponent( |
107 normalizedAlgorithmParameters.publicExponent)) { | 112 normalizedAlgorithmParameters.publicExponent)) { |
108 // Note: This deviates from WebCrypto.SubtleCrypto. | 113 // Note: This deviates from WebCrypto.SubtleCrypto. |
109 throw CreateNotSupportedError(); | 114 throw CreateNotSupportedError(); |
110 } | 115 } |
111 | 116 |
112 internalAPI.generateKey(subtleCrypto.tokenId, | 117 internalAPI.generateKey(subtleCrypto.tokenId, |
113 normalizedAlgorithmParameters.modulusLength, | 118 normalizedAlgorithmParameters.modulusLength, |
114 function(spki) { | 119 function(spki) { |
115 if (catchInvalidTokenError(reject)) | 120 if (catchInvalidTokenError(reject)) |
116 return; | 121 return; |
117 if (chrome.runtime.lastError) { | 122 if (chrome.runtime.lastError) { |
118 reject(CreateOperationError()); | 123 reject(CreateOperationError()); |
119 return; | 124 return; |
120 } | 125 } |
121 resolve(new KeyPair(spki, algorithm, keyUsages)); | 126 resolve(new KeyPair(spki, normalizedAlgorithmParameters, keyUsages)); |
122 }); | 127 }); |
123 }); | 128 }); |
124 }; | 129 }; |
125 | 130 |
126 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { | 131 SubtleCryptoImpl.prototype.sign = function(algorithm, key, dataView) { |
127 var subtleCrypto = this; | 132 var subtleCrypto = this; |
128 return new Promise(function(resolve, reject) { | 133 return new Promise(function(resolve, reject) { |
129 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) | 134 if (key.type != 'private' || key.usages.indexOf(KeyUsage.sign) == -1) |
130 throw CreateInvalidAccessError(); | 135 throw CreateInvalidAccessError(); |
131 | 136 |
132 var normalizedAlgorithmParameters = | 137 var normalizedAlgorithmParameters = |
133 normalizeAlgorithm(algorithm, 'Sign'); | 138 normalizeAlgorithm(algorithm, 'Sign'); |
134 if (!normalizedAlgorithmParameters) { | 139 if (!normalizedAlgorithmParameters) { |
135 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to | 140 // TODO(pneubeck): It's not clear from the WebCrypto spec which error to |
136 // throw here. | 141 // throw here. |
137 throw CreateSyntaxError(); | 142 throw CreateSyntaxError(); |
138 } | 143 } |
139 | 144 |
140 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer | 145 // Create an ArrayBuffer that equals the dataView. Note that dataView.buffer |
141 // might contain more data than dataView. | 146 // might contain more data than dataView. |
142 var data = dataView.buffer.slice(dataView.byteOffset, | 147 var data = dataView.buffer.slice(dataView.byteOffset, |
143 dataView.byteOffset + dataView.byteLength); | 148 dataView.byteOffset + dataView.byteLength); |
144 internalAPI.sign( | 149 internalAPI.sign(subtleCrypto.tokenId, |
145 subtleCrypto.tokenId, getSpki(key), data, function(signature) { | 150 getSpki(key), |
146 if (catchInvalidTokenError(reject)) | 151 key.algorithm.hash.name, |
147 return; | 152 data, |
148 if (chrome.runtime.lastError) { | 153 function(signature) { |
149 reject(CreateOperationError()); | 154 if (catchInvalidTokenError(reject)) |
150 return; | 155 return; |
151 } | 156 if (chrome.runtime.lastError) { |
152 resolve(signature); | 157 reject(CreateOperationError()); |
153 }); | 158 return; |
| 159 } |
| 160 resolve(signature); |
| 161 }); |
154 }); | 162 }); |
155 }; | 163 }; |
156 | 164 |
157 SubtleCryptoImpl.prototype.exportKey = function(format, key) { | 165 SubtleCryptoImpl.prototype.exportKey = function(format, key) { |
158 return new Promise(function(resolve, reject) { | 166 return new Promise(function(resolve, reject) { |
159 if (format == 'pkcs8') { | 167 if (format == 'pkcs8') { |
160 // Either key.type is not 'private' or the key is not extractable. In both | 168 // Either key.type is not 'private' or the key is not extractable. In both |
161 // cases the error is the same. | 169 // cases the error is the same. |
162 throw CreateInvalidAccessError(); | 170 throw CreateInvalidAccessError(); |
163 } else if (format == 'spki') { | 171 } else if (format == 'spki') { |
164 if (key.type != 'public') | 172 if (key.type != 'public') |
165 throw CreateInvalidAccessError(); | 173 throw CreateInvalidAccessError(); |
166 resolve(getSpki(key)); | 174 resolve(getSpki(key)); |
167 } else { | 175 } else { |
168 // TODO(pneubeck): It should be possible to export to format 'jwk'. | 176 // TODO(pneubeck): It should be possible to export to format 'jwk'. |
169 throw CreateNotSupportedError(); | 177 throw CreateNotSupportedError(); |
170 } | 178 } |
171 }); | 179 }); |
172 }; | 180 }; |
173 | 181 |
174 exports.SubtleCrypto = | 182 exports.SubtleCrypto = |
175 utils.expose('SubtleCrypto', | 183 utils.expose('SubtleCrypto', |
176 SubtleCryptoImpl, | 184 SubtleCryptoImpl, |
177 {functions:['generateKey', 'sign', 'exportKey']}); | 185 {functions:['generateKey', 'sign', 'exportKey']}); |
OLD | NEW |