summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/SignerInfo.ts
blob: a6f59805dd394ecd5ecc5f125c94ba06cb74749b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier";
import { SignedAndUnsignedAttributes, SignedAndUnsignedAttributesJson, SignedAndUnsignedAttributesSchema } from "./SignedAndUnsignedAttributes";
import { IssuerAndSerialNumber, IssuerAndSerialNumberSchema } from "./IssuerAndSerialNumber";
import * as Schema from "./Schema";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import { AsnError } from "./errors";

const VERSION = "version";
const SID = "sid";
const DIGEST_ALGORITHM = "digestAlgorithm";
const SIGNED_ATTRS = "signedAttrs";
const SIGNATURE_ALGORITHM = "signatureAlgorithm";
const SIGNATURE = "signature";
const UNSIGNED_ATTRS = "unsignedAttrs";
const SIGNER_INFO = "SignerInfo";
const SIGNER_INFO_VERSION = `${SIGNER_INFO}.${VERSION}`;
const SIGNER_INFO_SID = `${SIGNER_INFO}.${SID}`;
const SIGNER_INFO_DIGEST_ALGORITHM = `${SIGNER_INFO}.${DIGEST_ALGORITHM}`;
const SIGNER_INFO_SIGNED_ATTRS = `${SIGNER_INFO}.${SIGNED_ATTRS}`;
const SIGNER_INFO_SIGNATURE_ALGORITHM = `${SIGNER_INFO}.${SIGNATURE_ALGORITHM}`;
const SIGNER_INFO_SIGNATURE = `${SIGNER_INFO}.${SIGNATURE}`;
const SIGNER_INFO_UNSIGNED_ATTRS = `${SIGNER_INFO}.${UNSIGNED_ATTRS}`;
const CLEAR_PROPS = [
  SIGNER_INFO_VERSION,
  SIGNER_INFO_SID,
  SIGNER_INFO_DIGEST_ALGORITHM,
  SIGNER_INFO_SIGNED_ATTRS,
  SIGNER_INFO_SIGNATURE_ALGORITHM,
  SIGNER_INFO_SIGNATURE,
  SIGNER_INFO_UNSIGNED_ATTRS
];

export interface ISignerInfo {
  version: number;
  sid: Schema.SchemaType;
  digestAlgorithm: AlgorithmIdentifier;
  signedAttrs?: SignedAndUnsignedAttributes;
  signatureAlgorithm: AlgorithmIdentifier;
  signature: asn1js.OctetString;
  unsignedAttrs?: SignedAndUnsignedAttributes;
}

export interface SignerInfoJson {
  version: number;
  sid?: Schema.SchemaType;
  digestAlgorithm: AlgorithmIdentifierJson;
  signedAttrs?: SignedAndUnsignedAttributesJson;
  signatureAlgorithm: AlgorithmIdentifierJson;
  signature: asn1js.OctetStringJson;
  unsignedAttrs?: SignedAndUnsignedAttributesJson;
}

export type SignerInfoParameters = PkiObjectParameters & Partial<ISignerInfo>;

/**
 * Represents the SignerInfo structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
 */
export class SignerInfo extends PkiObject implements ISignerInfo {

  public static override CLASS_NAME = "SignerInfo";

  public version!: number;
  public sid: Schema.SchemaType;
  public digestAlgorithm!: AlgorithmIdentifier;
  public signedAttrs?: SignedAndUnsignedAttributes;
  public signatureAlgorithm!: AlgorithmIdentifier;
  public signature!: asn1js.OctetString;
  public unsignedAttrs?: SignedAndUnsignedAttributes;

  /**
   * Initializes a new instance of the {@link SignerInfo} class
   * @param parameters Initialization parameters
   */
  constructor(parameters: SignerInfoParameters = {}) {
    super();

    this.version = pvutils.getParametersValue(parameters, VERSION, SignerInfo.defaultValues(VERSION));
    this.sid = pvutils.getParametersValue(parameters, SID, SignerInfo.defaultValues(SID));
    this.digestAlgorithm = pvutils.getParametersValue(parameters, DIGEST_ALGORITHM, SignerInfo.defaultValues(DIGEST_ALGORITHM));
    if (SIGNED_ATTRS in parameters) {
      this.signedAttrs = pvutils.getParametersValue(parameters, SIGNED_ATTRS, SignerInfo.defaultValues(SIGNED_ATTRS));
    }
    this.signatureAlgorithm = pvutils.getParametersValue(parameters, SIGNATURE_ALGORITHM, SignerInfo.defaultValues(SIGNATURE_ALGORITHM));
    this.signature = pvutils.getParametersValue(parameters, SIGNATURE, SignerInfo.defaultValues(SIGNATURE));
    if (UNSIGNED_ATTRS in parameters) {
      this.unsignedAttrs = pvutils.getParametersValue(parameters, UNSIGNED_ATTRS, SignerInfo.defaultValues(UNSIGNED_ATTRS));
    }

    if (parameters.schema) {
      this.fromSchema(parameters.schema);
    }
  }

