1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
16 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>QUnit for PKCS5 private key reader 'pkcs5pkey.js'</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<!-- script type="text/javascript" src="qunit.js" /script -->
<!-- link rel="stylesheet" href="qunit.css" type="text/css" media="screen" -->
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css" type="text/css" media="screen"/>
<script language="JavaScript" type="text/javascript" src="../ext/jsbn.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/jsbn2.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/prng4.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rng.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/base64.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rsa.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rsa2.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/sha1.js"></script>
<script language="JavaScript" type="text/javascript" src="../asn1hex-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="../rsapem-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="../rsasign-1.2.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
<script language="JavaScript" type="text/javascript" src="../asn1-1.0.js"></script>
<!-- jsrsasign pkcs5pkey external codes -->
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/x64-core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/cipher-core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/tripledes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/md5.js"></script>
<!-- for PKCS5 PBKDF2 -->
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/hmac.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pbkdf2.js"></script>
<script src="../pkcs5pkey-1.0.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
var sk10pem = "" +
"-----BEGIN RSA PRIVATE KEY-----\r\n" +
"Proc-Type: 4,ENCRYPTED\r\n" +
"DEK-Info: AES-256-CBC,40555967F759530864FE022E257DE34E\r\n" +
"\r\n" +
"jV7uXajRw4cccDaliagcqiLOiQEUCe19l761pXRxzgQP+DH4rCi12T4puTdZyy6l\r\n" +
"eJHcSHZ/IC9N9iS3XNLgm/rRdUBahf0l1HOerHY76xIb74tB9v1S6sZe/IaOIYTO\r\n" +
"E/t63rzNmj33AzEup5xMyzuAninLL3jJGKwcwkTG5u+n8p7fuMPcML0L7beVBkvZ\r\n" +
"oXyySfMVB8h4u7yyZdhjU3O4K8QX1g1jntGJpQsh4ikfjM+3ddG/qaBrwzTBfC0C\r\n" +
"F9SUbv+Sz0nZX60PhAYEDhb7194ol7F5Y4QZuDhf6K6d1v8r5b9RqDo98INqwdsD\r\n" +
"1El24sGa3eBXi3lnpAytX7dOOBQKRh1uJyL3IuKyuFcNJj3OOGjBkR6imZwD895e\r\n" +
"qxLS+BASmyGm4DME6m+kltZ12LXwPgNU6+d+XQ4NXSA=\r\n" +
"-----END RSA PRIVATE KEY-----\r\n";
var sk10pem_passcode = "hogehoge";
var sk10b64 = "" +
"jV7uXajRw4cccDaliagcqiLOiQEUCe19l761pXRxzgQP+DH4rCi12T4puTdZyy6l" +
"eJHcSHZ/IC9N9iS3XNLgm/rRdUBahf0l1HOerHY76xIb74tB9v1S6sZe/IaOIYTO" +
"E/t63rzNmj33AzEup5xMyzuAninLL3jJGKwcwkTG5u+n8p7fuMPcML0L7beVBkvZ" +
"oXyySfMVB8h4u7yyZdhjU3O4K8QX1g1jntGJpQsh4ikfjM+3ddG/qaBrwzTBfC0C" +
"F9SUbv+Sz0nZX60PhAYEDhb7194ol7F5Y4QZuDhf6K6d1v8r5b9RqDo98INqwdsD" +
"1El24sGa3eBXi3lnpAytX7dOOBQKRh1uJyL3IuKyuFcNJj3OOGjBkR6imZwD895e" +
"qxLS+BASmyGm4DME6m+kltZ12LXwPgNU6+d+XQ4NXSA=";
var sk10dechex = "30820139020100024100b1e5aa7d6827b8c69037b269907bd8f4a7a1e6b798776e3be519fbda2966260dd56f6f3389d489a276a0a1e1b19e51328bc2d6a623f24be6b2e6718f3b292d5b02030100010240763582206ac15a4ab7320b5f921e797fb894205706fcf546df6970acfaad61c2db1fb6f9e335a1867c0f7c40a2ad39aa92b9312459bbf90d5eab831793c69321022100e584720c8f676623e94e7fb8616e667d66cd379559ffe36acd9068495e68adc5022100c66c72386fbdeded99c5963422d7380c19100757a4dcec581bd645d3d924409f0220101798337f3e7dafd8e4d319a763293673a5c1d6ebe7801775f0410f9bd9f50d02201bff2b329959a41b9549d2c2b273c97db37f9679a05267aa9aeae5d959570b23022043ea618b8f31af3aa762171cf2b5e9d563c5041a99a2217b6c692f807e18f81a";
var sk12pem = "" +
"-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n" +
"MIIBpjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIU9Y9p2EfWucCAggA\r\n" +
"MBQGCCqGSIb3DQMHBAhdNa9kLcmywQSCAWAoZaR4ib7mOcfpJQr7lyp/ume0srhJ\r\n" +
"6G1GuYaZzh/At3EHVrPtH9i8OaLiS/nIitWTdd1tITzWAIGNy1jDasvLUEk+agH2\r\n" +
"FGOuGfN86bNVUVbtHxkzpslNbcZcsNZtDvrDRJoZo2nAlcNW1tICpfzMTCBl/1Pl\r\n" +
"LNqit+IdcZVYRU8DJB0+AsaXevxBp7ifpthfHq2+9kCcSCqnonAH3pnsGlcBNinP\r\n" +
"M4L2QQ6YMMSRl7nCZNT4/AJg+OyKGzNgQpcRfgrxsQXA8/4GrAcMF0YqaRS6Am6M\r\n" +
"y42+85E5qsMYmQQu+PZDDOfmaNomwT5fA4TAp0nJP0zX8gBTBLywMubPlmuMWo4N\r\n" +
"XlHKSl4Wn1sQlJOhWOAIsamdtXYPryDcQGEeiHb9Y83f9di4e9JlViZVkv+iPcGv\r\n" +
"IjsZNp6zmlqf/RXnETsJjGd0TXRWaEdu+XOOyVyPskX2177X9DUJoD31\r\n" +
"-----END ENCRYPTED PRIVATE KEY-----\r\n";
var sk12pem_passcode = "hoge";
var sk12hex = "308201a6304006092a864886f70d01050d3033301b06092a864886f70d01050c300e040853d63da7611f5ae702020800301406082a864886f70d030704085d35af642dc9b2c1048201602865a47889bee639c7e9250afb972a7fba67b4b2b849e86d46b98699ce1fc0b7710756b3ed1fd8bc39a2e24bf9c88ad59375dd6d213cd600818dcb58c36acbcb50493e6a01f61463ae19f37ce9b3555156ed1f1933a6c94d6dc65cb0d66d0efac3449a19a369c095c356d6d202a5fccc4c2065ff53e52cdaa2b7e21d719558454f03241d3e02c6977afc41a7b89fa6d85f1eadbef6409c482aa7a27007de99ec1a57013629cf3382f6410e9830c49197b9c264d4f8fc0260f8ec8a1b33604297117e0af1b105c0f3fe06ac070c17462a6914ba026e8ccb8dbef39139aac31899042ef8f6430ce7e668da26c13e5f0384c0a749c93f4cd7f2005304bcb032e6cf966b8c5a8e0d5e51ca4a5e169f5b109493a158e008b1a99db5760faf20dc40611e8876fd63cddff5d8b87bd26556265592ffa23dc1af223b19369eb39a5a9ffd15e7113b098c67744d745668476ef9738ec95c8fb245f6d7bed7f43509a03df5"; // hexadecimal of k1.2.der
var sk12ciphertext = "2865a47889bee639c7e9250afb972a7fba67b4b2b849e86d46b98699ce1fc0b7710756b3ed1fd8bc39a2e24bf9c88ad59375dd6d213cd600818dcb58c36acbcb50493e6a01f61463ae19f37ce9b3555156ed1f1933a6c94d6dc65cb0d66d0efac3449a19a369c095c356d6d202a5fccc4c2065ff53e52cdaa2b7e21d719558454f03241d3e02c6977afc41a7b89fa6d85f1eadbef6409c482aa7a27007de99ec1a57013629cf3382f6410e9830c49197b9c264d4f8fc0260f8ec8a1b33604297117e0af1b105c0f3fe06ac070c17462a6914ba026e8ccb8dbef39139aac31899042ef8f6430ce7e668da26c13e5f0384c0a749c93f4cd7f2005304bcb032e6cf966b8c5a8e0d5e51ca4a5e169f5b109493a158e008b1a99db5760faf20dc40611e8876fd63cddff5d8b87bd26556265592ffa23dc1af223b19369eb39a5a9ffd15e7113b098c67744d745668476ef9738ec95c8fb245f6d7bed7f43509a03df5"; // hexadecimal encrypted key(=ciphertext) of k1.2.der
var sk12plainhex = "30820155020100300d06092a864886f70d01010105000482013f3082013b020100024100dc6d990d4a71c769ec704db7cce04da8b8f15bfad7b6e3e129e191c30df7db0243635a8ffb293a6e4a74599911c0d15ae8e72d9c84bd8dcf19ffee839fd051670203010001024100ac15f412e8378393323f4aad5f890d97d72ab6c0528039cfa33eb2e930927bab09994944a11e1cdcf4153f1c8d9c1825bdeed0a1e86b1c268b42e1c348ad3f61022100f474097558bfd1ab88f80737e07236ca25e93c4a38f1b37bf7138c84ebf94dfb022100e6d70ba0280830e2da884114184425cb4ffd02598e5167654ac8c5cb558d0a8502202c69c4aa2854b98063e344547797e8b2738edba054963d58e17c10c4b9a6d2ef02205712264072d924176d7697958cd1a7afd8ac0686e137b35871ace96e99066a41022100ec36209834b8826b005ba599d8c3fced3d50022b81303a9532faed7d70c07424"; // hexadecimal of k1.2.dec.der
// ======= TEST ===================================================================================
test("version", function() {
equal(PKCS5PKEY.version, "1.0.5", "check version.");
});
// k1.[0-1].{hex|pem|der}
test("parsePKCS5PEM_k1.0key", function() {
var info = PKCS5PKEY.parsePKCS5PEM(sk10pem, sk10pem_passcode);
var dataHex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(info.data));
expect(5);
equal(info.type, "RSA", "k1.0 type is 'RSA'.");
equal(info.cipher, "AES-256-CBC", "cipher attribute is the same");
equal(info.ivsalt, "40555967F759530864FE022E257DE34E", "ivsalt attribute is the same.");
equal(info.data, sk10b64, "data is the same");
equal(dataHex, "8d5eee5da8d1c3871c7036a589a81caa22ce89011409ed7d97beb5a57471ce040ff831f8ac28b5d93e29b93759cb2ea57891dc48767f202f4df624b75cd2e09bfad175405a85fd25d4739eac763beb121bef8b41f6fd52eac65efc868e2184ce13fb7adebccd9a3df703312ea79c4ccb3b809e29cb2f78c918ac1cc244c6e6efa7f29edfb8c3dc30bd0bedb795064bd9a17cb249f31507c878bbbcb265d8635373b82bc417d60d639ed189a50b21e2291f8ccfb775d1bfa9a06bc334c17c2d0217d4946eff92cf49d95fad0f8406040e16fbd7de2897b179638419b8385fe8ae9dd6ff2be5bf51a83a3df0836ac1db03d44976e2c19adde0578b7967a40cad5fb74e38140a461d6e2722f722e2b2b8570d263dce3868c1911ea2999c03f3de5eab12d2f810129b21a6e03304ea6fa496d675d8b5f03e0354ebe77e5d0e0d5d20", "data(hex) is the same");
});
test("getKeyAndUnusedIvByPasscodeAndIvsalt", function() {
var keyiv = PKCS5PKEY.getKeyAndUnusedIvByPasscodeAndIvsalt("AES-256-CBC", "hogehoge", "40555967F759530864FE022E257DE34E");
expect(2);
equal(keyiv.keyhex, "523c7720b6a5544d7ef212ccb5e9f78cddff8a91b8c335ce7219abbb44e6a883", "check version.");
equal(keyiv.ivhex, "0432604dae9830352f5a02736c8684e4", "check version.");
});
test("decryptKeyB64", function() {
var keyB64 = sk10b64;
var alg = "AES-256-CBC";
var keyhex = "523c7720b6a5544d7ef212ccb5e9f78cddff8a91b8c335ce7219abbb44e6a883";
var ivhex = "40555967f759530864fe022e257de34e";
//var ivhex = "0432604dae9830352f5a02736c8684e4";
var decrypted = PKCS5PKEY.decryptKeyB64(keyB64, alg, keyhex, ivhex);
equal(decrypted, sk10dechex, "key is the same.");
});
test("getDecryptedKeyHex_k10", function() {
var decrypted = PKCS5PKEY.getDecryptedKeyHex(sk10pem, "hogehoge");
equal(decrypted, sk10dechex, "key is the same.");
});
test("getRSAKeyFromEncryptedPKCS5PEM_k10", function() {
var k = PKCS5PKEY.getRSAKeyFromEncryptedPKCS5PEM(sk10pem, "hogehoge");
expect(1);
equal(k.e.toString(16), "10001", "e of key");
});
test("getRSAKeyFromPlainPKCS8PEM", function() {
var s = // =z1.prv.p8.pem
"-----BEGIN PRIVATE KEY-----\n" +
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA6GZN0rQFKRIVaPOz\n" +
"m8l6Yue6PAm6vcTw3NjfkOt5C5u2RaK3DjESdHtNPEG1FCSJURX++I951D6uWxpO\n" +
"NRj9WQIDAQABAkB4u8VMZGeV6aYjyw+RLH+faGFxFWDle63iHe0vfZV5+GJy+rnn\n" +
"GrJygsJ2DEBNrmrIo6uFLPa81WL5r/gkeiZNAiEA/pvUUIZrMlVhT96XanasCx1Y\n" +
"MN2mT1NZuUbYUSvBI58CIQDpq2gPXwXbsPQxGw5vQ2j0h0oSOJedi8YAw1xvIHB8\n" +
"BwIhANVJy2mNwX1P4w5ahPOt6GADPB7rf2fShkZcn9gX1Fs3AiAYSbS7REk7mJ0J\n" +
"LaLGdd9G63kLg85eldSy55uIAXsvqQIgfSYaliVtSbAgyx1Yfs3hJ+CTpNKzTNv/\n" +
"Fx80EltYV6k=\n" +
"-----END PRIVATE KEY-----\n";
var pkey = PKCS5PKEY.getRSAKeyFromPlainPKCS8PEM(s);
expect(3);
equal(pkey.n.toString(16), "e8664dd2b40529121568f3b39bc97a62e7ba3c09babdc4f0dcd8df90eb790b9bb645a2b70e3112747b4d3c41b51424895115fef88f79d43eae5b1a4e3518fd59", "n of key is the same.");
equal(pkey.e.toString(16), "10001", "e of key is the same.");
equal(pkey.d.toString(16), "78bbc54c646795e9a623cb0f912c7f9f6861711560e57bade21ded2f7d9579f86272fab9e71ab27282c2760c404dae6ac8a3ab852cf6bcd562f9aff8247a264d", "d of key is the same.");
});
test("getEryptedPKCS5PEMFromPrvKeyHex/1 key encryption", function() {
var p = PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(sk10dechex, "hogehoge", "AES-256-CBC", "40555967F759530864FE022E257DE34E");
equal(p, sk10pem, "encrypted");
});
test("getEryptedPKCS5PEMFromPrvKeyHex/2 without IV", function() {
var pem = PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(sk10dechex, "moge", "AES-256-CBC");
// when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});
test("getEryptedPKCS5PEMFromPrvKeyHex/3 without Alg and IV", function() {
var pem = PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(sk10dechex, "moge");
// when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});
test("getEryptedPKCS5PEMFromPrvKeyHex/4 alg check", function() {
expect(2);
// 1. no such alg
throws(
function() {
PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex("1234af", "password", "FOOALG", "012345");
},
"except: not supports alg FOOALG"
);
equal(PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex("1234af", "password", "AES-256-CBC", "012345") != null,
true,
"supports alg AES-128-CBC");
});
test("getEryptedPKCS5PEMFromRSAKey/1 key encryption", function() {
var k = new RSAKey();
k.readPrivateKeyFromASN1HexString(sk10dechex);
var p = PKCS5PKEY.getEryptedPKCS5PEMFromRSAKey(k, "hogehoge", "AES-256-CBC", "40555967F759530864FE022E257DE34E");
equal(p, sk10pem, "encrypted");
});
test("getEryptedPKCS5PEMFromRSAKey/2 without IV", function() {
var k = new RSAKey();
k.readPrivateKeyFromASN1HexString(sk10dechex);
var pem = PKCS5PKEY.getEryptedPKCS5PEMFromRSAKey(k, "moge", "AES-256-CBC");
// when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});
test("getEryptedPKCS5PEMFromRSAKey/3 without Alg and IV", function() {
var k = new RSAKey();
k.readPrivateKeyFromASN1HexString(sk10dechex);
var pem = PKCS5PKEY.getEryptedPKCS5PEMFromRSAKey(k, "moge");
// when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});
test("getEryptedPKCS5PEMFromRSAKey/4 with key generation", function() {
var k = new RSAKey();
k.generate(512, '10001');
var pem = PKCS5PKEY.getEryptedPKCS5PEMFromRSAKey(k, "moge");
// when AES-256-CBC, iv shall be 16 bytes (i.e. 32 chars)
equal(pem.match(/DEK-Info: AES-256-CBC,([0-9A-F]{32})\s+/) != null, true, "iv len match: iv=" + RegExp.$1 );
});
test("getHexFromPEM", function() {
var s = "-----BEGIN AAA TEST-----\r\n" +
"YWFh\r\n" +
"-----END AAA TEST-----\r\n";
equal(PKCS5PKEY.getHexFromPEM(s, "AAA TEST"), "616161", "aaa");
});
test("parseHexOfEncryptedPKCS8", function() {
var info = PKCS5PKEY.parseHexOfEncryptedPKCS8(sk12hex);
expect(5);
equal(info.ciphertext, sk12ciphertext, "ciphertext");
equal(info.encryptionSchemeAlg, "TripleDES", "encryptionSchemeAlg");
equal(info.encryptionSchemeIV, "5d35af642dc9b2c1", "encryptionSchemeIV");
equal(info.pbkdf2Salt, "53d63da7611f5ae7", "pbkdf2Salt");
equal(info.pbkdf2Iter, 2048, "pbkdf2Iter");
});
test("getPBKDF2KeyHexFromParam", function() {
var info = PKCS5PKEY.parseHexOfEncryptedPKCS8(sk12hex);
var hKey = PKCS5PKEY.getPBKDF2KeyHexFromParam(info, "hoge");
equal(hKey, "c540389de62486834ff3a208a9ab457291df38f4372ad920", "getPBKDF2KeyHexFromParam");
});
test("getPlainPKCS8HexFromEncryptedPKCS8PEM", function() {
var h = PKCS5PKEY.getPlainPKCS8HexFromEncryptedPKCS8PEM(sk12pem, "hoge");
equal(h, sk12plainhex, "sk12pem -> sk12plainhex");
});
test("getRSAKeyFromEncryptedPKCS8PEM", function() {
var k = PKCS5PKEY.getRSAKeyFromEncryptedPKCS8PEM(sk12pem, "hoge");
expect(3);
// n,e,d,p,q,dmp1,dmq1,coeff
equal(k.n.toString(16), "dc6d990d4a71c769ec704db7cce04da8b8f15bfad7b6e3e129e191c30df7db0243635a8ffb293a6e4a74599911c0d15ae8e72d9c84bd8dcf19ffee839fd05167", "rsaKey.n");
equal(k.e.toString(16), "10001", "rsaKey.e");
equal(k.d.toString(16), "ac15f412e8378393323f4aad5f890d97d72ab6c0528039cfa33eb2e930927bab09994944a11e1cdcf4153f1c8d9c1825bdeed0a1e86b1c268b42e1c348ad3f61", "rsaKey.d");
});
});
-->
</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
</body>
</html>