Debian Codegraph @

crt/s2n/bindings/rust/aws-kms-tls-auth/src/lib.rs

raw symbols SCIP
1 #![allow(dead_code)]
2 // allow dead code for piece-wise commits
3
4 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 // SPDX-License-Identifier: Apache-2.0
6
7 //! The KMS TLS PSK Provider provides a way to get a mutually authenticated TLS
8 //! connection using IAM credentials, KMS, and the external PSK feature of TLS 1.3.
9 //!
10 //! # Design
11 //!
12 //! `aws-kms-tls-auth` allows a fleet of instances to mutually authenticate each
13 //! other. You will configure a single [KMS HMAC Key](https://docs.aws.amazon.com/kms/latest/developerguide/hmac.html)
14 //! with a SHA384 signing spec and grant all of the instances IAM permissions to
15 //! call `kms:GenerateMAC` on the KMS key. Clients and servers are considered
16 //! interchangeable.
17 //!
18 //! Instances will call `kms:GenerateMAC(days_since_unix_epoch)` to obtain a secret
19 //! that is shared across the fleet. This is referred to as the `epoch_secret`.
20 //! This secret will rotate daily.
21 //!
22 //! For each new connection, the instance will generate a nonce (`session_name`)
23 //! and use that along with the `epoch_secret` to derive a connection-specific secret.
24 //! This unique secret will then be used for RFC-standard, TLS 1.3 PSK authentication
25 //! (PSK with (EC)DHE handshake mode).
26 //!
27 //! The authenticated identity of a peer is “the peer has IAM permissions to call
28 //! `kms:GenerateMAC` on the trusted KMS key”.
29 //!
30 //! ## Deployment Concerns
31 //!
32 //! The KMS Key ARN that the [`PskProvider`] is configured with must be supplied
33 //! to the [`PskReceiver`]. Otherwise handshakes will fail.
34 //!
35 //! Note that the [`PskReceiver`] supports lists for both of these items, so
36 //! zero-downtime migrations are possible. _Example_: if the client fleet wanted
37 //! to switch from Key A to Key B it would go through the following stages
38 //! 1. clients -> [A] server -> [A]
39 //! 2. clients -> [A] server -> [A & B]
40 //! 3. clients -> [A][B], server -> [A & B]
41 //! 4. clients -> [B], server -> [A & B]
42 //! 5. clients -> [B], server -> [B]
43
44 mod codec;
45 mod epoch_schedule;
46 mod prefixed_list;
47 mod provider;
48 mod psk_derivation;
49 mod psk_parser;
50 mod receiver;
51 #[cfg(test)]
52 pub(crate) mod test_utils;
53
54 use std::time::Duration;
55
56 pub type KeyArn = String;
57 pub use provider::PskProvider;
58 pub use psk_derivation::PskVersion;
59 pub use receiver::PskReceiver;
60
61 // We have "pub" use statement so these can be fuzz tested
62 pub use codec::DecodeValue;
63 pub use psk_parser::PresharedKeyClientHello;
64
65 const ONE_HOUR: Duration = Duration::from_secs(3_600);
66
67 #[cfg(test)]
68 mod integration_tests {
69 use aws_config::Region;
70 use aws_sdk_kms::Client;
71 use tracing_subscriber::EnvFilter;
72
73 use crate::{
74 provider::PskProvider,
75 receiver::PskReceiver,
76 test_utils::{configs_from_callbacks, handshake, KMS_KEY_ARN_A, KMS_KEY_ARN_B},
77 };
78
79 use super::*;
80
81 #[tokio::test]
82 async fn basic_handshake() {
83 let psk_provider_a = PskProvider::initialize(
84 test_utils::mocked_kms_client(),
85 KMS_KEY_ARN_A.to_owned(),
86 |_| {},
87 )
88 .await
89 .unwrap();
90 let psk_provider_b = PskProvider::initialize(
91 test_utils::mocked_kms_client(),
92 KMS_KEY_ARN_B.to_owned(),
93 |_| {},
94 )
95 .await
96 .unwrap();
97 let psk_receiver = PskReceiver::initialize(
98 test_utils::mocked_kms_client(),
99 vec![KMS_KEY_ARN_A.to_owned(), KMS_KEY_ARN_B.to_owned()],
100 |_| {},
101 )
102 .await
103 .unwrap();
104
105 let client_config_a = test_utils::make_client_config(psk_provider_a);
106 let client_config_b = test_utils::make_client_config(psk_provider_b);
107 let server_config = test_utils::make_server_config(psk_receiver);
108
109 handshake(&client_config_a, &server_config).await.unwrap();
110 handshake(&client_config_b, &server_config).await.unwrap();
111 }
112
113 /// if the server only trusts key a, then a handshake with a psk from key b
114 /// will fail
115 #[tokio::test]
116 async fn untrusted_key_arn() {
117 let psk_provider_a = PskProvider::initialize(
118 test_utils::mocked_kms_client(),
119 KMS_KEY_ARN_A.to_owned(),
120 |_| {},
121 )
122 .await
123 .unwrap();
124 let psk_provider_b = PskProvider::initialize(
125 test_utils::mocked_kms_client(),
126 KMS_KEY_ARN_B.to_owned(),
127 |_| {},
128 )
129 .await
130 .unwrap();
131 let psk_receiver = PskReceiver::initialize(
132 test_utils::mocked_kms_client(),
133 vec![KMS_KEY_ARN_A.to_owned()],
134 |_| {},
135 )
136 .await
137 .unwrap();
138
139 let client_config_a = test_utils::make_client_config(psk_provider_a);
140 let client_config_b = test_utils::make_client_config(psk_provider_b);
141 let server_config = test_utils::make_server_config(psk_receiver);
142
143 handshake(&client_config_a, &server_config).await.unwrap();
144 let err = handshake(&client_config_b, &server_config)
145 .await
146 .unwrap_err()
147 .to_string();
148 // e.g. "no matching kms binder found for session c69d62609826836e718a7f1509effbde"
149 assert!(err.contains("no matching kms binder found for session "));
150 }
151 }