Skip to content

Commit ab5f057

Browse files
authored
fix: remove ms from TLS notAfter date (#2464)
Partial revert of #2457. We can specify dates in the far future, causing `@peculiar/x509` to use `GeneralizedTime` values, but it doesn't strip fractional seconds as per RFC 5280 so do it ourselves.
1 parent 1f589c8 commit ab5f057

File tree

1 file changed

+7
-5
lines changed
  • packages/connection-encrypter-tls/src

1 file changed

+7
-5
lines changed

packages/connection-encrypter-tls/src/utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ const CERT_PREFIX = 'libp2p-tls-handshake:'
2424
// https://github.com/libp2p/go-libp2p/blob/28c0f6ab32cd69e4b18e9e4b550ef6ce059a9d1a/p2p/security/tls/crypto.go#L265
2525
const CERT_VALIDITY_PERIOD_FROM = 60 * 60 * 1000 // ~1 hour
2626

27-
// N.b. have to keep expiry date before 2050 - when https://github.com/PeculiarVentures/x509/issues/73
28-
// is fixed we can revert to 100 years
29-
const CERT_VALIDITY_PERIOD_TO = 10 * 365 * 24 * 60 * 60 * 1000 // ~10 years
3027
// https://github.com/libp2p/go-libp2p/blob/28c0f6ab32cd69e4b18e9e4b550ef6ce059a9d1a/p2p/security/tls/crypto.go#L24C28-L24C44
31-
// const CERT_VALIDITY_PERIOD_TO = 100 * 365 * 24 * 60 * 60 * 1000 // ~100 years
28+
const CERT_VALIDITY_PERIOD_TO = 100 * 365 * 24 * 60 * 60 * 1000 // ~100 years
3229

3330
export async function verifyPeerCertificate (rawCertificate: Uint8Array, expectedPeerId?: PeerId, log?: Logger): Promise<PeerId> {
3431
const now = Date.now()
@@ -152,10 +149,15 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
152149
throw new CodeError('Unknown PeerId type', 'ERR_UNKNOWN_PEER_ID_TYPE')
153150
}
154151

152+
const notAfter = new Date(now + CERT_VALIDITY_PERIOD_TO)
153+
// workaround for https://github.com/PeculiarVentures/x509/issues/73
154+
notAfter.setMilliseconds(0)
155+
155156
const selfCert = await x509.X509CertificateGenerator.createSelfSigned({
157+
// this should be a long, large, random(ish), positive integer
156158
serialNumber: generateSerialNumber(),
157159
notBefore: new Date(now - CERT_VALIDITY_PERIOD_FROM),
158-
notAfter: new Date(now + CERT_VALIDITY_PERIOD_TO),
160+
notAfter,
159161
signingAlgorithm: alg,
160162
keys,
161163
extensions: [

0 commit comments

Comments
 (0)