summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/RevokedCertificate.ts
blob: 9bfa8887c357ba38dae111b9c1e70f2102b0fba7 (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
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { Time, TimeJson } from "./Time";
import { Extensions, ExtensionsJson } from "./Extensions";
import * as Schema from "./Schema";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import { AsnError } from "./errors";
import { EMPTY_STRING } from "./constants";

const USER_CERTIFICATE = "userCertificate";
const REVOCATION_DATE = "revocationDate";
const CRL_ENTRY_EXTENSIONS = "crlEntryExtensions";
const CLEAR_PROPS = [
  USER_CERTIFICATE,
  REVOCATION_DATE,
  CRL_ENTRY_EXTENSIONS
];

export interface IRevokedCertificate {
  userCertificate: asn1js.Integer;
  revocationDate: Time;
  crlEntryExtensions?: Extensions;
}

export type RevokedCertificateParameters = PkiObjectParameters & Partial<IRevokedCertificate>;

export interface RevokedCertificateJson {
  userCertificate: asn1js.IntegerJson;
  revocationDate: TimeJson;
  crlEntryExtensions?: ExtensionsJson;
}

/**
 * Represents the RevokedCertificate structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280)
 */
export class RevokedCertificate extends PkiObject implements IRevokedCertificate {

  public static override CLASS_NAME = "RevokedCertificate";

  public userCertificate!: asn1js.Integer;
  public revocationDate!: Time;
  public crlEntryExtensions?: Extensions;

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

    this.userCertificate = pvutils.getParametersValue(parameters, USER_CERTIFICATE, RevokedCertificate.defaultValues(USER_CERTIFICATE));
    this.revocationDate = pvutils.getParametersValue(parameters, REVOCATION_DATE, RevokedCertificate.defaultValues(REVOCATION_DATE));
    if (CRL_ENTRY_EXTENSIONS in parameters) {
      this.crlEntryExtensions = pvutils.getParametersValue(parameters, CRL_ENTRY_EXTENSIONS, RevokedCertificate.defaultValues(CRL_ENTRY_EXTENSIONS));
    }

    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 USER_CERTIFICATE): asn1js.Integer;
  public static override defaultValues(memberName: typeof REVOCATION_DATE): Time;
  public static override defaultValues(memberName: typeof CRL_ENTRY_EXTENSIONS): Extensions;
  public static override defaultValues(memberName: string): any {
    switch (memberName) {
      case USER_CERTIFICATE:
        return new asn1js.Integer();
      case REVOCATION_DATE:
        return new Time();
      case CRL_ENTRY_EXTENSIONS:
        return new Extensions();
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * @inheritdoc
   * @asn ASN.1 schema
   * ```asn
   * revokedCertificates     SEQUENCE OF SEQUENCE  {
     *        userCertificate         CertificateSerialNumber,
     *        revocationDate          Time,
     *        crlEntryExtensions      Extensions OPTIONAL
     *                                 -- if present, version MUST be v2
     *                             }  OPTIONAL,
   *```
   */
  public static override schema(parameters: Schema.SchemaParameters<{
    userCertificate?: string;
    revocationDate?: string;
    crlEntryExtensions?: string;
  }> = {}): Schema.SchemaType {
    const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});

    return new asn1js.Sequence({
      name: (names.blockName || EMPTY_STRING),
      value: [
        new asn1js.Integer({ name: (names.userCertificate || USER_CERTIFICATE) }),
        Time.schema({
          names: {
            utcTimeName: (names.revocationDate || REVOCATION_DATE),
            generalTimeName: (names.revocationDate || REVOCATION_DATE)
          }
        }),
        Extensions.schema({
          names: {
            blockName: (names.crlEntryExtensions || CRL_ENTRY_EXTENSIONS)
          }
        }, true)
      ]
    });
  }

  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,
      RevokedCertificate.schema()
    );
    AsnError.assertSchema(asn1, this.className);

    // Get internal properties from parsed schema
    this.userCertificate = asn1.result.userCertificate;
    this.revocationDate = new Time({ schema: asn1.result.revocationDate });
    if (CRL_ENTRY_EXTENSIONS in asn1.result) {
      this.crlEntryExtensions = new Extensions({ schema: asn1.result.crlEntryExtensions });
    }
  }

  public toSchema(): asn1js.Sequence {
    // Create array for output sequence
    const outputArray: any[] = [
      this.userCertificate,
      this.revocationDate.toSchema()
    ];
    if (this.crlEntryExtensions) {
      outputArray.push(this.crlEntryExtensions.toSchema());
    }

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

  public toJSON(): RevokedCertificateJson {
    const res: RevokedCertificateJson = {
      userCertificate: this.userCertificate.toJSON(),
      revocationDate: this.revocationDate.toJSON(),
    };

    if (this.crlEntryExtensions) {
      res.crlEntryExtensions = this.crlEntryExtensions.toJSON();
    }

    return res;
  }

}