bun_boringssl 0.2.2

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// TODO: move all custom functions from the translated file into this file, then
// the translated file can be provided by `zig translate-c`

#![warn(unused_must_use)]
use core::ffi::{c_int, c_void};
use core::ptr;
use std::cell::Cell;

pub use bun_boringssl_sys as boring;
use bun_cares_sys as c_ares;
use bun_core::strings;

// MOVE_DOWN: ported from `src/runtime/api/bun/x509.zig::isSafeAltName`.
// Lives here so `boringssl` does not depend on `bun_runtime` (tier-6).
pub mod x509 {
    /// Returns `true` iff `name` contains no characters that would require
    /// escaping in a subjectAltName entry.
    #[inline]
    pub fn is_safe_alt_name(name: &[u8], utf8: bool) -> bool {
        for &c in name {
            match c {
                // These mess with encoding rules.
                // Commas make it impossible to split the list of subject
                // alternative names unambiguously, which is why we escape.
                // Single quotes are unlikely to appear in any legitimate values,
                // but they could be used to make a value look like it was escaped
                // (i.e., enclosed in single/double quotes).
                b'"' | b'\\' | b',' | b'\'' => return false,
                _ => {
                    if utf8 {
                        // In UTF-8 strings, require escaping for any ASCII control
                        // character, but NOT for non-ASCII characters. All bytes of
                        // any multi-byte code point have their MSB set.
                        if c < b' ' || c == 0x7f {
                            return false;
                        }
                    } else {
                        // Reject control characters and non-ASCII characters.
                        if c < b' ' || c > b'~' {
                            return false;
                        }
                    }
                }
            }
        }
        true
    }
}
use x509 as X509;

/// BoringSSL's translated C API
pub use boring as c;

pub fn load() {
    // Callers are expected to invoke this on a single thread during startup
    // before any concurrent BoringSSL use.
    bun_core::run_once! {{
        // BoringSSL no-arg init calls — declared `safe fn` in `bun_boringssl_sys`.
        boring::CRYPTO_library_init();
        // NB: do NOT fold this into `debug_assert!` — that macro elides its
        // argument entirely in release builds, which would skip the call.
        let rc = boring::SSL_library_init();
        debug_assert!(rc > 0);
        boring::SSL_load_error_strings();
        boring::ERR_load_BIO_strings();
        boring::OpenSSL_add_all_algorithms();

        if !cfg!(test) {
            // D006 added the bun_core dep for `run_once!`; keep_symbols! could
            // now be used here too — left as inline `black_box` for this pass.
            core::hint::black_box(OPENSSL_memory_alloc as *const ());
            core::hint::black_box(OPENSSL_memory_get_size as *const ());
            core::hint::black_box(OPENSSL_memory_free as *const ());
        }
    }}
}

// ──────────────────────────────────────────────────────────────────────────
// Extra FFI surface not yet exposed by `bun_boringssl_sys` (hand-curated
// subset). Ground truth: src/boringssl_sys/boringssl.zig + openssl/ssl.h.
// Remove once the bindgen pipeline lands these in the sys crate.
// ──────────────────────────────────────────────────────────────────────────

/// `enum ssl_verify_result_t` is `BORINGSSL_ENUM_INT`-backed; `ssl_verify_ok == 0`.
#[allow(non_camel_case_types)]
type ssl_verify_result_t = c_int;
#[allow(non_upper_case_globals)]
const ssl_verify_ok: ssl_verify_result_t = 0;

/// `#define SSL_DEFAULT_CIPHER_LIST "ALL"`
pub const SSL_DEFAULT_CIPHER_LIST: &core::ffi::CStr = c"ALL";

use boring::{
    CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_new, SSL_CTX_set_cipher_list, SSL_CTX_set0_buffer_pool,
};

type SslCustomVerifyCb =
    Option<unsafe extern "C" fn(ssl: *mut boring::SSL, out_alert: *mut u8) -> ssl_verify_result_t>;

