-
Notifications
You must be signed in to change notification settings - Fork 10
/
x509_cert.ts
340 lines (302 loc) · 10.5 KB
/
x509_cert.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
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
import { AsnConvert } from "@peculiar/asn1-schema";
import { Certificate, Version } from "@peculiar/asn1-x509";
import { BufferSourceConverter, Convert } from "pvtsutils";
import { container } from "tsyringe";
import { HashedAlgorithm } from "./types";
import { cryptoProvider } from "./provider";
import { Name } from "./name";
import { Extension } from "./extension";
import { ExtensionFactory } from "./extensions/extension_factory";
import { IPublicKeyContainer, PublicKey, PublicKeyType } from "./public_key";
import { AlgorithmProvider, diAlgorithmProvider } from "./algorithm";
import { AsnEncodedType, PemData } from "./pem_data";
import { diAsnSignatureFormatter, IAsnSignatureFormatter } from "./asn_signature_formatter";
import { PemConverter } from "./pem_converter";
import { TextConverter, TextObject } from "./text_converter";
/**
* Verification params of X509 certificate
*/
export interface X509CertificateVerifyParams {
date?: Date;
publicKey?: PublicKeyType;
signatureOnly?: boolean;
}
/**
* Representation of X509 certificate
*/
export class X509Certificate extends PemData<Certificate> implements IPublicKeyContainer {
public static override NAME = "Certificate";
protected readonly tag;
/**
* ToBeSigned block of certificate
*/
private tbs!: ArrayBuffer;
/**
* Gets a hexadecimal string of the serial number
*/
public serialNumber!: string;
/**
* Gets the subject value from the certificate as an Name
*/
public subjectName!: Name;
/**
* Gets a string subject name
*/
public subject!: string;
/**
* Gets the issuer value from the certificate as an Name
*/
public issuerName!: Name;
/**
* Gets a string issuer name
*/
public issuer!: string;
/**
* Gets a date before which certificate can't be used
*/
public notBefore!: Date;
/**
* Gets a date after which certificate can't be used
*/
public notAfter!: Date;
/**
* Gets a signature algorithm
*/
public signatureAlgorithm!: HashedAlgorithm;
/**
* Gets a signature
*/
public signature!: ArrayBuffer;
/**
* Gts a list of certificate extensions
*/
public extensions!: Extension[];
/**
* Gets a private key of the certificate
*/
public privateKey?: CryptoKey;
/**
* Gets a public key of the certificate
*/
public publicKey!: PublicKey;
/**
* Creates a new instance from ASN.1 Certificate object
* @param asn ASN.1 Certificate object
*/
public constructor(asn: Certificate);
/**
* Creates a new instance
* @param raw Encoded buffer (DER, PEM, HEX, Base64, Base64Url)
*/
public constructor(raw: AsnEncodedType);
public constructor(param: AsnEncodedType | Certificate) {
if (PemData.isAsnEncoded(param)) {
super(param, Certificate);
} else {
super(param);
}
this.tag = PemConverter.CertificateTag;
}
protected onInit(asn: Certificate) {
const tbs = asn.tbsCertificate;
this.tbs = AsnConvert.serialize(tbs);
this.serialNumber = Convert.ToHex(tbs.serialNumber);
this.subjectName = new Name(tbs.subject);
this.subject = new Name(tbs.subject).toString();
this.issuerName = new Name(tbs.issuer);
this.issuer = this.issuerName.toString();
const algProv = container.resolve<AlgorithmProvider>(diAlgorithmProvider);
this.signatureAlgorithm = algProv.toWebAlgorithm(asn.signatureAlgorithm) as HashedAlgorithm;
this.signature = asn.signatureValue;
const notBefore = tbs.validity.notBefore.utcTime || tbs.validity.notBefore.generalTime;
if (!notBefore) {
throw new Error("Cannot get 'notBefore' value");
}
this.notBefore = notBefore;
const notAfter = tbs.validity.notAfter.utcTime || tbs.validity.notAfter.generalTime;
if (!notAfter) {
throw new Error("Cannot get 'notAfter' value");
}
this.notAfter = notAfter;
this.extensions = [];
if (tbs.extensions) {
this.extensions = tbs.extensions.map(o => ExtensionFactory.create(AsnConvert.serialize(o)));
}
this.publicKey = new PublicKey(tbs.subjectPublicKeyInfo);
}
/**
* Returns an extension of specified type
* @param type Extension identifier
* @returns Extension or null
*/
public getExtension<T extends Extension>(type: string): T | null;
/**
* Returns an extension of specified type
* @param type Extension type
* @returns Extension or null
*/
public getExtension<T extends Extension>(type: { new(raw: BufferSource): T; }): T | null;
public getExtension<T extends Extension>(type: { new(raw: BufferSource): T; } | string): T | null {
for (const ext of this.extensions) {
if (typeof type === "string") {
if (ext.type === type) {
return ext as T;
}
} else {
if (ext instanceof type) {
return ext;
}
}
}
return null;
}
/**
* Returns a list of extensions of specified type
* @param type Extension identifier
*/
public getExtensions<T extends Extension>(type: string): T[];
/**
* Returns a list of extensions of specified type
* @param type Extension type
*/
public getExtensions<T extends Extension>(type: { new(raw: BufferSource): T; }): T[];
/**
* Returns a list of extensions of specified type
* @param type Extension identifier
*/
public getExtensions<T extends Extension>(type: string | { new(raw: BufferSource): T; }): T[] {
return this.extensions.filter(o => {
if (typeof type === "string") {
return o.type === type;
} else {
return o instanceof type;
}
}) as T[];
}
/**
* Validates a certificate signature
* @param params Verification parameters
* @param crypto Crypto provider. Default is from CryptoProvider
*/
public async verify(params: X509CertificateVerifyParams = {}, crypto = cryptoProvider.get()) {
let keyAlgorithm: Algorithm;
// Convert public key to CryptoKey
let publicKey: CryptoKey;
const paramsKey = params.publicKey;
try {
if (!paramsKey) {
// self-signed
keyAlgorithm = { ...this.publicKey.algorithm, ...this.signatureAlgorithm };
publicKey = await this.publicKey.export(keyAlgorithm, ["verify"], crypto);
} else if ("publicKey" in paramsKey) {
// IPublicKeyContainer
keyAlgorithm = { ...paramsKey.publicKey.algorithm, ...this.signatureAlgorithm };
publicKey = await paramsKey.publicKey.export(keyAlgorithm, ["verify"], crypto);
} else if (paramsKey instanceof PublicKey) {
// PublicKey
keyAlgorithm = { ...paramsKey.algorithm, ...this.signatureAlgorithm };
publicKey = await paramsKey.export(keyAlgorithm, ["verify"], crypto);
} else if (BufferSourceConverter.isBufferSource(paramsKey)) {
const key = new PublicKey(paramsKey);
keyAlgorithm = { ...key.algorithm, ...this.signatureAlgorithm };
publicKey = await key.export(keyAlgorithm, ["verify"], crypto);
} else {
// CryptoKey
keyAlgorithm = { ...paramsKey.algorithm, ...this.signatureAlgorithm };
publicKey = paramsKey;
}
} catch (e) {
// NOTE: Uncomment the next line to see more information about errors
// console.error(e);
// Application will throw exception if public key algorithm is not the same type which is needed for
// signature validation (eg leaf certificate is signed with RSA mechanism, public key is ECDSA)
return false;
}
// Convert ASN.1 signature to WebCrypto format
const signatureFormatters = container.resolveAll<IAsnSignatureFormatter>(diAsnSignatureFormatter).reverse();
let signature: ArrayBuffer | null = null;
for (const signatureFormatter of signatureFormatters) {
signature = signatureFormatter.toWebSignature(keyAlgorithm, this.signature);
if (signature) {
break;
}
}
if (!signature) {
throw Error("Cannot convert ASN.1 signature value to WebCrypto format");
}
const ok = await crypto.subtle.verify(this.signatureAlgorithm, publicKey, signature, this.tbs);
if (params.signatureOnly) {
return ok;
} else {
const date = params.date || new Date();
const time = date.getTime();
return ok && this.notBefore.getTime() < time && time < this.notAfter.getTime();
}
}
/**
* Returns a SHA-1 certificate thumbprint
* @param crypto Crypto provider. Default is from CryptoProvider
*/
public async getThumbprint(crypto?: Crypto): Promise<ArrayBuffer>;
/**
* Returns a certificate thumbprint for specified mechanism
* @param algorithm Hash algorithm
* @param crypto Crypto provider. Default is from CryptoProvider
*/
public async getThumbprint(algorithm: globalThis.AlgorithmIdentifier, crypto?: Crypto): Promise<ArrayBuffer>;
public async getThumbprint(...args: any[]) {
let crypto: Crypto;
let algorithm = "SHA-1";
if (args[0]) {
if (!args[0].subtle) {
// crypto?
algorithm = args[0] || algorithm;
crypto = args[1];
} else {
crypto = args[0];
}
}
crypto ??= cryptoProvider.get();
return await crypto.subtle.digest(algorithm, this.rawData);
}
public async isSelfSigned(crypto = cryptoProvider.get()): Promise<boolean> {
return this.subject === this.issuer && await this.verify({ signatureOnly: true }, crypto);
}
public override toTextObject(): TextObject {
const obj = this.toTextObjectEmpty();
const cert = AsnConvert.parse(this.rawData, Certificate);
const tbs = cert.tbsCertificate;
const data = new TextObject("", {
"Version": `${Version[tbs.version]} (${tbs.version})`,
"Serial Number": tbs.serialNumber,
"Signature Algorithm": TextConverter.serializeAlgorithm(tbs.signature),
"Issuer": this.issuer,
"Validity": new TextObject("", {
"Not Before": tbs.validity.notBefore.getTime(),
"Not After": tbs.validity.notAfter.getTime(),
}),
"Subject": this.subject,
"Subject Public Key Info": this.publicKey,
});
if (tbs.issuerUniqueID) {
data["Issuer Unique ID"] = tbs.issuerUniqueID;
}
if (tbs.subjectUniqueID) {
data["Subject Unique ID"] = tbs.subjectUniqueID;
}
if (this.extensions.length) {
const extensions = new TextObject("");
for (const ext of this.extensions) {
const extObj = ext.toTextObject();
extensions[extObj[TextObject.NAME]] = extObj;
}
data["Extensions"] = extensions;
}
obj["Data"] = data;
obj["Signature"] = new TextObject("", {
"Algorithm": TextConverter.serializeAlgorithm(cert.signatureAlgorithm),
"": cert.signatureValue,
});
return obj;
}
}