summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/PolicyConstraints.ts
blob: 9042b6daa1348f1c24f4e497d3e908faebeb3021 (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
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { EMPTY_STRING } from "./constants";
import { AsnError } from "./errors";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import * as Schema from "./Schema";

const REQUIRE_EXPLICIT_POLICY = "requireExplicitPolicy";
const INHIBIT_POLICY_MAPPING = "inhibitPolicyMapping";
const CLEAR_PROPS = [
  REQUIRE_EXPLICIT_POLICY,
  INHIBIT_POLICY_MAPPING,
];

export interface IPolicyConstraints {
  requireExplicitPolicy?: number;
  inhibitPolicyMapping?: number;
}

export interface PolicyConstraintsJson {
  requireExplicitPolicy?: number;
  inhibitPolicyMapping?: number;
}

export type PolicyConstraintsParameters = PkiObjectParameters & Partial<IPolicyConstraints>;

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

  public static override CLASS_NAME = "PolicyConstraints";

  public requireExplicitPolicy?: number;
  public inhibitPolicyMapping?: number;

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

    if (REQUIRE_EXPLICIT_POLICY in parameters) {
      this.requireExplicitPolicy = pvutils.getParametersValue(parameters, REQUIRE_EXPLICIT_POLICY, PolicyConstraints.defaultValues(REQUIRE_EXPLICIT_POLICY));
    }
    if (INHIBIT_POLICY_MAPPING in parameters) {
      this.inhibitPolicyMapping = pvutils.getParametersValue(parameters, INHIBIT_POLICY_MAPPING, PolicyConstraints.defaultValues(INHIBIT_POLICY_MAPPING));
    }

    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 REQUIRE_EXPLICIT_POLICY): number;
  public static override defaultValues(memberName: typeof INHIBIT_POLICY_MAPPING): number;
  public static override defaultValues(memberName: string): any {
    switch (memberName) {
      case REQUIRE_EXPLICIT_POLICY:
        return 0;
      case INHIBIT_POLICY_MAPPING:
        return 0;
      default:
        return super.defaultValues(memberName);
    }
  }

  /**
   * @inheritdoc
   * @asn ASN.1 schema
   * ```asn
   * PolicyConstraints ::= SEQUENCE {
   *    requireExplicitPolicy           [0] SkipCerts OPTIONAL,
   *    inhibitPolicyMapping            [1] SkipCerts OPTIONAL }
   *
   * SkipCerts ::= INTEGER (0..MAX)
   *```
   */
  public static override schema(parameters: Schema.SchemaParameters<{
    requireExplicitPolicy?: string;
    inhibitPolicyMapping?: 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.Primitive({
          name: (names.requireExplicitPolicy || EMPTY_STRING),
          optional: true,
          idBlock: {
            tagClass: 3, // CONTEXT-SPECIFIC
            tagNumber: 0 // [0]
          }
        }), // IMPLICIT integer value
        new asn1js.Primitive({
          name: (names.inhibitPolicyMapping || EMPTY_STRING),
          optional: true,
          idBlock: {
            tagClass: 3, // CONTEXT-SPECIFIC
            tagNumber: 1 // [1]
          }
        }) // IMPLICIT integer value
      ]
    }));
  }

  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,
      PolicyConstraints.schema({
        names: {
          requireExplicitPolicy: REQUIRE_EXPLICIT_POLICY,
          inhibitPolicyMapping: INHIBIT_POLICY_MAPPING
        }
      })
    );
    AsnError.assertSchema(asn1, this.className);

    //#region Get internal properties from parsed schema
    if (REQUIRE_EXPLICIT_POLICY in asn1.result) {
      const field1 = asn1.result.requireExplicitPolicy;

      field1.idBlock.tagClass = 1; // UNIVERSAL
      field1.idBlock.tagNumber = 2; // INTEGER

      const ber1 = field1.toBER(false);
      const int1 = asn1js.fromBER(ber1);
      AsnError.assert(int1, "Integer");

      this.requireExplicitPolicy = (int1.result as asn1js.Integer).valueBlock.valueDec;
    }

    if (INHIBIT_POLICY_MAPPING in asn1.result) {
      const field2 = asn1.result.inhibitPolicyMapping;

      field2.idBlock.tagClass = 1; // UNIVERSAL
      field2.idBlock.tagNumber = 2; // INTEGER

      const ber2 = field2.toBER(false);
      const int2 = asn1js.fromBER(ber2);
      AsnError.assert(int2, "Integer");

      this.inhibitPolicyMapping = (int2.result as asn1js.Integer).valueBlock.valueDec;
    }
    //#endregion
  }

  public toSchema(): asn1js.Sequence {
    //#region Create correct values for output sequence
    const outputArray = [];

    if (REQUIRE_EXPLICIT_POLICY in this) {
      const int1 = new asn1js.Integer({ value: this.requireExplicitPolicy });

      int1.idBlock.tagClass = 3; // CONTEXT-SPECIFIC
      int1.idBlock.tagNumber = 0; // [0]

      outputArray.push(int1);
    }

    if (INHIBIT_POLICY_MAPPING in this) {
      const int2 = new asn1js.Integer({ value: this.inhibitPolicyMapping });

      int2.idBlock.tagClass = 3; // CONTEXT-SPECIFIC
      int2.idBlock.tagNumber = 1; // [1]

      outputArray.push(int2);
    }
    //#endregion

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

  public toJSON(): PolicyConstraintsJson {
    const res: PolicyConstraintsJson = {};

    if (REQUIRE_EXPLICIT_POLICY in this) {
      res.requireExplicitPolicy = this.requireExplicitPolicy;
    }

    if (INHIBIT_POLICY_MAPPING in this) {
      res.inhibitPolicyMapping = this.inhibitPolicyMapping;
    }

    return res;
  }

}