人民医院前端

asn1-1.0.js 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /* asn1-1.0.13.js (c) 2013-2017 Kenji Urushima | kjur.github.com/jsrsasign/license
  2. */
  3. /*
  4. * asn1.js - ASN.1 DER encoder classes
  5. *
  6. * Copyright (c) 2013-2017 Kenji Urushima (kenji.urushima@gmail.com)
  7. *
  8. * This software is licensed under the terms of the MIT License.
  9. * https://kjur.github.io/jsrsasign/license
  10. *
  11. * The above copyright and license notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. */
  14. import { BigInteger } from "../jsbn/jsbn";
  15. import { YAHOO } from "./yahoo";
  16. /**
  17. * @fileOverview
  18. * @name asn1-1.0.js
  19. * @author Kenji Urushima kenji.urushima@gmail.com
  20. * @version asn1 1.0.13 (2017-Jun-02)
  21. * @since jsrsasign 2.1
  22. * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
  23. */
  24. /**
  25. * kjur's class library name space
  26. * <p>
  27. * This name space provides following name spaces:
  28. * <ul>
  29. * <li>{@link KJUR.asn1} - ASN.1 primitive hexadecimal encoder</li>
  30. * <li>{@link KJUR.asn1.x509} - ASN.1 structure for X.509 certificate and CRL</li>
  31. * <li>{@link KJUR.crypto} - Java Cryptographic Extension(JCE) style MessageDigest/Signature
  32. * class and utilities</li>
  33. * </ul>
  34. * </p>
  35. * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
  36. * @name KJUR
  37. * @namespace kjur's class library name space
  38. */
  39. export var KJUR = {};
  40. /**
  41. * kjur's ASN.1 class library name space
  42. * <p>
  43. * This is ITU-T X.690 ASN.1 DER encoder class library and
  44. * class structure and methods is very similar to
  45. * org.bouncycastle.asn1 package of
  46. * well known BouncyCaslte Cryptography Library.
  47. * <h4>PROVIDING ASN.1 PRIMITIVES</h4>
  48. * Here are ASN.1 DER primitive classes.
  49. * <ul>
  50. * <li>0x01 {@link KJUR.asn1.DERBoolean}</li>
  51. * <li>0x02 {@link KJUR.asn1.DERInteger}</li>
  52. * <li>0x03 {@link KJUR.asn1.DERBitString}</li>
  53. * <li>0x04 {@link KJUR.asn1.DEROctetString}</li>
  54. * <li>0x05 {@link KJUR.asn1.DERNull}</li>
  55. * <li>0x06 {@link KJUR.asn1.DERObjectIdentifier}</li>
  56. * <li>0x0a {@link KJUR.asn1.DEREnumerated}</li>
  57. * <li>0x0c {@link KJUR.asn1.DERUTF8String}</li>
  58. * <li>0x12 {@link KJUR.asn1.DERNumericString}</li>
  59. * <li>0x13 {@link KJUR.asn1.DERPrintableString}</li>
  60. * <li>0x14 {@link KJUR.asn1.DERTeletexString}</li>
  61. * <li>0x16 {@link KJUR.asn1.DERIA5String}</li>
  62. * <li>0x17 {@link KJUR.asn1.DERUTCTime}</li>
  63. * <li>0x18 {@link KJUR.asn1.DERGeneralizedTime}</li>
  64. * <li>0x30 {@link KJUR.asn1.DERSequence}</li>
  65. * <li>0x31 {@link KJUR.asn1.DERSet}</li>
  66. * </ul>
  67. * <h4>OTHER ASN.1 CLASSES</h4>
  68. * <ul>
  69. * <li>{@link KJUR.asn1.ASN1Object}</li>
  70. * <li>{@link KJUR.asn1.DERAbstractString}</li>
  71. * <li>{@link KJUR.asn1.DERAbstractTime}</li>
  72. * <li>{@link KJUR.asn1.DERAbstractStructured}</li>
  73. * <li>{@link KJUR.asn1.DERTaggedObject}</li>
  74. * </ul>
  75. * <h4>SUB NAME SPACES</h4>
  76. * <ul>
  77. * <li>{@link KJUR.asn1.cades} - CAdES long term signature format</li>
  78. * <li>{@link KJUR.asn1.cms} - Cryptographic Message Syntax</li>
  79. * <li>{@link KJUR.asn1.csr} - Certificate Signing Request (CSR/PKCS#10)</li>
  80. * <li>{@link KJUR.asn1.tsp} - RFC 3161 Timestamping Protocol Format</li>
  81. * <li>{@link KJUR.asn1.x509} - RFC 5280 X.509 certificate and CRL</li>
  82. * </ul>
  83. * </p>
  84. * NOTE: Please ignore method summary and document of this namespace.
  85. * This caused by a bug of jsdoc2.
  86. * @name KJUR.asn1
  87. * @namespace
  88. */
  89. if (typeof KJUR.asn1 == "undefined" || !KJUR.asn1)
  90. KJUR.asn1 = {};
  91. /**
  92. * ASN1 utilities class
  93. * @name KJUR.asn1.ASN1Util
  94. * @class ASN1 utilities class
  95. * @since asn1 1.0.2
  96. */
  97. KJUR.asn1.ASN1Util = new function () {
  98. this.integerToByteHex = function (i) {
  99. var h = i.toString(16);
  100. if ((h.length % 2) == 1)
  101. h = '0' + h;
  102. return h;
  103. };
  104. this.bigIntToMinTwosComplementsHex = function (bigIntegerValue) {
  105. var h = bigIntegerValue.toString(16);
  106. if (h.substr(0, 1) != '-') {
  107. if (h.length % 2 == 1) {
  108. h = '0' + h;
  109. }
  110. else {
  111. if (!h.match(/^[0-7]/)) {
  112. h = '00' + h;
  113. }
  114. }
  115. }
  116. else {
  117. var hPos = h.substr(1);
  118. var xorLen = hPos.length;
  119. if (xorLen % 2 == 1) {
  120. xorLen += 1;
  121. }
  122. else {
  123. if (!h.match(/^[0-7]/)) {
  124. xorLen += 2;
  125. }
  126. }
  127. var hMask = '';
  128. for (var i = 0; i < xorLen; i++) {
  129. hMask += 'f';
  130. }
  131. var biMask = new BigInteger(hMask, 16);
  132. var biNeg = biMask.xor(bigIntegerValue).add(BigInteger.ONE);
  133. h = biNeg.toString(16).replace(/^-/, '');
  134. }
  135. return h;
  136. };
  137. /**
  138. * get PEM string from hexadecimal data and header string
  139. * @name getPEMStringFromHex
  140. * @memberOf KJUR.asn1.ASN1Util
  141. * @function
  142. * @param {String} dataHex hexadecimal string of PEM body
  143. * @param {String} pemHeader PEM header string (ex. 'RSA PRIVATE KEY')
  144. * @return {String} PEM formatted string of input data
  145. * @description
  146. * This method converts a hexadecimal string to a PEM string with
  147. * a specified header. Its line break will be CRLF("\r\n").
  148. * @example
  149. * var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex('616161', 'RSA PRIVATE KEY');
  150. * // value of pem will be:
  151. * -----BEGIN PRIVATE KEY-----
  152. * YWFh
  153. * -----END PRIVATE KEY-----
  154. */
  155. this.getPEMStringFromHex = function (dataHex, pemHeader) {
  156. return hextopem(dataHex, pemHeader);
  157. };
  158. /**
  159. * generate ASN1Object specifed by JSON parameters
  160. * @name newObject
  161. * @memberOf KJUR.asn1.ASN1Util
  162. * @function
  163. * @param {Array} param JSON parameter to generate ASN1Object
  164. * @return {KJUR.asn1.ASN1Object} generated object
  165. * @since asn1 1.0.3
  166. * @description
  167. * generate any ASN1Object specified by JSON param
  168. * including ASN.1 primitive or structured.
  169. * Generally 'param' can be described as follows:
  170. * <blockquote>
  171. * {TYPE-OF-ASNOBJ: ASN1OBJ-PARAMETER}
  172. * </blockquote>
  173. * 'TYPE-OF-ASN1OBJ' can be one of following symbols:
  174. * <ul>
  175. * <li>'bool' - DERBoolean</li>
  176. * <li>'int' - DERInteger</li>
  177. * <li>'bitstr' - DERBitString</li>
  178. * <li>'octstr' - DEROctetString</li>
  179. * <li>'null' - DERNull</li>
  180. * <li>'oid' - DERObjectIdentifier</li>
  181. * <li>'enum' - DEREnumerated</li>
  182. * <li>'utf8str' - DERUTF8String</li>
  183. * <li>'numstr' - DERNumericString</li>
  184. * <li>'prnstr' - DERPrintableString</li>
  185. * <li>'telstr' - DERTeletexString</li>
  186. * <li>'ia5str' - DERIA5String</li>
  187. * <li>'utctime' - DERUTCTime</li>
  188. * <li>'gentime' - DERGeneralizedTime</li>
  189. * <li>'seq' - DERSequence</li>
  190. * <li>'set' - DERSet</li>
  191. * <li>'tag' - DERTaggedObject</li>
  192. * </ul>
  193. * @example
  194. * newObject({'prnstr': 'aaa'});
  195. * newObject({'seq': [{'int': 3}, {'prnstr': 'aaa'}]})
  196. * // ASN.1 Tagged Object
  197. * newObject({'tag': {'tag': 'a1',
  198. * 'explicit': true,
  199. * 'obj': {'seq': [{'int': 3}, {'prnstr': 'aaa'}]}}});
  200. * // more simple representation of ASN.1 Tagged Object
  201. * newObject({'tag': ['a1',
  202. * true,
  203. * {'seq': [
  204. * {'int': 3},
  205. * {'prnstr': 'aaa'}]}
  206. * ]});
  207. */
  208. this.newObject = function (param) {
  209. var _KJUR = KJUR, _KJUR_asn1 = _KJUR.asn1, _DERBoolean = _KJUR_asn1.DERBoolean, _DERInteger = _KJUR_asn1.DERInteger, _DERBitString = _KJUR_asn1.DERBitString, _DEROctetString = _KJUR_asn1.DEROctetString, _DERNull = _KJUR_asn1.DERNull, _DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier, _DEREnumerated = _KJUR_asn1.DEREnumerated, _DERUTF8String = _KJUR_asn1.DERUTF8String, _DERNumericString = _KJUR_asn1.DERNumericString, _DERPrintableString = _KJUR_asn1.DERPrintableString, _DERTeletexString = _KJUR_asn1.DERTeletexString, _DERIA5String = _KJUR_asn1.DERIA5String, _DERUTCTime = _KJUR_asn1.DERUTCTime, _DERGeneralizedTime = _KJUR_asn1.DERGeneralizedTime, _DERSequence = _KJUR_asn1.DERSequence, _DERSet = _KJUR_asn1.DERSet, _DERTaggedObject = _KJUR_asn1.DERTaggedObject, _newObject = _KJUR_asn1.ASN1Util.newObject;
  210. var keys = Object.keys(param);
  211. if (keys.length != 1)
  212. throw "key of param shall be only one.";
  213. var key = keys[0];
  214. if (":bool:int:bitstr:octstr:null:oid:enum:utf8str:numstr:prnstr:telstr:ia5str:utctime:gentime:seq:set:tag:".indexOf(":" + key + ":") == -1)
  215. throw "undefined key: " + key;
  216. if (key == "bool")
  217. return new _DERBoolean(param[key]);
  218. if (key == "int")
  219. return new _DERInteger(param[key]);
  220. if (key == "bitstr")
  221. return new _DERBitString(param[key]);
  222. if (key == "octstr")
  223. return new _DEROctetString(param[key]);
  224. if (key == "null")
  225. return new _DERNull(param[key]);
  226. if (key == "oid")
  227. return new _DERObjectIdentifier(param[key]);
  228. if (key == "enum")
  229. return new _DEREnumerated(param[key]);
  230. if (key == "utf8str")
  231. return new _DERUTF8String(param[key]);
  232. if (key == "numstr")
  233. return new _DERNumericString(param[key]);
  234. if (key == "prnstr")
  235. return new _DERPrintableString(param[key]);
  236. if (key == "telstr")
  237. return new _DERTeletexString(param[key]);
  238. if (key == "ia5str")
  239. return new _DERIA5String(param[key]);
  240. if (key == "utctime")
  241. return new _DERUTCTime(param[key]);
  242. if (key == "gentime")
  243. return new _DERGeneralizedTime(param[key]);
  244. if (key == "seq") {
  245. var paramList = param[key];
  246. var a = [];
  247. for (var i = 0; i < paramList.length; i++) {
  248. var asn1Obj = _newObject(paramList[i]);
  249. a.push(asn1Obj);
  250. }
  251. return new _DERSequence({ 'array': a });
  252. }
  253. if (key == "set") {
  254. var paramList = param[key];
  255. var a = [];
  256. for (var i = 0; i < paramList.length; i++) {
  257. var asn1Obj = _newObject(paramList[i]);
  258. a.push(asn1Obj);
  259. }
  260. return new _DERSet({ 'array': a });
  261. }
  262. if (key == "tag") {
  263. var tagParam = param[key];
  264. if (Object.prototype.toString.call(tagParam) === '[object Array]' &&
  265. tagParam.length == 3) {
  266. var obj = _newObject(tagParam[2]);
  267. return new _DERTaggedObject({ tag: tagParam[0],
  268. explicit: tagParam[1],
  269. obj: obj });
  270. }
  271. else {
  272. var newParam = {};
  273. if (tagParam.explicit !== undefined)
  274. newParam.explicit = tagParam.explicit;
  275. if (tagParam.tag !== undefined)
  276. newParam.tag = tagParam.tag;
  277. if (tagParam.obj === undefined)
  278. throw "obj shall be specified for 'tag'.";
  279. newParam.obj = _newObject(tagParam.obj);
  280. return new _DERTaggedObject(newParam);
  281. }
  282. }
  283. };
  284. /**
  285. * get encoded hexadecimal string of ASN1Object specifed by JSON parameters
  286. * @name jsonToASN1HEX
  287. * @memberOf KJUR.asn1.ASN1Util
  288. * @function
  289. * @param {Array} param JSON parameter to generate ASN1Object
  290. * @return hexadecimal string of ASN1Object
  291. * @since asn1 1.0.4
  292. * @description
  293. * As for ASN.1 object representation of JSON object,
  294. * please see {@link newObject}.
  295. * @example
  296. * jsonToASN1HEX({'prnstr': 'aaa'});
  297. */
  298. this.jsonToASN1HEX = function (param) {
  299. var asn1Obj = this.newObject(param);
  300. return asn1Obj.getEncodedHex();
  301. };
  302. };
  303. /**
  304. * get dot noted oid number string from hexadecimal value of OID
  305. * @name oidHexToInt
  306. * @memberOf KJUR.asn1.ASN1Util
  307. * @function
  308. * @param {String} hex hexadecimal value of object identifier
  309. * @return {String} dot noted string of object identifier
  310. * @since jsrsasign 4.8.3 asn1 1.0.7
  311. * @description
  312. * This static method converts from hexadecimal string representation of
  313. * ASN.1 value of object identifier to oid number string.
  314. * @example
  315. * KJUR.asn1.ASN1Util.oidHexToInt('550406') &rarr; "2.5.4.6"
  316. */
  317. KJUR.asn1.ASN1Util.oidHexToInt = function (hex) {
  318. var s = "";
  319. var i01 = parseInt(hex.substr(0, 2), 16);
  320. var i0 = Math.floor(i01 / 40);
  321. var i1 = i01 % 40;
  322. var s = i0 + "." + i1;
  323. var binbuf = "";
  324. for (var i = 2; i < hex.length; i += 2) {
  325. var value = parseInt(hex.substr(i, 2), 16);
  326. var bin = ("00000000" + value.toString(2)).slice(-8);
  327. binbuf = binbuf + bin.substr(1, 7);
  328. if (bin.substr(0, 1) == "0") {
  329. var bi = new BigInteger(binbuf, 2);
  330. s = s + "." + bi.toString(10);
  331. binbuf = "";
  332. }
  333. }
  334. ;
  335. return s;
  336. };
  337. /**
  338. * get hexadecimal value of object identifier from dot noted oid value
  339. * @name oidIntToHex
  340. * @memberOf KJUR.asn1.ASN1Util
  341. * @function
  342. * @param {String} oidString dot noted string of object identifier
  343. * @return {String} hexadecimal value of object identifier
  344. * @since jsrsasign 4.8.3 asn1 1.0.7
  345. * @description
  346. * This static method converts from object identifier value string.
  347. * to hexadecimal string representation of it.
  348. * @example
  349. * KJUR.asn1.ASN1Util.oidIntToHex("2.5.4.6") &rarr; "550406"
  350. */
  351. KJUR.asn1.ASN1Util.oidIntToHex = function (oidString) {
  352. var itox = function (i) {
  353. var h = i.toString(16);
  354. if (h.length == 1)
  355. h = '0' + h;
  356. return h;
  357. };
  358. var roidtox = function (roid) {
  359. var h = '';
  360. var bi = new BigInteger(roid, 10);
  361. var b = bi.toString(2);
  362. var padLen = 7 - b.length % 7;
  363. if (padLen == 7)
  364. padLen = 0;
  365. var bPad = '';
  366. for (var i = 0; i < padLen; i++)
  367. bPad += '0';
  368. b = bPad + b;
  369. for (var i = 0; i < b.length - 1; i += 7) {
  370. var b8 = b.substr(i, 7);
  371. if (i != b.length - 7)
  372. b8 = '1' + b8;
  373. h += itox(parseInt(b8, 2));
  374. }
  375. return h;
  376. };
  377. if (!oidString.match(/^[0-9.]+$/)) {
  378. throw "malformed oid string: " + oidString;
  379. }
  380. var h = '';
  381. var a = oidString.split('.');
  382. var i0 = parseInt(a[0]) * 40 + parseInt(a[1]);
  383. h += itox(i0);
  384. a.splice(0, 2);
  385. for (var i = 0; i < a.length; i++) {
  386. h += roidtox(a[i]);
  387. }
  388. return h;
  389. };
  390. // ********************************************************************
  391. // Abstract ASN.1 Classes
  392. // ********************************************************************
  393. // ********************************************************************
  394. /**
  395. * base class for ASN.1 DER encoder object
  396. * @name KJUR.asn1.ASN1Object
  397. * @class base class for ASN.1 DER encoder object
  398. * @property {Boolean} isModified flag whether internal data was changed
  399. * @property {String} hTLV hexadecimal string of ASN.1 TLV
  400. * @property {String} hT hexadecimal string of ASN.1 TLV tag(T)
  401. * @property {String} hL hexadecimal string of ASN.1 TLV length(L)
  402. * @property {String} hV hexadecimal string of ASN.1 TLV value(V)
  403. * @description
  404. */
  405. KJUR.asn1.ASN1Object = function () {
  406. var isModified = true;
  407. var hTLV = null;
  408. var hT = '00';
  409. var hL = '00';
  410. var hV = '';
  411. /**
  412. * get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)
  413. * @name getLengthHexFromValue
  414. * @memberOf KJUR.asn1.ASN1Object#
  415. * @function
  416. * @return {String} hexadecimal string of ASN.1 TLV length(L)
  417. */
  418. this.getLengthHexFromValue = function () {
  419. if (typeof this.hV == "undefined" || this.hV == null) {
  420. throw "this.hV is null or undefined.";
  421. }
  422. if (this.hV.length % 2 == 1) {
  423. throw "value hex must be even length: n=" + hV.length + ",v=" + this.hV;
  424. }
  425. var n = this.hV.length / 2;
  426. var hN = n.toString(16);
  427. if (hN.length % 2 == 1) {
  428. hN = "0" + hN;
  429. }
  430. if (n < 128) {
  431. return hN;
  432. }
  433. else {
  434. var hNlen = hN.length / 2;
  435. if (hNlen > 15) {
  436. throw "ASN.1 length too long to represent by 8x: n = " + n.toString(16);
  437. }
  438. var head = 128 + hNlen;
  439. return head.toString(16) + hN;
  440. }
  441. };
  442. /**
  443. * get hexadecimal string of ASN.1 TLV bytes
  444. * @name getEncodedHex
  445. * @memberOf KJUR.asn1.ASN1Object#
  446. * @function
  447. * @return {String} hexadecimal string of ASN.1 TLV
  448. */
  449. this.getEncodedHex = function () {
  450. if (this.hTLV == null || this.isModified) {
  451. this.hV = this.getFreshValueHex();
  452. this.hL = this.getLengthHexFromValue();
  453. this.hTLV = this.hT + this.hL + this.hV;
  454. this.isModified = false;
  455. //alert("first time: " + this.hTLV);
  456. }
  457. return this.hTLV;
  458. };
  459. /**
  460. * get hexadecimal string of ASN.1 TLV value(V) bytes
  461. * @name getValueHex
  462. * @memberOf KJUR.asn1.ASN1Object#
  463. * @function
  464. * @return {String} hexadecimal string of ASN.1 TLV value(V) bytes
  465. */
  466. this.getValueHex = function () {
  467. this.getEncodedHex();
  468. return this.hV;
  469. };
  470. this.getFreshValueHex = function () {
  471. return '';
  472. };
  473. };
  474. // == BEGIN DERAbstractString ================================================
  475. /**
  476. * base class for ASN.1 DER string classes
  477. * @name KJUR.asn1.DERAbstractString
  478. * @class base class for ASN.1 DER string classes
  479. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  480. * @property {String} s internal string of value
  481. * @extends KJUR.asn1.ASN1Object
  482. * @description
  483. * <br/>
  484. * As for argument 'params' for constructor, you can specify one of
  485. * following properties:
  486. * <ul>
  487. * <li>str - specify initial ASN.1 value(V) by a string</li>
  488. * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
  489. * </ul>
  490. * NOTE: 'params' can be omitted.
  491. */
  492. KJUR.asn1.DERAbstractString = function (params) {
  493. KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
  494. var s = null;
  495. var hV = null;
  496. /**
  497. * get string value of this string object
  498. * @name getString
  499. * @memberOf KJUR.asn1.DERAbstractString#
  500. * @function
  501. * @return {String} string value of this string object
  502. */
  503. this.getString = function () {
  504. return this.s;
  505. };
  506. /**
  507. * set value by a string
  508. * @name setString
  509. * @memberOf KJUR.asn1.DERAbstractString#
  510. * @function
  511. * @param {String} newS value by a string to set
  512. */
  513. this.setString = function (newS) {
  514. this.hTLV = null;
  515. this.isModified = true;
  516. this.s = newS;
  517. this.hV = stohex(this.s);
  518. };
  519. /**
  520. * set value by a hexadecimal string
  521. * @name setStringHex
  522. * @memberOf KJUR.asn1.DERAbstractString#
  523. * @function
  524. * @param {String} newHexString value by a hexadecimal string to set
  525. */
  526. this.setStringHex = function (newHexString) {
  527. this.hTLV = null;
  528. this.isModified = true;
  529. this.s = null;
  530. this.hV = newHexString;
  531. };
  532. this.getFreshValueHex = function () {
  533. return this.hV;
  534. };
  535. if (typeof params != "undefined") {
  536. if (typeof params == "string") {
  537. this.setString(params);
  538. }
  539. else if (typeof params['str'] != "undefined") {
  540. this.setString(params['str']);
  541. }
  542. else if (typeof params['hex'] != "undefined") {
  543. this.setStringHex(params['hex']);
  544. }
  545. }
  546. };
  547. YAHOO.lang.extend(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object);
  548. // == END DERAbstractString ================================================
  549. // == BEGIN DERAbstractTime ==================================================
  550. /**
  551. * base class for ASN.1 DER Generalized/UTCTime class
  552. * @name KJUR.asn1.DERAbstractTime
  553. * @class base class for ASN.1 DER Generalized/UTCTime class
  554. * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'})
  555. * @extends KJUR.asn1.ASN1Object
  556. * @description
  557. * @see KJUR.asn1.ASN1Object - superclass
  558. */
  559. KJUR.asn1.DERAbstractTime = function (params) {
  560. KJUR.asn1.DERAbstractTime.superclass.constructor.call(this);
  561. var s = null;
  562. var date = null;
  563. // --- PRIVATE METHODS --------------------
  564. this.localDateToUTC = function (d) {
  565. utc = d.getTime() + (d.getTimezoneOffset() * 60000);
  566. var utcDate = new Date(utc);
  567. return utcDate;
  568. };
  569. /*
  570. * format date string by Data object
  571. * @name formatDate
  572. * @memberOf KJUR.asn1.AbstractTime;
  573. * @param {Date} dateObject
  574. * @param {string} type 'utc' or 'gen'
  575. * @param {boolean} withMillis flag for with millisections or not
  576. * @description
  577. * 'withMillis' flag is supported from asn1 1.0.6.
  578. */
  579. this.formatDate = function (dateObject, type, withMillis) {
  580. var pad = this.zeroPadding;
  581. var d = this.localDateToUTC(dateObject);
  582. var year = String(d.getFullYear());
  583. if (type == 'utc')
  584. year = year.substr(2, 2);
  585. var month = pad(String(d.getMonth() + 1), 2);
  586. var day = pad(String(d.getDate()), 2);
  587. var hour = pad(String(d.getHours()), 2);
  588. var min = pad(String(d.getMinutes()), 2);
  589. var sec = pad(String(d.getSeconds()), 2);
  590. var s = year + month + day + hour + min + sec;
  591. if (withMillis === true) {
  592. var millis = d.getMilliseconds();
  593. if (millis != 0) {
  594. var sMillis = pad(String(millis), 3);
  595. sMillis = sMillis.replace(/[0]+$/, "");
  596. s = s + "." + sMillis;
  597. }
  598. }
  599. return s + "Z";
  600. };
  601. this.zeroPadding = function (s, len) {
  602. if (s.length >= len)
  603. return s;
  604. return new Array(len - s.length + 1).join('0') + s;
  605. };
  606. // --- PUBLIC METHODS --------------------
  607. /**
  608. * get string value of this string object
  609. * @name getString
  610. * @memberOf KJUR.asn1.DERAbstractTime#
  611. * @function
  612. * @return {String} string value of this time object
  613. */
  614. this.getString = function () {
  615. return this.s;
  616. };
  617. /**
  618. * set value by a string
  619. * @name setString
  620. * @memberOf KJUR.asn1.DERAbstractTime#
  621. * @function
  622. * @param {String} newS value by a string to set such like "130430235959Z"
  623. */
  624. this.setString = function (newS) {
  625. this.hTLV = null;
  626. this.isModified = true;
  627. this.s = newS;
  628. this.hV = stohex(newS);
  629. };
  630. /**
  631. * set value by a Date object
  632. * @name setByDateValue
  633. * @memberOf KJUR.asn1.DERAbstractTime#
  634. * @function
  635. * @param {Integer} year year of date (ex. 2013)
  636. * @param {Integer} month month of date between 1 and 12 (ex. 12)
  637. * @param {Integer} day day of month
  638. * @param {Integer} hour hours of date
  639. * @param {Integer} min minutes of date
  640. * @param {Integer} sec seconds of date
  641. */
  642. this.setByDateValue = function (year, month, day, hour, min, sec) {
  643. var dateObject = new Date(Date.UTC(year, month - 1, day, hour, min, sec, 0));
  644. this.setByDate(dateObject);
  645. };
  646. this.getFreshValueHex = function () {
  647. return this.hV;
  648. };
  649. };
  650. YAHOO.lang.extend(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object);
  651. // == END DERAbstractTime ==================================================
  652. // == BEGIN DERAbstractStructured ============================================
  653. /**
  654. * base class for ASN.1 DER structured class
  655. * @name KJUR.asn1.DERAbstractStructured
  656. * @class base class for ASN.1 DER structured class
  657. * @property {Array} asn1Array internal array of ASN1Object
  658. * @extends KJUR.asn1.ASN1Object
  659. * @description
  660. * @see KJUR.asn1.ASN1Object - superclass
  661. */
  662. KJUR.asn1.DERAbstractStructured = function (params) {
  663. KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
  664. var asn1Array = null;
  665. /**
  666. * set value by array of ASN1Object
  667. * @name setByASN1ObjectArray
  668. * @memberOf KJUR.asn1.DERAbstractStructured#
  669. * @function
  670. * @param {array} asn1ObjectArray array of ASN1Object to set
  671. */
  672. this.setByASN1ObjectArray = function (asn1ObjectArray) {
  673. this.hTLV = null;
  674. this.isModified = true;
  675. this.asn1Array = asn1ObjectArray;
  676. };
  677. /**
  678. * append an ASN1Object to internal array
  679. * @name appendASN1Object
  680. * @memberOf KJUR.asn1.DERAbstractStructured#
  681. * @function
  682. * @param {ASN1Object} asn1Object to add
  683. */
  684. this.appendASN1Object = function (asn1Object) {
  685. this.hTLV = null;
  686. this.isModified = true;
  687. this.asn1Array.push(asn1Object);
  688. };
  689. this.asn1Array = new Array();
  690. if (typeof params != "undefined") {
  691. if (typeof params['array'] != "undefined") {
  692. this.asn1Array = params['array'];
  693. }
  694. }
  695. };
  696. YAHOO.lang.extend(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object);
  697. // ********************************************************************
  698. // ASN.1 Object Classes
  699. // ********************************************************************
  700. // ********************************************************************
  701. /**
  702. * class for ASN.1 DER Boolean
  703. * @name KJUR.asn1.DERBoolean
  704. * @class class for ASN.1 DER Boolean
  705. * @extends KJUR.asn1.ASN1Object
  706. * @description
  707. * @see KJUR.asn1.ASN1Object - superclass
  708. */
  709. KJUR.asn1.DERBoolean = function () {
  710. KJUR.asn1.DERBoolean.superclass.constructor.call(this);
  711. this.hT = "01";
  712. this.hTLV = "0101ff";
  713. };
  714. YAHOO.lang.extend(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object);
  715. // ********************************************************************
  716. /**
  717. * class for ASN.1 DER Integer
  718. * @name KJUR.asn1.DERInteger
  719. * @class class for ASN.1 DER Integer
  720. * @extends KJUR.asn1.ASN1Object
  721. * @description
  722. * <br/>
  723. * As for argument 'params' for constructor, you can specify one of
  724. * following properties:
  725. * <ul>
  726. * <li>int - specify initial ASN.1 value(V) by integer value</li>
  727. * <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li>
  728. * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
  729. * </ul>
  730. * NOTE: 'params' can be omitted.
  731. */
  732. KJUR.asn1.DERInteger = function (params) {
  733. KJUR.asn1.DERInteger.superclass.constructor.call(this);
  734. this.hT = "02";
  735. /**
  736. * set value by Tom Wu's BigInteger object
  737. * @name setByBigInteger
  738. * @memberOf KJUR.asn1.DERInteger#
  739. * @function
  740. * @param {BigInteger} bigIntegerValue to set
  741. */
  742. this.setByBigInteger = function (bigIntegerValue) {
  743. this.hTLV = null;
  744. this.isModified = true;
  745. this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
  746. };
  747. /**
  748. * set value by integer value
  749. * @name setByInteger
  750. * @memberOf KJUR.asn1.DERInteger
  751. * @function
  752. * @param {Integer} integer value to set
  753. */
  754. this.setByInteger = function (intValue) {
  755. var bi = new BigInteger(String(intValue), 10);
  756. this.setByBigInteger(bi);
  757. };
  758. /**
  759. * set value by integer value
  760. * @name setValueHex
  761. * @memberOf KJUR.asn1.DERInteger#
  762. * @function
  763. * @param {String} hexadecimal string of integer value
  764. * @description
  765. * <br/>
  766. * NOTE: Value shall be represented by minimum octet length of
  767. * two's complement representation.
  768. * @example
  769. * new KJUR.asn1.DERInteger(123);
  770. * new KJUR.asn1.DERInteger({'int': 123});
  771. * new KJUR.asn1.DERInteger({'hex': '1fad'});
  772. */
  773. this.setValueHex = function (newHexString) {
  774. this.hV = newHexString;
  775. };
  776. this.getFreshValueHex = function () {
  777. return this.hV;
  778. };
  779. if (typeof params != "undefined") {
  780. if (typeof params['bigint'] != "undefined") {
  781. this.setByBigInteger(params['bigint']);
  782. }
  783. else if (typeof params['int'] != "undefined") {
  784. this.setByInteger(params['int']);
  785. }
  786. else if (typeof params == "number") {
  787. this.setByInteger(params);
  788. }
  789. else if (typeof params['hex'] != "undefined") {
  790. this.setValueHex(params['hex']);
  791. }
  792. }
  793. };
  794. YAHOO.lang.extend(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object);
  795. // ********************************************************************
  796. /**
  797. * class for ASN.1 DER encoded BitString primitive
  798. * @name KJUR.asn1.DERBitString
  799. * @class class for ASN.1 DER encoded BitString primitive
  800. * @extends KJUR.asn1.ASN1Object
  801. * @description
  802. * <br/>
  803. * As for argument 'params' for constructor, you can specify one of
  804. * following properties:
  805. * <ul>
  806. * <li>bin - specify binary string (ex. '10111')</li>
  807. * <li>array - specify array of boolean (ex. [true,false,true,true])</li>
  808. * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li>
  809. * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject}
  810. * argument for "BitString encapsulates" structure.</li>
  811. * </ul>
  812. * NOTE1: 'params' can be omitted.<br/>
  813. * NOTE2: 'obj' parameter have been supported since
  814. * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/>
  815. * @example
  816. * // default constructor
  817. * o = new KJUR.asn1.DERBitString();
  818. * // initialize with binary string
  819. * o = new KJUR.asn1.DERBitString({bin: "1011"});
  820. * // initialize with boolean array
  821. * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]});
  822. * // initialize with hexadecimal string (04 is unused bits)
  823. * o = new KJUR.asn1.DEROctetString({hex: "04bac0"});
  824. * // initialize with ASN1Util.newObject argument for encapsulated
  825. * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}});
  826. * // above generates a ASN.1 data like this:
  827. * // BIT STRING, encapsulates {
  828. * // SEQUENCE {
  829. * // INTEGER 3
  830. * // PrintableString 'aaa'
  831. * // }
  832. * // }
  833. */
  834. KJUR.asn1.DERBitString = function (params) {
  835. if (params !== undefined && typeof params.obj !== "undefined") {
  836. var o = KJUR.asn1.ASN1Util.newObject(params.obj);
  837. params.hex = "00" + o.getEncodedHex();
  838. }
  839. KJUR.asn1.DERBitString.superclass.constructor.call(this);
  840. this.hT = "03";
  841. /**
  842. * set ASN.1 value(V) by a hexadecimal string including unused bits
  843. * @name setHexValueIncludingUnusedBits
  844. * @memberOf KJUR.asn1.DERBitString#
  845. * @function
  846. * @param {String} newHexStringIncludingUnusedBits
  847. */
  848. this.setHexValueIncludingUnusedBits = function (newHexStringIncludingUnusedBits) {
  849. this.hTLV = null;
  850. this.isModified = true;
  851. this.hV = newHexStringIncludingUnusedBits;
  852. };
  853. /**
  854. * set ASN.1 value(V) by unused bit and hexadecimal string of value
  855. * @name setUnusedBitsAndHexValue
  856. * @memberOf KJUR.asn1.DERBitString#
  857. * @function
  858. * @param {Integer} unusedBits
  859. * @param {String} hValue
  860. */
  861. this.setUnusedBitsAndHexValue = function (unusedBits, hValue) {
  862. if (unusedBits < 0 || 7 < unusedBits) {
  863. throw "unused bits shall be from 0 to 7: u = " + unusedBits;
  864. }
  865. var hUnusedBits = "0" + unusedBits;
  866. this.hTLV = null;
  867. this.isModified = true;
  868. this.hV = hUnusedBits + hValue;
  869. };
  870. /**
  871. * set ASN.1 DER BitString by binary string<br/>
  872. * @name setByBinaryString
  873. * @memberOf KJUR.asn1.DERBitString#
  874. * @function
  875. * @param {String} binaryString binary value string (i.e. '10111')
  876. * @description
  877. * Its unused bits will be calculated automatically by length of
  878. * 'binaryValue'. <br/>
  879. * NOTE: Trailing zeros '0' will be ignored.
  880. * @example
  881. * o = new KJUR.asn1.DERBitString();
  882. * o.setByBooleanArray("01011");
  883. */
  884. this.setByBinaryString = function (binaryString) {
  885. binaryString = binaryString.replace(/0+$/, '');
  886. var unusedBits = 8 - binaryString.length % 8;
  887. if (unusedBits == 8)
  888. unusedBits = 0;
  889. for (var i = 0; i <= unusedBits; i++) {
  890. binaryString += '0';
  891. }
  892. var h = '';
  893. for (var i = 0; i < binaryString.length - 1; i += 8) {
  894. var b = binaryString.substr(i, 8);
  895. var x = parseInt(b, 2).toString(16);
  896. if (x.length == 1)
  897. x = '0' + x;
  898. h += x;
  899. }
  900. this.hTLV = null;
  901. this.isModified = true;
  902. this.hV = '0' + unusedBits + h;
  903. };
  904. /**
  905. * set ASN.1 TLV value(V) by an array of boolean<br/>
  906. * @name setByBooleanArray
  907. * @memberOf KJUR.asn1.DERBitString#
  908. * @function
  909. * @param {array} booleanArray array of boolean (ex. [true, false, true])
  910. * @description
  911. * NOTE: Trailing falses will be ignored in the ASN.1 DER Object.
  912. * @example
  913. * o = new KJUR.asn1.DERBitString();
  914. * o.setByBooleanArray([false, true, false, true, true]);
  915. */
  916. this.setByBooleanArray = function (booleanArray) {
  917. var s = '';
  918. for (var i = 0; i < booleanArray.length; i++) {
  919. if (booleanArray[i] == true) {
  920. s += '1';
  921. }
  922. else {
  923. s += '0';
  924. }
  925. }
  926. this.setByBinaryString(s);
  927. };
  928. /**
  929. * generate an array of falses with specified length<br/>
  930. * @name newFalseArray
  931. * @memberOf KJUR.asn1.DERBitString
  932. * @function
  933. * @param {Integer} nLength length of array to generate
  934. * @return {array} array of boolean falses
  935. * @description
  936. * This static method may be useful to initialize boolean array.
  937. * @example
  938. * o = new KJUR.asn1.DERBitString();
  939. * o.newFalseArray(3) &rarr; [false, false, false]
  940. */
  941. this.newFalseArray = function (nLength) {
  942. var a = new Array(nLength);
  943. for (var i = 0; i < nLength; i++) {
  944. a[i] = false;
  945. }
  946. return a;
  947. };
  948. this.getFreshValueHex = function () {
  949. return this.hV;
  950. };
  951. if (typeof params != "undefined") {
  952. if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) {
  953. this.setHexValueIncludingUnusedBits(params);
  954. }
  955. else if (typeof params['hex'] != "undefined") {
  956. this.setHexValueIncludingUnusedBits(params['hex']);
  957. }
  958. else if (typeof params['bin'] != "undefined") {
  959. this.setByBinaryString(params['bin']);
  960. }
  961. else if (typeof params['array'] != "undefined") {
  962. this.setByBooleanArray(params['array']);
  963. }
  964. }
  965. };
  966. YAHOO.lang.extend(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object);
  967. // ********************************************************************
  968. /**
  969. * class for ASN.1 DER OctetString<br/>
  970. * @name KJUR.asn1.DEROctetString
  971. * @class class for ASN.1 DER OctetString
  972. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  973. * @extends KJUR.asn1.DERAbstractString
  974. * @description
  975. * This class provides ASN.1 OctetString simple type.<br/>
  976. * Supported "params" attributes are:
  977. * <ul>
  978. * <li>str - to set a string as a value</li>
  979. * <li>hex - to set a hexadecimal string as a value</li>
  980. * <li>obj - to set a encapsulated ASN.1 value by JSON object
  981. * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li>
  982. * </ul>
  983. * NOTE: A parameter 'obj' have been supported
  984. * for "OCTET STRING, encapsulates" structure.
  985. * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).
  986. * @see KJUR.asn1.DERAbstractString - superclass
  987. * @example
  988. * // default constructor
  989. * o = new KJUR.asn1.DEROctetString();
  990. * // initialize with string
  991. * o = new KJUR.asn1.DEROctetString({str: "aaa"});
  992. * // initialize with hexadecimal string
  993. * o = new KJUR.asn1.DEROctetString({hex: "616161"});
  994. * // initialize with ASN1Util.newObject argument
  995. * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}});
  996. * // above generates a ASN.1 data like this:
  997. * // OCTET STRING, encapsulates {
  998. * // SEQUENCE {
  999. * // INTEGER 3
  1000. * // PrintableString 'aaa'
  1001. * // }
  1002. * // }
  1003. */
  1004. KJUR.asn1.DEROctetString = function (params) {
  1005. if (params !== undefined && typeof params.obj !== "undefined") {
  1006. var o = KJUR.asn1.ASN1Util.newObject(params.obj);
  1007. params.hex = o.getEncodedHex();
  1008. }
  1009. KJUR.asn1.DEROctetString.superclass.constructor.call(this, params);
  1010. this.hT = "04";
  1011. };
  1012. YAHOO.lang.extend(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString);
  1013. // ********************************************************************
  1014. /**
  1015. * class for ASN.1 DER Null
  1016. * @name KJUR.asn1.DERNull
  1017. * @class class for ASN.1 DER Null
  1018. * @extends KJUR.asn1.ASN1Object
  1019. * @description
  1020. * @see KJUR.asn1.ASN1Object - superclass
  1021. */
  1022. KJUR.asn1.DERNull = function () {
  1023. KJUR.asn1.DERNull.superclass.constructor.call(this);
  1024. this.hT = "05";
  1025. this.hTLV = "0500";
  1026. };
  1027. YAHOO.lang.extend(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object);
  1028. // ********************************************************************
  1029. /**
  1030. * class for ASN.1 DER ObjectIdentifier
  1031. * @name KJUR.asn1.DERObjectIdentifier
  1032. * @class class for ASN.1 DER ObjectIdentifier
  1033. * @param {Array} params associative array of parameters (ex. {'oid': '2.5.4.5'})
  1034. * @extends KJUR.asn1.ASN1Object
  1035. * @description
  1036. * <br/>
  1037. * As for argument 'params' for constructor, you can specify one of
  1038. * following properties:
  1039. * <ul>
  1040. * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li>
  1041. * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
  1042. * </ul>
  1043. * NOTE: 'params' can be omitted.
  1044. */
  1045. KJUR.asn1.DERObjectIdentifier = function (params) {
  1046. var itox = function (i) {
  1047. var h = i.toString(16);
  1048. if (h.length == 1)
  1049. h = '0' + h;
  1050. return h;
  1051. };
  1052. var roidtox = function (roid) {
  1053. var h = '';
  1054. var bi = new BigInteger(roid, 10);
  1055. var b = bi.toString(2);
  1056. var padLen = 7 - b.length % 7;
  1057. if (padLen == 7)
  1058. padLen = 0;
  1059. var bPad = '';
  1060. for (var i = 0; i < padLen; i++)
  1061. bPad += '0';
  1062. b = bPad + b;
  1063. for (var i = 0; i < b.length - 1; i += 7) {
  1064. var b8 = b.substr(i, 7);
  1065. if (i != b.length - 7)
  1066. b8 = '1' + b8;
  1067. h += itox(parseInt(b8, 2));
  1068. }
  1069. return h;
  1070. };
  1071. KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this);
  1072. this.hT = "06";
  1073. /**
  1074. * set value by a hexadecimal string
  1075. * @name setValueHex
  1076. * @memberOf KJUR.asn1.DERObjectIdentifier#
  1077. * @function
  1078. * @param {String} newHexString hexadecimal value of OID bytes
  1079. */
  1080. this.setValueHex = function (newHexString) {
  1081. this.hTLV = null;
  1082. this.isModified = true;
  1083. this.s = null;
  1084. this.hV = newHexString;
  1085. };
  1086. /**
  1087. * set value by a OID string<br/>
  1088. * @name setValueOidString
  1089. * @memberOf KJUR.asn1.DERObjectIdentifier#
  1090. * @function
  1091. * @param {String} oidString OID string (ex. 2.5.4.13)
  1092. * @example
  1093. * o = new KJUR.asn1.DERObjectIdentifier();
  1094. * o.setValueOidString("2.5.4.13");
  1095. */
  1096. this.setValueOidString = function (oidString) {
  1097. if (!oidString.match(/^[0-9.]+$/)) {
  1098. throw "malformed oid string: " + oidString;
  1099. }
  1100. var h = '';
  1101. var a = oidString.split('.');
  1102. var i0 = parseInt(a[0]) * 40 + parseInt(a[1]);
  1103. h += itox(i0);
  1104. a.splice(0, 2);
  1105. for (var i = 0; i < a.length; i++) {
  1106. h += roidtox(a[i]);
  1107. }
  1108. this.hTLV = null;
  1109. this.isModified = true;
  1110. this.s = null;
  1111. this.hV = h;
  1112. };
  1113. /**
  1114. * set value by a OID name
  1115. * @name setValueName
  1116. * @memberOf KJUR.asn1.DERObjectIdentifier#
  1117. * @function
  1118. * @param {String} oidName OID name (ex. 'serverAuth')
  1119. * @since 1.0.1
  1120. * @description
  1121. * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'.
  1122. * Otherwise raise error.
  1123. * @example
  1124. * o = new KJUR.asn1.DERObjectIdentifier();
  1125. * o.setValueName("serverAuth");
  1126. */
  1127. this.setValueName = function (oidName) {
  1128. var oid = KJUR.asn1.x509.OID.name2oid(oidName);
  1129. if (oid !== '') {
  1130. this.setValueOidString(oid);
  1131. }
  1132. else {
  1133. throw "DERObjectIdentifier oidName undefined: " + oidName;
  1134. }
  1135. };
  1136. this.getFreshValueHex = function () {
  1137. return this.hV;
  1138. };
  1139. if (params !== undefined) {
  1140. if (typeof params === "string") {
  1141. if (params.match(/^[0-2].[0-9.]+$/)) {
  1142. this.setValueOidString(params);
  1143. }
  1144. else {
  1145. this.setValueName(params);
  1146. }
  1147. }
  1148. else if (params.oid !== undefined) {
  1149. this.setValueOidString(params.oid);
  1150. }
  1151. else if (params.hex !== undefined) {
  1152. this.setValueHex(params.hex);
  1153. }
  1154. else if (params.name !== undefined) {
  1155. this.setValueName(params.name);
  1156. }
  1157. }
  1158. };
  1159. YAHOO.lang.extend(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object);
  1160. // ********************************************************************
  1161. /**
  1162. * class for ASN.1 DER Enumerated
  1163. * @name KJUR.asn1.DEREnumerated
  1164. * @class class for ASN.1 DER Enumerated
  1165. * @extends KJUR.asn1.ASN1Object
  1166. * @description
  1167. * <br/>
  1168. * As for argument 'params' for constructor, you can specify one of
  1169. * following properties:
  1170. * <ul>
  1171. * <li>int - specify initial ASN.1 value(V) by integer value</li>
  1172. * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
  1173. * </ul>
  1174. * NOTE: 'params' can be omitted.
  1175. * @example
  1176. * new KJUR.asn1.DEREnumerated(123);
  1177. * new KJUR.asn1.DEREnumerated({int: 123});
  1178. * new KJUR.asn1.DEREnumerated({hex: '1fad'});
  1179. */
  1180. KJUR.asn1.DEREnumerated = function (params) {
  1181. KJUR.asn1.DEREnumerated.superclass.constructor.call(this);
  1182. this.hT = "0a";
  1183. /**
  1184. * set value by Tom Wu's BigInteger object
  1185. * @name setByBigInteger
  1186. * @memberOf KJUR.asn1.DEREnumerated#
  1187. * @function
  1188. * @param {BigInteger} bigIntegerValue to set
  1189. */
  1190. this.setByBigInteger = function (bigIntegerValue) {
  1191. this.hTLV = null;
  1192. this.isModified = true;
  1193. this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
  1194. };
  1195. /**
  1196. * set value by integer value
  1197. * @name setByInteger
  1198. * @memberOf KJUR.asn1.DEREnumerated#
  1199. * @function
  1200. * @param {Integer} integer value to set
  1201. */
  1202. this.setByInteger = function (intValue) {
  1203. var bi = new BigInteger(String(intValue), 10);
  1204. this.setByBigInteger(bi);
  1205. };
  1206. /**
  1207. * set value by integer value
  1208. * @name setValueHex
  1209. * @memberOf KJUR.asn1.DEREnumerated#
  1210. * @function
  1211. * @param {String} hexadecimal string of integer value
  1212. * @description
  1213. * <br/>
  1214. * NOTE: Value shall be represented by minimum octet length of
  1215. * two's complement representation.
  1216. */
  1217. this.setValueHex = function (newHexString) {
  1218. this.hV = newHexString;
  1219. };
  1220. this.getFreshValueHex = function () {
  1221. return this.hV;
  1222. };
  1223. if (typeof params != "undefined") {
  1224. if (typeof params['int'] != "undefined") {
  1225. this.setByInteger(params['int']);
  1226. }
  1227. else if (typeof params == "number") {
  1228. this.setByInteger(params);
  1229. }
  1230. else if (typeof params['hex'] != "undefined") {
  1231. this.setValueHex(params['hex']);
  1232. }
  1233. }
  1234. };
  1235. YAHOO.lang.extend(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object);
  1236. // ********************************************************************
  1237. /**
  1238. * class for ASN.1 DER UTF8String
  1239. * @name KJUR.asn1.DERUTF8String
  1240. * @class class for ASN.1 DER UTF8String
  1241. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  1242. * @extends KJUR.asn1.DERAbstractString
  1243. * @description
  1244. * @see KJUR.asn1.DERAbstractString - superclass
  1245. */
  1246. KJUR.asn1.DERUTF8String = function (params) {
  1247. KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params);
  1248. this.hT = "0c";
  1249. };
  1250. YAHOO.lang.extend(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString);
  1251. // ********************************************************************
  1252. /**
  1253. * class for ASN.1 DER NumericString
  1254. * @name KJUR.asn1.DERNumericString
  1255. * @class class for ASN.1 DER NumericString
  1256. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  1257. * @extends KJUR.asn1.DERAbstractString
  1258. * @description
  1259. * @see KJUR.asn1.DERAbstractString - superclass
  1260. */
  1261. KJUR.asn1.DERNumericString = function (params) {
  1262. KJUR.asn1.DERNumericString.superclass.constructor.call(this, params);
  1263. this.hT = "12";
  1264. };
  1265. YAHOO.lang.extend(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString);
  1266. // ********************************************************************
  1267. /**
  1268. * class for ASN.1 DER PrintableString
  1269. * @name KJUR.asn1.DERPrintableString
  1270. * @class class for ASN.1 DER PrintableString
  1271. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  1272. * @extends KJUR.asn1.DERAbstractString
  1273. * @description
  1274. * @see KJUR.asn1.DERAbstractString - superclass
  1275. */
  1276. KJUR.asn1.DERPrintableString = function (params) {
  1277. KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params);
  1278. this.hT = "13";
  1279. };
  1280. YAHOO.lang.extend(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString);
  1281. // ********************************************************************
  1282. /**
  1283. * class for ASN.1 DER TeletexString
  1284. * @name KJUR.asn1.DERTeletexString
  1285. * @class class for ASN.1 DER TeletexString
  1286. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  1287. * @extends KJUR.asn1.DERAbstractString
  1288. * @description
  1289. * @see KJUR.asn1.DERAbstractString - superclass
  1290. */
  1291. KJUR.asn1.DERTeletexString = function (params) {
  1292. KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params);
  1293. this.hT = "14";
  1294. };
  1295. YAHOO.lang.extend(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString);
  1296. // ********************************************************************
  1297. /**
  1298. * class for ASN.1 DER IA5String
  1299. * @name KJUR.asn1.DERIA5String
  1300. * @class class for ASN.1 DER IA5String
  1301. * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
  1302. * @extends KJUR.asn1.DERAbstractString
  1303. * @description
  1304. * @see KJUR.asn1.DERAbstractString - superclass
  1305. */
  1306. KJUR.asn1.DERIA5String = function (params) {
  1307. KJUR.asn1.DERIA5String.superclass.constructor.call(this, params);
  1308. this.hT = "16";
  1309. };
  1310. YAHOO.lang.extend(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString);
  1311. // ********************************************************************
  1312. /**
  1313. * class for ASN.1 DER UTCTime
  1314. * @name KJUR.asn1.DERUTCTime
  1315. * @class class for ASN.1 DER UTCTime
  1316. * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'})
  1317. * @extends KJUR.asn1.DERAbstractTime
  1318. * @description
  1319. * <br/>
  1320. * As for argument 'params' for constructor, you can specify one of
  1321. * following properties:
  1322. * <ul>
  1323. * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li>
  1324. * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
  1325. * <li>date - specify Date object.</li>
  1326. * </ul>
  1327. * NOTE: 'params' can be omitted.
  1328. * <h4>EXAMPLES</h4>
  1329. * @example
  1330. * d1 = new KJUR.asn1.DERUTCTime();
  1331. * d1.setString('130430125959Z');
  1332. *
  1333. * d2 = new KJUR.asn1.DERUTCTime({'str': '130430125959Z'});
  1334. * d3 = new KJUR.asn1.DERUTCTime({'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))});
  1335. * d4 = new KJUR.asn1.DERUTCTime('130430125959Z');
  1336. */
  1337. KJUR.asn1.DERUTCTime = function (params) {
  1338. KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params);
  1339. this.hT = "17";
  1340. /**
  1341. * set value by a Date object<br/>
  1342. * @name setByDate
  1343. * @memberOf KJUR.asn1.DERUTCTime#
  1344. * @function
  1345. * @param {Date} dateObject Date object to set ASN.1 value(V)
  1346. * @example
  1347. * o = new KJUR.asn1.DERUTCTime();
  1348. * o.setByDate(new Date("2016/12/31"));
  1349. */
  1350. this.setByDate = function (dateObject) {
  1351. this.hTLV = null;
  1352. this.isModified = true;
  1353. this.date = dateObject;
  1354. this.s = this.formatDate(this.date, 'utc');
  1355. this.hV = stohex(this.s);
  1356. };
  1357. this.getFreshValueHex = function () {
  1358. if (typeof this.date == "undefined" && typeof this.s == "undefined") {
  1359. this.date = new Date();
  1360. this.s = this.formatDate(this.date, 'utc');
  1361. this.hV = stohex(this.s);
  1362. }
  1363. return this.hV;
  1364. };
  1365. if (params !== undefined) {
  1366. if (params.str !== undefined) {
  1367. this.setString(params.str);
  1368. }
  1369. else if (typeof params == "string" && params.match(/^[0-9]{12}Z$/)) {
  1370. this.setString(params);
  1371. }
  1372. else if (params.hex !== undefined) {
  1373. this.setStringHex(params.hex);
  1374. }
  1375. else if (params.date !== undefined) {
  1376. this.setByDate(params.date);
  1377. }
  1378. }
  1379. };
  1380. YAHOO.lang.extend(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime);
  1381. // ********************************************************************
  1382. /**
  1383. * class for ASN.1 DER GeneralizedTime
  1384. * @name KJUR.asn1.DERGeneralizedTime
  1385. * @class class for ASN.1 DER GeneralizedTime
  1386. * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'})
  1387. * @property {Boolean} withMillis flag to show milliseconds or not
  1388. * @extends KJUR.asn1.DERAbstractTime
  1389. * @description
  1390. * <br/>
  1391. * As for argument 'params' for constructor, you can specify one of
  1392. * following properties:
  1393. * <ul>
  1394. * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li>
  1395. * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
  1396. * <li>date - specify Date object.</li>
  1397. * <li>millis - specify flag to show milliseconds (from 1.0.6)</li>
  1398. * </ul>
  1399. * NOTE1: 'params' can be omitted.
  1400. * NOTE2: 'withMillis' property is supported from asn1 1.0.6.
  1401. */
  1402. KJUR.asn1.DERGeneralizedTime = function (params) {
  1403. KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params);
  1404. this.hT = "18";
  1405. this.withMillis = false;
  1406. /**
  1407. * set value by a Date object
  1408. * @name setByDate
  1409. * @memberOf KJUR.asn1.DERGeneralizedTime#
  1410. * @function
  1411. * @param {Date} dateObject Date object to set ASN.1 value(V)
  1412. * @example
  1413. * When you specify UTC time, use 'Date.UTC' method like this:<br/>
  1414. * o1 = new DERUTCTime();
  1415. * o1.setByDate(date);
  1416. *
  1417. * date = new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)); #2015JAN31 23:59:59
  1418. */
  1419. this.setByDate = function (dateObject) {
  1420. this.hTLV = null;
  1421. this.isModified = true;
  1422. this.date = dateObject;
  1423. this.s = this.formatDate(this.date, 'gen', this.withMillis);
  1424. this.hV = stohex(this.s);
  1425. };
  1426. this.getFreshValueHex = function () {
  1427. if (this.date === undefined && this.s === undefined) {
  1428. this.date = new Date();
  1429. this.s = this.formatDate(this.date, 'gen', this.withMillis);
  1430. this.hV = stohex(this.s);
  1431. }
  1432. return this.hV;
  1433. };
  1434. if (params !== undefined) {
  1435. if (params.str !== undefined) {
  1436. this.setString(params.str);
  1437. }
  1438. else if (typeof params == "string" && params.match(/^[0-9]{14}Z$/)) {
  1439. this.setString(params);
  1440. }
  1441. else if (params.hex !== undefined) {
  1442. this.setStringHex(params.hex);
  1443. }
  1444. else if (params.date !== undefined) {
  1445. this.setByDate(params.date);
  1446. }
  1447. if (params.millis === true) {
  1448. this.withMillis = true;
  1449. }
  1450. }
  1451. };
  1452. YAHOO.lang.extend(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime);
  1453. // ********************************************************************
  1454. /**
  1455. * class for ASN.1 DER Sequence
  1456. * @name KJUR.asn1.DERSequence
  1457. * @class class for ASN.1 DER Sequence
  1458. * @extends KJUR.asn1.DERAbstractStructured
  1459. * @description
  1460. * <br/>
  1461. * As for argument 'params' for constructor, you can specify one of
  1462. * following properties:
  1463. * <ul>
  1464. * <li>array - specify array of ASN1Object to set elements of content</li>
  1465. * </ul>
  1466. * NOTE: 'params' can be omitted.
  1467. */
  1468. KJUR.asn1.DERSequence = function (params) {
  1469. KJUR.asn1.DERSequence.superclass.constructor.call(this, params);
  1470. this.hT = "30";
  1471. this.getFreshValueHex = function () {
  1472. var h = '';
  1473. for (var i = 0; i < this.asn1Array.length; i++) {
  1474. var asn1Obj = this.asn1Array[i];
  1475. h += asn1Obj.getEncodedHex();
  1476. }
  1477. this.hV = h;
  1478. return this.hV;
  1479. };
  1480. };
  1481. YAHOO.lang.extend(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured);
  1482. // ********************************************************************
  1483. /**
  1484. * class for ASN.1 DER Set
  1485. * @name KJUR.asn1.DERSet
  1486. * @class class for ASN.1 DER Set
  1487. * @extends KJUR.asn1.DERAbstractStructured
  1488. * @description
  1489. * <br/>
  1490. * As for argument 'params' for constructor, you can specify one of
  1491. * following properties:
  1492. * <ul>
  1493. * <li>array - specify array of ASN1Object to set elements of content</li>
  1494. * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li>
  1495. * </ul>
  1496. * NOTE1: 'params' can be omitted.<br/>
  1497. * NOTE2: sortflag is supported since 1.0.5.
  1498. */
  1499. KJUR.asn1.DERSet = function (params) {
  1500. KJUR.asn1.DERSet.superclass.constructor.call(this, params);
  1501. this.hT = "31";
  1502. this.sortFlag = true; // item shall be sorted only in ASN.1 DER
  1503. this.getFreshValueHex = function () {
  1504. var a = new Array();
  1505. for (var i = 0; i < this.asn1Array.length; i++) {
  1506. var asn1Obj = this.asn1Array[i];
  1507. a.push(asn1Obj.getEncodedHex());
  1508. }
  1509. if (this.sortFlag == true)
  1510. a.sort();
  1511. this.hV = a.join('');
  1512. return this.hV;
  1513. };
  1514. if (typeof params != "undefined") {
  1515. if (typeof params.sortflag != "undefined" &&
  1516. params.sortflag == false)
  1517. this.sortFlag = false;
  1518. }
  1519. };
  1520. YAHOO.lang.extend(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured);
  1521. // ********************************************************************
  1522. /**
  1523. * class for ASN.1 DER TaggedObject
  1524. * @name KJUR.asn1.DERTaggedObject
  1525. * @class class for ASN.1 DER TaggedObject
  1526. * @extends KJUR.asn1.ASN1Object
  1527. * @description
  1528. * <br/>
  1529. * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object.
  1530. * For example, if you find '[1]' tag in a ASN.1 dump,
  1531. * 'tagNoHex' will be 'a1'.
  1532. * <br/>
  1533. * As for optional argument 'params' for constructor, you can specify *ANY* of
  1534. * following properties:
  1535. * <ul>
  1536. * <li>explicit - specify true if this is explicit tag otherwise false
  1537. * (default is 'true').</li>
  1538. * <li>tag - specify tag (default is 'a0' which means [0])</li>
  1539. * <li>obj - specify ASN1Object which is tagged</li>
  1540. * </ul>
  1541. * @example
  1542. * d1 = new KJUR.asn1.DERUTF8String({'str':'a'});
  1543. * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1});
  1544. * hex = d2.getEncodedHex();
  1545. */
  1546. KJUR.asn1.DERTaggedObject = function (params) {
  1547. KJUR.asn1.DERTaggedObject.superclass.constructor.call(this);
  1548. this.hT = "a0";
  1549. this.hV = '';
  1550. this.isExplicit = true;
  1551. this.asn1Object = null;
  1552. /**
  1553. * set value by an ASN1Object
  1554. * @name setString
  1555. * @memberOf KJUR.asn1.DERTaggedObject#
  1556. * @function
  1557. * @param {Boolean} isExplicitFlag flag for explicit/implicit tag
  1558. * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag
  1559. * @param {ASN1Object} asn1Object ASN.1 to encapsulate
  1560. */
  1561. this.setASN1Object = function (isExplicitFlag, tagNoHex, asn1Object) {
  1562. this.hT = tagNoHex;
  1563. this.isExplicit = isExplicitFlag;
  1564. this.asn1Object = asn1Object;
  1565. if (this.isExplicit) {
  1566. this.hV = this.asn1Object.getEncodedHex();
  1567. this.hTLV = null;
  1568. this.isModified = true;
  1569. }
  1570. else {
  1571. this.hV = null;
  1572. this.hTLV = asn1Object.getEncodedHex();
  1573. this.hTLV = this.hTLV.replace(/^../, tagNoHex);
  1574. this.isModified = false;
  1575. }
  1576. };
  1577. this.getFreshValueHex = function () {
  1578. return this.hV;
  1579. };
  1580. if (typeof params != "undefined") {
  1581. if (typeof params['tag'] != "undefined") {
  1582. this.hT = params['tag'];
  1583. }
  1584. if (typeof params['explicit'] != "undefined") {
  1585. this.isExplicit = params['explicit'];
  1586. }
  1587. if (typeof params['obj'] != "undefined") {
  1588. this.asn1Object = params['obj'];
  1589. this.setASN1Object(this.isExplicit, this.hT, this.asn1Object);
  1590. }
  1591. }
  1592. };
  1593. YAHOO.lang.extend(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object);