Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/boringssl_sys/boringssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ unsafe extern "C" {
name: *const c_char,
namelen: usize,
) -> c_int;
pub fn X509_VERIFY_PARAM_set1_ip(param: *mut c_void, ip: *const u8, iplen: usize) -> c_int;
pub fn SSL_CTX_set_keylog_callback(
ctx: *mut SSL_CTX,
cb: Option<unsafe extern "C" fn(ssl: *const SSL, line: *const c_char)>,
Expand Down
33 changes: 29 additions & 4 deletions src/runtime/node/quic/tls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::ffi::c_int;
use core::net::IpAddr;
use core::ptr::null_mut;

use bun_boringssl_sys as ssl;
Expand Down Expand Up @@ -519,10 +520,34 @@ impl TlsContext {
return Err("verifyHostname requires a non-empty servername");
}
let param = ssl::SSL_CTX_get0_param(ctx);
if param.is_null()
|| ssl::X509_VERIFY_PARAM_set1_host(param, host.as_ptr().cast(), host.len())
!= 1
{
if param.is_null() {
return Err("failed to bind hostname for certificate verification");
}
// OpenSSL's `SSL_set1_host` (what Node calls) binds an IP
// literal as an IP, so it matches the certificate's IP SANs.
// BoringSSL's `set1_host` matches DNS names only.
let bound = match bun_core::ip_address::parse_strict(host) {
Comment thread
robobun marked this conversation as resolved.
Resolved
Some(ip) => {
let (v4, v6);
let octets: &[u8] = match ip {
IpAddr::V4(ip) => {
v4 = ip.octets();
&v4
}
IpAddr::V6(ip) => {
v6 = ip.octets();
&v6
}
};
ssl::X509_VERIFY_PARAM_set1_ip(param, octets.as_ptr(), octets.len())
}
None => ssl::X509_VERIFY_PARAM_set1_host(
param,
host.as_ptr().cast(),
host.len(),
),
};
if bound != 1 {
return Err("failed to bind hostname for certificate verification");
}
}
Expand Down
38 changes: 37 additions & 1 deletion test/js/node/quic/quic-sni.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, tls } from "harness";
import { createPrivateKey, X509Certificate } from "node:crypto";
import { readFileSync } from "node:fs";
import { join } from "node:path";
Expand Down Expand Up @@ -250,3 +250,39 @@ test("connect() refuses a servername with a NUL byte", async () => {
} as SessionOptions))(),
).rejects.toThrow("servername must not contain null bytes");
});

// https://github.com/oven-sh/bun/issues/45514
// `tls` from harness has SANs DNS:localhost, IP:127.0.0.1 and IP:::1.
test.each(["127.0.0.1", "::1"])(
"a client verifies an IP servername %s against the certificate's IP SAN",
async servername => {
await using server = await listen(ignoreErrors, {
sni: { "*": { keys: [createPrivateKey(tls.key)], certs: [Buffer.from(tls.cert)] } },
alpn: ["quic-test"],
});
const session = await connect(server.address!, { alpn: "quic-test", servername, ca: [Buffer.from(tls.cert)] });
try {
expect(await session.opened).toMatchObject({ validationErrorCode: undefined, validationErrorReason: undefined });
} finally {
await session.close();
}
},
);

test.each([
["127.0.0.2", "IP address mismatch"],
["::2", "IP address mismatch"],
["example.com", "Hostname mismatch"],
])("a client rejects servername %s when the certificate does not name it", async (servername, reason) => {
await using server = await listen(ignoreErrors, {
sni: { "*": { keys: [createPrivateKey(tls.key)], certs: [Buffer.from(tls.cert)] } },
alpn: ["quic-test"],
});
const session = await connect(server.address!, { alpn: "quic-test", servername, ca: [Buffer.from(tls.cert)] });
ignoreErrors(session);
try {
await expect(session.opened).rejects.toThrow(`Peer certificate validation failed: ${reason}`);
} finally {
session.destroy();
}
});
Loading