

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 中強制執行最低 TLS 版本 適用於 Rust 的 AWS SDK
<a name="enforcing-tls"></a>

 適用於 Rust 的 AWS SDK 使用 TLS 來提高與 AWS 服務通訊時的安全性。軟體開發套件預設會強制執行最低 TLS 版本 1.2。根據預設，軟體開發套件也會交涉用戶端應用程式和服務可用的最高 TLS 版本。例如，開發套件可能可以交涉 TLS 1.3。

透過提供 SDK 使用的 TCP 連接器手動組態，可在應用程式中強制執行特定 TLS 版本。為了說明這一點，下列範例示範如何強制執行 TLS 1.3。

**注意**  
有些 AWS 服務尚未支援 TLS 1.3，因此強制執行此版本可能會影響 SDK 的互通性。建議您在生產部署之前，使用每個服務測試此組態。

```
pub async fn connect_via_tls_13() -> Result<(), Error> {
    println!("Attempting to connect to KMS using TLS 1.3: ");

    // Let webpki load the Mozilla root certificates.
    let mut root_store = RootCertStore::empty();
    root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
        rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
            ta.subject,
            ta.spki,
            ta.name_constraints,
        )
    }));

    // The .with_protocol_versions call is where we set TLS1.3. You can add rustls::version::TLS12 or replace them both with rustls::ALL_VERSIONS
    let config = rustls::ClientConfig::builder()
        .with_safe_default_cipher_suites()
        .with_safe_default_kx_groups()
        .with_protocol_versions(&[&rustls::version::TLS13])
        .expect("It looks like your system doesn't support TLS1.3")
        .with_root_certificates(root_store)
        .with_no_client_auth();

    // Finish setup of the rustls connector.
    let rustls_connector = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(config)
        .https_only()
        .enable_http1()
        .enable_http2()
        .build();

    // See https://github.com/awslabs/smithy-rs/discussions/3022 for the HyperClientBuilder
    let http_client = HyperClientBuilder::new().build(rustls_connector);

    let shared_conf = aws_config::defaults(BehaviorVersion::latest())
        .http_client(http_client)
        .load()
        .await;

    let kms_client = aws_sdk_kms::Client::new(&shared_conf);
    let response = kms_client.list_keys().send().await?;

    println!("{:?}", response);

    Ok(())
}
```