deno/ext/node/ops/tls.rs
Divy 3643a4d965
Some checks are pending
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
fix(ext/node): implement tls.checkServerIdentitiy (#30148)
Depends on https://github.com/denoland/deno/pull/30146
2025-07-23 16:38:15 +05:30

71 lines
1.9 KiB
Rust

// Copyright 2018-2025 the Deno authors. MIT license.
use base64::Engine;
use deno_core::OpState;
use deno_core::ResourceId;
use deno_core::op2;
use deno_net::ops_tls::TlsStreamResource;
use webpki_root_certs;
use super::crypto::x509::Certificate;
use super::crypto::x509::CertificateObject;
#[op2]
#[serde]
pub fn op_get_root_certificates() -> Vec<String> {
let certs = webpki_root_certs::TLS_SERVER_ROOT_CERTS
.iter()
.map(|cert| {
let b64 = base64::engine::general_purpose::STANDARD.encode(cert);
let pem_lines = b64
.chars()
.collect::<Vec<char>>()
// Node uses 72 characters per line, so we need to follow node even though
// it's not spec compliant https://datatracker.ietf.org/doc/html/rfc7468#section-2
.chunks(72)
.map(|c| c.iter().collect::<String>())
.collect::<Vec<String>>()
.join("\n");
let pem = format!(
"-----BEGIN CERTIFICATE-----\n{pem_lines}\n-----END CERTIFICATE-----\n",
);
pem
})
.collect::<Vec<String>>();
certs
}
#[op2]
#[serde]
pub fn op_tls_peer_certificate(
state: &mut OpState,
#[smi] rid: ResourceId,
detailed: bool,
) -> Option<CertificateObject> {
let resource = state.resource_table.get::<TlsStreamResource>(rid).ok()?;
let certs = resource.peer_certificates()?;
if certs.is_empty() {
return None;
}
// For Node.js compatibility, return the peer certificate (first in chain)
let cert_der = &certs[0];
let cert = Certificate::from_der(cert_der.as_ref()).ok()?;
cert.to_object(detailed).ok()
}
#[op2]
#[string]
pub fn op_tls_canonicalize_ipv4_address(
#[string] hostname: String,
) -> Option<String> {
let ip = hostname.parse::<std::net::IpAddr>().ok()?;
let canonical_ip = match ip {
std::net::IpAddr::V4(ipv4) => ipv4.to_string(),
std::net::IpAddr::V6(ipv6) => ipv6.to_string(),
};
Some(canonical_ip)
}