| OLD | NEW | 
 | (Empty) | 
|   1 <!DOCTYPE html> |  | 
|   2 <html> |  | 
|   3 <head> |  | 
|   4 <script src="../resources/js-test.js"></script> |  | 
|   5 <script src="resources/common.js"></script> |  | 
|   6 </head> |  | 
|   7 <body> |  | 
|   8 <p id="description"></p> |  | 
|   9 <div id="console"></div> |  | 
|  10  |  | 
|  11 <script> |  | 
|  12 description("Test generating, importing and exporting RSA keys for RSA-OAEP. Tes
    t that they can't be used with another algorithm."); |  | 
|  13  |  | 
|  14 jsTestIsAsync = true; |  | 
|  15  |  | 
|  16 var algorithmKeyGen = { |  | 
|  17     name: "RSA-OAEP", |  | 
|  18     hash: {name: "sha-1"}, |  | 
|  19     // RsaKeyGenParams |  | 
|  20     modulusLength: 512, |  | 
|  21     publicExponent: new Uint8Array([0x01, 0x00, 0x01]),  // Equivalent to 65537 |  | 
|  22 }; |  | 
|  23 var extractable = true; |  | 
|  24  |  | 
|  25 debug("Generating a key pair..."); |  | 
|  26 crypto.subtle.generateKey(algorithmKeyGen, extractable, ["encrypt", "decrypt", "
    wrapKey", "unwrapKey"]).then(function(result) { |  | 
|  27     keyPair = result; |  | 
|  28     shouldBe("keyPair.toString()", "'[object Object]'"); |  | 
|  29     shouldBe("keyPair.publicKey.type", "'public'"); |  | 
|  30     shouldBe("keyPair.publicKey.algorithm.name", "'RSA-OAEP'"); |  | 
|  31     shouldBe("keyPair.publicKey.algorithm.modulusLength", "512"); |  | 
|  32     shouldBe("keyPair.publicKey.usages", '["encrypt", "wrapKey"]'); |  | 
|  33     shouldBe("bytesToHexString(keyPair.publicKey.algorithm.publicExponent)", "'0
    10001'"); |  | 
|  34     shouldBe("keyPair.publicKey.algorithm.hash.name", "'SHA-1'"); |  | 
|  35     shouldBe("keyPair.privateKey.type", "'private'"); |  | 
|  36     shouldBe("keyPair.privateKey.algorithm.name", "'RSA-OAEP'"); |  | 
|  37     shouldBe("keyPair.privateKey.algorithm.modulusLength", "512"); |  | 
|  38     shouldBe("keyPair.privateKey.usages", '["decrypt", "unwrapKey"]'); |  | 
|  39     shouldBe("bytesToHexString(keyPair.privateKey.algorithm.publicExponent)", "'
    010001'"); |  | 
|  40     shouldBe("keyPair.privateKey.algorithm.hash.name", "'SHA-1'"); |  | 
|  41  |  | 
|  42     debug("\nTesting that the keys can't be used with different algorithms..."); |  | 
|  43     iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f"); |  | 
|  44  |  | 
|  45     return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.privateKey, 
    hexStringToUint8Array("00")); |  | 
|  46 }, failAndFinishJSTest).then(failAndFinishJSTest, function(result) { |  | 
|  47     logError(result); |  | 
|  48     return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.publicKey, h
    exStringToUint8Array("00")); |  | 
|  49 }, failAndFinishJSTest).then(failAndFinishJSTest, function(result) { |  | 
|  50     logError(result); |  | 
|  51     debug("\nTrying to export keys to raw..."); |  | 
|  52     return crypto.subtle.exportKey('raw', keyPair.publicKey); |  | 
|  53 }, failAndFinishJSTest).then(failAndFinishJSTest, function(result) { |  | 
|  54     logError(result); |  | 
|  55     testPassed("Promise rejected for exporting public key"); |  | 
|  56     return crypto.subtle.exportKey('raw', keyPair.privateKey); |  | 
|  57 }).then(failAndFinishJSTest, function(result) { |  | 
|  58     logError(result); |  | 
|  59     testPassed("Promise rejected for exporting private key"); |  | 
|  60  |  | 
|  61     debug("\nExporting public key to JWK..."); |  | 
|  62     return crypto.subtle.exportKey("jwk", keyPair.publicKey); |  | 
|  63 }).then(function(result) { |  | 
|  64     jwkPublicKey = result; |  | 
|  65     shouldBe("jwkPublicKey.alg", "'RSA-OAEP'"); |  | 
|  66     shouldBe("jwkPublicKey.ext", "true"); |  | 
|  67     shouldBe("jwkPublicKey.key_ops", "['encrypt', 'wrapKey']"); |  | 
|  68     shouldBe("jwkPublicKey.use", "undefined"); |  | 
|  69     shouldBe("jwkPublicKey.kty", "'RSA'"); |  | 
|  70     shouldBe("bytesToHexString(Base64URL.parse(jwkPublicKey.e))", "'010001'"); |  | 
|  71  |  | 
|  72     debug("\nImporting it back..."); |  | 
|  73     return crypto.subtle.importKey("jwk", jwkPublicKey, { name: "RSA-OAEP", hash
    : {name: "sha-1"} }, extractable, ["encrypt", "wrapKey"]); |  | 
|  74 }).then(function(result) { |  | 
|  75     exportedPublicKey = result; |  | 
|  76     shouldBe("exportedPublicKey.type", "'public'"); |  | 
|  77     shouldBe("exportedPublicKey.algorithm.name", "'RSA-OAEP'"); |  | 
|  78     shouldBe("exportedPublicKey.algorithm.modulusLength", "512"); |  | 
|  79     shouldBe("bytesToHexString(exportedPublicKey.algorithm.publicExponent)", "'0
    10001'"); |  | 
|  80     shouldBe("exportedPublicKey.algorithm.hash.name", "'SHA-1'"); |  | 
|  81     shouldBe("exportedPublicKey.extractable", "true"); |  | 
|  82     shouldBe("exportedPublicKey.usages", "['encrypt','wrapKey']"); |  | 
|  83  |  | 
|  84 }).then(finishJSTest, failAndFinishJSTest); |  | 
|  85  |  | 
|  86 </script> |  | 
|  87  |  | 
|  88 </body> |  | 
|  89 </html> |  | 
| OLD | NEW |