Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tls serial number causes illegal padding error #2459

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/connection-encrypter-tls/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
}

const selfCert = await x509.X509CertificateGenerator.createSelfSigned({
serialNumber: uint8ArrayToString(crypto.getRandomValues(new Uint8Array(9)), 'base16'),
name: '',
serialNumber: generateSerialNumber(),
notBefore: new Date(now - CERT_VALIDITY_PERIOD_FROM),
notAfter: new Date(now + CERT_VALIDITY_PERIOD_TO),
signingAlgorithm: alg,
Expand Down Expand Up @@ -186,6 +185,19 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
}
}

function generateSerialNumber (): string {
// HACK: serial numbers starting with 80 generated by @peculiar/x509 don't
// work with TLSSocket, remove when https://github.com/PeculiarVentures/x509/issues/74
// is resolved
while (true) {
const serialNumber = (Math.random() * Math.pow(2, 52)).toFixed(0)

if (!serialNumber.startsWith('80')) {
return serialNumber
}
}
}

/**
* @see https://github.com/libp2p/specs/blob/master/tls/tls.md#libp2p-public-key-extension
*/
Expand Down
Loading