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 // Must be packed to ../enterprise_platform_keys.crx using the private key | 5 // Must be packed to ../enterprise_platform_keys.crx using the private key |
6 // ../enterprise_platform_keys.pem . | 6 // ../enterprise_platform_keys.pem . |
7 | 7 |
| 8 'use strict'; |
| 9 |
8 var assertEq = chrome.test.assertEq; | 10 var assertEq = chrome.test.assertEq; |
9 var assertTrue = chrome.test.assertTrue; | 11 var assertTrue = chrome.test.assertTrue; |
10 var assertThrows = chrome.test.assertThrows; | 12 var assertThrows = chrome.test.assertThrows; |
11 var fail = chrome.test.fail; | 13 var fail = chrome.test.fail; |
12 var succeed = chrome.test.succeed; | 14 var succeed = chrome.test.succeed; |
13 var callbackPass = chrome.test.callbackPass; | 15 var callbackPass = chrome.test.callbackPass; |
14 var callbackFail= chrome.test.callbackFail; | 16 var callbackFail= chrome.test.callbackFail; |
15 | 17 |
16 // openssl req -new -x509 -key privkey.pem \ | 18 // openssl req -new -x509 -key privkey.pem \ |
17 // -outform der -out cert.der -days 36500 | 19 // -outform der -out cert.der -days 36500 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 assertTrue(!!chrome.enterprise.platformKeys, "No platformKeys namespace."); | 245 assertTrue(!!chrome.enterprise.platformKeys, "No platformKeys namespace."); |
244 assertTrue(!!chrome.enterprise.platformKeys.getTokens, | 246 assertTrue(!!chrome.enterprise.platformKeys.getTokens, |
245 "No getTokens function."); | 247 "No getTokens function."); |
246 assertTrue(!!chrome.enterprise.platformKeys.importCertificate, | 248 assertTrue(!!chrome.enterprise.platformKeys.importCertificate, |
247 "No importCertificate function."); | 249 "No importCertificate function."); |
248 assertTrue(!!chrome.enterprise.platformKeys.removeCertificate, | 250 assertTrue(!!chrome.enterprise.platformKeys.removeCertificate, |
249 "No removeCertificate function."); | 251 "No removeCertificate function."); |
250 | 252 |
251 getUserToken(function(userToken) { | 253 getUserToken(function(userToken) { |
252 if (!userToken) | 254 if (!userToken) |
253 chrome.test.fail('no user token'); | 255 fail('no user token'); |
254 if (userToken.id != 'user') | 256 if (userToken.id != 'user') |
255 chrome.test.fail('token is not named "user".'); | 257 fail('token is not named "user".'); |
256 | 258 |
257 callback(userToken); | 259 callback(userToken); |
258 }); | 260 }); |
259 } | 261 } |
260 | 262 |
261 function runTests(userToken) { | 263 function runTests(userToken) { |
262 chrome.test.runTests([ | 264 chrome.test.runTests([ |
263 function hasSubtleCryptoMethods() { | 265 function hasSubtleCryptoMethods() { |
264 assertTrue(!!userToken.subtleCrypto.generateKey, | 266 assertTrue(!!userToken.subtleCrypto.generateKey, |
265 "user token has no generateKey method"); | 267 "user token has no generateKey method"); |
266 assertTrue(!!userToken.subtleCrypto.sign, | 268 assertTrue(!!userToken.subtleCrypto.sign, |
267 "user token has no sign method"); | 269 "user token has no sign method"); |
268 assertTrue(!!userToken.subtleCrypto.exportKey, | 270 assertTrue(!!userToken.subtleCrypto.exportKey, |
269 "user token has no exportKey method"); | 271 "user token has no exportKey method"); |
270 succeed(); | 272 succeed(); |
271 }, | 273 }, |
272 function initiallyNoCerts() { assertCertsStored(userToken, []); }, | 274 function initiallyNoCerts() { assertCertsStored(userToken, []); }, |
273 | 275 |
274 // Generates a key and sign some data with it. Verifies the signature using | 276 // Generates a key and sign some data with it. Verifies the signature using |
275 // WebCrypto. | 277 // WebCrypto. |
276 function generateKeyAndSign() { | 278 function generateKeyAndSign() { |
277 var algorithm = { | 279 var algorithm = { |
278 name: "RSASSA-PKCS1-v1_5", | 280 name: "RSASSA-PKCS1-v1_5", |
279 // RsaHashedKeyGenParams | 281 // RsaHashedKeyGenParams |
280 modulusLength: 512, | 282 modulusLength: 512, |
281 publicExponent: | 283 // Equivalent to 65537 |
282 new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 | 284 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), |
283 hash: { | 285 hash: { |
284 name: "SHA-1", | 286 name: "SHA-1", |
285 } | 287 } |
286 }; | 288 }; |
| 289 // Ensure that this algorithm object is not modified, so that later |
| 290 // comparisons really do the right thing. |
| 291 Object.freeze(algorithm.hash); |
| 292 Object.freeze(algorithm); |
| 293 |
| 294 var signParams = {name: 'RSASSA-PKCS1-v1_5'}; |
| 295 |
287 // Some random data to sign. | 296 // Some random data to sign. |
288 var data = new Uint8Array([0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]); | 297 var data = new Uint8Array([0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]); |
289 var cachedKeyPair; | 298 var cachedKeyPair; |
290 var cachedSpki; | 299 var cachedSpki; |
291 var cachedSignature; | 300 var cachedSignature; |
292 userToken.subtleCrypto.generateKey(algorithm, false, ["sign"]) | 301 userToken.subtleCrypto.generateKey(algorithm, false, ["sign"]) |
293 .then(callbackPass(function(keyPair) { | 302 .then(callbackPass(function(keyPair) { |
294 assertTrue(!!keyPair, "No key pair."); | 303 assertTrue(!!keyPair, "No key pair."); |
295 cachedKeyPair = keyPair; | 304 cachedKeyPair = keyPair; |
296 return userToken.subtleCrypto.exportKey('spki', | 305 return userToken.subtleCrypto.exportKey('spki', |
297 keyPair.publicKey); | 306 keyPair.publicKey); |
298 }), | 307 }), |
299 function(error) { | 308 function(error) { fail("GenerateKey failed: " + error); }) |
300 assertTrue(false, "GenerateKey failed: " + error); | |
301 }) | |
302 .then(callbackPass(function(publicKeySpki) { | 309 .then(callbackPass(function(publicKeySpki) { |
| 310 // Ensure that the returned key pair has the expected format. |
| 311 // Checks depending on the generateKey arguments: |
| 312 var privateKey = cachedKeyPair.privateKey; |
| 313 assertEq(['sign'], privateKey.usages); |
| 314 assertEq(algorithm, privateKey.algorithm); |
| 315 |
| 316 var publicKey = cachedKeyPair.publicKey; |
| 317 assertEq([], publicKey.usages); |
| 318 assertEq(algorithm, publicKey.algorithm); |
| 319 |
303 cachedSpki = publicKeySpki; | 320 cachedSpki = publicKeySpki; |
304 var signParams = {name: 'RSASSA-PKCS1-v1_5'}; | |
305 return userToken.subtleCrypto.sign( | 321 return userToken.subtleCrypto.sign( |
306 signParams, cachedKeyPair.privateKey, data); | 322 signParams, privateKey, data); |
307 }), | 323 }), |
308 function(error) { | 324 function(error) { fail("Export failed: " + error); }) |
309 assertTrue(false, "Export failed: " + error); | |
310 }) | |
311 .then(callbackPass(function(signature) { | 325 .then(callbackPass(function(signature) { |
312 var importParams = { | 326 var importParams = { |
313 name: algorithm.name, | 327 name: algorithm.name, |
314 // RsaHashedImportParams | 328 // RsaHashedImportParams |
315 hash: { | 329 hash: { |
316 name: "SHA-1", | 330 name: "SHA-1", |
317 } | 331 } |
318 }; | 332 }; |
319 assertTrue(!!signature, "No signature."); | 333 assertTrue(!!signature, "No signature."); |
320 assertTrue(signature.length != 0, "Signature is empty."); | 334 assertTrue(signature.length != 0, "Signature is empty."); |
321 cachedSignature = signature; | 335 cachedSignature = signature; |
322 return window.crypto.subtle.importKey( | 336 return window.crypto.subtle.importKey( |
323 "spki", cachedSpki, importParams, false, ["verify"]); | 337 "spki", cachedSpki, importParams, false, ["verify"]); |
324 }), | 338 }), |
325 function(error) { assertTrue(false, "Sign failed: " + error); }) | 339 function(error) { fail("Sign failed: " + error); }) |
326 .then(callbackPass(function(webCryptoPublicKey) { | 340 .then(callbackPass(function(webCryptoPublicKey) { |
327 assertTrue(!!webCryptoPublicKey); | 341 assertTrue(!!webCryptoPublicKey); |
328 assertEq(algorithm.modulusLength, | 342 assertEq(algorithm.modulusLength, |
329 webCryptoPublicKey.algorithm.modulusLength); | 343 webCryptoPublicKey.algorithm.modulusLength); |
330 assertEq(algorithm.publicExponent, | 344 assertEq(algorithm.publicExponent, |
331 webCryptoPublicKey.algorithm.publicExponent); | 345 webCryptoPublicKey.algorithm.publicExponent); |
332 return window.crypto.subtle.verify( | 346 return window.crypto.subtle.verify( |
333 algorithm, webCryptoPublicKey, cachedSignature, data); | 347 algorithm, webCryptoPublicKey, cachedSignature, data); |
334 }), | 348 }), |
335 function(error) { | 349 function(error) { fail("Import failed: " + error); }) |
336 assertTrue(false, "Import failed: " + error); | |
337 }) | |
338 .then(callbackPass(function(success) { | 350 .then(callbackPass(function(success) { |
339 assertEq(true, success, "Signature invalid."); | 351 assertEq(true, success, "Signature invalid."); |
340 // Try to sign data with the same key a second time, which | 352 // Try to sign data with the same key a second time, which |
341 // must fail. | 353 // must fail. |
342 return userToken.subtleCrypto.sign( | 354 return userToken.subtleCrypto.sign( |
343 {}, cachedKeyPair.privateKey, data); | 355 signParams, cachedKeyPair.privateKey, data); |
344 }), | 356 }), |
345 function(error) { | 357 function(error) { fail("Verification failed: " + error); }) |
346 assertTrue(false, "Verification failed: " + error); | |
347 }) | |
348 .then(function(signature) { | 358 .then(function(signature) { |
349 assertTrue(false, "Second sign call was expected to fail."); | 359 fail("Second sign call was expected to fail."); |
350 }, callbackPass(function(error) { | 360 }, callbackPass(function(error) { |
351 assertTrue(error instanceof Error); | 361 assertTrue(error instanceof Error); |
352 assertEq( | 362 assertEq( |
353 'The operation failed for an operation-specific reason', | 363 'The operation failed for an operation-specific reason', |
354 error.message); | 364 error.message); |
355 })); | 365 })); |
356 }, | 366 }, |
357 | 367 |
358 // Imports and removes certificates for privateKeyPkcs8, which was imported | 368 // Imports and removes certificates for privateKeyPkcs8, which was imported |
359 // by on C++'s side. | 369 // by on C++'s side. |
(...skipping 25 matching lines...) Expand all Loading... |
385 function algorithmParameterMissingModulusLength() { | 395 function algorithmParameterMissingModulusLength() { |
386 var algorithm = { | 396 var algorithm = { |
387 name: "RSASSA-PKCS1-v1_5", | 397 name: "RSASSA-PKCS1-v1_5", |
388 // Equivalent to 65537 | 398 // Equivalent to 65537 |
389 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), | 399 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), |
390 hash: { | 400 hash: { |
391 name: "SHA-1", | 401 name: "SHA-1", |
392 } | 402 } |
393 }; | 403 }; |
394 userToken.subtleCrypto.generateKey(algorithm, false, ['sign']).then( | 404 userToken.subtleCrypto.generateKey(algorithm, false, ['sign']).then( |
395 function(keyPair) { | 405 function(keyPair) { fail('generateKey was expected to fail'); }, |
396 assertTrue(false, 'generateKey was expected to fail'); | |
397 }, | |
398 callbackPass(function(error) { | 406 callbackPass(function(error) { |
399 assertTrue(error instanceof Error); | 407 assertTrue(error instanceof Error); |
400 assertEq('A required parameter was missing or out-of-range', | 408 assertEq('A required parameter was missing or out-of-range', |
401 error.message); | 409 error.message); |
402 })); | 410 })); |
403 }, | 411 }, |
404 | 412 |
405 // Call generate key with invalid algorithm parameter, missing hash. | 413 // Call generate key with invalid algorithm parameter, missing hash. |
406 function algorithmParameterMissingHash() { | 414 function algorithmParameterMissingHash() { |
407 var algorithm = { | 415 var algorithm = { |
408 name: 'RSASSA-PKCS1-v1_5', | 416 name: 'RSASSA-PKCS1-v1_5', |
409 modulusLength: 512, | 417 modulusLength: 512, |
410 // Equivalent to 65537 | 418 // Equivalent to 65537 |
411 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), | 419 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), |
412 }; | 420 }; |
413 userToken.subtleCrypto.generateKey(algorithm, false, ['sign']).then( | 421 userToken.subtleCrypto.generateKey(algorithm, false, ['sign']).then( |
414 function(keyPair) { | 422 function(keyPair) { fail('generateKey was expected to fail'); }, |
415 assertTrue(false, 'generateKey was expected to fail'); | |
416 }, | |
417 callbackPass(function(error) { | 423 callbackPass(function(error) { |
418 assertEq( | 424 assertEq( |
419 new Error('Error: A required parameter was missing our out-of-range'), | 425 new Error('Error: A required parameter was missing our out-of-range'), |
420 error); | 426 error); |
421 })); | 427 })); |
422 }, | 428 }, |
423 | 429 |
424 // Call generate key with invalid algorithm parameter, unsupported public | 430 // Call generate key with invalid algorithm parameter, unsupported public |
425 // exponent. | 431 // exponent. |
426 function algorithmParameterUnsupportedPublicExponent() { | 432 function algorithmParameterUnsupportedPublicExponent() { |
427 var algorithm = { | 433 var algorithm = { |
428 name: 'RSASSA-PKCS1-v1_5', | 434 name: 'RSASSA-PKCS1-v1_5', |
429 modulusLength: 512, | 435 modulusLength: 512, |
430 // Different from 65537. | 436 // Different from 65537. |
431 publicExponent: new Uint8Array([0x01, 0x01]), | 437 publicExponent: new Uint8Array([0x01, 0x01]), |
432 }; | 438 }; |
433 userToken.subtleCrypto.generateKey(algorithm, false, ['sign']).then( | 439 userToken.subtleCrypto.generateKey(algorithm, false, ['sign']).then( |
434 function(keyPair) { | 440 function(keyPair) { fail('generateKey was expected to fail'); }, |
435 assertTrue(false, 'generateKey was expected to fail'); | |
436 }, | |
437 callbackPass(function(error) { | 441 callbackPass(function(error) { |
438 assertTrue(error instanceof Error); | 442 assertTrue(error instanceof Error); |
439 assertEq('A required parameter was missing or out-of-range', | 443 assertEq('A required parameter was missing or out-of-range', |
440 error.message); | 444 error.message); |
441 })); | 445 })); |
442 }, | 446 }, |
443 | 447 |
444 // Imports a certificate for which now private key was imported/generated | 448 // Imports a certificate for which now private key was imported/generated |
445 // before. | 449 // before. |
446 function missingPrivateKey() { | 450 function missingPrivateKey() { |
(...skipping 21 matching lines...) Expand all Loading... |
468 callbackFail('Certificate is not a valid X.509 certificate.')); | 472 callbackFail('Certificate is not a valid X.509 certificate.')); |
469 }, | 473 }, |
470 function getCertsInvalidToken() { | 474 function getCertsInvalidToken() { |
471 chrome.enterprise.platformKeys.getCertificates( | 475 chrome.enterprise.platformKeys.getCertificates( |
472 'invalid token id', callbackFail('The token is not valid.')); | 476 'invalid token id', callbackFail('The token is not valid.')); |
473 } | 477 } |
474 ]); | 478 ]); |
475 } | 479 } |
476 | 480 |
477 beforeTests(runTests); | 481 beforeTests(runTests); |
OLD | NEW |