unsafe extern "C" {
    fn SSL_CTX_set_custom_verify(
        ctx: *mut boring::SSL_CTX,
        mode: c_int,
        callback: SslCustomVerifyCb,
    );
}

unsafe extern "C" fn noop_custom_verify(
    _ssl: *mut boring::SSL,
    _out_alert: *mut u8,
) -> ssl_verify_result_t {
    ssl_verify_ok
}

/// `Send + Sync` newtype around the process-lifetime client `SSL_CTX*` so it
/// can sit inside a `OnceLock` (raw pointers opt out of `Send`/`Sync`).
struct CtxStore(ptr::NonNull<boring::SSL_CTX>);
// SAFETY: `SSL_CTX` is internally thread-safe per BoringSSL docs (its refcount
// and method tables are guarded by `CRYPTO_MUTEX`); we only ever bump the
// refcount and hand it to `SSL_new`, both of which BoringSSL documents as
// thread-safe on a shared `SSL_CTX*`.
unsafe impl Send for CtxStore {}
// SAFETY: same invariant as `Send` above — BoringSSL documents `SSL_CTX` as
// safe for concurrent use across threads, so sharing `&CtxStore` is sound.
unsafe impl Sync for CtxStore {}

static CTX_STORE: std::sync::OnceLock<CtxStore> = std::sync::OnceLock::new();

std::thread_local! {
    // Zig: `threadlocal var auto_crypto_buffer_pool: ?*CRYPTO_BUFFER_POOL = null`
    // (boringssl.zig:19225). One pool per thread, lazily allocated on first
    // `SSL_CTX.setup()` call from that thread.
    static AUTO_CRYPTO_BUFFER_POOL: Cell<*mut CRYPTO_BUFFER_POOL> =
        const { Cell::new(ptr::null_mut()) };
}

/// Zig: `SSL_CTX.setup(ctx)` (boringssl.zig:19204) — install the per-thread
/// `CRYPTO_BUFFER_POOL` and set the cipher list to BoringSSL's
/// `SSL_DEFAULT_CIPHER_LIST` (`"ALL"`).
///
/// # Safety
/// `ctx` must be a live `SSL_CTX*`.
pub unsafe fn ssl_ctx_setup(ctx: *mut boring::SSL_CTX) {
    AUTO_CRYPTO_BUFFER_POOL.with(|pool| {
        // SAFETY: caller guarantees `ctx` is a live `SSL_CTX*`; the pool pointer
        // is either freshly returned by `CRYPTO_BUFFER_POOL_new` or a previously
        // stored thread-local pool, and `SSL_DEFAULT_CIPHER_LIST` is a valid C string.
        unsafe {
            if pool.get().is_null() {
                pool.set(CRYPTO_BUFFER_POOL_new());
            }
            SSL_CTX_set0_buffer_pool(ctx, pool.get());
            let _ = SSL_CTX_set_cipher_list(ctx, SSL_DEFAULT_CIPHER_LIST.as_ptr());
        }
    });
}

pub fn init_client() -> *mut boring::SSL {
    // SAFETY: BoringSSL FFI; single-threaded startup assumption (matches Zig).
    unsafe {
        // Zig: `if (ctx_store != null) _ = boring.SSL_CTX_up_ref(ctx_store.?);`
        // Bump the refcount on every call after the first; the first call's
        // `SSL_CTX_new` already returns refcount = 1.
        if let Some(stored) = CTX_STORE.get() {
            let _ = boring::SSL_CTX_up_ref(stored.0.as_ptr());
        }
        let ctx = CTX_STORE
            .get_or_init(|| {
                // Zig: `SSL_CTX.init()` — see boringssl.zig:19197. Three steps:
                //   1. SSL_CTX_new(TLS_with_buffers_method())
                //   2. setCustomVerify(noop_custom_verify) → SSL_CTX_set_custom_verify(ctx, 0, cb)
                //   3. setup() → CRYPTO_BUFFER_POOL_new + set0_buffer_pool + set_cipher_list("ALL")
                let ctx = boring::SSL_CTX_new(boring::TLS_with_buffers_method());
                SSL_CTX_set_custom_verify(ctx, 0, Some(noop_custom_verify));
                ssl_ctx_setup(ctx);
                CtxStore(ptr::NonNull::new(ctx).expect("SSL_CTX_new"))
            })
            .0
            .as_ptr();

        // Zig: `SSL.init(ctx)` = `SSL_new(ctx)`
        let ssl = boring::SSL_new(ctx);
        // Zig: `setIsClient(true)` = `SSL_set_connect_state(ssl)`
        boring::SSL_set_connect_state(ssl);

        ssl
    }
}

