use mlua::{Lua, Result as LuaResult, UserData, UserDataMethods};
use native_tls::TlsConnector;
use native_tls::TlsStream;
use openssl::pkey::PKey;
use openssl::rsa::Rsa;
use std::io::{Read, Write};
use std::time::Duration;
use crate::brokered_stream::BrokeredTcpStream;
use crate::capabilities::NseCapabilityContext;
use crate::providers::NseHostServices;
use crate::wrappers;
struct TlsConnection {
stream: Option<TlsStream<BrokeredTcpStream>>,
host: String,
port: u16,
connected: bool,
version: String,
cipher: String,
}
impl TlsConnection {
fn new() -> Self {
Self {
stream: None,
host: String::new(),
port: 0,
connected: false,
version: String::new(),
cipher: String::new(),
}
}
fn connect(
&mut self,
ctx: &NseCapabilityContext,
services: &NseHostServices,
host: &str,
port: u16,
) -> Result<(), String> {
self.stream = Some(connect_tls_stream(
ctx,
services,
host,
port,
"tls.connect",
)?);
self.version = "TLS".to_string();
self.cipher = "negotiated".to_string();
self.host = host.to_string();
self.port = port;
self.connected = true;
Ok(())
}
fn write(&mut self, data: &str) -> Result<usize, String> {
let stream = self
.stream
.as_mut()
.ok_or_else(|| "TLS connection is not open".to_string())?;
stream.write(data.as_bytes()).map_err(|e| e.to_string())
}
fn read(&mut self, size: usize) -> Result<String, String> {
let stream = self
.stream
.as_mut()
.ok_or_else(|| "TLS connection is not open".to_string())?;
let mut buffer = vec![0u8; size];
let read = stream.read(&mut buffer).map_err(|e| e.to_string())?;
buffer.truncate(read);
String::from_utf8(buffer).map_err(|e| e.to_string())
}
fn close(&mut self) {
self.stream.take();
self.connected = false;
}
fn get_version(&self) -> String {
self.version.clone()
}
fn get_cipher(&self) -> String {
self.cipher.clone()
}
}
fn connect_tcp(
ctx: &NseCapabilityContext,
services: &NseHostServices,
host: &str,
port: u16,
operation: &'static str,
) -> Result<BrokeredTcpStream, String> {
let decision = wrappers::check_network_tcp(ctx, host, operation);
if decision.is_denied() {
return Err(format!(
"Network denied: {}",
decision.deny_reason().unwrap_or("policy violation")
));
}
let (mut stream, _endpoint) = BrokeredTcpStream::connect(
ctx,
services,
host,
port,
Duration::from_secs(10),
operation,
)?;
stream
.set_read_timeout(Some(Duration::from_secs(10)))
.map_err(|e| e.to_string())?;
stream
.set_write_timeout(Some(Duration::from_secs(10)))
.map_err(|e| e.to_string())?;
Ok(stream)
}
fn connect_tls_stream(
ctx: &NseCapabilityContext,
services: &NseHostServices,
host: &str,
port: u16,
operation: &'static str,
) -> Result<TlsStream<BrokeredTcpStream>, String> {
let stream = connect_tcp(ctx, services, host, port, operation)?;
let connector = TlsConnector::builder().build().map_err(|e| e.to_string())?;
connector.connect(host, stream).map_err(|e| match e {
native_tls::HandshakeError::Failure(e) => format!("TLS handshake failed: {e}"),
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
})
}
impl UserData for TlsConnection {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_method_mut(
"connect",
|_lua, _this, _args: (String, u16)| -> LuaResult<()> {
Err(mlua::Error::RuntimeError(
"use tls.connect to establish a capability-checked connection".to_string(),
))
},
);
methods.add_method_mut("write", |_lua, this, data: String| {
this.write(&data).map_err(mlua::Error::RuntimeError)
});
methods.add_method_mut("read", |_lua, this, size: Option<usize>| {
this.read(size.unwrap_or(4096))
.map_err(mlua::Error::RuntimeError)
});
methods.add_method_mut("close", |_lua, this, _: ()| {
this.close();
Ok(true)
});
methods.add_method("get_version", |_lua, this, _: ()| Ok(this.get_version()));
methods.add_method("get_cipher", |_lua, this, _: ()| Ok(this.get_cipher()));
}
}
pub fn register_tls_library_with_services(
lua: &Lua,
capability_ctx: &NseCapabilityContext,
services: &NseHostServices,
) -> LuaResult<()> {
let globals = lua.globals();
let tls = lua.create_table()?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let connect_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.connect");
if decision.is_denied() {
return Err(mlua::Error::RuntimeError(format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
)));
}
let mut conn = TlsConnection::new();
conn.connect(&cap_ctx, &svc, &host, port)
.map_err(mlua::Error::RuntimeError)?;
lua.create_userdata(conn)
})?;
tls.set("connect", connect_fn)?;
let get_clients_fn =
lua.create_function(|_lua, _: ()| Ok(vec!["TLS 1.3", "TLS 1.2", "TLS 1.1", "TLS 1.0"]))?;
tls.set("get_clients", get_clients_fn)?;
let get_servers_fn = lua.create_function(|_lua, _: ()| {
Ok(vec!["TLS 1.3", "TLS 1.2", "TLS 1.1", "TLS 1.0", "SSL 3.0"])
})?;
tls.set("get_servers", get_servers_fn)?;
let get_cipher_suites_fn = lua.create_function(|_lua, _: ()| {
Ok(vec![
"TLS_AES_256_GCM_SHA384",
"TLS_AES_128_GCM_SHA256",
"TLS_CHACHA20_POLY1305_SHA256",
"ECDHE-RSA-AES256-GCM-SHA384",
"ECDHE-RSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES256-SHA384",
"ECDHE-RSA-AES128-SHA256",
"AES256-GCM-SHA384",
"AES128-GCM-SHA256",
"AES256-SHA256",
"AES128-SHA256",
"AES256-SHA",
"AES128-SHA",
"DES-CBC3-SHA",
"RC4-SHA",
"RC4-MD5",
])
})?;
tls.set("get_cipher_suites", get_cipher_suites_fn)?;
let version_fn = lua.create_function(|_lua, _: ()| Ok("1.0.0"))?;
tls.set("version", version_fn)?;
let parse_protocol_version_fn = lua.create_function(|_lua, version_str: String| {
let version = match version_str.to_uppercase().as_str() {
"SSL 3.0" | "SSL3" | "SSL" => 0x0300,
"TLS 1.0" | "TLS1" | "TLS1_0" => 0x0301,
"TLS 1.1" | "TLS1_1" => 0x0302,
"TLS 1.2" | "TLS1_2" => 0x0303,
"TLS 1.3" | "TLS1_3" => 0x0304,
_ => 0,
};
Ok(version)
})?;
tls.set("parse_protocol_version", parse_protocol_version_fn)?;
let get_supported_versions_fn =
lua.create_function(|_lua, _: ()| Ok(vec!["TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1.0"]))?;
tls.set("get_supported_versions", get_supported_versions_fn)?;
let protocol_to_string_fn = lua.create_function(|_lua, version: i32| {
let version_str = match version {
0x0300 => "SSL 3.0",
0x0301 => "TLS 1.0",
0x0302 => "TLS 1.1",
0x0303 => "TLS 1.2",
0x0304 => "TLS 1.3",
_ => "Unknown",
};
Ok(version_str.to_string())
})?;
tls.set("protocol_to_string", protocol_to_string_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_curve_info_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_curve_info");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(_tls_stream) => {
result.set("version", "negotiated")?;
result.set("cipher", "negotiated")?;
result.set("curve", "negotiated")?;
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("get_curve_info", get_curve_info_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_cert_info_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_cert_info");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(tls_stream) => {
if let Some(cert) = tls_stream.peer_certificate().ok().flatten() {
if let Ok(der) = cert.to_der() {
if let Ok(x509) = openssl::x509::X509::from_der(&der) {
let subject: String = x509
.subject_name()
.entries()
.map(|e| {
let value = e
.data()
.as_utf8()
.map(|s| s.to_string())
.unwrap_or_else(|_| "?".to_string());
format!(
"{}={}",
e.object().nid().short_name().unwrap_or("?"),
value
)
})
.collect::<Vec<_>>()
.join(", ");
result.set("subject", subject)?;
let issuer: String = x509
.issuer_name()
.entries()
.map(|e| {
let value = e
.data()
.as_utf8()
.map(|s| s.to_string())
.unwrap_or_else(|_| "?".to_string());
format!(
"{}={}",
e.object().nid().short_name().unwrap_or("?"),
value
)
})
.collect::<Vec<_>>()
.join(", ");
result.set("issuer", issuer)?;
result.set("notbefore", x509.not_before().to_string())?;
result.set("notafter", x509.not_after().to_string())?;
result.set("version", x509.version())?;
}
}
}
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("get_cert_info", get_cert_info_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let check_hostname_fn =
lua.create_function(move |_lua, (host, hostname): (String, String)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.check_hostname");
if decision.is_denied() {
return Ok(false);
}
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(_) => return Ok(false),
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, 443, "tls.check_hostname") {
Ok(s) => s,
Err(_) => return Ok(false),
};
match connector.connect(&hostname, stream) {
Ok(_) => Ok(true),
Err(_) => Ok(false),
}
})?;
tls.set("check_hostname", check_hostname_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_session_info_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_session_info");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(_tls_stream) => {
result.set("version", "negotiated")?;
result.set("cipher", "negotiated")?;
result.set("peer_certificate", true)?;
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("get_session_info", get_session_info_fn)?;
let cap_ctx = capability_ctx.clone();
let generate_key_fn = lua.create_function(move |lua, bits: Option<usize>| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.generate_key");
if decision.is_denied() {
return Err(mlua::Error::RuntimeError(format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
)));
}
let bits = bits.unwrap_or(2048);
let rsa =
Rsa::generate(bits as u32).map_err(|e| mlua::Error::RuntimeError(e.to_string()))?;
let _private_key =
PKey::from_rsa(rsa).map_err(|e| mlua::Error::RuntimeError(e.to_string()))?;
let result = lua.create_table()?;
result.set("bits", bits as i32)?;
result.set("type", "RSA")?;
Ok(result)
})?;
tls.set("generate_key", generate_key_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let parse_certificate_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.parse_certificate");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(tls_stream) => {
if let Some(cert) = tls_stream.peer_certificate().ok().flatten() {
if let Ok(der) = cert.to_der() {
if let Ok(x509) = openssl::x509::X509::from_der(&der) {
let subject = x509
.subject_name()
.entries()
.map(|e| {
let value = e
.data()
.as_utf8()
.map(|s| s.to_string())
.unwrap_or_else(|_| "?".to_string());
format!(
"{}={}",
e.object().nid().short_name().unwrap_or("?"),
value
)
})
.collect::<Vec<_>>()
.join(", ");
result.set("subject", subject)?;
let issuer = x509
.issuer_name()
.entries()
.map(|e| {
let value = e
.data()
.as_utf8()
.map(|s| s.to_string())
.unwrap_or_else(|_| "?".to_string());
format!(
"{}={}",
e.object().nid().short_name().unwrap_or("?"),
value
)
})
.collect::<Vec<_>>()
.join(", ");
result.set("issuer", issuer)?;
result.set("not_before", x509.not_before().to_string())?;
result.set("not_after", x509.not_after().to_string())?;
result.set("version", x509.version())?;
let serial = x509
.serial_number()
.to_bn()
.ok()
.and_then(|bn| bn.to_hex_str().ok())
.map(|s| s.to_string())
.unwrap_or_default();
result.set("serial", serial)?;
}
}
}
result.set("parsed", true)?;
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("parse_certificate", parse_certificate_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let verify_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.verify");
if decision.is_denied() {
let result = lua.create_table()?;
result.set("valid", false)?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("valid", false)?;
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.verify") {
Ok(s) => s,
Err(e) => {
result.set("valid", false)?;
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(_) => {
result.set("valid", true)?;
}
Err(native_tls::HandshakeError::Failure(e)) => {
result.set("valid", false)?;
result.set("error", format!("Certificate verification failed: {e}"))?;
}
Err(native_tls::HandshakeError::WouldBlock(_)) => {
result.set("valid", false)?;
result.set("error", "TLS handshake blocked: retry required")?;
}
}
Ok(result)
})?;
tls.set("verify", verify_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_fingerprint_fn = lua.create_function(
move |lua, (host, port, hash): (String, u16, Option<String>)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_fingerprint");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let hash = hash.unwrap_or_else(|| "sha256".to_string());
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(tls_stream) => {
if let Some(cert) = tls_stream.peer_certificate().ok().flatten() {
if let Ok(der) = cert.to_der() {
match certificate_fingerprint(&der, &hash) {
Ok(hash_value) => {
result.set("fingerprint", hash_value)?;
result.set("hash", hash)?;
}
Err(error) => result.set("error", error)?,
}
}
}
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
},
)?;
tls.set("get_fingerprint", get_fingerprint_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_altnames_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_altnames");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(tls_stream) => {
if let Some(cert) = tls_stream.peer_certificate().ok().flatten() {
if let Ok(der) = cert.to_der() {
if let Ok(x509) = openssl::x509::X509::from_der(&der) {
let altnames = lua.create_table()?;
if let Some(names) = x509.subject_alt_names() {
for (index, name) in names.iter().enumerate() {
if let Some(dns_name) = name.dnsname() {
altnames.set(index + 1, dns_name)?;
}
}
}
result.set("altnames", altnames)?;
}
}
}
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("get_altnames", get_altnames_fn)?;
let cipher_to_string_fn = lua.create_function(|_lua, code: i32| {
let cipher = match code {
0x002F => "TLS_RSA_WITH_AES_128_CBC_SHA",
0x0035 => "TLS_RSA_WITH_AES_256_CBC_SHA",
0x003C => "TLS_RSA_WITH_AES_128_CBC_SHA256",
0x003D => "TLS_RSA_WITH_AES_256_CBC_SHA256",
0x009C => "TLS_RSA_WITH_AES_128_GCM_SHA256",
0x009D => "TLS_RSA_WITH_AES_256_GCM_SHA384",
0xC013 => "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
0xC014 => "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
0xC023 => "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
0xC024 => "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
0xC02F => "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
0xC030 => "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
0xCCA8 => "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
0xCCA9 => "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
0x1301 => "TLS_AES_128_GCM_SHA256",
0x1302 => "TLS_AES_256_GCM_SHA384",
0x1303 => "TLS_CHACHA20_POLY1305_SHA256",
_ => "TLS_UNKNOWN_CIPHER",
};
Ok(cipher.to_string())
})?;
tls.set("cipher_to_string", cipher_to_string_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_connection_info_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_connection_info");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(_tls_stream) => {
result.set("version", "negotiated")?;
result.set("cipher", "negotiated")?;
result.set("peer_certificate", true)?;
result.set("compressed", false)?;
result.set("secure_renegotiation", true)?;
result.set("server_name", host)?;
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("get_connection_info", get_connection_info_fn)?;
let get_supported_ciphers_fn = lua.create_function(|_lua, _: ()| {
Ok(vec![
"TLS_AES_256_GCM_SHA384",
"TLS_AES_128_GCM_SHA256",
"TLS_CHACHA20_POLY1305_SHA256",
"ECDHE-RSA-AES256-GCM-SHA384",
"ECDHE-RSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES256-SHA384",
"ECDHE-RSA-AES128-SHA256",
"AES256-GCM-SHA384",
"AES128-GCM-SHA256",
"AES256-SHA256",
"AES128-SHA256",
"AES256-SHA",
"AES128-SHA",
])
})?;
tls.set("get_supported_ciphers", get_supported_ciphers_fn)?;
let cap_ctx = capability_ctx.clone();
let svc = services.clone();
let get_cert_chain_fn = lua.create_function(move |lua, (host, port): (String, u16)| {
let decision = wrappers::check_crypto(&cap_ctx, "tls.get_cert_chain");
if decision.is_denied() {
let result = lua.create_table()?;
result.set(
"error",
format!(
"Crypto denied: {}",
decision.deny_reason().unwrap_or("policy violation")
),
)?;
return Ok(result);
}
let result = lua.create_table()?;
let connector = match native_tls::TlsConnector::builder().build() {
Ok(c) => c,
Err(e) => {
result.set("error", format!("TLS connector error: {}", e))?;
return Ok(result);
}
};
let stream = match connect_tcp(&cap_ctx, &svc, &host, port, "tls.network") {
Ok(s) => s,
Err(e) => {
result.set("error", format!("Connection error: {}", e))?;
return Ok(result);
}
};
match connector.connect(&host, stream) {
Ok(tls_stream) => {
let chain = lua.create_table()?;
if let Some(_cert) = tls_stream.peer_certificate().ok().flatten() {
let cert_info = lua.create_table()?;
cert_info.set("subject", "CN=".to_string())?;
cert_info.set("issuer", "CN=".to_string())?;
cert_info.set("valid", true)?;
chain.set(1, cert_info)?;
}
result.set("chain", chain)?;
result.set("length", 1)?;
}
Err(e) => {
result.set(
"error",
match e {
native_tls::HandshakeError::Failure(e) => {
format!("TLS handshake error: {e}")
}
native_tls::HandshakeError::WouldBlock(_) => {
"TLS handshake blocked: retry required".to_string()
}
},
)?;
}
}
Ok(result)
})?;
tls.set("get_cert_chain", get_cert_chain_fn)?;
let is_supported_fn = lua.create_function(|_lua, version: String| {
let supported = match version.to_uppercase().as_str() {
"TLSV1.3" | "TLS 1.3" | "1.3" => true,
"TLSV1.2" | "TLS 1.2" | "1.2" => true,
"TLSV1.1" | "TLS 1.1" | "1.1" => true,
"TLSV1.0" | "TLS 1.0" | "1.0" => true,
"SSL" | "SSL 3.0" | "3.0" => false,
_ => false,
};
Ok(supported)
})?;
tls.set("is_supported", is_supported_fn)?;
globals.set("tls", tls)?;
Ok(())
}
fn certificate_fingerprint(data: &[u8], algorithm: &str) -> Result<String, String> {
let digest = match algorithm.to_ascii_lowercase().as_str() {
"sha1" => openssl::hash::MessageDigest::sha1(),
"sha256" => openssl::hash::MessageDigest::sha256(),
"md5" => openssl::hash::MessageDigest::md5(),
other => return Err(format!("unsupported certificate hash algorithm: {}", other)),
};
openssl::hash::hash(digest, data)
.map(|digest| hex::encode_upper(digest.as_ref()))
.map_err(|error| error.to_string())
}