Skip to main content

Function signature

pub async fn atls_connect<S>(
    stream: S,
    server_name: &str,
    policy: Policy,
    alpn: Option<Vec<String>>,
) -> Result<(TlsStream<S>, Report), AtlsVerificationError>
where
    S: AsyncByteStream + 'static,

Description

Establishes a TLS connection with attestation verification in a single operation. This high-level function combines:
  1. TLS handshake with CA certificate verification
  2. Server leaf certificate capture
  3. Verifier creation from policy
  4. Attestation verification over the TLS stream
  5. Returns the verified TLS stream and attestation report

Parameters

stream
S
required
The underlying transport stream (e.g., TcpStream). Must implement AsyncByteStream.
server_name
&str
required
The server hostname for TLS SNI and verification.
policy
Policy
required
The attestation policy determining verifier and configuration. See Policy types.
alpn
Option<Vec<String>>
Optional ALPN protocols (e.g., ["http/1.1", "h2"]).

Return type

Result
(TlsStream<S>, Report)
Returns a tuple on success:
TlsStream
TlsStream<S>
The established TLS stream ready for encrypted communication.
Report
Report
The attestation report from the TEE. See Verifier trait for details.

Error handling

Returns AtlsVerificationError if:
  • TLS handshake fails
  • Certificate verification fails
  • Attestation verification fails
  • Policy configuration is invalid
See Error handling for complete error variants.

Usage examples

Basic usage with development policy

use atlas_rs::{atls_connect, Policy, DstackTdxPolicy};

async fn connect_to_tee() -> Result<(), Box<dyn std::error::Error>> {
    // Connect with development policy (relaxed TCB status)
    let tcp = tokio::net::TcpStream::connect("tee.example.com:443").await?;
    let policy = Policy::DstackTdx(DstackTdxPolicy::dev());
    let (tls_stream, report) = atls_connect(tcp, "tee.example.com", policy, None).await?;

    // Access report data via pattern matching
    match &report {
        atlas_rs::Report::Tdx(tdx_report) => {
            println!("TCB Status: {}", tdx_report.status);
        }
    }
    
    Ok(())
}

Production usage with custom policy

use atlas_rs::{atls_connect, Policy, DstackTdxPolicy};
use atlas_rs::tdx::ExpectedBootchain;

async fn connect_with_verification() -> Result<(), Box<dyn std::error::Error>> {
    let tcp = tokio::net::TcpStream::connect("tee.example.com:443").await?;
    
    // Create policy with expected measurements
    let policy = Policy::DstackTdx(DstackTdxPolicy {
        expected_bootchain: Some(ExpectedBootchain {
            mrtd: "abc123...".to_string(),
            rtmr0: "def456...".to_string(),
            rtmr1: "ghi789...".to_string(),
            rtmr2: "jkl012...".to_string(),
        }),
        allowed_tcb_status: vec!["UpToDate".to_string()],
        ..Default::default()
    });
    
    let (tls_stream, report) = atls_connect(tcp, "tee.example.com", policy, None).await?;
    
    // Use tls_stream for encrypted communication
    Ok(())
}

With ALPN protocols

use atlas_rs::{atls_connect, Policy, DstackTdxPolicy};

async fn connect_with_alpn() -> Result<(), Box<dyn std::error::Error>> {
    let tcp = tokio::net::TcpStream::connect("tee.example.com:443").await?;
    let policy = Policy::DstackTdx(DstackTdxPolicy::dev());
    
    // Specify ALPN protocols
    let alpn = Some(vec!["h2".to_string(), "http/1.1".to_string()]);
    let (tls_stream, report) = atls_connect(tcp, "tee.example.com", policy, alpn).await?;
    
    Ok(())
}

Loading policy from JSON

use atlas_rs::{atls_connect, Policy};

async fn connect_with_json_policy() -> Result<(), Box<dyn std::error::Error>> {
    let tcp = tokio::net::TcpStream::connect("tee.example.com:443").await?;
    
    // Load policy from JSON configuration
    let json = r#"{
        "type": "dstack_tdx",
        "allowed_tcb_status": ["UpToDate", "SWHardeningNeeded"]
    }"#;
    let policy: Policy = serde_json::from_str(json)?;
    
    let (tls_stream, report) = atls_connect(tcp, "tee.example.com", policy, None).await?;
    
    Ok(())
}

Platform support

This function supports both native (tokio) and WASM platforms:
  • Native: Uses tokio_rustls::client::TlsStream
  • WASM: Uses futures_rustls::client::TlsStream
The appropriate implementation is selected at compile time via conditional compilation.

See also

Build docs developers (and LLMs) love