asn1.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /// <reference types="node" />
  2. import { inspect } from 'util';
  3. /**
  4. * Template is use to create schema of ASN.1 object for `asn1.validate` method.
  5. */
  6. export interface Template {
  7. name: string;
  8. class: Class;
  9. tag: Tag | Tag[];
  10. optional?: boolean;
  11. capture?: string;
  12. value?: Template | Template[];
  13. }
  14. /**
  15. * Captures is use to capture sub objects from ASN.1 object for `asn1.validate` method.
  16. */
  17. export interface Captures {
  18. [index: string]: ASN1;
  19. }
  20. /**
  21. * ASN.1 classes.
  22. */
  23. export declare enum Class {
  24. UNIVERSAL = 0,
  25. APPLICATION = 64,
  26. CONTEXT_SPECIFIC = 128,
  27. PRIVATE = 192
  28. }
  29. /**
  30. * ASN.1 types. Not all types are supported by this implementation.
  31. */
  32. export declare enum Tag {
  33. NONE = 0,
  34. BOOLEAN = 1,
  35. INTEGER = 2,
  36. BITSTRING = 3,
  37. OCTETSTRING = 4,
  38. NULL = 5,
  39. OID = 6,
  40. ENUMERATED = 10,
  41. UTF8 = 12,
  42. SEQUENCE = 16,
  43. SET = 17,
  44. NUMERICSTRING = 18,
  45. PRINTABLESTRING = 19,
  46. T61STRING = 20,
  47. IA5STRING = 22,
  48. UTCTIME = 23,
  49. GENERALIZEDTIME = 24,
  50. GENERALSTRING = 27
  51. }
  52. /**
  53. * BitString is the structure to use when you want an ASN.1 BIT STRING type. A
  54. * bit string is padded up to the nearest byte in memory and the number of
  55. * valid bits is recorded. Padding bits will be zero.
  56. */
  57. export declare class BitString {
  58. /**
  59. * The underlying buffer
  60. */
  61. readonly buf: Buffer;
  62. /**
  63. * The length of bits
  64. */
  65. readonly bitLen: number;
  66. constructor(buf: Buffer, bitLen: number);
  67. /**
  68. * Returns the value for the given bits offset.
  69. * @param i bits offet
  70. */
  71. at(i: number): number;
  72. /**
  73. * Align buffer
  74. */
  75. rightAlign(): Buffer;
  76. }
  77. /**
  78. * Implements parsing of DER-encoded ASN.1 data structures,
  79. * as defined in ITU-T Rec X.690.
  80. *
  81. * See also ``A Layman's Guide to a Subset of ASN.1, BER, and DER,''
  82. * http://luca.ntop.org/Teaching/Appunti/asn1.html.
  83. *
  84. * ASN.1 is a syntax for specifying abstract objects and BER, DER, PER, XER etc
  85. * are different encoding formats for those objects. Here, we'll be dealing
  86. * with DER, the Distinguished Encoding Rules. DER is used in X.509 because
  87. * it's fast to parse and, unlike BER, has a unique encoding for every object.
  88. * When calculating hashes over objects, it's important that the resulting
  89. * bytes be the same at both ends and DER removes this margin of error.
  90. * ASN.1 is very complex and this package doesn't attempt to implement
  91. * everything by any means.
  92. *
  93. * DER Encoding of ASN.1 Types:
  94. * https://msdn.microsoft.com/en-us/library/windows/desktop/bb540792(v=vs.85).aspx
  95. */
  96. export declare class ASN1 {
  97. /**
  98. * Creates a Tag.BOOLEAN ASN.1 object.
  99. * @param val boolean value.
  100. */
  101. static Bool(val: boolean): ASN1;
  102. /**
  103. * Parse a Tag.BOOLEAN value from ASN.1 object' value.
  104. * @param buf the buffer to parse.
  105. */
  106. static parseBool(buf: Buffer): boolean;
  107. /**
  108. * Creates a Tag.INTEGER ASN.1 object.
  109. * @param val integer value or buffer.
  110. */
  111. static Integer(val: number | Buffer): ASN1;
  112. /**
  113. * Parse a Tag.INTEGER value from ASN.1 object' value.
  114. * @param buf the buffer to parse.
  115. */
  116. static parseInteger(buf: Buffer): number | string;
  117. /**
  118. * Parse a Tag.INTEGER value as a number from ASN.1 object' value.
  119. * @param buf the buffer to parse.
  120. */
  121. static parseIntegerNum(buf: Buffer): number;
  122. /**
  123. * Parse a Tag.INTEGER value as a hex string(for BigInt) from ASN.1 object' value.
  124. * @param buf the buffer to parse.
  125. */
  126. static parseIntegerStr(buf: Buffer): string;
  127. /**
  128. * Creates a Tag.BITSTRING ASN.1 object.
  129. * @param val BitString object or buffer.
  130. */
  131. static BitString(val: BitString | Buffer): ASN1;
  132. /**
  133. * Parse a Tag.BITSTRING value from ASN.1 object' value.
  134. * @param buf the buffer to parse.
  135. */
  136. static parseBitString(buf: Buffer): BitString;
  137. /**
  138. * Creates a Tag.NULL ASN.1 object.
  139. */
  140. static Null(): ASN1;
  141. /**
  142. * Parse a Tag.NULL value from ASN.1 object' value.
  143. * @param buf the buffer to parse.
  144. */
  145. static parseNull(buf: Buffer): null;
  146. /**
  147. * Creates an Tag.OID (dot-separated numeric string) ASN.1 object.
  148. * @param val dot-separated numeric string.
  149. */
  150. static OID(val: string): ASN1;
  151. /**
  152. * Parse a Tag.OID value from ASN.1 object' value.
  153. * @param buf the buffer to parse.
  154. */
  155. static parseOID(buf: Buffer): string;
  156. /**
  157. * Creates an Tag.UTF8 ASN.1 object.
  158. * @param val utf8 string.
  159. */
  160. static UTF8(val: string): ASN1;
  161. /**
  162. * Parse a Tag.UTF8 string from ASN.1 object' value.
  163. * @param buf the buffer to parse.
  164. */
  165. static parseUTF8(buf: Buffer): string;
  166. /**
  167. * Creates an Tag.NUMERICSTRING ASN.1 object.
  168. * @param val numeric string.
  169. */
  170. static NumericString(val: string): ASN1;
  171. /**
  172. * Parse a Tag.UTF8 string from ASN.1 object' value.
  173. * @param buf the buffer to parse.
  174. */
  175. static parseNumericString(buf: Buffer): string;
  176. /**
  177. * Creates an Tag.NUMERICSTRING ASN.1 object.
  178. * @param val printable string.
  179. */
  180. static PrintableString(val: string): ASN1;
  181. /**
  182. * Parse a Tag.PRINTABLESTRING string from ASN.1 object' value.
  183. * @param buf the buffer to parse.
  184. */
  185. static parsePrintableString(buf: Buffer): string;
  186. /**
  187. * Creates an Tag.IA5STRING (ASCII string) ASN.1 object.
  188. * @param val ASCII string.
  189. */
  190. static IA5String(val: string): ASN1;
  191. /**
  192. * Parse a Tag.IA5STRING string from ASN.1 object' value.
  193. * @param buf the buffer to parse.
  194. */
  195. static parseIA5String(buf: Buffer): string;
  196. /**
  197. * Creates an Tag.T61STRING (8-bit clean string) ASN.1 object.
  198. * @param val 8-bit clean string.
  199. */
  200. static T61String(val: string): ASN1;
  201. /**
  202. * Parse a Tag.T61STRING string from ASN.1 object' value.
  203. * @param buf the buffer to parse.
  204. */
  205. static parseT61String(buf: Buffer): string;
  206. /**
  207. * Creates an Tag.GENERALSTRING (specified in ISO-2022/ECMA-35) ASN.1 object.
  208. * @param val general string.
  209. */
  210. static GeneralString(val: string): ASN1;
  211. /**
  212. * Parse a Tag.GENERALSTRING string from ASN.1 object' value.
  213. * @param buf the buffer to parse.
  214. */
  215. static parseGeneralString(buf: Buffer): string;
  216. /**
  217. * Creates an Tag.UTCTIME ASN.1 object.
  218. *
  219. * Note: GeneralizedTime has 4 digits for the year and is used for X.509.
  220. * dates past 2049. Converting to a GeneralizedTime hasn't been implemented yet.
  221. * @param date date value.
  222. */
  223. static UTCTime(date: Date): ASN1;
  224. /**
  225. * Parse a Tag.UTCTIME date from ASN.1 object' value.
  226. * @param buf the buffer to parse.
  227. */
  228. static parseUTCTime(buf: Buffer): Date;
  229. /**
  230. * Creates an Tag.GENERALIZEDTIME ASN.1 object.
  231. * @param date date value.
  232. */
  233. static GeneralizedTime(date: Date): ASN1;
  234. /**
  235. * Parse a Tag.GENERALIZEDTIME date from ASN.1 object' value.
  236. * @param buf the buffer to parse.
  237. */
  238. static parseGeneralizedTime(buf: Buffer): Date;
  239. /**
  240. * Parse a Tag.UTCTIME date of Tag.GENERALIZEDTIME date from ASN.1 object' value.
  241. * @param tag the type.
  242. * @param buf the buffer to parse.
  243. */
  244. static parseTime(tag: Tag, buf: Buffer): Date;
  245. /**
  246. * Creates an Tag.SET ASN.1 object.
  247. * @param objs an array of ASN.1 objects.
  248. */
  249. static Set(objs: ASN1[]): ASN1;
  250. /**
  251. * Creates an Tag.SEQUENCE ASN.1 object.
  252. * @param objs an array of ASN.1 objects.
  253. */
  254. static Seq(objs: ASN1[]): ASN1;
  255. /**
  256. * Creates an Class.CONTEXT_SPECIFIC ASN.1 object.
  257. *
  258. * Note: the tag means nothing with Class.CONTEXT_SPECIFIC
  259. * @param tag number.
  260. * @param objs an array of ASN.1 objects or a ASN.1 object.
  261. * @param isCompound when objs is a array, the isCompound will be set to true.
  262. */
  263. static Spec(tag: Tag, objs: ASN1 | ASN1[], isCompound?: boolean): ASN1;
  264. /**
  265. * Parse a ASN.1 object from a buffer in DER format.
  266. *
  267. * @param buf the buffer to parse.
  268. * @param deepParse deeply parse or not.
  269. */
  270. static fromDER(buf: Buffer, deepParse?: boolean): ASN1;
  271. /**
  272. * Parse a ASN.1 object from a buffer in DER format with given class and tag.
  273. * If class or tag is not match, it will throw a error.
  274. *
  275. * @param tagClass expect class to parse.
  276. * @param tag expect type to parse.
  277. * @param buf the buffer to parse.
  278. */
  279. static parseDER(buf: Buffer, tagClass: Class, tag: Tag): ASN1;
  280. /**
  281. * Parse a ASN.1 object from a buffer in DER format with given Template object.
  282. * If template is not match, it will throw a error.
  283. *
  284. * @param buf the buffer to parse.
  285. * @param tpl expect template to parse.
  286. *
  287. * @return a Captures object with captured ASN.1 objects
  288. */
  289. static parseDERWithTemplate(buf: Buffer, tpl: Template): Captures;
  290. private static _parseCompound;
  291. private static _fromDER;
  292. readonly class: Class;
  293. readonly tag: Tag;
  294. readonly bytes: Buffer;
  295. readonly isCompound: boolean;
  296. private _value;
  297. private _der;
  298. constructor(tagClass: Class, tag: Tag, data: Buffer, isCompound?: boolean);
  299. /**
  300. * the well parsed value of this ASN.1 object.
  301. * It will be boolean, number, string, BitString, Date, array of ASN.1 objects and so on.
  302. */
  303. readonly value: any;
  304. /**
  305. * the DER format Buffer of this ASN.1 object.
  306. */
  307. readonly DER: Buffer;
  308. /**
  309. * Expecting it is compound ASN.1 object and returns an array of sub ASN.1 objects.
  310. * @param msg error message to throw when it is not compound ASN.1 object.
  311. */
  312. mustCompound(msg?: string): ASN1[];
  313. /**
  314. * Returns true if two ASN.1 objects equally.
  315. * @param obj another ASN.1 object.
  316. */
  317. equals(obj: ASN1): boolean;
  318. /**
  319. * Converts this ASN.1 object to a buffer of bytes in DER format.
  320. */
  321. toDER(): Buffer;
  322. /**
  323. * Parse the value of this ASN.1 object when it is Class.UNIVERSAL.
  324. * The value will be boolean, number, string, BitString, Date, array of ASN.1 objects and so on.
  325. */
  326. valueOf(): any;
  327. /**
  328. * Validates that the given ASN.1 object is at least a super set of the
  329. * given ASN.1 structure. Only tag classes and types are checked. An
  330. * optional map may also be provided to capture ASN.1 values while the
  331. * structure is checked.
  332. *
  333. * To capture an ASN.1 object, set an object in the validator's 'capture'
  334. * parameter to the key to use in the capture map.
  335. *
  336. * Objects in the validator may set a field 'optional' to true to indicate
  337. * that it isn't necessary to pass validation.
  338. *
  339. * @param tpl Template object to validate.
  340. * @param captures Captures object to capture ASN.1 object.
  341. */
  342. validate(tpl: Template, captures?: Captures): Error | null;
  343. /**
  344. * Return a friendly JSON object for debuging.
  345. */
  346. toJSON(): any;
  347. protected [inspect.custom](_depth: any, options: any): string;
  348. }