OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var assertEq = chrome.test.assertEq; |
| 6 var assertTrue = chrome.test.assertTrue; |
| 7 var assertThrows = chrome.test.assertThrows; |
| 8 var fail = chrome.test.fail; |
| 9 var succeed = chrome.test.succeed; |
| 10 var callback = chrome.test.callback; |
| 11 var callbackPass = chrome.test.callbackPass; |
| 12 var callbackFail = chrome.test.callbackFail; |
| 13 |
| 14 // openssl genrsa > privkey.pem |
| 15 // openssl pkcs8 -inform pem -in privkey.pem -topk8 \ |
| 16 // -outform der -out privkey8.der -nocrypt |
| 17 // xxd -i privkey8.der |
| 18 var privateKeyDer = new Uint8Array([ |
| 19 0x30, 0x82, 0x01, 0x56, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, |
| 20 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, |
| 21 0x01, 0x40, 0x30, 0x82, 0x01, 0x3c, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, |
| 22 0xac, 0x6c, 0x72, 0x46, 0xa2, 0xde, 0x88, 0x30, 0x54, 0x06, 0xad, 0xc7, |
| 23 0x2d, 0x64, 0x6e, 0xf6, 0x0f, 0x72, 0x3e, 0x92, 0x31, 0xcc, 0x0b, 0xa0, |
| 24 0x18, 0x20, 0xb0, 0xdb, 0x86, 0xab, 0x11, 0xc6, 0xa5, 0x78, 0xea, 0x64, |
| 25 0xe8, 0xeb, 0xa5, 0xb3, 0x78, 0x5d, 0xbb, 0x10, 0x57, 0xe6, 0x12, 0x23, |
| 26 0x89, 0x92, 0x1d, 0xa0, 0xe5, 0x1e, 0xd1, 0xc9, 0x0e, 0x62, 0xcb, 0xc9, |
| 27 0xaf, 0xde, 0x4e, 0x83, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x43, |
| 28 0xa4, 0x2f, 0x98, 0x82, 0x60, 0x66, 0x70, 0xff, 0x25, 0xb7, 0x9a, 0xc1, |
| 29 0xb5, 0xfa, 0xc9, 0xd0, 0xf9, 0x72, 0x8e, 0x8b, 0xb6, 0x8e, 0x80, 0xf9, |
| 30 0xb6, 0x29, 0x6b, 0x95, 0x83, 0x1a, 0xef, 0x7b, 0x98, 0xf1, 0x91, 0x52, |
| 31 0xe8, 0x07, 0x8e, 0x6d, 0x3f, 0xca, 0x37, 0x12, 0x3b, 0x77, 0xc6, 0x65, |
| 32 0xa0, 0xfd, 0xf6, 0x58, 0x24, 0xfd, 0x88, 0xb2, 0x56, 0x99, 0x7c, 0x3f, |
| 33 0x3b, 0x27, 0x31, 0x02, 0x21, 0x00, 0xe5, 0x16, 0x34, 0x1c, 0x60, 0x35, |
| 34 0x6d, 0x54, 0x4d, 0xe2, 0xc6, 0x40, 0xf4, 0xc5, 0x19, 0x1b, 0x58, 0xbe, |
| 35 0x30, 0xca, 0x72, 0xc7, 0x03, 0x31, 0x74, 0xe8, 0xf7, 0xad, 0xf5, 0xdd, |
| 36 0xba, 0x8f, 0x02, 0x21, 0x00, 0xc0, 0xae, 0x18, 0xb2, 0xa2, 0x26, 0x22, |
| 37 0x7b, 0x32, 0x8b, 0x21, 0xd3, 0xfb, 0xa5, 0xe6, 0xca, 0xae, 0x55, 0xab, |
| 38 0xf6, 0x8c, 0x32, 0xd4, 0x40, 0x41, 0x8f, 0x89, 0x7b, 0x02, 0x8e, 0x76, |
| 39 0xcd, 0x02, 0x21, 0x00, 0xbf, 0x67, 0xa6, 0xd0, 0x92, 0x9d, 0xf1, 0x24, |
| 40 0x00, 0x13, 0x5a, 0xbc, 0x09, 0x06, 0xe9, 0x2c, 0x4d, 0x20, 0x53, 0x99, |
| 41 0xc2, 0xfe, 0x7e, 0xaf, 0x82, 0xd3, 0xe7, 0xa5, 0x64, 0x89, 0x07, 0x07, |
| 42 0x02, 0x21, 0x00, 0xc0, 0x26, 0x9e, 0xd0, 0xbe, 0x6e, 0x03, 0x63, 0x13, |
| 43 0x03, 0xb7, 0x87, 0x9c, 0xe5, 0x54, 0xe4, 0xf9, 0xb8, 0xcc, 0x56, 0x82, |
| 44 0x78, 0x4a, 0x3d, 0x8e, 0x64, 0x23, 0xb9, 0xbd, 0xd1, 0xef, 0x5d, 0x02, |
| 45 0x21, 0x00, 0xdd, 0x15, 0xd3, 0x12, 0xe2, 0x85, 0xcf, 0x41, 0x70, 0xad, |
| 46 0xeb, 0xf6, 0xa0, 0x6d, 0xe6, 0x78, 0x10, 0x44, 0xdf, 0xce, 0xc4, 0x62, |
| 47 0x25, 0x3a, 0x98, 0x46, 0x9e, 0x7c, 0xc6, 0x01, 0x6c, 0x3d |
| 48 ]); |
| 49 |
| 50 // openssl req -new -x509 -key privkey.pem -out cacert.pem -days 36500 |
| 51 var certDer = new Uint8Array([ |
| 52 0x30, 0x82, 0x01, 0xd5, 0x30, 0x82, 0x01, 0x7f, 0xa0, 0x03, 0x02, 0x01, |
| 53 0x02, 0x02, 0x09, 0x00, 0x9e, 0x11, 0x7e, 0xff, 0x43, 0x84, 0xd4, 0xe6, |
| 54 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, |
| 55 0x05, 0x05, 0x00, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, |
| 56 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, |
| 57 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, |
| 58 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, |
| 59 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, |
| 60 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, |
| 61 0x74, 0x64, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x30, 0x37, |
| 62 0x31, 0x35, 0x35, 0x30, 0x30, 0x38, 0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31, |
| 63 0x34, 0x30, 0x33, 0x31, 0x34, 0x31, 0x35, 0x35, 0x30, 0x30, 0x38, 0x5a, |
| 64 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, |
| 65 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, |
| 66 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, |
| 67 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, |
| 68 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, |
| 69 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, |
| 70 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, |
| 71 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00, |
| 72 0xac, 0x6c, 0x72, 0x46, 0xa2, 0xde, 0x88, 0x30, 0x54, 0x06, 0xad, 0xc7, |
| 73 0x2d, 0x64, 0x6e, 0xf6, 0x0f, 0x72, 0x3e, 0x92, 0x31, 0xcc, 0x0b, 0xa0, |
| 74 0x18, 0x20, 0xb0, 0xdb, 0x86, 0xab, 0x11, 0xc6, 0xa5, 0x78, 0xea, 0x64, |
| 75 0xe8, 0xeb, 0xa5, 0xb3, 0x78, 0x5d, 0xbb, 0x10, 0x57, 0xe6, 0x12, 0x23, |
| 76 0x89, 0x92, 0x1d, 0xa0, 0xe5, 0x1e, 0xd1, 0xc9, 0x0e, 0x62, 0xcb, 0xc9, |
| 77 0xaf, 0xde, 0x4e, 0x83, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x50, 0x30, |
| 78 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, |
| 79 0x75, 0x6c, 0x61, 0xfb, 0xb0, 0x6e, 0x37, 0x32, 0x41, 0x62, 0x3b, 0x55, |
| 80 0xbd, 0x5f, 0x6b, 0xe0, 0xdb, 0xb9, 0xc7, 0xec, 0x30, 0x1f, 0x06, 0x03, |
| 81 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x75, 0x6c, 0x61, |
| 82 0xfb, 0xb0, 0x6e, 0x37, 0x32, 0x41, 0x62, 0x3b, 0x55, 0xbd, 0x5f, 0x6b, |
| 83 0xe0, 0xdb, 0xb9, 0xc7, 0xec, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, |
| 84 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a, |
| 85 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x41, |
| 86 0x00, 0xa5, 0xe8, 0x9d, 0x3d, 0xc4, 0x1a, 0x6e, 0xd2, 0x92, 0x42, 0x37, |
| 87 0xb9, 0x3a, 0xb3, 0x8e, 0x2f, 0x55, 0xb5, 0xf2, 0xe4, 0x6e, 0x39, 0x0d, |
| 88 0xa8, 0xba, 0x10, 0x43, 0x57, 0xdd, 0x4e, 0x4e, 0x52, 0xc6, 0xbe, 0x07, |
| 89 0xdb, 0x83, 0x05, 0x97, 0x97, 0xc1, 0x7b, 0xd5, 0x5c, 0x50, 0x64, 0x0f, |
| 90 0x96, 0xff, 0x3d, 0x83, 0x37, 0x8f, 0x3a, 0x85, 0x08, 0x62, 0x5c, 0xb1, |
| 91 0x2f, 0x68, 0xb2, 0x4a, 0x4a |
| 92 ]); |
| 93 |
| 94 var defaultRsaAlgorithm = { |
| 95 name: "RSASSA-PKCS1-v1_5", |
| 96 modulusLength: 512, |
| 97 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // Equivalent to 65537 |
| 98 hash: {name: "SHA-256"} |
| 99 }; |
| 100 |
| 101 function getUserToken(callback) { |
| 102 chrome.enterprise.certificates.getTokens(function(tokens) { |
| 103 for (var i = 0; i < tokens.length; i++) { |
| 104 if (tokens[i].name == 'User') { |
| 105 callback(tokens[i]); |
| 106 return; |
| 107 } |
| 108 } |
| 109 callback(undefined); |
| 110 }) |
| 111 }; |
| 112 |
| 113 assertTrue(!!window.crypto.subtle); |
| 114 assertTrue(!!chrome.enterprise); |
| 115 assertTrue(!!chrome.enterprise.certificates); |
| 116 assertTrue(!!chrome.enterprise.certificates.getTokens); |
| 117 |
| 118 getUserToken(function(userToken) { |
| 119 if (!userToken) |
| 120 chrome.test.fail('no user token'); |
| 121 if (userToken.name != 'User') |
| 122 chrome.test.fail('token is not named "User".'); |
| 123 |
| 124 chrome.test.runTests([ |
| 125 function importClientCertSuccessfully() { |
| 126 var importAlgorithm = { |
| 127 name: "RSASSA-PKCS1-v1_5", hash: {name: "SHA-256"} |
| 128 }; |
| 129 window.crypto.subtle |
| 130 .importKey('pkcs8', privateKeyDer, importAlgorithm, false, ["sign"]) |
| 131 .then(callback(function(key) { |
| 132 userToken.importClientCertificate( |
| 133 key, certDer.buffer, callbackPass()); |
| 134 }), |
| 135 fail) |
| 136 }, |
| 137 function importClientCertPublicKey() { |
| 138 window.crypto.subtle.generateKey(defaultRsaAlgorithm, false, ["sign"]) |
| 139 .then(callback(function(keyPair) { |
| 140 assertThrows(userToken.importClientCertificate, |
| 141 userToken, |
| 142 [keyPair.publicKey, certDer.buffer, fail], |
| 143 'Key is not a private key.'); |
| 144 assertThrows(userToken.importClientCertificate, |
| 145 userToken, |
| 146 [keyPair.publicKey, certDer.buffer, fail], |
| 147 'Key is not a private key.'); |
| 148 }), |
| 149 fail); |
| 150 }, |
| 151 function importClientCertSymmetricKey() { |
| 152 var symmetricAlgorithm = { |
| 153 name: "HMAC", length: 128, hash: {name: "SHA-1"} |
| 154 }; |
| 155 window.crypto.subtle.generateKey(symmetricAlgorithm, false, ["sign"]) |
| 156 .then(callback(function(aesKey) { |
| 157 assertThrows(userToken.importClientCertificate, |
| 158 userToken, |
| 159 [aesKey, certDer.buffer, fail], |
| 160 'Key is not a private key.'); |
| 161 }), |
| 162 fail); |
| 163 }, |
| 164 function importClientCertWrongUsage() { |
| 165 window.crypto.subtle.generateKey(defaultRsaAlgorithm, false, ["encrypt"]) |
| 166 .then(callback(function(keyPair) { |
| 167 assertThrows(userToken.importClientCertificate, |
| 168 userToken, |
| 169 [keyPair.privateKey, certDer.buffer, fail], |
| 170 'Key usage prohibits signing.'); |
| 171 }), |
| 172 fail); |
| 173 }, |
| 174 function importClientCertWrongKeyType() { |
| 175 assertThrows(userToken.importClientCertificate, |
| 176 userToken, |
| 177 ['thisIsNotAKey', certDer.buffer, fail], |
| 178 /Invocation of form.*doesn't match definition/); |
| 179 succeed(); |
| 180 }, |
| 181 ]); |
| 182 }); |
OLD | NEW |