-
Notifications
You must be signed in to change notification settings - Fork 10
/
general_name.ts
290 lines (256 loc) · 8.61 KB
/
general_name.ts
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
import { AsnConvert, AsnUtf8StringConverter, OctetString } from "@peculiar/asn1-schema";
import * as asn1X509 from "@peculiar/asn1-x509";
import { BufferSourceConverter, Convert } from "pvtsutils";
import { AsnData } from "./asn_data";
import { Name } from "./name";
import { OidSerializer, TextObject } from "./text_converter";
const ERR_GN_CONSTRUCTOR = "Cannot initialize GeneralName from ASN.1 data.";
const ERR_GN_STRING_FORMAT = `${ERR_GN_CONSTRUCTOR} Unsupported string format in use.`;
const ERR_GUID = `${ERR_GN_CONSTRUCTOR} Value doesn't match to GUID regular expression.`;
const GUID_REGEX = /^([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})$/i;
const id_GUID = "1.3.6.1.4.1.311.25.1";
const id_UPN = "1.3.6.1.4.1.311.20.2.3";
export interface JsonGeneralName {
type: GeneralNameType;
value: string;
}
const DNS = "dns";
const DN = "dn";
const EMAIL = "email";
const IP = "ip";
const URL = "url";
const GUID = "guid";
const UPN = "upn";
const REGISTERED_ID = "id";
export type GeneralNameType = typeof DNS | typeof DN | typeof EMAIL | typeof GUID | typeof IP | typeof URL | typeof UPN | typeof REGISTERED_ID;
/**
* Represents ASN.1 type of GeneralName.
*
* This class doesn't support no standard string format is defined for otherName, X.400 name, EDI party name, or any other type of names.
*/
export class GeneralName extends AsnData<asn1X509.GeneralName> {
/**
* Type of the storing value
*/
public type!: GeneralNameType;
/**
* Text representation of ASN.1 GeneralName
*/
public value!: string;
public constructor(type: GeneralNameType, value: string);
public constructor(asn: asn1X509.GeneralName);
public constructor(raw: BufferSource);
public constructor(...args: any[]) {
let name: asn1X509.GeneralName;
if (args.length === 2) {
// type: GeneralNameType, value: string
switch (args[0] as GeneralNameType) {
case DN: {
const derName = new Name(args[1]).toArrayBuffer();
const asnName = AsnConvert.parse(derName, asn1X509.Name);
name = new asn1X509.GeneralName({ directoryName: asnName });
break;
}
case DNS:
name = new asn1X509.GeneralName({ dNSName: args[1] });
break;
case EMAIL:
name = new asn1X509.GeneralName({ rfc822Name: args[1] });
break;
case GUID: {
const matches = new RegExp(GUID_REGEX, "i").exec(args[1]);
if (!matches) {
throw new Error("Cannot parse GUID value. Value doesn't match to regular expression");
}
const hex = matches
.slice(1)
.map((o, i) => {
if (i < 3) {
return Convert.ToHex(new Uint8Array(Convert.FromHex(o)).reverse());
}
return o;
})
.join("");
name = new asn1X509.GeneralName({
otherName: new asn1X509.OtherName({
typeId: id_GUID,
value: AsnConvert.serialize(new OctetString(Convert.FromHex(hex))),
}),
});
break;
}
case IP:
name = new asn1X509.GeneralName({ iPAddress: args[1] });
break;
case REGISTERED_ID:
name = new asn1X509.GeneralName({ registeredID: args[1] });
break;
case UPN: {
name = new asn1X509.GeneralName({
otherName: new asn1X509.OtherName({
typeId: id_UPN,
value: AsnConvert.serialize(AsnUtf8StringConverter.toASN(args[1])),
})
});
break;
}
case URL:
name = new asn1X509.GeneralName({ uniformResourceIdentifier: args[1] });
break;
default:
throw new Error("Cannot create GeneralName. Unsupported type of the name");
}
} else if (BufferSourceConverter.isBufferSource(args[0])) {
// raw: BufferSource
name = AsnConvert.parse(args[0], asn1X509.GeneralName);
} else {
// asn: asn1X509.GeneralName
name = args[0];
}
super(name);
}
/**
* Occurs on instance initialization
* @param asn
*
* @throws Throws error if ASN.1 GeneralName contains unsupported value (eg otherName, X400 address, EDI party name)
*/
protected onInit(asn: asn1X509.GeneralName): void {
if (asn.dNSName != undefined) {
this.type = DNS;
this.value = asn.dNSName;
} else if (asn.rfc822Name != undefined) {
this.type = EMAIL;
this.value = asn.rfc822Name;
} else if (asn.iPAddress != undefined) {
this.type = IP;
this.value = asn.iPAddress;
} else if (asn.uniformResourceIdentifier != undefined) {
this.type = URL;
this.value = asn.uniformResourceIdentifier;
} else if (asn.registeredID != undefined) {
this.type = REGISTERED_ID;
this.value = asn.registeredID;
} else if (asn.directoryName != undefined) {
this.type = DN;
this.value = new Name(asn.directoryName).toString();
} else if (asn.otherName != undefined) {
if (asn.otherName.typeId === id_GUID) {
this.type = GUID;
const guid = AsnConvert.parse(asn.otherName.value, OctetString);
const matches = new RegExp(GUID_REGEX, "i").exec(Convert.ToHex(guid));
if (!matches) {
throw new Error(ERR_GUID);
}
this.value = matches
.slice(1)
.map((o, i) => {
if (i < 3) {
return Convert.ToHex(new Uint8Array(Convert.FromHex(o)).reverse());
}
return o;
})
.join("-");
} else if (asn.otherName.typeId === id_UPN) {
this.type = UPN;
this.value = AsnConvert.parse(asn.otherName.value, asn1X509.DirectoryString).toString();
} else {
throw new Error(ERR_GN_STRING_FORMAT);
}
} else {
throw new Error(ERR_GN_STRING_FORMAT);
}
}
public toJSON(): JsonGeneralName {
return {
type: this.type,
value: this.value,
};
}
public override toTextObject(): TextObject {
let type: string;
switch (this.type) {
case DN:
case DNS:
case GUID:
case IP:
case REGISTERED_ID:
case UPN:
case URL:
type = this.type.toUpperCase();
break;
case EMAIL:
type = "Email";
break;
default:
throw new Error("Unsupported GeneralName type");
}
let value = this.value;
if (this.type === REGISTERED_ID) {
value = OidSerializer.toString(value);
}
return new TextObject(type, undefined, value);
}
}
export type JsonGeneralNames = JsonGeneralName[];
export class GeneralNames extends AsnData<asn1X509.GeneralNames> {
public static override NAME = "GeneralNames";
public items!: ReadonlyArray<GeneralName>;
constructor(json: JsonGeneralNames);
constructor(asn: asn1X509.GeneralNames | asn1X509.GeneralName[]);
constructor(raw: BufferSource);
constructor(params: JsonGeneralNames | asn1X509.GeneralNames | asn1X509.GeneralName[] | BufferSource) {
let names: asn1X509.GeneralNames;
if (params instanceof asn1X509.GeneralNames) {
// asn1X509.GeneralNames
names = params;
} else if (Array.isArray(params)) {
// JsonGeneralNames[] | asn1X509.GeneralName[]
const items: asn1X509.GeneralName[] = [];
for (const name of params) {
if (name instanceof asn1X509.GeneralName) {
items.push(name);
} else {
const asnName = AsnConvert.parse(new GeneralName(name.type, name.value).rawData, asn1X509.GeneralName);
items.push(asnName);
}
}
names = new asn1X509.GeneralNames(items);
} else if (BufferSourceConverter.isBufferSource(params)) {
names = AsnConvert.parse(params, asn1X509.GeneralNames);
} else {
throw new Error("Cannot initialize GeneralNames. Incorrect incoming arguments");
}
super(names);
}
protected onInit(asn: asn1X509.GeneralNames): void {
const items: GeneralName[] = [];
for (const asnName of asn) {
let name: GeneralName | null = null;
try {
name = new GeneralName(asnName);
} catch {
// skip unsupported ASN.1 GeneralName
continue;
}
items.push(name);
}
this.items = items;
}
public toJSON(): JsonGeneralNames {
return this.items.map(o => o.toJSON());
}
public override toTextObject(): TextObject {
const res = super.toTextObjectEmpty();
for (const name of this.items) {
const nameObj = name.toTextObject();
let field = res[nameObj[TextObject.NAME]];
if (!Array.isArray(field)) {
field = [];
res[nameObj[TextObject.NAME]] = field;
}
field.push(nameObj);
}
return res;
}
}