diff --git a/ChangeLog.txt b/ChangeLog.txt index ad67111d..b9a6e36f 100755 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,7 +1,13 @@ ChangeLog for jsrsasign -* Changes from 10.4.0 to next release +GeneralizedTime, UTCTime refactoring and some bug fix +* Changes from 10.4.0 to 10.4.1 release + - src/asn1.js + - refactoring of DERUTCTime, DERGeneralizedTime and DERAbstractTime + - now DERUTCTime support fraction of second + - src/asn1x509.js + - update Time class to follow DER{UTC,Generalized}Time update - tool/tool_tsreq.html - messageImprint bug fix (#504) - tool/tool_tsres.html @@ -9,6 +15,9 @@ ChangeLog for jsrsasign - jsrsasign-*-min.js - header URL fix to kjur.github.io (#503) (will be fixed in next release while rebuild) + - test/qunit-do-{asn1,asn1x509}.html updated + - KJUR.asn1.DER{UTC,Generalized}Time, KJUR.asn1.x509.Time class + test case added and updated. Full support for parsing OCSP response * Changes from 10.3.2 to 10.4.0 (2021-08-17) diff --git a/README.md b/README.md index 4a872fb6..101a73e7 100755 --- a/README.md +++ b/README.md @@ -29,13 +29,14 @@ HIGHLIGHTS ---------- - Swiss Army Knife style all in one package crypto and PKI library - available on [Node.js](https://www.npmjs.com/package/jsrsasign) and browsers +- Long live open source software from 2010 - very easy API to use - powerful various format key loader and ASN.1 API - rich document and samples - no dependency to other library - no dependency to [W3C Web Cryptography API](https://www.w3.org/TR/WebCryptoAPI/) nor [OpenSSL](https://www.openssl.org/) - no dependency on newer ECMAScirpt function. So old browsers also supported. -- very popular crypto library with [0.6M+ npm downloads/month](https://npm-stat.com/charts.html?package=jsrsasign&from=2016-05-01&to=2021-08-10) +- very popular crypto library with [0.6M+ npm downloads/month](https://npm-stat.com/charts.html?package=jsrsasign&from=2016-05-01&to=2021-09-28) INSTALL ------- diff --git a/api/files.html b/api/files.html index f2ec4fd9..09edad4b 100644 --- a/api/files.html +++ b/api/files.html @@ -523,7 +523,7 @@
o = new KJUR.asn1.DERUTCTime(); +o.setByDate(new Date("2016/12/31 23:59:59.12")); +// 2015-Jan-31 23:59:59.12 +o.setByDate(new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)));+ + + + +
d1 = new KJUR.asn1.DERGeneralizedTime(); +d1.setByParam("20210930235959.123Z"); +d1.setByParam({str: "20210930235959.123Z"}); + +d1.setByParam(new Date("2013/12/31 23:59:59.12")); +date1 = new Date(Date.UTC(2021,8,31,23,59,59,123)); +d1.setByParam(date1); +d1.setByParam({date: date1}); +d1.setByParam({date: date1, millis: true});+ + + + +
Method Attributes | -Method Name and Description | -
---|---|
- |
- setByDate(dateObject)
-
- set value by a Date object
- |
-
new DERGeneralizedTime("20151231235959Z") +new DERGeneralizedTime("20151231235959.123Z") +new DERGeneralizedTime(new Date()) +new DERGeneralizedTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) +new DERGeneralizedTime({str: "20151231235959.123Z"}) +new DERGeneralizedTime({date: new Date()}) +new DERGeneralizedTime({date: new Date(), millis: true}) +new DERGeneralizedTime({millis: true})+ + +
When you specify UTC time, use 'Date.UTC' method like this:- - - - -
-o1 = new DERUTCTime(); -o1.setByDate(date); - -date = new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)); #2015JAN31 23:59:59
Method Attributes | -Method Name and Description | -
---|---|
- |
- setByDate(dateObject)
-
- set value by a Date object
- |
-
d1 = new KJUR.asn1.DERUTCTime(); -d1.setString('130430125959Z'); - -d2 = new KJUR.asn1.DERUTCTime({'str': '130430125959Z'}); -d3 = new KJUR.asn1.DERUTCTime({'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))}); -d4 = new KJUR.asn1.DERUTCTime('130430125959Z');+
new DERUTCTime("20151231235959Z") +new DERUTCTime("20151231235959.123Z") +new DERUTCTime(new Date()) +new DERUTCTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) +new DERUTCTime({str: "20151231235959.123Z"}) +new DERUTCTime({date: new Date()}) +new DERUTCTime({date: new Date(), millis: true}) +new DERUTCTime({millis: true})@@ -668,6 +650,15 @@
o = new KJUR.asn1.DERUTCTime(); -o.setByDate(new Date("2016/12/31"));- - - - -
1 /* asn1-1.0.21.js (c) 2013-2020 Kenji Urushima | kjur.github.io/jsrsasign/license +1 /* asn1-1.0.22.js (c) 2013-2021 Kenji Urushima | kjur.github.io/jsrsasign/license 2 */ 3 /* 4 * asn1.js - ASN.1 DER encoder classes @@ -23,7 +23,7 @@ 16 * @fileOverview 17 * @name asn1-1.0.js 18 * @author Kenji Urushima kenji.urushima@gmail.com - 19 * @version jsrsasign 10.1.0 asn1 1.0.21 (2020-Nov-18) + 19 * @version jsrsasign 10.4.1 asn1 1.0.22 (2021-Sep-30) 20 * @since jsrsasign 2.1 21 * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a> 22 */ @@ -639,1132 +639,1192 @@ 632 * @extends KJUR.asn1.ASN1Object 633 * @description 634 * @see KJUR.asn1.ASN1Object - superclass -635 */ -636 KJUR.asn1.DERAbstractTime = function(params) { -637 KJUR.asn1.DERAbstractTime.superclass.constructor.call(this); -638 var s = null; -639 var date = null; -640 -641 // --- PRIVATE METHODS -------------------- -642 this.localDateToUTC = function(d) { -643 var utc = d.getTime() + (d.getTimezoneOffset() * 60000); -644 var utcDate = new Date(utc); -645 return utcDate; -646 }; -647 -648 /* -649 * format date string by Data object -650 * @name formatDate -651 * @memberOf KJUR.asn1.AbstractTime; -652 * @param {Date} dateObject -653 * @param {string} type 'utc' or 'gen' -654 * @param {boolean} withMillis flag for with millisections or not -655 * @description -656 * 'withMillis' flag is supported from asn1 1.0.6. -657 */ -658 this.formatDate = function(dateObject, type, withMillis) { -659 var pad = this.zeroPadding; -660 var d = this.localDateToUTC(dateObject); -661 var year = String(d.getFullYear()); -662 if (type == 'utc') year = year.substr(2, 2); -663 var month = pad(String(d.getMonth() + 1), 2); -664 var day = pad(String(d.getDate()), 2); -665 var hour = pad(String(d.getHours()), 2); -666 var min = pad(String(d.getMinutes()), 2); -667 var sec = pad(String(d.getSeconds()), 2); -668 var s = year + month + day + hour + min + sec; -669 if (withMillis === true) { -670 var millis = d.getMilliseconds(); -671 if (millis != 0) { -672 var sMillis = pad(String(millis), 3); -673 sMillis = sMillis.replace(/[0]+$/, ""); -674 s = s + "." + sMillis; -675 } -676 } -677 return s + "Z"; -678 }; -679 -680 this.zeroPadding = function(s, len) { -681 if (s.length >= len) return s; -682 return new Array(len - s.length + 1).join('0') + s; -683 }; -684 -685 // --- PUBLIC METHODS -------------------- -686 /** -687 * get string value of this string object -688 * @name getString -689 * @memberOf KJUR.asn1.DERAbstractTime# -690 * @function -691 * @return {String} string value of this time object -692 */ -693 this.getString = function() { -694 return this.s; -695 }; -696 -697 /** -698 * set value by a string -699 * @name setString -700 * @memberOf KJUR.asn1.DERAbstractTime# -701 * @function -702 * @param {String} newS value by a string to set such like "130430235959Z" -703 */ -704 this.setString = function(newS) { -705 this.hTLV = null; -706 this.isModified = true; -707 this.s = newS; -708 this.hV = stohex(newS); -709 }; -710 -711 /** -712 * set value by a Date object -713 * @name setByDateValue -714 * @memberOf KJUR.asn1.DERAbstractTime# -715 * @function -716 * @param {Integer} year year of date (ex. 2013) -717 * @param {Integer} month month of date between 1 and 12 (ex. 12) -718 * @param {Integer} day day of month -719 * @param {Integer} hour hours of date -720 * @param {Integer} min minutes of date -721 * @param {Integer} sec seconds of date -722 */ -723 this.setByDateValue = function(year, month, day, hour, min, sec) { -724 var dateObject = new Date(Date.UTC(year, month - 1, day, hour, min, sec, 0)); -725 this.setByDate(dateObject); -726 }; -727 -728 this.getFreshValueHex = function() { -729 return this.hV; -730 }; -731 }; -732 extendClass(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object); -733 // == END DERAbstractTime ================================================== -734 -735 // == BEGIN DERAbstractStructured ============================================ -736 /** -737 * base class for ASN.1 DER structured class -738 * @name KJUR.asn1.DERAbstractStructured -739 * @class base class for ASN.1 DER structured class -740 * @property {Array} asn1Array internal array of ASN1Object -741 * @extends KJUR.asn1.ASN1Object -742 * @description -743 * @see KJUR.asn1.ASN1Object - superclass -744 */ -745 KJUR.asn1.DERAbstractStructured = function(params) { -746 KJUR.asn1.DERAbstractString.superclass.constructor.call(this); -747 var asn1Array = null; -748 -749 /** -750 * set value by array of ASN1Object -751 * @name setByASN1ObjectArray -752 * @memberOf KJUR.asn1.DERAbstractStructured# -753 * @function -754 * @param {array} asn1ObjectArray array of ASN1Object to set -755 */ -756 this.setByASN1ObjectArray = function(asn1ObjectArray) { -757 this.hTLV = null; -758 this.isModified = true; -759 this.asn1Array = asn1ObjectArray; -760 }; -761 -762 /** -763 * append an ASN1Object to internal array -764 * @name appendASN1Object -765 * @memberOf KJUR.asn1.DERAbstractStructured# -766 * @function -767 * @param {ASN1Object} asn1Object to add -768 */ -769 this.appendASN1Object = function(asn1Object) { -770 this.hTLV = null; -771 this.isModified = true; -772 this.asn1Array.push(asn1Object); -773 }; -774 -775 this.asn1Array = new Array(); -776 if (typeof params != "undefined") { -777 if (typeof params['array'] != "undefined") { -778 this.asn1Array = params['array']; -779 } -780 } -781 }; -782 extendClass(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object); -783 +635 * @see KJUR.asn1.DERGeneralizedTime +636 * @see KJUR.asn1.DERUTCTime +637 * @see KJUR.asn1.x509.Time +638 */ +639 KJUR.asn1.DERAbstractTime = function(params) { +640 KJUR.asn1.DERAbstractTime.superclass.constructor.call(this); +641 var s = null; +642 var date = null; +643 +644 // --- PRIVATE METHODS -------------------- +645 this.localDateToUTC = function(d) { +646 var utc = d.getTime() + (d.getTimezoneOffset() * 60000); +647 var utcDate = new Date(utc); +648 return utcDate; +649 }; +650 +651 /* +652 * format date string by Data object +653 * @name formatDate +654 * @memberOf KJUR.asn1.AbstractTime; +655 * @param {Date} dateObject +656 * @param {string} type 'utc' or 'gen' +657 * @param {boolean} withMillis flag for with millisections or not +658 * @description +659 * 'withMillis' flag is supported from asn1 1.0.6. +660 */ +661 this.formatDate = function(dateObject, type, withMillis) { +662 var pad = this.zeroPadding; +663 var d = this.localDateToUTC(dateObject); +664 var year = String(d.getFullYear()); +665 if (type == 'utc') year = year.substr(2, 2); +666 var month = pad(String(d.getMonth() + 1), 2); +667 var day = pad(String(d.getDate()), 2); +668 var hour = pad(String(d.getHours()), 2); +669 var min = pad(String(d.getMinutes()), 2); +670 var sec = pad(String(d.getSeconds()), 2); +671 var s = year + month + day + hour + min + sec; +672 if (withMillis === true) { +673 var millis = d.getMilliseconds(); +674 if (millis != 0) { +675 var sMillis = pad(String(millis), 3); +676 sMillis = sMillis.replace(/[0]+$/, ""); +677 s = s + "." + sMillis; +678 } +679 } +680 return s + "Z"; +681 }; +682 +683 this.zeroPadding = function(s, len) { +684 if (s.length >= len) return s; +685 return new Array(len - s.length + 1).join('0') + s; +686 }; +687 +688 // --- PUBLIC METHODS -------------------- +689 +690 /** +691 * set parameter of time +692 * @name setByParam +693 * @memberOf KJUR.asn1.DERAbstractTime# +694 * @function +695 * @param {Object} params JSON object, Date object or string of time +696 * @since jsrsasign 10.4.1 asn1 1.0.22 +697 * +698 * NOTE: If a member "millis" has a value "true", +699 * a fraction of second will be specified for this object. +700 * This default is "false". +701 * +702 * @example +703 * d1 = new KJUR.asn1.DERGeneralizedTime(); +704 * d1.setByParam("20210930235959.123Z"); +705 * d1.setByParam({str: "20210930235959.123Z"}); +706 * +707 * d1.setByParam(new Date("2013/12/31 23:59:59.12")); +708 * date1 = new Date(Date.UTC(2021,8,31,23,59,59,123)); +709 * d1.setByParam(date1); +710 * d1.setByParam({date: date1}); +711 * d1.setByParam({date: date1, millis: true}); +712 */ +713 this.setByParam = function(params) { +714 this.hV = null; +715 this.hTLV = null; +716 this.params = params; +717 }; +718 +719 /** +720 * get string value of this string object (DEPRECATED) +721 * @name getString +722 * @memberOf KJUR.asn1.DERAbstractTime# +723 * @function +724 * @return {String} string value of this time object +725 * @deprecated from jsrsasign 10.4.1 asn1 1.0.22. +726 */ +727 this.getString = function() { +728 return undefined; +729 }; +730 +731 /** +732 * set value by a string (DEPRECATED) +733 * @name setString +734 * @memberOf KJUR.asn1.DERAbstractTime# +735 * @function +736 * @param {String} newS value by a string to set such like "130430235959Z" +737 * @deprecated from jsrsasign 10.4.1 asn1 1.0.22. +738 */ +739 this.setString = function(newS) { +740 this.hTLV = null; +741 this.isModified = true; +742 if (this.params == undefined) this.params = {}; +743 this.params.str = newS; +744 }; +745 +746 /** +747 * set value by a Date object<br/> +748 * @name setByDate +749 * @memberOf KJUR.asn1.DERAbstractTime# +750 * @function +751 * @param {Date} dateObject Date object to set ASN.1 value(V) +752 * @since jsrsasign 10.4.1 asn1 1.0.22 +753 * +754 * @example +755 * o = new KJUR.asn1.DERUTCTime(); +756 * o.setByDate(new Date("2016/12/31 23:59:59.12")); +757 * // 2015-Jan-31 23:59:59.12 +758 * o.setByDate(new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0))); +759 */ +760 this.setByDate = function(dateObject) { +761 this.hTLV = null; +762 this.isModified = true; +763 if (this.params == undefined) this.params = {}; +764 this.params.date = dateObject; +765 }; +766 +767 /** +768 * set value by a Date object +769 * @name setByDateValue +770 * @memberOf KJUR.asn1.DERAbstractTime# +771 * @function +772 * @param {Integer} year year of date (ex. 2013) +773 * @param {Integer} month month of date between 1 and 12 (ex. 12) +774 * @param {Integer} day day of month +775 * @param {Integer} hour hours of date +776 * @param {Integer} min minutes of date +777 * @param {Integer} sec seconds of date +778 */ +779 this.setByDateValue = function(year, month, day, hour, min, sec) { +780 var dateObject = new Date(Date.UTC(year, month - 1, day, +781 hour, min, sec, 0)); +782 this.setByDate(dateObject); +783 }; 784 -785 // ******************************************************************** -786 // ASN.1 Object Classes -787 // ******************************************************************** -788 -789 // ******************************************************************** -790 /** -791 * class for ASN.1 DER Boolean -792 * @name KJUR.asn1.DERBoolean -793 * @class class for ASN.1 DER Boolean -794 * @extends KJUR.asn1.ASN1Object -795 * @see KJUR.asn1.ASN1Object - superclass -796 * @description -797 * In ASN.1 DER, DER Boolean "false" shall be omitted. -798 * However this supports boolean false for future BER support. -799 * @example -800 * new KJUR.asn1.DERBoolean(true) -801 * new KJUR.asn1.DERBoolean(false) -802 */ -803 KJUR.asn1.DERBoolean = function(params) { -804 KJUR.asn1.DERBoolean.superclass.constructor.call(this); -805 this.hT = "01"; -806 if (params == false) -807 this.hTLV = "010100"; -808 else -809 this.hTLV = "0101ff"; -810 }; -811 extendClass(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object); -812 -813 // ******************************************************************** -814 /** -815 * class for ASN.1 DER Integer -816 * @name KJUR.asn1.DERInteger -817 * @class class for ASN.1 DER Integer -818 * @extends KJUR.asn1.ASN1Object -819 * @description -820 * <br/> -821 * As for argument 'params' for constructor, you can specify one of -822 * following properties: -823 * <ul> -824 * <li>int - specify initial ASN.1 value(V) by integer value</li> -825 * <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li> -826 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -827 * </ul> -828 * NOTE: 'params' can be omitted. -829 */ -830 KJUR.asn1.DERInteger = function(params) { -831 KJUR.asn1.DERInteger.superclass.constructor.call(this); -832 this.hT = "02"; -833 -834 /** -835 * set value by Tom Wu's BigInteger object -836 * @name setByBigInteger -837 * @memberOf KJUR.asn1.DERInteger# -838 * @function -839 * @param {BigInteger} bigIntegerValue to set -840 */ -841 this.setByBigInteger = function(bigIntegerValue) { -842 this.hTLV = null; -843 this.isModified = true; -844 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); -845 }; -846 -847 /** -848 * set value by integer value -849 * @name setByInteger -850 * @memberOf KJUR.asn1.DERInteger -851 * @function -852 * @param {Integer} integer value to set -853 */ -854 this.setByInteger = function(intValue) { -855 var bi = new BigInteger(String(intValue), 10); -856 this.setByBigInteger(bi); -857 }; -858 -859 /** -860 * set value by integer value -861 * @name setValueHex -862 * @memberOf KJUR.asn1.DERInteger# -863 * @function -864 * @param {String} hexadecimal string of integer value -865 * @description -866 * <br/> -867 * NOTE: Value shall be represented by minimum octet length of -868 * two's complement representation. -869 * @example -870 * new KJUR.asn1.DERInteger(123); -871 * new KJUR.asn1.DERInteger({'int': 123}); -872 * new KJUR.asn1.DERInteger({'hex': '1fad'}); -873 */ -874 this.setValueHex = function(newHexString) { -875 this.hV = newHexString; -876 }; -877 -878 this.getFreshValueHex = function() { -879 return this.hV; -880 }; -881 -882 if (typeof params != "undefined") { -883 if (typeof params['bigint'] != "undefined") { -884 this.setByBigInteger(params['bigint']); -885 } else if (typeof params['int'] != "undefined") { -886 this.setByInteger(params['int']); -887 } else if (typeof params == "number") { -888 this.setByInteger(params); -889 } else if (typeof params['hex'] != "undefined") { -890 this.setValueHex(params['hex']); -891 } -892 } -893 }; -894 extendClass(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object); -895 -896 // ******************************************************************** -897 /** -898 * class for ASN.1 DER encoded BitString primitive -899 * @name KJUR.asn1.DERBitString -900 * @class class for ASN.1 DER encoded BitString primitive -901 * @extends KJUR.asn1.ASN1Object -902 * @description -903 * <br/> -904 * As for argument 'params' for constructor, you can specify one of -905 * following properties: -906 * <ul> -907 * <li>bin - specify binary string (ex. '10111')</li> -908 * <li>array - specify array of boolean (ex. [true,false,true,true])</li> -909 * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li> -910 * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} -911 * argument for "BitString encapsulates" structure.</li> -912 * </ul> -913 * NOTE1: 'params' can be omitted.<br/> -914 * NOTE2: 'obj' parameter have been supported since -915 * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/> -916 * @example -917 * // default constructor -918 * o = new KJUR.asn1.DERBitString(); -919 * // initialize with binary string -920 * o = new KJUR.asn1.DERBitString({bin: "1011"}); -921 * // initialize with boolean array -922 * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]}); -923 * // initialize with hexadecimal string (04 is unused bits) -924 * o = new KJUR.asn1.DEROctetString({hex: "04bac0"}); -925 * // initialize with ASN1Util.newObject argument for encapsulated -926 * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); -927 * // above generates a ASN.1 data like this: -928 * // BIT STRING, encapsulates { -929 * // SEQUENCE { -930 * // INTEGER 3 -931 * // PrintableString 'aaa' -932 * // } -933 * // } -934 */ -935 KJUR.asn1.DERBitString = function(params) { -936 if (params !== undefined && typeof params.obj !== "undefined") { -937 var o = KJUR.asn1.ASN1Util.newObject(params.obj); -938 params.hex = "00" + o.getEncodedHex(); -939 } -940 KJUR.asn1.DERBitString.superclass.constructor.call(this); -941 this.hT = "03"; -942 -943 /** -944 * set ASN.1 value(V) by a hexadecimal string including unused bits -945 * @name setHexValueIncludingUnusedBits -946 * @memberOf KJUR.asn1.DERBitString# -947 * @function -948 * @param {String} newHexStringIncludingUnusedBits -949 */ -950 this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) { -951 this.hTLV = null; -952 this.isModified = true; -953 this.hV = newHexStringIncludingUnusedBits; -954 }; -955 -956 /** -957 * set ASN.1 value(V) by unused bit and hexadecimal string of value -958 * @name setUnusedBitsAndHexValue -959 * @memberOf KJUR.asn1.DERBitString# -960 * @function -961 * @param {Integer} unusedBits -962 * @param {String} hValue -963 */ -964 this.setUnusedBitsAndHexValue = function(unusedBits, hValue) { -965 if (unusedBits < 0 || 7 < unusedBits) { -966 throw "unused bits shall be from 0 to 7: u = " + unusedBits; -967 } -968 var hUnusedBits = "0" + unusedBits; -969 this.hTLV = null; -970 this.isModified = true; -971 this.hV = hUnusedBits + hValue; -972 }; -973 -974 /** -975 * set ASN.1 DER BitString by binary string<br/> -976 * @name setByBinaryString -977 * @memberOf KJUR.asn1.DERBitString# -978 * @function -979 * @param {String} binaryString binary value string (i.e. '10111') -980 * @description -981 * Its unused bits will be calculated automatically by length of -982 * 'binaryValue'. <br/> -983 * NOTE: Trailing zeros '0' will be ignored. -984 * @example -985 * o = new KJUR.asn1.DERBitString(); -986 * o.setByBooleanArray("01011"); -987 */ -988 this.setByBinaryString = function(binaryString) { -989 binaryString = binaryString.replace(/0+$/, ''); -990 var unusedBits = 8 - binaryString.length % 8; -991 if (unusedBits == 8) unusedBits = 0; -992 for (var i = 0; i <= unusedBits; i++) { -993 binaryString += '0'; -994 } -995 var h = ''; -996 for (var i = 0; i < binaryString.length - 1; i += 8) { -997 var b = binaryString.substr(i, 8); -998 var x = parseInt(b, 2).toString(16); -999 if (x.length == 1) x = '0' + x; -1000 h += x; -1001 } -1002 this.hTLV = null; -1003 this.isModified = true; -1004 this.hV = '0' + unusedBits + h; -1005 }; -1006 -1007 /** -1008 * set ASN.1 TLV value(V) by an array of boolean<br/> -1009 * @name setByBooleanArray -1010 * @memberOf KJUR.asn1.DERBitString# -1011 * @function -1012 * @param {array} booleanArray array of boolean (ex. [true, false, true]) -1013 * @description -1014 * NOTE: Trailing falses will be ignored in the ASN.1 DER Object. -1015 * @example -1016 * o = new KJUR.asn1.DERBitString(); -1017 * o.setByBooleanArray([false, true, false, true, true]); -1018 */ -1019 this.setByBooleanArray = function(booleanArray) { -1020 var s = ''; -1021 for (var i = 0; i < booleanArray.length; i++) { -1022 if (booleanArray[i] == true) { -1023 s += '1'; -1024 } else { -1025 s += '0'; -1026 } -1027 } -1028 this.setByBinaryString(s); +785 this.getFreshValueHex = function() { +786 return this.hV; +787 }; +788 }; +789 extendClass(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object); +790 // == END DERAbstractTime ================================================== +791 +792 // == BEGIN DERAbstractStructured ============================================ +793 /** +794 * base class for ASN.1 DER structured class +795 * @name KJUR.asn1.DERAbstractStructured +796 * @class base class for ASN.1 DER structured class +797 * @property {Array} asn1Array internal array of ASN1Object +798 * @extends KJUR.asn1.ASN1Object +799 * @description +800 * @see KJUR.asn1.ASN1Object - superclass +801 */ +802 KJUR.asn1.DERAbstractStructured = function(params) { +803 KJUR.asn1.DERAbstractString.superclass.constructor.call(this); +804 var asn1Array = null; +805 +806 /** +807 * set value by array of ASN1Object +808 * @name setByASN1ObjectArray +809 * @memberOf KJUR.asn1.DERAbstractStructured# +810 * @function +811 * @param {array} asn1ObjectArray array of ASN1Object to set +812 */ +813 this.setByASN1ObjectArray = function(asn1ObjectArray) { +814 this.hTLV = null; +815 this.isModified = true; +816 this.asn1Array = asn1ObjectArray; +817 }; +818 +819 /** +820 * append an ASN1Object to internal array +821 * @name appendASN1Object +822 * @memberOf KJUR.asn1.DERAbstractStructured# +823 * @function +824 * @param {ASN1Object} asn1Object to add +825 */ +826 this.appendASN1Object = function(asn1Object) { +827 this.hTLV = null; +828 this.isModified = true; +829 this.asn1Array.push(asn1Object); +830 }; +831 +832 this.asn1Array = new Array(); +833 if (typeof params != "undefined") { +834 if (typeof params['array'] != "undefined") { +835 this.asn1Array = params['array']; +836 } +837 } +838 }; +839 extendClass(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object); +840 +841 +842 // ******************************************************************** +843 // ASN.1 Object Classes +844 // ******************************************************************** +845 +846 // ******************************************************************** +847 /** +848 * class for ASN.1 DER Boolean +849 * @name KJUR.asn1.DERBoolean +850 * @class class for ASN.1 DER Boolean +851 * @extends KJUR.asn1.ASN1Object +852 * @see KJUR.asn1.ASN1Object - superclass +853 * @description +854 * In ASN.1 DER, DER Boolean "false" shall be omitted. +855 * However this supports boolean false for future BER support. +856 * @example +857 * new KJUR.asn1.DERBoolean(true) +858 * new KJUR.asn1.DERBoolean(false) +859 */ +860 KJUR.asn1.DERBoolean = function(params) { +861 KJUR.asn1.DERBoolean.superclass.constructor.call(this); +862 this.hT = "01"; +863 if (params == false) +864 this.hTLV = "010100"; +865 else +866 this.hTLV = "0101ff"; +867 }; +868 extendClass(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object); +869 +870 // ******************************************************************** +871 /** +872 * class for ASN.1 DER Integer +873 * @name KJUR.asn1.DERInteger +874 * @class class for ASN.1 DER Integer +875 * @extends KJUR.asn1.ASN1Object +876 * @description +877 * <br/> +878 * As for argument 'params' for constructor, you can specify one of +879 * following properties: +880 * <ul> +881 * <li>int - specify initial ASN.1 value(V) by integer value</li> +882 * <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li> +883 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +884 * </ul> +885 * NOTE: 'params' can be omitted. +886 */ +887 KJUR.asn1.DERInteger = function(params) { +888 KJUR.asn1.DERInteger.superclass.constructor.call(this); +889 this.hT = "02"; +890 +891 /** +892 * set value by Tom Wu's BigInteger object +893 * @name setByBigInteger +894 * @memberOf KJUR.asn1.DERInteger# +895 * @function +896 * @param {BigInteger} bigIntegerValue to set +897 */ +898 this.setByBigInteger = function(bigIntegerValue) { +899 this.hTLV = null; +900 this.isModified = true; +901 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); +902 }; +903 +904 /** +905 * set value by integer value +906 * @name setByInteger +907 * @memberOf KJUR.asn1.DERInteger +908 * @function +909 * @param {Integer} integer value to set +910 */ +911 this.setByInteger = function(intValue) { +912 var bi = new BigInteger(String(intValue), 10); +913 this.setByBigInteger(bi); +914 }; +915 +916 /** +917 * set value by integer value +918 * @name setValueHex +919 * @memberOf KJUR.asn1.DERInteger# +920 * @function +921 * @param {String} hexadecimal string of integer value +922 * @description +923 * <br/> +924 * NOTE: Value shall be represented by minimum octet length of +925 * two's complement representation. +926 * @example +927 * new KJUR.asn1.DERInteger(123); +928 * new KJUR.asn1.DERInteger({'int': 123}); +929 * new KJUR.asn1.DERInteger({'hex': '1fad'}); +930 */ +931 this.setValueHex = function(newHexString) { +932 this.hV = newHexString; +933 }; +934 +935 this.getFreshValueHex = function() { +936 return this.hV; +937 }; +938 +939 if (typeof params != "undefined") { +940 if (typeof params['bigint'] != "undefined") { +941 this.setByBigInteger(params['bigint']); +942 } else if (typeof params['int'] != "undefined") { +943 this.setByInteger(params['int']); +944 } else if (typeof params == "number") { +945 this.setByInteger(params); +946 } else if (typeof params['hex'] != "undefined") { +947 this.setValueHex(params['hex']); +948 } +949 } +950 }; +951 extendClass(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object); +952 +953 // ******************************************************************** +954 /** +955 * class for ASN.1 DER encoded BitString primitive +956 * @name KJUR.asn1.DERBitString +957 * @class class for ASN.1 DER encoded BitString primitive +958 * @extends KJUR.asn1.ASN1Object +959 * @description +960 * <br/> +961 * As for argument 'params' for constructor, you can specify one of +962 * following properties: +963 * <ul> +964 * <li>bin - specify binary string (ex. '10111')</li> +965 * <li>array - specify array of boolean (ex. [true,false,true,true])</li> +966 * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li> +967 * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} +968 * argument for "BitString encapsulates" structure.</li> +969 * </ul> +970 * NOTE1: 'params' can be omitted.<br/> +971 * NOTE2: 'obj' parameter have been supported since +972 * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/> +973 * @example +974 * // default constructor +975 * o = new KJUR.asn1.DERBitString(); +976 * // initialize with binary string +977 * o = new KJUR.asn1.DERBitString({bin: "1011"}); +978 * // initialize with boolean array +979 * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]}); +980 * // initialize with hexadecimal string (04 is unused bits) +981 * o = new KJUR.asn1.DEROctetString({hex: "04bac0"}); +982 * // initialize with ASN1Util.newObject argument for encapsulated +983 * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +984 * // above generates a ASN.1 data like this: +985 * // BIT STRING, encapsulates { +986 * // SEQUENCE { +987 * // INTEGER 3 +988 * // PrintableString 'aaa' +989 * // } +990 * // } +991 */ +992 KJUR.asn1.DERBitString = function(params) { +993 if (params !== undefined && typeof params.obj !== "undefined") { +994 var o = KJUR.asn1.ASN1Util.newObject(params.obj); +995 params.hex = "00" + o.getEncodedHex(); +996 } +997 KJUR.asn1.DERBitString.superclass.constructor.call(this); +998 this.hT = "03"; +999 +1000 /** +1001 * set ASN.1 value(V) by a hexadecimal string including unused bits +1002 * @name setHexValueIncludingUnusedBits +1003 * @memberOf KJUR.asn1.DERBitString# +1004 * @function +1005 * @param {String} newHexStringIncludingUnusedBits +1006 */ +1007 this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) { +1008 this.hTLV = null; +1009 this.isModified = true; +1010 this.hV = newHexStringIncludingUnusedBits; +1011 }; +1012 +1013 /** +1014 * set ASN.1 value(V) by unused bit and hexadecimal string of value +1015 * @name setUnusedBitsAndHexValue +1016 * @memberOf KJUR.asn1.DERBitString# +1017 * @function +1018 * @param {Integer} unusedBits +1019 * @param {String} hValue +1020 */ +1021 this.setUnusedBitsAndHexValue = function(unusedBits, hValue) { +1022 if (unusedBits < 0 || 7 < unusedBits) { +1023 throw "unused bits shall be from 0 to 7: u = " + unusedBits; +1024 } +1025 var hUnusedBits = "0" + unusedBits; +1026 this.hTLV = null; +1027 this.isModified = true; +1028 this.hV = hUnusedBits + hValue; 1029 }; 1030 1031 /** -1032 * generate an array of falses with specified length<br/> -1033 * @name newFalseArray -1034 * @memberOf KJUR.asn1.DERBitString +1032 * set ASN.1 DER BitString by binary string<br/> +1033 * @name setByBinaryString +1034 * @memberOf KJUR.asn1.DERBitString# 1035 * @function -1036 * @param {Integer} nLength length of array to generate -1037 * @return {array} array of boolean falses -1038 * @description -1039 * This static method may be useful to initialize boolean array. -1040 * @example -1041 * o = new KJUR.asn1.DERBitString(); -1042 * o.newFalseArray(3) → [false, false, false] -1043 */ -1044 this.newFalseArray = function(nLength) { -1045 var a = new Array(nLength); -1046 for (var i = 0; i < nLength; i++) { -1047 a[i] = false; -1048 } -1049 return a; -1050 }; -1051 -1052 this.getFreshValueHex = function() { -1053 return this.hV; -1054 }; -1055 -1056 if (typeof params != "undefined") { -1057 if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) { -1058 this.setHexValueIncludingUnusedBits(params); -1059 } else if (typeof params['hex'] != "undefined") { -1060 this.setHexValueIncludingUnusedBits(params['hex']); -1061 } else if (typeof params['bin'] != "undefined") { -1062 this.setByBinaryString(params['bin']); -1063 } else if (typeof params['array'] != "undefined") { -1064 this.setByBooleanArray(params['array']); -1065 } -1066 } -1067 }; -1068 extendClass(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object); -1069 -1070 // ******************************************************************** -1071 /** -1072 * class for ASN.1 DER OctetString<br/> -1073 * @name KJUR.asn1.DEROctetString -1074 * @class class for ASN.1 DER OctetString -1075 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1076 * @extends KJUR.asn1.DERAbstractString -1077 * @description -1078 * This class provides ASN.1 OctetString simple type.<br/> -1079 * Supported "params" attributes are: -1080 * <ul> -1081 * <li>str - to set a string as a value</li> -1082 * <li>hex - to set a hexadecimal string as a value</li> -1083 * <li>obj - to set a encapsulated ASN.1 value by JSON object -1084 * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li> -1085 * </ul> -1086 * NOTE: A parameter 'obj' have been supported -1087 * for "OCTET STRING, encapsulates" structure. -1088 * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25). -1089 * @see KJUR.asn1.DERAbstractString - superclass -1090 * @example -1091 * // default constructor -1092 * o = new KJUR.asn1.DEROctetString(); -1093 * // initialize with string -1094 * o = new KJUR.asn1.DEROctetString({str: "aaa"}); -1095 * // initialize with hexadecimal string -1096 * o = new KJUR.asn1.DEROctetString({hex: "616161"}); -1097 * // initialize with ASN1Util.newObject argument -1098 * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); -1099 * // above generates a ASN.1 data like this: -1100 * // OCTET STRING, encapsulates { -1101 * // SEQUENCE { -1102 * // INTEGER 3 -1103 * // PrintableString 'aaa' -1104 * // } -1105 * // } -1106 */ -1107 KJUR.asn1.DEROctetString = function(params) { -1108 if (params !== undefined && typeof params.obj !== "undefined") { -1109 var o = KJUR.asn1.ASN1Util.newObject(params.obj); -1110 params.hex = o.getEncodedHex(); -1111 } -1112 KJUR.asn1.DEROctetString.superclass.constructor.call(this, params); -1113 this.hT = "04"; -1114 }; -1115 extendClass(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString); -1116 -1117 // ******************************************************************** -1118 /** -1119 * class for ASN.1 DER Null -1120 * @name KJUR.asn1.DERNull -1121 * @class class for ASN.1 DER Null -1122 * @extends KJUR.asn1.ASN1Object -1123 * @description -1124 * @see KJUR.asn1.ASN1Object - superclass -1125 */ -1126 KJUR.asn1.DERNull = function() { -1127 KJUR.asn1.DERNull.superclass.constructor.call(this); -1128 this.hT = "05"; -1129 this.hTLV = "0500"; -1130 }; -1131 extendClass(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object); -1132 -1133 // ******************************************************************** -1134 /** -1135 * class for ASN.1 DER ObjectIdentifier -1136 * @name KJUR.asn1.DERObjectIdentifier -1137 * @class class for ASN.1 DER ObjectIdentifier -1138 * @param {Object} JSON object or string of parameters (ex. {'oid': '2.5.4.5'}) -1139 * @extends KJUR.asn1.ASN1Object -1140 * @see oidtohex -1141 * -1142 * @description -1143 * <br/> -1144 * As for argument 'params' for constructor, you can specify one of -1145 * following properties: -1146 * <ul> -1147 * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li> -1148 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1149 * </ul> -1150 * NOTE: 'params' can be omitted. -1151 * @example -1152 * new DERObjectIdentifier({"name": "sha1"}) -1153 * new DERObjectIdentifier({"oid": "1.2.3.4"}) -1154 * new DERObjectIdentifier({"hex": "2d..."}) -1155 * new DERObjectIdentifier("1.2.3.4") -1156 * new DERObjectIdentifier("SHA1withRSA") -1157 */ -1158 KJUR.asn1.DERObjectIdentifier = function(params) { -1159 KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this); -1160 this.hT = "06"; -1161 -1162 /** -1163 * set value by a hexadecimal string -1164 * @name setValueHex -1165 * @memberOf KJUR.asn1.DERObjectIdentifier# -1166 * @function -1167 * @param {String} newHexString hexadecimal value of OID bytes -1168 */ -1169 this.setValueHex = function(newHexString) { -1170 this.hTLV = null; -1171 this.isModified = true; -1172 this.s = null; -1173 this.hV = newHexString; -1174 }; -1175 -1176 /** -1177 * set value by a OID string<br/> -1178 * @name setValueOidString -1179 * @memberOf KJUR.asn1.DERObjectIdentifier# -1180 * @function -1181 * @param {String} oidString OID string (ex. 2.5.4.13) -1182 * @example -1183 * o = new KJUR.asn1.DERObjectIdentifier(); -1184 * o.setValueOidString("2.5.4.13"); -1185 */ -1186 this.setValueOidString = function(oidString) { -1187 var h = oidtohex(oidString); -1188 if (h == null) -1189 throw new Error("malformed oid string: " + oidString); -1190 this.hTLV = null; -1191 this.isModified = true; -1192 this.s = null; -1193 this.hV = h; -1194 }; -1195 -1196 /** -1197 * set value by a OID name -1198 * @name setValueName -1199 * @memberOf KJUR.asn1.DERObjectIdentifier# -1200 * @function -1201 * @param {String} oidName OID name (ex. 'serverAuth') -1202 * @since 1.0.1 -1203 * @description -1204 * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'. -1205 * Otherwise raise error. -1206 * @example -1207 * o = new KJUR.asn1.DERObjectIdentifier(); -1208 * o.setValueName("serverAuth"); -1209 */ -1210 this.setValueName = function(oidName) { -1211 var oid = KJUR.asn1.x509.OID.name2oid(oidName); -1212 if (oid !== '') { -1213 this.setValueOidString(oid); -1214 } else { -1215 throw new Error("DERObjectIdentifier oidName undefined: " + oidName); -1216 } -1217 }; +1036 * @param {String} binaryString binary value string (i.e. '10111') +1037 * @description +1038 * Its unused bits will be calculated automatically by length of +1039 * 'binaryValue'. <br/> +1040 * NOTE: Trailing zeros '0' will be ignored. +1041 * @example +1042 * o = new KJUR.asn1.DERBitString(); +1043 * o.setByBooleanArray("01011"); +1044 */ +1045 this.setByBinaryString = function(binaryString) { +1046 binaryString = binaryString.replace(/0+$/, ''); +1047 var unusedBits = 8 - binaryString.length % 8; +1048 if (unusedBits == 8) unusedBits = 0; +1049 for (var i = 0; i <= unusedBits; i++) { +1050 binaryString += '0'; +1051 } +1052 var h = ''; +1053 for (var i = 0; i < binaryString.length - 1; i += 8) { +1054 var b = binaryString.substr(i, 8); +1055 var x = parseInt(b, 2).toString(16); +1056 if (x.length == 1) x = '0' + x; +1057 h += x; +1058 } +1059 this.hTLV = null; +1060 this.isModified = true; +1061 this.hV = '0' + unusedBits + h; +1062 }; +1063 +1064 /** +1065 * set ASN.1 TLV value(V) by an array of boolean<br/> +1066 * @name setByBooleanArray +1067 * @memberOf KJUR.asn1.DERBitString# +1068 * @function +1069 * @param {array} booleanArray array of boolean (ex. [true, false, true]) +1070 * @description +1071 * NOTE: Trailing falses will be ignored in the ASN.1 DER Object. +1072 * @example +1073 * o = new KJUR.asn1.DERBitString(); +1074 * o.setByBooleanArray([false, true, false, true, true]); +1075 */ +1076 this.setByBooleanArray = function(booleanArray) { +1077 var s = ''; +1078 for (var i = 0; i < booleanArray.length; i++) { +1079 if (booleanArray[i] == true) { +1080 s += '1'; +1081 } else { +1082 s += '0'; +1083 } +1084 } +1085 this.setByBinaryString(s); +1086 }; +1087 +1088 /** +1089 * generate an array of falses with specified length<br/> +1090 * @name newFalseArray +1091 * @memberOf KJUR.asn1.DERBitString +1092 * @function +1093 * @param {Integer} nLength length of array to generate +1094 * @return {array} array of boolean falses +1095 * @description +1096 * This static method may be useful to initialize boolean array. +1097 * @example +1098 * o = new KJUR.asn1.DERBitString(); +1099 * o.newFalseArray(3) → [false, false, false] +1100 */ +1101 this.newFalseArray = function(nLength) { +1102 var a = new Array(nLength); +1103 for (var i = 0; i < nLength; i++) { +1104 a[i] = false; +1105 } +1106 return a; +1107 }; +1108 +1109 this.getFreshValueHex = function() { +1110 return this.hV; +1111 }; +1112 +1113 if (typeof params != "undefined") { +1114 if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) { +1115 this.setHexValueIncludingUnusedBits(params); +1116 } else if (typeof params['hex'] != "undefined") { +1117 this.setHexValueIncludingUnusedBits(params['hex']); +1118 } else if (typeof params['bin'] != "undefined") { +1119 this.setByBinaryString(params['bin']); +1120 } else if (typeof params['array'] != "undefined") { +1121 this.setByBooleanArray(params['array']); +1122 } +1123 } +1124 }; +1125 extendClass(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object); +1126 +1127 // ******************************************************************** +1128 /** +1129 * class for ASN.1 DER OctetString<br/> +1130 * @name KJUR.asn1.DEROctetString +1131 * @class class for ASN.1 DER OctetString +1132 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1133 * @extends KJUR.asn1.DERAbstractString +1134 * @description +1135 * This class provides ASN.1 OctetString simple type.<br/> +1136 * Supported "params" attributes are: +1137 * <ul> +1138 * <li>str - to set a string as a value</li> +1139 * <li>hex - to set a hexadecimal string as a value</li> +1140 * <li>obj - to set a encapsulated ASN.1 value by JSON object +1141 * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li> +1142 * </ul> +1143 * NOTE: A parameter 'obj' have been supported +1144 * for "OCTET STRING, encapsulates" structure. +1145 * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25). +1146 * @see KJUR.asn1.DERAbstractString - superclass +1147 * @example +1148 * // default constructor +1149 * o = new KJUR.asn1.DEROctetString(); +1150 * // initialize with string +1151 * o = new KJUR.asn1.DEROctetString({str: "aaa"}); +1152 * // initialize with hexadecimal string +1153 * o = new KJUR.asn1.DEROctetString({hex: "616161"}); +1154 * // initialize with ASN1Util.newObject argument +1155 * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +1156 * // above generates a ASN.1 data like this: +1157 * // OCTET STRING, encapsulates { +1158 * // SEQUENCE { +1159 * // INTEGER 3 +1160 * // PrintableString 'aaa' +1161 * // } +1162 * // } +1163 */ +1164 KJUR.asn1.DEROctetString = function(params) { +1165 if (params !== undefined && typeof params.obj !== "undefined") { +1166 var o = KJUR.asn1.ASN1Util.newObject(params.obj); +1167 params.hex = o.getEncodedHex(); +1168 } +1169 KJUR.asn1.DEROctetString.superclass.constructor.call(this, params); +1170 this.hT = "04"; +1171 }; +1172 extendClass(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString); +1173 +1174 // ******************************************************************** +1175 /** +1176 * class for ASN.1 DER Null +1177 * @name KJUR.asn1.DERNull +1178 * @class class for ASN.1 DER Null +1179 * @extends KJUR.asn1.ASN1Object +1180 * @description +1181 * @see KJUR.asn1.ASN1Object - superclass +1182 */ +1183 KJUR.asn1.DERNull = function() { +1184 KJUR.asn1.DERNull.superclass.constructor.call(this); +1185 this.hT = "05"; +1186 this.hTLV = "0500"; +1187 }; +1188 extendClass(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object); +1189 +1190 // ******************************************************************** +1191 /** +1192 * class for ASN.1 DER ObjectIdentifier +1193 * @name KJUR.asn1.DERObjectIdentifier +1194 * @class class for ASN.1 DER ObjectIdentifier +1195 * @param {Object} JSON object or string of parameters (ex. {'oid': '2.5.4.5'}) +1196 * @extends KJUR.asn1.ASN1Object +1197 * @see oidtohex +1198 * +1199 * @description +1200 * <br/> +1201 * As for argument 'params' for constructor, you can specify one of +1202 * following properties: +1203 * <ul> +1204 * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li> +1205 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1206 * </ul> +1207 * NOTE: 'params' can be omitted. +1208 * @example +1209 * new DERObjectIdentifier({"name": "sha1"}) +1210 * new DERObjectIdentifier({"oid": "1.2.3.4"}) +1211 * new DERObjectIdentifier({"hex": "2d..."}) +1212 * new DERObjectIdentifier("1.2.3.4") +1213 * new DERObjectIdentifier("SHA1withRSA") +1214 */ +1215 KJUR.asn1.DERObjectIdentifier = function(params) { +1216 KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this); +1217 this.hT = "06"; 1218 -1219 this.setValueNameOrOid = function(nameOrOid) { -1220 if (nameOrOid.match(/^[0-2].[0-9.]+$/)) { -1221 this.setValueOidString(nameOrOid); -1222 } else { -1223 this.setValueName(nameOrOid); -1224 } -1225 } -1226 -1227 this.getFreshValueHex = function() { -1228 return this.hV; -1229 }; -1230 -1231 this.setByParam = function(params) { -1232 if (typeof params === "string") { -1233 this.setValueNameOrOid(params); -1234 } else if (params.oid !== undefined) { -1235 this.setValueNameOrOid(params.oid); -1236 } else if (params.name !== undefined) { -1237 this.setValueNameOrOid(params.name); -1238 } else if (params.hex !== undefined) { -1239 this.setValueHex(params.hex); -1240 } -1241 }; -1242 -1243 if (params !== undefined) this.setByParam(params); -1244 }; -1245 extendClass(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object); -1246 -1247 // ******************************************************************** -1248 /** -1249 * class for ASN.1 DER Enumerated -1250 * @name KJUR.asn1.DEREnumerated -1251 * @class class for ASN.1 DER Enumerated -1252 * @extends KJUR.asn1.ASN1Object -1253 * @description -1254 * <br/> -1255 * As for argument 'params' for constructor, you can specify one of -1256 * following properties: -1257 * <ul> -1258 * <li>int - specify initial ASN.1 value(V) by integer value</li> -1259 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1260 * </ul> -1261 * NOTE: 'params' can be omitted. -1262 * @example -1263 * new KJUR.asn1.DEREnumerated(123); -1264 * new KJUR.asn1.DEREnumerated({int: 123}); -1265 * new KJUR.asn1.DEREnumerated({hex: '1fad'}); -1266 */ -1267 KJUR.asn1.DEREnumerated = function(params) { -1268 KJUR.asn1.DEREnumerated.superclass.constructor.call(this); -1269 this.hT = "0a"; -1270 -1271 /** -1272 * set value by Tom Wu's BigInteger object -1273 * @name setByBigInteger -1274 * @memberOf KJUR.asn1.DEREnumerated# -1275 * @function -1276 * @param {BigInteger} bigIntegerValue to set -1277 */ -1278 this.setByBigInteger = function(bigIntegerValue) { -1279 this.hTLV = null; -1280 this.isModified = true; -1281 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); -1282 }; +1219 /** +1220 * set value by a hexadecimal string +1221 * @name setValueHex +1222 * @memberOf KJUR.asn1.DERObjectIdentifier# +1223 * @function +1224 * @param {String} newHexString hexadecimal value of OID bytes +1225 */ +1226 this.setValueHex = function(newHexString) { +1227 this.hTLV = null; +1228 this.isModified = true; +1229 this.s = null; +1230 this.hV = newHexString; +1231 }; +1232 +1233 /** +1234 * set value by a OID string<br/> +1235 * @name setValueOidString +1236 * @memberOf KJUR.asn1.DERObjectIdentifier# +1237 * @function +1238 * @param {String} oidString OID string (ex. 2.5.4.13) +1239 * @example +1240 * o = new KJUR.asn1.DERObjectIdentifier(); +1241 * o.setValueOidString("2.5.4.13"); +1242 */ +1243 this.setValueOidString = function(oidString) { +1244 var h = oidtohex(oidString); +1245 if (h == null) +1246 throw new Error("malformed oid string: " + oidString); +1247 this.hTLV = null; +1248 this.isModified = true; +1249 this.s = null; +1250 this.hV = h; +1251 }; +1252 +1253 /** +1254 * set value by a OID name +1255 * @name setValueName +1256 * @memberOf KJUR.asn1.DERObjectIdentifier# +1257 * @function +1258 * @param {String} oidName OID name (ex. 'serverAuth') +1259 * @since 1.0.1 +1260 * @description +1261 * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'. +1262 * Otherwise raise error. +1263 * @example +1264 * o = new KJUR.asn1.DERObjectIdentifier(); +1265 * o.setValueName("serverAuth"); +1266 */ +1267 this.setValueName = function(oidName) { +1268 var oid = KJUR.asn1.x509.OID.name2oid(oidName); +1269 if (oid !== '') { +1270 this.setValueOidString(oid); +1271 } else { +1272 throw new Error("DERObjectIdentifier oidName undefined: " + oidName); +1273 } +1274 }; +1275 +1276 this.setValueNameOrOid = function(nameOrOid) { +1277 if (nameOrOid.match(/^[0-2].[0-9.]+$/)) { +1278 this.setValueOidString(nameOrOid); +1279 } else { +1280 this.setValueName(nameOrOid); +1281 } +1282 } 1283 -1284 /** -1285 * set value by integer value -1286 * @name setByInteger -1287 * @memberOf KJUR.asn1.DEREnumerated# -1288 * @function -1289 * @param {Integer} integer value to set -1290 */ -1291 this.setByInteger = function(intValue) { -1292 var bi = new BigInteger(String(intValue), 10); -1293 this.setByBigInteger(bi); -1294 }; -1295 -1296 /** -1297 * set value by integer value -1298 * @name setValueHex -1299 * @memberOf KJUR.asn1.DEREnumerated# -1300 * @function -1301 * @param {String} hexadecimal string of integer value -1302 * @description -1303 * <br/> -1304 * NOTE: Value shall be represented by minimum octet length of -1305 * two's complement representation. -1306 */ -1307 this.setValueHex = function(newHexString) { -1308 this.hV = newHexString; -1309 }; -1310 -1311 this.getFreshValueHex = function() { -1312 return this.hV; -1313 }; -1314 -1315 if (typeof params != "undefined") { -1316 if (typeof params['int'] != "undefined") { -1317 this.setByInteger(params['int']); -1318 } else if (typeof params == "number") { -1319 this.setByInteger(params); -1320 } else if (typeof params['hex'] != "undefined") { -1321 this.setValueHex(params['hex']); -1322 } -1323 } -1324 }; -1325 extendClass(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object); -1326 -1327 // ******************************************************************** -1328 /** -1329 * class for ASN.1 DER UTF8String -1330 * @name KJUR.asn1.DERUTF8String -1331 * @class class for ASN.1 DER UTF8String -1332 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1333 * @extends KJUR.asn1.DERAbstractString -1334 * @description -1335 * @see KJUR.asn1.DERAbstractString - superclass -1336 */ -1337 KJUR.asn1.DERUTF8String = function(params) { -1338 KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params); -1339 this.hT = "0c"; -1340 }; -1341 extendClass(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString); -1342 -1343 // ******************************************************************** -1344 /** -1345 * class for ASN.1 DER NumericString -1346 * @name KJUR.asn1.DERNumericString -1347 * @class class for ASN.1 DER NumericString -1348 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1349 * @extends KJUR.asn1.DERAbstractString -1350 * @description -1351 * @see KJUR.asn1.DERAbstractString - superclass -1352 */ -1353 KJUR.asn1.DERNumericString = function(params) { -1354 KJUR.asn1.DERNumericString.superclass.constructor.call(this, params); -1355 this.hT = "12"; -1356 }; -1357 extendClass(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString); -1358 -1359 // ******************************************************************** -1360 /** -1361 * class for ASN.1 DER PrintableString -1362 * @name KJUR.asn1.DERPrintableString -1363 * @class class for ASN.1 DER PrintableString -1364 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1365 * @extends KJUR.asn1.DERAbstractString -1366 * @description -1367 * @see KJUR.asn1.DERAbstractString - superclass -1368 */ -1369 KJUR.asn1.DERPrintableString = function(params) { -1370 KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params); -1371 this.hT = "13"; -1372 }; -1373 extendClass(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString); -1374 -1375 // ******************************************************************** -1376 /** -1377 * class for ASN.1 DER TeletexString -1378 * @name KJUR.asn1.DERTeletexString -1379 * @class class for ASN.1 DER TeletexString -1380 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1381 * @extends KJUR.asn1.DERAbstractString -1382 * @description -1383 * @see KJUR.asn1.DERAbstractString - superclass -1384 */ -1385 KJUR.asn1.DERTeletexString = function(params) { -1386 KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params); -1387 this.hT = "14"; -1388 }; -1389 extendClass(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString); -1390 -1391 // ******************************************************************** -1392 /** -1393 * class for ASN.1 DER IA5String -1394 * @name KJUR.asn1.DERIA5String -1395 * @class class for ASN.1 DER IA5String -1396 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1397 * @extends KJUR.asn1.DERAbstractString -1398 * @description -1399 * @see KJUR.asn1.DERAbstractString - superclass -1400 */ -1401 KJUR.asn1.DERIA5String = function(params) { -1402 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); -1403 this.hT = "16"; -1404 }; -1405 extendClass(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString); -1406 -1407 // ******************************************************************** -1408 /** -1409 * class for ASN.1 DER VisibleString -1410 * @name KJUR.asn1.DERVisibleString -1411 * @class class for ASN.1 DER VisibleString -1412 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1413 * @extends KJUR.asn1.DERAbstractString -1414 * @since jsrsasign 8.0.23 asn1 1.0.15 -1415 * @description -1416 * @see KJUR.asn1.DERAbstractString - superclass -1417 */ -1418 KJUR.asn1.DERVisibleString = function(params) { -1419 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); -1420 this.hT = "1a"; -1421 }; -1422 extendClass(KJUR.asn1.DERVisibleString, KJUR.asn1.DERAbstractString); -1423 -1424 // ******************************************************************** -1425 /** -1426 * class for ASN.1 DER BMPString -1427 * @name KJUR.asn1.DERBMPString -1428 * @class class for ASN.1 DER BMPString -1429 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1430 * @extends KJUR.asn1.DERAbstractString -1431 * @since jsrsasign 8.0.23 asn1 1.0.15 -1432 * @description -1433 * @see KJUR.asn1.DERAbstractString - superclass -1434 */ -1435 KJUR.asn1.DERBMPString = function(params) { -1436 KJUR.asn1.DERBMPString.superclass.constructor.call(this, params); -1437 this.hT = "1e"; -1438 }; -1439 extendClass(KJUR.asn1.DERBMPString, KJUR.asn1.DERAbstractString); -1440 -1441 // ******************************************************************** -1442 /** -1443 * class for ASN.1 DER UTCTime -1444 * @name KJUR.asn1.DERUTCTime -1445 * @class class for ASN.1 DER UTCTime -1446 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) -1447 * @extends KJUR.asn1.DERAbstractTime -1448 * @description -1449 * <br/> -1450 * As for argument 'params' for constructor, you can specify one of -1451 * following properties: -1452 * <ul> -1453 * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li> -1454 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1455 * <li>date - specify Date object.</li> -1456 * </ul> -1457 * NOTE: 'params' can be omitted. -1458 * <h4>EXAMPLES</h4> -1459 * @example -1460 * d1 = new KJUR.asn1.DERUTCTime(); -1461 * d1.setString('130430125959Z'); -1462 * -1463 * d2 = new KJUR.asn1.DERUTCTime({'str': '130430125959Z'}); -1464 * d3 = new KJUR.asn1.DERUTCTime({'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))}); -1465 * d4 = new KJUR.asn1.DERUTCTime('130430125959Z'); -1466 */ -1467 KJUR.asn1.DERUTCTime = function(params) { -1468 KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params); -1469 this.hT = "17"; -1470 -1471 /** -1472 * set value by a Date object<br/> -1473 * @name setByDate -1474 * @memberOf KJUR.asn1.DERUTCTime# -1475 * @function -1476 * @param {Date} dateObject Date object to set ASN.1 value(V) -1477 * @example -1478 * o = new KJUR.asn1.DERUTCTime(); -1479 * o.setByDate(new Date("2016/12/31")); -1480 */ -1481 this.setByDate = function(dateObject) { -1482 this.hTLV = null; -1483 this.isModified = true; -1484 this.date = dateObject; -1485 this.s = this.formatDate(this.date, 'utc'); -1486 this.hV = stohex(this.s); -1487 }; -1488 -1489 this.getFreshValueHex = function() { -1490 if (typeof this.date == "undefined" && typeof this.s == "undefined") { -1491 this.date = new Date(); -1492 this.s = this.formatDate(this.date, 'utc'); -1493 this.hV = stohex(this.s); -1494 } -1495 return this.hV; -1496 }; +1284 this.getFreshValueHex = function() { +1285 return this.hV; +1286 }; +1287 +1288 this.setByParam = function(params) { +1289 if (typeof params === "string") { +1290 this.setValueNameOrOid(params); +1291 } else if (params.oid !== undefined) { +1292 this.setValueNameOrOid(params.oid); +1293 } else if (params.name !== undefined) { +1294 this.setValueNameOrOid(params.name); +1295 } else if (params.hex !== undefined) { +1296 this.setValueHex(params.hex); +1297 } +1298 }; +1299 +1300 if (params !== undefined) this.setByParam(params); +1301 }; +1302 extendClass(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object); +1303 +1304 // ******************************************************************** +1305 /** +1306 * class for ASN.1 DER Enumerated +1307 * @name KJUR.asn1.DEREnumerated +1308 * @class class for ASN.1 DER Enumerated +1309 * @extends KJUR.asn1.ASN1Object +1310 * @description +1311 * <br/> +1312 * As for argument 'params' for constructor, you can specify one of +1313 * following properties: +1314 * <ul> +1315 * <li>int - specify initial ASN.1 value(V) by integer value</li> +1316 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1317 * </ul> +1318 * NOTE: 'params' can be omitted. +1319 * @example +1320 * new KJUR.asn1.DEREnumerated(123); +1321 * new KJUR.asn1.DEREnumerated({int: 123}); +1322 * new KJUR.asn1.DEREnumerated({hex: '1fad'}); +1323 */ +1324 KJUR.asn1.DEREnumerated = function(params) { +1325 KJUR.asn1.DEREnumerated.superclass.constructor.call(this); +1326 this.hT = "0a"; +1327 +1328 /** +1329 * set value by Tom Wu's BigInteger object +1330 * @name setByBigInteger +1331 * @memberOf KJUR.asn1.DEREnumerated# +1332 * @function +1333 * @param {BigInteger} bigIntegerValue to set +1334 */ +1335 this.setByBigInteger = function(bigIntegerValue) { +1336 this.hTLV = null; +1337 this.isModified = true; +1338 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); +1339 }; +1340 +1341 /** +1342 * set value by integer value +1343 * @name setByInteger +1344 * @memberOf KJUR.asn1.DEREnumerated# +1345 * @function +1346 * @param {Integer} integer value to set +1347 */ +1348 this.setByInteger = function(intValue) { +1349 var bi = new BigInteger(String(intValue), 10); +1350 this.setByBigInteger(bi); +1351 }; +1352 +1353 /** +1354 * set value by integer value +1355 * @name setValueHex +1356 * @memberOf KJUR.asn1.DEREnumerated# +1357 * @function +1358 * @param {String} hexadecimal string of integer value +1359 * @description +1360 * <br/> +1361 * NOTE: Value shall be represented by minimum octet length of +1362 * two's complement representation. +1363 */ +1364 this.setValueHex = function(newHexString) { +1365 this.hV = newHexString; +1366 }; +1367 +1368 this.getFreshValueHex = function() { +1369 return this.hV; +1370 }; +1371 +1372 if (typeof params != "undefined") { +1373 if (typeof params['int'] != "undefined") { +1374 this.setByInteger(params['int']); +1375 } else if (typeof params == "number") { +1376 this.setByInteger(params); +1377 } else if (typeof params['hex'] != "undefined") { +1378 this.setValueHex(params['hex']); +1379 } +1380 } +1381 }; +1382 extendClass(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object); +1383 +1384 // ******************************************************************** +1385 /** +1386 * class for ASN.1 DER UTF8String +1387 * @name KJUR.asn1.DERUTF8String +1388 * @class class for ASN.1 DER UTF8String +1389 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1390 * @extends KJUR.asn1.DERAbstractString +1391 * @description +1392 * @see KJUR.asn1.DERAbstractString - superclass +1393 */ +1394 KJUR.asn1.DERUTF8String = function(params) { +1395 KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params); +1396 this.hT = "0c"; +1397 }; +1398 extendClass(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString); +1399 +1400 // ******************************************************************** +1401 /** +1402 * class for ASN.1 DER NumericString +1403 * @name KJUR.asn1.DERNumericString +1404 * @class class for ASN.1 DER NumericString +1405 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1406 * @extends KJUR.asn1.DERAbstractString +1407 * @description +1408 * @see KJUR.asn1.DERAbstractString - superclass +1409 */ +1410 KJUR.asn1.DERNumericString = function(params) { +1411 KJUR.asn1.DERNumericString.superclass.constructor.call(this, params); +1412 this.hT = "12"; +1413 }; +1414 extendClass(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString); +1415 +1416 // ******************************************************************** +1417 /** +1418 * class for ASN.1 DER PrintableString +1419 * @name KJUR.asn1.DERPrintableString +1420 * @class class for ASN.1 DER PrintableString +1421 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1422 * @extends KJUR.asn1.DERAbstractString +1423 * @description +1424 * @see KJUR.asn1.DERAbstractString - superclass +1425 */ +1426 KJUR.asn1.DERPrintableString = function(params) { +1427 KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params); +1428 this.hT = "13"; +1429 }; +1430 extendClass(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString); +1431 +1432 // ******************************************************************** +1433 /** +1434 * class for ASN.1 DER TeletexString +1435 * @name KJUR.asn1.DERTeletexString +1436 * @class class for ASN.1 DER TeletexString +1437 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1438 * @extends KJUR.asn1.DERAbstractString +1439 * @description +1440 * @see KJUR.asn1.DERAbstractString - superclass +1441 */ +1442 KJUR.asn1.DERTeletexString = function(params) { +1443 KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params); +1444 this.hT = "14"; +1445 }; +1446 extendClass(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString); +1447 +1448 // ******************************************************************** +1449 /** +1450 * class for ASN.1 DER IA5String +1451 * @name KJUR.asn1.DERIA5String +1452 * @class class for ASN.1 DER IA5String +1453 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1454 * @extends KJUR.asn1.DERAbstractString +1455 * @description +1456 * @see KJUR.asn1.DERAbstractString - superclass +1457 */ +1458 KJUR.asn1.DERIA5String = function(params) { +1459 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); +1460 this.hT = "16"; +1461 }; +1462 extendClass(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString); +1463 +1464 // ******************************************************************** +1465 /** +1466 * class for ASN.1 DER VisibleString +1467 * @name KJUR.asn1.DERVisibleString +1468 * @class class for ASN.1 DER VisibleString +1469 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1470 * @extends KJUR.asn1.DERAbstractString +1471 * @since jsrsasign 8.0.23 asn1 1.0.15 +1472 * @description +1473 * @see KJUR.asn1.DERAbstractString - superclass +1474 */ +1475 KJUR.asn1.DERVisibleString = function(params) { +1476 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); +1477 this.hT = "1a"; +1478 }; +1479 extendClass(KJUR.asn1.DERVisibleString, KJUR.asn1.DERAbstractString); +1480 +1481 // ******************************************************************** +1482 /** +1483 * class for ASN.1 DER BMPString +1484 * @name KJUR.asn1.DERBMPString +1485 * @class class for ASN.1 DER BMPString +1486 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1487 * @extends KJUR.asn1.DERAbstractString +1488 * @since jsrsasign 8.0.23 asn1 1.0.15 +1489 * @description +1490 * @see KJUR.asn1.DERAbstractString - superclass +1491 */ +1492 KJUR.asn1.DERBMPString = function(params) { +1493 KJUR.asn1.DERBMPString.superclass.constructor.call(this, params); +1494 this.hT = "1e"; +1495 }; +1496 extendClass(KJUR.asn1.DERBMPString, KJUR.asn1.DERAbstractString); 1497 -1498 if (params !== undefined) { -1499 if (params.str !== undefined) { -1500 this.setString(params.str); -1501 } else if (typeof params == "string" && params.match(/^[0-9]{12}Z$/)) { -1502 this.setString(params); -1503 } else if (params.hex !== undefined) { -1504 this.setStringHex(params.hex); -1505 } else if (params.date !== undefined) { -1506 this.setByDate(params.date); -1507 } -1508 } -1509 }; -1510 extendClass(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime); -1511 -1512 // ******************************************************************** -1513 /** -1514 * class for ASN.1 DER GeneralizedTime -1515 * @name KJUR.asn1.DERGeneralizedTime -1516 * @class class for ASN.1 DER GeneralizedTime -1517 * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'}) -1518 * @property {Boolean} withMillis flag to show milliseconds or not -1519 * @extends KJUR.asn1.DERAbstractTime -1520 * @description -1521 * <br/> -1522 * As for argument 'params' for constructor, you can specify one of -1523 * following properties: -1524 * <ul> -1525 * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li> -1526 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1527 * <li>date - specify Date object.</li> -1528 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> -1529 * </ul> -1530 * NOTE1: 'params' can be omitted. -1531 * NOTE2: 'withMillis' property is supported from asn1 1.0.6. -1532 */ -1533 KJUR.asn1.DERGeneralizedTime = function(params) { -1534 KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params); -1535 this.hT = "18"; -1536 this.withMillis = false; -1537 -1538 /** -1539 * set value by a Date object -1540 * @name setByDate -1541 * @memberOf KJUR.asn1.DERGeneralizedTime# -1542 * @function -1543 * @param {Date} dateObject Date object to set ASN.1 value(V) -1544 * @example -1545 * When you specify UTC time, use 'Date.UTC' method like this:<br/> -1546 * o1 = new DERUTCTime(); -1547 * o1.setByDate(date); -1548 * -1549 * date = new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)); #2015JAN31 23:59:59 -1550 */ -1551 this.setByDate = function(dateObject) { -1552 this.hTLV = null; -1553 this.isModified = true; -1554 this.date = dateObject; -1555 this.s = this.formatDate(this.date, 'gen', this.withMillis); -1556 this.hV = stohex(this.s); -1557 }; -1558 -1559 this.getFreshValueHex = function() { -1560 if (this.date === undefined && this.s === undefined) { -1561 this.date = new Date(); -1562 this.s = this.formatDate(this.date, 'gen', this.withMillis); -1563 this.hV = stohex(this.s); -1564 } -1565 return this.hV; -1566 }; -1567 -1568 if (params !== undefined) { -1569 if (params.str !== undefined) { -1570 this.setString(params.str); -1571 } else if (typeof params == "string" && params.match(/^[0-9]{14}Z$/)) { -1572 this.setString(params); -1573 } else if (params.hex !== undefined) { -1574 this.setStringHex(params.hex); -1575 } else if (params.date !== undefined) { -1576 this.setByDate(params.date); -1577 } -1578 if (params.millis === true) { -1579 this.withMillis = true; -1580 } -1581 } -1582 }; -1583 extendClass(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime); -1584 -1585 // ******************************************************************** -1586 /** -1587 * class for ASN.1 DER Sequence -1588 * @name KJUR.asn1.DERSequence -1589 * @class class for ASN.1 DER Sequence -1590 * @extends KJUR.asn1.DERAbstractStructured -1591 * @description -1592 * <br/> -1593 * As for argument 'params' for constructor, you can specify one of -1594 * following properties: -1595 * <ul> -1596 * <li>array - specify array of ASN1Object to set elements of content</li> -1597 * </ul> -1598 * NOTE: 'params' can be omitted. -1599 */ -1600 KJUR.asn1.DERSequence = function(params) { -1601 KJUR.asn1.DERSequence.superclass.constructor.call(this, params); -1602 this.hT = "30"; -1603 this.getFreshValueHex = function() { -1604 var h = ''; -1605 for (var i = 0; i < this.asn1Array.length; i++) { -1606 var asn1Obj = this.asn1Array[i]; -1607 h += asn1Obj.getEncodedHex(); -1608 } -1609 this.hV = h; -1610 return this.hV; -1611 }; -1612 }; -1613 extendClass(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured); +1498 // ******************************************************************** +1499 /** +1500 * class for ASN.1 DER UTCTime +1501 * @name KJUR.asn1.DERUTCTime +1502 * @class class for ASN.1 DER UTCTime +1503 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) +1504 * @extends KJUR.asn1.DERAbstractTime +1505 * @see KJUR.asn1.DERGeneralizedTime +1506 * @see KJUR.asn1.x509.Time +1507 * +1508 * @description +1509 * <br/> +1510 * As for argument 'params' for constructor, you can specify one of +1511 * following properties: +1512 * <ul> +1513 * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li> +1514 * <li>date - specify Date object.</li> +1515 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> +1516 * </ul> +1517 * NOTE1: 'params' can be omitted. +1518 * NOTE2: 'millis' property is supported from jsrsasign 10.4.1 asn1 1.0.22. +1519 * +1520 * <h4>EXAMPLES</h4> +1521 * @example +1522 * new DERUTCTime("20151231235959Z") +1523 * new DERUTCTime("20151231235959.123Z") +1524 * new DERUTCTime(new Date()) +1525 * new DERUTCTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) +1526 * new DERUTCTime({str: "20151231235959.123Z"}) +1527 * new DERUTCTime({date: new Date()}) +1528 * new DERUTCTime({date: new Date(), millis: true}) +1529 * new DERUTCTime({millis: true}) +1530 */ +1531 KJUR.asn1.DERUTCTime = function(params) { +1532 KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params); +1533 this.hT = "17"; +1534 this.params = undefined; +1535 +1536 this.getFreshValueHex = function() { +1537 var params = this.params; +1538 +1539 if (this.params == undefined) params = { date: new Date() }; +1540 +1541 if (typeof params == "string") { +1542 if (params.match(/^[0-9]{12}Z$/) || +1543 params.match(/^[0-9]{12}\.[0-9]+Z$/)) { +1544 this.hV = stohex(params); +1545 } else { +1546 throw new Error("malformed string for GeneralizedTime: " + params); +1547 } +1548 } else if (params.str != undefined) { +1549 this.hV = stohex(params.str); +1550 } else if (params.date == undefined && params.millis == true) { +1551 var date = new Date(); +1552 this.hV = stohex(this.formatDate(date, 'utc', true)); +1553 } else if (params.date != undefined && +1554 params.date instanceof Date) { +1555 var withMillis = (params.millis === true); +1556 this.hV = stohex(this.formatDate(params.date, 'utc', withMillis)); +1557 } else if (params instanceof Date) { +1558 this.hV = stohex(this.formatDate(params, 'utc')); +1559 } +1560 +1561 if (this.hV == undefined) { +1562 throw new Error("parameter not specified properly for UTCTime"); +1563 } +1564 return this.hV; +1565 }; +1566 +1567 if (params != undefined) this.setByParam(params); +1568 }; +1569 extendClass(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime); +1570 +1571 // ******************************************************************** +1572 /** +1573 * class for ASN.1 DER GeneralizedTime +1574 * @name KJUR.asn1.DERGeneralizedTime +1575 * @class class for ASN.1 DER GeneralizedTime +1576 * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'}) +1577 * @property {Boolean} withMillis flag to show milliseconds or not +1578 * @extends KJUR.asn1.DERAbstractTime +1579 * @see KJUR.asn1.DERUTCTime +1580 * @see KJUR.asn1.x509.Time +1581 * +1582 * @description +1583 * <br/> +1584 * As for argument 'params' for constructor, you can specify one of +1585 * following properties: +1586 * <ul> +1587 * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li> +1588 * <li>date - specify Date object.</li> +1589 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> +1590 * </ul> +1591 * NOTE1: 'params' can be omitted. +1592 * NOTE2: 'millis' property is supported from asn1 1.0.6. +1593 * +1594 * <h4>EXAMPLES</h4> +1595 * @example +1596 * new DERGeneralizedTime("20151231235959Z") +1597 * new DERGeneralizedTime("20151231235959.123Z") +1598 * new DERGeneralizedTime(new Date()) +1599 * new DERGeneralizedTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) +1600 * new DERGeneralizedTime({str: "20151231235959.123Z"}) +1601 * new DERGeneralizedTime({date: new Date()}) +1602 * new DERGeneralizedTime({date: new Date(), millis: true}) +1603 * new DERGeneralizedTime({millis: true}) +1604 */ +1605 KJUR.asn1.DERGeneralizedTime = function(params) { +1606 KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params); +1607 this.hT = "18"; +1608 this.params = params; +1609 +1610 this.getFreshValueHex = function() { +1611 var params = this.params; +1612 +1613 if (this.params == undefined) params = { date: new Date() }; 1614 -1615 // ******************************************************************** -1616 /** -1617 * class for ASN.1 DER Set -1618 * @name KJUR.asn1.DERSet -1619 * @class class for ASN.1 DER Set -1620 * @extends KJUR.asn1.DERAbstractStructured -1621 * @description -1622 * <br/> -1623 * As for argument 'params' for constructor, you can specify one of -1624 * following properties: -1625 * <ul> -1626 * <li>array - specify array of ASN1Object to set elements of content</li> -1627 * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li> -1628 * </ul> -1629 * NOTE1: 'params' can be omitted.<br/> -1630 * NOTE2: sortflag is supported since 1.0.5. -1631 */ -1632 KJUR.asn1.DERSet = function(params) { -1633 KJUR.asn1.DERSet.superclass.constructor.call(this, params); -1634 this.hT = "31"; -1635 this.sortFlag = true; // item shall be sorted only in ASN.1 DER -1636 this.getFreshValueHex = function() { -1637 var a = new Array(); -1638 for (var i = 0; i < this.asn1Array.length; i++) { -1639 var asn1Obj = this.asn1Array[i]; -1640 a.push(asn1Obj.getEncodedHex()); -1641 } -1642 if (this.sortFlag == true) a.sort(); -1643 this.hV = a.join(''); -1644 return this.hV; -1645 }; -1646 -1647 if (typeof params != "undefined") { -1648 if (typeof params.sortflag != "undefined" && -1649 params.sortflag == false) -1650 this.sortFlag = false; -1651 } -1652 }; -1653 extendClass(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured); -1654 -1655 // ******************************************************************** -1656 /** -1657 * class for ASN.1 DER TaggedObject -1658 * @name KJUR.asn1.DERTaggedObject -1659 * @class class for ASN.1 DER TaggedObject -1660 * @extends KJUR.asn1.ASN1Object -1661 * -1662 * @description -1663 * <br/> -1664 * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object. -1665 * For example, if you find '[1]' tag in a ASN.1 dump, -1666 * 'tagNoHex' will be 'a1'. -1667 * <br/> -1668 * As for optional argument 'params' for constructor, you can specify *ANY* of -1669 * following properties: -1670 * <ul> -1671 * <li>tag - specify tag (default is 'a0' which means [0])</li> -1672 * <li>explicit - specify true if this is explicit tag otherwise false -1673 * (default is 'true').</li> -1674 * <li>obj - specify ASN1Object which is tagged</li> -1675 * <li>tage - specify tag with explicit</li> -1676 * <li>tagi - specify tag with implicit</li> -1677 * </ul> -1678 * -1679 * @example -1680 * new KJUR.asn1.DERTaggedObject({ -1681 * tage:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // explicit -1682 * }) -1683 * new KJUR.asn1.DERTaggedObject({ -1684 * tagi:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // implicit -1685 * }) -1686 * new KJUR.asn1.DERTaggedObject({ -1687 * tag:'a0', explicit: true, obj: new KJUR.asn1.DERInteger({int: 3}) // explicit -1688 * }) -1689 * -1690 * // to hexadecimal -1691 * d1 = new KJUR.asn1.DERUTF8String({str':'a'}) -1692 * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1}); -1693 * hex = d2.getEncodedHex(); -1694 */ -1695 KJUR.asn1.DERTaggedObject = function(params) { -1696 KJUR.asn1.DERTaggedObject.superclass.constructor.call(this); -1697 -1698 var _KJUR_asn1 = KJUR.asn1; -1699 -1700 this.hT = "a0"; -1701 this.hV = ''; -1702 this.isExplicit = true; -1703 this.asn1Object = null; -1704 -1705 /** -1706 * set value by an ASN1Object -1707 * @name setString -1708 * @memberOf KJUR.asn1.DERTaggedObject# -1709 * @function -1710 * @param {Boolean} isExplicitFlag flag for explicit/implicit tag -1711 * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag -1712 * @param {ASN1Object} asn1Object ASN.1 to encapsulate -1713 */ -1714 this.setASN1Object = function(isExplicitFlag, tagNoHex, asn1Object) { -1715 this.hT = tagNoHex; -1716 this.isExplicit = isExplicitFlag; -1717 this.asn1Object = asn1Object; -1718 if (this.isExplicit) { -1719 this.hV = this.asn1Object.getEncodedHex(); -1720 this.hTLV = null; -1721 this.isModified = true; -1722 } else { -1723 this.hV = null; -1724 this.hTLV = asn1Object.getEncodedHex(); -1725 this.hTLV = this.hTLV.replace(/^../, tagNoHex); -1726 this.isModified = false; -1727 } -1728 }; -1729 -1730 this.getFreshValueHex = function() { -1731 return this.hV; -1732 }; -1733 -1734 this.setByParam = function(params) { -1735 if (params.tag != undefined) { -1736 this.hT = params.tag; -1737 } -1738 if (params.explicit != undefined) { -1739 this.isExplicit = params.explicit; -1740 } -1741 if (params.tage != undefined) { -1742 this.hT = params.tage; -1743 this.isExplicit = true; -1744 } -1745 if (params.tagi != undefined) { -1746 this.hT = params.tagi; -1747 this.isExplicit = false; -1748 } -1749 if (params.obj != undefined) { -1750 if (params.obj instanceof _KJUR_asn1.ASN1Object) { -1751 this.asn1Object = params.obj; -1752 this.setASN1Object(this.isExplicit, this.hT, this.asn1Object); -1753 } else if (typeof params.obj == "object") { -1754 this.asn1Object = _KJUR_asn1.ASN1Util.newObject(params.obj); -1755 this.setASN1Object(this.isExplicit, this.hT, this.asn1Object); -1756 } -1757 } -1758 }; +1615 if (typeof params == "string") { +1616 if (params.match(/^[0-9]{14}Z$/) || +1617 params.match(/^[0-9]{14}\.[0-9]+Z$/)) { +1618 this.hV = stohex(params); +1619 } else { +1620 throw new Error("malformed string for GeneralizedTime: " + params); +1621 } +1622 } else if (params.str != undefined) { +1623 this.hV = stohex(params.str); +1624 } else if (params.date == undefined && params.millis == true) { +1625 var date = new Date(); +1626 this.hV = stohex(this.formatDate(date, 'gen', true)); +1627 } else if (params.date != undefined && +1628 params.date instanceof Date) { +1629 var withMillis = (params.millis === true); +1630 this.hV = stohex(this.formatDate(params.date, 'gen', withMillis)); +1631 } else if (params instanceof Date) { +1632 this.hV = stohex(this.formatDate(params, 'gen')); +1633 } +1634 +1635 if (this.hV == undefined) { +1636 throw new Error("parameter not specified properly for GeneralizedTime"); +1637 } +1638 return this.hV; +1639 }; +1640 +1641 if (params != undefined) this.setByParam(params); +1642 }; +1643 extendClass(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime); +1644 +1645 // ******************************************************************** +1646 /** +1647 * class for ASN.1 DER Sequence +1648 * @name KJUR.asn1.DERSequence +1649 * @class class for ASN.1 DER Sequence +1650 * @extends KJUR.asn1.DERAbstractStructured +1651 * @description +1652 * <br/> +1653 * As for argument 'params' for constructor, you can specify one of +1654 * following properties: +1655 * <ul> +1656 * <li>array - specify array of ASN1Object to set elements of content</li> +1657 * </ul> +1658 * NOTE: 'params' can be omitted. +1659 */ +1660 KJUR.asn1.DERSequence = function(params) { +1661 KJUR.asn1.DERSequence.superclass.constructor.call(this, params); +1662 this.hT = "30"; +1663 this.getFreshValueHex = function() { +1664 var h = ''; +1665 for (var i = 0; i < this.asn1Array.length; i++) { +1666 var asn1Obj = this.asn1Array[i]; +1667 h += asn1Obj.getEncodedHex(); +1668 } +1669 this.hV = h; +1670 return this.hV; +1671 }; +1672 }; +1673 extendClass(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured); +1674 +1675 // ******************************************************************** +1676 /** +1677 * class for ASN.1 DER Set +1678 * @name KJUR.asn1.DERSet +1679 * @class class for ASN.1 DER Set +1680 * @extends KJUR.asn1.DERAbstractStructured +1681 * @description +1682 * <br/> +1683 * As for argument 'params' for constructor, you can specify one of +1684 * following properties: +1685 * <ul> +1686 * <li>array - specify array of ASN1Object to set elements of content</li> +1687 * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li> +1688 * </ul> +1689 * NOTE1: 'params' can be omitted.<br/> +1690 * NOTE2: sortflag is supported since 1.0.5. +1691 */ +1692 KJUR.asn1.DERSet = function(params) { +1693 KJUR.asn1.DERSet.superclass.constructor.call(this, params); +1694 this.hT = "31"; +1695 this.sortFlag = true; // item shall be sorted only in ASN.1 DER +1696 this.getFreshValueHex = function() { +1697 var a = new Array(); +1698 for (var i = 0; i < this.asn1Array.length; i++) { +1699 var asn1Obj = this.asn1Array[i]; +1700 a.push(asn1Obj.getEncodedHex()); +1701 } +1702 if (this.sortFlag == true) a.sort(); +1703 this.hV = a.join(''); +1704 return this.hV; +1705 }; +1706 +1707 if (typeof params != "undefined") { +1708 if (typeof params.sortflag != "undefined" && +1709 params.sortflag == false) +1710 this.sortFlag = false; +1711 } +1712 }; +1713 extendClass(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured); +1714 +1715 // ******************************************************************** +1716 /** +1717 * class for ASN.1 DER TaggedObject +1718 * @name KJUR.asn1.DERTaggedObject +1719 * @class class for ASN.1 DER TaggedObject +1720 * @extends KJUR.asn1.ASN1Object +1721 * +1722 * @description +1723 * <br/> +1724 * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object. +1725 * For example, if you find '[1]' tag in a ASN.1 dump, +1726 * 'tagNoHex' will be 'a1'. +1727 * <br/> +1728 * As for optional argument 'params' for constructor, you can specify *ANY* of +1729 * following properties: +1730 * <ul> +1731 * <li>tag - specify tag (default is 'a0' which means [0])</li> +1732 * <li>explicit - specify true if this is explicit tag otherwise false +1733 * (default is 'true').</li> +1734 * <li>obj - specify ASN1Object which is tagged</li> +1735 * <li>tage - specify tag with explicit</li> +1736 * <li>tagi - specify tag with implicit</li> +1737 * </ul> +1738 * +1739 * @example +1740 * new KJUR.asn1.DERTaggedObject({ +1741 * tage:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // explicit +1742 * }) +1743 * new KJUR.asn1.DERTaggedObject({ +1744 * tagi:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // implicit +1745 * }) +1746 * new KJUR.asn1.DERTaggedObject({ +1747 * tag:'a0', explicit: true, obj: new KJUR.asn1.DERInteger({int: 3}) // explicit +1748 * }) +1749 * +1750 * // to hexadecimal +1751 * d1 = new KJUR.asn1.DERUTF8String({str':'a'}) +1752 * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1}); +1753 * hex = d2.getEncodedHex(); +1754 */ +1755 KJUR.asn1.DERTaggedObject = function(params) { +1756 KJUR.asn1.DERTaggedObject.superclass.constructor.call(this); +1757 +1758 var _KJUR_asn1 = KJUR.asn1; 1759 -1760 if (params != undefined) this.setByParam(params); -1761 }; -1762 extendClass(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object); -1763