1 /*! rsapem-1.1.js (c) 2012 Kenji Urushima | kjur.github.com/jsrsasign/license 2 */ 3 // 4 // rsa-pem.js - adding function for reading/writing PKCS#1 PEM private key 5 // to RSAKey class. 6 // 7 // version: 1.1.1 (2013-Apr-12) 8 // 9 // Copyright (c) 2010-2013 Kenji Urushima (kenji.urushima@gmail.com) 10 // 11 // This software is licensed under the terms of the MIT License. 12 // http://kjur.github.com/jsrsasign/license/ 13 // 14 // The above copyright and license notice shall be 15 // included in all copies or substantial portions of the Software. 16 // 17 // 18 // Depends on: 19 // 20 // 21 // 22 // _RSApem_pemToBase64(sPEM) 23 // 24 // removing PEM header, PEM footer and space characters including 25 // new lines from PEM formatted RSA private key string. 26 // 27 28 /** 29 * @fileOverview 30 * @name rsapem-1.1.js 31 * @author Kenji Urushima kenji.urushima@gmail.com 32 * @version 1.1 33 * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a> 34 */ 35 function _rsapem_pemToBase64(sPEMPrivateKey) { 36 var s = sPEMPrivateKey; 37 s = s.replace("-----BEGIN RSA PRIVATE KEY-----", ""); 38 s = s.replace("-----END RSA PRIVATE KEY-----", ""); 39 s = s.replace(/[ \n]+/g, ""); 40 return s; 41 } 42 43 function _rsapem_getPosArrayOfChildrenFromHex(hPrivateKey) { 44 var a = new Array(); 45 var v1 = ASN1HEX.getStartPosOfV_AtObj(hPrivateKey, 0); 46 var n1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, v1); 47 var e1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, n1); 48 var d1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, e1); 49 var p1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, d1); 50 var q1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, p1); 51 var dp1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, q1); 52 var dq1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, dp1); 53 var co1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, dq1); 54 a.push(v1, n1, e1, d1, p1, q1, dp1, dq1, co1); 55 return a; 56 } 57 58 function _rsapem_getHexValueArrayOfChildrenFromHex(hPrivateKey) { 59 var posArray = _rsapem_getPosArrayOfChildrenFromHex(hPrivateKey); 60 var v = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[0]); 61 var n = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[1]); 62 var e = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[2]); 63 var d = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[3]); 64 var p = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[4]); 65 var q = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[5]); 66 var dp = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[6]); 67 var dq = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[7]); 68 var co = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[8]); 69 var a = new Array(); 70 a.push(v, n, e, d, p, q, dp, dq, co); 71 return a; 72 } 73 74 /** 75 * read RSA private key from a ASN.1 hexadecimal string 76 * @name readPrivateKeyFromASN1HexString 77 * @memberOf RSAKey# 78 * @function 79 * @param {String} keyHex ASN.1 hexadecimal string of PKCS#1 private key. 80 * @since 1.1.1 81 */ 82 function _rsapem_readPrivateKeyFromASN1HexString(keyHex) { 83 var a = _rsapem_getHexValueArrayOfChildrenFromHex(keyHex); 84 this.setPrivateEx(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]); 85 } 86 87 /** 88 * read PKCS#1 private key from a string 89 * @name readPrivateKeyFromPEMString 90 * @memberOf RSAKey# 91 * @function 92 * @param {String} keyPEM string of PKCS#1 private key. 93 */ 94 function _rsapem_readPrivateKeyFromPEMString(keyPEM) { 95 var keyB64 = _rsapem_pemToBase64(keyPEM); 96 var keyHex = b64tohex(keyB64) // depends base64.js 97 var a = _rsapem_getHexValueArrayOfChildrenFromHex(keyHex); 98 this.setPrivateEx(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]); 99 } 100 101 RSAKey.prototype.readPrivateKeyFromPEMString = _rsapem_readPrivateKeyFromPEMString; 102 RSAKey.prototype.readPrivateKeyFromASN1HexString = _rsapem_readPrivateKeyFromASN1HexString; 103