// void*, OPENSSL_memory_alloc, (size_t size)
// void, OPENSSL_memory_free, (void *ptr)
// size_t, OPENSSL_memory_get_size, (void *ptr)

// The following three functions can be defined to override default heap
// allocation and freeing. If defined, it is the responsibility of
// |OPENSSL_memory_free| to zero out the memory before returning it to the
// system. |OPENSSL_memory_free| will not be passed NULL pointers.
//
// WARNING: These functions are called on every allocation and free in
// BoringSSL across the entire process. They may be called by any code in the
// process which calls BoringSSL, including in process initializers and thread
// destructors. When called, BoringSSL may hold pthreads locks. Any other code
// in the process which, directly or indirectly, calls BoringSSL may be on the
// call stack and may itself be using arbitrary synchronization primitives.
//
// As a result, these functions may not have the usual programming environment
// available to most C or C++ code. In particular, they may not call into
// BoringSSL, or any library which depends on BoringSSL. Any synchronization
// primitives used must tolerate every other synchronization primitive linked
// into the process, including pthreads locks. Failing to meet these constraints
// may result in deadlocks, crashes, or memory corruption.

#[unsafe(no_mangle)]
pub extern "C" fn OPENSSL_memory_alloc(size: usize) -> *mut c_void {
    bun_alloc::default_alloc::malloc(size)
}

// BoringSSL always expects memory to be zero'd
/// # Safety
/// `ptr` must be non-null and have been returned by `OPENSSL_memory_alloc`
/// (i.e. `default_alloc::malloc`); BoringSSL guarantees both for this hook.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn OPENSSL_memory_free(ptr: *mut c_void) {
    // SAFETY: BoringSSL guarantees ptr is non-null and was returned by
    // OPENSSL_memory_alloc above (i.e. default_alloc::malloc).
    unsafe {
        let len = bun_alloc::default_alloc::usable_size(ptr);
        ptr::write_bytes(ptr.cast::<u8>(), 0, len);
        bun_alloc::default_alloc::free(ptr);
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn OPENSSL_memory_get_size(ptr: *const c_void) -> usize {
    // ptr was returned by default_alloc::malloc (or is null, which
    // usable_size handles).
    // SAFETY: usable_size is null-safe; non-null ptrs come from the
    // OPENSSL_memory_alloc pairing above.
    unsafe { bun_alloc::default_alloc::usable_size(ptr) }
}

pub use bun_sys::posix::INET6_ADDRSTRLEN;

// Canonical cross-platform AF_* surface — handles the Windows ws2def.h split.
use bun_sys::posix::AF::{INET as AF_INET, INET6 as AF_INET6};

/// converts IP string to canonicalized IP string
/// return null when the IP is invalid
pub fn canonicalize_ip<'a>(
    addr_str: &[u8],
    out_ip: &'a mut [u8; INET6_ADDRSTRLEN + 1],
) -> Option<&'a [u8]> {
    if addr_str.len() >= INET6_ADDRSTRLEN {
        return None;
    }
    let mut ip_std_text = [0u8; INET6_ADDRSTRLEN + 1];
    // we need a null terminated string as input
    out_ip[..addr_str.len()].copy_from_slice(addr_str);
    out_ip[addr_str.len()] = 0;

    let mut af: c_int = AF_INET;
    // get the standard text representation of the IP
    // SAFETY: out_ip is NUL-terminated above; ip_std_text is large enough for any address.
    unsafe {
        if c_ares::ares_inet_pton(af, out_ip.as_ptr().cast(), ip_std_text.as_mut_ptr().cast()) <= 0
        {
            af = AF_INET6;
            if c_ares::ares_inet_pton(af, out_ip.as_ptr().cast(), ip_std_text.as_mut_ptr().cast())
                <= 0
            {
                return None;
            }
        }
    }
    // out_ip will contain the null-terminated canonicalized IP
    // SAFETY: ip_std_text holds the in_addr/in6_addr written by ares_inet_pton above.
    unsafe { c_ares::ntop(af, ip_std_text.as_ptr().cast(), &mut out_ip[..]) }
}

