diff --git a/ChangeLog.txt b/ChangeLog.txt index 0fc864d4..b4cc8369 100755 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,13 @@ ChangeLog for jsrsasign +* Changes between 6.1.0 to 6.1.1 (2016-Sep-25) + - asn1 1.0.10 to 1.0.11 + - encapsulated ASN.1 structure supported + in DEROctetString and DERBitString. + - API document update + - tool_asn1encoder.html added + * Changes between 6.0.1 to 6.1.0 (2016-Sep-24) - asn1ocsp 1.0.0 - now start to add OCSP protocol support diff --git a/api/files.html b/api/files.html index 8dd2c284..5a8c84e3 100644 --- a/api/files.html +++ b/api/files.html @@ -435,7 +435,7 @@
// default constructor +o = new KJUR.asn1.DERBitString(); +// initialize with binary string +o = new KJUR.asn1.DERBitString({bin: "1011"}); +// initialize with boolean array +o = new KJUR.asn1.DERBitString({array: [true,false,true,true]}); +// initialize with hexadecimal string (04 is unused bits) +o = new KJUR.asn1.DEROctetString({hex: "04bac0"}); +// initialize with ASN1Util.newObject argument for encapsulated +o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +// above generates a ASN.1 data like this: +// BIT STRING, encapsulates { +// SEQUENCE { +// INTEGER 3 +// PrintableString 'aaa' +// } +// }+ + + diff --git a/api/symbols/KJUR.asn1.DEREnumerated.html b/api/symbols/KJUR.asn1.DEREnumerated.html index ffebe067..0763cf81 100644 --- a/api/symbols/KJUR.asn1.DEREnumerated.html +++ b/api/symbols/KJUR.asn1.DEREnumerated.html @@ -686,8 +686,8 @@
new KJUR.asn1.DEREnumerated(123); -new KJUR.asn1.DEREnumerated({'int': 123}); -new KJUR.asn1.DEREnumerated({'hex': '1fad'});+new KJUR.asn1.DEREnumerated({int: 123}); +new KJUR.asn1.DEREnumerated({hex: '1fad'}); diff --git a/api/symbols/KJUR.asn1.DEROctetString.html b/api/symbols/KJUR.asn1.DEROctetString.html index 18a494d9..448a0446 100644 --- a/api/symbols/KJUR.asn1.DEROctetString.html +++ b/api/symbols/KJUR.asn1.DEROctetString.html @@ -464,8 +464,8 @@
// default constructor +o = new KJUR.asn1.DEROctetString(); +// initialize with string +o = new KJUR.asn1.DEROctetString({str: "aaa"}); +// initialize with hexadecimal string +o = new KJUR.asn1.DEROctetString({hex: "616161"}); +// initialize with ASN1Util.newObject argument +o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +// above generates a ASN.1 data like this: +// OCTET STRING, encapsulates { +// SEQUENCE { +// INTEGER 3 +// PrintableString 'aaa' +// } +// }+ + +
1 /*! asn1-1.0.10.js (c) 2013-2016 Kenji Urushima | kjur.github.com/jsrsasign/license +1 /*! asn1-1.0.11.js (c) 2013-2016 Kenji Urushima | kjur.github.com/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 asn1 1.0.10 (2016-Aug-27) + 19 * @version asn1 1.0.11 (2016-Sep-25) 20 * @since jsrsasign 2.1 21 * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a> 22 */ @@ -834,728 +834,785 @@ 827 * <li>bin - specify binary string (ex. '10111')</li> 828 * <li>array - specify array of boolean (ex. [true,false,true,true])</li> 829 * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li> -830 * </ul> -831 * NOTE: 'params' can be omitted. -832 */ -833 KJUR.asn1.DERBitString = function(params) { -834 KJUR.asn1.DERBitString.superclass.constructor.call(this); -835 this.hT = "03"; -836 -837 /** -838 * set ASN.1 value(V) by a hexadecimal string including unused bits -839 * @name setHexValueIncludingUnusedBits -840 * @memberOf KJUR.asn1.DERBitString -841 * @function -842 * @param {String} newHexStringIncludingUnusedBits -843 */ -844 this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) { -845 this.hTLV = null; -846 this.isModified = true; -847 this.hV = newHexStringIncludingUnusedBits; -848 }; -849 -850 /** -851 * set ASN.1 value(V) by unused bit and hexadecimal string of value -852 * @name setUnusedBitsAndHexValue -853 * @memberOf KJUR.asn1.DERBitString -854 * @function -855 * @param {Integer} unusedBits -856 * @param {String} hValue -857 */ -858 this.setUnusedBitsAndHexValue = function(unusedBits, hValue) { -859 if (unusedBits < 0 || 7 < unusedBits) { -860 throw "unused bits shall be from 0 to 7: u = " + unusedBits; -861 } -862 var hUnusedBits = "0" + unusedBits; -863 this.hTLV = null; -864 this.isModified = true; -865 this.hV = hUnusedBits + hValue; -866 }; -867 -868 /** -869 * set ASN.1 DER BitString by binary string -870 * @name setByBinaryString -871 * @memberOf KJUR.asn1.DERBitString -872 * @function -873 * @param {String} binaryString binary value string (i.e. '10111') -874 * @description -875 * Its unused bits will be calculated automatically by length of -876 * 'binaryValue'. <br/> -877 * NOTE: Trailing zeros '0' will be ignored. -878 */ -879 this.setByBinaryString = function(binaryString) { -880 binaryString = binaryString.replace(/0+$/, ''); -881 var unusedBits = 8 - binaryString.length % 8; -882 if (unusedBits == 8) unusedBits = 0; -883 for (var i = 0; i <= unusedBits; i++) { -884 binaryString += '0'; -885 } -886 var h = ''; -887 for (var i = 0; i < binaryString.length - 1; i += 8) { -888 var b = binaryString.substr(i, 8); -889 var x = parseInt(b, 2).toString(16); -890 if (x.length == 1) x = '0' + x; -891 h += x; -892 } -893 this.hTLV = null; -894 this.isModified = true; -895 this.hV = '0' + unusedBits + h; -896 }; -897 -898 /** -899 * set ASN.1 TLV value(V) by an array of boolean -900 * @name setByBooleanArray -901 * @memberOf KJUR.asn1.DERBitString -902 * @function -903 * @param {array} booleanArray array of boolean (ex. [true, false, true]) -904 * @description -905 * NOTE: Trailing falses will be ignored. -906 */ -907 this.setByBooleanArray = function(booleanArray) { -908 var s = ''; -909 for (var i = 0; i < booleanArray.length; i++) { -910 if (booleanArray[i] == true) { -911 s += '1'; -912 } else { -913 s += '0'; -914 } -915 } -916 this.setByBinaryString(s); -917 }; -918 -919 /** -920 * generate an array of false with specified length -921 * @name newFalseArray -922 * @memberOf KJUR.asn1.DERBitString -923 * @function -924 * @param {Integer} nLength length of array to generate -925 * @return {array} array of boolean faluse -926 * @description -927 * This static method may be useful to initialize boolean array. -928 */ -929 this.newFalseArray = function(nLength) { -930 var a = new Array(nLength); -931 for (var i = 0; i < nLength; i++) { -932 a[i] = false; -933 } -934 return a; -935 }; -936 -937 this.getFreshValueHex = function() { -938 return this.hV; -939 }; -940 -941 if (typeof params != "undefined") { -942 if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) { -943 this.setHexValueIncludingUnusedBits(params); -944 } else if (typeof params['hex'] != "undefined") { -945 this.setHexValueIncludingUnusedBits(params['hex']); -946 } else if (typeof params['bin'] != "undefined") { -947 this.setByBinaryString(params['bin']); -948 } else if (typeof params['array'] != "undefined") { -949 this.setByBooleanArray(params['array']); -950 } -951 } -952 }; -953 YAHOO.lang.extend(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object); -954 -955 // ******************************************************************** -956 /** -957 * class for ASN.1 DER OctetString -958 * @name KJUR.asn1.DEROctetString -959 * @class class for ASN.1 DER OctetString -960 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -961 * @extends KJUR.asn1.DERAbstractString -962 * @description -963 * @see KJUR.asn1.DERAbstractString - superclass -964 */ -965 KJUR.asn1.DEROctetString = function(params) { -966 KJUR.asn1.DEROctetString.superclass.constructor.call(this, params); -967 this.hT = "04"; -968 }; -969 YAHOO.lang.extend(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString); -970 -971 // ******************************************************************** -972 /** -973 * class for ASN.1 DER Null -974 * @name KJUR.asn1.DERNull -975 * @class class for ASN.1 DER Null -976 * @extends KJUR.asn1.ASN1Object -977 * @description -978 * @see KJUR.asn1.ASN1Object - superclass -979 */ -980 KJUR.asn1.DERNull = function() { -981 KJUR.asn1.DERNull.superclass.constructor.call(this); -982 this.hT = "05"; -983 this.hTLV = "0500"; -984 }; -985 YAHOO.lang.extend(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object); -986 -987 // ******************************************************************** -988 /** -989 * class for ASN.1 DER ObjectIdentifier -990 * @name KJUR.asn1.DERObjectIdentifier -991 * @class class for ASN.1 DER ObjectIdentifier -992 * @param {Array} params associative array of parameters (ex. {'oid': '2.5.4.5'}) -993 * @extends KJUR.asn1.ASN1Object -994 * @description -995 * <br/> -996 * As for argument 'params' for constructor, you can specify one of -997 * following properties: -998 * <ul> -999 * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li> -1000 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1001 * </ul> -1002 * NOTE: 'params' can be omitted. -1003 */ -1004 KJUR.asn1.DERObjectIdentifier = function(params) { -1005 var itox = function(i) { -1006 var h = i.toString(16); -1007 if (h.length == 1) h = '0' + h; -1008 return h; -1009 }; -1010 var roidtox = function(roid) { -1011 var h = ''; -1012 var bi = new BigInteger(roid, 10); -1013 var b = bi.toString(2); -1014 var padLen = 7 - b.length % 7; -1015 if (padLen == 7) padLen = 0; -1016 var bPad = ''; -1017 for (var i = 0; i < padLen; i++) bPad += '0'; -1018 b = bPad + b; -1019 for (var i = 0; i < b.length - 1; i += 7) { -1020 var b8 = b.substr(i, 7); -1021 if (i != b.length - 7) b8 = '1' + b8; -1022 h += itox(parseInt(b8, 2)); -1023 } -1024 return h; -1025 } -1026 -1027 KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this); -1028 this.hT = "06"; -1029 -1030 /** -1031 * set value by a hexadecimal string -1032 * @name setValueHex -1033 * @memberOf KJUR.asn1.DERObjectIdentifier -1034 * @function -1035 * @param {String} newHexString hexadecimal value of OID bytes -1036 */ -1037 this.setValueHex = function(newHexString) { -1038 this.hTLV = null; -1039 this.isModified = true; -1040 this.s = null; -1041 this.hV = newHexString; -1042 }; +830 * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} +831 * argument for "BitString encapsulates" structure.</li> +832 * </ul> +833 * NOTE1: 'params' can be omitted.<br/> +834 * NOTE2: 'obj' parameter have been supported since +835 * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/> +836 * @example +837 * // default constructor +838 * o = new KJUR.asn1.DERBitString(); +839 * // initialize with binary string +840 * o = new KJUR.asn1.DERBitString({bin: "1011"}); +841 * // initialize with boolean array +842 * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]}); +843 * // initialize with hexadecimal string (04 is unused bits) +844 * o = new KJUR.asn1.DEROctetString({hex: "04bac0"}); +845 * // initialize with ASN1Util.newObject argument for encapsulated +846 * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +847 * // above generates a ASN.1 data like this: +848 * // BIT STRING, encapsulates { +849 * // SEQUENCE { +850 * // INTEGER 3 +851 * // PrintableString 'aaa' +852 * // } +853 * // } +854 */ +855 KJUR.asn1.DERBitString = function(params) { +856 if (params !== undefined && typeof params.obj !== "undefined") { +857 var o = KJUR.asn1.ASN1Util.newObject(params.obj); +858 params.hex = "00" + o.getEncodedHex(); +859 } +860 KJUR.asn1.DERBitString.superclass.constructor.call(this); +861 this.hT = "03"; +862 +863 /** +864 * set ASN.1 value(V) by a hexadecimal string including unused bits +865 * @name setHexValueIncludingUnusedBits +866 * @memberOf KJUR.asn1.DERBitString +867 * @function +868 * @param {String} newHexStringIncludingUnusedBits +869 */ +870 this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) { +871 this.hTLV = null; +872 this.isModified = true; +873 this.hV = newHexStringIncludingUnusedBits; +874 }; +875 +876 /** +877 * set ASN.1 value(V) by unused bit and hexadecimal string of value +878 * @name setUnusedBitsAndHexValue +879 * @memberOf KJUR.asn1.DERBitString +880 * @function +881 * @param {Integer} unusedBits +882 * @param {String} hValue +883 */ +884 this.setUnusedBitsAndHexValue = function(unusedBits, hValue) { +885 if (unusedBits < 0 || 7 < unusedBits) { +886 throw "unused bits shall be from 0 to 7: u = " + unusedBits; +887 } +888 var hUnusedBits = "0" + unusedBits; +889 this.hTLV = null; +890 this.isModified = true; +891 this.hV = hUnusedBits + hValue; +892 }; +893 +894 /** +895 * set ASN.1 DER BitString by binary string +896 * @name setByBinaryString +897 * @memberOf KJUR.asn1.DERBitString +898 * @function +899 * @param {String} binaryString binary value string (i.e. '10111') +900 * @description +901 * Its unused bits will be calculated automatically by length of +902 * 'binaryValue'. <br/> +903 * NOTE: Trailing zeros '0' will be ignored. +904 */ +905 this.setByBinaryString = function(binaryString) { +906 binaryString = binaryString.replace(/0+$/, ''); +907 var unusedBits = 8 - binaryString.length % 8; +908 if (unusedBits == 8) unusedBits = 0; +909 for (var i = 0; i <= unusedBits; i++) { +910 binaryString += '0'; +911 } +912 var h = ''; +913 for (var i = 0; i < binaryString.length - 1; i += 8) { +914 var b = binaryString.substr(i, 8); +915 var x = parseInt(b, 2).toString(16); +916 if (x.length == 1) x = '0' + x; +917 h += x; +918 } +919 this.hTLV = null; +920 this.isModified = true; +921 this.hV = '0' + unusedBits + h; +922 }; +923 +924 /** +925 * set ASN.1 TLV value(V) by an array of boolean +926 * @name setByBooleanArray +927 * @memberOf KJUR.asn1.DERBitString +928 * @function +929 * @param {array} booleanArray array of boolean (ex. [true, false, true]) +930 * @description +931 * NOTE: Trailing falses will be ignored. +932 */ +933 this.setByBooleanArray = function(booleanArray) { +934 var s = ''; +935 for (var i = 0; i < booleanArray.length; i++) { +936 if (booleanArray[i] == true) { +937 s += '1'; +938 } else { +939 s += '0'; +940 } +941 } +942 this.setByBinaryString(s); +943 }; +944 +945 /** +946 * generate an array of false with specified length +947 * @name newFalseArray +948 * @memberOf KJUR.asn1.DERBitString +949 * @function +950 * @param {Integer} nLength length of array to generate +951 * @return {array} array of boolean faluse +952 * @description +953 * This static method may be useful to initialize boolean array. +954 */ +955 this.newFalseArray = function(nLength) { +956 var a = new Array(nLength); +957 for (var i = 0; i < nLength; i++) { +958 a[i] = false; +959 } +960 return a; +961 }; +962 +963 this.getFreshValueHex = function() { +964 return this.hV; +965 }; +966 +967 if (typeof params != "undefined") { +968 if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) { +969 this.setHexValueIncludingUnusedBits(params); +970 } else if (typeof params['hex'] != "undefined") { +971 this.setHexValueIncludingUnusedBits(params['hex']); +972 } else if (typeof params['bin'] != "undefined") { +973 this.setByBinaryString(params['bin']); +974 } else if (typeof params['array'] != "undefined") { +975 this.setByBooleanArray(params['array']); +976 } +977 } +978 }; +979 YAHOO.lang.extend(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object); +980 +981 // ******************************************************************** +982 /** +983 * class for ASN.1 DER OctetString<br/> +984 * @name KJUR.asn1.DEROctetString +985 * @class class for ASN.1 DER OctetString +986 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +987 * @extends KJUR.asn1.DERAbstractString +988 * @description +989 * This class provides ASN.1 OctetString simple type.<br/> +990 * Supported "params" attributes are: +991 * <ul> +992 * <li>str - to set a string as a value</li> +993 * <li>hex - to set a hexadecimal string as a value</li> +994 * <li>obj - to set a encapsulated ASN.1 value by JSON object +995 * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li> +996 * </ul> +997 * NOTE: A parameter 'obj' have been supported +998 * for "OCTET STRING, encapsulates" structure. +999 * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25). +1000 * @see KJUR.asn1.DERAbstractString - superclass +1001 * @example +1002 * // default constructor +1003 * o = new KJUR.asn1.DEROctetString(); +1004 * // initialize with string +1005 * o = new KJUR.asn1.DEROctetString({str: "aaa"}); +1006 * // initialize with hexadecimal string +1007 * o = new KJUR.asn1.DEROctetString({hex: "616161"}); +1008 * // initialize with ASN1Util.newObject argument +1009 * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +1010 * // above generates a ASN.1 data like this: +1011 * // OCTET STRING, encapsulates { +1012 * // SEQUENCE { +1013 * // INTEGER 3 +1014 * // PrintableString 'aaa' +1015 * // } +1016 * // } +1017 */ +1018 KJUR.asn1.DEROctetString = function(params) { +1019 if (params !== undefined && typeof params.obj !== "undefined") { +1020 var o = KJUR.asn1.ASN1Util.newObject(params.obj); +1021 params.hex = o.getEncodedHex(); +1022 } +1023 KJUR.asn1.DEROctetString.superclass.constructor.call(this, params); +1024 this.hT = "04"; +1025 }; +1026 YAHOO.lang.extend(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString); +1027 +1028 // ******************************************************************** +1029 /** +1030 * class for ASN.1 DER Null +1031 * @name KJUR.asn1.DERNull +1032 * @class class for ASN.1 DER Null +1033 * @extends KJUR.asn1.ASN1Object +1034 * @description +1035 * @see KJUR.asn1.ASN1Object - superclass +1036 */ +1037 KJUR.asn1.DERNull = function() { +1038 KJUR.asn1.DERNull.superclass.constructor.call(this); +1039 this.hT = "05"; +1040 this.hTLV = "0500"; +1041 }; +1042 YAHOO.lang.extend(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object); 1043 -1044 /** -1045 * set value by a OID string -1046 * @name setValueOidString -1047 * @memberOf KJUR.asn1.DERObjectIdentifier -1048 * @function -1049 * @param {String} oidString OID string (ex. 2.5.4.13) -1050 */ -1051 this.setValueOidString = function(oidString) { -1052 if (! oidString.match(/^[0-9.]+$/)) { -1053 throw "malformed oid string: " + oidString; -1054 } -1055 var h = ''; -1056 var a = oidString.split('.'); -1057 var i0 = parseInt(a[0]) * 40 + parseInt(a[1]); -1058 h += itox(i0); -1059 a.splice(0, 2); -1060 for (var i = 0; i < a.length; i++) { -1061 h += roidtox(a[i]); -1062 } -1063 this.hTLV = null; -1064 this.isModified = true; -1065 this.s = null; -1066 this.hV = h; -1067 }; -1068 -1069 /** -1070 * set value by a OID name -1071 * @name setValueName -1072 * @memberOf KJUR.asn1.DERObjectIdentifier -1073 * @function -1074 * @param {String} oidName OID name (ex. 'serverAuth') -1075 * @since 1.0.1 -1076 * @description -1077 * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'. -1078 * Otherwise raise error. -1079 */ -1080 this.setValueName = function(oidName) { -1081 if (typeof KJUR.asn1.x509.OID.name2oidList[oidName] != "undefined") { -1082 var oid = KJUR.asn1.x509.OID.name2oidList[oidName]; -1083 this.setValueOidString(oid); -1084 } else { -1085 throw "DERObjectIdentifier oidName undefined: " + oidName; -1086 } -1087 }; -1088 -1089 this.getFreshValueHex = function() { -1090 return this.hV; -1091 }; -1092 -1093 if (typeof params != "undefined") { -1094 if (typeof params == "string" && params.match(/^[0-2].[0-9.]+$/)) { -1095 this.setValueOidString(params); -1096 } else if (KJUR.asn1.x509.OID.name2oidList[params] !== undefined) { -1097 this.setValueOidString(KJUR.asn1.x509.OID.name2oidList[params]); -1098 } else if (typeof params['oid'] != "undefined") { -1099 this.setValueOidString(params['oid']); -1100 } else if (typeof params['hex'] != "undefined") { -1101 this.setValueHex(params['hex']); -1102 } else if (typeof params['name'] != "undefined") { -1103 this.setValueName(params['name']); -1104 } -1105 } -1106 }; -1107 YAHOO.lang.extend(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object); -1108 -1109 // ******************************************************************** -1110 /** -1111 * class for ASN.1 DER Enumerated -1112 * @name KJUR.asn1.DEREnumerated -1113 * @class class for ASN.1 DER Enumerated -1114 * @extends KJUR.asn1.ASN1Object -1115 * @description -1116 * <br/> -1117 * As for argument 'params' for constructor, you can specify one of -1118 * following properties: -1119 * <ul> -1120 * <li>int - specify initial ASN.1 value(V) by integer value</li> -1121 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1122 * </ul> -1123 * NOTE: 'params' can be omitted. -1124 */ -1125 KJUR.asn1.DEREnumerated = function(params) { -1126 KJUR.asn1.DEREnumerated.superclass.constructor.call(this); -1127 this.hT = "0a"; -1128 -1129 /** -1130 * set value by Tom Wu's BigInteger object -1131 * @name setByBigInteger -1132 * @memberOf KJUR.asn1.DEREnumerated -1133 * @function -1134 * @param {BigInteger} bigIntegerValue to set -1135 */ -1136 this.setByBigInteger = function(bigIntegerValue) { -1137 this.hTLV = null; -1138 this.isModified = true; -1139 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); -1140 }; -1141 -1142 /** -1143 * set value by integer value -1144 * @name setByInteger -1145 * @memberOf KJUR.asn1.DEREnumerated -1146 * @function -1147 * @param {Integer} integer value to set -1148 */ -1149 this.setByInteger = function(intValue) { -1150 var bi = new BigInteger(String(intValue), 10); -1151 this.setByBigInteger(bi); -1152 }; -1153 -1154 /** -1155 * set value by integer value -1156 * @name setValueHex -1157 * @memberOf KJUR.asn1.DEREnumerated -1158 * @function -1159 * @param {String} hexadecimal string of integer value -1160 * @description -1161 * <br/> -1162 * NOTE: Value shall be represented by minimum octet length of -1163 * two's complement representation. -1164 * @example -1165 * new KJUR.asn1.DEREnumerated(123); -1166 * new KJUR.asn1.DEREnumerated({'int': 123}); -1167 * new KJUR.asn1.DEREnumerated({'hex': '1fad'}); -1168 */ -1169 this.setValueHex = function(newHexString) { -1170 this.hV = newHexString; -1171 }; -1172 -1173 this.getFreshValueHex = function() { -1174 return this.hV; -1175 }; -1176 -1177 if (typeof params != "undefined") { -1178 if (typeof params['int'] != "undefined") { -1179 this.setByInteger(params['int']); -1180 } else if (typeof params == "number") { -1181 this.setByInteger(params); -1182 } else if (typeof params['hex'] != "undefined") { -1183 this.setValueHex(params['hex']); -1184 } -1185 } -1186 }; -1187 YAHOO.lang.extend(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object); -1188 -1189 // ******************************************************************** -1190 /** -1191 * class for ASN.1 DER UTF8String -1192 * @name KJUR.asn1.DERUTF8String -1193 * @class class for ASN.1 DER UTF8String -1194 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1195 * @extends KJUR.asn1.DERAbstractString -1196 * @description -1197 * @see KJUR.asn1.DERAbstractString - superclass -1198 */ -1199 KJUR.asn1.DERUTF8String = function(params) { -1200 KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params); -1201 this.hT = "0c"; -1202 }; -1203 YAHOO.lang.extend(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString); -1204 -1205 // ******************************************************************** -1206 /** -1207 * class for ASN.1 DER NumericString -1208 * @name KJUR.asn1.DERNumericString -1209 * @class class for ASN.1 DER NumericString -1210 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1211 * @extends KJUR.asn1.DERAbstractString -1212 * @description -1213 * @see KJUR.asn1.DERAbstractString - superclass -1214 */ -1215 KJUR.asn1.DERNumericString = function(params) { -1216 KJUR.asn1.DERNumericString.superclass.constructor.call(this, params); -1217 this.hT = "12"; -1218 }; -1219 YAHOO.lang.extend(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString); -1220 -1221 // ******************************************************************** -1222 /** -1223 * class for ASN.1 DER PrintableString -1224 * @name KJUR.asn1.DERPrintableString -1225 * @class class for ASN.1 DER PrintableString -1226 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1227 * @extends KJUR.asn1.DERAbstractString -1228 * @description -1229 * @see KJUR.asn1.DERAbstractString - superclass -1230 */ -1231 KJUR.asn1.DERPrintableString = function(params) { -1232 KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params); -1233 this.hT = "13"; -1234 }; -1235 YAHOO.lang.extend(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString); -1236 -1237 // ******************************************************************** -1238 /** -1239 * class for ASN.1 DER TeletexString -1240 * @name KJUR.asn1.DERTeletexString -1241 * @class class for ASN.1 DER TeletexString -1242 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1243 * @extends KJUR.asn1.DERAbstractString -1244 * @description -1245 * @see KJUR.asn1.DERAbstractString - superclass -1246 */ -1247 KJUR.asn1.DERTeletexString = function(params) { -1248 KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params); -1249 this.hT = "14"; -1250 }; -1251 YAHOO.lang.extend(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString); -1252 -1253 // ******************************************************************** -1254 /** -1255 * class for ASN.1 DER IA5String -1256 * @name KJUR.asn1.DERIA5String -1257 * @class class for ASN.1 DER IA5String -1258 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1259 * @extends KJUR.asn1.DERAbstractString -1260 * @description -1261 * @see KJUR.asn1.DERAbstractString - superclass -1262 */ -1263 KJUR.asn1.DERIA5String = function(params) { -1264 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); -1265 this.hT = "16"; -1266 }; -1267 YAHOO.lang.extend(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString); -1268 -1269 // ******************************************************************** -1270 /** -1271 * class for ASN.1 DER UTCTime -1272 * @name KJUR.asn1.DERUTCTime -1273 * @class class for ASN.1 DER UTCTime -1274 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) -1275 * @extends KJUR.asn1.DERAbstractTime -1276 * @description -1277 * <br/> -1278 * As for argument 'params' for constructor, you can specify one of -1279 * following properties: -1280 * <ul> -1281 * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li> -1282 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1283 * <li>date - specify Date object.</li> -1284 * </ul> -1285 * NOTE: 'params' can be omitted. -1286 * <h4>EXAMPLES</h4> -1287 * @example -1288 * var d1 = new KJUR.asn1.DERUTCTime(); -1289 * d1.setString('130430125959Z'); -1290 * -1291 * var d2 = new KJUR.asn1.DERUTCTime({'str': '130430125959Z'}); -1292 * var d3 = new KJUR.asn1.DERUTCTime({'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))}); -1293 * var d4 = new KJUR.asn1.DERUTCTime('130430125959Z'); -1294 */ -1295 KJUR.asn1.DERUTCTime = function(params) { -1296 KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params); -1297 this.hT = "17"; -1298 -1299 /** -1300 * set value by a Date object -1301 * @name setByDate -1302 * @memberOf KJUR.asn1.DERUTCTime -1303 * @function -1304 * @param {Date} dateObject Date object to set ASN.1 value(V) -1305 */ -1306 this.setByDate = function(dateObject) { -1307 this.hTLV = null; -1308 this.isModified = true; -1309 this.date = dateObject; -1310 this.s = this.formatDate(this.date, 'utc'); -1311 this.hV = stohex(this.s); -1312 }; -1313 -1314 this.getFreshValueHex = function() { -1315 if (typeof this.date == "undefined" && typeof this.s == "undefined") { -1316 this.date = new Date(); -1317 this.s = this.formatDate(this.date, 'utc'); -1318 this.hV = stohex(this.s); -1319 } -1320 return this.hV; -1321 }; -1322 -1323 if (params !== undefined) { -1324 if (params.str !== undefined) { -1325 this.setString(params.str); -1326 } else if (typeof params == "string" && params.match(/^[0-9]{12}Z$/)) { -1327 this.setString(params); -1328 } else if (params.hex !== undefined) { -1329 this.setStringHex(params.hex); -1330 } else if (params.date !== undefined) { -1331 this.setByDate(params.date); -1332 } -1333 } -1334 }; -1335 YAHOO.lang.extend(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime); -1336 -1337 // ******************************************************************** -1338 /** -1339 * class for ASN.1 DER GeneralizedTime -1340 * @name KJUR.asn1.DERGeneralizedTime -1341 * @class class for ASN.1 DER GeneralizedTime -1342 * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'}) -1343 * @property {Boolean} withMillis flag to show milliseconds or not -1344 * @extends KJUR.asn1.DERAbstractTime -1345 * @description -1346 * <br/> -1347 * As for argument 'params' for constructor, you can specify one of -1348 * following properties: -1349 * <ul> -1350 * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li> -1351 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1352 * <li>date - specify Date object.</li> -1353 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> -1354 * </ul> -1355 * NOTE1: 'params' can be omitted. -1356 * NOTE2: 'withMillis' property is supported from asn1 1.0.6. -1357 */ -1358 KJUR.asn1.DERGeneralizedTime = function(params) { -1359 KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params); -1360 this.hT = "18"; -1361 this.withMillis = false; -1362 -1363 /** -1364 * set value by a Date object -1365 * @name setByDate -1366 * @memberOf KJUR.asn1.DERGeneralizedTime -1367 * @function -1368 * @param {Date} dateObject Date object to set ASN.1 value(V) -1369 * @example -1370 * When you specify UTC time, use 'Date.UTC' method like this:<br/> -1371 * var o = new DERUTCTime(); -1372 * var date = new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)); #2015JAN31 23:59:59 -1373 * o.setByDate(date); -1374 */ -1375 this.setByDate = function(dateObject) { -1376 this.hTLV = null; -1377 this.isModified = true; -1378 this.date = dateObject; -1379 this.s = this.formatDate(this.date, 'gen', this.withMillis); -1380 this.hV = stohex(this.s); -1381 }; -1382 -1383 this.getFreshValueHex = function() { -1384 if (this.date === undefined && this.s === undefined) { -1385 this.date = new Date(); -1386 this.s = this.formatDate(this.date, 'gen', this.withMillis); -1387 this.hV = stohex(this.s); -1388 } -1389 return this.hV; -1390 }; -1391 -1392 if (params !== undefined) { -1393 if (params.str !== undefined) { -1394 this.setString(params.str); -1395 } else if (typeof params == "string" && params.match(/^[0-9]{14}Z$/)) { -1396 this.setString(params); -1397 } else if (params.hex !== undefined) { -1398 this.setStringHex(params.hex); -1399 } else if (params.date !== undefined) { -1400 this.setByDate(params.date); -1401 } -1402 if (params.millis === true) { -1403 this.withMillis = true; -1404 } -1405 } -1406 }; -1407 YAHOO.lang.extend(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime); -1408 -1409 // ******************************************************************** -1410 /** -1411 * class for ASN.1 DER Sequence -1412 * @name KJUR.asn1.DERSequence -1413 * @class class for ASN.1 DER Sequence -1414 * @extends KJUR.asn1.DERAbstractStructured -1415 * @description -1416 * <br/> -1417 * As for argument 'params' for constructor, you can specify one of -1418 * following properties: -1419 * <ul> -1420 * <li>array - specify array of ASN1Object to set elements of content</li> -1421 * </ul> -1422 * NOTE: 'params' can be omitted. -1423 */ -1424 KJUR.asn1.DERSequence = function(params) { -1425 KJUR.asn1.DERSequence.superclass.constructor.call(this, params); -1426 this.hT = "30"; -1427 this.getFreshValueHex = function() { -1428 var h = ''; -1429 for (var i = 0; i < this.asn1Array.length; i++) { -1430 var asn1Obj = this.asn1Array[i]; -1431 h += asn1Obj.getEncodedHex(); -1432 } -1433 this.hV = h; -1434 return this.hV; -1435 }; -1436 }; -1437 YAHOO.lang.extend(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured); -1438 -1439 // ******************************************************************** -1440 /** -1441 * class for ASN.1 DER Set -1442 * @name KJUR.asn1.DERSet -1443 * @class class for ASN.1 DER Set -1444 * @extends KJUR.asn1.DERAbstractStructured -1445 * @description -1446 * <br/> -1447 * As for argument 'params' for constructor, you can specify one of -1448 * following properties: -1449 * <ul> -1450 * <li>array - specify array of ASN1Object to set elements of content</li> -1451 * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li> -1452 * </ul> -1453 * NOTE1: 'params' can be omitted.<br/> -1454 * NOTE2: sortflag is supported since 1.0.5. -1455 */ -1456 KJUR.asn1.DERSet = function(params) { -1457 KJUR.asn1.DERSet.superclass.constructor.call(this, params); -1458 this.hT = "31"; -1459 this.sortFlag = true; // item shall be sorted only in ASN.1 DER -1460 this.getFreshValueHex = function() { -1461 var a = new Array(); -1462 for (var i = 0; i < this.asn1Array.length; i++) { -1463 var asn1Obj = this.asn1Array[i]; -1464 a.push(asn1Obj.getEncodedHex()); -1465 } -1466 if (this.sortFlag == true) a.sort(); -1467 this.hV = a.join(''); -1468 return this.hV; -1469 }; -1470 -1471 if (typeof params != "undefined") { -1472 if (typeof params.sortflag != "undefined" && -1473 params.sortflag == false) -1474 this.sortFlag = false; -1475 } -1476 }; -1477 YAHOO.lang.extend(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured); -1478 -1479 // ******************************************************************** -1480 /** -1481 * class for ASN.1 DER TaggedObject -1482 * @name KJUR.asn1.DERTaggedObject -1483 * @class class for ASN.1 DER TaggedObject -1484 * @extends KJUR.asn1.ASN1Object -1485 * @description -1486 * <br/> -1487 * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object. -1488 * For example, if you find '[1]' tag in a ASN.1 dump, -1489 * 'tagNoHex' will be 'a1'. -1490 * <br/> -1491 * As for optional argument 'params' for constructor, you can specify *ANY* of -1492 * following properties: -1493 * <ul> -1494 * <li>explicit - specify true if this is explicit tag otherwise false -1495 * (default is 'true').</li> -1496 * <li>tag - specify tag (default is 'a0' which means [0])</li> -1497 * <li>obj - specify ASN1Object which is tagged</li> -1498 * </ul> -1499 * @example -1500 * d1 = new KJUR.asn1.DERUTF8String({'str':'a'}); -1501 * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1}); -1502 * hex = d2.getEncodedHex(); -1503 */ -1504 KJUR.asn1.DERTaggedObject = function(params) { -1505 KJUR.asn1.DERTaggedObject.superclass.constructor.call(this); -1506 this.hT = "a0"; -1507 this.hV = ''; -1508 this.isExplicit = true; -1509 this.asn1Object = null; -1510 -1511 /** -1512 * set value by an ASN1Object -1513 * @name setString -1514 * @memberOf KJUR.asn1.DERTaggedObject -1515 * @function -1516 * @param {Boolean} isExplicitFlag flag for explicit/implicit tag -1517 * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag -1518 * @param {ASN1Object} asn1Object ASN.1 to encapsulate -1519 */ -1520 this.setASN1Object = function(isExplicitFlag, tagNoHex, asn1Object) { -1521 this.hT = tagNoHex; -1522 this.isExplicit = isExplicitFlag; -1523 this.asn1Object = asn1Object; -1524 if (this.isExplicit) { -1525 this.hV = this.asn1Object.getEncodedHex(); -1526 this.hTLV = null; -1527 this.isModified = true; -1528 } else { -1529 this.hV = null; -1530 this.hTLV = asn1Object.getEncodedHex(); -1531 this.hTLV = this.hTLV.replace(/^../, tagNoHex); -1532 this.isModified = false; -1533 } -1534 }; +1044 // ******************************************************************** +1045 /** +1046 * class for ASN.1 DER ObjectIdentifier +1047 * @name KJUR.asn1.DERObjectIdentifier +1048 * @class class for ASN.1 DER ObjectIdentifier +1049 * @param {Array} params associative array of parameters (ex. {'oid': '2.5.4.5'}) +1050 * @extends KJUR.asn1.ASN1Object +1051 * @description +1052 * <br/> +1053 * As for argument 'params' for constructor, you can specify one of +1054 * following properties: +1055 * <ul> +1056 * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li> +1057 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1058 * </ul> +1059 * NOTE: 'params' can be omitted. +1060 */ +1061 KJUR.asn1.DERObjectIdentifier = function(params) { +1062 var itox = function(i) { +1063 var h = i.toString(16); +1064 if (h.length == 1) h = '0' + h; +1065 return h; +1066 }; +1067 var roidtox = function(roid) { +1068 var h = ''; +1069 var bi = new BigInteger(roid, 10); +1070 var b = bi.toString(2); +1071 var padLen = 7 - b.length % 7; +1072 if (padLen == 7) padLen = 0; +1073 var bPad = ''; +1074 for (var i = 0; i < padLen; i++) bPad += '0'; +1075 b = bPad + b; +1076 for (var i = 0; i < b.length - 1; i += 7) { +1077 var b8 = b.substr(i, 7); +1078 if (i != b.length - 7) b8 = '1' + b8; +1079 h += itox(parseInt(b8, 2)); +1080 } +1081 return h; +1082 } +1083 +1084 KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this); +1085 this.hT = "06"; +1086 +1087 /** +1088 * set value by a hexadecimal string +1089 * @name setValueHex +1090 * @memberOf KJUR.asn1.DERObjectIdentifier +1091 * @function +1092 * @param {String} newHexString hexadecimal value of OID bytes +1093 */ +1094 this.setValueHex = function(newHexString) { +1095 this.hTLV = null; +1096 this.isModified = true; +1097 this.s = null; +1098 this.hV = newHexString; +1099 }; +1100 +1101 /** +1102 * set value by a OID string +1103 * @name setValueOidString +1104 * @memberOf KJUR.asn1.DERObjectIdentifier +1105 * @function +1106 * @param {String} oidString OID string (ex. 2.5.4.13) +1107 */ +1108 this.setValueOidString = function(oidString) { +1109 if (! oidString.match(/^[0-9.]+$/)) { +1110 throw "malformed oid string: " + oidString; +1111 } +1112 var h = ''; +1113 var a = oidString.split('.'); +1114 var i0 = parseInt(a[0]) * 40 + parseInt(a[1]); +1115 h += itox(i0); +1116 a.splice(0, 2); +1117 for (var i = 0; i < a.length; i++) { +1118 h += roidtox(a[i]); +1119 } +1120 this.hTLV = null; +1121 this.isModified = true; +1122 this.s = null; +1123 this.hV = h; +1124 }; +1125 +1126 /** +1127 * set value by a OID name +1128 * @name setValueName +1129 * @memberOf KJUR.asn1.DERObjectIdentifier +1130 * @function +1131 * @param {String} oidName OID name (ex. 'serverAuth') +1132 * @since 1.0.1 +1133 * @description +1134 * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'. +1135 * Otherwise raise error. +1136 */ +1137 this.setValueName = function(oidName) { +1138 if (typeof KJUR.asn1.x509.OID.name2oidList[oidName] != "undefined") { +1139 var oid = KJUR.asn1.x509.OID.name2oidList[oidName]; +1140 this.setValueOidString(oid); +1141 } else { +1142 throw "DERObjectIdentifier oidName undefined: " + oidName; +1143 } +1144 }; +1145 +1146 this.getFreshValueHex = function() { +1147 return this.hV; +1148 }; +1149 +1150 if (typeof params != "undefined") { +1151 if (typeof params == "string" && params.match(/^[0-2].[0-9.]+$/)) { +1152 this.setValueOidString(params); +1153 } else if (KJUR.asn1.x509.OID.name2oidList[params] !== undefined) { +1154 this.setValueOidString(KJUR.asn1.x509.OID.name2oidList[params]); +1155 } else if (typeof params['oid'] != "undefined") { +1156 this.setValueOidString(params['oid']); +1157 } else if (typeof params['hex'] != "undefined") { +1158 this.setValueHex(params['hex']); +1159 } else if (typeof params['name'] != "undefined") { +1160 this.setValueName(params['name']); +1161 } +1162 } +1163 }; +1164 YAHOO.lang.extend(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object); +1165 +1166 // ******************************************************************** +1167 /** +1168 * class for ASN.1 DER Enumerated +1169 * @name KJUR.asn1.DEREnumerated +1170 * @class class for ASN.1 DER Enumerated +1171 * @extends KJUR.asn1.ASN1Object +1172 * @description +1173 * <br/> +1174 * As for argument 'params' for constructor, you can specify one of +1175 * following properties: +1176 * <ul> +1177 * <li>int - specify initial ASN.1 value(V) by integer value</li> +1178 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1179 * </ul> +1180 * NOTE: 'params' can be omitted. +1181 */ +1182 KJUR.asn1.DEREnumerated = function(params) { +1183 KJUR.asn1.DEREnumerated.superclass.constructor.call(this); +1184 this.hT = "0a"; +1185 +1186 /** +1187 * set value by Tom Wu's BigInteger object +1188 * @name setByBigInteger +1189 * @memberOf KJUR.asn1.DEREnumerated +1190 * @function +1191 * @param {BigInteger} bigIntegerValue to set +1192 */ +1193 this.setByBigInteger = function(bigIntegerValue) { +1194 this.hTLV = null; +1195 this.isModified = true; +1196 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); +1197 }; +1198 +1199 /** +1200 * set value by integer value +1201 * @name setByInteger +1202 * @memberOf KJUR.asn1.DEREnumerated +1203 * @function +1204 * @param {Integer} integer value to set +1205 */ +1206 this.setByInteger = function(intValue) { +1207 var bi = new BigInteger(String(intValue), 10); +1208 this.setByBigInteger(bi); +1209 }; +1210 +1211 /** +1212 * set value by integer value +1213 * @name setValueHex +1214 * @memberOf KJUR.asn1.DEREnumerated +1215 * @function +1216 * @param {String} hexadecimal string of integer value +1217 * @description +1218 * <br/> +1219 * NOTE: Value shall be represented by minimum octet length of +1220 * two's complement representation. +1221 * @example +1222 * new KJUR.asn1.DEREnumerated(123); +1223 * new KJUR.asn1.DEREnumerated({int: 123}); +1224 * new KJUR.asn1.DEREnumerated({hex: '1fad'}); +1225 */ +1226 this.setValueHex = function(newHexString) { +1227 this.hV = newHexString; +1228 }; +1229 +1230 this.getFreshValueHex = function() { +1231 return this.hV; +1232 }; +1233 +1234 if (typeof params != "undefined") { +1235 if (typeof params['int'] != "undefined") { +1236 this.setByInteger(params['int']); +1237 } else if (typeof params == "number") { +1238 this.setByInteger(params); +1239 } else if (typeof params['hex'] != "undefined") { +1240 this.setValueHex(params['hex']); +1241 } +1242 } +1243 }; +1244 YAHOO.lang.extend(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object); +1245 +1246 // ******************************************************************** +1247 /** +1248 * class for ASN.1 DER UTF8String +1249 * @name KJUR.asn1.DERUTF8String +1250 * @class class for ASN.1 DER UTF8String +1251 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1252 * @extends KJUR.asn1.DERAbstractString +1253 * @description +1254 * @see KJUR.asn1.DERAbstractString - superclass +1255 */ +1256 KJUR.asn1.DERUTF8String = function(params) { +1257 KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params); +1258 this.hT = "0c"; +1259 }; +1260 YAHOO.lang.extend(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString); +1261 +1262 // ******************************************************************** +1263 /** +1264 * class for ASN.1 DER NumericString +1265 * @name KJUR.asn1.DERNumericString +1266 * @class class for ASN.1 DER NumericString +1267 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1268 * @extends KJUR.asn1.DERAbstractString +1269 * @description +1270 * @see KJUR.asn1.DERAbstractString - superclass +1271 */ +1272 KJUR.asn1.DERNumericString = function(params) { +1273 KJUR.asn1.DERNumericString.superclass.constructor.call(this, params); +1274 this.hT = "12"; +1275 }; +1276 YAHOO.lang.extend(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString); +1277 +1278 // ******************************************************************** +1279 /** +1280 * class for ASN.1 DER PrintableString +1281 * @name KJUR.asn1.DERPrintableString +1282 * @class class for ASN.1 DER PrintableString +1283 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1284 * @extends KJUR.asn1.DERAbstractString +1285 * @description +1286 * @see KJUR.asn1.DERAbstractString - superclass +1287 */ +1288 KJUR.asn1.DERPrintableString = function(params) { +1289 KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params); +1290 this.hT = "13"; +1291 }; +1292 YAHOO.lang.extend(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString); +1293 +1294 // ******************************************************************** +1295 /** +1296 * class for ASN.1 DER TeletexString +1297 * @name KJUR.asn1.DERTeletexString +1298 * @class class for ASN.1 DER TeletexString +1299 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1300 * @extends KJUR.asn1.DERAbstractString +1301 * @description +1302 * @see KJUR.asn1.DERAbstractString - superclass +1303 */ +1304 KJUR.asn1.DERTeletexString = function(params) { +1305 KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params); +1306 this.hT = "14"; +1307 }; +1308 YAHOO.lang.extend(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString); +1309 +1310 // ******************************************************************** +1311 /** +1312 * class for ASN.1 DER IA5String +1313 * @name KJUR.asn1.DERIA5String +1314 * @class class for ASN.1 DER IA5String +1315 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1316 * @extends KJUR.asn1.DERAbstractString +1317 * @description +1318 * @see KJUR.asn1.DERAbstractString - superclass +1319 */ +1320 KJUR.asn1.DERIA5String = function(params) { +1321 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); +1322 this.hT = "16"; +1323 }; +1324 YAHOO.lang.extend(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString); +1325 +1326 // ******************************************************************** +1327 /** +1328 * class for ASN.1 DER UTCTime +1329 * @name KJUR.asn1.DERUTCTime +1330 * @class class for ASN.1 DER UTCTime +1331 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) +1332 * @extends KJUR.asn1.DERAbstractTime +1333 * @description +1334 * <br/> +1335 * As for argument 'params' for constructor, you can specify one of +1336 * following properties: +1337 * <ul> +1338 * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li> +1339 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1340 * <li>date - specify Date object.</li> +1341 * </ul> +1342 * NOTE: 'params' can be omitted. +1343 * <h4>EXAMPLES</h4> +1344 * @example +1345 * var d1 = new KJUR.asn1.DERUTCTime(); +1346 * d1.setString('130430125959Z'); +1347 * +1348 * var d2 = new KJUR.asn1.DERUTCTime({'str': '130430125959Z'}); +1349 * var d3 = new KJUR.asn1.DERUTCTime({'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))}); +1350 * var d4 = new KJUR.asn1.DERUTCTime('130430125959Z'); +1351 */ +1352 KJUR.asn1.DERUTCTime = function(params) { +1353 KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params); +1354 this.hT = "17"; +1355 +1356 /** +1357 * set value by a Date object +1358 * @name setByDate +1359 * @memberOf KJUR.asn1.DERUTCTime +1360 * @function +1361 * @param {Date} dateObject Date object to set ASN.1 value(V) +1362 */ +1363 this.setByDate = function(dateObject) { +1364 this.hTLV = null; +1365 this.isModified = true; +1366 this.date = dateObject; +1367 this.s = this.formatDate(this.date, 'utc'); +1368 this.hV = stohex(this.s); +1369 }; +1370 +1371 this.getFreshValueHex = function() { +1372 if (typeof this.date == "undefined" && typeof this.s == "undefined") { +1373 this.date = new Date(); +1374 this.s = this.formatDate(this.date, 'utc'); +1375 this.hV = stohex(this.s); +1376 } +1377 return this.hV; +1378 }; +1379 +1380 if (params !== undefined) { +1381 if (params.str !== undefined) { +1382 this.setString(params.str); +1383 } else if (typeof params == "string" && params.match(/^[0-9]{12}Z$/)) { +1384 this.setString(params); +1385 } else if (params.hex !== undefined) { +1386 this.setStringHex(params.hex); +1387 } else if (params.date !== undefined) { +1388 this.setByDate(params.date); +1389 } +1390 } +1391 }; +1392 YAHOO.lang.extend(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime); +1393 +1394 // ******************************************************************** +1395 /** +1396 * class for ASN.1 DER GeneralizedTime +1397 * @name KJUR.asn1.DERGeneralizedTime +1398 * @class class for ASN.1 DER GeneralizedTime +1399 * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'}) +1400 * @property {Boolean} withMillis flag to show milliseconds or not +1401 * @extends KJUR.asn1.DERAbstractTime +1402 * @description +1403 * <br/> +1404 * As for argument 'params' for constructor, you can specify one of +1405 * following properties: +1406 * <ul> +1407 * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li> +1408 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1409 * <li>date - specify Date object.</li> +1410 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> +1411 * </ul> +1412 * NOTE1: 'params' can be omitted. +1413 * NOTE2: 'withMillis' property is supported from asn1 1.0.6. +1414 */ +1415 KJUR.asn1.DERGeneralizedTime = function(params) { +1416 KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params); +1417 this.hT = "18"; +1418 this.withMillis = false; +1419 +1420 /** +1421 * set value by a Date object +1422 * @name setByDate +1423 * @memberOf KJUR.asn1.DERGeneralizedTime +1424 * @function +1425 * @param {Date} dateObject Date object to set ASN.1 value(V) +1426 * @example +1427 * When you specify UTC time, use 'Date.UTC' method like this:<br/> +1428 * var o = new DERUTCTime(); +1429 * var date = new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)); #2015JAN31 23:59:59 +1430 * o.setByDate(date); +1431 */ +1432 this.setByDate = function(dateObject) { +1433 this.hTLV = null; +1434 this.isModified = true; +1435 this.date = dateObject; +1436 this.s = this.formatDate(this.date, 'gen', this.withMillis); +1437 this.hV = stohex(this.s); +1438 }; +1439 +1440 this.getFreshValueHex = function() { +1441 if (this.date === undefined && this.s === undefined) { +1442 this.date = new Date(); +1443 this.s = this.formatDate(this.date, 'gen', this.withMillis); +1444 this.hV = stohex(this.s); +1445 } +1446 return this.hV; +1447 }; +1448 +1449 if (params !== undefined) { +1450 if (params.str !== undefined) { +1451 this.setString(params.str); +1452 } else if (typeof params == "string" && params.match(/^[0-9]{14}Z$/)) { +1453 this.setString(params); +1454 } else if (params.hex !== undefined) { +1455 this.setStringHex(params.hex); +1456 } else if (params.date !== undefined) { +1457 this.setByDate(params.date); +1458 } +1459 if (params.millis === true) { +1460 this.withMillis = true; +1461 } +1462 } +1463 }; +1464 YAHOO.lang.extend(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime); +1465 +1466 // ******************************************************************** +1467 /** +1468 * class for ASN.1 DER Sequence +1469 * @name KJUR.asn1.DERSequence +1470 * @class class for ASN.1 DER Sequence +1471 * @extends KJUR.asn1.DERAbstractStructured +1472 * @description +1473 * <br/> +1474 * As for argument 'params' for constructor, you can specify one of +1475 * following properties: +1476 * <ul> +1477 * <li>array - specify array of ASN1Object to set elements of content</li> +1478 * </ul> +1479 * NOTE: 'params' can be omitted. +1480 */ +1481 KJUR.asn1.DERSequence = function(params) { +1482 KJUR.asn1.DERSequence.superclass.constructor.call(this, params); +1483 this.hT = "30"; +1484 this.getFreshValueHex = function() { +1485 var h = ''; +1486 for (var i = 0; i < this.asn1Array.length; i++) { +1487 var asn1Obj = this.asn1Array[i]; +1488 h += asn1Obj.getEncodedHex(); +1489 } +1490 this.hV = h; +1491 return this.hV; +1492 }; +1493 }; +1494 YAHOO.lang.extend(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured); +1495 +1496 // ******************************************************************** +1497 /** +1498 * class for ASN.1 DER Set +1499 * @name KJUR.asn1.DERSet +1500 * @class class for ASN.1 DER Set +1501 * @extends KJUR.asn1.DERAbstractStructured +1502 * @description +1503 * <br/> +1504 * As for argument 'params' for constructor, you can specify one of +1505 * following properties: +1506 * <ul> +1507 * <li>array - specify array of ASN1Object to set elements of content</li> +1508 * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li> +1509 * </ul> +1510 * NOTE1: 'params' can be omitted.<br/> +1511 * NOTE2: sortflag is supported since 1.0.5. +1512 */ +1513 KJUR.asn1.DERSet = function(params) { +1514 KJUR.asn1.DERSet.superclass.constructor.call(this, params); +1515 this.hT = "31"; +1516 this.sortFlag = true; // item shall be sorted only in ASN.1 DER +1517 this.getFreshValueHex = function() { +1518 var a = new Array(); +1519 for (var i = 0; i < this.asn1Array.length; i++) { +1520 var asn1Obj = this.asn1Array[i]; +1521 a.push(asn1Obj.getEncodedHex()); +1522 } +1523 if (this.sortFlag == true) a.sort(); +1524 this.hV = a.join(''); +1525 return this.hV; +1526 }; +1527 +1528 if (typeof params != "undefined") { +1529 if (typeof params.sortflag != "undefined" && +1530 params.sortflag == false) +1531 this.sortFlag = false; +1532 } +1533 }; +1534 YAHOO.lang.extend(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured); 1535 -1536 this.getFreshValueHex = function() { -1537 return this.hV; -1538 }; -1539 -1540 if (typeof params != "undefined") { -1541 if (typeof params['tag'] != "undefined") { -1542 this.hT = params['tag']; -1543 } -1544 if (typeof params['explicit'] != "undefined") { -1545 this.isExplicit = params['explicit']; -1546 } -1547 if (typeof params['obj'] != "undefined") { -1548 this.asn1Object = params['obj']; -1549 this.setASN1Object(this.isExplicit, this.hT, this.asn1Object); -1550 } -1551 } -1552 }; -1553 YAHOO.lang.extend(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object); -1554