blob: 7a8f762f0a562361f8b44cf1ad20c2baf71ac06b [file] [log] [blame]
Alexander Afanasyev181a8b92013-02-28 13:28:53 -08001/*! 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 (2012-May-10)
8//
9// Copyright (c) 2010-2012 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
28function _rsapem_pemToBase64(sPEMPrivateKey) {
29 var s = sPEMPrivateKey;
30 s = s.replace("-----BEGIN RSA PRIVATE KEY-----", "");
31 s = s.replace("-----END RSA PRIVATE KEY-----", "");
32 s = s.replace(/[ \n]+/g, "");
33 return s;
34}
35
36function _rsapem_getPosArrayOfChildrenFromHex(hPrivateKey) {
37 var a = new Array();
38 var v1 = ASN1HEX.getStartPosOfV_AtObj(hPrivateKey, 0);
39 var n1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, v1);
40 var e1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, n1);
41 var d1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, e1);
42 var p1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, d1);
43 var q1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, p1);
44 var dp1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, q1);
45 var dq1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, dp1);
46 var co1 = ASN1HEX.getPosOfNextSibling_AtObj(hPrivateKey, dq1);
47 a.push(v1, n1, e1, d1, p1, q1, dp1, dq1, co1);
48 return a;
49}
50
51function _rsapem_getHexValueArrayOfChildrenFromHex(hPrivateKey) {
52 var posArray = _rsapem_getPosArrayOfChildrenFromHex(hPrivateKey);
53 var v = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[0]);
54 var n = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[1]);
55 var e = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[2]);
56 var d = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[3]);
57 var p = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[4]);
58 var q = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[5]);
59 var dp = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[6]);
60 var dq = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[7]);
61 var co = ASN1HEX.getHexOfV_AtObj(hPrivateKey, posArray[8]);
62 var a = new Array();
63 a.push(v, n, e, d, p, q, dp, dq, co);
64 return a;
65}
66
67/**
68 * read PKCS#1 private key from a string
69 * @name readPrivateKeyFromPEMString
70 * @memberOf RSAKey#
71 * @function
72 * @param {String} keyPEM string of PKCS#1 private key.
73 */
74function _rsapem_readPrivateKeyFromPEMString(keyPEM) {
75 var keyB64 = _rsapem_pemToBase64(keyPEM);
76 var keyHex = b64tohex(keyB64) // depends base64.js
77 var a = _rsapem_getHexValueArrayOfChildrenFromHex(keyHex);
78 this.setPrivateEx(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
79}
80
81RSAKey.prototype.readPrivateKeyFromPEMString = _rsapem_readPrivateKeyFromPEMString;