/// converts ASN1_OCTET_STRING to canonicalized IP string
/// return null when the IP is invalid
pub fn ip2_string<'a>(
    ip: &boring::ASN1_OCTET_STRING,
    out_ip: &'a mut [u8; INET6_ADDRSTRLEN + 1],
) -> Option<&'a [u8]> {
    let af: c_int = match ip.length {
        4 => AF_INET,
        16 => AF_INET6,
        _ => return None,
    };
    // SAFETY: ip.data points to ip.length bytes (4 or 16); out_ip is INET6_ADDRSTRLEN+1 bytes.
    unsafe { c_ares::ntop(af, ip.data.cast(), &mut out_ip[..]) }
}

/// Matches a DNS name pattern (possibly with a leading `*.` wildcard) against
/// `hostname`. Mirrors Node.js `check()` in lib/tls.js for a single pattern.
fn match_dns_name(pattern: &[u8], hostname: &[u8]) -> bool {
    if pattern.is_empty() {
        return false;
    }
    if !X509::is_safe_alt_name(pattern, false) {
        return false;
    }

    if pattern[0] == b'*' {
        // RFC 6125 Section 6.4.3: Wildcard must match exactly one label.
        // Enforce "*." prefix (wildcard must be leftmost and followed by a dot).
        if pattern.len() >= 2 && pattern[1] == b'.' {
            let suffix = &pattern[2..];
            // Disallow "*.tld" (suffix must contain at least one dot for proper domain hierarchy)
            if strings::index_of_char(suffix, b'.').is_some() {
                // Host must be at least "label.suffix" (suffix_len + 1 for dot + at least 1 char for label)
                if hostname.len() > suffix.len() + 1 {
                    let dot_index = hostname.len() - suffix.len() - 1;
                    // The character before suffix must be a dot, and there must be no other
                    // dots in the prefix (single-label wildcard only).
                    if hostname[dot_index] == b'.'
                        && strings::index_of_char(&hostname[..dot_index], b'.').is_none()
                    {
                        let host_suffix = &hostname[dot_index + 1..];
                        // RFC 4343: DNS names are case-insensitive
                        if strings::eql_case_insensitive_ascii(suffix, host_suffix, true) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    // RFC 4343: DNS names are case-insensitive
    strings::eql_case_insensitive_ascii(pattern, hostname, true)
}

/// Plain UTS #46 (IDNA) ToASCII for the native server-identity matcher
/// (CVE-2026-48618, upstream 8705d893b5 + 9103862a8f): IDNA maps U+3002 /
/// U+FF0E / U+FF61 to ".", so splitting on ASCII "." alone lets
/// "foo。bar.example.com" match `*.example.com` two labels deep. A host that
/// does not convert matches nothing. NOT `url.domainToASCII`-equivalent: that
/// is a WHATWG URL host parse, which cuts the name at "/", "?", "#" and "\"
/// and decodes "%xx" (the #43040 bypass); this is the mapping alone, as
/// Node's resolver path applies it. Uses the `idna` crate (UTS #46).
// @trace REQ-ENG-007 [entity:TlsConnection]
fn domain_to_ascii_host(host: &[u8]) -> Option<Vec<u8>> {
    let domain = core::str::from_utf8(host).ok()?;
    idna::domain_to_ascii(domain).ok().map(String::into_bytes)
}

/// Node.js `unfqdn`: strip ONE trailing dot ("Remove trailing dots for error
/// messages and matching"). Applied to the as-typed host before the IP check
/// and to the (possibly IDNA-mapped) host before SAN/CN matching
/// (upstream 9103862a8f).
fn unfqdn(name: &[u8]) -> &[u8] {
    name.strip_suffix(b".").unwrap_or(name)
}

// @trace REQ-ENG-007 [entity:TlsConnection]
pub fn check_x509_server_identity(x509: &mut boring::X509, hostname: &[u8]) -> bool {
    // As in Node.js, a host is an IP address only as typed, not after the
    // IDNA mapping — Node keeps `domainToASCII("::1") == ""` out of the IP
    // path (CVE-2026-48618, upstream 8705d893b5; unfqdn per 9103862a8f).
    let host_is_ip = strings::is_ip_address(unfqdn(hostname));
    let ascii_hostname;
    // Match a non-ASCII host on its UTS #46 form, as in
    // `tls.checkServerIdentity`; a host that does not convert matches
    // nothing. ASCII hosts are unchanged (upstream 9103862a8f: the plain
    // IDNA mapping, never a URL host parse).
    let hostname = if strings::first_non_ascii(hostname).is_some() {
        match domain_to_ascii_host(hostname) {
            Some(ascii) => {
                ascii_hostname = ascii;
                &ascii_hostname[..]
            }
            None => return false,
        }
    } else {
        hostname
    };
    // Node.js: "Remove trailing dots for error messages and matching."
    let hostname = unfqdn(hostname);
    // Node.js: CN is consulted only when the certificate carries no
    // DNS / IP / URI subjectAltName entries. Track whether any were seen.
    let mut has_identifier_san = false;

    // we check with native code if the cert is valid
    // SAFETY: x509 is a valid &mut so non-null/aligned; all boring:: fns are
    // null-safe where documented.
    unsafe {
        let x509: *mut boring::X509 = x509;
        let index = boring::X509_get_ext_by_NID(x509, boring::NID_subject_alt_name, -1);
        if index >= 0 {
            // we can check hostname
            if let Some(ext) = boring::X509_get_ext(x509, index).as_mut() {
                let method = boring::X509V3_EXT_get(ext);
                if method != boring::X509V3_EXT_get_nid(boring::NID_subject_alt_name) {
                    return false;
                }

                // we safely ensure buffer size with max len + 1
                let mut canonical_ip_buf = [0u8; INET6_ADDRSTRLEN + 1];
                let mut cert_ip_buf = [0u8; INET6_ADDRSTRLEN + 1];
                // we try to canonicalize the IP before comparing (only when host is an IP literal)
                let host_ip: Option<&[u8]> = if host_is_ip {
                    Some(canonicalize_ip(hostname, &mut canonical_ip_buf).unwrap_or(hostname))
                } else {
                    None
                };

                let names_ = boring::X509V3_EXT_d2i(ext);
                if !names_.is_null() {
                    let names = names_.cast::<boring::struct_stack_st_GENERAL_NAME>();
                    let _guard = scopeguard::guard(names, |n| {
                        boring::sk_GENERAL_NAME_pop_free(n, boring::sk_GENERAL_NAME_free)
                    });
                    for i in 0..boring::sk_GENERAL_NAME_num(names) {
                        let r#gen = boring::sk_GENERAL_NAME_value(names, i);
                        if let Some(name) = r#gen.as_ref() {
                            // TODO(port): name_type discriminants — verify GEN_* are c_int consts in bun_boringssl_sys
                            match name.name_type {
                                boring::GEN_URI => {
                                    has_identifier_san = true;
                                }
                                boring::GEN_DNS => {
                                    has_identifier_san = true;
                                    if !host_is_ip {
                                        let dns_name = &*name.d.dNSName;
                                        let dns_name_slice = core::slice::from_raw_parts(
                                            dns_name.data,
                                            usize::try_from(dns_name.length).expect("int cast"),
                                        );
                                        if match_dns_name(dns_name_slice, hostname) {
                                            return true;
                                        }
                                    }
                                }
                                boring::GEN_IPADD => {
                                    has_identifier_san = true;
                                    if let Some(hip) = host_ip {
                                        if let Some(cert_ip) =
                                            ip2_string(&*name.d.ip, &mut cert_ip_buf)
                                        {
                                            if hip == cert_ip {
                                                return true;
                                            }
                                        }
                                    }
                                }
                                _ => {}
                            }
                        }
                    }
                }
            }
        }

        // Node.js tls.checkServerIdentity: when the certificate has no
        // DNS/IP/URI subjectAltName entries, fall back to the Subject
        // Common Name. Never for IP-literal hosts (RFC 2818 §3.1).
        if !host_is_ip && !has_identifier_san {
            let subject = boring::X509_get_subject_name(x509);
            if !subject.is_null() {
                let mut last: c_int = -1;
                loop {
                    let entry_idx =
                        boring::X509_NAME_get_index_by_NID(subject, boring::NID_commonName, last);
                    if entry_idx < 0 {
                        break;
                    }
                    last = entry_idx;
                    let entry = boring::X509_NAME_get_entry(subject, entry_idx);
                    if entry.is_null() {
                        continue;
                    }
                    let data = boring::X509_NAME_ENTRY_get_data(entry);
                    if data.is_null() {
                        continue;
                    }
                    let cn_ptr = boring::ASN1_STRING_get0_data(data);
                    let cn_len = boring::ASN1_STRING_length(data);
                    if cn_ptr.is_null() || cn_len <= 0 {
                        continue;
                    }
                    let cn = core::slice::from_raw_parts(
                        cn_ptr,
                        usize::try_from(cn_len).expect("int cast"),
                    );
                    if match_dns_name(cn, hostname) {
                        return true;
                    }
                }
            }
        }
    }

    false
}

pub fn check_server_identity(ssl_ptr: &mut boring::SSL, hostname: &[u8]) -> bool {
    // SAFETY: ssl_ptr is a valid &mut so non-null/aligned; sk_X509_value returns
    // a borrowed cert pointer valid for the lifetime of the chain.
    unsafe {
        let cert_chain = boring::SSL_get_peer_cert_chain(std::ptr::from_mut(ssl_ptr));
        if !cert_chain.is_null() {
            let x509 = boring::sk_X509_value(cert_chain, 0);
            if let Some(x509) = x509.as_mut() {
                return check_x509_server_identity(x509, hostname);
            }
        }
    }
    false
}

// NOTE: `pub const ERR_toJS = @import("../runtime/crypto/boringssl_jsc.zig").ERR_toJS;`
// is intentionally dropped — *_jsc alias; in Rust the JS conversion lives in the
// `bun_runtime`/`*_jsc` crate as an extension method.

// ported from: src/boringssl/boringssl.zig

#[cfg(test)]
mod server_identity_tests {
    //! CVE-2026-48618 (upstream 8705d893b5 + 9103862a8f) matcher rows:
    //! non-ASCII hosts are matched on their UTS #46 form (the plain IDNA
    //! mapping, not a URL host parse), conversion failures match nothing,
    //! ASCII behavior is unchanged, and one trailing dot is stripped
    //! (`unfqdn`) before the IP check and before matching. `*` never matches
    //! an empty label.
    // @trace REQ-ENG-007 [entity:TlsConnection]
    use super::*;

    #[test]
    fn idna_full_stop_host_converts_and_rejects_wildcard() {
        // "foo。bar.example.com" converts to a two-label-deep host under
        // "*.example.com" — before the fix the U+3002 bytes hid the second
        // label from the ASCII "." split and the wildcard matched.
        let ascii =
            domain_to_ascii_host("foo\u{3002}bar.example.com".as_bytes()).expect("converts");
        assert_eq!(ascii, b"foo.bar.example.com".to_vec());
        assert!(
            !match_dns_name(b"*.example.com", &ascii),
            "two-label host must not match a single-label wildcard"
        );
    }

    #[test]
    fn idna_fullwidth_and_halfwidth_full_stops_convert_identically() {
        for host in ["foo\u{FF0E}bar.example.com", "foo\u{FF61}bar.example.com"] {
            let ascii = domain_to_ascii_host(host.as_bytes()).expect("converts");
            assert_eq!(ascii, b"foo.bar.example.com".to_vec());
        }
    }

    #[test]
    fn idna_unconvertible_host_matches_nothing() {
        // Invalid UTF-8 has no UTS #46 form — the identity check rejects
        // before any SAN/CN comparison (matches nothing, as in Node).
        assert!(domain_to_ascii_host(b"\xff\xfe\x80").is_none());
    }

    #[test]
    fn wildcard_never_matches_an_empty_first_label() {
        // The deliberate difference from Node kept by upstream: "。example.com"
        // converts to ".example.com", whose empty first label `*` must not
        // match. Both the converted form (if UTS #46 emits it) and the raw
        // form must be rejected.
        let raw = "。example.com".as_bytes();
        let effective = domain_to_ascii_host(raw).unwrap_or_else(|| raw.to_vec());
        assert!(!match_dns_name(b"*.example.com", &effective[..]));
        // The pure empty-label shape pins the matcher directly.
        assert!(!match_dns_name(b"*.example.com", b".example.com"));
    }

    #[test]
    fn ascii_hosts_keep_parity() {
        assert!(match_dns_name(b"*.example.com", b"foo.example.com"));
        assert!(match_dns_name(b"*.example.com", b"FOO.EXAMPLE.COM"));
        assert!(!match_dns_name(b"*.example.com", b"foo.bar.example.com"));
        assert!(!match_dns_name(b"*.example.com", b"example.com"));
        assert!(match_dns_name(b"example.com", b"example.com"));
    }

    #[test]
    fn idna_ulabel_converts_to_alabel() {
        // "bücher" has no ASCII form without punycode; the matcher sees the
        // A-label, exactly like a URL host would.
        let ascii =
            domain_to_ascii_host("b\u{00fc}cher.example.com".as_bytes()).expect("converts");
        assert_eq!(ascii, b"xn--bcher-kva.example.com".to_vec());
        assert!(match_dns_name(b"*.example.com", &ascii));
    }

    #[test]
    fn unfqdn_strips_exactly_one_trailing_dot() {
        // Node.js unfqdn: "Remove trailing dots for error messages and
        // matching" — one dot, not all of them (upstream 9103862a8f).
        assert_eq!(unfqdn(b"example.com."), b"example.com");
        assert_eq!(unfqdn(b"example.com"), b"example.com");
        // Two trailing dots keep one: an empty last label is not an FQDN
        // shape Node's checkServerIdentity normalizes away.
        assert_eq!(unfqdn(b"example.com.."), b"example.com.");
        assert_eq!(unfqdn(b"."), b"");
        assert_eq!(unfqdn(b""), b"");
    }

    #[test]
    fn fqdn_host_matches_after_unfqdn() {
        // "example.com." must match a cert for "example.com": the trailing
        // dot is stripped before SAN/CN comparison, as in Node.
        let host = unfqdn(b"example.com.");
        assert!(match_dns_name(b"example.com", host));
        let wild_host = unfqdn(b"foo.example.com.");
        assert!(match_dns_name(b"*.example.com", wild_host));
        // Sanity: without the strip the raw FQDN would not compare equal.
        assert!(!match_dns_name(b"example.com", b"example.com."));
    }

    #[test]
    fn trailing_dot_ip_literal_is_still_an_ip() {
        // The IP gate runs on the unfqdn form: "127.0.0.1." is an IP host,
        // so it is matched against IP SANs only, never DNS names.
        assert!(strings::is_ip_address(unfqdn(b"127.0.0.1.")));
        assert!(strings::is_ip_address(unfqdn(b"::1.")));
        assert!(!strings::is_ip_address(unfqdn(b"example.com.")));
    }
}