From cf31c931bd67d68278c45dba885f057e13bba507 Mon Sep 17 00:00:00 2001 From: Kenji Urushima Date: Sat, 8 Oct 2016 10:02:32 +0900 Subject: [PATCH] 6.1.2 release --- ChangeLog.txt | 4 + README.md | 4 + api/files.html | 14 +- api/symbols/ASN1HEX.html | 136 +- api/symbols/src/asn1hex-1.1.js.html | 1117 ++++---- api/symbols/src/asn1ocsp-1.0.js.html | 44 +- api/symbols/src/asn1x509-1.0.js.html | 1799 ++++++------ api/symbols/src/crypto-1.1.js.html | 823 +++--- api/symbols/src/jws-3.3.js.html | 1898 ++++++------ api/symbols/src/keyutil-1.0.js.html | 3814 +++++++++++++------------ api/symbols/src/pkcs5pkey-1.0.js.html | 1966 ++++++------- asn1x509-1.0.js | 6 +- bower.json | 2 +- crypto-1.1.js | 4 +- jsrsasign-latest-all-min.js | 30 +- jws-3.3.js | 4 +- keyutil-1.0.js | 4 +- min/asn1hex-1.1.min.js | 4 +- min/asn1ocsp-1.0.min.js | 4 +- min/asn1x509-1.0.min.js | 4 +- min/crypto-1.1.min.js | 4 +- min/jws-3.3.min.js | 4 +- min/keyutil-1.0.min.js | 4 +- min/pkcs5pkey-1.0.min.js | 4 +- npm/README.md | 4 + npm/lib/jsrsasign.js | 30 +- npm/package.json | 2 +- pkcs5pkey-1.0.js | 6 +- 28 files changed, 5958 insertions(+), 5781 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index b970f75d..a098e4c8 100755 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -2,12 +2,16 @@ ChangeLog for jsrsasign * Changes between 6.1.1 to 6.1.2 (2016-Oct-08) + - OCSP Response support - asn1hex 1.1.6 to 1.1.7 - refactoring for static methods - encapsulated BitString support - more API documents - asn1ocsp 1.0.0 to 1.0.1 - OCSPUtil.getOCSPResponseInfo added + - merge RegExp fix pull request from @huokko. + Thank you @huokko. + (asn1x509, crypto, jws, keyutil, pkcs5pkey) * Changes between 6.1.0 to 6.1.1 (2016-Sep-25) - asn1 1.0.10 to 1.0.11 diff --git a/README.md b/README.md index 998e18a4..1c0f5703 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ jsrsasign ========= +[![license](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/kjur/jsrsasign/blob/master/LICENSE.txt) +[![bower](https://img.shields.io/bower/v/jsrsasign.svg?maxAge=2592000)](https://libraries.io/bower/jsrsasign) +[![npm version](https://badge.fury.io/js/jsrsasign.svg)](https://badge.fury.io/js/jsrsasign) + The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES JSON Web Signature/Token/Key in pure JavaScript. Public page is http://kjur.github.com/jsrsasign . diff --git a/api/files.html b/api/files.html index 5a8c84e3..59e520b6 100644 --- a/api/files.html +++ b/api/files.html @@ -511,7 +511,7 @@

asn1hex-1.1.js

Version:
-
asn1hex 1.1.6 (2015-Jun-11)
+
asn1hex 1.1.7 (2016-Oct-02)
@@ -530,7 +530,7 @@

asn1ocsp-1.0.js

Version:
-
1.0.0 (2016-Sep-22)
+
1.0.1 (2016-Oct-02)
@@ -568,7 +568,7 @@

asn1x509-1.0.js

Version:
-
1.0.14 (2016-May-10)
+
1.0.15 (2016-Oct-08)
@@ -606,7 +606,7 @@

crypto-1.1.js

Version:
-
1.1.8 (2016-Feb-28)
+
1.1.9 (2016-Oct-08)
@@ -682,7 +682,7 @@

jws-3.3.js

Version:
-
3.3.4 (2016-May-17)
+
3.3.5 (2016-Oct-08)
@@ -720,7 +720,7 @@

keyutil-1.0.js

Version:
-
keyutil 1.0.13 (2016-Aug-11)
+
keyutil 1.0.14 (2016-Oct-08)
@@ -758,7 +758,7 @@

pkcs5pkey-1.0.js

Version:
-
pkcs5pkey 1.0.6 (2014-Apr-16)
+
pkcs5pkey 1.0.7 (2016-Oct-08)
diff --git a/api/symbols/ASN1HEX.html b/api/symbols/ASN1HEX.html index 4fdc1e33..f451268a 100644 --- a/api/symbols/ASN1HEX.html +++ b/api/symbols/ASN1HEX.html @@ -504,7 +504,7 @@

ASN1HEX.getByteLengthOfL_AtObj(s, pos)
-
get byte length for ASN.1 L(length) bytes
+
get byte length for ASN.1 L(length) bytes
@@ -531,7 +531,7 @@

ASN1HEX.getDecendantIndexByNthList(h, currentIndex, nthList)
-
get string index of nth child object of ASN.1 object refered by h, idx
+
get string index of nth child object of ASN.1 object refered by h, idx
@@ -540,7 +540,7 @@

ASN1HEX.getHexOfL_AtObj(s, pos)
-
get hexadecimal string for ASN.1 L(length) bytes
+
get hexadecimal string for ASN.1 L(length) bytes
@@ -549,7 +549,7 @@

ASN1HEX.getHexOfTLV_AtObj(s, pos)
-
get hexadecimal string of ASN.1 TLV at
+
get hexadecimal string of ASN.1 TLV at
@@ -567,7 +567,7 @@

ASN1HEX.getIntOfL_AtObj(s, pos)
-
get integer value of ASN.1 length for ASN.1 data
+
get integer value of ASN.1 length for ASN.1 data
@@ -576,16 +576,18 @@

ASN1HEX.getNthChildIndex_AtObj(h, idx, nth)
-
get string index of nth child object of ASN.1 object refered by h, idx
+
get string index of nth child object of ASN.1 object refered by h, idx
<static>   -
ASN1HEX.getPosArrayOfChildren_AtObj(s, start) + -
get array of indexes of child ASN.1 objects
+
get array of string indexes of child ASN.1 objects
+This method returns array of integers for a concatination of ASN.1 objects +in a ASN.1 value.
@@ -594,7 +596,7 @@

ASN1HEX.getPosOfNextSibling_AtObj(s, pos)
-
get next sibling starting index for ASN.1 object string
+
get next sibling starting index for ASN.1 object string
@@ -607,12 +609,23 @@

+ + <static>   + +
ASN1HEX.getVbyList(h, currentIndex, nthList, checkingTag) +
+
get ASN.1 value by nthList
+This static method is to get a ASN.1 value which specified "nthList" position +with checking expected tag "checkingTag".
+ + + <static>  
ASN1HEX.hextooidstr(hex)
-
get OID string from hexadecimal encoded value
+
get OID string from hexadecimal encoded value
@@ -671,6 +684,7 @@

  • ACCESS NESTED ASN.1 STRUCTURE
  • - get byte length for ASN.1 L(length) bytes + get byte length for ASN.1 L(length) bytes
    @@ -1015,7 +1029,7 @@

    - get string index of nth child object of ASN.1 object refered by h, idx + get string index of nth child object of ASN.1 object refered by h, idx
    @@ -1090,7 +1104,7 @@

    - get hexadecimal string for ASN.1 L(length) bytes + get hexadecimal string for ASN.1 L(length) bytes
    @@ -1139,7 +1153,7 @@

    - get hexadecimal string of ASN.1 TLV at + get hexadecimal string of ASN.1 TLV at
    @@ -1168,7 +1182,7 @@

    Since:
    -
    1.1
    +
    asn1hex 1.1
    @@ -1243,7 +1257,7 @@

    - get integer value of ASN.1 length for ASN.1 data + get integer value of ASN.1 length for ASN.1 data
    @@ -1292,7 +1306,7 @@

    - get string index of nth child object of ASN.1 object refered by h, idx + get string index of nth child object of ASN.1 object refered by h, idx
    @@ -1349,32 +1363,44 @@

    <static> {Array of Number} - ASN1HEX.getPosArrayOfChildren_AtObj(s, start) + ASN1HEX.getPosArrayOfChildren_AtObj(h, pos)
    - get array of indexes of child ASN.1 objects + get array of string indexes of child ASN.1 objects
    +This method returns array of integers for a concatination of ASN.1 objects +in a ASN.1 value. As for BITSTRING, one byte of unusedbits is skipped. +As for other ASN.1 simple types such as INTEGER, OCTET STRING or PRINTABLE STRING, +it returns a array of a string index of its ASN.1 value.
    +NOTE: Since asn1hex 1.1.7 of jsrsasign 6.1.2, Encapsulated BitString is supported.
    +
    ASN1HEX.getPosArrayOfChildren_AtObj("0203012345", 0) ⇒ [4] // INTEGER 012345
    +ASN1HEX.getPosArrayOfChildren_AtObj("1303616161", 0) ⇒ [4] // PrintableString aaa
    +ASN1HEX.getPosArrayOfChildren_AtObj("030300ffff", 0) ⇒ [6] // BITSTRING ffff (unusedbits=00a)
    +ASN1HEX.getPosArrayOfChildren_AtObj("3006020104020105", 0) ⇒ [4, 10] // SEQUENCE(INT4,INT5)
    + + +
    Parameters:
    - {String} s + {String} h
    hexadecimal string of ASN.1 DER encoded data
    - {Number} start + {Number} pos
    -
    string index of ASN.1 object
    +
    start string index of ASN.1 object
    @@ -1402,7 +1428,7 @@

    - get next sibling starting index for ASN.1 object string + get next sibling starting index for ASN.1 object string
    @@ -1483,6 +1509,68 @@

    +
    + + +
    <static> + + + ASN1HEX.getVbyList(h, currentIndex, nthList, checkingTag) + +
    +
    + get ASN.1 value by nthList
    +This static method is to get a ASN.1 value which specified "nthList" position +with checking expected tag "checkingTag". + + +
    + + + + +
    +
    Parameters:
    + +
    + {String} h + +
    +
    hexadecimal string of ASN.1 structure
    + +
    + {Integer} currentIndex + +
    +
    string index to start searching in hexadecimal string "h"
    + +
    + {Array} nthList + +
    +
    array of nth list index
    + +
    + {String} checkingTag + +
    +
    (OPTIONAL) string of expected ASN.1 tag for nthList
    + +
    + + + +
    +
    Since:
    +
    asn1hex 1.1.4
    +
    + + + + + + +
    @@ -1493,7 +1581,7 @@

    - get OID string from hexadecimal encoded value + get OID string from hexadecimal encoded value
    diff --git a/api/symbols/src/asn1hex-1.1.js.html b/api/symbols/src/asn1hex-1.1.js.html index 7e3f7ae5..0a4084e0 100644 --- a/api/symbols/src/asn1hex-1.1.js.html +++ b/api/symbols/src/asn1hex-1.1.js.html @@ -5,7 +5,7 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! asn1hex-1.1.6.js (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! asn1hex-1.1.7.js (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
       2  */
       3 /*
       4  * asn1hex.js - Hexadecimal represented ASN.1 string library
    @@ -23,7 +23,7 @@
      16  * @fileOverview
      17  * @name asn1hex-1.1.js
      18  * @author Kenji Urushima kenji.urushima@gmail.com
    - 19  * @version asn1hex 1.1.6 (2015-Jun-11)
    + 19  * @version asn1hex 1.1.7 (2016-Oct-02)
      20  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      21  */
      22 
    @@ -68,546 +68,577 @@
      61  * </li>
      62  * <li><b>ACCESS NESTED ASN.1 STRUCTURE</b>
      63  *   <ul>
    - 64  *   <li>{@link ASN1HEX.getDecendantHexTLVByNthList} - get ASN.1 TLV at specified list index</li>
    - 65  *   <li>{@link ASN1HEX.getDecendantHexVByNthList} - get ASN.1 V at specified list index</li>
    - 66  *   <li>{@link ASN1HEX.getDecendantIndexByNthList} - get index at specified list index</li>
    - 67  *   </ul>
    - 68  * </li>
    - 69  * <li><b>UTILITIES</b>
    - 70  *   <ul>
    - 71  *   <li>{@link ASN1HEX.dump} - dump ASN.1 structure</li>
    - 72  *   <li>{@link ASN1HEX.isASN1HEX} - check whether ASN.1 hexadecimal string or not</li>
    - 73  *   <li>{@link ASN1HEX.hextooidstr} - convert hexadecimal string of OID to dotted integer list</li>
    - 74  *   </ul>
    - 75  * </li>
    - 76  * </ul>
    - 77  */
    - 78 var ASN1HEX = new function() {
    - 79     /**
    - 80      * get byte length for ASN.1 L(length) bytes
    - 81      * @name getByteLengthOfL_AtObj
    - 82      * @memberOf ASN1HEX
    - 83      * @function
    - 84      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    - 85      * @param {Number} pos string index
    - 86      * @return byte length for ASN.1 L(length) bytes
    - 87      */
    - 88     this.getByteLengthOfL_AtObj = function(s, pos) {
    - 89         if (s.substring(pos + 2, pos + 3) != '8') return 1;
    - 90         var i = parseInt(s.substring(pos + 3, pos + 4));
    - 91         if (i == 0) return -1;          // length octet '80' indefinite length
    - 92         if (0 < i && i < 10) return i + 1;      // including '8?' octet;
    - 93         return -2;                              // malformed format
    - 94     };
    - 95 
    - 96     /**
    - 97      * get hexadecimal string for ASN.1 L(length) bytes
    - 98      * @name getHexOfL_AtObj
    - 99      * @memberOf ASN1HEX
    -100      * @function
    -101      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -102      * @param {Number} pos string index
    -103      * @return {String} hexadecimal string for ASN.1 L(length) bytes
    -104      */
    -105     this.getHexOfL_AtObj = function(s, pos) {
    -106         var len = this.getByteLengthOfL_AtObj(s, pos);
    -107         if (len < 1) return '';
    -108         return s.substring(pos + 2, pos + 2 + len * 2);
    -109     };
    -110 
    -111     //   getting ASN.1 length value at the position 'idx' of
    -112     //   hexa decimal string 's'.
    -113     //
    -114     //   f('3082025b02...', 0) ... 82025b ... ???
    -115     //   f('020100', 0) ... 01 ... 1
    -116     //   f('0203001...', 0) ... 03 ... 3
    -117     //   f('02818003...', 0) ... 8180 ... 128
    -118     /**
    -119      * get integer value of ASN.1 length for ASN.1 data
    -120      * @name getIntOfL_AtObj
    -121      * @memberOf ASN1HEX
    -122      * @function
    -123      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -124      * @param {Number} pos string index
    -125      * @return ASN.1 L(length) integer value
    -126      */
    -127     this.getIntOfL_AtObj = function(s, pos) {
    -128         var hLength = this.getHexOfL_AtObj(s, pos);
    -129         if (hLength == '') return -1;
    -130         var bi;
    -131         if (parseInt(hLength.substring(0, 1)) < 8) {
    -132             bi = new BigInteger(hLength, 16);
    -133         } else {
    -134             bi = new BigInteger(hLength.substring(2), 16);
    -135         }
    -136         return bi.intValue();
    -137     };
    -138 
    -139     /**
    -140      * get ASN.1 value starting string position for ASN.1 object refered by index 'idx'.
    -141      * @name getStartPosOfV_AtObj
    -142      * @memberOf ASN1HEX
    -143      * @function
    -144      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -145      * @param {Number} pos string index
    -146      */
    -147     this.getStartPosOfV_AtObj = function(s, pos) {
    -148         var l_len = this.getByteLengthOfL_AtObj(s, pos);
    -149         if (l_len < 0) return l_len;
    -150         return pos + (l_len + 1) * 2;
    -151     };
    -152 
    -153     /**
    -154      * get hexadecimal string of ASN.1 V(value)
    -155      * @name getHexOfV_AtObj
    -156      * @memberOf ASN1HEX
    -157      * @function
    -158      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -159      * @param {Number} pos string index
    -160      * @return {String} hexadecimal string of ASN.1 value.
    -161      */
    -162     this.getHexOfV_AtObj = function(s, pos) {
    -163         var pos1 = this.getStartPosOfV_AtObj(s, pos);
    -164         var len = this.getIntOfL_AtObj(s, pos);
    -165         return s.substring(pos1, pos1 + len * 2);
    -166     };
    -167 
    -168     /**
    -169      * get hexadecimal string of ASN.1 TLV at
    -170      * @name getHexOfTLV_AtObj
    -171      * @memberOf ASN1HEX
    -172      * @function
    -173      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -174      * @param {Number} pos string index
    -175      * @return {String} hexadecimal string of ASN.1 TLV.
    -176      * @since 1.1
    -177      */
    -178     this.getHexOfTLV_AtObj = function(s, pos) {
    -179         var hT = s.substr(pos, 2);
    -180         var hL = this.getHexOfL_AtObj(s, pos);
    -181         var hV = this.getHexOfV_AtObj(s, pos);
    -182         return hT + hL + hV;
    -183     };
    -184 
    -185     /**
    -186      * get next sibling starting index for ASN.1 object string
    -187      * @name getPosOfNextSibling_AtObj
    -188      * @memberOf ASN1HEX
    -189      * @function
    -190      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -191      * @param {Number} pos string index
    -192      * @return next sibling starting index for ASN.1 object string
    -193      */
    -194     this.getPosOfNextSibling_AtObj = function(s, pos) {
    -195         var pos1 = this.getStartPosOfV_AtObj(s, pos);
    -196         var len = this.getIntOfL_AtObj(s, pos);
    -197         return pos1 + len * 2;
    -198     };
    -199 
    -200     /**
    -201      * get array of indexes of child ASN.1 objects
    -202      * @name getPosArrayOfChildren_AtObj
    -203      * @memberOf ASN1HEX
    -204      * @function
    -205      * @param {String} s hexadecimal string of ASN.1 DER encoded data
    -206      * @param {Number} start string index of ASN.1 object
    -207      * @return {Array of Number} array of indexes for childen of ASN.1 objects
    -208      */
    -209     this.getPosArrayOfChildren_AtObj = function(h, pos) {
    -210         var a = new Array();
    -211         var p0 = this.getStartPosOfV_AtObj(h, pos);
    -212         a.push(p0);
    -213 
    -214         var len = this.getIntOfL_AtObj(h, pos);
    -215         var p = p0;
    -216         var k = 0;
    -217         while (1) {
    -218             var pNext = this.getPosOfNextSibling_AtObj(h, p);
    -219             if (pNext == null || (pNext - p0  >= (len * 2))) break;
    -220             if (k >= 200) break;
    -221             
    -222             a.push(pNext);
    -223             p = pNext;
    -224             
    -225             k++;
    -226         }
    -227         
    -228         return a;
    -229     };
    -230 
    -231     /**
    -232      * get string index of nth child object of ASN.1 object refered by h, idx
    -233      * @name getNthChildIndex_AtObj
    -234      * @memberOf ASN1HEX
    -235      * @function
    -236      * @param {String} h hexadecimal string of ASN.1 DER encoded data
    -237      * @param {Number} idx start string index of ASN.1 object
    -238      * @param {Number} nth for child
    -239      * @return {Number} string index of nth child.
    -240      * @since 1.1
    -241      */
    -242     this.getNthChildIndex_AtObj = function(h, idx, nth) {
    -243         var a = this.getPosArrayOfChildren_AtObj(h, idx);
    -244         return a[nth];
    -245     };
    -246 
    -247     // ========== decendant methods ==============================
    -248     /**
    -249      * get string index of nth child object of ASN.1 object refered by h, idx
    -250      * @name getDecendantIndexByNthList
    -251      * @memberOf ASN1HEX
    -252      * @function
    -253      * @param {String} h hexadecimal string of ASN.1 DER encoded data
    -254      * @param {Number} currentIndex start string index of ASN.1 object
    -255      * @param {Array of Number} nthList array list of nth
    -256      * @return {Number} string index refered by nthList
    -257      * @since 1.1
    -258      * @example
    -259      * The "nthList" is a index list of structured ASN.1 object
    -260      * reference. Here is a sample structure and "nthList"s which
    -261      * refers each objects.
    -262      *
    -263      * SQUENCE               - 
    -264      *   SEQUENCE            - [0]
    -265      *     IA5STRING 000     - [0, 0]
    -266      *     UTF8STRING 001    - [0, 1]
    -267      *   SET                 - [1]
    -268      *     IA5STRING 010     - [1, 0]
    -269      *     UTF8STRING 011    - [1, 1]
    -270      */
    -271     this.getDecendantIndexByNthList = function(h, currentIndex, nthList) {
    -272         if (nthList.length == 0) {
    -273             return currentIndex;
    -274         }
    -275         var firstNth = nthList.shift();
    -276         var a = this.getPosArrayOfChildren_AtObj(h, currentIndex);
    -277         return this.getDecendantIndexByNthList(h, a[firstNth], nthList);
    -278     };
    -279 
    -280     /**
    -281      * get hexadecimal string of ASN.1 TLV refered by current index and nth index list.
    -282      * @name getDecendantHexTLVByNthList
    -283      * @memberOf ASN1HEX
    -284      * @function
    -285      * @param {String} h hexadecimal string of ASN.1 DER encoded data
    -286      * @param {Number} currentIndex start string index of ASN.1 object
    -287      * @param {Array of Number} nthList array list of nth
    -288      * @return {Number} hexadecimal string of ASN.1 TLV refered by nthList
    -289      * @since 1.1
    -290      */
    -291     this.getDecendantHexTLVByNthList = function(h, currentIndex, nthList) {
    -292         var idx = this.getDecendantIndexByNthList(h, currentIndex, nthList);
    -293         return this.getHexOfTLV_AtObj(h, idx);
    -294     };
    -295 
    -296     /**
    -297      * get hexadecimal string of ASN.1 V refered by current index and nth index list.
    -298      * @name getDecendantHexVByNthList
    -299      * @memberOf ASN1HEX
    -300      * @function
    -301      * @param {String} h hexadecimal string of ASN.1 DER encoded data
    -302      * @param {Number} currentIndex start string index of ASN.1 object
    -303      * @param {Array of Number} nthList array list of nth
    -304      * @return {Number} hexadecimal string of ASN.1 V refered by nthList
    -305      * @since 1.1
    -306      */
    -307     this.getDecendantHexVByNthList = function(h, currentIndex, nthList) {
    -308         var idx = this.getDecendantIndexByNthList(h, currentIndex, nthList);
    -309         return this.getHexOfV_AtObj(h, idx);
    -310     };
    -311 };
    -312 
    -313 /*
    -314  * @since asn1hex 1.1.4
    -315  */
    -316 ASN1HEX.getVbyList = function(h, currentIndex, nthList, checkingTag) {
    -317     var idx = this.getDecendantIndexByNthList(h, currentIndex, nthList);
    -318     if (idx === undefined) {
    -319         throw "can't find nthList object";
    -320     }
    -321     if (checkingTag !== undefined) {
    -322         if (h.substr(idx, 2) != checkingTag) {
    -323             throw "checking tag doesn't match: " + 
    -324                 h.substr(idx,2) + "!=" + checkingTag;
    -325         }
    -326     }
    -327     return this.getHexOfV_AtObj(h, idx);
    -328 };
    -329 
    -330 /**
    -331  * get OID string from hexadecimal encoded value
    -332  * @name hextooidstr
    -333  * @memberOf ASN1HEX
    -334  * @function
    -335  * @param {String} hex hexadecmal string of ASN.1 DER encoded OID value
    -336  * @return {String} OID string (ex. '1.2.3.4.567')
    -337  * @since asn1hex 1.1.5
    -338  */
    -339 ASN1HEX.hextooidstr = function(hex) {
    -340     var zeroPadding = function(s, len) {
    -341         if (s.length >= len) return s;
    -342         return new Array(len - s.length + 1).join('0') + s;
    -343     };
    -344 
    -345     var a = [];
    -346 
    -347     // a[0], a[1]
    -348     var hex0 = hex.substr(0, 2);
    -349     var i0 = parseInt(hex0, 16);
    -350     a[0] = new String(Math.floor(i0 / 40));
    -351     a[1] = new String(i0 % 40);
    -352 
    -353     // a[2]..a[n]
    -354    var hex1 = hex.substr(2);
    -355     var b = [];
    -356     for (var i = 0; i < hex1.length / 2; i++) {
    -357     b.push(parseInt(hex1.substr(i * 2, 2), 16));
    -358     }
    -359     var c = [];
    -360     var cbin = "";
    -361     for (var i = 0; i < b.length; i++) {
    -362         if (b[i] & 0x80) {
    -363             cbin = cbin + zeroPadding((b[i] & 0x7f).toString(2), 7);
    -364         } else {
    -365             cbin = cbin + zeroPadding((b[i] & 0x7f).toString(2), 7);
    -366             c.push(new String(parseInt(cbin, 2)));
    -367             cbin = "";
    -368         }
    -369     }
    -370 
    -371     var s = a.join(".");
    -372     if (c.length > 0) s = s + "." + c.join(".");
    -373     return s;
    -374 };
    + 64  *   <li>{@link ASN1HEX.getVbyList} - get ASN.1 V at specified nth list index with checking expected tag</li>
    + 65  *   <li>{@link ASN1HEX.getDecendantHexTLVByNthList} - get ASN.1 TLV at specified list index</li>
    + 66  *   <li>{@link ASN1HEX.getDecendantHexVByNthList} - get ASN.1 V at specified list index</li>
    + 67  *   <li>{@link ASN1HEX.getDecendantIndexByNthList} - get index at specified list index</li>
    + 68  *   </ul>
    + 69  * </li>
    + 70  * <li><b>UTILITIES</b>
    + 71  *   <ul>
    + 72  *   <li>{@link ASN1HEX.dump} - dump ASN.1 structure</li>
    + 73  *   <li>{@link ASN1HEX.isASN1HEX} - check whether ASN.1 hexadecimal string or not</li>
    + 74  *   <li>{@link ASN1HEX.hextooidstr} - convert hexadecimal string of OID to dotted integer list</li>
    + 75  *   </ul>
    + 76  * </li>
    + 77  * </ul>
    + 78  */
    + 79 var ASN1HEX = new function() {
    + 80 };
    + 81 
    + 82 /**
    + 83  * get byte length for ASN.1 L(length) bytes<br/>
    + 84  * @name getByteLengthOfL_AtObj
    + 85  * @memberOf ASN1HEX
    + 86  * @function
    + 87  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    + 88  * @param {Number} pos string index
    + 89  * @return byte length for ASN.1 L(length) bytes
    + 90  */
    + 91 ASN1HEX.getByteLengthOfL_AtObj = function(s, pos) {
    + 92     if (s.substring(pos + 2, pos + 3) != '8') return 1;
    + 93     var i = parseInt(s.substring(pos + 3, pos + 4));
    + 94     if (i == 0) return -1;             // length octet '80' indefinite length
    + 95     if (0 < i && i < 10) return i + 1; // including '8?' octet;
    + 96     return -2;                         // malformed format
    + 97 };
    + 98 
    + 99 /**
    +100  * get hexadecimal string for ASN.1 L(length) bytes<br/>
    +101  * @name getHexOfL_AtObj
    +102  * @memberOf ASN1HEX
    +103  * @function
    +104  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    +105  * @param {Number} pos string index
    +106  * @return {String} hexadecimal string for ASN.1 L(length) bytes
    +107  */
    +108 ASN1HEX.getHexOfL_AtObj = function(s, pos) {
    +109     var len = ASN1HEX.getByteLengthOfL_AtObj(s, pos);
    +110     if (len < 1) return '';
    +111     return s.substring(pos + 2, pos + 2 + len * 2);
    +112 };
    +113 
    +114 /**
    +115  * get integer value of ASN.1 length for ASN.1 data<br/>
    +116  * @name getIntOfL_AtObj
    +117  * @memberOf ASN1HEX
    +118  * @function
    +119  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    +120  * @param {Number} pos string index
    +121  * @return ASN.1 L(length) integer value
    +122  */
    +123 /*
    +124  getting ASN.1 length value at the position 'idx' of
    +125  hexa decimal string 's'.
    +126  f('3082025b02...', 0) ... 82025b ... ???
    +127  f('020100', 0) ... 01 ... 1
    +128  f('0203001...', 0) ... 03 ... 3
    +129  f('02818003...', 0) ... 8180 ... 128
    +130  */
    +131 ASN1HEX.getIntOfL_AtObj = function(s, pos) {
    +132     var hLength = ASN1HEX.getHexOfL_AtObj(s, pos);
    +133     if (hLength == '') return -1;
    +134     var bi;
    +135     if (parseInt(hLength.substring(0, 1)) < 8) {
    +136         bi = new BigInteger(hLength, 16);
    +137     } else {
    +138         bi = new BigInteger(hLength.substring(2), 16);
    +139     }
    +140     return bi.intValue();
    +141 };
    +142 
    +143 /**
    +144  * get ASN.1 value starting string position for ASN.1 object refered by index 'idx'.
    +145  * @name getStartPosOfV_AtObj
    +146  * @memberOf ASN1HEX
    +147  * @function
    +148  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    +149  * @param {Number} pos string index
    +150  */
    +151 ASN1HEX.getStartPosOfV_AtObj = function(s, pos) {
    +152     var l_len = ASN1HEX.getByteLengthOfL_AtObj(s, pos);
    +153     if (l_len < 0) return l_len;
    +154     return pos + (l_len + 1) * 2;
    +155 };
    +156 
    +157 /**
    +158  * get hexadecimal string of ASN.1 V(value)
    +159  * @name getHexOfV_AtObj
    +160  * @memberOf ASN1HEX
    +161  * @function
    +162  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    +163  * @param {Number} pos string index
    +164  * @return {String} hexadecimal string of ASN.1 value.
    +165  */
    +166 ASN1HEX.getHexOfV_AtObj = function(s, pos) {
    +167     var pos1 = ASN1HEX.getStartPosOfV_AtObj(s, pos);
    +168     var len = ASN1HEX.getIntOfL_AtObj(s, pos);
    +169     return s.substring(pos1, pos1 + len * 2);
    +170 };
    +171 
    +172 /**
    +173  * get hexadecimal string of ASN.1 TLV at<br/>
    +174  * @name getHexOfTLV_AtObj
    +175  * @memberOf ASN1HEX
    +176  * @function
    +177  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    +178  * @param {Number} pos string index
    +179  * @return {String} hexadecimal string of ASN.1 TLV.
    +180  * @since asn1hex 1.1
    +181  */
    +182 ASN1HEX.getHexOfTLV_AtObj = function(s, pos) {
    +183     var hT = s.substr(pos, 2);
    +184     var hL = ASN1HEX.getHexOfL_AtObj(s, pos);
    +185     var hV = ASN1HEX.getHexOfV_AtObj(s, pos);
    +186     return hT + hL + hV;
    +187 };
    +188 
    +189 // ========== sibling methods ================================
    +190 /**
    +191  * get next sibling starting index for ASN.1 object string<br/>
    +192  * @name getPosOfNextSibling_AtObj
    +193  * @memberOf ASN1HEX
    +194  * @function
    +195  * @param {String} s hexadecimal string of ASN.1 DER encoded data
    +196  * @param {Number} pos string index
    +197  * @return next sibling starting index for ASN.1 object string
    +198  */
    +199 ASN1HEX.getPosOfNextSibling_AtObj = function(s, pos) {
    +200     var pos1 = ASN1HEX.getStartPosOfV_AtObj(s, pos);
    +201     var len = ASN1HEX.getIntOfL_AtObj(s, pos);
    +202     return pos1 + len * 2;
    +203 };
    +204 
    +205 // ========== children methods ===============================
    +206 /**
    +207  * get array of string indexes of child ASN.1 objects<br/>
    +208  * @name getPosArrayOfChildren_AtObj
    +209  * @memberOf ASN1HEX
    +210  * @function
    +211  * @param {String} h hexadecimal string of ASN.1 DER encoded data
    +212  * @param {Number} pos start string index of ASN.1 object
    +213  * @return {Array of Number} array of indexes for childen of ASN.1 objects
    +214  * @description
    +215  * This method returns array of integers for a concatination of ASN.1 objects
    +216  * in a ASN.1 value. As for BITSTRING, one byte of unusedbits is skipped.
    +217  * As for other ASN.1 simple types such as INTEGER, OCTET STRING or PRINTABLE STRING,
    +218  * it returns a array of a string index of its ASN.1 value.<br/>
    +219  * NOTE: Since asn1hex 1.1.7 of jsrsasign 6.1.2, Encapsulated BitString is supported.
    +220  * @example
    +221  * ASN1HEX.getPosArrayOfChildren_AtObj("0203012345", 0) ⇒ [4] // INTEGER 012345
    +222  * ASN1HEX.getPosArrayOfChildren_AtObj("1303616161", 0) ⇒ [4] // PrintableString aaa
    +223  * ASN1HEX.getPosArrayOfChildren_AtObj("030300ffff", 0) ⇒ [6] // BITSTRING ffff (unusedbits=00a)
    +224  * ASN1HEX.getPosArrayOfChildren_AtObj("3006020104020105", 0) ⇒ [4, 10] // SEQUENCE(INT4,INT5)
    +225  */
    +226 ASN1HEX.getPosArrayOfChildren_AtObj = function(h, pos) {
    +227     var a = new Array();
    +228     var p0 = ASN1HEX.getStartPosOfV_AtObj(h, pos);
    +229     if (h.substr(pos, 2) == "03") {
    +230 	a.push(p0 + 2); // BITSTRING value without unusedbits
    +231     } else {
    +232 	a.push(p0);
    +233     }
    +234 
    +235     var len = ASN1HEX.getIntOfL_AtObj(h, pos);
    +236     var p = p0;
    +237     var k = 0;
    +238     while (1) {
    +239         var pNext = ASN1HEX.getPosOfNextSibling_AtObj(h, p);
    +240         if (pNext == null || (pNext - p0  >= (len * 2))) break;
    +241         if (k >= 200) break;
    +242             
    +243         a.push(pNext);
    +244         p = pNext;
    +245             
    +246         k++;
    +247     }
    +248     
    +249     return a;
    +250 };
    +251 
    +252 /**
    +253  * get string index of nth child object of ASN.1 object refered by h, idx<br/>
    +254  * @name getNthChildIndex_AtObj
    +255  * @memberOf ASN1HEX
    +256  * @function
    +257  * @param {String} h hexadecimal string of ASN.1 DER encoded data
    +258  * @param {Number} idx start string index of ASN.1 object
    +259  * @param {Number} nth for child
    +260  * @return {Number} string index of nth child.
    +261  * @since 1.1
    +262  */
    +263 ASN1HEX.getNthChildIndex_AtObj = function(h, idx, nth) {
    +264     var a = ASN1HEX.getPosArrayOfChildren_AtObj(h, idx);
    +265     return a[nth];
    +266 };
    +267 
    +268 // ========== decendant methods ==============================
    +269 /**
    +270  * get string index of nth child object of ASN.1 object refered by h, idx<br/>
    +271  * @name getDecendantIndexByNthList
    +272  * @memberOf ASN1HEX
    +273  * @function
    +274  * @param {String} h hexadecimal string of ASN.1 DER encoded data
    +275  * @param {Number} currentIndex start string index of ASN.1 object
    +276  * @param {Array of Number} nthList array list of nth
    +277  * @return {Number} string index refered by nthList
    +278  * @since 1.1
    +279  * @example
    +280  * The "nthList" is a index list of structured ASN.1 object
    +281  * reference. Here is a sample structure and "nthList"s which
    +282  * refers each objects.
    +283  *
    +284  * SQUENCE               - 
    +285  *   SEQUENCE            - [0]
    +286  *     IA5STRING 000     - [0, 0]
    +287  *     UTF8STRING 001    - [0, 1]
    +288  *   SET                 - [1]
    +289  *     IA5STRING 010     - [1, 0]
    +290  *     UTF8STRING 011    - [1, 1]
    +291  */
    +292 ASN1HEX.getDecendantIndexByNthList = function(h, currentIndex, nthList) {
    +293     if (nthList.length == 0) {
    +294         return currentIndex;
    +295     }
    +296     var firstNth = nthList.shift();
    +297     var a = ASN1HEX.getPosArrayOfChildren_AtObj(h, currentIndex);
    +298     return ASN1HEX.getDecendantIndexByNthList(h, a[firstNth], nthList);
    +299 };
    +300 
    +301 /**
    +302  * get hexadecimal string of ASN.1 TLV refered by current index and nth index list.
    +303  * @name getDecendantHexTLVByNthList
    +304  * @memberOf ASN1HEX
    +305  * @function
    +306  * @param {String} h hexadecimal string of ASN.1 DER encoded data
    +307  * @param {Number} currentIndex start string index of ASN.1 object
    +308  * @param {Array of Number} nthList array list of nth
    +309  * @return {Number} hexadecimal string of ASN.1 TLV refered by nthList
    +310  * @since 1.1
    +311  */
    +312 ASN1HEX.getDecendantHexTLVByNthList = function(h, currentIndex, nthList) {
    +313     var idx = ASN1HEX.getDecendantIndexByNthList(h, currentIndex, nthList);
    +314     return ASN1HEX.getHexOfTLV_AtObj(h, idx);
    +315 };
    +316 
    +317 /**
    +318  * get hexadecimal string of ASN.1 V refered by current index and nth index list.
    +319  * @name getDecendantHexVByNthList
    +320  * @memberOf ASN1HEX
    +321  * @function
    +322  * @param {String} h hexadecimal string of ASN.1 DER encoded data
    +323  * @param {Number} currentIndex start string index of ASN.1 object
    +324  * @param {Array of Number} nthList array list of nth
    +325  * @return {Number} hexadecimal string of ASN.1 V refered by nthList
    +326  * @since 1.1
    +327  */
    +328 ASN1HEX.getDecendantHexVByNthList = function(h, currentIndex, nthList) {
    +329     var idx = ASN1HEX.getDecendantIndexByNthList(h, currentIndex, nthList);
    +330     return ASN1HEX.getHexOfV_AtObj(h, idx);
    +331 };
    +332 
    +333 /**
    +334  * get ASN.1 value by nthList<br/>
    +335  * @name getVbyList
    +336  * @memberOf ASN1HEX
    +337  * @function
    +338  * @param {String} h hexadecimal string of ASN.1 structure
    +339  * @param {Integer} currentIndex string index to start searching in hexadecimal string "h"
    +340  * @param {Array} nthList array of nth list index
    +341  * @param {String} checkingTag (OPTIONAL) string of expected ASN.1 tag for nthList 
    +342  * @description
    +343  * This static method is to get a ASN.1 value which specified "nthList" position
    +344  * with checking expected tag "checkingTag".
    +345  * @since asn1hex 1.1.4
    +346  */
    +347 ASN1HEX.getVbyList = function(h, currentIndex, nthList, checkingTag) {
    +348     var idx = ASN1HEX.getDecendantIndexByNthList(h, currentIndex, nthList);
    +349     if (idx === undefined) {
    +350         throw "can't find nthList object";
    +351     }
    +352     if (checkingTag !== undefined) {
    +353         if (h.substr(idx, 2) != checkingTag) {
    +354             throw "checking tag doesn't match: " + 
    +355                 h.substr(idx,2) + "!=" + checkingTag;
    +356         }
    +357     }
    +358     return ASN1HEX.getHexOfV_AtObj(h, idx);
    +359 };
    +360 
    +361 /**
    +362  * get OID string from hexadecimal encoded value<br/>
    +363  * @name hextooidstr
    +364  * @memberOf ASN1HEX
    +365  * @function
    +366  * @param {String} hex hexadecmal string of ASN.1 DER encoded OID value
    +367  * @return {String} OID string (ex. '1.2.3.4.567')
    +368  * @since asn1hex 1.1.5
    +369  */
    +370 ASN1HEX.hextooidstr = function(hex) {
    +371     var zeroPadding = function(s, len) {
    +372         if (s.length >= len) return s;
    +373         return new Array(len - s.length + 1).join('0') + s;
    +374     };
     375 
    -376 /**
    -377  * get string of simple ASN.1 dump from hexadecimal ASN.1 data
    -378  * @name dump
    -379  * @memberOf ASN1HEX
    -380  * @function
    -381  * @param {String} hex hexadecmal string of ASN.1 data
    -382  * @param {Array} associative array of flags for dump (OPTION)
    -383  * @param {Number} idx string index for starting dump (OPTION)
    -384  * @param {String} indent string (OPTION)
    -385  * @return {String} string of simple ASN.1 dump
    -386  * @since jsrsasign 4.8.3 asn1hex 1.1.6
    -387  * @description
    -388  * This method will get an ASN.1 dump from
    -389  * hexadecmal string of ASN.1 DER encoded data.
    -390  * Here are features:
    -391  * <ul>
    -392  * <li>ommit long hexadecimal string</li>
    -393  * <li>dump encapsulated OCTET STRING (good for X.509v3 extensions)</li>
    -394  * <li>structured/primitive context specific tag support (i.e. [0], [3] ...)</li>
    -395  * <li>automatic decode for implicit primitive context specific tag 
    -396  * (good for X.509v3 extension value)
    -397  *   <ul>
    -398  *   <li>if hex starts '68747470'(i.e. http) it is decoded as utf8 encoded string.</li>
    -399  *   <li>if it is in 'subjectAltName' extension value and is '[2]'(dNSName) tag
    -400  *   value will be encoded as utf8 string</li>
    -401  *   <li>otherwise it shows as hexadecimal string</li>
    -402  *   </ul>
    -403  * </li>
    -404  * </ul>
    -405  * @example
    -406  * // ASN.1 INTEGER
    -407  * ASN1HEX.dump('0203012345')
    -408  * ↓
    -409  * INTEGER 012345
    -410  *
    -411  * // ASN.1 Object Identifier
    -412  * ASN1HEX.dump('06052b0e03021a')
    -413  * ↓
    -414  * ObjectIdentifier sha1 (1 3 14 3 2 26)
    -415  *
    -416  * // ASN.1 SEQUENCE
    -417  * ASN1HEX.dump('3006020101020102')
    -418  * ↓
    -419  * SEQUENCE
    -420  *   INTEGER 01
    -421  *   INTEGER 02
    -422  *
    -423  * // ASN.1 DUMP FOR X.509 CERTIFICATE
    -424  * ASN1HEX.dump(X509.pemToHex(certPEM))
    -425  * ↓
    -426  * SEQUENCE
    -427  *   SEQUENCE
    -428  *     [0]
    -429  *       INTEGER 02
    -430  *     INTEGER 0c009310d206dbe337553580118ddc87
    -431  *     SEQUENCE
    -432  *       ObjectIdentifier SHA256withRSA (1 2 840 113549 1 1 11)
    -433  *       NULL
    -434  *     SEQUENCE
    -435  *       SET
    -436  *         SEQUENCE
    -437  *           ObjectIdentifier countryName (2 5 4 6)
    -438  *           PrintableString 'US'
    -439  *             :
    -440  */
    -441 ASN1HEX.dump = function(hex, flags, idx, indent) {
    -442     var _skipLongHex = function(hex, limitNumOctet) {
    -443 	if (hex.length <= limitNumOctet * 2) {
    -444 	    return hex;
    -445 	} else {
    -446 	    var s = hex.substr(0, limitNumOctet) + 
    -447 		    "..(total " + hex.length / 2 + "bytes).." +
    -448 		    hex.substr(hex.length - limitNumOctet, limitNumOctet);
    -449 	    return s;
    -450 	};
    -451     };
    -452 
    -453     if (flags === undefined) flags = { "ommit_long_octet": 32 };
    -454     if (idx === undefined) idx = 0;
    -455     if (indent === undefined) indent = "";
    -456     var skipLongHex = flags.ommit_long_octet;
    -457 
    -458     if (hex.substr(idx, 2) == "01") {
    -459 	var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    -460 	if (v == "00") {
    -461 	    return indent + "BOOLEAN FALSE\n";
    -462 	} else {
    -463 	    return indent + "BOOLEAN TRUE\n";
    -464 	}
    -465     }
    -466     if (hex.substr(idx, 2) == "02") {
    -467 	var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    -468 	return indent + "INTEGER " + _skipLongHex(v, skipLongHex) + "\n";
    -469     }
    -470     if (hex.substr(idx, 2) == "03") {
    -471 	var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    -472 	return indent + "BITSTRING " + _skipLongHex(v, skipLongHex) + "\n";
    -473     }
    -474     if (hex.substr(idx, 2) == "04") {
    -475 	var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    -476 	if (ASN1HEX.isASN1HEX(v)) {
    -477 	    var s = indent + "OCTETSTRING, encapsulates\n";
    -478 	    s = s + ASN1HEX.dump(v, flags, 0, indent + "  ");
    -479 	    return s;
    -480 	} else {
    -481 	    return indent + "OCTETSTRING " + _skipLongHex(v, skipLongHex) + "\n";
    -482 	}
    -483     }
    -484     if (hex.substr(idx, 2) == "05") {
    -485 	return indent + "NULL\n";
    -486     }
    -487     if (hex.substr(idx, 2) == "06") {
    -488 	var hV = ASN1HEX.getHexOfV_AtObj(hex, idx);
    -489         var oidDot = KJUR.asn1.ASN1Util.oidHexToInt(hV);
    -490         var oidName = KJUR.asn1.x509.OID.oid2name(oidDot);
    -491 	var oidSpc = oidDot.replace(/\./g, ' ');
    -492         if (oidName != '') {
    -493   	    return indent + "ObjectIdentifier " + oidName + " (" + oidSpc + ")\n";
    -494 	} else {
    -495   	    return indent + "ObjectIdentifier (" + oidSpc + ")\n";
    -496 	}
    -497     }
    -498     if (hex.substr(idx, 2) == "0c") {
    -499 	return indent + "UTF8String '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n";
    +376     var a = [];
    +377 
    +378     // a[0], a[1]
    +379     var hex0 = hex.substr(0, 2);
    +380     var i0 = parseInt(hex0, 16);
    +381     a[0] = new String(Math.floor(i0 / 40));
    +382     a[1] = new String(i0 % 40);
    +383 
    +384     // a[2]..a[n]
    +385    var hex1 = hex.substr(2);
    +386     var b = [];
    +387     for (var i = 0; i < hex1.length / 2; i++) {
    +388     b.push(parseInt(hex1.substr(i * 2, 2), 16));
    +389     }
    +390     var c = [];
    +391     var cbin = "";
    +392     for (var i = 0; i < b.length; i++) {
    +393         if (b[i] & 0x80) {
    +394             cbin = cbin + zeroPadding((b[i] & 0x7f).toString(2), 7);
    +395         } else {
    +396             cbin = cbin + zeroPadding((b[i] & 0x7f).toString(2), 7);
    +397             c.push(new String(parseInt(cbin, 2)));
    +398             cbin = "";
    +399         }
    +400     }
    +401 
    +402     var s = a.join(".");
    +403     if (c.length > 0) s = s + "." + c.join(".");
    +404     return s;
    +405 };
    +406 
    +407 /**
    +408  * get string of simple ASN.1 dump from hexadecimal ASN.1 data
    +409  * @name dump
    +410  * @memberOf ASN1HEX
    +411  * @function
    +412  * @param {String} hex hexadecmal string of ASN.1 data
    +413  * @param {Array} associative array of flags for dump (OPTION)
    +414  * @param {Number} idx string index for starting dump (OPTION)
    +415  * @param {String} indent string (OPTION)
    +416  * @return {String} string of simple ASN.1 dump
    +417  * @since jsrsasign 4.8.3 asn1hex 1.1.6
    +418  * @description
    +419  * This method will get an ASN.1 dump from
    +420  * hexadecmal string of ASN.1 DER encoded data.
    +421  * Here are features:
    +422  * <ul>
    +423  * <li>ommit long hexadecimal string</li>
    +424  * <li>dump encapsulated OCTET STRING (good for X.509v3 extensions)</li>
    +425  * <li>structured/primitive context specific tag support (i.e. [0], [3] ...)</li>
    +426  * <li>automatic decode for implicit primitive context specific tag 
    +427  * (good for X.509v3 extension value)
    +428  *   <ul>
    +429  *   <li>if hex starts '68747470'(i.e. http) it is decoded as utf8 encoded string.</li>
    +430  *   <li>if it is in 'subjectAltName' extension value and is '[2]'(dNSName) tag
    +431  *   value will be encoded as utf8 string</li>
    +432  *   <li>otherwise it shows as hexadecimal string</li>
    +433  *   </ul>
    +434  * </li>
    +435  * </ul>
    +436  * @example
    +437  * // ASN.1 INTEGER
    +438  * ASN1HEX.dump('0203012345')
    +439  * ↓
    +440  * INTEGER 012345
    +441  *
    +442  * // ASN.1 Object Identifier
    +443  * ASN1HEX.dump('06052b0e03021a')
    +444  * ↓
    +445  * ObjectIdentifier sha1 (1 3 14 3 2 26)
    +446  *
    +447  * // ASN.1 SEQUENCE
    +448  * ASN1HEX.dump('3006020101020102')
    +449  * ↓
    +450  * SEQUENCE
    +451  *   INTEGER 01
    +452  *   INTEGER 02
    +453  *
    +454  * // ASN.1 DUMP FOR X.509 CERTIFICATE
    +455  * ASN1HEX.dump(X509.pemToHex(certPEM))
    +456  * ↓
    +457  * SEQUENCE
    +458  *   SEQUENCE
    +459  *     [0]
    +460  *       INTEGER 02
    +461  *     INTEGER 0c009310d206dbe337553580118ddc87
    +462  *     SEQUENCE
    +463  *       ObjectIdentifier SHA256withRSA (1 2 840 113549 1 1 11)
    +464  *       NULL
    +465  *     SEQUENCE
    +466  *       SET
    +467  *         SEQUENCE
    +468  *           ObjectIdentifier countryName (2 5 4 6)
    +469  *           PrintableString 'US'
    +470  *             :
    +471  */
    +472 ASN1HEX.dump = function(hex, flags, idx, indent) {
    +473     var _skipLongHex = function(hex, limitNumOctet) {
    +474 	if (hex.length <= limitNumOctet * 2) {
    +475 	    return hex;
    +476 	} else {
    +477 	    var s = hex.substr(0, limitNumOctet) + 
    +478 		    "..(total " + hex.length / 2 + "bytes).." +
    +479 		    hex.substr(hex.length - limitNumOctet, limitNumOctet);
    +480 	    return s;
    +481 	};
    +482     };
    +483 
    +484     if (flags === undefined) flags = { "ommit_long_octet": 32 };
    +485     if (idx === undefined) idx = 0;
    +486     if (indent === undefined) indent = "";
    +487     var skipLongHex = flags.ommit_long_octet;
    +488 
    +489     if (hex.substr(idx, 2) == "01") {
    +490 	var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    +491 	if (v == "00") {
    +492 	    return indent + "BOOLEAN FALSE\n";
    +493 	} else {
    +494 	    return indent + "BOOLEAN TRUE\n";
    +495 	}
    +496     }
    +497     if (hex.substr(idx, 2) == "02") {
    +498 	var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    +499 	return indent + "INTEGER " + _skipLongHex(v, skipLongHex) + "\n";
     500     }
    -501     if (hex.substr(idx, 2) == "13") {
    -502 	return indent + "PrintableString '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n";
    -503     }
    -504     if (hex.substr(idx, 2) == "14") {
    -505 	return indent + "TeletexString '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n";
    -506     }
    -507     if (hex.substr(idx, 2) == "16") {
    -508 	return indent + "IA5String '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n";
    -509     }
    -510     if (hex.substr(idx, 2) == "17") {
    -511 	return indent + "UTCTime " + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "\n";
    -512     }
    -513     if (hex.substr(idx, 2) == "18") {
    -514 	return indent + "GeneralizedTime " + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "\n";
    -515     }
    -516     if (hex.substr(idx, 2) == "30") {
    -517 	if (hex.substr(idx, 4) == "3000") {
    -518 	    return indent + "SEQUENCE {}\n";
    -519 	}
    -520 
    -521 	var s = indent + "SEQUENCE\n";
    -522 	var aIdx = ASN1HEX.getPosArrayOfChildren_AtObj(hex, idx);
    -523 
    -524 	var flagsTemp = flags;
    -525 	
    -526 	if ((aIdx.length == 2 || aIdx.length == 3) &&
    -527 	    hex.substr(aIdx[0], 2) == "06" &&
    -528 	    hex.substr(aIdx[aIdx.length - 1], 2) == "04") { // supposed X.509v3 extension
    -529 	    var oidHex = ASN1HEX.getHexOfV_AtObj(hex, aIdx[0]);
    -530 	    var oidDot = KJUR.asn1.ASN1Util.oidHexToInt(oidHex);
    -531 	    var oidName = KJUR.asn1.x509.OID.oid2name(oidDot);
    -532 
    -533 	    var flagsClone = JSON.parse(JSON.stringify(flags));
    -534 	    flagsClone.x509ExtName = oidName;
    -535 	    flagsTemp = flagsClone;
    -536 	}
    -537 	
    -538 	for (var i = 0; i < aIdx.length; i++) {
    -539 	    s = s + ASN1HEX.dump(hex, flagsTemp, aIdx[i], indent + "  ");
    -540 	}
    -541 	return s;
    -542     }
    -543     if (hex.substr(idx, 2) == "31") {
    -544 	var s = indent + "SET\n";
    -545 	var aIdx = ASN1HEX.getPosArrayOfChildren_AtObj(hex, idx);
    -546 	for (var i = 0; i < aIdx.length; i++) {
    -547 	    s = s + ASN1HEX.dump(hex, flags, aIdx[i], indent + "  ");
    -548 	}
    -549 	return s;
    -550     }
    -551     var tag = parseInt(hex.substr(idx, 2), 16);
    -552     if ((tag & 128) != 0) { // context specific 
    -553 	var tagNumber = tag & 31;
    -554 	if ((tag & 32) != 0) { // structured tag
    -555 	    var s = indent + "[" + tagNumber + "]\n";
    -556 	    var aIdx = ASN1HEX.getPosArrayOfChildren_AtObj(hex, idx);
    -557 	    for (var i = 0; i < aIdx.length; i++) {
    -558 		s = s + ASN1HEX.dump(hex, flags, aIdx[i], indent + "  ");
    -559 	    }
    -560 	    return s;
    -561 	} else { // primitive tag
    -562 	    var v = ASN1HEX.getHexOfV_AtObj(hex, idx);
    -563 	    if (v.substr(0, 8) == "68747470") { // http
    -564 		v = hextoutf8(v);
    -565 	    }
    -566 	    if (flags.x509ExtName === "subjectAltName" &&
    -567 		tagNumber == 2) {
    -568 		v = hextoutf8(v);
    -569 	    }
    -570 	    
    -571 	    var s = indent + "[" + tagNumber + "] " + v + "\n";
    -572 	    return s;
    -573 	}
    -574     }
    -575     return indent + "UNKNOWN(" + hex.substr(idx, 2) + ") " + ASN1HEX.getHexOfV_AtObj(hex, idx) + "\n";
    -576 };
    -577 
    -578 /**
    -579  * check wheather the string is ASN.1 hexadecimal string or not
    -580  * @name isASN1HEX
    -581  * @memberOf ASN1HEX
    -582  * @function
    -583  * @param {String} hex string to check whether it is hexadecmal string for ASN.1 DER or not
    -584  * @return {Boolean} true if it is hexadecimal string of ASN.1 data otherwise false
    -585  * @since jsrsasign 4.8.3 asn1hex 1.1.6
    -586  * @description
    -587  * This method checks wheather the argument 'hex' is a hexadecimal string of
    -588  * ASN.1 data or not.
    -589  * @example
    -590  * ASN1HEX.isASN1HEX('0203012345') → true // PROPER ASN.1 INTEGER
    -591  * ASN1HEX.isASN1HEX('0203012345ff') → false // TOO LONG VALUE
    -592  * ASN1HEX.isASN1HEX('02030123') → false // TOO SHORT VALUE
    -593  * ASN1HEX.isASN1HEX('fa3bcd') → false // WRONG FOR ASN.1
    -594  */
    -595 ASN1HEX.isASN1HEX = function(hex) {
    -596     if (hex.length % 2 == 1) return false;
    -597 
    -598     var intL = ASN1HEX.getIntOfL_AtObj(hex, 0);
    -599     var tV = hex.substr(0, 2);
    -600     var lV = ASN1HEX.getHexOfL_AtObj(hex, 0);
    -601     var hVLength = hex.length - tV.length - lV.length;
    -602     if (hVLength == intL * 2) return true;
    -603 
    -604     return false;
    -605 };
    -606 
    \ No newline at end of file +501
    if (hex.substr(idx, 2) == "03") { +502 var v = ASN1HEX.getHexOfV_AtObj(hex, idx); +503 return indent + "BITSTRING " + _skipLongHex(v, skipLongHex) + "\n"; +504 } +505 if (hex.substr(idx, 2) == "04") { +506 var v = ASN1HEX.getHexOfV_AtObj(hex, idx); +507 if (ASN1HEX.isASN1HEX(v)) { +508 var s = indent + "OCTETSTRING, encapsulates\n"; +509 s = s + ASN1HEX.dump(v, flags, 0, indent + " "); +510 return s; +511 } else { +512 return indent + "OCTETSTRING " + _skipLongHex(v, skipLongHex) + "\n"; +513 } +514 } +515 if (hex.substr(idx, 2) == "05") { +516 return indent + "NULL\n"; +517 } +518 if (hex.substr(idx, 2) == "06") { +519 var hV = ASN1HEX.getHexOfV_AtObj(hex, idx); +520 var oidDot = KJUR.asn1.ASN1Util.oidHexToInt(hV); +521 var oidName = KJUR.asn1.x509.OID.oid2name(oidDot); +522 var oidSpc = oidDot.replace(/\./g, ' '); +523 if (oidName != '') { +524 return indent + "ObjectIdentifier " + oidName + " (" + oidSpc + ")\n"; +525 } else { +526 return indent + "ObjectIdentifier (" + oidSpc + ")\n"; +527 } +528 } +529 if (hex.substr(idx, 2) == "0c") { +530 return indent + "UTF8String '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n"; +531 } +532 if (hex.substr(idx, 2) == "13") { +533 return indent + "PrintableString '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n"; +534 } +535 if (hex.substr(idx, 2) == "14") { +536 return indent + "TeletexString '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n"; +537 } +538 if (hex.substr(idx, 2) == "16") { +539 return indent + "IA5String '" + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "'\n"; +540 } +541 if (hex.substr(idx, 2) == "17") { +542 return indent + "UTCTime " + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "\n"; +543 } +544 if (hex.substr(idx, 2) == "18") { +545 return indent + "GeneralizedTime " + hextoutf8(ASN1HEX.getHexOfV_AtObj(hex, idx)) + "\n"; +546 } +547 if (hex.substr(idx, 2) == "30") { +548 if (hex.substr(idx, 4) == "3000") { +549 return indent + "SEQUENCE {}\n"; +550 } +551 +552 var s = indent + "SEQUENCE\n"; +553 var aIdx = ASN1HEX.getPosArrayOfChildren_AtObj(hex, idx); +554 +555 var flagsTemp = flags; +556 +557 if ((aIdx.length == 2 || aIdx.length == 3) && +558 hex.substr(aIdx[0], 2) == "06" && +559 hex.substr(aIdx[aIdx.length - 1], 2) == "04") { // supposed X.509v3 extension +560 var oidHex = ASN1HEX.getHexOfV_AtObj(hex, aIdx[0]); +561 var oidDot = KJUR.asn1.ASN1Util.oidHexToInt(oidHex); +562 var oidName = KJUR.asn1.x509.OID.oid2name(oidDot); +563 +564 var flagsClone = JSON.parse(JSON.stringify(flags)); +565 flagsClone.x509ExtName = oidName; +566 flagsTemp = flagsClone; +567 } +568 +569 for (var i = 0; i < aIdx.length; i++) { +570 s = s + ASN1HEX.dump(hex, flagsTemp, aIdx[i], indent + " "); +571 } +572 return s; +573 } +574 if (hex.substr(idx, 2) == "31") { +575 var s = indent + "SET\n"; +576 var aIdx = ASN1HEX.getPosArrayOfChildren_AtObj(hex, idx); +577 for (var i = 0; i < aIdx.length; i++) { +578 s = s + ASN1HEX.dump(hex, flags, aIdx[i], indent + " "); +579 } +580 return s; +581 } +582 var tag = parseInt(hex.substr(idx, 2), 16); +583 if ((tag & 128) != 0) { // context specific +584 var tagNumber = tag & 31; +585 if ((tag & 32) != 0) { // structured tag +586 var s = indent + "[" + tagNumber + "]\n"; +587 var aIdx = ASN1HEX.getPosArrayOfChildren_AtObj(hex, idx); +588 for (var i = 0; i < aIdx.length; i++) { +589 s = s + ASN1HEX.dump(hex, flags, aIdx[i], indent + " "); +590 } +591 return s; +592 } else { // primitive tag +593 var v = ASN1HEX.getHexOfV_AtObj(hex, idx); +594 if (v.substr(0, 8) == "68747470") { // http +595 v = hextoutf8(v); +596 } +597 if (flags.x509ExtName === "subjectAltName" && +598 tagNumber == 2) { +599 v = hextoutf8(v); +600 } +601 +602 var s = indent + "[" + tagNumber + "] " + v + "\n"; +603 return s; +604 } +605 } +606 return indent + "UNKNOWN(" + hex.substr(idx, 2) + ") " + ASN1HEX.getHexOfV_AtObj(hex, idx) + "\n"; +607 }; +608 +609 /** +610 * check wheather the string is ASN.1 hexadecimal string or not +611 * @name isASN1HEX +612 * @memberOf ASN1HEX +613 * @function +614 * @param {String} hex string to check whether it is hexadecmal string for ASN.1 DER or not +615 * @return {Boolean} true if it is hexadecimal string of ASN.1 data otherwise false +616 * @since jsrsasign 4.8.3 asn1hex 1.1.6 +617 * @description +618 * This method checks wheather the argument 'hex' is a hexadecimal string of +619 * ASN.1 data or not. +620 * @example +621 * ASN1HEX.isASN1HEX('0203012345') → true // PROPER ASN.1 INTEGER +622 * ASN1HEX.isASN1HEX('0203012345ff') → false // TOO LONG VALUE +623 * ASN1HEX.isASN1HEX('02030123') → false // TOO SHORT VALUE +624 * ASN1HEX.isASN1HEX('fa3bcd') → false // WRONG FOR ASN.1 +625 */ +626 ASN1HEX.isASN1HEX = function(hex) { +627 if (hex.length % 2 == 1) return false; +628 +629 var intL = ASN1HEX.getIntOfL_AtObj(hex, 0); +630 var tV = hex.substr(0, 2); +631 var lV = ASN1HEX.getHexOfL_AtObj(hex, 0); +632 var hVLength = hex.length - tV.length - lV.length; +633 if (hVLength == intL * 2) return true; +634 +635 return false; +636 }; +637
    \ No newline at end of file diff --git a/api/symbols/src/asn1ocsp-1.0.js.html b/api/symbols/src/asn1ocsp-1.0.js.html index 59a4ddac..5f49b965 100644 --- a/api/symbols/src/asn1ocsp-1.0.js.html +++ b/api/symbols/src/asn1ocsp-1.0.js.html @@ -5,7 +5,7 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! asn1ocsp-1.0.0.js (c) 2016 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! asn1ocsp-1.0.1.js (c) 2016 Kenji Urushima | kjur.github.com/jsrsasign/license
       2  */
       3 /*
       4  * asn1ocsp.js - ASN.1 DER encoder classes for OCSP protocol
    @@ -23,7 +23,7 @@
      16  * @fileOverview
      17  * @name asn1ocsp-1.0.js
      18  * @author Kenji Urushima kenji.urushima@gmail.com
    - 19  * @version 1.0.0 (2016-Sep-22)
    + 19  * @version 1.0.1 (2016-Oct-02)
      20  * @since jsrsasign 6.1.0
      21  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      22  */
    @@ -430,4 +430,42 @@
     423     return o.getEncodedHex();
     424 };
     425 
    -426 
    \ No newline at end of file +426
    KJUR.asn1.ocsp.OCSPUtil.getOCSPResponseInfo = function(h) { +427 var result = {}; +428 try { +429 var v = ASN1HEX.getVbyList(h, 0, [0], "0a"); +430 result.responseStatus = parseInt(v, 16); +431 } catch(ex) {}; +432 if (result.responseStatus !== 0) return result; +433 +434 try { +435 // certStatus +436 var idxCertStatus = ASN1HEX.getDecendantIndexByNthList(h, 0, [1,0,1,0,0,2,0,1]); +437 if (h.substr(idxCertStatus, 2) === "80") { +438 result.certStatus = "good"; +439 } else if (h.substr(idxCertStatus, 2) === "a1") { +440 result.certStatus = "revoked"; +441 result.revocationTime = +442 hextoutf8(ASN1HEX.getDecendantHexVByNthList(h, idxCertStatus, [0])); +443 } else if (h.substr(idxCertStatus, 2) === "82") { +444 result.certStatus = "unknown"; +445 } +446 } catch (ex) {}; +447 +448 try { +449 var idxThisUpdate = ASN1HEX.getDecendantIndexByNthList(h, 0, [1,0,1,0,0,2,0,2]); +450 result.thisUpdate = hextoutf8(ASN1HEX.getHexOfV_AtObj(h, idxThisUpdate)); +451 } catch (ex) {}; +452 +453 try { +454 var idxEncapNextUpdate = ASN1HEX.getDecendantIndexByNthList(h, 0, [1,0,1,0,0,2,0,3]); +455 if (h.substr(idxEncapNextUpdate, 2) === "a0") { +456 result.nextUpdate = +457 hextoutf8(ASN1HEX.getDecendantHexVByNthList(h, idxEncapNextUpdate, [0])); +458 } +459 } catch (ex) {}; +460 +461 return result; +462 }; +463 +464
    \ No newline at end of file diff --git a/api/symbols/src/asn1x509-1.0.js.html b/api/symbols/src/asn1x509-1.0.js.html index b37c5111..65ca9c07 100644 --- a/api/symbols/src/asn1x509-1.0.js.html +++ b/api/symbols/src/asn1x509-1.0.js.html @@ -5,12 +5,12 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! asn1x509-1.0.14.js (c) 2013-2015 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! asn1x509-1.0.15.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
       2  */
       3 /*
       4  * asn1x509.js - ASN.1 DER encoder classes for X.509 certificate
       5  *
    -  6  * Copyright (c) 2013-2015 Kenji Urushima (kenji.urushima@gmail.com)
    +  6  * Copyright (c) 2013-2016 Kenji Urushima (kenji.urushima@gmail.com)
       7  *
       8  * This software is licensed under the terms of the MIT License.
       9  * http://kjur.github.com/jsrsasign/license
    @@ -23,7 +23,7 @@
      16  * @fileOverview
      17  * @name asn1x509-1.0.js
      18  * @author Kenji Urushima kenji.urushima@gmail.com
    - 19  * @version 1.0.14 (2016-May-10)
    + 19  * @version 1.0.15 (2016-Oct-08)
      20  * @since jsrsasign 2.1
      21  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      22  */
    @@ -1272,899 +1272,900 @@
     1265     var defaultDSType = "utf8";
     1266 
     1267     this.setByString = function(attrTypeAndValueStr) {
    -1268         if (attrTypeAndValueStr.match(/^([^=]+)=(.+)$/)) {
    -1269             this.setByAttrTypeAndValueStr(RegExp.$1, RegExp.$2);
    -1270         } else {
    -1271             throw "malformed attrTypeAndValueStr: " + attrTypeAndValueStr;
    -1272         }
    -1273     };
    -1274 
    -1275     this.setByAttrTypeAndValueStr = function(shortAttrType, valueStr) {
    -1276         this.typeObj = KJUR.asn1.x509.OID.atype2obj(shortAttrType);
    -1277         var dsType = defaultDSType;
    -1278         if (shortAttrType == "C") dsType = "prn";
    -1279         this.valueObj = this.getValueObj(dsType, valueStr);
    -1280     };
    -1281 
    -1282     this.getValueObj = function(dsType, valueStr) {
    -1283         if (dsType == "utf8")   return new KJUR.asn1.DERUTF8String({"str": valueStr});
    -1284         if (dsType == "prn")    return new KJUR.asn1.DERPrintableString({"str": valueStr});
    -1285         if (dsType == "tel")    return new KJUR.asn1.DERTeletexString({"str": valueStr});
    -1286         if (dsType == "ia5")    return new KJUR.asn1.DERIA5String({"str": valueStr});
    -1287         throw "unsupported directory string type: type=" + dsType + " value=" + valueStr;
    -1288     };
    -1289 
    -1290     this.getEncodedHex = function() {
    -1291         var o = new KJUR.asn1.DERSequence({"array": [this.typeObj, this.valueObj]});
    -1292         this.TLV = o.getEncodedHex();
    -1293         return this.TLV;
    -1294     };
    -1295 
    -1296     if (typeof params != "undefined") {
    -1297         if (typeof params['str'] != "undefined") {
    -1298             this.setByString(params['str']);
    -1299         }
    -1300     }
    -1301 };
    -1302 YAHOO.lang.extend(KJUR.asn1.x509.AttributeTypeAndValue, KJUR.asn1.ASN1Object);
    -1303 
    -1304 // === END   X500Name Related =================================================
    -1305 
    -1306 // === BEGIN Other ASN1 structure class  ======================================
    -1307 
    -1308 /**
    -1309  * SubjectPublicKeyInfo ASN.1 structure class
    -1310  * @name KJUR.asn1.x509.SubjectPublicKeyInfo
    -1311  * @class SubjectPublicKeyInfo ASN.1 structure class
    -1312  * @param {Object} params parameter for subject public key
    -1313  * @extends KJUR.asn1.ASN1Object
    -1314  * @description
    -1315  * <br/>
    -1316  * As for argument 'params' for constructor, you can specify one of
    -1317  * following properties:
    -1318  * <ul>
    -1319  * <li>{@link RSAKey} object</li>
    -1320  * <li>{@link KJUR.crypto.ECDSA} object</li>
    -1321  * <li>{@link KJUR.crypto.DSA} object</li>
    -1322  * <li>(DEPRECATED)rsakey - specify {@link RSAKey} object of subject public key</li>
    -1323  * <li>(DEPRECATED)rsapem - specify a string of PEM public key of RSA key</li>
    -1324  * </ul>
    -1325  * NOTE1: 'params' can be omitted.<br/>
    -1326  * NOTE2: DSA/ECDSA key object is also supported since asn1x509 1.0.6.<br/>
    -1327  * <h4>EXAMPLE</h4>
    -1328  * @example
    -1329  * var spki = new KJUR.asn1.x509.SubjectPublicKeyInfo(RSAKey_object);
    -1330  * var spki = new KJUR.asn1.x509.SubjectPublicKeyInfo(KJURcryptoECDSA_object);
    -1331  * var spki = new KJUR.asn1.x509.SubjectPublicKeyInfo(KJURcryptoDSA_object);
    -1332  */
    -1333 KJUR.asn1.x509.SubjectPublicKeyInfo = function(params) {
    -1334     KJUR.asn1.x509.SubjectPublicKeyInfo.superclass.constructor.call(this);
    -1335     var asn1AlgId = null;
    -1336     var asn1SubjPKey = null;
    -1337     var rsaKey = null;
    -1338 
    -1339     /**
    -1340      * (DEPRECATED) set RSAKey object as subject public key
    -1341      * @name setRSAKey
    -1342      * @memberOf KJUR.asn1.x509.SubjectPublicKeyInfo
    -1343      * @function
    -1344      * @param {RSAKey} rsaKey {@link RSAKey} object for RSA public key
    -1345      * @description
    -1346      * @deprecated
    -1347      * @example
    -1348      * spki.setRSAKey(rsaKey);
    -1349      */
    -1350     this.setRSAKey = function(rsaKey) {
    -1351         if (! RSAKey.prototype.isPrototypeOf(rsaKey))
    -1352             throw "argument is not RSAKey instance";
    -1353         this.rsaKey = rsaKey;
    -1354         var asn1RsaN = new KJUR.asn1.DERInteger({'bigint': rsaKey.n});
    -1355         var asn1RsaE = new KJUR.asn1.DERInteger({'int': rsaKey.e});
    -1356         var asn1RsaPub = new KJUR.asn1.DERSequence({'array': [asn1RsaN, asn1RsaE]});
    -1357         var rsaKeyHex = asn1RsaPub.getEncodedHex();
    -1358         this.asn1AlgId = new KJUR.asn1.x509.AlgorithmIdentifier({'name':'rsaEncryption'});
    -1359         this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex':'00'+rsaKeyHex});
    -1360     };
    -1361 
    -1362     /**
    -1363      * (DEPRECATED) set a PEM formatted RSA public key string as RSA public key
    -1364      * @name setRSAPEM
    -1365      * @memberOf KJUR.asn1.x509.SubjectPublicKeyInfo
    -1366      * @function
    -1367      * @param {String} rsaPubPEM PEM formatted RSA public key string
    -1368      * @deprecated
    -1369      * @description
    -1370      * @example
    -1371      * spki.setRSAPEM(rsaPubPEM);
    -1372      */
    -1373     this.setRSAPEM = function(rsaPubPEM) {
    -1374         if (rsaPubPEM.match(/-----BEGIN PUBLIC KEY-----/)) {
    -1375             var s = rsaPubPEM;
    -1376             s = s.replace(/^-----[^-]+-----/, '');
    -1377             s = s.replace(/-----[^-]+-----\s*$/, '');
    -1378             var rsaB64 = s.replace(/\s+/g, '');
    -1379             var rsaWA = CryptoJS.enc.Base64.parse(rsaB64);
    -1380             var rsaP8Hex = CryptoJS.enc.Hex.stringify(rsaWA);
    -1381             var a = _rsapem_getHexValueArrayOfChildrenFromHex(rsaP8Hex);
    -1382             var hBitStrVal = a[1];
    -1383             var rsaHex = hBitStrVal.substr(2);
    -1384             var a3 = _rsapem_getHexValueArrayOfChildrenFromHex(rsaHex);
    -1385             var rsaKey = new RSAKey();
    -1386             rsaKey.setPublic(a3[0], a3[1]);
    -1387             this.setRSAKey(rsaKey);
    -1388         } else {
    -1389             throw "key not supported";
    -1390         }
    -1391     };
    -1392 
    -1393     /*
    -1394      * @since asn1x509 1.0.7
    -1395      */
    -1396     this.getASN1Object = function() {
    -1397         if (this.asn1AlgId == null || this.asn1SubjPKey == null)
    -1398             throw "algId and/or subjPubKey not set";
    -1399         var o = new KJUR.asn1.DERSequence({'array':
    -1400                                            [this.asn1AlgId, this.asn1SubjPKey]});
    -1401         return o;
    -1402     };
    -1403 
    -1404     this.getEncodedHex = function() {
    -1405         var o = this.getASN1Object();
    -1406         this.hTLV = o.getEncodedHex();
    -1407         return this.hTLV;
    -1408     };
    -1409 
    -1410     this._setRSAKey = function(key) {
    -1411         var asn1RsaPub = KJUR.asn1.ASN1Util.newObject({
    -1412             'seq': [{'int': {'bigint': key.n}}, {'int': {'int': key.e}}]
    -1413         });
    -1414         var rsaKeyHex = asn1RsaPub.getEncodedHex();
    -1415         this.asn1AlgId = new KJUR.asn1.x509.AlgorithmIdentifier({'name':'rsaEncryption'});
    -1416         this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex':'00'+rsaKeyHex});
    -1417     };
    -1418 
    -1419     this._setEC = function(key) {
    -1420         var asn1Params = new KJUR.asn1.DERObjectIdentifier({'name': key.curveName});
    -1421         this.asn1AlgId = 
    -1422             new KJUR.asn1.x509.AlgorithmIdentifier({'name': 'ecPublicKey',
    -1423                                                     'asn1params': asn1Params});
    -1424         this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex': '00' + key.pubKeyHex});
    -1425     };
    -1426 
    -1427     this._setDSA = function(key) {
    -1428         var asn1Params = new KJUR.asn1.ASN1Util.newObject({
    -1429             'seq': [{'int': {'bigint': key.p}},
    -1430                     {'int': {'bigint': key.q}},
    -1431                     {'int': {'bigint': key.g}}]
    -1432         });
    -1433         this.asn1AlgId = 
    -1434             new KJUR.asn1.x509.AlgorithmIdentifier({'name': 'dsa',
    -1435                                                     'asn1params': asn1Params});
    -1436         var pubInt = new KJUR.asn1.DERInteger({'bigint': key.y});
    -1437         this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex': '00' + pubInt.getEncodedHex()});
    -1438     };
    -1439 
    -1440     if (typeof params != "undefined") {
    -1441         if (typeof RSAKey != 'undefined' && params instanceof RSAKey) {
    -1442             this._setRSAKey(params);
    -1443         } else if (typeof KJUR.crypto.ECDSA != 'undefined' &&
    -1444                    params instanceof KJUR.crypto.ECDSA) {
    -1445             this._setEC(params);
    -1446         } else if (typeof KJUR.crypto.DSA != 'undefined' &&
    -1447                    params instanceof KJUR.crypto.DSA) {
    -1448             this._setDSA(params);
    -1449         } else if (typeof params['rsakey'] != "undefined") {
    -1450             this.setRSAKey(params['rsakey']);
    -1451         } else if (typeof params['rsapem'] != "undefined") {
    -1452             this.setRSAPEM(params['rsapem']);
    -1453         }
    -1454     }
    -1455 };
    -1456 YAHOO.lang.extend(KJUR.asn1.x509.SubjectPublicKeyInfo, KJUR.asn1.ASN1Object);
    -1457 
    -1458 /**
    -1459  * Time ASN.1 structure class
    -1460  * @name KJUR.asn1.x509.Time
    -1461  * @class Time ASN.1 structure class
    -1462  * @param {Array} params associative array of parameters (ex. {'str': '130508235959Z'})
    -1463  * @extends KJUR.asn1.ASN1Object
    -1464  * @description
    -1465  * <br/>
    -1466  * <h4>EXAMPLES</h4>
    -1467  * @example
    -1468  * var t1 = new KJUR.asn1.x509.Time{'str': '130508235959Z'} // UTCTime by default
    -1469  * var t2 = new KJUR.asn1.x509.Time{'type': 'gen',  'str': '20130508235959Z'} // GeneralizedTime
    -1470  */
    -1471 KJUR.asn1.x509.Time = function(params) {
    -1472     KJUR.asn1.x509.Time.superclass.constructor.call(this);
    -1473     var type = null;
    -1474     var timeParams = null;
    -1475 
    -1476     this.setTimeParams = function(timeParams) {
    -1477         this.timeParams = timeParams;
    -1478     }
    -1479 
    -1480     this.getEncodedHex = function() {
    -1481         var o = null;
    -1482 
    -1483         if (this.timeParams != null) {
    -1484             if (this.type == "utc") {
    -1485                 o = new KJUR.asn1.DERUTCTime(this.timeParams);
    -1486             } else {
    -1487                 o = new KJUR.asn1.DERGeneralizedTime(this.timeParams);
    -1488             }
    -1489         } else {
    -1490             if (this.type == "utc") {
    -1491                 o = new KJUR.asn1.DERUTCTime();
    -1492             } else {
    -1493                 o = new KJUR.asn1.DERGeneralizedTime();
    -1494             }
    -1495         }
    -1496         this.TLV = o.getEncodedHex();
    -1497         return this.TLV;
    -1498     };
    -1499     
    -1500     this.type = "utc";
    -1501     if (typeof params != "undefined") {
    -1502         if (typeof params.type != "undefined") {
    -1503             this.type = params.type;
    -1504         } else {
    -1505             if (typeof params.str != "undefined") {
    -1506                 if (params.str.match(/^[0-9]{12}Z$/)) this.type = "utc";
    -1507                 if (params.str.match(/^[0-9]{14}Z$/)) this.type = "gen";
    -1508             }
    -1509         }
    -1510         this.timeParams = params;
    -1511     }
    -1512 };
    -1513 YAHOO.lang.extend(KJUR.asn1.x509.Time, KJUR.asn1.ASN1Object);
    -1514 
    -1515 /**
    -1516  * AlgorithmIdentifier ASN.1 structure class
    -1517  * @name KJUR.asn1.x509.AlgorithmIdentifier
    -1518  * @class AlgorithmIdentifier ASN.1 structure class
    -1519  * @param {Array} params associative array of parameters (ex. {'name': 'SHA1withRSA'})
    -1520  * @extends KJUR.asn1.ASN1Object
    -1521  * @description
    -1522  * @example
    -1523  * algId1 = new KJUR.asn1.x509.AlgorithmIdentifier({name: "sha1"});
    -1524  */
    -1525 KJUR.asn1.x509.AlgorithmIdentifier = function(params) {
    -1526     KJUR.asn1.x509.AlgorithmIdentifier.superclass.constructor.call(this);
    -1527     var nameAlg = null;
    -1528     var asn1Alg = null;
    -1529     var asn1Params = null;
    -1530     var paramEmpty = false;
    -1531 
    -1532     this.getEncodedHex = function() {
    -1533         if (this.nameAlg == null && this.asn1Alg == null) {
    -1534             throw "algorithm not specified";
    -1535         }
    -1536         if (this.nameAlg != null && this.asn1Alg == null) {
    -1537             this.asn1Alg = KJUR.asn1.x509.OID.name2obj(this.nameAlg);
    -1538         }
    -1539         var a = [this.asn1Alg];
    -1540         if (! this.paramEmpty) a.push(this.asn1Params);
    -1541         var o = new KJUR.asn1.DERSequence({'array': a});
    -1542         this.hTLV = o.getEncodedHex();
    -1543         return this.hTLV;
    -1544     };
    -1545 
    -1546     if (typeof params != "undefined") {
    -1547         if (typeof params['name'] != "undefined") {
    -1548             this.nameAlg = params['name'];
    -1549         }
    -1550         if (typeof params['asn1params'] != "undefined") {
    -1551             this.asn1Params = params['asn1params'];
    -1552         }
    -1553         if (typeof params['paramempty'] != "undefined") {
    -1554             this.paramEmpty = params['paramempty'];
    -1555         }
    -1556     }
    -1557     if (this.asn1Params == null) {
    -1558         this.asn1Params = new KJUR.asn1.DERNull();
    -1559     }
    -1560 };
    -1561 YAHOO.lang.extend(KJUR.asn1.x509.AlgorithmIdentifier, KJUR.asn1.ASN1Object);
    -1562 
    -1563 /**
    -1564  * GeneralName ASN.1 structure class
    -1565  * @name KJUR.asn1.x509.GeneralName
    -1566  * @class GeneralName ASN.1 structure class
    -1567  * @description
    -1568  * <br/>
    -1569  * As for argument 'params' for constructor, you can specify one of
    -1570  * following properties:
    -1571  * <ul>
    -1572  * <li>rfc822 - rfc822Name[1] (ex. user1@foo.com)</li>
    -1573  * <li>dns - dNSName[2] (ex. foo.com)</li>
    -1574  * <li>uri - uniformResourceIdentifier[6] (ex. http://foo.com/)</li>
    -1575  * <li>certissuer - directoryName[4] (PEM or hex string of cert)</li>
    -1576  * <li>certsubj - directoryName[4] (PEM or hex string of cert)</li>
    -1577  * </ul>
    -1578  * NOTE1: certissuer and certsubj is supported since asn1x509 1.0.10.
    -1579  *
    -1580  * Here is definition of the ASN.1 syntax:
    -1581  * <pre>
    -1582  * -- NOTE: under the CHOICE, it will always be explicit.
    -1583  * GeneralName ::= CHOICE {
    -1584  *         otherName                       [0]     OtherName,
    -1585  *         rfc822Name                      [1]     IA5String,
    -1586  *         dNSName                         [2]     IA5String,
    -1587  *         x400Address                     [3]     ORAddress,
    -1588  *         directoryName                   [4]     Name,
    -1589  *         ediPartyName                    [5]     EDIPartyName,
    -1590  *         uniformResourceIdentifier       [6]     IA5String,
    -1591  *         iPAddress                       [7]     OCTET STRING,
    -1592  *         registeredID                    [8]     OBJECT IDENTIFIER } 
    -1593  * </pre>
    -1594  *
    -1595  * 
    -1596  *
    -1597  * @example
    -1598  * gn = new KJUR.asn1.x509.GeneralName({rfc822:      'test@aaa.com'});
    -1599  * gn = new KJUR.asn1.x509.GeneralName({dns:         'aaa.com'});
    -1600  * gn = new KJUR.asn1.x509.GeneralName({uri:         'http://aaa.com/'});
    -1601  * gn = new KJUR.asn1.x509.GeneralName({certissuer:  certPEM});
    -1602  * gn = new KJUR.asn1.x509.GeneralName({certsubj:    certPEM});
    -1603  */
    -1604 KJUR.asn1.x509.GeneralName = function(params) {
    -1605     KJUR.asn1.x509.GeneralName.superclass.constructor.call(this);
    -1606     var asn1Obj = null;
    -1607     var type = null;
    -1608     var pTag = {rfc822: '81', dns: '82', dn: 'a4',  uri: '86'};
    -1609     this.explicit = false;
    -1610 
    -1611     this.setByParam = function(params) {
    -1612         var str = null;
    -1613         var v = null;
    -1614 
    -1615 		if (typeof params == "undefined") return;
    -1616 		
    -1617         if (typeof params.rfc822 != "undefined") {
    -1618             this.type = 'rfc822';
    -1619             v = new KJUR.asn1.DERIA5String({'str': params[this.type]});
    -1620         }
    -1621         if (typeof params.dns != "undefined") {
    -1622             this.type = 'dns';
    -1623             v = new KJUR.asn1.DERIA5String({'str': params[this.type]});
    -1624         }
    -1625         if (typeof params.uri != "undefined") {
    -1626             this.type = 'uri';
    -1627             v = new KJUR.asn1.DERIA5String({'str': params[this.type]});
    -1628         }
    -1629 		if (typeof params.certissuer != "undefined") {
    -1630 			this.type = 'dn';
    -1631 			this.explicit = true;
    -1632 			var certStr = params.certissuer;
    -1633 			var certHex = null;
    -1634 			if (certStr.match(/^[0-9A-Fa-f]+$/)) {
    -1635 				certHex == certStr;
    -1636             }
    -1637 		    if (certStr.indexOf("-----BEGIN ") != -1) {
    -1638 				certHex = X509.pemToHex(certStr);
    -1639 			}
    -1640 		    if (certHex == null) throw "certissuer param not cert";
    -1641 			var x = new X509();
    -1642 			x.hex = certHex;
    -1643 			var dnHex = x.getIssuerHex();
    -1644 			v = new KJUR.asn1.ASN1Object();
    -1645 			v.hTLV = dnHex;
    -1646 		}
    -1647 		if (typeof params.certsubj != "undefined") {
    -1648 			this.type = 'dn';
    -1649 			this.explicit = true;
    -1650 			var certStr = params.certsubj;
    -1651 			var certHex = null;
    -1652 			if (certStr.match(/^[0-9A-Fa-f]+$/)) {
    -1653 				certHex == certStr;
    -1654             }
    -1655 		    if (certStr.indexOf("-----BEGIN ") != -1) {
    -1656 				certHex = X509.pemToHex(certStr);
    -1657 			}
    -1658 		    if (certHex == null) throw "certsubj param not cert";
    -1659 			var x = new X509();
    -1660 			x.hex = certHex;
    -1661 			var dnHex = x.getSubjectHex();
    -1662 			v = new KJUR.asn1.ASN1Object();
    -1663 			v.hTLV = dnHex;
    -1664 		}
    -1665 
    -1666         if (this.type == null)
    -1667             throw "unsupported type in params=" + params;
    -1668         this.asn1Obj = new KJUR.asn1.DERTaggedObject({'explicit': this.explicit,
    -1669                                                       'tag': pTag[this.type],
    -1670                                                       'obj': v});
    -1671     };
    -1672 
    -1673     this.getEncodedHex = function() {
    -1674         return this.asn1Obj.getEncodedHex();
    -1675     }
    -1676 
    -1677     if (typeof params != "undefined") {
    -1678         this.setByParam(params);
    -1679     }
    -1680 
    -1681 };
    -1682 YAHOO.lang.extend(KJUR.asn1.x509.GeneralName, KJUR.asn1.ASN1Object);
    -1683 
    -1684 /**
    -1685  * GeneralNames ASN.1 structure class
    -1686  * @name KJUR.asn1.x509.GeneralNames
    -1687  * @class GeneralNames ASN.1 structure class
    -1688  * @description
    -1689  * <br/>
    -1690  * <h4>EXAMPLE AND ASN.1 SYNTAX</h4>
    -1691  * @example
    -1692  * var gns = new KJUR.asn1.x509.GeneralNames([{'uri': 'http://aaa.com/'}, {'uri': 'http://bbb.com/'}]); 
    -1693  *
    -1694  * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
    -1695  */
    -1696 KJUR.asn1.x509.GeneralNames = function(paramsArray) {
    -1697     KJUR.asn1.x509.GeneralNames.superclass.constructor.call(this);
    -1698     var asn1Array = null;
    -1699 
    -1700     /**
    -1701      * set a array of {@link KJUR.asn1.x509.GeneralName} parameters
    -1702      * @name setByParamArray
    -1703      * @memberOf KJUR.asn1.x509.GeneralNames
    -1704      * @function
    -1705      * @param {Array} paramsArray Array of {@link KJUR.asn1.x509.GeneralNames}
    -1706      * @description
    -1707      * <br/>
    -1708      * <h4>EXAMPLES</h4>
    -1709      * @example
    -1710      * var gns = new KJUR.asn1.x509.GeneralNames();
    -1711      * gns.setByParamArray([{'uri': 'http://aaa.com/'}, {'uri': 'http://bbb.com/'}]);
    -1712      */
    -1713     this.setByParamArray = function(paramsArray) {
    -1714         for (var i = 0; i < paramsArray.length; i++) {
    -1715             var o = new KJUR.asn1.x509.GeneralName(paramsArray[i]);
    -1716             this.asn1Array.push(o);
    -1717         }
    -1718     };
    -1719 
    -1720     this.getEncodedHex = function() {
    -1721         var o = new KJUR.asn1.DERSequence({'array': this.asn1Array});
    -1722         return o.getEncodedHex();
    -1723     };
    -1724 
    -1725     this.asn1Array = new Array();
    -1726     if (typeof paramsArray != "undefined") {
    -1727         this.setByParamArray(paramsArray);
    -1728     }
    -1729 };
    -1730 YAHOO.lang.extend(KJUR.asn1.x509.GeneralNames, KJUR.asn1.ASN1Object);
    -1731 
    -1732 /**
    -1733  * DistributionPointName ASN.1 structure class
    -1734  * @name KJUR.asn1.x509.DistributionPointName
    -1735  * @class DistributionPointName ASN.1 structure class
    -1736  * @description
    -1737  * @example
    -1738  */
    -1739 KJUR.asn1.x509.DistributionPointName = function(gnOrRdn) {
    -1740     KJUR.asn1.x509.DistributionPointName.superclass.constructor.call(this);
    -1741     var asn1Obj = null;
    -1742     var type = null;
    -1743     var tag = null;
    -1744     var asn1V = null;
    -1745 
    -1746     this.getEncodedHex = function() {
    -1747         if (this.type != "full")
    -1748             throw "currently type shall be 'full': " + this.type;
    -1749         this.asn1Obj = new KJUR.asn1.DERTaggedObject({'explicit': false,
    -1750                                                       'tag': this.tag,
    -1751                                                       'obj': this.asn1V});
    -1752         this.hTLV = this.asn1Obj.getEncodedHex();
    -1753         return this.hTLV;
    -1754     };
    -1755 
    -1756     if (typeof gnOrRdn != "undefined") {
    -1757         if (KJUR.asn1.x509.GeneralNames.prototype.isPrototypeOf(gnOrRdn)) {
    -1758             this.type = "full";
    -1759             this.tag = "a0";
    -1760             this.asn1V = gnOrRdn;
    -1761         } else {
    -1762             throw "This class supports GeneralNames only as argument";
    -1763         }
    -1764     }
    -1765 };
    -1766 YAHOO.lang.extend(KJUR.asn1.x509.DistributionPointName, KJUR.asn1.ASN1Object);
    -1767 
    -1768 /**
    -1769  * DistributionPoint ASN.1 structure class
    -1770  * @name KJUR.asn1.x509.DistributionPoint
    -1771  * @class DistributionPoint ASN.1 structure class
    -1772  * @description
    -1773  * @example
    -1774  */
    -1775 KJUR.asn1.x509.DistributionPoint = function(params) {
    -1776     KJUR.asn1.x509.DistributionPoint.superclass.constructor.call(this);
    -1777     var asn1DP = null;
    -1778 
    -1779     this.getEncodedHex = function() {
    -1780         var seq = new KJUR.asn1.DERSequence();
    -1781         if (this.asn1DP != null) {
    -1782             var o1 = new KJUR.asn1.DERTaggedObject({'explicit': true,
    -1783                                                     'tag': 'a0',
    -1784                                                     'obj': this.asn1DP});
    -1785             seq.appendASN1Object(o1);
    -1786         }
    -1787         this.hTLV = seq.getEncodedHex();
    -1788         return this.hTLV;
    -1789     };
    -1790 
    -1791     if (typeof params != "undefined") {
    -1792         if (typeof params['dpobj'] != "undefined") {
    -1793             this.asn1DP = params['dpobj'];
    -1794         }
    -1795     }
    -1796 };
    -1797 YAHOO.lang.extend(KJUR.asn1.x509.DistributionPoint, KJUR.asn1.ASN1Object);
    -1798 
    -1799 /**
    -1800  * static object for OID
    -1801  * @name KJUR.asn1.x509.OID
    -1802  * @class static object for OID
    -1803  * @property {Assoc Array} atype2oidList for short attribyte type name and oid (i.e. 'C' and '2.5.4.6')
    -1804  * @property {Assoc Array} name2oidList for oid name and oid (i.e. 'keyUsage' and '2.5.29.15')
    -1805  * @property {Assoc Array} objCache for caching name and DERObjectIdentifier object 
    -1806  * @description
    -1807  * <dl>
    -1808  * <dt><b>atype2oidList</b>
    -1809  * <dd>currently supports 'C', 'O', 'OU', 'ST', 'L' and 'CN' only.
    -1810  * <dt><b>name2oidList</b>
    -1811  * <dd>currently supports 'SHA1withRSA', 'rsaEncryption' and some extension OIDs
    -1812  * </dl>
    -1813  * @example
    -1814  */
    -1815 KJUR.asn1.x509.OID = new function(params) {
    -1816     this.atype2oidList = {
    -1817         'C':    '2.5.4.6',
    -1818         'O':    '2.5.4.10',
    -1819         'OU':   '2.5.4.11',
    -1820         'ST':   '2.5.4.8',
    -1821         'L':    '2.5.4.7',
    -1822         'CN':   '2.5.4.3',
    -1823         'SN':   '2.5.4.4',
    -1824         'DN':   '2.5.4.49',
    -1825         'DC':   '0.9.2342.19200300.100.1.25',
    -1826     };
    -1827     this.name2oidList = {
    -1828         'sha1':                 '1.3.14.3.2.26',
    -1829         'sha256':               '2.16.840.1.101.3.4.2.1',
    -1830         'sha384':               '2.16.840.1.101.3.4.2.2',
    -1831         'sha512':               '2.16.840.1.101.3.4.2.3',
    -1832         'sha224':               '2.16.840.1.101.3.4.2.4',
    -1833         'md5':                  '1.2.840.113549.2.5',
    -1834         'md2':                  '1.3.14.7.2.2.1',
    -1835         'ripemd160':            '1.3.36.3.2.1',
    -1836 
    -1837         'MD2withRSA':           '1.2.840.113549.1.1.2',
    -1838         'MD4withRSA':           '1.2.840.113549.1.1.3',
    -1839         'MD5withRSA':           '1.2.840.113549.1.1.4',
    -1840         'SHA1withRSA':          '1.2.840.113549.1.1.5',
    -1841         'SHA224withRSA':        '1.2.840.113549.1.1.14',
    -1842         'SHA256withRSA':        '1.2.840.113549.1.1.11',
    -1843         'SHA384withRSA':        '1.2.840.113549.1.1.12',
    -1844         'SHA512withRSA':        '1.2.840.113549.1.1.13',
    -1845 
    -1846         'SHA1withECDSA':        '1.2.840.10045.4.1',
    -1847         'SHA224withECDSA':      '1.2.840.10045.4.3.1',
    -1848         'SHA256withECDSA':      '1.2.840.10045.4.3.2',
    -1849         'SHA384withECDSA':      '1.2.840.10045.4.3.3',
    -1850         'SHA512withECDSA':      '1.2.840.10045.4.3.4',
    -1851 
    -1852         'dsa':                  '1.2.840.10040.4.1',
    -1853         'SHA1withDSA':          '1.2.840.10040.4.3',
    -1854         'SHA224withDSA':        '2.16.840.1.101.3.4.3.1',
    -1855         'SHA256withDSA':        '2.16.840.1.101.3.4.3.2',
    -1856 
    -1857         'rsaEncryption':        '1.2.840.113549.1.1.1',
    -1858 
    -1859         'countryName':          '2.5.4.6',
    -1860         'organization':         '2.5.4.10',
    -1861         'organizationalUnit':   '2.5.4.11',
    -1862         'stateOrProvinceName':  '2.5.4.8',
    -1863         'locality':             '2.5.4.7',
    -1864         'commonName':           '2.5.4.3',
    -1865 
    -1866         'subjectKeyIdentifier': '2.5.29.14',
    -1867         'keyUsage':             '2.5.29.15',
    -1868         'subjectAltName':       '2.5.29.17',
    -1869         'basicConstraints':     '2.5.29.19',
    -1870         'nameConstraints':      '2.5.29.30',
    -1871         'cRLDistributionPoints':'2.5.29.31',
    -1872         'certificatePolicies':  '2.5.29.32',
    -1873         'authorityKeyIdentifier':'2.5.29.35',
    -1874         'policyConstraints':    '2.5.29.36',
    -1875         'extKeyUsage':          '2.5.29.37',
    -1876 	'authorityInfoAccess':  '1.3.6.1.5.5.7.1.1',
    -1877 
    -1878         'anyExtendedKeyUsage':  '2.5.29.37.0',
    -1879         'serverAuth':           '1.3.6.1.5.5.7.3.1',
    -1880         'clientAuth':           '1.3.6.1.5.5.7.3.2',
    -1881         'codeSigning':          '1.3.6.1.5.5.7.3.3',
    -1882         'emailProtection':      '1.3.6.1.5.5.7.3.4',
    -1883         'timeStamping':         '1.3.6.1.5.5.7.3.8',
    -1884         'ocspSigning':          '1.3.6.1.5.5.7.3.9',
    -1885 
    -1886         'ecPublicKey':          '1.2.840.10045.2.1',
    -1887         'secp256r1':            '1.2.840.10045.3.1.7',
    -1888         'secp256k1':            '1.3.132.0.10',
    -1889         'secp384r1':            '1.3.132.0.34',
    -1890 
    -1891         'pkcs5PBES2':           '1.2.840.113549.1.5.13',
    -1892         'pkcs5PBKDF2':          '1.2.840.113549.1.5.12',
    -1893 
    -1894         'des-EDE3-CBC':         '1.2.840.113549.3.7',
    -1895 
    -1896         'data':                 '1.2.840.113549.1.7.1', // CMS data
    -1897         'signed-data':          '1.2.840.113549.1.7.2', // CMS signed-data
    -1898         'enveloped-data':       '1.2.840.113549.1.7.3', // CMS enveloped-data
    -1899         'digested-data':        '1.2.840.113549.1.7.5', // CMS digested-data
    -1900         'encrypted-data':       '1.2.840.113549.1.7.6', // CMS encrypted-data
    -1901         'authenticated-data':   '1.2.840.113549.1.9.16.1.2', // CMS authenticated-data
    -1902         'tstinfo':              '1.2.840.113549.1.9.16.1.4', // RFC3161 TSTInfo
    -1903     };
    -1904 
    -1905     this.objCache = {};
    -1906 
    -1907     /**
    -1908      * get DERObjectIdentifier by registered OID name
    -1909      * @name name2obj
    -1910      * @memberOf KJUR.asn1.x509.OID
    -1911      * @function
    -1912      * @param {String} name OID
    -1913      * @description
    -1914      * @example
    -1915      * var asn1ObjOID = OID.name2obj('SHA1withRSA');
    -1916      */
    -1917     this.name2obj = function(name) {
    -1918         if (typeof this.objCache[name] != "undefined")
    -1919             return this.objCache[name];
    -1920         if (typeof this.name2oidList[name] == "undefined")
    -1921             throw "Name of ObjectIdentifier not defined: " + name;
    -1922         var oid = this.name2oidList[name];
    -1923         var obj = new KJUR.asn1.DERObjectIdentifier({'oid': oid});
    -1924         this.objCache[name] = obj;
    -1925         return obj;
    -1926     };
    -1927 
    -1928     /**
    -1929      * get DERObjectIdentifier by registered attribyte type name such like 'C' or 'CN'
    -1930      * @name atype2obj
    -1931      * @memberOf KJUR.asn1.x509.OID
    -1932      * @function
    -1933      * @param {String} atype short attribute type name such like 'C' or 'CN'
    -1934      * @description
    -1935      * @example
    -1936      * var asn1ObjOID = OID.atype2obj('CN');
    -1937      */
    -1938     this.atype2obj = function(atype) {
    -1939         if (typeof this.objCache[atype] != "undefined")
    -1940             return this.objCache[atype];
    -1941         if (typeof this.atype2oidList[atype] == "undefined")
    -1942             throw "AttributeType name undefined: " + atype;
    -1943         var oid = this.atype2oidList[atype];
    -1944         var obj = new KJUR.asn1.DERObjectIdentifier({'oid': oid});
    -1945         this.objCache[atype] = obj;
    -1946         return obj;
    -1947     };
    -1948 };
    -1949 
    -1950 /*
    -1951  * convert OID to name
    -1952  * @name oid2name
    -1953  * @memberOf KJUR.asn1.x509.OID
    -1954  * @function
    -1955  * @param {String} dot noted Object Identifer string (ex. 1.2.3.4)
    -1956  * @return {String} OID name
    -1957  * @description
    -1958  * This static method converts OID string to its name.
    -1959  * If OID is undefined then it returns empty string (i.e. '').
    -1960  * @example
    -1961  * name = KJUR.asn1.x509.OID.oid2name("1.3.6.1.5.5.7.1.1");
    -1962  * // name will be 'authorityInfoAccess'.
    -1963  * @since asn1x509 1.0.9
    -1964  */
    -1965 KJUR.asn1.x509.OID.oid2name = function(oid) {
    -1966     var list = KJUR.asn1.x509.OID.name2oidList;
    -1967     for (var name in list) {
    -1968         if (list[name] == oid) return name;
    -1969     }
    -1970     return '';
    -1971 };
    -1972 
    -1973 /*
    -1974  * convert name to OID
    -1975  * @name name2oid
    -1976  * @memberOf KJUR.asn1.x509.OID
    -1977  * @function
    -1978  * @param {String} OID name
    -1979  * @return {String} dot noted Object Identifer string (ex. 1.2.3.4)
    -1980  * @description
    -1981  * This static method converts from OID name to OID string.
    -1982  * If OID is undefined then it returns empty string (i.e. '').
    -1983  * @example
    -1984  * name = KJUR.asn1.x509.OID.name2oid("authorityInfoAccess");
    -1985  * // name will be '1.3.6.1.5.5.7.1.1'.
    -1986  * @since asn1x509 1.0.11
    -1987  */
    -1988 KJUR.asn1.x509.OID.name2oid = function(name) {
    -1989     var list = KJUR.asn1.x509.OID.name2oidList;
    -1990     if (list[name] === undefined) return '';
    -1991     return list[name];
    -1992 };
    -1993 
    -1994 /**
    -1995  * X.509 certificate and CRL utilities class
    -1996  * @name KJUR.asn1.x509.X509Util
    -1997  * @class X.509 certificate and CRL utilities class
    -1998  */
    -1999 KJUR.asn1.x509.X509Util = new function() {
    -2000     /**
    -2001      * get PKCS#8 PEM public key string from RSAKey object
    -2002      * @name getPKCS8PubKeyPEMfromRSAKey
    -2003      * @memberOf KJUR.asn1.x509.X509Util
    -2004      * @function
    -2005      * @param {RSAKey} rsaKey RSA public key of {@link RSAKey} object
    -2006      * @description
    -2007      * @example
    -2008      * var pem = KJUR.asn1.x509.X509Util.getPKCS8PubKeyPEMfromRSAKey(pubKey);
    -2009      */
    -2010     this.getPKCS8PubKeyPEMfromRSAKey = function(rsaKey) {
    -2011         var pem = null;
    -2012         var hN = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(rsaKey.n);
    -2013         var hE = KJUR.asn1.ASN1Util.integerToByteHex(rsaKey.e);
    -2014         var iN = new KJUR.asn1.DERInteger({hex: hN});
    -2015         var iE = new KJUR.asn1.DERInteger({hex: hE});
    -2016         var asn1PubKey = new KJUR.asn1.DERSequence({array: [iN, iE]});
    -2017         var hPubKey = asn1PubKey.getEncodedHex();
    -2018         var o1 = new KJUR.asn1.x509.AlgorithmIdentifier({name: 'rsaEncryption'});
    -2019         var o2 = new KJUR.asn1.DERBitString({hex: '00' + hPubKey});
    -2020         var seq = new KJUR.asn1.DERSequence({array: [o1, o2]});
    -2021         var hP8 = seq.getEncodedHex();
    -2022         var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(hP8, "PUBLIC KEY");
    -2023         return pem;
    -2024     };
    -2025 };
    -2026 /**
    -2027  * issue a certificate in PEM format
    -2028  * @name newCertPEM
    -2029  * @memberOf KJUR.asn1.x509.X509Util
    -2030  * @function
    -2031  * @param {Array} param parameter to issue a certificate
    -2032  * @since asn1x509 1.0.6
    -2033  * @description
    -2034  * This method can issue a certificate by a simple
    -2035  * JSON object.
    -2036  * Signature value will be provided by signing with 
    -2037  * private key using 'cakey' parameter or 
    -2038  * hexa decimal signature value by 'sighex' parameter.
    -2039  *
    -2040  * NOTE: When using DSA or ECDSA CA signing key,
    -2041  * use 'paramempty' in 'sigalg' to ommit parameter field
    -2042  * of AlgorithmIdentifer. In case of RSA, parameter
    -2043  * NULL will be specified by default.
    -2044  *
    -2045  * @example
    -2046  * var certPEM = KJUR.asn1.x509.X509Util.newCertPEM(
    -2047  * { serial: {int: 4},
    -2048  *   sigalg: {name: 'SHA1withECDSA', paramempty: true},
    -2049  *   issuer: {str: '/C=US/O=a'},
    -2050  *   notbefore: {'str': '130504235959Z'},
    -2051  *   notafter: {'str': '140504235959Z'},
    -2052  *   subject: {str: '/C=US/O=b'},
    -2053  *   sbjpubkey: pubKeyPEM,
    -2054  *   ext: [
    -2055  *     {basicConstraints: {cA: true, critical: true}},
    -2056  *     {keyUsage: {bin: '11'}},
    -2057  *   ],
    -2058  *   cakey: [prvkey, pass]}
    -2059  * );
    -2060  * // -- or --
    -2061  * var certPEM = KJUR.asn1.x509.X509Util.newCertPEM(
    -2062  * { serial: {int: 1},
    -2063  *   sigalg: {name: 'SHA1withRSA', paramempty: true},
    -2064  *   issuer: {str: '/C=US/O=T1'},
    -2065  *   notbefore: {'str': '130504235959Z'},
    -2066  *   notafter: {'str': '140504235959Z'},
    -2067  *   subject: {str: '/C=US/O=T1'},
    -2068  *   sbjpubkey: pubKeyObj,
    -2069  *   sighex: '0102030405..'}
    -2070  * );
    -2071  * // for the issuer and subject field, another
    -2072  * // representation is also available
    -2073  * var certPEM = KJUR.asn1.x509.X509Util.newCertPEM(
    -2074  * { serial: {int: 1},
    -2075  *   sigalg: {name: 'SHA1withRSA', paramempty: true},
    -2076  *   issuer: {C: "US", O: "T1"},
    -2077  *   notbefore: {'str': '130504235959Z'},
    -2078  *   notafter: {'str': '140504235959Z'},
    -2079  *   subject: {C: "US", O: "T1", CN: "http://example.com/"},
    -2080  *   sbjpubkey: pubKeyObj,
    -2081  *   sighex: '0102030405..'}
    -2082  * );
    -2083  */
    -2084 KJUR.asn1.x509.X509Util.newCertPEM = function(param) {
    -2085     var ns1 = KJUR.asn1.x509;
    -2086     var o = new ns1.TBSCertificate();
    -2087 
    -2088     if (param.serial !== undefined)
    -2089         o.setSerialNumberByParam(param.serial);
    -2090     else
    -2091         throw "serial number undefined.";
    -2092 
    -2093     if (typeof param.sigalg.name == 'string')
    -2094         o.setSignatureAlgByParam(param.sigalg);
    -2095     else 
    -2096         throw "unproper signature algorithm name";
    -2097 
    -2098     if (param.issuer !== undefined)
    -2099         o.setIssuerByParam(param.issuer);
    -2100     else
    -2101         throw "issuer name undefined.";
    -2102     
    -2103     if (param.notbefore !== undefined)
    -2104         o.setNotBeforeByParam(param.notbefore);
    -2105     else
    -2106         throw "notbefore undefined.";
    -2107 
    -2108     if (param.notafter !== undefined)
    -2109         o.setNotAfterByParam(param.notafter);
    -2110     else
    -2111         throw "notafter undefined.";
    -2112 
    -2113     if (param.subject !== undefined)
    -2114         o.setSubjectByParam(param.subject);
    -2115     else
    -2116         throw "subject name undefined.";
    -2117 
    -2118     if (param.sbjpubkey !== undefined)
    -2119         o.setSubjectPublicKeyByGetKey(param.sbjpubkey);
    -2120     else
    -2121         throw "subject public key undefined.";
    -2122 
    -2123     if (param.ext !== undefined && param.ext.length !== undefined) {
    -2124         for (var i = 0; i < param.ext.length; i++) {
    -2125             for (key in param.ext[i]) {
    -2126                 o.appendExtensionByName(key, param.ext[i][key]);
    -2127             }
    -2128         }
    -2129     }
    -2130 
    -2131     // set signature
    -2132     if (param.cakey === undefined && param.sighex === undefined)
    -2133         throw "param cakey and sighex undefined.";
    -2134 
    -2135     var caKey = null;
    -2136     var cert = null;
    -2137 
    -2138     if (param.cakey) {
    -2139         caKey = KEYUTIL.getKey.apply(null, param.cakey);
    -2140         cert = new ns1.Certificate({'tbscertobj': o, 'prvkeyobj': caKey});
    -2141         cert.sign();
    -2142     }
    -2143 
    -2144     if (param.sighex) {
    -2145         cert = new ns1.Certificate({'tbscertobj': o});
    -2146         cert.setSignatureHex(param.sighex);
    -2147     }
    -2148 
    -2149     return cert.getPEMString();
    -2150 };
    -2151 
    -2152 /*
    -2153   org.bouncycastle.asn1.x500
    -2154   AttributeTypeAndValue
    -2155   DirectoryString
    -2156   RDN
    -2157   X500Name
    -2158   X500NameBuilder
    -2159 
    -2160   org.bouncycastleasn1.x509
    -2161   TBSCertificate
    -2162 */
    -2163 
    \ No newline at end of file +1268
    var matchResult = attrTypeAndValueStr.match(/^([^=]+)=(.+)$/); +1269 if (matchResult) { +1270 this.setByAttrTypeAndValueStr(matchResult[1], matchResult[2]); +1271 } else { +1272 throw "malformed attrTypeAndValueStr: " + attrTypeAndValueStr; +1273 } +1274 }; +1275 +1276 this.setByAttrTypeAndValueStr = function(shortAttrType, valueStr) { +1277 this.typeObj = KJUR.asn1.x509.OID.atype2obj(shortAttrType); +1278 var dsType = defaultDSType; +1279 if (shortAttrType == "C") dsType = "prn"; +1280 this.valueObj = this.getValueObj(dsType, valueStr); +1281 }; +1282 +1283 this.getValueObj = function(dsType, valueStr) { +1284 if (dsType == "utf8") return new KJUR.asn1.DERUTF8String({"str": valueStr}); +1285 if (dsType == "prn") return new KJUR.asn1.DERPrintableString({"str": valueStr}); +1286 if (dsType == "tel") return new KJUR.asn1.DERTeletexString({"str": valueStr}); +1287 if (dsType == "ia5") return new KJUR.asn1.DERIA5String({"str": valueStr}); +1288 throw "unsupported directory string type: type=" + dsType + " value=" + valueStr; +1289 }; +1290 +1291 this.getEncodedHex = function() { +1292 var o = new KJUR.asn1.DERSequence({"array": [this.typeObj, this.valueObj]}); +1293 this.TLV = o.getEncodedHex(); +1294 return this.TLV; +1295 }; +1296 +1297 if (typeof params != "undefined") { +1298 if (typeof params['str'] != "undefined") { +1299 this.setByString(params['str']); +1300 } +1301 } +1302 }; +1303 YAHOO.lang.extend(KJUR.asn1.x509.AttributeTypeAndValue, KJUR.asn1.ASN1Object); +1304 +1305 // === END X500Name Related ================================================= +1306 +1307 // === BEGIN Other ASN1 structure class ====================================== +1308 +1309 /** +1310 * SubjectPublicKeyInfo ASN.1 structure class +1311 * @name KJUR.asn1.x509.SubjectPublicKeyInfo +1312 * @class SubjectPublicKeyInfo ASN.1 structure class +1313 * @param {Object} params parameter for subject public key +1314 * @extends KJUR.asn1.ASN1Object +1315 * @description +1316 * <br/> +1317 * As for argument 'params' for constructor, you can specify one of +1318 * following properties: +1319 * <ul> +1320 * <li>{@link RSAKey} object</li> +1321 * <li>{@link KJUR.crypto.ECDSA} object</li> +1322 * <li>{@link KJUR.crypto.DSA} object</li> +1323 * <li>(DEPRECATED)rsakey - specify {@link RSAKey} object of subject public key</li> +1324 * <li>(DEPRECATED)rsapem - specify a string of PEM public key of RSA key</li> +1325 * </ul> +1326 * NOTE1: 'params' can be omitted.<br/> +1327 * NOTE2: DSA/ECDSA key object is also supported since asn1x509 1.0.6.<br/> +1328 * <h4>EXAMPLE</h4> +1329 * @example +1330 * var spki = new KJUR.asn1.x509.SubjectPublicKeyInfo(RSAKey_object); +1331 * var spki = new KJUR.asn1.x509.SubjectPublicKeyInfo(KJURcryptoECDSA_object); +1332 * var spki = new KJUR.asn1.x509.SubjectPublicKeyInfo(KJURcryptoDSA_object); +1333 */ +1334 KJUR.asn1.x509.SubjectPublicKeyInfo = function(params) { +1335 KJUR.asn1.x509.SubjectPublicKeyInfo.superclass.constructor.call(this); +1336 var asn1AlgId = null; +1337 var asn1SubjPKey = null; +1338 var rsaKey = null; +1339 +1340 /** +1341 * (DEPRECATED) set RSAKey object as subject public key +1342 * @name setRSAKey +1343 * @memberOf KJUR.asn1.x509.SubjectPublicKeyInfo +1344 * @function +1345 * @param {RSAKey} rsaKey {@link RSAKey} object for RSA public key +1346 * @description +1347 * @deprecated +1348 * @example +1349 * spki.setRSAKey(rsaKey); +1350 */ +1351 this.setRSAKey = function(rsaKey) { +1352 if (! RSAKey.prototype.isPrototypeOf(rsaKey)) +1353 throw "argument is not RSAKey instance"; +1354 this.rsaKey = rsaKey; +1355 var asn1RsaN = new KJUR.asn1.DERInteger({'bigint': rsaKey.n}); +1356 var asn1RsaE = new KJUR.asn1.DERInteger({'int': rsaKey.e}); +1357 var asn1RsaPub = new KJUR.asn1.DERSequence({'array': [asn1RsaN, asn1RsaE]}); +1358 var rsaKeyHex = asn1RsaPub.getEncodedHex(); +1359 this.asn1AlgId = new KJUR.asn1.x509.AlgorithmIdentifier({'name':'rsaEncryption'}); +1360 this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex':'00'+rsaKeyHex}); +1361 }; +1362 +1363 /** +1364 * (DEPRECATED) set a PEM formatted RSA public key string as RSA public key +1365 * @name setRSAPEM +1366 * @memberOf KJUR.asn1.x509.SubjectPublicKeyInfo +1367 * @function +1368 * @param {String} rsaPubPEM PEM formatted RSA public key string +1369 * @deprecated +1370 * @description +1371 * @example +1372 * spki.setRSAPEM(rsaPubPEM); +1373 */ +1374 this.setRSAPEM = function(rsaPubPEM) { +1375 if (rsaPubPEM.match(/-----BEGIN PUBLIC KEY-----/)) { +1376 var s = rsaPubPEM; +1377 s = s.replace(/^-----[^-]+-----/, ''); +1378 s = s.replace(/-----[^-]+-----\s*$/, ''); +1379 var rsaB64 = s.replace(/\s+/g, ''); +1380 var rsaWA = CryptoJS.enc.Base64.parse(rsaB64); +1381 var rsaP8Hex = CryptoJS.enc.Hex.stringify(rsaWA); +1382 var a = _rsapem_getHexValueArrayOfChildrenFromHex(rsaP8Hex); +1383 var hBitStrVal = a[1]; +1384 var rsaHex = hBitStrVal.substr(2); +1385 var a3 = _rsapem_getHexValueArrayOfChildrenFromHex(rsaHex); +1386 var rsaKey = new RSAKey(); +1387 rsaKey.setPublic(a3[0], a3[1]); +1388 this.setRSAKey(rsaKey); +1389 } else { +1390 throw "key not supported"; +1391 } +1392 }; +1393 +1394 /* +1395 * @since asn1x509 1.0.7 +1396 */ +1397 this.getASN1Object = function() { +1398 if (this.asn1AlgId == null || this.asn1SubjPKey == null) +1399 throw "algId and/or subjPubKey not set"; +1400 var o = new KJUR.asn1.DERSequence({'array': +1401 [this.asn1AlgId, this.asn1SubjPKey]}); +1402 return o; +1403 }; +1404 +1405 this.getEncodedHex = function() { +1406 var o = this.getASN1Object(); +1407 this.hTLV = o.getEncodedHex(); +1408 return this.hTLV; +1409 }; +1410 +1411 this._setRSAKey = function(key) { +1412 var asn1RsaPub = KJUR.asn1.ASN1Util.newObject({ +1413 'seq': [{'int': {'bigint': key.n}}, {'int': {'int': key.e}}] +1414 }); +1415 var rsaKeyHex = asn1RsaPub.getEncodedHex(); +1416 this.asn1AlgId = new KJUR.asn1.x509.AlgorithmIdentifier({'name':'rsaEncryption'}); +1417 this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex':'00'+rsaKeyHex}); +1418 }; +1419 +1420 this._setEC = function(key) { +1421 var asn1Params = new KJUR.asn1.DERObjectIdentifier({'name': key.curveName}); +1422 this.asn1AlgId = +1423 new KJUR.asn1.x509.AlgorithmIdentifier({'name': 'ecPublicKey', +1424 'asn1params': asn1Params}); +1425 this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex': '00' + key.pubKeyHex}); +1426 }; +1427 +1428 this._setDSA = function(key) { +1429 var asn1Params = new KJUR.asn1.ASN1Util.newObject({ +1430 'seq': [{'int': {'bigint': key.p}}, +1431 {'int': {'bigint': key.q}}, +1432 {'int': {'bigint': key.g}}] +1433 }); +1434 this.asn1AlgId = +1435 new KJUR.asn1.x509.AlgorithmIdentifier({'name': 'dsa', +1436 'asn1params': asn1Params}); +1437 var pubInt = new KJUR.asn1.DERInteger({'bigint': key.y}); +1438 this.asn1SubjPKey = new KJUR.asn1.DERBitString({'hex': '00' + pubInt.getEncodedHex()}); +1439 }; +1440 +1441 if (typeof params != "undefined") { +1442 if (typeof RSAKey != 'undefined' && params instanceof RSAKey) { +1443 this._setRSAKey(params); +1444 } else if (typeof KJUR.crypto.ECDSA != 'undefined' && +1445 params instanceof KJUR.crypto.ECDSA) { +1446 this._setEC(params); +1447 } else if (typeof KJUR.crypto.DSA != 'undefined' && +1448 params instanceof KJUR.crypto.DSA) { +1449 this._setDSA(params); +1450 } else if (typeof params['rsakey'] != "undefined") { +1451 this.setRSAKey(params['rsakey']); +1452 } else if (typeof params['rsapem'] != "undefined") { +1453 this.setRSAPEM(params['rsapem']); +1454 } +1455 } +1456 }; +1457 YAHOO.lang.extend(KJUR.asn1.x509.SubjectPublicKeyInfo, KJUR.asn1.ASN1Object); +1458 +1459 /** +1460 * Time ASN.1 structure class +1461 * @name KJUR.asn1.x509.Time +1462 * @class Time ASN.1 structure class +1463 * @param {Array} params associative array of parameters (ex. {'str': '130508235959Z'}) +1464 * @extends KJUR.asn1.ASN1Object +1465 * @description +1466 * <br/> +1467 * <h4>EXAMPLES</h4> +1468 * @example +1469 * var t1 = new KJUR.asn1.x509.Time{'str': '130508235959Z'} // UTCTime by default +1470 * var t2 = new KJUR.asn1.x509.Time{'type': 'gen', 'str': '20130508235959Z'} // GeneralizedTime +1471 */ +1472 KJUR.asn1.x509.Time = function(params) { +1473 KJUR.asn1.x509.Time.superclass.constructor.call(this); +1474 var type = null; +1475 var timeParams = null; +1476 +1477 this.setTimeParams = function(timeParams) { +1478 this.timeParams = timeParams; +1479 } +1480 +1481 this.getEncodedHex = function() { +1482 var o = null; +1483 +1484 if (this.timeParams != null) { +1485 if (this.type == "utc") { +1486 o = new KJUR.asn1.DERUTCTime(this.timeParams); +1487 } else { +1488 o = new KJUR.asn1.DERGeneralizedTime(this.timeParams); +1489 } +1490 } else { +1491 if (this.type == "utc") { +1492 o = new KJUR.asn1.DERUTCTime(); +1493 } else { +1494 o = new KJUR.asn1.DERGeneralizedTime(); +1495 } +1496 } +1497 this.TLV = o.getEncodedHex(); +1498 return this.TLV; +1499 }; +1500 +1501 this.type = "utc"; +1502 if (typeof params != "undefined") { +1503 if (typeof params.type != "undefined") { +1504 this.type = params.type; +1505 } else { +1506 if (typeof params.str != "undefined") { +1507 if (params.str.match(/^[0-9]{12}Z$/)) this.type = "utc"; +1508 if (params.str.match(/^[0-9]{14}Z$/)) this.type = "gen"; +1509 } +1510 } +1511 this.timeParams = params; +1512 } +1513 }; +1514 YAHOO.lang.extend(KJUR.asn1.x509.Time, KJUR.asn1.ASN1Object); +1515 +1516 /** +1517 * AlgorithmIdentifier ASN.1 structure class +1518 * @name KJUR.asn1.x509.AlgorithmIdentifier +1519 * @class AlgorithmIdentifier ASN.1 structure class +1520 * @param {Array} params associative array of parameters (ex. {'name': 'SHA1withRSA'}) +1521 * @extends KJUR.asn1.ASN1Object +1522 * @description +1523 * @example +1524 * algId1 = new KJUR.asn1.x509.AlgorithmIdentifier({name: "sha1"}); +1525 */ +1526 KJUR.asn1.x509.AlgorithmIdentifier = function(params) { +1527 KJUR.asn1.x509.AlgorithmIdentifier.superclass.constructor.call(this); +1528 var nameAlg = null; +1529 var asn1Alg = null; +1530 var asn1Params = null; +1531 var paramEmpty = false; +1532 +1533 this.getEncodedHex = function() { +1534 if (this.nameAlg == null && this.asn1Alg == null) { +1535 throw "algorithm not specified"; +1536 } +1537 if (this.nameAlg != null && this.asn1Alg == null) { +1538 this.asn1Alg = KJUR.asn1.x509.OID.name2obj(this.nameAlg); +1539 } +1540 var a = [this.asn1Alg]; +1541 if (! this.paramEmpty) a.push(this.asn1Params); +1542 var o = new KJUR.asn1.DERSequence({'array': a}); +1543 this.hTLV = o.getEncodedHex(); +1544 return this.hTLV; +1545 }; +1546 +1547 if (typeof params != "undefined") { +1548 if (typeof params['name'] != "undefined") { +1549 this.nameAlg = params['name']; +1550 } +1551 if (typeof params['asn1params'] != "undefined") { +1552 this.asn1Params = params['asn1params']; +1553 } +1554 if (typeof params['paramempty'] != "undefined") { +1555 this.paramEmpty = params['paramempty']; +1556 } +1557 } +1558 if (this.asn1Params == null) { +1559 this.asn1Params = new KJUR.asn1.DERNull(); +1560 } +1561 }; +1562 YAHOO.lang.extend(KJUR.asn1.x509.AlgorithmIdentifier, KJUR.asn1.ASN1Object); +1563 +1564 /** +1565 * GeneralName ASN.1 structure class +1566 * @name KJUR.asn1.x509.GeneralName +1567 * @class GeneralName ASN.1 structure class +1568 * @description +1569 * <br/> +1570 * As for argument 'params' for constructor, you can specify one of +1571 * following properties: +1572 * <ul> +1573 * <li>rfc822 - rfc822Name[1] (ex. user1@foo.com)</li> +1574 * <li>dns - dNSName[2] (ex. foo.com)</li> +1575 * <li>uri - uniformResourceIdentifier[6] (ex. http://foo.com/)</li> +1576 * <li>certissuer - directoryName[4] (PEM or hex string of cert)</li> +1577 * <li>certsubj - directoryName[4] (PEM or hex string of cert)</li> +1578 * </ul> +1579 * NOTE1: certissuer and certsubj is supported since asn1x509 1.0.10. +1580 * +1581 * Here is definition of the ASN.1 syntax: +1582 * <pre> +1583 * -- NOTE: under the CHOICE, it will always be explicit. +1584 * GeneralName ::= CHOICE { +1585 * otherName [0] OtherName, +1586 * rfc822Name [1] IA5String, +1587 * dNSName [2] IA5String, +1588 * x400Address [3] ORAddress, +1589 * directoryName [4] Name, +1590 * ediPartyName [5] EDIPartyName, +1591 * uniformResourceIdentifier [6] IA5String, +1592 * iPAddress [7] OCTET STRING, +1593 * registeredID [8] OBJECT IDENTIFIER } +1594 * </pre> +1595 * +1596 * +1597 * +1598 * @example +1599 * gn = new KJUR.asn1.x509.GeneralName({rfc822: 'test@aaa.com'}); +1600 * gn = new KJUR.asn1.x509.GeneralName({dns: 'aaa.com'}); +1601 * gn = new KJUR.asn1.x509.GeneralName({uri: 'http://aaa.com/'}); +1602 * gn = new KJUR.asn1.x509.GeneralName({certissuer: certPEM}); +1603 * gn = new KJUR.asn1.x509.GeneralName({certsubj: certPEM}); +1604 */ +1605 KJUR.asn1.x509.GeneralName = function(params) { +1606 KJUR.asn1.x509.GeneralName.superclass.constructor.call(this); +1607 var asn1Obj = null; +1608 var type = null; +1609 var pTag = {rfc822: '81', dns: '82', dn: 'a4', uri: '86'}; +1610 this.explicit = false; +1611 +1612 this.setByParam = function(params) { +1613 var str = null; +1614 var v = null; +1615 +1616 if (typeof params == "undefined") return; +1617 +1618 if (typeof params.rfc822 != "undefined") { +1619 this.type = 'rfc822'; +1620 v = new KJUR.asn1.DERIA5String({'str': params[this.type]}); +1621 } +1622 if (typeof params.dns != "undefined") { +1623 this.type = 'dns'; +1624 v = new KJUR.asn1.DERIA5String({'str': params[this.type]}); +1625 } +1626 if (typeof params.uri != "undefined") { +1627 this.type = 'uri'; +1628 v = new KJUR.asn1.DERIA5String({'str': params[this.type]}); +1629 } +1630 if (typeof params.certissuer != "undefined") { +1631 this.type = 'dn'; +1632 this.explicit = true; +1633 var certStr = params.certissuer; +1634 var certHex = null; +1635 if (certStr.match(/^[0-9A-Fa-f]+$/)) { +1636 certHex == certStr; +1637 } +1638 if (certStr.indexOf("-----BEGIN ") != -1) { +1639 certHex = X509.pemToHex(certStr); +1640 } +1641 if (certHex == null) throw "certissuer param not cert"; +1642 var x = new X509(); +1643 x.hex = certHex; +1644 var dnHex = x.getIssuerHex(); +1645 v = new KJUR.asn1.ASN1Object(); +1646 v.hTLV = dnHex; +1647 } +1648 if (typeof params.certsubj != "undefined") { +1649 this.type = 'dn'; +1650 this.explicit = true; +1651 var certStr = params.certsubj; +1652 var certHex = null; +1653 if (certStr.match(/^[0-9A-Fa-f]+$/)) { +1654 certHex == certStr; +1655 } +1656 if (certStr.indexOf("-----BEGIN ") != -1) { +1657 certHex = X509.pemToHex(certStr); +1658 } +1659 if (certHex == null) throw "certsubj param not cert"; +1660 var x = new X509(); +1661 x.hex = certHex; +1662 var dnHex = x.getSubjectHex(); +1663 v = new KJUR.asn1.ASN1Object(); +1664 v.hTLV = dnHex; +1665 } +1666 +1667 if (this.type == null) +1668 throw "unsupported type in params=" + params; +1669 this.asn1Obj = new KJUR.asn1.DERTaggedObject({'explicit': this.explicit, +1670 'tag': pTag[this.type], +1671 'obj': v}); +1672 }; +1673 +1674 this.getEncodedHex = function() { +1675 return this.asn1Obj.getEncodedHex(); +1676 } +1677 +1678 if (typeof params != "undefined") { +1679 this.setByParam(params); +1680 } +1681 +1682 }; +1683 YAHOO.lang.extend(KJUR.asn1.x509.GeneralName, KJUR.asn1.ASN1Object); +1684 +1685 /** +1686 * GeneralNames ASN.1 structure class +1687 * @name KJUR.asn1.x509.GeneralNames +1688 * @class GeneralNames ASN.1 structure class +1689 * @description +1690 * <br/> +1691 * <h4>EXAMPLE AND ASN.1 SYNTAX</h4> +1692 * @example +1693 * var gns = new KJUR.asn1.x509.GeneralNames([{'uri': 'http://aaa.com/'}, {'uri': 'http://bbb.com/'}]); +1694 * +1695 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName +1696 */ +1697 KJUR.asn1.x509.GeneralNames = function(paramsArray) { +1698 KJUR.asn1.x509.GeneralNames.superclass.constructor.call(this); +1699 var asn1Array = null; +1700 +1701 /** +1702 * set a array of {@link KJUR.asn1.x509.GeneralName} parameters +1703 * @name setByParamArray +1704 * @memberOf KJUR.asn1.x509.GeneralNames +1705 * @function +1706 * @param {Array} paramsArray Array of {@link KJUR.asn1.x509.GeneralNames} +1707 * @description +1708 * <br/> +1709 * <h4>EXAMPLES</h4> +1710 * @example +1711 * var gns = new KJUR.asn1.x509.GeneralNames(); +1712 * gns.setByParamArray([{'uri': 'http://aaa.com/'}, {'uri': 'http://bbb.com/'}]); +1713 */ +1714 this.setByParamArray = function(paramsArray) { +1715 for (var i = 0; i < paramsArray.length; i++) { +1716 var o = new KJUR.asn1.x509.GeneralName(paramsArray[i]); +1717 this.asn1Array.push(o); +1718 } +1719 }; +1720 +1721 this.getEncodedHex = function() { +1722 var o = new KJUR.asn1.DERSequence({'array': this.asn1Array}); +1723 return o.getEncodedHex(); +1724 }; +1725 +1726 this.asn1Array = new Array(); +1727 if (typeof paramsArray != "undefined") { +1728 this.setByParamArray(paramsArray); +1729 } +1730 }; +1731 YAHOO.lang.extend(KJUR.asn1.x509.GeneralNames, KJUR.asn1.ASN1Object); +1732 +1733 /** +1734 * DistributionPointName ASN.1 structure class +1735 * @name KJUR.asn1.x509.DistributionPointName +1736 * @class DistributionPointName ASN.1 structure class +1737 * @description +1738 * @example +1739 */ +1740 KJUR.asn1.x509.DistributionPointName = function(gnOrRdn) { +1741 KJUR.asn1.x509.DistributionPointName.superclass.constructor.call(this); +1742 var asn1Obj = null; +1743 var type = null; +1744 var tag = null; +1745 var asn1V = null; +1746 +1747 this.getEncodedHex = function() { +1748 if (this.type != "full") +1749 throw "currently type shall be 'full': " + this.type; +1750 this.asn1Obj = new KJUR.asn1.DERTaggedObject({'explicit': false, +1751 'tag': this.tag, +1752 'obj': this.asn1V}); +1753 this.hTLV = this.asn1Obj.getEncodedHex(); +1754 return this.hTLV; +1755 }; +1756 +1757 if (typeof gnOrRdn != "undefined") { +1758 if (KJUR.asn1.x509.GeneralNames.prototype.isPrototypeOf(gnOrRdn)) { +1759 this.type = "full"; +1760 this.tag = "a0"; +1761 this.asn1V = gnOrRdn; +1762 } else { +1763 throw "This class supports GeneralNames only as argument"; +1764 } +1765 } +1766 }; +1767 YAHOO.lang.extend(KJUR.asn1.x509.DistributionPointName, KJUR.asn1.ASN1Object); +1768 +1769 /** +1770 * DistributionPoint ASN.1 structure class +1771 * @name KJUR.asn1.x509.DistributionPoint +1772 * @class DistributionPoint ASN.1 structure class +1773 * @description +1774 * @example +1775 */ +1776 KJUR.asn1.x509.DistributionPoint = function(params) { +1777 KJUR.asn1.x509.DistributionPoint.superclass.constructor.call(this); +1778 var asn1DP = null; +1779 +1780 this.getEncodedHex = function() { +1781 var seq = new KJUR.asn1.DERSequence(); +1782 if (this.asn1DP != null) { +1783 var o1 = new KJUR.asn1.DERTaggedObject({'explicit': true, +1784 'tag': 'a0', +1785 'obj': this.asn1DP}); +1786 seq.appendASN1Object(o1); +1787 } +1788 this.hTLV = seq.getEncodedHex(); +1789 return this.hTLV; +1790 }; +1791 +1792 if (typeof params != "undefined") { +1793 if (typeof params['dpobj'] != "undefined") { +1794 this.asn1DP = params['dpobj']; +1795 } +1796 } +1797 }; +1798 YAHOO.lang.extend(KJUR.asn1.x509.DistributionPoint, KJUR.asn1.ASN1Object); +1799 +1800 /** +1801 * static object for OID +1802 * @name KJUR.asn1.x509.OID +1803 * @class static object for OID +1804 * @property {Assoc Array} atype2oidList for short attribyte type name and oid (i.e. 'C' and '2.5.4.6') +1805 * @property {Assoc Array} name2oidList for oid name and oid (i.e. 'keyUsage' and '2.5.29.15') +1806 * @property {Assoc Array} objCache for caching name and DERObjectIdentifier object +1807 * @description +1808 * <dl> +1809 * <dt><b>atype2oidList</b> +1810 * <dd>currently supports 'C', 'O', 'OU', 'ST', 'L' and 'CN' only. +1811 * <dt><b>name2oidList</b> +1812 * <dd>currently supports 'SHA1withRSA', 'rsaEncryption' and some extension OIDs +1813 * </dl> +1814 * @example +1815 */ +1816 KJUR.asn1.x509.OID = new function(params) { +1817 this.atype2oidList = { +1818 'C': '2.5.4.6', +1819 'O': '2.5.4.10', +1820 'OU': '2.5.4.11', +1821 'ST': '2.5.4.8', +1822 'L': '2.5.4.7', +1823 'CN': '2.5.4.3', +1824 'SN': '2.5.4.4', +1825 'DN': '2.5.4.49', +1826 'DC': '0.9.2342.19200300.100.1.25', +1827 }; +1828 this.name2oidList = { +1829 'sha1': '1.3.14.3.2.26', +1830 'sha256': '2.16.840.1.101.3.4.2.1', +1831 'sha384': '2.16.840.1.101.3.4.2.2', +1832 'sha512': '2.16.840.1.101.3.4.2.3', +1833 'sha224': '2.16.840.1.101.3.4.2.4', +1834 'md5': '1.2.840.113549.2.5', +1835 'md2': '1.3.14.7.2.2.1', +1836 'ripemd160': '1.3.36.3.2.1', +1837 +1838 'MD2withRSA': '1.2.840.113549.1.1.2', +1839 'MD4withRSA': '1.2.840.113549.1.1.3', +1840 'MD5withRSA': '1.2.840.113549.1.1.4', +1841 'SHA1withRSA': '1.2.840.113549.1.1.5', +1842 'SHA224withRSA': '1.2.840.113549.1.1.14', +1843 'SHA256withRSA': '1.2.840.113549.1.1.11', +1844 'SHA384withRSA': '1.2.840.113549.1.1.12', +1845 'SHA512withRSA': '1.2.840.113549.1.1.13', +1846 +1847 'SHA1withECDSA': '1.2.840.10045.4.1', +1848 'SHA224withECDSA': '1.2.840.10045.4.3.1', +1849 'SHA256withECDSA': '1.2.840.10045.4.3.2', +1850 'SHA384withECDSA': '1.2.840.10045.4.3.3', +1851 'SHA512withECDSA': '1.2.840.10045.4.3.4', +1852 +1853 'dsa': '1.2.840.10040.4.1', +1854 'SHA1withDSA': '1.2.840.10040.4.3', +1855 'SHA224withDSA': '2.16.840.1.101.3.4.3.1', +1856 'SHA256withDSA': '2.16.840.1.101.3.4.3.2', +1857 +1858 'rsaEncryption': '1.2.840.113549.1.1.1', +1859 +1860 'countryName': '2.5.4.6', +1861 'organization': '2.5.4.10', +1862 'organizationalUnit': '2.5.4.11', +1863 'stateOrProvinceName': '2.5.4.8', +1864 'locality': '2.5.4.7', +1865 'commonName': '2.5.4.3', +1866 +1867 'subjectKeyIdentifier': '2.5.29.14', +1868 'keyUsage': '2.5.29.15', +1869 'subjectAltName': '2.5.29.17', +1870 'basicConstraints': '2.5.29.19', +1871 'nameConstraints': '2.5.29.30', +1872 'cRLDistributionPoints':'2.5.29.31', +1873 'certificatePolicies': '2.5.29.32', +1874 'authorityKeyIdentifier':'2.5.29.35', +1875 'policyConstraints': '2.5.29.36', +1876 'extKeyUsage': '2.5.29.37', +1877 'authorityInfoAccess': '1.3.6.1.5.5.7.1.1', +1878 +1879 'anyExtendedKeyUsage': '2.5.29.37.0', +1880 'serverAuth': '1.3.6.1.5.5.7.3.1', +1881 'clientAuth': '1.3.6.1.5.5.7.3.2', +1882 'codeSigning': '1.3.6.1.5.5.7.3.3', +1883 'emailProtection': '1.3.6.1.5.5.7.3.4', +1884 'timeStamping': '1.3.6.1.5.5.7.3.8', +1885 'ocspSigning': '1.3.6.1.5.5.7.3.9', +1886 +1887 'ecPublicKey': '1.2.840.10045.2.1', +1888 'secp256r1': '1.2.840.10045.3.1.7', +1889 'secp256k1': '1.3.132.0.10', +1890 'secp384r1': '1.3.132.0.34', +1891 +1892 'pkcs5PBES2': '1.2.840.113549.1.5.13', +1893 'pkcs5PBKDF2': '1.2.840.113549.1.5.12', +1894 +1895 'des-EDE3-CBC': '1.2.840.113549.3.7', +1896 +1897 'data': '1.2.840.113549.1.7.1', // CMS data +1898 'signed-data': '1.2.840.113549.1.7.2', // CMS signed-data +1899 'enveloped-data': '1.2.840.113549.1.7.3', // CMS enveloped-data +1900 'digested-data': '1.2.840.113549.1.7.5', // CMS digested-data +1901 'encrypted-data': '1.2.840.113549.1.7.6', // CMS encrypted-data +1902 'authenticated-data': '1.2.840.113549.1.9.16.1.2', // CMS authenticated-data +1903 'tstinfo': '1.2.840.113549.1.9.16.1.4', // RFC3161 TSTInfo +1904 }; +1905 +1906 this.objCache = {}; +1907 +1908 /** +1909 * get DERObjectIdentifier by registered OID name +1910 * @name name2obj +1911 * @memberOf KJUR.asn1.x509.OID +1912 * @function +1913 * @param {String} name OID +1914 * @description +1915 * @example +1916 * var asn1ObjOID = OID.name2obj('SHA1withRSA'); +1917 */ +1918 this.name2obj = function(name) { +1919 if (typeof this.objCache[name] != "undefined") +1920 return this.objCache[name]; +1921 if (typeof this.name2oidList[name] == "undefined") +1922 throw "Name of ObjectIdentifier not defined: " + name; +1923 var oid = this.name2oidList[name]; +1924 var obj = new KJUR.asn1.DERObjectIdentifier({'oid': oid}); +1925 this.objCache[name] = obj; +1926 return obj; +1927 }; +1928 +1929 /** +1930 * get DERObjectIdentifier by registered attribyte type name such like 'C' or 'CN' +1931 * @name atype2obj +1932 * @memberOf KJUR.asn1.x509.OID +1933 * @function +1934 * @param {String} atype short attribute type name such like 'C' or 'CN' +1935 * @description +1936 * @example +1937 * var asn1ObjOID = OID.atype2obj('CN'); +1938 */ +1939 this.atype2obj = function(atype) { +1940 if (typeof this.objCache[atype] != "undefined") +1941 return this.objCache[atype]; +1942 if (typeof this.atype2oidList[atype] == "undefined") +1943 throw "AttributeType name undefined: " + atype; +1944 var oid = this.atype2oidList[atype]; +1945 var obj = new KJUR.asn1.DERObjectIdentifier({'oid': oid}); +1946 this.objCache[atype] = obj; +1947 return obj; +1948 }; +1949 }; +1950 +1951 /* +1952 * convert OID to name +1953 * @name oid2name +1954 * @memberOf KJUR.asn1.x509.OID +1955 * @function +1956 * @param {String} dot noted Object Identifer string (ex. 1.2.3.4) +1957 * @return {String} OID name +1958 * @description +1959 * This static method converts OID string to its name. +1960 * If OID is undefined then it returns empty string (i.e. ''). +1961 * @example +1962 * name = KJUR.asn1.x509.OID.oid2name("1.3.6.1.5.5.7.1.1"); +1963 * // name will be 'authorityInfoAccess'. +1964 * @since asn1x509 1.0.9 +1965 */ +1966 KJUR.asn1.x509.OID.oid2name = function(oid) { +1967 var list = KJUR.asn1.x509.OID.name2oidList; +1968 for (var name in list) { +1969 if (list[name] == oid) return name; +1970 } +1971 return ''; +1972 }; +1973 +1974 /* +1975 * convert name to OID +1976 * @name name2oid +1977 * @memberOf KJUR.asn1.x509.OID +1978 * @function +1979 * @param {String} OID name +1980 * @return {String} dot noted Object Identifer string (ex. 1.2.3.4) +1981 * @description +1982 * This static method converts from OID name to OID string. +1983 * If OID is undefined then it returns empty string (i.e. ''). +1984 * @example +1985 * name = KJUR.asn1.x509.OID.name2oid("authorityInfoAccess"); +1986 * // name will be '1.3.6.1.5.5.7.1.1'. +1987 * @since asn1x509 1.0.11 +1988 */ +1989 KJUR.asn1.x509.OID.name2oid = function(name) { +1990 var list = KJUR.asn1.x509.OID.name2oidList; +1991 if (list[name] === undefined) return ''; +1992 return list[name]; +1993 }; +1994 +1995 /** +1996 * X.509 certificate and CRL utilities class +1997 * @name KJUR.asn1.x509.X509Util +1998 * @class X.509 certificate and CRL utilities class +1999 */ +2000 KJUR.asn1.x509.X509Util = new function() { +2001 /** +2002 * get PKCS#8 PEM public key string from RSAKey object +2003 * @name getPKCS8PubKeyPEMfromRSAKey +2004 * @memberOf KJUR.asn1.x509.X509Util +2005 * @function +2006 * @param {RSAKey} rsaKey RSA public key of {@link RSAKey} object +2007 * @description +2008 * @example +2009 * var pem = KJUR.asn1.x509.X509Util.getPKCS8PubKeyPEMfromRSAKey(pubKey); +2010 */ +2011 this.getPKCS8PubKeyPEMfromRSAKey = function(rsaKey) { +2012 var pem = null; +2013 var hN = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(rsaKey.n); +2014 var hE = KJUR.asn1.ASN1Util.integerToByteHex(rsaKey.e); +2015 var iN = new KJUR.asn1.DERInteger({hex: hN}); +2016 var iE = new KJUR.asn1.DERInteger({hex: hE}); +2017 var asn1PubKey = new KJUR.asn1.DERSequence({array: [iN, iE]}); +2018 var hPubKey = asn1PubKey.getEncodedHex(); +2019 var o1 = new KJUR.asn1.x509.AlgorithmIdentifier({name: 'rsaEncryption'}); +2020 var o2 = new KJUR.asn1.DERBitString({hex: '00' + hPubKey}); +2021 var seq = new KJUR.asn1.DERSequence({array: [o1, o2]}); +2022 var hP8 = seq.getEncodedHex(); +2023 var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(hP8, "PUBLIC KEY"); +2024 return pem; +2025 }; +2026 }; +2027 /** +2028 * issue a certificate in PEM format +2029 * @name newCertPEM +2030 * @memberOf KJUR.asn1.x509.X509Util +2031 * @function +2032 * @param {Array} param parameter to issue a certificate +2033 * @since asn1x509 1.0.6 +2034 * @description +2035 * This method can issue a certificate by a simple +2036 * JSON object. +2037 * Signature value will be provided by signing with +2038 * private key using 'cakey' parameter or +2039 * hexa decimal signature value by 'sighex' parameter. +2040 * +2041 * NOTE: When using DSA or ECDSA CA signing key, +2042 * use 'paramempty' in 'sigalg' to ommit parameter field +2043 * of AlgorithmIdentifer. In case of RSA, parameter +2044 * NULL will be specified by default. +2045 * +2046 * @example +2047 * var certPEM = KJUR.asn1.x509.X509Util.newCertPEM( +2048 * { serial: {int: 4}, +2049 * sigalg: {name: 'SHA1withECDSA', paramempty: true}, +2050 * issuer: {str: '/C=US/O=a'}, +2051 * notbefore: {'str': '130504235959Z'}, +2052 * notafter: {'str': '140504235959Z'}, +2053 * subject: {str: '/C=US/O=b'}, +2054 * sbjpubkey: pubKeyPEM, +2055 * ext: [ +2056 * {basicConstraints: {cA: true, critical: true}}, +2057 * {keyUsage: {bin: '11'}}, +2058 * ], +2059 * cakey: [prvkey, pass]} +2060 * ); +2061 * // -- or -- +2062 * var certPEM = KJUR.asn1.x509.X509Util.newCertPEM( +2063 * { serial: {int: 1}, +2064 * sigalg: {name: 'SHA1withRSA', paramempty: true}, +2065 * issuer: {str: '/C=US/O=T1'}, +2066 * notbefore: {'str': '130504235959Z'}, +2067 * notafter: {'str': '140504235959Z'}, +2068 * subject: {str: '/C=US/O=T1'}, +2069 * sbjpubkey: pubKeyObj, +2070 * sighex: '0102030405..'} +2071 * ); +2072 * // for the issuer and subject field, another +2073 * // representation is also available +2074 * var certPEM = KJUR.asn1.x509.X509Util.newCertPEM( +2075 * { serial: {int: 1}, +2076 * sigalg: {name: 'SHA1withRSA', paramempty: true}, +2077 * issuer: {C: "US", O: "T1"}, +2078 * notbefore: {'str': '130504235959Z'}, +2079 * notafter: {'str': '140504235959Z'}, +2080 * subject: {C: "US", O: "T1", CN: "http://example.com/"}, +2081 * sbjpubkey: pubKeyObj, +2082 * sighex: '0102030405..'} +2083 * ); +2084 */ +2085 KJUR.asn1.x509.X509Util.newCertPEM = function(param) { +2086 var ns1 = KJUR.asn1.x509; +2087 var o = new ns1.TBSCertificate(); +2088 +2089 if (param.serial !== undefined) +2090 o.setSerialNumberByParam(param.serial); +2091 else +2092 throw "serial number undefined."; +2093 +2094 if (typeof param.sigalg.name == 'string') +2095 o.setSignatureAlgByParam(param.sigalg); +2096 else +2097 throw "unproper signature algorithm name"; +2098 +2099 if (param.issuer !== undefined) +2100 o.setIssuerByParam(param.issuer); +2101 else +2102 throw "issuer name undefined."; +2103 +2104 if (param.notbefore !== undefined) +2105 o.setNotBeforeByParam(param.notbefore); +2106 else +2107 throw "notbefore undefined."; +2108 +2109 if (param.notafter !== undefined) +2110 o.setNotAfterByParam(param.notafter); +2111 else +2112 throw "notafter undefined."; +2113 +2114 if (param.subject !== undefined) +2115 o.setSubjectByParam(param.subject); +2116 else +2117 throw "subject name undefined."; +2118 +2119 if (param.sbjpubkey !== undefined) +2120 o.setSubjectPublicKeyByGetKey(param.sbjpubkey); +2121 else +2122 throw "subject public key undefined."; +2123 +2124 if (param.ext !== undefined && param.ext.length !== undefined) { +2125 for (var i = 0; i < param.ext.length; i++) { +2126 for (key in param.ext[i]) { +2127 o.appendExtensionByName(key, param.ext[i][key]); +2128 } +2129 } +2130 } +2131 +2132 // set signature +2133 if (param.cakey === undefined && param.sighex === undefined) +2134 throw "param cakey and sighex undefined."; +2135 +2136 var caKey = null; +2137 var cert = null; +2138 +2139 if (param.cakey) { +2140 caKey = KEYUTIL.getKey.apply(null, param.cakey); +2141 cert = new ns1.Certificate({'tbscertobj': o, 'prvkeyobj': caKey}); +2142 cert.sign(); +2143 } +2144 +2145 if (param.sighex) { +2146 cert = new ns1.Certificate({'tbscertobj': o}); +2147 cert.setSignatureHex(param.sighex); +2148 } +2149 +2150 return cert.getPEMString(); +2151 }; +2152 +2153 /* +2154 org.bouncycastle.asn1.x500 +2155 AttributeTypeAndValue +2156 DirectoryString +2157 RDN +2158 X500Name +2159 X500NameBuilder +2160 +2161 org.bouncycastleasn1.x509 +2162 TBSCertificate +2163 */ +2164
    \ No newline at end of file diff --git a/api/symbols/src/crypto-1.1.js.html b/api/symbols/src/crypto-1.1.js.html index 237551db..5916f016 100644 --- a/api/symbols/src/crypto-1.1.js.html +++ b/api/symbols/src/crypto-1.1.js.html @@ -5,7 +5,7 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! crypto-1.1.8.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! crypto-1.1.9.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
       2  */
       3 /*
       4  * crypto.js - Cryptographic Algorithm Provider class
    @@ -23,7 +23,7 @@
      16  * @fileOverview
      17  * @name crypto-1.1.js
      18  * @author Kenji Urushima kenji.urushima@gmail.com
    - 19  * @version 1.1.8 (2016-Feb-28)
    + 19  * @version 1.1.9 (2016-Oct-08)
      20  * @since jsrsasign 2.2
      21  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      22  */
    @@ -825,413 +825,414 @@
     818     var hSign = null;
     819 
     820     this._setAlgNames = function() {
    -821 	if (this.algName.match(/^(.+)with(.+)$/)) {
    -822 	    this.mdAlgName = RegExp.$1.toLowerCase();
    -823 	    this.pubkeyAlgName = RegExp.$2.toLowerCase();
    -824 	}
    -825     };
    -826 
    -827     this._zeroPaddingOfSignature = function(hex, bitLength) {
    -828 	var s = "";
    -829 	var nZero = bitLength / 4 - hex.length;
    -830 	for (var i = 0; i < nZero; i++) {
    -831 	    s = s + "0";
    -832 	}
    -833 	return s + hex;
    -834     };
    -835 
    -836     /**
    -837      * set signature algorithm and provider
    -838      * @name setAlgAndProvider
    -839      * @memberOf KJUR.crypto.Signature
    -840      * @function
    -841      * @param {String} alg signature algorithm name
    -842      * @param {String} prov provider name
    -843      * @description
    -844      * @example
    -845      * md.setAlgAndProvider('SHA1withRSA', 'cryptojs/jsrsa');
    -846      */
    -847     this.setAlgAndProvider = function(alg, prov) {
    -848 	this._setAlgNames();
    -849 	if (prov != 'cryptojs/jsrsa')
    -850 	    throw "provider not supported: " + prov;
    -851 
    -852 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(this.mdAlgName) != -1) {
    -853 	    try {
    -854 		this.md = new KJUR.crypto.MessageDigest({'alg':this.mdAlgName});
    -855 	    } catch (ex) {
    -856 		throw "setAlgAndProvider hash alg set fail alg=" +
    -857                       this.mdAlgName + "/" + ex;
    -858 	    }
    -859 
    -860 	    this.init = function(keyparam, pass) {
    -861 		var keyObj = null;
    -862 		try {
    -863 		    if (pass === undefined) {
    -864 			keyObj = KEYUTIL.getKey(keyparam);
    -865 		    } else {
    -866 			keyObj = KEYUTIL.getKey(keyparam, pass);
    -867 		    }
    -868 		} catch (ex) {
    -869 		    throw "init failed:" + ex;
    -870 		}
    -871 
    -872 		if (keyObj.isPrivate === true) {
    -873 		    this.prvKey = keyObj;
    -874 		    this.state = "SIGN";
    -875 		} else if (keyObj.isPublic === true) {
    -876 		    this.pubKey = keyObj;
    -877 		    this.state = "VERIFY";
    -878 		} else {
    -879 		    throw "init failed.:" + keyObj;
    -880 		}
    -881 	    };
    -882 
    -883 	    this.initSign = function(params) {
    -884 		if (typeof params['ecprvhex'] == 'string' &&
    -885                     typeof params['eccurvename'] == 'string') {
    -886 		    this.ecprvhex = params['ecprvhex'];
    -887 		    this.eccurvename = params['eccurvename'];
    -888 		} else {
    -889 		    this.prvKey = params;
    -890 		}
    -891 		this.state = "SIGN";
    -892 	    };
    -893 
    -894 	    this.initVerifyByPublicKey = function(params) {
    -895 		if (typeof params['ecpubhex'] == 'string' &&
    -896 		    typeof params['eccurvename'] == 'string') {
    -897 		    this.ecpubhex = params['ecpubhex'];
    -898 		    this.eccurvename = params['eccurvename'];
    -899 		} else if (params instanceof KJUR.crypto.ECDSA) {
    -900 		    this.pubKey = params;
    -901 		} else if (params instanceof RSAKey) {
    -902 		    this.pubKey = params;
    -903 		}
    -904 		this.state = "VERIFY";
    -905 	    };
    -906 
    -907 	    this.initVerifyByCertificatePEM = function(certPEM) {
    -908 		var x509 = new X509();
    -909 		x509.readCertPEM(certPEM);
    -910 		this.pubKey = x509.subjectPublicKeyRSA;
    -911 		this.state = "VERIFY";
    -912 	    };
    -913 
    -914 	    this.updateString = function(str) {
    -915 		this.md.updateString(str);
    -916 	    };
    -917 
    -918 	    this.updateHex = function(hex) {
    -919 		this.md.updateHex(hex);
    -920 	    };
    -921 
    -922 	    this.sign = function() {
    -923 		this.sHashHex = this.md.digest();
    -924 		if (typeof this.ecprvhex != "undefined" &&
    -925 		    typeof this.eccurvename != "undefined") {
    -926 		    var ec = new KJUR.crypto.ECDSA({'curve': this.eccurvename});
    -927 		    this.hSign = ec.signHex(this.sHashHex, this.ecprvhex);
    -928 		} else if (this.prvKey instanceof RSAKey &&
    -929 		           this.pubkeyAlgName == "rsaandmgf1") {
    -930 		    this.hSign = this.prvKey.signWithMessageHashPSS(this.sHashHex,
    -931 								    this.mdAlgName,
    -932 								    this.pssSaltLen);
    -933 		} else if (this.prvKey instanceof RSAKey &&
    -934 			   this.pubkeyAlgName == "rsa") {
    -935 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex,
    -936 								 this.mdAlgName);
    -937 		} else if (this.prvKey instanceof KJUR.crypto.ECDSA) {
    -938 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
    -939 		} else if (this.prvKey instanceof KJUR.crypto.DSA) {
    -940 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
    -941 		} else {
    -942 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
    -943 		}
    -944 		return this.hSign;
    -945 	    };
    -946 	    this.signString = function(str) {
    -947 		this.updateString(str);
    -948 		return this.sign();
    -949 	    };
    -950 	    this.signHex = function(hex) {
    -951 		this.updateHex(hex);
    -952 		return this.sign();
    -953 	    };
    -954 	    this.verify = function(hSigVal) {
    -955 	        this.sHashHex = this.md.digest();
    -956 		if (typeof this.ecpubhex != "undefined" &&
    -957 		    typeof this.eccurvename != "undefined") {
    -958 		    var ec = new KJUR.crypto.ECDSA({curve: this.eccurvename});
    -959 		    return ec.verifyHex(this.sHashHex, hSigVal, this.ecpubhex);
    -960 		} else if (this.pubKey instanceof RSAKey &&
    -961 			   this.pubkeyAlgName == "rsaandmgf1") {
    -962 		    return this.pubKey.verifyWithMessageHashPSS(this.sHashHex, hSigVal, 
    -963 								this.mdAlgName,
    -964 								this.pssSaltLen);
    -965 		} else if (this.pubKey instanceof RSAKey &&
    -966 			   this.pubkeyAlgName == "rsa") {
    -967 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
    -968 		} else if (this.pubKey instanceof KJUR.crypto.ECDSA) {
    -969 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
    -970 		} else if (this.pubKey instanceof KJUR.crypto.DSA) {
    -971 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
    -972 		} else {
    -973 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
    -974 		}
    -975 	    };
    -976 	}
    -977     };
    -978 
    -979     /**
    -980      * Initialize this object for signing or verifying depends on key
    -981      * @name init
    -982      * @memberOf KJUR.crypto.Signature
    -983      * @function
    -984      * @param {Object} key specifying public or private key as plain/encrypted PKCS#5/8 PEM file, certificate PEM or {@link RSAKey}, {@link KJUR.crypto.DSA} or {@link KJUR.crypto.ECDSA} object
    -985      * @param {String} pass (OPTION) passcode for encrypted private key
    -986      * @since crypto 1.1.3
    -987      * @description
    -988      * This method is very useful initialize method for Signature class since
    -989      * you just specify key then this method will automatically initialize it
    -990      * using {@link KEYUTIL.getKey} method.
    -991      * As for 'key',  following argument type are supported:
    -992      * <h5>signing</h5>
    -993      * <ul>
    -994      * <li>PEM formatted PKCS#8 encrypted RSA/ECDSA private key concluding "BEGIN ENCRYPTED PRIVATE KEY"</li>
    -995      * <li>PEM formatted PKCS#5 encrypted RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" and ",ENCRYPTED"</li>
    -996      * <li>PEM formatted PKCS#8 plain RSA/ECDSA private key concluding "BEGIN PRIVATE KEY"</li>
    -997      * <li>PEM formatted PKCS#5 plain RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" without ",ENCRYPTED"</li>
    -998      * <li>RSAKey object of private key</li>
    -999      * <li>KJUR.crypto.ECDSA object of private key</li>
    -1000      * <li>KJUR.crypto.DSA object of private key</li>
    -1001      * </ul>
    -1002      * <h5>verification</h5>
    -1003      * <ul>
    -1004      * <li>PEM formatted PKCS#8 RSA/EC/DSA public key concluding "BEGIN PUBLIC KEY"</li>
    -1005      * <li>PEM formatted X.509 certificate with RSA/EC/DSA public key concluding
    -1006      *     "BEGIN CERTIFICATE", "BEGIN X509 CERTIFICATE" or "BEGIN TRUSTED CERTIFICATE".</li>
    -1007      * <li>RSAKey object of public key</li>
    -1008      * <li>KJUR.crypto.ECDSA object of public key</li>
    -1009      * <li>KJUR.crypto.DSA object of public key</li>
    -1010      * </ul>
    -1011      * @example
    -1012      * sig.init(sCertPEM)
    -1013      */
    -1014     this.init = function(key, pass) {
    -1015 	throw "init(key, pass) not supported for this alg:prov=" +
    -1016 	      this.algProvName;
    -1017     };
    -1018 
    -1019     /**
    -1020      * Initialize this object for verifying with a public key
    -1021      * @name initVerifyByPublicKey
    -1022      * @memberOf KJUR.crypto.Signature
    -1023      * @function
    -1024      * @param {Object} param RSAKey object of public key or associative array for ECDSA
    -1025      * @since 1.0.2
    -1026      * @deprecated from crypto 1.1.5. please use init() method instead.
    -1027      * @description
    -1028      * Public key information will be provided as 'param' parameter and the value will be
    -1029      * following:
    -1030      * <ul>
    -1031      * <li>{@link RSAKey} object for RSA verification</li>
    -1032      * <li>associative array for ECDSA verification
    -1033      *     (ex. <code>{'ecpubhex': '041f..', 'eccurvename': 'secp256r1'}</code>)
    -1034      * </li>
    -1035      * </ul>
    -1036      * @example
    -1037      * sig.initVerifyByPublicKey(rsaPrvKey)
    -1038      */
    -1039     this.initVerifyByPublicKey = function(rsaPubKey) {
    -1040 	throw "initVerifyByPublicKey(rsaPubKeyy) not supported for this alg:prov=" +
    -1041 	      this.algProvName;
    -1042     };
    -1043 
    -1044     /**
    -1045      * Initialize this object for verifying with a certficate
    -1046      * @name initVerifyByCertificatePEM
    -1047      * @memberOf KJUR.crypto.Signature
    -1048      * @function
    -1049      * @param {String} certPEM PEM formatted string of certificate
    -1050      * @since 1.0.2
    -1051      * @deprecated from crypto 1.1.5. please use init() method instead.
    -1052      * @description
    -1053      * @example
    -1054      * sig.initVerifyByCertificatePEM(certPEM)
    -1055      */
    -1056     this.initVerifyByCertificatePEM = function(certPEM) {
    -1057 	throw "initVerifyByCertificatePEM(certPEM) not supported for this alg:prov=" +
    -1058 	    this.algProvName;
    -1059     };
    -1060 
    -1061     /**
    -1062      * Initialize this object for signing
    -1063      * @name initSign
    -1064      * @memberOf KJUR.crypto.Signature
    -1065      * @function
    -1066      * @param {Object} param RSAKey object of public key or associative array for ECDSA
    -1067      * @deprecated from crypto 1.1.5. please use init() method instead.
    -1068      * @description
    -1069      * Private key information will be provided as 'param' parameter and the value will be
    -1070      * following:
    -1071      * <ul>
    -1072      * <li>{@link RSAKey} object for RSA signing</li>
    -1073      * <li>associative array for ECDSA signing
    -1074      *     (ex. <code>{'ecprvhex': '1d3f..', 'eccurvename': 'secp256r1'}</code>)</li>
    -1075      * </ul>
    -1076      * @example
    -1077      * sig.initSign(prvKey)
    -1078      */
    -1079     this.initSign = function(prvKey) {
    -1080 	throw "initSign(prvKey) not supported for this alg:prov=" + this.algProvName;
    -1081     };
    -1082 
    -1083     /**
    -1084      * Updates the data to be signed or verified by a string
    -1085      * @name updateString
    -1086      * @memberOf KJUR.crypto.Signature
    -1087      * @function
    -1088      * @param {String} str string to use for the update
    -1089      * @description
    -1090      * @example
    -1091      * sig.updateString('aaa')
    -1092      */
    -1093     this.updateString = function(str) {
    -1094 	throw "updateString(str) not supported for this alg:prov=" + this.algProvName;
    -1095     };
    -1096 
    -1097     /**
    -1098      * Updates the data to be signed or verified by a hexadecimal string
    -1099      * @name updateHex
    -1100      * @memberOf KJUR.crypto.Signature
    -1101      * @function
    -1102      * @param {String} hex hexadecimal string to use for the update
    -1103      * @description
    -1104      * @example
    -1105      * sig.updateHex('1f2f3f')
    -1106      */
    -1107     this.updateHex = function(hex) {
    -1108 	throw "updateHex(hex) not supported for this alg:prov=" + this.algProvName;
    -1109     };
    -1110 
    -1111     /**
    -1112      * Returns the signature bytes of all data updates as a hexadecimal string
    -1113      * @name sign
    -1114      * @memberOf KJUR.crypto.Signature
    -1115      * @function
    -1116      * @return the signature bytes as a hexadecimal string
    -1117      * @description
    -1118      * @example
    -1119      * var hSigValue = sig.sign()
    -1120      */
    -1121     this.sign = function() {
    -1122 	throw "sign() not supported for this alg:prov=" + this.algProvName;
    -1123     };
    -1124 
    -1125     /**
    -1126      * performs final update on the sign using string, then returns the signature bytes of all data updates as a hexadecimal string
    -1127      * @name signString
    -1128      * @memberOf KJUR.crypto.Signature
    -1129      * @function
    -1130      * @param {String} str string to final update
    -1131      * @return the signature bytes of a hexadecimal string
    -1132      * @description
    -1133      * @example
    -1134      * var hSigValue = sig.signString('aaa')
    -1135      */
    -1136     this.signString = function(str) {
    -1137 	throw "digestString(str) not supported for this alg:prov=" + this.algProvName;
    -1138     };
    -1139 
    -1140     /**
    -1141      * performs final update on the sign using hexadecimal string, then returns the signature bytes of all data updates as a hexadecimal string
    -1142      * @name signHex
    -1143      * @memberOf KJUR.crypto.Signature
    -1144      * @function
    -1145      * @param {String} hex hexadecimal string to final update
    -1146      * @return the signature bytes of a hexadecimal string
    -1147      * @description
    -1148      * @example
    -1149      * var hSigValue = sig.signHex('1fdc33')
    -1150      */
    -1151     this.signHex = function(hex) {
    -1152 	throw "digestHex(hex) not supported for this alg:prov=" + this.algProvName;
    -1153     };
    -1154 
    -1155     /**
    -1156      * verifies the passed-in signature.
    -1157      * @name verify
    -1158      * @memberOf KJUR.crypto.Signature
    -1159      * @function
    -1160      * @param {String} str string to final update
    -1161      * @return {Boolean} true if the signature was verified, otherwise false
    -1162      * @description
    -1163      * @example
    -1164      * var isValid = sig.verify('1fbcefdca4823a7(snip)')
    -1165      */
    -1166     this.verify = function(hSigVal) {
    -1167 	throw "verify(hSigVal) not supported for this alg:prov=" + this.algProvName;
    -1168     };
    -1169 
    -1170     this.initParams = params;
    -1171 
    -1172     if (params !== undefined) {
    -1173 	if (params['alg'] !== undefined) {
    -1174 	    this.algName = params['alg'];
    -1175 	    if (params['prov'] === undefined) {
    -1176 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
    -1177 	    } else {
    -1178 		this.provName = params['prov'];
    -1179 	    }
    -1180 	    this.algProvName = this.algName + ":" + this.provName;
    -1181 	    this.setAlgAndProvider(this.algName, this.provName);
    -1182 	    this._setAlgNames();
    -1183 	}
    -1184 
    -1185 	if (params['psssaltlen'] !== undefined) this.pssSaltLen = params['psssaltlen'];
    -1186 
    -1187 	if (params['prvkeypem'] !== undefined) {
    -1188 	    if (params['prvkeypas'] !== undefined) {
    -1189 		throw "both prvkeypem and prvkeypas parameters not supported";
    -1190 	    } else {
    -1191 		try {
    -1192 		    var prvKey = new RSAKey();
    -1193 		    prvKey.readPrivateKeyFromPEMString(params['prvkeypem']);
    -1194 		    this.initSign(prvKey);
    -1195 		} catch (ex) {
    -1196 		    throw "fatal error to load pem private key: " + ex;
    -1197 		}
    -1198 	    }
    -1199 	}
    -1200     }
    -1201 };
    -1202 
    -1203 /**
    -1204  * static object for cryptographic function utilities
    -1205  * @name KJUR.crypto.OID
    -1206  * @class static object for cryptography related OIDs
    -1207  * @property {Array} oidhex2name key value of hexadecimal OID and its name
    -1208  *           (ex. '2a8648ce3d030107' and 'secp256r1')
    -1209  * @since crypto 1.1.3
    -1210  * @description
    -1211  */
    -1212 
    +821     var matchResult = this.algName.match(/^(.+)with(.+)$/);
    +822 	if (matchResult) {
    +823 	    this.mdAlgName = matchResult[1].toLowerCase();
    +824 	    this.pubkeyAlgName = matchResult[2].toLowerCase();
    +825 	}
    +826     };
    +827 
    +828     this._zeroPaddingOfSignature = function(hex, bitLength) {
    +829 	var s = "";
    +830 	var nZero = bitLength / 4 - hex.length;
    +831 	for (var i = 0; i < nZero; i++) {
    +832 	    s = s + "0";
    +833 	}
    +834 	return s + hex;
    +835     };
    +836 
    +837     /**
    +838      * set signature algorithm and provider
    +839      * @name setAlgAndProvider
    +840      * @memberOf KJUR.crypto.Signature
    +841      * @function
    +842      * @param {String} alg signature algorithm name
    +843      * @param {String} prov provider name
    +844      * @description
    +845      * @example
    +846      * md.setAlgAndProvider('SHA1withRSA', 'cryptojs/jsrsa');
    +847      */
    +848     this.setAlgAndProvider = function(alg, prov) {
    +849 	this._setAlgNames();
    +850 	if (prov != 'cryptojs/jsrsa')
    +851 	    throw "provider not supported: " + prov;
    +852 
    +853 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(this.mdAlgName) != -1) {
    +854 	    try {
    +855 		this.md = new KJUR.crypto.MessageDigest({'alg':this.mdAlgName});
    +856 	    } catch (ex) {
    +857 		throw "setAlgAndProvider hash alg set fail alg=" +
    +858                       this.mdAlgName + "/" + ex;
    +859 	    }
    +860 
    +861 	    this.init = function(keyparam, pass) {
    +862 		var keyObj = null;
    +863 		try {
    +864 		    if (pass === undefined) {
    +865 			keyObj = KEYUTIL.getKey(keyparam);
    +866 		    } else {
    +867 			keyObj = KEYUTIL.getKey(keyparam, pass);
    +868 		    }
    +869 		} catch (ex) {
    +870 		    throw "init failed:" + ex;
    +871 		}
    +872 
    +873 		if (keyObj.isPrivate === true) {
    +874 		    this.prvKey = keyObj;
    +875 		    this.state = "SIGN";
    +876 		} else if (keyObj.isPublic === true) {
    +877 		    this.pubKey = keyObj;
    +878 		    this.state = "VERIFY";
    +879 		} else {
    +880 		    throw "init failed.:" + keyObj;
    +881 		}
    +882 	    };
    +883 
    +884 	    this.initSign = function(params) {
    +885 		if (typeof params['ecprvhex'] == 'string' &&
    +886                     typeof params['eccurvename'] == 'string') {
    +887 		    this.ecprvhex = params['ecprvhex'];
    +888 		    this.eccurvename = params['eccurvename'];
    +889 		} else {
    +890 		    this.prvKey = params;
    +891 		}
    +892 		this.state = "SIGN";
    +893 	    };
    +894 
    +895 	    this.initVerifyByPublicKey = function(params) {
    +896 		if (typeof params['ecpubhex'] == 'string' &&
    +897 		    typeof params['eccurvename'] == 'string') {
    +898 		    this.ecpubhex = params['ecpubhex'];
    +899 		    this.eccurvename = params['eccurvename'];
    +900 		} else if (params instanceof KJUR.crypto.ECDSA) {
    +901 		    this.pubKey = params;
    +902 		} else if (params instanceof RSAKey) {
    +903 		    this.pubKey = params;
    +904 		}
    +905 		this.state = "VERIFY";
    +906 	    };
    +907 
    +908 	    this.initVerifyByCertificatePEM = function(certPEM) {
    +909 		var x509 = new X509();
    +910 		x509.readCertPEM(certPEM);
    +911 		this.pubKey = x509.subjectPublicKeyRSA;
    +912 		this.state = "VERIFY";
    +913 	    };
    +914 
    +915 	    this.updateString = function(str) {
    +916 		this.md.updateString(str);
    +917 	    };
    +918 
    +919 	    this.updateHex = function(hex) {
    +920 		this.md.updateHex(hex);
    +921 	    };
    +922 
    +923 	    this.sign = function() {
    +924 		this.sHashHex = this.md.digest();
    +925 		if (typeof this.ecprvhex != "undefined" &&
    +926 		    typeof this.eccurvename != "undefined") {
    +927 		    var ec = new KJUR.crypto.ECDSA({'curve': this.eccurvename});
    +928 		    this.hSign = ec.signHex(this.sHashHex, this.ecprvhex);
    +929 		} else if (this.prvKey instanceof RSAKey &&
    +930 		           this.pubkeyAlgName == "rsaandmgf1") {
    +931 		    this.hSign = this.prvKey.signWithMessageHashPSS(this.sHashHex,
    +932 								    this.mdAlgName,
    +933 								    this.pssSaltLen);
    +934 		} else if (this.prvKey instanceof RSAKey &&
    +935 			   this.pubkeyAlgName == "rsa") {
    +936 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex,
    +937 								 this.mdAlgName);
    +938 		} else if (this.prvKey instanceof KJUR.crypto.ECDSA) {
    +939 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
    +940 		} else if (this.prvKey instanceof KJUR.crypto.DSA) {
    +941 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
    +942 		} else {
    +943 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
    +944 		}
    +945 		return this.hSign;
    +946 	    };
    +947 	    this.signString = function(str) {
    +948 		this.updateString(str);
    +949 		return this.sign();
    +950 	    };
    +951 	    this.signHex = function(hex) {
    +952 		this.updateHex(hex);
    +953 		return this.sign();
    +954 	    };
    +955 	    this.verify = function(hSigVal) {
    +956 	        this.sHashHex = this.md.digest();
    +957 		if (typeof this.ecpubhex != "undefined" &&
    +958 		    typeof this.eccurvename != "undefined") {
    +959 		    var ec = new KJUR.crypto.ECDSA({curve: this.eccurvename});
    +960 		    return ec.verifyHex(this.sHashHex, hSigVal, this.ecpubhex);
    +961 		} else if (this.pubKey instanceof RSAKey &&
    +962 			   this.pubkeyAlgName == "rsaandmgf1") {
    +963 		    return this.pubKey.verifyWithMessageHashPSS(this.sHashHex, hSigVal, 
    +964 								this.mdAlgName,
    +965 								this.pssSaltLen);
    +966 		} else if (this.pubKey instanceof RSAKey &&
    +967 			   this.pubkeyAlgName == "rsa") {
    +968 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
    +969 		} else if (this.pubKey instanceof KJUR.crypto.ECDSA) {
    +970 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
    +971 		} else if (this.pubKey instanceof KJUR.crypto.DSA) {
    +972 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
    +973 		} else {
    +974 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
    +975 		}
    +976 	    };
    +977 	}
    +978     };
    +979 
    +980     /**
    +981      * Initialize this object for signing or verifying depends on key
    +982      * @name init
    +983      * @memberOf KJUR.crypto.Signature
    +984      * @function
    +985      * @param {Object} key specifying public or private key as plain/encrypted PKCS#5/8 PEM file, certificate PEM or {@link RSAKey}, {@link KJUR.crypto.DSA} or {@link KJUR.crypto.ECDSA} object
    +986      * @param {String} pass (OPTION) passcode for encrypted private key
    +987      * @since crypto 1.1.3
    +988      * @description
    +989      * This method is very useful initialize method for Signature class since
    +990      * you just specify key then this method will automatically initialize it
    +991      * using {@link KEYUTIL.getKey} method.
    +992      * As for 'key',  following argument type are supported:
    +993      * <h5>signing</h5>
    +994      * <ul>
    +995      * <li>PEM formatted PKCS#8 encrypted RSA/ECDSA private key concluding "BEGIN ENCRYPTED PRIVATE KEY"</li>
    +996      * <li>PEM formatted PKCS#5 encrypted RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" and ",ENCRYPTED"</li>
    +997      * <li>PEM formatted PKCS#8 plain RSA/ECDSA private key concluding "BEGIN PRIVATE KEY"</li>
    +998      * <li>PEM formatted PKCS#5 plain RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" without ",ENCRYPTED"</li>
    +999      * <li>RSAKey object of private key</li>
    +1000      * <li>KJUR.crypto.ECDSA object of private key</li>
    +1001      * <li>KJUR.crypto.DSA object of private key</li>
    +1002      * </ul>
    +1003      * <h5>verification</h5>
    +1004      * <ul>
    +1005      * <li>PEM formatted PKCS#8 RSA/EC/DSA public key concluding "BEGIN PUBLIC KEY"</li>
    +1006      * <li>PEM formatted X.509 certificate with RSA/EC/DSA public key concluding
    +1007      *     "BEGIN CERTIFICATE", "BEGIN X509 CERTIFICATE" or "BEGIN TRUSTED CERTIFICATE".</li>
    +1008      * <li>RSAKey object of public key</li>
    +1009      * <li>KJUR.crypto.ECDSA object of public key</li>
    +1010      * <li>KJUR.crypto.DSA object of public key</li>
    +1011      * </ul>
    +1012      * @example
    +1013      * sig.init(sCertPEM)
    +1014      */
    +1015     this.init = function(key, pass) {
    +1016 	throw "init(key, pass) not supported for this alg:prov=" +
    +1017 	      this.algProvName;
    +1018     };
    +1019 
    +1020     /**
    +1021      * Initialize this object for verifying with a public key
    +1022      * @name initVerifyByPublicKey
    +1023      * @memberOf KJUR.crypto.Signature
    +1024      * @function
    +1025      * @param {Object} param RSAKey object of public key or associative array for ECDSA
    +1026      * @since 1.0.2
    +1027      * @deprecated from crypto 1.1.5. please use init() method instead.
    +1028      * @description
    +1029      * Public key information will be provided as 'param' parameter and the value will be
    +1030      * following:
    +1031      * <ul>
    +1032      * <li>{@link RSAKey} object for RSA verification</li>
    +1033      * <li>associative array for ECDSA verification
    +1034      *     (ex. <code>{'ecpubhex': '041f..', 'eccurvename': 'secp256r1'}</code>)
    +1035      * </li>
    +1036      * </ul>
    +1037      * @example
    +1038      * sig.initVerifyByPublicKey(rsaPrvKey)
    +1039      */
    +1040     this.initVerifyByPublicKey = function(rsaPubKey) {
    +1041 	throw "initVerifyByPublicKey(rsaPubKeyy) not supported for this alg:prov=" +
    +1042 	      this.algProvName;
    +1043     };
    +1044 
    +1045     /**
    +1046      * Initialize this object for verifying with a certficate
    +1047      * @name initVerifyByCertificatePEM
    +1048      * @memberOf KJUR.crypto.Signature
    +1049      * @function
    +1050      * @param {String} certPEM PEM formatted string of certificate
    +1051      * @since 1.0.2
    +1052      * @deprecated from crypto 1.1.5. please use init() method instead.
    +1053      * @description
    +1054      * @example
    +1055      * sig.initVerifyByCertificatePEM(certPEM)
    +1056      */
    +1057     this.initVerifyByCertificatePEM = function(certPEM) {
    +1058 	throw "initVerifyByCertificatePEM(certPEM) not supported for this alg:prov=" +
    +1059 	    this.algProvName;
    +1060     };
    +1061 
    +1062     /**
    +1063      * Initialize this object for signing
    +1064      * @name initSign
    +1065      * @memberOf KJUR.crypto.Signature
    +1066      * @function
    +1067      * @param {Object} param RSAKey object of public key or associative array for ECDSA
    +1068      * @deprecated from crypto 1.1.5. please use init() method instead.
    +1069      * @description
    +1070      * Private key information will be provided as 'param' parameter and the value will be
    +1071      * following:
    +1072      * <ul>
    +1073      * <li>{@link RSAKey} object for RSA signing</li>
    +1074      * <li>associative array for ECDSA signing
    +1075      *     (ex. <code>{'ecprvhex': '1d3f..', 'eccurvename': 'secp256r1'}</code>)</li>
    +1076      * </ul>
    +1077      * @example
    +1078      * sig.initSign(prvKey)
    +1079      */
    +1080     this.initSign = function(prvKey) {
    +1081 	throw "initSign(prvKey) not supported for this alg:prov=" + this.algProvName;
    +1082     };
    +1083 
    +1084     /**
    +1085      * Updates the data to be signed or verified by a string
    +1086      * @name updateString
    +1087      * @memberOf KJUR.crypto.Signature
    +1088      * @function
    +1089      * @param {String} str string to use for the update
    +1090      * @description
    +1091      * @example
    +1092      * sig.updateString('aaa')
    +1093      */
    +1094     this.updateString = function(str) {
    +1095 	throw "updateString(str) not supported for this alg:prov=" + this.algProvName;
    +1096     };
    +1097 
    +1098     /**
    +1099      * Updates the data to be signed or verified by a hexadecimal string
    +1100      * @name updateHex
    +1101      * @memberOf KJUR.crypto.Signature
    +1102      * @function
    +1103      * @param {String} hex hexadecimal string to use for the update
    +1104      * @description
    +1105      * @example
    +1106      * sig.updateHex('1f2f3f')
    +1107      */
    +1108     this.updateHex = function(hex) {
    +1109 	throw "updateHex(hex) not supported for this alg:prov=" + this.algProvName;
    +1110     };
    +1111 
    +1112     /**
    +1113      * Returns the signature bytes of all data updates as a hexadecimal string
    +1114      * @name sign
    +1115      * @memberOf KJUR.crypto.Signature
    +1116      * @function
    +1117      * @return the signature bytes as a hexadecimal string
    +1118      * @description
    +1119      * @example
    +1120      * var hSigValue = sig.sign()
    +1121      */
    +1122     this.sign = function() {
    +1123 	throw "sign() not supported for this alg:prov=" + this.algProvName;
    +1124     };
    +1125 
    +1126     /**
    +1127      * performs final update on the sign using string, then returns the signature bytes of all data updates as a hexadecimal string
    +1128      * @name signString
    +1129      * @memberOf KJUR.crypto.Signature
    +1130      * @function
    +1131      * @param {String} str string to final update
    +1132      * @return the signature bytes of a hexadecimal string
    +1133      * @description
    +1134      * @example
    +1135      * var hSigValue = sig.signString('aaa')
    +1136      */
    +1137     this.signString = function(str) {
    +1138 	throw "digestString(str) not supported for this alg:prov=" + this.algProvName;
    +1139     };
    +1140 
    +1141     /**
    +1142      * performs final update on the sign using hexadecimal string, then returns the signature bytes of all data updates as a hexadecimal string
    +1143      * @name signHex
    +1144      * @memberOf KJUR.crypto.Signature
    +1145      * @function
    +1146      * @param {String} hex hexadecimal string to final update
    +1147      * @return the signature bytes of a hexadecimal string
    +1148      * @description
    +1149      * @example
    +1150      * var hSigValue = sig.signHex('1fdc33')
    +1151      */
    +1152     this.signHex = function(hex) {
    +1153 	throw "digestHex(hex) not supported for this alg:prov=" + this.algProvName;
    +1154     };
    +1155 
    +1156     /**
    +1157      * verifies the passed-in signature.
    +1158      * @name verify
    +1159      * @memberOf KJUR.crypto.Signature
    +1160      * @function
    +1161      * @param {String} str string to final update
    +1162      * @return {Boolean} true if the signature was verified, otherwise false
    +1163      * @description
    +1164      * @example
    +1165      * var isValid = sig.verify('1fbcefdca4823a7(snip)')
    +1166      */
    +1167     this.verify = function(hSigVal) {
    +1168 	throw "verify(hSigVal) not supported for this alg:prov=" + this.algProvName;
    +1169     };
    +1170 
    +1171     this.initParams = params;
    +1172 
    +1173     if (params !== undefined) {
    +1174 	if (params['alg'] !== undefined) {
    +1175 	    this.algName = params['alg'];
    +1176 	    if (params['prov'] === undefined) {
    +1177 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
    +1178 	    } else {
    +1179 		this.provName = params['prov'];
    +1180 	    }
    +1181 	    this.algProvName = this.algName + ":" + this.provName;
    +1182 	    this.setAlgAndProvider(this.algName, this.provName);
    +1183 	    this._setAlgNames();
    +1184 	}
    +1185 
    +1186 	if (params['psssaltlen'] !== undefined) this.pssSaltLen = params['psssaltlen'];
    +1187 
    +1188 	if (params['prvkeypem'] !== undefined) {
    +1189 	    if (params['prvkeypas'] !== undefined) {
    +1190 		throw "both prvkeypem and prvkeypas parameters not supported";
    +1191 	    } else {
    +1192 		try {
    +1193 		    var prvKey = new RSAKey();
    +1194 		    prvKey.readPrivateKeyFromPEMString(params['prvkeypem']);
    +1195 		    this.initSign(prvKey);
    +1196 		} catch (ex) {
    +1197 		    throw "fatal error to load pem private key: " + ex;
    +1198 		}
    +1199 	    }
    +1200 	}
    +1201     }
    +1202 };
    +1203 
    +1204 /**
    +1205  * static object for cryptographic function utilities
    +1206  * @name KJUR.crypto.OID
    +1207  * @class static object for cryptography related OIDs
    +1208  * @property {Array} oidhex2name key value of hexadecimal OID and its name
    +1209  *           (ex. '2a8648ce3d030107' and 'secp256r1')
    +1210  * @since crypto 1.1.3
    +1211  * @description
    +1212  */
     1213 
    -1214 KJUR.crypto.OID = new function() {
    -1215     this.oidhex2name = {
    -1216 	'2a864886f70d010101': 'rsaEncryption',
    -1217 	'2a8648ce3d0201': 'ecPublicKey',
    -1218 	'2a8648ce380401': 'dsa',
    -1219 	'2a8648ce3d030107': 'secp256r1',
    -1220 	'2b8104001f': 'secp192k1',
    -1221 	'2b81040021': 'secp224r1',
    -1222 	'2b8104000a': 'secp256k1',
    -1223 	'2b81040023': 'secp521r1',
    -1224 	'2b81040022': 'secp384r1',
    -1225 	'2a8648ce380403': 'SHA1withDSA', // 1.2.840.10040.4.3
    -1226 	'608648016503040301': 'SHA224withDSA', // 2.16.840.1.101.3.4.3.1
    -1227 	'608648016503040302': 'SHA256withDSA', // 2.16.840.1.101.3.4.3.2
    -1228     };
    -1229 };
    -1230 
    \ No newline at end of file +1214 +1215
    KJUR.crypto.OID = new function() { +1216 this.oidhex2name = { +1217 '2a864886f70d010101': 'rsaEncryption', +1218 '2a8648ce3d0201': 'ecPublicKey', +1219 '2a8648ce380401': 'dsa', +1220 '2a8648ce3d030107': 'secp256r1', +1221 '2b8104001f': 'secp192k1', +1222 '2b81040021': 'secp224r1', +1223 '2b8104000a': 'secp256k1', +1224 '2b81040023': 'secp521r1', +1225 '2b81040022': 'secp384r1', +1226 '2a8648ce380403': 'SHA1withDSA', // 1.2.840.10040.4.3 +1227 '608648016503040301': 'SHA224withDSA', // 2.16.840.1.101.3.4.3.1 +1228 '608648016503040302': 'SHA256withDSA', // 2.16.840.1.101.3.4.3.2 +1229 }; +1230 }; +1231
    \ No newline at end of file diff --git a/api/symbols/src/jws-3.3.js.html b/api/symbols/src/jws-3.3.js.html index c7288d5a..b87c4296 100644 --- a/api/symbols/src/jws-3.3.js.html +++ b/api/symbols/src/jws-3.3.js.html @@ -5,7 +5,7 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! jws-3.3.4 (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! jws-3.3.5 (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
       2  */
       3 /*
       4  * jws.js - JSON Web Signature(JWS) and JSON Web Token(JWT) Class
    @@ -25,7 +25,7 @@
      18  * @fileOverview
      19  * @name jws-3.3.js
      20  * @author Kenji Urushima kenji.urushima@gmail.com
    - 21  * @version 3.3.4 (2016-May-17)
    + 21  * @version 3.3.5 (2016-Oct-08)
      22  * @since jsjws 1.0, jsrsasign 4.8.0
      23  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      24  */
    @@ -141,949 +141,951 @@
     134 	    (sigValNotNeeded || (this.parsedJWS.sigvalH !== undefined))) {
     135 	    return;
     136 	}
    -137 	if (sJWS.match(/^([^.]+)\.([^.]+)\.([^.]+)$/) == null) {
    -138 	    throw "JWS signature is not a form of 'Head.Payload.SigValue'.";
    -139 	}
    -140 	var b6Head = RegExp.$1;
    -141 	var b6Payload = RegExp.$2;
    -142 	var b6SigVal = RegExp.$3;
    -143 	var sSI = b6Head + "." + b6Payload;
    -144 	this.parsedJWS = {};
    -145 	this.parsedJWS.headB64U = b6Head;
    -146 	this.parsedJWS.payloadB64U = b6Payload;
    -147 	this.parsedJWS.sigvalB64U = b6SigVal;
    -148 	this.parsedJWS.si = sSI;
    -149 
    -150 	if (!sigValNotNeeded) {
    -151 	    var hSigVal = b64utohex(b6SigVal);
    -152 	    var biSigVal = parseBigInt(hSigVal, 16);
    -153 	    this.parsedJWS.sigvalH = hSigVal;
    -154 	    this.parsedJWS.sigvalBI = biSigVal;
    -155 	}
    -156 
    -157 	var sHead = b64utoutf8(b6Head);
    -158 	var sPayload = b64utoutf8(b6Payload);
    -159 	this.parsedJWS.headS = sHead;
    -160 	this.parsedJWS.payloadS = sPayload;
    -161 
    -162 	if (! ns1.isSafeJSONString(sHead, this.parsedJWS, 'headP'))
    -163 	    throw "malformed JSON string for JWS Head: " + sHead;
    -164     };
    -165 };
    -166 
    -167 // === major static method ========================================================
    -168 
    -169 /**
    -170  * generate JWS signature by specified key<br/>
    -171  * @name sign
    -172  * @memberOf KJUR.jws.JWS
    -173  * @function
    -174  * @static
    -175  * @param {String} alg JWS algorithm name to sign and force set to sHead or null 
    -176  * @param {String} spHead string or object of JWS Header
    -177  * @param {String} spPayload string or object of JWS Payload
    -178  * @param {String} key string of private key or mac key object to sign
    -179  * @param {String} pass (OPTION)passcode to use encrypted asymmetric private key 
    -180  * @return {String} JWS signature string
    -181  * @since jws 3.0.0
    -182  * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Signature.html">jsrsasign KJUR.crypto.Signature method</a>
    -183  * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Mac.html">jsrsasign KJUR.crypto.Mac method</a>
    -184  * @description
    -185  * This method supports following algorithms.
    -186  * <table>
    -187  * <tr><th>alg value</th><th>spec requirement</th><th>jsjws support</th></tr>
    -188  * <tr><td>HS256</td><td>REQUIRED</td><td>SUPPORTED</td></tr>
    -189  * <tr><td>HS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -190  * <tr><td>HS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -191  * <tr><td>RS256</td><td>RECOMMENDED</td><td>SUPPORTED</td></tr>
    -192  * <tr><td>RS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -193  * <tr><td>RS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -194  * <tr><td>ES256</td><td>RECOMMENDED+</td><td>SUPPORTED</td></tr>
    -195  * <tr><td>ES384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -196  * <tr><td>ES512</td><td>OPTIONAL</td><td>-</td></tr>
    -197  * <tr><td>PS256</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -198  * <tr><td>PS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -199  * <tr><td>PS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr>
    -200  * <tr><td>none</td><td>REQUIRED</td><td>SUPPORTED(signature generation only)</td></tr>
    -201  * </table>
    -202  * <dl>
    -203  * <dt>NOTE1:
    -204  * <dd>salt length of RSAPSS signature is the same as the hash algorithm length
    -205  * because of <a href="http://www.ietf.org/mail-archive/web/jose/current/msg02901.html">IETF JOSE ML discussion</a>.
    -206  * <dt>NOTE2:
    -207  * <dd>To support HS384, patched version of CryptoJS is used.
    -208  * <a href="https://code.google.com/p/crypto-js/issues/detail?id=84">See here for detail</a>.
    -209  * <dt>NOTE3:
    -210  * From jsrsasign 4.10.0 jws 3.3.0, Way to provide password
    -211  * for HS* algorithm is changed. The 'key' attribute value is
    -212  * passed to {@link KJUR.crypto.Mac.setPassword} so please see
    -213  * {@link KJUR.crypto.Mac.setPassword} for detail.
    -214  * As for backword compatibility, if key is a string, has even length and
    -215  * 0..9, A-F or a-f characters, key string is treated as a hexadecimal
    -216  * otherwise it is treated as a raw string.
    -217  * <dd>
    -218  * </dl>
    -219  * <b>EXAMPLE</b><br/>
    -220  * @example
    -221  * // sign HS256 signature with password "aaa" implicitly handled as string
    -222  * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256", cty: "JWT"}, {age: 21}, "aaa");
    -223  * // sign HS256 signature with password "6161" implicitly handled as hex
    -224  * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256", cty: "JWT"}, {age: 21}, "6161");
    -225  * // sign HS256 signature with base64 password
    -226  * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256"}, {age: 21}, {b64: "Mi/8..a="});
    -227  * // sign RS256 signature with PKCS#8 PEM RSA private key
    -228  * sJWS = KJUR.jws.JWS.sign(null, {alg: "RS256"}, {age: 21}, "-----BEGIN PRIVATE KEY...");
    -229  * // sign RS256 signature with PKCS#8 PEM ECC private key with passcode
    -230  * sJWS = KJUR.jws.JWS.sign(null, {alg: "ES256"}, {age: 21}, 
    -231  *                          "-----BEGIN PRIVATE KEY...", "keypass");
    -232  * // header and payload can be passed by both string and object
    -233  * sJWS = KJUR.jws.JWS.sign(null, '{alg:"HS256",cty:"JWT"}', '{age:21}', "aaa");
    -234  */
    -235 KJUR.jws.JWS.sign = function(alg, spHeader, spPayload, key, pass) {
    -236     var ns1 = KJUR.jws.JWS;
    -237     var sHeader, pHeader, sPayload;
    -238 
    -239     // 1. check signatureInput(Header, Payload) is string or object
    -240     if (typeof spHeader != 'string' && typeof spHeader != 'object')
    -241 	throw "spHeader must be JSON string or object: " + spHeader;
    -242 
    -243     if (typeof spHeader == 'object') {
    -244 	pHeader = spHeader;
    -245 	sHeader = JSON.stringify(pHeader);
    -246     }
    -247 
    -248     if (typeof spHeader == 'string') {
    -249 	sHeader = spHeader;
    -250 	if (! ns1.isSafeJSONString(sHeader))
    -251 	    throw "JWS Head is not safe JSON string: " + sHeader;
    -252 	pHeader = ns1.readSafeJSONString(sHeader);
    -253 
    -254     }
    -255 
    -256     sPayload = spPayload;
    -257     if (typeof spPayload == 'object') sPayload = JSON.stringify(spPayload);
    -258 
    -259     // 2. use alg if defined in sHeader
    -260     if ((alg == '' || alg == null) &&
    -261 	pHeader['alg'] !== undefined) {
    -262 	alg = pHeader['alg'];
    -263     }
    -264 
    -265     // 3. update sHeader to add alg if alg undefined
    -266     if ((alg != '' && alg != null) &&
    -267 	pHeader['alg'] === undefined) {
    -268 	pHeader['alg'] = alg;
    -269 	sHeader = JSON.stringify(pHeader);
    -270     }
    -271 
    -272     // 4. check explicit algorithm doesn't match with JWS header.
    -273     if (alg !== pHeader.alg)
    -274 	throw "alg and sHeader.alg doesn't match: " + alg + "!=" + pHeader.alg;
    -275 
    -276     // 5. set signature algorithm like SHA1withRSA
    -277     var sigAlg = null;
    -278     if (ns1.jwsalg2sigalg[alg] === undefined) {
    -279 	throw "unsupported alg name: " + alg;
    -280     } else {
    -281 	sigAlg = ns1.jwsalg2sigalg[alg];
    -282     }
    -283     
    -284     var uHeader = utf8tob64u(sHeader);
    -285     var uPayload = utf8tob64u(sPayload);
    -286     var uSignatureInput = uHeader + "." + uPayload
    -287     // 6. sign
    -288     var hSig = "";
    -289     if (sigAlg.substr(0, 4) == "Hmac") {
    -290 	if (key === undefined)
    -291 	    throw "mac key shall be specified for HS* alg";
    -292 	//alert("sigAlg=" + sigAlg);
    -293 	var mac = new KJUR.crypto.Mac({'alg': sigAlg, 'prov': 'cryptojs', 'pass': key});
    -294 	mac.updateString(uSignatureInput);
    -295 	hSig = mac.doFinal();
    -296     } else if (sigAlg.indexOf("withECDSA") != -1) {
    -297 	var sig = new KJUR.crypto.Signature({'alg': sigAlg});
    -298 	sig.init(key, pass);
    -299 	sig.updateString(uSignatureInput);
    -300 	hASN1Sig = sig.sign();
    -301 	hSig = KJUR.crypto.ECDSA.asn1SigToConcatSig(hASN1Sig);
    -302     } else if (sigAlg != "none") {
    -303 	var sig = new KJUR.crypto.Signature({'alg': sigAlg});
    -304 	sig.init(key, pass);
    -305 	sig.updateString(uSignatureInput);
    -306 	hSig = sig.sign();
    -307     }
    -308 
    -309     var uSig = hextob64u(hSig);
    -310     return uSignatureInput + "." + uSig;
    -311 };
    -312 
    -313 /**
    -314  * verify JWS signature by specified key or certificate<br/>
    -315  * @name verify
    -316  * @memberOf KJUR.jws.JWS
    -317  * @function
    -318  * @static
    -319  * @param {String} sJWS string of JWS signature to verify
    -320  * @param {Object} key string of public key, certificate or key object to verify
    -321  * @param {String} acceptAlgs array of algorithm name strings (OPTION)
    -322  * @return {Boolean} true if the signature is valid otherwise false
    -323  * @since jws 3.0.0
    -324  * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Signature.html">jsrsasign KJUR.crypto.Signature method</a>
    -325  * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Mac.html">jsrsasign KJUR.crypto.Mac method</a>
    -326  * @description
    -327  * <p>
    -328  * This method verifies a JSON Web Signature Compact Serialization string by the validation 
    -329  * algorithm as described in 
    -330  * <a href="http://self-issued.info/docs/draft-jones-json-web-signature-04.html#anchor5">
    -331  * the section 5 of Internet Draft draft-jones-json-web-signature-04.</a>
    -332  * </p>
    -333  * <p>
    -334  * Since 3.2.0 strict key checking has been provided against a JWS algorithm
    -335  * in a JWS header.
    -336  * <ul>
    -337  * <li>In case 'alg' is 'HS*' in the JWS header,
    -338  * 'key' shall be hexadecimal string for Hmac{256,384,512} shared secret key.
    -339  * Otherwise it raise an error.</li>
    -340  * <li>In case 'alg' is 'RS*' or 'PS*' in the JWS header,
    -341  * 'key' shall be a RSAKey object or a PEM string of
    -342  * X.509 RSA public key certificate or PKCS#8 RSA public key.
    -343  * Otherwise it raise an error.</li>
    -344  * <li>In case 'alg' is 'ES*' in the JWS header,
    -345  * 'key' shall be a KJUR.crypto.ECDSA object or a PEM string of
    -346  * X.509 ECC public key certificate or PKCS#8 ECC public key.
    -347  * Otherwise it raise an error.</li>
    -348  * <li>In case 'alg' is 'none' in the JWS header,
    -349  * validation not supported after jsjws 3.1.0.</li>
    -350  * </ul>
    -351  * </p>
    -352  * <p>
    -353  * NOTE1: The argument 'acceptAlgs' is supported since 3.2.0.
    -354  * Strongly recommended to provide acceptAlgs to mitigate
    -355  * signature replacement attacks.<br/>
    -356  * </p>
    -357  * <p>
    -358  * NOTE2: From jsrsasign 4.9.0 jws 3.2.5, Way to provide password
    -359  * for HS* algorithm is changed. The 'key' attribute value is
    -360  * passed to {@link KJUR.crypto.Mac.setPassword} so please see
    -361  * {@link KJUR.crypto.Mac.setPassword} for detail.
    -362  * As for backword compatibility, if key is a string, has even length and
    -363  * 0..9, A-F or a-f characters, key string is treated as a hexadecimal
    -364  * otherwise it is treated as a raw string.
    -365  * </p>
    -366  * @example
    -367  * // 1) verify a RS256 JWS signature by a certificate string.
    -368  * isValid = KJUR.jws.JWS.verify('eyJh...', '-----BEGIN...', ['RS256']);
    -369  * 
    -370  * // 2) verify a HS256 JWS signature by a certificate string.
    -371  * isValid = KJUR.jws.JWS.verify('eyJh...', {hex: '6f62ad...'}, ['HS256']);
    -372  * isValid = KJUR.jws.JWS.verify('eyJh...', {b64: 'Mi/ab8...a=='}, ['HS256']);
    -373  * isValid = KJUR.jws.JWS.verify('eyJh...', {utf8: 'Secret秘密'}, ['HS256']);
    -374  * isValid = KJUR.jws.JWS.verify('eyJh...', '6f62ad', ['HS256']); // implicit hex
    -375  * isValid = KJUR.jws.JWS.verify('eyJh...', '6f62ada', ['HS256']); // implicit raw string
    -376  *
    -377  * // 3) verify a ES256 JWS signature by a KJUR.crypto.ECDSA key object.
    -378  * var pubkey = KEYUTIL.getKey('-----BEGIN CERT...');
    -379  * var isValid = KJUR.jws.JWS.verify('eyJh...', pubkey);
    -380  */
    -381 KJUR.jws.JWS.verify = function(sJWS, key, acceptAlgs) {
    -382     var jws = KJUR.jws.JWS;
    -383     var a = sJWS.split(".");
    -384     var uHeader = a[0];
    -385     var uPayload = a[1];
    -386     var uSignatureInput = uHeader + "." + uPayload;
    -387     var hSig = b64utohex(a[2]);
    -388 
    -389     // 1. parse JWS header
    -390     var pHeader = jws.readSafeJSONString(b64utoutf8(a[0]));
    -391     var alg = null;
    -392     var algType = null; // HS|RS|PS|ES|no
    -393     if (pHeader.alg === undefined) {
    -394 	throw "algorithm not specified in header";
    -395     } else {
    -396 	alg = pHeader.alg;
    -397 	algType = alg.substr(0, 2);
    -398     }
    -399 
    -400     // 2. check whether alg is acceptable algorithms
    -401     if (acceptAlgs != null &&
    -402         Object.prototype.toString.call(acceptAlgs) === '[object Array]' &&
    -403         acceptAlgs.length > 0) {
    -404 	var acceptAlgStr = ":" + acceptAlgs.join(":") + ":";
    -405 	if (acceptAlgStr.indexOf(":" + alg + ":") == -1) {
    -406 	    throw "algorithm '" + alg + "' not accepted in the list";
    -407 	}
    -408     }
    -409 
    -410     // 3. check whether key is a proper key for alg.
    -411     if (alg != "none" && key === null) {
    -412 	throw "key shall be specified to verify.";
    -413     }
    -414 
    -415     // 3.1. There is no key check for HS* because Mac will check it.
    -416     //      since jsrsasign 5.0.0.
    -417 
    -418     // 3.2. convert key object if key is a public key or cert PEM string
    -419     if (typeof key == "string" &&
    -420 	key.indexOf("-----BEGIN ") != -1) {
    -421 	key = KEYUTIL.getKey(key);
    -422     }
    -423 
    -424     // 3.3. check whether key is RSAKey obj if alg is RS* or PS*.
    -425     if (algType == "RS" || algType == "PS") {
    -426 	if (!(key instanceof RSAKey)) {
    -427 	    throw "key shall be a RSAKey obj for RS* and PS* algs";
    -428 	}
    -429     }
    -430 
    -431     // 3.4. check whether key is ECDSA obj if alg is ES*.
    -432     if (algType == "ES") {
    -433 	if (!(key instanceof KJUR.crypto.ECDSA)) {
    -434 	    throw "key shall be a ECDSA obj for ES* algs";
    -435 	}
    -436     }
    -437 
    -438     // 3.5. check when alg is 'none'
    -439     if (alg == "none") {
    -440     }
    -441 
    -442     // 4. check whether alg is supported alg in jsjws.
    -443     var sigAlg = null;
    -444     if (jws.jwsalg2sigalg[pHeader.alg] === undefined) {
    -445 	throw "unsupported alg name: " + alg;
    -446     } else {
    -447 	sigAlg = jws.jwsalg2sigalg[alg];
    -448     }
    -449 
    -450     // 5. verify
    -451     if (sigAlg == "none") {
    -452         throw "not supported";
    -453     } else if (sigAlg.substr(0, 4) == "Hmac") {
    -454 	var hSig2 = null;
    -455 	if (key === undefined)
    -456 	    throw "hexadecimal key shall be specified for HMAC";
    -457 	//try {
    -458 	    var mac = new KJUR.crypto.Mac({'alg': sigAlg, 'pass': key});
    -459 	    mac.updateString(uSignatureInput);
    -460 	    hSig2 = mac.doFinal();
    -461 	//} catch(ex) {};
    -462 	return hSig == hSig2;
    -463     } else if (sigAlg.indexOf("withECDSA") != -1) {
    -464 	var hASN1Sig = null;
    -465         try {
    -466 	    hASN1Sig = KJUR.crypto.ECDSA.concatSigToASN1Sig(hSig);
    -467 	} catch (ex) {
    -468 	    return false;
    -469 	}
    -470 	var sig = new KJUR.crypto.Signature({'alg': sigAlg});
    -471 	sig.init(key)
    -472 	sig.updateString(uSignatureInput);
    -473 	return sig.verify(hASN1Sig);
    -474     } else {
    -475 	var sig = new KJUR.crypto.Signature({'alg': sigAlg});
    -476 	sig.init(key)
    -477 	sig.updateString(uSignatureInput);
    -478 	return sig.verify(hSig);
    -479     }
    -480 };
    -481 
    -482 /**
    -483  * parse header and payload of JWS signature<br/>
    -484  * @name parse
    -485  * @memberOf KJUR.jws.JWS
    -486  * @function
    -487  * @static
    -488  * @param {String} sJWS string of JWS signature to parse
    -489  * @return {Array} associative array of parsed header and payload. See below.
    -490  * @throws if sJWS is malformed JWS signature
    -491  * @since jws 3.3.3
    -492  * @description
    -493  * This method parses JWS signature string. 
    -494  * Resulted associative array has following properties:
    -495  * <ul>
    -496  * <li>headerObj - JSON object of header</li>
    -497  * <li>payloadObj - JSON object of payload if payload is JSON string otherwise undefined</li>
    -498  * <li>headerPP - pretty printed JSON header by stringify</li>
    -499  * <li>payloadPP - pretty printed JSON payload by stringify if payload is JSON otherwise Base64URL decoded raw string of payload</li>
    -500  * <li>sigHex - hexadecimal string of signature</li>
    -501  * </ul>
    -502  * @example
    -503  * KJUR.jws.JWS.parse(sJWS) ->
    -504  * { 
    -505  *   headerObj: {"alg": "RS256", "typ": "JWS"},
    -506  *   payloadObj: {"product": "orange", "quantity": 100},
    -507  *   headerPP: 
    -508  *   '{
    -509  *     "alg": "RS256",
    -510  *     "typ": "JWS"
    -511  *   }',
    -512  *   payloadPP: 
    -513  *   '{
    -514  *     "product": "orange",
    -515  *     "quantity": 100
    -516  *   }',
    -517  *   sigHex: "91f3cd..." 
    -518  * }
    -519  */
    -520 KJUR.jws.JWS.parse = function(sJWS) {
    -521     var a = sJWS.split(".");
    -522     var result = {};
    -523     var uHeader, uPayload, uSig;
    -524     if (a.length != 2 && a.length != 3)
    -525 	throw "malformed sJWS: wrong number of '.' splitted elements";
    -526 
    -527     uHeader = a[0];
    -528     uPayload = a[1];
    -529     if (a.length == 3) uSig = a[2]; 
    -530 
    -531     result.headerObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(uHeader));
    -532     result.payloadObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(uPayload));
    -533 
    -534     result.headerPP = JSON.stringify(result.headerObj, null, "  ");
    -535     if (result.payloadObj == null) {
    -536 	result.payloadPP = b64utoutf8(uPayload);
    -537     } else {
    -538 	result.payloadPP = JSON.stringify(result.payloadObj, null, "  ");
    -539     }
    -540 
    -541     if (uSig !== undefined) {
    -542 	result.sigHex = b64utohex(uSig);
    -543     }
    -544 
    -545     return result;
    -546 };
    -547 
    -548 /**
    -549  * @name verifyJWT
    -550  * @memberOf KJUR.jws.JWS
    -551  * @function
    -552  * @static
    -553  * @param {String} sJWT string of JSON Web Token(JWT) to verify
    -554  * @param {Object} key string of public key, certificate or key object to verify
    -555  * @param {Array} acceptField associative array of acceptable fields (OPTION)
    -556  * @return {Boolean} true if the JWT token is valid otherwise false
    -557  * @since jws 3.2.3 jsrsasign 4.8.0
    -558  *
    -559  * @description
    -560  * This method verifies a
    -561  * <a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a> 
    -562  * JSON Web Token(JWT).
    -563  * It will verify following:
    -564  * <ul>
    -565  * <li>Header.alg
    -566  * <ul>
    -567  * <li>alg is specified in JWT header.</li>
    -568  * <li>alg is included in acceptField.alg array. (MANDATORY)</li>
    -569  * <li>alg is proper for key.</li>
    -570  * </ul>
    -571  * </li>
    -572  * <li>Payload.iss (issuer) - Payload.iss is included in acceptField.iss array if specified. (OPTION)</li>
    -573  * <li>Payload.sub (subject) - Payload.sub is included in acceptField.sub array if specified. (OPTION)</li>
    -574  * <li>Payload.aud (audience) - Payload.aud is included in acceptField.aud array or 
    -575  *     the same as value if specified. (OPTION)</li>
    -576  * <li>Time validity
    -577  * <ul>
    -578  * <li>
    -579  * If acceptField.verifyAt as number of UNIX origin time is specifed for validation time, 
    -580  * this method will verify at the time for it, otherwise current time will be used to verify.
    -581  * </li>
    -582  * <li>
    -583  * Clock of JWT generator or verifier can be fast or slow. If these clocks are
    -584  * very different, JWT validation may fail. To avoid such case, 'jsrsasign' supports
    -585  * 'acceptField.gracePeriod' parameter which specifies acceptable time difference
    -586  * of those clocks in seconds. So if you want to accept slow or fast in 2 hours,
    -587  * you can specify <code>acceptField.gracePeriod = 2 * 60 * 60;</code>.
    -588  * "gracePeriod" is zero by default.
    -589  * "gracePeriod" is supported since jsrsasign 5.0.12.
    -590  * </li>
    -591  * <li>Payload.exp (expire) - Validation time is smaller than Payload.exp + gracePeriod.</li>
    -592  * <li>Payload.nbf (not before) - Validation time is greater than Payload.nbf - gracePeriod.</li>
    -593  * <li>Payload.iat (issued at) - Validation time is greater than Payload.iat - gracePeriod.</li>
    -594  * </ul>
    -595  * </li>
    -596  * <li>Payload.jti (JWT id) - Payload.jti is included in acceptField.jti if specified. (OPTION)</li>
    -597  * <li>JWS signature of JWS is valid for specified key.</li>
    -598  * </ul>
    -599  *
    -600  * <h4>acceptField parameters</h4>
    -601  * Here is available acceptField argument parameters:
    -602  * <ul>
    -603  * <li>alg - array of acceptable signature algorithm names (ex. ["HS256", "HS384"])</li>
    -604  * <li>iss - array of acceptable issuer names (ex. ['http://foo.com'])</li>
    -605  * <li>sub - array of acceptable subject names (ex. ['mailto:john@foo.com'])</li>
    -606  * <li>aud - array or string of acceptable audience name(s) (ex. ['http://foo.com'])</li>
    -607  * <li>jti - string of acceptable JWT ID (OPTION) (ex. 'id1234')</li>
    -608  * <li>
    -609  * verifyAt - time to verify 'nbf', 'iat' and 'exp' in UNIX seconds 
    -610  * (OPTION) (ex. 1377663900).  
    -611  * If this is not specified, current time of verifier will be used. 
    -612  * {@link KJUR.jws.IntDate} may be useful to specify it.
    -613  * </li>
    -614  * <li>gracePeriod - acceptable time difference between signer and verifier
    -615  * in seconds (ex. 3600). If this is not specified, zero will be used.</li>
    -616  * </ul>
    -617  *
    -618  * @example
    -619  * // simple validation for HS256
    -620  * isValid = KJUR.jws.JWS.verifyJWT("eyJhbG...", "616161", {alg: ["HS256"]}),
    -621  *
    -622  * // full validation for RS or PS
    -623  * pubkey = KEYUTIL.getKey('-----BEGIN CERT...');
    -624  * isValid = KJUR.jws.JWS.verifyJWT('eyJh...', pubkey, {
    -625  *   alg: ['RS256', 'RS512', 'PS256', 'PS512'],
    -626  *   iss: ['http://foo.com'],
    -627  *   sub: ['mailto:john@foo.com', 'mailto:alice@foo.com'],
    -628  *   verifyAt: KJUR.jws.IntDate.get('20150520235959Z'),
    -629  *   aud: ['http://foo.com'], // aud: 'http://foo.com' is fine too.
    -630  *   jti: 'id123456',
    -631  *   gracePeriod: 1 * 60 * 60 // accept 1 hour slow or fast
    -632  * });
    -633  */
    -634 KJUR.jws.JWS.verifyJWT = function(sJWT, key, acceptField) {
    -635     var ns1 = KJUR.jws.JWS;
    -636 
    -637     // 1. parse JWT
    -638     var a = sJWT.split(".");
    -639     var uHeader = a[0];
    -640     var uPayload = a[1];
    -641     var uSignatureInput = uHeader + "." + uPayload;
    -642     var hSig = b64utohex(a[2]);
    -643 
    -644     // 2. parse JWS header
    -645     var pHeader = ns1.readSafeJSONString(b64utoutf8(uHeader));
    -646 
    -647     // 3. parse JWS payload
    -648     var pPayload = ns1.readSafeJSONString(b64utoutf8(uPayload));
    -649 
    -650     // 4. algorithm ('alg' in header) check
    -651     if (pHeader.alg === undefined) return false;
    -652     if (acceptField.alg === undefined)
    -653 	throw "acceptField.alg shall be specified";
    -654     if (! ns1.inArray(pHeader.alg, acceptField.alg)) return false;
    -655 
    -656     // 5. issuer ('iss' in payload) check
    -657     if (pPayload.iss !== undefined && typeof acceptField.iss === "object") {
    -658 	if (! ns1.inArray(pPayload.iss, acceptField.iss)) return false;
    -659     }
    -660 
    -661     // 6. subject ('sub' in payload) check
    -662     if (pPayload.sub !== undefined && typeof acceptField.sub === "object") {
    -663 	if (! ns1.inArray(pPayload.sub, acceptField.sub)) return false;
    -664     }
    -665 
    -666     // 7. audience ('aud' in payload) check
    -667     if (pPayload.aud !== undefined && typeof acceptField.aud === "object") {
    -668 	if (typeof pPayload.aud == "string") {
    -669 	    if (! ns1.inArray(pPayload.aud, acceptField.aud))
    -670 		return false;
    -671 	} else if (typeof pPayload.aud == "object") {
    -672 	    if (! ns1.includedArray(pPayload.aud, acceptField.aud))
    -673 		return false;
    -674 	}
    -675     }
    -676 
    -677     // 8. time validity 
    -678     //   (nbf - gracePeriod < now < exp + gracePeriod) && (iat - gracePeriod < now)
    -679     var now = KJUR.jws.IntDate.getNow();
    -680     if (acceptField.verifyAt !== undefined && typeof acceptField.verifyAt === "number") {
    -681 	now = acceptField.verifyAt;
    -682     }
    -683     if (acceptField.gracePeriod === undefined || 
    -684         typeof acceptField.gracePeriod !== "number") {
    -685 	acceptField.gracePeriod = 0;
    -686     }
    -687 
    -688     // 8.1 expired time 'exp' check
    -689     if (pPayload.exp !== undefined && typeof pPayload.exp == "number") {
    -690 	if (pPayload.exp + acceptField.gracePeriod < now) return false;
    -691     }
    -692 
    -693     // 8.2 not before time 'nbf' check
    -694     if (pPayload.nbf !== undefined && typeof pPayload.nbf == "number") {
    -695 	if (now < pPayload.nbf - acceptField.gracePeriod) return false;
    -696     }
    -697     
    -698     // 8.3 issued at time 'iat' check
    -699     if (pPayload.iat !== undefined && typeof pPayload.iat == "number") {
    -700 	if (now < pPayload.iat - acceptField.gracePeriod) return false;
    -701     }
    -702 
    -703     // 9 JWT id 'jti' check
    -704     if (pPayload.jti !== undefined && acceptField.jti !== undefined) {
    -705       if (pPayload.jti !== acceptField.jti) return false;
    -706     }
    -707 
    -708     // 10 JWS signature check
    -709     if (! KJUR.jws.JWS.verify(sJWT, key, acceptField.alg)) return false;
    -710 
    -711     // 11 passed all check
    -712     return true;
    -713 };
    -714 
    -715 /**
    -716  * check whether array is included by another array
    -717  * @name includedArray
    -718  * @memberOf KJUR.jws.JWS
    -719  * @function
    -720  * @static
    -721  * @param {Array} a1 check whether set a1 is included by a2
    -722  * @param {Array} a2 check whether set a1 is included by a2
    -723  * @return {Boolean} check whether set a1 is included by a2
    -724  * @since jws 3.2.3
    -725  * This method verifies whether an array is included by another array.
    -726  * It doesn't care about item ordering in a array.
    -727  * @example
    -728  * KJUR.jws.JWS.includedArray(['b'], ['b', 'c', 'a']) => true
    -729  * KJUR.jws.JWS.includedArray(['a', 'b'], ['b', 'c', 'a']) => true
    -730  * KJUR.jws.JWS.includedArray(['a', 'b'], ['b', 'c']) => false
    -731  */
    -732 KJUR.jws.JWS.includedArray = function(a1, a2) {
    -733     var inArray = KJUR.jws.JWS.inArray;
    -734     if (a1 === null) return false;
    -735     if (typeof a1 !== "object") return false;
    -736     if (typeof a1.length !== "number") return false;
    -737 
    -738     for (var i = 0; i < a1.length; i++) {
    -739 	if (! inArray(a1[i], a2)) return false;
    -740     }
    -741     return true;
    -742 };
    -743 
    -744 /**
    -745  * check whether item is included by array
    -746  * @name inArray
    -747  * @memberOf KJUR.jws.JWS
    -748  * @function
    -749  * @static
    -750  * @param {String} item check whether item is included by array
    -751  * @param {Array} a check whether item is included by array
    -752  * @return {Boolean} check whether item is included by array
    -753  * @since jws 3.2.3
    -754  * This method verifies whether an item is included by an array.
    -755  * It doesn't care about item ordering in an array.
    -756  * @example
    -757  * KJUR.jws.JWS.inArray('b', ['b', 'c', 'a']) => true
    -758  * KJUR.jws.JWS.inArray('a', ['b', 'c', 'a']) => true
    -759  * KJUR.jws.JWS.inArray('a', ['b', 'c']) => false
    -760  */
    -761 KJUR.jws.JWS.inArray = function(item, a) {
    -762     if (a === null) return false;
    -763     if (typeof a !== "object") return false;
    -764     if (typeof a.length !== "number") return false;
    -765     for (var i = 0; i < a.length; i++) {
    -766 	if (a[i] == item) return true;
    -767     }
    -768     return false;
    -769 };
    -770 
    -771 /**
    -772  * static associative array of general signature algorithm name from JWS algorithm name
    -773  * @since jws 3.0.0
    -774  */
    -775 KJUR.jws.JWS.jwsalg2sigalg = {
    -776     "HS256":	"HmacSHA256",
    -777     "HS384":	"HmacSHA384",
    -778     "HS512":	"HmacSHA512",
    -779     "RS256":	"SHA256withRSA",
    -780     "RS384":	"SHA384withRSA",
    -781     "RS512":	"SHA512withRSA",
    -782     "ES256":	"SHA256withECDSA",
    -783     "ES384":	"SHA384withECDSA",
    -784     //"ES512":	"SHA512withECDSA", // unsupported because of jsrsasign's bug
    -785     "PS256":	"SHA256withRSAandMGF1",
    -786     "PS384":	"SHA384withRSAandMGF1",
    -787     "PS512":	"SHA512withRSAandMGF1",
    -788     "none":	"none",
    -789 };
    -790 
    -791 // === utility static method ==================================================
    -792 
    -793 /**
    -794  * check whether a String "s" is a safe JSON string or not.<br/>
    -795  * If a String "s" is a malformed JSON string or an other object type
    -796  * this returns 0, otherwise this returns 1.
    -797  * @name isSafeJSONString
    -798  * @memberOf KJUR.jws.JWS
    -799  * @function
    -800  * @static
    -801  * @param {String} s JSON string
    -802  * @return {Number} 1 or 0
    -803  */
    -804 KJUR.jws.JWS.isSafeJSONString = function(s, h, p) {
    -805     var o = null;
    -806     try {
    -807 	o = jsonParse(s);
    -808 	if (typeof o != "object") return 0;
    -809 	if (o.constructor === Array) return 0;
    -810 	if (h) h[p] = o;
    -811 	return 1;
    -812     } catch (ex) {
    -813 	return 0;
    -814     }
    -815 };
    -816 
    -817 /**
    -818  * read a String "s" as JSON object if it is safe.<br/>
    -819  * If a String "s" is a malformed JSON string or not JSON string,
    -820  * this returns null, otherwise returns JSON object.
    -821  * @name readSafeJSONString
    -822  * @memberOf KJUR.jws.JWS
    -823  * @function
    -824  * @static
    -825  * @param {String} s JSON string
    -826  * @return {Object} JSON object or null
    -827  * @since 1.1.1
    -828  */
    -829 KJUR.jws.JWS.readSafeJSONString = function(s) {
    -830     var o = null;
    -831     try {
    -832 	o = jsonParse(s);
    -833 	if (typeof o != "object") return null;
    -834 	if (o.constructor === Array) return null;
    -835 	return o;
    -836     } catch (ex) {
    -837 	return null;
    -838     }
    -839 };
    -840 
    -841 /**
    -842  * get Encoed Signature Value from JWS string.<br/>
    -843  * @name getEncodedSignatureValueFromJWS
    -844  * @memberOf KJUR.jws.JWS
    -845  * @function
    -846  * @static
    -847  * @param {String} sJWS JWS signature string to be verified
    -848  * @return {String} string of Encoded Signature Value 
    -849  * @throws if sJWS is not comma separated string such like "Header.Payload.Signature".
    -850  */
    -851 KJUR.jws.JWS.getEncodedSignatureValueFromJWS = function(sJWS) {
    -852     if (sJWS.match(/^[^.]+\.[^.]+\.([^.]+)$/) == null) {
    -853 	throw "JWS signature is not a form of 'Head.Payload.SigValue'.";
    -854     }
    -855     return RegExp.$1;
    -856 };
    -857 
    -858 /**
    -859  * get RFC 7638 JWK thumbprint from JWK object
    -860  * @name getJWKthumbprint
    -861  * @memberOf KJUR.jws.JWS
    -862  * @function
    -863  * @static
    -864  * @param {Object} o JWK object to be calculated thumbprint
    -865  * @return {String} Base64 URL encoded JWK thumbprint value
    -866  * @since jsrsasign 5.0.2 jws 3.3.2
    -867  * @description
    -868  * This method calculates JWK thmubprint for specified JWK object
    -869  * as described in 
    -870  * <a href="https://tools.ietf.org/html/rfc7638">RFC 7638</a>.
    -871  * It supports all type of "kty". (i.e. "RSA", "EC" and "oct"
    -872  * (for symmetric key))
    -873  * Working sample is 
    -874  * <a href="https://kjur.github.io/jsrsasign/sample/tool_jwktp.html">here</a>.
    -875  * @example
    -876  * jwk = {"kty":"RSA", "n":"0vx...", "e":"AQAB", ...};
    -877  * thumbprint = KJUR.jws.JWS.getJWKthumbprint(jwk);
    -878  */
    -879 KJUR.jws.JWS.getJWKthumbprint = function(o) {
    -880     if (o.kty !== "RSA" &&
    -881 	o.kty !== "EC" &&
    -882 	o.kty !== "oct")
    -883 	throw "unsupported algorithm for JWK Thumprint";
    -884 
    -885     // 1. get canonically ordered json string
    -886     var s = '{';
    -887     if (o.kty === "RSA") {
    -888 	if (typeof o.n != "string" || typeof o.e != "string")
    -889 	    throw "wrong n and e value for RSA key";
    -890 	s += '"' + 'e' + '":"' + o.e + '",';
    -891 	s += '"' + 'kty' + '":"' + o.kty + '",';
    -892 	s += '"' + 'n' + '":"' + o.n + '"}';
    -893     } else if (o.kty === "EC") {
    -894 	if (typeof o.crv != "string" || 
    -895 	    typeof o.x != "string" ||
    -896 	    typeof o.y != "string")
    -897 	    throw "wrong crv, x and y value for EC key";
    -898 	s += '"' + 'crv' + '":"' + o.crv + '",';
    -899 	s += '"' + 'kty' + '":"' + o.kty + '",';
    -900 	s += '"' + 'x' + '":"' + o.x + '",';
    -901 	s += '"' + 'y' + '":"' + o.y + '"}';
    -902     } else if (o.kty === "oct") {
    -903 	if (typeof o.k != "string")
    -904 	    throw "wrong k value for oct(symmetric) key";
    -905 	s += '"' + 'kty' + '":"' + o.kty + '",';
    -906 	s += '"' + 'k' + '":"' + o.k + '"}';
    -907     }
    -908     //alert(s);
    -909 
    -910     // 2. get thumb print
    -911     var hJWK = rstrtohex(s);
    -912     var hash = KJUR.crypto.Util.hashHex(hJWK, "sha256");
    -913     var hashB64U = hextob64u(hash);
    -914 
    -915     return hashB64U;
    -916 };
    -917 
    -918 /**
    -919  * IntDate class for time representation for JSON Web Token(JWT)
    -920  * @class KJUR.jws.IntDate class
    -921  * @name KJUR.jws.IntDate
    -922  * @since jws 3.0.1
    -923  * @description
    -924  * Utility class for IntDate which is integer representation of UNIX origin time
    -925  * used in JSON Web Token(JWT).
    -926  */
    -927 KJUR.jws.IntDate = {};
    -928 
    -929 /**
    -930  * get UNIX origin time from by string
    -931  * @name get
    -932  * @memberOf KJUR.jws.IntDate
    -933  * @function
    -934  * @static
    -935  * @param {String} s string of time representation
    -936  * @return {Integer} UNIX origin time in seconds for argument 's'
    -937  * @since jws 3.0.1
    -938  * @throws "unsupported format: s" when malformed format
    -939  * @description
    -940  * This method will accept following representation of time.
    -941  * <ul>
    -942  * <li>now - current time</li>
    -943  * <li>now + 1hour - after 1 hour from now</li>
    -944  * <li>now + 1day - after 1 day from now</li>
    -945  * <li>now + 1month - after 30 days from now</li>
    -946  * <li>now + 1year - after 365 days from now</li>
    -947  * <li>YYYYmmDDHHMMSSZ - UTC time (ex. 20130828235959Z)</li>
    -948  * <li>number - UNIX origin time (seconds from 1970-01-01 00:00:00) (ex. 1377714748)</li>
    -949  * </ul>
    -950  */
    -951 KJUR.jws.IntDate.get = function(s) {
    -952     if (s == "now") {
    -953 	return KJUR.jws.IntDate.getNow();
    -954     } else if (s == "now + 1hour") {
    -955 	return KJUR.jws.IntDate.getNow() + 60 * 60;
    -956     } else if (s == "now + 1day") {
    -957 	return KJUR.jws.IntDate.getNow() + 60 * 60 * 24;
    -958     } else if (s == "now + 1month") {
    -959 	return KJUR.jws.IntDate.getNow() + 60 * 60 * 24 * 30;
    -960     } else if (s == "now + 1year") {
    -961 	return KJUR.jws.IntDate.getNow() + 60 * 60 * 24 * 365;
    -962     } else if (s.match(/Z$/)) {
    -963 	return KJUR.jws.IntDate.getZulu(s);
    -964     } else if (s.match(/^[0-9]+$/)) {
    -965 	return parseInt(s);
    -966     }
    -967     throw "unsupported format: " + s;
    -968 };
    -969 
    -970 /**
    -971  * get UNIX origin time from Zulu time representation string
    -972  * @name getZulu
    -973  * @memberOf KJUR.jws.IntDate
    -974  * @function
    -975  * @static
    -976  * @param {String} s string of Zulu time representation (ex. 20151012125959Z)
    -977  * @return {Integer} UNIX origin time in seconds for argument 's'
    -978  * @since jws 3.0.1
    -979  * @throws "unsupported format: s" when malformed format
    -980  * @description
    -981  * This method provides UNIX origin time from Zulu time.
    -982  * Following representations are supported:
    -983  * <ul>
    -984  * <li>YYYYMMDDHHmmSSZ - GeneralizedTime format</li>
    -985  * <li>YYMMDDHHmmSSZ - UTCTime format. If YY is greater or equal to 
    -986  * 50 then it represents 19YY otherwise 20YY.</li>
    -987  * </ul>
    -988  * @example
    -989  * KJUR.jws.IntDate.getZulu("20151012125959Z") => 1478...
    -990  * KJUR.jws.IntDate.getZulu("151012125959Z") => 1478...
    -991  */
    -992 KJUR.jws.IntDate.getZulu = function(s) {
    -993     var a;
    -994     if (a = s.match(/(\d+)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z/)) {
    -995         var sYear = RegExp.$1;
    -996 	var year = parseInt(sYear);
    -997 	if (sYear.length == 4) {
    -998         } else if (sYear.length == 2) {
    -999 	    if (50 <= year && year < 100) {
    -1000 		year = 1900 + year;
    -1001 	    } else if (0 <= year && year < 50) {
    -1002 		year = 2000 + year;
    -1003 	    } else {
    -1004 		throw "malformed year string for UTCTime";
    -1005 	    }
    -1006 	} else {
    -1007 	    throw "malformed year string";
    -1008 	}
    -1009 	var month = parseInt(RegExp.$2) - 1;
    -1010 	var day = parseInt(RegExp.$3);
    -1011 	var hour = parseInt(RegExp.$4);
    -1012 	var min = parseInt(RegExp.$5);
    -1013 	var sec = parseInt(RegExp.$6);
    -1014 	var d = new Date(Date.UTC(year, month, day, hour, min, sec));
    -1015 	return ~~(d / 1000);
    -1016     }
    -1017     throw "unsupported format: " + s;
    -1018 };
    -1019 
    -1020 /**
    -1021  * get UNIX origin time of current time
    -1022  * @name getNow
    -1023  * @memberOf KJUR.jws.IntDate
    -1024  * @function
    -1025  * @static
    -1026  * @return {Integer} UNIX origin time for current time
    -1027  * @since jws 3.0.1
    -1028  * @description
    -1029  * This method provides UNIX origin time for current time
    -1030  * @example
    -1031  * KJUR.jws.IntDate.getNow() => 1478...
    -1032  */
    -1033 KJUR.jws.IntDate.getNow = function() {
    -1034     var d = ~~(new Date() / 1000);
    -1035     return d;
    -1036 };
    -1037 
    -1038 /**
    -1039  * get UTC time string from UNIX origin time value
    -1040  * @name intDate2UTCString
    -1041  * @memberOf KJUR.jws.IntDate
    -1042  * @function
    -1043  * @static
    -1044  * @param {Integer} intDate UNIX origin time value (ex. 1478...)
    -1045  * @return {String} UTC time string
    -1046  * @since jws 3.0.1
    -1047  * @description
    -1048  * This method provides UTC time string for UNIX origin time value.
    -1049  * @example
    -1050  * KJUR.jws.IntDate.intDate2UTCString(1478...) => "2015 Oct ..."
    -1051  */
    -1052 KJUR.jws.IntDate.intDate2UTCString = function(intDate) {
    -1053     var d = new Date(intDate * 1000);
    -1054     return d.toUTCString();
    -1055 };
    -1056 
    -1057 /**
    -1058  * get UTC time string from UNIX origin time value
    -1059  * @name intDate2Zulu
    -1060  * @memberOf KJUR.jws.IntDate
    -1061  * @function
    -1062  * @static
    -1063  * @param {Integer} intDate UNIX origin time value (ex. 1478...)
    -1064  * @return {String} Zulu time string
    -1065  * @since jws 3.0.1
    -1066  * @description
    -1067  * This method provides Zulu time string for UNIX origin time value.
    -1068  * @example
    -1069  * KJUR.jws.IntDate.intDate2UTCString(1478...) => "20151012...Z"
    -1070  */
    -1071 KJUR.jws.IntDate.intDate2Zulu = function(intDate) {
    -1072     var d = new Date(intDate * 1000);
    -1073     var year = ("0000" + d.getUTCFullYear()).slice(-4);    
    -1074     var mon =  ("00" + (d.getUTCMonth() + 1)).slice(-2);    
    -1075     var day =  ("00" + d.getUTCDate()).slice(-2);    
    -1076     var hour = ("00" + d.getUTCHours()).slice(-2);    
    -1077     var min =  ("00" + d.getUTCMinutes()).slice(-2);    
    -1078     var sec =  ("00" + d.getUTCSeconds()).slice(-2);    
    -1079     return year + mon + day + hour + min + sec + "Z";
    -1080 };
    -1081 
    -1082 
    \ No newline at end of file +137
    var matchResult = sJWS.match(/^([^.]+)\.([^.]+)\.([^.]+)$/); +138 if (matchResult == null) { +139 throw "JWS signature is not a form of 'Head.Payload.SigValue'."; +140 } +141 var b6Head = matchResult[1]; +142 var b6Payload = matchResult[2]; +143 var b6SigVal = matchResult[3]; +144 var sSI = b6Head + "." + b6Payload; +145 this.parsedJWS = {}; +146 this.parsedJWS.headB64U = b6Head; +147 this.parsedJWS.payloadB64U = b6Payload; +148 this.parsedJWS.sigvalB64U = b6SigVal; +149 this.parsedJWS.si = sSI; +150 +151 if (!sigValNotNeeded) { +152 var hSigVal = b64utohex(b6SigVal); +153 var biSigVal = parseBigInt(hSigVal, 16); +154 this.parsedJWS.sigvalH = hSigVal; +155 this.parsedJWS.sigvalBI = biSigVal; +156 } +157 +158 var sHead = b64utoutf8(b6Head); +159 var sPayload = b64utoutf8(b6Payload); +160 this.parsedJWS.headS = sHead; +161 this.parsedJWS.payloadS = sPayload; +162 +163 if (! ns1.isSafeJSONString(sHead, this.parsedJWS, 'headP')) +164 throw "malformed JSON string for JWS Head: " + sHead; +165 }; +166 }; +167 +168 // === major static method ======================================================== +169 +170 /** +171 * generate JWS signature by specified key<br/> +172 * @name sign +173 * @memberOf KJUR.jws.JWS +174 * @function +175 * @static +176 * @param {String} alg JWS algorithm name to sign and force set to sHead or null +177 * @param {String} spHead string or object of JWS Header +178 * @param {String} spPayload string or object of JWS Payload +179 * @param {String} key string of private key or mac key object to sign +180 * @param {String} pass (OPTION)passcode to use encrypted asymmetric private key +181 * @return {String} JWS signature string +182 * @since jws 3.0.0 +183 * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Signature.html">jsrsasign KJUR.crypto.Signature method</a> +184 * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Mac.html">jsrsasign KJUR.crypto.Mac method</a> +185 * @description +186 * This method supports following algorithms. +187 * <table> +188 * <tr><th>alg value</th><th>spec requirement</th><th>jsjws support</th></tr> +189 * <tr><td>HS256</td><td>REQUIRED</td><td>SUPPORTED</td></tr> +190 * <tr><td>HS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +191 * <tr><td>HS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +192 * <tr><td>RS256</td><td>RECOMMENDED</td><td>SUPPORTED</td></tr> +193 * <tr><td>RS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +194 * <tr><td>RS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +195 * <tr><td>ES256</td><td>RECOMMENDED+</td><td>SUPPORTED</td></tr> +196 * <tr><td>ES384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +197 * <tr><td>ES512</td><td>OPTIONAL</td><td>-</td></tr> +198 * <tr><td>PS256</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +199 * <tr><td>PS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +200 * <tr><td>PS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> +201 * <tr><td>none</td><td>REQUIRED</td><td>SUPPORTED(signature generation only)</td></tr> +202 * </table> +203 * <dl> +204 * <dt>NOTE1: +205 * <dd>salt length of RSAPSS signature is the same as the hash algorithm length +206 * because of <a href="http://www.ietf.org/mail-archive/web/jose/current/msg02901.html">IETF JOSE ML discussion</a>. +207 * <dt>NOTE2: +208 * <dd>To support HS384, patched version of CryptoJS is used. +209 * <a href="https://code.google.com/p/crypto-js/issues/detail?id=84">See here for detail</a>. +210 * <dt>NOTE3: +211 * From jsrsasign 4.10.0 jws 3.3.0, Way to provide password +212 * for HS* algorithm is changed. The 'key' attribute value is +213 * passed to {@link KJUR.crypto.Mac.setPassword} so please see +214 * {@link KJUR.crypto.Mac.setPassword} for detail. +215 * As for backword compatibility, if key is a string, has even length and +216 * 0..9, A-F or a-f characters, key string is treated as a hexadecimal +217 * otherwise it is treated as a raw string. +218 * <dd> +219 * </dl> +220 * <b>EXAMPLE</b><br/> +221 * @example +222 * // sign HS256 signature with password "aaa" implicitly handled as string +223 * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256", cty: "JWT"}, {age: 21}, "aaa"); +224 * // sign HS256 signature with password "6161" implicitly handled as hex +225 * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256", cty: "JWT"}, {age: 21}, "6161"); +226 * // sign HS256 signature with base64 password +227 * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256"}, {age: 21}, {b64: "Mi/8..a="}); +228 * // sign RS256 signature with PKCS#8 PEM RSA private key +229 * sJWS = KJUR.jws.JWS.sign(null, {alg: "RS256"}, {age: 21}, "-----BEGIN PRIVATE KEY..."); +230 * // sign RS256 signature with PKCS#8 PEM ECC private key with passcode +231 * sJWS = KJUR.jws.JWS.sign(null, {alg: "ES256"}, {age: 21}, +232 * "-----BEGIN PRIVATE KEY...", "keypass"); +233 * // header and payload can be passed by both string and object +234 * sJWS = KJUR.jws.JWS.sign(null, '{alg:"HS256",cty:"JWT"}', '{age:21}', "aaa"); +235 */ +236 KJUR.jws.JWS.sign = function(alg, spHeader, spPayload, key, pass) { +237 var ns1 = KJUR.jws.JWS; +238 var sHeader, pHeader, sPayload; +239 +240 // 1. check signatureInput(Header, Payload) is string or object +241 if (typeof spHeader != 'string' && typeof spHeader != 'object') +242 throw "spHeader must be JSON string or object: " + spHeader; +243 +244 if (typeof spHeader == 'object') { +245 pHeader = spHeader; +246 sHeader = JSON.stringify(pHeader); +247 } +248 +249 if (typeof spHeader == 'string') { +250 sHeader = spHeader; +251 if (! ns1.isSafeJSONString(sHeader)) +252 throw "JWS Head is not safe JSON string: " + sHeader; +253 pHeader = ns1.readSafeJSONString(sHeader); +254 +255 } +256 +257 sPayload = spPayload; +258 if (typeof spPayload == 'object') sPayload = JSON.stringify(spPayload); +259 +260 // 2. use alg if defined in sHeader +261 if ((alg == '' || alg == null) && +262 pHeader['alg'] !== undefined) { +263 alg = pHeader['alg']; +264 } +265 +266 // 3. update sHeader to add alg if alg undefined +267 if ((alg != '' && alg != null) && +268 pHeader['alg'] === undefined) { +269 pHeader['alg'] = alg; +270 sHeader = JSON.stringify(pHeader); +271 } +272 +273 // 4. check explicit algorithm doesn't match with JWS header. +274 if (alg !== pHeader.alg) +275 throw "alg and sHeader.alg doesn't match: " + alg + "!=" + pHeader.alg; +276 +277 // 5. set signature algorithm like SHA1withRSA +278 var sigAlg = null; +279 if (ns1.jwsalg2sigalg[alg] === undefined) { +280 throw "unsupported alg name: " + alg; +281 } else { +282 sigAlg = ns1.jwsalg2sigalg[alg]; +283 } +284 +285 var uHeader = utf8tob64u(sHeader); +286 var uPayload = utf8tob64u(sPayload); +287 var uSignatureInput = uHeader + "." + uPayload +288 // 6. sign +289 var hSig = ""; +290 if (sigAlg.substr(0, 4) == "Hmac") { +291 if (key === undefined) +292 throw "mac key shall be specified for HS* alg"; +293 //alert("sigAlg=" + sigAlg); +294 var mac = new KJUR.crypto.Mac({'alg': sigAlg, 'prov': 'cryptojs', 'pass': key}); +295 mac.updateString(uSignatureInput); +296 hSig = mac.doFinal(); +297 } else if (sigAlg.indexOf("withECDSA") != -1) { +298 var sig = new KJUR.crypto.Signature({'alg': sigAlg}); +299 sig.init(key, pass); +300 sig.updateString(uSignatureInput); +301 hASN1Sig = sig.sign(); +302 hSig = KJUR.crypto.ECDSA.asn1SigToConcatSig(hASN1Sig); +303 } else if (sigAlg != "none") { +304 var sig = new KJUR.crypto.Signature({'alg': sigAlg}); +305 sig.init(key, pass); +306 sig.updateString(uSignatureInput); +307 hSig = sig.sign(); +308 } +309 +310 var uSig = hextob64u(hSig); +311 return uSignatureInput + "." + uSig; +312 }; +313 +314 /** +315 * verify JWS signature by specified key or certificate<br/> +316 * @name verify +317 * @memberOf KJUR.jws.JWS +318 * @function +319 * @static +320 * @param {String} sJWS string of JWS signature to verify +321 * @param {Object} key string of public key, certificate or key object to verify +322 * @param {String} acceptAlgs array of algorithm name strings (OPTION) +323 * @return {Boolean} true if the signature is valid otherwise false +324 * @since jws 3.0.0 +325 * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Signature.html">jsrsasign KJUR.crypto.Signature method</a> +326 * @see <a href="http://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Mac.html">jsrsasign KJUR.crypto.Mac method</a> +327 * @description +328 * <p> +329 * This method verifies a JSON Web Signature Compact Serialization string by the validation +330 * algorithm as described in +331 * <a href="http://self-issued.info/docs/draft-jones-json-web-signature-04.html#anchor5"> +332 * the section 5 of Internet Draft draft-jones-json-web-signature-04.</a> +333 * </p> +334 * <p> +335 * Since 3.2.0 strict key checking has been provided against a JWS algorithm +336 * in a JWS header. +337 * <ul> +338 * <li>In case 'alg' is 'HS*' in the JWS header, +339 * 'key' shall be hexadecimal string for Hmac{256,384,512} shared secret key. +340 * Otherwise it raise an error.</li> +341 * <li>In case 'alg' is 'RS*' or 'PS*' in the JWS header, +342 * 'key' shall be a RSAKey object or a PEM string of +343 * X.509 RSA public key certificate or PKCS#8 RSA public key. +344 * Otherwise it raise an error.</li> +345 * <li>In case 'alg' is 'ES*' in the JWS header, +346 * 'key' shall be a KJUR.crypto.ECDSA object or a PEM string of +347 * X.509 ECC public key certificate or PKCS#8 ECC public key. +348 * Otherwise it raise an error.</li> +349 * <li>In case 'alg' is 'none' in the JWS header, +350 * validation not supported after jsjws 3.1.0.</li> +351 * </ul> +352 * </p> +353 * <p> +354 * NOTE1: The argument 'acceptAlgs' is supported since 3.2.0. +355 * Strongly recommended to provide acceptAlgs to mitigate +356 * signature replacement attacks.<br/> +357 * </p> +358 * <p> +359 * NOTE2: From jsrsasign 4.9.0 jws 3.2.5, Way to provide password +360 * for HS* algorithm is changed. The 'key' attribute value is +361 * passed to {@link KJUR.crypto.Mac.setPassword} so please see +362 * {@link KJUR.crypto.Mac.setPassword} for detail. +363 * As for backword compatibility, if key is a string, has even length and +364 * 0..9, A-F or a-f characters, key string is treated as a hexadecimal +365 * otherwise it is treated as a raw string. +366 * </p> +367 * @example +368 * // 1) verify a RS256 JWS signature by a certificate string. +369 * isValid = KJUR.jws.JWS.verify('eyJh...', '-----BEGIN...', ['RS256']); +370 * +371 * // 2) verify a HS256 JWS signature by a certificate string. +372 * isValid = KJUR.jws.JWS.verify('eyJh...', {hex: '6f62ad...'}, ['HS256']); +373 * isValid = KJUR.jws.JWS.verify('eyJh...', {b64: 'Mi/ab8...a=='}, ['HS256']); +374 * isValid = KJUR.jws.JWS.verify('eyJh...', {utf8: 'Secret秘密'}, ['HS256']); +375 * isValid = KJUR.jws.JWS.verify('eyJh...', '6f62ad', ['HS256']); // implicit hex +376 * isValid = KJUR.jws.JWS.verify('eyJh...', '6f62ada', ['HS256']); // implicit raw string +377 * +378 * // 3) verify a ES256 JWS signature by a KJUR.crypto.ECDSA key object. +379 * var pubkey = KEYUTIL.getKey('-----BEGIN CERT...'); +380 * var isValid = KJUR.jws.JWS.verify('eyJh...', pubkey); +381 */ +382 KJUR.jws.JWS.verify = function(sJWS, key, acceptAlgs) { +383 var jws = KJUR.jws.JWS; +384 var a = sJWS.split("."); +385 var uHeader = a[0]; +386 var uPayload = a[1]; +387 var uSignatureInput = uHeader + "." + uPayload; +388 var hSig = b64utohex(a[2]); +389 +390 // 1. parse JWS header +391 var pHeader = jws.readSafeJSONString(b64utoutf8(a[0])); +392 var alg = null; +393 var algType = null; // HS|RS|PS|ES|no +394 if (pHeader.alg === undefined) { +395 throw "algorithm not specified in header"; +396 } else { +397 alg = pHeader.alg; +398 algType = alg.substr(0, 2); +399 } +400 +401 // 2. check whether alg is acceptable algorithms +402 if (acceptAlgs != null && +403 Object.prototype.toString.call(acceptAlgs) === '[object Array]' && +404 acceptAlgs.length > 0) { +405 var acceptAlgStr = ":" + acceptAlgs.join(":") + ":"; +406 if (acceptAlgStr.indexOf(":" + alg + ":") == -1) { +407 throw "algorithm '" + alg + "' not accepted in the list"; +408 } +409 } +410 +411 // 3. check whether key is a proper key for alg. +412 if (alg != "none" && key === null) { +413 throw "key shall be specified to verify."; +414 } +415 +416 // 3.1. There is no key check for HS* because Mac will check it. +417 // since jsrsasign 5.0.0. +418 +419 // 3.2. convert key object if key is a public key or cert PEM string +420 if (typeof key == "string" && +421 key.indexOf("-----BEGIN ") != -1) { +422 key = KEYUTIL.getKey(key); +423 } +424 +425 // 3.3. check whether key is RSAKey obj if alg is RS* or PS*. +426 if (algType == "RS" || algType == "PS") { +427 if (!(key instanceof RSAKey)) { +428 throw "key shall be a RSAKey obj for RS* and PS* algs"; +429 } +430 } +431 +432 // 3.4. check whether key is ECDSA obj if alg is ES*. +433 if (algType == "ES") { +434 if (!(key instanceof KJUR.crypto.ECDSA)) { +435 throw "key shall be a ECDSA obj for ES* algs"; +436 } +437 } +438 +439 // 3.5. check when alg is 'none' +440 if (alg == "none") { +441 } +442 +443 // 4. check whether alg is supported alg in jsjws. +444 var sigAlg = null; +445 if (jws.jwsalg2sigalg[pHeader.alg] === undefined) { +446 throw "unsupported alg name: " + alg; +447 } else { +448 sigAlg = jws.jwsalg2sigalg[alg]; +449 } +450 +451 // 5. verify +452 if (sigAlg == "none") { +453 throw "not supported"; +454 } else if (sigAlg.substr(0, 4) == "Hmac") { +455 var hSig2 = null; +456 if (key === undefined) +457 throw "hexadecimal key shall be specified for HMAC"; +458 //try { +459 var mac = new KJUR.crypto.Mac({'alg': sigAlg, 'pass': key}); +460 mac.updateString(uSignatureInput); +461 hSig2 = mac.doFinal(); +462 //} catch(ex) {}; +463 return hSig == hSig2; +464 } else if (sigAlg.indexOf("withECDSA") != -1) { +465 var hASN1Sig = null; +466 try { +467 hASN1Sig = KJUR.crypto.ECDSA.concatSigToASN1Sig(hSig); +468 } catch (ex) { +469 return false; +470 } +471 var sig = new KJUR.crypto.Signature({'alg': sigAlg}); +472 sig.init(key) +473 sig.updateString(uSignatureInput); +474 return sig.verify(hASN1Sig); +475 } else { +476 var sig = new KJUR.crypto.Signature({'alg': sigAlg}); +477 sig.init(key) +478 sig.updateString(uSignatureInput); +479 return sig.verify(hSig); +480 } +481 }; +482 +483 /** +484 * parse header and payload of JWS signature<br/> +485 * @name parse +486 * @memberOf KJUR.jws.JWS +487 * @function +488 * @static +489 * @param {String} sJWS string of JWS signature to parse +490 * @return {Array} associative array of parsed header and payload. See below. +491 * @throws if sJWS is malformed JWS signature +492 * @since jws 3.3.3 +493 * @description +494 * This method parses JWS signature string. +495 * Resulted associative array has following properties: +496 * <ul> +497 * <li>headerObj - JSON object of header</li> +498 * <li>payloadObj - JSON object of payload if payload is JSON string otherwise undefined</li> +499 * <li>headerPP - pretty printed JSON header by stringify</li> +500 * <li>payloadPP - pretty printed JSON payload by stringify if payload is JSON otherwise Base64URL decoded raw string of payload</li> +501 * <li>sigHex - hexadecimal string of signature</li> +502 * </ul> +503 * @example +504 * KJUR.jws.JWS.parse(sJWS) -> +505 * { +506 * headerObj: {"alg": "RS256", "typ": "JWS"}, +507 * payloadObj: {"product": "orange", "quantity": 100}, +508 * headerPP: +509 * '{ +510 * "alg": "RS256", +511 * "typ": "JWS" +512 * }', +513 * payloadPP: +514 * '{ +515 * "product": "orange", +516 * "quantity": 100 +517 * }', +518 * sigHex: "91f3cd..." +519 * } +520 */ +521 KJUR.jws.JWS.parse = function(sJWS) { +522 var a = sJWS.split("."); +523 var result = {}; +524 var uHeader, uPayload, uSig; +525 if (a.length != 2 && a.length != 3) +526 throw "malformed sJWS: wrong number of '.' splitted elements"; +527 +528 uHeader = a[0]; +529 uPayload = a[1]; +530 if (a.length == 3) uSig = a[2]; +531 +532 result.headerObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(uHeader)); +533 result.payloadObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(uPayload)); +534 +535 result.headerPP = JSON.stringify(result.headerObj, null, " "); +536 if (result.payloadObj == null) { +537 result.payloadPP = b64utoutf8(uPayload); +538 } else { +539 result.payloadPP = JSON.stringify(result.payloadObj, null, " "); +540 } +541 +542 if (uSig !== undefined) { +543 result.sigHex = b64utohex(uSig); +544 } +545 +546 return result; +547 }; +548 +549 /** +550 * @name verifyJWT +551 * @memberOf KJUR.jws.JWS +552 * @function +553 * @static +554 * @param {String} sJWT string of JSON Web Token(JWT) to verify +555 * @param {Object} key string of public key, certificate or key object to verify +556 * @param {Array} acceptField associative array of acceptable fields (OPTION) +557 * @return {Boolean} true if the JWT token is valid otherwise false +558 * @since jws 3.2.3 jsrsasign 4.8.0 +559 * +560 * @description +561 * This method verifies a +562 * <a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a> +563 * JSON Web Token(JWT). +564 * It will verify following: +565 * <ul> +566 * <li>Header.alg +567 * <ul> +568 * <li>alg is specified in JWT header.</li> +569 * <li>alg is included in acceptField.alg array. (MANDATORY)</li> +570 * <li>alg is proper for key.</li> +571 * </ul> +572 * </li> +573 * <li>Payload.iss (issuer) - Payload.iss is included in acceptField.iss array if specified. (OPTION)</li> +574 * <li>Payload.sub (subject) - Payload.sub is included in acceptField.sub array if specified. (OPTION)</li> +575 * <li>Payload.aud (audience) - Payload.aud is included in acceptField.aud array or +576 * the same as value if specified. (OPTION)</li> +577 * <li>Time validity +578 * <ul> +579 * <li> +580 * If acceptField.verifyAt as number of UNIX origin time is specifed for validation time, +581 * this method will verify at the time for it, otherwise current time will be used to verify. +582 * </li> +583 * <li> +584 * Clock of JWT generator or verifier can be fast or slow. If these clocks are +585 * very different, JWT validation may fail. To avoid such case, 'jsrsasign' supports +586 * 'acceptField.gracePeriod' parameter which specifies acceptable time difference +587 * of those clocks in seconds. So if you want to accept slow or fast in 2 hours, +588 * you can specify <code>acceptField.gracePeriod = 2 * 60 * 60;</code>. +589 * "gracePeriod" is zero by default. +590 * "gracePeriod" is supported since jsrsasign 5.0.12. +591 * </li> +592 * <li>Payload.exp (expire) - Validation time is smaller than Payload.exp + gracePeriod.</li> +593 * <li>Payload.nbf (not before) - Validation time is greater than Payload.nbf - gracePeriod.</li> +594 * <li>Payload.iat (issued at) - Validation time is greater than Payload.iat - gracePeriod.</li> +595 * </ul> +596 * </li> +597 * <li>Payload.jti (JWT id) - Payload.jti is included in acceptField.jti if specified. (OPTION)</li> +598 * <li>JWS signature of JWS is valid for specified key.</li> +599 * </ul> +600 * +601 * <h4>acceptField parameters</h4> +602 * Here is available acceptField argument parameters: +603 * <ul> +604 * <li>alg - array of acceptable signature algorithm names (ex. ["HS256", "HS384"])</li> +605 * <li>iss - array of acceptable issuer names (ex. ['http://foo.com'])</li> +606 * <li>sub - array of acceptable subject names (ex. ['mailto:john@foo.com'])</li> +607 * <li>aud - array or string of acceptable audience name(s) (ex. ['http://foo.com'])</li> +608 * <li>jti - string of acceptable JWT ID (OPTION) (ex. 'id1234')</li> +609 * <li> +610 * verifyAt - time to verify 'nbf', 'iat' and 'exp' in UNIX seconds +611 * (OPTION) (ex. 1377663900). +612 * If this is not specified, current time of verifier will be used. +613 * {@link KJUR.jws.IntDate} may be useful to specify it. +614 * </li> +615 * <li>gracePeriod - acceptable time difference between signer and verifier +616 * in seconds (ex. 3600). If this is not specified, zero will be used.</li> +617 * </ul> +618 * +619 * @example +620 * // simple validation for HS256 +621 * isValid = KJUR.jws.JWS.verifyJWT("eyJhbG...", "616161", {alg: ["HS256"]}), +622 * +623 * // full validation for RS or PS +624 * pubkey = KEYUTIL.getKey('-----BEGIN CERT...'); +625 * isValid = KJUR.jws.JWS.verifyJWT('eyJh...', pubkey, { +626 * alg: ['RS256', 'RS512', 'PS256', 'PS512'], +627 * iss: ['http://foo.com'], +628 * sub: ['mailto:john@foo.com', 'mailto:alice@foo.com'], +629 * verifyAt: KJUR.jws.IntDate.get('20150520235959Z'), +630 * aud: ['http://foo.com'], // aud: 'http://foo.com' is fine too. +631 * jti: 'id123456', +632 * gracePeriod: 1 * 60 * 60 // accept 1 hour slow or fast +633 * }); +634 */ +635 KJUR.jws.JWS.verifyJWT = function(sJWT, key, acceptField) { +636 var ns1 = KJUR.jws.JWS; +637 +638 // 1. parse JWT +639 var a = sJWT.split("."); +640 var uHeader = a[0]; +641 var uPayload = a[1]; +642 var uSignatureInput = uHeader + "." + uPayload; +643 var hSig = b64utohex(a[2]); +644 +645 // 2. parse JWS header +646 var pHeader = ns1.readSafeJSONString(b64utoutf8(uHeader)); +647 +648 // 3. parse JWS payload +649 var pPayload = ns1.readSafeJSONString(b64utoutf8(uPayload)); +650 +651 // 4. algorithm ('alg' in header) check +652 if (pHeader.alg === undefined) return false; +653 if (acceptField.alg === undefined) +654 throw "acceptField.alg shall be specified"; +655 if (! ns1.inArray(pHeader.alg, acceptField.alg)) return false; +656 +657 // 5. issuer ('iss' in payload) check +658 if (pPayload.iss !== undefined && typeof acceptField.iss === "object") { +659 if (! ns1.inArray(pPayload.iss, acceptField.iss)) return false; +660 } +661 +662 // 6. subject ('sub' in payload) check +663 if (pPayload.sub !== undefined && typeof acceptField.sub === "object") { +664 if (! ns1.inArray(pPayload.sub, acceptField.sub)) return false; +665 } +666 +667 // 7. audience ('aud' in payload) check +668 if (pPayload.aud !== undefined && typeof acceptField.aud === "object") { +669 if (typeof pPayload.aud == "string") { +670 if (! ns1.inArray(pPayload.aud, acceptField.aud)) +671 return false; +672 } else if (typeof pPayload.aud == "object") { +673 if (! ns1.includedArray(pPayload.aud, acceptField.aud)) +674 return false; +675 } +676 } +677 +678 // 8. time validity +679 // (nbf - gracePeriod < now < exp + gracePeriod) && (iat - gracePeriod < now) +680 var now = KJUR.jws.IntDate.getNow(); +681 if (acceptField.verifyAt !== undefined && typeof acceptField.verifyAt === "number") { +682 now = acceptField.verifyAt; +683 } +684 if (acceptField.gracePeriod === undefined || +685 typeof acceptField.gracePeriod !== "number") { +686 acceptField.gracePeriod = 0; +687 } +688 +689 // 8.1 expired time 'exp' check +690 if (pPayload.exp !== undefined && typeof pPayload.exp == "number") { +691 if (pPayload.exp + acceptField.gracePeriod < now) return false; +692 } +693 +694 // 8.2 not before time 'nbf' check +695 if (pPayload.nbf !== undefined && typeof pPayload.nbf == "number") { +696 if (now < pPayload.nbf - acceptField.gracePeriod) return false; +697 } +698 +699 // 8.3 issued at time 'iat' check +700 if (pPayload.iat !== undefined && typeof pPayload.iat == "number") { +701 if (now < pPayload.iat - acceptField.gracePeriod) return false; +702 } +703 +704 // 9 JWT id 'jti' check +705 if (pPayload.jti !== undefined && acceptField.jti !== undefined) { +706 if (pPayload.jti !== acceptField.jti) return false; +707 } +708 +709 // 10 JWS signature check +710 if (! KJUR.jws.JWS.verify(sJWT, key, acceptField.alg)) return false; +711 +712 // 11 passed all check +713 return true; +714 }; +715 +716 /** +717 * check whether array is included by another array +718 * @name includedArray +719 * @memberOf KJUR.jws.JWS +720 * @function +721 * @static +722 * @param {Array} a1 check whether set a1 is included by a2 +723 * @param {Array} a2 check whether set a1 is included by a2 +724 * @return {Boolean} check whether set a1 is included by a2 +725 * @since jws 3.2.3 +726 * This method verifies whether an array is included by another array. +727 * It doesn't care about item ordering in a array. +728 * @example +729 * KJUR.jws.JWS.includedArray(['b'], ['b', 'c', 'a']) => true +730 * KJUR.jws.JWS.includedArray(['a', 'b'], ['b', 'c', 'a']) => true +731 * KJUR.jws.JWS.includedArray(['a', 'b'], ['b', 'c']) => false +732 */ +733 KJUR.jws.JWS.includedArray = function(a1, a2) { +734 var inArray = KJUR.jws.JWS.inArray; +735 if (a1 === null) return false; +736 if (typeof a1 !== "object") return false; +737 if (typeof a1.length !== "number") return false; +738 +739 for (var i = 0; i < a1.length; i++) { +740 if (! inArray(a1[i], a2)) return false; +741 } +742 return true; +743 }; +744 +745 /** +746 * check whether item is included by array +747 * @name inArray +748 * @memberOf KJUR.jws.JWS +749 * @function +750 * @static +751 * @param {String} item check whether item is included by array +752 * @param {Array} a check whether item is included by array +753 * @return {Boolean} check whether item is included by array +754 * @since jws 3.2.3 +755 * This method verifies whether an item is included by an array. +756 * It doesn't care about item ordering in an array. +757 * @example +758 * KJUR.jws.JWS.inArray('b', ['b', 'c', 'a']) => true +759 * KJUR.jws.JWS.inArray('a', ['b', 'c', 'a']) => true +760 * KJUR.jws.JWS.inArray('a', ['b', 'c']) => false +761 */ +762 KJUR.jws.JWS.inArray = function(item, a) { +763 if (a === null) return false; +764 if (typeof a !== "object") return false; +765 if (typeof a.length !== "number") return false; +766 for (var i = 0; i < a.length; i++) { +767 if (a[i] == item) return true; +768 } +769 return false; +770 }; +771 +772 /** +773 * static associative array of general signature algorithm name from JWS algorithm name +774 * @since jws 3.0.0 +775 */ +776 KJUR.jws.JWS.jwsalg2sigalg = { +777 "HS256": "HmacSHA256", +778 "HS384": "HmacSHA384", +779 "HS512": "HmacSHA512", +780 "RS256": "SHA256withRSA", +781 "RS384": "SHA384withRSA", +782 "RS512": "SHA512withRSA", +783 "ES256": "SHA256withECDSA", +784 "ES384": "SHA384withECDSA", +785 //"ES512": "SHA512withECDSA", // unsupported because of jsrsasign's bug +786 "PS256": "SHA256withRSAandMGF1", +787 "PS384": "SHA384withRSAandMGF1", +788 "PS512": "SHA512withRSAandMGF1", +789 "none": "none", +790 }; +791 +792 // === utility static method ================================================== +793 +794 /** +795 * check whether a String "s" is a safe JSON string or not.<br/> +796 * If a String "s" is a malformed JSON string or an other object type +797 * this returns 0, otherwise this returns 1. +798 * @name isSafeJSONString +799 * @memberOf KJUR.jws.JWS +800 * @function +801 * @static +802 * @param {String} s JSON string +803 * @return {Number} 1 or 0 +804 */ +805 KJUR.jws.JWS.isSafeJSONString = function(s, h, p) { +806 var o = null; +807 try { +808 o = jsonParse(s); +809 if (typeof o != "object") return 0; +810 if (o.constructor === Array) return 0; +811 if (h) h[p] = o; +812 return 1; +813 } catch (ex) { +814 return 0; +815 } +816 }; +817 +818 /** +819 * read a String "s" as JSON object if it is safe.<br/> +820 * If a String "s" is a malformed JSON string or not JSON string, +821 * this returns null, otherwise returns JSON object. +822 * @name readSafeJSONString +823 * @memberOf KJUR.jws.JWS +824 * @function +825 * @static +826 * @param {String} s JSON string +827 * @return {Object} JSON object or null +828 * @since 1.1.1 +829 */ +830 KJUR.jws.JWS.readSafeJSONString = function(s) { +831 var o = null; +832 try { +833 o = jsonParse(s); +834 if (typeof o != "object") return null; +835 if (o.constructor === Array) return null; +836 return o; +837 } catch (ex) { +838 return null; +839 } +840 }; +841 +842 /** +843 * get Encoed Signature Value from JWS string.<br/> +844 * @name getEncodedSignatureValueFromJWS +845 * @memberOf KJUR.jws.JWS +846 * @function +847 * @static +848 * @param {String} sJWS JWS signature string to be verified +849 * @return {String} string of Encoded Signature Value +850 * @throws if sJWS is not comma separated string such like "Header.Payload.Signature". +851 */ +852 KJUR.jws.JWS.getEncodedSignatureValueFromJWS = function(sJWS) { +853 var matchResult = sJWS.match(/^[^.]+\.[^.]+\.([^.]+)$/); +854 if (matchResult == null) { +855 throw "JWS signature is not a form of 'Head.Payload.SigValue'."; +856 } +857 return matchResult[1]; +858 }; +859 +860 /** +861 * get RFC 7638 JWK thumbprint from JWK object +862 * @name getJWKthumbprint +863 * @memberOf KJUR.jws.JWS +864 * @function +865 * @static +866 * @param {Object} o JWK object to be calculated thumbprint +867 * @return {String} Base64 URL encoded JWK thumbprint value +868 * @since jsrsasign 5.0.2 jws 3.3.2 +869 * @description +870 * This method calculates JWK thmubprint for specified JWK object +871 * as described in +872 * <a href="https://tools.ietf.org/html/rfc7638">RFC 7638</a>. +873 * It supports all type of "kty". (i.e. "RSA", "EC" and "oct" +874 * (for symmetric key)) +875 * Working sample is +876 * <a href="https://kjur.github.io/jsrsasign/sample/tool_jwktp.html">here</a>. +877 * @example +878 * jwk = {"kty":"RSA", "n":"0vx...", "e":"AQAB", ...}; +879 * thumbprint = KJUR.jws.JWS.getJWKthumbprint(jwk); +880 */ +881 KJUR.jws.JWS.getJWKthumbprint = function(o) { +882 if (o.kty !== "RSA" && +883 o.kty !== "EC" && +884 o.kty !== "oct") +885 throw "unsupported algorithm for JWK Thumprint"; +886 +887 // 1. get canonically ordered json string +888 var s = '{'; +889 if (o.kty === "RSA") { +890 if (typeof o.n != "string" || typeof o.e != "string") +891 throw "wrong n and e value for RSA key"; +892 s += '"' + 'e' + '":"' + o.e + '",'; +893 s += '"' + 'kty' + '":"' + o.kty + '",'; +894 s += '"' + 'n' + '":"' + o.n + '"}'; +895 } else if (o.kty === "EC") { +896 if (typeof o.crv != "string" || +897 typeof o.x != "string" || +898 typeof o.y != "string") +899 throw "wrong crv, x and y value for EC key"; +900 s += '"' + 'crv' + '":"' + o.crv + '",'; +901 s += '"' + 'kty' + '":"' + o.kty + '",'; +902 s += '"' + 'x' + '":"' + o.x + '",'; +903 s += '"' + 'y' + '":"' + o.y + '"}'; +904 } else if (o.kty === "oct") { +905 if (typeof o.k != "string") +906 throw "wrong k value for oct(symmetric) key"; +907 s += '"' + 'kty' + '":"' + o.kty + '",'; +908 s += '"' + 'k' + '":"' + o.k + '"}'; +909 } +910 //alert(s); +911 +912 // 2. get thumb print +913 var hJWK = rstrtohex(s); +914 var hash = KJUR.crypto.Util.hashHex(hJWK, "sha256"); +915 var hashB64U = hextob64u(hash); +916 +917 return hashB64U; +918 }; +919 +920 /** +921 * IntDate class for time representation for JSON Web Token(JWT) +922 * @class KJUR.jws.IntDate class +923 * @name KJUR.jws.IntDate +924 * @since jws 3.0.1 +925 * @description +926 * Utility class for IntDate which is integer representation of UNIX origin time +927 * used in JSON Web Token(JWT). +928 */ +929 KJUR.jws.IntDate = {}; +930 +931 /** +932 * get UNIX origin time from by string +933 * @name get +934 * @memberOf KJUR.jws.IntDate +935 * @function +936 * @static +937 * @param {String} s string of time representation +938 * @return {Integer} UNIX origin time in seconds for argument 's' +939 * @since jws 3.0.1 +940 * @throws "unsupported format: s" when malformed format +941 * @description +942 * This method will accept following representation of time. +943 * <ul> +944 * <li>now - current time</li> +945 * <li>now + 1hour - after 1 hour from now</li> +946 * <li>now + 1day - after 1 day from now</li> +947 * <li>now + 1month - after 30 days from now</li> +948 * <li>now + 1year - after 365 days from now</li> +949 * <li>YYYYmmDDHHMMSSZ - UTC time (ex. 20130828235959Z)</li> +950 * <li>number - UNIX origin time (seconds from 1970-01-01 00:00:00) (ex. 1377714748)</li> +951 * </ul> +952 */ +953 KJUR.jws.IntDate.get = function(s) { +954 if (s == "now") { +955 return KJUR.jws.IntDate.getNow(); +956 } else if (s == "now + 1hour") { +957 return KJUR.jws.IntDate.getNow() + 60 * 60; +958 } else if (s == "now + 1day") { +959 return KJUR.jws.IntDate.getNow() + 60 * 60 * 24; +960 } else if (s == "now + 1month") { +961 return KJUR.jws.IntDate.getNow() + 60 * 60 * 24 * 30; +962 } else if (s == "now + 1year") { +963 return KJUR.jws.IntDate.getNow() + 60 * 60 * 24 * 365; +964 } else if (s.match(/Z$/)) { +965 return KJUR.jws.IntDate.getZulu(s); +966 } else if (s.match(/^[0-9]+$/)) { +967 return parseInt(s); +968 } +969 throw "unsupported format: " + s; +970 }; +971 +972 /** +973 * get UNIX origin time from Zulu time representation string +974 * @name getZulu +975 * @memberOf KJUR.jws.IntDate +976 * @function +977 * @static +978 * @param {String} s string of Zulu time representation (ex. 20151012125959Z) +979 * @return {Integer} UNIX origin time in seconds for argument 's' +980 * @since jws 3.0.1 +981 * @throws "unsupported format: s" when malformed format +982 * @description +983 * This method provides UNIX origin time from Zulu time. +984 * Following representations are supported: +985 * <ul> +986 * <li>YYYYMMDDHHmmSSZ - GeneralizedTime format</li> +987 * <li>YYMMDDHHmmSSZ - UTCTime format. If YY is greater or equal to +988 * 50 then it represents 19YY otherwise 20YY.</li> +989 * </ul> +990 * @example +991 * KJUR.jws.IntDate.getZulu("20151012125959Z") => 1478... +992 * KJUR.jws.IntDate.getZulu("151012125959Z") => 1478... +993 */ +994 KJUR.jws.IntDate.getZulu = function(s) { +995 var matchResult = s.match(/(\d+)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z/); +996 if (matchResult) { +997 var sYear = matchResult[1]; +998 var year = parseInt(sYear); +999 if (sYear.length == 4) { +1000 } else if (sYear.length == 2) { +1001 if (50 <= year && year < 100) { +1002 year = 1900 + year; +1003 } else if (0 <= year && year < 50) { +1004 year = 2000 + year; +1005 } else { +1006 throw "malformed year string for UTCTime"; +1007 } +1008 } else { +1009 throw "malformed year string"; +1010 } +1011 var month = parseInt(matchResult[2]) - 1; +1012 var day = parseInt(matchResult[3]); +1013 var hour = parseInt(matchResult[4]); +1014 var min = parseInt(matchResult[5]); +1015 var sec = parseInt(matchResult[6]); +1016 var d = new Date(Date.UTC(year, month, day, hour, min, sec)); +1017 return ~~(d / 1000); +1018 } +1019 throw "unsupported format: " + s; +1020 }; +1021 +1022 /** +1023 * get UNIX origin time of current time +1024 * @name getNow +1025 * @memberOf KJUR.jws.IntDate +1026 * @function +1027 * @static +1028 * @return {Integer} UNIX origin time for current time +1029 * @since jws 3.0.1 +1030 * @description +1031 * This method provides UNIX origin time for current time +1032 * @example +1033 * KJUR.jws.IntDate.getNow() => 1478... +1034 */ +1035 KJUR.jws.IntDate.getNow = function() { +1036 var d = ~~(new Date() / 1000); +1037 return d; +1038 }; +1039 +1040 /** +1041 * get UTC time string from UNIX origin time value +1042 * @name intDate2UTCString +1043 * @memberOf KJUR.jws.IntDate +1044 * @function +1045 * @static +1046 * @param {Integer} intDate UNIX origin time value (ex. 1478...) +1047 * @return {String} UTC time string +1048 * @since jws 3.0.1 +1049 * @description +1050 * This method provides UTC time string for UNIX origin time value. +1051 * @example +1052 * KJUR.jws.IntDate.intDate2UTCString(1478...) => "2015 Oct ..." +1053 */ +1054 KJUR.jws.IntDate.intDate2UTCString = function(intDate) { +1055 var d = new Date(intDate * 1000); +1056 return d.toUTCString(); +1057 }; +1058 +1059 /** +1060 * get UTC time string from UNIX origin time value +1061 * @name intDate2Zulu +1062 * @memberOf KJUR.jws.IntDate +1063 * @function +1064 * @static +1065 * @param {Integer} intDate UNIX origin time value (ex. 1478...) +1066 * @return {String} Zulu time string +1067 * @since jws 3.0.1 +1068 * @description +1069 * This method provides Zulu time string for UNIX origin time value. +1070 * @example +1071 * KJUR.jws.IntDate.intDate2UTCString(1478...) => "20151012...Z" +1072 */ +1073 KJUR.jws.IntDate.intDate2Zulu = function(intDate) { +1074 var d = new Date(intDate * 1000); +1075 var year = ("0000" + d.getUTCFullYear()).slice(-4); +1076 var mon = ("00" + (d.getUTCMonth() + 1)).slice(-2); +1077 var day = ("00" + d.getUTCDate()).slice(-2); +1078 var hour = ("00" + d.getUTCHours()).slice(-2); +1079 var min = ("00" + d.getUTCMinutes()).slice(-2); +1080 var sec = ("00" + d.getUTCSeconds()).slice(-2); +1081 return year + mon + day + hour + min + sec + "Z"; +1082 }; +1083 +1084
    \ No newline at end of file diff --git a/api/symbols/src/keyutil-1.0.js.html b/api/symbols/src/keyutil-1.0.js.html index 24a7fff5..a6326089 100644 --- a/api/symbols/src/keyutil-1.0.js.html +++ b/api/symbols/src/keyutil-1.0.js.html @@ -5,7 +5,7 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! keyutil-1.0.13.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! keyutil-1.0.14.js (c) 2013-2016 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.13 (2016-Aug-11)
    + 18  * @version keyutil 1.0.14 (2016-Oct-08)
      19  * @since jsrsasign 4.1.4
      20  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      21  */
    @@ -180,1925 +180,1927 @@
     173 
     174     var _parsePKCS5PEM = function(sPKCS5PEM) {
     175         var info = {};
    -176         if (sPKCS5PEM.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)", "m"))) {
    -177             info.cipher = RegExp.$1;
    -178             info.ivsalt = RegExp.$2;
    -179         }
    -180         if (sPKCS5PEM.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"))) {
    -181             info.type = RegExp.$1;
    -182         }
    -183         var i1 = -1;
    -184         var lenNEWLINE = 0;
    -185         if (sPKCS5PEM.indexOf("\r\n\r\n") != -1) {
    -186             i1 = sPKCS5PEM.indexOf("\r\n\r\n");
    -187             lenNEWLINE = 2;
    -188         }
    -189         if (sPKCS5PEM.indexOf("\n\n") != -1) {
    -190             i1 = sPKCS5PEM.indexOf("\n\n");
    -191             lenNEWLINE = 1;
    -192         }
    -193         var i2 = sPKCS5PEM.indexOf("-----END");
    -194         if (i1 != -1 && i2 != -1) {
    -195             var s = sPKCS5PEM.substring(i1 + lenNEWLINE * 2, i2 - lenNEWLINE);
    -196             s = s.replace(/\s+/g, '');
    -197             info.data = s;
    -198         }
    -199         return info;
    -200     };
    -201 
    -202     var _getKeyAndUnusedIvByPasscodeAndIvsalt = function(algName, passcode, ivsaltHex) {
    -203         //alert("ivsaltHex(2) = " + ivsaltHex);
    -204         var saltHex = ivsaltHex.substring(0, 16);
    -205         //alert("salt = " + saltHex);
    -206         
    -207         var salt = CryptoJS.enc.Hex.parse(saltHex);
    -208         var data = CryptoJS.enc.Utf8.parse(passcode);
    -209         //alert("salt = " + salt);
    -210         //alert("data = " + data);
    -211 
    -212         var nRequiredBytes = ALGLIST[algName]['keylen'] + ALGLIST[algName]['ivlen'];
    -213         var hHexValueJoined = '';
    -214         var hLastValue = null;
    -215         //alert("nRequiredBytes = " + nRequiredBytes);
    -216         for (;;) {
    -217             var h = CryptoJS.algo.MD5.create();
    -218             if (hLastValue != null) {
    -219                 h.update(hLastValue);
    -220             }
    -221             h.update(data);
    -222             h.update(salt);
    -223             hLastValue = h.finalize();
    -224             hHexValueJoined = hHexValueJoined + CryptoJS.enc.Hex.stringify(hLastValue);
    -225             //alert("joined = " + hHexValueJoined);
    -226             if (hHexValueJoined.length >= nRequiredBytes * 2) {
    -227                 break;
    -228             }
    -229         }
    -230         var result = {};
    -231         result.keyhex = hHexValueJoined.substr(0, ALGLIST[algName]['keylen'] * 2);
    -232         result.ivhex = hHexValueJoined.substr(ALGLIST[algName]['keylen'] * 2, ALGLIST[algName]['ivlen'] * 2);
    -233         return result;
    -234     };
    -235 
    -236     /*
    -237      * @param {String} privateKeyB64 base64 string of encrypted private key
    -238      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    -239      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    -240      * @param {String} ivsaltHex hexadecimal string of IV and salt
    -241      * @param {String} hexadecimal string of decrypted private key
    -242      */
    -243     var _decryptKeyB64 = function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    -244         var privateKeyWA = CryptoJS.enc.Base64.parse(privateKeyB64);
    -245         var privateKeyHex = CryptoJS.enc.Hex.stringify(privateKeyWA);
    -246         var f = ALGLIST[sharedKeyAlgName]['proc'];
    -247         var decryptedKeyHex = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    -248         return decryptedKeyHex;
    -249     };
    -250     
    -251     /*
    -252      * @param {String} privateKeyHex hexadecimal string of private key
    -253      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    -254      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    -255      * @param {String} ivsaltHex hexadecimal string of IV and salt
    -256      * @param {String} base64 string of encrypted private key
    -257      */
    -258     var _encryptKeyHex = function(privateKeyHex, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    -259         var f = ALGLIST[sharedKeyAlgName]['eproc'];
    -260         var encryptedKeyB64 = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    -261         return encryptedKeyB64;
    -262     };
    -263 
    -264     // *****************************************************************
    -265     // *** PUBLIC PROPERTIES AND METHODS *******************************
    +176         var matchResult1 = sPKCS5PEM.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)", "m"));
    +177         if (matchResult1) {
    +178             info.cipher = matchResult1[1];
    +179             info.ivsalt = matchResult1[2];
    +180         }
    +181         var matchResult2 = sPKCS5PEM.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));
    +182         if (matchResult2) {
    +183             info.type = matchResult2[1];
    +184         }
    +185         var i1 = -1;
    +186         var lenNEWLINE = 0;
    +187         if (sPKCS5PEM.indexOf("\r\n\r\n") != -1) {
    +188             i1 = sPKCS5PEM.indexOf("\r\n\r\n");
    +189             lenNEWLINE = 2;
    +190         }
    +191         if (sPKCS5PEM.indexOf("\n\n") != -1) {
    +192             i1 = sPKCS5PEM.indexOf("\n\n");
    +193             lenNEWLINE = 1;
    +194         }
    +195         var i2 = sPKCS5PEM.indexOf("-----END");
    +196         if (i1 != -1 && i2 != -1) {
    +197             var s = sPKCS5PEM.substring(i1 + lenNEWLINE * 2, i2 - lenNEWLINE);
    +198             s = s.replace(/\s+/g, '');
    +199             info.data = s;
    +200         }
    +201         return info;
    +202     };
    +203 
    +204     var _getKeyAndUnusedIvByPasscodeAndIvsalt = function(algName, passcode, ivsaltHex) {
    +205         //alert("ivsaltHex(2) = " + ivsaltHex);
    +206         var saltHex = ivsaltHex.substring(0, 16);
    +207         //alert("salt = " + saltHex);
    +208         
    +209         var salt = CryptoJS.enc.Hex.parse(saltHex);
    +210         var data = CryptoJS.enc.Utf8.parse(passcode);
    +211         //alert("salt = " + salt);
    +212         //alert("data = " + data);
    +213 
    +214         var nRequiredBytes = ALGLIST[algName]['keylen'] + ALGLIST[algName]['ivlen'];
    +215         var hHexValueJoined = '';
    +216         var hLastValue = null;
    +217         //alert("nRequiredBytes = " + nRequiredBytes);
    +218         for (;;) {
    +219             var h = CryptoJS.algo.MD5.create();
    +220             if (hLastValue != null) {
    +221                 h.update(hLastValue);
    +222             }
    +223             h.update(data);
    +224             h.update(salt);
    +225             hLastValue = h.finalize();
    +226             hHexValueJoined = hHexValueJoined + CryptoJS.enc.Hex.stringify(hLastValue);
    +227             //alert("joined = " + hHexValueJoined);
    +228             if (hHexValueJoined.length >= nRequiredBytes * 2) {
    +229                 break;
    +230             }
    +231         }
    +232         var result = {};
    +233         result.keyhex = hHexValueJoined.substr(0, ALGLIST[algName]['keylen'] * 2);
    +234         result.ivhex = hHexValueJoined.substr(ALGLIST[algName]['keylen'] * 2, ALGLIST[algName]['ivlen'] * 2);
    +235         return result;
    +236     };
    +237 
    +238     /*
    +239      * @param {String} privateKeyB64 base64 string of encrypted private key
    +240      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    +241      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    +242      * @param {String} ivsaltHex hexadecimal string of IV and salt
    +243      * @param {String} hexadecimal string of decrypted private key
    +244      */
    +245     var _decryptKeyB64 = function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    +246         var privateKeyWA = CryptoJS.enc.Base64.parse(privateKeyB64);
    +247         var privateKeyHex = CryptoJS.enc.Hex.stringify(privateKeyWA);
    +248         var f = ALGLIST[sharedKeyAlgName]['proc'];
    +249         var decryptedKeyHex = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    +250         return decryptedKeyHex;
    +251     };
    +252     
    +253     /*
    +254      * @param {String} privateKeyHex hexadecimal string of private key
    +255      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    +256      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    +257      * @param {String} ivsaltHex hexadecimal string of IV and salt
    +258      * @param {String} base64 string of encrypted private key
    +259      */
    +260     var _encryptKeyHex = function(privateKeyHex, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    +261         var f = ALGLIST[sharedKeyAlgName]['eproc'];
    +262         var encryptedKeyB64 = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    +263         return encryptedKeyB64;
    +264     };
    +265 
     266     // *****************************************************************
    -267     return {
    -268         // -- UTILITY METHODS ------------------------------------------------------------
    -269         /**
    -270          * decrypt private key by shared key
    -271          * @name version
    -272          * @memberOf KEYUTIL
    -273          * @property {String} version
    -274          * @description version string of KEYUTIL class
    -275          */
    -276         version: "1.0.0",
    -277 
    -278         /**
    -279          * get hexacedimal string of PEM format
    -280          * @name getHexFromPEM
    -281          * @memberOf KEYUTIL
    -282          * @function
    -283          * @param {String} sPEM PEM formatted string
    -284          * @param {String} sHead PEM header string without BEGIN/END
    -285          * @return {String} hexadecimal string data of PEM contents
    -286          * @since pkcs5pkey 1.0.5
    -287          */
    -288         getHexFromPEM: function(sPEM, sHead) {
    -289             var s = sPEM;
    -290             if (s.indexOf("-----BEGIN ") == -1) {
    -291                 throw "can't find PEM header: " + sHead;
    -292             }
    -293             if (typeof sHead == "string" && sHead != "") {
    -294                 s = s.replace("-----BEGIN " + sHead + "-----", "");
    -295                 s = s.replace("-----END " + sHead + "-----", "");
    -296             } else {
    -297                 s = s.replace(/-----BEGIN [^-]+-----/, '');
    -298                 s = s.replace(/-----END [^-]+-----/, '');
    -299             }
    -300             var sB64 = s.replace(/\s+/g, '');
    -301             var dataHex = b64tohex(sB64);
    -302             return dataHex;
    -303         },
    -304 
    -305         /**
    -306          * decrypt private key by shared key
    -307          * @name getDecryptedKeyHexByKeyIV
    -308          * @memberOf KEYUTIL
    -309          * @function
    -310          * @param {String} encryptedKeyHex hexadecimal string of encrypted private key
    -311          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    -312          * @param {String} sharedKeyHex hexadecimal string of symmetric key
    -313          * @param {String} ivHex hexadecimal string of initial vector(IV).
    -314          * @return {String} hexadecimal string of decrypted privated key
    -315          */
    -316         getDecryptedKeyHexByKeyIV: function(encryptedKeyHex, algName, sharedKeyHex, ivHex) {
    -317             var f1 = getFuncByName(algName);
    -318             return f1(encryptedKeyHex, sharedKeyHex, ivHex);
    -319         },
    -320 
    -321         /**
    -322          * parse PEM formatted passcode protected PKCS#5 private key
    -323          * @name parsePKCS5PEM
    -324          * @memberOf KEYUTIL
    -325          * @function
    -326          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    -327          * @return {Hash} hash of key information
    -328          * @description
    -329          * Resulted hash has following attributes.
    -330          * <ul>
    -331          * <li>cipher - symmetric key algorithm name (ex. 'DES-EBE3-CBC', 'AES-256-CBC')</li>
    -332          * <li>ivsalt - IV used for decrypt. Its heading 8 bytes will be used for passcode salt.</li>
    -333          * <li>type - asymmetric key algorithm name of private key described in PEM header.</li>
    -334          * <li>data - base64 encoded encrypted private key.</li>
    -335          * </ul>
    -336          *
    -337          */
    -338         parsePKCS5PEM: function(sPKCS5PEM) {
    -339             return _parsePKCS5PEM(sPKCS5PEM);
    -340         },
    -341 
    -342         /**
    -343          * the same function as OpenSSL EVP_BytsToKey to generate shared key and IV
    -344          * @name getKeyAndUnusedIvByPasscodeAndIvsalt
    -345          * @memberOf KEYUTIL
    -346          * @function
    -347          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    -348          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    -349          * @param {String} hexadecimal string of IV. heading 8 bytes will be used for passcode salt
    -350          * @return {Hash} hash of key and unused IV (ex. {keyhex:2fe3..., ivhex:3fad..})
    -351          */
    -352         getKeyAndUnusedIvByPasscodeAndIvsalt: function(algName, passcode, ivsaltHex) {
    -353             return _getKeyAndUnusedIvByPasscodeAndIvsalt(algName, passcode, ivsaltHex);
    -354         },
    -355 
    -356         decryptKeyB64: function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    -357             return _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    -358         },
    -359 
    -360         /**
    -361          * decrypt PEM formatted protected PKCS#5 private key with passcode
    -362          * @name getDecryptedKeyHex
    -363          * @memberOf KEYUTIL
    -364          * @function
    -365          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    -366          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    -367          * @return {String} hexadecimal string of decrypted RSA priavte key
    -368          */
    -369         getDecryptedKeyHex: function(sEncryptedPEM, passcode) {
    -370             // 1. parse pem
    -371             var info = _parsePKCS5PEM(sEncryptedPEM);
    -372             var publicKeyAlgName = info.type;
    -373             var sharedKeyAlgName = info.cipher;
    -374             var ivsaltHex = info.ivsalt;
    -375             var privateKeyB64 = info.data;
    -376             //alert("ivsaltHex = " + ivsaltHex);
    -377 
    -378             // 2. generate shared key
    -379             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    -380             var sharedKeyHex = sharedKeyInfo.keyhex;
    -381             //alert("sharedKeyHex = " + sharedKeyHex);
    -382 
    -383             // 3. decrypt private key
    -384             var decryptedKey = _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    -385             return decryptedKey;
    -386         },
    -387 
    -388         /**
    -389          * (DEPRECATED) read PEM formatted encrypted PKCS#5 private key and returns RSAKey object
    -390          * @name getRSAKeyFromEncryptedPKCS5PEM
    -391          * @memberOf KEYUTIL
    -392          * @function
    -393          * @param {String} sEncryptedP5PEM PEM formatted encrypted PKCS#5 private key
    -394          * @param {String} passcode passcode to decrypt private key
    -395          * @return {RSAKey} loaded RSAKey object of RSA private key
    -396          * @since pkcs5pkey 1.0.2
    -397          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -398          */
    -399         getRSAKeyFromEncryptedPKCS5PEM: function(sEncryptedP5PEM, passcode) {
    -400             var hPKey = this.getDecryptedKeyHex(sEncryptedP5PEM, passcode);
    -401             var rsaKey = new RSAKey();
    -402             rsaKey.readPrivateKeyFromASN1HexString(hPKey);
    -403             return rsaKey;
    -404         },
    -405 
    -406         /*
    -407          * get PEM formatted encrypted PKCS#5 private key from hexadecimal string of plain private key
    -408          * @name getEncryptedPKCS5PEMFromPrvKeyHex
    -409          * @memberOf KEYUTIL
    -410          * @function
    -411          * @param {String} pemHeadAlg algorithm name in the pem header (i.e. RSA,EC or DSA)
    -412          * @param {String} hPrvKey hexadecimal string of plain private key
    -413          * @param {String} passcode pass code to protect private key (ex. password)
    -414          * @param {String} sharedKeyAlgName algorithm name to protect private key (ex. AES-256-CBC)
    -415          * @param {String} ivsaltHex hexadecimal string of IV and salt
    -416          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    -417          * @since pkcs5pkey 1.0.2
    -418          * @description
    -419          * <br/>
    -420          * generate PEM formatted encrypted PKCS#5 private key by hexadecimal string encoded
    -421          * ASN.1 object of plain RSA private key.
    -422          * Following arguments can be omitted.
    -423          * <ul>
    -424          * <li>alg - AES-256-CBC will be used if omitted.</li>
    -425          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    -426          * </ul>
    -427          * NOTE1: DES-CBC, DES-EDE3-CBC, AES-{128,192.256}-CBC algorithm are supported.
    -428          * @example
    -429          * var pem = 
    -430          *   KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password");
    -431          * var pem2 = 
    -432          *   KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC");
    -433          * var pem3 = 
    -434          *   KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC", "1f3d02...");
    -435          */
    -436         getEncryptedPKCS5PEMFromPrvKeyHex: function(pemHeadAlg, hPrvKey, passcode, sharedKeyAlgName, ivsaltHex) {
    -437             var sPEM = "";
    -438 
    -439             // 1. set sharedKeyAlgName if undefined (default AES-256-CBC)
    -440             if (typeof sharedKeyAlgName == "undefined" || sharedKeyAlgName == null) {
    -441                 sharedKeyAlgName = "AES-256-CBC";
    -442             }
    -443             if (typeof ALGLIST[sharedKeyAlgName] == "undefined")
    -444                 throw "KEYUTIL unsupported algorithm: " + sharedKeyAlgName;
    -445 
    -446             // 2. set ivsaltHex if undefined
    -447             if (typeof ivsaltHex == "undefined" || ivsaltHex == null) {
    -448                 var ivlen = ALGLIST[sharedKeyAlgName]['ivlen'];
    -449                 var randIV = _generateIvSaltHex(ivlen);
    -450                 ivsaltHex = randIV.toUpperCase();
    -451             }
    -452 
    -453             // 3. get shared key
    -454             //alert("ivsalthex=" + ivsaltHex);
    -455             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    -456             var sharedKeyHex = sharedKeyInfo.keyhex;
    -457             // alert("sharedKeyHex = " + sharedKeyHex);
    -458 
    -459             // 3. get encrypted Key in Base64
    -460             var encryptedKeyB64 = _encryptKeyHex(hPrvKey, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    -461 
    -462             var pemBody = encryptedKeyB64.replace(/(.{64})/g, "$1\r\n");
    -463             var sPEM = "-----BEGIN " + pemHeadAlg + " PRIVATE KEY-----\r\n";
    -464             sPEM += "Proc-Type: 4,ENCRYPTED\r\n";
    -465             sPEM += "DEK-Info: " + sharedKeyAlgName + "," + ivsaltHex + "\r\n";
    -466             sPEM += "\r\n";
    -467             sPEM += pemBody;
    -468             sPEM += "\r\n-----END " + pemHeadAlg + " PRIVATE KEY-----\r\n";
    -469 
    -470             return sPEM;
    -471         },
    -472 
    -473         /**
    -474          * (DEPRECATED) get PEM formatted encrypted PKCS#5 private key from RSAKey object of private key
    -475          * @name getEncryptedPKCS5PEMFromRSAKey
    -476          * @memberOf KEYUTIL
    -477          * @function
    -478          * @param {RSAKey} pKey RSAKey object of private key
    -479          * @param {String} passcode pass code to protect private key (ex. password)
    -480          * @param {String} alg algorithm name to protect private key (default AES-256-CBC)
    -481          * @param {String} ivsaltHex hexadecimal string of IV and salt (default generated random IV)
    -482          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    -483          * @since pkcs5pkey 1.0.2
    -484          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getPEM#}.
    -485          * @description
    -486          * <br/>
    -487          * generate PEM formatted encrypted PKCS#5 private key by
    -488          * {@link RSAKey} object of RSA private key and passcode.
    -489          * Following argument can be omitted.
    -490          * <ul>
    -491          * <li>alg - AES-256-CBC will be used if omitted.</li>
    -492          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    -493          * </ul>
    -494          * @example
    -495          * var pkey = new RSAKey();
    -496          * pkey.generate(1024, '10001'); // generate 1024bit RSA private key with public exponent 'x010001'
    -497          * var pem = KEYUTIL.getEncryptedPKCS5PEMFromRSAKey(pkey, "password");
    -498          */
    -499         getEncryptedPKCS5PEMFromRSAKey: function(pKey, passcode, alg, ivsaltHex) {
    -500             var version = new KJUR.asn1.DERInteger({'int': 0});
    -501             var n = new KJUR.asn1.DERInteger({'bigint': pKey.n});
    -502             var e = new KJUR.asn1.DERInteger({'int': pKey.e});
    -503             var d = new KJUR.asn1.DERInteger({'bigint': pKey.d});
    -504             var p = new KJUR.asn1.DERInteger({'bigint': pKey.p});
    -505             var q = new KJUR.asn1.DERInteger({'bigint': pKey.q});
    -506             var dmp1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmp1});
    -507             var dmq1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmq1});
    -508             var coeff = new KJUR.asn1.DERInteger({'bigint': pKey.coeff});
    -509             var seq = new KJUR.asn1.DERSequence({'array': [version, n, e, d, p, q, dmp1, dmq1, coeff]});
    -510             var hex = seq.getEncodedHex();
    -511             return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", hex, passcode, alg, ivsaltHex);
    -512         },
    -513 
    -514         /**
    -515          * generate RSAKey and PEM formatted encrypted PKCS#5 private key
    -516          * @name newEncryptedPKCS5PEM
    -517          * @memberOf KEYUTIL
    -518          * @function
    -519          * @param {String} passcode pass code to protect private key (ex. password)
    -520          * @param {Integer} keyLen key bit length of RSA key to be generated. (default 1024)
    -521          * @param {String} hPublicExponent hexadecimal string of public exponent (default 10001)
    -522          * @param {String} alg shared key algorithm to encrypt private key (default AES-258-CBC)
    -523          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    -524          * @since pkcs5pkey 1.0.2
    -525          * @example
    -526          * var pem1 = KEYUTIL.newEncryptedPKCS5PEM("password");           // RSA1024bit/10001/AES-256-CBC
    -527          * var pem2 = KEYUTIL.newEncryptedPKCS5PEM("password", 512);      // RSA 512bit/10001/AES-256-CBC
    -528          * var pem3 = KEYUTIL.newEncryptedPKCS5PEM("password", 512, '3'); // RSA 512bit/    3/AES-256-CBC
    -529          */
    -530         newEncryptedPKCS5PEM: function(passcode, keyLen, hPublicExponent, alg) {
    -531             if (typeof keyLen == "undefined" || keyLen == null) {
    -532                 keyLen = 1024;
    -533             }
    -534             if (typeof hPublicExponent == "undefined" || hPublicExponent == null) {
    -535                 hPublicExponent = '10001';
    -536             }
    -537             var pKey = new RSAKey();
    -538             pKey.generate(keyLen, hPublicExponent);
    -539             var pem = null;
    -540             if (typeof alg == "undefined" || alg == null) {
    -541                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pKey, passcode);
    -542             } else {
    -543                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pKey, passcode, alg);
    -544             }
    -545             return pem;
    -546         },
    -547 
    -548         // === PKCS8 ===============================================================
    +267     // *** PUBLIC PROPERTIES AND METHODS *******************************
    +268     // *****************************************************************
    +269     return {
    +270         // -- UTILITY METHODS ------------------------------------------------------------
    +271         /**
    +272          * decrypt private key by shared key
    +273          * @name version
    +274          * @memberOf KEYUTIL
    +275          * @property {String} version
    +276          * @description version string of KEYUTIL class
    +277          */
    +278         version: "1.0.0",
    +279 
    +280         /**
    +281          * get hexacedimal string of PEM format
    +282          * @name getHexFromPEM
    +283          * @memberOf KEYUTIL
    +284          * @function
    +285          * @param {String} sPEM PEM formatted string
    +286          * @param {String} sHead PEM header string without BEGIN/END
    +287          * @return {String} hexadecimal string data of PEM contents
    +288          * @since pkcs5pkey 1.0.5
    +289          */
    +290         getHexFromPEM: function(sPEM, sHead) {
    +291             var s = sPEM;
    +292             if (s.indexOf("-----BEGIN ") == -1) {
    +293                 throw "can't find PEM header: " + sHead;
    +294             }
    +295             if (typeof sHead == "string" && sHead != "") {
    +296                 s = s.replace("-----BEGIN " + sHead + "-----", "");
    +297                 s = s.replace("-----END " + sHead + "-----", "");
    +298             } else {
    +299                 s = s.replace(/-----BEGIN [^-]+-----/, '');
    +300                 s = s.replace(/-----END [^-]+-----/, '');
    +301             }
    +302             var sB64 = s.replace(/\s+/g, '');
    +303             var dataHex = b64tohex(sB64);
    +304             return dataHex;
    +305         },
    +306 
    +307         /**
    +308          * decrypt private key by shared key
    +309          * @name getDecryptedKeyHexByKeyIV
    +310          * @memberOf KEYUTIL
    +311          * @function
    +312          * @param {String} encryptedKeyHex hexadecimal string of encrypted private key
    +313          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    +314          * @param {String} sharedKeyHex hexadecimal string of symmetric key
    +315          * @param {String} ivHex hexadecimal string of initial vector(IV).
    +316          * @return {String} hexadecimal string of decrypted privated key
    +317          */
    +318         getDecryptedKeyHexByKeyIV: function(encryptedKeyHex, algName, sharedKeyHex, ivHex) {
    +319             var f1 = getFuncByName(algName);
    +320             return f1(encryptedKeyHex, sharedKeyHex, ivHex);
    +321         },
    +322 
    +323         /**
    +324          * parse PEM formatted passcode protected PKCS#5 private key
    +325          * @name parsePKCS5PEM
    +326          * @memberOf KEYUTIL
    +327          * @function
    +328          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    +329          * @return {Hash} hash of key information
    +330          * @description
    +331          * Resulted hash has following attributes.
    +332          * <ul>
    +333          * <li>cipher - symmetric key algorithm name (ex. 'DES-EBE3-CBC', 'AES-256-CBC')</li>
    +334          * <li>ivsalt - IV used for decrypt. Its heading 8 bytes will be used for passcode salt.</li>
    +335          * <li>type - asymmetric key algorithm name of private key described in PEM header.</li>
    +336          * <li>data - base64 encoded encrypted private key.</li>
    +337          * </ul>
    +338          *
    +339          */
    +340         parsePKCS5PEM: function(sPKCS5PEM) {
    +341             return _parsePKCS5PEM(sPKCS5PEM);
    +342         },
    +343 
    +344         /**
    +345          * the same function as OpenSSL EVP_BytsToKey to generate shared key and IV
    +346          * @name getKeyAndUnusedIvByPasscodeAndIvsalt
    +347          * @memberOf KEYUTIL
    +348          * @function
    +349          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    +350          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    +351          * @param {String} hexadecimal string of IV. heading 8 bytes will be used for passcode salt
    +352          * @return {Hash} hash of key and unused IV (ex. {keyhex:2fe3..., ivhex:3fad..})
    +353          */
    +354         getKeyAndUnusedIvByPasscodeAndIvsalt: function(algName, passcode, ivsaltHex) {
    +355             return _getKeyAndUnusedIvByPasscodeAndIvsalt(algName, passcode, ivsaltHex);
    +356         },
    +357 
    +358         decryptKeyB64: function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    +359             return _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    +360         },
    +361 
    +362         /**
    +363          * decrypt PEM formatted protected PKCS#5 private key with passcode
    +364          * @name getDecryptedKeyHex
    +365          * @memberOf KEYUTIL
    +366          * @function
    +367          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    +368          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    +369          * @return {String} hexadecimal string of decrypted RSA priavte key
    +370          */
    +371         getDecryptedKeyHex: function(sEncryptedPEM, passcode) {
    +372             // 1. parse pem
    +373             var info = _parsePKCS5PEM(sEncryptedPEM);
    +374             var publicKeyAlgName = info.type;
    +375             var sharedKeyAlgName = info.cipher;
    +376             var ivsaltHex = info.ivsalt;
    +377             var privateKeyB64 = info.data;
    +378             //alert("ivsaltHex = " + ivsaltHex);
    +379 
    +380             // 2. generate shared key
    +381             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    +382             var sharedKeyHex = sharedKeyInfo.keyhex;
    +383             //alert("sharedKeyHex = " + sharedKeyHex);
    +384 
    +385             // 3. decrypt private key
    +386             var decryptedKey = _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    +387             return decryptedKey;
    +388         },
    +389 
    +390         /**
    +391          * (DEPRECATED) read PEM formatted encrypted PKCS#5 private key and returns RSAKey object
    +392          * @name getRSAKeyFromEncryptedPKCS5PEM
    +393          * @memberOf KEYUTIL
    +394          * @function
    +395          * @param {String} sEncryptedP5PEM PEM formatted encrypted PKCS#5 private key
    +396          * @param {String} passcode passcode to decrypt private key
    +397          * @return {RSAKey} loaded RSAKey object of RSA private key
    +398          * @since pkcs5pkey 1.0.2
    +399          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +400          */
    +401         getRSAKeyFromEncryptedPKCS5PEM: function(sEncryptedP5PEM, passcode) {
    +402             var hPKey = this.getDecryptedKeyHex(sEncryptedP5PEM, passcode);
    +403             var rsaKey = new RSAKey();
    +404             rsaKey.readPrivateKeyFromASN1HexString(hPKey);
    +405             return rsaKey;
    +406         },
    +407 
    +408         /*
    +409          * get PEM formatted encrypted PKCS#5 private key from hexadecimal string of plain private key
    +410          * @name getEncryptedPKCS5PEMFromPrvKeyHex
    +411          * @memberOf KEYUTIL
    +412          * @function
    +413          * @param {String} pemHeadAlg algorithm name in the pem header (i.e. RSA,EC or DSA)
    +414          * @param {String} hPrvKey hexadecimal string of plain private key
    +415          * @param {String} passcode pass code to protect private key (ex. password)
    +416          * @param {String} sharedKeyAlgName algorithm name to protect private key (ex. AES-256-CBC)
    +417          * @param {String} ivsaltHex hexadecimal string of IV and salt
    +418          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    +419          * @since pkcs5pkey 1.0.2
    +420          * @description
    +421          * <br/>
    +422          * generate PEM formatted encrypted PKCS#5 private key by hexadecimal string encoded
    +423          * ASN.1 object of plain RSA private key.
    +424          * Following arguments can be omitted.
    +425          * <ul>
    +426          * <li>alg - AES-256-CBC will be used if omitted.</li>
    +427          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    +428          * </ul>
    +429          * NOTE1: DES-CBC, DES-EDE3-CBC, AES-{128,192.256}-CBC algorithm are supported.
    +430          * @example
    +431          * var pem = 
    +432          *   KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password");
    +433          * var pem2 = 
    +434          *   KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC");
    +435          * var pem3 = 
    +436          *   KEYUTIL.getEncryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC", "1f3d02...");
    +437          */
    +438         getEncryptedPKCS5PEMFromPrvKeyHex: function(pemHeadAlg, hPrvKey, passcode, sharedKeyAlgName, ivsaltHex) {
    +439             var sPEM = "";
    +440 
    +441             // 1. set sharedKeyAlgName if undefined (default AES-256-CBC)
    +442             if (typeof sharedKeyAlgName == "undefined" || sharedKeyAlgName == null) {
    +443                 sharedKeyAlgName = "AES-256-CBC";
    +444             }
    +445             if (typeof ALGLIST[sharedKeyAlgName] == "undefined")
    +446                 throw "KEYUTIL unsupported algorithm: " + sharedKeyAlgName;
    +447 
    +448             // 2. set ivsaltHex if undefined
    +449             if (typeof ivsaltHex == "undefined" || ivsaltHex == null) {
    +450                 var ivlen = ALGLIST[sharedKeyAlgName]['ivlen'];
    +451                 var randIV = _generateIvSaltHex(ivlen);
    +452                 ivsaltHex = randIV.toUpperCase();
    +453             }
    +454 
    +455             // 3. get shared key
    +456             //alert("ivsalthex=" + ivsaltHex);
    +457             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    +458             var sharedKeyHex = sharedKeyInfo.keyhex;
    +459             // alert("sharedKeyHex = " + sharedKeyHex);
    +460 
    +461             // 3. get encrypted Key in Base64
    +462             var encryptedKeyB64 = _encryptKeyHex(hPrvKey, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    +463 
    +464             var pemBody = encryptedKeyB64.replace(/(.{64})/g, "$1\r\n");
    +465             var sPEM = "-----BEGIN " + pemHeadAlg + " PRIVATE KEY-----\r\n";
    +466             sPEM += "Proc-Type: 4,ENCRYPTED\r\n";
    +467             sPEM += "DEK-Info: " + sharedKeyAlgName + "," + ivsaltHex + "\r\n";
    +468             sPEM += "\r\n";
    +469             sPEM += pemBody;
    +470             sPEM += "\r\n-----END " + pemHeadAlg + " PRIVATE KEY-----\r\n";
    +471 
    +472             return sPEM;
    +473         },
    +474 
    +475         /**
    +476          * (DEPRECATED) get PEM formatted encrypted PKCS#5 private key from RSAKey object of private key
    +477          * @name getEncryptedPKCS5PEMFromRSAKey
    +478          * @memberOf KEYUTIL
    +479          * @function
    +480          * @param {RSAKey} pKey RSAKey object of private key
    +481          * @param {String} passcode pass code to protect private key (ex. password)
    +482          * @param {String} alg algorithm name to protect private key (default AES-256-CBC)
    +483          * @param {String} ivsaltHex hexadecimal string of IV and salt (default generated random IV)
    +484          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    +485          * @since pkcs5pkey 1.0.2
    +486          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getPEM#}.
    +487          * @description
    +488          * <br/>
    +489          * generate PEM formatted encrypted PKCS#5 private key by
    +490          * {@link RSAKey} object of RSA private key and passcode.
    +491          * Following argument can be omitted.
    +492          * <ul>
    +493          * <li>alg - AES-256-CBC will be used if omitted.</li>
    +494          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    +495          * </ul>
    +496          * @example
    +497          * var pkey = new RSAKey();
    +498          * pkey.generate(1024, '10001'); // generate 1024bit RSA private key with public exponent 'x010001'
    +499          * var pem = KEYUTIL.getEncryptedPKCS5PEMFromRSAKey(pkey, "password");
    +500          */
    +501         getEncryptedPKCS5PEMFromRSAKey: function(pKey, passcode, alg, ivsaltHex) {
    +502             var version = new KJUR.asn1.DERInteger({'int': 0});
    +503             var n = new KJUR.asn1.DERInteger({'bigint': pKey.n});
    +504             var e = new KJUR.asn1.DERInteger({'int': pKey.e});
    +505             var d = new KJUR.asn1.DERInteger({'bigint': pKey.d});
    +506             var p = new KJUR.asn1.DERInteger({'bigint': pKey.p});
    +507             var q = new KJUR.asn1.DERInteger({'bigint': pKey.q});
    +508             var dmp1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmp1});
    +509             var dmq1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmq1});
    +510             var coeff = new KJUR.asn1.DERInteger({'bigint': pKey.coeff});
    +511             var seq = new KJUR.asn1.DERSequence({'array': [version, n, e, d, p, q, dmp1, dmq1, coeff]});
    +512             var hex = seq.getEncodedHex();
    +513             return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", hex, passcode, alg, ivsaltHex);
    +514         },
    +515 
    +516         /**
    +517          * generate RSAKey and PEM formatted encrypted PKCS#5 private key
    +518          * @name newEncryptedPKCS5PEM
    +519          * @memberOf KEYUTIL
    +520          * @function
    +521          * @param {String} passcode pass code to protect private key (ex. password)
    +522          * @param {Integer} keyLen key bit length of RSA key to be generated. (default 1024)
    +523          * @param {String} hPublicExponent hexadecimal string of public exponent (default 10001)
    +524          * @param {String} alg shared key algorithm to encrypt private key (default AES-258-CBC)
    +525          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    +526          * @since pkcs5pkey 1.0.2
    +527          * @example
    +528          * var pem1 = KEYUTIL.newEncryptedPKCS5PEM("password");           // RSA1024bit/10001/AES-256-CBC
    +529          * var pem2 = KEYUTIL.newEncryptedPKCS5PEM("password", 512);      // RSA 512bit/10001/AES-256-CBC
    +530          * var pem3 = KEYUTIL.newEncryptedPKCS5PEM("password", 512, '3'); // RSA 512bit/    3/AES-256-CBC
    +531          */
    +532         newEncryptedPKCS5PEM: function(passcode, keyLen, hPublicExponent, alg) {
    +533             if (typeof keyLen == "undefined" || keyLen == null) {
    +534                 keyLen = 1024;
    +535             }
    +536             if (typeof hPublicExponent == "undefined" || hPublicExponent == null) {
    +537                 hPublicExponent = '10001';
    +538             }
    +539             var pKey = new RSAKey();
    +540             pKey.generate(keyLen, hPublicExponent);
    +541             var pem = null;
    +542             if (typeof alg == "undefined" || alg == null) {
    +543                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pKey, passcode);
    +544             } else {
    +545                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pKey, passcode, alg);
    +546             }
    +547             return pem;
    +548         },
     549 
    -550         /**
    -551          * (DEPRECATED) read PEM formatted unencrypted PKCS#8 private key and returns RSAKey object
    -552          * @name getRSAKeyFromPlainPKCS8PEM
    -553          * @memberOf KEYUTIL
    -554          * @function
    -555          * @param {String} pkcs8PEM PEM formatted unencrypted PKCS#8 private key
    -556          * @return {RSAKey} loaded RSAKey object of RSA private key
    -557          * @since pkcs5pkey 1.0.1
    -558          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -559          */
    -560         getRSAKeyFromPlainPKCS8PEM: function(pkcs8PEM) {
    -561             if (pkcs8PEM.match(/ENCRYPTED/))
    -562                 throw "pem shall be not ENCRYPTED";
    -563             var prvKeyHex = this.getHexFromPEM(pkcs8PEM, "PRIVATE KEY");
    -564             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    -565             return rsaKey;
    -566         },
    -567 
    -568         /**
    -569          * (DEPRECATED) provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    -570          * @name getRSAKeyFromPlainPKCS8Hex
    -571          * @memberOf KEYUTIL
    -572          * @function
    -573          * @param {String} prvKeyHex hexadecimal string of unencrypted PKCS#8 private key
    -574          * @return {RSAKey} loaded RSAKey object of RSA private key
    -575          * @since pkcs5pkey 1.0.3
    -576          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -577          */
    -578         getRSAKeyFromPlainPKCS8Hex: function(prvKeyHex) {
    -579             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(prvKeyHex, 0);
    -580             if (a1.length != 3)
    -581                 throw "outer DERSequence shall have 3 elements: " + a1.length;
    -582             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    -583             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    -584                 throw "PKCS8 AlgorithmIdentifier is not rsaEnc: " + algIdTLV;
    -585             var algIdTLV = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    -586             var octetStr = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[2]);
    -587             var p5KeyHex = ASN1HEX.getHexOfV_AtObj(octetStr, 0);
    -588             //alert(p5KeyHex);
    -589             var rsaKey = new RSAKey();
    -590             rsaKey.readPrivateKeyFromASN1HexString(p5KeyHex);
    -591             return rsaKey;
    -592         },
    -593 
    -594         /**
    -595          * generate PBKDF2 key hexstring with specified passcode and information
    -596          * @name parseHexOfEncryptedPKCS8
    -597          * @memberOf KEYUTIL
    -598          * @function
    -599          * @param {String} passcode passcode to decrypto private key
    -600          * @return {Array} info associative array of PKCS#8 parameters
    -601          * @since pkcs5pkey 1.0.3
    -602          * @description
    -603          * The associative array which is returned by this method has following properties:
    -604          * <ul>
    -605          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    -606          * <li>info.pkbdf2Iter - iteration count</li>
    -607          * <li>info.ciphertext - hexadecimal string of encrypted private key</li>
    -608          * <li>info.encryptionSchemeAlg - encryption algorithm name (currently TripleDES only)</li>
    -609          * <li>info.encryptionSchemeIV - initial vector for encryption algorithm</li>
    -610          * </ul>
    -611          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -612          * <ul>
    -613          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -614          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -615          * </ul>
    -616          * @example
    -617          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -618          * // key with PBKDF2 with TripleDES
    -619          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -620          */
    -621         parseHexOfEncryptedPKCS8: function(sHEX) {
    -622             var info = {};
    -623             
    -624             var a0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, 0);
    -625             if (a0.length != 2)
    -626                 throw "malformed format: SEQUENCE(0).items != 2: " + a0.length;
    -627 
    -628             // 1. ciphertext
    -629             info.ciphertext = ASN1HEX.getHexOfV_AtObj(sHEX, a0[1]);
    -630 
    -631             // 2. pkcs5PBES2
    -632             var a0_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0[0]); 
    -633             if (a0_0.length != 2)
    -634                 throw "malformed format: SEQUENCE(0.0).items != 2: " + a0_0.length;
    -635 
    -636             // 2.1 check if pkcs5PBES2(1 2 840 113549 1 5 13)
    -637             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0[0]) != "2a864886f70d01050d")
    -638                 throw "this only supports pkcs5PBES2";
    -639 
    -640             // 2.2 pkcs5PBES2 param
    -641             var a0_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0[1]); 
    -642             if (a0_0.length != 2)
    -643                 throw "malformed format: SEQUENCE(0.0.1).items != 2: " + a0_0_1.length;
    -644 
    -645             // 2.2.1 encryptionScheme
    -646             var a0_0_1_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[1]); 
    -647             if (a0_0_1_1.length != 2)
    -648                 throw "malformed format: SEQUENCE(0.0.1.1).items != 2: " + a0_0_1_1.length;
    -649             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[0]) != "2a864886f70d0307")
    -650                 throw "this only supports TripleDES";
    -651             info.encryptionSchemeAlg = "TripleDES";
    -652 
    -653             // 2.2.1.1 IV of encryptionScheme
    -654             info.encryptionSchemeIV = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[1]);
    -655 
    -656             // 2.2.2 keyDerivationFunc
    -657             var a0_0_1_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[0]); 
    -658             if (a0_0_1_0.length != 2)
    -659                 throw "malformed format: SEQUENCE(0.0.1.0).items != 2: " + a0_0_1_0.length;
    -660             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0[0]) != "2a864886f70d01050c")
    -661                 throw "this only supports pkcs5PBKDF2";
    -662 
    -663             // 2.2.2.1 pkcs5PBKDF2 param
    -664             var a0_0_1_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1_0[1]); 
    -665             if (a0_0_1_0_1.length < 2)
    -666                 throw "malformed format: SEQUENCE(0.0.1.0.1).items < 2: " + a0_0_1_0_1.length;
    -667 
    -668             // 2.2.2.1.1 PBKDF2 salt
    -669             info.pbkdf2Salt = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[0]);
    -670 
    -671             // 2.2.2.1.2 PBKDF2 iter
    -672             var iterNumHex = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[1]);
    -673             try {
    -674                 info.pbkdf2Iter = parseInt(iterNumHex, 16);
    -675             } catch(ex) {
    -676                 throw "malformed format pbkdf2Iter: " + iterNumHex;
    -677             }
    -678 
    -679             return info;
    -680         },
    -681 
    -682         /**
    -683          * generate PBKDF2 key hexstring with specified passcode and information
    -684          * @name getPBKDF2KeyHexFromParam
    -685          * @memberOf KEYUTIL
    -686          * @function
    -687          * @param {Array} info result of {@link parseHexOfEncryptedPKCS8} which has preference of PKCS#8 file
    -688          * @param {String} passcode passcode to decrypto private key
    -689          * @return {String} hexadecimal string of PBKDF2 key
    -690          * @since pkcs5pkey 1.0.3
    -691          * @description
    -692          * As for info, this uses following properties:
    -693          * <ul>
    -694          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    -695          * <li>info.pkbdf2Iter - iteration count</li>
    -696          * </ul>
    -697          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -698          * <ul>
    -699          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -700          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -701          * </ul>
    -702          * @example
    -703          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -704          * // key with PBKDF2 with TripleDES
    -705          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -706          */
    -707         getPBKDF2KeyHexFromParam: function(info, passcode) {
    -708             var pbkdf2SaltWS = CryptoJS.enc.Hex.parse(info.pbkdf2Salt);
    -709             var pbkdf2Iter = info.pbkdf2Iter;
    -710             var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, 
    -711                                               pbkdf2SaltWS, 
    -712                                               { keySize: 192/32, iterations: pbkdf2Iter });
    -713             var pbkdf2KeyHex = CryptoJS.enc.Hex.stringify(pbkdf2KeyWS);
    -714             return pbkdf2KeyHex;
    -715         },
    -716 
    -717         /**
    -718          * read PEM formatted encrypted PKCS#8 private key and returns hexadecimal string of plain PKCS#8 private key
    -719          * @name getPlainPKCS8HexFromEncryptedPKCS8PEM
    -720          * @memberOf KEYUTIL
    -721          * @function
    -722          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    -723          * @param {String} passcode passcode to decrypto private key
    -724          * @return {String} hexadecimal string of plain PKCS#8 private key
    -725          * @since pkcs5pkey 1.0.3
    -726          * @description
    -727          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -728          * <ul>
    -729          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -730          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -731          * </ul>
    -732          * @example
    -733          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -734          * // key with PBKDF2 with TripleDES
    -735          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -736          */
    -737         getPlainPKCS8HexFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    -738             // 1. derHex - PKCS#8 private key encrypted by PBKDF2
    -739             var derHex = this.getHexFromPEM(pkcs8PEM, "ENCRYPTED PRIVATE KEY");
    -740             // 2. info - PKCS#5 PBES info
    -741             var info = this.parseHexOfEncryptedPKCS8(derHex);
    -742             // 3. hKey - PBKDF2 key
    -743             var pbkdf2KeyHex = KEYUTIL.getPBKDF2KeyHexFromParam(info, passcode);
    -744             // 4. decrypt ciphertext by PBKDF2 key
    -745             var encrypted = {};
    -746             encrypted.ciphertext = CryptoJS.enc.Hex.parse(info.ciphertext);
    -747             var pbkdf2KeyWS = CryptoJS.enc.Hex.parse(pbkdf2KeyHex);
    -748             var des3IVWS = CryptoJS.enc.Hex.parse(info.encryptionSchemeIV);
    -749             var decWS = CryptoJS.TripleDES.decrypt(encrypted, pbkdf2KeyWS, { iv: des3IVWS });
    -750             var decHex = CryptoJS.enc.Hex.stringify(decWS);
    -751             return decHex;
    -752         },
    -753 
    -754         /**
    -755          * (DEPRECATED) read PEM formatted encrypted PKCS#8 private key and returns RSAKey object
    -756          * @name getRSAKeyFromEncryptedPKCS8PEM
    -757          * @memberOf KEYUTIL
    -758          * @function
    -759          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    -760          * @param {String} passcode passcode to decrypto private key
    -761          * @return {RSAKey} loaded RSAKey object of RSA private key
    -762          * @since pkcs5pkey 1.0.3
    -763          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -764          * @description
    -765          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -766          * <ul>
    -767          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -768          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -769          * </ul>
    -770          * @example
    -771          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -772          * // key with PBKDF2 with TripleDES
    -773          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -774          */
    -775         getRSAKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    -776             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    -777             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    -778             return rsaKey;
    -779         },
    -780 
    -781         /**
    -782          * get RSAKey/ECDSA private key object from encrypted PEM PKCS#8 private key
    -783          * @name getKeyFromEncryptedPKCS8PEM
    -784          * @memberOf KEYUTIL
    -785          * @function
    -786          * @param {String} pkcs8PEM string of PEM formatted PKCS#8 private key
    -787          * @param {String} passcode passcode string to decrypt key
    -788          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -789          * @since pkcs5pkey 1.0.5
    -790          */
    -791         getKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    -792             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    -793             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    -794             return key;
    -795         },
    -796 
    -797         /**
    -798          * parse hexadecimal string of plain PKCS#8 private key
    -799          * @name parsePlainPrivatePKCS8Hex
    -800          * @memberOf KEYUTIL
    -801          * @function
    -802          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 plain private key
    -803          * @return {Array} associative array of parsed key
    -804          * @since pkcs5pkey 1.0.5
    -805          * @description
    -806          * Resulted associative array has following properties:
    -807          * <ul>
    -808          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    -809          * <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
    -810          * <li>keyidx - string starting index of key in pkcs8PrvHex</li>
    -811          * </ul>
    -812          */
    -813         parsePlainPrivatePKCS8Hex: function(pkcs8PrvHex) {
    -814             var result = {};
    -815             result.algparam = null;
    -816 
    -817             // 1. sequence
    -818             if (pkcs8PrvHex.substr(0, 2) != "30")
    -819                 throw "malformed plain PKCS8 private key(code:001)"; // not sequence
    -820 
    -821             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, 0);
    -822             if (a1.length != 3)
    -823                 throw "malformed plain PKCS8 private key(code:002)";
    -824 
    -825             // 2. AlgID
    -826             if (pkcs8PrvHex.substr(a1[1], 2) != "30")
    -827                 throw "malformed PKCS8 private key(code:003)"; // AlgId not sequence
    -828 
    -829             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, a1[1]);
    -830             if (a2.length != 2)
    -831                 throw "malformed PKCS8 private key(code:004)"; // AlgId not have two elements
    -832 
    -833             // 2.1. AlgID OID
    -834             if (pkcs8PrvHex.substr(a2[0], 2) != "06")
    -835                 throw "malformed PKCS8 private key(code:005)"; // AlgId.oid is not OID
    -836 
    -837             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[0]);
    +550         // === PKCS8 ===============================================================
    +551 
    +552         /**
    +553          * (DEPRECATED) read PEM formatted unencrypted PKCS#8 private key and returns RSAKey object
    +554          * @name getRSAKeyFromPlainPKCS8PEM
    +555          * @memberOf KEYUTIL
    +556          * @function
    +557          * @param {String} pkcs8PEM PEM formatted unencrypted PKCS#8 private key
    +558          * @return {RSAKey} loaded RSAKey object of RSA private key
    +559          * @since pkcs5pkey 1.0.1
    +560          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +561          */
    +562         getRSAKeyFromPlainPKCS8PEM: function(pkcs8PEM) {
    +563             if (pkcs8PEM.match(/ENCRYPTED/))
    +564                 throw "pem shall be not ENCRYPTED";
    +565             var prvKeyHex = this.getHexFromPEM(pkcs8PEM, "PRIVATE KEY");
    +566             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    +567             return rsaKey;
    +568         },
    +569 
    +570         /**
    +571          * (DEPRECATED) provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    +572          * @name getRSAKeyFromPlainPKCS8Hex
    +573          * @memberOf KEYUTIL
    +574          * @function
    +575          * @param {String} prvKeyHex hexadecimal string of unencrypted PKCS#8 private key
    +576          * @return {RSAKey} loaded RSAKey object of RSA private key
    +577          * @since pkcs5pkey 1.0.3
    +578          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +579          */
    +580         getRSAKeyFromPlainPKCS8Hex: function(prvKeyHex) {
    +581             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(prvKeyHex, 0);
    +582             if (a1.length != 3)
    +583                 throw "outer DERSequence shall have 3 elements: " + a1.length;
    +584             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    +585             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    +586                 throw "PKCS8 AlgorithmIdentifier is not rsaEnc: " + algIdTLV;
    +587             var algIdTLV = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    +588             var octetStr = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[2]);
    +589             var p5KeyHex = ASN1HEX.getHexOfV_AtObj(octetStr, 0);
    +590             //alert(p5KeyHex);
    +591             var rsaKey = new RSAKey();
    +592             rsaKey.readPrivateKeyFromASN1HexString(p5KeyHex);
    +593             return rsaKey;
    +594         },
    +595 
    +596         /**
    +597          * generate PBKDF2 key hexstring with specified passcode and information
    +598          * @name parseHexOfEncryptedPKCS8
    +599          * @memberOf KEYUTIL
    +600          * @function
    +601          * @param {String} passcode passcode to decrypto private key
    +602          * @return {Array} info associative array of PKCS#8 parameters
    +603          * @since pkcs5pkey 1.0.3
    +604          * @description
    +605          * The associative array which is returned by this method has following properties:
    +606          * <ul>
    +607          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    +608          * <li>info.pkbdf2Iter - iteration count</li>
    +609          * <li>info.ciphertext - hexadecimal string of encrypted private key</li>
    +610          * <li>info.encryptionSchemeAlg - encryption algorithm name (currently TripleDES only)</li>
    +611          * <li>info.encryptionSchemeIV - initial vector for encryption algorithm</li>
    +612          * </ul>
    +613          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +614          * <ul>
    +615          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +616          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +617          * </ul>
    +618          * @example
    +619          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +620          * // key with PBKDF2 with TripleDES
    +621          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +622          */
    +623         parseHexOfEncryptedPKCS8: function(sHEX) {
    +624             var info = {};
    +625             
    +626             var a0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, 0);
    +627             if (a0.length != 2)
    +628                 throw "malformed format: SEQUENCE(0).items != 2: " + a0.length;
    +629 
    +630             // 1. ciphertext
    +631             info.ciphertext = ASN1HEX.getHexOfV_AtObj(sHEX, a0[1]);
    +632 
    +633             // 2. pkcs5PBES2
    +634             var a0_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0[0]); 
    +635             if (a0_0.length != 2)
    +636                 throw "malformed format: SEQUENCE(0.0).items != 2: " + a0_0.length;
    +637 
    +638             // 2.1 check if pkcs5PBES2(1 2 840 113549 1 5 13)
    +639             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0[0]) != "2a864886f70d01050d")
    +640                 throw "this only supports pkcs5PBES2";
    +641 
    +642             // 2.2 pkcs5PBES2 param
    +643             var a0_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0[1]); 
    +644             if (a0_0.length != 2)
    +645                 throw "malformed format: SEQUENCE(0.0.1).items != 2: " + a0_0_1.length;
    +646 
    +647             // 2.2.1 encryptionScheme
    +648             var a0_0_1_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[1]); 
    +649             if (a0_0_1_1.length != 2)
    +650                 throw "malformed format: SEQUENCE(0.0.1.1).items != 2: " + a0_0_1_1.length;
    +651             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[0]) != "2a864886f70d0307")
    +652                 throw "this only supports TripleDES";
    +653             info.encryptionSchemeAlg = "TripleDES";
    +654 
    +655             // 2.2.1.1 IV of encryptionScheme
    +656             info.encryptionSchemeIV = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[1]);
    +657 
    +658             // 2.2.2 keyDerivationFunc
    +659             var a0_0_1_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[0]); 
    +660             if (a0_0_1_0.length != 2)
    +661                 throw "malformed format: SEQUENCE(0.0.1.0).items != 2: " + a0_0_1_0.length;
    +662             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0[0]) != "2a864886f70d01050c")
    +663                 throw "this only supports pkcs5PBKDF2";
    +664 
    +665             // 2.2.2.1 pkcs5PBKDF2 param
    +666             var a0_0_1_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1_0[1]); 
    +667             if (a0_0_1_0_1.length < 2)
    +668                 throw "malformed format: SEQUENCE(0.0.1.0.1).items < 2: " + a0_0_1_0_1.length;
    +669 
    +670             // 2.2.2.1.1 PBKDF2 salt
    +671             info.pbkdf2Salt = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[0]);
    +672 
    +673             // 2.2.2.1.2 PBKDF2 iter
    +674             var iterNumHex = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[1]);
    +675             try {
    +676                 info.pbkdf2Iter = parseInt(iterNumHex, 16);
    +677             } catch(ex) {
    +678                 throw "malformed format pbkdf2Iter: " + iterNumHex;
    +679             }
    +680 
    +681             return info;
    +682         },
    +683 
    +684         /**
    +685          * generate PBKDF2 key hexstring with specified passcode and information
    +686          * @name getPBKDF2KeyHexFromParam
    +687          * @memberOf KEYUTIL
    +688          * @function
    +689          * @param {Array} info result of {@link parseHexOfEncryptedPKCS8} which has preference of PKCS#8 file
    +690          * @param {String} passcode passcode to decrypto private key
    +691          * @return {String} hexadecimal string of PBKDF2 key
    +692          * @since pkcs5pkey 1.0.3
    +693          * @description
    +694          * As for info, this uses following properties:
    +695          * <ul>
    +696          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    +697          * <li>info.pkbdf2Iter - iteration count</li>
    +698          * </ul>
    +699          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +700          * <ul>
    +701          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +702          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +703          * </ul>
    +704          * @example
    +705          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +706          * // key with PBKDF2 with TripleDES
    +707          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +708          */
    +709         getPBKDF2KeyHexFromParam: function(info, passcode) {
    +710             var pbkdf2SaltWS = CryptoJS.enc.Hex.parse(info.pbkdf2Salt);
    +711             var pbkdf2Iter = info.pbkdf2Iter;
    +712             var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, 
    +713                                               pbkdf2SaltWS, 
    +714                                               { keySize: 192/32, iterations: pbkdf2Iter });
    +715             var pbkdf2KeyHex = CryptoJS.enc.Hex.stringify(pbkdf2KeyWS);
    +716             return pbkdf2KeyHex;
    +717         },
    +718 
    +719         /**
    +720          * read PEM formatted encrypted PKCS#8 private key and returns hexadecimal string of plain PKCS#8 private key
    +721          * @name getPlainPKCS8HexFromEncryptedPKCS8PEM
    +722          * @memberOf KEYUTIL
    +723          * @function
    +724          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    +725          * @param {String} passcode passcode to decrypto private key
    +726          * @return {String} hexadecimal string of plain PKCS#8 private key
    +727          * @since pkcs5pkey 1.0.3
    +728          * @description
    +729          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +730          * <ul>
    +731          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +732          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +733          * </ul>
    +734          * @example
    +735          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +736          * // key with PBKDF2 with TripleDES
    +737          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +738          */
    +739         getPlainPKCS8HexFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    +740             // 1. derHex - PKCS#8 private key encrypted by PBKDF2
    +741             var derHex = this.getHexFromPEM(pkcs8PEM, "ENCRYPTED PRIVATE KEY");
    +742             // 2. info - PKCS#5 PBES info
    +743             var info = this.parseHexOfEncryptedPKCS8(derHex);
    +744             // 3. hKey - PBKDF2 key
    +745             var pbkdf2KeyHex = KEYUTIL.getPBKDF2KeyHexFromParam(info, passcode);
    +746             // 4. decrypt ciphertext by PBKDF2 key
    +747             var encrypted = {};
    +748             encrypted.ciphertext = CryptoJS.enc.Hex.parse(info.ciphertext);
    +749             var pbkdf2KeyWS = CryptoJS.enc.Hex.parse(pbkdf2KeyHex);
    +750             var des3IVWS = CryptoJS.enc.Hex.parse(info.encryptionSchemeIV);
    +751             var decWS = CryptoJS.TripleDES.decrypt(encrypted, pbkdf2KeyWS, { iv: des3IVWS });
    +752             var decHex = CryptoJS.enc.Hex.stringify(decWS);
    +753             return decHex;
    +754         },
    +755 
    +756         /**
    +757          * (DEPRECATED) read PEM formatted encrypted PKCS#8 private key and returns RSAKey object
    +758          * @name getRSAKeyFromEncryptedPKCS8PEM
    +759          * @memberOf KEYUTIL
    +760          * @function
    +761          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    +762          * @param {String} passcode passcode to decrypto private key
    +763          * @return {RSAKey} loaded RSAKey object of RSA private key
    +764          * @since pkcs5pkey 1.0.3
    +765          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +766          * @description
    +767          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +768          * <ul>
    +769          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +770          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +771          * </ul>
    +772          * @example
    +773          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +774          * // key with PBKDF2 with TripleDES
    +775          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +776          */
    +777         getRSAKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    +778             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    +779             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    +780             return rsaKey;
    +781         },
    +782 
    +783         /**
    +784          * get RSAKey/ECDSA private key object from encrypted PEM PKCS#8 private key
    +785          * @name getKeyFromEncryptedPKCS8PEM
    +786          * @memberOf KEYUTIL
    +787          * @function
    +788          * @param {String} pkcs8PEM string of PEM formatted PKCS#8 private key
    +789          * @param {String} passcode passcode string to decrypt key
    +790          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +791          * @since pkcs5pkey 1.0.5
    +792          */
    +793         getKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    +794             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    +795             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    +796             return key;
    +797         },
    +798 
    +799         /**
    +800          * parse hexadecimal string of plain PKCS#8 private key
    +801          * @name parsePlainPrivatePKCS8Hex
    +802          * @memberOf KEYUTIL
    +803          * @function
    +804          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 plain private key
    +805          * @return {Array} associative array of parsed key
    +806          * @since pkcs5pkey 1.0.5
    +807          * @description
    +808          * Resulted associative array has following properties:
    +809          * <ul>
    +810          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    +811          * <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
    +812          * <li>keyidx - string starting index of key in pkcs8PrvHex</li>
    +813          * </ul>
    +814          */
    +815         parsePlainPrivatePKCS8Hex: function(pkcs8PrvHex) {
    +816             var result = {};
    +817             result.algparam = null;
    +818 
    +819             // 1. sequence
    +820             if (pkcs8PrvHex.substr(0, 2) != "30")
    +821                 throw "malformed plain PKCS8 private key(code:001)"; // not sequence
    +822 
    +823             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, 0);
    +824             if (a1.length != 3)
    +825                 throw "malformed plain PKCS8 private key(code:002)";
    +826 
    +827             // 2. AlgID
    +828             if (pkcs8PrvHex.substr(a1[1], 2) != "30")
    +829                 throw "malformed PKCS8 private key(code:003)"; // AlgId not sequence
    +830 
    +831             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, a1[1]);
    +832             if (a2.length != 2)
    +833                 throw "malformed PKCS8 private key(code:004)"; // AlgId not have two elements
    +834 
    +835             // 2.1. AlgID OID
    +836             if (pkcs8PrvHex.substr(a2[0], 2) != "06")
    +837                 throw "malformed PKCS8 private key(code:005)"; // AlgId.oid is not OID
     838 
    -839             // 2.2. AlgID param
    -840             if (pkcs8PrvHex.substr(a2[1], 2) == "06") {
    -841                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[1]);
    -842             }
    -843 
    -844             // 3. Key index
    -845             if (pkcs8PrvHex.substr(a1[2], 2) != "04")
    -846                 throw "malformed PKCS8 private key(code:006)"; // not octet string
    -847 
    -848             result.keyidx = ASN1HEX.getStartPosOfV_AtObj(pkcs8PrvHex, a1[2]);
    +839             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[0]);
    +840 
    +841             // 2.2. AlgID param
    +842             if (pkcs8PrvHex.substr(a2[1], 2) == "06") {
    +843                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[1]);
    +844             }
    +845 
    +846             // 3. Key index
    +847             if (pkcs8PrvHex.substr(a1[2], 2) != "04")
    +848                 throw "malformed PKCS8 private key(code:006)"; // not octet string
     849 
    -850             return result;
    -851         },
    -852 
    -853         /**
    -854          * get RSAKey/ECDSA private key object from PEM plain PEM PKCS#8 private key
    -855          * @name getKeyFromPlainPrivatePKCS8PEM
    -856          * @memberOf KEYUTIL
    -857          * @function
    -858          * @param {String} pkcs8PEM string of plain PEM formatted PKCS#8 private key
    -859          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -860          * @since pkcs5pkey 1.0.5
    -861          */
    -862         getKeyFromPlainPrivatePKCS8PEM: function(prvKeyPEM) {
    -863             var prvKeyHex = this.getHexFromPEM(prvKeyPEM, "PRIVATE KEY");
    -864             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    -865             return key;
    -866         },
    -867 
    -868         /**
    -869          * get RSAKey/ECDSA private key object from HEX plain PEM PKCS#8 private key
    -870          * @name getKeyFromPlainPrivatePKCS8Hex
    -871          * @memberOf KEYUTIL
    -872          * @function
    -873          * @param {String} prvKeyHex hexadecimal string of plain PKCS#8 private key
    -874          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -875          * @since pkcs5pkey 1.0.5
    -876          */
    -877         getKeyFromPlainPrivatePKCS8Hex: function(prvKeyHex) {
    -878             var p8 = this.parsePlainPrivatePKCS8Hex(prvKeyHex);
    -879             
    -880             if (p8.algoid == "2a864886f70d010101") { // RSA
    -881                 this.parsePrivateRawRSAKeyHexAtObj(prvKeyHex, p8);
    -882                 var k = p8.key;
    -883                 var key = new RSAKey();
    -884                 key.setPrivateEx(k.n, k.e, k.d, k.p, k.q, k.dp, k.dq, k.co);
    -885                 return key;
    -886             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    -887                 this.parsePrivateRawECKeyHexAtObj(prvKeyHex, p8);
    -888                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    -889                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    -890                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    -891                 var key = new KJUR.crypto.ECDSA({'curve': curveName});
    -892                 key.setPublicKeyHex(p8.pubkey);
    -893                 key.setPrivateKeyHex(p8.key);
    -894                 key.isPublic = false;
    -895                 return key;
    -896             } else if (p8.algoid == "2a8648ce380401") { // DSA
    -897                 var hP = ASN1HEX.getVbyList(prvKeyHex, 0, [1,1,0], "02");
    -898                 var hQ = ASN1HEX.getVbyList(prvKeyHex, 0, [1,1,1], "02");
    -899                 var hG = ASN1HEX.getVbyList(prvKeyHex, 0, [1,1,2], "02");
    -900                 var hX = ASN1HEX.getVbyList(prvKeyHex, 0, [2,0], "02");
    -901                 var biP = new BigInteger(hP, 16);
    -902                 var biQ = new BigInteger(hQ, 16);
    -903                 var biG = new BigInteger(hG, 16);
    -904                 var biX = new BigInteger(hX, 16);
    -905                 var key = new KJUR.crypto.DSA();
    -906                 key.setPrivate(biP, biQ, biG, null, biX);
    -907                 return key;
    -908             } else {
    -909                 throw "unsupported private key algorithm";
    -910             }
    -911         },
    -912 
    -913         // === PKCS8 RSA Public Key ================================================
    -914         /**
    -915          * (DEPRECATED) read PEM formatted PKCS#8 public key and returns RSAKey object
    -916          * @name getRSAKeyFromPublicPKCS8PEM
    -917          * @memberOf KEYUTIL
    -918          * @function
    -919          * @param {String} pkcs8PubPEM PEM formatted PKCS#8 public key
    -920          * @return {RSAKey} loaded RSAKey object of RSA public key
    -921          * @since pkcs5pkey 1.0.4
    -922          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -923          */
    -924         getRSAKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    -925             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    -926             var rsaKey = this.getRSAKeyFromPublicPKCS8Hex(pubKeyHex);
    -927             return rsaKey;
    -928         },
    -929 
    -930         /**
    -931          * (DEPRECATED) get RSAKey/ECDSA public key object from PEM PKCS#8 public key
    -932          * @name getKeyFromPublicPKCS8PEM
    -933          * @memberOf KEYUTIL
    -934          * @function
    -935          * @param {String} pkcsPub8PEM string of PEM formatted PKCS#8 public key
    -936          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -937          * @since pkcs5pkey 1.0.5
    -938          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -939          */
    -940         getKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    -941             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    -942             var key = this.getKeyFromPublicPKCS8Hex(pubKeyHex);
    -943             return key;
    -944         },
    -945 
    -946         /**
    -947          * (DEPRECATED) get RSAKey/DSA/ECDSA public key object from hexadecimal string of PKCS#8 public key
    -948          * @name getKeyFromPublicPKCS8Hex
    -949          * @memberOf KEYUTIL
    -950          * @function
    -951          * @param {String} pkcsPub8Hex hexadecimal string of PKCS#8 public key
    -952          * @return {Object} RSAKey or KJUR.crypto.{ECDSA,DSA} private key object
    -953          * @since pkcs5pkey 1.0.5
    -954          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -955          */
    -956         getKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    -957             var p8 = this.parsePublicPKCS8Hex(pkcs8PubHex);
    -958             
    -959             if (p8.algoid == "2a864886f70d010101") { // RSA
    -960                 var aRSA = this.parsePublicRawRSAKeyHex(p8.key);
    -961                 var key = new RSAKey();
    -962                 key.setPublic(aRSA.n, aRSA.e);
    -963                 return key;
    -964             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    -965                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    -966                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    -967                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    -968                 var key = new KJUR.crypto.ECDSA({'curve': curveName, 'pub': p8.key});
    -969                 return key;
    -970             } else if (p8.algoid == "2a8648ce380401") { // DSA 1.2.840.10040.4.1
    -971                 var param = p8.algparam;
    -972                 var y = ASN1HEX.getHexOfV_AtObj(p8.key, 0);
    -973                 var key = new KJUR.crypto.DSA();
    -974                 key.setPublic(new BigInteger(param.p, 16),
    -975                               new BigInteger(param.q, 16),
    -976                               new BigInteger(param.g, 16),
    -977                               new BigInteger(y, 16));
    -978                 return key;
    -979             } else {
    -980                 throw "unsupported public key algorithm";
    -981             }
    -982         },
    -983 
    -984         /**
    -985          * parse hexadecimal string of plain PKCS#8 private key
    -986          * @name parsePublicRawRSAKeyHex
    -987          * @memberOf KEYUTIL
    -988          * @function
    -989          * @param {String} pubRawRSAHex hexadecimal string of ASN.1 encoded PKCS#8 public key
    -990          * @return {Array} associative array of parsed key
    -991          * @since pkcs5pkey 1.0.5
    -992          * @description
    -993          * Resulted associative array has following properties:
    -994          * <ul>
    -995          * <li>n - hexadecimal string of public key
    -996          * <li>e - hexadecimal string of public exponent
    -997          * </ul>
    -998          */
    -999         parsePublicRawRSAKeyHex: function(pubRawRSAHex) {
    -1000             var result = {};
    -1001             
    -1002             // 1. Sequence
    -1003             if (pubRawRSAHex.substr(0, 2) != "30")
    -1004                 throw "malformed RSA key(code:001)"; // not sequence
    -1005             
    -1006             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pubRawRSAHex, 0);
    -1007             if (a1.length != 2)
    -1008                 throw "malformed RSA key(code:002)"; // not 2 items in seq
    -1009 
    -1010             // 2. public key "N"
    -1011             if (pubRawRSAHex.substr(a1[0], 2) != "02")
    -1012                 throw "malformed RSA key(code:003)"; // 1st item is not integer
    -1013 
    -1014             result.n = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[0]);
    +850             result.keyidx = ASN1HEX.getStartPosOfV_AtObj(pkcs8PrvHex, a1[2]);
    +851 
    +852             return result;
    +853         },
    +854 
    +855         /**
    +856          * get RSAKey/ECDSA private key object from PEM plain PEM PKCS#8 private key
    +857          * @name getKeyFromPlainPrivatePKCS8PEM
    +858          * @memberOf KEYUTIL
    +859          * @function
    +860          * @param {String} pkcs8PEM string of plain PEM formatted PKCS#8 private key
    +861          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +862          * @since pkcs5pkey 1.0.5
    +863          */
    +864         getKeyFromPlainPrivatePKCS8PEM: function(prvKeyPEM) {
    +865             var prvKeyHex = this.getHexFromPEM(prvKeyPEM, "PRIVATE KEY");
    +866             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    +867             return key;
    +868         },
    +869 
    +870         /**
    +871          * get RSAKey/ECDSA private key object from HEX plain PEM PKCS#8 private key
    +872          * @name getKeyFromPlainPrivatePKCS8Hex
    +873          * @memberOf KEYUTIL
    +874          * @function
    +875          * @param {String} prvKeyHex hexadecimal string of plain PKCS#8 private key
    +876          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +877          * @since pkcs5pkey 1.0.5
    +878          */
    +879         getKeyFromPlainPrivatePKCS8Hex: function(prvKeyHex) {
    +880             var p8 = this.parsePlainPrivatePKCS8Hex(prvKeyHex);
    +881             
    +882             if (p8.algoid == "2a864886f70d010101") { // RSA
    +883                 this.parsePrivateRawRSAKeyHexAtObj(prvKeyHex, p8);
    +884                 var k = p8.key;
    +885                 var key = new RSAKey();
    +886                 key.setPrivateEx(k.n, k.e, k.d, k.p, k.q, k.dp, k.dq, k.co);
    +887                 return key;
    +888             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    +889                 this.parsePrivateRawECKeyHexAtObj(prvKeyHex, p8);
    +890                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    +891                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    +892                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    +893                 var key = new KJUR.crypto.ECDSA({'curve': curveName});
    +894                 key.setPublicKeyHex(p8.pubkey);
    +895                 key.setPrivateKeyHex(p8.key);
    +896                 key.isPublic = false;
    +897                 return key;
    +898             } else if (p8.algoid == "2a8648ce380401") { // DSA
    +899                 var hP = ASN1HEX.getVbyList(prvKeyHex, 0, [1,1,0], "02");
    +900                 var hQ = ASN1HEX.getVbyList(prvKeyHex, 0, [1,1,1], "02");
    +901                 var hG = ASN1HEX.getVbyList(prvKeyHex, 0, [1,1,2], "02");
    +902                 var hX = ASN1HEX.getVbyList(prvKeyHex, 0, [2,0], "02");
    +903                 var biP = new BigInteger(hP, 16);
    +904                 var biQ = new BigInteger(hQ, 16);
    +905                 var biG = new BigInteger(hG, 16);
    +906                 var biX = new BigInteger(hX, 16);
    +907                 var key = new KJUR.crypto.DSA();
    +908                 key.setPrivate(biP, biQ, biG, null, biX);
    +909                 return key;
    +910             } else {
    +911                 throw "unsupported private key algorithm";
    +912             }
    +913         },
    +914 
    +915         // === PKCS8 RSA Public Key ================================================
    +916         /**
    +917          * (DEPRECATED) read PEM formatted PKCS#8 public key and returns RSAKey object
    +918          * @name getRSAKeyFromPublicPKCS8PEM
    +919          * @memberOf KEYUTIL
    +920          * @function
    +921          * @param {String} pkcs8PubPEM PEM formatted PKCS#8 public key
    +922          * @return {RSAKey} loaded RSAKey object of RSA public key
    +923          * @since pkcs5pkey 1.0.4
    +924          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +925          */
    +926         getRSAKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    +927             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    +928             var rsaKey = this.getRSAKeyFromPublicPKCS8Hex(pubKeyHex);
    +929             return rsaKey;
    +930         },
    +931 
    +932         /**
    +933          * (DEPRECATED) get RSAKey/ECDSA public key object from PEM PKCS#8 public key
    +934          * @name getKeyFromPublicPKCS8PEM
    +935          * @memberOf KEYUTIL
    +936          * @function
    +937          * @param {String} pkcsPub8PEM string of PEM formatted PKCS#8 public key
    +938          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +939          * @since pkcs5pkey 1.0.5
    +940          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +941          */
    +942         getKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    +943             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    +944             var key = this.getKeyFromPublicPKCS8Hex(pubKeyHex);
    +945             return key;
    +946         },
    +947 
    +948         /**
    +949          * (DEPRECATED) get RSAKey/DSA/ECDSA public key object from hexadecimal string of PKCS#8 public key
    +950          * @name getKeyFromPublicPKCS8Hex
    +951          * @memberOf KEYUTIL
    +952          * @function
    +953          * @param {String} pkcsPub8Hex hexadecimal string of PKCS#8 public key
    +954          * @return {Object} RSAKey or KJUR.crypto.{ECDSA,DSA} private key object
    +955          * @since pkcs5pkey 1.0.5
    +956          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +957          */
    +958         getKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    +959             var p8 = this.parsePublicPKCS8Hex(pkcs8PubHex);
    +960             
    +961             if (p8.algoid == "2a864886f70d010101") { // RSA
    +962                 var aRSA = this.parsePublicRawRSAKeyHex(p8.key);
    +963                 var key = new RSAKey();
    +964                 key.setPublic(aRSA.n, aRSA.e);
    +965                 return key;
    +966             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    +967                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    +968                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    +969                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    +970                 var key = new KJUR.crypto.ECDSA({'curve': curveName, 'pub': p8.key});
    +971                 return key;
    +972             } else if (p8.algoid == "2a8648ce380401") { // DSA 1.2.840.10040.4.1
    +973                 var param = p8.algparam;
    +974                 var y = ASN1HEX.getHexOfV_AtObj(p8.key, 0);
    +975                 var key = new KJUR.crypto.DSA();
    +976                 key.setPublic(new BigInteger(param.p, 16),
    +977                               new BigInteger(param.q, 16),
    +978                               new BigInteger(param.g, 16),
    +979                               new BigInteger(y, 16));
    +980                 return key;
    +981             } else {
    +982                 throw "unsupported public key algorithm";
    +983             }
    +984         },
    +985 
    +986         /**
    +987          * parse hexadecimal string of plain PKCS#8 private key
    +988          * @name parsePublicRawRSAKeyHex
    +989          * @memberOf KEYUTIL
    +990          * @function
    +991          * @param {String} pubRawRSAHex hexadecimal string of ASN.1 encoded PKCS#8 public key
    +992          * @return {Array} associative array of parsed key
    +993          * @since pkcs5pkey 1.0.5
    +994          * @description
    +995          * Resulted associative array has following properties:
    +996          * <ul>
    +997          * <li>n - hexadecimal string of public key
    +998          * <li>e - hexadecimal string of public exponent
    +999          * </ul>
    +1000          */
    +1001         parsePublicRawRSAKeyHex: function(pubRawRSAHex) {
    +1002             var result = {};
    +1003             
    +1004             // 1. Sequence
    +1005             if (pubRawRSAHex.substr(0, 2) != "30")
    +1006                 throw "malformed RSA key(code:001)"; // not sequence
    +1007             
    +1008             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pubRawRSAHex, 0);
    +1009             if (a1.length != 2)
    +1010                 throw "malformed RSA key(code:002)"; // not 2 items in seq
    +1011 
    +1012             // 2. public key "N"
    +1013             if (pubRawRSAHex.substr(a1[0], 2) != "02")
    +1014                 throw "malformed RSA key(code:003)"; // 1st item is not integer
     1015 
    -1016             // 3. public key "E"
    -1017             if (pubRawRSAHex.substr(a1[1], 2) != "02")
    -1018                 throw "malformed RSA key(code:004)"; // 2nd item is not integer
    -1019 
    -1020             result.e = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[1]);
    +1016             result.n = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[0]);
    +1017 
    +1018             // 3. public key "E"
    +1019             if (pubRawRSAHex.substr(a1[1], 2) != "02")
    +1020                 throw "malformed RSA key(code:004)"; // 2nd item is not integer
     1021 
    -1022             return result;
    -1023         },
    -1024 
    -1025         /**
    -1026          * parse hexadecimal string of RSA private key
    -1027          * @name parsePrivateRawRSAKeyHexAtObj
    -1028          * @memberOf KEYUTIL
    -1029          * @function
    -1030          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding RSA private key
    -1031          * @return {Array} info associative array to add parsed RSA private key information
    -1032          * @since pkcs5pkey 1.0.5
    -1033          * @description
    -1034          * Following properties are added to associative array 'info'
    -1035          * <ul>
    -1036          * <li>n - hexadecimal string of public key
    -1037          * <li>e - hexadecimal string of public exponent
    -1038          * <li>d - hexadecimal string of private key
    -1039          * <li>p - hexadecimal string
    -1040          * <li>q - hexadecimal string
    -1041          * <li>dp - hexadecimal string
    -1042          * <li>dq - hexadecimal string
    -1043          * <li>co - hexadecimal string
    -1044          * </ul>
    -1045          */
    -1046         parsePrivateRawRSAKeyHexAtObj: function(pkcs8PrvHex, info) {
    -1047             var keyIdx = info.keyidx;
    -1048             
    -1049             // 1. sequence
    -1050             if (pkcs8PrvHex.substr(keyIdx, 2) != "30")
    -1051                 throw "malformed RSA private key(code:001)"; // not sequence
    -1052 
    -1053             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, keyIdx);
    -1054             if (a1.length != 9)
    -1055                 throw "malformed RSA private key(code:002)"; // not sequence
    -1056 
    -1057             // 2. RSA key
    -1058             info.key = {};
    -1059             info.key.n = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[1]);
    -1060             info.key.e = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[2]);
    -1061             info.key.d = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[3]);
    -1062             info.key.p = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[4]);
    -1063             info.key.q = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[5]);
    -1064             info.key.dp = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[6]);
    -1065             info.key.dq = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[7]);
    -1066             info.key.co = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[8]);
    -1067         },
    -1068 
    -1069         /**
    -1070          * parse hexadecimal string of ECC private key
    -1071          * @name parsePrivateRawECKeyHexAtObj
    -1072          * @memberOf KEYUTIL
    -1073          * @function
    -1074          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding EC private key
    -1075          * @return {Array} info associative array to add parsed ECC private key information
    -1076          * @since pkcs5pkey 1.0.5
    -1077          * @description
    -1078          * Following properties are added to associative array 'info'
    -1079          * <ul>
    -1080          * <li>key - hexadecimal string of ECC private key
    -1081          * </ul>
    -1082          */
    -1083         parsePrivateRawECKeyHexAtObj: function(pkcs8PrvHex, info) {
    -1084             var keyIdx = info.keyidx;
    -1085             
    -1086             var key = ASN1HEX.getVbyList(pkcs8PrvHex, keyIdx, [1], "04");
    -1087             var pubkey = ASN1HEX.getVbyList(pkcs8PrvHex, keyIdx, [2,0], "03").substr(2);
    -1088 
    -1089             info.key = key;
    -1090             info.pubkey = pubkey;
    -1091         },
    -1092 
    -1093         /**
    -1094          * parse hexadecimal string of PKCS#8 RSA/EC/DSA public key
    -1095          * @name parsePublicPKCS8Hex
    -1096          * @memberOf KEYUTIL
    -1097          * @function
    -1098          * @param {String} pkcs8PubHex hexadecimal string of PKCS#8 public key
    -1099          * @return {Hash} hash of key information
    -1100          * @description
    -1101          * Resulted hash has following attributes.
    -1102          * <ul>
    -1103          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    -1104          * <li>algparam - hexadecimal string of OID of ECC curve name, parameter SEQUENCE of DSA or null</li>
    -1105          * <li>key - hexadecimal string of public key</li>
    -1106          * </ul>
    -1107          */
    -1108         parsePublicPKCS8Hex: function(pkcs8PubHex) {
    -1109             var result = {};
    -1110             result.algparam = null;
    -1111 
    -1112             // 1. AlgID and Key bit string
    -1113             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    -1114             if (a1.length != 2)
    -1115                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    -1116 
    -1117             // 2. AlgID
    -1118             var idxAlgIdTLV = a1[0];
    -1119             if (pkcs8PubHex.substr(idxAlgIdTLV, 2) != "30")
    -1120                 throw "malformed PKCS8 public key(code:001)"; // AlgId not sequence
    -1121 
    -1122             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxAlgIdTLV);
    -1123             if (a2.length != 2)
    -1124                 throw "malformed PKCS8 public key(code:002)"; // AlgId not have two elements
    -1125 
    -1126             // 2.1. AlgID OID
    -1127             if (pkcs8PubHex.substr(a2[0], 2) != "06")
    -1128                 throw "malformed PKCS8 public key(code:003)"; // AlgId.oid is not OID
    -1129 
    -1130             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]);
    +1022             result.e = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[1]);
    +1023 
    +1024             return result;
    +1025         },
    +1026 
    +1027         /**
    +1028          * parse hexadecimal string of RSA private key
    +1029          * @name parsePrivateRawRSAKeyHexAtObj
    +1030          * @memberOf KEYUTIL
    +1031          * @function
    +1032          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding RSA private key
    +1033          * @return {Array} info associative array to add parsed RSA private key information
    +1034          * @since pkcs5pkey 1.0.5
    +1035          * @description
    +1036          * Following properties are added to associative array 'info'
    +1037          * <ul>
    +1038          * <li>n - hexadecimal string of public key
    +1039          * <li>e - hexadecimal string of public exponent
    +1040          * <li>d - hexadecimal string of private key
    +1041          * <li>p - hexadecimal string
    +1042          * <li>q - hexadecimal string
    +1043          * <li>dp - hexadecimal string
    +1044          * <li>dq - hexadecimal string
    +1045          * <li>co - hexadecimal string
    +1046          * </ul>
    +1047          */
    +1048         parsePrivateRawRSAKeyHexAtObj: function(pkcs8PrvHex, info) {
    +1049             var keyIdx = info.keyidx;
    +1050             
    +1051             // 1. sequence
    +1052             if (pkcs8PrvHex.substr(keyIdx, 2) != "30")
    +1053                 throw "malformed RSA private key(code:001)"; // not sequence
    +1054 
    +1055             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, keyIdx);
    +1056             if (a1.length != 9)
    +1057                 throw "malformed RSA private key(code:002)"; // not sequence
    +1058 
    +1059             // 2. RSA key
    +1060             info.key = {};
    +1061             info.key.n = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[1]);
    +1062             info.key.e = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[2]);
    +1063             info.key.d = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[3]);
    +1064             info.key.p = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[4]);
    +1065             info.key.q = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[5]);
    +1066             info.key.dp = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[6]);
    +1067             info.key.dq = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[7]);
    +1068             info.key.co = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[8]);
    +1069         },
    +1070 
    +1071         /**
    +1072          * parse hexadecimal string of ECC private key
    +1073          * @name parsePrivateRawECKeyHexAtObj
    +1074          * @memberOf KEYUTIL
    +1075          * @function
    +1076          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding EC private key
    +1077          * @return {Array} info associative array to add parsed ECC private key information
    +1078          * @since pkcs5pkey 1.0.5
    +1079          * @description
    +1080          * Following properties are added to associative array 'info'
    +1081          * <ul>
    +1082          * <li>key - hexadecimal string of ECC private key
    +1083          * </ul>
    +1084          */
    +1085         parsePrivateRawECKeyHexAtObj: function(pkcs8PrvHex, info) {
    +1086             var keyIdx = info.keyidx;
    +1087             
    +1088             var key = ASN1HEX.getVbyList(pkcs8PrvHex, keyIdx, [1], "04");
    +1089             var pubkey = ASN1HEX.getVbyList(pkcs8PrvHex, keyIdx, [2,0], "03").substr(2);
    +1090 
    +1091             info.key = key;
    +1092             info.pubkey = pubkey;
    +1093         },
    +1094 
    +1095         /**
    +1096          * parse hexadecimal string of PKCS#8 RSA/EC/DSA public key
    +1097          * @name parsePublicPKCS8Hex
    +1098          * @memberOf KEYUTIL
    +1099          * @function
    +1100          * @param {String} pkcs8PubHex hexadecimal string of PKCS#8 public key
    +1101          * @return {Hash} hash of key information
    +1102          * @description
    +1103          * Resulted hash has following attributes.
    +1104          * <ul>
    +1105          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    +1106          * <li>algparam - hexadecimal string of OID of ECC curve name, parameter SEQUENCE of DSA or null</li>
    +1107          * <li>key - hexadecimal string of public key</li>
    +1108          * </ul>
    +1109          */
    +1110         parsePublicPKCS8Hex: function(pkcs8PubHex) {
    +1111             var result = {};
    +1112             result.algparam = null;
    +1113 
    +1114             // 1. AlgID and Key bit string
    +1115             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    +1116             if (a1.length != 2)
    +1117                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    +1118 
    +1119             // 2. AlgID
    +1120             var idxAlgIdTLV = a1[0];
    +1121             if (pkcs8PubHex.substr(idxAlgIdTLV, 2) != "30")
    +1122                 throw "malformed PKCS8 public key(code:001)"; // AlgId not sequence
    +1123 
    +1124             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxAlgIdTLV);
    +1125             if (a2.length != 2)
    +1126                 throw "malformed PKCS8 public key(code:002)"; // AlgId not have two elements
    +1127 
    +1128             // 2.1. AlgID OID
    +1129             if (pkcs8PubHex.substr(a2[0], 2) != "06")
    +1130                 throw "malformed PKCS8 public key(code:003)"; // AlgId.oid is not OID
     1131 
    -1132             // 2.2. AlgID param
    -1133             if (pkcs8PubHex.substr(a2[1], 2) == "06") { // OID for EC
    -1134                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]);
    -1135             } else if (pkcs8PubHex.substr(a2[1], 2) == "30") { // SEQ for DSA
    -1136                 result.algparam = {};
    -1137                 result.algparam.p = ASN1HEX.getVbyList(pkcs8PubHex, a2[1], [0], "02");
    -1138                 result.algparam.q = ASN1HEX.getVbyList(pkcs8PubHex, a2[1], [1], "02");
    -1139                 result.algparam.g = ASN1HEX.getVbyList(pkcs8PubHex, a2[1], [2], "02");
    -1140             }
    -1141 
    -1142             // 3. Key
    -1143             if (pkcs8PubHex.substr(a1[1], 2) != "03")
    -1144                 throw "malformed PKCS8 public key(code:004)"; // Key is not bit string
    -1145 
    -1146             result.key = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a1[1]).substr(2);
    -1147             
    -1148             // 4. return result assoc array
    -1149             return result;
    -1150         },
    -1151 
    -1152         /**
    -1153          * (DEPRECATED) provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    -1154          * @name getRSAKeyFromPublicPKCS8Hex
    -1155          * @memberOf KEYUTIL
    -1156          * @function
    -1157          * @param {String} pkcs8PubHex hexadecimal string of unencrypted PKCS#8 public key
    -1158          * @return {RSAKey} loaded RSAKey object of RSA public key
    -1159          * @since pkcs5pkey 1.0.4
    -1160          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    -1161          */
    -1162         getRSAKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    -1163             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    -1164             if (a1.length != 2)
    -1165                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    -1166 
    -1167             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(pkcs8PubHex, a1[0]);
    -1168             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    -1169                 throw "PKCS8 AlgorithmId is not rsaEncryption";
    -1170             
    -1171             if (pkcs8PubHex.substr(a1[1], 2) != "03")
    -1172                 throw "PKCS8 Public Key is not BITSTRING encapslated.";
    -1173 
    -1174             var idxPub = ASN1HEX.getStartPosOfV_AtObj(pkcs8PubHex, a1[1]) + 2; // 2 for unused bit
    -1175             
    -1176             if (pkcs8PubHex.substr(idxPub, 2) != "30")
    -1177                 throw "PKCS8 Public Key is not SEQUENCE.";
    -1178 
    -1179             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxPub);
    -1180             if (a2.length != 2)
    -1181                 throw "inner DERSequence shall have 2 elements: " + a2.length;
    -1182 
    -1183             if (pkcs8PubHex.substr(a2[0], 2) != "02") 
    -1184                 throw "N is not ASN.1 INTEGER";
    -1185             if (pkcs8PubHex.substr(a2[1], 2) != "02") 
    -1186                 throw "E is not ASN.1 INTEGER";
    -1187             
    -1188             var hN = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]);
    -1189             var hE = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]);
    -1190 
    -1191             var pubKey = new RSAKey();
    -1192             pubKey.setPublic(hN, hE);
    -1193             
    -1194             return pubKey;
    -1195         },
    -1196 
    -1197         //addAlgorithm: function(functionObject, algName, keyLen, ivLen) {
    -1198         //}
    -1199     };
    -1200 }();
    -1201 
    -1202 // -- MAJOR PUBLIC METHODS -------------------------------------------------------
    -1203 /**
    -1204  * get private or public key object from any arguments
    -1205  * @name getKey
    -1206  * @memberOf KEYUTIL
    -1207  * @function
    -1208  * @static
    -1209  * @param {Object} param parameter to get key object. see description in detail.
    -1210  * @param {String} passcode (OPTION) parameter to get key object. see description in detail.
    -1211  * @param {String} hextype (OPTOIN) parameter to get key object. see description in detail.
    -1212  * @return {Object} {@link RSAKey}, {@link KJUR.crypto.ECDSA} or {@link KJUR.crypto.ECDSA} object
    -1213  * @since keyutil 1.0.0
    -1214  * @description
    -1215  * This method gets private or public key object({@link RSAKey}, {@link KJUR.crypto.DSA} or {@link KJUR.crypto.ECDSA})
    -1216  * for RSA, DSA and ECC.
    -1217  * Arguments for this methods depends on a key format you specify.
    -1218  * Following key representations are supported.
    -1219  * <ul>
    -1220  * <li>ECC private/public key object(as is): param=KJUR.crypto.ECDSA</li>
    -1221  * <li>DSA private/public key object(as is): param=KJUR.crypto.DSA</li>
    -1222  * <li>RSA private/public key object(as is): param=RSAKey </li>
    -1223  * <li>ECC private key parameters: param={d: d, curve: curveName}</li>
    -1224  * <li>RSA private key parameters: param={n: n, e: e, d: d, p: p, q: q, dp: dp, dq: dq, co: co}<br/>
    -1225  * NOTE: Each value shall be hexadecimal string of key spec.</li>
    -1226  * <li>DSA private key parameters: param={p: p, q: q, g: g, y: y, x: x}<br/>
    +1132             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]);
    +1133 
    +1134             // 2.2. AlgID param
    +1135             if (pkcs8PubHex.substr(a2[1], 2) == "06") { // OID for EC
    +1136                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]);
    +1137             } else if (pkcs8PubHex.substr(a2[1], 2) == "30") { // SEQ for DSA
    +1138                 result.algparam = {};
    +1139                 result.algparam.p = ASN1HEX.getVbyList(pkcs8PubHex, a2[1], [0], "02");
    +1140                 result.algparam.q = ASN1HEX.getVbyList(pkcs8PubHex, a2[1], [1], "02");
    +1141                 result.algparam.g = ASN1HEX.getVbyList(pkcs8PubHex, a2[1], [2], "02");
    +1142             }
    +1143 
    +1144             // 3. Key
    +1145             if (pkcs8PubHex.substr(a1[1], 2) != "03")
    +1146                 throw "malformed PKCS8 public key(code:004)"; // Key is not bit string
    +1147 
    +1148             result.key = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a1[1]).substr(2);
    +1149             
    +1150             // 4. return result assoc array
    +1151             return result;
    +1152         },
    +1153 
    +1154         /**
    +1155          * (DEPRECATED) provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    +1156          * @name getRSAKeyFromPublicPKCS8Hex
    +1157          * @memberOf KEYUTIL
    +1158          * @function
    +1159          * @param {String} pkcs8PubHex hexadecimal string of unencrypted PKCS#8 public key
    +1160          * @return {RSAKey} loaded RSAKey object of RSA public key
    +1161          * @since pkcs5pkey 1.0.4
    +1162          * @deprecated From jsrsasign 4.2.1 please use {@link KEYUTIL.getKey#}.
    +1163          */
    +1164         getRSAKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    +1165             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    +1166             if (a1.length != 2)
    +1167                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    +1168 
    +1169             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(pkcs8PubHex, a1[0]);
    +1170             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    +1171                 throw "PKCS8 AlgorithmId is not rsaEncryption";
    +1172             
    +1173             if (pkcs8PubHex.substr(a1[1], 2) != "03")
    +1174                 throw "PKCS8 Public Key is not BITSTRING encapslated.";
    +1175 
    +1176             var idxPub = ASN1HEX.getStartPosOfV_AtObj(pkcs8PubHex, a1[1]) + 2; // 2 for unused bit
    +1177             
    +1178             if (pkcs8PubHex.substr(idxPub, 2) != "30")
    +1179                 throw "PKCS8 Public Key is not SEQUENCE.";
    +1180 
    +1181             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxPub);
    +1182             if (a2.length != 2)
    +1183                 throw "inner DERSequence shall have 2 elements: " + a2.length;
    +1184 
    +1185             if (pkcs8PubHex.substr(a2[0], 2) != "02") 
    +1186                 throw "N is not ASN.1 INTEGER";
    +1187             if (pkcs8PubHex.substr(a2[1], 2) != "02") 
    +1188                 throw "E is not ASN.1 INTEGER";
    +1189             
    +1190             var hN = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]);
    +1191             var hE = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]);
    +1192 
    +1193             var pubKey = new RSAKey();
    +1194             pubKey.setPublic(hN, hE);
    +1195             
    +1196             return pubKey;
    +1197         },
    +1198 
    +1199         //addAlgorithm: function(functionObject, algName, keyLen, ivLen) {
    +1200         //}
    +1201     };
    +1202 }();
    +1203 
    +1204 // -- MAJOR PUBLIC METHODS -------------------------------------------------------
    +1205 /**
    +1206  * get private or public key object from any arguments
    +1207  * @name getKey
    +1208  * @memberOf KEYUTIL
    +1209  * @function
    +1210  * @static
    +1211  * @param {Object} param parameter to get key object. see description in detail.
    +1212  * @param {String} passcode (OPTION) parameter to get key object. see description in detail.
    +1213  * @param {String} hextype (OPTOIN) parameter to get key object. see description in detail.
    +1214  * @return {Object} {@link RSAKey}, {@link KJUR.crypto.ECDSA} or {@link KJUR.crypto.ECDSA} object
    +1215  * @since keyutil 1.0.0
    +1216  * @description
    +1217  * This method gets private or public key object({@link RSAKey}, {@link KJUR.crypto.DSA} or {@link KJUR.crypto.ECDSA})
    +1218  * for RSA, DSA and ECC.
    +1219  * Arguments for this methods depends on a key format you specify.
    +1220  * Following key representations are supported.
    +1221  * <ul>
    +1222  * <li>ECC private/public key object(as is): param=KJUR.crypto.ECDSA</li>
    +1223  * <li>DSA private/public key object(as is): param=KJUR.crypto.DSA</li>
    +1224  * <li>RSA private/public key object(as is): param=RSAKey </li>
    +1225  * <li>ECC private key parameters: param={d: d, curve: curveName}</li>
    +1226  * <li>RSA private key parameters: param={n: n, e: e, d: d, p: p, q: q, dp: dp, dq: dq, co: co}<br/>
     1227  * NOTE: Each value shall be hexadecimal string of key spec.</li>
    -1228  * <li>ECC public key parameters: param={xy: xy, curve: curveName}<br/>
    -1229  * NOTE: ECC public key 'xy' shall be concatination of "04", x-bytes-hex and y-bytes-hex.</li>
    -1230  * <li>DSA public key parameters: param={p: p, q: q, g: g, y: y}<br/>
    -1231  * NOTE: Each value shall be hexadecimal string of key spec.</li>
    -1232  * <li>RSA public key parameters: param={n: n, e: e} </li>
    -1233  * <li>X.509v1/v3 PEM certificate (RSA/DSA/ECC): param=pemString</li>
    -1234  * <li>PKCS#8 hexadecimal RSA/ECC public key: param=pemString, null, "pkcs8pub"</li>
    -1235  * <li>PKCS#8 PEM RSA/DSA/ECC public key: param=pemString</li>
    -1236  * <li>PKCS#5 plain hexadecimal RSA private key: param=hexString, null, "pkcs5prv"</li>
    -1237  * <li>PKCS#5 plain PEM DSA/RSA private key: param=pemString</li>
    -1238  * <li>PKCS#8 plain PEM RSA/ECDSA private key: param=pemString</li>
    -1239  * <li>PKCS#5 encrypted PEM RSA/DSA private key: param=pemString, passcode</li>
    -1240  * <li>PKCS#8 encrypted PEM RSA/ECDSA private key: param=pemString, passcode</li>
    -1241  * </ul>
    -1242  * Please note following limitation on encrypted keys:
    -1243  * <ul>
    -1244  * <li>Encrypted PKCS#8 only supports PBKDF2/HmacSHA1/3DES</li>
    -1245  * <li>Encrypted PKCS#5 supports DES-CBC, DES-EDE3-CBC, AES-{128,192.256}-CBC</li>
    -1246  * <li>JWT plain ECC private/public key</li>
    -1247  * <li>JWT plain RSA public key</li>
    -1248  * <li>JWT plain RSA private key with P/Q/DP/DQ/COEFF</li>
    -1249  * <li>JWT plain RSA private key without P/Q/DP/DQ/COEFF (since jsrsasign 5.0.0)</li>
    -1250  * </ul>
    -1251  * NOTE1: <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.<br/>
    -1252  * NOTE2: X509v1 support is added since jsrsasign 5.0.11.
    -1253  * 
    -1254  * <h5>EXAMPLE</h5>
    -1255  * @example
    -1256  * // 1. loading private key from PEM string
    -1257  * keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY...");
    -1258  * keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY..., "passcode");
    -1259  * keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY...");
    -1260  * keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY...", "passcode");
    -1261  * // 2. loading public key from PEM string
    -1262  * keyObj = KEYUTIL.getKey("-----BEGIN PUBLIC KEY...");
    -1263  * keyObj = KEYUTIL.getKey("-----BEGIN X509 CERTIFICATE...");
    -1264  * // 3. loading hexadecimal PKCS#5/PKCS#8 key
    -1265  * keyObj = KEYUTIL.getKey("308205c1...", null, "pkcs8pub");
    -1266  * keyObj = KEYUTIL.getKey("3082048b...", null, "pkcs5prv");
    -1267  * // 4. loading JSON Web Key(JWK)
    -1268  * keyObj = KEYUTIL.getKey({kty: "RSA", n: "0vx7...", e: "AQAB"});
    -1269  * keyObj = KEYUTIL.getKey({kty: "EC", crv: "P-256", 
    -1270  *                          x: "MKBC...", y: "4Etl6...", d: "870Mb..."});
    -1271  * // 5. bare hexadecimal key
    -1272  * keyObj = KEYUTIL.getKey({n: "75ab..", e: "010001"});
    -1273  */
    -1274 KEYUTIL.getKey = function(param, passcode, hextype) {
    -1275     // 1. by key RSAKey/KJUR.crypto.ECDSA/KJUR.crypto.DSA object
    -1276     if (typeof RSAKey != 'undefined' && param instanceof RSAKey)
    -1277         return param;
    -1278     if (typeof KJUR.crypto.ECDSA != 'undefined' && param instanceof KJUR.crypto.ECDSA)
    +1228  * <li>DSA private key parameters: param={p: p, q: q, g: g, y: y, x: x}<br/>
    +1229  * NOTE: Each value shall be hexadecimal string of key spec.</li>
    +1230  * <li>ECC public key parameters: param={xy: xy, curve: curveName}<br/>
    +1231  * NOTE: ECC public key 'xy' shall be concatination of "04", x-bytes-hex and y-bytes-hex.</li>
    +1232  * <li>DSA public key parameters: param={p: p, q: q, g: g, y: y}<br/>
    +1233  * NOTE: Each value shall be hexadecimal string of key spec.</li>
    +1234  * <li>RSA public key parameters: param={n: n, e: e} </li>
    +1235  * <li>X.509v1/v3 PEM certificate (RSA/DSA/ECC): param=pemString</li>
    +1236  * <li>PKCS#8 hexadecimal RSA/ECC public key: param=pemString, null, "pkcs8pub"</li>
    +1237  * <li>PKCS#8 PEM RSA/DSA/ECC public key: param=pemString</li>
    +1238  * <li>PKCS#5 plain hexadecimal RSA private key: param=hexString, null, "pkcs5prv"</li>
    +1239  * <li>PKCS#5 plain PEM DSA/RSA private key: param=pemString</li>
    +1240  * <li>PKCS#8 plain PEM RSA/ECDSA private key: param=pemString</li>
    +1241  * <li>PKCS#5 encrypted PEM RSA/DSA private key: param=pemString, passcode</li>
    +1242  * <li>PKCS#8 encrypted PEM RSA/ECDSA private key: param=pemString, passcode</li>
    +1243  * </ul>
    +1244  * Please note following limitation on encrypted keys:
    +1245  * <ul>
    +1246  * <li>Encrypted PKCS#8 only supports PBKDF2/HmacSHA1/3DES</li>
    +1247  * <li>Encrypted PKCS#5 supports DES-CBC, DES-EDE3-CBC, AES-{128,192.256}-CBC</li>
    +1248  * <li>JWT plain ECC private/public key</li>
    +1249  * <li>JWT plain RSA public key</li>
    +1250  * <li>JWT plain RSA private key with P/Q/DP/DQ/COEFF</li>
    +1251  * <li>JWT plain RSA private key without P/Q/DP/DQ/COEFF (since jsrsasign 5.0.0)</li>
    +1252  * </ul>
    +1253  * NOTE1: <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.<br/>
    +1254  * NOTE2: X509v1 support is added since jsrsasign 5.0.11.
    +1255  * 
    +1256  * <h5>EXAMPLE</h5>
    +1257  * @example
    +1258  * // 1. loading private key from PEM string
    +1259  * keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY...");
    +1260  * keyObj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY..., "passcode");
    +1261  * keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY...");
    +1262  * keyObj = KEYUTIL.getKey("-----BEGIN PRIVATE KEY...", "passcode");
    +1263  * // 2. loading public key from PEM string
    +1264  * keyObj = KEYUTIL.getKey("-----BEGIN PUBLIC KEY...");
    +1265  * keyObj = KEYUTIL.getKey("-----BEGIN X509 CERTIFICATE...");
    +1266  * // 3. loading hexadecimal PKCS#5/PKCS#8 key
    +1267  * keyObj = KEYUTIL.getKey("308205c1...", null, "pkcs8pub");
    +1268  * keyObj = KEYUTIL.getKey("3082048b...", null, "pkcs5prv");
    +1269  * // 4. loading JSON Web Key(JWK)
    +1270  * keyObj = KEYUTIL.getKey({kty: "RSA", n: "0vx7...", e: "AQAB"});
    +1271  * keyObj = KEYUTIL.getKey({kty: "EC", crv: "P-256", 
    +1272  *                          x: "MKBC...", y: "4Etl6...", d: "870Mb..."});
    +1273  * // 5. bare hexadecimal key
    +1274  * keyObj = KEYUTIL.getKey({n: "75ab..", e: "010001"});
    +1275  */
    +1276 KEYUTIL.getKey = function(param, passcode, hextype) {
    +1277     // 1. by key RSAKey/KJUR.crypto.ECDSA/KJUR.crypto.DSA object
    +1278     if (typeof RSAKey != 'undefined' && param instanceof RSAKey)
     1279         return param;
    -1280     if (typeof KJUR.crypto.DSA != 'undefined' && param instanceof KJUR.crypto.DSA)
    +1280     if (typeof KJUR.crypto.ECDSA != 'undefined' && param instanceof KJUR.crypto.ECDSA)
     1281         return param;
    -1282 
    -1283     // 2. by parameters of key
    +1282     if (typeof KJUR.crypto.DSA != 'undefined' && param instanceof KJUR.crypto.DSA)
    +1283         return param;
     1284 
    -1285     // 2.1. bare ECC
    -1286     // 2.1.1. bare ECC public key by hex values
    -1287     if (param.curve !== undefined &&
    -1288 	param.xy !== undefined && param.d === undefined) {
    -1289         return new KJUR.crypto.ECDSA({pub: param.xy, curve: param.curve});
    -1290     }
    -1291 
    -1292     // 2.1.2. bare ECC private key by hex values
    -1293     if (param.curve !== undefined && param.d !== undefined) {
    -1294         return new KJUR.crypto.ECDSA({prv: param.d, curve: param.curve});
    -1295     }
    -1296 
    -1297     // 2.2. bare RSA
    -1298     // 2.2.1. bare RSA public key by hex values
    -1299     if (param.kty === undefined &&
    -1300 	param.n !== undefined && param.e !== undefined &&
    -1301         param.d === undefined) {
    -1302         var key = new RSAKey();
    -1303         key.setPublic(param.n, param.e);
    -1304         return key;
    -1305     }
    -1306 
    -1307     // 2.2.2. bare RSA private key with P/Q/DP/DQ/COEFF by hex values
    -1308     if (param.kty === undefined &&
    -1309 	param.n !== undefined &&
    -1310 	param.e !== undefined &&
    -1311 	param.d !== undefined &&
    -1312         param.p !== undefined &&
    -1313 	param.q !== undefined &&
    -1314         param.dp !== undefined &&
    -1315 	param.dq !== undefined &&
    -1316 	param.co !== undefined &&
    -1317         param.qi === undefined) {
    -1318         var key = new RSAKey();
    -1319         key.setPrivateEx(param.n, param.e, param.d, param.p, param.q,
    -1320                          param.dp, param.dq, param.co);
    -1321         return key;
    -1322     }
    -1323 
    -1324     // 2.2.3. bare RSA public key without P/Q/DP/DQ/COEFF by hex values
    -1325     if (param.kty === undefined &&
    -1326 	param.n !== undefined &&
    -1327 	param.e !== undefined &&
    -1328 	param.d !== undefined &&
    -1329         param.p === undefined) {
    -1330         var key = new RSAKey();
    -1331         key.setPrivate(param.n, param.e, param.d);
    -1332         return key;
    -1333     }
    -1334 
    -1335     // 2.3. bare DSA
    -1336     // 2.3.1. bare DSA public key by hex values
    -1337     if (param.p !== undefined && param.q !== undefined &&
    -1338 	param.g !== undefined &&
    -1339         param.y !== undefined && param.x === undefined) {
    -1340         var key = new KJUR.crypto.DSA();
    -1341         key.setPublic(param.p, param.q, param.g, param.y);
    -1342         return key;
    -1343     }
    -1344 
    -1345     // 2.3.2. bare DSA private key by hex values
    -1346     if (param.p !== undefined && param.q !== undefined &&
    -1347 	param.g !== undefined &&
    -1348         param.y !== undefined && param.x !== undefined) {
    -1349         var key = new KJUR.crypto.DSA();
    -1350         key.setPrivate(param.p, param.q, param.g, param.y, param.x);
    -1351         return key;
    -1352     }
    -1353 
    -1354     // 3. JWK
    -1355     // 3.1. JWK RSA
    -1356     // 3.1.1. JWK RSA public key by b64u values
    -1357     if (param.kty === "RSA" &&
    -1358 	param.n !== undefined &&
    -1359 	param.e !== undefined &&
    -1360 	param.d === undefined) {
    -1361 	var key = new RSAKey();
    -1362 	key.setPublic(b64utohex(param.n), b64utohex(param.e));
    -1363 	return key;
    -1364     }
    -1365 
    -1366     // 3.1.2. JWK RSA private key with p/q/dp/dq/coeff by b64u values
    -1367     if (param.kty === "RSA" &&
    -1368 	param.n !== undefined &&
    -1369 	param.e !== undefined &&
    -1370 	param.d !== undefined &&
    -1371 	param.p !== undefined &&
    -1372 	param.q !== undefined &&
    -1373 	param.dp !== undefined &&
    -1374 	param.dq !== undefined &&
    -1375 	param.qi !== undefined) {
    -1376 	var key = new RSAKey();
    -1377         key.setPrivateEx(b64utohex(param.n),
    -1378 			 b64utohex(param.e),
    -1379 			 b64utohex(param.d),
    -1380 			 b64utohex(param.p),
    -1381 			 b64utohex(param.q),
    -1382                          b64utohex(param.dp),
    -1383 			 b64utohex(param.dq),
    -1384 			 b64utohex(param.qi));
    -1385 	return key;
    -1386     }
    -1387 
    -1388     // 3.1.3. JWK RSA private key without p/q/dp/dq/coeff by b64u
    -1389     //        since jsrsasign 5.0.0 keyutil 1.0.11
    -1390     if (param.kty === "RSA" &&
    -1391 	param.n !== undefined &&
    -1392 	param.e !== undefined &&
    -1393 	param.d !== undefined) {
    -1394 	var key = new RSAKey();
    -1395         key.setPrivate(b64utohex(param.n),
    -1396 		       b64utohex(param.e),
    -1397 		       b64utohex(param.d));
    -1398 	return key;
    -1399     }
    -1400 
    -1401     // 3.2. JWK ECC
    -1402     // 3.2.1. JWK ECC public key by b64u values
    -1403     if (param.kty === "EC" &&
    -1404 	param.crv !== undefined &&
    -1405 	param.x !== undefined &&
    -1406 	param.y !== undefined &&
    -1407         param.d === undefined) {
    -1408 	var ec = new KJUR.crypto.ECDSA({"curve": param.crv});
    -1409 	var charlen = ec.ecparams.keylen / 4;
    -1410         var hX   = ("0000000000" + b64utohex(param.x)).slice(- charlen);
    -1411         var hY   = ("0000000000" + b64utohex(param.y)).slice(- charlen);
    -1412         var hPub = "04" + hX + hY;
    -1413 	ec.setPublicKeyHex(hPub);
    -1414 	return ec;
    -1415     }
    -1416 
    -1417     // 3.2.2. JWK ECC private key by b64u values
    -1418     if (param.kty === "EC" &&
    -1419 	param.crv !== undefined &&
    -1420 	param.x !== undefined &&
    -1421 	param.y !== undefined &&
    -1422         param.d !== undefined) {
    -1423 	var ec = new KJUR.crypto.ECDSA({"curve": param.crv});
    -1424 	var charlen = ec.ecparams.keylen / 4;
    -1425         var hX   = ("0000000000" + b64utohex(param.x)).slice(- charlen);
    -1426         var hY   = ("0000000000" + b64utohex(param.y)).slice(- charlen);
    -1427         var hPub = "04" + hX + hY;
    -1428         var hPrv = ("0000000000" + b64utohex(param.d)).slice(- charlen);
    -1429 	ec.setPublicKeyHex(hPub);
    -1430 	ec.setPrivateKeyHex(hPrv);
    -1431 	return ec;
    -1432     }
    -1433     
    -1434     // 4. by PEM certificate (-----BEGIN ... CERTIFITE----)
    -1435     if (param.indexOf("-END CERTIFICATE-", 0) != -1 ||
    -1436         param.indexOf("-END X509 CERTIFICATE-", 0) != -1 ||
    -1437         param.indexOf("-END TRUSTED CERTIFICATE-", 0) != -1) {
    -1438         return X509.getPublicKeyFromCertPEM(param);
    -1439     }
    -1440 
    -1441     // 4. public key by PKCS#8 hexadecimal string
    -1442     if (hextype === "pkcs8pub") {
    -1443         return KEYUTIL.getKeyFromPublicPKCS8Hex(param);
    -1444     }
    -1445 
    -1446     // 5. public key by PKCS#8 PEM string
    -1447     if (param.indexOf("-END PUBLIC KEY-") != -1) {
    -1448         return KEYUTIL.getKeyFromPublicPKCS8PEM(param);
    -1449     }
    -1450     
    -1451     // 6. private key by PKCS#5 plain hexadecimal RSA string
    -1452     if (hextype === "pkcs5prv") {
    -1453         var key = new RSAKey();
    -1454         key.readPrivateKeyFromASN1HexString(param);
    -1455         return key;
    -1456     }
    -1457 
    -1458     // 7. private key by plain PKCS#5 hexadecimal RSA string
    -1459     if (hextype === "pkcs5prv") {
    -1460         var key = new RSAKey();
    -1461         key.readPrivateKeyFromASN1HexString(param);
    -1462         return key;
    -1463     }
    -1464 
    -1465     // 8. private key by plain PKCS#5 PEM RSA string 
    -1466     //    getKey("-----BEGIN RSA PRIVATE KEY-...")
    -1467     if (param.indexOf("-END RSA PRIVATE KEY-") != -1 &&
    -1468         param.indexOf("4,ENCRYPTED") == -1) {
    -1469         var hex = KEYUTIL.getHexFromPEM(param, "RSA PRIVATE KEY");
    -1470         return KEYUTIL.getKey(hex, null, "pkcs5prv");
    -1471     }
    -1472 
    -1473     // 8.2. private key by plain PKCS#5 PEM DSA string
    -1474     if (param.indexOf("-END DSA PRIVATE KEY-") != -1 &&
    -1475         param.indexOf("4,ENCRYPTED") == -1) {
    -1476 
    -1477         var hKey = this.getHexFromPEM(param, "DSA PRIVATE KEY");
    -1478         var p = ASN1HEX.getVbyList(hKey, 0, [1], "02");
    -1479         var q = ASN1HEX.getVbyList(hKey, 0, [2], "02");
    -1480         var g = ASN1HEX.getVbyList(hKey, 0, [3], "02");
    -1481         var y = ASN1HEX.getVbyList(hKey, 0, [4], "02");
    -1482         var x = ASN1HEX.getVbyList(hKey, 0, [5], "02");
    -1483         var key = new KJUR.crypto.DSA();
    -1484         key.setPrivate(new BigInteger(p, 16),
    -1485                        new BigInteger(q, 16),
    -1486                        new BigInteger(g, 16),
    -1487                        new BigInteger(y, 16),
    -1488                        new BigInteger(x, 16));
    -1489         return key;
    -1490     }
    -1491 
    -1492     // 9. private key by plain PKCS#8 PEM ECC/RSA string
    -1493     if (param.indexOf("-END PRIVATE KEY-") != -1) {
    -1494         return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(param);
    -1495     }
    -1496 
    -1497     // 10. private key by encrypted PKCS#5 PEM RSA string
    -1498     if (param.indexOf("-END RSA PRIVATE KEY-") != -1 &&
    -1499         param.indexOf("4,ENCRYPTED") != -1) {
    -1500         return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(param, passcode);
    -1501     }
    -1502 
    -1503     // 10.2. private key by encrypted PKCS#5 PEM ECDSA string
    -1504     if (param.indexOf("-END EC PRIVATE KEY-") != -1 &&
    -1505         param.indexOf("4,ENCRYPTED") != -1) {
    -1506         var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode);
    -1507 
    -1508         var key = ASN1HEX.getVbyList(hKey, 0, [1], "04");
    -1509         var curveNameOidHex = ASN1HEX.getVbyList(hKey, 0, [2,0], "06");
    -1510         var pubkey = ASN1HEX.getVbyList(hKey, 0, [3,0], "03").substr(2);
    -1511         var curveName = "";
    -1512 
    -1513         if (KJUR.crypto.OID.oidhex2name[curveNameOidHex] !== undefined) {
    -1514             curveName = KJUR.crypto.OID.oidhex2name[curveNameOidHex];
    -1515         } else {
    -1516             throw "undefined OID(hex) in KJUR.crypto.OID: " + curveNameOidHex;
    -1517         }
    -1518 
    -1519         var ec = new KJUR.crypto.ECDSA({'name': curveName});
    -1520         ec.setPublicKeyHex(pubkey);
    -1521         ec.setPrivateKeyHex(key);
    -1522         ec.isPublic = false;
    -1523         return ec;
    -1524     }
    -1525 
    -1526     // 10.3. private key by encrypted PKCS#5 PEM DSA string
    -1527     if (param.indexOf("-END DSA PRIVATE KEY-") != -1 &&
    -1528         param.indexOf("4,ENCRYPTED") != -1) {
    -1529         var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode);
    -1530         var p = ASN1HEX.getVbyList(hKey, 0, [1], "02");
    -1531         var q = ASN1HEX.getVbyList(hKey, 0, [2], "02");
    -1532         var g = ASN1HEX.getVbyList(hKey, 0, [3], "02");
    -1533         var y = ASN1HEX.getVbyList(hKey, 0, [4], "02");
    -1534         var x = ASN1HEX.getVbyList(hKey, 0, [5], "02");
    -1535         var key = new KJUR.crypto.DSA();
    -1536         key.setPrivate(new BigInteger(p, 16),
    -1537                        new BigInteger(q, 16),
    -1538                        new BigInteger(g, 16),
    -1539                        new BigInteger(y, 16),
    -1540                        new BigInteger(x, 16));
    -1541         return key;
    -1542     }
    -1543 
    -1544     // 11. private key by encrypted PKCS#8 hexadecimal RSA/ECDSA string
    -1545     if (param.indexOf("-END ENCRYPTED PRIVATE KEY-") != -1) {
    -1546         return KEYUTIL.getKeyFromEncryptedPKCS8PEM(param, passcode);
    -1547     }
    -1548 
    -1549     throw "not supported argument";
    -1550 };
    -1551 
    -1552 /**
    -1553  * @name generateKeypair
    -1554  * @memberOf KEYUTIL
    -1555  * @function
    -1556  * @static
    -1557  * @param {String} alg 'RSA' or 'EC'
    -1558  * @param {Object} keylenOrCurve key length for RSA or curve name for EC
    -1559  * @return {Array} associative array of keypair which has prvKeyObj and pubKeyObj parameters
    -1560  * @since keyutil 1.0.1
    -1561  * @description
    -1562  * This method generates a key pair of public key algorithm.
    -1563  * The result will be an associative array which has following
    -1564  * parameters:
    -1565  * <ul>
    -1566  * <li>prvKeyObj - RSAKey or ECDSA object of private key</li>
    -1567  * <li>pubKeyObj - RSAKey or ECDSA object of public key</li>
    -1568  * </ul>
    -1569  * NOTE1: As for RSA algoirthm, public exponent has fixed
    -1570  * value '0x10001'.
    -1571  * NOTE2: As for EC algorithm, supported names of curve are
    -1572  * secp256r1, secp256k1 and secp384r1.
    -1573  * NOTE3: DSA is not supported yet.
    -1574  * @example
    -1575  * var rsaKeypair = KEYUTIL.generateKeypair("RSA", 1024);
    -1576  * var ecKeypair = KEYUTIL.generateKeypair("EC", "secp256r1");
    -1577  *
    -1578  */
    -1579 KEYUTIL.generateKeypair = function(alg, keylenOrCurve) {
    -1580     if (alg == "RSA") {
    -1581         var keylen = keylenOrCurve;
    -1582         var prvKey = new RSAKey();
    -1583         prvKey.generate(keylen, '10001');
    -1584         prvKey.isPrivate = true;
    -1585         prvKey.isPublic = true;
    -1586         
    -1587         var pubKey = new RSAKey();
    -1588         var hN = prvKey.n.toString(16);
    -1589         var hE = prvKey.e.toString(16);
    -1590         pubKey.setPublic(hN, hE);
    -1591         pubKey.isPrivate = false;
    -1592         pubKey.isPublic = true;
    -1593         
    -1594         var result = {};
    -1595         result.prvKeyObj = prvKey;
    -1596         result.pubKeyObj = pubKey;
    -1597         return result;
    -1598     } else if (alg == "EC") {
    -1599         var curve = keylenOrCurve;
    -1600         var ec = new KJUR.crypto.ECDSA({curve: curve});
    -1601         var keypairHex = ec.generateKeyPairHex();
    -1602 
    -1603         var prvKey = new KJUR.crypto.ECDSA({curve: curve});
    -1604         prvKey.setPublicKeyHex(keypairHex.ecpubhex);
    -1605         prvKey.setPrivateKeyHex(keypairHex.ecprvhex);
    -1606         prvKey.isPrivate = true;
    -1607         prvKey.isPublic = false;
    -1608 
    -1609         var pubKey = new KJUR.crypto.ECDSA({curve: curve});
    -1610         pubKey.setPublicKeyHex(keypairHex.ecpubhex);
    -1611         pubKey.isPrivate = false;
    -1612         pubKey.isPublic = true;
    -1613 
    -1614         var result = {};
    -1615         result.prvKeyObj = prvKey;
    -1616         result.pubKeyObj = pubKey;
    -1617         return result;
    -1618     } else {
    -1619         throw "unknown algorithm: " + alg;
    -1620     }
    -1621 };
    -1622 
    -1623 /**
    -1624  * get PEM formatted private or public key file from a RSA/ECDSA/DSA key object
    -1625  * @name getPEM
    -1626  * @memberOf KEYUTIL
    -1627  * @function
    -1628  * @static
    -1629  * @param {Object} keyObjOrHex key object {@link RSAKey}, {@link KJUR.crypto.ECDSA} or {@link KJUR.crypto.DSA} to encode to
    -1630  * @param {String} formatType (OPTION) output format type of "PKCS1PRV", "PKCS5PRV" or "PKCS8PRV" for private key
    -1631  * @param {String} passwd (OPTION) password to protect private key
    -1632  * @param {String} encAlg (OPTION) encryption algorithm for PKCS#5. currently supports DES-CBC, DES-EDE3-CBC and AES-{128,192,256}-CBC
    -1633  * @since keyutil 1.0.4
    -1634  * @description
    -1635  * <dl>
    -1636  * <dt><b>NOTE1:</b>
    -1637  * <dd>
    -1638  * PKCS#5 encrypted private key protection algorithm supports DES-CBC, 
    -1639  * DES-EDE3-CBC and AES-{128,192,256}-CBC
    -1640  * <dt><b>NOTE2:</b>
    -1641  * <dd>
    -1642  * OpenSSL supports
    -1643  * </dl>
    -1644  * @example
    -1645  * KEUUTIL.getPEM(publicKey) => generates PEM PKCS#8 public key 
    -1646  * KEUUTIL.getPEM(privateKey, "PKCS1PRV") => generates PEM PKCS#1 plain private key
    -1647  * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass") => generates PEM PKCS#5 encrypted private key 
    -1648  *                                                          with DES-EDE3-CBC (DEFAULT)
    -1649  * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass", "DES-CBC") => generates PEM PKCS#5 encrypted 
    -1650  *                                                                 private key with DES-CBC
    -1651  * KEUUTIL.getPEM(privateKey, "PKCS8PRV") => generates PEM PKCS#8 plain private key
    -1652  * KEUUTIL.getPEM(privateKey, "PKCS8PRV", "pass") => generates PEM PKCS#8 encrypted private key
    -1653  *                                                      with PBKDF2_HmacSHA1_3DES
    -1654  */
    -1655 KEYUTIL.getPEM = function(keyObjOrHex, formatType, passwd, encAlg, hexType) {
    -1656     var ns1 = KJUR.asn1;
    -1657     var ns2 = KJUR.crypto;
    -1658 
    -1659     function _rsaprv2asn1obj(keyObjOrHex) {
    -1660         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    -1661             "seq": [
    -1662                 {"int": 0 },
    -1663                 {"int": {"bigint": keyObjOrHex.n}},
    -1664                 {"int": keyObjOrHex.e},
    -1665                 {"int": {"bigint": keyObjOrHex.d}},
    -1666                 {"int": {"bigint": keyObjOrHex.p}},
    -1667                 {"int": {"bigint": keyObjOrHex.q}},
    -1668                 {"int": {"bigint": keyObjOrHex.dmp1}},
    -1669                 {"int": {"bigint": keyObjOrHex.dmq1}},
    -1670                 {"int": {"bigint": keyObjOrHex.coeff}}
    -1671             ]
    -1672         });
    -1673         return asn1Obj;
    -1674     };
    -1675 
    -1676     function _ecdsaprv2asn1obj(keyObjOrHex) {
    -1677         var asn1Obj2 = KJUR.asn1.ASN1Util.newObject({
    -1678             "seq": [
    -1679                 {"int": 1 },
    -1680                 {"octstr": {"hex": keyObjOrHex.prvKeyHex}},
    -1681                 {"tag": ['a0', true, {'oid': {'name': keyObjOrHex.curveName}}]},
    -1682                 {"tag": ['a1', true, {'bitstr': {'hex': '00' + keyObjOrHex.pubKeyHex}}]}
    -1683             ]
    -1684         });
    -1685         return asn1Obj2;
    -1686     };
    -1687 
    -1688     function _dsaprv2asn1obj(keyObjOrHex) {
    -1689         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    -1690             "seq": [
    -1691                 {"int": 0 },
    -1692                 {"int": {"bigint": keyObjOrHex.p}},
    -1693                 {"int": {"bigint": keyObjOrHex.q}},
    -1694                 {"int": {"bigint": keyObjOrHex.g}},
    -1695                 {"int": {"bigint": keyObjOrHex.y}},
    -1696                 {"int": {"bigint": keyObjOrHex.x}}
    -1697             ]
    -1698         });
    -1699         return asn1Obj;
    -1700     };
    -1701 
    -1702     // 1. public key
    +1285     // 2. by parameters of key
    +1286 
    +1287     // 2.1. bare ECC
    +1288     // 2.1.1. bare ECC public key by hex values
    +1289     if (param.curve !== undefined &&
    +1290 	param.xy !== undefined && param.d === undefined) {
    +1291         return new KJUR.crypto.ECDSA({pub: param.xy, curve: param.curve});
    +1292     }
    +1293 
    +1294     // 2.1.2. bare ECC private key by hex values
    +1295     if (param.curve !== undefined && param.d !== undefined) {
    +1296         return new KJUR.crypto.ECDSA({prv: param.d, curve: param.curve});
    +1297     }
    +1298 
    +1299     // 2.2. bare RSA
    +1300     // 2.2.1. bare RSA public key by hex values
    +1301     if (param.kty === undefined &&
    +1302 	param.n !== undefined && param.e !== undefined &&
    +1303         param.d === undefined) {
    +1304         var key = new RSAKey();
    +1305         key.setPublic(param.n, param.e);
    +1306         return key;
    +1307     }
    +1308 
    +1309     // 2.2.2. bare RSA private key with P/Q/DP/DQ/COEFF by hex values
    +1310     if (param.kty === undefined &&
    +1311 	param.n !== undefined &&
    +1312 	param.e !== undefined &&
    +1313 	param.d !== undefined &&
    +1314         param.p !== undefined &&
    +1315 	param.q !== undefined &&
    +1316         param.dp !== undefined &&
    +1317 	param.dq !== undefined &&
    +1318 	param.co !== undefined &&
    +1319         param.qi === undefined) {
    +1320         var key = new RSAKey();
    +1321         key.setPrivateEx(param.n, param.e, param.d, param.p, param.q,
    +1322                          param.dp, param.dq, param.co);
    +1323         return key;
    +1324     }
    +1325 
    +1326     // 2.2.3. bare RSA public key without P/Q/DP/DQ/COEFF by hex values
    +1327     if (param.kty === undefined &&
    +1328 	param.n !== undefined &&
    +1329 	param.e !== undefined &&
    +1330 	param.d !== undefined &&
    +1331         param.p === undefined) {
    +1332         var key = new RSAKey();
    +1333         key.setPrivate(param.n, param.e, param.d);
    +1334         return key;
    +1335     }
    +1336 
    +1337     // 2.3. bare DSA
    +1338     // 2.3.1. bare DSA public key by hex values
    +1339     if (param.p !== undefined && param.q !== undefined &&
    +1340 	param.g !== undefined &&
    +1341         param.y !== undefined && param.x === undefined) {
    +1342         var key = new KJUR.crypto.DSA();
    +1343         key.setPublic(param.p, param.q, param.g, param.y);
    +1344         return key;
    +1345     }
    +1346 
    +1347     // 2.3.2. bare DSA private key by hex values
    +1348     if (param.p !== undefined && param.q !== undefined &&
    +1349 	param.g !== undefined &&
    +1350         param.y !== undefined && param.x !== undefined) {
    +1351         var key = new KJUR.crypto.DSA();
    +1352         key.setPrivate(param.p, param.q, param.g, param.y, param.x);
    +1353         return key;
    +1354     }
    +1355 
    +1356     // 3. JWK
    +1357     // 3.1. JWK RSA
    +1358     // 3.1.1. JWK RSA public key by b64u values
    +1359     if (param.kty === "RSA" &&
    +1360 	param.n !== undefined &&
    +1361 	param.e !== undefined &&
    +1362 	param.d === undefined) {
    +1363 	var key = new RSAKey();
    +1364 	key.setPublic(b64utohex(param.n), b64utohex(param.e));
    +1365 	return key;
    +1366     }
    +1367 
    +1368     // 3.1.2. JWK RSA private key with p/q/dp/dq/coeff by b64u values
    +1369     if (param.kty === "RSA" &&
    +1370 	param.n !== undefined &&
    +1371 	param.e !== undefined &&
    +1372 	param.d !== undefined &&
    +1373 	param.p !== undefined &&
    +1374 	param.q !== undefined &&
    +1375 	param.dp !== undefined &&
    +1376 	param.dq !== undefined &&
    +1377 	param.qi !== undefined) {
    +1378 	var key = new RSAKey();
    +1379         key.setPrivateEx(b64utohex(param.n),
    +1380 			 b64utohex(param.e),
    +1381 			 b64utohex(param.d),
    +1382 			 b64utohex(param.p),
    +1383 			 b64utohex(param.q),
    +1384                          b64utohex(param.dp),
    +1385 			 b64utohex(param.dq),
    +1386 			 b64utohex(param.qi));
    +1387 	return key;
    +1388     }
    +1389 
    +1390     // 3.1.3. JWK RSA private key without p/q/dp/dq/coeff by b64u
    +1391     //        since jsrsasign 5.0.0 keyutil 1.0.11
    +1392     if (param.kty === "RSA" &&
    +1393 	param.n !== undefined &&
    +1394 	param.e !== undefined &&
    +1395 	param.d !== undefined) {
    +1396 	var key = new RSAKey();
    +1397         key.setPrivate(b64utohex(param.n),
    +1398 		       b64utohex(param.e),
    +1399 		       b64utohex(param.d));
    +1400 	return key;
    +1401     }
    +1402 
    +1403     // 3.2. JWK ECC
    +1404     // 3.2.1. JWK ECC public key by b64u values
    +1405     if (param.kty === "EC" &&
    +1406 	param.crv !== undefined &&
    +1407 	param.x !== undefined &&
    +1408 	param.y !== undefined &&
    +1409         param.d === undefined) {
    +1410 	var ec = new KJUR.crypto.ECDSA({"curve": param.crv});
    +1411 	var charlen = ec.ecparams.keylen / 4;
    +1412         var hX   = ("0000000000" + b64utohex(param.x)).slice(- charlen);
    +1413         var hY   = ("0000000000" + b64utohex(param.y)).slice(- charlen);
    +1414         var hPub = "04" + hX + hY;
    +1415 	ec.setPublicKeyHex(hPub);
    +1416 	return ec;
    +1417     }
    +1418 
    +1419     // 3.2.2. JWK ECC private key by b64u values
    +1420     if (param.kty === "EC" &&
    +1421 	param.crv !== undefined &&
    +1422 	param.x !== undefined &&
    +1423 	param.y !== undefined &&
    +1424         param.d !== undefined) {
    +1425 	var ec = new KJUR.crypto.ECDSA({"curve": param.crv});
    +1426 	var charlen = ec.ecparams.keylen / 4;
    +1427         var hX   = ("0000000000" + b64utohex(param.x)).slice(- charlen);
    +1428         var hY   = ("0000000000" + b64utohex(param.y)).slice(- charlen);
    +1429         var hPub = "04" + hX + hY;
    +1430         var hPrv = ("0000000000" + b64utohex(param.d)).slice(- charlen);
    +1431 	ec.setPublicKeyHex(hPub);
    +1432 	ec.setPrivateKeyHex(hPrv);
    +1433 	return ec;
    +1434     }
    +1435     
    +1436     // 4. by PEM certificate (-----BEGIN ... CERTIFITE----)
    +1437     if (param.indexOf("-END CERTIFICATE-", 0) != -1 ||
    +1438         param.indexOf("-END X509 CERTIFICATE-", 0) != -1 ||
    +1439         param.indexOf("-END TRUSTED CERTIFICATE-", 0) != -1) {
    +1440         return X509.getPublicKeyFromCertPEM(param);
    +1441     }
    +1442 
    +1443     // 4. public key by PKCS#8 hexadecimal string
    +1444     if (hextype === "pkcs8pub") {
    +1445         return KEYUTIL.getKeyFromPublicPKCS8Hex(param);
    +1446     }
    +1447 
    +1448     // 5. public key by PKCS#8 PEM string
    +1449     if (param.indexOf("-END PUBLIC KEY-") != -1) {
    +1450         return KEYUTIL.getKeyFromPublicPKCS8PEM(param);
    +1451     }
    +1452     
    +1453     // 6. private key by PKCS#5 plain hexadecimal RSA string
    +1454     if (hextype === "pkcs5prv") {
    +1455         var key = new RSAKey();
    +1456         key.readPrivateKeyFromASN1HexString(param);
    +1457         return key;
    +1458     }
    +1459 
    +1460     // 7. private key by plain PKCS#5 hexadecimal RSA string
    +1461     if (hextype === "pkcs5prv") {
    +1462         var key = new RSAKey();
    +1463         key.readPrivateKeyFromASN1HexString(param);
    +1464         return key;
    +1465     }
    +1466 
    +1467     // 8. private key by plain PKCS#5 PEM RSA string 
    +1468     //    getKey("-----BEGIN RSA PRIVATE KEY-...")
    +1469     if (param.indexOf("-END RSA PRIVATE KEY-") != -1 &&
    +1470         param.indexOf("4,ENCRYPTED") == -1) {
    +1471         var hex = KEYUTIL.getHexFromPEM(param, "RSA PRIVATE KEY");
    +1472         return KEYUTIL.getKey(hex, null, "pkcs5prv");
    +1473     }
    +1474 
    +1475     // 8.2. private key by plain PKCS#5 PEM DSA string
    +1476     if (param.indexOf("-END DSA PRIVATE KEY-") != -1 &&
    +1477         param.indexOf("4,ENCRYPTED") == -1) {
    +1478 
    +1479         var hKey = this.getHexFromPEM(param, "DSA PRIVATE KEY");
    +1480         var p = ASN1HEX.getVbyList(hKey, 0, [1], "02");
    +1481         var q = ASN1HEX.getVbyList(hKey, 0, [2], "02");
    +1482         var g = ASN1HEX.getVbyList(hKey, 0, [3], "02");
    +1483         var y = ASN1HEX.getVbyList(hKey, 0, [4], "02");
    +1484         var x = ASN1HEX.getVbyList(hKey, 0, [5], "02");
    +1485         var key = new KJUR.crypto.DSA();
    +1486         key.setPrivate(new BigInteger(p, 16),
    +1487                        new BigInteger(q, 16),
    +1488                        new BigInteger(g, 16),
    +1489                        new BigInteger(y, 16),
    +1490                        new BigInteger(x, 16));
    +1491         return key;
    +1492     }
    +1493 
    +1494     // 9. private key by plain PKCS#8 PEM ECC/RSA string
    +1495     if (param.indexOf("-END PRIVATE KEY-") != -1) {
    +1496         return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(param);
    +1497     }
    +1498 
    +1499     // 10. private key by encrypted PKCS#5 PEM RSA string
    +1500     if (param.indexOf("-END RSA PRIVATE KEY-") != -1 &&
    +1501         param.indexOf("4,ENCRYPTED") != -1) {
    +1502         return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(param, passcode);
    +1503     }
    +1504 
    +1505     // 10.2. private key by encrypted PKCS#5 PEM ECDSA string
    +1506     if (param.indexOf("-END EC PRIVATE KEY-") != -1 &&
    +1507         param.indexOf("4,ENCRYPTED") != -1) {
    +1508         var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode);
    +1509 
    +1510         var key = ASN1HEX.getVbyList(hKey, 0, [1], "04");
    +1511         var curveNameOidHex = ASN1HEX.getVbyList(hKey, 0, [2,0], "06");
    +1512         var pubkey = ASN1HEX.getVbyList(hKey, 0, [3,0], "03").substr(2);
    +1513         var curveName = "";
    +1514 
    +1515         if (KJUR.crypto.OID.oidhex2name[curveNameOidHex] !== undefined) {
    +1516             curveName = KJUR.crypto.OID.oidhex2name[curveNameOidHex];
    +1517         } else {
    +1518             throw "undefined OID(hex) in KJUR.crypto.OID: " + curveNameOidHex;
    +1519         }
    +1520 
    +1521         var ec = new KJUR.crypto.ECDSA({'name': curveName});
    +1522         ec.setPublicKeyHex(pubkey);
    +1523         ec.setPrivateKeyHex(key);
    +1524         ec.isPublic = false;
    +1525         return ec;
    +1526     }
    +1527 
    +1528     // 10.3. private key by encrypted PKCS#5 PEM DSA string
    +1529     if (param.indexOf("-END DSA PRIVATE KEY-") != -1 &&
    +1530         param.indexOf("4,ENCRYPTED") != -1) {
    +1531         var hKey = KEYUTIL.getDecryptedKeyHex(param, passcode);
    +1532         var p = ASN1HEX.getVbyList(hKey, 0, [1], "02");
    +1533         var q = ASN1HEX.getVbyList(hKey, 0, [2], "02");
    +1534         var g = ASN1HEX.getVbyList(hKey, 0, [3], "02");
    +1535         var y = ASN1HEX.getVbyList(hKey, 0, [4], "02");
    +1536         var x = ASN1HEX.getVbyList(hKey, 0, [5], "02");
    +1537         var key = new KJUR.crypto.DSA();
    +1538         key.setPrivate(new BigInteger(p, 16),
    +1539                        new BigInteger(q, 16),
    +1540                        new BigInteger(g, 16),
    +1541                        new BigInteger(y, 16),
    +1542                        new BigInteger(x, 16));
    +1543         return key;
    +1544     }
    +1545 
    +1546     // 11. private key by encrypted PKCS#8 hexadecimal RSA/ECDSA string
    +1547     if (param.indexOf("-END ENCRYPTED PRIVATE KEY-") != -1) {
    +1548         return KEYUTIL.getKeyFromEncryptedPKCS8PEM(param, passcode);
    +1549     }
    +1550 
    +1551     throw "not supported argument";
    +1552 };
    +1553 
    +1554 /**
    +1555  * @name generateKeypair
    +1556  * @memberOf KEYUTIL
    +1557  * @function
    +1558  * @static
    +1559  * @param {String} alg 'RSA' or 'EC'
    +1560  * @param {Object} keylenOrCurve key length for RSA or curve name for EC
    +1561  * @return {Array} associative array of keypair which has prvKeyObj and pubKeyObj parameters
    +1562  * @since keyutil 1.0.1
    +1563  * @description
    +1564  * This method generates a key pair of public key algorithm.
    +1565  * The result will be an associative array which has following
    +1566  * parameters:
    +1567  * <ul>
    +1568  * <li>prvKeyObj - RSAKey or ECDSA object of private key</li>
    +1569  * <li>pubKeyObj - RSAKey or ECDSA object of public key</li>
    +1570  * </ul>
    +1571  * NOTE1: As for RSA algoirthm, public exponent has fixed
    +1572  * value '0x10001'.
    +1573  * NOTE2: As for EC algorithm, supported names of curve are
    +1574  * secp256r1, secp256k1 and secp384r1.
    +1575  * NOTE3: DSA is not supported yet.
    +1576  * @example
    +1577  * var rsaKeypair = KEYUTIL.generateKeypair("RSA", 1024);
    +1578  * var ecKeypair = KEYUTIL.generateKeypair("EC", "secp256r1");
    +1579  *
    +1580  */
    +1581 KEYUTIL.generateKeypair = function(alg, keylenOrCurve) {
    +1582     if (alg == "RSA") {
    +1583         var keylen = keylenOrCurve;
    +1584         var prvKey = new RSAKey();
    +1585         prvKey.generate(keylen, '10001');
    +1586         prvKey.isPrivate = true;
    +1587         prvKey.isPublic = true;
    +1588         
    +1589         var pubKey = new RSAKey();
    +1590         var hN = prvKey.n.toString(16);
    +1591         var hE = prvKey.e.toString(16);
    +1592         pubKey.setPublic(hN, hE);
    +1593         pubKey.isPrivate = false;
    +1594         pubKey.isPublic = true;
    +1595         
    +1596         var result = {};
    +1597         result.prvKeyObj = prvKey;
    +1598         result.pubKeyObj = pubKey;
    +1599         return result;
    +1600     } else if (alg == "EC") {
    +1601         var curve = keylenOrCurve;
    +1602         var ec = new KJUR.crypto.ECDSA({curve: curve});
    +1603         var keypairHex = ec.generateKeyPairHex();
    +1604 
    +1605         var prvKey = new KJUR.crypto.ECDSA({curve: curve});
    +1606         prvKey.setPublicKeyHex(keypairHex.ecpubhex);
    +1607         prvKey.setPrivateKeyHex(keypairHex.ecprvhex);
    +1608         prvKey.isPrivate = true;
    +1609         prvKey.isPublic = false;
    +1610 
    +1611         var pubKey = new KJUR.crypto.ECDSA({curve: curve});
    +1612         pubKey.setPublicKeyHex(keypairHex.ecpubhex);
    +1613         pubKey.isPrivate = false;
    +1614         pubKey.isPublic = true;
    +1615 
    +1616         var result = {};
    +1617         result.prvKeyObj = prvKey;
    +1618         result.pubKeyObj = pubKey;
    +1619         return result;
    +1620     } else {
    +1621         throw "unknown algorithm: " + alg;
    +1622     }
    +1623 };
    +1624 
    +1625 /**
    +1626  * get PEM formatted private or public key file from a RSA/ECDSA/DSA key object
    +1627  * @name getPEM
    +1628  * @memberOf KEYUTIL
    +1629  * @function
    +1630  * @static
    +1631  * @param {Object} keyObjOrHex key object {@link RSAKey}, {@link KJUR.crypto.ECDSA} or {@link KJUR.crypto.DSA} to encode to
    +1632  * @param {String} formatType (OPTION) output format type of "PKCS1PRV", "PKCS5PRV" or "PKCS8PRV" for private key
    +1633  * @param {String} passwd (OPTION) password to protect private key
    +1634  * @param {String} encAlg (OPTION) encryption algorithm for PKCS#5. currently supports DES-CBC, DES-EDE3-CBC and AES-{128,192,256}-CBC
    +1635  * @since keyutil 1.0.4
    +1636  * @description
    +1637  * <dl>
    +1638  * <dt><b>NOTE1:</b>
    +1639  * <dd>
    +1640  * PKCS#5 encrypted private key protection algorithm supports DES-CBC, 
    +1641  * DES-EDE3-CBC and AES-{128,192,256}-CBC
    +1642  * <dt><b>NOTE2:</b>
    +1643  * <dd>
    +1644  * OpenSSL supports
    +1645  * </dl>
    +1646  * @example
    +1647  * KEUUTIL.getPEM(publicKey) => generates PEM PKCS#8 public key 
    +1648  * KEUUTIL.getPEM(privateKey, "PKCS1PRV") => generates PEM PKCS#1 plain private key
    +1649  * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass") => generates PEM PKCS#5 encrypted private key 
    +1650  *                                                          with DES-EDE3-CBC (DEFAULT)
    +1651  * KEUUTIL.getPEM(privateKey, "PKCS5PRV", "pass", "DES-CBC") => generates PEM PKCS#5 encrypted 
    +1652  *                                                                 private key with DES-CBC
    +1653  * KEUUTIL.getPEM(privateKey, "PKCS8PRV") => generates PEM PKCS#8 plain private key
    +1654  * KEUUTIL.getPEM(privateKey, "PKCS8PRV", "pass") => generates PEM PKCS#8 encrypted private key
    +1655  *                                                      with PBKDF2_HmacSHA1_3DES
    +1656  */
    +1657 KEYUTIL.getPEM = function(keyObjOrHex, formatType, passwd, encAlg, hexType) {
    +1658     var ns1 = KJUR.asn1;
    +1659     var ns2 = KJUR.crypto;
    +1660 
    +1661     function _rsaprv2asn1obj(keyObjOrHex) {
    +1662         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    +1663             "seq": [
    +1664                 {"int": 0 },
    +1665                 {"int": {"bigint": keyObjOrHex.n}},
    +1666                 {"int": keyObjOrHex.e},
    +1667                 {"int": {"bigint": keyObjOrHex.d}},
    +1668                 {"int": {"bigint": keyObjOrHex.p}},
    +1669                 {"int": {"bigint": keyObjOrHex.q}},
    +1670                 {"int": {"bigint": keyObjOrHex.dmp1}},
    +1671                 {"int": {"bigint": keyObjOrHex.dmq1}},
    +1672                 {"int": {"bigint": keyObjOrHex.coeff}}
    +1673             ]
    +1674         });
    +1675         return asn1Obj;
    +1676     };
    +1677 
    +1678     function _ecdsaprv2asn1obj(keyObjOrHex) {
    +1679         var asn1Obj2 = KJUR.asn1.ASN1Util.newObject({
    +1680             "seq": [
    +1681                 {"int": 1 },
    +1682                 {"octstr": {"hex": keyObjOrHex.prvKeyHex}},
    +1683                 {"tag": ['a0', true, {'oid': {'name': keyObjOrHex.curveName}}]},
    +1684                 {"tag": ['a1', true, {'bitstr': {'hex': '00' + keyObjOrHex.pubKeyHex}}]}
    +1685             ]
    +1686         });
    +1687         return asn1Obj2;
    +1688     };
    +1689 
    +1690     function _dsaprv2asn1obj(keyObjOrHex) {
    +1691         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    +1692             "seq": [
    +1693                 {"int": 0 },
    +1694                 {"int": {"bigint": keyObjOrHex.p}},
    +1695                 {"int": {"bigint": keyObjOrHex.q}},
    +1696                 {"int": {"bigint": keyObjOrHex.g}},
    +1697                 {"int": {"bigint": keyObjOrHex.y}},
    +1698                 {"int": {"bigint": keyObjOrHex.x}}
    +1699             ]
    +1700         });
    +1701         return asn1Obj;
    +1702     };
     1703 
    -1704     // x. PEM PKCS#8 public key of RSA/ECDSA/DSA public key object
    -1705     if (((typeof RSAKey != "undefined" && keyObjOrHex instanceof RSAKey) ||
    -1706          (typeof ns2.DSA != "undefined" && keyObjOrHex instanceof ns2.DSA) ||
    -1707          (typeof ns2.ECDSA != "undefined" && keyObjOrHex instanceof ns2.ECDSA)) &&
    -1708         keyObjOrHex.isPublic == true &&
    -1709         (formatType === undefined || formatType == "PKCS8PUB")) {
    -1710         var asn1Obj = new KJUR.asn1.x509.SubjectPublicKeyInfo(keyObjOrHex);
    -1711         var asn1Hex = asn1Obj.getEncodedHex();
    -1712         return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PUBLIC KEY");
    -1713     }
    -1714     
    -1715     // 2. private
    -1716 
    -1717     // x. PEM PKCS#1 plain private key of RSA private key object
    -1718     if (formatType == "PKCS1PRV" &&
    -1719         typeof RSAKey != "undefined" &&
    -1720         keyObjOrHex instanceof RSAKey &&
    -1721         (passwd === undefined || passwd == null) &&
    -1722         keyObjOrHex.isPrivate  == true) {
    -1723 
    -1724         var asn1Obj = _rsaprv2asn1obj(keyObjOrHex);
    -1725         var asn1Hex = asn1Obj.getEncodedHex();
    -1726         return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "RSA PRIVATE KEY");
    -1727     }
    -1728 
    -1729     // x. PEM PKCS#1 plain private key of ECDSA private key object
    -1730     if (formatType == "PKCS1PRV" &&
    -1731         typeof RSAKey != "undefined" &&
    -1732         keyObjOrHex instanceof KJUR.crypto.ECDSA &&
    -1733         (passwd === undefined || passwd == null) &&
    -1734         keyObjOrHex.isPrivate  == true) {
    -1735 
    -1736         var asn1Obj1 = new KJUR.asn1.DERObjectIdentifier({'name': keyObjOrHex.curveName});
    -1737         var asn1Hex1 = asn1Obj1.getEncodedHex();
    -1738         var asn1Obj2 = _ecdsaprv2asn1obj(keyObjOrHex);
    -1739         var asn1Hex2 = asn1Obj2.getEncodedHex();
    -1740 
    -1741         var s = "";
    -1742         s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex1, "EC PARAMETERS");
    -1743         s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "EC PRIVATE KEY");
    -1744         return s;
    -1745     }
    -1746 
    -1747     // x. PEM PKCS#1 plain private key of DSA private key object
    -1748     if (formatType == "PKCS1PRV" &&
    -1749         typeof KJUR.crypto.DSA != "undefined" &&
    -1750         keyObjOrHex instanceof KJUR.crypto.DSA &&
    -1751         (passwd === undefined || passwd == null) &&
    -1752         keyObjOrHex.isPrivate  == true) {
    -1753 
    -1754         var asn1Obj = _dsaprv2asn1obj(keyObjOrHex);
    -1755         var asn1Hex = asn1Obj.getEncodedHex();
    -1756         return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "DSA PRIVATE KEY");
    -1757     }
    -1758 
    -1759     // 3. private
    +1704     // 1. public key
    +1705 
    +1706     // x. PEM PKCS#8 public key of RSA/ECDSA/DSA public key object
    +1707     if (((typeof RSAKey != "undefined" && keyObjOrHex instanceof RSAKey) ||
    +1708          (typeof ns2.DSA != "undefined" && keyObjOrHex instanceof ns2.DSA) ||
    +1709          (typeof ns2.ECDSA != "undefined" && keyObjOrHex instanceof ns2.ECDSA)) &&
    +1710         keyObjOrHex.isPublic == true &&
    +1711         (formatType === undefined || formatType == "PKCS8PUB")) {
    +1712         var asn1Obj = new KJUR.asn1.x509.SubjectPublicKeyInfo(keyObjOrHex);
    +1713         var asn1Hex = asn1Obj.getEncodedHex();
    +1714         return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PUBLIC KEY");
    +1715     }
    +1716     
    +1717     // 2. private
    +1718 
    +1719     // x. PEM PKCS#1 plain private key of RSA private key object
    +1720     if (formatType == "PKCS1PRV" &&
    +1721         typeof RSAKey != "undefined" &&
    +1722         keyObjOrHex instanceof RSAKey &&
    +1723         (passwd === undefined || passwd == null) &&
    +1724         keyObjOrHex.isPrivate  == true) {
    +1725 
    +1726         var asn1Obj = _rsaprv2asn1obj(keyObjOrHex);
    +1727         var asn1Hex = asn1Obj.getEncodedHex();
    +1728         return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "RSA PRIVATE KEY");
    +1729     }
    +1730 
    +1731     // x. PEM PKCS#1 plain private key of ECDSA private key object
    +1732     if (formatType == "PKCS1PRV" &&
    +1733         typeof RSAKey != "undefined" &&
    +1734         keyObjOrHex instanceof KJUR.crypto.ECDSA &&
    +1735         (passwd === undefined || passwd == null) &&
    +1736         keyObjOrHex.isPrivate  == true) {
    +1737 
    +1738         var asn1Obj1 = new KJUR.asn1.DERObjectIdentifier({'name': keyObjOrHex.curveName});
    +1739         var asn1Hex1 = asn1Obj1.getEncodedHex();
    +1740         var asn1Obj2 = _ecdsaprv2asn1obj(keyObjOrHex);
    +1741         var asn1Hex2 = asn1Obj2.getEncodedHex();
    +1742 
    +1743         var s = "";
    +1744         s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex1, "EC PARAMETERS");
    +1745         s += ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "EC PRIVATE KEY");
    +1746         return s;
    +1747     }
    +1748 
    +1749     // x. PEM PKCS#1 plain private key of DSA private key object
    +1750     if (formatType == "PKCS1PRV" &&
    +1751         typeof KJUR.crypto.DSA != "undefined" &&
    +1752         keyObjOrHex instanceof KJUR.crypto.DSA &&
    +1753         (passwd === undefined || passwd == null) &&
    +1754         keyObjOrHex.isPrivate  == true) {
    +1755 
    +1756         var asn1Obj = _dsaprv2asn1obj(keyObjOrHex);
    +1757         var asn1Hex = asn1Obj.getEncodedHex();
    +1758         return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "DSA PRIVATE KEY");
    +1759     }
     1760 
    -1761     // x. PEM PKCS#5 encrypted private key of RSA private key object
    -1762     if (formatType == "PKCS5PRV" &&
    -1763         typeof RSAKey != "undefined" &&
    -1764         keyObjOrHex instanceof RSAKey &&
    -1765         (passwd !== undefined && passwd != null) &&
    -1766         keyObjOrHex.isPrivate  == true) {
    -1767 
    -1768         var asn1Obj = _rsaprv2asn1obj(keyObjOrHex);
    -1769         var asn1Hex = asn1Obj.getEncodedHex();
    -1770 
    -1771         if (encAlg === undefined) encAlg = "DES-EDE3-CBC";
    -1772         return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", asn1Hex, passwd, encAlg);
    -1773     }
    -1774 
    -1775     // x. PEM PKCS#5 encrypted private key of ECDSA private key object
    -1776     if (formatType == "PKCS5PRV" &&
    -1777         typeof KJUR.crypto.ECDSA != "undefined" &&
    -1778         keyObjOrHex instanceof KJUR.crypto.ECDSA &&
    -1779         (passwd !== undefined && passwd != null) &&
    -1780         keyObjOrHex.isPrivate  == true) {
    -1781 
    -1782         var asn1Obj = _ecdsaprv2asn1obj(keyObjOrHex);
    -1783         var asn1Hex = asn1Obj.getEncodedHex();
    -1784 
    -1785         if (encAlg === undefined) encAlg = "DES-EDE3-CBC";
    -1786         return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC", asn1Hex, passwd, encAlg);
    -1787     }
    -1788 
    -1789     // x. PEM PKCS#5 encrypted private key of DSA private key object
    -1790     if (formatType == "PKCS5PRV" &&
    -1791         typeof KJUR.crypto.DSA != "undefined" &&
    -1792         keyObjOrHex instanceof KJUR.crypto.DSA &&
    -1793         (passwd !== undefined && passwd != null) &&
    -1794         keyObjOrHex.isPrivate  == true) {
    -1795 
    -1796         var asn1Obj = _dsaprv2asn1obj(keyObjOrHex);
    -1797         var asn1Hex = asn1Obj.getEncodedHex();
    -1798 
    -1799         if (encAlg === undefined) encAlg = "DES-EDE3-CBC";
    -1800         return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA", asn1Hex, passwd, encAlg);
    -1801     }
    -1802 
    -1803     // x. ======================================================================
    +1761     // 3. private
    +1762 
    +1763     // x. PEM PKCS#5 encrypted private key of RSA private key object
    +1764     if (formatType == "PKCS5PRV" &&
    +1765         typeof RSAKey != "undefined" &&
    +1766         keyObjOrHex instanceof RSAKey &&
    +1767         (passwd !== undefined && passwd != null) &&
    +1768         keyObjOrHex.isPrivate  == true) {
    +1769 
    +1770         var asn1Obj = _rsaprv2asn1obj(keyObjOrHex);
    +1771         var asn1Hex = asn1Obj.getEncodedHex();
    +1772 
    +1773         if (encAlg === undefined) encAlg = "DES-EDE3-CBC";
    +1774         return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA", asn1Hex, passwd, encAlg);
    +1775     }
    +1776 
    +1777     // x. PEM PKCS#5 encrypted private key of ECDSA private key object
    +1778     if (formatType == "PKCS5PRV" &&
    +1779         typeof KJUR.crypto.ECDSA != "undefined" &&
    +1780         keyObjOrHex instanceof KJUR.crypto.ECDSA &&
    +1781         (passwd !== undefined && passwd != null) &&
    +1782         keyObjOrHex.isPrivate  == true) {
    +1783 
    +1784         var asn1Obj = _ecdsaprv2asn1obj(keyObjOrHex);
    +1785         var asn1Hex = asn1Obj.getEncodedHex();
    +1786 
    +1787         if (encAlg === undefined) encAlg = "DES-EDE3-CBC";
    +1788         return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC", asn1Hex, passwd, encAlg);
    +1789     }
    +1790 
    +1791     // x. PEM PKCS#5 encrypted private key of DSA private key object
    +1792     if (formatType == "PKCS5PRV" &&
    +1793         typeof KJUR.crypto.DSA != "undefined" &&
    +1794         keyObjOrHex instanceof KJUR.crypto.DSA &&
    +1795         (passwd !== undefined && passwd != null) &&
    +1796         keyObjOrHex.isPrivate  == true) {
    +1797 
    +1798         var asn1Obj = _dsaprv2asn1obj(keyObjOrHex);
    +1799         var asn1Hex = asn1Obj.getEncodedHex();
    +1800 
    +1801         if (encAlg === undefined) encAlg = "DES-EDE3-CBC";
    +1802         return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA", asn1Hex, passwd, encAlg);
    +1803     }
     1804 
    -1805     var _getEncryptedPKCS8 = function(plainKeyHex, passcode) {
    -1806         var info = _getEencryptedPKCS8Info(plainKeyHex, passcode);
    -1807         //alert("iv=" + info.encryptionSchemeIV);
    -1808         //alert("info.ciphertext2[" + info.ciphertext.length + "=" + info.ciphertext);
    -1809         var asn1Obj = new KJUR.asn1.ASN1Util.newObject({
    -1810             "seq": [
    -1811                 {"seq": [
    -1812                     {"oid": {"name": "pkcs5PBES2"}},
    -1813                     {"seq": [
    -1814                         {"seq": [
    -1815                             {"oid": {"name": "pkcs5PBKDF2"}},
    -1816                             {"seq": [
    -1817                                 {"octstr": {"hex": info.pbkdf2Salt}},
    -1818                                 {"int": info.pbkdf2Iter}
    -1819                             ]}
    -1820                         ]},
    -1821                         {"seq": [
    -1822                             {"oid": {"name": "des-EDE3-CBC"}},
    -1823                             {"octstr": {"hex": info.encryptionSchemeIV}}
    -1824                         ]}
    -1825                     ]}
    -1826                 ]},
    -1827                 {"octstr": {"hex": info.ciphertext}}
    -1828             ]
    -1829         });
    -1830         return asn1Obj.getEncodedHex();
    -1831     };
    -1832 
    -1833     var _getEencryptedPKCS8Info = function(plainKeyHex, passcode) {
    -1834         var pbkdf2Iter = 100;
    -1835         var pbkdf2SaltWS = CryptoJS.lib.WordArray.random(8);
    -1836         var encryptionSchemeAlg = "DES-EDE3-CBC";
    -1837         var encryptionSchemeIVWS = CryptoJS.lib.WordArray.random(8);
    -1838         // PBKDF2 key
    -1839         var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, 
    -1840                                           pbkdf2SaltWS, { "keySize": 192/32,
    -1841                                                           "iterations": pbkdf2Iter });
    -1842         // ENCRYPT
    -1843         var plainKeyWS = CryptoJS.enc.Hex.parse(plainKeyHex);
    -1844         var encryptedKeyHex = 
    -1845             CryptoJS.TripleDES.encrypt(plainKeyWS, pbkdf2KeyWS, { "iv": encryptionSchemeIVWS }) + "";
    -1846 
    -1847         //alert("encryptedKeyHex=" + encryptedKeyHex);
    +1805     // x. ======================================================================
    +1806 
    +1807     var _getEncryptedPKCS8 = function(plainKeyHex, passcode) {
    +1808         var info = _getEencryptedPKCS8Info(plainKeyHex, passcode);
    +1809         //alert("iv=" + info.encryptionSchemeIV);
    +1810         //alert("info.ciphertext2[" + info.ciphertext.length + "=" + info.ciphertext);
    +1811         var asn1Obj = new KJUR.asn1.ASN1Util.newObject({
    +1812             "seq": [
    +1813                 {"seq": [
    +1814                     {"oid": {"name": "pkcs5PBES2"}},
    +1815                     {"seq": [
    +1816                         {"seq": [
    +1817                             {"oid": {"name": "pkcs5PBKDF2"}},
    +1818                             {"seq": [
    +1819                                 {"octstr": {"hex": info.pbkdf2Salt}},
    +1820                                 {"int": info.pbkdf2Iter}
    +1821                             ]}
    +1822                         ]},
    +1823                         {"seq": [
    +1824                             {"oid": {"name": "des-EDE3-CBC"}},
    +1825                             {"octstr": {"hex": info.encryptionSchemeIV}}
    +1826                         ]}
    +1827                     ]}
    +1828                 ]},
    +1829                 {"octstr": {"hex": info.ciphertext}}
    +1830             ]
    +1831         });
    +1832         return asn1Obj.getEncodedHex();
    +1833     };
    +1834 
    +1835     var _getEencryptedPKCS8Info = function(plainKeyHex, passcode) {
    +1836         var pbkdf2Iter = 100;
    +1837         var pbkdf2SaltWS = CryptoJS.lib.WordArray.random(8);
    +1838         var encryptionSchemeAlg = "DES-EDE3-CBC";
    +1839         var encryptionSchemeIVWS = CryptoJS.lib.WordArray.random(8);
    +1840         // PBKDF2 key
    +1841         var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, 
    +1842                                           pbkdf2SaltWS, { "keySize": 192/32,
    +1843                                                           "iterations": pbkdf2Iter });
    +1844         // ENCRYPT
    +1845         var plainKeyWS = CryptoJS.enc.Hex.parse(plainKeyHex);
    +1846         var encryptedKeyHex = 
    +1847             CryptoJS.TripleDES.encrypt(plainKeyWS, pbkdf2KeyWS, { "iv": encryptionSchemeIVWS }) + "";
     1848 
    -1849         var info = {};
    -1850         info.ciphertext = encryptedKeyHex;
    -1851         //alert("info.ciphertext=" + info.ciphertext);
    -1852         info.pbkdf2Salt = CryptoJS.enc.Hex.stringify(pbkdf2SaltWS);
    -1853         info.pbkdf2Iter = pbkdf2Iter;
    -1854         info.encryptionSchemeAlg = encryptionSchemeAlg;
    -1855         info.encryptionSchemeIV = CryptoJS.enc.Hex.stringify(encryptionSchemeIVWS);
    -1856         return info;
    -1857     };
    -1858 
    -1859     // x. PEM PKCS#8 plain private key of RSA private key object
    -1860     if (formatType == "PKCS8PRV" &&
    -1861         typeof RSAKey != "undefined" &&
    -1862         keyObjOrHex instanceof RSAKey &&
    -1863         keyObjOrHex.isPrivate  == true) {
    -1864 
    -1865         var keyObj = _rsaprv2asn1obj(keyObjOrHex);
    -1866         var keyHex = keyObj.getEncodedHex();
    -1867 
    -1868         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    -1869             "seq": [
    -1870                 {"int": 0},
    -1871                 {"seq": [{"oid": {"name": "rsaEncryption"}},{"null": true}]},
    -1872                 {"octstr": {"hex": keyHex}}
    -1873             ]
    -1874         });
    -1875         var asn1Hex = asn1Obj.getEncodedHex();
    -1876 
    -1877         if (passwd === undefined || passwd == null) {
    -1878             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY");
    -1879         } else {
    -1880             var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd);
    -1881             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY");
    -1882         }
    -1883     }
    -1884 
    -1885     // x. PEM PKCS#8 plain private key of ECDSA private key object
    -1886     if (formatType == "PKCS8PRV" &&
    -1887         typeof KJUR.crypto.ECDSA != "undefined" &&
    -1888         keyObjOrHex instanceof KJUR.crypto.ECDSA &&
    -1889         keyObjOrHex.isPrivate  == true) {
    -1890 
    -1891         var keyObj = new KJUR.asn1.ASN1Util.newObject({
    -1892             "seq": [
    -1893                 {"int": 1},
    -1894                 {"octstr": {"hex": keyObjOrHex.prvKeyHex}},
    -1895                 {"tag": ['a1', true, {"bitstr": {"hex": "00" + keyObjOrHex.pubKeyHex}}]}
    -1896             ]
    -1897         });
    -1898         var keyHex = keyObj.getEncodedHex();
    -1899 
    -1900         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    -1901             "seq": [
    -1902                 {"int": 0},
    -1903                 {"seq": [
    -1904                     {"oid": {"name": "ecPublicKey"}},
    -1905                     {"oid": {"name": keyObjOrHex.curveName}}
    -1906                 ]},
    -1907                 {"octstr": {"hex": keyHex}}
    -1908             ]
    -1909         });
    -1910 
    -1911         var asn1Hex = asn1Obj.getEncodedHex();
    -1912         if (passwd === undefined || passwd == null) {
    -1913             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY");
    -1914         } else {
    -1915             var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd);
    -1916             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY");
    -1917         }
    -1918     }
    -1919 
    -1920     // x. PEM PKCS#8 plain private key of DSA private key object
    -1921     if (formatType == "PKCS8PRV" &&
    -1922         typeof KJUR.crypto.DSA != "undefined" &&
    -1923         keyObjOrHex instanceof KJUR.crypto.DSA &&
    -1924         keyObjOrHex.isPrivate  == true) {
    -1925 
    -1926         var keyObj = new KJUR.asn1.DERInteger({'bigint': keyObjOrHex.x});
    -1927         var keyHex = keyObj.getEncodedHex();
    -1928 
    -1929         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    -1930             "seq": [
    -1931                 {"int": 0},
    -1932                 {"seq": [
    -1933                     {"oid": {"name": "dsa"}},
    -1934                     {"seq": [
    -1935                         {"int": {"bigint": keyObjOrHex.p}},
    -1936                         {"int": {"bigint": keyObjOrHex.q}},
    -1937                         {"int": {"bigint": keyObjOrHex.g}}
    -1938                     ]}
    -1939                 ]},
    -1940                 {"octstr": {"hex": keyHex}}
    -1941             ]
    -1942         });
    -1943 
    -1944         var asn1Hex = asn1Obj.getEncodedHex();
    -1945         if (passwd === undefined || passwd == null) {
    -1946             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY");
    -1947         } else {
    -1948             var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd);
    -1949             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY");
    -1950         }
    -1951     }
    -1952 
    -1953     throw "unsupported object nor format";
    -1954 };
    -1955 
    -1956 // -- PUBLIC METHODS FOR CSR -------------------------------------------------------
    +1849         //alert("encryptedKeyHex=" + encryptedKeyHex);
    +1850 
    +1851         var info = {};
    +1852         info.ciphertext = encryptedKeyHex;
    +1853         //alert("info.ciphertext=" + info.ciphertext);
    +1854         info.pbkdf2Salt = CryptoJS.enc.Hex.stringify(pbkdf2SaltWS);
    +1855         info.pbkdf2Iter = pbkdf2Iter;
    +1856         info.encryptionSchemeAlg = encryptionSchemeAlg;
    +1857         info.encryptionSchemeIV = CryptoJS.enc.Hex.stringify(encryptionSchemeIVWS);
    +1858         return info;
    +1859     };
    +1860 
    +1861     // x. PEM PKCS#8 plain private key of RSA private key object
    +1862     if (formatType == "PKCS8PRV" &&
    +1863         typeof RSAKey != "undefined" &&
    +1864         keyObjOrHex instanceof RSAKey &&
    +1865         keyObjOrHex.isPrivate  == true) {
    +1866 
    +1867         var keyObj = _rsaprv2asn1obj(keyObjOrHex);
    +1868         var keyHex = keyObj.getEncodedHex();
    +1869 
    +1870         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    +1871             "seq": [
    +1872                 {"int": 0},
    +1873                 {"seq": [{"oid": {"name": "rsaEncryption"}},{"null": true}]},
    +1874                 {"octstr": {"hex": keyHex}}
    +1875             ]
    +1876         });
    +1877         var asn1Hex = asn1Obj.getEncodedHex();
    +1878 
    +1879         if (passwd === undefined || passwd == null) {
    +1880             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY");
    +1881         } else {
    +1882             var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd);
    +1883             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY");
    +1884         }
    +1885     }
    +1886 
    +1887     // x. PEM PKCS#8 plain private key of ECDSA private key object
    +1888     if (formatType == "PKCS8PRV" &&
    +1889         typeof KJUR.crypto.ECDSA != "undefined" &&
    +1890         keyObjOrHex instanceof KJUR.crypto.ECDSA &&
    +1891         keyObjOrHex.isPrivate  == true) {
    +1892 
    +1893         var keyObj = new KJUR.asn1.ASN1Util.newObject({
    +1894             "seq": [
    +1895                 {"int": 1},
    +1896                 {"octstr": {"hex": keyObjOrHex.prvKeyHex}},
    +1897                 {"tag": ['a1', true, {"bitstr": {"hex": "00" + keyObjOrHex.pubKeyHex}}]}
    +1898             ]
    +1899         });
    +1900         var keyHex = keyObj.getEncodedHex();
    +1901 
    +1902         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    +1903             "seq": [
    +1904                 {"int": 0},
    +1905                 {"seq": [
    +1906                     {"oid": {"name": "ecPublicKey"}},
    +1907                     {"oid": {"name": keyObjOrHex.curveName}}
    +1908                 ]},
    +1909                 {"octstr": {"hex": keyHex}}
    +1910             ]
    +1911         });
    +1912 
    +1913         var asn1Hex = asn1Obj.getEncodedHex();
    +1914         if (passwd === undefined || passwd == null) {
    +1915             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY");
    +1916         } else {
    +1917             var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd);
    +1918             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY");
    +1919         }
    +1920     }
    +1921 
    +1922     // x. PEM PKCS#8 plain private key of DSA private key object
    +1923     if (formatType == "PKCS8PRV" &&
    +1924         typeof KJUR.crypto.DSA != "undefined" &&
    +1925         keyObjOrHex instanceof KJUR.crypto.DSA &&
    +1926         keyObjOrHex.isPrivate  == true) {
    +1927 
    +1928         var keyObj = new KJUR.asn1.DERInteger({'bigint': keyObjOrHex.x});
    +1929         var keyHex = keyObj.getEncodedHex();
    +1930 
    +1931         var asn1Obj = KJUR.asn1.ASN1Util.newObject({
    +1932             "seq": [
    +1933                 {"int": 0},
    +1934                 {"seq": [
    +1935                     {"oid": {"name": "dsa"}},
    +1936                     {"seq": [
    +1937                         {"int": {"bigint": keyObjOrHex.p}},
    +1938                         {"int": {"bigint": keyObjOrHex.q}},
    +1939                         {"int": {"bigint": keyObjOrHex.g}}
    +1940                     ]}
    +1941                 ]},
    +1942                 {"octstr": {"hex": keyHex}}
    +1943             ]
    +1944         });
    +1945 
    +1946         var asn1Hex = asn1Obj.getEncodedHex();
    +1947         if (passwd === undefined || passwd == null) {
    +1948             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex, "PRIVATE KEY");
    +1949         } else {
    +1950             var asn1Hex2 = _getEncryptedPKCS8(asn1Hex, passwd);
    +1951             return ns1.ASN1Util.getPEMStringFromHex(asn1Hex2, "ENCRYPTED PRIVATE KEY");
    +1952         }
    +1953     }
    +1954 
    +1955     throw "unsupported object nor format";
    +1956 };
     1957 
    -1958 /**
    -1959  * get RSAKey/DSA/ECDSA public key object from PEM formatted PKCS#10 CSR string
    -1960  * @name getKeyFromCSRPEM
    -1961  * @memberOf KEYUTIL
    -1962  * @function
    -1963  * @param {String} csrPEM PEM formatted PKCS#10 CSR string
    -1964  * @return {Object} RSAKey/DSA/ECDSA public key object
    -1965  * @since keyutil 1.0.5
    -1966  */
    -1967 KEYUTIL.getKeyFromCSRPEM = function(csrPEM) {
    -1968     var csrHex = KEYUTIL.getHexFromPEM(csrPEM, "CERTIFICATE REQUEST");
    -1969     var key = KEYUTIL.getKeyFromCSRHex(csrHex);
    -1970     return key;
    -1971 };
    -1972 
    -1973 /**
    -1974  * get RSAKey/DSA/ECDSA public key object from hexadecimal string of PKCS#10 CSR
    -1975  * @name getKeyFromCSRHex
    -1976  * @memberOf KEYUTIL
    -1977  * @function
    -1978  * @param {String} csrHex hexadecimal string of PKCS#10 CSR
    -1979  * @return {Object} RSAKey/DSA/ECDSA public key object
    -1980  * @since keyutil 1.0.5
    -1981  */
    -1982 KEYUTIL.getKeyFromCSRHex = function(csrHex) {
    -1983     var info = KEYUTIL.parseCSRHex(csrHex);
    -1984     var key = KEYUTIL.getKey(info.p8pubkeyhex, null, "pkcs8pub");
    -1985     return key;
    -1986 };
    -1987 
    -1988 /**
    -1989  * parse hexadecimal string of PKCS#10 CSR (certificate signing request)
    -1990  * @name parseCSRHex
    -1991  * @memberOf KEYUTIL
    -1992  * @function
    -1993  * @param {String} csrHex hexadecimal string of PKCS#10 CSR
    -1994  * @return {Array} associative array of parsed CSR
    -1995  * @since keyutil 1.0.5
    -1996  * @description
    -1997  * Resulted associative array has following properties:
    -1998  * <ul>
    -1999  * <li>p8pubkeyhex - hexadecimal string of subject public key in PKCS#8</li>
    -2000  * </ul>
    -2001  */
    -2002 KEYUTIL.parseCSRHex = function(csrHex) {
    -2003     var result = {};
    -2004     var h = csrHex;
    -2005 
    -2006     // 1. sequence
    -2007     if (h.substr(0, 2) != "30")
    -2008         throw "malformed CSR(code:001)"; // not sequence
    -2009 
    -2010     var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(h, 0);
    -2011     if (a1.length < 1)
    -2012         throw "malformed CSR(code:002)"; // short length
    -2013 
    -2014     // 2. 2nd sequence
    -2015     if (h.substr(a1[0], 2) != "30")
    -2016         throw "malformed CSR(code:003)"; // not sequence
    -2017 
    -2018     var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(h, a1[0]);
    -2019     if (a2.length < 3)
    -2020         throw "malformed CSR(code:004)"; // 2nd seq short elem
    -2021 
    -2022     result.p8pubkeyhex = ASN1HEX.getHexOfTLV_AtObj(h, a2[2]);
    +1958 // -- PUBLIC METHODS FOR CSR -------------------------------------------------------
    +1959 
    +1960 /**
    +1961  * get RSAKey/DSA/ECDSA public key object from PEM formatted PKCS#10 CSR string
    +1962  * @name getKeyFromCSRPEM
    +1963  * @memberOf KEYUTIL
    +1964  * @function
    +1965  * @param {String} csrPEM PEM formatted PKCS#10 CSR string
    +1966  * @return {Object} RSAKey/DSA/ECDSA public key object
    +1967  * @since keyutil 1.0.5
    +1968  */
    +1969 KEYUTIL.getKeyFromCSRPEM = function(csrPEM) {
    +1970     var csrHex = KEYUTIL.getHexFromPEM(csrPEM, "CERTIFICATE REQUEST");
    +1971     var key = KEYUTIL.getKeyFromCSRHex(csrHex);
    +1972     return key;
    +1973 };
    +1974 
    +1975 /**
    +1976  * get RSAKey/DSA/ECDSA public key object from hexadecimal string of PKCS#10 CSR
    +1977  * @name getKeyFromCSRHex
    +1978  * @memberOf KEYUTIL
    +1979  * @function
    +1980  * @param {String} csrHex hexadecimal string of PKCS#10 CSR
    +1981  * @return {Object} RSAKey/DSA/ECDSA public key object
    +1982  * @since keyutil 1.0.5
    +1983  */
    +1984 KEYUTIL.getKeyFromCSRHex = function(csrHex) {
    +1985     var info = KEYUTIL.parseCSRHex(csrHex);
    +1986     var key = KEYUTIL.getKey(info.p8pubkeyhex, null, "pkcs8pub");
    +1987     return key;
    +1988 };
    +1989 
    +1990 /**
    +1991  * parse hexadecimal string of PKCS#10 CSR (certificate signing request)
    +1992  * @name parseCSRHex
    +1993  * @memberOf KEYUTIL
    +1994  * @function
    +1995  * @param {String} csrHex hexadecimal string of PKCS#10 CSR
    +1996  * @return {Array} associative array of parsed CSR
    +1997  * @since keyutil 1.0.5
    +1998  * @description
    +1999  * Resulted associative array has following properties:
    +2000  * <ul>
    +2001  * <li>p8pubkeyhex - hexadecimal string of subject public key in PKCS#8</li>
    +2002  * </ul>
    +2003  */
    +2004 KEYUTIL.parseCSRHex = function(csrHex) {
    +2005     var result = {};
    +2006     var h = csrHex;
    +2007 
    +2008     // 1. sequence
    +2009     if (h.substr(0, 2) != "30")
    +2010         throw "malformed CSR(code:001)"; // not sequence
    +2011 
    +2012     var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(h, 0);
    +2013     if (a1.length < 1)
    +2014         throw "malformed CSR(code:002)"; // short length
    +2015 
    +2016     // 2. 2nd sequence
    +2017     if (h.substr(a1[0], 2) != "30")
    +2018         throw "malformed CSR(code:003)"; // not sequence
    +2019 
    +2020     var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(h, a1[0]);
    +2021     if (a2.length < 3)
    +2022         throw "malformed CSR(code:004)"; // 2nd seq short elem
     2023 
    -2024     return result;
    -2025 };
    -2026 
    -2027 // -- OTHER STATIC PUBLIC METHODS  -------------------------------------------------
    +2024     result.p8pubkeyhex = ASN1HEX.getHexOfTLV_AtObj(h, a2[2]);
    +2025 
    +2026     return result;
    +2027 };
     2028 
    -2029 /**
    -2030  * convert from RSAKey/KJUR.crypto.ECDSA public/private key object to RFC 7517 JSON Web Key(JWK)
    -2031  * @name getJWKFromKey
    -2032  * @memberOf KEYUTIL
    -2033  * @function
    -2034  * @static
    -2035  * @param {Object} RSAKey/KJUR.crypto.ECDSA public/private key object
    -2036  * @return {Object} JWK object
    -2037  * @since keyutil 1.0.13 jsrsasign 5.0.14
    -2038  * @description
    -2039  * This static method convert from RSAKey/KJUR.crypto.ECDSA public/private key object 
    -2040  * to RFC 7517 JSON Web Key(JWK)
    -2041  * @example
    -2042  * kp1 = KEYUTIL.generateKeypair("EC", "P-256");
    -2043  * jwkPrv1 = KEYUTIL.getJWKFromKey(kp1.prvKeyObj);
    -2044  * jwkPub1 = KEYUTIL.getJWKFromKey(kp1.pubKeyObj);
    -2045  *
    -2046  * kp2 = KEYUTIL.generateKeypair("RSA", 2048);
    -2047  * jwkPrv2 = KEYUTIL.getJWKFromKey(kp2.prvKeyObj);
    -2048  * jwkPub2 = KEYUTIL.getJWKFromKey(kp2.pubKeyObj);
    -2049  *
    -2050  * // if you need RFC 7636 JWK thumprint as kid do like this:
    -2051  * jwkPub2.kid = KJUR.jws.JWS.getJWKthumbprint(jwkPub2);
    -2052  */
    -2053 KEYUTIL.getJWKFromKey = function(keyObj) {
    -2054     var jwk = {};
    -2055     if (keyObj instanceof RSAKey && keyObj.isPrivate) {
    -2056 	jwk.kty = "RSA";
    -2057 	jwk.n = hextob64u(keyObj.n.toString(16));
    -2058 	jwk.e = hextob64u(keyObj.e.toString(16));
    -2059 	jwk.d = hextob64u(keyObj.d.toString(16));
    -2060 	jwk.p = hextob64u(keyObj.p.toString(16));
    -2061 	jwk.q = hextob64u(keyObj.q.toString(16));
    -2062 	jwk.dp = hextob64u(keyObj.dmp1.toString(16));
    -2063 	jwk.dq = hextob64u(keyObj.dmq1.toString(16));
    -2064 	jwk.qi = hextob64u(keyObj.coeff.toString(16));
    -2065 	return jwk;
    -2066     } else if (keyObj instanceof RSAKey && keyObj.isPublic) {
    -2067 	jwk.kty = "RSA";
    -2068 	jwk.n = hextob64u(keyObj.n.toString(16));
    -2069 	jwk.e = hextob64u(keyObj.e.toString(16));
    -2070 	return jwk;
    -2071     } else if (keyObj instanceof KJUR.crypto.ECDSA && keyObj.isPrivate) {
    -2072 	var name = keyObj.getShortNISTPCurveName();
    -2073 	if (name !== "P-256" && name !== "P-384")
    -2074 	    throw "unsupported curve name for JWT: " + name;
    -2075 	var xy = keyObj.getPublicKeyXYHex();
    -2076 	jwk.kty = "EC";
    -2077 	jwk.crv =  name;
    -2078 	jwk.x = hextob64u(xy.x);
    -2079 	jwk.y = hextob64u(xy.y);
    -2080 	jwk.d = hextob64u(keyObj.prvKeyHex);
    -2081 	return jwk;
    -2082     } else if (keyObj instanceof KJUR.crypto.ECDSA && keyObj.isPublic) {
    -2083 	var name = keyObj.getShortNISTPCurveName();
    -2084 	if (name !== "P-256" && name !== "P-384")
    -2085 	    throw "unsupported curve name for JWT: " + name;
    -2086 	var xy = keyObj.getPublicKeyXYHex();
    -2087 	jwk.kty = "EC";
    -2088 	jwk.crv =  name;
    -2089 	jwk.x = hextob64u(xy.x);
    -2090 	jwk.y = hextob64u(xy.y);
    -2091 	return jwk;
    -2092     }
    -2093     throw "not supported key object";
    -2094 };
    -2095 
    -2096 
    -2097 
    \ No newline at end of file +2029
    // -- OTHER STATIC PUBLIC METHODS ------------------------------------------------- +2030 +2031 /** +2032 * convert from RSAKey/KJUR.crypto.ECDSA public/private key object to RFC 7517 JSON Web Key(JWK) +2033 * @name getJWKFromKey +2034 * @memberOf KEYUTIL +2035 * @function +2036 * @static +2037 * @param {Object} RSAKey/KJUR.crypto.ECDSA public/private key object +2038 * @return {Object} JWK object +2039 * @since keyutil 1.0.13 jsrsasign 5.0.14 +2040 * @description +2041 * This static method convert from RSAKey/KJUR.crypto.ECDSA public/private key object +2042 * to RFC 7517 JSON Web Key(JWK) +2043 * @example +2044 * kp1 = KEYUTIL.generateKeypair("EC", "P-256"); +2045 * jwkPrv1 = KEYUTIL.getJWKFromKey(kp1.prvKeyObj); +2046 * jwkPub1 = KEYUTIL.getJWKFromKey(kp1.pubKeyObj); +2047 * +2048 * kp2 = KEYUTIL.generateKeypair("RSA", 2048); +2049 * jwkPrv2 = KEYUTIL.getJWKFromKey(kp2.prvKeyObj); +2050 * jwkPub2 = KEYUTIL.getJWKFromKey(kp2.pubKeyObj); +2051 * +2052 * // if you need RFC 7636 JWK thumprint as kid do like this: +2053 * jwkPub2.kid = KJUR.jws.JWS.getJWKthumbprint(jwkPub2); +2054 */ +2055 KEYUTIL.getJWKFromKey = function(keyObj) { +2056 var jwk = {}; +2057 if (keyObj instanceof RSAKey && keyObj.isPrivate) { +2058 jwk.kty = "RSA"; +2059 jwk.n = hextob64u(keyObj.n.toString(16)); +2060 jwk.e = hextob64u(keyObj.e.toString(16)); +2061 jwk.d = hextob64u(keyObj.d.toString(16)); +2062 jwk.p = hextob64u(keyObj.p.toString(16)); +2063 jwk.q = hextob64u(keyObj.q.toString(16)); +2064 jwk.dp = hextob64u(keyObj.dmp1.toString(16)); +2065 jwk.dq = hextob64u(keyObj.dmq1.toString(16)); +2066 jwk.qi = hextob64u(keyObj.coeff.toString(16)); +2067 return jwk; +2068 } else if (keyObj instanceof RSAKey && keyObj.isPublic) { +2069 jwk.kty = "RSA"; +2070 jwk.n = hextob64u(keyObj.n.toString(16)); +2071 jwk.e = hextob64u(keyObj.e.toString(16)); +2072 return jwk; +2073 } else if (keyObj instanceof KJUR.crypto.ECDSA && keyObj.isPrivate) { +2074 var name = keyObj.getShortNISTPCurveName(); +2075 if (name !== "P-256" && name !== "P-384") +2076 throw "unsupported curve name for JWT: " + name; +2077 var xy = keyObj.getPublicKeyXYHex(); +2078 jwk.kty = "EC"; +2079 jwk.crv = name; +2080 jwk.x = hextob64u(xy.x); +2081 jwk.y = hextob64u(xy.y); +2082 jwk.d = hextob64u(keyObj.prvKeyHex); +2083 return jwk; +2084 } else if (keyObj instanceof KJUR.crypto.ECDSA && keyObj.isPublic) { +2085 var name = keyObj.getShortNISTPCurveName(); +2086 if (name !== "P-256" && name !== "P-384") +2087 throw "unsupported curve name for JWT: " + name; +2088 var xy = keyObj.getPublicKeyXYHex(); +2089 jwk.kty = "EC"; +2090 jwk.crv = name; +2091 jwk.x = hextob64u(xy.x); +2092 jwk.y = hextob64u(xy.y); +2093 return jwk; +2094 } +2095 throw "not supported key object"; +2096 }; +2097 +2098 +2099
    \ No newline at end of file diff --git a/api/symbols/src/pkcs5pkey-1.0.js.html b/api/symbols/src/pkcs5pkey-1.0.js.html index 041a86c0..583acaa9 100644 --- a/api/symbols/src/pkcs5pkey-1.0.js.html +++ b/api/symbols/src/pkcs5pkey-1.0.js.html @@ -5,12 +5,12 @@ .STRN {color: #393;} .REGX {color: #339;} .line {border-right: 1px dotted #666; color: #666; font-style: normal;} -
      1 /*! pkcs5pkey-1.0.6.js (c) 2013-2014 Kenji Urushima | kjur.github.com/jsrsasign/license
    +	
      1 /*! pkcs5pkey-1.0.7.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
       2  */
       3 /*
       4  * pkcs5pkey.js - reading passcode protected PKCS#5 PEM formatted RSA private key
       5  *
    -  6  * Copyright (c) 2013-2014 Kenji Urushima (kenji.urushima@gmail.com)
    +  6  * Copyright (c) 2013-2016 Kenji Urushima (kenji.urushima@gmail.com)
       7  *
       8  * This software is licensed under the terms of the MIT License.
       9  * http://kjur.github.com/jsrsasign/license
    @@ -22,7 +22,7 @@
      15  * @fileOverview
      16  * @name pkcs5pkey-1.0.js
      17  * @author Kenji Urushima kenji.urushima@gmail.com
    - 18  * @version pkcs5pkey 1.0.6 (2014-Apr-16)
    + 18  * @version pkcs5pkey 1.0.7 (2016-Oct-08)
      19  * @since jsrsasign 2.0.0
      20  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
      21  */
    @@ -154,989 +154,991 @@
     147 
     148     var _parsePKCS5PEM = function(sPKCS5PEM) {
     149         var info = {};
    -150         if (sPKCS5PEM.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)", "m"))) {
    -151             info.cipher = RegExp.$1;
    -152             info.ivsalt = RegExp.$2;
    -153         }
    -154         if (sPKCS5PEM.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"))) {
    -155             info.type = RegExp.$1;
    -156         }
    -157         var i1 = -1;
    -158         var lenNEWLINE = 0;
    -159         if (sPKCS5PEM.indexOf("\r\n\r\n") != -1) {
    -160             i1 = sPKCS5PEM.indexOf("\r\n\r\n");
    -161             lenNEWLINE = 2;
    -162         }
    -163         if (sPKCS5PEM.indexOf("\n\n") != -1) {
    -164             i1 = sPKCS5PEM.indexOf("\n\n");
    -165             lenNEWLINE = 1;
    -166         }
    -167         var i2 = sPKCS5PEM.indexOf("-----END");
    -168         if (i1 != -1 && i2 != -1) {
    -169             var s = sPKCS5PEM.substring(i1 + lenNEWLINE * 2, i2 - lenNEWLINE);
    -170             s = s.replace(/\s+/g, '');
    -171             info.data = s;
    -172         }
    -173         return info;
    -174     };
    -175 
    -176     var _getKeyAndUnusedIvByPasscodeAndIvsalt = function(algName, passcode, ivsaltHex) {
    -177         //alert("ivsaltHex(2) = " + ivsaltHex);
    -178         var saltHex = ivsaltHex.substring(0, 16);
    -179         //alert("salt = " + saltHex);
    -180         
    -181         var salt = CryptoJS.enc.Hex.parse(saltHex);
    -182         var data = CryptoJS.enc.Utf8.parse(passcode);
    -183         //alert("salt = " + salt);
    -184         //alert("data = " + data);
    -185 
    -186         var nRequiredBytes = ALGLIST[algName]['keylen'] + ALGLIST[algName]['ivlen'];
    -187         var hHexValueJoined = '';
    -188         var hLastValue = null;
    -189         //alert("nRequiredBytes = " + nRequiredBytes);
    -190         for (;;) {
    -191             var h = CryptoJS.algo.MD5.create();
    -192             if (hLastValue != null) {
    -193                 h.update(hLastValue);
    -194             }
    -195             h.update(data);
    -196             h.update(salt);
    -197             hLastValue = h.finalize();
    -198             hHexValueJoined = hHexValueJoined + CryptoJS.enc.Hex.stringify(hLastValue);
    -199             //alert("joined = " + hHexValueJoined);
    -200             if (hHexValueJoined.length >= nRequiredBytes * 2) {
    -201                 break;
    -202             }
    -203         }
    -204         var result = {};
    -205         result.keyhex = hHexValueJoined.substr(0, ALGLIST[algName]['keylen'] * 2);
    -206         result.ivhex = hHexValueJoined.substr(ALGLIST[algName]['keylen'] * 2, ALGLIST[algName]['ivlen'] * 2);
    -207         return result;
    -208     };
    -209 
    -210     /*
    -211      * @param {String} privateKeyB64 base64 string of encrypted private key
    -212      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    -213      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    -214      * @param {String} ivsaltHex hexadecimal string of IV and salt
    -215      * @param {String} hexadecimal string of decrypted private key
    -216      */
    -217     var _decryptKeyB64 = function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    -218         var privateKeyWA = CryptoJS.enc.Base64.parse(privateKeyB64);
    -219         var privateKeyHex = CryptoJS.enc.Hex.stringify(privateKeyWA);
    -220         var f = ALGLIST[sharedKeyAlgName]['proc'];
    -221         var decryptedKeyHex = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    -222         return decryptedKeyHex;
    -223     };
    -224     
    -225     /*
    -226      * @param {String} privateKeyHex hexadecimal string of private key
    -227      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    -228      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    -229      * @param {String} ivsaltHex hexadecimal string of IV and salt
    -230      * @param {String} base64 string of encrypted private key
    -231      */
    -232     var _encryptKeyHex = function(privateKeyHex, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    -233         var f = ALGLIST[sharedKeyAlgName]['eproc'];
    -234         var encryptedKeyB64 = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    -235         return encryptedKeyB64;
    -236     };
    -237 
    -238     // *****************************************************************
    -239     // *** PUBLIC PROPERTIES AND METHODS *******************************
    +150         var matchResult1 = sPKCS5PEM.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)", "m"));
    +151         if (matchResult1) {
    +152             info.cipher = matchResult1[1];
    +153             info.ivsalt = matchResult1[2];
    +154         }
    +155         var matchResult2 = sPKCS5PEM.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));
    +156         if (matchResult2) {
    +157             info.type = matchResult2[1];
    +158         }
    +159         var i1 = -1;
    +160         var lenNEWLINE = 0;
    +161         if (sPKCS5PEM.indexOf("\r\n\r\n") != -1) {
    +162             i1 = sPKCS5PEM.indexOf("\r\n\r\n");
    +163             lenNEWLINE = 2;
    +164         }
    +165         if (sPKCS5PEM.indexOf("\n\n") != -1) {
    +166             i1 = sPKCS5PEM.indexOf("\n\n");
    +167             lenNEWLINE = 1;
    +168         }
    +169         var i2 = sPKCS5PEM.indexOf("-----END");
    +170         if (i1 != -1 && i2 != -1) {
    +171             var s = sPKCS5PEM.substring(i1 + lenNEWLINE * 2, i2 - lenNEWLINE);
    +172             s = s.replace(/\s+/g, '');
    +173             info.data = s;
    +174         }
    +175         return info;
    +176     };
    +177 
    +178     var _getKeyAndUnusedIvByPasscodeAndIvsalt = function(algName, passcode, ivsaltHex) {
    +179         //alert("ivsaltHex(2) = " + ivsaltHex);
    +180         var saltHex = ivsaltHex.substring(0, 16);
    +181         //alert("salt = " + saltHex);
    +182         
    +183         var salt = CryptoJS.enc.Hex.parse(saltHex);
    +184         var data = CryptoJS.enc.Utf8.parse(passcode);
    +185         //alert("salt = " + salt);
    +186         //alert("data = " + data);
    +187 
    +188         var nRequiredBytes = ALGLIST[algName]['keylen'] + ALGLIST[algName]['ivlen'];
    +189         var hHexValueJoined = '';
    +190         var hLastValue = null;
    +191         //alert("nRequiredBytes = " + nRequiredBytes);
    +192         for (;;) {
    +193             var h = CryptoJS.algo.MD5.create();
    +194             if (hLastValue != null) {
    +195                 h.update(hLastValue);
    +196             }
    +197             h.update(data);
    +198             h.update(salt);
    +199             hLastValue = h.finalize();
    +200             hHexValueJoined = hHexValueJoined + CryptoJS.enc.Hex.stringify(hLastValue);
    +201             //alert("joined = " + hHexValueJoined);
    +202             if (hHexValueJoined.length >= nRequiredBytes * 2) {
    +203                 break;
    +204             }
    +205         }
    +206         var result = {};
    +207         result.keyhex = hHexValueJoined.substr(0, ALGLIST[algName]['keylen'] * 2);
    +208         result.ivhex = hHexValueJoined.substr(ALGLIST[algName]['keylen'] * 2, ALGLIST[algName]['ivlen'] * 2);
    +209         return result;
    +210     };
    +211 
    +212     /*
    +213      * @param {String} privateKeyB64 base64 string of encrypted private key
    +214      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    +215      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    +216      * @param {String} ivsaltHex hexadecimal string of IV and salt
    +217      * @param {String} hexadecimal string of decrypted private key
    +218      */
    +219     var _decryptKeyB64 = function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    +220         var privateKeyWA = CryptoJS.enc.Base64.parse(privateKeyB64);
    +221         var privateKeyHex = CryptoJS.enc.Hex.stringify(privateKeyWA);
    +222         var f = ALGLIST[sharedKeyAlgName]['proc'];
    +223         var decryptedKeyHex = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    +224         return decryptedKeyHex;
    +225     };
    +226     
    +227     /*
    +228      * @param {String} privateKeyHex hexadecimal string of private key
    +229      * @param {String} sharedKeyAlgName algorithm name of shared key encryption
    +230      * @param {String} sharedKeyHex hexadecimal string of shared key to encrypt
    +231      * @param {String} ivsaltHex hexadecimal string of IV and salt
    +232      * @param {String} base64 string of encrypted private key
    +233      */
    +234     var _encryptKeyHex = function(privateKeyHex, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    +235         var f = ALGLIST[sharedKeyAlgName]['eproc'];
    +236         var encryptedKeyB64 = f(privateKeyHex, sharedKeyHex, ivsaltHex);
    +237         return encryptedKeyB64;
    +238     };
    +239 
     240     // *****************************************************************
    -241     return {
    -242         // -- UTILITY METHODS ------------------------------------------
    -243         /**
    -244          * decrypt private key by shared key
    -245          * @name version
    -246          * @memberOf PKCS5PKEY
    -247          * @property {String} version
    -248          * @description version string of PKCS5PKEY class
    -249          */
    -250         version: "1.0.5",
    -251 
    -252         /**
    -253          * get hexacedimal string of PEM format
    -254          * @name getHexFromPEM
    -255          * @memberOf PKCS5PKEY
    -256          * @function
    -257          * @param {String} sPEM PEM formatted string
    -258          * @param {String} sHead PEM header string without BEGIN/END
    -259          * @return {String} hexadecimal string data of PEM contents
    -260          * @since pkcs5pkey 1.0.5
    -261          */
    -262         getHexFromPEM: function(sPEM, sHead) {
    -263             var s = sPEM;
    -264             if (s.indexOf("BEGIN " + sHead) == -1) {
    -265                 throw "can't find PEM header: " + sHead;
    -266             }
    -267             s = s.replace("-----BEGIN " + sHead + "-----", "");
    -268             s = s.replace("-----END " + sHead + "-----", "");
    -269             var sB64 = s.replace(/\s+/g, '');
    -270             var dataHex = b64tohex(sB64);
    -271             return dataHex;
    -272         },
    -273 
    -274         /**
    -275          * decrypt private key by shared key
    -276          * @name getDecryptedKeyHexByKeyIV
    -277          * @memberOf PKCS5PKEY
    -278          * @function
    -279          * @param {String} encryptedKeyHex hexadecimal string of encrypted private key
    -280          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    -281          * @param {String} sharedKeyHex hexadecimal string of symmetric key
    -282          * @param {String} ivHex hexadecimal string of initial vector(IV).
    -283          * @return {String} hexadecimal string of decrypted privated key
    -284          */
    -285         getDecryptedKeyHexByKeyIV: function(encryptedKeyHex, algName, sharedKeyHex, ivHex) {
    -286             var f1 = getFuncByName(algName);
    -287             return f1(encryptedKeyHex, sharedKeyHex, ivHex);
    -288         },
    -289 
    -290         /**
    -291          * parse PEM formatted passcode protected PKCS#5 private key
    -292          * @name parsePKCS5PEM
    -293          * @memberOf PKCS5PKEY
    -294          * @function
    -295          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    -296          * @return {Hash} hash of key information
    -297          * @description
    -298          * Resulted hash has following attributes.
    -299          * <ul>
    -300          * <li>cipher - symmetric key algorithm name (ex. 'DES-EBE3-CBC', 'AES-256-CBC')</li>
    -301          * <li>ivsalt - IV used for decrypt. Its heading 8 bytes will be used for passcode salt.</li>
    -302          * <li>type - asymmetric key algorithm name of private key described in PEM header.</li>
    -303          * <li>data - base64 encoded encrypted private key.</li>
    -304          * </ul>
    -305          *
    -306          */
    -307         parsePKCS5PEM: function(sPKCS5PEM) {
    -308             return _parsePKCS5PEM(sPKCS5PEM);
    -309         },
    -310 
    -311         /**
    -312          * the same function as OpenSSL EVP_BytsToKey to generate shared key and IV
    -313          * @name getKeyAndUnusedIvByPasscodeAndIvsalt
    -314          * @memberOf PKCS5PKEY
    -315          * @function
    -316          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    -317          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    -318          * @param {String} hexadecimal string of IV. heading 8 bytes will be used for passcode salt
    -319          * @return {Hash} hash of key and unused IV (ex. {keyhex:2fe3..., ivhex:3fad..})
    -320          */
    -321         getKeyAndUnusedIvByPasscodeAndIvsalt: function(algName, passcode, ivsaltHex) {
    -322             return _getKeyAndUnusedIvByPasscodeAndIvsalt(algName, passcode, ivsaltHex);
    -323         },
    -324 
    -325         decryptKeyB64: function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    -326             return _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    -327         },
    -328 
    -329         /**
    -330          * decrypt PEM formatted protected PKCS#5 private key with passcode
    -331          * @name getDecryptedKeyHex
    -332          * @memberOf PKCS5PKEY
    -333          * @function
    -334          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    -335          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    -336          * @return {String} hexadecimal string of decrypted RSA priavte key
    -337          */
    -338         getDecryptedKeyHex: function(sEncryptedPEM, passcode) {
    -339             // 1. parse pem
    -340             var info = _parsePKCS5PEM(sEncryptedPEM);
    -341             var publicKeyAlgName = info.type;
    -342             var sharedKeyAlgName = info.cipher;
    -343             var ivsaltHex = info.ivsalt;
    -344             var privateKeyB64 = info.data;
    -345             //alert("ivsaltHex = " + ivsaltHex);
    -346 
    -347             // 2. generate shared key
    -348             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    -349             var sharedKeyHex = sharedKeyInfo.keyhex;
    -350             //alert("sharedKeyHex = " + sharedKeyHex);
    -351 
    -352             // 3. decrypt private key
    -353             var decryptedKey = _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    -354             return decryptedKey;
    -355         },
    -356 
    -357         /**
    -358          * read PEM formatted encrypted PKCS#5 private key and returns RSAKey object
    -359          * @name getRSAKeyFromEncryptedPKCS5PEM
    -360          * @memberOf PKCS5PKEY
    -361          * @function
    -362          * @param {String} sEncryptedP5PEM PEM formatted encrypted PKCS#5 private key
    -363          * @param {String} passcode passcode to decrypt private key
    -364          * @return {RSAKey} loaded RSAKey object of RSA private key
    -365          * @since pkcs5pkey 1.0.2
    -366          */
    -367         getRSAKeyFromEncryptedPKCS5PEM: function(sEncryptedP5PEM, passcode) {
    -368             var hPKey = this.getDecryptedKeyHex(sEncryptedP5PEM, passcode);
    -369             var rsaKey = new RSAKey();
    -370             rsaKey.readPrivateKeyFromASN1HexString(hPKey);
    -371             return rsaKey;
    -372         },
    -373 
    -374         /**
    -375          * get PEM formatted encrypted PKCS#5 private key from hexadecimal string of plain private key
    -376          * @name getEryptedPKCS5PEMFromPrvKeyHex
    -377          * @memberOf PKCS5PKEY
    -378          * @function
    -379          * @param {String} hPrvKey hexadecimal string of plain private key
    -380          * @param {String} passcode pass code to protect private key (ex. password)
    -381          * @param {String} sharedKeyAlgName algorithm name to protect private key (ex. AES-256-CBC)
    -382          * @param {String} ivsaltHex hexadecimal string of IV and salt
    -383          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    -384          * @since pkcs5pkey 1.0.2
    -385          * @description
    -386          * <br/>
    -387          * generate PEM formatted encrypted PKCS#5 private key by hexadecimal string encoded
    -388          * ASN.1 object of plain RSA private key.
    -389          * Following arguments can be omitted.
    -390          * <ul>
    -391          * <li>alg - AES-256-CBC will be used if omitted.</li>
    -392          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    -393          * </ul>
    -394          * @example
    -395          * var pem = 
    -396          *   PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password");
    -397          * var pem2 = 
    -398          *   PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC");
    -399          * var pem3 = 
    -400          *   PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC", "1f3d02...");
    -401          */
    -402         getEryptedPKCS5PEMFromPrvKeyHex: function(hPrvKey, passcode, sharedKeyAlgName, ivsaltHex) {
    -403             var sPEM = "";
    -404 
    -405             // 1. set sharedKeyAlgName if undefined (default AES-256-CBC)
    -406             if (typeof sharedKeyAlgName == "undefined" || sharedKeyAlgName == null) {
    -407                 sharedKeyAlgName = "AES-256-CBC";
    -408             }
    -409             if (typeof ALGLIST[sharedKeyAlgName] == "undefined")
    -410                 throw "PKCS5PKEY unsupported algorithm: " + sharedKeyAlgName;
    -411 
    -412             // 2. set ivsaltHex if undefined
    -413             if (typeof ivsaltHex == "undefined" || ivsaltHex == null) {
    -414                 var ivlen = ALGLIST[sharedKeyAlgName]['ivlen'];
    -415                 var randIV = _generateIvSaltHex(ivlen);
    -416                 ivsaltHex = randIV.toUpperCase();
    -417             }
    -418 
    -419             // 3. get shared key
    -420             //alert("ivsalthex=" + ivsaltHex);
    -421             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    -422             var sharedKeyHex = sharedKeyInfo.keyhex;
    -423             // alert("sharedKeyHex = " + sharedKeyHex);
    -424 
    -425             // 3. get encrypted Key in Base64
    -426             var encryptedKeyB64 = _encryptKeyHex(hPrvKey, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    -427 
    -428             var pemBody = encryptedKeyB64.replace(/(.{64})/g, "$1\r\n");
    -429             var sPEM = "-----BEGIN RSA PRIVATE KEY-----\r\n";
    -430             sPEM += "Proc-Type: 4,ENCRYPTED\r\n";
    -431             sPEM += "DEK-Info: " + sharedKeyAlgName + "," + ivsaltHex + "\r\n";
    -432             sPEM += "\r\n";
    -433             sPEM += pemBody;
    -434             sPEM += "\r\n-----END RSA PRIVATE KEY-----\r\n";
    -435             
    -436             return sPEM;
    -437         },
    -438 
    -439         /**
    -440          * get PEM formatted encrypted PKCS#5 private key from RSAKey object of private key
    -441          * @name getEryptedPKCS5PEMFromRSAKey
    -442          * @memberOf PKCS5PKEY
    -443          * @function
    -444          * @param {RSAKey} pKey RSAKey object of private key
    -445          * @param {String} passcode pass code to protect private key (ex. password)
    -446          * @param {String} alg algorithm name to protect private key (default AES-256-CBC)
    -447          * @param {String} ivsaltHex hexadecimal string of IV and salt (default generated random IV)
    -448          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    -449          * @since pkcs5pkey 1.0.2
    -450          * @description
    -451          * <br/>
    -452          * generate PEM formatted encrypted PKCS#5 private key by
    -453          * {@link RSAKey} object of RSA private key and passcode.
    -454          * Following argument can be omitted.
    -455          * <ul>
    -456          * <li>alg - AES-256-CBC will be used if omitted.</li>
    -457          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    -458          * </ul>
    -459          * @example
    -460          * var pkey = new RSAKey();
    -461          * pkey.generate(1024, '10001'); // generate 1024bit RSA private key with public exponent 'x010001'
    -462          * var pem = PKCS5PKEY.getEryptedPKCS5PEMFromRSAKey(pkey, "password");
    -463          */
    -464         getEryptedPKCS5PEMFromRSAKey: function(pKey, passcode, alg, ivsaltHex) {
    -465             var version = new KJUR.asn1.DERInteger({'int': 0});
    -466             var n = new KJUR.asn1.DERInteger({'bigint': pKey.n});
    -467             var e = new KJUR.asn1.DERInteger({'int': pKey.e});
    -468             var d = new KJUR.asn1.DERInteger({'bigint': pKey.d});
    -469             var p = new KJUR.asn1.DERInteger({'bigint': pKey.p});
    -470             var q = new KJUR.asn1.DERInteger({'bigint': pKey.q});
    -471             var dmp1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmp1});
    -472             var dmq1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmq1});
    -473             var coeff = new KJUR.asn1.DERInteger({'bigint': pKey.coeff});
    -474             var seq = new KJUR.asn1.DERSequence({'array': [version, n, e, d, p, q, dmp1, dmq1, coeff]});
    -475             var hex = seq.getEncodedHex();
    -476             return this.getEryptedPKCS5PEMFromPrvKeyHex(hex, passcode, alg, ivsaltHex);
    -477         },
    -478 
    -479         /**
    -480          * generate RSAKey and PEM formatted encrypted PKCS#5 private key
    -481          * @name newEncryptedPKCS5PEM
    -482          * @memberOf PKCS5PKEY
    -483          * @function
    -484          * @param {String} passcode pass code to protect private key (ex. password)
    -485          * @param {Integer} keyLen key bit length of RSA key to be generated. (default 1024)
    -486          * @param {String} hPublicExponent hexadecimal string of public exponent (default 10001)
    -487          * @param {String} alg shared key algorithm to encrypt private key (default AES-258-CBC)
    -488          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    -489          * @since pkcs5pkey 1.0.2
    -490          * @example
    -491          * var pem1 = PKCS5PKEY.newEncryptedPKCS5PEM("password");           // RSA1024bit/10001/AES-256-CBC
    -492          * var pem2 = PKCS5PKEY.newEncryptedPKCS5PEM("password", 512);      // RSA 512bit/10001/AES-256-CBC
    -493          * var pem3 = PKCS5PKEY.newEncryptedPKCS5PEM("password", 512, '3'); // RSA 512bit/    3/AES-256-CBC
    -494          */
    -495         newEncryptedPKCS5PEM: function(passcode, keyLen, hPublicExponent, alg) {
    -496             if (typeof keyLen == "undefined" || keyLen == null) {
    -497                 keyLen = 1024;
    -498             }
    -499             if (typeof hPublicExponent == "undefined" || hPublicExponent == null) {
    -500                 hPublicExponent = '10001';
    -501             }
    -502             var pKey = new RSAKey();
    -503             pKey.generate(keyLen, hPublicExponent);
    -504             var pem = null;
    -505             if (typeof alg == "undefined" || alg == null) {
    -506                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pkey, passcode);
    -507             } else {
    -508                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pkey, passcode, alg);
    -509             }
    -510             return pem;
    -511         },
    -512 
    -513         // === PKCS8 ===============================================================
    +241     // *** PUBLIC PROPERTIES AND METHODS *******************************
    +242     // *****************************************************************
    +243     return {
    +244         // -- UTILITY METHODS ------------------------------------------
    +245         /**
    +246          * decrypt private key by shared key
    +247          * @name version
    +248          * @memberOf PKCS5PKEY
    +249          * @property {String} version
    +250          * @description version string of PKCS5PKEY class
    +251          */
    +252         version: "1.0.5",
    +253 
    +254         /**
    +255          * get hexacedimal string of PEM format
    +256          * @name getHexFromPEM
    +257          * @memberOf PKCS5PKEY
    +258          * @function
    +259          * @param {String} sPEM PEM formatted string
    +260          * @param {String} sHead PEM header string without BEGIN/END
    +261          * @return {String} hexadecimal string data of PEM contents
    +262          * @since pkcs5pkey 1.0.5
    +263          */
    +264         getHexFromPEM: function(sPEM, sHead) {
    +265             var s = sPEM;
    +266             if (s.indexOf("BEGIN " + sHead) == -1) {
    +267                 throw "can't find PEM header: " + sHead;
    +268             }
    +269             s = s.replace("-----BEGIN " + sHead + "-----", "");
    +270             s = s.replace("-----END " + sHead + "-----", "");
    +271             var sB64 = s.replace(/\s+/g, '');
    +272             var dataHex = b64tohex(sB64);
    +273             return dataHex;
    +274         },
    +275 
    +276         /**
    +277          * decrypt private key by shared key
    +278          * @name getDecryptedKeyHexByKeyIV
    +279          * @memberOf PKCS5PKEY
    +280          * @function
    +281          * @param {String} encryptedKeyHex hexadecimal string of encrypted private key
    +282          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    +283          * @param {String} sharedKeyHex hexadecimal string of symmetric key
    +284          * @param {String} ivHex hexadecimal string of initial vector(IV).
    +285          * @return {String} hexadecimal string of decrypted privated key
    +286          */
    +287         getDecryptedKeyHexByKeyIV: function(encryptedKeyHex, algName, sharedKeyHex, ivHex) {
    +288             var f1 = getFuncByName(algName);
    +289             return f1(encryptedKeyHex, sharedKeyHex, ivHex);
    +290         },
    +291 
    +292         /**
    +293          * parse PEM formatted passcode protected PKCS#5 private key
    +294          * @name parsePKCS5PEM
    +295          * @memberOf PKCS5PKEY
    +296          * @function
    +297          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    +298          * @return {Hash} hash of key information
    +299          * @description
    +300          * Resulted hash has following attributes.
    +301          * <ul>
    +302          * <li>cipher - symmetric key algorithm name (ex. 'DES-EBE3-CBC', 'AES-256-CBC')</li>
    +303          * <li>ivsalt - IV used for decrypt. Its heading 8 bytes will be used for passcode salt.</li>
    +304          * <li>type - asymmetric key algorithm name of private key described in PEM header.</li>
    +305          * <li>data - base64 encoded encrypted private key.</li>
    +306          * </ul>
    +307          *
    +308          */
    +309         parsePKCS5PEM: function(sPKCS5PEM) {
    +310             return _parsePKCS5PEM(sPKCS5PEM);
    +311         },
    +312 
    +313         /**
    +314          * the same function as OpenSSL EVP_BytsToKey to generate shared key and IV
    +315          * @name getKeyAndUnusedIvByPasscodeAndIvsalt
    +316          * @memberOf PKCS5PKEY
    +317          * @function
    +318          * @param {String} algName name of symmetric key algorithm (ex. 'DES-EBE3-CBC')
    +319          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    +320          * @param {String} hexadecimal string of IV. heading 8 bytes will be used for passcode salt
    +321          * @return {Hash} hash of key and unused IV (ex. {keyhex:2fe3..., ivhex:3fad..})
    +322          */
    +323         getKeyAndUnusedIvByPasscodeAndIvsalt: function(algName, passcode, ivsaltHex) {
    +324             return _getKeyAndUnusedIvByPasscodeAndIvsalt(algName, passcode, ivsaltHex);
    +325         },
    +326 
    +327         decryptKeyB64: function(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex) {
    +328             return _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    +329         },
    +330 
    +331         /**
    +332          * decrypt PEM formatted protected PKCS#5 private key with passcode
    +333          * @name getDecryptedKeyHex
    +334          * @memberOf PKCS5PKEY
    +335          * @function
    +336          * @param {String} sEncryptedPEM PEM formatted protected passcode protected PKCS#5 private key
    +337          * @param {String} passcode passcode to decrypt private key (ex. 'password')
    +338          * @return {String} hexadecimal string of decrypted RSA priavte key
    +339          */
    +340         getDecryptedKeyHex: function(sEncryptedPEM, passcode) {
    +341             // 1. parse pem
    +342             var info = _parsePKCS5PEM(sEncryptedPEM);
    +343             var publicKeyAlgName = info.type;
    +344             var sharedKeyAlgName = info.cipher;
    +345             var ivsaltHex = info.ivsalt;
    +346             var privateKeyB64 = info.data;
    +347             //alert("ivsaltHex = " + ivsaltHex);
    +348 
    +349             // 2. generate shared key
    +350             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    +351             var sharedKeyHex = sharedKeyInfo.keyhex;
    +352             //alert("sharedKeyHex = " + sharedKeyHex);
    +353 
    +354             // 3. decrypt private key
    +355             var decryptedKey = _decryptKeyB64(privateKeyB64, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    +356             return decryptedKey;
    +357         },
    +358 
    +359         /**
    +360          * read PEM formatted encrypted PKCS#5 private key and returns RSAKey object
    +361          * @name getRSAKeyFromEncryptedPKCS5PEM
    +362          * @memberOf PKCS5PKEY
    +363          * @function
    +364          * @param {String} sEncryptedP5PEM PEM formatted encrypted PKCS#5 private key
    +365          * @param {String} passcode passcode to decrypt private key
    +366          * @return {RSAKey} loaded RSAKey object of RSA private key
    +367          * @since pkcs5pkey 1.0.2
    +368          */
    +369         getRSAKeyFromEncryptedPKCS5PEM: function(sEncryptedP5PEM, passcode) {
    +370             var hPKey = this.getDecryptedKeyHex(sEncryptedP5PEM, passcode);
    +371             var rsaKey = new RSAKey();
    +372             rsaKey.readPrivateKeyFromASN1HexString(hPKey);
    +373             return rsaKey;
    +374         },
    +375 
    +376         /**
    +377          * get PEM formatted encrypted PKCS#5 private key from hexadecimal string of plain private key
    +378          * @name getEryptedPKCS5PEMFromPrvKeyHex
    +379          * @memberOf PKCS5PKEY
    +380          * @function
    +381          * @param {String} hPrvKey hexadecimal string of plain private key
    +382          * @param {String} passcode pass code to protect private key (ex. password)
    +383          * @param {String} sharedKeyAlgName algorithm name to protect private key (ex. AES-256-CBC)
    +384          * @param {String} ivsaltHex hexadecimal string of IV and salt
    +385          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    +386          * @since pkcs5pkey 1.0.2
    +387          * @description
    +388          * <br/>
    +389          * generate PEM formatted encrypted PKCS#5 private key by hexadecimal string encoded
    +390          * ASN.1 object of plain RSA private key.
    +391          * Following arguments can be omitted.
    +392          * <ul>
    +393          * <li>alg - AES-256-CBC will be used if omitted.</li>
    +394          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    +395          * </ul>
    +396          * @example
    +397          * var pem = 
    +398          *   PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password");
    +399          * var pem2 = 
    +400          *   PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC");
    +401          * var pem3 = 
    +402          *   PKCS5PKEY.getEryptedPKCS5PEMFromPrvKeyHex(plainKeyHex, "password", "AES-128-CBC", "1f3d02...");
    +403          */
    +404         getEryptedPKCS5PEMFromPrvKeyHex: function(hPrvKey, passcode, sharedKeyAlgName, ivsaltHex) {
    +405             var sPEM = "";
    +406 
    +407             // 1. set sharedKeyAlgName if undefined (default AES-256-CBC)
    +408             if (typeof sharedKeyAlgName == "undefined" || sharedKeyAlgName == null) {
    +409                 sharedKeyAlgName = "AES-256-CBC";
    +410             }
    +411             if (typeof ALGLIST[sharedKeyAlgName] == "undefined")
    +412                 throw "PKCS5PKEY unsupported algorithm: " + sharedKeyAlgName;
    +413 
    +414             // 2. set ivsaltHex if undefined
    +415             if (typeof ivsaltHex == "undefined" || ivsaltHex == null) {
    +416                 var ivlen = ALGLIST[sharedKeyAlgName]['ivlen'];
    +417                 var randIV = _generateIvSaltHex(ivlen);
    +418                 ivsaltHex = randIV.toUpperCase();
    +419             }
    +420 
    +421             // 3. get shared key
    +422             //alert("ivsalthex=" + ivsaltHex);
    +423             var sharedKeyInfo = _getKeyAndUnusedIvByPasscodeAndIvsalt(sharedKeyAlgName, passcode, ivsaltHex);
    +424             var sharedKeyHex = sharedKeyInfo.keyhex;
    +425             // alert("sharedKeyHex = " + sharedKeyHex);
    +426 
    +427             // 3. get encrypted Key in Base64
    +428             var encryptedKeyB64 = _encryptKeyHex(hPrvKey, sharedKeyAlgName, sharedKeyHex, ivsaltHex);
    +429 
    +430             var pemBody = encryptedKeyB64.replace(/(.{64})/g, "$1\r\n");
    +431             var sPEM = "-----BEGIN RSA PRIVATE KEY-----\r\n";
    +432             sPEM += "Proc-Type: 4,ENCRYPTED\r\n";
    +433             sPEM += "DEK-Info: " + sharedKeyAlgName + "," + ivsaltHex + "\r\n";
    +434             sPEM += "\r\n";
    +435             sPEM += pemBody;
    +436             sPEM += "\r\n-----END RSA PRIVATE KEY-----\r\n";
    +437             
    +438             return sPEM;
    +439         },
    +440 
    +441         /**
    +442          * get PEM formatted encrypted PKCS#5 private key from RSAKey object of private key
    +443          * @name getEryptedPKCS5PEMFromRSAKey
    +444          * @memberOf PKCS5PKEY
    +445          * @function
    +446          * @param {RSAKey} pKey RSAKey object of private key
    +447          * @param {String} passcode pass code to protect private key (ex. password)
    +448          * @param {String} alg algorithm name to protect private key (default AES-256-CBC)
    +449          * @param {String} ivsaltHex hexadecimal string of IV and salt (default generated random IV)
    +450          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    +451          * @since pkcs5pkey 1.0.2
    +452          * @description
    +453          * <br/>
    +454          * generate PEM formatted encrypted PKCS#5 private key by
    +455          * {@link RSAKey} object of RSA private key and passcode.
    +456          * Following argument can be omitted.
    +457          * <ul>
    +458          * <li>alg - AES-256-CBC will be used if omitted.</li>
    +459          * <li>ivsaltHex - automatically generate IV and salt which length depends on algorithm</li>
    +460          * </ul>
    +461          * @example
    +462          * var pkey = new RSAKey();
    +463          * pkey.generate(1024, '10001'); // generate 1024bit RSA private key with public exponent 'x010001'
    +464          * var pem = PKCS5PKEY.getEryptedPKCS5PEMFromRSAKey(pkey, "password");
    +465          */
    +466         getEryptedPKCS5PEMFromRSAKey: function(pKey, passcode, alg, ivsaltHex) {
    +467             var version = new KJUR.asn1.DERInteger({'int': 0});
    +468             var n = new KJUR.asn1.DERInteger({'bigint': pKey.n});
    +469             var e = new KJUR.asn1.DERInteger({'int': pKey.e});
    +470             var d = new KJUR.asn1.DERInteger({'bigint': pKey.d});
    +471             var p = new KJUR.asn1.DERInteger({'bigint': pKey.p});
    +472             var q = new KJUR.asn1.DERInteger({'bigint': pKey.q});
    +473             var dmp1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmp1});
    +474             var dmq1 = new KJUR.asn1.DERInteger({'bigint': pKey.dmq1});
    +475             var coeff = new KJUR.asn1.DERInteger({'bigint': pKey.coeff});
    +476             var seq = new KJUR.asn1.DERSequence({'array': [version, n, e, d, p, q, dmp1, dmq1, coeff]});
    +477             var hex = seq.getEncodedHex();
    +478             return this.getEryptedPKCS5PEMFromPrvKeyHex(hex, passcode, alg, ivsaltHex);
    +479         },
    +480 
    +481         /**
    +482          * generate RSAKey and PEM formatted encrypted PKCS#5 private key
    +483          * @name newEncryptedPKCS5PEM
    +484          * @memberOf PKCS5PKEY
    +485          * @function
    +486          * @param {String} passcode pass code to protect private key (ex. password)
    +487          * @param {Integer} keyLen key bit length of RSA key to be generated. (default 1024)
    +488          * @param {String} hPublicExponent hexadecimal string of public exponent (default 10001)
    +489          * @param {String} alg shared key algorithm to encrypt private key (default AES-258-CBC)
    +490          * @return {String} string of PEM formatted encrypted PKCS#5 private key
    +491          * @since pkcs5pkey 1.0.2
    +492          * @example
    +493          * var pem1 = PKCS5PKEY.newEncryptedPKCS5PEM("password");           // RSA1024bit/10001/AES-256-CBC
    +494          * var pem2 = PKCS5PKEY.newEncryptedPKCS5PEM("password", 512);      // RSA 512bit/10001/AES-256-CBC
    +495          * var pem3 = PKCS5PKEY.newEncryptedPKCS5PEM("password", 512, '3'); // RSA 512bit/    3/AES-256-CBC
    +496          */
    +497         newEncryptedPKCS5PEM: function(passcode, keyLen, hPublicExponent, alg) {
    +498             if (typeof keyLen == "undefined" || keyLen == null) {
    +499                 keyLen = 1024;
    +500             }
    +501             if (typeof hPublicExponent == "undefined" || hPublicExponent == null) {
    +502                 hPublicExponent = '10001';
    +503             }
    +504             var pKey = new RSAKey();
    +505             pKey.generate(keyLen, hPublicExponent);
    +506             var pem = null;
    +507             if (typeof alg == "undefined" || alg == null) {
    +508                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pkey, passcode);
    +509             } else {
    +510                 pem = this.getEncryptedPKCS5PEMFromRSAKey(pkey, passcode, alg);
    +511             }
    +512             return pem;
    +513         },
     514 
    -515         /**
    -516          * read PEM formatted unencrypted PKCS#8 private key and returns RSAKey object
    -517          * @name getRSAKeyFromPlainPKCS8PEM
    -518          * @memberOf PKCS5PKEY
    -519          * @function
    -520          * @param {String} pkcs8PEM PEM formatted unencrypted PKCS#8 private key
    -521          * @return {RSAKey} loaded RSAKey object of RSA private key
    -522          * @since pkcs5pkey 1.0.1
    -523          */
    -524         getRSAKeyFromPlainPKCS8PEM: function(pkcs8PEM) {
    -525             if (pkcs8PEM.match(/ENCRYPTED/))
    -526                 throw "pem shall be not ENCRYPTED";
    -527             var prvKeyHex = this.getHexFromPEM(pkcs8PEM, "PRIVATE KEY");
    -528             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    -529             return rsaKey;
    -530         },
    -531 
    -532         /**
    -533          * provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    -534          * @name getRSAKeyFromPlainPKCS8Hex
    -535          * @memberOf PKCS5PKEY
    -536          * @function
    -537          * @param {String} prvKeyHex hexadecimal string of unencrypted PKCS#8 private key
    -538          * @return {RSAKey} loaded RSAKey object of RSA private key
    -539          * @since pkcs5pkey 1.0.3
    -540          */
    -541         getRSAKeyFromPlainPKCS8Hex: function(prvKeyHex) {
    -542             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(prvKeyHex, 0);
    -543             if (a1.length != 3)
    -544                 throw "outer DERSequence shall have 3 elements: " + a1.length;
    -545             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    -546             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    -547                 throw "PKCS8 AlgorithmIdentifier is not rsaEnc: " + algIdTLV;
    -548             var algIdTLV = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    -549             var octetStr = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[2]);
    -550             var p5KeyHex = ASN1HEX.getHexOfV_AtObj(octetStr, 0);
    -551             //alert(p5KeyHex);
    -552             var rsaKey = new RSAKey();
    -553             rsaKey.readPrivateKeyFromASN1HexString(p5KeyHex);
    -554             return rsaKey;
    -555         },
    -556 
    -557         /**
    -558          * generate PBKDF2 key hexstring with specified passcode and information
    -559          * @name parseHexOfEncryptedPKCS8
    -560          * @memberOf PKCS5PKEY
    -561          * @function
    -562          * @param {String} passcode passcode to decrypto private key
    -563          * @return {Array} info associative array of PKCS#8 parameters
    -564          * @since pkcs5pkey 1.0.3
    -565          * @description
    -566          * The associative array which is returned by this method has following properties:
    -567          * <ul>
    -568          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    -569          * <li>info.pkbdf2Iter - iteration count</li>
    -570          * <li>info.ciphertext - hexadecimal string of encrypted private key</li>
    -571          * <li>info.encryptionSchemeAlg - encryption algorithm name (currently TripleDES only)</li>
    -572          * <li>info.encryptionSchemeIV - initial vector for encryption algorithm</li>
    -573          * </ul>
    -574          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -575          * <ul>
    -576          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -577          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -578          * </ul>
    -579          * @example
    -580          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -581          * // key with PBKDF2 with TripleDES
    -582          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -583          */
    -584         parseHexOfEncryptedPKCS8: function(sHEX) {
    -585             var info = {};
    -586         
    -587             var a0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, 0);
    -588             if (a0.length != 2)
    -589                 throw "malformed format: SEQUENCE(0).items != 2: " + a0.length;
    -590 
    -591             // 1. ciphertext
    -592             info.ciphertext = ASN1HEX.getHexOfV_AtObj(sHEX, a0[1]);
    -593 
    -594             // 2. pkcs5PBES2
    -595             var a0_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0[0]); 
    -596             if (a0_0.length != 2)
    -597                 throw "malformed format: SEQUENCE(0.0).items != 2: " + a0_0.length;
    -598 
    -599             // 2.1 check if pkcs5PBES2(1 2 840 113549 1 5 13)
    -600             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0[0]) != "2a864886f70d01050d")
    -601                 throw "this only supports pkcs5PBES2";
    -602 
    -603             // 2.2 pkcs5PBES2 param
    -604             var a0_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0[1]); 
    -605             if (a0_0.length != 2)
    -606                 throw "malformed format: SEQUENCE(0.0.1).items != 2: " + a0_0_1.length;
    -607 
    -608             // 2.2.1 encryptionScheme
    -609             var a0_0_1_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[1]); 
    -610             if (a0_0_1_1.length != 2)
    -611                 throw "malformed format: SEQUENCE(0.0.1.1).items != 2: " + a0_0_1_1.length;
    -612             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[0]) != "2a864886f70d0307")
    -613                 throw "this only supports TripleDES";
    -614             info.encryptionSchemeAlg = "TripleDES";
    -615 
    -616             // 2.2.1.1 IV of encryptionScheme
    -617             info.encryptionSchemeIV = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[1]);
    -618 
    -619             // 2.2.2 keyDerivationFunc
    -620             var a0_0_1_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[0]); 
    -621             if (a0_0_1_0.length != 2)
    -622                 throw "malformed format: SEQUENCE(0.0.1.0).items != 2: " + a0_0_1_0.length;
    -623             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0[0]) != "2a864886f70d01050c")
    -624                 throw "this only supports pkcs5PBKDF2";
    -625             
    -626             // 2.2.2.1 pkcs5PBKDF2 param
    -627             var a0_0_1_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1_0[1]); 
    -628             if (a0_0_1_0_1.length < 2)
    -629                 throw "malformed format: SEQUENCE(0.0.1.0.1).items < 2: " + a0_0_1_0_1.length;
    -630 
    -631             // 2.2.2.1.1 PBKDF2 salt
    -632             info.pbkdf2Salt = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[0]);
    -633 
    -634             // 2.2.2.1.2 PBKDF2 iter
    -635             var iterNumHex = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[1]);
    -636             try {
    -637                 info.pbkdf2Iter = parseInt(iterNumHex, 16);
    -638             } catch(ex) {
    -639                 throw "malformed format pbkdf2Iter: " + iterNumHex;
    -640             }
    -641 
    -642             return info;
    -643         },
    -644 
    -645         /**
    -646          * generate PBKDF2 key hexstring with specified passcode and information
    -647          * @name getPBKDF2KeyHexFromParam
    -648          * @memberOf PKCS5PKEY
    -649          * @function
    -650          * @param {Array} info result of {@link parseHexOfEncryptedPKCS8} which has preference of PKCS#8 file
    -651          * @param {String} passcode passcode to decrypto private key
    -652          * @return {String} hexadecimal string of PBKDF2 key
    -653          * @since pkcs5pkey 1.0.3
    -654          * @description
    -655          * As for info, this uses following properties:
    -656          * <ul>
    -657          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    -658          * <li>info.pkbdf2Iter - iteration count</li>
    -659          * </ul>
    -660          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -661          * <ul>
    -662          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -663          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -664          * </ul>
    -665          * @example
    -666          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -667          * // key with PBKDF2 with TripleDES
    -668          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -669          */
    -670         getPBKDF2KeyHexFromParam: function(info, passcode) {
    -671             var pbkdf2SaltWS = CryptoJS.enc.Hex.parse(info.pbkdf2Salt);
    -672             var pbkdf2Iter = info.pbkdf2Iter;
    -673             var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, 
    -674                                               pbkdf2SaltWS, 
    -675                                               { keySize: 192/32, iterations: pbkdf2Iter });
    -676             var pbkdf2KeyHex = CryptoJS.enc.Hex.stringify(pbkdf2KeyWS);
    -677             return pbkdf2KeyHex;
    -678         },
    -679 
    -680         /**
    -681          * read PEM formatted encrypted PKCS#8 private key and returns hexadecimal string of plain PKCS#8 private key
    -682          * @name getPlainPKCS8HexFromEncryptedPKCS8PEM
    -683          * @memberOf PKCS5PKEY
    -684          * @function
    -685          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    -686          * @param {String} passcode passcode to decrypto private key
    -687          * @return {String} hexadecimal string of plain PKCS#8 private key
    -688          * @since pkcs5pkey 1.0.3
    -689          * @description
    -690          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -691          * <ul>
    -692          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -693          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -694          * </ul>
    -695          * @example
    -696          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -697          * // key with PBKDF2 with TripleDES
    -698          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -699          */
    -700         getPlainPKCS8HexFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    -701             // 1. derHex - PKCS#8 private key encrypted by PBKDF2
    -702             var derHex = this.getHexFromPEM(pkcs8PEM, "ENCRYPTED PRIVATE KEY");
    -703             // 2. info - PKCS#5 PBES info
    -704             var info = this.parseHexOfEncryptedPKCS8(derHex);
    -705             // 3. hKey - PBKDF2 key
    -706             var pbkdf2KeyHex = PKCS5PKEY.getPBKDF2KeyHexFromParam(info, passcode);
    -707             // 4. decrypt ciphertext by PBKDF2 key
    -708             var encrypted = {};
    -709             encrypted.ciphertext = CryptoJS.enc.Hex.parse(info.ciphertext);
    -710             var pbkdf2KeyWS = CryptoJS.enc.Hex.parse(pbkdf2KeyHex);
    -711             var des3IVWS = CryptoJS.enc.Hex.parse(info.encryptionSchemeIV);
    -712             var decWS = CryptoJS.TripleDES.decrypt(encrypted, pbkdf2KeyWS, { iv: des3IVWS });
    -713             var decHex = CryptoJS.enc.Hex.stringify(decWS);
    -714             return decHex;
    -715         },
    -716 
    -717         /**
    -718          * read PEM formatted encrypted PKCS#8 private key and returns RSAKey object
    -719          * @name getRSAKeyFromEncryptedPKCS8PEM
    -720          * @memberOf PKCS5PKEY
    -721          * @function
    -722          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    -723          * @param {String} passcode passcode to decrypto private key
    -724          * @return {RSAKey} loaded RSAKey object of RSA private key
    -725          * @since pkcs5pkey 1.0.3
    -726          * @description
    -727          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    -728          * <ul>
    -729          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    -730          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    -731          * </ul>
    -732          * @example
    -733          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    -734          * // key with PBKDF2 with TripleDES
    -735          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    -736          */
    -737         getRSAKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    -738             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    -739             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    -740             return rsaKey;
    -741         },
    -742 
    -743         /**
    -744          * get RSAKey/ECDSA private key object from encrypted PEM PKCS#8 private key
    -745          * @name getKeyFromEncryptedPKCS8PEM
    -746          * @memberOf PKCS5PKEY
    -747          * @function
    -748          * @param {String} pkcs8PEM string of PEM formatted PKCS#8 private key
    -749          * @param {String} passcode passcode string to decrypt key
    -750          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -751          * @since pkcs5pkey 1.0.5
    -752          */
    -753         getKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    -754             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    -755             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    -756             return key;
    -757         },
    -758 
    -759         /**
    -760          * parse hexadecimal string of plain PKCS#8 private key
    -761          * @name parsePlainPrivatePKCS8Hex
    -762          * @memberOf PKCS5PKEY
    -763          * @function
    -764          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 plain private key
    -765          * @return {Array} associative array of parsed key
    -766          * @since pkcs5pkey 1.0.5
    -767          * @description
    -768          * Resulted associative array has following properties:
    -769          * <ul>
    -770          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    -771          * <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
    -772          * <li>keyidx - string starting index of key in pkcs8PrvHex</li>
    -773          * </ul>
    -774          */
    -775         parsePlainPrivatePKCS8Hex: function(pkcs8PrvHex) {
    -776             var result = {};
    -777             result.algparam = null;
    -778 
    -779             // 1. sequence
    -780             if (pkcs8PrvHex.substr(0, 2) != "30")
    -781                 throw "malformed plain PKCS8 private key(code:001)"; // not sequence
    -782 
    -783             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, 0);
    -784             if (a1.length != 3)
    -785                 throw "malformed plain PKCS8 private key(code:002)";
    -786 
    -787             // 2. AlgID
    -788             if (pkcs8PrvHex.substr(a1[1], 2) != "30")
    -789                 throw "malformed PKCS8 private key(code:003)"; // AlgId not sequence
    -790 
    -791             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, a1[1]);
    -792             if (a2.length != 2)
    -793                 throw "malformed PKCS8 private key(code:004)"; // AlgId not have two elements
    -794 
    -795             // 2.1. AlgID OID
    -796             if (pkcs8PrvHex.substr(a2[0], 2) != "06")
    -797                 throw "malformed PKCS8 private key(code:005)"; // AlgId.oid is not OID
    -798 
    -799             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[0]);
    +515         // === PKCS8 ===============================================================
    +516 
    +517         /**
    +518          * read PEM formatted unencrypted PKCS#8 private key and returns RSAKey object
    +519          * @name getRSAKeyFromPlainPKCS8PEM
    +520          * @memberOf PKCS5PKEY
    +521          * @function
    +522          * @param {String} pkcs8PEM PEM formatted unencrypted PKCS#8 private key
    +523          * @return {RSAKey} loaded RSAKey object of RSA private key
    +524          * @since pkcs5pkey 1.0.1
    +525          */
    +526         getRSAKeyFromPlainPKCS8PEM: function(pkcs8PEM) {
    +527             if (pkcs8PEM.match(/ENCRYPTED/))
    +528                 throw "pem shall be not ENCRYPTED";
    +529             var prvKeyHex = this.getHexFromPEM(pkcs8PEM, "PRIVATE KEY");
    +530             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    +531             return rsaKey;
    +532         },
    +533 
    +534         /**
    +535          * provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    +536          * @name getRSAKeyFromPlainPKCS8Hex
    +537          * @memberOf PKCS5PKEY
    +538          * @function
    +539          * @param {String} prvKeyHex hexadecimal string of unencrypted PKCS#8 private key
    +540          * @return {RSAKey} loaded RSAKey object of RSA private key
    +541          * @since pkcs5pkey 1.0.3
    +542          */
    +543         getRSAKeyFromPlainPKCS8Hex: function(prvKeyHex) {
    +544             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(prvKeyHex, 0);
    +545             if (a1.length != 3)
    +546                 throw "outer DERSequence shall have 3 elements: " + a1.length;
    +547             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    +548             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    +549                 throw "PKCS8 AlgorithmIdentifier is not rsaEnc: " + algIdTLV;
    +550             var algIdTLV = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[1]);
    +551             var octetStr = ASN1HEX.getHexOfTLV_AtObj(prvKeyHex, a1[2]);
    +552             var p5KeyHex = ASN1HEX.getHexOfV_AtObj(octetStr, 0);
    +553             //alert(p5KeyHex);
    +554             var rsaKey = new RSAKey();
    +555             rsaKey.readPrivateKeyFromASN1HexString(p5KeyHex);
    +556             return rsaKey;
    +557         },
    +558 
    +559         /**
    +560          * generate PBKDF2 key hexstring with specified passcode and information
    +561          * @name parseHexOfEncryptedPKCS8
    +562          * @memberOf PKCS5PKEY
    +563          * @function
    +564          * @param {String} passcode passcode to decrypto private key
    +565          * @return {Array} info associative array of PKCS#8 parameters
    +566          * @since pkcs5pkey 1.0.3
    +567          * @description
    +568          * The associative array which is returned by this method has following properties:
    +569          * <ul>
    +570          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    +571          * <li>info.pkbdf2Iter - iteration count</li>
    +572          * <li>info.ciphertext - hexadecimal string of encrypted private key</li>
    +573          * <li>info.encryptionSchemeAlg - encryption algorithm name (currently TripleDES only)</li>
    +574          * <li>info.encryptionSchemeIV - initial vector for encryption algorithm</li>
    +575          * </ul>
    +576          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +577          * <ul>
    +578          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +579          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +580          * </ul>
    +581          * @example
    +582          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +583          * // key with PBKDF2 with TripleDES
    +584          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +585          */
    +586         parseHexOfEncryptedPKCS8: function(sHEX) {
    +587             var info = {};
    +588         
    +589             var a0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, 0);
    +590             if (a0.length != 2)
    +591                 throw "malformed format: SEQUENCE(0).items != 2: " + a0.length;
    +592 
    +593             // 1. ciphertext
    +594             info.ciphertext = ASN1HEX.getHexOfV_AtObj(sHEX, a0[1]);
    +595 
    +596             // 2. pkcs5PBES2
    +597             var a0_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0[0]); 
    +598             if (a0_0.length != 2)
    +599                 throw "malformed format: SEQUENCE(0.0).items != 2: " + a0_0.length;
    +600 
    +601             // 2.1 check if pkcs5PBES2(1 2 840 113549 1 5 13)
    +602             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0[0]) != "2a864886f70d01050d")
    +603                 throw "this only supports pkcs5PBES2";
    +604 
    +605             // 2.2 pkcs5PBES2 param
    +606             var a0_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0[1]); 
    +607             if (a0_0.length != 2)
    +608                 throw "malformed format: SEQUENCE(0.0.1).items != 2: " + a0_0_1.length;
    +609 
    +610             // 2.2.1 encryptionScheme
    +611             var a0_0_1_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[1]); 
    +612             if (a0_0_1_1.length != 2)
    +613                 throw "malformed format: SEQUENCE(0.0.1.1).items != 2: " + a0_0_1_1.length;
    +614             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[0]) != "2a864886f70d0307")
    +615                 throw "this only supports TripleDES";
    +616             info.encryptionSchemeAlg = "TripleDES";
    +617 
    +618             // 2.2.1.1 IV of encryptionScheme
    +619             info.encryptionSchemeIV = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_1[1]);
    +620 
    +621             // 2.2.2 keyDerivationFunc
    +622             var a0_0_1_0 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1[0]); 
    +623             if (a0_0_1_0.length != 2)
    +624                 throw "malformed format: SEQUENCE(0.0.1.0).items != 2: " + a0_0_1_0.length;
    +625             if (ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0[0]) != "2a864886f70d01050c")
    +626                 throw "this only supports pkcs5PBKDF2";
    +627             
    +628             // 2.2.2.1 pkcs5PBKDF2 param
    +629             var a0_0_1_0_1 = ASN1HEX.getPosArrayOfChildren_AtObj(sHEX, a0_0_1_0[1]); 
    +630             if (a0_0_1_0_1.length < 2)
    +631                 throw "malformed format: SEQUENCE(0.0.1.0.1).items < 2: " + a0_0_1_0_1.length;
    +632 
    +633             // 2.2.2.1.1 PBKDF2 salt
    +634             info.pbkdf2Salt = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[0]);
    +635 
    +636             // 2.2.2.1.2 PBKDF2 iter
    +637             var iterNumHex = ASN1HEX.getHexOfV_AtObj(sHEX, a0_0_1_0_1[1]);
    +638             try {
    +639                 info.pbkdf2Iter = parseInt(iterNumHex, 16);
    +640             } catch(ex) {
    +641                 throw "malformed format pbkdf2Iter: " + iterNumHex;
    +642             }
    +643 
    +644             return info;
    +645         },
    +646 
    +647         /**
    +648          * generate PBKDF2 key hexstring with specified passcode and information
    +649          * @name getPBKDF2KeyHexFromParam
    +650          * @memberOf PKCS5PKEY
    +651          * @function
    +652          * @param {Array} info result of {@link parseHexOfEncryptedPKCS8} which has preference of PKCS#8 file
    +653          * @param {String} passcode passcode to decrypto private key
    +654          * @return {String} hexadecimal string of PBKDF2 key
    +655          * @since pkcs5pkey 1.0.3
    +656          * @description
    +657          * As for info, this uses following properties:
    +658          * <ul>
    +659          * <li>info.pbkdf2Salt - hexadecimal string of PBKDF2 salt</li>
    +660          * <li>info.pkbdf2Iter - iteration count</li>
    +661          * </ul>
    +662          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +663          * <ul>
    +664          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +665          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +666          * </ul>
    +667          * @example
    +668          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +669          * // key with PBKDF2 with TripleDES
    +670          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +671          */
    +672         getPBKDF2KeyHexFromParam: function(info, passcode) {
    +673             var pbkdf2SaltWS = CryptoJS.enc.Hex.parse(info.pbkdf2Salt);
    +674             var pbkdf2Iter = info.pbkdf2Iter;
    +675             var pbkdf2KeyWS = CryptoJS.PBKDF2(passcode, 
    +676                                               pbkdf2SaltWS, 
    +677                                               { keySize: 192/32, iterations: pbkdf2Iter });
    +678             var pbkdf2KeyHex = CryptoJS.enc.Hex.stringify(pbkdf2KeyWS);
    +679             return pbkdf2KeyHex;
    +680         },
    +681 
    +682         /**
    +683          * read PEM formatted encrypted PKCS#8 private key and returns hexadecimal string of plain PKCS#8 private key
    +684          * @name getPlainPKCS8HexFromEncryptedPKCS8PEM
    +685          * @memberOf PKCS5PKEY
    +686          * @function
    +687          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    +688          * @param {String} passcode passcode to decrypto private key
    +689          * @return {String} hexadecimal string of plain PKCS#8 private key
    +690          * @since pkcs5pkey 1.0.3
    +691          * @description
    +692          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +693          * <ul>
    +694          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +695          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +696          * </ul>
    +697          * @example
    +698          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +699          * // key with PBKDF2 with TripleDES
    +700          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +701          */
    +702         getPlainPKCS8HexFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    +703             // 1. derHex - PKCS#8 private key encrypted by PBKDF2
    +704             var derHex = this.getHexFromPEM(pkcs8PEM, "ENCRYPTED PRIVATE KEY");
    +705             // 2. info - PKCS#5 PBES info
    +706             var info = this.parseHexOfEncryptedPKCS8(derHex);
    +707             // 3. hKey - PBKDF2 key
    +708             var pbkdf2KeyHex = PKCS5PKEY.getPBKDF2KeyHexFromParam(info, passcode);
    +709             // 4. decrypt ciphertext by PBKDF2 key
    +710             var encrypted = {};
    +711             encrypted.ciphertext = CryptoJS.enc.Hex.parse(info.ciphertext);
    +712             var pbkdf2KeyWS = CryptoJS.enc.Hex.parse(pbkdf2KeyHex);
    +713             var des3IVWS = CryptoJS.enc.Hex.parse(info.encryptionSchemeIV);
    +714             var decWS = CryptoJS.TripleDES.decrypt(encrypted, pbkdf2KeyWS, { iv: des3IVWS });
    +715             var decHex = CryptoJS.enc.Hex.stringify(decWS);
    +716             return decHex;
    +717         },
    +718 
    +719         /**
    +720          * read PEM formatted encrypted PKCS#8 private key and returns RSAKey object
    +721          * @name getRSAKeyFromEncryptedPKCS8PEM
    +722          * @memberOf PKCS5PKEY
    +723          * @function
    +724          * @param {String} pkcs8PEM PEM formatted encrypted PKCS#8 private key
    +725          * @param {String} passcode passcode to decrypto private key
    +726          * @return {RSAKey} loaded RSAKey object of RSA private key
    +727          * @since pkcs5pkey 1.0.3
    +728          * @description
    +729          * Currently, this method only supports PKCS#5v2.0 with PBES2/PBDKF2 of HmacSHA1 and TripleDES.
    +730          * <ul>
    +731          * <li>keyDerivationFunc = pkcs5PBKDF2 with HmacSHA1</li>
    +732          * <li>encryptionScheme = des-EDE3-CBC(i.e. TripleDES</li>
    +733          * </ul>
    +734          * @example
    +735          * // to convert plain PKCS#5 private key to encrypted PKCS#8 private
    +736          * // key with PBKDF2 with TripleDES
    +737          * % openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem
    +738          */
    +739         getRSAKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    +740             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    +741             var rsaKey = this.getRSAKeyFromPlainPKCS8Hex(prvKeyHex);
    +742             return rsaKey;
    +743         },
    +744 
    +745         /**
    +746          * get RSAKey/ECDSA private key object from encrypted PEM PKCS#8 private key
    +747          * @name getKeyFromEncryptedPKCS8PEM
    +748          * @memberOf PKCS5PKEY
    +749          * @function
    +750          * @param {String} pkcs8PEM string of PEM formatted PKCS#8 private key
    +751          * @param {String} passcode passcode string to decrypt key
    +752          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +753          * @since pkcs5pkey 1.0.5
    +754          */
    +755         getKeyFromEncryptedPKCS8PEM: function(pkcs8PEM, passcode) {
    +756             var prvKeyHex = this.getPlainPKCS8HexFromEncryptedPKCS8PEM(pkcs8PEM, passcode);
    +757             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    +758             return key;
    +759         },
    +760 
    +761         /**
    +762          * parse hexadecimal string of plain PKCS#8 private key
    +763          * @name parsePlainPrivatePKCS8Hex
    +764          * @memberOf PKCS5PKEY
    +765          * @function
    +766          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 plain private key
    +767          * @return {Array} associative array of parsed key
    +768          * @since pkcs5pkey 1.0.5
    +769          * @description
    +770          * Resulted associative array has following properties:
    +771          * <ul>
    +772          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    +773          * <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
    +774          * <li>keyidx - string starting index of key in pkcs8PrvHex</li>
    +775          * </ul>
    +776          */
    +777         parsePlainPrivatePKCS8Hex: function(pkcs8PrvHex) {
    +778             var result = {};
    +779             result.algparam = null;
    +780 
    +781             // 1. sequence
    +782             if (pkcs8PrvHex.substr(0, 2) != "30")
    +783                 throw "malformed plain PKCS8 private key(code:001)"; // not sequence
    +784 
    +785             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, 0);
    +786             if (a1.length != 3)
    +787                 throw "malformed plain PKCS8 private key(code:002)";
    +788 
    +789             // 2. AlgID
    +790             if (pkcs8PrvHex.substr(a1[1], 2) != "30")
    +791                 throw "malformed PKCS8 private key(code:003)"; // AlgId not sequence
    +792 
    +793             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, a1[1]);
    +794             if (a2.length != 2)
    +795                 throw "malformed PKCS8 private key(code:004)"; // AlgId not have two elements
    +796 
    +797             // 2.1. AlgID OID
    +798             if (pkcs8PrvHex.substr(a2[0], 2) != "06")
    +799                 throw "malformed PKCS8 private key(code:005)"; // AlgId.oid is not OID
     800 
    -801             // 2.2. AlgID param
    -802             if (pkcs8PrvHex.substr(a2[1], 2) == "06") {
    -803                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[1]);
    -804             }
    -805 
    -806             // 3. Key index
    -807             if (pkcs8PrvHex.substr(a1[2], 2) != "04")
    -808                 throw "malformed PKCS8 private key(code:006)"; // not octet string
    -809 
    -810             result.keyidx = ASN1HEX.getStartPosOfV_AtObj(pkcs8PrvHex, a1[2]);
    +801             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[0]);
    +802 
    +803             // 2.2. AlgID param
    +804             if (pkcs8PrvHex.substr(a2[1], 2) == "06") {
    +805                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a2[1]);
    +806             }
    +807 
    +808             // 3. Key index
    +809             if (pkcs8PrvHex.substr(a1[2], 2) != "04")
    +810                 throw "malformed PKCS8 private key(code:006)"; // not octet string
     811 
    -812             return result;
    -813         },
    -814 
    -815         /**
    -816          * get RSAKey/ECDSA private key object from PEM plain PEM PKCS#8 private key
    -817          * @name getKeyFromPlainPrivatePKCS8PEM
    -818          * @memberOf PKCS5PKEY
    -819          * @function
    -820          * @param {String} pkcs8PEM string of plain PEM formatted PKCS#8 private key
    -821          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -822          * @since pkcs5pkey 1.0.5
    -823          */
    -824         getKeyFromPlainPrivatePKCS8PEM: function(prvKeyPEM) {
    -825             var prvKeyHex = this.getHexFromPEM(prvKeyPEM, "PRIVATE KEY");
    -826             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    -827             return key;
    -828         },
    -829 
    -830         /**
    -831          * get RSAKey/ECDSA private key object from HEX plain PEM PKCS#8 private key
    -832          * @name getKeyFromPlainPrivatePKCS8Hex
    -833          * @memberOf PKCS5PKEY
    -834          * @function
    -835          * @param {String} prvKeyHex hexadecimal string of plain PKCS#8 private key
    -836          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -837          * @since pkcs5pkey 1.0.5
    -838          */
    -839         getKeyFromPlainPrivatePKCS8Hex: function(prvKeyHex) {
    -840             var p8 = this.parsePlainPrivatePKCS8Hex(prvKeyHex);
    -841             
    -842             if (p8.algoid == "2a864886f70d010101") { // RSA
    -843                 this.parsePrivateRawRSAKeyHexAtObj(prvKeyHex, p8);
    -844                 var k = p8.key;
    -845                 var key = new RSAKey();
    -846                 key.setPrivateEx(k.n, k.e, k.d, k.p, k.q, k.dp, k.dq, k.co);
    -847                 return key;
    -848             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    -849                 this.parsePrivateRawECKeyHexAtObj(prvKeyHex, p8);
    -850                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    -851                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    -852                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    -853                 var key = new KJUR.crypto.ECDSA({'curve': curveName, 'prv': p8.key});
    -854                 return key;
    -855             } else {
    -856                 throw "unsupported private key algorithm";
    -857             }
    -858         },
    -859 
    -860         // === PKCS8 RSA Public Key ================================================
    -861         /**
    -862          * read PEM formatted PKCS#8 public key and returns RSAKey object
    -863          * @name getRSAKeyFromPublicPKCS8PEM
    -864          * @memberOf PKCS5PKEY
    -865          * @function
    -866          * @param {String} pkcs8PubPEM PEM formatted PKCS#8 public key
    -867          * @return {RSAKey} loaded RSAKey object of RSA public key
    -868          * @since pkcs5pkey 1.0.4
    -869          */
    -870         getRSAKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    -871             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    -872             var rsaKey = this.getRSAKeyFromPublicPKCS8Hex(pubKeyHex);
    -873             return rsaKey;
    -874         },
    -875 
    -876         /**
    -877          * get RSAKey/ECDSA public key object from PEM PKCS#8 public key
    -878          * @name getKeyFromPublicPKCS8PEM
    -879          * @memberOf PKCS5PKEY
    -880          * @function
    -881          * @param {String} pkcsPub8PEM string of PEM formatted PKCS#8 public key
    -882          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -883          * @since pkcs5pkey 1.0.5
    -884          */
    -885         getKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    -886             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    -887             var key = this.getKeyFromPublicPKCS8Hex(pubKeyHex);
    -888             return key;
    -889         },
    -890 
    -891         /**
    -892          * get RSAKey/ECDSA public key object from hexadecimal string of PKCS#8 public key
    -893          * @name getKeyFromPublicPKCS8Hex
    -894          * @memberOf PKCS5PKEY
    -895          * @function
    -896          * @param {String} pkcsPub8Hex hexadecimal string of PKCS#8 public key
    -897          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    -898          * @since pkcs5pkey 1.0.5
    -899          */
    -900         getKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    -901             var p8 = this.parsePublicPKCS8Hex(pkcs8PubHex);
    -902             
    -903             if (p8.algoid == "2a864886f70d010101") { // RSA
    -904                 var aRSA = this.parsePublicRawRSAKeyHex(p8.key);
    -905                 var key = new RSAKey();
    -906                 key.setPublic(aRSA.n, aRSA.e);
    -907                 return key;
    -908             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    -909                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    -910                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    -911                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    -912                 var key = new KJUR.crypto.ECDSA({'curve': curveName, 'pub': p8.key});
    -913                 return key;
    -914             } else {
    -915                 throw "unsupported public key algorithm";
    -916             }
    -917         },
    -918 
    -919         /**
    -920          * parse hexadecimal string of plain PKCS#8 private key
    -921          * @name parsePublicRawRSAKeyHex
    -922          * @memberOf PKCS5PKEY
    -923          * @function
    -924          * @param {String} pubRawRSAHex hexadecimal string of ASN.1 encoded PKCS#8 public key
    -925          * @return {Array} associative array of parsed key
    -926          * @since pkcs5pkey 1.0.5
    -927          * @description
    -928          * Resulted associative array has following properties:
    -929          * <ul>
    -930          * <li>n - hexadecimal string of public key
    -931          * <li>e - hexadecimal string of public exponent
    -932          * </ul>
    -933          */
    -934         parsePublicRawRSAKeyHex: function(pubRawRSAHex) {
    -935             var result = {};
    -936             
    -937             // 1. Sequence
    -938             if (pubRawRSAHex.substr(0, 2) != "30")
    -939                 throw "malformed RSA key(code:001)"; // not sequence
    -940             
    -941             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pubRawRSAHex, 0);
    -942             if (a1.length != 2)
    -943                 throw "malformed RSA key(code:002)"; // not 2 items in seq
    -944 
    -945             // 2. public key "N"
    -946             if (pubRawRSAHex.substr(a1[0], 2) != "02")
    -947                 throw "malformed RSA key(code:003)"; // 1st item is not integer
    -948 
    -949             result.n = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[0]);
    +812             result.keyidx = ASN1HEX.getStartPosOfV_AtObj(pkcs8PrvHex, a1[2]);
    +813 
    +814             return result;
    +815         },
    +816 
    +817         /**
    +818          * get RSAKey/ECDSA private key object from PEM plain PEM PKCS#8 private key
    +819          * @name getKeyFromPlainPrivatePKCS8PEM
    +820          * @memberOf PKCS5PKEY
    +821          * @function
    +822          * @param {String} pkcs8PEM string of plain PEM formatted PKCS#8 private key
    +823          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +824          * @since pkcs5pkey 1.0.5
    +825          */
    +826         getKeyFromPlainPrivatePKCS8PEM: function(prvKeyPEM) {
    +827             var prvKeyHex = this.getHexFromPEM(prvKeyPEM, "PRIVATE KEY");
    +828             var key = this.getKeyFromPlainPrivatePKCS8Hex(prvKeyHex);
    +829             return key;
    +830         },
    +831 
    +832         /**
    +833          * get RSAKey/ECDSA private key object from HEX plain PEM PKCS#8 private key
    +834          * @name getKeyFromPlainPrivatePKCS8Hex
    +835          * @memberOf PKCS5PKEY
    +836          * @function
    +837          * @param {String} prvKeyHex hexadecimal string of plain PKCS#8 private key
    +838          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +839          * @since pkcs5pkey 1.0.5
    +840          */
    +841         getKeyFromPlainPrivatePKCS8Hex: function(prvKeyHex) {
    +842             var p8 = this.parsePlainPrivatePKCS8Hex(prvKeyHex);
    +843             
    +844             if (p8.algoid == "2a864886f70d010101") { // RSA
    +845                 this.parsePrivateRawRSAKeyHexAtObj(prvKeyHex, p8);
    +846                 var k = p8.key;
    +847                 var key = new RSAKey();
    +848                 key.setPrivateEx(k.n, k.e, k.d, k.p, k.q, k.dp, k.dq, k.co);
    +849                 return key;
    +850             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    +851                 this.parsePrivateRawECKeyHexAtObj(prvKeyHex, p8);
    +852                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    +853                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    +854                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    +855                 var key = new KJUR.crypto.ECDSA({'curve': curveName, 'prv': p8.key});
    +856                 return key;
    +857             } else {
    +858                 throw "unsupported private key algorithm";
    +859             }
    +860         },
    +861 
    +862         // === PKCS8 RSA Public Key ================================================
    +863         /**
    +864          * read PEM formatted PKCS#8 public key and returns RSAKey object
    +865          * @name getRSAKeyFromPublicPKCS8PEM
    +866          * @memberOf PKCS5PKEY
    +867          * @function
    +868          * @param {String} pkcs8PubPEM PEM formatted PKCS#8 public key
    +869          * @return {RSAKey} loaded RSAKey object of RSA public key
    +870          * @since pkcs5pkey 1.0.4
    +871          */
    +872         getRSAKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    +873             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    +874             var rsaKey = this.getRSAKeyFromPublicPKCS8Hex(pubKeyHex);
    +875             return rsaKey;
    +876         },
    +877 
    +878         /**
    +879          * get RSAKey/ECDSA public key object from PEM PKCS#8 public key
    +880          * @name getKeyFromPublicPKCS8PEM
    +881          * @memberOf PKCS5PKEY
    +882          * @function
    +883          * @param {String} pkcsPub8PEM string of PEM formatted PKCS#8 public key
    +884          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +885          * @since pkcs5pkey 1.0.5
    +886          */
    +887         getKeyFromPublicPKCS8PEM: function(pkcs8PubPEM) {
    +888             var pubKeyHex = this.getHexFromPEM(pkcs8PubPEM, "PUBLIC KEY");
    +889             var key = this.getKeyFromPublicPKCS8Hex(pubKeyHex);
    +890             return key;
    +891         },
    +892 
    +893         /**
    +894          * get RSAKey/ECDSA public key object from hexadecimal string of PKCS#8 public key
    +895          * @name getKeyFromPublicPKCS8Hex
    +896          * @memberOf PKCS5PKEY
    +897          * @function
    +898          * @param {String} pkcsPub8Hex hexadecimal string of PKCS#8 public key
    +899          * @return {Object} RSAKey or KJUR.crypto.ECDSA private key object
    +900          * @since pkcs5pkey 1.0.5
    +901          */
    +902         getKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    +903             var p8 = this.parsePublicPKCS8Hex(pkcs8PubHex);
    +904             
    +905             if (p8.algoid == "2a864886f70d010101") { // RSA
    +906                 var aRSA = this.parsePublicRawRSAKeyHex(p8.key);
    +907                 var key = new RSAKey();
    +908                 key.setPublic(aRSA.n, aRSA.e);
    +909                 return key;
    +910             } else if (p8.algoid == "2a8648ce3d0201") { // ECC
    +911                 if (KJUR.crypto.OID.oidhex2name[p8.algparam] === undefined)
    +912                     throw "KJUR.crypto.OID.oidhex2name undefined: " + p8.algparam;
    +913                 var curveName = KJUR.crypto.OID.oidhex2name[p8.algparam];
    +914                 var key = new KJUR.crypto.ECDSA({'curve': curveName, 'pub': p8.key});
    +915                 return key;
    +916             } else {
    +917                 throw "unsupported public key algorithm";
    +918             }
    +919         },
    +920 
    +921         /**
    +922          * parse hexadecimal string of plain PKCS#8 private key
    +923          * @name parsePublicRawRSAKeyHex
    +924          * @memberOf PKCS5PKEY
    +925          * @function
    +926          * @param {String} pubRawRSAHex hexadecimal string of ASN.1 encoded PKCS#8 public key
    +927          * @return {Array} associative array of parsed key
    +928          * @since pkcs5pkey 1.0.5
    +929          * @description
    +930          * Resulted associative array has following properties:
    +931          * <ul>
    +932          * <li>n - hexadecimal string of public key
    +933          * <li>e - hexadecimal string of public exponent
    +934          * </ul>
    +935          */
    +936         parsePublicRawRSAKeyHex: function(pubRawRSAHex) {
    +937             var result = {};
    +938             
    +939             // 1. Sequence
    +940             if (pubRawRSAHex.substr(0, 2) != "30")
    +941                 throw "malformed RSA key(code:001)"; // not sequence
    +942             
    +943             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pubRawRSAHex, 0);
    +944             if (a1.length != 2)
    +945                 throw "malformed RSA key(code:002)"; // not 2 items in seq
    +946 
    +947             // 2. public key "N"
    +948             if (pubRawRSAHex.substr(a1[0], 2) != "02")
    +949                 throw "malformed RSA key(code:003)"; // 1st item is not integer
     950 
    -951             // 3. public key "E"
    -952             if (pubRawRSAHex.substr(a1[1], 2) != "02")
    -953                 throw "malformed RSA key(code:004)"; // 2nd item is not integer
    -954 
    -955             result.e = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[1]);
    +951             result.n = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[0]);
    +952 
    +953             // 3. public key "E"
    +954             if (pubRawRSAHex.substr(a1[1], 2) != "02")
    +955                 throw "malformed RSA key(code:004)"; // 2nd item is not integer
     956 
    -957             return result;
    -958         },
    -959 
    -960         /**
    -961          * parse hexadecimal string of RSA private key
    -962          * @name parsePrivateRawRSAKeyHexAtObj
    -963          * @memberOf PKCS5PKEY
    -964          * @function
    -965          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding RSA private key
    -966          * @return {Array} info associative array to add parsed RSA private key information
    -967          * @since pkcs5pkey 1.0.5
    -968          * @description
    -969          * Following properties are added to associative array 'info'
    -970          * <ul>
    -971          * <li>n - hexadecimal string of public key
    -972          * <li>e - hexadecimal string of public exponent
    -973          * <li>d - hexadecimal string of private key
    -974          * <li>p - hexadecimal string
    -975          * <li>q - hexadecimal string
    -976          * <li>dp - hexadecimal string
    -977          * <li>dq - hexadecimal string
    -978          * <li>co - hexadecimal string
    -979          * </ul>
    -980          */
    -981         parsePrivateRawRSAKeyHexAtObj: function(pkcs8PrvHex, info) {
    -982             var keyIdx = info.keyidx;
    -983             
    -984             // 1. sequence
    -985             if (pkcs8PrvHex.substr(keyIdx, 2) != "30")
    -986                 throw "malformed RSA private key(code:001)"; // not sequence
    -987 
    -988             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, keyIdx);
    -989             if (a1.length != 9)
    -990                 throw "malformed RSA private key(code:002)"; // not sequence
    -991 
    -992             // 2. RSA key
    -993             info.key = {};
    -994             info.key.n = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[1]);
    -995             info.key.e = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[2]);
    -996             info.key.d = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[3]);
    -997             info.key.p = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[4]);
    -998             info.key.q = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[5]);
    -999             info.key.dp = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[6]);
    -1000             info.key.dq = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[7]);
    -1001             info.key.co = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[8]);
    -1002         },
    -1003 
    -1004         /**
    -1005          * parse hexadecimal string of ECC private key
    -1006          * @name parsePrivateRawECKeyHexAtObj
    -1007          * @memberOf PKCS5PKEY
    -1008          * @function
    -1009          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding EC private key
    -1010          * @return {Array} info associative array to add parsed ECC private key information
    -1011          * @since pkcs5pkey 1.0.5
    -1012          * @description
    -1013          * Following properties are added to associative array 'info'
    -1014          * <ul>
    -1015          * <li>key - hexadecimal string of ECC private key
    -1016          * </ul>
    -1017          */
    -1018         parsePrivateRawECKeyHexAtObj: function(pkcs8PrvHex, info) {
    -1019             var keyIdx = info.keyidx;
    -1020             
    -1021             // 1. sequence
    -1022             if (pkcs8PrvHex.substr(keyIdx, 2) != "30")
    -1023                 throw "malformed ECC private key(code:001)"; // not sequence
    -1024 
    -1025             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, keyIdx);
    -1026             if (a1.length != 3)
    -1027                 throw "malformed ECC private key(code:002)"; // not sequence
    -1028 
    -1029             // 2. EC private key
    -1030             if (pkcs8PrvHex.substr(a1[1], 2) != "04")
    -1031                 throw "malformed ECC private key(code:003)"; // not octetstring
    -1032 
    -1033             info.key = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[1]);
    -1034         },
    -1035 
    -1036         /**
    -1037          * parse hexadecimal string of PKCS#8 public key
    -1038          * @name parsePublicPKCS8Hex
    -1039          * @memberOf PKCS5PKEY
    -1040          * @function
    -1041          * @param {String} pkcs8PubHex hexadecimal string of PKCS#8 public key
    -1042          * @return {Hash} hash of key information
    -1043          * @description
    -1044          * Resulted hash has following attributes.
    -1045          * <ul>
    -1046          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    -1047          * <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
    -1048          * <li>key - hexadecimal string of public key</li>
    -1049          * </ul>
    -1050          */
    -1051         parsePublicPKCS8Hex: function(pkcs8PubHex) {
    -1052             var result = {};
    -1053             result.algparam = null;
    -1054 
    -1055             // 1. AlgID and Key bit string
    -1056             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    -1057             if (a1.length != 2)
    -1058                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    -1059 
    -1060             // 2. AlgID
    -1061             var idxAlgIdTLV = a1[0];
    -1062             if (pkcs8PubHex.substr(idxAlgIdTLV, 2) != "30")
    -1063                 throw "malformed PKCS8 public key(code:001)"; // AlgId not sequence
    -1064 
    -1065             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxAlgIdTLV);
    -1066             if (a2.length != 2)
    -1067                 throw "malformed PKCS8 public key(code:002)"; // AlgId not have two elements
    -1068 
    -1069             // 2.1. AlgID OID
    -1070             if (pkcs8PubHex.substr(a2[0], 2) != "06")
    -1071                 throw "malformed PKCS8 public key(code:003)"; // AlgId.oid is not OID
    -1072 
    -1073             result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]);
    +957             result.e = ASN1HEX.getHexOfV_AtObj(pubRawRSAHex, a1[1]);
    +958 
    +959             return result;
    +960         },
    +961 
    +962         /**
    +963          * parse hexadecimal string of RSA private key
    +964          * @name parsePrivateRawRSAKeyHexAtObj
    +965          * @memberOf PKCS5PKEY
    +966          * @function
    +967          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding RSA private key
    +968          * @return {Array} info associative array to add parsed RSA private key information
    +969          * @since pkcs5pkey 1.0.5
    +970          * @description
    +971          * Following properties are added to associative array 'info'
    +972          * <ul>
    +973          * <li>n - hexadecimal string of public key
    +974          * <li>e - hexadecimal string of public exponent
    +975          * <li>d - hexadecimal string of private key
    +976          * <li>p - hexadecimal string
    +977          * <li>q - hexadecimal string
    +978          * <li>dp - hexadecimal string
    +979          * <li>dq - hexadecimal string
    +980          * <li>co - hexadecimal string
    +981          * </ul>
    +982          */
    +983         parsePrivateRawRSAKeyHexAtObj: function(pkcs8PrvHex, info) {
    +984             var keyIdx = info.keyidx;
    +985             
    +986             // 1. sequence
    +987             if (pkcs8PrvHex.substr(keyIdx, 2) != "30")
    +988                 throw "malformed RSA private key(code:001)"; // not sequence
    +989 
    +990             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, keyIdx);
    +991             if (a1.length != 9)
    +992                 throw "malformed RSA private key(code:002)"; // not sequence
    +993 
    +994             // 2. RSA key
    +995             info.key = {};
    +996             info.key.n = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[1]);
    +997             info.key.e = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[2]);
    +998             info.key.d = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[3]);
    +999             info.key.p = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[4]);
    +1000             info.key.q = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[5]);
    +1001             info.key.dp = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[6]);
    +1002             info.key.dq = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[7]);
    +1003             info.key.co = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[8]);
    +1004         },
    +1005 
    +1006         /**
    +1007          * parse hexadecimal string of ECC private key
    +1008          * @name parsePrivateRawECKeyHexAtObj
    +1009          * @memberOf PKCS5PKEY
    +1010          * @function
    +1011          * @param {String} pkcs8PrvHex hexadecimal string of PKCS#8 private key concluding EC private key
    +1012          * @return {Array} info associative array to add parsed ECC private key information
    +1013          * @since pkcs5pkey 1.0.5
    +1014          * @description
    +1015          * Following properties are added to associative array 'info'
    +1016          * <ul>
    +1017          * <li>key - hexadecimal string of ECC private key
    +1018          * </ul>
    +1019          */
    +1020         parsePrivateRawECKeyHexAtObj: function(pkcs8PrvHex, info) {
    +1021             var keyIdx = info.keyidx;
    +1022             
    +1023             // 1. sequence
    +1024             if (pkcs8PrvHex.substr(keyIdx, 2) != "30")
    +1025                 throw "malformed ECC private key(code:001)"; // not sequence
    +1026 
    +1027             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PrvHex, keyIdx);
    +1028             if (a1.length != 3)
    +1029                 throw "malformed ECC private key(code:002)"; // not sequence
    +1030 
    +1031             // 2. EC private key
    +1032             if (pkcs8PrvHex.substr(a1[1], 2) != "04")
    +1033                 throw "malformed ECC private key(code:003)"; // not octetstring
    +1034 
    +1035             info.key = ASN1HEX.getHexOfV_AtObj(pkcs8PrvHex, a1[1]);
    +1036         },
    +1037 
    +1038         /**
    +1039          * parse hexadecimal string of PKCS#8 public key
    +1040          * @name parsePublicPKCS8Hex
    +1041          * @memberOf PKCS5PKEY
    +1042          * @function
    +1043          * @param {String} pkcs8PubHex hexadecimal string of PKCS#8 public key
    +1044          * @return {Hash} hash of key information
    +1045          * @description
    +1046          * Resulted hash has following attributes.
    +1047          * <ul>
    +1048          * <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
    +1049          * <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
    +1050          * <li>key - hexadecimal string of public key</li>
    +1051          * </ul>
    +1052          */
    +1053         parsePublicPKCS8Hex: function(pkcs8PubHex) {
    +1054             var result = {};
    +1055             result.algparam = null;
    +1056 
    +1057             // 1. AlgID and Key bit string
    +1058             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    +1059             if (a1.length != 2)
    +1060                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    +1061 
    +1062             // 2. AlgID
    +1063             var idxAlgIdTLV = a1[0];
    +1064             if (pkcs8PubHex.substr(idxAlgIdTLV, 2) != "30")
    +1065                 throw "malformed PKCS8 public key(code:001)"; // AlgId not sequence
    +1066 
    +1067             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxAlgIdTLV);
    +1068             if (a2.length != 2)
    +1069                 throw "malformed PKCS8 public key(code:002)"; // AlgId not have two elements
    +1070 
    +1071             // 2.1. AlgID OID
    +1072             if (pkcs8PubHex.substr(a2[0], 2) != "06")
    +1073                 throw "malformed PKCS8 public key(code:003)"; // AlgId.oid is not OID
     1074 
    -1075             // 2.2. AlgID param
    -1076             if (pkcs8PubHex.substr(a2[1], 2) == "06") {
    -1077                 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]);
    -1078             }
    -1079 
    -1080             // 3. Key
    -1081             if (pkcs8PubHex.substr(a1[1], 2) != "03")
    -1082                 throw "malformed PKCS8 public key(code:004)"; // Key is not bit string
    -1083 
    -1084             result.key = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a1[1]).substr(2);
    -1085             
    -1086             // 4. return result assoc array
    -1087             return result;
    -1088         },
    -1089 
    -1090         /**
    -1091          * provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object
    -1092          * @name getRSAKeyFromPublicPKCS8Hex
    -1093          * @memberOf PKCS5PKEY
    -1094          * @function
    -1095          * @param {String} pkcs8PubHex hexadecimal string of unencrypted PKCS#8 public key
    -1096          * @return {RSAKey} loaded RSAKey object of RSA public key
    -1097          * @since pkcs5pkey 1.0.4
    -1098          */
    -1099         getRSAKeyFromPublicPKCS8Hex: function(pkcs8PubHex) {
    -1100             var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0);
    -1101             if (a1.length != 2)
    -1102                 throw "outer DERSequence shall have 2 elements: " + a1.length;
    -1103 
    -1104             var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(pkcs8PubHex, a1[0]);
    -1105             if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption
    -1106                 throw "PKCS8 AlgorithmId is not rsaEncryption";
    -1107             
    -1108             if (pkcs8PubHex.substr(a1[1], 2) != "03")
    -1109                 throw "PKCS8 Public Key is not BITSTRING encapslated.";
    -1110 
    -1111             var idxPub = ASN1HEX.getStartPosOfV_AtObj(pkcs8PubHex, a1[1]) + 2; // 2 for unused bit
    -1112             
    -1113             if (pkcs8PubHex.substr(idxPub, 2) != "30")
    -1114                 throw "PKCS8 Public Key is not SEQUENCE.";
    -1115 
    -1116             var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxPub);
    -1117             if (a2.length != 2)
    -1118                 throw "inner DERSequence shall have 2 elements: " + a2.length;
    -1119 
    -1120             if (pkcs8PubHex.substr(a2[0], 2) != "02") 
    -1121                 throw "N is not ASN.1 INTEGER";
    -1122             if (pkcs8PubHex.substr(a2[1], 2) != "02") 
    -1123                 throw "E is not ASN.1 INTEGER";
    -1124             
    -1125             var hN = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]);
    -1126             var hE = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]);
    -1127 
    -1128             var pubKey = new RSAKey();
    -1129             pubKey.setPublic(hN, hE);
    -1130             
    -1131             return pubKey;
    -1132         },
    -1133     };
    -1134 }();
    -1135 
    \ No newline at end of file +1075
    result.algoid = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]); +1076 +1077 // 2.2. AlgID param +1078 if (pkcs8PubHex.substr(a2[1], 2) == "06") { +1079 result.algparam = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]); +1080 } +1081 +1082 // 3. Key +1083 if (pkcs8PubHex.substr(a1[1], 2) != "03") +1084 throw "malformed PKCS8 public key(code:004)"; // Key is not bit string +1085 +1086 result.key = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a1[1]).substr(2); +1087 +1088 // 4. return result assoc array +1089 return result; +1090 }, +1091 +1092 /** +1093 * provide hexadecimal string of unencrypted PKCS#8 private key and returns RSAKey object +1094 * @name getRSAKeyFromPublicPKCS8Hex +1095 * @memberOf PKCS5PKEY +1096 * @function +1097 * @param {String} pkcs8PubHex hexadecimal string of unencrypted PKCS#8 public key +1098 * @return {RSAKey} loaded RSAKey object of RSA public key +1099 * @since pkcs5pkey 1.0.4 +1100 */ +1101 getRSAKeyFromPublicPKCS8Hex: function(pkcs8PubHex) { +1102 var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, 0); +1103 if (a1.length != 2) +1104 throw "outer DERSequence shall have 2 elements: " + a1.length; +1105 +1106 var algIdTLV =ASN1HEX.getHexOfTLV_AtObj(pkcs8PubHex, a1[0]); +1107 if (algIdTLV != "300d06092a864886f70d0101010500") // AlgId rsaEncryption +1108 throw "PKCS8 AlgorithmId is not rsaEncryption"; +1109 +1110 if (pkcs8PubHex.substr(a1[1], 2) != "03") +1111 throw "PKCS8 Public Key is not BITSTRING encapslated."; +1112 +1113 var idxPub = ASN1HEX.getStartPosOfV_AtObj(pkcs8PubHex, a1[1]) + 2; // 2 for unused bit +1114 +1115 if (pkcs8PubHex.substr(idxPub, 2) != "30") +1116 throw "PKCS8 Public Key is not SEQUENCE."; +1117 +1118 var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(pkcs8PubHex, idxPub); +1119 if (a2.length != 2) +1120 throw "inner DERSequence shall have 2 elements: " + a2.length; +1121 +1122 if (pkcs8PubHex.substr(a2[0], 2) != "02") +1123 throw "N is not ASN.1 INTEGER"; +1124 if (pkcs8PubHex.substr(a2[1], 2) != "02") +1125 throw "E is not ASN.1 INTEGER"; +1126 +1127 var hN = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[0]); +1128 var hE = ASN1HEX.getHexOfV_AtObj(pkcs8PubHex, a2[1]); +1129 +1130 var pubKey = new RSAKey(); +1131 pubKey.setPublic(hN, hE); +1132 +1133 return pubKey; +1134 }, +1135 }; +1136 }(); +1137
    \ No newline at end of file diff --git a/asn1x509-1.0.js b/asn1x509-1.0.js index 5ba3a846..eb83378b 100644 --- a/asn1x509-1.0.js +++ b/asn1x509-1.0.js @@ -1,9 +1,9 @@ -/*! asn1x509-1.0.14.js (c) 2013-2015 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! asn1x509-1.0.15.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* * asn1x509.js - ASN.1 DER encoder classes for X.509 certificate * - * Copyright (c) 2013-2015 Kenji Urushima (kenji.urushima@gmail.com) + * Copyright (c) 2013-2016 Kenji Urushima (kenji.urushima@gmail.com) * * This software is licensed under the terms of the MIT License. * http://kjur.github.com/jsrsasign/license @@ -16,7 +16,7 @@ * @fileOverview * @name asn1x509-1.0.js * @author Kenji Urushima kenji.urushima@gmail.com - * @version 1.0.14 (2016-May-10) + * @version 1.0.15 (2016-Oct-08) * @since jsrsasign 2.1 * @license MIT License */ diff --git a/bower.json b/bower.json index 351abea0..75d6d6a7 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "kjur-jsrsasign", - "version": "6.1.1", + "version": "6.1.2", "main": "jsrsasign-latest-all-min.js", "description": "The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES, JWS and JWT in pure JavaScript.", "license": "MIT", diff --git a/crypto-1.1.js b/crypto-1.1.js index 7c6bd5ac..32c62d89 100644 --- a/crypto-1.1.js +++ b/crypto-1.1.js @@ -1,4 +1,4 @@ -/*! crypto-1.1.8.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! crypto-1.1.9.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* * crypto.js - Cryptographic Algorithm Provider class @@ -16,7 +16,7 @@ * @fileOverview * @name crypto-1.1.js * @author Kenji Urushima kenji.urushima@gmail.com - * @version 1.1.8 (2016-Feb-28) + * @version 1.1.9 (2016-Oct-08) * @since jsrsasign 2.2 * @license MIT License */ diff --git a/jsrsasign-latest-all-min.js b/jsrsasign-latest-all-min.js index 178a9f1c..7add602e 100644 --- a/jsrsasign-latest-all-min.js +++ b/jsrsasign-latest-all-min.js @@ -1,5 +1,5 @@ /* - * jsrsasign 6.1.1 (2016-09-25) (c) 2010-2016 Kenji Urushima | kjur.github.com/jsrsasign/license + * jsrsasign 6.1.2 (2016-10-08) (c) 2010-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* @@ -230,12 +230,12 @@ var jsonParse=(function(){var e="(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][ /*! asn1-1.0.11.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.asn1=="undefined"||!KJUR.asn1){KJUR.asn1={}}KJUR.asn1.ASN1Util=new function(){this.integerToByteHex=function(a){var b=a.toString(16);if((b.length%2)==1){b="0"+b}return b};this.bigIntToMinTwosComplementsHex=function(j){var f=j.toString(16);if(f.substr(0,1)!="-"){if(f.length%2==1){f="0"+f}else{if(!f.match(/^[0-7]/)){f="00"+f}}}else{var a=f.substr(1);var e=a.length;if(e%2==1){e+=1}else{if(!f.match(/^[0-7]/)){e+=2}}var g="";for(var d=0;d15){throw"ASN.1 length too long to represent by 8x: n = "+i.toString(16)}var f=128+g;return f.toString(16)+h}};this.getEncodedHex=function(){if(this.hTLV==null||this.isModified){this.hV=this.getFreshValueHex();this.hL=this.getLengthHexFromValue();this.hTLV=this.hT+this.hL+this.hV;this.isModified=false}return this.hTLV};this.getValueHex=function(){this.getEncodedHex();return this.hV};this.getFreshValueHex=function(){return""}};KJUR.asn1.DERAbstractString=function(c){KJUR.asn1.DERAbstractString.superclass.constructor.call(this);var b=null;var a=null;this.getString=function(){return this.s};this.setString=function(d){this.hTLV=null;this.isModified=true;this.s=d;this.hV=stohex(this.s)};this.setStringHex=function(d){this.hTLV=null;this.isModified=true;this.s=null;this.hV=d};this.getFreshValueHex=function(){return this.hV};if(typeof c!="undefined"){if(typeof c=="string"){this.setString(c)}else{if(typeof c.str!="undefined"){this.setString(c.str)}else{if(typeof c.hex!="undefined"){this.setStringHex(c.hex)}}}}};YAHOO.lang.extend(KJUR.asn1.DERAbstractString,KJUR.asn1.ASN1Object);KJUR.asn1.DERAbstractTime=function(c){KJUR.asn1.DERAbstractTime.superclass.constructor.call(this);var b=null;var a=null;this.localDateToUTC=function(f){utc=f.getTime()+(f.getTimezoneOffset()*60000);var e=new Date(utc);return e};this.formatDate=function(m,o,e){var g=this.zeroPadding;var n=this.localDateToUTC(m);var p=String(n.getFullYear());if(o=="utc"){p=p.substr(2,2)}var l=g(String(n.getMonth()+1),2);var q=g(String(n.getDate()),2);var h=g(String(n.getHours()),2);var i=g(String(n.getMinutes()),2);var j=g(String(n.getSeconds()),2);var r=p+l+q+h+i+j;if(e===true){var f=n.getMilliseconds();if(f!=0){var k=g(String(f),3);k=k.replace(/[0]+$/,"");r=r+"."+k}}return r+"Z"};this.zeroPadding=function(e,d){if(e.length>=d){return e}return new Array(d-e.length+1).join("0")+e};this.getString=function(){return this.s};this.setString=function(d){this.hTLV=null;this.isModified=true;this.s=d;this.hV=stohex(d)};this.setByDateValue=function(h,j,e,d,f,g){var i=new Date(Date.UTC(h,j-1,e,d,f,g,0));this.setByDate(i)};this.getFreshValueHex=function(){return this.hV}};YAHOO.lang.extend(KJUR.asn1.DERAbstractTime,KJUR.asn1.ASN1Object);KJUR.asn1.DERAbstractStructured=function(b){KJUR.asn1.DERAbstractString.superclass.constructor.call(this);var a=null;this.setByASN1ObjectArray=function(c){this.hTLV=null;this.isModified=true;this.asn1Array=c};this.appendASN1Object=function(c){this.hTLV=null;this.isModified=true;this.asn1Array.push(c)};this.asn1Array=new Array();if(typeof b!="undefined"){if(typeof b.array!="undefined"){this.asn1Array=b.array}}};YAHOO.lang.extend(KJUR.asn1.DERAbstractStructured,KJUR.asn1.ASN1Object);KJUR.asn1.DERBoolean=function(){KJUR.asn1.DERBoolean.superclass.constructor.call(this);this.hT="01";this.hTLV="0101ff"};YAHOO.lang.extend(KJUR.asn1.DERBoolean,KJUR.asn1.ASN1Object);KJUR.asn1.DERInteger=function(a){KJUR.asn1.DERInteger.superclass.constructor.call(this);this.hT="02";this.setByBigInteger=function(b){this.hTLV=null;this.isModified=true;this.hV=KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(b)};this.setByInteger=function(c){var b=new BigInteger(String(c),10);this.setByBigInteger(b)};this.setValueHex=function(b){this.hV=b};this.getFreshValueHex=function(){return this.hV};if(typeof a!="undefined"){if(typeof a.bigint!="undefined"){this.setByBigInteger(a.bigint)}else{if(typeof a["int"]!="undefined"){this.setByInteger(a["int"])}else{if(typeof a=="number"){this.setByInteger(a)}else{if(typeof a.hex!="undefined"){this.setValueHex(a.hex)}}}}}};YAHOO.lang.extend(KJUR.asn1.DERInteger,KJUR.asn1.ASN1Object);KJUR.asn1.DERBitString=function(b){if(b!==undefined&&typeof b.obj!=="undefined"){var a=KJUR.asn1.ASN1Util.newObject(b.obj);b.hex="00"+a.getEncodedHex()}KJUR.asn1.DERBitString.superclass.constructor.call(this);this.hT="03";this.setHexValueIncludingUnusedBits=function(c){this.hTLV=null;this.isModified=true;this.hV=c};this.setUnusedBitsAndHexValue=function(c,e){if(c<0||7=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c};this.getNthChildIndex_AtObj=function(d,b,e){var c=this.getPosArrayOfChildren_AtObj(d,b);return c[e]};this.getDecendantIndexByNthList=function(e,d,c){if(c.length==0){return d}var f=c.shift();var b=this.getPosArrayOfChildren_AtObj(e,d);return this.getDecendantIndexByNthList(e,b[f],c)};this.getDecendantHexTLVByNthList=function(d,c,b){var a=this.getDecendantIndexByNthList(d,c,b);return this.getHexOfTLV_AtObj(d,a)};this.getDecendantHexVByNthList=function(d,c,b){var a=this.getDecendantIndexByNthList(d,c,b);return this.getHexOfV_AtObj(d,a)}};ASN1HEX.getVbyList=function(d,c,b,e){var a=this.getDecendantIndexByNthList(d,c,b);if(a===undefined){throw"can't find nthList object"}if(e!==undefined){if(d.substr(a,2)!=e){throw"checking tag doesn't match: "+d.substr(a,2)+"!="+e}}return this.getHexOfV_AtObj(d,a)};ASN1HEX.hextooidstr=function(e){var h=function(b,a){if(b.length>=a){return b}return new Array(a-b.length+1).join("0")+b};var l=[];var o=e.substr(0,2);var f=parseInt(o,16);l[0]=new String(Math.floor(f/40));l[1]=new String(f%40);var m=e.substr(2);var k=[];for(var g=0;g0){n=n+"."+j.join(".")}return n};ASN1HEX.dump=function(e,c,k,g){var o=function(w,i){if(w.length<=i*2){return w}else{var v=w.substr(0,i)+"..(total "+w.length/2+"bytes).."+w.substr(w.length-i,i);return v}};if(c===undefined){c={ommit_long_octet:32}}if(k===undefined){k=0}if(g===undefined){g=""}var r=c.ommit_long_octet;if(e.substr(k,2)=="01"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(h=="00"){return g+"BOOLEAN FALSE\n"}else{return g+"BOOLEAN TRUE\n"}}if(e.substr(k,2)=="02"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"INTEGER "+o(h,r)+"\n"}if(e.substr(k,2)=="03"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"BITSTRING "+o(h,r)+"\n"}if(e.substr(k,2)=="04"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(ASN1HEX.isASN1HEX(h)){var j=g+"OCTETSTRING, encapsulates\n";j=j+ASN1HEX.dump(h,c,0,g+" ");return j}else{return g+"OCTETSTRING "+o(h,r)+"\n"}}if(e.substr(k,2)=="05"){return g+"NULL\n"}if(e.substr(k,2)=="06"){var l=ASN1HEX.getHexOfV_AtObj(e,k);var a=KJUR.asn1.ASN1Util.oidHexToInt(l);var n=KJUR.asn1.x509.OID.oid2name(a);var b=a.replace(/\./g," ");if(n!=""){return g+"ObjectIdentifier "+n+" ("+b+")\n"}else{return g+"ObjectIdentifier ("+b+")\n"}}if(e.substr(k,2)=="0c"){return g+"UTF8String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="13"){return g+"PrintableString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="14"){return g+"TeletexString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="16"){return g+"IA5String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="17"){return g+"UTCTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="18"){return g+"GeneralizedTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="30"){if(e.substr(k,4)=="3000"){return g+"SEQUENCE {}\n"}var j=g+"SEQUENCE\n";var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,k);var f=c;if((d.length==2||d.length==3)&&e.substr(d[0],2)=="06"&&e.substr(d[d.length-1],2)=="04"){var t=ASN1HEX.getHexOfV_AtObj(e,d[0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(t);var n=KJUR.asn1.x509.OID.oid2name(a);var p=JSON.parse(JSON.stringify(c));p.x509ExtName=n;f=p}for(var q=0;q=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c};ASN1HEX.getNthChildIndex_AtObj=function(d,b,e){var c=ASN1HEX.getPosArrayOfChildren_AtObj(d,b);return c[e]};ASN1HEX.getDecendantIndexByNthList=function(e,d,c){if(c.length==0){return d}var f=c.shift();var b=ASN1HEX.getPosArrayOfChildren_AtObj(e,d);return ASN1HEX.getDecendantIndexByNthList(e,b[f],c)};ASN1HEX.getDecendantHexTLVByNthList=function(d,c,b){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);return ASN1HEX.getHexOfTLV_AtObj(d,a)};ASN1HEX.getDecendantHexVByNthList=function(d,c,b){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);return ASN1HEX.getHexOfV_AtObj(d,a)};ASN1HEX.getVbyList=function(d,c,b,e){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);if(a===undefined){throw"can't find nthList object"}if(e!==undefined){if(d.substr(a,2)!=e){throw"checking tag doesn't match: "+d.substr(a,2)+"!="+e}}return ASN1HEX.getHexOfV_AtObj(d,a)};ASN1HEX.hextooidstr=function(e){var h=function(b,a){if(b.length>=a){return b}return new Array(a-b.length+1).join("0")+b};var l=[];var o=e.substr(0,2);var f=parseInt(o,16);l[0]=new String(Math.floor(f/40));l[1]=new String(f%40);var m=e.substr(2);var k=[];for(var g=0;g0){n=n+"."+j.join(".")}return n};ASN1HEX.dump=function(e,c,k,g){var o=function(w,i){if(w.length<=i*2){return w}else{var v=w.substr(0,i)+"..(total "+w.length/2+"bytes).."+w.substr(w.length-i,i);return v}};if(c===undefined){c={ommit_long_octet:32}}if(k===undefined){k=0}if(g===undefined){g=""}var r=c.ommit_long_octet;if(e.substr(k,2)=="01"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(h=="00"){return g+"BOOLEAN FALSE\n"}else{return g+"BOOLEAN TRUE\n"}}if(e.substr(k,2)=="02"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"INTEGER "+o(h,r)+"\n"}if(e.substr(k,2)=="03"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"BITSTRING "+o(h,r)+"\n"}if(e.substr(k,2)=="04"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(ASN1HEX.isASN1HEX(h)){var j=g+"OCTETSTRING, encapsulates\n";j=j+ASN1HEX.dump(h,c,0,g+" ");return j}else{return g+"OCTETSTRING "+o(h,r)+"\n"}}if(e.substr(k,2)=="05"){return g+"NULL\n"}if(e.substr(k,2)=="06"){var l=ASN1HEX.getHexOfV_AtObj(e,k);var a=KJUR.asn1.ASN1Util.oidHexToInt(l);var n=KJUR.asn1.x509.OID.oid2name(a);var b=a.replace(/\./g," ");if(n!=""){return g+"ObjectIdentifier "+n+" ("+b+")\n"}else{return g+"ObjectIdentifier ("+b+")\n"}}if(e.substr(k,2)=="0c"){return g+"UTF8String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="13"){return g+"PrintableString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="14"){return g+"TeletexString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="16"){return g+"IA5String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="17"){return g+"UTCTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="18"){return g+"GeneralizedTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="30"){if(e.substr(k,4)=="3000"){return g+"SEQUENCE {}\n"}var j=g+"SEQUENCE\n";var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,k);var f=c;if((d.length==2||d.length==3)&&e.substr(d[0],2)=="06"&&e.substr(d[d.length-1],2)=="04"){var t=ASN1HEX.getHexOfV_AtObj(e,d[0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(t);var n=KJUR.asn1.x509.OID.oid2name(a);var p=JSON.parse(JSON.stringify(c));p.x509ExtName=n;f=p}for(var q=0;q0){var d=new KJUR.asn1.DERSequence({array:this.extensionsArray});var b=new KJUR.asn1.DERTaggedObject({explicit:true,tag:"a3",obj:d});this.asn1Array.push(b)}var e=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=e.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertificate,KJUR.asn1.ASN1Object);KJUR.asn1.x509.Extension=function(b){KJUR.asn1.x509.Extension.superclass.constructor.call(this);var a=null;this.getEncodedHex=function(){var f=new KJUR.asn1.DERObjectIdentifier({oid:this.oid});var e=new KJUR.asn1.DEROctetString({hex:this.getExtnValueHex()});var d=new Array();d.push(f);if(this.critical){d.push(new KJUR.asn1.DERBoolean())}d.push(e);var c=new KJUR.asn1.DERSequence({array:d});return c.getEncodedHex()};this.critical=false;if(typeof b!="undefined"){if(typeof b.critical!="undefined"){this.critical=b.critical}}};YAHOO.lang.extend(KJUR.asn1.x509.Extension,KJUR.asn1.ASN1Object);KJUR.asn1.x509.KeyUsage=function(a){KJUR.asn1.x509.KeyUsage.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.15";if(typeof a!="undefined"){if(typeof a.bin!="undefined"){this.asn1ExtnValue=new KJUR.asn1.DERBitString(a)}}};YAHOO.lang.extend(KJUR.asn1.x509.KeyUsage,KJUR.asn1.x509.Extension);KJUR.asn1.x509.BasicConstraints=function(c){KJUR.asn1.x509.BasicConstraints.superclass.constructor.call(this,c);var a=false;var b=-1;this.getExtnValueHex=function(){var e=new Array();if(this.cA){e.push(new KJUR.asn1.DERBoolean())}if(this.pathLen>-1){e.push(new KJUR.asn1.DERInteger({"int":this.pathLen}))}var d=new KJUR.asn1.DERSequence({array:e});this.asn1ExtnValue=d;return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.19";this.cA=false;this.pathLen=-1;if(typeof c!="undefined"){if(typeof c.cA!="undefined"){this.cA=c.cA}if(typeof c.pathLen!="undefined"){this.pathLen=c.pathLen}}};YAHOO.lang.extend(KJUR.asn1.x509.BasicConstraints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.CRLDistributionPoints=function(a){KJUR.asn1.x509.CRLDistributionPoints.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.setByDPArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence({array:b})};this.setByOneURI=function(e){var b=new KJUR.asn1.x509.GeneralNames([{uri:e}]);var d=new KJUR.asn1.x509.DistributionPointName(b);var c=new KJUR.asn1.x509.DistributionPoint({dpobj:d});this.setByDPArray([c])};this.oid="2.5.29.31";if(typeof a!="undefined"){if(typeof a.array!="undefined"){this.setByDPArray(a.array)}else{if(typeof a.uri!="undefined"){this.setByOneURI(a.uri)}}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLDistributionPoints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.ExtKeyUsage=function(a){KJUR.asn1.x509.ExtKeyUsage.superclass.constructor.call(this,a);this.setPurposeArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence();for(var c=0;c0){var c=new KJUR.asn1.DERSequence({array:this.aRevokedCert});this.asn1Array.push(c)}var d=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=d.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize=function(){this.asn1Version=null;this.asn1SignatureAlg=null;this.asn1Issuer=null;this.asn1ThisUpdate=null;this.asn1NextUpdate=null;this.aRevokedCert=new Array()};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertList,KJUR.asn1.ASN1Object);KJUR.asn1.x509.CRLEntry=function(c){KJUR.asn1.x509.CRLEntry.superclass.constructor.call(this);var b=null;var a=null;this.setCertSerial=function(d){this.sn=new KJUR.asn1.DERInteger(d)};this.setRevocationDate=function(d){this.time=new KJUR.asn1.x509.Time(d)};this.getEncodedHex=function(){var d=new KJUR.asn1.DERSequence({array:[this.sn,this.time]});this.TLV=d.getEncodedHex();return this.TLV};if(typeof c!="undefined"){if(typeof c.time!="undefined"){this.setRevocationDate(c.time)}if(typeof c.sn!="undefined"){this.setCertSerial(c.sn)}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLEntry,KJUR.asn1.ASN1Object);KJUR.asn1.x509.X500Name=function(b){KJUR.asn1.x509.X500Name.superclass.constructor.call(this);this.asn1Array=new Array();this.setByString=function(c){var d=c.split("/");d.shift();for(var e=0;e0){var d=new KJUR.asn1.DERSequence({array:this.extensionsArray});var b=new KJUR.asn1.DERTaggedObject({explicit:true,tag:"a3",obj:d});this.asn1Array.push(b)}var e=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=e.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertificate,KJUR.asn1.ASN1Object);KJUR.asn1.x509.Extension=function(b){KJUR.asn1.x509.Extension.superclass.constructor.call(this);var a=null;this.getEncodedHex=function(){var f=new KJUR.asn1.DERObjectIdentifier({oid:this.oid});var e=new KJUR.asn1.DEROctetString({hex:this.getExtnValueHex()});var d=new Array();d.push(f);if(this.critical){d.push(new KJUR.asn1.DERBoolean())}d.push(e);var c=new KJUR.asn1.DERSequence({array:d});return c.getEncodedHex()};this.critical=false;if(typeof b!="undefined"){if(typeof b.critical!="undefined"){this.critical=b.critical}}};YAHOO.lang.extend(KJUR.asn1.x509.Extension,KJUR.asn1.ASN1Object);KJUR.asn1.x509.KeyUsage=function(a){KJUR.asn1.x509.KeyUsage.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.15";if(typeof a!="undefined"){if(typeof a.bin!="undefined"){this.asn1ExtnValue=new KJUR.asn1.DERBitString(a)}}};YAHOO.lang.extend(KJUR.asn1.x509.KeyUsage,KJUR.asn1.x509.Extension);KJUR.asn1.x509.BasicConstraints=function(c){KJUR.asn1.x509.BasicConstraints.superclass.constructor.call(this,c);var a=false;var b=-1;this.getExtnValueHex=function(){var e=new Array();if(this.cA){e.push(new KJUR.asn1.DERBoolean())}if(this.pathLen>-1){e.push(new KJUR.asn1.DERInteger({"int":this.pathLen}))}var d=new KJUR.asn1.DERSequence({array:e});this.asn1ExtnValue=d;return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.19";this.cA=false;this.pathLen=-1;if(typeof c!="undefined"){if(typeof c.cA!="undefined"){this.cA=c.cA}if(typeof c.pathLen!="undefined"){this.pathLen=c.pathLen}}};YAHOO.lang.extend(KJUR.asn1.x509.BasicConstraints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.CRLDistributionPoints=function(a){KJUR.asn1.x509.CRLDistributionPoints.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.setByDPArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence({array:b})};this.setByOneURI=function(e){var b=new KJUR.asn1.x509.GeneralNames([{uri:e}]);var d=new KJUR.asn1.x509.DistributionPointName(b);var c=new KJUR.asn1.x509.DistributionPoint({dpobj:d});this.setByDPArray([c])};this.oid="2.5.29.31";if(typeof a!="undefined"){if(typeof a.array!="undefined"){this.setByDPArray(a.array)}else{if(typeof a.uri!="undefined"){this.setByOneURI(a.uri)}}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLDistributionPoints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.ExtKeyUsage=function(a){KJUR.asn1.x509.ExtKeyUsage.superclass.constructor.call(this,a);this.setPurposeArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence();for(var c=0;c0){var c=new KJUR.asn1.DERSequence({array:this.aRevokedCert});this.asn1Array.push(c)}var d=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=d.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize=function(){this.asn1Version=null;this.asn1SignatureAlg=null;this.asn1Issuer=null;this.asn1ThisUpdate=null;this.asn1NextUpdate=null;this.aRevokedCert=new Array()};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertList,KJUR.asn1.ASN1Object);KJUR.asn1.x509.CRLEntry=function(c){KJUR.asn1.x509.CRLEntry.superclass.constructor.call(this);var b=null;var a=null;this.setCertSerial=function(d){this.sn=new KJUR.asn1.DERInteger(d)};this.setRevocationDate=function(d){this.time=new KJUR.asn1.x509.Time(d)};this.getEncodedHex=function(){var d=new KJUR.asn1.DERSequence({array:[this.sn,this.time]});this.TLV=d.getEncodedHex();return this.TLV};if(typeof c!="undefined"){if(typeof c.time!="undefined"){this.setRevocationDate(c.time)}if(typeof c.sn!="undefined"){this.setCertSerial(c.sn)}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLEntry,KJUR.asn1.ASN1Object);KJUR.asn1.x509.X500Name=function(b){KJUR.asn1.x509.X500Name.superclass.constructor.call(this);this.asn1Array=new Array();this.setByString=function(c){var d=c.split("/");d.shift();for(var e=0;e0){h=new a.DERTaggedObject({obj:this.dUnsignedAttrs,tag:"a1",explicit:false})}var g=[this.dCMSVersion,this.dSignerIdentifier,this.dDigestAlgorithm,e,this.dSigAlg,this.dSig,];if(h!=null){g.push(h)}var f=new a.DERSequence({array:g});this.hTLV=f.getEncodedHex();return this.hTLV}};YAHOO.lang.extend(KJUR.asn1.cms.SignerInfo,KJUR.asn1.ASN1Object);KJUR.asn1.cms.EncapsulatedContentInfo=function(c){KJUR.asn1.cms.EncapsulatedContentInfo.superclass.constructor.call(this);var a=KJUR.asn1;var b=KJUR.asn1.cms;var d=KJUR.asn1.x509;this.dEContentType=new a.DERObjectIdentifier({name:"data"});this.dEContent=null;this.isDetached=false;this.eContentValueHex=null;this.setContentType=function(e){if(e.match(/^[0-2][.][0-9.]+$/)){this.dEContentType=new a.DERObjectIdentifier({oid:e})}else{this.dEContentType=new a.DERObjectIdentifier({name:e})}};this.setContentValue=function(e){if(typeof e!="undefined"){if(typeof e.hex=="string"){this.eContentValueHex=e.hex}else{if(typeof e.str=="string"){this.eContentValueHex=utf8tohex(e.str)}}}};this.setContentValueHex=function(e){this.eContentValueHex=e};this.setContentValueStr=function(e){this.eContentValueHex=utf8tohex(e)};this.getEncodedHex=function(){if(typeof this.eContentValueHex!="string"){throw"eContentValue not yet set"}var g=new a.DEROctetString({hex:this.eContentValueHex});this.dEContent=new a.DERTaggedObject({obj:g,tag:"a0",explicit:true});var e=[this.dEContentType];if(!this.isDetached){e.push(this.dEContent)}var f=new a.DERSequence({array:e});this.hTLV=f.getEncodedHex();return this.hTLV}};YAHOO.lang.extend(KJUR.asn1.cms.EncapsulatedContentInfo,KJUR.asn1.ASN1Object);KJUR.asn1.cms.ContentInfo=function(c){KJUR.asn1.cms.ContentInfo.superclass.constructor.call(this);var a=KJUR.asn1;var b=KJUR.asn1.cms;var d=KJUR.asn1.x509;this.dContentType=null;this.dContent=null;this.setContentType=function(e){if(typeof e=="string"){this.dContentType=d.OID.name2obj(e)}};this.getEncodedHex=function(){var f=new a.DERTaggedObject({obj:this.dContent,tag:"a0",explicit:true});var e=new a.DERSequence({array:[this.dContentType,f]});this.hTLV=e.getEncodedHex();return this.hTLV};if(typeof c!="undefined"){if(c.type){this.setContentType(c.type)}if(c.obj&&c.obj instanceof a.ASN1Object){this.dContent=c.obj}}};YAHOO.lang.extend(KJUR.asn1.cms.ContentInfo,KJUR.asn1.ASN1Object);KJUR.asn1.cms.SignedData=function(c){KJUR.asn1.cms.SignedData.superclass.constructor.call(this);var a=KJUR.asn1;var b=KJUR.asn1.cms;var d=KJUR.asn1.x509;this.dCMSVersion=new a.DERInteger({"int":1});this.dDigestAlgs=null;this.digestAlgNameList=[];this.dEncapContentInfo=new b.EncapsulatedContentInfo();this.dCerts=null;this.certificateList=[];this.crlList=[];this.signerInfoList=[new b.SignerInfo()];this.addCertificatesByPEM=function(e){var f=KEYUTIL.getHexFromPEM(e);var g=new a.ASN1Object();g.hTLV=f;this.certificateList.push(g)};this.getEncodedHex=function(){if(typeof this.hTLV=="string"){return this.hTLV}if(this.dDigestAlgs==null){var k=[];for(var j=0;j0){var l=new a.DERSet({array:this.certificateList});this.dCerts=new a.DERTaggedObject({obj:l,tag:"a0",explicit:false})}}if(this.dCerts!=null){e.push(this.dCerts)}var g=new a.DERSet({array:this.signerInfoList});e.push(g);var f=new a.DERSequence({array:e});this.hTLV=f.getEncodedHex();return this.hTLV};this.getContentInfo=function(){this.getEncodedHex();var e=new b.ContentInfo({type:"signed-data",obj:this});return e};this.getContentInfoEncodedHex=function(){var e=this.getContentInfo();var f=e.getEncodedHex();return f};this.getPEM=function(){var e=this.getContentInfoEncodedHex();var f=a.ASN1Util.getPEMStringFromHex(e,"CMS");return f}};YAHOO.lang.extend(KJUR.asn1.cms.SignedData,KJUR.asn1.ASN1Object);KJUR.asn1.cms.CMSUtil=new function(){};KJUR.asn1.cms.CMSUtil.newSignedData=function(a){var h=KJUR.asn1.cms;var g=KJUR.asn1.cades;var f=new h.SignedData();f.dEncapContentInfo.setContentValue(a.content);if(typeof a.certs=="object"){for(var b=0;ba.length){d=a.length}for(var b=0;bd){throw"key is too short for SigAlg: keylen="+j+","+a}var b="0001";var k="00"+c;var g="";var l=d-b.length-k.length;for(var f=0;fd){throw"key is too short for SigAlg: keylen="+j+","+a}var b="0001";var k="00"+c;var g="";var l=d-b.length-k.length;for(var f=0;f=0;--p){q=q.twice2D();q.z=BigInteger.ONE;if(o.testBit(p)){if(n.testBit(p)){q=q.add2D(t)}else{q=q.add2D(s)}}else{if(n.testBit(p)){q=q.add2D(r)}}}return q}this.getBigRandom=function(i){return new BigInteger(i.bitLength(),a).mod(i.subtract(BigInteger.ONE)).add(BigInteger.ONE)};this.setNamedCurve=function(i){this.ecparams=KJUR.crypto.ECParameterDB.getByName(i);this.prvKeyHex=null;this.pubKeyHex=null;this.curveName=i};this.setPrivateKeyHex=function(i){this.isPrivate=true;this.prvKeyHex=i};this.setPublicKeyHex=function(i){this.isPublic=true;this.pubKeyHex=i};this.getPublicKeyXYHex=function(){var k=this.pubKeyHex;if(k.substr(0,2)!=="04"){throw"this method supports uncompressed format(04) only"}var j=this.ecparams.keylen/4;if(k.length!==2+j*2){throw"malformed public key hex length"}var i={};i.x=k.substr(2,j);i.y=k.substr(2+j);return i};this.getShortNISTPCurveName=function(){var i=this.curveName;if(i==="secp256r1"||i==="NIST P-256"||i==="P-256"||i==="prime256v1"){return"P-256"}if(i==="secp384r1"||i==="NIST P-384"||i==="P-384"){return"P-384"}return null};this.generateKeyPairHex=function(){var k=this.ecparams.n;var n=this.getBigRandom(k);var l=this.ecparams.G.multiply(n);var q=l.getX().toBigInteger();var o=l.getY().toBigInteger();var i=this.ecparams.keylen/4;var m=("0000000000"+n.toString(16)).slice(-i);var r=("0000000000"+q.toString(16)).slice(-i);var p=("0000000000"+o.toString(16)).slice(-i);var j="04"+r+p;this.setPrivateKeyHex(m);this.setPublicKeyHex(j);return{ecprvhex:m,ecpubhex:j}};this.signWithMessageHash=function(i){return this.signHex(i,this.prvKeyHex)};this.signHex=function(o,j){var t=new BigInteger(j,16);var l=this.ecparams.n;var q=new BigInteger(o,16);do{var m=this.getBigRandom(l);var u=this.ecparams.G;var p=u.multiply(m);var i=p.getX().toBigInteger().mod(l)}while(i.compareTo(BigInteger.ZERO)<=0);var v=m.modInverse(l).multiply(q.add(t.multiply(i))).mod(l);return KJUR.crypto.ECDSA.biRSSigToASN1Sig(i,v)};this.sign=function(m,u){var q=u;var j=this.ecparams.n;var p=BigInteger.fromByteArrayUnsigned(m);do{var l=this.getBigRandom(j);var t=this.ecparams.G;var o=t.multiply(l);var i=o.getX().toBigInteger().mod(j)}while(i.compareTo(BigInteger.ZERO)<=0);var v=l.modInverse(j).multiply(p.add(q.multiply(i))).mod(j);return this.serializeSig(i,v)};this.verifyWithMessageHash=function(j,i){return this.verifyHex(j,i,this.pubKeyHex)};this.verifyHex=function(m,i,p){var l,j;var o=KJUR.crypto.ECDSA.parseSigHex(i);l=o.r;j=o.s;var k;k=ECPointFp.decodeFromHex(this.ecparams.curve,p);var n=new BigInteger(m,16);return this.verifyRaw(n,l,j,k)};this.verify=function(o,p,j){var l,i;if(Bitcoin.Util.isArray(p)){var n=this.parseSig(p);l=n.r;i=n.s}else{if("object"===typeof p&&p.r&&p.s){l=p.r;i=p.s}else{throw"Invalid value for signature"}}var k;if(j instanceof ECPointFp){k=j}else{if(Bitcoin.Util.isArray(j)){k=ECPointFp.decodeFrom(this.ecparams.curve,j)}else{throw"Invalid format for pubkey value, must be byte array or ECPointFp"}}var m=BigInteger.fromByteArrayUnsigned(o);return this.verifyRaw(m,l,i,k)};this.verifyRaw=function(o,i,w,m){var l=this.ecparams.n;var u=this.ecparams.G;if(i.compareTo(BigInteger.ONE)<0||i.compareTo(l)>=0){return false}if(w.compareTo(BigInteger.ONE)<0||w.compareTo(l)>=0){return false}var p=w.modInverse(l);var k=o.multiply(p).mod(l);var j=i.multiply(p).mod(l);var q=u.multiply(k).add(m.multiply(j));var t=q.getX().toBigInteger().mod(l);return t.equals(i)};this.serializeSig=function(k,j){var l=k.toByteArraySigned();var i=j.toByteArraySigned();var m=[];m.push(2);m.push(l.length);m=m.concat(l);m.push(2);m.push(i.length);m=m.concat(i);m.unshift(m.length);m.unshift(48);return m};this.parseSig=function(n){var m;if(n[0]!=48){throw new Error("Signature not a valid DERSequence")}m=2;if(n[m]!=2){throw new Error("First element in signature must be a DERInteger")}var l=n.slice(m+2,m+2+n[m+1]);m+=2+n[m+1];if(n[m]!=2){throw new Error("Second element in signature must be a DERInteger")}var i=n.slice(m+2,m+2+n[m+1]);m+=2+n[m+1];var k=BigInteger.fromByteArrayUnsigned(l);var j=BigInteger.fromByteArrayUnsigned(i);return{r:k,s:j}};this.parseSigCompact=function(m){if(m.length!==65){throw"Signature has the wrong length"}var j=m[0]-27;if(j<0||j>7){throw"Invalid signature type"}var o=this.ecparams.n;var l=BigInteger.fromByteArrayUnsigned(m.slice(1,33)).mod(o);var k=BigInteger.fromByteArrayUnsigned(m.slice(33,65)).mod(o);return{r:l,s:k,i:j}};if(h!==undefined){if(h.curve!==undefined){this.curveName=h.curve}}if(this.curveName===undefined){this.curveName=e}this.setNamedCurve(this.curveName);if(h!==undefined){if(h.prv!==undefined){this.setPrivateKeyHex(h.prv)}if(h.pub!==undefined){this.setPublicKeyHex(h.pub)}}};KJUR.crypto.ECDSA.parseSigHex=function(a){var b=KJUR.crypto.ECDSA.parseSigHexInHexRS(a);var d=new BigInteger(b.r,16);var c=new BigInteger(b.s,16);return{r:d,s:c}};KJUR.crypto.ECDSA.parseSigHexInHexRS=function(c){if(c.substr(0,2)!="30"){throw"signature is not a ASN.1 sequence"}var b=ASN1HEX.getPosArrayOfChildren_AtObj(c,0);if(b.length!=2){throw"number of signature ASN.1 sequence elements seem wrong"}var g=b[0];var f=b[1];if(c.substr(g,2)!="02"){throw"1st item of sequene of signature is not ASN.1 integer"}if(c.substr(f,2)!="02"){throw"2nd item of sequene of signature is not ASN.1 integer"}var e=ASN1HEX.getHexOfV_AtObj(c,g);var d=ASN1HEX.getHexOfV_AtObj(c,f);return{r:e,s:d}};KJUR.crypto.ECDSA.asn1SigToConcatSig=function(c){var d=KJUR.crypto.ECDSA.parseSigHexInHexRS(c);var b=d.r;var a=d.s;if(b.substr(0,2)=="00"&&(((b.length/2)*8)%(16*8))==8){b=b.substr(2)}if(a.substr(0,2)=="00"&&(((a.length/2)*8)%(16*8))==8){a=a.substr(2)}if((((b.length/2)*8)%(16*8))!=0){throw"unknown ECDSA sig r length error"}if((((a.length/2)*8)%(16*8))!=0){throw"unknown ECDSA sig s length error"}return b+a};KJUR.crypto.ECDSA.concatSigToASN1Sig=function(a){if((((a.length/2)*8)%(16*8))!=0){throw"unknown ECDSA concatinated r-s sig length error"}var c=a.substr(0,a.length/2);var b=a.substr(a.length/2);return KJUR.crypto.ECDSA.hexRSSigToASN1Sig(c,b)};KJUR.crypto.ECDSA.hexRSSigToASN1Sig=function(b,a){var d=new BigInteger(b,16);var c=new BigInteger(a,16);return KJUR.crypto.ECDSA.biRSSigToASN1Sig(d,c)};KJUR.crypto.ECDSA.biRSSigToASN1Sig=function(e,c){var b=new KJUR.asn1.DERInteger({bigint:e});var a=new KJUR.asn1.DERInteger({bigint:c});var d=new KJUR.asn1.DERSequence({array:[b,a]});return d.getEncodedHex()}; @@ -266,12 +266,12 @@ if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.crypto=="undefined"|| /*! dsa-modified-1.0.1.js (c) Recurity Labs GmbH, Kenji Urushimma | github.com/openpgpjs/openpgpjs/blob/master/LICENSE */ if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.crypto=="undefined"||!KJUR.crypto){KJUR.crypto={}}KJUR.crypto.DSA=function(){this.p=null;this.q=null;this.g=null;this.y=null;this.x=null;this.type="DSA";this.setPrivate=function(z,w,v,A,u){this.isPrivate=true;this.p=z;this.q=w;this.g=v;this.y=A;this.x=u};this.setPublic=function(w,v,u,z){this.isPublic=true;this.p=w;this.q=v;this.g=u;this.y=z;this.x=null};this.signWithMessageHash=function(z){var v=this.p;var u=this.q;var C=this.g;var D=this.y;var E=this.x;var A=z.substr(0,u.bitLength()/4);var B=new BigInteger(z,16);var w=n(BigInteger.ONE.add(BigInteger.ONE),u.subtract(BigInteger.ONE));var G=(C.modPow(w,v)).mod(u);var F=(w.modInverse(u).multiply(B.add(E.multiply(G)))).mod(u);var H=KJUR.asn1.ASN1Util.jsonToASN1HEX({seq:[{"int":{bigint:G}},{"int":{bigint:F}}]});return H};this.verifyWithMessageHash=function(C,B){var z=this.p;var u=this.q;var G=this.g;var H=this.y;var E=this.parseASN1Signature(B);var K=E[0];var J=E[1];var C=C.substr(0,u.bitLength()/4);var D=new BigInteger(C,16);if(BigInteger.ZERO.compareTo(K)>0||K.compareTo(u)>0||BigInteger.ZERO.compareTo(J)>0||J.compareTo(u)>0){throw"invalid DSA signature"}var I=J.modInverse(u);var A=D.multiply(I).mod(u);var v=K.multiply(I).mod(u);var F=G.modPow(A,z).multiply(H.modPow(v,z)).mod(z).mod(u);return F.compareTo(K)==0};this.parseASN1Signature=function(u){try{var y=new BigInteger(ASN1HEX.getVbyList(u,0,[0],"02"),16);var v=new BigInteger(ASN1HEX.getVbyList(u,0,[1],"02"),16);return[y,v]}catch(w){throw"malformed DSA signature"}};function d(E,w,B,v,u,C){var z=KJUR.crypto.Util.hashString(w,E.toLowerCase());var z=z.substr(0,u.bitLength()/4);var A=new BigInteger(z,16);var y=n(BigInteger.ONE.add(BigInteger.ONE),u.subtract(BigInteger.ONE));var F=(B.modPow(y,v)).mod(u);var D=(y.modInverse(u).multiply(A.add(C.multiply(F)))).mod(u);var G=new Array();G[0]=F;G[1]=D;return G}function r(v){var u=openpgp.config.config.prefer_hash_algorithm;switch(Math.round(v.bitLength()/8)){case 20:if(u!=2&&u>11&&u!=10&&u<8){return 2}return u;case 28:if(u>11&&u<8){return 11}return u;case 32:if(u>10&&u<8){return 8}return u;default:util.print_debug("DSA select hash algorithm: returning null for an unknown length of q");return null}}this.select_hash_algorithm=r;function m(I,K,J,B,z,u,F,G){var C=KJUR.crypto.Util.hashString(B,I.toLowerCase());var C=C.substr(0,u.bitLength()/4);var D=new BigInteger(C,16);if(BigInteger.ZERO.compareTo(K)>0||K.compareTo(u)>0||BigInteger.ZERO.compareTo(J)>0||J.compareTo(u)>0){util.print_error("invalid DSA Signature");return null}var H=J.modInverse(u);var A=D.multiply(H).mod(u);var v=K.multiply(H).mod(u);var E=F.modPow(A,z).multiply(G.modPow(v,z)).mod(z).mod(u);return E.compareTo(K)==0}function a(z){var A=new BigInteger(z,primeCenterie);var y=j(q,512);var u=t(p,q,z);var v;do{v=new BigInteger(q.bitCount(),rand)}while(x.compareTo(BigInteger.ZERO)!=1&&x.compareTo(q)!=-1);var w=g.modPow(x,p);return{x:v,q:A,p:y,g:u,y:w}}function j(y,z,w){if(z%64!=0){return false}var u;var v;do{u=w(bitcount,true);v=u.subtract(BigInteger.ONE);u=u.subtract(v.remainder(y))}while(!u.isProbablePrime(primeCenterie)||u.bitLength()!=l);return u}function t(B,z,A,w){var u=B.subtract(BigInteger.ONE);var y=u.divide(z);var v;do{v=w(A)}while(v.compareTo(u)!=-1&&v.compareTo(BigInteger.ONE)!=1);return v.modPow(y,B)}function o(w,y,u){var v;do{v=u(y,false)}while(v.compareTo(w)!=-1&&v.compareTo(BigInteger.ZERO)!=1);return v}function i(v,w){k=o(v);var u=g.modPow(k,w).mod(v);return u}function h(B,w,y,v,z,u){var A=B(v);s=(w.modInverse(z).multiply(A.add(u.multiply(y)))).mod(z);return s}this.sign=d;this.verify=m;function n(w,u){if(u.compareTo(w)<=0){return}var v=u.subtract(w);var y=e(v.bitLength());while(y>v){y=e(v.bitLength())}return w.add(y)}function e(w){if(w<0){return null}var u=Math.floor((w+7)/8);var v=c(u);if(w%8>0){v=String.fromCharCode((Math.pow(2,w%8)-1)&v.charCodeAt(0))+v.substring(1)}return new BigInteger(f(v),16)}function c(w){var u="";for(var v=0;v=s*2){break}}var x={};x.keyhex=v.substr(0,g[o]["keylen"]*2);x.ivhex=v.substr(g[o]["keylen"]*2,g[o]["ivlen"]*2);return x};var a=function(n,t,p,u){var q=CryptoJS.enc.Base64.parse(n);var o=CryptoJS.enc.Hex.stringify(q);var s=g[t]["proc"];var r=s(o,p,u);return r};var f=function(n,q,o,s){var p=g[q]["eproc"];var r=p(n,o,s);return r};return{version:"1.0.5",getHexFromPEM:function(o,r){var p=o;if(p.indexOf("BEGIN "+r)==-1){throw"can't find PEM header: "+r}p=p.replace("-----BEGIN "+r+"-----","");p=p.replace("-----END "+r+"-----","");var q=p.replace(/\s+/g,"");var n=b64tohex(q);return n},getDecryptedKeyHexByKeyIV:function(o,r,q,p){var n=b(r);return n(o,q,p)},parsePKCS5PEM:function(n){return l(n)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(o,n,p){return h(o,n,p)},decryptKeyB64:function(n,p,o,q){return a(n,p,o,q)},getDecryptedKeyHex:function(w,v){var o=l(w);var r=o.type;var p=o.cipher;var n=o.ivsalt;var q=o.data;var u=h(p,v,n);var t=u.keyhex;var s=a(q,p,t,n);return s},getRSAKeyFromEncryptedPKCS5PEM:function(p,o){var q=this.getDecryptedKeyHex(p,o);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(q);return n},getEryptedPKCS5PEMFromPrvKeyHex:function(q,x,r,p){var n="";if(typeof r=="undefined"||r==null){r="AES-256-CBC"}if(typeof g[r]=="undefined"){throw"PKCS5PKEY unsupported algorithm: "+r}if(typeof p=="undefined"||p==null){var t=g[r]["ivlen"];var s=k(t);p=s.toUpperCase()}var w=h(r,x,p);var v=w.keyhex;var u=f(q,r,v,p);var o=u.replace(/(.{64})/g,"$1\r\n");var n="-----BEGIN RSA PRIVATE KEY-----\r\n";n+="Proc-Type: 4,ENCRYPTED\r\n";n+="DEK-Info: "+r+","+p+"\r\n";n+="\r\n";n+=o;n+="\r\n-----END RSA PRIVATE KEY-----\r\n";return n},getEryptedPKCS5PEMFromRSAKey:function(C,D,o,s){var A=new KJUR.asn1.DERInteger({"int":0});var v=new KJUR.asn1.DERInteger({bigint:C.n});var z=new KJUR.asn1.DERInteger({"int":C.e});var B=new KJUR.asn1.DERInteger({bigint:C.d});var t=new KJUR.asn1.DERInteger({bigint:C.p});var r=new KJUR.asn1.DERInteger({bigint:C.q});var y=new KJUR.asn1.DERInteger({bigint:C.dmp1});var u=new KJUR.asn1.DERInteger({bigint:C.dmq1});var x=new KJUR.asn1.DERInteger({bigint:C.coeff});var E=new KJUR.asn1.DERSequence({array:[A,v,z,B,t,r,y,u,x]});var w=E.getEncodedHex();return this.getEryptedPKCS5PEMFromPrvKeyHex(w,D,o,s)},newEncryptedPKCS5PEM:function(n,o,r,s){if(typeof o=="undefined"||o==null){o=1024}if(typeof r=="undefined"||r==null){r="10001"}var p=new RSAKey();p.generate(o,r);var q=null;if(typeof s=="undefined"||s==null){q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n)}else{q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n,s)}return q},getRSAKeyFromPlainPKCS8PEM:function(p){if(p.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var o=this.getHexFromPEM(p,"PRIVATE KEY");var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getRSAKeyFromPlainPKCS8Hex:function(q){var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"outer DERSequence shall have 3 elements: "+p.length}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);if(o!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+o}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);var r=ASN1HEX.getHexOfTLV_AtObj(q,p[2]);var s=ASN1HEX.getHexOfV_AtObj(r,0);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(s);return n},parseHexOfEncryptedPKCS8:function(u){var q={};var p=ASN1HEX.getPosArrayOfChildren_AtObj(u,0);if(p.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+p.length}q.ciphertext=ASN1HEX.getHexOfV_AtObj(u,p[1]);var w=ASN1HEX.getPosArrayOfChildren_AtObj(u,p[0]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+w.length}if(ASN1HEX.getHexOfV_AtObj(u,w[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(u,w[1]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+n.length}var o=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[1]);if(o.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+o.length}if(ASN1HEX.getHexOfV_AtObj(u,o[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}q.encryptionSchemeAlg="TripleDES";q.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(u,o[1]);var r=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[0]);if(r.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+r.length}if(ASN1HEX.getHexOfV_AtObj(u,r[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var v=ASN1HEX.getPosArrayOfChildren_AtObj(u,r[1]);if(v.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+v.length}q.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(u,v[0]);var s=ASN1HEX.getHexOfV_AtObj(u,v[1]);try{q.pbkdf2Iter=parseInt(s,16)}catch(t){throw"malformed format pbkdf2Iter: "+s}return q},getPBKDF2KeyHexFromParam:function(s,n){var r=CryptoJS.enc.Hex.parse(s.pbkdf2Salt);var o=s.pbkdf2Iter;var q=CryptoJS.PBKDF2(n,r,{keySize:192/32,iterations:o});var p=CryptoJS.enc.Hex.stringify(q);return p},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(v,w){var p=this.getHexFromPEM(v,"ENCRYPTED PRIVATE KEY");var n=this.parseHexOfEncryptedPKCS8(p);var s=PKCS5PKEY.getPBKDF2KeyHexFromParam(n,w);var t={};t.ciphertext=CryptoJS.enc.Hex.parse(n.ciphertext);var r=CryptoJS.enc.Hex.parse(s);var q=CryptoJS.enc.Hex.parse(n.encryptionSchemeIV);var u=CryptoJS.TripleDES.decrypt(t,r,{iv:q});var o=CryptoJS.enc.Hex.stringify(u);return o},getRSAKeyFromEncryptedPKCS8PEM:function(q,p){var o=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,p);var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getKeyFromEncryptedPKCS8PEM:function(q,o){var n=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,o);var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},parsePlainPrivatePKCS8Hex:function(q){var o={};o.algparam=null;if(q.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(q.substr(p[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,p[1]);if(n.length!=2){throw"malformed PKCS8 private key(code:004)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}o.keyidx=ASN1HEX.getStartPosOfV_AtObj(q,p[2]);return o},getKeyFromPlainPrivatePKCS8PEM:function(o){var n=this.getHexFromPEM(o,"PRIVATE KEY");var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},getKeyFromPlainPrivatePKCS8Hex:function(n){var p=this.parsePlainPrivatePKCS8Hex(n);if(p.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(n,p);var o=p.key;var q=new RSAKey();q.setPrivateEx(o.n,o.e,o.d,o.p,o.q,o.dp,o.dq,o.co);return q}else{if(p.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(n,p);if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var r=KJUR.crypto.OID.oidhex2name[p.algparam];var q=new KJUR.crypto.ECDSA({curve:r,prv:p.key});return q}else{throw"unsupported private key algorithm"}}},getRSAKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getRSAKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8Hex:function(o){var n=this.parsePublicPKCS8Hex(o);if(n.algoid=="2a864886f70d010101"){var r=this.parsePublicRawRSAKeyHex(n.key);var p=new RSAKey();p.setPublic(r.n,r.e);return p}else{if(n.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[n.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+n.algparam}var q=KJUR.crypto.OID.oidhex2name[n.algparam];var p=new KJUR.crypto.ECDSA({curve:q,pub:n.key});return p}else{throw"unsupported public key algorithm"}}},parsePublicRawRSAKeyHex:function(p){var n={};if(p.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var o=ASN1HEX.getPosArrayOfChildren_AtObj(p,0);if(o.length!=2){throw"malformed RSA key(code:002)"}if(p.substr(o[0],2)!="02"){throw"malformed RSA key(code:003)"}n.n=ASN1HEX.getHexOfV_AtObj(p,o[0]);if(p.substr(o[1],2)!="02"){throw"malformed RSA key(code:004)"}n.e=ASN1HEX.getHexOfV_AtObj(p,o[1]);return n},parsePrivateRawRSAKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed RSA private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=9){throw"malformed RSA private key(code:002)"}q.key={};q.key.n=ASN1HEX.getHexOfV_AtObj(o,n[1]);q.key.e=ASN1HEX.getHexOfV_AtObj(o,n[2]);q.key.d=ASN1HEX.getHexOfV_AtObj(o,n[3]);q.key.p=ASN1HEX.getHexOfV_AtObj(o,n[4]);q.key.q=ASN1HEX.getHexOfV_AtObj(o,n[5]);q.key.dp=ASN1HEX.getHexOfV_AtObj(o,n[6]);q.key.dq=ASN1HEX.getHexOfV_AtObj(o,n[7]);q.key.co=ASN1HEX.getHexOfV_AtObj(o,n[8])},parsePrivateRawECKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed ECC private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=3){throw"malformed ECC private key(code:002)"}if(o.substr(n[1],2)!="04"){throw"malformed ECC private key(code:003)"}q.key=ASN1HEX.getHexOfV_AtObj(o,n[1])},parsePublicPKCS8Hex:function(q){var o={};o.algparam=null;var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=2){throw"outer DERSequence shall have 2 elements: "+p.length}var r=p[0];if(q.substr(r,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(n.length!=2){throw"malformed PKCS8 public key(code:002)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}o.key=ASN1HEX.getHexOfV_AtObj(q,p[1]).substr(2);return o},getRSAKeyFromPublicPKCS8Hex:function(r){var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"outer DERSequence shall have 2 elements: "+q.length}var p=ASN1HEX.getHexOfTLV_AtObj(r,q[0]);if(p!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(r.substr(q[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var t=ASN1HEX.getStartPosOfV_AtObj(r,q[1])+2;if(r.substr(t,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var n=ASN1HEX.getPosArrayOfChildren_AtObj(r,t);if(n.length!=2){throw"inner DERSequence shall have 2 elements: "+n.length}if(r.substr(n[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(r.substr(n[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var u=ASN1HEX.getHexOfV_AtObj(r,n[0]);var s=ASN1HEX.getHexOfV_AtObj(r,n[1]);var o=new RSAKey();o.setPublic(u,s);return o},}}(); -/*! keyutil-1.0.13.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license +var PKCS5PKEY=function(){var c=function(n,p,o){return i(CryptoJS.AES,n,p,o)};var d=function(n,p,o){return i(CryptoJS.TripleDES,n,p,o)};var i=function(q,v,s,o){var p=CryptoJS.enc.Hex.parse(v);var u=CryptoJS.enc.Hex.parse(s);var n=CryptoJS.enc.Hex.parse(o);var r={};r.key=u;r.iv=n;r.ciphertext=p;var t=q.decrypt(r,u,{iv:n});return CryptoJS.enc.Hex.stringify(t)};var j=function(n,p,o){return e(CryptoJS.AES,n,p,o)};var m=function(n,p,o){return e(CryptoJS.TripleDES,n,p,o)};var e=function(s,x,v,p){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(v);var o=CryptoJS.enc.Hex.parse(p);var n={};var u=s.encrypt(r,w,{iv:o});var q=CryptoJS.enc.Hex.parse(u.toString());var t=CryptoJS.enc.Base64.stringify(q);return t};var g={"AES-256-CBC":{proc:c,eproc:j,keylen:32,ivlen:16},"AES-192-CBC":{proc:c,eproc:j,keylen:24,ivlen:16},"AES-128-CBC":{proc:c,eproc:j,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:d,eproc:m,keylen:24,ivlen:8}};var b=function(n){return g[n]["proc"]};var k=function(n){var p=CryptoJS.lib.WordArray.random(n);var o=CryptoJS.enc.Hex.stringify(p);return o};var l=function(t){var u={};var o=t.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"));if(o){u.cipher=o[1];u.ivsalt=o[2]}var n=t.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));if(n){u.type=n[1]}var r=-1;var v=0;if(t.indexOf("\r\n\r\n")!=-1){r=t.indexOf("\r\n\r\n");v=2}if(t.indexOf("\n\n")!=-1){r=t.indexOf("\n\n");v=1}var q=t.indexOf("-----END");if(r!=-1&&q!=-1){var p=t.substring(r+v*2,q-v);p=p.replace(/\s+/g,"");u.data=p}return u};var h=function(o,w,n){var t=n.substring(0,16);var r=CryptoJS.enc.Hex.parse(t);var p=CryptoJS.enc.Utf8.parse(w);var s=g[o]["keylen"]+g[o]["ivlen"];var v="";var u=null;for(;;){var q=CryptoJS.algo.MD5.create();if(u!=null){q.update(u)}q.update(p);q.update(r);u=q.finalize();v=v+CryptoJS.enc.Hex.stringify(u);if(v.length>=s*2){break}}var x={};x.keyhex=v.substr(0,g[o]["keylen"]*2);x.ivhex=v.substr(g[o]["keylen"]*2,g[o]["ivlen"]*2);return x};var a=function(n,t,p,u){var q=CryptoJS.enc.Base64.parse(n);var o=CryptoJS.enc.Hex.stringify(q);var s=g[t]["proc"];var r=s(o,p,u);return r};var f=function(n,q,o,s){var p=g[q]["eproc"];var r=p(n,o,s);return r};return{version:"1.0.5",getHexFromPEM:function(o,r){var p=o;if(p.indexOf("BEGIN "+r)==-1){throw"can't find PEM header: "+r}p=p.replace("-----BEGIN "+r+"-----","");p=p.replace("-----END "+r+"-----","");var q=p.replace(/\s+/g,"");var n=b64tohex(q);return n},getDecryptedKeyHexByKeyIV:function(o,r,q,p){var n=b(r);return n(o,q,p)},parsePKCS5PEM:function(n){return l(n)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(o,n,p){return h(o,n,p)},decryptKeyB64:function(n,p,o,q){return a(n,p,o,q)},getDecryptedKeyHex:function(w,v){var o=l(w);var r=o.type;var p=o.cipher;var n=o.ivsalt;var q=o.data;var u=h(p,v,n);var t=u.keyhex;var s=a(q,p,t,n);return s},getRSAKeyFromEncryptedPKCS5PEM:function(p,o){var q=this.getDecryptedKeyHex(p,o);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(q);return n},getEryptedPKCS5PEMFromPrvKeyHex:function(q,x,r,p){var n="";if(typeof r=="undefined"||r==null){r="AES-256-CBC"}if(typeof g[r]=="undefined"){throw"PKCS5PKEY unsupported algorithm: "+r}if(typeof p=="undefined"||p==null){var t=g[r]["ivlen"];var s=k(t);p=s.toUpperCase()}var w=h(r,x,p);var v=w.keyhex;var u=f(q,r,v,p);var o=u.replace(/(.{64})/g,"$1\r\n");var n="-----BEGIN RSA PRIVATE KEY-----\r\n";n+="Proc-Type: 4,ENCRYPTED\r\n";n+="DEK-Info: "+r+","+p+"\r\n";n+="\r\n";n+=o;n+="\r\n-----END RSA PRIVATE KEY-----\r\n";return n},getEryptedPKCS5PEMFromRSAKey:function(C,D,o,s){var A=new KJUR.asn1.DERInteger({"int":0});var v=new KJUR.asn1.DERInteger({bigint:C.n});var z=new KJUR.asn1.DERInteger({"int":C.e});var B=new KJUR.asn1.DERInteger({bigint:C.d});var t=new KJUR.asn1.DERInteger({bigint:C.p});var r=new KJUR.asn1.DERInteger({bigint:C.q});var y=new KJUR.asn1.DERInteger({bigint:C.dmp1});var u=new KJUR.asn1.DERInteger({bigint:C.dmq1});var x=new KJUR.asn1.DERInteger({bigint:C.coeff});var E=new KJUR.asn1.DERSequence({array:[A,v,z,B,t,r,y,u,x]});var w=E.getEncodedHex();return this.getEryptedPKCS5PEMFromPrvKeyHex(w,D,o,s)},newEncryptedPKCS5PEM:function(n,o,r,s){if(typeof o=="undefined"||o==null){o=1024}if(typeof r=="undefined"||r==null){r="10001"}var p=new RSAKey();p.generate(o,r);var q=null;if(typeof s=="undefined"||s==null){q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n)}else{q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n,s)}return q},getRSAKeyFromPlainPKCS8PEM:function(p){if(p.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var o=this.getHexFromPEM(p,"PRIVATE KEY");var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getRSAKeyFromPlainPKCS8Hex:function(q){var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"outer DERSequence shall have 3 elements: "+p.length}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);if(o!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+o}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);var r=ASN1HEX.getHexOfTLV_AtObj(q,p[2]);var s=ASN1HEX.getHexOfV_AtObj(r,0);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(s);return n},parseHexOfEncryptedPKCS8:function(u){var q={};var p=ASN1HEX.getPosArrayOfChildren_AtObj(u,0);if(p.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+p.length}q.ciphertext=ASN1HEX.getHexOfV_AtObj(u,p[1]);var w=ASN1HEX.getPosArrayOfChildren_AtObj(u,p[0]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+w.length}if(ASN1HEX.getHexOfV_AtObj(u,w[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(u,w[1]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+n.length}var o=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[1]);if(o.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+o.length}if(ASN1HEX.getHexOfV_AtObj(u,o[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}q.encryptionSchemeAlg="TripleDES";q.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(u,o[1]);var r=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[0]);if(r.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+r.length}if(ASN1HEX.getHexOfV_AtObj(u,r[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var v=ASN1HEX.getPosArrayOfChildren_AtObj(u,r[1]);if(v.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+v.length}q.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(u,v[0]);var s=ASN1HEX.getHexOfV_AtObj(u,v[1]);try{q.pbkdf2Iter=parseInt(s,16)}catch(t){throw"malformed format pbkdf2Iter: "+s}return q},getPBKDF2KeyHexFromParam:function(s,n){var r=CryptoJS.enc.Hex.parse(s.pbkdf2Salt);var o=s.pbkdf2Iter;var q=CryptoJS.PBKDF2(n,r,{keySize:192/32,iterations:o});var p=CryptoJS.enc.Hex.stringify(q);return p},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(v,w){var p=this.getHexFromPEM(v,"ENCRYPTED PRIVATE KEY");var n=this.parseHexOfEncryptedPKCS8(p);var s=PKCS5PKEY.getPBKDF2KeyHexFromParam(n,w);var t={};t.ciphertext=CryptoJS.enc.Hex.parse(n.ciphertext);var r=CryptoJS.enc.Hex.parse(s);var q=CryptoJS.enc.Hex.parse(n.encryptionSchemeIV);var u=CryptoJS.TripleDES.decrypt(t,r,{iv:q});var o=CryptoJS.enc.Hex.stringify(u);return o},getRSAKeyFromEncryptedPKCS8PEM:function(q,p){var o=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,p);var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getKeyFromEncryptedPKCS8PEM:function(q,o){var n=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,o);var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},parsePlainPrivatePKCS8Hex:function(q){var o={};o.algparam=null;if(q.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(q.substr(p[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,p[1]);if(n.length!=2){throw"malformed PKCS8 private key(code:004)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}o.keyidx=ASN1HEX.getStartPosOfV_AtObj(q,p[2]);return o},getKeyFromPlainPrivatePKCS8PEM:function(o){var n=this.getHexFromPEM(o,"PRIVATE KEY");var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},getKeyFromPlainPrivatePKCS8Hex:function(n){var p=this.parsePlainPrivatePKCS8Hex(n);if(p.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(n,p);var o=p.key;var q=new RSAKey();q.setPrivateEx(o.n,o.e,o.d,o.p,o.q,o.dp,o.dq,o.co);return q}else{if(p.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(n,p);if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var r=KJUR.crypto.OID.oidhex2name[p.algparam];var q=new KJUR.crypto.ECDSA({curve:r,prv:p.key});return q}else{throw"unsupported private key algorithm"}}},getRSAKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getRSAKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8Hex:function(o){var n=this.parsePublicPKCS8Hex(o);if(n.algoid=="2a864886f70d010101"){var r=this.parsePublicRawRSAKeyHex(n.key);var p=new RSAKey();p.setPublic(r.n,r.e);return p}else{if(n.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[n.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+n.algparam}var q=KJUR.crypto.OID.oidhex2name[n.algparam];var p=new KJUR.crypto.ECDSA({curve:q,pub:n.key});return p}else{throw"unsupported public key algorithm"}}},parsePublicRawRSAKeyHex:function(p){var n={};if(p.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var o=ASN1HEX.getPosArrayOfChildren_AtObj(p,0);if(o.length!=2){throw"malformed RSA key(code:002)"}if(p.substr(o[0],2)!="02"){throw"malformed RSA key(code:003)"}n.n=ASN1HEX.getHexOfV_AtObj(p,o[0]);if(p.substr(o[1],2)!="02"){throw"malformed RSA key(code:004)"}n.e=ASN1HEX.getHexOfV_AtObj(p,o[1]);return n},parsePrivateRawRSAKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed RSA private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=9){throw"malformed RSA private key(code:002)"}q.key={};q.key.n=ASN1HEX.getHexOfV_AtObj(o,n[1]);q.key.e=ASN1HEX.getHexOfV_AtObj(o,n[2]);q.key.d=ASN1HEX.getHexOfV_AtObj(o,n[3]);q.key.p=ASN1HEX.getHexOfV_AtObj(o,n[4]);q.key.q=ASN1HEX.getHexOfV_AtObj(o,n[5]);q.key.dp=ASN1HEX.getHexOfV_AtObj(o,n[6]);q.key.dq=ASN1HEX.getHexOfV_AtObj(o,n[7]);q.key.co=ASN1HEX.getHexOfV_AtObj(o,n[8])},parsePrivateRawECKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed ECC private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=3){throw"malformed ECC private key(code:002)"}if(o.substr(n[1],2)!="04"){throw"malformed ECC private key(code:003)"}q.key=ASN1HEX.getHexOfV_AtObj(o,n[1])},parsePublicPKCS8Hex:function(q){var o={};o.algparam=null;var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=2){throw"outer DERSequence shall have 2 elements: "+p.length}var r=p[0];if(q.substr(r,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(n.length!=2){throw"malformed PKCS8 public key(code:002)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}o.key=ASN1HEX.getHexOfV_AtObj(q,p[1]).substr(2);return o},getRSAKeyFromPublicPKCS8Hex:function(r){var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"outer DERSequence shall have 2 elements: "+q.length}var p=ASN1HEX.getHexOfTLV_AtObj(r,q[0]);if(p!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(r.substr(q[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var t=ASN1HEX.getStartPosOfV_AtObj(r,q[1])+2;if(r.substr(t,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var n=ASN1HEX.getPosArrayOfChildren_AtObj(r,t);if(n.length!=2){throw"inner DERSequence shall have 2 elements: "+n.length}if(r.substr(n[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(r.substr(n[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var u=ASN1HEX.getHexOfV_AtObj(r,n[0]);var s=ASN1HEX.getHexOfV_AtObj(r,n[1]);var o=new RSAKey();o.setPublic(u,s);return o},}}(); +/*! keyutil-1.0.14.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ -var KEYUTIL=function(){var d=function(p,r,q){return k(CryptoJS.AES,p,r,q)};var e=function(p,r,q){return k(CryptoJS.TripleDES,p,r,q)};var a=function(p,r,q){return k(CryptoJS.DES,p,r,q)};var k=function(s,x,u,q){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(u);var p=CryptoJS.enc.Hex.parse(q);var t={};t.key=w;t.iv=p;t.ciphertext=r;var v=s.decrypt(t,w,{iv:p});return CryptoJS.enc.Hex.stringify(v)};var l=function(p,r,q){return g(CryptoJS.AES,p,r,q)};var o=function(p,r,q){return g(CryptoJS.TripleDES,p,r,q)};var f=function(p,r,q){return g(CryptoJS.DES,p,r,q)};var g=function(t,y,v,q){var s=CryptoJS.enc.Hex.parse(y);var x=CryptoJS.enc.Hex.parse(v);var p=CryptoJS.enc.Hex.parse(q);var w=t.encrypt(s,x,{iv:p});var r=CryptoJS.enc.Hex.parse(w.toString());var u=CryptoJS.enc.Base64.stringify(r);return u};var i={"AES-256-CBC":{proc:d,eproc:l,keylen:32,ivlen:16},"AES-192-CBC":{proc:d,eproc:l,keylen:24,ivlen:16},"AES-128-CBC":{proc:d,eproc:l,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:e,eproc:o,keylen:24,ivlen:8},"DES-CBC":{proc:a,eproc:f,keylen:8,ivlen:8}};var c=function(p){return i[p]["proc"]};var m=function(p){var r=CryptoJS.lib.WordArray.random(p);var q=CryptoJS.enc.Hex.stringify(r);return q};var n=function(t){var u={};if(t.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"))){u.cipher=RegExp.$1;u.ivsalt=RegExp.$2}if(t.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"))){u.type=RegExp.$1}var r=-1;var v=0;if(t.indexOf("\r\n\r\n")!=-1){r=t.indexOf("\r\n\r\n");v=2}if(t.indexOf("\n\n")!=-1){r=t.indexOf("\n\n");v=1}var q=t.indexOf("-----END");if(r!=-1&&q!=-1){var p=t.substring(r+v*2,q-v);p=p.replace(/\s+/g,"");u.data=p}return u};var j=function(q,y,p){var v=p.substring(0,16);var t=CryptoJS.enc.Hex.parse(v);var r=CryptoJS.enc.Utf8.parse(y);var u=i[q]["keylen"]+i[q]["ivlen"];var x="";var w=null;for(;;){var s=CryptoJS.algo.MD5.create();if(w!=null){s.update(w)}s.update(r);s.update(t);w=s.finalize();x=x+CryptoJS.enc.Hex.stringify(w);if(x.length>=u*2){break}}var z={};z.keyhex=x.substr(0,i[q]["keylen"]*2);z.ivhex=x.substr(i[q]["keylen"]*2,i[q]["ivlen"]*2);return z};var b=function(p,v,r,w){var s=CryptoJS.enc.Base64.parse(p);var q=CryptoJS.enc.Hex.stringify(s);var u=i[v]["proc"];var t=u(q,r,w);return t};var h=function(p,s,q,u){var r=i[s]["eproc"];var t=r(p,q,u);return t};return{version:"1.0.0",getHexFromPEM:function(q,u){var r=q;if(r.indexOf("-----BEGIN ")==-1){throw"can't find PEM header: "+u}if(typeof u=="string"&&u!=""){r=r.replace("-----BEGIN "+u+"-----","");r=r.replace("-----END "+u+"-----","")}else{r=r.replace(/-----BEGIN [^-]+-----/,"");r=r.replace(/-----END [^-]+-----/,"")}var t=r.replace(/\s+/g,"");var p=b64tohex(t);return p},getDecryptedKeyHexByKeyIV:function(q,t,s,r){var p=c(t);return p(q,s,r)},parsePKCS5PEM:function(p){return n(p)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(q,p,r){return j(q,p,r)},decryptKeyB64:function(p,r,q,s){return b(p,r,q,s)},getDecryptedKeyHex:function(y,x){var q=n(y);var t=q.type;var r=q.cipher;var p=q.ivsalt;var s=q.data;var w=j(r,x,p);var v=w.keyhex;var u=b(s,r,v,p);return u},getRSAKeyFromEncryptedPKCS5PEM:function(r,q){var s=this.getDecryptedKeyHex(r,q);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(s);return p},getEncryptedPKCS5PEMFromPrvKeyHex:function(x,s,A,t,r){var p="";if(typeof t=="undefined"||t==null){t="AES-256-CBC"}if(typeof i[t]=="undefined"){throw"KEYUTIL unsupported algorithm: "+t}if(typeof r=="undefined"||r==null){var v=i[t]["ivlen"];var u=m(v);r=u.toUpperCase()}var z=j(t,A,r);var y=z.keyhex;var w=h(s,t,y,r);var q=w.replace(/(.{64})/g,"$1\r\n");var p="-----BEGIN "+x+" PRIVATE KEY-----\r\n";p+="Proc-Type: 4,ENCRYPTED\r\n";p+="DEK-Info: "+t+","+r+"\r\n";p+="\r\n";p+=q;p+="\r\n-----END "+x+" PRIVATE KEY-----\r\n";return p},getEncryptedPKCS5PEMFromRSAKey:function(D,E,r,t){var B=new KJUR.asn1.DERInteger({"int":0});var w=new KJUR.asn1.DERInteger({bigint:D.n});var A=new KJUR.asn1.DERInteger({"int":D.e});var C=new KJUR.asn1.DERInteger({bigint:D.d});var u=new KJUR.asn1.DERInteger({bigint:D.p});var s=new KJUR.asn1.DERInteger({bigint:D.q});var z=new KJUR.asn1.DERInteger({bigint:D.dmp1});var v=new KJUR.asn1.DERInteger({bigint:D.dmq1});var y=new KJUR.asn1.DERInteger({bigint:D.coeff});var F=new KJUR.asn1.DERSequence({array:[B,w,A,C,u,s,z,v,y]});var x=F.getEncodedHex();return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",x,E,r,t)},newEncryptedPKCS5PEM:function(p,q,t,u){if(typeof q=="undefined"||q==null){q=1024}if(typeof t=="undefined"||t==null){t="10001"}var r=new RSAKey();r.generate(q,t);var s=null;if(typeof u=="undefined"||u==null){s=this.getEncryptedPKCS5PEMFromRSAKey(r,p)}else{s=this.getEncryptedPKCS5PEMFromRSAKey(r,p,u)}return s},getRSAKeyFromPlainPKCS8PEM:function(r){if(r.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var q=this.getHexFromPEM(r,"PRIVATE KEY");var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getRSAKeyFromPlainPKCS8Hex:function(s){var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"outer DERSequence shall have 3 elements: "+r.length}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);if(q!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+q}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);var t=ASN1HEX.getHexOfTLV_AtObj(s,r[2]);var u=ASN1HEX.getHexOfV_AtObj(t,0);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(u);return p},parseHexOfEncryptedPKCS8:function(w){var s={};var r=ASN1HEX.getPosArrayOfChildren_AtObj(w,0);if(r.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+r.length}s.ciphertext=ASN1HEX.getHexOfV_AtObj(w,r[1]);var y=ASN1HEX.getPosArrayOfChildren_AtObj(w,r[0]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+y.length}if(ASN1HEX.getHexOfV_AtObj(w,y[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(w,y[1]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+p.length}var q=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[1]);if(q.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+q.length}if(ASN1HEX.getHexOfV_AtObj(w,q[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}s.encryptionSchemeAlg="TripleDES";s.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(w,q[1]);var t=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[0]);if(t.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+t.length}if(ASN1HEX.getHexOfV_AtObj(w,t[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var x=ASN1HEX.getPosArrayOfChildren_AtObj(w,t[1]);if(x.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+x.length}s.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(w,x[0]);var u=ASN1HEX.getHexOfV_AtObj(w,x[1]);try{s.pbkdf2Iter=parseInt(u,16)}catch(v){throw"malformed format pbkdf2Iter: "+u}return s},getPBKDF2KeyHexFromParam:function(u,p){var t=CryptoJS.enc.Hex.parse(u.pbkdf2Salt);var q=u.pbkdf2Iter;var s=CryptoJS.PBKDF2(p,t,{keySize:192/32,iterations:q});var r=CryptoJS.enc.Hex.stringify(s);return r},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(x,y){var r=this.getHexFromPEM(x,"ENCRYPTED PRIVATE KEY");var p=this.parseHexOfEncryptedPKCS8(r);var u=KEYUTIL.getPBKDF2KeyHexFromParam(p,y);var v={};v.ciphertext=CryptoJS.enc.Hex.parse(p.ciphertext);var t=CryptoJS.enc.Hex.parse(u);var s=CryptoJS.enc.Hex.parse(p.encryptionSchemeIV);var w=CryptoJS.TripleDES.decrypt(v,t,{iv:s});var q=CryptoJS.enc.Hex.stringify(w);return q},getRSAKeyFromEncryptedPKCS8PEM:function(s,r){var q=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,r);var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getKeyFromEncryptedPKCS8PEM:function(s,q){var p=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,q);var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},parsePlainPrivatePKCS8Hex:function(s){var q={};q.algparam=null;if(s.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(s.substr(r[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,r[1]);if(p.length!=2){throw"malformed PKCS8 private key(code:004)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}if(s.substr(r[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}q.keyidx=ASN1HEX.getStartPosOfV_AtObj(s,r[2]);return q},getKeyFromPlainPrivatePKCS8PEM:function(q){var p=this.getHexFromPEM(q,"PRIVATE KEY");var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},getKeyFromPlainPrivatePKCS8Hex:function(p){var w=this.parsePlainPrivatePKCS8Hex(p);if(w.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(p,w);var u=w.key;var z=new RSAKey();z.setPrivateEx(u.n,u.e,u.d,u.p,u.q,u.dp,u.dq,u.co);return z}else{if(w.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(p,w);if(KJUR.crypto.OID.oidhex2name[w.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+w.algparam}var v=KJUR.crypto.OID.oidhex2name[w.algparam];var z=new KJUR.crypto.ECDSA({curve:v});z.setPublicKeyHex(w.pubkey);z.setPrivateKeyHex(w.key);z.isPublic=false;return z}else{if(w.algoid=="2a8648ce380401"){var t=ASN1HEX.getVbyList(p,0,[1,1,0],"02");var s=ASN1HEX.getVbyList(p,0,[1,1,1],"02");var y=ASN1HEX.getVbyList(p,0,[1,1,2],"02");var B=ASN1HEX.getVbyList(p,0,[2,0],"02");var r=new BigInteger(t,16);var q=new BigInteger(s,16);var x=new BigInteger(y,16);var A=new BigInteger(B,16);var z=new KJUR.crypto.DSA();z.setPrivate(r,q,x,null,A);return z}else{throw"unsupported private key algorithm"}}}},getRSAKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getRSAKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8Hex:function(q){var p=this.parsePublicPKCS8Hex(q);if(p.algoid=="2a864886f70d010101"){var u=this.parsePublicRawRSAKeyHex(p.key);var r=new RSAKey();r.setPublic(u.n,u.e);return r}else{if(p.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var s=KJUR.crypto.OID.oidhex2name[p.algparam];var r=new KJUR.crypto.ECDSA({curve:s,pub:p.key});return r}else{if(p.algoid=="2a8648ce380401"){var t=p.algparam;var v=ASN1HEX.getHexOfV_AtObj(p.key,0);var r=new KJUR.crypto.DSA();r.setPublic(new BigInteger(t.p,16),new BigInteger(t.q,16),new BigInteger(t.g,16),new BigInteger(v,16));return r}else{throw"unsupported public key algorithm"}}}},parsePublicRawRSAKeyHex:function(r){var p={};if(r.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"malformed RSA key(code:002)"}if(r.substr(q[0],2)!="02"){throw"malformed RSA key(code:003)"}p.n=ASN1HEX.getHexOfV_AtObj(r,q[0]);if(r.substr(q[1],2)!="02"){throw"malformed RSA key(code:004)"}p.e=ASN1HEX.getHexOfV_AtObj(r,q[1]);return p},parsePrivateRawRSAKeyHexAtObj:function(q,s){var r=s.keyidx;if(q.substr(r,2)!="30"){throw"malformed RSA private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(p.length!=9){throw"malformed RSA private key(code:002)"}s.key={};s.key.n=ASN1HEX.getHexOfV_AtObj(q,p[1]);s.key.e=ASN1HEX.getHexOfV_AtObj(q,p[2]);s.key.d=ASN1HEX.getHexOfV_AtObj(q,p[3]);s.key.p=ASN1HEX.getHexOfV_AtObj(q,p[4]);s.key.q=ASN1HEX.getHexOfV_AtObj(q,p[5]);s.key.dp=ASN1HEX.getHexOfV_AtObj(q,p[6]);s.key.dq=ASN1HEX.getHexOfV_AtObj(q,p[7]);s.key.co=ASN1HEX.getHexOfV_AtObj(q,p[8])},parsePrivateRawECKeyHexAtObj:function(p,t){var q=t.keyidx;var r=ASN1HEX.getVbyList(p,q,[1],"04");var s=ASN1HEX.getVbyList(p,q,[2,0],"03").substr(2);t.key=r;t.pubkey=s},parsePublicPKCS8Hex:function(s){var q={};q.algparam=null;var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=2){throw"outer DERSequence shall have 2 elements: "+r.length}var t=r[0];if(s.substr(t,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,t);if(p.length!=2){throw"malformed PKCS8 public key(code:002)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}else{if(s.substr(p[1],2)=="30"){q.algparam={};q.algparam.p=ASN1HEX.getVbyList(s,p[1],[0],"02");q.algparam.q=ASN1HEX.getVbyList(s,p[1],[1],"02");q.algparam.g=ASN1HEX.getVbyList(s,p[1],[2],"02")}}if(s.substr(r[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}q.key=ASN1HEX.getHexOfV_AtObj(s,r[1]).substr(2);return q},getRSAKeyFromPublicPKCS8Hex:function(t){var s=ASN1HEX.getPosArrayOfChildren_AtObj(t,0);if(s.length!=2){throw"outer DERSequence shall have 2 elements: "+s.length}var r=ASN1HEX.getHexOfTLV_AtObj(t,s[0]);if(r!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(t.substr(s[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var v=ASN1HEX.getStartPosOfV_AtObj(t,s[1])+2;if(t.substr(v,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var p=ASN1HEX.getPosArrayOfChildren_AtObj(t,v);if(p.length!=2){throw"inner DERSequence shall have 2 elements: "+p.length}if(t.substr(p[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(t.substr(p[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var w=ASN1HEX.getHexOfV_AtObj(t,p[0]);var u=ASN1HEX.getHexOfV_AtObj(t,p[1]);var q=new RSAKey();q.setPublic(w,u);return q},}}();KEYUTIL.getKey=function(f,e,h){if(typeof RSAKey!="undefined"&&f instanceof RSAKey){return f}if(typeof KJUR.crypto.ECDSA!="undefined"&&f instanceof KJUR.crypto.ECDSA){return f}if(typeof KJUR.crypto.DSA!="undefined"&&f instanceof KJUR.crypto.DSA){return f}if(f.curve!==undefined&&f.xy!==undefined&&f.d===undefined){return new KJUR.crypto.ECDSA({pub:f.xy,curve:f.curve})}if(f.curve!==undefined&&f.d!==undefined){return new KJUR.crypto.ECDSA({prv:f.d,curve:f.curve})}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(f.n,f.e);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.co!==undefined&&f.qi===undefined){var w=new RSAKey();w.setPrivateEx(f.n,f.e,f.d,f.p,f.q,f.dp,f.dq,f.co);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p===undefined){var w=new RSAKey();w.setPrivate(f.n,f.e,f.d);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x===undefined){var w=new KJUR.crypto.DSA();w.setPublic(f.p,f.q,f.g,f.y);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x!==undefined){var w=new KJUR.crypto.DSA();w.setPrivate(f.p,f.q,f.g,f.y,f.x);return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(b64utohex(f.n),b64utohex(f.e));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.qi!==undefined){var w=new RSAKey();w.setPrivateEx(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d),b64utohex(f.p),b64utohex(f.q),b64utohex(f.dp),b64utohex(f.dq),b64utohex(f.qi));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined){var w=new RSAKey();w.setPrivate(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d));return w}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d===undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;d.setPublicKeyHex(m);return d}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d!==undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;var a=("0000000000"+b64utohex(f.d)).slice(-l);d.setPublicKeyHex(m);d.setPrivateKeyHex(a);return d}if(f.indexOf("-END CERTIFICATE-",0)!=-1||f.indexOf("-END X509 CERTIFICATE-",0)!=-1||f.indexOf("-END TRUSTED CERTIFICATE-",0)!=-1){return X509.getPublicKeyFromCertPEM(f)}if(h==="pkcs8pub"){return KEYUTIL.getKeyFromPublicPKCS8Hex(f)}if(f.indexOf("-END PUBLIC KEY-")!=-1){return KEYUTIL.getKeyFromPublicPKCS8PEM(f)}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var i=KEYUTIL.getHexFromPEM(f,"RSA PRIVATE KEY");return KEYUTIL.getKey(i,null,"pkcs5prv")}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var u=this.getHexFromPEM(f,"DSA PRIVATE KEY");var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(f)}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(f,e)}if(f.indexOf("-END EC PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var w=ASN1HEX.getVbyList(u,0,[1],"04");var c=ASN1HEX.getVbyList(u,0,[2,0],"06");var o=ASN1HEX.getVbyList(u,0,[3,0],"03").substr(2);var b="";if(KJUR.crypto.OID.oidhex2name[c]!==undefined){b=KJUR.crypto.OID.oidhex2name[c]}else{throw"undefined OID(hex) in KJUR.crypto.OID: "+c}var d=new KJUR.crypto.ECDSA({name:b});d.setPublicKeyHex(o);d.setPrivateKeyHex(w);d.isPublic=false;return d}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END ENCRYPTED PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromEncryptedPKCS8PEM(f,e)}throw"not supported argument"};KEYUTIL.generateKeypair=function(a,c){if(a=="RSA"){var b=c;var h=new RSAKey();h.generate(b,"10001");h.isPrivate=true;h.isPublic=true;var f=new RSAKey();var e=h.n.toString(16);var i=h.e.toString(16);f.setPublic(e,i);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{if(a=="EC"){var d=c;var g=new KJUR.crypto.ECDSA({curve:d});var j=g.generateKeyPairHex();var h=new KJUR.crypto.ECDSA({curve:d});h.setPublicKeyHex(j.ecpubhex);h.setPrivateKeyHex(j.ecprvhex);h.isPrivate=true;h.isPublic=false;var f=new KJUR.crypto.ECDSA({curve:d});f.setPublicKeyHex(j.ecpubhex);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{throw"unknown algorithm: "+a}}};KEYUTIL.getPEM=function(a,r,o,g,j){var v=KJUR.asn1;var u=KJUR.crypto;function p(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.n}},{"int":s.e},{"int":{bigint:s.d}},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.dmp1}},{"int":{bigint:s.dmq1}},{"int":{bigint:s.coeff}}]});return w}function q(w){var s=KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:w.prvKeyHex}},{tag:["a0",true,{oid:{name:w.curveName}}]},{tag:["a1",true,{bitstr:{hex:"00"+w.pubKeyHex}}]}]});return s}function n(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.g}},{"int":{bigint:s.y}},{"int":{bigint:s.x}}]});return w}if(((typeof RSAKey!="undefined"&&a instanceof RSAKey)||(typeof u.DSA!="undefined"&&a instanceof u.DSA)||(typeof u.ECDSA!="undefined"&&a instanceof u.ECDSA))&&a.isPublic==true&&(r===undefined||r=="PKCS8PUB")){var t=new KJUR.asn1.x509.SubjectPublicKeyInfo(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"PUBLIC KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o===undefined||o==null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"RSA PRIVATE KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o===undefined||o==null)&&a.isPrivate==true){var f=new KJUR.asn1.DERObjectIdentifier({name:a.curveName});var l=f.getEncodedHex();var e=q(a);var k=e.getEncodedHex();var i="";i+=v.ASN1Util.getPEMStringFromHex(l,"EC PARAMETERS");i+=v.ASN1Util.getPEMStringFromHex(k,"EC PRIVATE KEY");return i}if(r=="PKCS1PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o===undefined||o==null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"DSA PRIVATE KEY")}if(r=="PKCS5PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=q(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA",m,o,g)}var h=function(w,s){var y=b(w,s);var x=new KJUR.asn1.ASN1Util.newObject({seq:[{seq:[{oid:{name:"pkcs5PBES2"}},{seq:[{seq:[{oid:{name:"pkcs5PBKDF2"}},{seq:[{octstr:{hex:y.pbkdf2Salt}},{"int":y.pbkdf2Iter}]}]},{seq:[{oid:{name:"des-EDE3-CBC"}},{octstr:{hex:y.encryptionSchemeIV}}]}]}]},{octstr:{hex:y.ciphertext}}]});return x.getEncodedHex()};var b=function(D,E){var x=100;var C=CryptoJS.lib.WordArray.random(8);var B="DES-EDE3-CBC";var s=CryptoJS.lib.WordArray.random(8);var y=CryptoJS.PBKDF2(E,C,{keySize:192/32,iterations:x});var z=CryptoJS.enc.Hex.parse(D);var A=CryptoJS.TripleDES.encrypt(z,y,{iv:s})+"";var w={};w.ciphertext=A;w.pbkdf2Salt=CryptoJS.enc.Hex.stringify(C);w.pbkdf2Iter=x;w.encryptionSchemeAlg=B;w.encryptionSchemeIV=CryptoJS.enc.Hex.stringify(s);return w};if(r=="PKCS8PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&a.isPrivate==true){var d=p(a);var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"rsaEncryption"}},{"null":true}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&a.isPrivate==true){var d=new KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:a.prvKeyHex}},{tag:["a1",true,{bitstr:{hex:"00"+a.pubKeyHex}}]}]});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"ecPublicKey"}},{oid:{name:a.curveName}}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&a.isPrivate==true){var d=new KJUR.asn1.DERInteger({bigint:a.x});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"dsa"}},{seq:[{"int":{bigint:a.p}},{"int":{bigint:a.q}},{"int":{bigint:a.g}}]}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}throw"unsupported object nor format"};KEYUTIL.getKeyFromCSRPEM=function(b){var a=KEYUTIL.getHexFromPEM(b,"CERTIFICATE REQUEST");var c=KEYUTIL.getKeyFromCSRHex(a);return c};KEYUTIL.getKeyFromCSRHex=function(a){var c=KEYUTIL.parseCSRHex(a);var b=KEYUTIL.getKey(c.p8pubkeyhex,null,"pkcs8pub");return b};KEYUTIL.parseCSRHex=function(c){var b={};var e=c;if(e.substr(0,2)!="30"){throw"malformed CSR(code:001)"}var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,0);if(d.length<1){throw"malformed CSR(code:002)"}if(e.substr(d[0],2)!="30"){throw"malformed CSR(code:003)"}var a=ASN1HEX.getPosArrayOfChildren_AtObj(e,d[0]);if(a.length<3){throw"malformed CSR(code:004)"}b.p8pubkeyhex=ASN1HEX.getHexOfTLV_AtObj(e,a[2]);return b};KEYUTIL.getJWKFromKey=function(d){var b={};if(d instanceof RSAKey&&d.isPrivate){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));b.d=hextob64u(d.d.toString(16));b.p=hextob64u(d.p.toString(16));b.q=hextob64u(d.q.toString(16));b.dp=hextob64u(d.dmp1.toString(16));b.dq=hextob64u(d.dmq1.toString(16));b.qi=hextob64u(d.coeff.toString(16));return b}else{if(d instanceof RSAKey&&d.isPublic){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPrivate){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);b.d=hextob64u(d.prvKeyHex);return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPublic){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);return b}}}}throw"not supported key object"}; +var KEYUTIL=function(){var d=function(p,r,q){return k(CryptoJS.AES,p,r,q)};var e=function(p,r,q){return k(CryptoJS.TripleDES,p,r,q)};var a=function(p,r,q){return k(CryptoJS.DES,p,r,q)};var k=function(s,x,u,q){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(u);var p=CryptoJS.enc.Hex.parse(q);var t={};t.key=w;t.iv=p;t.ciphertext=r;var v=s.decrypt(t,w,{iv:p});return CryptoJS.enc.Hex.stringify(v)};var l=function(p,r,q){return g(CryptoJS.AES,p,r,q)};var o=function(p,r,q){return g(CryptoJS.TripleDES,p,r,q)};var f=function(p,r,q){return g(CryptoJS.DES,p,r,q)};var g=function(t,y,v,q){var s=CryptoJS.enc.Hex.parse(y);var x=CryptoJS.enc.Hex.parse(v);var p=CryptoJS.enc.Hex.parse(q);var w=t.encrypt(s,x,{iv:p});var r=CryptoJS.enc.Hex.parse(w.toString());var u=CryptoJS.enc.Base64.stringify(r);return u};var i={"AES-256-CBC":{proc:d,eproc:l,keylen:32,ivlen:16},"AES-192-CBC":{proc:d,eproc:l,keylen:24,ivlen:16},"AES-128-CBC":{proc:d,eproc:l,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:e,eproc:o,keylen:24,ivlen:8},"DES-CBC":{proc:a,eproc:f,keylen:8,ivlen:8}};var c=function(p){return i[p]["proc"]};var m=function(p){var r=CryptoJS.lib.WordArray.random(p);var q=CryptoJS.enc.Hex.stringify(r);return q};var n=function(v){var w={};var q=v.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"));if(q){w.cipher=q[1];w.ivsalt=q[2]}var p=v.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));if(p){w.type=p[1]}var u=-1;var x=0;if(v.indexOf("\r\n\r\n")!=-1){u=v.indexOf("\r\n\r\n");x=2}if(v.indexOf("\n\n")!=-1){u=v.indexOf("\n\n");x=1}var t=v.indexOf("-----END");if(u!=-1&&t!=-1){var r=v.substring(u+x*2,t-x);r=r.replace(/\s+/g,"");w.data=r}return w};var j=function(q,y,p){var v=p.substring(0,16);var t=CryptoJS.enc.Hex.parse(v);var r=CryptoJS.enc.Utf8.parse(y);var u=i[q]["keylen"]+i[q]["ivlen"];var x="";var w=null;for(;;){var s=CryptoJS.algo.MD5.create();if(w!=null){s.update(w)}s.update(r);s.update(t);w=s.finalize();x=x+CryptoJS.enc.Hex.stringify(w);if(x.length>=u*2){break}}var z={};z.keyhex=x.substr(0,i[q]["keylen"]*2);z.ivhex=x.substr(i[q]["keylen"]*2,i[q]["ivlen"]*2);return z};var b=function(p,v,r,w){var s=CryptoJS.enc.Base64.parse(p);var q=CryptoJS.enc.Hex.stringify(s);var u=i[v]["proc"];var t=u(q,r,w);return t};var h=function(p,s,q,u){var r=i[s]["eproc"];var t=r(p,q,u);return t};return{version:"1.0.0",getHexFromPEM:function(q,u){var r=q;if(r.indexOf("-----BEGIN ")==-1){throw"can't find PEM header: "+u}if(typeof u=="string"&&u!=""){r=r.replace("-----BEGIN "+u+"-----","");r=r.replace("-----END "+u+"-----","")}else{r=r.replace(/-----BEGIN [^-]+-----/,"");r=r.replace(/-----END [^-]+-----/,"")}var t=r.replace(/\s+/g,"");var p=b64tohex(t);return p},getDecryptedKeyHexByKeyIV:function(q,t,s,r){var p=c(t);return p(q,s,r)},parsePKCS5PEM:function(p){return n(p)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(q,p,r){return j(q,p,r)},decryptKeyB64:function(p,r,q,s){return b(p,r,q,s)},getDecryptedKeyHex:function(y,x){var q=n(y);var t=q.type;var r=q.cipher;var p=q.ivsalt;var s=q.data;var w=j(r,x,p);var v=w.keyhex;var u=b(s,r,v,p);return u},getRSAKeyFromEncryptedPKCS5PEM:function(r,q){var s=this.getDecryptedKeyHex(r,q);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(s);return p},getEncryptedPKCS5PEMFromPrvKeyHex:function(x,s,A,t,r){var p="";if(typeof t=="undefined"||t==null){t="AES-256-CBC"}if(typeof i[t]=="undefined"){throw"KEYUTIL unsupported algorithm: "+t}if(typeof r=="undefined"||r==null){var v=i[t]["ivlen"];var u=m(v);r=u.toUpperCase()}var z=j(t,A,r);var y=z.keyhex;var w=h(s,t,y,r);var q=w.replace(/(.{64})/g,"$1\r\n");var p="-----BEGIN "+x+" PRIVATE KEY-----\r\n";p+="Proc-Type: 4,ENCRYPTED\r\n";p+="DEK-Info: "+t+","+r+"\r\n";p+="\r\n";p+=q;p+="\r\n-----END "+x+" PRIVATE KEY-----\r\n";return p},getEncryptedPKCS5PEMFromRSAKey:function(D,E,r,t){var B=new KJUR.asn1.DERInteger({"int":0});var w=new KJUR.asn1.DERInteger({bigint:D.n});var A=new KJUR.asn1.DERInteger({"int":D.e});var C=new KJUR.asn1.DERInteger({bigint:D.d});var u=new KJUR.asn1.DERInteger({bigint:D.p});var s=new KJUR.asn1.DERInteger({bigint:D.q});var z=new KJUR.asn1.DERInteger({bigint:D.dmp1});var v=new KJUR.asn1.DERInteger({bigint:D.dmq1});var y=new KJUR.asn1.DERInteger({bigint:D.coeff});var F=new KJUR.asn1.DERSequence({array:[B,w,A,C,u,s,z,v,y]});var x=F.getEncodedHex();return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",x,E,r,t)},newEncryptedPKCS5PEM:function(p,q,t,u){if(typeof q=="undefined"||q==null){q=1024}if(typeof t=="undefined"||t==null){t="10001"}var r=new RSAKey();r.generate(q,t);var s=null;if(typeof u=="undefined"||u==null){s=this.getEncryptedPKCS5PEMFromRSAKey(r,p)}else{s=this.getEncryptedPKCS5PEMFromRSAKey(r,p,u)}return s},getRSAKeyFromPlainPKCS8PEM:function(r){if(r.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var q=this.getHexFromPEM(r,"PRIVATE KEY");var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getRSAKeyFromPlainPKCS8Hex:function(s){var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"outer DERSequence shall have 3 elements: "+r.length}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);if(q!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+q}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);var t=ASN1HEX.getHexOfTLV_AtObj(s,r[2]);var u=ASN1HEX.getHexOfV_AtObj(t,0);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(u);return p},parseHexOfEncryptedPKCS8:function(w){var s={};var r=ASN1HEX.getPosArrayOfChildren_AtObj(w,0);if(r.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+r.length}s.ciphertext=ASN1HEX.getHexOfV_AtObj(w,r[1]);var y=ASN1HEX.getPosArrayOfChildren_AtObj(w,r[0]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+y.length}if(ASN1HEX.getHexOfV_AtObj(w,y[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(w,y[1]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+p.length}var q=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[1]);if(q.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+q.length}if(ASN1HEX.getHexOfV_AtObj(w,q[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}s.encryptionSchemeAlg="TripleDES";s.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(w,q[1]);var t=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[0]);if(t.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+t.length}if(ASN1HEX.getHexOfV_AtObj(w,t[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var x=ASN1HEX.getPosArrayOfChildren_AtObj(w,t[1]);if(x.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+x.length}s.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(w,x[0]);var u=ASN1HEX.getHexOfV_AtObj(w,x[1]);try{s.pbkdf2Iter=parseInt(u,16)}catch(v){throw"malformed format pbkdf2Iter: "+u}return s},getPBKDF2KeyHexFromParam:function(u,p){var t=CryptoJS.enc.Hex.parse(u.pbkdf2Salt);var q=u.pbkdf2Iter;var s=CryptoJS.PBKDF2(p,t,{keySize:192/32,iterations:q});var r=CryptoJS.enc.Hex.stringify(s);return r},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(x,y){var r=this.getHexFromPEM(x,"ENCRYPTED PRIVATE KEY");var p=this.parseHexOfEncryptedPKCS8(r);var u=KEYUTIL.getPBKDF2KeyHexFromParam(p,y);var v={};v.ciphertext=CryptoJS.enc.Hex.parse(p.ciphertext);var t=CryptoJS.enc.Hex.parse(u);var s=CryptoJS.enc.Hex.parse(p.encryptionSchemeIV);var w=CryptoJS.TripleDES.decrypt(v,t,{iv:s});var q=CryptoJS.enc.Hex.stringify(w);return q},getRSAKeyFromEncryptedPKCS8PEM:function(s,r){var q=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,r);var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getKeyFromEncryptedPKCS8PEM:function(s,q){var p=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,q);var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},parsePlainPrivatePKCS8Hex:function(s){var q={};q.algparam=null;if(s.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(s.substr(r[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,r[1]);if(p.length!=2){throw"malformed PKCS8 private key(code:004)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}if(s.substr(r[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}q.keyidx=ASN1HEX.getStartPosOfV_AtObj(s,r[2]);return q},getKeyFromPlainPrivatePKCS8PEM:function(q){var p=this.getHexFromPEM(q,"PRIVATE KEY");var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},getKeyFromPlainPrivatePKCS8Hex:function(p){var w=this.parsePlainPrivatePKCS8Hex(p);if(w.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(p,w);var u=w.key;var z=new RSAKey();z.setPrivateEx(u.n,u.e,u.d,u.p,u.q,u.dp,u.dq,u.co);return z}else{if(w.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(p,w);if(KJUR.crypto.OID.oidhex2name[w.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+w.algparam}var v=KJUR.crypto.OID.oidhex2name[w.algparam];var z=new KJUR.crypto.ECDSA({curve:v});z.setPublicKeyHex(w.pubkey);z.setPrivateKeyHex(w.key);z.isPublic=false;return z}else{if(w.algoid=="2a8648ce380401"){var t=ASN1HEX.getVbyList(p,0,[1,1,0],"02");var s=ASN1HEX.getVbyList(p,0,[1,1,1],"02");var y=ASN1HEX.getVbyList(p,0,[1,1,2],"02");var B=ASN1HEX.getVbyList(p,0,[2,0],"02");var r=new BigInteger(t,16);var q=new BigInteger(s,16);var x=new BigInteger(y,16);var A=new BigInteger(B,16);var z=new KJUR.crypto.DSA();z.setPrivate(r,q,x,null,A);return z}else{throw"unsupported private key algorithm"}}}},getRSAKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getRSAKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8Hex:function(q){var p=this.parsePublicPKCS8Hex(q);if(p.algoid=="2a864886f70d010101"){var u=this.parsePublicRawRSAKeyHex(p.key);var r=new RSAKey();r.setPublic(u.n,u.e);return r}else{if(p.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var s=KJUR.crypto.OID.oidhex2name[p.algparam];var r=new KJUR.crypto.ECDSA({curve:s,pub:p.key});return r}else{if(p.algoid=="2a8648ce380401"){var t=p.algparam;var v=ASN1HEX.getHexOfV_AtObj(p.key,0);var r=new KJUR.crypto.DSA();r.setPublic(new BigInteger(t.p,16),new BigInteger(t.q,16),new BigInteger(t.g,16),new BigInteger(v,16));return r}else{throw"unsupported public key algorithm"}}}},parsePublicRawRSAKeyHex:function(r){var p={};if(r.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"malformed RSA key(code:002)"}if(r.substr(q[0],2)!="02"){throw"malformed RSA key(code:003)"}p.n=ASN1HEX.getHexOfV_AtObj(r,q[0]);if(r.substr(q[1],2)!="02"){throw"malformed RSA key(code:004)"}p.e=ASN1HEX.getHexOfV_AtObj(r,q[1]);return p},parsePrivateRawRSAKeyHexAtObj:function(q,s){var r=s.keyidx;if(q.substr(r,2)!="30"){throw"malformed RSA private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(p.length!=9){throw"malformed RSA private key(code:002)"}s.key={};s.key.n=ASN1HEX.getHexOfV_AtObj(q,p[1]);s.key.e=ASN1HEX.getHexOfV_AtObj(q,p[2]);s.key.d=ASN1HEX.getHexOfV_AtObj(q,p[3]);s.key.p=ASN1HEX.getHexOfV_AtObj(q,p[4]);s.key.q=ASN1HEX.getHexOfV_AtObj(q,p[5]);s.key.dp=ASN1HEX.getHexOfV_AtObj(q,p[6]);s.key.dq=ASN1HEX.getHexOfV_AtObj(q,p[7]);s.key.co=ASN1HEX.getHexOfV_AtObj(q,p[8])},parsePrivateRawECKeyHexAtObj:function(p,t){var q=t.keyidx;var r=ASN1HEX.getVbyList(p,q,[1],"04");var s=ASN1HEX.getVbyList(p,q,[2,0],"03").substr(2);t.key=r;t.pubkey=s},parsePublicPKCS8Hex:function(s){var q={};q.algparam=null;var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=2){throw"outer DERSequence shall have 2 elements: "+r.length}var t=r[0];if(s.substr(t,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,t);if(p.length!=2){throw"malformed PKCS8 public key(code:002)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}else{if(s.substr(p[1],2)=="30"){q.algparam={};q.algparam.p=ASN1HEX.getVbyList(s,p[1],[0],"02");q.algparam.q=ASN1HEX.getVbyList(s,p[1],[1],"02");q.algparam.g=ASN1HEX.getVbyList(s,p[1],[2],"02")}}if(s.substr(r[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}q.key=ASN1HEX.getHexOfV_AtObj(s,r[1]).substr(2);return q},getRSAKeyFromPublicPKCS8Hex:function(t){var s=ASN1HEX.getPosArrayOfChildren_AtObj(t,0);if(s.length!=2){throw"outer DERSequence shall have 2 elements: "+s.length}var r=ASN1HEX.getHexOfTLV_AtObj(t,s[0]);if(r!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(t.substr(s[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var v=ASN1HEX.getStartPosOfV_AtObj(t,s[1])+2;if(t.substr(v,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var p=ASN1HEX.getPosArrayOfChildren_AtObj(t,v);if(p.length!=2){throw"inner DERSequence shall have 2 elements: "+p.length}if(t.substr(p[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(t.substr(p[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var w=ASN1HEX.getHexOfV_AtObj(t,p[0]);var u=ASN1HEX.getHexOfV_AtObj(t,p[1]);var q=new RSAKey();q.setPublic(w,u);return q},}}();KEYUTIL.getKey=function(f,e,h){if(typeof RSAKey!="undefined"&&f instanceof RSAKey){return f}if(typeof KJUR.crypto.ECDSA!="undefined"&&f instanceof KJUR.crypto.ECDSA){return f}if(typeof KJUR.crypto.DSA!="undefined"&&f instanceof KJUR.crypto.DSA){return f}if(f.curve!==undefined&&f.xy!==undefined&&f.d===undefined){return new KJUR.crypto.ECDSA({pub:f.xy,curve:f.curve})}if(f.curve!==undefined&&f.d!==undefined){return new KJUR.crypto.ECDSA({prv:f.d,curve:f.curve})}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(f.n,f.e);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.co!==undefined&&f.qi===undefined){var w=new RSAKey();w.setPrivateEx(f.n,f.e,f.d,f.p,f.q,f.dp,f.dq,f.co);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p===undefined){var w=new RSAKey();w.setPrivate(f.n,f.e,f.d);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x===undefined){var w=new KJUR.crypto.DSA();w.setPublic(f.p,f.q,f.g,f.y);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x!==undefined){var w=new KJUR.crypto.DSA();w.setPrivate(f.p,f.q,f.g,f.y,f.x);return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(b64utohex(f.n),b64utohex(f.e));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.qi!==undefined){var w=new RSAKey();w.setPrivateEx(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d),b64utohex(f.p),b64utohex(f.q),b64utohex(f.dp),b64utohex(f.dq),b64utohex(f.qi));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined){var w=new RSAKey();w.setPrivate(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d));return w}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d===undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;d.setPublicKeyHex(m);return d}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d!==undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;var a=("0000000000"+b64utohex(f.d)).slice(-l);d.setPublicKeyHex(m);d.setPrivateKeyHex(a);return d}if(f.indexOf("-END CERTIFICATE-",0)!=-1||f.indexOf("-END X509 CERTIFICATE-",0)!=-1||f.indexOf("-END TRUSTED CERTIFICATE-",0)!=-1){return X509.getPublicKeyFromCertPEM(f)}if(h==="pkcs8pub"){return KEYUTIL.getKeyFromPublicPKCS8Hex(f)}if(f.indexOf("-END PUBLIC KEY-")!=-1){return KEYUTIL.getKeyFromPublicPKCS8PEM(f)}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var i=KEYUTIL.getHexFromPEM(f,"RSA PRIVATE KEY");return KEYUTIL.getKey(i,null,"pkcs5prv")}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var u=this.getHexFromPEM(f,"DSA PRIVATE KEY");var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(f)}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(f,e)}if(f.indexOf("-END EC PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var w=ASN1HEX.getVbyList(u,0,[1],"04");var c=ASN1HEX.getVbyList(u,0,[2,0],"06");var o=ASN1HEX.getVbyList(u,0,[3,0],"03").substr(2);var b="";if(KJUR.crypto.OID.oidhex2name[c]!==undefined){b=KJUR.crypto.OID.oidhex2name[c]}else{throw"undefined OID(hex) in KJUR.crypto.OID: "+c}var d=new KJUR.crypto.ECDSA({name:b});d.setPublicKeyHex(o);d.setPrivateKeyHex(w);d.isPublic=false;return d}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END ENCRYPTED PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromEncryptedPKCS8PEM(f,e)}throw"not supported argument"};KEYUTIL.generateKeypair=function(a,c){if(a=="RSA"){var b=c;var h=new RSAKey();h.generate(b,"10001");h.isPrivate=true;h.isPublic=true;var f=new RSAKey();var e=h.n.toString(16);var i=h.e.toString(16);f.setPublic(e,i);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{if(a=="EC"){var d=c;var g=new KJUR.crypto.ECDSA({curve:d});var j=g.generateKeyPairHex();var h=new KJUR.crypto.ECDSA({curve:d});h.setPublicKeyHex(j.ecpubhex);h.setPrivateKeyHex(j.ecprvhex);h.isPrivate=true;h.isPublic=false;var f=new KJUR.crypto.ECDSA({curve:d});f.setPublicKeyHex(j.ecpubhex);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{throw"unknown algorithm: "+a}}};KEYUTIL.getPEM=function(a,r,o,g,j){var v=KJUR.asn1;var u=KJUR.crypto;function p(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.n}},{"int":s.e},{"int":{bigint:s.d}},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.dmp1}},{"int":{bigint:s.dmq1}},{"int":{bigint:s.coeff}}]});return w}function q(w){var s=KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:w.prvKeyHex}},{tag:["a0",true,{oid:{name:w.curveName}}]},{tag:["a1",true,{bitstr:{hex:"00"+w.pubKeyHex}}]}]});return s}function n(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.g}},{"int":{bigint:s.y}},{"int":{bigint:s.x}}]});return w}if(((typeof RSAKey!="undefined"&&a instanceof RSAKey)||(typeof u.DSA!="undefined"&&a instanceof u.DSA)||(typeof u.ECDSA!="undefined"&&a instanceof u.ECDSA))&&a.isPublic==true&&(r===undefined||r=="PKCS8PUB")){var t=new KJUR.asn1.x509.SubjectPublicKeyInfo(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"PUBLIC KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o===undefined||o==null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"RSA PRIVATE KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o===undefined||o==null)&&a.isPrivate==true){var f=new KJUR.asn1.DERObjectIdentifier({name:a.curveName});var l=f.getEncodedHex();var e=q(a);var k=e.getEncodedHex();var i="";i+=v.ASN1Util.getPEMStringFromHex(l,"EC PARAMETERS");i+=v.ASN1Util.getPEMStringFromHex(k,"EC PRIVATE KEY");return i}if(r=="PKCS1PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o===undefined||o==null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"DSA PRIVATE KEY")}if(r=="PKCS5PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=q(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA",m,o,g)}var h=function(w,s){var y=b(w,s);var x=new KJUR.asn1.ASN1Util.newObject({seq:[{seq:[{oid:{name:"pkcs5PBES2"}},{seq:[{seq:[{oid:{name:"pkcs5PBKDF2"}},{seq:[{octstr:{hex:y.pbkdf2Salt}},{"int":y.pbkdf2Iter}]}]},{seq:[{oid:{name:"des-EDE3-CBC"}},{octstr:{hex:y.encryptionSchemeIV}}]}]}]},{octstr:{hex:y.ciphertext}}]});return x.getEncodedHex()};var b=function(D,E){var x=100;var C=CryptoJS.lib.WordArray.random(8);var B="DES-EDE3-CBC";var s=CryptoJS.lib.WordArray.random(8);var y=CryptoJS.PBKDF2(E,C,{keySize:192/32,iterations:x});var z=CryptoJS.enc.Hex.parse(D);var A=CryptoJS.TripleDES.encrypt(z,y,{iv:s})+"";var w={};w.ciphertext=A;w.pbkdf2Salt=CryptoJS.enc.Hex.stringify(C);w.pbkdf2Iter=x;w.encryptionSchemeAlg=B;w.encryptionSchemeIV=CryptoJS.enc.Hex.stringify(s);return w};if(r=="PKCS8PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&a.isPrivate==true){var d=p(a);var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"rsaEncryption"}},{"null":true}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&a.isPrivate==true){var d=new KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:a.prvKeyHex}},{tag:["a1",true,{bitstr:{hex:"00"+a.pubKeyHex}}]}]});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"ecPublicKey"}},{oid:{name:a.curveName}}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&a.isPrivate==true){var d=new KJUR.asn1.DERInteger({bigint:a.x});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"dsa"}},{seq:[{"int":{bigint:a.p}},{"int":{bigint:a.q}},{"int":{bigint:a.g}}]}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}throw"unsupported object nor format"};KEYUTIL.getKeyFromCSRPEM=function(b){var a=KEYUTIL.getHexFromPEM(b,"CERTIFICATE REQUEST");var c=KEYUTIL.getKeyFromCSRHex(a);return c};KEYUTIL.getKeyFromCSRHex=function(a){var c=KEYUTIL.parseCSRHex(a);var b=KEYUTIL.getKey(c.p8pubkeyhex,null,"pkcs8pub");return b};KEYUTIL.parseCSRHex=function(c){var b={};var e=c;if(e.substr(0,2)!="30"){throw"malformed CSR(code:001)"}var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,0);if(d.length<1){throw"malformed CSR(code:002)"}if(e.substr(d[0],2)!="30"){throw"malformed CSR(code:003)"}var a=ASN1HEX.getPosArrayOfChildren_AtObj(e,d[0]);if(a.length<3){throw"malformed CSR(code:004)"}b.p8pubkeyhex=ASN1HEX.getHexOfTLV_AtObj(e,a[2]);return b};KEYUTIL.getJWKFromKey=function(d){var b={};if(d instanceof RSAKey&&d.isPrivate){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));b.d=hextob64u(d.d.toString(16));b.p=hextob64u(d.p.toString(16));b.q=hextob64u(d.q.toString(16));b.dp=hextob64u(d.dmp1.toString(16));b.dq=hextob64u(d.dmq1.toString(16));b.qi=hextob64u(d.coeff.toString(16));return b}else{if(d instanceof RSAKey&&d.isPublic){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPrivate){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);b.d=hextob64u(d.prvKeyHex);return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPublic){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);return b}}}}throw"not supported key object"}; /*! rsapem-1.1.js (c) 2012 Kenji Urushima | kjur.github.com/jsrsasign/license */ function _rsapem_pemToBase64(b){var a=b;a=a.replace("-----BEGIN RSA PRIVATE KEY-----","");a=a.replace("-----END RSA PRIVATE KEY-----","");a=a.replace(/[ \n]+/g,"");return a}function _rsapem_getPosArrayOfChildrenFromHex(d){var j=new Array();var k=ASN1HEX.getStartPosOfV_AtObj(d,0);var f=ASN1HEX.getPosOfNextSibling_AtObj(d,k);var h=ASN1HEX.getPosOfNextSibling_AtObj(d,f);var b=ASN1HEX.getPosOfNextSibling_AtObj(d,h);var l=ASN1HEX.getPosOfNextSibling_AtObj(d,b);var e=ASN1HEX.getPosOfNextSibling_AtObj(d,l);var g=ASN1HEX.getPosOfNextSibling_AtObj(d,e);var c=ASN1HEX.getPosOfNextSibling_AtObj(d,g);var i=ASN1HEX.getPosOfNextSibling_AtObj(d,c);j.push(k,f,h,b,l,e,g,c,i);return j}function _rsapem_getHexValueArrayOfChildrenFromHex(i){var o=_rsapem_getPosArrayOfChildrenFromHex(i);var r=ASN1HEX.getHexOfV_AtObj(i,o[0]);var f=ASN1HEX.getHexOfV_AtObj(i,o[1]);var j=ASN1HEX.getHexOfV_AtObj(i,o[2]);var k=ASN1HEX.getHexOfV_AtObj(i,o[3]);var c=ASN1HEX.getHexOfV_AtObj(i,o[4]);var b=ASN1HEX.getHexOfV_AtObj(i,o[5]);var h=ASN1HEX.getHexOfV_AtObj(i,o[6]);var g=ASN1HEX.getHexOfV_AtObj(i,o[7]);var l=ASN1HEX.getHexOfV_AtObj(i,o[8]);var m=new Array();m.push(r,f,j,k,c,b,h,g,l);return m}function _rsapem_readPrivateKeyFromASN1HexString(c){var b=_rsapem_getHexValueArrayOfChildrenFromHex(c);this.setPrivateEx(b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8])}function _rsapem_readPrivateKeyFromPEMString(e){var c=_rsapem_pemToBase64(e);var d=b64tohex(c);var b=_rsapem_getHexValueArrayOfChildrenFromHex(d);this.setPrivateEx(b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8])}RSAKey.prototype.readPrivateKeyFromPEMString=_rsapem_readPrivateKeyFromPEMString;RSAKey.prototype.readPrivateKeyFromASN1HexString=_rsapem_readPrivateKeyFromASN1HexString; @@ -281,9 +281,9 @@ var _RE_HEXDECONLY=new RegExp("");_RE_HEXDECONLY.compile("[^0-9a-f]","gi");funct /*! x509-1.1.9.js (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ function X509(){this.subjectPublicKeyRSA=null;this.subjectPublicKeyRSA_hN=null;this.subjectPublicKeyRSA_hE=null;this.hex=null;this.getSerialNumberHex=function(){return ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,1])};this.getSignatureAlgorithmField=function(){var b=ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,2,0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(b);var c=KJUR.asn1.x509.OID.oid2name(a);return c};this.getIssuerHex=function(){return ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,3])};this.getIssuerString=function(){return X509.hex2dn(ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,3]))};this.getSubjectHex=function(){return ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,5])};this.getSubjectString=function(){return X509.hex2dn(ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,5]))};this.getNotBefore=function(){var a=ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,4,0]);a=a.replace(/(..)/g,"%$1");a=decodeURIComponent(a);return a};this.getNotAfter=function(){var a=ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,4,1]);a=a.replace(/(..)/g,"%$1");a=decodeURIComponent(a);return a};this.readCertPEM=function(c){var e=X509.pemToHex(c);var b=X509.getPublicKeyHexArrayFromCertHex(e);var d=new RSAKey();d.setPublic(b[0],b[1]);this.subjectPublicKeyRSA=d;this.subjectPublicKeyRSA_hN=b[0];this.subjectPublicKeyRSA_hE=b[1];this.hex=e};this.readCertPEMWithoutRSAInit=function(c){var d=X509.pemToHex(c);var b=X509.getPublicKeyHexArrayFromCertHex(d);this.subjectPublicKeyRSA.setPublic(b[0],b[1]);this.subjectPublicKeyRSA_hN=b[0];this.subjectPublicKeyRSA_hE=b[1];this.hex=d};this.getInfo=function(){var p="Basic Fields\n";p+=" serial number: "+this.getSerialNumberHex()+"\n";p+=" signature algorithm: "+this.getSignatureAlgorithmField()+"\n";p+=" issuer: "+this.getIssuerString()+"\n";p+=" notBefore: "+this.getNotBefore()+"\n";p+=" notAfter: "+this.getNotAfter()+"\n";p+=" subject: "+this.getSubjectString()+"\n";p+=" subject public key info: \n";var j=X509.getSubjectPublicKeyInfoPosFromCertHex(this.hex);var d=ASN1HEX.getHexOfTLV_AtObj(this.hex,j);var n=KEYUTIL.getKey(d,null,"pkcs8pub");if(n instanceof RSAKey){p+=" key algorithm: RSA\n";p+=" n="+n.n.toString(16).substr(0,16)+"...\n";p+=" e="+n.e.toString(16)+"\n"}p+="X509v3 Extensions:\n";var m=X509.getV3ExtInfoListOfCertHex(this.hex);for(var e=0;e0){var c=":"+j.join(":")+":";if(c.indexOf(":"+h+":")==-1){throw"algorithm '"+h+"' not accepted in the list"}}if(h!="none"&&t===null){throw"key shall be specified to verify."}if(typeof t=="string"&&t.indexOf("-----BEGIN ")!=-1){t=KEYUTIL.getKey(t)}if(s=="RS"||s=="PS"){if(!(t instanceof RSAKey)){throw"key shall be a RSAKey obj for RS* and PS* algs"}}if(s=="ES"){if(!(t instanceof KJUR.crypto.ECDSA)){throw"key shall be a ECDSA obj for ES* algs"}}if(h=="none"){}var n=null;if(m.jwsalg2sigalg[i.alg]===undefined){throw"unsupported alg name: "+h}else{n=m.jwsalg2sigalg[h]}if(n=="none"){throw"not supported"}else{if(n.substr(0,4)=="Hmac"){var k=null;if(t===undefined){throw"hexadecimal key shall be specified for HMAC"}var g=new KJUR.crypto.Mac({alg:n,pass:t});g.updateString(b);k=g.doFinal();return r==k}else{if(n.indexOf("withECDSA")!=-1){var f=null;try{f=KJUR.crypto.ECDSA.concatSigToASN1Sig(r)}catch(o){return false}var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(f)}else{var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(r)}}}};KJUR.jws.JWS.parse=function(g){var c=g.split(".");var b={};var f,e,d;if(c.length!=2&&c.length!=3){throw"malformed sJWS: wrong number of '.' splitted elements"}f=c[0];e=c[1];if(c.length==3){d=c[2]}b.headerObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(f));b.payloadObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(e));b.headerPP=JSON.stringify(b.headerObj,null," ");if(b.payloadObj==null){b.payloadPP=b64utoutf8(e)}else{b.payloadPP=JSON.stringify(b.payloadObj,null," ")}if(d!==undefined){b.sigHex=b64utohex(d)}return b};KJUR.jws.JWS.verifyJWT=function(d,j,l){var h=KJUR.jws.JWS;var i=d.split(".");var c=i[0];var g=i[1];var m=c+"."+g;var k=b64utohex(i[2]);var f=h.readSafeJSONString(b64utoutf8(c));var e=h.readSafeJSONString(b64utoutf8(g));if(f.alg===undefined){return false}if(l.alg===undefined){throw"acceptField.alg shall be specified"}if(!h.inArray(f.alg,l.alg)){return false}if(e.iss!==undefined&&typeof l.iss==="object"){if(!h.inArray(e.iss,l.iss)){return false}}if(e.sub!==undefined&&typeof l.sub==="object"){if(!h.inArray(e.sub,l.sub)){return false}}if(e.aud!==undefined&&typeof l.aud==="object"){if(typeof e.aud=="string"){if(!h.inArray(e.aud,l.aud)){return false}}else{if(typeof e.aud=="object"){if(!h.includedArray(e.aud,l.aud)){return false}}}}var b=KJUR.jws.IntDate.getNow();if(l.verifyAt!==undefined&&typeof l.verifyAt==="number"){b=l.verifyAt}if(l.gracePeriod===undefined||typeof l.gracePeriod!=="number"){l.gracePeriod=0}if(e.exp!==undefined&&typeof e.exp=="number"){if(e.exp+l.gracePeriod0){var c=":"+j.join(":")+":";if(c.indexOf(":"+h+":")==-1){throw"algorithm '"+h+"' not accepted in the list"}}if(h!="none"&&t===null){throw"key shall be specified to verify."}if(typeof t=="string"&&t.indexOf("-----BEGIN ")!=-1){t=KEYUTIL.getKey(t)}if(s=="RS"||s=="PS"){if(!(t instanceof RSAKey)){throw"key shall be a RSAKey obj for RS* and PS* algs"}}if(s=="ES"){if(!(t instanceof KJUR.crypto.ECDSA)){throw"key shall be a ECDSA obj for ES* algs"}}if(h=="none"){}var n=null;if(m.jwsalg2sigalg[i.alg]===undefined){throw"unsupported alg name: "+h}else{n=m.jwsalg2sigalg[h]}if(n=="none"){throw"not supported"}else{if(n.substr(0,4)=="Hmac"){var k=null;if(t===undefined){throw"hexadecimal key shall be specified for HMAC"}var g=new KJUR.crypto.Mac({alg:n,pass:t});g.updateString(b);k=g.doFinal();return r==k}else{if(n.indexOf("withECDSA")!=-1){var f=null;try{f=KJUR.crypto.ECDSA.concatSigToASN1Sig(r)}catch(o){return false}var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(f)}else{var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(r)}}}};KJUR.jws.JWS.parse=function(g){var c=g.split(".");var b={};var f,e,d;if(c.length!=2&&c.length!=3){throw"malformed sJWS: wrong number of '.' splitted elements"}f=c[0];e=c[1];if(c.length==3){d=c[2]}b.headerObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(f));b.payloadObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(e));b.headerPP=JSON.stringify(b.headerObj,null," ");if(b.payloadObj==null){b.payloadPP=b64utoutf8(e)}else{b.payloadPP=JSON.stringify(b.payloadObj,null," ")}if(d!==undefined){b.sigHex=b64utohex(d)}return b};KJUR.jws.JWS.verifyJWT=function(d,j,l){var h=KJUR.jws.JWS;var i=d.split(".");var c=i[0];var g=i[1];var m=c+"."+g;var k=b64utohex(i[2]);var f=h.readSafeJSONString(b64utoutf8(c));var e=h.readSafeJSONString(b64utoutf8(g));if(f.alg===undefined){return false}if(l.alg===undefined){throw"acceptField.alg shall be specified"}if(!h.inArray(f.alg,l.alg)){return false}if(e.iss!==undefined&&typeof l.iss==="object"){if(!h.inArray(e.iss,l.iss)){return false}}if(e.sub!==undefined&&typeof l.sub==="object"){if(!h.inArray(e.sub,l.sub)){return false}}if(e.aud!==undefined&&typeof l.aud==="object"){if(typeof e.aud=="string"){if(!h.inArray(e.aud,l.aud)){return false}}else{if(typeof e.aud=="object"){if(!h.includedArray(e.aud,l.aud)){return false}}}}var b=KJUR.jws.IntDate.getNow();if(l.verifyAt!==undefined&&typeof l.verifyAt==="number"){b=l.verifyAt}if(l.gracePeriod===undefined||typeof l.gracePeriod!=="number"){l.gracePeriod=0}if(e.exp!==undefined&&typeof e.exp=="number"){if(e.exp+l.gracePeriodj){this.aHeader.pop()}if(this.aSignature.length>j){this.aSignature.pop()}throw"addSignature failed: "+g}};this.addSignatureByHeaderKey=function(f,c){var e=b64utoutf8(this.sPayload);var d=new KJUR.jws.JWS();var g=d.generateJWSByP1PrvKey(f,e,c);this.aHeader.push(d.parsedJWS.headB64U);this.aSignature.push(d.parsedJWS.sigvalB64U)};this.addSignatureByHeaderPayloadKey=function(f,e,c){var d=new KJUR.jws.JWS();var g=d.generateJWSByP1PrvKey(f,e,c);this.aHeader.push(d.parsedJWS.headB64U);this.sPayload=d.parsedJWS.payloadB64U;this.aSignature.push(d.parsedJWS.sigvalB64U)};this.verifyAll=function(f){if(this.aHeader.length!==f.length||this.aSignature.length!==f.length){return false}for(var e=0;e0){this.aHeader=e.headers}else{throw"malformed header"}if(typeof e.payload==="string"){this.sPayload=e.payload}else{throw"malformed signatures"}if(e.signatures.length>0){this.signatures=e.signatures}else{throw"malformed signatures"}}catch(c){throw"malformed JWS-JS JSON object: "+c}}};this.getJSON=function(){return{headers:this.aHeader,payload:this.sPayload,signatures:this.aSignature}};this.isEmpty=function(){if(this.aHeader.length==0){return 1}return 0}}; diff --git a/jws-3.3.js b/jws-3.3.js index 8c5bf18c..5308e3ae 100644 --- a/jws-3.3.js +++ b/jws-3.3.js @@ -1,4 +1,4 @@ -/*! jws-3.3.4 (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! jws-3.3.5 (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* * jws.js - JSON Web Signature(JWS) and JSON Web Token(JWT) Class @@ -18,7 +18,7 @@ * @fileOverview * @name jws-3.3.js * @author Kenji Urushima kenji.urushima@gmail.com - * @version 3.3.4 (2016-May-17) + * @version 3.3.5 (2016-Oct-08) * @since jsjws 1.0, jsrsasign 4.8.0 * @license MIT License */ diff --git a/keyutil-1.0.js b/keyutil-1.0.js index ea38df94..6ea28833 100644 --- a/keyutil-1.0.js +++ b/keyutil-1.0.js @@ -1,4 +1,4 @@ -/*! keyutil-1.0.13.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! keyutil-1.0.14.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* * keyutil.js - key utility for PKCS#1/5/8 PEM, RSA/DSA/ECDSA key object @@ -15,7 +15,7 @@ * @fileOverview * @name keyutil-1.0.js * @author Kenji Urushima kenji.urushima@gmail.com - * @version keyutil 1.0.13 (2016-Aug-11) + * @version keyutil 1.0.14 (2016-Oct-08) * @since jsrsasign 4.1.4 * @license MIT License */ diff --git a/min/asn1hex-1.1.min.js b/min/asn1hex-1.1.min.js index 3f968f2e..ea9bbd21 100644 --- a/min/asn1hex-1.1.min.js +++ b/min/asn1hex-1.1.min.js @@ -1,3 +1,3 @@ -/*! asn1hex-1.1.6.js (c) 2012-2015 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! asn1hex-1.1.7.js (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ -var ASN1HEX=new function(){this.getByteLengthOfL_AtObj=function(b,c){if(b.substring(c+2,c+3)!="8"){return 1}var a=parseInt(b.substring(c+3,c+4));if(a==0){return -1}if(0=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c};this.getNthChildIndex_AtObj=function(d,b,e){var c=this.getPosArrayOfChildren_AtObj(d,b);return c[e]};this.getDecendantIndexByNthList=function(e,d,c){if(c.length==0){return d}var f=c.shift();var b=this.getPosArrayOfChildren_AtObj(e,d);return this.getDecendantIndexByNthList(e,b[f],c)};this.getDecendantHexTLVByNthList=function(d,c,b){var a=this.getDecendantIndexByNthList(d,c,b);return this.getHexOfTLV_AtObj(d,a)};this.getDecendantHexVByNthList=function(d,c,b){var a=this.getDecendantIndexByNthList(d,c,b);return this.getHexOfV_AtObj(d,a)}};ASN1HEX.getVbyList=function(d,c,b,e){var a=this.getDecendantIndexByNthList(d,c,b);if(a===undefined){throw"can't find nthList object"}if(e!==undefined){if(d.substr(a,2)!=e){throw"checking tag doesn't match: "+d.substr(a,2)+"!="+e}}return this.getHexOfV_AtObj(d,a)};ASN1HEX.hextooidstr=function(e){var h=function(b,a){if(b.length>=a){return b}return new Array(a-b.length+1).join("0")+b};var l=[];var o=e.substr(0,2);var f=parseInt(o,16);l[0]=new String(Math.floor(f/40));l[1]=new String(f%40);var m=e.substr(2);var k=[];for(var g=0;g0){n=n+"."+j.join(".")}return n};ASN1HEX.dump=function(e,c,k,g){var o=function(w,i){if(w.length<=i*2){return w}else{var v=w.substr(0,i)+"..(total "+w.length/2+"bytes).."+w.substr(w.length-i,i);return v}};if(c===undefined){c={ommit_long_octet:32}}if(k===undefined){k=0}if(g===undefined){g=""}var r=c.ommit_long_octet;if(e.substr(k,2)=="01"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(h=="00"){return g+"BOOLEAN FALSE\n"}else{return g+"BOOLEAN TRUE\n"}}if(e.substr(k,2)=="02"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"INTEGER "+o(h,r)+"\n"}if(e.substr(k,2)=="03"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"BITSTRING "+o(h,r)+"\n"}if(e.substr(k,2)=="04"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(ASN1HEX.isASN1HEX(h)){var j=g+"OCTETSTRING, encapsulates\n";j=j+ASN1HEX.dump(h,c,0,g+" ");return j}else{return g+"OCTETSTRING "+o(h,r)+"\n"}}if(e.substr(k,2)=="05"){return g+"NULL\n"}if(e.substr(k,2)=="06"){var l=ASN1HEX.getHexOfV_AtObj(e,k);var a=KJUR.asn1.ASN1Util.oidHexToInt(l);var n=KJUR.asn1.x509.OID.oid2name(a);var b=a.replace(/\./g," ");if(n!=""){return g+"ObjectIdentifier "+n+" ("+b+")\n"}else{return g+"ObjectIdentifier ("+b+")\n"}}if(e.substr(k,2)=="0c"){return g+"UTF8String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="13"){return g+"PrintableString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="14"){return g+"TeletexString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="16"){return g+"IA5String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="17"){return g+"UTCTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="18"){return g+"GeneralizedTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="30"){if(e.substr(k,4)=="3000"){return g+"SEQUENCE {}\n"}var j=g+"SEQUENCE\n";var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,k);var f=c;if((d.length==2||d.length==3)&&e.substr(d[0],2)=="06"&&e.substr(d[d.length-1],2)=="04"){var t=ASN1HEX.getHexOfV_AtObj(e,d[0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(t);var n=KJUR.asn1.x509.OID.oid2name(a);var p=JSON.parse(JSON.stringify(c));p.x509ExtName=n;f=p}for(var q=0;q=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c};ASN1HEX.getNthChildIndex_AtObj=function(d,b,e){var c=ASN1HEX.getPosArrayOfChildren_AtObj(d,b);return c[e]};ASN1HEX.getDecendantIndexByNthList=function(e,d,c){if(c.length==0){return d}var f=c.shift();var b=ASN1HEX.getPosArrayOfChildren_AtObj(e,d);return ASN1HEX.getDecendantIndexByNthList(e,b[f],c)};ASN1HEX.getDecendantHexTLVByNthList=function(d,c,b){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);return ASN1HEX.getHexOfTLV_AtObj(d,a)};ASN1HEX.getDecendantHexVByNthList=function(d,c,b){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);return ASN1HEX.getHexOfV_AtObj(d,a)};ASN1HEX.getVbyList=function(d,c,b,e){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);if(a===undefined){throw"can't find nthList object"}if(e!==undefined){if(d.substr(a,2)!=e){throw"checking tag doesn't match: "+d.substr(a,2)+"!="+e}}return ASN1HEX.getHexOfV_AtObj(d,a)};ASN1HEX.hextooidstr=function(e){var h=function(b,a){if(b.length>=a){return b}return new Array(a-b.length+1).join("0")+b};var l=[];var o=e.substr(0,2);var f=parseInt(o,16);l[0]=new String(Math.floor(f/40));l[1]=new String(f%40);var m=e.substr(2);var k=[];for(var g=0;g0){n=n+"."+j.join(".")}return n};ASN1HEX.dump=function(e,c,k,g){var o=function(w,i){if(w.length<=i*2){return w}else{var v=w.substr(0,i)+"..(total "+w.length/2+"bytes).."+w.substr(w.length-i,i);return v}};if(c===undefined){c={ommit_long_octet:32}}if(k===undefined){k=0}if(g===undefined){g=""}var r=c.ommit_long_octet;if(e.substr(k,2)=="01"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(h=="00"){return g+"BOOLEAN FALSE\n"}else{return g+"BOOLEAN TRUE\n"}}if(e.substr(k,2)=="02"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"INTEGER "+o(h,r)+"\n"}if(e.substr(k,2)=="03"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"BITSTRING "+o(h,r)+"\n"}if(e.substr(k,2)=="04"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(ASN1HEX.isASN1HEX(h)){var j=g+"OCTETSTRING, encapsulates\n";j=j+ASN1HEX.dump(h,c,0,g+" ");return j}else{return g+"OCTETSTRING "+o(h,r)+"\n"}}if(e.substr(k,2)=="05"){return g+"NULL\n"}if(e.substr(k,2)=="06"){var l=ASN1HEX.getHexOfV_AtObj(e,k);var a=KJUR.asn1.ASN1Util.oidHexToInt(l);var n=KJUR.asn1.x509.OID.oid2name(a);var b=a.replace(/\./g," ");if(n!=""){return g+"ObjectIdentifier "+n+" ("+b+")\n"}else{return g+"ObjectIdentifier ("+b+")\n"}}if(e.substr(k,2)=="0c"){return g+"UTF8String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="13"){return g+"PrintableString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="14"){return g+"TeletexString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="16"){return g+"IA5String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="17"){return g+"UTCTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="18"){return g+"GeneralizedTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="30"){if(e.substr(k,4)=="3000"){return g+"SEQUENCE {}\n"}var j=g+"SEQUENCE\n";var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,k);var f=c;if((d.length==2||d.length==3)&&e.substr(d[0],2)=="06"&&e.substr(d[d.length-1],2)=="04"){var t=ASN1HEX.getHexOfV_AtObj(e,d[0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(t);var n=KJUR.asn1.x509.OID.oid2name(a);var p=JSON.parse(JSON.stringify(c));p.x509ExtName=n;f=p}for(var q=0;q0){var d=new KJUR.asn1.DERSequence({array:this.extensionsArray});var b=new KJUR.asn1.DERTaggedObject({explicit:true,tag:"a3",obj:d});this.asn1Array.push(b)}var e=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=e.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertificate,KJUR.asn1.ASN1Object);KJUR.asn1.x509.Extension=function(b){KJUR.asn1.x509.Extension.superclass.constructor.call(this);var a=null;this.getEncodedHex=function(){var f=new KJUR.asn1.DERObjectIdentifier({oid:this.oid});var e=new KJUR.asn1.DEROctetString({hex:this.getExtnValueHex()});var d=new Array();d.push(f);if(this.critical){d.push(new KJUR.asn1.DERBoolean())}d.push(e);var c=new KJUR.asn1.DERSequence({array:d});return c.getEncodedHex()};this.critical=false;if(typeof b!="undefined"){if(typeof b.critical!="undefined"){this.critical=b.critical}}};YAHOO.lang.extend(KJUR.asn1.x509.Extension,KJUR.asn1.ASN1Object);KJUR.asn1.x509.KeyUsage=function(a){KJUR.asn1.x509.KeyUsage.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.15";if(typeof a!="undefined"){if(typeof a.bin!="undefined"){this.asn1ExtnValue=new KJUR.asn1.DERBitString(a)}}};YAHOO.lang.extend(KJUR.asn1.x509.KeyUsage,KJUR.asn1.x509.Extension);KJUR.asn1.x509.BasicConstraints=function(c){KJUR.asn1.x509.BasicConstraints.superclass.constructor.call(this,c);var a=false;var b=-1;this.getExtnValueHex=function(){var e=new Array();if(this.cA){e.push(new KJUR.asn1.DERBoolean())}if(this.pathLen>-1){e.push(new KJUR.asn1.DERInteger({"int":this.pathLen}))}var d=new KJUR.asn1.DERSequence({array:e});this.asn1ExtnValue=d;return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.19";this.cA=false;this.pathLen=-1;if(typeof c!="undefined"){if(typeof c.cA!="undefined"){this.cA=c.cA}if(typeof c.pathLen!="undefined"){this.pathLen=c.pathLen}}};YAHOO.lang.extend(KJUR.asn1.x509.BasicConstraints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.CRLDistributionPoints=function(a){KJUR.asn1.x509.CRLDistributionPoints.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.setByDPArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence({array:b})};this.setByOneURI=function(e){var b=new KJUR.asn1.x509.GeneralNames([{uri:e}]);var d=new KJUR.asn1.x509.DistributionPointName(b);var c=new KJUR.asn1.x509.DistributionPoint({dpobj:d});this.setByDPArray([c])};this.oid="2.5.29.31";if(typeof a!="undefined"){if(typeof a.array!="undefined"){this.setByDPArray(a.array)}else{if(typeof a.uri!="undefined"){this.setByOneURI(a.uri)}}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLDistributionPoints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.ExtKeyUsage=function(a){KJUR.asn1.x509.ExtKeyUsage.superclass.constructor.call(this,a);this.setPurposeArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence();for(var c=0;c0){var c=new KJUR.asn1.DERSequence({array:this.aRevokedCert});this.asn1Array.push(c)}var d=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=d.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize=function(){this.asn1Version=null;this.asn1SignatureAlg=null;this.asn1Issuer=null;this.asn1ThisUpdate=null;this.asn1NextUpdate=null;this.aRevokedCert=new Array()};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertList,KJUR.asn1.ASN1Object);KJUR.asn1.x509.CRLEntry=function(c){KJUR.asn1.x509.CRLEntry.superclass.constructor.call(this);var b=null;var a=null;this.setCertSerial=function(d){this.sn=new KJUR.asn1.DERInteger(d)};this.setRevocationDate=function(d){this.time=new KJUR.asn1.x509.Time(d)};this.getEncodedHex=function(){var d=new KJUR.asn1.DERSequence({array:[this.sn,this.time]});this.TLV=d.getEncodedHex();return this.TLV};if(typeof c!="undefined"){if(typeof c.time!="undefined"){this.setRevocationDate(c.time)}if(typeof c.sn!="undefined"){this.setCertSerial(c.sn)}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLEntry,KJUR.asn1.ASN1Object);KJUR.asn1.x509.X500Name=function(b){KJUR.asn1.x509.X500Name.superclass.constructor.call(this);this.asn1Array=new Array();this.setByString=function(c){var d=c.split("/");d.shift();for(var e=0;e0){var d=new KJUR.asn1.DERSequence({array:this.extensionsArray});var b=new KJUR.asn1.DERTaggedObject({explicit:true,tag:"a3",obj:d});this.asn1Array.push(b)}var e=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=e.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertificate,KJUR.asn1.ASN1Object);KJUR.asn1.x509.Extension=function(b){KJUR.asn1.x509.Extension.superclass.constructor.call(this);var a=null;this.getEncodedHex=function(){var f=new KJUR.asn1.DERObjectIdentifier({oid:this.oid});var e=new KJUR.asn1.DEROctetString({hex:this.getExtnValueHex()});var d=new Array();d.push(f);if(this.critical){d.push(new KJUR.asn1.DERBoolean())}d.push(e);var c=new KJUR.asn1.DERSequence({array:d});return c.getEncodedHex()};this.critical=false;if(typeof b!="undefined"){if(typeof b.critical!="undefined"){this.critical=b.critical}}};YAHOO.lang.extend(KJUR.asn1.x509.Extension,KJUR.asn1.ASN1Object);KJUR.asn1.x509.KeyUsage=function(a){KJUR.asn1.x509.KeyUsage.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.15";if(typeof a!="undefined"){if(typeof a.bin!="undefined"){this.asn1ExtnValue=new KJUR.asn1.DERBitString(a)}}};YAHOO.lang.extend(KJUR.asn1.x509.KeyUsage,KJUR.asn1.x509.Extension);KJUR.asn1.x509.BasicConstraints=function(c){KJUR.asn1.x509.BasicConstraints.superclass.constructor.call(this,c);var a=false;var b=-1;this.getExtnValueHex=function(){var e=new Array();if(this.cA){e.push(new KJUR.asn1.DERBoolean())}if(this.pathLen>-1){e.push(new KJUR.asn1.DERInteger({"int":this.pathLen}))}var d=new KJUR.asn1.DERSequence({array:e});this.asn1ExtnValue=d;return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.19";this.cA=false;this.pathLen=-1;if(typeof c!="undefined"){if(typeof c.cA!="undefined"){this.cA=c.cA}if(typeof c.pathLen!="undefined"){this.pathLen=c.pathLen}}};YAHOO.lang.extend(KJUR.asn1.x509.BasicConstraints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.CRLDistributionPoints=function(a){KJUR.asn1.x509.CRLDistributionPoints.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.setByDPArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence({array:b})};this.setByOneURI=function(e){var b=new KJUR.asn1.x509.GeneralNames([{uri:e}]);var d=new KJUR.asn1.x509.DistributionPointName(b);var c=new KJUR.asn1.x509.DistributionPoint({dpobj:d});this.setByDPArray([c])};this.oid="2.5.29.31";if(typeof a!="undefined"){if(typeof a.array!="undefined"){this.setByDPArray(a.array)}else{if(typeof a.uri!="undefined"){this.setByOneURI(a.uri)}}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLDistributionPoints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.ExtKeyUsage=function(a){KJUR.asn1.x509.ExtKeyUsage.superclass.constructor.call(this,a);this.setPurposeArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence();for(var c=0;c0){var c=new KJUR.asn1.DERSequence({array:this.aRevokedCert});this.asn1Array.push(c)}var d=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=d.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize=function(){this.asn1Version=null;this.asn1SignatureAlg=null;this.asn1Issuer=null;this.asn1ThisUpdate=null;this.asn1NextUpdate=null;this.aRevokedCert=new Array()};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertList,KJUR.asn1.ASN1Object);KJUR.asn1.x509.CRLEntry=function(c){KJUR.asn1.x509.CRLEntry.superclass.constructor.call(this);var b=null;var a=null;this.setCertSerial=function(d){this.sn=new KJUR.asn1.DERInteger(d)};this.setRevocationDate=function(d){this.time=new KJUR.asn1.x509.Time(d)};this.getEncodedHex=function(){var d=new KJUR.asn1.DERSequence({array:[this.sn,this.time]});this.TLV=d.getEncodedHex();return this.TLV};if(typeof c!="undefined"){if(typeof c.time!="undefined"){this.setRevocationDate(c.time)}if(typeof c.sn!="undefined"){this.setCertSerial(c.sn)}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLEntry,KJUR.asn1.ASN1Object);KJUR.asn1.x509.X500Name=function(b){KJUR.asn1.x509.X500Name.superclass.constructor.call(this);this.asn1Array=new Array();this.setByString=function(c){var d=c.split("/");d.shift();for(var e=0;ed){throw"key is too short for SigAlg: keylen="+j+","+a}var b="0001";var k="00"+c;var g="";var l=d-b.length-k.length;for(var f=0;fd){throw"key is too short for SigAlg: keylen="+j+","+a}var b="0001";var k="00"+c;var g="";var l=d-b.length-k.length;for(var f=0;f0){var c=":"+j.join(":")+":";if(c.indexOf(":"+h+":")==-1){throw"algorithm '"+h+"' not accepted in the list"}}if(h!="none"&&t===null){throw"key shall be specified to verify."}if(typeof t=="string"&&t.indexOf("-----BEGIN ")!=-1){t=KEYUTIL.getKey(t)}if(s=="RS"||s=="PS"){if(!(t instanceof RSAKey)){throw"key shall be a RSAKey obj for RS* and PS* algs"}}if(s=="ES"){if(!(t instanceof KJUR.crypto.ECDSA)){throw"key shall be a ECDSA obj for ES* algs"}}if(h=="none"){}var n=null;if(m.jwsalg2sigalg[i.alg]===undefined){throw"unsupported alg name: "+h}else{n=m.jwsalg2sigalg[h]}if(n=="none"){throw"not supported"}else{if(n.substr(0,4)=="Hmac"){var k=null;if(t===undefined){throw"hexadecimal key shall be specified for HMAC"}var g=new KJUR.crypto.Mac({alg:n,pass:t});g.updateString(b);k=g.doFinal();return r==k}else{if(n.indexOf("withECDSA")!=-1){var f=null;try{f=KJUR.crypto.ECDSA.concatSigToASN1Sig(r)}catch(o){return false}var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(f)}else{var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(r)}}}};KJUR.jws.JWS.parse=function(g){var c=g.split(".");var b={};var f,e,d;if(c.length!=2&&c.length!=3){throw"malformed sJWS: wrong number of '.' splitted elements"}f=c[0];e=c[1];if(c.length==3){d=c[2]}b.headerObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(f));b.payloadObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(e));b.headerPP=JSON.stringify(b.headerObj,null," ");if(b.payloadObj==null){b.payloadPP=b64utoutf8(e)}else{b.payloadPP=JSON.stringify(b.payloadObj,null," ")}if(d!==undefined){b.sigHex=b64utohex(d)}return b};KJUR.jws.JWS.verifyJWT=function(d,j,l){var h=KJUR.jws.JWS;var i=d.split(".");var c=i[0];var g=i[1];var m=c+"."+g;var k=b64utohex(i[2]);var f=h.readSafeJSONString(b64utoutf8(c));var e=h.readSafeJSONString(b64utoutf8(g));if(f.alg===undefined){return false}if(l.alg===undefined){throw"acceptField.alg shall be specified"}if(!h.inArray(f.alg,l.alg)){return false}if(e.iss!==undefined&&typeof l.iss==="object"){if(!h.inArray(e.iss,l.iss)){return false}}if(e.sub!==undefined&&typeof l.sub==="object"){if(!h.inArray(e.sub,l.sub)){return false}}if(e.aud!==undefined&&typeof l.aud==="object"){if(typeof e.aud=="string"){if(!h.inArray(e.aud,l.aud)){return false}}else{if(typeof e.aud=="object"){if(!h.includedArray(e.aud,l.aud)){return false}}}}var b=KJUR.jws.IntDate.getNow();if(l.verifyAt!==undefined&&typeof l.verifyAt==="number"){b=l.verifyAt}if(l.gracePeriod===undefined||typeof l.gracePeriod!=="number"){l.gracePeriod=0}if(e.exp!==undefined&&typeof e.exp=="number"){if(e.exp+l.gracePeriod0){var c=":"+j.join(":")+":";if(c.indexOf(":"+h+":")==-1){throw"algorithm '"+h+"' not accepted in the list"}}if(h!="none"&&t===null){throw"key shall be specified to verify."}if(typeof t=="string"&&t.indexOf("-----BEGIN ")!=-1){t=KEYUTIL.getKey(t)}if(s=="RS"||s=="PS"){if(!(t instanceof RSAKey)){throw"key shall be a RSAKey obj for RS* and PS* algs"}}if(s=="ES"){if(!(t instanceof KJUR.crypto.ECDSA)){throw"key shall be a ECDSA obj for ES* algs"}}if(h=="none"){}var n=null;if(m.jwsalg2sigalg[i.alg]===undefined){throw"unsupported alg name: "+h}else{n=m.jwsalg2sigalg[h]}if(n=="none"){throw"not supported"}else{if(n.substr(0,4)=="Hmac"){var k=null;if(t===undefined){throw"hexadecimal key shall be specified for HMAC"}var g=new KJUR.crypto.Mac({alg:n,pass:t});g.updateString(b);k=g.doFinal();return r==k}else{if(n.indexOf("withECDSA")!=-1){var f=null;try{f=KJUR.crypto.ECDSA.concatSigToASN1Sig(r)}catch(o){return false}var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(f)}else{var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(r)}}}};KJUR.jws.JWS.parse=function(g){var c=g.split(".");var b={};var f,e,d;if(c.length!=2&&c.length!=3){throw"malformed sJWS: wrong number of '.' splitted elements"}f=c[0];e=c[1];if(c.length==3){d=c[2]}b.headerObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(f));b.payloadObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(e));b.headerPP=JSON.stringify(b.headerObj,null," ");if(b.payloadObj==null){b.payloadPP=b64utoutf8(e)}else{b.payloadPP=JSON.stringify(b.payloadObj,null," ")}if(d!==undefined){b.sigHex=b64utohex(d)}return b};KJUR.jws.JWS.verifyJWT=function(d,j,l){var h=KJUR.jws.JWS;var i=d.split(".");var c=i[0];var g=i[1];var m=c+"."+g;var k=b64utohex(i[2]);var f=h.readSafeJSONString(b64utoutf8(c));var e=h.readSafeJSONString(b64utoutf8(g));if(f.alg===undefined){return false}if(l.alg===undefined){throw"acceptField.alg shall be specified"}if(!h.inArray(f.alg,l.alg)){return false}if(e.iss!==undefined&&typeof l.iss==="object"){if(!h.inArray(e.iss,l.iss)){return false}}if(e.sub!==undefined&&typeof l.sub==="object"){if(!h.inArray(e.sub,l.sub)){return false}}if(e.aud!==undefined&&typeof l.aud==="object"){if(typeof e.aud=="string"){if(!h.inArray(e.aud,l.aud)){return false}}else{if(typeof e.aud=="object"){if(!h.includedArray(e.aud,l.aud)){return false}}}}var b=KJUR.jws.IntDate.getNow();if(l.verifyAt!==undefined&&typeof l.verifyAt==="number"){b=l.verifyAt}if(l.gracePeriod===undefined||typeof l.gracePeriod!=="number"){l.gracePeriod=0}if(e.exp!==undefined&&typeof e.exp=="number"){if(e.exp+l.gracePeriod=u*2){break}}var z={};z.keyhex=x.substr(0,i[q]["keylen"]*2);z.ivhex=x.substr(i[q]["keylen"]*2,i[q]["ivlen"]*2);return z};var b=function(p,v,r,w){var s=CryptoJS.enc.Base64.parse(p);var q=CryptoJS.enc.Hex.stringify(s);var u=i[v]["proc"];var t=u(q,r,w);return t};var h=function(p,s,q,u){var r=i[s]["eproc"];var t=r(p,q,u);return t};return{version:"1.0.0",getHexFromPEM:function(q,u){var r=q;if(r.indexOf("-----BEGIN ")==-1){throw"can't find PEM header: "+u}if(typeof u=="string"&&u!=""){r=r.replace("-----BEGIN "+u+"-----","");r=r.replace("-----END "+u+"-----","")}else{r=r.replace(/-----BEGIN [^-]+-----/,"");r=r.replace(/-----END [^-]+-----/,"")}var t=r.replace(/\s+/g,"");var p=b64tohex(t);return p},getDecryptedKeyHexByKeyIV:function(q,t,s,r){var p=c(t);return p(q,s,r)},parsePKCS5PEM:function(p){return n(p)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(q,p,r){return j(q,p,r)},decryptKeyB64:function(p,r,q,s){return b(p,r,q,s)},getDecryptedKeyHex:function(y,x){var q=n(y);var t=q.type;var r=q.cipher;var p=q.ivsalt;var s=q.data;var w=j(r,x,p);var v=w.keyhex;var u=b(s,r,v,p);return u},getRSAKeyFromEncryptedPKCS5PEM:function(r,q){var s=this.getDecryptedKeyHex(r,q);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(s);return p},getEncryptedPKCS5PEMFromPrvKeyHex:function(x,s,A,t,r){var p="";if(typeof t=="undefined"||t==null){t="AES-256-CBC"}if(typeof i[t]=="undefined"){throw"KEYUTIL unsupported algorithm: "+t}if(typeof r=="undefined"||r==null){var v=i[t]["ivlen"];var u=m(v);r=u.toUpperCase()}var z=j(t,A,r);var y=z.keyhex;var w=h(s,t,y,r);var q=w.replace(/(.{64})/g,"$1\r\n");var p="-----BEGIN "+x+" PRIVATE KEY-----\r\n";p+="Proc-Type: 4,ENCRYPTED\r\n";p+="DEK-Info: "+t+","+r+"\r\n";p+="\r\n";p+=q;p+="\r\n-----END "+x+" PRIVATE KEY-----\r\n";return p},getEncryptedPKCS5PEMFromRSAKey:function(D,E,r,t){var B=new KJUR.asn1.DERInteger({"int":0});var w=new KJUR.asn1.DERInteger({bigint:D.n});var A=new KJUR.asn1.DERInteger({"int":D.e});var C=new KJUR.asn1.DERInteger({bigint:D.d});var u=new KJUR.asn1.DERInteger({bigint:D.p});var s=new KJUR.asn1.DERInteger({bigint:D.q});var z=new KJUR.asn1.DERInteger({bigint:D.dmp1});var v=new KJUR.asn1.DERInteger({bigint:D.dmq1});var y=new KJUR.asn1.DERInteger({bigint:D.coeff});var F=new KJUR.asn1.DERSequence({array:[B,w,A,C,u,s,z,v,y]});var x=F.getEncodedHex();return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",x,E,r,t)},newEncryptedPKCS5PEM:function(p,q,t,u){if(typeof q=="undefined"||q==null){q=1024}if(typeof t=="undefined"||t==null){t="10001"}var r=new RSAKey();r.generate(q,t);var s=null;if(typeof u=="undefined"||u==null){s=this.getEncryptedPKCS5PEMFromRSAKey(r,p)}else{s=this.getEncryptedPKCS5PEMFromRSAKey(r,p,u)}return s},getRSAKeyFromPlainPKCS8PEM:function(r){if(r.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var q=this.getHexFromPEM(r,"PRIVATE KEY");var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getRSAKeyFromPlainPKCS8Hex:function(s){var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"outer DERSequence shall have 3 elements: "+r.length}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);if(q!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+q}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);var t=ASN1HEX.getHexOfTLV_AtObj(s,r[2]);var u=ASN1HEX.getHexOfV_AtObj(t,0);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(u);return p},parseHexOfEncryptedPKCS8:function(w){var s={};var r=ASN1HEX.getPosArrayOfChildren_AtObj(w,0);if(r.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+r.length}s.ciphertext=ASN1HEX.getHexOfV_AtObj(w,r[1]);var y=ASN1HEX.getPosArrayOfChildren_AtObj(w,r[0]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+y.length}if(ASN1HEX.getHexOfV_AtObj(w,y[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(w,y[1]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+p.length}var q=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[1]);if(q.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+q.length}if(ASN1HEX.getHexOfV_AtObj(w,q[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}s.encryptionSchemeAlg="TripleDES";s.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(w,q[1]);var t=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[0]);if(t.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+t.length}if(ASN1HEX.getHexOfV_AtObj(w,t[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var x=ASN1HEX.getPosArrayOfChildren_AtObj(w,t[1]);if(x.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+x.length}s.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(w,x[0]);var u=ASN1HEX.getHexOfV_AtObj(w,x[1]);try{s.pbkdf2Iter=parseInt(u,16)}catch(v){throw"malformed format pbkdf2Iter: "+u}return s},getPBKDF2KeyHexFromParam:function(u,p){var t=CryptoJS.enc.Hex.parse(u.pbkdf2Salt);var q=u.pbkdf2Iter;var s=CryptoJS.PBKDF2(p,t,{keySize:192/32,iterations:q});var r=CryptoJS.enc.Hex.stringify(s);return r},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(x,y){var r=this.getHexFromPEM(x,"ENCRYPTED PRIVATE KEY");var p=this.parseHexOfEncryptedPKCS8(r);var u=KEYUTIL.getPBKDF2KeyHexFromParam(p,y);var v={};v.ciphertext=CryptoJS.enc.Hex.parse(p.ciphertext);var t=CryptoJS.enc.Hex.parse(u);var s=CryptoJS.enc.Hex.parse(p.encryptionSchemeIV);var w=CryptoJS.TripleDES.decrypt(v,t,{iv:s});var q=CryptoJS.enc.Hex.stringify(w);return q},getRSAKeyFromEncryptedPKCS8PEM:function(s,r){var q=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,r);var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getKeyFromEncryptedPKCS8PEM:function(s,q){var p=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,q);var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},parsePlainPrivatePKCS8Hex:function(s){var q={};q.algparam=null;if(s.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(s.substr(r[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,r[1]);if(p.length!=2){throw"malformed PKCS8 private key(code:004)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}if(s.substr(r[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}q.keyidx=ASN1HEX.getStartPosOfV_AtObj(s,r[2]);return q},getKeyFromPlainPrivatePKCS8PEM:function(q){var p=this.getHexFromPEM(q,"PRIVATE KEY");var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},getKeyFromPlainPrivatePKCS8Hex:function(p){var w=this.parsePlainPrivatePKCS8Hex(p);if(w.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(p,w);var u=w.key;var z=new RSAKey();z.setPrivateEx(u.n,u.e,u.d,u.p,u.q,u.dp,u.dq,u.co);return z}else{if(w.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(p,w);if(KJUR.crypto.OID.oidhex2name[w.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+w.algparam}var v=KJUR.crypto.OID.oidhex2name[w.algparam];var z=new KJUR.crypto.ECDSA({curve:v});z.setPublicKeyHex(w.pubkey);z.setPrivateKeyHex(w.key);z.isPublic=false;return z}else{if(w.algoid=="2a8648ce380401"){var t=ASN1HEX.getVbyList(p,0,[1,1,0],"02");var s=ASN1HEX.getVbyList(p,0,[1,1,1],"02");var y=ASN1HEX.getVbyList(p,0,[1,1,2],"02");var B=ASN1HEX.getVbyList(p,0,[2,0],"02");var r=new BigInteger(t,16);var q=new BigInteger(s,16);var x=new BigInteger(y,16);var A=new BigInteger(B,16);var z=new KJUR.crypto.DSA();z.setPrivate(r,q,x,null,A);return z}else{throw"unsupported private key algorithm"}}}},getRSAKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getRSAKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8Hex:function(q){var p=this.parsePublicPKCS8Hex(q);if(p.algoid=="2a864886f70d010101"){var u=this.parsePublicRawRSAKeyHex(p.key);var r=new RSAKey();r.setPublic(u.n,u.e);return r}else{if(p.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var s=KJUR.crypto.OID.oidhex2name[p.algparam];var r=new KJUR.crypto.ECDSA({curve:s,pub:p.key});return r}else{if(p.algoid=="2a8648ce380401"){var t=p.algparam;var v=ASN1HEX.getHexOfV_AtObj(p.key,0);var r=new KJUR.crypto.DSA();r.setPublic(new BigInteger(t.p,16),new BigInteger(t.q,16),new BigInteger(t.g,16),new BigInteger(v,16));return r}else{throw"unsupported public key algorithm"}}}},parsePublicRawRSAKeyHex:function(r){var p={};if(r.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"malformed RSA key(code:002)"}if(r.substr(q[0],2)!="02"){throw"malformed RSA key(code:003)"}p.n=ASN1HEX.getHexOfV_AtObj(r,q[0]);if(r.substr(q[1],2)!="02"){throw"malformed RSA key(code:004)"}p.e=ASN1HEX.getHexOfV_AtObj(r,q[1]);return p},parsePrivateRawRSAKeyHexAtObj:function(q,s){var r=s.keyidx;if(q.substr(r,2)!="30"){throw"malformed RSA private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(p.length!=9){throw"malformed RSA private key(code:002)"}s.key={};s.key.n=ASN1HEX.getHexOfV_AtObj(q,p[1]);s.key.e=ASN1HEX.getHexOfV_AtObj(q,p[2]);s.key.d=ASN1HEX.getHexOfV_AtObj(q,p[3]);s.key.p=ASN1HEX.getHexOfV_AtObj(q,p[4]);s.key.q=ASN1HEX.getHexOfV_AtObj(q,p[5]);s.key.dp=ASN1HEX.getHexOfV_AtObj(q,p[6]);s.key.dq=ASN1HEX.getHexOfV_AtObj(q,p[7]);s.key.co=ASN1HEX.getHexOfV_AtObj(q,p[8])},parsePrivateRawECKeyHexAtObj:function(p,t){var q=t.keyidx;var r=ASN1HEX.getVbyList(p,q,[1],"04");var s=ASN1HEX.getVbyList(p,q,[2,0],"03").substr(2);t.key=r;t.pubkey=s},parsePublicPKCS8Hex:function(s){var q={};q.algparam=null;var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=2){throw"outer DERSequence shall have 2 elements: "+r.length}var t=r[0];if(s.substr(t,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,t);if(p.length!=2){throw"malformed PKCS8 public key(code:002)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}else{if(s.substr(p[1],2)=="30"){q.algparam={};q.algparam.p=ASN1HEX.getVbyList(s,p[1],[0],"02");q.algparam.q=ASN1HEX.getVbyList(s,p[1],[1],"02");q.algparam.g=ASN1HEX.getVbyList(s,p[1],[2],"02")}}if(s.substr(r[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}q.key=ASN1HEX.getHexOfV_AtObj(s,r[1]).substr(2);return q},getRSAKeyFromPublicPKCS8Hex:function(t){var s=ASN1HEX.getPosArrayOfChildren_AtObj(t,0);if(s.length!=2){throw"outer DERSequence shall have 2 elements: "+s.length}var r=ASN1HEX.getHexOfTLV_AtObj(t,s[0]);if(r!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(t.substr(s[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var v=ASN1HEX.getStartPosOfV_AtObj(t,s[1])+2;if(t.substr(v,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var p=ASN1HEX.getPosArrayOfChildren_AtObj(t,v);if(p.length!=2){throw"inner DERSequence shall have 2 elements: "+p.length}if(t.substr(p[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(t.substr(p[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var w=ASN1HEX.getHexOfV_AtObj(t,p[0]);var u=ASN1HEX.getHexOfV_AtObj(t,p[1]);var q=new RSAKey();q.setPublic(w,u);return q},}}();KEYUTIL.getKey=function(f,e,h){if(typeof RSAKey!="undefined"&&f instanceof RSAKey){return f}if(typeof KJUR.crypto.ECDSA!="undefined"&&f instanceof KJUR.crypto.ECDSA){return f}if(typeof KJUR.crypto.DSA!="undefined"&&f instanceof KJUR.crypto.DSA){return f}if(f.curve!==undefined&&f.xy!==undefined&&f.d===undefined){return new KJUR.crypto.ECDSA({pub:f.xy,curve:f.curve})}if(f.curve!==undefined&&f.d!==undefined){return new KJUR.crypto.ECDSA({prv:f.d,curve:f.curve})}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(f.n,f.e);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.co!==undefined&&f.qi===undefined){var w=new RSAKey();w.setPrivateEx(f.n,f.e,f.d,f.p,f.q,f.dp,f.dq,f.co);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p===undefined){var w=new RSAKey();w.setPrivate(f.n,f.e,f.d);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x===undefined){var w=new KJUR.crypto.DSA();w.setPublic(f.p,f.q,f.g,f.y);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x!==undefined){var w=new KJUR.crypto.DSA();w.setPrivate(f.p,f.q,f.g,f.y,f.x);return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(b64utohex(f.n),b64utohex(f.e));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.qi!==undefined){var w=new RSAKey();w.setPrivateEx(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d),b64utohex(f.p),b64utohex(f.q),b64utohex(f.dp),b64utohex(f.dq),b64utohex(f.qi));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined){var w=new RSAKey();w.setPrivate(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d));return w}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d===undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;d.setPublicKeyHex(m);return d}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d!==undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;var a=("0000000000"+b64utohex(f.d)).slice(-l);d.setPublicKeyHex(m);d.setPrivateKeyHex(a);return d}if(f.indexOf("-END CERTIFICATE-",0)!=-1||f.indexOf("-END X509 CERTIFICATE-",0)!=-1||f.indexOf("-END TRUSTED CERTIFICATE-",0)!=-1){return X509.getPublicKeyFromCertPEM(f)}if(h==="pkcs8pub"){return KEYUTIL.getKeyFromPublicPKCS8Hex(f)}if(f.indexOf("-END PUBLIC KEY-")!=-1){return KEYUTIL.getKeyFromPublicPKCS8PEM(f)}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var i=KEYUTIL.getHexFromPEM(f,"RSA PRIVATE KEY");return KEYUTIL.getKey(i,null,"pkcs5prv")}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var u=this.getHexFromPEM(f,"DSA PRIVATE KEY");var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(f)}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(f,e)}if(f.indexOf("-END EC PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var w=ASN1HEX.getVbyList(u,0,[1],"04");var c=ASN1HEX.getVbyList(u,0,[2,0],"06");var o=ASN1HEX.getVbyList(u,0,[3,0],"03").substr(2);var b="";if(KJUR.crypto.OID.oidhex2name[c]!==undefined){b=KJUR.crypto.OID.oidhex2name[c]}else{throw"undefined OID(hex) in KJUR.crypto.OID: "+c}var d=new KJUR.crypto.ECDSA({name:b});d.setPublicKeyHex(o);d.setPrivateKeyHex(w);d.isPublic=false;return d}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END ENCRYPTED PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromEncryptedPKCS8PEM(f,e)}throw"not supported argument"};KEYUTIL.generateKeypair=function(a,c){if(a=="RSA"){var b=c;var h=new RSAKey();h.generate(b,"10001");h.isPrivate=true;h.isPublic=true;var f=new RSAKey();var e=h.n.toString(16);var i=h.e.toString(16);f.setPublic(e,i);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{if(a=="EC"){var d=c;var g=new KJUR.crypto.ECDSA({curve:d});var j=g.generateKeyPairHex();var h=new KJUR.crypto.ECDSA({curve:d});h.setPublicKeyHex(j.ecpubhex);h.setPrivateKeyHex(j.ecprvhex);h.isPrivate=true;h.isPublic=false;var f=new KJUR.crypto.ECDSA({curve:d});f.setPublicKeyHex(j.ecpubhex);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{throw"unknown algorithm: "+a}}};KEYUTIL.getPEM=function(a,r,o,g,j){var v=KJUR.asn1;var u=KJUR.crypto;function p(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.n}},{"int":s.e},{"int":{bigint:s.d}},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.dmp1}},{"int":{bigint:s.dmq1}},{"int":{bigint:s.coeff}}]});return w}function q(w){var s=KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:w.prvKeyHex}},{tag:["a0",true,{oid:{name:w.curveName}}]},{tag:["a1",true,{bitstr:{hex:"00"+w.pubKeyHex}}]}]});return s}function n(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.g}},{"int":{bigint:s.y}},{"int":{bigint:s.x}}]});return w}if(((typeof RSAKey!="undefined"&&a instanceof RSAKey)||(typeof u.DSA!="undefined"&&a instanceof u.DSA)||(typeof u.ECDSA!="undefined"&&a instanceof u.ECDSA))&&a.isPublic==true&&(r===undefined||r=="PKCS8PUB")){var t=new KJUR.asn1.x509.SubjectPublicKeyInfo(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"PUBLIC KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o===undefined||o==null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"RSA PRIVATE KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o===undefined||o==null)&&a.isPrivate==true){var f=new KJUR.asn1.DERObjectIdentifier({name:a.curveName});var l=f.getEncodedHex();var e=q(a);var k=e.getEncodedHex();var i="";i+=v.ASN1Util.getPEMStringFromHex(l,"EC PARAMETERS");i+=v.ASN1Util.getPEMStringFromHex(k,"EC PRIVATE KEY");return i}if(r=="PKCS1PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o===undefined||o==null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"DSA PRIVATE KEY")}if(r=="PKCS5PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=q(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA",m,o,g)}var h=function(w,s){var y=b(w,s);var x=new KJUR.asn1.ASN1Util.newObject({seq:[{seq:[{oid:{name:"pkcs5PBES2"}},{seq:[{seq:[{oid:{name:"pkcs5PBKDF2"}},{seq:[{octstr:{hex:y.pbkdf2Salt}},{"int":y.pbkdf2Iter}]}]},{seq:[{oid:{name:"des-EDE3-CBC"}},{octstr:{hex:y.encryptionSchemeIV}}]}]}]},{octstr:{hex:y.ciphertext}}]});return x.getEncodedHex()};var b=function(D,E){var x=100;var C=CryptoJS.lib.WordArray.random(8);var B="DES-EDE3-CBC";var s=CryptoJS.lib.WordArray.random(8);var y=CryptoJS.PBKDF2(E,C,{keySize:192/32,iterations:x});var z=CryptoJS.enc.Hex.parse(D);var A=CryptoJS.TripleDES.encrypt(z,y,{iv:s})+"";var w={};w.ciphertext=A;w.pbkdf2Salt=CryptoJS.enc.Hex.stringify(C);w.pbkdf2Iter=x;w.encryptionSchemeAlg=B;w.encryptionSchemeIV=CryptoJS.enc.Hex.stringify(s);return w};if(r=="PKCS8PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&a.isPrivate==true){var d=p(a);var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"rsaEncryption"}},{"null":true}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&a.isPrivate==true){var d=new KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:a.prvKeyHex}},{tag:["a1",true,{bitstr:{hex:"00"+a.pubKeyHex}}]}]});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"ecPublicKey"}},{oid:{name:a.curveName}}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&a.isPrivate==true){var d=new KJUR.asn1.DERInteger({bigint:a.x});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"dsa"}},{seq:[{"int":{bigint:a.p}},{"int":{bigint:a.q}},{"int":{bigint:a.g}}]}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}throw"unsupported object nor format"};KEYUTIL.getKeyFromCSRPEM=function(b){var a=KEYUTIL.getHexFromPEM(b,"CERTIFICATE REQUEST");var c=KEYUTIL.getKeyFromCSRHex(a);return c};KEYUTIL.getKeyFromCSRHex=function(a){var c=KEYUTIL.parseCSRHex(a);var b=KEYUTIL.getKey(c.p8pubkeyhex,null,"pkcs8pub");return b};KEYUTIL.parseCSRHex=function(c){var b={};var e=c;if(e.substr(0,2)!="30"){throw"malformed CSR(code:001)"}var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,0);if(d.length<1){throw"malformed CSR(code:002)"}if(e.substr(d[0],2)!="30"){throw"malformed CSR(code:003)"}var a=ASN1HEX.getPosArrayOfChildren_AtObj(e,d[0]);if(a.length<3){throw"malformed CSR(code:004)"}b.p8pubkeyhex=ASN1HEX.getHexOfTLV_AtObj(e,a[2]);return b};KEYUTIL.getJWKFromKey=function(d){var b={};if(d instanceof RSAKey&&d.isPrivate){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));b.d=hextob64u(d.d.toString(16));b.p=hextob64u(d.p.toString(16));b.q=hextob64u(d.q.toString(16));b.dp=hextob64u(d.dmp1.toString(16));b.dq=hextob64u(d.dmq1.toString(16));b.qi=hextob64u(d.coeff.toString(16));return b}else{if(d instanceof RSAKey&&d.isPublic){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPrivate){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);b.d=hextob64u(d.prvKeyHex);return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPublic){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);return b}}}}throw"not supported key object"}; \ No newline at end of file +var KEYUTIL=function(){var d=function(p,r,q){return k(CryptoJS.AES,p,r,q)};var e=function(p,r,q){return k(CryptoJS.TripleDES,p,r,q)};var a=function(p,r,q){return k(CryptoJS.DES,p,r,q)};var k=function(s,x,u,q){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(u);var p=CryptoJS.enc.Hex.parse(q);var t={};t.key=w;t.iv=p;t.ciphertext=r;var v=s.decrypt(t,w,{iv:p});return CryptoJS.enc.Hex.stringify(v)};var l=function(p,r,q){return g(CryptoJS.AES,p,r,q)};var o=function(p,r,q){return g(CryptoJS.TripleDES,p,r,q)};var f=function(p,r,q){return g(CryptoJS.DES,p,r,q)};var g=function(t,y,v,q){var s=CryptoJS.enc.Hex.parse(y);var x=CryptoJS.enc.Hex.parse(v);var p=CryptoJS.enc.Hex.parse(q);var w=t.encrypt(s,x,{iv:p});var r=CryptoJS.enc.Hex.parse(w.toString());var u=CryptoJS.enc.Base64.stringify(r);return u};var i={"AES-256-CBC":{proc:d,eproc:l,keylen:32,ivlen:16},"AES-192-CBC":{proc:d,eproc:l,keylen:24,ivlen:16},"AES-128-CBC":{proc:d,eproc:l,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:e,eproc:o,keylen:24,ivlen:8},"DES-CBC":{proc:a,eproc:f,keylen:8,ivlen:8}};var c=function(p){return i[p]["proc"]};var m=function(p){var r=CryptoJS.lib.WordArray.random(p);var q=CryptoJS.enc.Hex.stringify(r);return q};var n=function(v){var w={};var q=v.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"));if(q){w.cipher=q[1];w.ivsalt=q[2]}var p=v.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));if(p){w.type=p[1]}var u=-1;var x=0;if(v.indexOf("\r\n\r\n")!=-1){u=v.indexOf("\r\n\r\n");x=2}if(v.indexOf("\n\n")!=-1){u=v.indexOf("\n\n");x=1}var t=v.indexOf("-----END");if(u!=-1&&t!=-1){var r=v.substring(u+x*2,t-x);r=r.replace(/\s+/g,"");w.data=r}return w};var j=function(q,y,p){var v=p.substring(0,16);var t=CryptoJS.enc.Hex.parse(v);var r=CryptoJS.enc.Utf8.parse(y);var u=i[q]["keylen"]+i[q]["ivlen"];var x="";var w=null;for(;;){var s=CryptoJS.algo.MD5.create();if(w!=null){s.update(w)}s.update(r);s.update(t);w=s.finalize();x=x+CryptoJS.enc.Hex.stringify(w);if(x.length>=u*2){break}}var z={};z.keyhex=x.substr(0,i[q]["keylen"]*2);z.ivhex=x.substr(i[q]["keylen"]*2,i[q]["ivlen"]*2);return z};var b=function(p,v,r,w){var s=CryptoJS.enc.Base64.parse(p);var q=CryptoJS.enc.Hex.stringify(s);var u=i[v]["proc"];var t=u(q,r,w);return t};var h=function(p,s,q,u){var r=i[s]["eproc"];var t=r(p,q,u);return t};return{version:"1.0.0",getHexFromPEM:function(q,u){var r=q;if(r.indexOf("-----BEGIN ")==-1){throw"can't find PEM header: "+u}if(typeof u=="string"&&u!=""){r=r.replace("-----BEGIN "+u+"-----","");r=r.replace("-----END "+u+"-----","")}else{r=r.replace(/-----BEGIN [^-]+-----/,"");r=r.replace(/-----END [^-]+-----/,"")}var t=r.replace(/\s+/g,"");var p=b64tohex(t);return p},getDecryptedKeyHexByKeyIV:function(q,t,s,r){var p=c(t);return p(q,s,r)},parsePKCS5PEM:function(p){return n(p)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(q,p,r){return j(q,p,r)},decryptKeyB64:function(p,r,q,s){return b(p,r,q,s)},getDecryptedKeyHex:function(y,x){var q=n(y);var t=q.type;var r=q.cipher;var p=q.ivsalt;var s=q.data;var w=j(r,x,p);var v=w.keyhex;var u=b(s,r,v,p);return u},getRSAKeyFromEncryptedPKCS5PEM:function(r,q){var s=this.getDecryptedKeyHex(r,q);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(s);return p},getEncryptedPKCS5PEMFromPrvKeyHex:function(x,s,A,t,r){var p="";if(typeof t=="undefined"||t==null){t="AES-256-CBC"}if(typeof i[t]=="undefined"){throw"KEYUTIL unsupported algorithm: "+t}if(typeof r=="undefined"||r==null){var v=i[t]["ivlen"];var u=m(v);r=u.toUpperCase()}var z=j(t,A,r);var y=z.keyhex;var w=h(s,t,y,r);var q=w.replace(/(.{64})/g,"$1\r\n");var p="-----BEGIN "+x+" PRIVATE KEY-----\r\n";p+="Proc-Type: 4,ENCRYPTED\r\n";p+="DEK-Info: "+t+","+r+"\r\n";p+="\r\n";p+=q;p+="\r\n-----END "+x+" PRIVATE KEY-----\r\n";return p},getEncryptedPKCS5PEMFromRSAKey:function(D,E,r,t){var B=new KJUR.asn1.DERInteger({"int":0});var w=new KJUR.asn1.DERInteger({bigint:D.n});var A=new KJUR.asn1.DERInteger({"int":D.e});var C=new KJUR.asn1.DERInteger({bigint:D.d});var u=new KJUR.asn1.DERInteger({bigint:D.p});var s=new KJUR.asn1.DERInteger({bigint:D.q});var z=new KJUR.asn1.DERInteger({bigint:D.dmp1});var v=new KJUR.asn1.DERInteger({bigint:D.dmq1});var y=new KJUR.asn1.DERInteger({bigint:D.coeff});var F=new KJUR.asn1.DERSequence({array:[B,w,A,C,u,s,z,v,y]});var x=F.getEncodedHex();return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",x,E,r,t)},newEncryptedPKCS5PEM:function(p,q,t,u){if(typeof q=="undefined"||q==null){q=1024}if(typeof t=="undefined"||t==null){t="10001"}var r=new RSAKey();r.generate(q,t);var s=null;if(typeof u=="undefined"||u==null){s=this.getEncryptedPKCS5PEMFromRSAKey(r,p)}else{s=this.getEncryptedPKCS5PEMFromRSAKey(r,p,u)}return s},getRSAKeyFromPlainPKCS8PEM:function(r){if(r.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var q=this.getHexFromPEM(r,"PRIVATE KEY");var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getRSAKeyFromPlainPKCS8Hex:function(s){var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"outer DERSequence shall have 3 elements: "+r.length}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);if(q!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+q}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);var t=ASN1HEX.getHexOfTLV_AtObj(s,r[2]);var u=ASN1HEX.getHexOfV_AtObj(t,0);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(u);return p},parseHexOfEncryptedPKCS8:function(w){var s={};var r=ASN1HEX.getPosArrayOfChildren_AtObj(w,0);if(r.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+r.length}s.ciphertext=ASN1HEX.getHexOfV_AtObj(w,r[1]);var y=ASN1HEX.getPosArrayOfChildren_AtObj(w,r[0]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+y.length}if(ASN1HEX.getHexOfV_AtObj(w,y[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(w,y[1]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+p.length}var q=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[1]);if(q.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+q.length}if(ASN1HEX.getHexOfV_AtObj(w,q[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}s.encryptionSchemeAlg="TripleDES";s.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(w,q[1]);var t=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[0]);if(t.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+t.length}if(ASN1HEX.getHexOfV_AtObj(w,t[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var x=ASN1HEX.getPosArrayOfChildren_AtObj(w,t[1]);if(x.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+x.length}s.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(w,x[0]);var u=ASN1HEX.getHexOfV_AtObj(w,x[1]);try{s.pbkdf2Iter=parseInt(u,16)}catch(v){throw"malformed format pbkdf2Iter: "+u}return s},getPBKDF2KeyHexFromParam:function(u,p){var t=CryptoJS.enc.Hex.parse(u.pbkdf2Salt);var q=u.pbkdf2Iter;var s=CryptoJS.PBKDF2(p,t,{keySize:192/32,iterations:q});var r=CryptoJS.enc.Hex.stringify(s);return r},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(x,y){var r=this.getHexFromPEM(x,"ENCRYPTED PRIVATE KEY");var p=this.parseHexOfEncryptedPKCS8(r);var u=KEYUTIL.getPBKDF2KeyHexFromParam(p,y);var v={};v.ciphertext=CryptoJS.enc.Hex.parse(p.ciphertext);var t=CryptoJS.enc.Hex.parse(u);var s=CryptoJS.enc.Hex.parse(p.encryptionSchemeIV);var w=CryptoJS.TripleDES.decrypt(v,t,{iv:s});var q=CryptoJS.enc.Hex.stringify(w);return q},getRSAKeyFromEncryptedPKCS8PEM:function(s,r){var q=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,r);var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getKeyFromEncryptedPKCS8PEM:function(s,q){var p=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,q);var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},parsePlainPrivatePKCS8Hex:function(s){var q={};q.algparam=null;if(s.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(s.substr(r[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,r[1]);if(p.length!=2){throw"malformed PKCS8 private key(code:004)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}if(s.substr(r[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}q.keyidx=ASN1HEX.getStartPosOfV_AtObj(s,r[2]);return q},getKeyFromPlainPrivatePKCS8PEM:function(q){var p=this.getHexFromPEM(q,"PRIVATE KEY");var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},getKeyFromPlainPrivatePKCS8Hex:function(p){var w=this.parsePlainPrivatePKCS8Hex(p);if(w.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(p,w);var u=w.key;var z=new RSAKey();z.setPrivateEx(u.n,u.e,u.d,u.p,u.q,u.dp,u.dq,u.co);return z}else{if(w.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(p,w);if(KJUR.crypto.OID.oidhex2name[w.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+w.algparam}var v=KJUR.crypto.OID.oidhex2name[w.algparam];var z=new KJUR.crypto.ECDSA({curve:v});z.setPublicKeyHex(w.pubkey);z.setPrivateKeyHex(w.key);z.isPublic=false;return z}else{if(w.algoid=="2a8648ce380401"){var t=ASN1HEX.getVbyList(p,0,[1,1,0],"02");var s=ASN1HEX.getVbyList(p,0,[1,1,1],"02");var y=ASN1HEX.getVbyList(p,0,[1,1,2],"02");var B=ASN1HEX.getVbyList(p,0,[2,0],"02");var r=new BigInteger(t,16);var q=new BigInteger(s,16);var x=new BigInteger(y,16);var A=new BigInteger(B,16);var z=new KJUR.crypto.DSA();z.setPrivate(r,q,x,null,A);return z}else{throw"unsupported private key algorithm"}}}},getRSAKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getRSAKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8Hex:function(q){var p=this.parsePublicPKCS8Hex(q);if(p.algoid=="2a864886f70d010101"){var u=this.parsePublicRawRSAKeyHex(p.key);var r=new RSAKey();r.setPublic(u.n,u.e);return r}else{if(p.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var s=KJUR.crypto.OID.oidhex2name[p.algparam];var r=new KJUR.crypto.ECDSA({curve:s,pub:p.key});return r}else{if(p.algoid=="2a8648ce380401"){var t=p.algparam;var v=ASN1HEX.getHexOfV_AtObj(p.key,0);var r=new KJUR.crypto.DSA();r.setPublic(new BigInteger(t.p,16),new BigInteger(t.q,16),new BigInteger(t.g,16),new BigInteger(v,16));return r}else{throw"unsupported public key algorithm"}}}},parsePublicRawRSAKeyHex:function(r){var p={};if(r.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"malformed RSA key(code:002)"}if(r.substr(q[0],2)!="02"){throw"malformed RSA key(code:003)"}p.n=ASN1HEX.getHexOfV_AtObj(r,q[0]);if(r.substr(q[1],2)!="02"){throw"malformed RSA key(code:004)"}p.e=ASN1HEX.getHexOfV_AtObj(r,q[1]);return p},parsePrivateRawRSAKeyHexAtObj:function(q,s){var r=s.keyidx;if(q.substr(r,2)!="30"){throw"malformed RSA private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(p.length!=9){throw"malformed RSA private key(code:002)"}s.key={};s.key.n=ASN1HEX.getHexOfV_AtObj(q,p[1]);s.key.e=ASN1HEX.getHexOfV_AtObj(q,p[2]);s.key.d=ASN1HEX.getHexOfV_AtObj(q,p[3]);s.key.p=ASN1HEX.getHexOfV_AtObj(q,p[4]);s.key.q=ASN1HEX.getHexOfV_AtObj(q,p[5]);s.key.dp=ASN1HEX.getHexOfV_AtObj(q,p[6]);s.key.dq=ASN1HEX.getHexOfV_AtObj(q,p[7]);s.key.co=ASN1HEX.getHexOfV_AtObj(q,p[8])},parsePrivateRawECKeyHexAtObj:function(p,t){var q=t.keyidx;var r=ASN1HEX.getVbyList(p,q,[1],"04");var s=ASN1HEX.getVbyList(p,q,[2,0],"03").substr(2);t.key=r;t.pubkey=s},parsePublicPKCS8Hex:function(s){var q={};q.algparam=null;var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=2){throw"outer DERSequence shall have 2 elements: "+r.length}var t=r[0];if(s.substr(t,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,t);if(p.length!=2){throw"malformed PKCS8 public key(code:002)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}else{if(s.substr(p[1],2)=="30"){q.algparam={};q.algparam.p=ASN1HEX.getVbyList(s,p[1],[0],"02");q.algparam.q=ASN1HEX.getVbyList(s,p[1],[1],"02");q.algparam.g=ASN1HEX.getVbyList(s,p[1],[2],"02")}}if(s.substr(r[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}q.key=ASN1HEX.getHexOfV_AtObj(s,r[1]).substr(2);return q},getRSAKeyFromPublicPKCS8Hex:function(t){var s=ASN1HEX.getPosArrayOfChildren_AtObj(t,0);if(s.length!=2){throw"outer DERSequence shall have 2 elements: "+s.length}var r=ASN1HEX.getHexOfTLV_AtObj(t,s[0]);if(r!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(t.substr(s[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var v=ASN1HEX.getStartPosOfV_AtObj(t,s[1])+2;if(t.substr(v,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var p=ASN1HEX.getPosArrayOfChildren_AtObj(t,v);if(p.length!=2){throw"inner DERSequence shall have 2 elements: "+p.length}if(t.substr(p[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(t.substr(p[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var w=ASN1HEX.getHexOfV_AtObj(t,p[0]);var u=ASN1HEX.getHexOfV_AtObj(t,p[1]);var q=new RSAKey();q.setPublic(w,u);return q},}}();KEYUTIL.getKey=function(f,e,h){if(typeof RSAKey!="undefined"&&f instanceof RSAKey){return f}if(typeof KJUR.crypto.ECDSA!="undefined"&&f instanceof KJUR.crypto.ECDSA){return f}if(typeof KJUR.crypto.DSA!="undefined"&&f instanceof KJUR.crypto.DSA){return f}if(f.curve!==undefined&&f.xy!==undefined&&f.d===undefined){return new KJUR.crypto.ECDSA({pub:f.xy,curve:f.curve})}if(f.curve!==undefined&&f.d!==undefined){return new KJUR.crypto.ECDSA({prv:f.d,curve:f.curve})}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(f.n,f.e);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.co!==undefined&&f.qi===undefined){var w=new RSAKey();w.setPrivateEx(f.n,f.e,f.d,f.p,f.q,f.dp,f.dq,f.co);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p===undefined){var w=new RSAKey();w.setPrivate(f.n,f.e,f.d);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x===undefined){var w=new KJUR.crypto.DSA();w.setPublic(f.p,f.q,f.g,f.y);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x!==undefined){var w=new KJUR.crypto.DSA();w.setPrivate(f.p,f.q,f.g,f.y,f.x);return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(b64utohex(f.n),b64utohex(f.e));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.qi!==undefined){var w=new RSAKey();w.setPrivateEx(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d),b64utohex(f.p),b64utohex(f.q),b64utohex(f.dp),b64utohex(f.dq),b64utohex(f.qi));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined){var w=new RSAKey();w.setPrivate(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d));return w}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d===undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;d.setPublicKeyHex(m);return d}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d!==undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;var a=("0000000000"+b64utohex(f.d)).slice(-l);d.setPublicKeyHex(m);d.setPrivateKeyHex(a);return d}if(f.indexOf("-END CERTIFICATE-",0)!=-1||f.indexOf("-END X509 CERTIFICATE-",0)!=-1||f.indexOf("-END TRUSTED CERTIFICATE-",0)!=-1){return X509.getPublicKeyFromCertPEM(f)}if(h==="pkcs8pub"){return KEYUTIL.getKeyFromPublicPKCS8Hex(f)}if(f.indexOf("-END PUBLIC KEY-")!=-1){return KEYUTIL.getKeyFromPublicPKCS8PEM(f)}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var i=KEYUTIL.getHexFromPEM(f,"RSA PRIVATE KEY");return KEYUTIL.getKey(i,null,"pkcs5prv")}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var u=this.getHexFromPEM(f,"DSA PRIVATE KEY");var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(f)}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(f,e)}if(f.indexOf("-END EC PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var w=ASN1HEX.getVbyList(u,0,[1],"04");var c=ASN1HEX.getVbyList(u,0,[2,0],"06");var o=ASN1HEX.getVbyList(u,0,[3,0],"03").substr(2);var b="";if(KJUR.crypto.OID.oidhex2name[c]!==undefined){b=KJUR.crypto.OID.oidhex2name[c]}else{throw"undefined OID(hex) in KJUR.crypto.OID: "+c}var d=new KJUR.crypto.ECDSA({name:b});d.setPublicKeyHex(o);d.setPrivateKeyHex(w);d.isPublic=false;return d}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END ENCRYPTED PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromEncryptedPKCS8PEM(f,e)}throw"not supported argument"};KEYUTIL.generateKeypair=function(a,c){if(a=="RSA"){var b=c;var h=new RSAKey();h.generate(b,"10001");h.isPrivate=true;h.isPublic=true;var f=new RSAKey();var e=h.n.toString(16);var i=h.e.toString(16);f.setPublic(e,i);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{if(a=="EC"){var d=c;var g=new KJUR.crypto.ECDSA({curve:d});var j=g.generateKeyPairHex();var h=new KJUR.crypto.ECDSA({curve:d});h.setPublicKeyHex(j.ecpubhex);h.setPrivateKeyHex(j.ecprvhex);h.isPrivate=true;h.isPublic=false;var f=new KJUR.crypto.ECDSA({curve:d});f.setPublicKeyHex(j.ecpubhex);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{throw"unknown algorithm: "+a}}};KEYUTIL.getPEM=function(a,r,o,g,j){var v=KJUR.asn1;var u=KJUR.crypto;function p(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.n}},{"int":s.e},{"int":{bigint:s.d}},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.dmp1}},{"int":{bigint:s.dmq1}},{"int":{bigint:s.coeff}}]});return w}function q(w){var s=KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:w.prvKeyHex}},{tag:["a0",true,{oid:{name:w.curveName}}]},{tag:["a1",true,{bitstr:{hex:"00"+w.pubKeyHex}}]}]});return s}function n(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.g}},{"int":{bigint:s.y}},{"int":{bigint:s.x}}]});return w}if(((typeof RSAKey!="undefined"&&a instanceof RSAKey)||(typeof u.DSA!="undefined"&&a instanceof u.DSA)||(typeof u.ECDSA!="undefined"&&a instanceof u.ECDSA))&&a.isPublic==true&&(r===undefined||r=="PKCS8PUB")){var t=new KJUR.asn1.x509.SubjectPublicKeyInfo(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"PUBLIC KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o===undefined||o==null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"RSA PRIVATE KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o===undefined||o==null)&&a.isPrivate==true){var f=new KJUR.asn1.DERObjectIdentifier({name:a.curveName});var l=f.getEncodedHex();var e=q(a);var k=e.getEncodedHex();var i="";i+=v.ASN1Util.getPEMStringFromHex(l,"EC PARAMETERS");i+=v.ASN1Util.getPEMStringFromHex(k,"EC PRIVATE KEY");return i}if(r=="PKCS1PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o===undefined||o==null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"DSA PRIVATE KEY")}if(r=="PKCS5PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=q(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA",m,o,g)}var h=function(w,s){var y=b(w,s);var x=new KJUR.asn1.ASN1Util.newObject({seq:[{seq:[{oid:{name:"pkcs5PBES2"}},{seq:[{seq:[{oid:{name:"pkcs5PBKDF2"}},{seq:[{octstr:{hex:y.pbkdf2Salt}},{"int":y.pbkdf2Iter}]}]},{seq:[{oid:{name:"des-EDE3-CBC"}},{octstr:{hex:y.encryptionSchemeIV}}]}]}]},{octstr:{hex:y.ciphertext}}]});return x.getEncodedHex()};var b=function(D,E){var x=100;var C=CryptoJS.lib.WordArray.random(8);var B="DES-EDE3-CBC";var s=CryptoJS.lib.WordArray.random(8);var y=CryptoJS.PBKDF2(E,C,{keySize:192/32,iterations:x});var z=CryptoJS.enc.Hex.parse(D);var A=CryptoJS.TripleDES.encrypt(z,y,{iv:s})+"";var w={};w.ciphertext=A;w.pbkdf2Salt=CryptoJS.enc.Hex.stringify(C);w.pbkdf2Iter=x;w.encryptionSchemeAlg=B;w.encryptionSchemeIV=CryptoJS.enc.Hex.stringify(s);return w};if(r=="PKCS8PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&a.isPrivate==true){var d=p(a);var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"rsaEncryption"}},{"null":true}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&a.isPrivate==true){var d=new KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:a.prvKeyHex}},{tag:["a1",true,{bitstr:{hex:"00"+a.pubKeyHex}}]}]});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"ecPublicKey"}},{oid:{name:a.curveName}}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&a.isPrivate==true){var d=new KJUR.asn1.DERInteger({bigint:a.x});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"dsa"}},{seq:[{"int":{bigint:a.p}},{"int":{bigint:a.q}},{"int":{bigint:a.g}}]}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}throw"unsupported object nor format"};KEYUTIL.getKeyFromCSRPEM=function(b){var a=KEYUTIL.getHexFromPEM(b,"CERTIFICATE REQUEST");var c=KEYUTIL.getKeyFromCSRHex(a);return c};KEYUTIL.getKeyFromCSRHex=function(a){var c=KEYUTIL.parseCSRHex(a);var b=KEYUTIL.getKey(c.p8pubkeyhex,null,"pkcs8pub");return b};KEYUTIL.parseCSRHex=function(c){var b={};var e=c;if(e.substr(0,2)!="30"){throw"malformed CSR(code:001)"}var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,0);if(d.length<1){throw"malformed CSR(code:002)"}if(e.substr(d[0],2)!="30"){throw"malformed CSR(code:003)"}var a=ASN1HEX.getPosArrayOfChildren_AtObj(e,d[0]);if(a.length<3){throw"malformed CSR(code:004)"}b.p8pubkeyhex=ASN1HEX.getHexOfTLV_AtObj(e,a[2]);return b};KEYUTIL.getJWKFromKey=function(d){var b={};if(d instanceof RSAKey&&d.isPrivate){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));b.d=hextob64u(d.d.toString(16));b.p=hextob64u(d.p.toString(16));b.q=hextob64u(d.q.toString(16));b.dp=hextob64u(d.dmp1.toString(16));b.dq=hextob64u(d.dmq1.toString(16));b.qi=hextob64u(d.coeff.toString(16));return b}else{if(d instanceof RSAKey&&d.isPublic){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPrivate){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);b.d=hextob64u(d.prvKeyHex);return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPublic){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);return b}}}}throw"not supported key object"}; \ No newline at end of file diff --git a/min/pkcs5pkey-1.0.min.js b/min/pkcs5pkey-1.0.min.js index 6f3235b9..278d7357 100644 --- a/min/pkcs5pkey-1.0.min.js +++ b/min/pkcs5pkey-1.0.min.js @@ -1,3 +1,3 @@ -/*! pkcs5pkey-1.0.6.js (c) 2013-2014 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! pkcs5pkey-1.0.7.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ -var PKCS5PKEY=function(){var c=function(n,p,o){return i(CryptoJS.AES,n,p,o)};var d=function(n,p,o){return i(CryptoJS.TripleDES,n,p,o)};var i=function(q,v,s,o){var p=CryptoJS.enc.Hex.parse(v);var u=CryptoJS.enc.Hex.parse(s);var n=CryptoJS.enc.Hex.parse(o);var r={};r.key=u;r.iv=n;r.ciphertext=p;var t=q.decrypt(r,u,{iv:n});return CryptoJS.enc.Hex.stringify(t)};var j=function(n,p,o){return e(CryptoJS.AES,n,p,o)};var m=function(n,p,o){return e(CryptoJS.TripleDES,n,p,o)};var e=function(s,x,v,p){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(v);var o=CryptoJS.enc.Hex.parse(p);var n={};var u=s.encrypt(r,w,{iv:o});var q=CryptoJS.enc.Hex.parse(u.toString());var t=CryptoJS.enc.Base64.stringify(q);return t};var g={"AES-256-CBC":{proc:c,eproc:j,keylen:32,ivlen:16},"AES-192-CBC":{proc:c,eproc:j,keylen:24,ivlen:16},"AES-128-CBC":{proc:c,eproc:j,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:d,eproc:m,keylen:24,ivlen:8}};var b=function(n){return g[n]["proc"]};var k=function(n){var p=CryptoJS.lib.WordArray.random(n);var o=CryptoJS.enc.Hex.stringify(p);return o};var l=function(q){var r={};if(q.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"))){r.cipher=RegExp.$1;r.ivsalt=RegExp.$2}if(q.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"))){r.type=RegExp.$1}var p=-1;var t=0;if(q.indexOf("\r\n\r\n")!=-1){p=q.indexOf("\r\n\r\n");t=2}if(q.indexOf("\n\n")!=-1){p=q.indexOf("\n\n");t=1}var o=q.indexOf("-----END");if(p!=-1&&o!=-1){var n=q.substring(p+t*2,o-t);n=n.replace(/\s+/g,"");r.data=n}return r};var h=function(o,w,n){var t=n.substring(0,16);var r=CryptoJS.enc.Hex.parse(t);var p=CryptoJS.enc.Utf8.parse(w);var s=g[o]["keylen"]+g[o]["ivlen"];var v="";var u=null;for(;;){var q=CryptoJS.algo.MD5.create();if(u!=null){q.update(u)}q.update(p);q.update(r);u=q.finalize();v=v+CryptoJS.enc.Hex.stringify(u);if(v.length>=s*2){break}}var x={};x.keyhex=v.substr(0,g[o]["keylen"]*2);x.ivhex=v.substr(g[o]["keylen"]*2,g[o]["ivlen"]*2);return x};var a=function(n,t,p,u){var q=CryptoJS.enc.Base64.parse(n);var o=CryptoJS.enc.Hex.stringify(q);var s=g[t]["proc"];var r=s(o,p,u);return r};var f=function(n,q,o,s){var p=g[q]["eproc"];var r=p(n,o,s);return r};return{version:"1.0.5",getHexFromPEM:function(o,r){var p=o;if(p.indexOf("BEGIN "+r)==-1){throw"can't find PEM header: "+r}p=p.replace("-----BEGIN "+r+"-----","");p=p.replace("-----END "+r+"-----","");var q=p.replace(/\s+/g,"");var n=b64tohex(q);return n},getDecryptedKeyHexByKeyIV:function(o,r,q,p){var n=b(r);return n(o,q,p)},parsePKCS5PEM:function(n){return l(n)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(o,n,p){return h(o,n,p)},decryptKeyB64:function(n,p,o,q){return a(n,p,o,q)},getDecryptedKeyHex:function(w,v){var o=l(w);var r=o.type;var p=o.cipher;var n=o.ivsalt;var q=o.data;var u=h(p,v,n);var t=u.keyhex;var s=a(q,p,t,n);return s},getRSAKeyFromEncryptedPKCS5PEM:function(p,o){var q=this.getDecryptedKeyHex(p,o);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(q);return n},getEryptedPKCS5PEMFromPrvKeyHex:function(q,x,r,p){var n="";if(typeof r=="undefined"||r==null){r="AES-256-CBC"}if(typeof g[r]=="undefined"){throw"PKCS5PKEY unsupported algorithm: "+r}if(typeof p=="undefined"||p==null){var t=g[r]["ivlen"];var s=k(t);p=s.toUpperCase()}var w=h(r,x,p);var v=w.keyhex;var u=f(q,r,v,p);var o=u.replace(/(.{64})/g,"$1\r\n");var n="-----BEGIN RSA PRIVATE KEY-----\r\n";n+="Proc-Type: 4,ENCRYPTED\r\n";n+="DEK-Info: "+r+","+p+"\r\n";n+="\r\n";n+=o;n+="\r\n-----END RSA PRIVATE KEY-----\r\n";return n},getEryptedPKCS5PEMFromRSAKey:function(C,D,o,s){var A=new KJUR.asn1.DERInteger({"int":0});var v=new KJUR.asn1.DERInteger({bigint:C.n});var z=new KJUR.asn1.DERInteger({"int":C.e});var B=new KJUR.asn1.DERInteger({bigint:C.d});var t=new KJUR.asn1.DERInteger({bigint:C.p});var r=new KJUR.asn1.DERInteger({bigint:C.q});var y=new KJUR.asn1.DERInteger({bigint:C.dmp1});var u=new KJUR.asn1.DERInteger({bigint:C.dmq1});var x=new KJUR.asn1.DERInteger({bigint:C.coeff});var E=new KJUR.asn1.DERSequence({array:[A,v,z,B,t,r,y,u,x]});var w=E.getEncodedHex();return this.getEryptedPKCS5PEMFromPrvKeyHex(w,D,o,s)},newEncryptedPKCS5PEM:function(n,o,r,s){if(typeof o=="undefined"||o==null){o=1024}if(typeof r=="undefined"||r==null){r="10001"}var p=new RSAKey();p.generate(o,r);var q=null;if(typeof s=="undefined"||s==null){q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n)}else{q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n,s)}return q},getRSAKeyFromPlainPKCS8PEM:function(p){if(p.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var o=this.getHexFromPEM(p,"PRIVATE KEY");var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getRSAKeyFromPlainPKCS8Hex:function(q){var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"outer DERSequence shall have 3 elements: "+p.length}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);if(o!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+o}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);var r=ASN1HEX.getHexOfTLV_AtObj(q,p[2]);var s=ASN1HEX.getHexOfV_AtObj(r,0);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(s);return n},parseHexOfEncryptedPKCS8:function(u){var q={};var p=ASN1HEX.getPosArrayOfChildren_AtObj(u,0);if(p.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+p.length}q.ciphertext=ASN1HEX.getHexOfV_AtObj(u,p[1]);var w=ASN1HEX.getPosArrayOfChildren_AtObj(u,p[0]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+w.length}if(ASN1HEX.getHexOfV_AtObj(u,w[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(u,w[1]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+n.length}var o=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[1]);if(o.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+o.length}if(ASN1HEX.getHexOfV_AtObj(u,o[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}q.encryptionSchemeAlg="TripleDES";q.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(u,o[1]);var r=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[0]);if(r.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+r.length}if(ASN1HEX.getHexOfV_AtObj(u,r[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var v=ASN1HEX.getPosArrayOfChildren_AtObj(u,r[1]);if(v.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+v.length}q.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(u,v[0]);var s=ASN1HEX.getHexOfV_AtObj(u,v[1]);try{q.pbkdf2Iter=parseInt(s,16)}catch(t){throw"malformed format pbkdf2Iter: "+s}return q},getPBKDF2KeyHexFromParam:function(s,n){var r=CryptoJS.enc.Hex.parse(s.pbkdf2Salt);var o=s.pbkdf2Iter;var q=CryptoJS.PBKDF2(n,r,{keySize:192/32,iterations:o});var p=CryptoJS.enc.Hex.stringify(q);return p},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(v,w){var p=this.getHexFromPEM(v,"ENCRYPTED PRIVATE KEY");var n=this.parseHexOfEncryptedPKCS8(p);var s=PKCS5PKEY.getPBKDF2KeyHexFromParam(n,w);var t={};t.ciphertext=CryptoJS.enc.Hex.parse(n.ciphertext);var r=CryptoJS.enc.Hex.parse(s);var q=CryptoJS.enc.Hex.parse(n.encryptionSchemeIV);var u=CryptoJS.TripleDES.decrypt(t,r,{iv:q});var o=CryptoJS.enc.Hex.stringify(u);return o},getRSAKeyFromEncryptedPKCS8PEM:function(q,p){var o=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,p);var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getKeyFromEncryptedPKCS8PEM:function(q,o){var n=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,o);var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},parsePlainPrivatePKCS8Hex:function(q){var o={};o.algparam=null;if(q.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(q.substr(p[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,p[1]);if(n.length!=2){throw"malformed PKCS8 private key(code:004)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}o.keyidx=ASN1HEX.getStartPosOfV_AtObj(q,p[2]);return o},getKeyFromPlainPrivatePKCS8PEM:function(o){var n=this.getHexFromPEM(o,"PRIVATE KEY");var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},getKeyFromPlainPrivatePKCS8Hex:function(n){var p=this.parsePlainPrivatePKCS8Hex(n);if(p.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(n,p);var o=p.key;var q=new RSAKey();q.setPrivateEx(o.n,o.e,o.d,o.p,o.q,o.dp,o.dq,o.co);return q}else{if(p.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(n,p);if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var r=KJUR.crypto.OID.oidhex2name[p.algparam];var q=new KJUR.crypto.ECDSA({curve:r,prv:p.key});return q}else{throw"unsupported private key algorithm"}}},getRSAKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getRSAKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8Hex:function(o){var n=this.parsePublicPKCS8Hex(o);if(n.algoid=="2a864886f70d010101"){var r=this.parsePublicRawRSAKeyHex(n.key);var p=new RSAKey();p.setPublic(r.n,r.e);return p}else{if(n.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[n.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+n.algparam}var q=KJUR.crypto.OID.oidhex2name[n.algparam];var p=new KJUR.crypto.ECDSA({curve:q,pub:n.key});return p}else{throw"unsupported public key algorithm"}}},parsePublicRawRSAKeyHex:function(p){var n={};if(p.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var o=ASN1HEX.getPosArrayOfChildren_AtObj(p,0);if(o.length!=2){throw"malformed RSA key(code:002)"}if(p.substr(o[0],2)!="02"){throw"malformed RSA key(code:003)"}n.n=ASN1HEX.getHexOfV_AtObj(p,o[0]);if(p.substr(o[1],2)!="02"){throw"malformed RSA key(code:004)"}n.e=ASN1HEX.getHexOfV_AtObj(p,o[1]);return n},parsePrivateRawRSAKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed RSA private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=9){throw"malformed RSA private key(code:002)"}q.key={};q.key.n=ASN1HEX.getHexOfV_AtObj(o,n[1]);q.key.e=ASN1HEX.getHexOfV_AtObj(o,n[2]);q.key.d=ASN1HEX.getHexOfV_AtObj(o,n[3]);q.key.p=ASN1HEX.getHexOfV_AtObj(o,n[4]);q.key.q=ASN1HEX.getHexOfV_AtObj(o,n[5]);q.key.dp=ASN1HEX.getHexOfV_AtObj(o,n[6]);q.key.dq=ASN1HEX.getHexOfV_AtObj(o,n[7]);q.key.co=ASN1HEX.getHexOfV_AtObj(o,n[8])},parsePrivateRawECKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed ECC private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=3){throw"malformed ECC private key(code:002)"}if(o.substr(n[1],2)!="04"){throw"malformed ECC private key(code:003)"}q.key=ASN1HEX.getHexOfV_AtObj(o,n[1])},parsePublicPKCS8Hex:function(q){var o={};o.algparam=null;var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=2){throw"outer DERSequence shall have 2 elements: "+p.length}var r=p[0];if(q.substr(r,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(n.length!=2){throw"malformed PKCS8 public key(code:002)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}o.key=ASN1HEX.getHexOfV_AtObj(q,p[1]).substr(2);return o},getRSAKeyFromPublicPKCS8Hex:function(r){var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"outer DERSequence shall have 2 elements: "+q.length}var p=ASN1HEX.getHexOfTLV_AtObj(r,q[0]);if(p!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(r.substr(q[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var t=ASN1HEX.getStartPosOfV_AtObj(r,q[1])+2;if(r.substr(t,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var n=ASN1HEX.getPosArrayOfChildren_AtObj(r,t);if(n.length!=2){throw"inner DERSequence shall have 2 elements: "+n.length}if(r.substr(n[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(r.substr(n[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var u=ASN1HEX.getHexOfV_AtObj(r,n[0]);var s=ASN1HEX.getHexOfV_AtObj(r,n[1]);var o=new RSAKey();o.setPublic(u,s);return o},}}(); \ No newline at end of file +var PKCS5PKEY=function(){var c=function(n,p,o){return i(CryptoJS.AES,n,p,o)};var d=function(n,p,o){return i(CryptoJS.TripleDES,n,p,o)};var i=function(q,v,s,o){var p=CryptoJS.enc.Hex.parse(v);var u=CryptoJS.enc.Hex.parse(s);var n=CryptoJS.enc.Hex.parse(o);var r={};r.key=u;r.iv=n;r.ciphertext=p;var t=q.decrypt(r,u,{iv:n});return CryptoJS.enc.Hex.stringify(t)};var j=function(n,p,o){return e(CryptoJS.AES,n,p,o)};var m=function(n,p,o){return e(CryptoJS.TripleDES,n,p,o)};var e=function(s,x,v,p){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(v);var o=CryptoJS.enc.Hex.parse(p);var n={};var u=s.encrypt(r,w,{iv:o});var q=CryptoJS.enc.Hex.parse(u.toString());var t=CryptoJS.enc.Base64.stringify(q);return t};var g={"AES-256-CBC":{proc:c,eproc:j,keylen:32,ivlen:16},"AES-192-CBC":{proc:c,eproc:j,keylen:24,ivlen:16},"AES-128-CBC":{proc:c,eproc:j,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:d,eproc:m,keylen:24,ivlen:8}};var b=function(n){return g[n]["proc"]};var k=function(n){var p=CryptoJS.lib.WordArray.random(n);var o=CryptoJS.enc.Hex.stringify(p);return o};var l=function(t){var u={};var o=t.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"));if(o){u.cipher=o[1];u.ivsalt=o[2]}var n=t.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));if(n){u.type=n[1]}var r=-1;var v=0;if(t.indexOf("\r\n\r\n")!=-1){r=t.indexOf("\r\n\r\n");v=2}if(t.indexOf("\n\n")!=-1){r=t.indexOf("\n\n");v=1}var q=t.indexOf("-----END");if(r!=-1&&q!=-1){var p=t.substring(r+v*2,q-v);p=p.replace(/\s+/g,"");u.data=p}return u};var h=function(o,w,n){var t=n.substring(0,16);var r=CryptoJS.enc.Hex.parse(t);var p=CryptoJS.enc.Utf8.parse(w);var s=g[o]["keylen"]+g[o]["ivlen"];var v="";var u=null;for(;;){var q=CryptoJS.algo.MD5.create();if(u!=null){q.update(u)}q.update(p);q.update(r);u=q.finalize();v=v+CryptoJS.enc.Hex.stringify(u);if(v.length>=s*2){break}}var x={};x.keyhex=v.substr(0,g[o]["keylen"]*2);x.ivhex=v.substr(g[o]["keylen"]*2,g[o]["ivlen"]*2);return x};var a=function(n,t,p,u){var q=CryptoJS.enc.Base64.parse(n);var o=CryptoJS.enc.Hex.stringify(q);var s=g[t]["proc"];var r=s(o,p,u);return r};var f=function(n,q,o,s){var p=g[q]["eproc"];var r=p(n,o,s);return r};return{version:"1.0.5",getHexFromPEM:function(o,r){var p=o;if(p.indexOf("BEGIN "+r)==-1){throw"can't find PEM header: "+r}p=p.replace("-----BEGIN "+r+"-----","");p=p.replace("-----END "+r+"-----","");var q=p.replace(/\s+/g,"");var n=b64tohex(q);return n},getDecryptedKeyHexByKeyIV:function(o,r,q,p){var n=b(r);return n(o,q,p)},parsePKCS5PEM:function(n){return l(n)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(o,n,p){return h(o,n,p)},decryptKeyB64:function(n,p,o,q){return a(n,p,o,q)},getDecryptedKeyHex:function(w,v){var o=l(w);var r=o.type;var p=o.cipher;var n=o.ivsalt;var q=o.data;var u=h(p,v,n);var t=u.keyhex;var s=a(q,p,t,n);return s},getRSAKeyFromEncryptedPKCS5PEM:function(p,o){var q=this.getDecryptedKeyHex(p,o);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(q);return n},getEryptedPKCS5PEMFromPrvKeyHex:function(q,x,r,p){var n="";if(typeof r=="undefined"||r==null){r="AES-256-CBC"}if(typeof g[r]=="undefined"){throw"PKCS5PKEY unsupported algorithm: "+r}if(typeof p=="undefined"||p==null){var t=g[r]["ivlen"];var s=k(t);p=s.toUpperCase()}var w=h(r,x,p);var v=w.keyhex;var u=f(q,r,v,p);var o=u.replace(/(.{64})/g,"$1\r\n");var n="-----BEGIN RSA PRIVATE KEY-----\r\n";n+="Proc-Type: 4,ENCRYPTED\r\n";n+="DEK-Info: "+r+","+p+"\r\n";n+="\r\n";n+=o;n+="\r\n-----END RSA PRIVATE KEY-----\r\n";return n},getEryptedPKCS5PEMFromRSAKey:function(C,D,o,s){var A=new KJUR.asn1.DERInteger({"int":0});var v=new KJUR.asn1.DERInteger({bigint:C.n});var z=new KJUR.asn1.DERInteger({"int":C.e});var B=new KJUR.asn1.DERInteger({bigint:C.d});var t=new KJUR.asn1.DERInteger({bigint:C.p});var r=new KJUR.asn1.DERInteger({bigint:C.q});var y=new KJUR.asn1.DERInteger({bigint:C.dmp1});var u=new KJUR.asn1.DERInteger({bigint:C.dmq1});var x=new KJUR.asn1.DERInteger({bigint:C.coeff});var E=new KJUR.asn1.DERSequence({array:[A,v,z,B,t,r,y,u,x]});var w=E.getEncodedHex();return this.getEryptedPKCS5PEMFromPrvKeyHex(w,D,o,s)},newEncryptedPKCS5PEM:function(n,o,r,s){if(typeof o=="undefined"||o==null){o=1024}if(typeof r=="undefined"||r==null){r="10001"}var p=new RSAKey();p.generate(o,r);var q=null;if(typeof s=="undefined"||s==null){q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n)}else{q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n,s)}return q},getRSAKeyFromPlainPKCS8PEM:function(p){if(p.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var o=this.getHexFromPEM(p,"PRIVATE KEY");var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getRSAKeyFromPlainPKCS8Hex:function(q){var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"outer DERSequence shall have 3 elements: "+p.length}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);if(o!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+o}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);var r=ASN1HEX.getHexOfTLV_AtObj(q,p[2]);var s=ASN1HEX.getHexOfV_AtObj(r,0);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(s);return n},parseHexOfEncryptedPKCS8:function(u){var q={};var p=ASN1HEX.getPosArrayOfChildren_AtObj(u,0);if(p.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+p.length}q.ciphertext=ASN1HEX.getHexOfV_AtObj(u,p[1]);var w=ASN1HEX.getPosArrayOfChildren_AtObj(u,p[0]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+w.length}if(ASN1HEX.getHexOfV_AtObj(u,w[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(u,w[1]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+n.length}var o=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[1]);if(o.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+o.length}if(ASN1HEX.getHexOfV_AtObj(u,o[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}q.encryptionSchemeAlg="TripleDES";q.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(u,o[1]);var r=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[0]);if(r.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+r.length}if(ASN1HEX.getHexOfV_AtObj(u,r[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var v=ASN1HEX.getPosArrayOfChildren_AtObj(u,r[1]);if(v.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+v.length}q.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(u,v[0]);var s=ASN1HEX.getHexOfV_AtObj(u,v[1]);try{q.pbkdf2Iter=parseInt(s,16)}catch(t){throw"malformed format pbkdf2Iter: "+s}return q},getPBKDF2KeyHexFromParam:function(s,n){var r=CryptoJS.enc.Hex.parse(s.pbkdf2Salt);var o=s.pbkdf2Iter;var q=CryptoJS.PBKDF2(n,r,{keySize:192/32,iterations:o});var p=CryptoJS.enc.Hex.stringify(q);return p},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(v,w){var p=this.getHexFromPEM(v,"ENCRYPTED PRIVATE KEY");var n=this.parseHexOfEncryptedPKCS8(p);var s=PKCS5PKEY.getPBKDF2KeyHexFromParam(n,w);var t={};t.ciphertext=CryptoJS.enc.Hex.parse(n.ciphertext);var r=CryptoJS.enc.Hex.parse(s);var q=CryptoJS.enc.Hex.parse(n.encryptionSchemeIV);var u=CryptoJS.TripleDES.decrypt(t,r,{iv:q});var o=CryptoJS.enc.Hex.stringify(u);return o},getRSAKeyFromEncryptedPKCS8PEM:function(q,p){var o=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,p);var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getKeyFromEncryptedPKCS8PEM:function(q,o){var n=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,o);var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},parsePlainPrivatePKCS8Hex:function(q){var o={};o.algparam=null;if(q.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(q.substr(p[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,p[1]);if(n.length!=2){throw"malformed PKCS8 private key(code:004)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}o.keyidx=ASN1HEX.getStartPosOfV_AtObj(q,p[2]);return o},getKeyFromPlainPrivatePKCS8PEM:function(o){var n=this.getHexFromPEM(o,"PRIVATE KEY");var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},getKeyFromPlainPrivatePKCS8Hex:function(n){var p=this.parsePlainPrivatePKCS8Hex(n);if(p.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(n,p);var o=p.key;var q=new RSAKey();q.setPrivateEx(o.n,o.e,o.d,o.p,o.q,o.dp,o.dq,o.co);return q}else{if(p.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(n,p);if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var r=KJUR.crypto.OID.oidhex2name[p.algparam];var q=new KJUR.crypto.ECDSA({curve:r,prv:p.key});return q}else{throw"unsupported private key algorithm"}}},getRSAKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getRSAKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8Hex:function(o){var n=this.parsePublicPKCS8Hex(o);if(n.algoid=="2a864886f70d010101"){var r=this.parsePublicRawRSAKeyHex(n.key);var p=new RSAKey();p.setPublic(r.n,r.e);return p}else{if(n.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[n.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+n.algparam}var q=KJUR.crypto.OID.oidhex2name[n.algparam];var p=new KJUR.crypto.ECDSA({curve:q,pub:n.key});return p}else{throw"unsupported public key algorithm"}}},parsePublicRawRSAKeyHex:function(p){var n={};if(p.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var o=ASN1HEX.getPosArrayOfChildren_AtObj(p,0);if(o.length!=2){throw"malformed RSA key(code:002)"}if(p.substr(o[0],2)!="02"){throw"malformed RSA key(code:003)"}n.n=ASN1HEX.getHexOfV_AtObj(p,o[0]);if(p.substr(o[1],2)!="02"){throw"malformed RSA key(code:004)"}n.e=ASN1HEX.getHexOfV_AtObj(p,o[1]);return n},parsePrivateRawRSAKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed RSA private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=9){throw"malformed RSA private key(code:002)"}q.key={};q.key.n=ASN1HEX.getHexOfV_AtObj(o,n[1]);q.key.e=ASN1HEX.getHexOfV_AtObj(o,n[2]);q.key.d=ASN1HEX.getHexOfV_AtObj(o,n[3]);q.key.p=ASN1HEX.getHexOfV_AtObj(o,n[4]);q.key.q=ASN1HEX.getHexOfV_AtObj(o,n[5]);q.key.dp=ASN1HEX.getHexOfV_AtObj(o,n[6]);q.key.dq=ASN1HEX.getHexOfV_AtObj(o,n[7]);q.key.co=ASN1HEX.getHexOfV_AtObj(o,n[8])},parsePrivateRawECKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed ECC private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=3){throw"malformed ECC private key(code:002)"}if(o.substr(n[1],2)!="04"){throw"malformed ECC private key(code:003)"}q.key=ASN1HEX.getHexOfV_AtObj(o,n[1])},parsePublicPKCS8Hex:function(q){var o={};o.algparam=null;var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=2){throw"outer DERSequence shall have 2 elements: "+p.length}var r=p[0];if(q.substr(r,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(n.length!=2){throw"malformed PKCS8 public key(code:002)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}o.key=ASN1HEX.getHexOfV_AtObj(q,p[1]).substr(2);return o},getRSAKeyFromPublicPKCS8Hex:function(r){var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"outer DERSequence shall have 2 elements: "+q.length}var p=ASN1HEX.getHexOfTLV_AtObj(r,q[0]);if(p!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(r.substr(q[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var t=ASN1HEX.getStartPosOfV_AtObj(r,q[1])+2;if(r.substr(t,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var n=ASN1HEX.getPosArrayOfChildren_AtObj(r,t);if(n.length!=2){throw"inner DERSequence shall have 2 elements: "+n.length}if(r.substr(n[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(r.substr(n[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var u=ASN1HEX.getHexOfV_AtObj(r,n[0]);var s=ASN1HEX.getHexOfV_AtObj(r,n[1]);var o=new RSAKey();o.setPublic(u,s);return o},}}(); \ No newline at end of file diff --git a/npm/README.md b/npm/README.md index 21c9da43..f8f82f46 100755 --- a/npm/README.md +++ b/npm/README.md @@ -3,6 +3,10 @@ jsrsasign The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free pure JavaScript cryptographic library supports RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, CMS SignedData, TimeStamp and CAdES and JSON Web Signature(JWS)/Token(JWT)/Key(JWK). +[![license](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/kjur/jsrsasign/blob/master/LICENSE.txt) +[![bower](https://img.shields.io/bower/v/jsrsasign.svg?maxAge=2592000)](https://libraries.io/bower/jsrsasign) +[![npm version](https://badge.fury.io/js/jsrsasign.svg)](https://badge.fury.io/js/jsrsasign) + Public page is http://kjur.github.com/jsrsasign . [github TOP](http://kjur.github.io/jsrsasign/)|[API doc](http://kjur.github.io/jsrsasign/api/)|[Wiki](https://github.com/kjur/jsrsasign/wiki)|[Node sample](https://github.com/kjur/jsrsasign/tree/master/sample_node) diff --git a/npm/lib/jsrsasign.js b/npm/lib/jsrsasign.js index 583c870b..117be8ff 100755 --- a/npm/lib/jsrsasign.js +++ b/npm/lib/jsrsasign.js @@ -4,7 +4,7 @@ navigator.userAgent = false; var window = {}; /* - * jsrsasign 6.1.1 (2016-09-25) (c) 2010-2016 Kenji Urushima | kjur.github.com/jsrsasign/license + * jsrsasign 6.1.2 (2016-10-08) (c) 2010-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* @@ -235,12 +235,12 @@ var jsonParse=(function(){var e="(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][ /*! asn1-1.0.11.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.asn1=="undefined"||!KJUR.asn1){KJUR.asn1={}}KJUR.asn1.ASN1Util=new function(){this.integerToByteHex=function(a){var b=a.toString(16);if((b.length%2)==1){b="0"+b}return b};this.bigIntToMinTwosComplementsHex=function(j){var f=j.toString(16);if(f.substr(0,1)!="-"){if(f.length%2==1){f="0"+f}else{if(!f.match(/^[0-7]/)){f="00"+f}}}else{var a=f.substr(1);var e=a.length;if(e%2==1){e+=1}else{if(!f.match(/^[0-7]/)){e+=2}}var g="";for(var d=0;d15){throw"ASN.1 length too long to represent by 8x: n = "+i.toString(16)}var f=128+g;return f.toString(16)+h}};this.getEncodedHex=function(){if(this.hTLV==null||this.isModified){this.hV=this.getFreshValueHex();this.hL=this.getLengthHexFromValue();this.hTLV=this.hT+this.hL+this.hV;this.isModified=false}return this.hTLV};this.getValueHex=function(){this.getEncodedHex();return this.hV};this.getFreshValueHex=function(){return""}};KJUR.asn1.DERAbstractString=function(c){KJUR.asn1.DERAbstractString.superclass.constructor.call(this);var b=null;var a=null;this.getString=function(){return this.s};this.setString=function(d){this.hTLV=null;this.isModified=true;this.s=d;this.hV=stohex(this.s)};this.setStringHex=function(d){this.hTLV=null;this.isModified=true;this.s=null;this.hV=d};this.getFreshValueHex=function(){return this.hV};if(typeof c!="undefined"){if(typeof c=="string"){this.setString(c)}else{if(typeof c.str!="undefined"){this.setString(c.str)}else{if(typeof c.hex!="undefined"){this.setStringHex(c.hex)}}}}};YAHOO.lang.extend(KJUR.asn1.DERAbstractString,KJUR.asn1.ASN1Object);KJUR.asn1.DERAbstractTime=function(c){KJUR.asn1.DERAbstractTime.superclass.constructor.call(this);var b=null;var a=null;this.localDateToUTC=function(f){utc=f.getTime()+(f.getTimezoneOffset()*60000);var e=new Date(utc);return e};this.formatDate=function(m,o,e){var g=this.zeroPadding;var n=this.localDateToUTC(m);var p=String(n.getFullYear());if(o=="utc"){p=p.substr(2,2)}var l=g(String(n.getMonth()+1),2);var q=g(String(n.getDate()),2);var h=g(String(n.getHours()),2);var i=g(String(n.getMinutes()),2);var j=g(String(n.getSeconds()),2);var r=p+l+q+h+i+j;if(e===true){var f=n.getMilliseconds();if(f!=0){var k=g(String(f),3);k=k.replace(/[0]+$/,"");r=r+"."+k}}return r+"Z"};this.zeroPadding=function(e,d){if(e.length>=d){return e}return new Array(d-e.length+1).join("0")+e};this.getString=function(){return this.s};this.setString=function(d){this.hTLV=null;this.isModified=true;this.s=d;this.hV=stohex(d)};this.setByDateValue=function(h,j,e,d,f,g){var i=new Date(Date.UTC(h,j-1,e,d,f,g,0));this.setByDate(i)};this.getFreshValueHex=function(){return this.hV}};YAHOO.lang.extend(KJUR.asn1.DERAbstractTime,KJUR.asn1.ASN1Object);KJUR.asn1.DERAbstractStructured=function(b){KJUR.asn1.DERAbstractString.superclass.constructor.call(this);var a=null;this.setByASN1ObjectArray=function(c){this.hTLV=null;this.isModified=true;this.asn1Array=c};this.appendASN1Object=function(c){this.hTLV=null;this.isModified=true;this.asn1Array.push(c)};this.asn1Array=new Array();if(typeof b!="undefined"){if(typeof b.array!="undefined"){this.asn1Array=b.array}}};YAHOO.lang.extend(KJUR.asn1.DERAbstractStructured,KJUR.asn1.ASN1Object);KJUR.asn1.DERBoolean=function(){KJUR.asn1.DERBoolean.superclass.constructor.call(this);this.hT="01";this.hTLV="0101ff"};YAHOO.lang.extend(KJUR.asn1.DERBoolean,KJUR.asn1.ASN1Object);KJUR.asn1.DERInteger=function(a){KJUR.asn1.DERInteger.superclass.constructor.call(this);this.hT="02";this.setByBigInteger=function(b){this.hTLV=null;this.isModified=true;this.hV=KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(b)};this.setByInteger=function(c){var b=new BigInteger(String(c),10);this.setByBigInteger(b)};this.setValueHex=function(b){this.hV=b};this.getFreshValueHex=function(){return this.hV};if(typeof a!="undefined"){if(typeof a.bigint!="undefined"){this.setByBigInteger(a.bigint)}else{if(typeof a["int"]!="undefined"){this.setByInteger(a["int"])}else{if(typeof a=="number"){this.setByInteger(a)}else{if(typeof a.hex!="undefined"){this.setValueHex(a.hex)}}}}}};YAHOO.lang.extend(KJUR.asn1.DERInteger,KJUR.asn1.ASN1Object);KJUR.asn1.DERBitString=function(b){if(b!==undefined&&typeof b.obj!=="undefined"){var a=KJUR.asn1.ASN1Util.newObject(b.obj);b.hex="00"+a.getEncodedHex()}KJUR.asn1.DERBitString.superclass.constructor.call(this);this.hT="03";this.setHexValueIncludingUnusedBits=function(c){this.hTLV=null;this.isModified=true;this.hV=c};this.setUnusedBitsAndHexValue=function(c,e){if(c<0||7=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c};this.getNthChildIndex_AtObj=function(d,b,e){var c=this.getPosArrayOfChildren_AtObj(d,b);return c[e]};this.getDecendantIndexByNthList=function(e,d,c){if(c.length==0){return d}var f=c.shift();var b=this.getPosArrayOfChildren_AtObj(e,d);return this.getDecendantIndexByNthList(e,b[f],c)};this.getDecendantHexTLVByNthList=function(d,c,b){var a=this.getDecendantIndexByNthList(d,c,b);return this.getHexOfTLV_AtObj(d,a)};this.getDecendantHexVByNthList=function(d,c,b){var a=this.getDecendantIndexByNthList(d,c,b);return this.getHexOfV_AtObj(d,a)}};ASN1HEX.getVbyList=function(d,c,b,e){var a=this.getDecendantIndexByNthList(d,c,b);if(a===undefined){throw"can't find nthList object"}if(e!==undefined){if(d.substr(a,2)!=e){throw"checking tag doesn't match: "+d.substr(a,2)+"!="+e}}return this.getHexOfV_AtObj(d,a)};ASN1HEX.hextooidstr=function(e){var h=function(b,a){if(b.length>=a){return b}return new Array(a-b.length+1).join("0")+b};var l=[];var o=e.substr(0,2);var f=parseInt(o,16);l[0]=new String(Math.floor(f/40));l[1]=new String(f%40);var m=e.substr(2);var k=[];for(var g=0;g0){n=n+"."+j.join(".")}return n};ASN1HEX.dump=function(e,c,k,g){var o=function(w,i){if(w.length<=i*2){return w}else{var v=w.substr(0,i)+"..(total "+w.length/2+"bytes).."+w.substr(w.length-i,i);return v}};if(c===undefined){c={ommit_long_octet:32}}if(k===undefined){k=0}if(g===undefined){g=""}var r=c.ommit_long_octet;if(e.substr(k,2)=="01"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(h=="00"){return g+"BOOLEAN FALSE\n"}else{return g+"BOOLEAN TRUE\n"}}if(e.substr(k,2)=="02"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"INTEGER "+o(h,r)+"\n"}if(e.substr(k,2)=="03"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"BITSTRING "+o(h,r)+"\n"}if(e.substr(k,2)=="04"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(ASN1HEX.isASN1HEX(h)){var j=g+"OCTETSTRING, encapsulates\n";j=j+ASN1HEX.dump(h,c,0,g+" ");return j}else{return g+"OCTETSTRING "+o(h,r)+"\n"}}if(e.substr(k,2)=="05"){return g+"NULL\n"}if(e.substr(k,2)=="06"){var l=ASN1HEX.getHexOfV_AtObj(e,k);var a=KJUR.asn1.ASN1Util.oidHexToInt(l);var n=KJUR.asn1.x509.OID.oid2name(a);var b=a.replace(/\./g," ");if(n!=""){return g+"ObjectIdentifier "+n+" ("+b+")\n"}else{return g+"ObjectIdentifier ("+b+")\n"}}if(e.substr(k,2)=="0c"){return g+"UTF8String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="13"){return g+"PrintableString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="14"){return g+"TeletexString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="16"){return g+"IA5String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="17"){return g+"UTCTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="18"){return g+"GeneralizedTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="30"){if(e.substr(k,4)=="3000"){return g+"SEQUENCE {}\n"}var j=g+"SEQUENCE\n";var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,k);var f=c;if((d.length==2||d.length==3)&&e.substr(d[0],2)=="06"&&e.substr(d[d.length-1],2)=="04"){var t=ASN1HEX.getHexOfV_AtObj(e,d[0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(t);var n=KJUR.asn1.x509.OID.oid2name(a);var p=JSON.parse(JSON.stringify(c));p.x509ExtName=n;f=p}for(var q=0;q=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c};ASN1HEX.getNthChildIndex_AtObj=function(d,b,e){var c=ASN1HEX.getPosArrayOfChildren_AtObj(d,b);return c[e]};ASN1HEX.getDecendantIndexByNthList=function(e,d,c){if(c.length==0){return d}var f=c.shift();var b=ASN1HEX.getPosArrayOfChildren_AtObj(e,d);return ASN1HEX.getDecendantIndexByNthList(e,b[f],c)};ASN1HEX.getDecendantHexTLVByNthList=function(d,c,b){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);return ASN1HEX.getHexOfTLV_AtObj(d,a)};ASN1HEX.getDecendantHexVByNthList=function(d,c,b){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);return ASN1HEX.getHexOfV_AtObj(d,a)};ASN1HEX.getVbyList=function(d,c,b,e){var a=ASN1HEX.getDecendantIndexByNthList(d,c,b);if(a===undefined){throw"can't find nthList object"}if(e!==undefined){if(d.substr(a,2)!=e){throw"checking tag doesn't match: "+d.substr(a,2)+"!="+e}}return ASN1HEX.getHexOfV_AtObj(d,a)};ASN1HEX.hextooidstr=function(e){var h=function(b,a){if(b.length>=a){return b}return new Array(a-b.length+1).join("0")+b};var l=[];var o=e.substr(0,2);var f=parseInt(o,16);l[0]=new String(Math.floor(f/40));l[1]=new String(f%40);var m=e.substr(2);var k=[];for(var g=0;g0){n=n+"."+j.join(".")}return n};ASN1HEX.dump=function(e,c,k,g){var o=function(w,i){if(w.length<=i*2){return w}else{var v=w.substr(0,i)+"..(total "+w.length/2+"bytes).."+w.substr(w.length-i,i);return v}};if(c===undefined){c={ommit_long_octet:32}}if(k===undefined){k=0}if(g===undefined){g=""}var r=c.ommit_long_octet;if(e.substr(k,2)=="01"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(h=="00"){return g+"BOOLEAN FALSE\n"}else{return g+"BOOLEAN TRUE\n"}}if(e.substr(k,2)=="02"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"INTEGER "+o(h,r)+"\n"}if(e.substr(k,2)=="03"){var h=ASN1HEX.getHexOfV_AtObj(e,k);return g+"BITSTRING "+o(h,r)+"\n"}if(e.substr(k,2)=="04"){var h=ASN1HEX.getHexOfV_AtObj(e,k);if(ASN1HEX.isASN1HEX(h)){var j=g+"OCTETSTRING, encapsulates\n";j=j+ASN1HEX.dump(h,c,0,g+" ");return j}else{return g+"OCTETSTRING "+o(h,r)+"\n"}}if(e.substr(k,2)=="05"){return g+"NULL\n"}if(e.substr(k,2)=="06"){var l=ASN1HEX.getHexOfV_AtObj(e,k);var a=KJUR.asn1.ASN1Util.oidHexToInt(l);var n=KJUR.asn1.x509.OID.oid2name(a);var b=a.replace(/\./g," ");if(n!=""){return g+"ObjectIdentifier "+n+" ("+b+")\n"}else{return g+"ObjectIdentifier ("+b+")\n"}}if(e.substr(k,2)=="0c"){return g+"UTF8String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="13"){return g+"PrintableString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="14"){return g+"TeletexString '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="16"){return g+"IA5String '"+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"'\n"}if(e.substr(k,2)=="17"){return g+"UTCTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="18"){return g+"GeneralizedTime "+hextoutf8(ASN1HEX.getHexOfV_AtObj(e,k))+"\n"}if(e.substr(k,2)=="30"){if(e.substr(k,4)=="3000"){return g+"SEQUENCE {}\n"}var j=g+"SEQUENCE\n";var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,k);var f=c;if((d.length==2||d.length==3)&&e.substr(d[0],2)=="06"&&e.substr(d[d.length-1],2)=="04"){var t=ASN1HEX.getHexOfV_AtObj(e,d[0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(t);var n=KJUR.asn1.x509.OID.oid2name(a);var p=JSON.parse(JSON.stringify(c));p.x509ExtName=n;f=p}for(var q=0;q0){var d=new KJUR.asn1.DERSequence({array:this.extensionsArray});var b=new KJUR.asn1.DERTaggedObject({explicit:true,tag:"a3",obj:d});this.asn1Array.push(b)}var e=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=e.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertificate,KJUR.asn1.ASN1Object);KJUR.asn1.x509.Extension=function(b){KJUR.asn1.x509.Extension.superclass.constructor.call(this);var a=null;this.getEncodedHex=function(){var f=new KJUR.asn1.DERObjectIdentifier({oid:this.oid});var e=new KJUR.asn1.DEROctetString({hex:this.getExtnValueHex()});var d=new Array();d.push(f);if(this.critical){d.push(new KJUR.asn1.DERBoolean())}d.push(e);var c=new KJUR.asn1.DERSequence({array:d});return c.getEncodedHex()};this.critical=false;if(typeof b!="undefined"){if(typeof b.critical!="undefined"){this.critical=b.critical}}};YAHOO.lang.extend(KJUR.asn1.x509.Extension,KJUR.asn1.ASN1Object);KJUR.asn1.x509.KeyUsage=function(a){KJUR.asn1.x509.KeyUsage.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.15";if(typeof a!="undefined"){if(typeof a.bin!="undefined"){this.asn1ExtnValue=new KJUR.asn1.DERBitString(a)}}};YAHOO.lang.extend(KJUR.asn1.x509.KeyUsage,KJUR.asn1.x509.Extension);KJUR.asn1.x509.BasicConstraints=function(c){KJUR.asn1.x509.BasicConstraints.superclass.constructor.call(this,c);var a=false;var b=-1;this.getExtnValueHex=function(){var e=new Array();if(this.cA){e.push(new KJUR.asn1.DERBoolean())}if(this.pathLen>-1){e.push(new KJUR.asn1.DERInteger({"int":this.pathLen}))}var d=new KJUR.asn1.DERSequence({array:e});this.asn1ExtnValue=d;return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.19";this.cA=false;this.pathLen=-1;if(typeof c!="undefined"){if(typeof c.cA!="undefined"){this.cA=c.cA}if(typeof c.pathLen!="undefined"){this.pathLen=c.pathLen}}};YAHOO.lang.extend(KJUR.asn1.x509.BasicConstraints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.CRLDistributionPoints=function(a){KJUR.asn1.x509.CRLDistributionPoints.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.setByDPArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence({array:b})};this.setByOneURI=function(e){var b=new KJUR.asn1.x509.GeneralNames([{uri:e}]);var d=new KJUR.asn1.x509.DistributionPointName(b);var c=new KJUR.asn1.x509.DistributionPoint({dpobj:d});this.setByDPArray([c])};this.oid="2.5.29.31";if(typeof a!="undefined"){if(typeof a.array!="undefined"){this.setByDPArray(a.array)}else{if(typeof a.uri!="undefined"){this.setByOneURI(a.uri)}}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLDistributionPoints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.ExtKeyUsage=function(a){KJUR.asn1.x509.ExtKeyUsage.superclass.constructor.call(this,a);this.setPurposeArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence();for(var c=0;c0){var c=new KJUR.asn1.DERSequence({array:this.aRevokedCert});this.asn1Array.push(c)}var d=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=d.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize=function(){this.asn1Version=null;this.asn1SignatureAlg=null;this.asn1Issuer=null;this.asn1ThisUpdate=null;this.asn1NextUpdate=null;this.aRevokedCert=new Array()};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertList,KJUR.asn1.ASN1Object);KJUR.asn1.x509.CRLEntry=function(c){KJUR.asn1.x509.CRLEntry.superclass.constructor.call(this);var b=null;var a=null;this.setCertSerial=function(d){this.sn=new KJUR.asn1.DERInteger(d)};this.setRevocationDate=function(d){this.time=new KJUR.asn1.x509.Time(d)};this.getEncodedHex=function(){var d=new KJUR.asn1.DERSequence({array:[this.sn,this.time]});this.TLV=d.getEncodedHex();return this.TLV};if(typeof c!="undefined"){if(typeof c.time!="undefined"){this.setRevocationDate(c.time)}if(typeof c.sn!="undefined"){this.setCertSerial(c.sn)}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLEntry,KJUR.asn1.ASN1Object);KJUR.asn1.x509.X500Name=function(b){KJUR.asn1.x509.X500Name.superclass.constructor.call(this);this.asn1Array=new Array();this.setByString=function(c){var d=c.split("/");d.shift();for(var e=0;e0){var d=new KJUR.asn1.DERSequence({array:this.extensionsArray});var b=new KJUR.asn1.DERTaggedObject({explicit:true,tag:"a3",obj:d});this.asn1Array.push(b)}var e=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=e.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertificate,KJUR.asn1.ASN1Object);KJUR.asn1.x509.Extension=function(b){KJUR.asn1.x509.Extension.superclass.constructor.call(this);var a=null;this.getEncodedHex=function(){var f=new KJUR.asn1.DERObjectIdentifier({oid:this.oid});var e=new KJUR.asn1.DEROctetString({hex:this.getExtnValueHex()});var d=new Array();d.push(f);if(this.critical){d.push(new KJUR.asn1.DERBoolean())}d.push(e);var c=new KJUR.asn1.DERSequence({array:d});return c.getEncodedHex()};this.critical=false;if(typeof b!="undefined"){if(typeof b.critical!="undefined"){this.critical=b.critical}}};YAHOO.lang.extend(KJUR.asn1.x509.Extension,KJUR.asn1.ASN1Object);KJUR.asn1.x509.KeyUsage=function(a){KJUR.asn1.x509.KeyUsage.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.15";if(typeof a!="undefined"){if(typeof a.bin!="undefined"){this.asn1ExtnValue=new KJUR.asn1.DERBitString(a)}}};YAHOO.lang.extend(KJUR.asn1.x509.KeyUsage,KJUR.asn1.x509.Extension);KJUR.asn1.x509.BasicConstraints=function(c){KJUR.asn1.x509.BasicConstraints.superclass.constructor.call(this,c);var a=false;var b=-1;this.getExtnValueHex=function(){var e=new Array();if(this.cA){e.push(new KJUR.asn1.DERBoolean())}if(this.pathLen>-1){e.push(new KJUR.asn1.DERInteger({"int":this.pathLen}))}var d=new KJUR.asn1.DERSequence({array:e});this.asn1ExtnValue=d;return this.asn1ExtnValue.getEncodedHex()};this.oid="2.5.29.19";this.cA=false;this.pathLen=-1;if(typeof c!="undefined"){if(typeof c.cA!="undefined"){this.cA=c.cA}if(typeof c.pathLen!="undefined"){this.pathLen=c.pathLen}}};YAHOO.lang.extend(KJUR.asn1.x509.BasicConstraints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.CRLDistributionPoints=function(a){KJUR.asn1.x509.CRLDistributionPoints.superclass.constructor.call(this,a);this.getExtnValueHex=function(){return this.asn1ExtnValue.getEncodedHex()};this.setByDPArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence({array:b})};this.setByOneURI=function(e){var b=new KJUR.asn1.x509.GeneralNames([{uri:e}]);var d=new KJUR.asn1.x509.DistributionPointName(b);var c=new KJUR.asn1.x509.DistributionPoint({dpobj:d});this.setByDPArray([c])};this.oid="2.5.29.31";if(typeof a!="undefined"){if(typeof a.array!="undefined"){this.setByDPArray(a.array)}else{if(typeof a.uri!="undefined"){this.setByOneURI(a.uri)}}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLDistributionPoints,KJUR.asn1.x509.Extension);KJUR.asn1.x509.ExtKeyUsage=function(a){KJUR.asn1.x509.ExtKeyUsage.superclass.constructor.call(this,a);this.setPurposeArray=function(b){this.asn1ExtnValue=new KJUR.asn1.DERSequence();for(var c=0;c0){var c=new KJUR.asn1.DERSequence({array:this.aRevokedCert});this.asn1Array.push(c)}var d=new KJUR.asn1.DERSequence({array:this.asn1Array});this.hTLV=d.getEncodedHex();this.isModified=false;return this.hTLV};this._initialize=function(){this.asn1Version=null;this.asn1SignatureAlg=null;this.asn1Issuer=null;this.asn1ThisUpdate=null;this.asn1NextUpdate=null;this.aRevokedCert=new Array()};this._initialize()};YAHOO.lang.extend(KJUR.asn1.x509.TBSCertList,KJUR.asn1.ASN1Object);KJUR.asn1.x509.CRLEntry=function(c){KJUR.asn1.x509.CRLEntry.superclass.constructor.call(this);var b=null;var a=null;this.setCertSerial=function(d){this.sn=new KJUR.asn1.DERInteger(d)};this.setRevocationDate=function(d){this.time=new KJUR.asn1.x509.Time(d)};this.getEncodedHex=function(){var d=new KJUR.asn1.DERSequence({array:[this.sn,this.time]});this.TLV=d.getEncodedHex();return this.TLV};if(typeof c!="undefined"){if(typeof c.time!="undefined"){this.setRevocationDate(c.time)}if(typeof c.sn!="undefined"){this.setCertSerial(c.sn)}}};YAHOO.lang.extend(KJUR.asn1.x509.CRLEntry,KJUR.asn1.ASN1Object);KJUR.asn1.x509.X500Name=function(b){KJUR.asn1.x509.X500Name.superclass.constructor.call(this);this.asn1Array=new Array();this.setByString=function(c){var d=c.split("/");d.shift();for(var e=0;e0){h=new a.DERTaggedObject({obj:this.dUnsignedAttrs,tag:"a1",explicit:false})}var g=[this.dCMSVersion,this.dSignerIdentifier,this.dDigestAlgorithm,e,this.dSigAlg,this.dSig,];if(h!=null){g.push(h)}var f=new a.DERSequence({array:g});this.hTLV=f.getEncodedHex();return this.hTLV}};YAHOO.lang.extend(KJUR.asn1.cms.SignerInfo,KJUR.asn1.ASN1Object);KJUR.asn1.cms.EncapsulatedContentInfo=function(c){KJUR.asn1.cms.EncapsulatedContentInfo.superclass.constructor.call(this);var a=KJUR.asn1;var b=KJUR.asn1.cms;var d=KJUR.asn1.x509;this.dEContentType=new a.DERObjectIdentifier({name:"data"});this.dEContent=null;this.isDetached=false;this.eContentValueHex=null;this.setContentType=function(e){if(e.match(/^[0-2][.][0-9.]+$/)){this.dEContentType=new a.DERObjectIdentifier({oid:e})}else{this.dEContentType=new a.DERObjectIdentifier({name:e})}};this.setContentValue=function(e){if(typeof e!="undefined"){if(typeof e.hex=="string"){this.eContentValueHex=e.hex}else{if(typeof e.str=="string"){this.eContentValueHex=utf8tohex(e.str)}}}};this.setContentValueHex=function(e){this.eContentValueHex=e};this.setContentValueStr=function(e){this.eContentValueHex=utf8tohex(e)};this.getEncodedHex=function(){if(typeof this.eContentValueHex!="string"){throw"eContentValue not yet set"}var g=new a.DEROctetString({hex:this.eContentValueHex});this.dEContent=new a.DERTaggedObject({obj:g,tag:"a0",explicit:true});var e=[this.dEContentType];if(!this.isDetached){e.push(this.dEContent)}var f=new a.DERSequence({array:e});this.hTLV=f.getEncodedHex();return this.hTLV}};YAHOO.lang.extend(KJUR.asn1.cms.EncapsulatedContentInfo,KJUR.asn1.ASN1Object);KJUR.asn1.cms.ContentInfo=function(c){KJUR.asn1.cms.ContentInfo.superclass.constructor.call(this);var a=KJUR.asn1;var b=KJUR.asn1.cms;var d=KJUR.asn1.x509;this.dContentType=null;this.dContent=null;this.setContentType=function(e){if(typeof e=="string"){this.dContentType=d.OID.name2obj(e)}};this.getEncodedHex=function(){var f=new a.DERTaggedObject({obj:this.dContent,tag:"a0",explicit:true});var e=new a.DERSequence({array:[this.dContentType,f]});this.hTLV=e.getEncodedHex();return this.hTLV};if(typeof c!="undefined"){if(c.type){this.setContentType(c.type)}if(c.obj&&c.obj instanceof a.ASN1Object){this.dContent=c.obj}}};YAHOO.lang.extend(KJUR.asn1.cms.ContentInfo,KJUR.asn1.ASN1Object);KJUR.asn1.cms.SignedData=function(c){KJUR.asn1.cms.SignedData.superclass.constructor.call(this);var a=KJUR.asn1;var b=KJUR.asn1.cms;var d=KJUR.asn1.x509;this.dCMSVersion=new a.DERInteger({"int":1});this.dDigestAlgs=null;this.digestAlgNameList=[];this.dEncapContentInfo=new b.EncapsulatedContentInfo();this.dCerts=null;this.certificateList=[];this.crlList=[];this.signerInfoList=[new b.SignerInfo()];this.addCertificatesByPEM=function(e){var f=KEYUTIL.getHexFromPEM(e);var g=new a.ASN1Object();g.hTLV=f;this.certificateList.push(g)};this.getEncodedHex=function(){if(typeof this.hTLV=="string"){return this.hTLV}if(this.dDigestAlgs==null){var k=[];for(var j=0;j0){var l=new a.DERSet({array:this.certificateList});this.dCerts=new a.DERTaggedObject({obj:l,tag:"a0",explicit:false})}}if(this.dCerts!=null){e.push(this.dCerts)}var g=new a.DERSet({array:this.signerInfoList});e.push(g);var f=new a.DERSequence({array:e});this.hTLV=f.getEncodedHex();return this.hTLV};this.getContentInfo=function(){this.getEncodedHex();var e=new b.ContentInfo({type:"signed-data",obj:this});return e};this.getContentInfoEncodedHex=function(){var e=this.getContentInfo();var f=e.getEncodedHex();return f};this.getPEM=function(){var e=this.getContentInfoEncodedHex();var f=a.ASN1Util.getPEMStringFromHex(e,"CMS");return f}};YAHOO.lang.extend(KJUR.asn1.cms.SignedData,KJUR.asn1.ASN1Object);KJUR.asn1.cms.CMSUtil=new function(){};KJUR.asn1.cms.CMSUtil.newSignedData=function(a){var h=KJUR.asn1.cms;var g=KJUR.asn1.cades;var f=new h.SignedData();f.dEncapContentInfo.setContentValue(a.content);if(typeof a.certs=="object"){for(var b=0;ba.length){d=a.length}for(var b=0;bd){throw"key is too short for SigAlg: keylen="+j+","+a}var b="0001";var k="00"+c;var g="";var l=d-b.length-k.length;for(var f=0;fd){throw"key is too short for SigAlg: keylen="+j+","+a}var b="0001";var k="00"+c;var g="";var l=d-b.length-k.length;for(var f=0;f=0;--p){q=q.twice2D();q.z=BigInteger.ONE;if(o.testBit(p)){if(n.testBit(p)){q=q.add2D(t)}else{q=q.add2D(s)}}else{if(n.testBit(p)){q=q.add2D(r)}}}return q}this.getBigRandom=function(i){return new BigInteger(i.bitLength(),a).mod(i.subtract(BigInteger.ONE)).add(BigInteger.ONE)};this.setNamedCurve=function(i){this.ecparams=KJUR.crypto.ECParameterDB.getByName(i);this.prvKeyHex=null;this.pubKeyHex=null;this.curveName=i};this.setPrivateKeyHex=function(i){this.isPrivate=true;this.prvKeyHex=i};this.setPublicKeyHex=function(i){this.isPublic=true;this.pubKeyHex=i};this.getPublicKeyXYHex=function(){var k=this.pubKeyHex;if(k.substr(0,2)!=="04"){throw"this method supports uncompressed format(04) only"}var j=this.ecparams.keylen/4;if(k.length!==2+j*2){throw"malformed public key hex length"}var i={};i.x=k.substr(2,j);i.y=k.substr(2+j);return i};this.getShortNISTPCurveName=function(){var i=this.curveName;if(i==="secp256r1"||i==="NIST P-256"||i==="P-256"||i==="prime256v1"){return"P-256"}if(i==="secp384r1"||i==="NIST P-384"||i==="P-384"){return"P-384"}return null};this.generateKeyPairHex=function(){var k=this.ecparams.n;var n=this.getBigRandom(k);var l=this.ecparams.G.multiply(n);var q=l.getX().toBigInteger();var o=l.getY().toBigInteger();var i=this.ecparams.keylen/4;var m=("0000000000"+n.toString(16)).slice(-i);var r=("0000000000"+q.toString(16)).slice(-i);var p=("0000000000"+o.toString(16)).slice(-i);var j="04"+r+p;this.setPrivateKeyHex(m);this.setPublicKeyHex(j);return{ecprvhex:m,ecpubhex:j}};this.signWithMessageHash=function(i){return this.signHex(i,this.prvKeyHex)};this.signHex=function(o,j){var t=new BigInteger(j,16);var l=this.ecparams.n;var q=new BigInteger(o,16);do{var m=this.getBigRandom(l);var u=this.ecparams.G;var p=u.multiply(m);var i=p.getX().toBigInteger().mod(l)}while(i.compareTo(BigInteger.ZERO)<=0);var v=m.modInverse(l).multiply(q.add(t.multiply(i))).mod(l);return KJUR.crypto.ECDSA.biRSSigToASN1Sig(i,v)};this.sign=function(m,u){var q=u;var j=this.ecparams.n;var p=BigInteger.fromByteArrayUnsigned(m);do{var l=this.getBigRandom(j);var t=this.ecparams.G;var o=t.multiply(l);var i=o.getX().toBigInteger().mod(j)}while(i.compareTo(BigInteger.ZERO)<=0);var v=l.modInverse(j).multiply(p.add(q.multiply(i))).mod(j);return this.serializeSig(i,v)};this.verifyWithMessageHash=function(j,i){return this.verifyHex(j,i,this.pubKeyHex)};this.verifyHex=function(m,i,p){var l,j;var o=KJUR.crypto.ECDSA.parseSigHex(i);l=o.r;j=o.s;var k;k=ECPointFp.decodeFromHex(this.ecparams.curve,p);var n=new BigInteger(m,16);return this.verifyRaw(n,l,j,k)};this.verify=function(o,p,j){var l,i;if(Bitcoin.Util.isArray(p)){var n=this.parseSig(p);l=n.r;i=n.s}else{if("object"===typeof p&&p.r&&p.s){l=p.r;i=p.s}else{throw"Invalid value for signature"}}var k;if(j instanceof ECPointFp){k=j}else{if(Bitcoin.Util.isArray(j)){k=ECPointFp.decodeFrom(this.ecparams.curve,j)}else{throw"Invalid format for pubkey value, must be byte array or ECPointFp"}}var m=BigInteger.fromByteArrayUnsigned(o);return this.verifyRaw(m,l,i,k)};this.verifyRaw=function(o,i,w,m){var l=this.ecparams.n;var u=this.ecparams.G;if(i.compareTo(BigInteger.ONE)<0||i.compareTo(l)>=0){return false}if(w.compareTo(BigInteger.ONE)<0||w.compareTo(l)>=0){return false}var p=w.modInverse(l);var k=o.multiply(p).mod(l);var j=i.multiply(p).mod(l);var q=u.multiply(k).add(m.multiply(j));var t=q.getX().toBigInteger().mod(l);return t.equals(i)};this.serializeSig=function(k,j){var l=k.toByteArraySigned();var i=j.toByteArraySigned();var m=[];m.push(2);m.push(l.length);m=m.concat(l);m.push(2);m.push(i.length);m=m.concat(i);m.unshift(m.length);m.unshift(48);return m};this.parseSig=function(n){var m;if(n[0]!=48){throw new Error("Signature not a valid DERSequence")}m=2;if(n[m]!=2){throw new Error("First element in signature must be a DERInteger")}var l=n.slice(m+2,m+2+n[m+1]);m+=2+n[m+1];if(n[m]!=2){throw new Error("Second element in signature must be a DERInteger")}var i=n.slice(m+2,m+2+n[m+1]);m+=2+n[m+1];var k=BigInteger.fromByteArrayUnsigned(l);var j=BigInteger.fromByteArrayUnsigned(i);return{r:k,s:j}};this.parseSigCompact=function(m){if(m.length!==65){throw"Signature has the wrong length"}var j=m[0]-27;if(j<0||j>7){throw"Invalid signature type"}var o=this.ecparams.n;var l=BigInteger.fromByteArrayUnsigned(m.slice(1,33)).mod(o);var k=BigInteger.fromByteArrayUnsigned(m.slice(33,65)).mod(o);return{r:l,s:k,i:j}};if(h!==undefined){if(h.curve!==undefined){this.curveName=h.curve}}if(this.curveName===undefined){this.curveName=e}this.setNamedCurve(this.curveName);if(h!==undefined){if(h.prv!==undefined){this.setPrivateKeyHex(h.prv)}if(h.pub!==undefined){this.setPublicKeyHex(h.pub)}}};KJUR.crypto.ECDSA.parseSigHex=function(a){var b=KJUR.crypto.ECDSA.parseSigHexInHexRS(a);var d=new BigInteger(b.r,16);var c=new BigInteger(b.s,16);return{r:d,s:c}};KJUR.crypto.ECDSA.parseSigHexInHexRS=function(c){if(c.substr(0,2)!="30"){throw"signature is not a ASN.1 sequence"}var b=ASN1HEX.getPosArrayOfChildren_AtObj(c,0);if(b.length!=2){throw"number of signature ASN.1 sequence elements seem wrong"}var g=b[0];var f=b[1];if(c.substr(g,2)!="02"){throw"1st item of sequene of signature is not ASN.1 integer"}if(c.substr(f,2)!="02"){throw"2nd item of sequene of signature is not ASN.1 integer"}var e=ASN1HEX.getHexOfV_AtObj(c,g);var d=ASN1HEX.getHexOfV_AtObj(c,f);return{r:e,s:d}};KJUR.crypto.ECDSA.asn1SigToConcatSig=function(c){var d=KJUR.crypto.ECDSA.parseSigHexInHexRS(c);var b=d.r;var a=d.s;if(b.substr(0,2)=="00"&&(((b.length/2)*8)%(16*8))==8){b=b.substr(2)}if(a.substr(0,2)=="00"&&(((a.length/2)*8)%(16*8))==8){a=a.substr(2)}if((((b.length/2)*8)%(16*8))!=0){throw"unknown ECDSA sig r length error"}if((((a.length/2)*8)%(16*8))!=0){throw"unknown ECDSA sig s length error"}return b+a};KJUR.crypto.ECDSA.concatSigToASN1Sig=function(a){if((((a.length/2)*8)%(16*8))!=0){throw"unknown ECDSA concatinated r-s sig length error"}var c=a.substr(0,a.length/2);var b=a.substr(a.length/2);return KJUR.crypto.ECDSA.hexRSSigToASN1Sig(c,b)};KJUR.crypto.ECDSA.hexRSSigToASN1Sig=function(b,a){var d=new BigInteger(b,16);var c=new BigInteger(a,16);return KJUR.crypto.ECDSA.biRSSigToASN1Sig(d,c)};KJUR.crypto.ECDSA.biRSSigToASN1Sig=function(e,c){var b=new KJUR.asn1.DERInteger({bigint:e});var a=new KJUR.asn1.DERInteger({bigint:c});var d=new KJUR.asn1.DERSequence({array:[b,a]});return d.getEncodedHex()}; @@ -271,12 +271,12 @@ if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.crypto=="undefined"|| /*! dsa-modified-1.0.1.js (c) Recurity Labs GmbH, Kenji Urushimma | github.com/openpgpjs/openpgpjs/blob/master/LICENSE */ if(typeof KJUR=="undefined"||!KJUR){KJUR={}}if(typeof KJUR.crypto=="undefined"||!KJUR.crypto){KJUR.crypto={}}KJUR.crypto.DSA=function(){this.p=null;this.q=null;this.g=null;this.y=null;this.x=null;this.type="DSA";this.setPrivate=function(z,w,v,A,u){this.isPrivate=true;this.p=z;this.q=w;this.g=v;this.y=A;this.x=u};this.setPublic=function(w,v,u,z){this.isPublic=true;this.p=w;this.q=v;this.g=u;this.y=z;this.x=null};this.signWithMessageHash=function(z){var v=this.p;var u=this.q;var C=this.g;var D=this.y;var E=this.x;var A=z.substr(0,u.bitLength()/4);var B=new BigInteger(z,16);var w=n(BigInteger.ONE.add(BigInteger.ONE),u.subtract(BigInteger.ONE));var G=(C.modPow(w,v)).mod(u);var F=(w.modInverse(u).multiply(B.add(E.multiply(G)))).mod(u);var H=KJUR.asn1.ASN1Util.jsonToASN1HEX({seq:[{"int":{bigint:G}},{"int":{bigint:F}}]});return H};this.verifyWithMessageHash=function(C,B){var z=this.p;var u=this.q;var G=this.g;var H=this.y;var E=this.parseASN1Signature(B);var K=E[0];var J=E[1];var C=C.substr(0,u.bitLength()/4);var D=new BigInteger(C,16);if(BigInteger.ZERO.compareTo(K)>0||K.compareTo(u)>0||BigInteger.ZERO.compareTo(J)>0||J.compareTo(u)>0){throw"invalid DSA signature"}var I=J.modInverse(u);var A=D.multiply(I).mod(u);var v=K.multiply(I).mod(u);var F=G.modPow(A,z).multiply(H.modPow(v,z)).mod(z).mod(u);return F.compareTo(K)==0};this.parseASN1Signature=function(u){try{var y=new BigInteger(ASN1HEX.getVbyList(u,0,[0],"02"),16);var v=new BigInteger(ASN1HEX.getVbyList(u,0,[1],"02"),16);return[y,v]}catch(w){throw"malformed DSA signature"}};function d(E,w,B,v,u,C){var z=KJUR.crypto.Util.hashString(w,E.toLowerCase());var z=z.substr(0,u.bitLength()/4);var A=new BigInteger(z,16);var y=n(BigInteger.ONE.add(BigInteger.ONE),u.subtract(BigInteger.ONE));var F=(B.modPow(y,v)).mod(u);var D=(y.modInverse(u).multiply(A.add(C.multiply(F)))).mod(u);var G=new Array();G[0]=F;G[1]=D;return G}function r(v){var u=openpgp.config.config.prefer_hash_algorithm;switch(Math.round(v.bitLength()/8)){case 20:if(u!=2&&u>11&&u!=10&&u<8){return 2}return u;case 28:if(u>11&&u<8){return 11}return u;case 32:if(u>10&&u<8){return 8}return u;default:util.print_debug("DSA select hash algorithm: returning null for an unknown length of q");return null}}this.select_hash_algorithm=r;function m(I,K,J,B,z,u,F,G){var C=KJUR.crypto.Util.hashString(B,I.toLowerCase());var C=C.substr(0,u.bitLength()/4);var D=new BigInteger(C,16);if(BigInteger.ZERO.compareTo(K)>0||K.compareTo(u)>0||BigInteger.ZERO.compareTo(J)>0||J.compareTo(u)>0){util.print_error("invalid DSA Signature");return null}var H=J.modInverse(u);var A=D.multiply(H).mod(u);var v=K.multiply(H).mod(u);var E=F.modPow(A,z).multiply(G.modPow(v,z)).mod(z).mod(u);return E.compareTo(K)==0}function a(z){var A=new BigInteger(z,primeCenterie);var y=j(q,512);var u=t(p,q,z);var v;do{v=new BigInteger(q.bitCount(),rand)}while(x.compareTo(BigInteger.ZERO)!=1&&x.compareTo(q)!=-1);var w=g.modPow(x,p);return{x:v,q:A,p:y,g:u,y:w}}function j(y,z,w){if(z%64!=0){return false}var u;var v;do{u=w(bitcount,true);v=u.subtract(BigInteger.ONE);u=u.subtract(v.remainder(y))}while(!u.isProbablePrime(primeCenterie)||u.bitLength()!=l);return u}function t(B,z,A,w){var u=B.subtract(BigInteger.ONE);var y=u.divide(z);var v;do{v=w(A)}while(v.compareTo(u)!=-1&&v.compareTo(BigInteger.ONE)!=1);return v.modPow(y,B)}function o(w,y,u){var v;do{v=u(y,false)}while(v.compareTo(w)!=-1&&v.compareTo(BigInteger.ZERO)!=1);return v}function i(v,w){k=o(v);var u=g.modPow(k,w).mod(v);return u}function h(B,w,y,v,z,u){var A=B(v);s=(w.modInverse(z).multiply(A.add(u.multiply(y)))).mod(z);return s}this.sign=d;this.verify=m;function n(w,u){if(u.compareTo(w)<=0){return}var v=u.subtract(w);var y=e(v.bitLength());while(y>v){y=e(v.bitLength())}return w.add(y)}function e(w){if(w<0){return null}var u=Math.floor((w+7)/8);var v=c(u);if(w%8>0){v=String.fromCharCode((Math.pow(2,w%8)-1)&v.charCodeAt(0))+v.substring(1)}return new BigInteger(f(v),16)}function c(w){var u="";for(var v=0;v=s*2){break}}var x={};x.keyhex=v.substr(0,g[o]["keylen"]*2);x.ivhex=v.substr(g[o]["keylen"]*2,g[o]["ivlen"]*2);return x};var a=function(n,t,p,u){var q=CryptoJS.enc.Base64.parse(n);var o=CryptoJS.enc.Hex.stringify(q);var s=g[t]["proc"];var r=s(o,p,u);return r};var f=function(n,q,o,s){var p=g[q]["eproc"];var r=p(n,o,s);return r};return{version:"1.0.5",getHexFromPEM:function(o,r){var p=o;if(p.indexOf("BEGIN "+r)==-1){throw"can't find PEM header: "+r}p=p.replace("-----BEGIN "+r+"-----","");p=p.replace("-----END "+r+"-----","");var q=p.replace(/\s+/g,"");var n=b64tohex(q);return n},getDecryptedKeyHexByKeyIV:function(o,r,q,p){var n=b(r);return n(o,q,p)},parsePKCS5PEM:function(n){return l(n)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(o,n,p){return h(o,n,p)},decryptKeyB64:function(n,p,o,q){return a(n,p,o,q)},getDecryptedKeyHex:function(w,v){var o=l(w);var r=o.type;var p=o.cipher;var n=o.ivsalt;var q=o.data;var u=h(p,v,n);var t=u.keyhex;var s=a(q,p,t,n);return s},getRSAKeyFromEncryptedPKCS5PEM:function(p,o){var q=this.getDecryptedKeyHex(p,o);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(q);return n},getEryptedPKCS5PEMFromPrvKeyHex:function(q,x,r,p){var n="";if(typeof r=="undefined"||r==null){r="AES-256-CBC"}if(typeof g[r]=="undefined"){throw"PKCS5PKEY unsupported algorithm: "+r}if(typeof p=="undefined"||p==null){var t=g[r]["ivlen"];var s=k(t);p=s.toUpperCase()}var w=h(r,x,p);var v=w.keyhex;var u=f(q,r,v,p);var o=u.replace(/(.{64})/g,"$1\r\n");var n="-----BEGIN RSA PRIVATE KEY-----\r\n";n+="Proc-Type: 4,ENCRYPTED\r\n";n+="DEK-Info: "+r+","+p+"\r\n";n+="\r\n";n+=o;n+="\r\n-----END RSA PRIVATE KEY-----\r\n";return n},getEryptedPKCS5PEMFromRSAKey:function(C,D,o,s){var A=new KJUR.asn1.DERInteger({"int":0});var v=new KJUR.asn1.DERInteger({bigint:C.n});var z=new KJUR.asn1.DERInteger({"int":C.e});var B=new KJUR.asn1.DERInteger({bigint:C.d});var t=new KJUR.asn1.DERInteger({bigint:C.p});var r=new KJUR.asn1.DERInteger({bigint:C.q});var y=new KJUR.asn1.DERInteger({bigint:C.dmp1});var u=new KJUR.asn1.DERInteger({bigint:C.dmq1});var x=new KJUR.asn1.DERInteger({bigint:C.coeff});var E=new KJUR.asn1.DERSequence({array:[A,v,z,B,t,r,y,u,x]});var w=E.getEncodedHex();return this.getEryptedPKCS5PEMFromPrvKeyHex(w,D,o,s)},newEncryptedPKCS5PEM:function(n,o,r,s){if(typeof o=="undefined"||o==null){o=1024}if(typeof r=="undefined"||r==null){r="10001"}var p=new RSAKey();p.generate(o,r);var q=null;if(typeof s=="undefined"||s==null){q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n)}else{q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n,s)}return q},getRSAKeyFromPlainPKCS8PEM:function(p){if(p.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var o=this.getHexFromPEM(p,"PRIVATE KEY");var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getRSAKeyFromPlainPKCS8Hex:function(q){var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"outer DERSequence shall have 3 elements: "+p.length}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);if(o!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+o}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);var r=ASN1HEX.getHexOfTLV_AtObj(q,p[2]);var s=ASN1HEX.getHexOfV_AtObj(r,0);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(s);return n},parseHexOfEncryptedPKCS8:function(u){var q={};var p=ASN1HEX.getPosArrayOfChildren_AtObj(u,0);if(p.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+p.length}q.ciphertext=ASN1HEX.getHexOfV_AtObj(u,p[1]);var w=ASN1HEX.getPosArrayOfChildren_AtObj(u,p[0]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+w.length}if(ASN1HEX.getHexOfV_AtObj(u,w[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(u,w[1]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+n.length}var o=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[1]);if(o.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+o.length}if(ASN1HEX.getHexOfV_AtObj(u,o[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}q.encryptionSchemeAlg="TripleDES";q.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(u,o[1]);var r=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[0]);if(r.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+r.length}if(ASN1HEX.getHexOfV_AtObj(u,r[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var v=ASN1HEX.getPosArrayOfChildren_AtObj(u,r[1]);if(v.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+v.length}q.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(u,v[0]);var s=ASN1HEX.getHexOfV_AtObj(u,v[1]);try{q.pbkdf2Iter=parseInt(s,16)}catch(t){throw"malformed format pbkdf2Iter: "+s}return q},getPBKDF2KeyHexFromParam:function(s,n){var r=CryptoJS.enc.Hex.parse(s.pbkdf2Salt);var o=s.pbkdf2Iter;var q=CryptoJS.PBKDF2(n,r,{keySize:192/32,iterations:o});var p=CryptoJS.enc.Hex.stringify(q);return p},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(v,w){var p=this.getHexFromPEM(v,"ENCRYPTED PRIVATE KEY");var n=this.parseHexOfEncryptedPKCS8(p);var s=PKCS5PKEY.getPBKDF2KeyHexFromParam(n,w);var t={};t.ciphertext=CryptoJS.enc.Hex.parse(n.ciphertext);var r=CryptoJS.enc.Hex.parse(s);var q=CryptoJS.enc.Hex.parse(n.encryptionSchemeIV);var u=CryptoJS.TripleDES.decrypt(t,r,{iv:q});var o=CryptoJS.enc.Hex.stringify(u);return o},getRSAKeyFromEncryptedPKCS8PEM:function(q,p){var o=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,p);var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getKeyFromEncryptedPKCS8PEM:function(q,o){var n=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,o);var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},parsePlainPrivatePKCS8Hex:function(q){var o={};o.algparam=null;if(q.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(q.substr(p[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,p[1]);if(n.length!=2){throw"malformed PKCS8 private key(code:004)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}o.keyidx=ASN1HEX.getStartPosOfV_AtObj(q,p[2]);return o},getKeyFromPlainPrivatePKCS8PEM:function(o){var n=this.getHexFromPEM(o,"PRIVATE KEY");var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},getKeyFromPlainPrivatePKCS8Hex:function(n){var p=this.parsePlainPrivatePKCS8Hex(n);if(p.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(n,p);var o=p.key;var q=new RSAKey();q.setPrivateEx(o.n,o.e,o.d,o.p,o.q,o.dp,o.dq,o.co);return q}else{if(p.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(n,p);if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var r=KJUR.crypto.OID.oidhex2name[p.algparam];var q=new KJUR.crypto.ECDSA({curve:r,prv:p.key});return q}else{throw"unsupported private key algorithm"}}},getRSAKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getRSAKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8Hex:function(o){var n=this.parsePublicPKCS8Hex(o);if(n.algoid=="2a864886f70d010101"){var r=this.parsePublicRawRSAKeyHex(n.key);var p=new RSAKey();p.setPublic(r.n,r.e);return p}else{if(n.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[n.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+n.algparam}var q=KJUR.crypto.OID.oidhex2name[n.algparam];var p=new KJUR.crypto.ECDSA({curve:q,pub:n.key});return p}else{throw"unsupported public key algorithm"}}},parsePublicRawRSAKeyHex:function(p){var n={};if(p.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var o=ASN1HEX.getPosArrayOfChildren_AtObj(p,0);if(o.length!=2){throw"malformed RSA key(code:002)"}if(p.substr(o[0],2)!="02"){throw"malformed RSA key(code:003)"}n.n=ASN1HEX.getHexOfV_AtObj(p,o[0]);if(p.substr(o[1],2)!="02"){throw"malformed RSA key(code:004)"}n.e=ASN1HEX.getHexOfV_AtObj(p,o[1]);return n},parsePrivateRawRSAKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed RSA private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=9){throw"malformed RSA private key(code:002)"}q.key={};q.key.n=ASN1HEX.getHexOfV_AtObj(o,n[1]);q.key.e=ASN1HEX.getHexOfV_AtObj(o,n[2]);q.key.d=ASN1HEX.getHexOfV_AtObj(o,n[3]);q.key.p=ASN1HEX.getHexOfV_AtObj(o,n[4]);q.key.q=ASN1HEX.getHexOfV_AtObj(o,n[5]);q.key.dp=ASN1HEX.getHexOfV_AtObj(o,n[6]);q.key.dq=ASN1HEX.getHexOfV_AtObj(o,n[7]);q.key.co=ASN1HEX.getHexOfV_AtObj(o,n[8])},parsePrivateRawECKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed ECC private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=3){throw"malformed ECC private key(code:002)"}if(o.substr(n[1],2)!="04"){throw"malformed ECC private key(code:003)"}q.key=ASN1HEX.getHexOfV_AtObj(o,n[1])},parsePublicPKCS8Hex:function(q){var o={};o.algparam=null;var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=2){throw"outer DERSequence shall have 2 elements: "+p.length}var r=p[0];if(q.substr(r,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(n.length!=2){throw"malformed PKCS8 public key(code:002)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}o.key=ASN1HEX.getHexOfV_AtObj(q,p[1]).substr(2);return o},getRSAKeyFromPublicPKCS8Hex:function(r){var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"outer DERSequence shall have 2 elements: "+q.length}var p=ASN1HEX.getHexOfTLV_AtObj(r,q[0]);if(p!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(r.substr(q[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var t=ASN1HEX.getStartPosOfV_AtObj(r,q[1])+2;if(r.substr(t,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var n=ASN1HEX.getPosArrayOfChildren_AtObj(r,t);if(n.length!=2){throw"inner DERSequence shall have 2 elements: "+n.length}if(r.substr(n[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(r.substr(n[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var u=ASN1HEX.getHexOfV_AtObj(r,n[0]);var s=ASN1HEX.getHexOfV_AtObj(r,n[1]);var o=new RSAKey();o.setPublic(u,s);return o},}}(); -/*! keyutil-1.0.13.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license +var PKCS5PKEY=function(){var c=function(n,p,o){return i(CryptoJS.AES,n,p,o)};var d=function(n,p,o){return i(CryptoJS.TripleDES,n,p,o)};var i=function(q,v,s,o){var p=CryptoJS.enc.Hex.parse(v);var u=CryptoJS.enc.Hex.parse(s);var n=CryptoJS.enc.Hex.parse(o);var r={};r.key=u;r.iv=n;r.ciphertext=p;var t=q.decrypt(r,u,{iv:n});return CryptoJS.enc.Hex.stringify(t)};var j=function(n,p,o){return e(CryptoJS.AES,n,p,o)};var m=function(n,p,o){return e(CryptoJS.TripleDES,n,p,o)};var e=function(s,x,v,p){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(v);var o=CryptoJS.enc.Hex.parse(p);var n={};var u=s.encrypt(r,w,{iv:o});var q=CryptoJS.enc.Hex.parse(u.toString());var t=CryptoJS.enc.Base64.stringify(q);return t};var g={"AES-256-CBC":{proc:c,eproc:j,keylen:32,ivlen:16},"AES-192-CBC":{proc:c,eproc:j,keylen:24,ivlen:16},"AES-128-CBC":{proc:c,eproc:j,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:d,eproc:m,keylen:24,ivlen:8}};var b=function(n){return g[n]["proc"]};var k=function(n){var p=CryptoJS.lib.WordArray.random(n);var o=CryptoJS.enc.Hex.stringify(p);return o};var l=function(t){var u={};var o=t.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"));if(o){u.cipher=o[1];u.ivsalt=o[2]}var n=t.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));if(n){u.type=n[1]}var r=-1;var v=0;if(t.indexOf("\r\n\r\n")!=-1){r=t.indexOf("\r\n\r\n");v=2}if(t.indexOf("\n\n")!=-1){r=t.indexOf("\n\n");v=1}var q=t.indexOf("-----END");if(r!=-1&&q!=-1){var p=t.substring(r+v*2,q-v);p=p.replace(/\s+/g,"");u.data=p}return u};var h=function(o,w,n){var t=n.substring(0,16);var r=CryptoJS.enc.Hex.parse(t);var p=CryptoJS.enc.Utf8.parse(w);var s=g[o]["keylen"]+g[o]["ivlen"];var v="";var u=null;for(;;){var q=CryptoJS.algo.MD5.create();if(u!=null){q.update(u)}q.update(p);q.update(r);u=q.finalize();v=v+CryptoJS.enc.Hex.stringify(u);if(v.length>=s*2){break}}var x={};x.keyhex=v.substr(0,g[o]["keylen"]*2);x.ivhex=v.substr(g[o]["keylen"]*2,g[o]["ivlen"]*2);return x};var a=function(n,t,p,u){var q=CryptoJS.enc.Base64.parse(n);var o=CryptoJS.enc.Hex.stringify(q);var s=g[t]["proc"];var r=s(o,p,u);return r};var f=function(n,q,o,s){var p=g[q]["eproc"];var r=p(n,o,s);return r};return{version:"1.0.5",getHexFromPEM:function(o,r){var p=o;if(p.indexOf("BEGIN "+r)==-1){throw"can't find PEM header: "+r}p=p.replace("-----BEGIN "+r+"-----","");p=p.replace("-----END "+r+"-----","");var q=p.replace(/\s+/g,"");var n=b64tohex(q);return n},getDecryptedKeyHexByKeyIV:function(o,r,q,p){var n=b(r);return n(o,q,p)},parsePKCS5PEM:function(n){return l(n)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(o,n,p){return h(o,n,p)},decryptKeyB64:function(n,p,o,q){return a(n,p,o,q)},getDecryptedKeyHex:function(w,v){var o=l(w);var r=o.type;var p=o.cipher;var n=o.ivsalt;var q=o.data;var u=h(p,v,n);var t=u.keyhex;var s=a(q,p,t,n);return s},getRSAKeyFromEncryptedPKCS5PEM:function(p,o){var q=this.getDecryptedKeyHex(p,o);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(q);return n},getEryptedPKCS5PEMFromPrvKeyHex:function(q,x,r,p){var n="";if(typeof r=="undefined"||r==null){r="AES-256-CBC"}if(typeof g[r]=="undefined"){throw"PKCS5PKEY unsupported algorithm: "+r}if(typeof p=="undefined"||p==null){var t=g[r]["ivlen"];var s=k(t);p=s.toUpperCase()}var w=h(r,x,p);var v=w.keyhex;var u=f(q,r,v,p);var o=u.replace(/(.{64})/g,"$1\r\n");var n="-----BEGIN RSA PRIVATE KEY-----\r\n";n+="Proc-Type: 4,ENCRYPTED\r\n";n+="DEK-Info: "+r+","+p+"\r\n";n+="\r\n";n+=o;n+="\r\n-----END RSA PRIVATE KEY-----\r\n";return n},getEryptedPKCS5PEMFromRSAKey:function(C,D,o,s){var A=new KJUR.asn1.DERInteger({"int":0});var v=new KJUR.asn1.DERInteger({bigint:C.n});var z=new KJUR.asn1.DERInteger({"int":C.e});var B=new KJUR.asn1.DERInteger({bigint:C.d});var t=new KJUR.asn1.DERInteger({bigint:C.p});var r=new KJUR.asn1.DERInteger({bigint:C.q});var y=new KJUR.asn1.DERInteger({bigint:C.dmp1});var u=new KJUR.asn1.DERInteger({bigint:C.dmq1});var x=new KJUR.asn1.DERInteger({bigint:C.coeff});var E=new KJUR.asn1.DERSequence({array:[A,v,z,B,t,r,y,u,x]});var w=E.getEncodedHex();return this.getEryptedPKCS5PEMFromPrvKeyHex(w,D,o,s)},newEncryptedPKCS5PEM:function(n,o,r,s){if(typeof o=="undefined"||o==null){o=1024}if(typeof r=="undefined"||r==null){r="10001"}var p=new RSAKey();p.generate(o,r);var q=null;if(typeof s=="undefined"||s==null){q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n)}else{q=this.getEncryptedPKCS5PEMFromRSAKey(pkey,n,s)}return q},getRSAKeyFromPlainPKCS8PEM:function(p){if(p.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var o=this.getHexFromPEM(p,"PRIVATE KEY");var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getRSAKeyFromPlainPKCS8Hex:function(q){var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"outer DERSequence shall have 3 elements: "+p.length}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);if(o!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+o}var o=ASN1HEX.getHexOfTLV_AtObj(q,p[1]);var r=ASN1HEX.getHexOfTLV_AtObj(q,p[2]);var s=ASN1HEX.getHexOfV_AtObj(r,0);var n=new RSAKey();n.readPrivateKeyFromASN1HexString(s);return n},parseHexOfEncryptedPKCS8:function(u){var q={};var p=ASN1HEX.getPosArrayOfChildren_AtObj(u,0);if(p.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+p.length}q.ciphertext=ASN1HEX.getHexOfV_AtObj(u,p[1]);var w=ASN1HEX.getPosArrayOfChildren_AtObj(u,p[0]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+w.length}if(ASN1HEX.getHexOfV_AtObj(u,w[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(u,w[1]);if(w.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+n.length}var o=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[1]);if(o.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+o.length}if(ASN1HEX.getHexOfV_AtObj(u,o[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}q.encryptionSchemeAlg="TripleDES";q.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(u,o[1]);var r=ASN1HEX.getPosArrayOfChildren_AtObj(u,n[0]);if(r.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+r.length}if(ASN1HEX.getHexOfV_AtObj(u,r[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var v=ASN1HEX.getPosArrayOfChildren_AtObj(u,r[1]);if(v.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+v.length}q.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(u,v[0]);var s=ASN1HEX.getHexOfV_AtObj(u,v[1]);try{q.pbkdf2Iter=parseInt(s,16)}catch(t){throw"malformed format pbkdf2Iter: "+s}return q},getPBKDF2KeyHexFromParam:function(s,n){var r=CryptoJS.enc.Hex.parse(s.pbkdf2Salt);var o=s.pbkdf2Iter;var q=CryptoJS.PBKDF2(n,r,{keySize:192/32,iterations:o});var p=CryptoJS.enc.Hex.stringify(q);return p},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(v,w){var p=this.getHexFromPEM(v,"ENCRYPTED PRIVATE KEY");var n=this.parseHexOfEncryptedPKCS8(p);var s=PKCS5PKEY.getPBKDF2KeyHexFromParam(n,w);var t={};t.ciphertext=CryptoJS.enc.Hex.parse(n.ciphertext);var r=CryptoJS.enc.Hex.parse(s);var q=CryptoJS.enc.Hex.parse(n.encryptionSchemeIV);var u=CryptoJS.TripleDES.decrypt(t,r,{iv:q});var o=CryptoJS.enc.Hex.stringify(u);return o},getRSAKeyFromEncryptedPKCS8PEM:function(q,p){var o=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,p);var n=this.getRSAKeyFromPlainPKCS8Hex(o);return n},getKeyFromEncryptedPKCS8PEM:function(q,o){var n=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(q,o);var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},parsePlainPrivatePKCS8Hex:function(q){var o={};o.algparam=null;if(q.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(q.substr(p[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,p[1]);if(n.length!=2){throw"malformed PKCS8 private key(code:004)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}o.keyidx=ASN1HEX.getStartPosOfV_AtObj(q,p[2]);return o},getKeyFromPlainPrivatePKCS8PEM:function(o){var n=this.getHexFromPEM(o,"PRIVATE KEY");var p=this.getKeyFromPlainPrivatePKCS8Hex(n);return p},getKeyFromPlainPrivatePKCS8Hex:function(n){var p=this.parsePlainPrivatePKCS8Hex(n);if(p.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(n,p);var o=p.key;var q=new RSAKey();q.setPrivateEx(o.n,o.e,o.d,o.p,o.q,o.dp,o.dq,o.co);return q}else{if(p.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(n,p);if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var r=KJUR.crypto.OID.oidhex2name[p.algparam];var q=new KJUR.crypto.ECDSA({curve:r,prv:p.key});return q}else{throw"unsupported private key algorithm"}}},getRSAKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getRSAKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8PEM:function(o){var p=this.getHexFromPEM(o,"PUBLIC KEY");var n=this.getKeyFromPublicPKCS8Hex(p);return n},getKeyFromPublicPKCS8Hex:function(o){var n=this.parsePublicPKCS8Hex(o);if(n.algoid=="2a864886f70d010101"){var r=this.parsePublicRawRSAKeyHex(n.key);var p=new RSAKey();p.setPublic(r.n,r.e);return p}else{if(n.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[n.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+n.algparam}var q=KJUR.crypto.OID.oidhex2name[n.algparam];var p=new KJUR.crypto.ECDSA({curve:q,pub:n.key});return p}else{throw"unsupported public key algorithm"}}},parsePublicRawRSAKeyHex:function(p){var n={};if(p.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var o=ASN1HEX.getPosArrayOfChildren_AtObj(p,0);if(o.length!=2){throw"malformed RSA key(code:002)"}if(p.substr(o[0],2)!="02"){throw"malformed RSA key(code:003)"}n.n=ASN1HEX.getHexOfV_AtObj(p,o[0]);if(p.substr(o[1],2)!="02"){throw"malformed RSA key(code:004)"}n.e=ASN1HEX.getHexOfV_AtObj(p,o[1]);return n},parsePrivateRawRSAKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed RSA private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=9){throw"malformed RSA private key(code:002)"}q.key={};q.key.n=ASN1HEX.getHexOfV_AtObj(o,n[1]);q.key.e=ASN1HEX.getHexOfV_AtObj(o,n[2]);q.key.d=ASN1HEX.getHexOfV_AtObj(o,n[3]);q.key.p=ASN1HEX.getHexOfV_AtObj(o,n[4]);q.key.q=ASN1HEX.getHexOfV_AtObj(o,n[5]);q.key.dp=ASN1HEX.getHexOfV_AtObj(o,n[6]);q.key.dq=ASN1HEX.getHexOfV_AtObj(o,n[7]);q.key.co=ASN1HEX.getHexOfV_AtObj(o,n[8])},parsePrivateRawECKeyHexAtObj:function(o,q){var p=q.keyidx;if(o.substr(p,2)!="30"){throw"malformed ECC private key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(o,p);if(n.length!=3){throw"malformed ECC private key(code:002)"}if(o.substr(n[1],2)!="04"){throw"malformed ECC private key(code:003)"}q.key=ASN1HEX.getHexOfV_AtObj(o,n[1])},parsePublicPKCS8Hex:function(q){var o={};o.algparam=null;var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,0);if(p.length!=2){throw"outer DERSequence shall have 2 elements: "+p.length}var r=p[0];if(q.substr(r,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var n=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(n.length!=2){throw"malformed PKCS8 public key(code:002)"}if(q.substr(n[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}o.algoid=ASN1HEX.getHexOfV_AtObj(q,n[0]);if(q.substr(n[1],2)=="06"){o.algparam=ASN1HEX.getHexOfV_AtObj(q,n[1])}if(q.substr(p[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}o.key=ASN1HEX.getHexOfV_AtObj(q,p[1]).substr(2);return o},getRSAKeyFromPublicPKCS8Hex:function(r){var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"outer DERSequence shall have 2 elements: "+q.length}var p=ASN1HEX.getHexOfTLV_AtObj(r,q[0]);if(p!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(r.substr(q[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var t=ASN1HEX.getStartPosOfV_AtObj(r,q[1])+2;if(r.substr(t,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var n=ASN1HEX.getPosArrayOfChildren_AtObj(r,t);if(n.length!=2){throw"inner DERSequence shall have 2 elements: "+n.length}if(r.substr(n[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(r.substr(n[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var u=ASN1HEX.getHexOfV_AtObj(r,n[0]);var s=ASN1HEX.getHexOfV_AtObj(r,n[1]);var o=new RSAKey();o.setPublic(u,s);return o},}}(); +/*! keyutil-1.0.14.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ -var KEYUTIL=function(){var d=function(p,r,q){return k(CryptoJS.AES,p,r,q)};var e=function(p,r,q){return k(CryptoJS.TripleDES,p,r,q)};var a=function(p,r,q){return k(CryptoJS.DES,p,r,q)};var k=function(s,x,u,q){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(u);var p=CryptoJS.enc.Hex.parse(q);var t={};t.key=w;t.iv=p;t.ciphertext=r;var v=s.decrypt(t,w,{iv:p});return CryptoJS.enc.Hex.stringify(v)};var l=function(p,r,q){return g(CryptoJS.AES,p,r,q)};var o=function(p,r,q){return g(CryptoJS.TripleDES,p,r,q)};var f=function(p,r,q){return g(CryptoJS.DES,p,r,q)};var g=function(t,y,v,q){var s=CryptoJS.enc.Hex.parse(y);var x=CryptoJS.enc.Hex.parse(v);var p=CryptoJS.enc.Hex.parse(q);var w=t.encrypt(s,x,{iv:p});var r=CryptoJS.enc.Hex.parse(w.toString());var u=CryptoJS.enc.Base64.stringify(r);return u};var i={"AES-256-CBC":{proc:d,eproc:l,keylen:32,ivlen:16},"AES-192-CBC":{proc:d,eproc:l,keylen:24,ivlen:16},"AES-128-CBC":{proc:d,eproc:l,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:e,eproc:o,keylen:24,ivlen:8},"DES-CBC":{proc:a,eproc:f,keylen:8,ivlen:8}};var c=function(p){return i[p]["proc"]};var m=function(p){var r=CryptoJS.lib.WordArray.random(p);var q=CryptoJS.enc.Hex.stringify(r);return q};var n=function(t){var u={};if(t.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"))){u.cipher=RegExp.$1;u.ivsalt=RegExp.$2}if(t.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"))){u.type=RegExp.$1}var r=-1;var v=0;if(t.indexOf("\r\n\r\n")!=-1){r=t.indexOf("\r\n\r\n");v=2}if(t.indexOf("\n\n")!=-1){r=t.indexOf("\n\n");v=1}var q=t.indexOf("-----END");if(r!=-1&&q!=-1){var p=t.substring(r+v*2,q-v);p=p.replace(/\s+/g,"");u.data=p}return u};var j=function(q,y,p){var v=p.substring(0,16);var t=CryptoJS.enc.Hex.parse(v);var r=CryptoJS.enc.Utf8.parse(y);var u=i[q]["keylen"]+i[q]["ivlen"];var x="";var w=null;for(;;){var s=CryptoJS.algo.MD5.create();if(w!=null){s.update(w)}s.update(r);s.update(t);w=s.finalize();x=x+CryptoJS.enc.Hex.stringify(w);if(x.length>=u*2){break}}var z={};z.keyhex=x.substr(0,i[q]["keylen"]*2);z.ivhex=x.substr(i[q]["keylen"]*2,i[q]["ivlen"]*2);return z};var b=function(p,v,r,w){var s=CryptoJS.enc.Base64.parse(p);var q=CryptoJS.enc.Hex.stringify(s);var u=i[v]["proc"];var t=u(q,r,w);return t};var h=function(p,s,q,u){var r=i[s]["eproc"];var t=r(p,q,u);return t};return{version:"1.0.0",getHexFromPEM:function(q,u){var r=q;if(r.indexOf("-----BEGIN ")==-1){throw"can't find PEM header: "+u}if(typeof u=="string"&&u!=""){r=r.replace("-----BEGIN "+u+"-----","");r=r.replace("-----END "+u+"-----","")}else{r=r.replace(/-----BEGIN [^-]+-----/,"");r=r.replace(/-----END [^-]+-----/,"")}var t=r.replace(/\s+/g,"");var p=b64tohex(t);return p},getDecryptedKeyHexByKeyIV:function(q,t,s,r){var p=c(t);return p(q,s,r)},parsePKCS5PEM:function(p){return n(p)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(q,p,r){return j(q,p,r)},decryptKeyB64:function(p,r,q,s){return b(p,r,q,s)},getDecryptedKeyHex:function(y,x){var q=n(y);var t=q.type;var r=q.cipher;var p=q.ivsalt;var s=q.data;var w=j(r,x,p);var v=w.keyhex;var u=b(s,r,v,p);return u},getRSAKeyFromEncryptedPKCS5PEM:function(r,q){var s=this.getDecryptedKeyHex(r,q);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(s);return p},getEncryptedPKCS5PEMFromPrvKeyHex:function(x,s,A,t,r){var p="";if(typeof t=="undefined"||t==null){t="AES-256-CBC"}if(typeof i[t]=="undefined"){throw"KEYUTIL unsupported algorithm: "+t}if(typeof r=="undefined"||r==null){var v=i[t]["ivlen"];var u=m(v);r=u.toUpperCase()}var z=j(t,A,r);var y=z.keyhex;var w=h(s,t,y,r);var q=w.replace(/(.{64})/g,"$1\r\n");var p="-----BEGIN "+x+" PRIVATE KEY-----\r\n";p+="Proc-Type: 4,ENCRYPTED\r\n";p+="DEK-Info: "+t+","+r+"\r\n";p+="\r\n";p+=q;p+="\r\n-----END "+x+" PRIVATE KEY-----\r\n";return p},getEncryptedPKCS5PEMFromRSAKey:function(D,E,r,t){var B=new KJUR.asn1.DERInteger({"int":0});var w=new KJUR.asn1.DERInteger({bigint:D.n});var A=new KJUR.asn1.DERInteger({"int":D.e});var C=new KJUR.asn1.DERInteger({bigint:D.d});var u=new KJUR.asn1.DERInteger({bigint:D.p});var s=new KJUR.asn1.DERInteger({bigint:D.q});var z=new KJUR.asn1.DERInteger({bigint:D.dmp1});var v=new KJUR.asn1.DERInteger({bigint:D.dmq1});var y=new KJUR.asn1.DERInteger({bigint:D.coeff});var F=new KJUR.asn1.DERSequence({array:[B,w,A,C,u,s,z,v,y]});var x=F.getEncodedHex();return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",x,E,r,t)},newEncryptedPKCS5PEM:function(p,q,t,u){if(typeof q=="undefined"||q==null){q=1024}if(typeof t=="undefined"||t==null){t="10001"}var r=new RSAKey();r.generate(q,t);var s=null;if(typeof u=="undefined"||u==null){s=this.getEncryptedPKCS5PEMFromRSAKey(r,p)}else{s=this.getEncryptedPKCS5PEMFromRSAKey(r,p,u)}return s},getRSAKeyFromPlainPKCS8PEM:function(r){if(r.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var q=this.getHexFromPEM(r,"PRIVATE KEY");var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getRSAKeyFromPlainPKCS8Hex:function(s){var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"outer DERSequence shall have 3 elements: "+r.length}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);if(q!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+q}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);var t=ASN1HEX.getHexOfTLV_AtObj(s,r[2]);var u=ASN1HEX.getHexOfV_AtObj(t,0);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(u);return p},parseHexOfEncryptedPKCS8:function(w){var s={};var r=ASN1HEX.getPosArrayOfChildren_AtObj(w,0);if(r.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+r.length}s.ciphertext=ASN1HEX.getHexOfV_AtObj(w,r[1]);var y=ASN1HEX.getPosArrayOfChildren_AtObj(w,r[0]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+y.length}if(ASN1HEX.getHexOfV_AtObj(w,y[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(w,y[1]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+p.length}var q=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[1]);if(q.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+q.length}if(ASN1HEX.getHexOfV_AtObj(w,q[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}s.encryptionSchemeAlg="TripleDES";s.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(w,q[1]);var t=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[0]);if(t.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+t.length}if(ASN1HEX.getHexOfV_AtObj(w,t[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var x=ASN1HEX.getPosArrayOfChildren_AtObj(w,t[1]);if(x.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+x.length}s.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(w,x[0]);var u=ASN1HEX.getHexOfV_AtObj(w,x[1]);try{s.pbkdf2Iter=parseInt(u,16)}catch(v){throw"malformed format pbkdf2Iter: "+u}return s},getPBKDF2KeyHexFromParam:function(u,p){var t=CryptoJS.enc.Hex.parse(u.pbkdf2Salt);var q=u.pbkdf2Iter;var s=CryptoJS.PBKDF2(p,t,{keySize:192/32,iterations:q});var r=CryptoJS.enc.Hex.stringify(s);return r},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(x,y){var r=this.getHexFromPEM(x,"ENCRYPTED PRIVATE KEY");var p=this.parseHexOfEncryptedPKCS8(r);var u=KEYUTIL.getPBKDF2KeyHexFromParam(p,y);var v={};v.ciphertext=CryptoJS.enc.Hex.parse(p.ciphertext);var t=CryptoJS.enc.Hex.parse(u);var s=CryptoJS.enc.Hex.parse(p.encryptionSchemeIV);var w=CryptoJS.TripleDES.decrypt(v,t,{iv:s});var q=CryptoJS.enc.Hex.stringify(w);return q},getRSAKeyFromEncryptedPKCS8PEM:function(s,r){var q=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,r);var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getKeyFromEncryptedPKCS8PEM:function(s,q){var p=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,q);var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},parsePlainPrivatePKCS8Hex:function(s){var q={};q.algparam=null;if(s.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(s.substr(r[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,r[1]);if(p.length!=2){throw"malformed PKCS8 private key(code:004)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}if(s.substr(r[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}q.keyidx=ASN1HEX.getStartPosOfV_AtObj(s,r[2]);return q},getKeyFromPlainPrivatePKCS8PEM:function(q){var p=this.getHexFromPEM(q,"PRIVATE KEY");var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},getKeyFromPlainPrivatePKCS8Hex:function(p){var w=this.parsePlainPrivatePKCS8Hex(p);if(w.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(p,w);var u=w.key;var z=new RSAKey();z.setPrivateEx(u.n,u.e,u.d,u.p,u.q,u.dp,u.dq,u.co);return z}else{if(w.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(p,w);if(KJUR.crypto.OID.oidhex2name[w.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+w.algparam}var v=KJUR.crypto.OID.oidhex2name[w.algparam];var z=new KJUR.crypto.ECDSA({curve:v});z.setPublicKeyHex(w.pubkey);z.setPrivateKeyHex(w.key);z.isPublic=false;return z}else{if(w.algoid=="2a8648ce380401"){var t=ASN1HEX.getVbyList(p,0,[1,1,0],"02");var s=ASN1HEX.getVbyList(p,0,[1,1,1],"02");var y=ASN1HEX.getVbyList(p,0,[1,1,2],"02");var B=ASN1HEX.getVbyList(p,0,[2,0],"02");var r=new BigInteger(t,16);var q=new BigInteger(s,16);var x=new BigInteger(y,16);var A=new BigInteger(B,16);var z=new KJUR.crypto.DSA();z.setPrivate(r,q,x,null,A);return z}else{throw"unsupported private key algorithm"}}}},getRSAKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getRSAKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8Hex:function(q){var p=this.parsePublicPKCS8Hex(q);if(p.algoid=="2a864886f70d010101"){var u=this.parsePublicRawRSAKeyHex(p.key);var r=new RSAKey();r.setPublic(u.n,u.e);return r}else{if(p.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var s=KJUR.crypto.OID.oidhex2name[p.algparam];var r=new KJUR.crypto.ECDSA({curve:s,pub:p.key});return r}else{if(p.algoid=="2a8648ce380401"){var t=p.algparam;var v=ASN1HEX.getHexOfV_AtObj(p.key,0);var r=new KJUR.crypto.DSA();r.setPublic(new BigInteger(t.p,16),new BigInteger(t.q,16),new BigInteger(t.g,16),new BigInteger(v,16));return r}else{throw"unsupported public key algorithm"}}}},parsePublicRawRSAKeyHex:function(r){var p={};if(r.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"malformed RSA key(code:002)"}if(r.substr(q[0],2)!="02"){throw"malformed RSA key(code:003)"}p.n=ASN1HEX.getHexOfV_AtObj(r,q[0]);if(r.substr(q[1],2)!="02"){throw"malformed RSA key(code:004)"}p.e=ASN1HEX.getHexOfV_AtObj(r,q[1]);return p},parsePrivateRawRSAKeyHexAtObj:function(q,s){var r=s.keyidx;if(q.substr(r,2)!="30"){throw"malformed RSA private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(p.length!=9){throw"malformed RSA private key(code:002)"}s.key={};s.key.n=ASN1HEX.getHexOfV_AtObj(q,p[1]);s.key.e=ASN1HEX.getHexOfV_AtObj(q,p[2]);s.key.d=ASN1HEX.getHexOfV_AtObj(q,p[3]);s.key.p=ASN1HEX.getHexOfV_AtObj(q,p[4]);s.key.q=ASN1HEX.getHexOfV_AtObj(q,p[5]);s.key.dp=ASN1HEX.getHexOfV_AtObj(q,p[6]);s.key.dq=ASN1HEX.getHexOfV_AtObj(q,p[7]);s.key.co=ASN1HEX.getHexOfV_AtObj(q,p[8])},parsePrivateRawECKeyHexAtObj:function(p,t){var q=t.keyidx;var r=ASN1HEX.getVbyList(p,q,[1],"04");var s=ASN1HEX.getVbyList(p,q,[2,0],"03").substr(2);t.key=r;t.pubkey=s},parsePublicPKCS8Hex:function(s){var q={};q.algparam=null;var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=2){throw"outer DERSequence shall have 2 elements: "+r.length}var t=r[0];if(s.substr(t,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,t);if(p.length!=2){throw"malformed PKCS8 public key(code:002)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}else{if(s.substr(p[1],2)=="30"){q.algparam={};q.algparam.p=ASN1HEX.getVbyList(s,p[1],[0],"02");q.algparam.q=ASN1HEX.getVbyList(s,p[1],[1],"02");q.algparam.g=ASN1HEX.getVbyList(s,p[1],[2],"02")}}if(s.substr(r[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}q.key=ASN1HEX.getHexOfV_AtObj(s,r[1]).substr(2);return q},getRSAKeyFromPublicPKCS8Hex:function(t){var s=ASN1HEX.getPosArrayOfChildren_AtObj(t,0);if(s.length!=2){throw"outer DERSequence shall have 2 elements: "+s.length}var r=ASN1HEX.getHexOfTLV_AtObj(t,s[0]);if(r!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(t.substr(s[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var v=ASN1HEX.getStartPosOfV_AtObj(t,s[1])+2;if(t.substr(v,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var p=ASN1HEX.getPosArrayOfChildren_AtObj(t,v);if(p.length!=2){throw"inner DERSequence shall have 2 elements: "+p.length}if(t.substr(p[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(t.substr(p[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var w=ASN1HEX.getHexOfV_AtObj(t,p[0]);var u=ASN1HEX.getHexOfV_AtObj(t,p[1]);var q=new RSAKey();q.setPublic(w,u);return q},}}();KEYUTIL.getKey=function(f,e,h){if(typeof RSAKey!="undefined"&&f instanceof RSAKey){return f}if(typeof KJUR.crypto.ECDSA!="undefined"&&f instanceof KJUR.crypto.ECDSA){return f}if(typeof KJUR.crypto.DSA!="undefined"&&f instanceof KJUR.crypto.DSA){return f}if(f.curve!==undefined&&f.xy!==undefined&&f.d===undefined){return new KJUR.crypto.ECDSA({pub:f.xy,curve:f.curve})}if(f.curve!==undefined&&f.d!==undefined){return new KJUR.crypto.ECDSA({prv:f.d,curve:f.curve})}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(f.n,f.e);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.co!==undefined&&f.qi===undefined){var w=new RSAKey();w.setPrivateEx(f.n,f.e,f.d,f.p,f.q,f.dp,f.dq,f.co);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p===undefined){var w=new RSAKey();w.setPrivate(f.n,f.e,f.d);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x===undefined){var w=new KJUR.crypto.DSA();w.setPublic(f.p,f.q,f.g,f.y);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x!==undefined){var w=new KJUR.crypto.DSA();w.setPrivate(f.p,f.q,f.g,f.y,f.x);return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(b64utohex(f.n),b64utohex(f.e));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.qi!==undefined){var w=new RSAKey();w.setPrivateEx(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d),b64utohex(f.p),b64utohex(f.q),b64utohex(f.dp),b64utohex(f.dq),b64utohex(f.qi));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined){var w=new RSAKey();w.setPrivate(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d));return w}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d===undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;d.setPublicKeyHex(m);return d}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d!==undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;var a=("0000000000"+b64utohex(f.d)).slice(-l);d.setPublicKeyHex(m);d.setPrivateKeyHex(a);return d}if(f.indexOf("-END CERTIFICATE-",0)!=-1||f.indexOf("-END X509 CERTIFICATE-",0)!=-1||f.indexOf("-END TRUSTED CERTIFICATE-",0)!=-1){return X509.getPublicKeyFromCertPEM(f)}if(h==="pkcs8pub"){return KEYUTIL.getKeyFromPublicPKCS8Hex(f)}if(f.indexOf("-END PUBLIC KEY-")!=-1){return KEYUTIL.getKeyFromPublicPKCS8PEM(f)}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var i=KEYUTIL.getHexFromPEM(f,"RSA PRIVATE KEY");return KEYUTIL.getKey(i,null,"pkcs5prv")}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var u=this.getHexFromPEM(f,"DSA PRIVATE KEY");var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(f)}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(f,e)}if(f.indexOf("-END EC PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var w=ASN1HEX.getVbyList(u,0,[1],"04");var c=ASN1HEX.getVbyList(u,0,[2,0],"06");var o=ASN1HEX.getVbyList(u,0,[3,0],"03").substr(2);var b="";if(KJUR.crypto.OID.oidhex2name[c]!==undefined){b=KJUR.crypto.OID.oidhex2name[c]}else{throw"undefined OID(hex) in KJUR.crypto.OID: "+c}var d=new KJUR.crypto.ECDSA({name:b});d.setPublicKeyHex(o);d.setPrivateKeyHex(w);d.isPublic=false;return d}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END ENCRYPTED PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromEncryptedPKCS8PEM(f,e)}throw"not supported argument"};KEYUTIL.generateKeypair=function(a,c){if(a=="RSA"){var b=c;var h=new RSAKey();h.generate(b,"10001");h.isPrivate=true;h.isPublic=true;var f=new RSAKey();var e=h.n.toString(16);var i=h.e.toString(16);f.setPublic(e,i);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{if(a=="EC"){var d=c;var g=new KJUR.crypto.ECDSA({curve:d});var j=g.generateKeyPairHex();var h=new KJUR.crypto.ECDSA({curve:d});h.setPublicKeyHex(j.ecpubhex);h.setPrivateKeyHex(j.ecprvhex);h.isPrivate=true;h.isPublic=false;var f=new KJUR.crypto.ECDSA({curve:d});f.setPublicKeyHex(j.ecpubhex);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{throw"unknown algorithm: "+a}}};KEYUTIL.getPEM=function(a,r,o,g,j){var v=KJUR.asn1;var u=KJUR.crypto;function p(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.n}},{"int":s.e},{"int":{bigint:s.d}},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.dmp1}},{"int":{bigint:s.dmq1}},{"int":{bigint:s.coeff}}]});return w}function q(w){var s=KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:w.prvKeyHex}},{tag:["a0",true,{oid:{name:w.curveName}}]},{tag:["a1",true,{bitstr:{hex:"00"+w.pubKeyHex}}]}]});return s}function n(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.g}},{"int":{bigint:s.y}},{"int":{bigint:s.x}}]});return w}if(((typeof RSAKey!="undefined"&&a instanceof RSAKey)||(typeof u.DSA!="undefined"&&a instanceof u.DSA)||(typeof u.ECDSA!="undefined"&&a instanceof u.ECDSA))&&a.isPublic==true&&(r===undefined||r=="PKCS8PUB")){var t=new KJUR.asn1.x509.SubjectPublicKeyInfo(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"PUBLIC KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o===undefined||o==null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"RSA PRIVATE KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o===undefined||o==null)&&a.isPrivate==true){var f=new KJUR.asn1.DERObjectIdentifier({name:a.curveName});var l=f.getEncodedHex();var e=q(a);var k=e.getEncodedHex();var i="";i+=v.ASN1Util.getPEMStringFromHex(l,"EC PARAMETERS");i+=v.ASN1Util.getPEMStringFromHex(k,"EC PRIVATE KEY");return i}if(r=="PKCS1PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o===undefined||o==null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"DSA PRIVATE KEY")}if(r=="PKCS5PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=q(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA",m,o,g)}var h=function(w,s){var y=b(w,s);var x=new KJUR.asn1.ASN1Util.newObject({seq:[{seq:[{oid:{name:"pkcs5PBES2"}},{seq:[{seq:[{oid:{name:"pkcs5PBKDF2"}},{seq:[{octstr:{hex:y.pbkdf2Salt}},{"int":y.pbkdf2Iter}]}]},{seq:[{oid:{name:"des-EDE3-CBC"}},{octstr:{hex:y.encryptionSchemeIV}}]}]}]},{octstr:{hex:y.ciphertext}}]});return x.getEncodedHex()};var b=function(D,E){var x=100;var C=CryptoJS.lib.WordArray.random(8);var B="DES-EDE3-CBC";var s=CryptoJS.lib.WordArray.random(8);var y=CryptoJS.PBKDF2(E,C,{keySize:192/32,iterations:x});var z=CryptoJS.enc.Hex.parse(D);var A=CryptoJS.TripleDES.encrypt(z,y,{iv:s})+"";var w={};w.ciphertext=A;w.pbkdf2Salt=CryptoJS.enc.Hex.stringify(C);w.pbkdf2Iter=x;w.encryptionSchemeAlg=B;w.encryptionSchemeIV=CryptoJS.enc.Hex.stringify(s);return w};if(r=="PKCS8PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&a.isPrivate==true){var d=p(a);var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"rsaEncryption"}},{"null":true}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&a.isPrivate==true){var d=new KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:a.prvKeyHex}},{tag:["a1",true,{bitstr:{hex:"00"+a.pubKeyHex}}]}]});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"ecPublicKey"}},{oid:{name:a.curveName}}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&a.isPrivate==true){var d=new KJUR.asn1.DERInteger({bigint:a.x});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"dsa"}},{seq:[{"int":{bigint:a.p}},{"int":{bigint:a.q}},{"int":{bigint:a.g}}]}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}throw"unsupported object nor format"};KEYUTIL.getKeyFromCSRPEM=function(b){var a=KEYUTIL.getHexFromPEM(b,"CERTIFICATE REQUEST");var c=KEYUTIL.getKeyFromCSRHex(a);return c};KEYUTIL.getKeyFromCSRHex=function(a){var c=KEYUTIL.parseCSRHex(a);var b=KEYUTIL.getKey(c.p8pubkeyhex,null,"pkcs8pub");return b};KEYUTIL.parseCSRHex=function(c){var b={};var e=c;if(e.substr(0,2)!="30"){throw"malformed CSR(code:001)"}var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,0);if(d.length<1){throw"malformed CSR(code:002)"}if(e.substr(d[0],2)!="30"){throw"malformed CSR(code:003)"}var a=ASN1HEX.getPosArrayOfChildren_AtObj(e,d[0]);if(a.length<3){throw"malformed CSR(code:004)"}b.p8pubkeyhex=ASN1HEX.getHexOfTLV_AtObj(e,a[2]);return b};KEYUTIL.getJWKFromKey=function(d){var b={};if(d instanceof RSAKey&&d.isPrivate){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));b.d=hextob64u(d.d.toString(16));b.p=hextob64u(d.p.toString(16));b.q=hextob64u(d.q.toString(16));b.dp=hextob64u(d.dmp1.toString(16));b.dq=hextob64u(d.dmq1.toString(16));b.qi=hextob64u(d.coeff.toString(16));return b}else{if(d instanceof RSAKey&&d.isPublic){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPrivate){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);b.d=hextob64u(d.prvKeyHex);return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPublic){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);return b}}}}throw"not supported key object"}; +var KEYUTIL=function(){var d=function(p,r,q){return k(CryptoJS.AES,p,r,q)};var e=function(p,r,q){return k(CryptoJS.TripleDES,p,r,q)};var a=function(p,r,q){return k(CryptoJS.DES,p,r,q)};var k=function(s,x,u,q){var r=CryptoJS.enc.Hex.parse(x);var w=CryptoJS.enc.Hex.parse(u);var p=CryptoJS.enc.Hex.parse(q);var t={};t.key=w;t.iv=p;t.ciphertext=r;var v=s.decrypt(t,w,{iv:p});return CryptoJS.enc.Hex.stringify(v)};var l=function(p,r,q){return g(CryptoJS.AES,p,r,q)};var o=function(p,r,q){return g(CryptoJS.TripleDES,p,r,q)};var f=function(p,r,q){return g(CryptoJS.DES,p,r,q)};var g=function(t,y,v,q){var s=CryptoJS.enc.Hex.parse(y);var x=CryptoJS.enc.Hex.parse(v);var p=CryptoJS.enc.Hex.parse(q);var w=t.encrypt(s,x,{iv:p});var r=CryptoJS.enc.Hex.parse(w.toString());var u=CryptoJS.enc.Base64.stringify(r);return u};var i={"AES-256-CBC":{proc:d,eproc:l,keylen:32,ivlen:16},"AES-192-CBC":{proc:d,eproc:l,keylen:24,ivlen:16},"AES-128-CBC":{proc:d,eproc:l,keylen:16,ivlen:16},"DES-EDE3-CBC":{proc:e,eproc:o,keylen:24,ivlen:8},"DES-CBC":{proc:a,eproc:f,keylen:8,ivlen:8}};var c=function(p){return i[p]["proc"]};var m=function(p){var r=CryptoJS.lib.WordArray.random(p);var q=CryptoJS.enc.Hex.stringify(r);return q};var n=function(v){var w={};var q=v.match(new RegExp("DEK-Info: ([^,]+),([0-9A-Fa-f]+)","m"));if(q){w.cipher=q[1];w.ivsalt=q[2]}var p=v.match(new RegExp("-----BEGIN ([A-Z]+) PRIVATE KEY-----"));if(p){w.type=p[1]}var u=-1;var x=0;if(v.indexOf("\r\n\r\n")!=-1){u=v.indexOf("\r\n\r\n");x=2}if(v.indexOf("\n\n")!=-1){u=v.indexOf("\n\n");x=1}var t=v.indexOf("-----END");if(u!=-1&&t!=-1){var r=v.substring(u+x*2,t-x);r=r.replace(/\s+/g,"");w.data=r}return w};var j=function(q,y,p){var v=p.substring(0,16);var t=CryptoJS.enc.Hex.parse(v);var r=CryptoJS.enc.Utf8.parse(y);var u=i[q]["keylen"]+i[q]["ivlen"];var x="";var w=null;for(;;){var s=CryptoJS.algo.MD5.create();if(w!=null){s.update(w)}s.update(r);s.update(t);w=s.finalize();x=x+CryptoJS.enc.Hex.stringify(w);if(x.length>=u*2){break}}var z={};z.keyhex=x.substr(0,i[q]["keylen"]*2);z.ivhex=x.substr(i[q]["keylen"]*2,i[q]["ivlen"]*2);return z};var b=function(p,v,r,w){var s=CryptoJS.enc.Base64.parse(p);var q=CryptoJS.enc.Hex.stringify(s);var u=i[v]["proc"];var t=u(q,r,w);return t};var h=function(p,s,q,u){var r=i[s]["eproc"];var t=r(p,q,u);return t};return{version:"1.0.0",getHexFromPEM:function(q,u){var r=q;if(r.indexOf("-----BEGIN ")==-1){throw"can't find PEM header: "+u}if(typeof u=="string"&&u!=""){r=r.replace("-----BEGIN "+u+"-----","");r=r.replace("-----END "+u+"-----","")}else{r=r.replace(/-----BEGIN [^-]+-----/,"");r=r.replace(/-----END [^-]+-----/,"")}var t=r.replace(/\s+/g,"");var p=b64tohex(t);return p},getDecryptedKeyHexByKeyIV:function(q,t,s,r){var p=c(t);return p(q,s,r)},parsePKCS5PEM:function(p){return n(p)},getKeyAndUnusedIvByPasscodeAndIvsalt:function(q,p,r){return j(q,p,r)},decryptKeyB64:function(p,r,q,s){return b(p,r,q,s)},getDecryptedKeyHex:function(y,x){var q=n(y);var t=q.type;var r=q.cipher;var p=q.ivsalt;var s=q.data;var w=j(r,x,p);var v=w.keyhex;var u=b(s,r,v,p);return u},getRSAKeyFromEncryptedPKCS5PEM:function(r,q){var s=this.getDecryptedKeyHex(r,q);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(s);return p},getEncryptedPKCS5PEMFromPrvKeyHex:function(x,s,A,t,r){var p="";if(typeof t=="undefined"||t==null){t="AES-256-CBC"}if(typeof i[t]=="undefined"){throw"KEYUTIL unsupported algorithm: "+t}if(typeof r=="undefined"||r==null){var v=i[t]["ivlen"];var u=m(v);r=u.toUpperCase()}var z=j(t,A,r);var y=z.keyhex;var w=h(s,t,y,r);var q=w.replace(/(.{64})/g,"$1\r\n");var p="-----BEGIN "+x+" PRIVATE KEY-----\r\n";p+="Proc-Type: 4,ENCRYPTED\r\n";p+="DEK-Info: "+t+","+r+"\r\n";p+="\r\n";p+=q;p+="\r\n-----END "+x+" PRIVATE KEY-----\r\n";return p},getEncryptedPKCS5PEMFromRSAKey:function(D,E,r,t){var B=new KJUR.asn1.DERInteger({"int":0});var w=new KJUR.asn1.DERInteger({bigint:D.n});var A=new KJUR.asn1.DERInteger({"int":D.e});var C=new KJUR.asn1.DERInteger({bigint:D.d});var u=new KJUR.asn1.DERInteger({bigint:D.p});var s=new KJUR.asn1.DERInteger({bigint:D.q});var z=new KJUR.asn1.DERInteger({bigint:D.dmp1});var v=new KJUR.asn1.DERInteger({bigint:D.dmq1});var y=new KJUR.asn1.DERInteger({bigint:D.coeff});var F=new KJUR.asn1.DERSequence({array:[B,w,A,C,u,s,z,v,y]});var x=F.getEncodedHex();return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",x,E,r,t)},newEncryptedPKCS5PEM:function(p,q,t,u){if(typeof q=="undefined"||q==null){q=1024}if(typeof t=="undefined"||t==null){t="10001"}var r=new RSAKey();r.generate(q,t);var s=null;if(typeof u=="undefined"||u==null){s=this.getEncryptedPKCS5PEMFromRSAKey(r,p)}else{s=this.getEncryptedPKCS5PEMFromRSAKey(r,p,u)}return s},getRSAKeyFromPlainPKCS8PEM:function(r){if(r.match(/ENCRYPTED/)){throw"pem shall be not ENCRYPTED"}var q=this.getHexFromPEM(r,"PRIVATE KEY");var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getRSAKeyFromPlainPKCS8Hex:function(s){var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"outer DERSequence shall have 3 elements: "+r.length}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);if(q!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmIdentifier is not rsaEnc: "+q}var q=ASN1HEX.getHexOfTLV_AtObj(s,r[1]);var t=ASN1HEX.getHexOfTLV_AtObj(s,r[2]);var u=ASN1HEX.getHexOfV_AtObj(t,0);var p=new RSAKey();p.readPrivateKeyFromASN1HexString(u);return p},parseHexOfEncryptedPKCS8:function(w){var s={};var r=ASN1HEX.getPosArrayOfChildren_AtObj(w,0);if(r.length!=2){throw"malformed format: SEQUENCE(0).items != 2: "+r.length}s.ciphertext=ASN1HEX.getHexOfV_AtObj(w,r[1]);var y=ASN1HEX.getPosArrayOfChildren_AtObj(w,r[0]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0).items != 2: "+y.length}if(ASN1HEX.getHexOfV_AtObj(w,y[0])!="2a864886f70d01050d"){throw"this only supports pkcs5PBES2"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(w,y[1]);if(y.length!=2){throw"malformed format: SEQUENCE(0.0.1).items != 2: "+p.length}var q=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[1]);if(q.length!=2){throw"malformed format: SEQUENCE(0.0.1.1).items != 2: "+q.length}if(ASN1HEX.getHexOfV_AtObj(w,q[0])!="2a864886f70d0307"){throw"this only supports TripleDES"}s.encryptionSchemeAlg="TripleDES";s.encryptionSchemeIV=ASN1HEX.getHexOfV_AtObj(w,q[1]);var t=ASN1HEX.getPosArrayOfChildren_AtObj(w,p[0]);if(t.length!=2){throw"malformed format: SEQUENCE(0.0.1.0).items != 2: "+t.length}if(ASN1HEX.getHexOfV_AtObj(w,t[0])!="2a864886f70d01050c"){throw"this only supports pkcs5PBKDF2"}var x=ASN1HEX.getPosArrayOfChildren_AtObj(w,t[1]);if(x.length<2){throw"malformed format: SEQUENCE(0.0.1.0.1).items < 2: "+x.length}s.pbkdf2Salt=ASN1HEX.getHexOfV_AtObj(w,x[0]);var u=ASN1HEX.getHexOfV_AtObj(w,x[1]);try{s.pbkdf2Iter=parseInt(u,16)}catch(v){throw"malformed format pbkdf2Iter: "+u}return s},getPBKDF2KeyHexFromParam:function(u,p){var t=CryptoJS.enc.Hex.parse(u.pbkdf2Salt);var q=u.pbkdf2Iter;var s=CryptoJS.PBKDF2(p,t,{keySize:192/32,iterations:q});var r=CryptoJS.enc.Hex.stringify(s);return r},getPlainPKCS8HexFromEncryptedPKCS8PEM:function(x,y){var r=this.getHexFromPEM(x,"ENCRYPTED PRIVATE KEY");var p=this.parseHexOfEncryptedPKCS8(r);var u=KEYUTIL.getPBKDF2KeyHexFromParam(p,y);var v={};v.ciphertext=CryptoJS.enc.Hex.parse(p.ciphertext);var t=CryptoJS.enc.Hex.parse(u);var s=CryptoJS.enc.Hex.parse(p.encryptionSchemeIV);var w=CryptoJS.TripleDES.decrypt(v,t,{iv:s});var q=CryptoJS.enc.Hex.stringify(w);return q},getRSAKeyFromEncryptedPKCS8PEM:function(s,r){var q=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,r);var p=this.getRSAKeyFromPlainPKCS8Hex(q);return p},getKeyFromEncryptedPKCS8PEM:function(s,q){var p=this.getPlainPKCS8HexFromEncryptedPKCS8PEM(s,q);var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},parsePlainPrivatePKCS8Hex:function(s){var q={};q.algparam=null;if(s.substr(0,2)!="30"){throw"malformed plain PKCS8 private key(code:001)"}var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=3){throw"malformed plain PKCS8 private key(code:002)"}if(s.substr(r[1],2)!="30"){throw"malformed PKCS8 private key(code:003)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,r[1]);if(p.length!=2){throw"malformed PKCS8 private key(code:004)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 private key(code:005)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}if(s.substr(r[2],2)!="04"){throw"malformed PKCS8 private key(code:006)"}q.keyidx=ASN1HEX.getStartPosOfV_AtObj(s,r[2]);return q},getKeyFromPlainPrivatePKCS8PEM:function(q){var p=this.getHexFromPEM(q,"PRIVATE KEY");var r=this.getKeyFromPlainPrivatePKCS8Hex(p);return r},getKeyFromPlainPrivatePKCS8Hex:function(p){var w=this.parsePlainPrivatePKCS8Hex(p);if(w.algoid=="2a864886f70d010101"){this.parsePrivateRawRSAKeyHexAtObj(p,w);var u=w.key;var z=new RSAKey();z.setPrivateEx(u.n,u.e,u.d,u.p,u.q,u.dp,u.dq,u.co);return z}else{if(w.algoid=="2a8648ce3d0201"){this.parsePrivateRawECKeyHexAtObj(p,w);if(KJUR.crypto.OID.oidhex2name[w.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+w.algparam}var v=KJUR.crypto.OID.oidhex2name[w.algparam];var z=new KJUR.crypto.ECDSA({curve:v});z.setPublicKeyHex(w.pubkey);z.setPrivateKeyHex(w.key);z.isPublic=false;return z}else{if(w.algoid=="2a8648ce380401"){var t=ASN1HEX.getVbyList(p,0,[1,1,0],"02");var s=ASN1HEX.getVbyList(p,0,[1,1,1],"02");var y=ASN1HEX.getVbyList(p,0,[1,1,2],"02");var B=ASN1HEX.getVbyList(p,0,[2,0],"02");var r=new BigInteger(t,16);var q=new BigInteger(s,16);var x=new BigInteger(y,16);var A=new BigInteger(B,16);var z=new KJUR.crypto.DSA();z.setPrivate(r,q,x,null,A);return z}else{throw"unsupported private key algorithm"}}}},getRSAKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getRSAKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8PEM:function(q){var r=this.getHexFromPEM(q,"PUBLIC KEY");var p=this.getKeyFromPublicPKCS8Hex(r);return p},getKeyFromPublicPKCS8Hex:function(q){var p=this.parsePublicPKCS8Hex(q);if(p.algoid=="2a864886f70d010101"){var u=this.parsePublicRawRSAKeyHex(p.key);var r=new RSAKey();r.setPublic(u.n,u.e);return r}else{if(p.algoid=="2a8648ce3d0201"){if(KJUR.crypto.OID.oidhex2name[p.algparam]===undefined){throw"KJUR.crypto.OID.oidhex2name undefined: "+p.algparam}var s=KJUR.crypto.OID.oidhex2name[p.algparam];var r=new KJUR.crypto.ECDSA({curve:s,pub:p.key});return r}else{if(p.algoid=="2a8648ce380401"){var t=p.algparam;var v=ASN1HEX.getHexOfV_AtObj(p.key,0);var r=new KJUR.crypto.DSA();r.setPublic(new BigInteger(t.p,16),new BigInteger(t.q,16),new BigInteger(t.g,16),new BigInteger(v,16));return r}else{throw"unsupported public key algorithm"}}}},parsePublicRawRSAKeyHex:function(r){var p={};if(r.substr(0,2)!="30"){throw"malformed RSA key(code:001)"}var q=ASN1HEX.getPosArrayOfChildren_AtObj(r,0);if(q.length!=2){throw"malformed RSA key(code:002)"}if(r.substr(q[0],2)!="02"){throw"malformed RSA key(code:003)"}p.n=ASN1HEX.getHexOfV_AtObj(r,q[0]);if(r.substr(q[1],2)!="02"){throw"malformed RSA key(code:004)"}p.e=ASN1HEX.getHexOfV_AtObj(r,q[1]);return p},parsePrivateRawRSAKeyHexAtObj:function(q,s){var r=s.keyidx;if(q.substr(r,2)!="30"){throw"malformed RSA private key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(q,r);if(p.length!=9){throw"malformed RSA private key(code:002)"}s.key={};s.key.n=ASN1HEX.getHexOfV_AtObj(q,p[1]);s.key.e=ASN1HEX.getHexOfV_AtObj(q,p[2]);s.key.d=ASN1HEX.getHexOfV_AtObj(q,p[3]);s.key.p=ASN1HEX.getHexOfV_AtObj(q,p[4]);s.key.q=ASN1HEX.getHexOfV_AtObj(q,p[5]);s.key.dp=ASN1HEX.getHexOfV_AtObj(q,p[6]);s.key.dq=ASN1HEX.getHexOfV_AtObj(q,p[7]);s.key.co=ASN1HEX.getHexOfV_AtObj(q,p[8])},parsePrivateRawECKeyHexAtObj:function(p,t){var q=t.keyidx;var r=ASN1HEX.getVbyList(p,q,[1],"04");var s=ASN1HEX.getVbyList(p,q,[2,0],"03").substr(2);t.key=r;t.pubkey=s},parsePublicPKCS8Hex:function(s){var q={};q.algparam=null;var r=ASN1HEX.getPosArrayOfChildren_AtObj(s,0);if(r.length!=2){throw"outer DERSequence shall have 2 elements: "+r.length}var t=r[0];if(s.substr(t,2)!="30"){throw"malformed PKCS8 public key(code:001)"}var p=ASN1HEX.getPosArrayOfChildren_AtObj(s,t);if(p.length!=2){throw"malformed PKCS8 public key(code:002)"}if(s.substr(p[0],2)!="06"){throw"malformed PKCS8 public key(code:003)"}q.algoid=ASN1HEX.getHexOfV_AtObj(s,p[0]);if(s.substr(p[1],2)=="06"){q.algparam=ASN1HEX.getHexOfV_AtObj(s,p[1])}else{if(s.substr(p[1],2)=="30"){q.algparam={};q.algparam.p=ASN1HEX.getVbyList(s,p[1],[0],"02");q.algparam.q=ASN1HEX.getVbyList(s,p[1],[1],"02");q.algparam.g=ASN1HEX.getVbyList(s,p[1],[2],"02")}}if(s.substr(r[1],2)!="03"){throw"malformed PKCS8 public key(code:004)"}q.key=ASN1HEX.getHexOfV_AtObj(s,r[1]).substr(2);return q},getRSAKeyFromPublicPKCS8Hex:function(t){var s=ASN1HEX.getPosArrayOfChildren_AtObj(t,0);if(s.length!=2){throw"outer DERSequence shall have 2 elements: "+s.length}var r=ASN1HEX.getHexOfTLV_AtObj(t,s[0]);if(r!="300d06092a864886f70d0101010500"){throw"PKCS8 AlgorithmId is not rsaEncryption"}if(t.substr(s[1],2)!="03"){throw"PKCS8 Public Key is not BITSTRING encapslated."}var v=ASN1HEX.getStartPosOfV_AtObj(t,s[1])+2;if(t.substr(v,2)!="30"){throw"PKCS8 Public Key is not SEQUENCE."}var p=ASN1HEX.getPosArrayOfChildren_AtObj(t,v);if(p.length!=2){throw"inner DERSequence shall have 2 elements: "+p.length}if(t.substr(p[0],2)!="02"){throw"N is not ASN.1 INTEGER"}if(t.substr(p[1],2)!="02"){throw"E is not ASN.1 INTEGER"}var w=ASN1HEX.getHexOfV_AtObj(t,p[0]);var u=ASN1HEX.getHexOfV_AtObj(t,p[1]);var q=new RSAKey();q.setPublic(w,u);return q},}}();KEYUTIL.getKey=function(f,e,h){if(typeof RSAKey!="undefined"&&f instanceof RSAKey){return f}if(typeof KJUR.crypto.ECDSA!="undefined"&&f instanceof KJUR.crypto.ECDSA){return f}if(typeof KJUR.crypto.DSA!="undefined"&&f instanceof KJUR.crypto.DSA){return f}if(f.curve!==undefined&&f.xy!==undefined&&f.d===undefined){return new KJUR.crypto.ECDSA({pub:f.xy,curve:f.curve})}if(f.curve!==undefined&&f.d!==undefined){return new KJUR.crypto.ECDSA({prv:f.d,curve:f.curve})}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(f.n,f.e);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.co!==undefined&&f.qi===undefined){var w=new RSAKey();w.setPrivateEx(f.n,f.e,f.d,f.p,f.q,f.dp,f.dq,f.co);return w}if(f.kty===undefined&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p===undefined){var w=new RSAKey();w.setPrivate(f.n,f.e,f.d);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x===undefined){var w=new KJUR.crypto.DSA();w.setPublic(f.p,f.q,f.g,f.y);return w}if(f.p!==undefined&&f.q!==undefined&&f.g!==undefined&&f.y!==undefined&&f.x!==undefined){var w=new KJUR.crypto.DSA();w.setPrivate(f.p,f.q,f.g,f.y,f.x);return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d===undefined){var w=new RSAKey();w.setPublic(b64utohex(f.n),b64utohex(f.e));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined&&f.p!==undefined&&f.q!==undefined&&f.dp!==undefined&&f.dq!==undefined&&f.qi!==undefined){var w=new RSAKey();w.setPrivateEx(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d),b64utohex(f.p),b64utohex(f.q),b64utohex(f.dp),b64utohex(f.dq),b64utohex(f.qi));return w}if(f.kty==="RSA"&&f.n!==undefined&&f.e!==undefined&&f.d!==undefined){var w=new RSAKey();w.setPrivate(b64utohex(f.n),b64utohex(f.e),b64utohex(f.d));return w}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d===undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;d.setPublicKeyHex(m);return d}if(f.kty==="EC"&&f.crv!==undefined&&f.x!==undefined&&f.y!==undefined&&f.d!==undefined){var d=new KJUR.crypto.ECDSA({curve:f.crv});var l=d.ecparams.keylen/4;var r=("0000000000"+b64utohex(f.x)).slice(-l);var n=("0000000000"+b64utohex(f.y)).slice(-l);var m="04"+r+n;var a=("0000000000"+b64utohex(f.d)).slice(-l);d.setPublicKeyHex(m);d.setPrivateKeyHex(a);return d}if(f.indexOf("-END CERTIFICATE-",0)!=-1||f.indexOf("-END X509 CERTIFICATE-",0)!=-1||f.indexOf("-END TRUSTED CERTIFICATE-",0)!=-1){return X509.getPublicKeyFromCertPEM(f)}if(h==="pkcs8pub"){return KEYUTIL.getKeyFromPublicPKCS8Hex(f)}if(f.indexOf("-END PUBLIC KEY-")!=-1){return KEYUTIL.getKeyFromPublicPKCS8PEM(f)}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(h==="pkcs5prv"){var w=new RSAKey();w.readPrivateKeyFromASN1HexString(f);return w}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var i=KEYUTIL.getHexFromPEM(f,"RSA PRIVATE KEY");return KEYUTIL.getKey(i,null,"pkcs5prv")}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")==-1){var u=this.getHexFromPEM(f,"DSA PRIVATE KEY");var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(f)}if(f.indexOf("-END RSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){return KEYUTIL.getRSAKeyFromEncryptedPKCS5PEM(f,e)}if(f.indexOf("-END EC PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var w=ASN1HEX.getVbyList(u,0,[1],"04");var c=ASN1HEX.getVbyList(u,0,[2,0],"06");var o=ASN1HEX.getVbyList(u,0,[3,0],"03").substr(2);var b="";if(KJUR.crypto.OID.oidhex2name[c]!==undefined){b=KJUR.crypto.OID.oidhex2name[c]}else{throw"undefined OID(hex) in KJUR.crypto.OID: "+c}var d=new KJUR.crypto.ECDSA({name:b});d.setPublicKeyHex(o);d.setPrivateKeyHex(w);d.isPublic=false;return d}if(f.indexOf("-END DSA PRIVATE KEY-")!=-1&&f.indexOf("4,ENCRYPTED")!=-1){var u=KEYUTIL.getDecryptedKeyHex(f,e);var t=ASN1HEX.getVbyList(u,0,[1],"02");var s=ASN1HEX.getVbyList(u,0,[2],"02");var v=ASN1HEX.getVbyList(u,0,[3],"02");var j=ASN1HEX.getVbyList(u,0,[4],"02");var k=ASN1HEX.getVbyList(u,0,[5],"02");var w=new KJUR.crypto.DSA();w.setPrivate(new BigInteger(t,16),new BigInteger(s,16),new BigInteger(v,16),new BigInteger(j,16),new BigInteger(k,16));return w}if(f.indexOf("-END ENCRYPTED PRIVATE KEY-")!=-1){return KEYUTIL.getKeyFromEncryptedPKCS8PEM(f,e)}throw"not supported argument"};KEYUTIL.generateKeypair=function(a,c){if(a=="RSA"){var b=c;var h=new RSAKey();h.generate(b,"10001");h.isPrivate=true;h.isPublic=true;var f=new RSAKey();var e=h.n.toString(16);var i=h.e.toString(16);f.setPublic(e,i);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{if(a=="EC"){var d=c;var g=new KJUR.crypto.ECDSA({curve:d});var j=g.generateKeyPairHex();var h=new KJUR.crypto.ECDSA({curve:d});h.setPublicKeyHex(j.ecpubhex);h.setPrivateKeyHex(j.ecprvhex);h.isPrivate=true;h.isPublic=false;var f=new KJUR.crypto.ECDSA({curve:d});f.setPublicKeyHex(j.ecpubhex);f.isPrivate=false;f.isPublic=true;var k={};k.prvKeyObj=h;k.pubKeyObj=f;return k}else{throw"unknown algorithm: "+a}}};KEYUTIL.getPEM=function(a,r,o,g,j){var v=KJUR.asn1;var u=KJUR.crypto;function p(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.n}},{"int":s.e},{"int":{bigint:s.d}},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.dmp1}},{"int":{bigint:s.dmq1}},{"int":{bigint:s.coeff}}]});return w}function q(w){var s=KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:w.prvKeyHex}},{tag:["a0",true,{oid:{name:w.curveName}}]},{tag:["a1",true,{bitstr:{hex:"00"+w.pubKeyHex}}]}]});return s}function n(s){var w=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{"int":{bigint:s.p}},{"int":{bigint:s.q}},{"int":{bigint:s.g}},{"int":{bigint:s.y}},{"int":{bigint:s.x}}]});return w}if(((typeof RSAKey!="undefined"&&a instanceof RSAKey)||(typeof u.DSA!="undefined"&&a instanceof u.DSA)||(typeof u.ECDSA!="undefined"&&a instanceof u.ECDSA))&&a.isPublic==true&&(r===undefined||r=="PKCS8PUB")){var t=new KJUR.asn1.x509.SubjectPublicKeyInfo(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"PUBLIC KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o===undefined||o==null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"RSA PRIVATE KEY")}if(r=="PKCS1PRV"&&typeof RSAKey!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o===undefined||o==null)&&a.isPrivate==true){var f=new KJUR.asn1.DERObjectIdentifier({name:a.curveName});var l=f.getEncodedHex();var e=q(a);var k=e.getEncodedHex();var i="";i+=v.ASN1Util.getPEMStringFromHex(l,"EC PARAMETERS");i+=v.ASN1Util.getPEMStringFromHex(k,"EC PRIVATE KEY");return i}if(r=="PKCS1PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o===undefined||o==null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();return v.ASN1Util.getPEMStringFromHex(m,"DSA PRIVATE KEY")}if(r=="PKCS5PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=p(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("RSA",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=q(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("EC",m,o,g)}if(r=="PKCS5PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&(o!==undefined&&o!=null)&&a.isPrivate==true){var t=n(a);var m=t.getEncodedHex();if(g===undefined){g="DES-EDE3-CBC"}return this.getEncryptedPKCS5PEMFromPrvKeyHex("DSA",m,o,g)}var h=function(w,s){var y=b(w,s);var x=new KJUR.asn1.ASN1Util.newObject({seq:[{seq:[{oid:{name:"pkcs5PBES2"}},{seq:[{seq:[{oid:{name:"pkcs5PBKDF2"}},{seq:[{octstr:{hex:y.pbkdf2Salt}},{"int":y.pbkdf2Iter}]}]},{seq:[{oid:{name:"des-EDE3-CBC"}},{octstr:{hex:y.encryptionSchemeIV}}]}]}]},{octstr:{hex:y.ciphertext}}]});return x.getEncodedHex()};var b=function(D,E){var x=100;var C=CryptoJS.lib.WordArray.random(8);var B="DES-EDE3-CBC";var s=CryptoJS.lib.WordArray.random(8);var y=CryptoJS.PBKDF2(E,C,{keySize:192/32,iterations:x});var z=CryptoJS.enc.Hex.parse(D);var A=CryptoJS.TripleDES.encrypt(z,y,{iv:s})+"";var w={};w.ciphertext=A;w.pbkdf2Salt=CryptoJS.enc.Hex.stringify(C);w.pbkdf2Iter=x;w.encryptionSchemeAlg=B;w.encryptionSchemeIV=CryptoJS.enc.Hex.stringify(s);return w};if(r=="PKCS8PRV"&&typeof RSAKey!="undefined"&&a instanceof RSAKey&&a.isPrivate==true){var d=p(a);var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"rsaEncryption"}},{"null":true}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.ECDSA!="undefined"&&a instanceof KJUR.crypto.ECDSA&&a.isPrivate==true){var d=new KJUR.asn1.ASN1Util.newObject({seq:[{"int":1},{octstr:{hex:a.prvKeyHex}},{tag:["a1",true,{bitstr:{hex:"00"+a.pubKeyHex}}]}]});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"ecPublicKey"}},{oid:{name:a.curveName}}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}if(r=="PKCS8PRV"&&typeof KJUR.crypto.DSA!="undefined"&&a instanceof KJUR.crypto.DSA&&a.isPrivate==true){var d=new KJUR.asn1.DERInteger({bigint:a.x});var c=d.getEncodedHex();var t=KJUR.asn1.ASN1Util.newObject({seq:[{"int":0},{seq:[{oid:{name:"dsa"}},{seq:[{"int":{bigint:a.p}},{"int":{bigint:a.q}},{"int":{bigint:a.g}}]}]},{octstr:{hex:c}}]});var m=t.getEncodedHex();if(o===undefined||o==null){return v.ASN1Util.getPEMStringFromHex(m,"PRIVATE KEY")}else{var k=h(m,o);return v.ASN1Util.getPEMStringFromHex(k,"ENCRYPTED PRIVATE KEY")}}throw"unsupported object nor format"};KEYUTIL.getKeyFromCSRPEM=function(b){var a=KEYUTIL.getHexFromPEM(b,"CERTIFICATE REQUEST");var c=KEYUTIL.getKeyFromCSRHex(a);return c};KEYUTIL.getKeyFromCSRHex=function(a){var c=KEYUTIL.parseCSRHex(a);var b=KEYUTIL.getKey(c.p8pubkeyhex,null,"pkcs8pub");return b};KEYUTIL.parseCSRHex=function(c){var b={};var e=c;if(e.substr(0,2)!="30"){throw"malformed CSR(code:001)"}var d=ASN1HEX.getPosArrayOfChildren_AtObj(e,0);if(d.length<1){throw"malformed CSR(code:002)"}if(e.substr(d[0],2)!="30"){throw"malformed CSR(code:003)"}var a=ASN1HEX.getPosArrayOfChildren_AtObj(e,d[0]);if(a.length<3){throw"malformed CSR(code:004)"}b.p8pubkeyhex=ASN1HEX.getHexOfTLV_AtObj(e,a[2]);return b};KEYUTIL.getJWKFromKey=function(d){var b={};if(d instanceof RSAKey&&d.isPrivate){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));b.d=hextob64u(d.d.toString(16));b.p=hextob64u(d.p.toString(16));b.q=hextob64u(d.q.toString(16));b.dp=hextob64u(d.dmp1.toString(16));b.dq=hextob64u(d.dmq1.toString(16));b.qi=hextob64u(d.coeff.toString(16));return b}else{if(d instanceof RSAKey&&d.isPublic){b.kty="RSA";b.n=hextob64u(d.n.toString(16));b.e=hextob64u(d.e.toString(16));return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPrivate){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);b.d=hextob64u(d.prvKeyHex);return b}else{if(d instanceof KJUR.crypto.ECDSA&&d.isPublic){var a=d.getShortNISTPCurveName();if(a!=="P-256"&&a!=="P-384"){throw"unsupported curve name for JWT: "+a}var c=d.getPublicKeyXYHex();b.kty="EC";b.crv=a;b.x=hextob64u(c.x);b.y=hextob64u(c.y);return b}}}}throw"not supported key object"}; /*! rsapem-1.1.js (c) 2012 Kenji Urushima | kjur.github.com/jsrsasign/license */ function _rsapem_pemToBase64(b){var a=b;a=a.replace("-----BEGIN RSA PRIVATE KEY-----","");a=a.replace("-----END RSA PRIVATE KEY-----","");a=a.replace(/[ \n]+/g,"");return a}function _rsapem_getPosArrayOfChildrenFromHex(d){var j=new Array();var k=ASN1HEX.getStartPosOfV_AtObj(d,0);var f=ASN1HEX.getPosOfNextSibling_AtObj(d,k);var h=ASN1HEX.getPosOfNextSibling_AtObj(d,f);var b=ASN1HEX.getPosOfNextSibling_AtObj(d,h);var l=ASN1HEX.getPosOfNextSibling_AtObj(d,b);var e=ASN1HEX.getPosOfNextSibling_AtObj(d,l);var g=ASN1HEX.getPosOfNextSibling_AtObj(d,e);var c=ASN1HEX.getPosOfNextSibling_AtObj(d,g);var i=ASN1HEX.getPosOfNextSibling_AtObj(d,c);j.push(k,f,h,b,l,e,g,c,i);return j}function _rsapem_getHexValueArrayOfChildrenFromHex(i){var o=_rsapem_getPosArrayOfChildrenFromHex(i);var r=ASN1HEX.getHexOfV_AtObj(i,o[0]);var f=ASN1HEX.getHexOfV_AtObj(i,o[1]);var j=ASN1HEX.getHexOfV_AtObj(i,o[2]);var k=ASN1HEX.getHexOfV_AtObj(i,o[3]);var c=ASN1HEX.getHexOfV_AtObj(i,o[4]);var b=ASN1HEX.getHexOfV_AtObj(i,o[5]);var h=ASN1HEX.getHexOfV_AtObj(i,o[6]);var g=ASN1HEX.getHexOfV_AtObj(i,o[7]);var l=ASN1HEX.getHexOfV_AtObj(i,o[8]);var m=new Array();m.push(r,f,j,k,c,b,h,g,l);return m}function _rsapem_readPrivateKeyFromASN1HexString(c){var b=_rsapem_getHexValueArrayOfChildrenFromHex(c);this.setPrivateEx(b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8])}function _rsapem_readPrivateKeyFromPEMString(e){var c=_rsapem_pemToBase64(e);var d=b64tohex(c);var b=_rsapem_getHexValueArrayOfChildrenFromHex(d);this.setPrivateEx(b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8])}RSAKey.prototype.readPrivateKeyFromPEMString=_rsapem_readPrivateKeyFromPEMString;RSAKey.prototype.readPrivateKeyFromASN1HexString=_rsapem_readPrivateKeyFromASN1HexString; @@ -286,9 +286,9 @@ var _RE_HEXDECONLY=new RegExp("");_RE_HEXDECONLY.compile("[^0-9a-f]","gi");funct /*! x509-1.1.9.js (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ function X509(){this.subjectPublicKeyRSA=null;this.subjectPublicKeyRSA_hN=null;this.subjectPublicKeyRSA_hE=null;this.hex=null;this.getSerialNumberHex=function(){return ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,1])};this.getSignatureAlgorithmField=function(){var b=ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,2,0]);var a=KJUR.asn1.ASN1Util.oidHexToInt(b);var c=KJUR.asn1.x509.OID.oid2name(a);return c};this.getIssuerHex=function(){return ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,3])};this.getIssuerString=function(){return X509.hex2dn(ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,3]))};this.getSubjectHex=function(){return ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,5])};this.getSubjectString=function(){return X509.hex2dn(ASN1HEX.getDecendantHexTLVByNthList(this.hex,0,[0,5]))};this.getNotBefore=function(){var a=ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,4,0]);a=a.replace(/(..)/g,"%$1");a=decodeURIComponent(a);return a};this.getNotAfter=function(){var a=ASN1HEX.getDecendantHexVByNthList(this.hex,0,[0,4,1]);a=a.replace(/(..)/g,"%$1");a=decodeURIComponent(a);return a};this.readCertPEM=function(c){var e=X509.pemToHex(c);var b=X509.getPublicKeyHexArrayFromCertHex(e);var d=new RSAKey();d.setPublic(b[0],b[1]);this.subjectPublicKeyRSA=d;this.subjectPublicKeyRSA_hN=b[0];this.subjectPublicKeyRSA_hE=b[1];this.hex=e};this.readCertPEMWithoutRSAInit=function(c){var d=X509.pemToHex(c);var b=X509.getPublicKeyHexArrayFromCertHex(d);this.subjectPublicKeyRSA.setPublic(b[0],b[1]);this.subjectPublicKeyRSA_hN=b[0];this.subjectPublicKeyRSA_hE=b[1];this.hex=d};this.getInfo=function(){var p="Basic Fields\n";p+=" serial number: "+this.getSerialNumberHex()+"\n";p+=" signature algorithm: "+this.getSignatureAlgorithmField()+"\n";p+=" issuer: "+this.getIssuerString()+"\n";p+=" notBefore: "+this.getNotBefore()+"\n";p+=" notAfter: "+this.getNotAfter()+"\n";p+=" subject: "+this.getSubjectString()+"\n";p+=" subject public key info: \n";var j=X509.getSubjectPublicKeyInfoPosFromCertHex(this.hex);var d=ASN1HEX.getHexOfTLV_AtObj(this.hex,j);var n=KEYUTIL.getKey(d,null,"pkcs8pub");if(n instanceof RSAKey){p+=" key algorithm: RSA\n";p+=" n="+n.n.toString(16).substr(0,16)+"...\n";p+=" e="+n.e.toString(16)+"\n"}p+="X509v3 Extensions:\n";var m=X509.getV3ExtInfoListOfCertHex(this.hex);for(var e=0;e0){var c=":"+j.join(":")+":";if(c.indexOf(":"+h+":")==-1){throw"algorithm '"+h+"' not accepted in the list"}}if(h!="none"&&t===null){throw"key shall be specified to verify."}if(typeof t=="string"&&t.indexOf("-----BEGIN ")!=-1){t=KEYUTIL.getKey(t)}if(s=="RS"||s=="PS"){if(!(t instanceof RSAKey)){throw"key shall be a RSAKey obj for RS* and PS* algs"}}if(s=="ES"){if(!(t instanceof KJUR.crypto.ECDSA)){throw"key shall be a ECDSA obj for ES* algs"}}if(h=="none"){}var n=null;if(m.jwsalg2sigalg[i.alg]===undefined){throw"unsupported alg name: "+h}else{n=m.jwsalg2sigalg[h]}if(n=="none"){throw"not supported"}else{if(n.substr(0,4)=="Hmac"){var k=null;if(t===undefined){throw"hexadecimal key shall be specified for HMAC"}var g=new KJUR.crypto.Mac({alg:n,pass:t});g.updateString(b);k=g.doFinal();return r==k}else{if(n.indexOf("withECDSA")!=-1){var f=null;try{f=KJUR.crypto.ECDSA.concatSigToASN1Sig(r)}catch(o){return false}var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(f)}else{var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(r)}}}};KJUR.jws.JWS.parse=function(g){var c=g.split(".");var b={};var f,e,d;if(c.length!=2&&c.length!=3){throw"malformed sJWS: wrong number of '.' splitted elements"}f=c[0];e=c[1];if(c.length==3){d=c[2]}b.headerObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(f));b.payloadObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(e));b.headerPP=JSON.stringify(b.headerObj,null," ");if(b.payloadObj==null){b.payloadPP=b64utoutf8(e)}else{b.payloadPP=JSON.stringify(b.payloadObj,null," ")}if(d!==undefined){b.sigHex=b64utohex(d)}return b};KJUR.jws.JWS.verifyJWT=function(d,j,l){var h=KJUR.jws.JWS;var i=d.split(".");var c=i[0];var g=i[1];var m=c+"."+g;var k=b64utohex(i[2]);var f=h.readSafeJSONString(b64utoutf8(c));var e=h.readSafeJSONString(b64utoutf8(g));if(f.alg===undefined){return false}if(l.alg===undefined){throw"acceptField.alg shall be specified"}if(!h.inArray(f.alg,l.alg)){return false}if(e.iss!==undefined&&typeof l.iss==="object"){if(!h.inArray(e.iss,l.iss)){return false}}if(e.sub!==undefined&&typeof l.sub==="object"){if(!h.inArray(e.sub,l.sub)){return false}}if(e.aud!==undefined&&typeof l.aud==="object"){if(typeof e.aud=="string"){if(!h.inArray(e.aud,l.aud)){return false}}else{if(typeof e.aud=="object"){if(!h.includedArray(e.aud,l.aud)){return false}}}}var b=KJUR.jws.IntDate.getNow();if(l.verifyAt!==undefined&&typeof l.verifyAt==="number"){b=l.verifyAt}if(l.gracePeriod===undefined||typeof l.gracePeriod!=="number"){l.gracePeriod=0}if(e.exp!==undefined&&typeof e.exp=="number"){if(e.exp+l.gracePeriod0){var c=":"+j.join(":")+":";if(c.indexOf(":"+h+":")==-1){throw"algorithm '"+h+"' not accepted in the list"}}if(h!="none"&&t===null){throw"key shall be specified to verify."}if(typeof t=="string"&&t.indexOf("-----BEGIN ")!=-1){t=KEYUTIL.getKey(t)}if(s=="RS"||s=="PS"){if(!(t instanceof RSAKey)){throw"key shall be a RSAKey obj for RS* and PS* algs"}}if(s=="ES"){if(!(t instanceof KJUR.crypto.ECDSA)){throw"key shall be a ECDSA obj for ES* algs"}}if(h=="none"){}var n=null;if(m.jwsalg2sigalg[i.alg]===undefined){throw"unsupported alg name: "+h}else{n=m.jwsalg2sigalg[h]}if(n=="none"){throw"not supported"}else{if(n.substr(0,4)=="Hmac"){var k=null;if(t===undefined){throw"hexadecimal key shall be specified for HMAC"}var g=new KJUR.crypto.Mac({alg:n,pass:t});g.updateString(b);k=g.doFinal();return r==k}else{if(n.indexOf("withECDSA")!=-1){var f=null;try{f=KJUR.crypto.ECDSA.concatSigToASN1Sig(r)}catch(o){return false}var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(f)}else{var e=new KJUR.crypto.Signature({alg:n});e.init(t);e.updateString(b);return e.verify(r)}}}};KJUR.jws.JWS.parse=function(g){var c=g.split(".");var b={};var f,e,d;if(c.length!=2&&c.length!=3){throw"malformed sJWS: wrong number of '.' splitted elements"}f=c[0];e=c[1];if(c.length==3){d=c[2]}b.headerObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(f));b.payloadObj=KJUR.jws.JWS.readSafeJSONString(b64utoutf8(e));b.headerPP=JSON.stringify(b.headerObj,null," ");if(b.payloadObj==null){b.payloadPP=b64utoutf8(e)}else{b.payloadPP=JSON.stringify(b.payloadObj,null," ")}if(d!==undefined){b.sigHex=b64utohex(d)}return b};KJUR.jws.JWS.verifyJWT=function(d,j,l){var h=KJUR.jws.JWS;var i=d.split(".");var c=i[0];var g=i[1];var m=c+"."+g;var k=b64utohex(i[2]);var f=h.readSafeJSONString(b64utoutf8(c));var e=h.readSafeJSONString(b64utoutf8(g));if(f.alg===undefined){return false}if(l.alg===undefined){throw"acceptField.alg shall be specified"}if(!h.inArray(f.alg,l.alg)){return false}if(e.iss!==undefined&&typeof l.iss==="object"){if(!h.inArray(e.iss,l.iss)){return false}}if(e.sub!==undefined&&typeof l.sub==="object"){if(!h.inArray(e.sub,l.sub)){return false}}if(e.aud!==undefined&&typeof l.aud==="object"){if(typeof e.aud=="string"){if(!h.inArray(e.aud,l.aud)){return false}}else{if(typeof e.aud=="object"){if(!h.includedArray(e.aud,l.aud)){return false}}}}var b=KJUR.jws.IntDate.getNow();if(l.verifyAt!==undefined&&typeof l.verifyAt==="number"){b=l.verifyAt}if(l.gracePeriod===undefined||typeof l.gracePeriod!=="number"){l.gracePeriod=0}if(e.exp!==undefined&&typeof e.exp=="number"){if(e.exp+l.gracePeriodj){this.aHeader.pop()}if(this.aSignature.length>j){this.aSignature.pop()}throw"addSignature failed: "+g}};this.addSignatureByHeaderKey=function(f,c){var e=b64utoutf8(this.sPayload);var d=new KJUR.jws.JWS();var g=d.generateJWSByP1PrvKey(f,e,c);this.aHeader.push(d.parsedJWS.headB64U);this.aSignature.push(d.parsedJWS.sigvalB64U)};this.addSignatureByHeaderPayloadKey=function(f,e,c){var d=new KJUR.jws.JWS();var g=d.generateJWSByP1PrvKey(f,e,c);this.aHeader.push(d.parsedJWS.headB64U);this.sPayload=d.parsedJWS.payloadB64U;this.aSignature.push(d.parsedJWS.sigvalB64U)};this.verifyAll=function(f){if(this.aHeader.length!==f.length||this.aSignature.length!==f.length){return false}for(var e=0;e0){this.aHeader=e.headers}else{throw"malformed header"}if(typeof e.payload==="string"){this.sPayload=e.payload}else{throw"malformed signatures"}if(e.signatures.length>0){this.signatures=e.signatures}else{throw"malformed signatures"}}catch(c){throw"malformed JWS-JS JSON object: "+c}}};this.getJSON=function(){return{headers:this.aHeader,payload:this.sPayload,signatures:this.aSignature}};this.isEmpty=function(){if(this.aHeader.length==0){return 1}return 0}}; diff --git a/npm/package.json b/npm/package.json index 2061d4f1..88097a78 100755 --- a/npm/package.json +++ b/npm/package.json @@ -1,6 +1,6 @@ { "name": "jsrsasign", - "version": "6.1.1", + "version": "6.1.2", "description": "opensource free pure JavaScript cryptographic library supports RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp and CAdES and JSON Web Signature(JWS)/Token(JWT)/Key(JWK).", "main": "lib/jsrsasign.js", "scripts": { diff --git a/pkcs5pkey-1.0.js b/pkcs5pkey-1.0.js index 2cd5a33d..f76226ec 100644 --- a/pkcs5pkey-1.0.js +++ b/pkcs5pkey-1.0.js @@ -1,9 +1,9 @@ -/*! pkcs5pkey-1.0.6.js (c) 2013-2014 Kenji Urushima | kjur.github.com/jsrsasign/license +/*! pkcs5pkey-1.0.7.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license */ /* * pkcs5pkey.js - reading passcode protected PKCS#5 PEM formatted RSA private key * - * Copyright (c) 2013-2014 Kenji Urushima (kenji.urushima@gmail.com) + * Copyright (c) 2013-2016 Kenji Urushima (kenji.urushima@gmail.com) * * This software is licensed under the terms of the MIT License. * http://kjur.github.com/jsrsasign/license @@ -15,7 +15,7 @@ * @fileOverview * @name pkcs5pkey-1.0.js * @author Kenji Urushima kenji.urushima@gmail.com - * @version pkcs5pkey 1.0.6 (2014-Apr-16) + * @version pkcs5pkey 1.0.7 (2016-Oct-08) * @since jsrsasign 2.0.0 * @license MIT License */