  /**
   * Returns default values for all class members
   * @param memberName String name for a class member
   * @returns Default value
   */
  public static override defaultValues(memberName: typeof VERSION): number;
  public static override defaultValues(memberName: typeof SID): Schema.SchemaType;
  public static override defaultValues(memberName: typeof DIGEST_ALGORITHM): AlgorithmIdentifier;
  public static override defaultValues(memberName: typeof SIGNED_ATTRS): SignedAndUnsignedAttributes;
  public static override defaultValues(memberName: typeof SIGNATURE_ALGORITHM): AlgorithmIdentifier;
  public static override defaultValues(memberName: typeof SIGNATURE): asn1js.OctetString;
  public static override defaultValues(memberName: typeof UNSIGNED_ATTRS): SignedAndUnsignedAttributes;
  public static override defaultValues(memberName: string): any {
    switch (memberName) {
      case VERSION:
        return 0;
      case SID:
        return new asn1js.Any();
      case DIGEST_ALGORITHM:
        return new AlgorithmIdentifier();
      case SIGNED_ATTRS:
        return new SignedAndUnsignedAttributes({ type: 0 });
      case SIGNATURE_ALGORITHM:
        return new AlgorithmIdentifier();
      case SIGNATURE:
        return new asn1js.OctetString();
      case UNSIGNED_ATTRS:
        return new SignedAndUnsignedAttributes({ type: 1 });
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * Compare values with default values for all class members
   * @param memberName String name for a class member
   * @param memberValue Value to compare with default value
   */
  public static compareWithDefault(memberName: string, memberValue: any): boolean {
    switch (memberName) {
      case VERSION:
        return (SignerInfo.defaultValues(VERSION) === memberValue);
      case SID:
        return (memberValue instanceof asn1js.Any);
      case DIGEST_ALGORITHM:
        if ((memberValue instanceof AlgorithmIdentifier) === false)
          return false;

        return memberValue.isEqual(SignerInfo.defaultValues(DIGEST_ALGORITHM));
      case SIGNED_ATTRS:
        return ((SignedAndUnsignedAttributes.compareWithDefault("type", memberValue.type))
          && (SignedAndUnsignedAttributes.compareWithDefault("attributes", memberValue.attributes))
          && (SignedAndUnsignedAttributes.compareWithDefault("encodedValue", memberValue.encodedValue)));
      case SIGNATURE_ALGORITHM:
        if ((memberValue instanceof AlgorithmIdentifier) === false)
          return false;

        return memberValue.isEqual(SignerInfo.defaultValues(SIGNATURE_ALGORITHM));
      case SIGNATURE:
      case UNSIGNED_ATTRS:
        return ((SignedAndUnsignedAttributes.compareWithDefault("type", memberValue.type))
          && (SignedAndUnsignedAttributes.compareWithDefault("attributes", memberValue.attributes))
          && (SignedAndUnsignedAttributes.compareWithDefault("encodedValue", memberValue.encodedValue)));
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * @inheritdoc
   * @asn ASN.1 schema
   * ```asn
   * SignerInfo ::= SEQUENCE {
   *    version CMSVersion,
   *    sid SignerIdentifier,
   *    digestAlgorithm DigestAlgorithmIdentifier,
   *    signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL,
   *    signatureAlgorithm SignatureAlgorithmIdentifier,
   *    signature SignatureValue,
   *    unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL }
   *
   * SignerIdentifier ::= CHOICE {
   *    issuerAndSerialNumber IssuerAndSerialNumber,
   *    subjectKeyIdentifier [0] SubjectKeyIdentifier }
   *
   * SubjectKeyIdentifier ::= OCTET STRING
   *```
   */
  public static override schema(parameters: Schema.SchemaParameters<{
    version?: string;
    sidSchema?: IssuerAndSerialNumberSchema;
    sid?: string;
    digestAlgorithm?: AlgorithmIdentifierSchema;
    signedAttrs?: SignedAndUnsignedAttributesSchema;
    signatureAlgorithm?: AlgorithmIdentifierSchema;
    signature?: string;
    unsignedAttrs?: SignedAndUnsignedAttributesSchema;
  }> = {}): Schema.SchemaType {
    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});

    return (
      new asn1js.Sequence({
        name: SIGNER_INFO,
        value: [
          new asn1js.Integer({ name: (names.version || SIGNER_INFO_VERSION) }),
          new asn1js.Choice({
            value: [
              IssuerAndSerialNumber.schema(names.sidSchema || {
                names: {
                  blockName: SIGNER_INFO_SID
                }
              }),
              new asn1js.Choice({
                value: [
                  new asn1js.Constructed({
                    optional: true,
                    name: (names.sid || SIGNER_INFO_SID),
                    idBlock: {
                      tagClass: 3, // CONTEXT-SPECIFIC
                      tagNumber: 0 // [0]
                    },
                    value: [new asn1js.OctetString()]
                  }),
                  new asn1js.Primitive({
                    optional: true,
                    name: (names.sid || SIGNER_INFO_SID),
                    idBlock: {
                      tagClass: 3, // CONTEXT-SPECIFIC
                      tagNumber: 0 // [0]
                    }
                  }),
                ]
              }),
            ]
          }),
          AlgorithmIdentifier.schema(names.digestAlgorithm || {
            names: {
              blockName: SIGNER_INFO_DIGEST_ALGORITHM
            }
          }),
          SignedAndUnsignedAttributes.schema(names.signedAttrs || {
            names: {
              blockName: SIGNER_INFO_SIGNED_ATTRS,
              tagNumber: 0
            }
          }),
          AlgorithmIdentifier.schema(names.signatureAlgorithm || {
            names: {
              blockName: SIGNER_INFO_SIGNATURE_ALGORITHM
            }
          }),
          new asn1js.OctetString({ name: (names.signature || SIGNER_INFO_SIGNATURE) }),
          SignedAndUnsignedAttributes.schema(names.unsignedAttrs || {
            names: {
              blockName: SIGNER_INFO_UNSIGNED_ATTRS,
              tagNumber: 1
            }
          })
        ]
      })
    );
  }

  public fromSchema(schema: Schema.SchemaType): void {
    // Clear input data first
    pvutils.clearProps(schema, CLEAR_PROPS);

    // Check the schema is valid
    const asn1 = asn1js.compareSchema(schema,
      schema,
      SignerInfo.schema()
    );
    AsnError.assertSchema(asn1, this.className);

    // Get internal properties from parsed schema
    this.version = asn1.result[SIGNER_INFO_VERSION].valueBlock.valueDec;

    const currentSid = asn1.result[SIGNER_INFO_SID];
    if (currentSid.idBlock.tagClass === 1)
      this.sid = new IssuerAndSerialNumber({ schema: currentSid });
    else
      this.sid = currentSid;

    this.digestAlgorithm = new AlgorithmIdentifier({ schema: asn1.result[SIGNER_INFO_DIGEST_ALGORITHM] });
    if (SIGNER_INFO_SIGNED_ATTRS in asn1.result)
      this.signedAttrs = new SignedAndUnsignedAttributes({ type: 0, schema: asn1.result[SIGNER_INFO_SIGNED_ATTRS] });

    this.signatureAlgorithm = new AlgorithmIdentifier({ schema: asn1.result[SIGNER_INFO_SIGNATURE_ALGORITHM] });
    this.signature = asn1.result[SIGNER_INFO_SIGNATURE];
    if (SIGNER_INFO_UNSIGNED_ATTRS in asn1.result)
      this.unsignedAttrs = new SignedAndUnsignedAttributes({ type: 1, schema: asn1.result[SIGNER_INFO_UNSIGNED_ATTRS] });
  }

  public toSchema(): asn1js.Sequence {
    if (SignerInfo.compareWithDefault(SID, this.sid))
      throw new Error("Incorrectly initialized \"SignerInfo\" class");

    //#region Create array for output sequence
    const outputArray = [];

    outputArray.push(new asn1js.Integer({ value: this.version }));

    if (this.sid instanceof IssuerAndSerialNumber)
      outputArray.push(this.sid.toSchema());
    else
      outputArray.push(this.sid);

    outputArray.push(this.digestAlgorithm.toSchema());

    if (this.signedAttrs) {
      if (SignerInfo.compareWithDefault(SIGNED_ATTRS, this.signedAttrs) === false)
        outputArray.push(this.signedAttrs.toSchema());
    }

    outputArray.push(this.signatureAlgorithm.toSchema());
    outputArray.push(this.signature);

    if (this.unsignedAttrs) {
      if (SignerInfo.compareWithDefault(UNSIGNED_ATTRS, this.unsignedAttrs) === false)
        outputArray.push(this.unsignedAttrs.toSchema());
    }
    //#endregion

    //#region Construct and return new ASN.1 schema for this object
    return (new asn1js.Sequence({
      value: outputArray
    }));
    //#endregion
  }

  public toJSON(): SignerInfoJson {
    if (SignerInfo.compareWithDefault(SID, this.sid)) {
      throw new Error("Incorrectly initialized \"SignerInfo\" class");
    }

    const res: SignerInfoJson = {
      version: this.version,
      digestAlgorithm: this.digestAlgorithm.toJSON(),
      signatureAlgorithm: this.signatureAlgorithm.toJSON(),
      signature: this.signature.toJSON(),
    };

    if (!(this.sid instanceof asn1js.Any))
      res.sid = this.sid.toJSON();

    if (this.signedAttrs && SignerInfo.compareWithDefault(SIGNED_ATTRS, this.signedAttrs) === false) {
      res.signedAttrs = this.signedAttrs.toJSON();
    }

    if (this.unsignedAttrs && SignerInfo.compareWithDefault(UNSIGNED_ATTRS, this.unsignedAttrs) === false) {
      res.unsignedAttrs = this.unsignedAttrs.toJSON();
    }

    return res;
  }

}