Skip to content

Commit 4af0eee

Browse files
committed
fix: TLS verification
[ci skip]
1 parent c70cd08 commit 4af0eee

1 file changed

Lines changed: 53 additions & 57 deletions

File tree

src/network/utils.ts

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ import { IPv4, IPv6, Validator } from 'ip-num';
1212
import * as networkErrors from './errors';
1313
import timedCancellable from '../contexts/functions/timedCancellable';
1414
import * as keysUtils from '../keys/utils';
15-
import * as nodesUtils from '../nodes/utils';
1615
import * as utils from '../utils';
1716
import { CertificateASN1 } from '../keys/types';
18-
import { keys } from '@matrixai/logger/dist/formatting';
1917
import { never } from '../utils';
2018

2119
const pingBuffer = serializeNetworkMessage({
@@ -322,46 +320,45 @@ function verifyServerCertificateChain(
322320
},
323321
);
324322
}
325-
// TODO: re-enable this and fix it.
326-
// const commonName = cert.subject.getField({ type: '2.5.4.3' });
327-
// if (commonName == null) {
328-
// throw new networkErrors.ErrorCertChainNameInvalid(
329-
// 'Chain certificate common name attribute is missing',
330-
// {
331-
// data: {
332-
// cert,
333-
// certIndex,
334-
// },
335-
// },
336-
// );
337-
// }
338-
// const certNodeId = keysUtils.publicKeyToNodeId(cert.publicKey as PublicKey);
339-
// if (commonName.value !== nodesUtils.encodeNodeId(certNodeId)) {
340-
// throw new networkErrors.ErrorCertChainKeyInvalid(
341-
// 'Chain certificate public key does not generate its node id',
342-
// {
343-
// data: {
344-
// cert,
345-
// certIndex,
346-
// nodeId: certNodeId,
347-
// commonName: commonName.value,
348-
// },
349-
// },
350-
// );
351-
// }
323+
const certNodeId = keysUtils.certNodeId(cert);
324+
if (certNodeId == null) {
325+
throw new networkErrors.ErrorCertChainNameInvalid(
326+
'Chain certificate common name attribute is missing',
327+
{
328+
data: {
329+
cert,
330+
certIndex,
331+
},
332+
},
333+
);
334+
}
335+
const certPublicKey = keysUtils.certPublicKey(cert);
336+
if (certPublicKey == null) {
337+
throw new networkErrors.ErrorCertChainKeyInvalid(
338+
'Chain certificate public key is missing',
339+
{
340+
data: {
341+
cert,
342+
certIndex,
343+
},
344+
},
345+
);
346+
}
352347
if (!keysUtils.certNodeSigned(cert)) {
353348
throw new networkErrors.ErrorCertChainSignatureInvalid(
354349
'Chain certificate does not have a valid node-signature',
355350
{
356351
data: {
357352
cert,
358353
certIndex,
354+
nodeId: keysUtils.publicKeyToNodeId(certPublicKey),
355+
commonName: certNodeId,
359356
},
360357
},
361358
);
362359
}
363360
for (const nodeId of nodeIds) {
364-
if (nodeId.equals(keysUtils.certNodeId(cert)!)) {
361+
if (certNodeId.equals(nodeId)) {
365362
// Found the certificate claiming the nodeId
366363
certClaim = cert;
367364
certClaimIndex = certIndex;
@@ -433,40 +430,39 @@ function verifyClientCertificateChain(certChain: Array<Certificate>): void {
433430
},
434431
);
435432
}
436-
// FIXME: re-enable and fix this
437-
// const commonName = cert.subject.getField({ type: '2.5.4.3' });
438-
// if (commonName == null) {
439-
// throw new networkErrors.ErrorCertChainNameInvalid(
440-
// 'Chain certificate common name attribute is missing',
441-
// {
442-
// data: {
443-
// cert,
444-
// certIndex,
445-
// },
446-
// },
447-
// );
448-
// }
449-
// const certNodeId = keysUtils.publicKeyToNodeId(cert.publicKey as PublicKey);
450-
// if (commonName.value !== nodesUtils.encodeNodeId(certNodeId)) {
451-
// throw new networkErrors.ErrorCertChainKeyInvalid(
452-
// 'Chain certificate public key does not generate its node id',
453-
// {
454-
// data: {
455-
// cert,
456-
// certIndex,
457-
// nodeId: certNodeId,
458-
// commonName: commonName.value,
459-
// },
460-
// },
461-
// );
462-
// }
433+
const certNodeId = keysUtils.certNodeId(cert);
434+
if (certNodeId == null) {
435+
throw new networkErrors.ErrorCertChainNameInvalid(
436+
'Chain certificate common name attribute is missing',
437+
{
438+
data: {
439+
cert,
440+
certIndex,
441+
},
442+
},
443+
);
444+
}
445+
const certPublicKey = keysUtils.certPublicKey(cert);
446+
if (certPublicKey == null) {
447+
throw new networkErrors.ErrorCertChainKeyInvalid(
448+
'Chain certificate public key is missing',
449+
{
450+
data: {
451+
cert,
452+
certIndex,
453+
},
454+
},
455+
);
456+
}
463457
if (!keysUtils.certNodeSigned(cert)) {
464458
throw new networkErrors.ErrorCertChainSignatureInvalid(
465459
'Chain certificate does not have a valid node-signature',
466460
{
467461
data: {
468462
cert,
469463
certIndex,
464+
nodeId: keysUtils.publicKeyToNodeId(certPublicKey),
465+
commonName: certNodeId,
470466
},
471467
},
472468
);

0 commit comments

Comments
 (0)