serializer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import * as asn1js from "asn1js";
  2. import { toArrayBuffer } from "@peculiar/utils";
  3. import * as converters from "./converters.js";
  4. import { AsnPropTypes, AsnTypeTypes } from "./enums.js";
  5. import { isConvertible, isArrayEqual } from "./helper.js";
  6. import { schemaStorage } from "./storage.js";
  7. export class AsnSerializer {
  8. static serialize(obj) {
  9. if (obj instanceof asn1js.BaseBlock) {
  10. return obj.toBER(false);
  11. }
  12. return this.toASN(obj).toBER(false);
  13. }
  14. static toASN(obj) {
  15. if (obj && typeof obj === "object" && isConvertible(obj)) {
  16. return obj.toASN();
  17. }
  18. if (!(obj && typeof obj === "object")) {
  19. throw new TypeError("Parameter 1 should be type of Object.");
  20. }
  21. const target = obj.constructor;
  22. const schema = schemaStorage.get(target);
  23. schemaStorage.cache(target);
  24. let asn1Value = [];
  25. if (schema.itemType) {
  26. if (!Array.isArray(obj)) {
  27. throw new TypeError("Parameter 1 should be type of Array.");
  28. }
  29. if (typeof schema.itemType === "number") {
  30. const converter = converters.defaultConverter(schema.itemType);
  31. if (!converter) {
  32. throw new Error(`Cannot get default converter for array item of ${target.name} ASN1 schema`);
  33. }
  34. asn1Value = obj.map((o) => converter.toASN(o));
  35. }
  36. else {
  37. asn1Value = obj.map((o) => this.toAsnItem({ type: schema.itemType }, "[]", target, o));
  38. }
  39. }
  40. else {
  41. for (const key in schema.items) {
  42. const schemaItem = schema.items[key];
  43. const objProp = obj[key];
  44. if (objProp === undefined ||
  45. schemaItem.defaultValue === objProp ||
  46. (typeof schemaItem.defaultValue === "object" && typeof objProp === "object" && isArrayEqual(this.serialize(schemaItem.defaultValue), this.serialize(objProp)))) {
  47. continue;
  48. }
  49. const asn1Item = AsnSerializer.toAsnItem(schemaItem, key, target, objProp);
  50. if (typeof schemaItem.context === "number") {
  51. if (schemaItem.implicit) {
  52. if (!schemaItem.repeated && (typeof schemaItem.type === "number" || isConvertible(schemaItem.type))) {
  53. const value = {};
  54. value.valueHex = asn1Item instanceof asn1js.Null ? toArrayBuffer(asn1Item.valueBeforeDecodeView) : asn1Item.valueBlock.toBER();
  55. asn1Value.push(new asn1js.Primitive({
  56. optional: schemaItem.optional,
  57. idBlock: {
  58. tagClass: 3,
  59. tagNumber: schemaItem.context,
  60. },
  61. ...value,
  62. }));
  63. }
  64. else {
  65. asn1Value.push(new asn1js.Constructed({
  66. optional: schemaItem.optional,
  67. idBlock: {
  68. tagClass: 3,
  69. tagNumber: schemaItem.context,
  70. },
  71. value: asn1Item.valueBlock.value,
  72. }));
  73. }
  74. }
  75. else {
  76. asn1Value.push(new asn1js.Constructed({
  77. optional: schemaItem.optional,
  78. idBlock: {
  79. tagClass: 3,
  80. tagNumber: schemaItem.context,
  81. },
  82. value: [asn1Item],
  83. }));
  84. }
  85. }
  86. else if (schemaItem.repeated) {
  87. asn1Value = asn1Value.concat(asn1Item);
  88. }
  89. else {
  90. asn1Value.push(asn1Item);
  91. }
  92. }
  93. }
  94. let asnSchema;
  95. switch (schema.type) {
  96. case AsnTypeTypes.Sequence:
  97. asnSchema = new asn1js.Sequence({ value: asn1Value });
  98. break;
  99. case AsnTypeTypes.Set:
  100. asnSchema = new asn1js.Set({ value: asn1Value });
  101. break;
  102. case AsnTypeTypes.Choice:
  103. if (!asn1Value[0]) {
  104. throw new Error(`Schema '${target.name}' has wrong data. Choice cannot be empty.`);
  105. }
  106. asnSchema = asn1Value[0];
  107. break;
  108. }
  109. return asnSchema;
  110. }
  111. static toAsnItem(schemaItem, key, target, objProp) {
  112. let asn1Item;
  113. if (typeof schemaItem.type === "number") {
  114. const converter = schemaItem.converter;
  115. if (!converter) {
  116. throw new Error(`Property '${key}' doesn't have converter for type ${AsnPropTypes[schemaItem.type]} in schema '${target.name}'`);
  117. }
  118. if (schemaItem.repeated) {
  119. if (!Array.isArray(objProp)) {
  120. throw new TypeError("Parameter 'objProp' should be type of Array.");
  121. }
  122. const items = Array.from(objProp, (element) => converter.toASN(element));
  123. const Container = schemaItem.repeated === "sequence" ? asn1js.Sequence : asn1js.Set;
  124. asn1Item = new Container({ value: items });
  125. }
  126. else {
  127. asn1Item = converter.toASN(objProp);
  128. }
  129. }
  130. else {
  131. if (schemaItem.repeated) {
  132. if (!Array.isArray(objProp)) {
  133. throw new TypeError("Parameter 'objProp' should be type of Array.");
  134. }
  135. const items = Array.from(objProp, (element) => this.toASN(element));
  136. const Container = schemaItem.repeated === "sequence" ? asn1js.Sequence : asn1js.Set;
  137. asn1Item = new Container({ value: items });
  138. }
  139. else {
  140. asn1Item = this.toASN(objProp);
  141. }
  142. }
  143. return asn1Item;
  144. }
  145. }