diff --git a/ChangeLog.txt b/ChangeLog.txt index 18d1bfc1..e5dc2ad7 100755 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,10 @@ ChangeLog for jsrsasign +* Changes between 5.0.0 to 5.0.1 (2015-Oct-17) + - keyutil 1.0.10 to 1.0.11 + - add support for bare RSA NED hexadecimal key in KEYUTIL.getKey + * Changes between 4.9.2 to 5.0.0 (2015-Oct-14) - *NOTICE* release 4.10.0 declined since API semver violation - some JWS HS* and Crypto Mac signature issue was fixed. diff --git a/api/files.html b/api/files.html index f3df1aa0..008f5032 100644 --- a/api/files.html +++ b/api/files.html @@ -687,7 +687,7 @@
// 1. loading private key +// 1. loading PEM private key var key = KEYUTIL.getKey(pemPKCS1PrivateKey); var key = KEYUTIL.getKey(pemPKCS5EncryptedPrivateKey, "passcode"); var key = KEYUTIL.getKey(pemPKC85PlainPrivateKey); var key = KEYUTIL.getKey(pemPKC85EncryptedPrivateKey, "passcode"); -// 2. loading public key +// 2. loading PEM public key var key = KEYUTIL.getKey(pemPKCS8PublicKey); var key = KEYUTIL.getKey(pemX509Certificate); // 3. exporting private key @@ -1353,12 +1353,34 @@
// 1. loading private key from PEM string +keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY..."); +keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY..., "passcode"); +keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY..."); +keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY...", "passcode"); +// 2. loading public key from PEM string +keyObj = KEYUTIL.getKey("-----BEGIN PUBLIC KEY..."); +keyObj = KEYUTIL.getKey("-----BEGIN X509 CERTIFICATE..."); +// 3. loading hexadecimal PKCS#5/PKCS#8 key +keyObj = KEYUTIL.getKey("308205c1...", null, "pkcs8pub"); +keyObj = KEYUTIL.getKey("3082048b...", null, "pkcs5prv"); +// 4. loading JSON Web Key(JWK) +keyObj = KEYUTIL.getKey({kty: "RSA", n: "0vx7...", e: "AQAB"}); +keyObj = KEYUTIL.getKey({kty: "EC", crv: "P-256", + x: "MKBC...", y: "4Etl6...", d: "870Mb..."}); +// 5. bare hexadecimal key +keyObj = KEYUTIL.getKey({n: "75ab..", e: "010001"});+ + +
1 /*! keyutil-1.0.11.js (c) 2013-2015 Kenji Urushima | kjur.github.com/jsrsasign/license +1 /*! keyutil-1.0.12.js (c) 2013-2015 Kenji Urushima | kjur.github.com/jsrsasign/license 2 */ 3 /* 4 * keyutil.js - key utility for PKCS#1/5/8 PEM, RSA/DSA/ECDSA key object @@ -22,7 +22,7 @@ 15 * @fileOverview 16 * @name keyutil-1.0.js 17 * @author Kenji Urushima kenji.urushima@gmail.com - 18 * @version keyutil 1.0.11 (2015-Oct-14) + 18 * @version keyutil 1.0.12 (2015-Oct-14) 19 * @since jsrsasign 4.1.4 20 * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a> 21 */ @@ -69,12 +69,12 @@ 62 * </dl> 63 * 64 * @example - 65 * // 1. loading private key + 65 * // 1. loading PEM private key 66 * var key = KEYUTIL.getKey(pemPKCS1PrivateKey); 67 * var key = KEYUTIL.getKey(pemPKCS5EncryptedPrivateKey, "passcode"); 68 * var key = KEYUTIL.getKey(pemPKC85PlainPrivateKey); 69 * var key = KEYUTIL.getKey(pemPKC85EncryptedPrivateKey, "passcode"); - 70 * // 2. loading public key + 70 * // 2. loading PEM public key 71 * var key = KEYUTIL.getKey(pemPKCS8PublicKey); 72 * var key = KEYUTIL.getKey(pemX509Certificate); 73 * // 3. exporting private key @@ -1256,724 +1256,772 @@ 1249 * <li>JWT plain RSA private key without P/Q/DP/DQ/COEFF (since jsrsasign 5.0.0)</li> 1250 * </ul> 1251 * NOTE: <a href="https://tools.ietf.org/html/rfc7517">RFC 7517 JSON Web Key(JWK)</a> support for RSA/ECC private/public key from jsrsasign 4.8.1. -1252 */ -1253 KEYUTIL.getKey = function(param, passcode, hextype) { -1254 // 1. by key RSAKey/KJUR.crypto.ECDSA/KJUR.crypto.DSA object -1255 if (typeof RSAKey != 'undefined' && param instanceof RSAKey) -1256 return param; -1257 if (typeof KJUR.crypto.ECDSA != 'undefined' && param instanceof KJUR.crypto.ECDSA) -1258 return param; -1259 if (typeof KJUR.crypto.DSA != 'undefined' && param instanceof KJUR.crypto.DSA) -1260 return param; -1261 -1262 // 2. by parameters of key -1263 // 2.1. ECC private key -1264 if (param.d !== undefined && param.curve !== undefined) { -1265 return new KJUR.crypto.ECDSA({prv: param.d, curve: param.curve}); -1266 } -1267 // 2.2. bare RSA private key -1268 if (param.n !== undefined && -1269 param.e !== undefined && -1270 param.d !== undefined && -1271 param.p !== undefined && -1272 param.q !== undefined && -1273 param.dp !== undefined && -1274 param.dq !== undefined && -1275 param.co !== undefined && -1276 param.qi === undefined) { -1277 var key = new RSAKey(); -1278 key.setPrivateEx(param.n, param.e, param.d, param.p, param.q, -1279 param.dp, param.dq, param.co); -1280 return key; -1281 } -1282 // 2.3. DSA private key -1283 if (param.p !== undefined && param.q !== undefined && param.g !== undefined && -1284 param.y !== undefined && param.x !== undefined) { -1285 var key = new KJUR.crypto.DSA(); -1286 key.setPrivate(param.p, param.q, param.g, param.y, param.x); -1287 return key; -1288 } -1289 -1290 // 2.4. ECC public key -1291 if (param.xy !== undefined && param.d === undefined && param.curve !== undefined) { -1292 return new KJUR.crypto.ECDSA({pub: param.xy, curve: param.curve}); -1293 } -1294 // 2.5. bare RSA public key -1295 if (param.kty === undefined && param.n !== undefined && param.e) { -1296 var key = new RSAKey(); -1297 key.setPublic(param.n, param.e); -1298 return key; -1299 } -1300 // 2.6. DSA public key -1301 if (param.p !== undefined && param.q !== undefined && param.g !== undefined && -1302 param.y !== undefined && param.x === undefined) { -1303 var key = new KJUR.crypto.DSA(); -1304 key.setPublic(param.p, param.q, param.g, param.y); -1305 return key; -1306 } -1307 -1308 // 2.7. JWK RSA public key -1309 if (param.kty === "RSA" && -1310 param.n !== undefined && -1311 param.e !== undefined && -1312 param.d === undefined) { -1313 var key = new RSAKey(); -1314 key.setPublic(b64utohex(param.n), b64utohex(param.e)); -1315 return key; -1316 } -1317 -1318 // 2.8.1. JWK RSA private key with p/q/dp/dq/coeff -1319 if (param.kty === "RSA" && -1320 param.n !== undefined && -1321 param.e !== undefined && -1322 param.d !== undefined && -1323 param.p !== undefined && -1324 param.q !== undefined && -1325 param.dp !== undefined && -1326 param.dq !== undefined && -1327 param.qi !== undefined) { -1328 var key = new RSAKey(); -1329 key.setPrivateEx(b64utohex(param.n), -1330 b64utohex(param.e), -1331 b64utohex(param.d), -1332 b64utohex(param.p), -1333 b64utohex(param.q), -1334 b64utohex(param.dp), -1335 b64utohex(param.dq), -1336 b64utohex(param.qi)); -1337 return key; -1338 } -1339 -1340 // 2.8.2. JWK RSA private key without p/q/dp/dq/coeff -1341 // since jsrsasign 5.0.0 keyutil 1.0.11 -1342 if (param.kty === "RSA" && -1343 param.n !== undefined && -1344 param.e !== undefined && -1345 param.d !== undefined) { -1346 var key = new RSAKey(); -1347 key.setPrivate(b64utohex(param.n), -1348 b64utohex(param.e), -1349 b64utohex(param.d)); -1350 return key; +1252 * +1253 * <h5>EXAMPLE</h5> +1254 * @example +1255 * // 1. loading private key from PEM string +1256 * keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY..."); +1257 * keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY..., "passcode"); +1258 * keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY..."); +1259 * keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY...", "passcode"); +1260 * // 2. loading public key from PEM string +1261 * keyObj = KEYUTIL.getKey("-----BEGIN PUBLIC KEY..."); +1262 * keyObj = KEYUTIL.getKey("-----BEGIN X509 CERTIFICATE..."); +1263 * // 3. loading hexadecimal PKCS#5/PKCS#8 key +1264 * keyObj = KEYUTIL.getKey("308205c1...", null, "pkcs8pub"); +1265 * keyObj = KEYUTIL.getKey("3082048b...", null, "pkcs5prv"); +1266 * // 4. loading JSON Web Key(JWK) +1267 * keyObj = KEYUTIL.getKey({kty: "RSA", n: "0vx7...", e: "AQAB"}); +1268 * keyObj = KEYUTIL.getKey({kty: "EC", crv: "P-256", +1269 * x: "MKBC...", y: "4Etl6...", d: "870Mb..."}); +1270 * // 5. bare hexadecimal key +1271 * keyObj = KEYUTIL.getKey({n: "75ab..", e: "010001"}); +1272 */ +1273 KEYUTIL.getKey = function(param, passcode, hextype) { +1274 // 1. by key RSAKey/KJUR.crypto.ECDSA/KJUR.crypto.DSA object +1275 if (typeof RSAKey != 'undefined' && param instanceof RSAKey) +1276 return param; +1277 if (typeof KJUR.crypto.ECDSA != 'undefined' && param instanceof KJUR.crypto.ECDSA) +1278 return param; +1279 if (typeof KJUR.crypto.DSA != 'undefined' && param instanceof KJUR.crypto.DSA) +1280 return param; +1281 +1282 // 2. by parameters of key +1283 +1284 // 2.1. bare ECC +1285 // 2.1.1. bare ECC public key by hex values +1286 if (param.curve !== undefined && +1287 param.xy !== undefined && param.d === undefined) { +1288 return new KJUR.crypto.ECDSA({pub: param.xy, curve: param.curve}); +1289 } +1290 +1291 // 2.1.2. bare ECC private key by hex values +1292 if (param.curve !== undefined && param.d !== undefined && ) { +1293 return new KJUR.crypto.ECDSA({prv: param.d, curve: param.curve}); +1294 } +1295 +1296 // 2.2. bare RSA +1297 // 2.2.1. bare RSA public key by hex values +1298 if (param.kty === undefined && +1299 param.n !== undefined && param.e !== undefined && +1300 param.d === undefined) { +1301 var key = new RSAKey(); +1302 key.setPublic(param.n, param.e); +1303 return key; +1304 } +1305 +1306 // 2.2.2. bare RSA private key with P/Q/DP/DQ/COEFF by hex values +1307 if (param.kty === undefined && +1308 param.n !== undefined && +1309 param.e !== undefined && +1310 param.d !== undefined && +1311 param.p !== undefined && +1312 param.q !== undefined && +1313 param.dp !== undefined && +1314 param.dq !== undefined && +1315 param.co !== undefined && +1316 param.qi === undefined) { +1317 var key = new RSAKey(); +1318 key.setPrivateEx(param.n, param.e, param.d, param.p, param.q, +1319 param.dp, param.dq, param.co); +1320 return key; +1321 } +1322 +1323 // 2.2.3. bare RSA public key without P/Q/DP/DQ/COEFF by hex values +1324 if (param.kty === undefined && +1325 param.n !== undefined && +1326 param.e !== undefined && +1327 param.d !== undefined && +1328 param.p === undefined) { +1329 var key = new RSAKey(); +1330 key.setPrivate(param.n, param.e, param.d); +1331 return key; +1332 } +1333 +1334 // 2.3. bare DSA +1335 // 2.3.1. bare DSA public key by hex values +1336 if (param.p !== undefined && param.q !== undefined && +1337 param.g !== undefined && +1338 param.y !== undefined && param.x === undefined) { +1339 var key = new KJUR.crypto.DSA(); +1340 key.setPublic(param.p, param.q, param.g, param.y); +1341 return key; +1342 } +1343 +1344 // 2.3.2. bare DSA private key by hex values +1345 if (param.p !== undefined && param.q !== undefined && +1346 param.g !== undefined && +1347 param.y !== undefined && param.x !== undefined) { +1348 var key = new KJUR.crypto.DSA(); +1349 key.setPrivate(param.p, param.q, param.g, param.y, param.x); +1350 return key; 1351 } 1352 -1353 // 2.9. JWK ECC public key -1354 if (param.kty === "EC" && -1355 param.crv !== undefined && -1356 param.x !== undefined && -1357 param.y !== undefined && -1358 param.d === undefined) { -1359 var ec = new KJUR.crypto.ECDSA({"curve": param.crv}); -1360 var charlen = ec.ecparams.keylen / 4; -1361 var hX = ("0000000000" + b64utohex(param.x)).slice(- charlen); -1362 var hY = ("0000000000" + b64utohex(param.y)).slice(- charlen); -1363 var hPub = "04" + hX + hY; -1364 ec.setPublicKeyHex(hPub); -1365 return ec; -1366 } -1367 -1368 // 2.10. JWK ECC private key -1369 if (param.kty === "EC" && -1370 param.crv !== undefined && -1371 param.x !== undefined && -1372 param.y !== undefined && -1373 param.d !== undefined) { -1374 var ec = new KJUR.crypto.ECDSA({"curve": param.crv}); -1375 var charlen = ec.ecparams.keylen / 4; -1376 var hPrv = ("0000000000" + b64utohex(param.d)).slice(- charlen); -1377 ec.setPrivateKeyHex(hPrv); -1378 return ec; -1379 } -1380 -1381 // 3. by PEM certificate (-----BEGIN ... CERTIFITE----) -1382 if (param.indexOf("-END CERTIFICATE-", 0) != -1 || -1383 param.indexOf("-END X509 CERTIFICATE-", 0) != -1 || -1384 param.indexOf("-END TRUSTED CERTIFICATE-", 0) != -1) { -1385 return X509.getPublicKeyFromCertPEM(param); -1386 } -1387 -1388 // 4. public key by PKCS#8 hexadecimal string -1389 if (hextype === "pkcs8pub") { -1390 return KEYUTIL.getKeyFromPublicPKCS8Hex(param); -1391 } -1392 -1393 // 5. public key by PKCS#8 PEM string -1394 if (param.indexOf("-END PUBLIC KEY-") != -1) { -1395 return KEYUTIL.getKeyFromPublicPKCS8PEM(param); -1396 } -1397 -1398 // 6. private key by PKCS#5 plain hexadecimal RSA string -1399 if (hextype === "pkcs5prv") { -1400 var key = new RSAKey(); -1401 key.readPrivateKeyFromASN1HexString(param); -1402 return key; -1403 } -1404 -1405 // 7. private key by plain PKCS#5 hexadecimal RSA string -1406 if (hextype === "pkcs5prv") { -1407 var key = new RSAKey(); -1408 key.readPrivateKeyFromASN1HexString(param); -1409 return key; -1410 } -1411 -1412 // 8. private key by plain PKCS#5 PEM RSA string -1413 // getKey("-----BEGIN RSA PRIVATE KEY-...") -1414 if (param.indexOf("-END RSA PRIVATE KEY-") != -1 && -1415 param.indexOf("4,ENCRYPTED") == -1) { -1416 var hex = KEYUTIL.getHexFromPEM(param, "RSA PRIVATE KEY"); -1417 return KEYUTIL.getKey(hex, null, "pkcs5prv"); -1418 } -1419 -1420 // 8.2. private key by plain PKCS#5 PEM DSA string -1421 if (param.indexOf("-END DSA PRIVATE KEY-") != -1 && -1422 param.indexOf("4,ENCRYPTED") == -1) { -1423 -1424 var hKey = this.getHexFromPEM(param, "DSA PRIVATE KEY"); -1425 var p = ASN1HEX.getVbyList(hKey, 0, [1], "02"); -1426 var q = ASN1HEX.getVbyList(hKey, 0, [2], "02"); -1427 var g = ASN1HEX.getVbyList(hKey, 0, [3], "02"); -1428 var y = ASN1HEX.getVbyList(hKey, 0, [4], "02"); -1429 var x = ASN1HEX.getVbyList(hKey, 0, [5], "02"); -1430 var key = new KJUR.crypto.DSA(); -1431 key.setPrivate(new BigInteger(p, 16), -1432 new BigInteger(q, 16), -1433 new BigInteger(g, 16), -1434 new BigInteger(y, 16), -1435 new BigInteger(x, 16)); -1436 return key; -1437 } -1438 -1439 // 9. private key by plain PKCS#8 PEM ECC/RSA string -1440 if (param.indexOf("-END PRIVATE KEY-") != -1) { -1441 return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(param); -1442 } -1443 -1444 // 10. private key by encrypted PKCS#5 PEM RSA string -1445 if (param.indexOf("-END RSA PRIVATE KEY-") != -1 && -1446 param.indexOf("4,ENCRYPTED") != -1) { -1447 return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(param, passcode); -1448 } -1449 -1450 // 10.2. private key by encrypted PKCS#5 PEM ECDSA string -1451 if (param.indexOf("-END EC PRIVATE KEY-") != -1 && -1452 param.indexOf("4,ENCRYPTED") != -1) { -1453 var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode); -1454 -1455 var key = ASN1HEX.getVbyList(hKey, 0, [1], "04"); -1456 var curveNameOidHex = ASN1HEX.getVbyList(hKey, 0, [2,0], "06"); -1457 var pubkey = ASN1HEX.getVbyList(hKey, 0, [3,0], "03").substr(2); -1458 var curveName = ""; +1353 // 3. JWK +1354 // 3.1. JWK RSA +1355 // 3.1.1. JWK RSA public key by b64u values +1356 if (param.kty === "RSA" && +1357 param.n !== undefined && +1358 param.e !== undefined && +1359 param.d === undefined) { +1360 var key = new RSAKey(); +1361 key.setPublic(b64utohex(param.n), b64utohex(param.e)); +1362 return key; +1363 } +1364 +1365 // 3.1.2. JWK RSA private key with p/q/dp/dq/coeff by b64u values +1366 if (param.kty === "RSA" && +1367 param.n !== undefined && +1368 param.e !== undefined && +1369 param.d !== undefined && +1370 param.p !== undefined && +1371 param.q !== undefined && +1372 param.dp !== undefined && +1373 param.dq !== undefined && +1374 param.qi !== undefined) { +1375 var key = new RSAKey(); +1376 key.setPrivateEx(b64utohex(param.n), +1377 b64utohex(param.e), +1378 b64utohex(param.d), +1379 b64utohex(param.p), +1380 b64utohex(param.q), +1381 b64utohex(param.dp), +1382 b64utohex(param.dq), +1383 b64utohex(param.qi)); +1384 return key; +1385 } +1386 +1387 // 3.1.3. JWK RSA private key without p/q/dp/dq/coeff by b64u +1388 // since jsrsasign 5.0.0 keyutil 1.0.11 +1389 if (param.kty === "RSA" && +1390 param.n !== undefined && +1391 param.e !== undefined && +1392 param.d !== undefined) { +1393 var key = new RSAKey(); +1394 key.setPrivate(b64utohex(param.n), +1395 b64utohex(param.e), +1396 b64utohex(param.d)); +1397 return key; +1398 } +1399 +1400 // 3.2. JWK ECC +1401 // 3.2.1. JWK ECC public key by b64u values +1402 if (param.kty === "EC" && +1403 param.crv !== undefined && +1404 param.x !== undefined && +1405 param.y !== undefined && +1406 param.d === undefined) { +1407 var ec = new KJUR.crypto.ECDSA({"curve": param.crv}); +1408 var charlen = ec.ecparams.keylen / 4; +1409 var hX = ("0000000000" + b64utohex(param.x)).slice(- charlen); +1410 var hY = ("0000000000" + b64utohex(param.y)).slice(- charlen); +1411 var hPub = "04" + hX + hY; +1412 ec.setPublicKeyHex(hPub); +1413 return ec; +1414 } +1415 +1416 // 3.2.2. JWK ECC private key by b64u values +1417 if (param.kty === "EC" && +1418 param.crv !== undefined && +1419 param.x !== undefined && +1420 param.y !== undefined && +1421 param.d !== undefined) { +1422 var ec = new KJUR.crypto.ECDSA({"curve": param.crv}); +1423 var charlen = ec.ecparams.keylen / 4; +1424 var hPrv = ("0000000000" + b64utohex(param.d)).slice(- charlen); +1425 ec.setPrivateKeyHex(hPrv); +1426 return ec; +1427 } +1428 +1429 // 4. by PEM certificate (-----BEGIN ... CERTIFITE----) +1430 if (param.indexOf("-END CERTIFICATE-", 0) != -1 || +1431 param.indexOf("-END X509 CERTIFICATE-", 0) != -1 || +1432 param.indexOf("-END TRUSTED CERTIFICATE-", 0) != -1) { +1433 return X509.getPublicKeyFromCertPEM(param); +1434 } +1435 +1436 // 4. public key by PKCS#8 hexadecimal string +1437 if (hextype === "pkcs8pub") { +1438 return KEYUTIL.getKeyFromPublicPKCS8Hex(param); +1439 } +1440 +1441 // 5. public key by PKCS#8 PEM string +1442 if (param.indexOf("-END PUBLIC KEY-") != -1) { +1443 return KEYUTIL.getKeyFromPublicPKCS8PEM(param); +1444 } +1445 +1446 // 6. private key by PKCS#5 plain hexadecimal RSA string +1447 if (hextype === "pkcs5prv") { +1448 var key = new RSAKey(); +1449 key.readPrivateKeyFromASN1HexString(param); +1450 return key; +1451 } +1452 +1453 // 7. private key by plain PKCS#5 hexadecimal RSA string +1454 if (hextype === "pkcs5prv") { +1455 var key = new RSAKey(); +1456 key.readPrivateKeyFromASN1HexString(param); +1457 return key; +1458 } 1459 -1460 if (KJUR.crypto.OID.oidhex2name[curveNameOidHex] !== undefined) { -1461 curveName = KJUR.crypto.OID.oidhex2name[curveNameOidHex]; -1462 } else { -1463 throw "undefined OID(hex) in KJUR.crypto.OID: " + curveNameOidHex; -1464 } -1465 -1466 var ec = new KJUR.crypto.ECDSA({'name': curveName}); -1467 ec.setPublicKeyHex(pubkey); -1468 ec.setPrivateKeyHex(key); -1469 ec.isPublic = false; -1470 return ec; -1471 } -1472 -1473 // 10.3. private key by encrypted PKCS#5 PEM DSA string -1474 if (param.indexOf("-END DSA PRIVATE KEY-") != -1 && -1475 param.indexOf("4,ENCRYPTED") != -1) { -1476 var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode); -1477 var p = ASN1HEX.getVbyList(hKey, 0, [1], "02"); -1478 var q = ASN1HEX.getVbyList(hKey, 0, [2], "02"); -1479 var g = ASN1HEX.getVbyList(hKey, 0, [3], "02"); -1480 var y = ASN1HEX.getVbyList(hKey, 0, [4], "02"); -1481 var x = ASN1HEX.getVbyList(hKey, 0, [5], "02"); -1482 var key = new KJUR.crypto.DSA(); -1483 key.setPrivate(new BigInteger(p, 16), -1484 new BigInteger(q, 16), -1485 new BigInteger(g, 16), -1486 new BigInteger(y, 16), -1487 new BigInteger(x, 16)); -1488 return key; -1489 } -1490 -1491 // 11. private key by encrypted PKCS#8 hexadecimal RSA/ECDSA string -1492 if (param.indexOf("-END ENCRYPTED PRIVATE KEY-") != -1) { -1493 return KEYUTIL.getKeyFromEncryptedPKCS8PEM(param, passcode); -1494 } -1495 -1496 throw "not supported argument"; -1497 }; -1498 -1499 /** -1500 * @name generateKeypair -1501 * @memberOf KEYUTIL -1502 * @function -1503 * @static -1504 * @param {String} alg 'RSA' or 'EC' -1505 * @param {Object} keylenOrCurve key length for RSA or curve name for EC -1506 * @return {Array} associative array of keypair which has prvKeyObj and pubKeyObj parameters -1507 * @since keyutil 1.0.1 -1508 * @description -1509 * This method generates a key pair of public key algorithm. -1510 * The result will be an associative array which has following -1511 * parameters: -1512 * <ul> -1513 * <li>prvKeyObj - RSAKey or ECDSA object of private key</li> -1514 * <li>pubKeyObj - RSAKey or ECDSA object of public key</li> -1515 * </ul> -1516 * NOTE1: As for RSA algoirthm, public exponent has fixed -1517 * value '0x10001'. -1518 * NOTE2: As for EC algorithm, supported names of curve are -1519 * secp256r1, secp256k1 and secp384r1. -1520 * NOTE3: DSA is not supported yet. -1521 * @example -1522 * var rsaKeypair = KEYUTIL.generateKeypair("RSA", 1024); -1523 * var ecKeypair = KEYUTIL.generateKeypair("EC", "secp256r1"); -1524 * -1525 */ -1526 KEYUTIL.generateKeypair = function(alg, keylenOrCurve) { -1527 if (alg == "RSA") { -1528 var keylen = keylenOrCurve; -1529 var prvKey = new RSAKey(); -1530 prvKey.generate(keylen, '10001'); -1531 prvKey.isPrivate = true; -1532 prvKey.isPublic = true; -1533 -1534 var pubKey = new RSAKey(); -1535 var hN = prvKey.n.toString(16); -1536 var hE = prvKey.e.toString(16); -1537 pubKey.setPublic(hN, hE); -1538 pubKey.isPrivate = false; -1539 pubKey.isPublic = true; -1540 -1541 var result = {}; -1542 result.prvKeyObj = prvKey; -1543 result.pubKeyObj = pubKey; -1544 return result; -1545 } else if (alg == "EC") { -1546 var curve = keylenOrCurve; -1547 var ec = new KJUR.crypto.ECDSA({curve: curve}); -1548 var keypairHex = ec.generateKeyPairHex(); -1549 -1550 var prvKey = new KJUR.crypto.ECDSA({curve: curve}); -1551 prvKey.setPrivateKeyHex(keypairHex.ecprvhex); -1552 prvKey.isPrivate = true; -1553 prvKey.isPublic = false; -1554 -1555 var pubKey = new KJUR.crypto.ECDSA({curve: curve}); -1556 pubKey.setPublicKeyHex(keypairHex.ecpubhex); -1557 pubKey.isPrivate = false; -1558 pubKey.isPublic = true; -1559 -1560 var result = {}; -1561 result.prvKeyObj = prvKey; -1562 result.pubKeyObj = pubKey; -1563 return result; -1564 } else { -1565 throw "unknown algorithm: " + alg; -1566 } -1567 }; -1568 -1569 /** -1570 * get PEM formatted private or public key file from a RSA/ECDSA/DSA key object -1571 * @name getPEM -1572 * @memberOf KEYUTIL -1573 * @function -1574 * @static -1575 * @param {Object} keyObjOrHex key object {@link RSAKey}, {@link KJUR.crypto.ECDSA} or {@link KJUR.crypto.DSA} to encode to -1576 * @param {String} formatType (OPTION) output format type of "PKCS1PRV", "PKCS5PRV" or "PKCS8PRV" for private key -1577 * @param {String} passwd (OPTION) password to protect private key -1578 * @param {String} encAlg (OPTION) encryption algorithm for PKCS#5. currently supports DES-CBC, DES-EDE3-CBC and AES-{128,192,256}-CBC -1579 * @since keyutil 1.0.4 -1580 * @description -1581 * <dl> -1582 * <dt><b>NOTE1:</b> -1583 * <dd> -1584 * PKCS#5 encrypted private key protection algorithm supports DES-CBC, -1585 * DES-EDE3-CBC and AES-{128,192,256}-CBC -1586 * <dt><b>NOTE2:</b> -1587 * <dd> -1588 * OpenSSL supports -1589 * </dl> -1590 * @example -1591 * KEUUTIL.getPEM(publicKey) => generates PEM PKCS#8 public key -1592 * KEUUTIL.getPEM(privateKey, "PKCS1PRV") => generates PEM PKCS#1 plain private key -1593 * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass") => generates PEM PKCS#5 encrypted private key -1594 * with DES-EDE3-CBC (DEFAULT) -1595 * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass", "DES-CBC") => generates PEM PKCS#5 encrypted -1596 * private key with DES-CBC -1597 * KEUUTIL.getPEM(privateKey, "PKCS8PRV") => generates PEM PKCS#8 plain private key -1598 * KEUUTIL.getPEM(privateKey, "PKCS8PRV", "pass") => generates PEM PKCS#8 encrypted private key -1599 * with PBKDF2_HmacSHA1_3DES -1600 */ -1601 KEYUTIL.getPEM = function(keyObjOrHex, formatType, passwd, encAlg, hexType) { -1602 var ns1 = KJUR.asn1; -1603 var ns2 = KJUR.crypto; -1604 -1605 function _rsaprv2asn1obj(keyObjOrHex) { -1606 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ -1607 "seq": [ -1608 {"int": 0 }, -1609 {"int": {"bigint": keyObjOrHex.n}}, -1610 {"int": keyObjOrHex.e}, -1611 {"int": {"bigint": keyObjOrHex.d}}, -1612 {"int": {"bigint": keyObjOrHex.p}}, -1613 {"int": {"bigint": keyObjOrHex.q}}, -1614 {"int": {"bigint": keyObjOrHex.dmp1}}, -1615 {"int": {"bigint": keyObjOrHex.dmq1}}, -1616 {"int": {"bigint": keyObjOrHex.coeff}} -1617 ] -1618 }); -1619 return asn1Obj; -1620 }; -1621 -1622 function _ecdsaprv2asn1obj(keyObjOrHex) { -1623 var asn1Obj2 = KJUR.asn1.ASN1Util.newObject({ -1624 "seq": [ -1625 {"int": 1 }, -1626 {"octstr": {"hex": keyObjOrHex.prvKeyHex}}, -1627 {"tag": ['a0', true, {'oid': {'name': keyObjOrHex.curveName}}]}, -1628 {"tag": ['a1', true, {'bitstr': {'hex': '00' + keyObjOrHex.pubKeyHex}}]} -1629 ] -1630 }); -1631 return asn1Obj2; -1632 }; -1633 -1634 function _dsaprv2asn1obj(keyObjOrHex) { -1635 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ -1636 "seq": [ -1637 {"int": 0 }, -1638 {"int": {"bigint": keyObjOrHex.p}}, -1639 {"int": {"bigint": keyObjOrHex.q}}, -1640 {"int": {"bigint": keyObjOrHex.g}}, -1641 {"int": {"bigint": keyObjOrHex.y}}, -1642 {"int": {"bigint": keyObjOrHex.x}} -1643 ] -1644 }); -1645 return asn1Obj; -1646 }; -1647 -1648 // 1. public key -1649 -1650 // x. PEM PKCS#8 public key of RSA/ECDSA/DSA public key object -1651 if (((typeof RSAKey != "undefined" && keyObjOrHex instanceof RSAKey) || -1652 (typeof ns2.DSA != "undefined" && keyObjOrHex instanceof ns2.DSA) || -1653 (typeof ns2.ECDSA != "undefined" && keyObjOrHex instanceof ns2.ECDSA)) && -1654 keyObjOrHex.isPublic == true && -1655 (formatType === undefined || formatType == "PKCS8PUB")) { -1656 var asn1Obj = new KJUR.asn1.x509.SubjectPublicKeyInfo(keyObjOrHex); -1657 var asn1Hex = asn1Obj.getEncodedHex(); -1658 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PUBLIC KEY"); -1659 } -1660 -1661 // 2. private -1662 -1663 // x. PEM PKCS#1 plain private key of RSA private key object -1664 if (formatType == "PKCS1PRV" && -1665 typeof RSAKey != "undefined" && -1666 keyObjOrHex instanceof RSAKey && -1667 (passwd === undefined || passwd == null) && -1668 keyObjOrHex.isPrivate == true) { +1460 // 8. private key by plain PKCS#5 PEM RSA string +1461 // getKey("-----BEGIN RSA PRIVATE KEY-...") +1462 if (param.indexOf("-END RSA PRIVATE KEY-") != -1 && +1463 param.indexOf("4,ENCRYPTED") == -1) { +1464 var hex = KEYUTIL.getHexFromPEM(param, "RSA PRIVATE KEY"); +1465 return KEYUTIL.getKey(hex, null, "pkcs5prv"); +1466 } +1467 +1468 // 8.2. private key by plain PKCS#5 PEM DSA string +1469 if (param.indexOf("-END DSA PRIVATE KEY-") != -1 && +1470 param.indexOf("4,ENCRYPTED") == -1) { +1471 +1472 var hKey = this.getHexFromPEM(param, "DSA PRIVATE KEY"); +1473 var p = ASN1HEX.getVbyList(hKey, 0, [1], "02"); +1474 var q = ASN1HEX.getVbyList(hKey, 0, [2], "02"); +1475 var g = ASN1HEX.getVbyList(hKey, 0, [3], "02"); +1476 var y = ASN1HEX.getVbyList(hKey, 0, [4], "02"); +1477 var x = ASN1HEX.getVbyList(hKey, 0, [5], "02"); +1478 var key = new KJUR.crypto.DSA(); +1479 key.setPrivate(new BigInteger(p, 16), +1480 new BigInteger(q, 16), +1481 new BigInteger(g, 16), +1482 new BigInteger(y, 16), +1483 new BigInteger(x, 16)); +1484 return key; +1485 } +1486 +1487 // 9. private key by plain PKCS#8 PEM ECC/RSA string +1488 if (param.indexOf("-END PRIVATE KEY-") != -1) { +1489 return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(param); +1490 } +1491 +1492 // 10. private key by encrypted PKCS#5 PEM RSA string +1493 if (param.indexOf("-END RSA PRIVATE KEY-") != -1 && +1494 param.indexOf("4,ENCRYPTED") != -1) { +1495 return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(param, passcode); +1496 } +1497 +1498 // 10.2. private key by encrypted PKCS#5 PEM ECDSA string +1499 if (param.indexOf("-END EC PRIVATE KEY-") != -1 && +1500 param.indexOf("4,ENCRYPTED") != -1) { +1501 var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode); +1502 +1503 var key = ASN1HEX.getVbyList(hKey, 0, [1], "04"); +1504 var curveNameOidHex = ASN1HEX.getVbyList(hKey, 0, [2,0], "06"); +1505 var pubkey = ASN1HEX.getVbyList(hKey, 0, [3,0], "03").substr(2); +1506 var curveName = ""; +1507 +1508 if (KJUR.crypto.OID.oidhex2name[curveNameOidHex] !== undefined) { +1509 curveName = KJUR.crypto.OID.oidhex2name[curveNameOidHex]; +1510 } else { +1511 throw "undefined OID(hex) in KJUR.crypto.OID: " + curveNameOidHex; +1512 } +1513 +1514 var ec = new KJUR.crypto.ECDSA({'name': curveName}); +1515 ec.setPublicKeyHex(pubkey); +1516 ec.setPrivateKeyHex(key); +1517 ec.isPublic = false; +1518 return ec; +1519 } +1520 +1521 // 10.3. private key by encrypted PKCS#5 PEM DSA string +1522 if (param.indexOf("-END DSA PRIVATE KEY-") != -1 && +1523 param.indexOf("4,ENCRYPTED") != -1) { +1524 var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode); +1525 var p = ASN1HEX.getVbyList(hKey, 0, [1], "02"); +1526 var q = ASN1HEX.getVbyList(hKey, 0, [2], "02"); +1527 var g = ASN1HEX.getVbyList(hKey, 0, [3], "02"); +1528 var y = ASN1HEX.getVbyList(hKey, 0, [4], "02"); +1529 var x = ASN1HEX.getVbyList(hKey, 0, [5], "02"); +1530 var key = new KJUR.crypto.DSA(); +1531 key.setPrivate(new BigInteger(p, 16), +1532 new BigInteger(q, 16), +1533 new BigInteger(g, 16), +1534 new BigInteger(y, 16), +1535 new BigInteger(x, 16)); +1536 return key; +1537 } +1538 +1539 // 11. private key by encrypted PKCS#8 hexadecimal RSA/ECDSA string +1540 if (param.indexOf("-END ENCRYPTED PRIVATE KEY-") != -1) { +1541 return KEYUTIL.getKeyFromEncryptedPKCS8PEM(param, passcode); +1542 } +1543 +1544 throw "not supported argument"; +1545 }; +1546 +1547 /** +1548 * @name generateKeypair +1549 * @memberOf KEYUTIL +1550 * @function +1551 * @static +1552 * @param {String} alg 'RSA' or 'EC' +1553 * @param {Object} keylenOrCurve key length for RSA or curve name for EC +1554 * @return {Array} associative array of keypair which has prvKeyObj and pubKeyObj parameters +1555 * @since keyutil 1.0.1 +1556 * @description +1557 * This method generates a key pair of public key algorithm. +1558 * The result will be an associative array which has following +1559 * parameters: +1560 * <ul> +1561 * <li>prvKeyObj - RSAKey or ECDSA object of private key</li> +1562 * <li>pubKeyObj - RSAKey or ECDSA object of public key</li> +1563 * </ul> +1564 * NOTE1: As for RSA algoirthm, public exponent has fixed +1565 * value '0x10001'. +1566 * NOTE2: As for EC algorithm, supported names of curve are +1567 * secp256r1, secp256k1 and secp384r1. +1568 * NOTE3: DSA is not supported yet. +1569 * @example +1570 * var rsaKeypair = KEYUTIL.generateKeypair("RSA", 1024); +1571 * var ecKeypair = KEYUTIL.generateKeypair("EC", "secp256r1"); +1572 * +1573 */ +1574 KEYUTIL.generateKeypair = function(alg, keylenOrCurve) { +1575 if (alg == "RSA") { +1576 var keylen = keylenOrCurve; +1577 var prvKey = new RSAKey(); +1578 prvKey.generate(keylen, '10001'); +1579 prvKey.isPrivate = true; +1580 prvKey.isPublic = true; +1581 +1582 var pubKey = new RSAKey(); +1583 var hN = prvKey.n.toString(16); +1584 var hE = prvKey.e.toString(16); +1585 pubKey.setPublic(hN, hE); +1586 pubKey.isPrivate = false; +1587 pubKey.isPublic = true; +1588 +1589 var result = {}; +1590 result.prvKeyObj = prvKey; +1591 result.pubKeyObj = pubKey; +1592 return result; +1593 } else if (alg == "EC") { +1594 var curve = keylenOrCurve; +1595 var ec = new KJUR.crypto.ECDSA({curve: curve}); +1596 var keypairHex = ec.generateKeyPairHex(); +1597 +1598 var prvKey = new KJUR.crypto.ECDSA({curve: curve}); +1599 prvKey.setPrivateKeyHex(keypairHex.ecprvhex); +1600 prvKey.isPrivate = true; +1601 prvKey.isPublic = false; +1602 +1603 var pubKey = new KJUR.crypto.ECDSA({curve: curve}); +1604 pubKey.setPublicKeyHex(keypairHex.ecpubhex); +1605 pubKey.isPrivate = false; +1606 pubKey.isPublic = true; +1607 +1608 var result = {}; +1609 result.prvKeyObj = prvKey; +1610 result.pubKeyObj = pubKey; +1611 return result; +1612 } else { +1613 throw "unknown algorithm: " + alg; +1614 } +1615 }; +1616 +1617 /** +1618 * get PEM formatted private or public key file from a RSA/ECDSA/DSA key object +1619 * @name getPEM +1620 * @memberOf KEYUTIL +1621 * @function +1622 * @static +1623 * @param {Object} keyObjOrHex key object {@link RSAKey}, {@link KJUR.crypto.ECDSA} or {@link KJUR.crypto.DSA} to encode to +1624 * @param {String} formatType (OPTION) output format type of "PKCS1PRV", "PKCS5PRV" or "PKCS8PRV" for private key +1625 * @param {String} passwd (OPTION) password to protect private key +1626 * @param {String} encAlg (OPTION) encryption algorithm for PKCS#5. currently supports DES-CBC, DES-EDE3-CBC and AES-{128,192,256}-CBC +1627 * @since keyutil 1.0.4 +1628 * @description +1629 * <dl> +1630 * <dt><b>NOTE1:</b> +1631 * <dd> +1632 * PKCS#5 encrypted private key protection algorithm supports DES-CBC, +1633 * DES-EDE3-CBC and AES-{128,192,256}-CBC +1634 * <dt><b>NOTE2:</b> +1635 * <dd> +1636 * OpenSSL supports +1637 * </dl> +1638 * @example +1639 * KEUUTIL.getPEM(publicKey) => generates PEM PKCS#8 public key +1640 * KEUUTIL.getPEM(privateKey, "PKCS1PRV") => generates PEM PKCS#1 plain private key +1641 * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass") => generates PEM PKCS#5 encrypted private key +1642 * with DES-EDE3-CBC (DEFAULT) +1643 * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass", "DES-CBC") => generates PEM PKCS#5 encrypted +1644 * private key with DES-CBC +1645 * KEUUTIL.getPEM(privateKey, "PKCS8PRV") => generates PEM PKCS#8 plain private key +1646 * KEUUTIL.getPEM(privateKey, "PKCS8PRV", "pass") => generates PEM PKCS#8 encrypted private key +1647 * with PBKDF2_HmacSHA1_3DES +1648 */ +1649 KEYUTIL.getPEM = function(keyObjOrHex, formatType, passwd, encAlg, hexType) { +1650 var ns1 = KJUR.asn1; +1651 var ns2 = KJUR.crypto; +1652 +1653 function _rsaprv2asn1obj(keyObjOrHex) { +1654 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ +1655 "seq": [ +1656 {"int": 0 }, +1657 {"int": {"bigint": keyObjOrHex.n}}, +1658 {"int": keyObjOrHex.e}, +1659 {"int": {"bigint": keyObjOrHex.d}}, +1660 {"int": {"bigint": keyObjOrHex.p}}, +1661 {"int": {"bigint": keyObjOrHex.q}}, +1662 {"int": {"bigint": keyObjOrHex.dmp1}}, +1663 {"int": {"bigint": keyObjOrHex.dmq1}}, +1664 {"int": {"bigint": keyObjOrHex.coeff}} +1665 ] +1666 }); +1667 return asn1Obj; +1668 }; 1669 -1670 var asn1Obj = _rsaprv2asn1obj(keyObjOrHex); -1671 var asn1Hex = asn1Obj.getEncodedHex(); -1672 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "RSA PRIVATE KEY"); -1673 } -1674 -1675 // x. PEM PKCS#1 plain private key of ECDSA private key object -1676 if (formatType == "PKCS1PRV" && -1677 typeof RSAKey != "undefined" && -1678 keyObjOrHex instanceof KJUR.crypto.ECDSA && -1679 (passwd === undefined || passwd == null) && -1680 keyObjOrHex.isPrivate == true) { +1670 function _ecdsaprv2asn1obj(keyObjOrHex) { +1671 var asn1Obj2 = KJUR.asn1.ASN1Util.newObject({ +1672 "seq": [ +1673 {"int": 1 }, +1674 {"octstr": {"hex": keyObjOrHex.prvKeyHex}}, +1675 {"tag": ['a0', true, {'oid': {'name': keyObjOrHex.curveName}}]}, +1676 {"tag": ['a1', true, {'bitstr': {'hex': '00' + keyObjOrHex.pubKeyHex}}]} +1677 ] +1678 }); +1679 return asn1Obj2; +1680 }; 1681 -1682 var asn1Obj1 = new KJUR.asn1.DERObjectIdentifier({'name': keyObjOrHex.curveName}); -1683 var asn1Hex1 = asn1Obj1.getEncodedHex(); -1684 var asn1Obj2 = _ecdsaprv2asn1obj(keyObjOrHex); -1685 var asn1Hex2 = asn1Obj2.getEncodedHex(); -1686 -1687 var s = ""; -1688 s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex1, "EC PARAMETERS"); -1689 s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "EC PRIVATE KEY"); -1690 return s; -1691 } -1692 -1693 // x. PEM PKCS#1 plain private key of DSA private key object -1694 if (formatType == "PKCS1PRV" && -1695 typeof KJUR.crypto.DSA != "undefined" && -1696 keyObjOrHex instanceof KJUR.crypto.DSA && -1697 (passwd === undefined || passwd == null) && -1698 keyObjOrHex.isPrivate == true) { -1699 -1700 var asn1Obj = _dsaprv2asn1obj(keyObjOrHex); -1701 var asn1Hex = asn1Obj.getEncodedHex(); -1702 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "DSA PRIVATE KEY"); -1703 } -1704 -1705 // 3. private -1706 -1707 // x. PEM PKCS#5 encrypted private key of RSA private key object -1708 if (formatType == "PKCS5PRV" && -1709 typeof RSAKey != "undefined" && -1710 keyObjOrHex instanceof RSAKey && -1711 (passwd !== undefined && passwd != null) && -1712 keyObjOrHex.isPrivate == true) { -1713 -1714 var asn1Obj = _rsaprv2asn1obj(keyObjOrHex); -1715 var asn1Hex = asn1Obj.getEncodedHex(); -1716 -1717 if (encAlg === undefined) encAlg = "DES-EDE3-CBC"; -1718 return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", asn1Hex, passwd, encAlg); -1719 } -1720 -1721 // x. PEM PKCS#5 encrypted private key of ECDSA private key object -1722 if (formatType == "PKCS5PRV" && -1723 typeof KJUR.crypto.ECDSA != "undefined" && -1724 keyObjOrHex instanceof KJUR.crypto.ECDSA && -1725 (passwd !== undefined && passwd != null) && -1726 keyObjOrHex.isPrivate == true) { -1727 -1728 var asn1Obj = _ecdsaprv2asn1obj(keyObjOrHex); -1729 var asn1Hex = asn1Obj.getEncodedHex(); -1730 -1731 if (encAlg === undefined) encAlg = "DES-EDE3-CBC"; -1732 return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC", asn1Hex, passwd, encAlg); -1733 } +1682 function _dsaprv2asn1obj(keyObjOrHex) { +1683 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ +1684 "seq": [ +1685 {"int": 0 }, +1686 {"int": {"bigint": keyObjOrHex.p}}, +1687 {"int": {"bigint": keyObjOrHex.q}}, +1688 {"int": {"bigint": keyObjOrHex.g}}, +1689 {"int": {"bigint": keyObjOrHex.y}}, +1690 {"int": {"bigint": keyObjOrHex.x}} +1691 ] +1692 }); +1693 return asn1Obj; +1694 }; +1695 +1696 // 1. public key +1697 +1698 // x. PEM PKCS#8 public key of RSA/ECDSA/DSA public key object +1699 if (((typeof RSAKey != "undefined" && keyObjOrHex instanceof RSAKey) || +1700 (typeof ns2.DSA != "undefined" && keyObjOrHex instanceof ns2.DSA) || +1701 (typeof ns2.ECDSA != "undefined" && keyObjOrHex instanceof ns2.ECDSA)) && +1702 keyObjOrHex.isPublic == true && +1703 (formatType === undefined || formatType == "PKCS8PUB")) { +1704 var asn1Obj = new KJUR.asn1.x509.SubjectPublicKeyInfo(keyObjOrHex); +1705 var asn1Hex = asn1Obj.getEncodedHex(); +1706 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PUBLIC KEY"); +1707 } +1708 +1709 // 2. private +1710 +1711 // x. PEM PKCS#1 plain private key of RSA private key object +1712 if (formatType == "PKCS1PRV" && +1713 typeof RSAKey != "undefined" && +1714 keyObjOrHex instanceof RSAKey && +1715 (passwd === undefined || passwd == null) && +1716 keyObjOrHex.isPrivate == true) { +1717 +1718 var asn1Obj = _rsaprv2asn1obj(keyObjOrHex); +1719 var asn1Hex = asn1Obj.getEncodedHex(); +1720 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "RSA PRIVATE KEY"); +1721 } +1722 +1723 // x. PEM PKCS#1 plain private key of ECDSA private key object +1724 if (formatType == "PKCS1PRV" && +1725 typeof RSAKey != "undefined" && +1726 keyObjOrHex instanceof KJUR.crypto.ECDSA && +1727 (passwd === undefined || passwd == null) && +1728 keyObjOrHex.isPrivate == true) { +1729 +1730 var asn1Obj1 = new KJUR.asn1.DERObjectIdentifier({'name': keyObjOrHex.curveName}); +1731 var asn1Hex1 = asn1Obj1.getEncodedHex(); +1732 var asn1Obj2 = _ecdsaprv2asn1obj(keyObjOrHex); +1733 var asn1Hex2 = asn1Obj2.getEncodedHex(); 1734 -1735 // x. PEM PKCS#5 encrypted private key of DSA private key object -1736 if (formatType == "PKCS5PRV" && -1737 typeof KJUR.crypto.DSA != "undefined" && -1738 keyObjOrHex instanceof KJUR.crypto.DSA && -1739 (passwd !== undefined && passwd != null) && -1740 keyObjOrHex.isPrivate == true) { -1741 -1742 var asn1Obj = _dsaprv2asn1obj(keyObjOrHex); -1743 var asn1Hex = asn1Obj.getEncodedHex(); -1744 -1745 if (encAlg === undefined) encAlg = "DES-EDE3-CBC"; -1746 return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA", asn1Hex, passwd, encAlg); -1747 } -1748 -1749 // x. ====================================================================== -1750 -1751 var _getEncryptedPKCS8 = function(plainKeyHex, passcode) { -1752 var info = _getEencryptedPKCS8Info(plainKeyHex, passcode); -1753 //alert("iv=" + info.encryptionSchemeIV); -1754 //alert("info.ciphertext2[" + info.ciphertext.length + "=" + info.ciphertext); -1755 var asn1Obj = new KJUR.asn1.ASN1Util.newObject({ -1756 "seq": [ -1757 {"seq": [ -1758 {"oid": {"name": "pkcs5PBES2"}}, -1759 {"seq": [ -1760 {"seq": [ -1761 {"oid": {"name": "pkcs5PBKDF2"}}, -1762 {"seq": [ -1763 {"octstr": {"hex": info.pbkdf2Salt}}, -1764 {"int": info.pbkdf2Iter} -1765 ]} -1766 ]}, -1767 {"seq": [ -1768 {"oid": {"name": "des-EDE3-CBC"}}, -1769 {"octstr": {"hex": info.encryptionSchemeIV}} -1770 ]} -1771 ]} -1772 ]}, -1773 {"octstr": {"hex": info.ciphertext}} -1774 ] -1775 }); -1776 return asn1Obj.getEncodedHex(); -1777 }; +1735 var s = ""; +1736 s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex1, "EC PARAMETERS"); +1737 s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "EC PRIVATE KEY"); +1738 return s; +1739 } +1740 +1741 // x. PEM PKCS#1 plain private key of DSA private key object +1742 if (formatType == "PKCS1PRV" && +1743 typeof KJUR.crypto.DSA != "undefined" && +1744 keyObjOrHex instanceof KJUR.crypto.DSA && +1745 (passwd === undefined || passwd == null) && +1746 keyObjOrHex.isPrivate == true) { +1747 +1748 var asn1Obj = _dsaprv2asn1obj(keyObjOrHex); +1749 var asn1Hex = asn1Obj.getEncodedHex(); +1750 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "DSA PRIVATE KEY"); +1751 } +1752 +1753 // 3. private +1754 +1755 // x. PEM PKCS#5 encrypted private key of RSA private key object +1756 if (formatType == "PKCS5PRV" && +1757 typeof RSAKey != "undefined" && +1758 keyObjOrHex instanceof RSAKey && +1759 (passwd !== undefined && passwd != null) && +1760 keyObjOrHex.isPrivate == true) { +1761 +1762 var asn1Obj = _rsaprv2asn1obj(keyObjOrHex); +1763 var asn1Hex = asn1Obj.getEncodedHex(); +1764 +1765 if (encAlg === undefined) encAlg = "DES-EDE3-CBC"; +1766 return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", asn1Hex, passwd, encAlg); +1767 } +1768 +1769 // x. PEM PKCS#5 encrypted private key of ECDSA private key object +1770 if (formatType == "PKCS5PRV" && +1771 typeof KJUR.crypto.ECDSA != "undefined" && +1772 keyObjOrHex instanceof KJUR.crypto.ECDSA && +1773 (passwd !== undefined && passwd != null) && +1774 keyObjOrHex.isPrivate == true) { +1775 +1776 var asn1Obj = _ecdsaprv2asn1obj(keyObjOrHex); +1777 var asn1Hex = asn1Obj.getEncodedHex(); 1778 -1779 var _getEencryptedPKCS8Info = function(plainKeyHex, passcode) { -1780 var pbkdf2Iter = 100; -1781 var pbkdf2SaltWS = CryptoJS.lib.WordArray.random(8); -1782 var encryptionSchemeAlg = "DES-EDE3-CBC"; -1783 var encryptionSchemeIVWS = CryptoJS.lib.WordArray.random(8); -1784 // PBKDF2 key -1785 var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, -1786 pbkdf2SaltWS, { "keySize": 192/32, -1787 "iterations": pbkdf2Iter }); -1788 // ENCRYPT -1789 var plainKeyWS = CryptoJS.enc.Hex.parse(plainKeyHex); -1790 var encryptedKeyHex = -1791 CryptoJS.TripleDES.encrypt(plainKeyWS, pbkdf2KeyWS, { "iv": encryptionSchemeIVWS }) + ""; +1779 if (encAlg === undefined) encAlg = "DES-EDE3-CBC"; +1780 return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC", asn1Hex, passwd, encAlg); +1781 } +1782 +1783 // x. PEM PKCS#5 encrypted private key of DSA private key object +1784 if (formatType == "PKCS5PRV" && +1785 typeof KJUR.crypto.DSA != "undefined" && +1786 keyObjOrHex instanceof KJUR.crypto.DSA && +1787 (passwd !== undefined && passwd != null) && +1788 keyObjOrHex.isPrivate == true) { +1789 +1790 var asn1Obj = _dsaprv2asn1obj(keyObjOrHex); +1791 var asn1Hex = asn1Obj.getEncodedHex(); 1792 -1793 //alert("encryptedKeyHex=" + encryptedKeyHex); -1794 -1795 var info = {}; -1796 info.ciphertext = encryptedKeyHex; -1797 //alert("info.ciphertext=" + info.ciphertext); -1798 info.pbkdf2Salt = CryptoJS.enc.Hex.stringify(pbkdf2SaltWS); -1799 info.pbkdf2Iter = pbkdf2Iter; -1800 info.encryptionSchemeAlg = encryptionSchemeAlg; -1801 info.encryptionSchemeIV = CryptoJS.enc.Hex.stringify(encryptionSchemeIVWS); -1802 return info; -1803 }; -1804 -1805 // x. PEM PKCS#8 plain private key of RSA private key object -1806 if (formatType == "PKCS8PRV" && -1807 typeof RSAKey != "undefined" && -1808 keyObjOrHex instanceof RSAKey && -1809 keyObjOrHex.isPrivate == true) { -1810 -1811 var keyObj = _rsaprv2asn1obj(keyObjOrHex); -1812 var keyHex = keyObj.getEncodedHex(); -1813 -1814 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ -1815 "seq": [ -1816 {"int": 0}, -1817 {"seq": [{"oid": {"name": "rsaEncryption"}},{"null": true}]}, -1818 {"octstr": {"hex": keyHex}} -1819 ] -1820 }); -1821 var asn1Hex = asn1Obj.getEncodedHex(); -1822 -1823 if (passwd === undefined || passwd == null) { -1824 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY"); -1825 } else { -1826 var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd); -1827 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY"); -1828 } -1829 } -1830 -1831 // x. PEM PKCS#8 plain private key of ECDSA private key object -1832 if (formatType == "PKCS8PRV" && -1833 typeof KJUR.crypto.ECDSA != "undefined" && -1834 keyObjOrHex instanceof KJUR.crypto.ECDSA && -1835 keyObjOrHex.isPrivate == true) { -1836 -1837 var keyObj = new KJUR.asn1.ASN1Util.newObject({ -1838 "seq": [ -1839 {"int": 1}, -1840 {"octstr": {"hex": keyObjOrHex.prvKeyHex}}, -1841 {"tag": ['a1', true, {"bitstr": {"hex": "00" + keyObjOrHex.pubKeyHex}}]} -1842 ] -1843 }); -1844 var keyHex = keyObj.getEncodedHex(); -1845 -1846 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ -1847 "seq": [ -1848 {"int": 0}, -1849 {"seq": [ -1850 {"oid": {"name": "ecPublicKey"}}, -1851 {"oid": {"name": keyObjOrHex.curveName}} -1852 ]}, -1853 {"octstr": {"hex": keyHex}} -1854 ] -1855 }); -1856 -1857 var asn1Hex = asn1Obj.getEncodedHex(); -1858 if (passwd === undefined || passwd == null) { -1859 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY"); -1860 } else { -1861 var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd); -1862 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY"); -1863 } -1864 } -1865 -1866 // x. PEM PKCS#8 plain private key of DSA private key object -1867 if (formatType == "PKCS8PRV" && -1868 typeof KJUR.crypto.DSA != "undefined" && -1869 keyObjOrHex instanceof KJUR.crypto.DSA && -1870 keyObjOrHex.isPrivate == true) { -1871 -1872 var keyObj = new KJUR.asn1.DERInteger({'bigint': keyObjOrHex.x}); -1873 var keyHex = keyObj.getEncodedHex(); -1874 -1875 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ -1876 "seq": [ -1877 {"int": 0}, -1878 {"seq": [ -1879 {"oid": {"name": "dsa"}}, -1880 {"seq": [ -1881 {"int": {"bigint": keyObjOrHex.p}}, -1882 {"int": {"bigint": keyObjOrHex.q}}, -1883 {"int": {"bigint": keyObjOrHex.g}} -1884 ]} -1885 ]}, -1886 {"octstr": {"hex": keyHex}} -1887 ] -1888 }); -1889 -1890 var asn1Hex = asn1Obj.getEncodedHex(); -1891 if (passwd === undefined || passwd == null) { -1892 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY"); -1893 } else { -1894 var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd); -1895 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY"); -1896 } -1897 } -1898 -1899 throw "unsupported object nor format"; -1900 }; -1901 -1902 // -- PUBLIC METHODS FOR CSR ------------------------------------------------------- -1903 -1904 /** -1905 * get RSAKey/DSA/ECDSA public key object from PEM formatted PKCS#10 CSR string -1906 * @name getKeyFromCSRPEM -1907 * @memberOf KEYUTIL -1908 * @function -1909 * @param {String} csrPEM PEM formatted PKCS#10 CSR string -1910 * @return {Object} RSAKey/DSA/ECDSA public key object -1911 * @since keyutil 1.0.5 -1912 */ -1913 KEYUTIL.getKeyFromCSRPEM = function(csrPEM) { -1914 var csrHex = KEYUTIL.getHexFromPEM(csrPEM, "CERTIFICATE REQUEST"); -1915 var key = KEYUTIL.getKeyFromCSRHex(csrHex); -1916 return key; -1917 }; -1918 -1919 /** -1920 * get RSAKey/DSA/ECDSA public key object from hexadecimal string of PKCS#10 CSR -1921 * @name getKeyFromCSRHex -1922 * @memberOf KEYUTIL -1923 * @function -1924 * @param {String} csrHex hexadecimal string of PKCS#10 CSR -1925 * @return {Object} RSAKey/DSA/ECDSA public key object -1926 * @since keyutil 1.0.5 -1927 */ -1928 KEYUTIL.getKeyFromCSRHex = function(csrHex) { -1929 var info = KEYUTIL.parseCSRHex(csrHex); -1930 var key = KEYUTIL.getKey(info.p8pubkeyhex, null, "pkcs8pub"); -1931 return key; -1932 }; -1933 -1934 /** -1935 * parse hexadecimal string of PKCS#10 CSR (certificate signing request) -1936 * @name parseCSRHex -1937 * @memberOf KEYUTIL -1938 * @function -1939 * @param {String} csrHex hexadecimal string of PKCS#10 CSR -1940 * @return {Array} associative array of parsed CSR -1941 * @since keyutil 1.0.5 -1942 * @description -1943 * Resulted associative array has following properties: -1944 * <ul> -1945 * <li>p8pubkeyhex - hexadecimal string of subject public key in PKCS#8</li> -1946 * </ul> -1947 */ -1948 KEYUTIL.parseCSRHex = function(csrHex) { -1949 var result = {}; -1950 var h = csrHex; +1793 if (encAlg === undefined) encAlg = "DES-EDE3-CBC"; +1794 return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA", asn1Hex, passwd, encAlg); +1795 } +1796 +1797 // x. ====================================================================== +1798 +1799 var _getEncryptedPKCS8 = function(plainKeyHex, passcode) { +1800 var info = _getEencryptedPKCS8Info(plainKeyHex, passcode); +1801 //alert("iv=" + info.encryptionSchemeIV); +1802 //alert("info.ciphertext2[" + info.ciphertext.length + "=" + info.ciphertext); +1803 var asn1Obj = new KJUR.asn1.ASN1Util.newObject({ +1804 "seq": [ +1805 {"seq": [ +1806 {"oid": {"name": "pkcs5PBES2"}}, +1807 {"seq": [ +1808 {"seq": [ +1809 {"oid": {"name": "pkcs5PBKDF2"}}, +1810 {"seq": [ +1811 {"octstr": {"hex": info.pbkdf2Salt}}, +1812 {"int": info.pbkdf2Iter} +1813 ]} +1814 ]}, +1815 {"seq": [ +1816 {"oid": {"name": "des-EDE3-CBC"}}, +1817 {"octstr": {"hex": info.encryptionSchemeIV}} +1818 ]} +1819 ]} +1820 ]}, +1821 {"octstr": {"hex": info.ciphertext}} +1822 ] +1823 }); +1824 return asn1Obj.getEncodedHex(); +1825 }; +1826 +1827 var _getEencryptedPKCS8Info = function(plainKeyHex, passcode) { +1828 var pbkdf2Iter = 100; +1829 var pbkdf2SaltWS = CryptoJS.lib.WordArray.random(8); +1830 var encryptionSchemeAlg = "DES-EDE3-CBC"; +1831 var encryptionSchemeIVWS = CryptoJS.lib.WordArray.random(8); +1832 // PBKDF2 key +1833 var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, +1834 pbkdf2SaltWS, { "keySize": 192/32, +1835 "iterations": pbkdf2Iter }); +1836 // ENCRYPT +1837 var plainKeyWS = CryptoJS.enc.Hex.parse(plainKeyHex); +1838 var encryptedKeyHex = +1839 CryptoJS.TripleDES.encrypt(plainKeyWS, pbkdf2KeyWS, { "iv": encryptionSchemeIVWS }) + ""; +1840 +1841 //alert("encryptedKeyHex=" + encryptedKeyHex); +1842 +1843 var info = {}; +1844 info.ciphertext = encryptedKeyHex; +1845 //alert("info.ciphertext=" + info.ciphertext); +1846 info.pbkdf2Salt = CryptoJS.enc.Hex.stringify(pbkdf2SaltWS); +1847 info.pbkdf2Iter = pbkdf2Iter; +1848 info.encryptionSchemeAlg = encryptionSchemeAlg; +1849 info.encryptionSchemeIV = CryptoJS.enc.Hex.stringify(encryptionSchemeIVWS); +1850 return info; +1851 }; +1852 +1853 // x. PEM PKCS#8 plain private key of RSA private key object +1854 if (formatType == "PKCS8PRV" && +1855 typeof RSAKey != "undefined" && +1856 keyObjOrHex instanceof RSAKey && +1857 keyObjOrHex.isPrivate == true) { +1858 +1859 var keyObj = _rsaprv2asn1obj(keyObjOrHex); +1860 var keyHex = keyObj.getEncodedHex(); +1861 +1862 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ +1863 "seq": [ +1864 {"int": 0}, +1865 {"seq": [{"oid": {"name": "rsaEncryption"}},{"null": true}]}, +1866 {"octstr": {"hex": keyHex}} +1867 ] +1868 }); +1869 var asn1Hex = asn1Obj.getEncodedHex(); +1870 +1871 if (passwd === undefined || passwd == null) { +1872 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY"); +1873 } else { +1874 var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd); +1875 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY"); +1876 } +1877 } +1878 +1879 // x. PEM PKCS#8 plain private key of ECDSA private key object +1880 if (formatType == "PKCS8PRV" && +1881 typeof KJUR.crypto.ECDSA != "undefined" && +1882 keyObjOrHex instanceof KJUR.crypto.ECDSA && +1883 keyObjOrHex.isPrivate == true) { +1884 +1885 var keyObj = new KJUR.asn1.ASN1Util.newObject({ +1886 "seq": [ +1887 {"int": 1}, +1888 {"octstr": {"hex": keyObjOrHex.prvKeyHex}}, +1889 {"tag": ['a1', true, {"bitstr": {"hex": "00" + keyObjOrHex.pubKeyHex}}]} +1890 ] +1891 }); +1892 var keyHex = keyObj.getEncodedHex(); +1893 +1894 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ +1895 "seq": [ +1896 {"int": 0}, +1897 {"seq": [ +1898 {"oid": {"name": "ecPublicKey"}}, +1899 {"oid": {"name": keyObjOrHex.curveName}} +1900 ]}, +1901 {"octstr": {"hex": keyHex}} +1902 ] +1903 }); +1904 +1905 var asn1Hex = asn1Obj.getEncodedHex(); +1906 if (passwd === undefined || passwd == null) { +1907 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY"); +1908 } else { +1909 var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd); +1910 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY"); +1911 } +1912 } +1913 +1914 // x. PEM PKCS#8 plain private key of DSA private key object +1915 if (formatType == "PKCS8PRV" && +1916 typeof KJUR.crypto.DSA != "undefined" && +1917 keyObjOrHex instanceof KJUR.crypto.DSA && +1918 keyObjOrHex.isPrivate == true) { +1919 +1920 var keyObj = new KJUR.asn1.DERInteger({'bigint': keyObjOrHex.x}); +1921 var keyHex = keyObj.getEncodedHex(); +1922 +1923 var asn1Obj = KJUR.asn1.ASN1Util.newObject({ +1924 "seq": [ +1925 {"int": 0}, +1926 {"seq": [ +1927 {"oid": {"name": "dsa"}}, +1928 {"seq": [ +1929 {"int": {"bigint": keyObjOrHex.p}}, +1930 {"int": {"bigint": keyObjOrHex.q}}, +1931 {"int": {"bigint": keyObjOrHex.g}} +1932 ]} +1933 ]}, +1934 {"octstr": {"hex": keyHex}} +1935 ] +1936 }); +1937 +1938 var asn1Hex = asn1Obj.getEncodedHex(); +1939 if (passwd === undefined || passwd == null) { +1940 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY"); +1941 } else { +1942 var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd); +1943 return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY"); +1944 } +1945 } +1946 +1947 throw "unsupported object nor format"; +1948 }; +1949 +1950 // -- PUBLIC METHODS FOR CSR ------------------------------------------------------- 1951 -1952 // 1. sequence -1953 if (h.substr(0, 2) != "30") -1954 throw "malformed CSR(code:001)"; // not sequence -1955 -1956 var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(h, 0); -1957 if (a1.length < 1) -1958 throw "malformed CSR(code:002)"; // short length -1959 -1960 // 2. 2nd sequence -1961 if (h.substr(a1[0], 2) != "30") -1962 throw "malformed CSR(code:003)"; // not sequence -1963 -1964 var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(h, a1[0]); -1965 if (a2.length < 3) -1966 throw "malformed CSR(code:004)"; // 2nd seq short elem -1967 -1968 result.p8pubkeyhex = ASN1HEX.getHexOfTLV_AtObj(h, a2[2]); -1969 -1970 return result; -1971 }; -1972