parser.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import * as asn1js from "asn1js";
  2. import { toArrayBuffer } from "@peculiar/utils/bytes";
  3. import { AsnPropTypes, AsnTypeTypes } from "./enums.js";
  4. import * as converters from "./converters.js";
  5. import { AsnSchemaValidationError } from "./errors/index.js";
  6. import { isConvertible, isTypeOfArray } from "./helper.js";
  7. import { schemaStorage } from "./storage.js";
  8. export class AsnParser {
  9. static parse(data, target) {
  10. const asn1Parsed = asn1js.fromBER(toArrayBuffer(data));
  11. if (asn1Parsed.result.error) {
  12. throw new Error(asn1Parsed.result.error);
  13. }
  14. const res = this.fromASN(asn1Parsed.result, target);
  15. return res;
  16. }
  17. static fromASN(asn1Schema, target) {
  18. try {
  19. if (isConvertible(target)) {
  20. const value = new target();
  21. return value.fromASN(asn1Schema);
  22. }
  23. const schema = schemaStorage.get(target);
  24. schemaStorage.cache(target);
  25. let targetSchema = schema.schema;
  26. const choiceResult = this.handleChoiceTypes(asn1Schema, schema, target, targetSchema);
  27. if (choiceResult?.result) {
  28. return choiceResult.result;
  29. }
  30. if (choiceResult?.targetSchema) {
  31. targetSchema = choiceResult.targetSchema;
  32. }
  33. const sequenceResult = this.handleSequenceTypes(asn1Schema, schema, target, targetSchema);
  34. const res = new target();
  35. if (isTypeOfArray(target)) {
  36. return this.handleArrayTypes(asn1Schema, schema, target);
  37. }
  38. this.processSchemaItems(schema, sequenceResult, res);
  39. return res;
  40. }
  41. catch (error) {
  42. if (error instanceof AsnSchemaValidationError) {
  43. error.schemas.push(target.name);
  44. }
  45. throw error;
  46. }
  47. }
  48. static handleChoiceTypes(asn1Schema, schema, target, targetSchema) {
  49. if (asn1Schema.constructor === asn1js.Constructed
  50. && schema.type === AsnTypeTypes.Choice
  51. && asn1Schema.idBlock.tagClass === 3) {
  52. for (const key in schema.items) {
  53. const schemaItem = schema.items[key];
  54. if (schemaItem.context === asn1Schema.idBlock.tagNumber && schemaItem.implicit) {
  55. if (typeof schemaItem.type === "function"
  56. && schemaStorage.has(schemaItem.type)) {
  57. const fieldSchema = schemaStorage.get(schemaItem.type);
  58. if (fieldSchema && fieldSchema.type === AsnTypeTypes.Sequence) {
  59. const newSeq = new asn1js.Sequence();
  60. if ("value" in asn1Schema.valueBlock
  61. && Array.isArray(asn1Schema.valueBlock.value)
  62. && "value" in newSeq.valueBlock) {
  63. newSeq.valueBlock.value = asn1Schema.valueBlock.value;
  64. const fieldValue = this.fromASN(newSeq, schemaItem.type);
  65. const res = new target();
  66. res[key] = fieldValue;
  67. return { result: res };
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. else if (asn1Schema.constructor === asn1js.Constructed
  75. && schema.type !== AsnTypeTypes.Choice) {
  76. const newTargetSchema = new asn1js.Constructed({
  77. idBlock: {
  78. tagClass: 3,
  79. tagNumber: asn1Schema.idBlock.tagNumber,
  80. },
  81. value: schema.schema.valueBlock.value,
  82. });
  83. for (const key in schema.items) {
  84. delete asn1Schema[key];
  85. }
  86. return { targetSchema: newTargetSchema };
  87. }
  88. return null;
  89. }
  90. static handleSequenceTypes(asn1Schema, schema, target, targetSchema) {
  91. if (schema.type === AsnTypeTypes.Sequence) {
  92. const asn1ComparedSchema = asn1js.compareSchema({}, asn1Schema, targetSchema);
  93. if (!asn1ComparedSchema.verified) {
  94. throw new AsnSchemaValidationError(`Data does not match to ${target.name} ASN1 schema.${asn1ComparedSchema.result.error ? ` ${asn1ComparedSchema.result.error}` : ""}`);
  95. }
  96. return asn1ComparedSchema;
  97. }
  98. else {
  99. const asn1ComparedSchema = asn1js.compareSchema({}, asn1Schema, targetSchema);
  100. if (!asn1ComparedSchema.verified) {
  101. throw new AsnSchemaValidationError(`Data does not match to ${target.name} ASN1 schema.${asn1ComparedSchema.result.error ? ` ${asn1ComparedSchema.result.error}` : ""}`);
  102. }
  103. return asn1ComparedSchema;
  104. }
  105. }
  106. static processRepeatedField(asn1Elements, asn1Index, schemaItem) {
  107. let elementsToProcess = asn1Elements.slice(asn1Index);
  108. if (elementsToProcess.length === 1 && elementsToProcess[0].constructor.name === "Sequence") {
  109. const seq = elementsToProcess[0];
  110. if (seq.valueBlock && seq.valueBlock.value && Array.isArray(seq.valueBlock.value)) {
  111. elementsToProcess = seq.valueBlock.value;
  112. }
  113. }
  114. if (typeof schemaItem.type === "number") {
  115. const converter = converters.defaultConverter(schemaItem.type);
  116. if (!converter)
  117. throw new Error(`No converter for ASN.1 type ${schemaItem.type}`);
  118. return elementsToProcess
  119. .filter((el) => el && el.valueBlock)
  120. .map((el) => {
  121. try {
  122. return converter.fromASN(el);
  123. }
  124. catch {
  125. return undefined;
  126. }
  127. })
  128. .filter((v) => v !== undefined);
  129. }
  130. else {
  131. return elementsToProcess
  132. .filter((el) => el && el.valueBlock)
  133. .map((el) => {
  134. try {
  135. return this.fromASN(el, schemaItem.type);
  136. }
  137. catch {
  138. return undefined;
  139. }
  140. })
  141. .filter((v) => v !== undefined);
  142. }
  143. }
  144. static processPrimitiveField(asn1Element, schemaItem) {
  145. const converter = converters.defaultConverter(schemaItem.type);
  146. if (!converter)
  147. throw new Error(`No converter for ASN.1 type ${schemaItem.type}`);
  148. return converter.fromASN(asn1Element);
  149. }
  150. static isOptionalChoiceField(schemaItem) {
  151. return (schemaItem.optional
  152. && typeof schemaItem.type === "function"
  153. && schemaStorage.has(schemaItem.type)
  154. && schemaStorage.get(schemaItem.type).type === AsnTypeTypes.Choice);
  155. }
  156. static processOptionalChoiceField(asn1Element, schemaItem) {
  157. try {
  158. const value = this.fromASN(asn1Element, schemaItem.type);
  159. return {
  160. processed: true, value,
  161. };
  162. }
  163. catch (err) {
  164. if (err instanceof AsnSchemaValidationError
  165. && /Wrong values for Choice type/.test(err.message)) {
  166. return { processed: false };
  167. }
  168. throw err;
  169. }
  170. }
  171. static handleArrayTypes(asn1Schema, schema, target) {
  172. if (!("value" in asn1Schema.valueBlock && Array.isArray(asn1Schema.valueBlock.value))) {
  173. throw new Error("Cannot get items from the ASN.1 parsed value. ASN.1 object is not constructed.");
  174. }
  175. const itemType = schema.itemType;
  176. if (typeof itemType === "number") {
  177. const converter = converters.defaultConverter(itemType);
  178. if (!converter) {
  179. throw new Error(`Cannot get default converter for array item of ${target.name} ASN1 schema`);
  180. }
  181. return target.from(asn1Schema.valueBlock.value, (element) => converter.fromASN(element));
  182. }
  183. else {
  184. return target.from(asn1Schema.valueBlock.value, (element) => this.fromASN(element, itemType));
  185. }
  186. }
  187. static processSchemaItems(schema, asn1ComparedSchema, res) {
  188. for (const key in schema.items) {
  189. const asn1SchemaValue = asn1ComparedSchema.result[key];
  190. if (!asn1SchemaValue) {
  191. continue;
  192. }
  193. const schemaItem = schema.items[key];
  194. const schemaItemType = schemaItem.type;
  195. let parsedValue;
  196. if (typeof schemaItemType === "number" || isConvertible(schemaItemType)) {
  197. parsedValue = this.processPrimitiveSchemaItem(asn1SchemaValue, schemaItem, schemaItemType);
  198. }
  199. else {
  200. parsedValue = this.processComplexSchemaItem(asn1SchemaValue, schemaItem, schemaItemType);
  201. }
  202. if (parsedValue
  203. && typeof parsedValue === "object"
  204. && "value" in parsedValue
  205. && "raw" in parsedValue) {
  206. res[key] = parsedValue.value;
  207. res[`${key}Raw`] = parsedValue.raw;
  208. }
  209. else {
  210. res[key] = parsedValue;
  211. }
  212. }
  213. }
  214. static processPrimitiveSchemaItem(asn1SchemaValue, schemaItem, schemaItemType) {
  215. const converter = schemaItem.converter
  216. ?? (isConvertible(schemaItemType)
  217. ? new schemaItemType()
  218. : null);
  219. if (!converter) {
  220. throw new Error("Converter is empty");
  221. }
  222. if (schemaItem.repeated) {
  223. return this.processRepeatedPrimitiveItem(asn1SchemaValue, schemaItem, converter);
  224. }
  225. else {
  226. return this.processSinglePrimitiveItem(asn1SchemaValue, schemaItem, schemaItemType, converter);
  227. }
  228. }
  229. static processRepeatedPrimitiveItem(asn1SchemaValue, schemaItem, converter) {
  230. if (schemaItem.implicit) {
  231. const Container = schemaItem.repeated === "sequence" ? asn1js.Sequence : asn1js.Set;
  232. const newItem = new Container();
  233. newItem.valueBlock = asn1SchemaValue.valueBlock;
  234. const newItemAsn = asn1js.fromBER(newItem.toBER(false));
  235. if (newItemAsn.offset === -1) {
  236. throw new Error(`Cannot parse the child item. ${newItemAsn.result.error}`);
  237. }
  238. if (!("value" in newItemAsn.result.valueBlock
  239. && Array.isArray(newItemAsn.result.valueBlock.value))) {
  240. throw new Error("Cannot get items from the ASN.1 parsed value. ASN.1 object is not constructed.");
  241. }
  242. const value = newItemAsn.result.valueBlock.value;
  243. return Array.from(value, (element) => converter.fromASN(element));
  244. }
  245. else {
  246. return Array.from(asn1SchemaValue, (element) => converter.fromASN(element));
  247. }
  248. }
  249. static processSinglePrimitiveItem(asn1SchemaValue, schemaItem, schemaItemType, converter) {
  250. let value = asn1SchemaValue;
  251. if (schemaItem.implicit) {
  252. let newItem;
  253. if (isConvertible(schemaItemType)) {
  254. newItem = new schemaItemType().toSchema("");
  255. }
  256. else {
  257. const Asn1TypeName = AsnPropTypes[schemaItemType];
  258. const Asn1Type = asn1js[Asn1TypeName];
  259. if (!Asn1Type) {
  260. throw new Error(`Cannot get '${Asn1TypeName}' class from asn1js module`);
  261. }
  262. newItem = new Asn1Type();
  263. }
  264. newItem.valueBlock = value.valueBlock;
  265. value = asn1js.fromBER(newItem.toBER(false)).result;
  266. }
  267. return converter.fromASN(value);
  268. }
  269. static processComplexSchemaItem(asn1SchemaValue, schemaItem, schemaItemType) {
  270. if (schemaItem.repeated) {
  271. if (!Array.isArray(asn1SchemaValue)) {
  272. throw new Error("Cannot get list of items from the ASN.1 parsed value. ASN.1 value should be iterable.");
  273. }
  274. return Array.from(asn1SchemaValue, (element) => this.fromASN(element, schemaItemType));
  275. }
  276. else {
  277. const valueToProcess = this.handleImplicitTagging(asn1SchemaValue, schemaItem, schemaItemType);
  278. if (this.isOptionalChoiceField(schemaItem)) {
  279. try {
  280. return this.fromASN(valueToProcess, schemaItemType);
  281. }
  282. catch (err) {
  283. if (err instanceof AsnSchemaValidationError
  284. && /Wrong values for Choice type/.test(err.message)) {
  285. return undefined;
  286. }
  287. throw err;
  288. }
  289. }
  290. else {
  291. const parsedValue = this.fromASN(valueToProcess, schemaItemType);
  292. if (schemaItem.raw) {
  293. return {
  294. value: parsedValue,
  295. raw: asn1SchemaValue.valueBeforeDecodeView,
  296. };
  297. }
  298. return parsedValue;
  299. }
  300. }
  301. }
  302. static handleImplicitTagging(asn1SchemaValue, schemaItem, schemaItemType) {
  303. if (schemaItem.implicit && typeof schemaItem.context === "number") {
  304. const schema = schemaStorage.get(schemaItemType);
  305. if (schema.type === AsnTypeTypes.Sequence) {
  306. const newSeq = new asn1js.Sequence();
  307. if ("value" in asn1SchemaValue.valueBlock
  308. && Array.isArray(asn1SchemaValue.valueBlock.value)
  309. && "value" in newSeq.valueBlock) {
  310. newSeq.valueBlock.value = asn1SchemaValue.valueBlock.value;
  311. return newSeq;
  312. }
  313. }
  314. else if (schema.type === AsnTypeTypes.Set) {
  315. const newSet = new asn1js.Set();
  316. if ("value" in asn1SchemaValue.valueBlock
  317. && Array.isArray(asn1SchemaValue.valueBlock.value)
  318. && "value" in newSet.valueBlock) {
  319. newSet.valueBlock.value = asn1SchemaValue.valueBlock.value;
  320. return newSet;
  321. }
  322. }
  323. }
  324. return asn1SchemaValue;
  325. }
  326. }