1 /* rsapem-1.3.1.js (c) 2012-2023 Kenji Urushima | kjur.github.io/jsrsasign/license
  2  */
  3 /*
  4  * rsapem.js - Cryptographic Algorithm Provider class
  5  *
  6  * Copyright (c) 2013-2020 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * https://kjur.github.io/jsrsasign/license
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name rsapem-1.1.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version jsrsasign 8.0.21 rsapem 1.3.1 (2020-Jul-24)
 20  * @since jsrsasign 1.0
 21  * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 
 24 /**
 25  * static method to get array of field positions from hexadecimal PKCS#5 RSA private key.<br/>
 26  * @name getPosArrayOfChildrenFromHex
 27  * @memberOf RSAKey
 28  * @function
 29  * @param {String} sPEMPrivateKey PEM PKCS#1/5 s private key string
 30  * @return {Array} array of field positions
 31  * @example
 32  * RSAKey.getPosArrayOfChildrenFromHex("3082...") → [8, 32, ...]
 33  */
 34 RSAKey.getPosArrayOfChildrenFromHex = function(hPrivateKey) {
 35     return ASN1HEX.getChildIdx(hPrivateKey, 0);
 36 };
 37 
 38 /**
 39  * static method to get array of hex field values from hexadecimal PKCS#5 RSA private key.<br/>
 40  * @name getHexValueArrayOfChildrenFromHex
 41  * @memberOf RSAKey
 42  * @function
 43  * @param {String} sPEMPrivateKey PEM PKCS#1/5 s private key string
 44  * @return {Array} array of field hex value
 45  * @example
 46  * RSAKey.getHexValueArrayOfChildrenFromHex("3082...") → ["00", "3b42...", ...]
 47  */
 48 RSAKey.getHexValueArrayOfChildrenFromHex = function(hPrivateKey) {
 49     var _ASN1HEX = ASN1HEX;
 50     var _getV = _ASN1HEX.getV;
 51     var a = RSAKey.getPosArrayOfChildrenFromHex(hPrivateKey);
 52     var h_v =  _getV(hPrivateKey, a[0]);
 53     var h_n =  _getV(hPrivateKey, a[1]);
 54     var h_e =  _getV(hPrivateKey, a[2]);
 55     var h_d =  _getV(hPrivateKey, a[3]);
 56     var h_p =  _getV(hPrivateKey, a[4]);
 57     var h_q =  _getV(hPrivateKey, a[5]);
 58     var h_dp = _getV(hPrivateKey, a[6]);
 59     var h_dq = _getV(hPrivateKey, a[7]);
 60     var h_co = _getV(hPrivateKey, a[8]);
 61     var a = new Array();
 62     a.push(h_v, h_n, h_e, h_d, h_p, h_q, h_dp, h_dq, h_co);
 63     return a;
 64 };
 65 
 66 /**
 67  * read PKCS#1 private key from a string<br/>
 68  * @name readPrivateKeyFromPEMString
 69  * @memberOf RSAKey#
 70  * @function
 71  * @param {String} keyPEM string of PKCS#1 private key.
 72  */
 73 RSAKey.prototype.readPrivateKeyFromPEMString = function(keyPEM) {
 74     var keyHex = pemtohex(keyPEM);
 75     var a = RSAKey.getHexValueArrayOfChildrenFromHex(keyHex);
 76     this.setPrivateEx(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
 77 };
 78 
 79 /**
 80  * read an ASN.1 hexadecimal string of PKCS#1/5 plain RSA private key<br/>
 81  * @name readPKCS5PrvKeyHex
 82  * @memberOf RSAKey#
 83  * @function
 84  * @param {String} h hexadecimal string of PKCS#1/5 plain RSA private key
 85  * @since jsrsasign 7.1.0 rsapem 1.2.0
 86  * @see {@link RSAKey.readPrivateKeyFromASN1HexString} former method
 87  */
 88 RSAKey.prototype.readPKCS5PrvKeyHex = function(h) {
 89     var a = RSAKey.getHexValueArrayOfChildrenFromHex(h);
 90     this.setPrivateEx(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
 91 };
 92 
 93 /**
 94  * read an ASN.1 hexadecimal string of PKCS#8 plain RSA private key<br/>
 95  * @name readPKCS8PrvKeyHex
 96  * @memberOf RSAKey#
 97  * @function
 98  * @param {String} h hexadecimal string of PKCS#8 plain RSA private key
 99  * @since jsrsasign 7.1.0 rsapem 1.2.0
100  */
101 RSAKey.prototype.readPKCS8PrvKeyHex = function(h) {
102     var hN, hE, hD, hP, hQ, hDP, hDQ, hCO;
103     var _ASN1HEX = ASN1HEX;
104     var _getVbyListEx = _ASN1HEX.getVbyListEx;
105 
106     if (_ASN1HEX.isASN1HEX(h) === false)
107 	throw new Error("not ASN.1 hex string");
108 
109     try {
110 	hN  = _getVbyListEx(h, 0, [2, 0, 1], "02");
111 	hE  = _getVbyListEx(h, 0, [2, 0, 2], "02");
112 	hD  = _getVbyListEx(h, 0, [2, 0, 3], "02");
113 	hP  = _getVbyListEx(h, 0, [2, 0, 4], "02");
114 	hQ  = _getVbyListEx(h, 0, [2, 0, 5], "02");
115 	hDP = _getVbyListEx(h, 0, [2, 0, 6], "02");
116 	hDQ = _getVbyListEx(h, 0, [2, 0, 7], "02");
117 	hCO = _getVbyListEx(h, 0, [2, 0, 8], "02");
118     } catch(ex) {
119 	throw new Error("malformed PKCS#8 plain RSA private key");
120     }
121 
122     this.setPrivateEx(hN, hE, hD, hP, hQ, hDP, hDQ, hCO);
123 };
124 
125 /**
126  * read an ASN.1 hexadecimal string of PKCS#5 RSA public key<br/>
127  * @name readPKCS5PubKeyHex
128  * @memberOf RSAKey#
129  * @function
130  * @param {String} h hexadecimal string of PKCS#5 public key
131  * @since jsrsasign 7.1.0 rsapem 1.2.0
132  */
133 RSAKey.prototype.readPKCS5PubKeyHex = function(h) {
134     var _ASN1HEX = ASN1HEX;
135     var _getV = _ASN1HEX.getV;
136 
137     if (_ASN1HEX.isASN1HEX(h) === false)
138 	throw new Error("keyHex is not ASN.1 hex string");
139     var aIdx = _ASN1HEX.getChildIdx(h, 0);
140     if (aIdx.length !== 2 ||
141 	h.substr(aIdx[0], 2) !== "02" ||
142 	h.substr(aIdx[1], 2) !== "02")
143 	throw new Error("wrong hex for PKCS#5 public key");
144     var hN = _getV(h, aIdx[0]);
145     var hE = _getV(h, aIdx[1]);
146     this.setPublic(hN, hE);
147 };
148 
149 /**
150  * read an ASN.1 hexadecimal string of PKCS#8 RSA public key<br/>
151  * @name readPKCS8PubKeyHex
152  * @memberOf RSAKey#
153  * @function
154  * @param {String} h hexadecimal string of PKCS#8 public key
155  * @since jsrsasign 7.1.0 rsapem 1.2.0
156  */
157 RSAKey.prototype.readPKCS8PubKeyHex = function(h) {
158     var _ASN1HEX = ASN1HEX;
159     if (_ASN1HEX.isASN1HEX(h) === false)
160 	throw new Error("not ASN.1 hex string");
161 
162     // 06092a864886f70d010101: OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
163     if (_ASN1HEX.getTLVbyListEx(h, 0, [0, 0]) !== "06092a864886f70d010101")
164 	throw new Error("not PKCS8 RSA public key");
165 
166     var p5hex = _ASN1HEX.getTLVbyListEx(h, 0, [1, 0]);
167     this.readPKCS5PubKeyHex(p5hex);
168 };
169 
170 /**
171  * read an ASN.1 hexadecimal string of X.509 RSA public key certificate<br/>
172  * @name readCertPubKeyHex
173  * @memberOf RSAKey#
174  * @function
175  * @param {String} h hexadecimal string of X.509 RSA public key certificate
176  * @param {Integer} nthPKI nth index of publicKeyInfo. (DEFAULT: 6 for X509v3)
177  * @since jsrsasign 7.1.0 rsapem 1.2.0
178  */
179 RSAKey.prototype.readCertPubKeyHex = function(h, nthPKI) {
180     var x, hPub;
181     x = new X509();
182     x.readCertHex(h);
183     hPub = x.getPublicKeyHex();
184     this.readPKCS8PubKeyHex(hPub);
185 };
186