main/keyring/raw_rsa_keyring.rs
1// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::test_utils;
5use aws_db_esdk::dynamodb::types::DynamoDbTableEncryptionConfig;
6use aws_db_esdk::intercept::DbEsdkInterceptor;
7use aws_db_esdk::material_providers::client as mpl_client;
8use aws_db_esdk::material_providers::types::material_providers_config::MaterialProvidersConfig;
9use aws_db_esdk::material_providers::types::PaddingScheme;
10use aws_db_esdk::CryptoAction;
11use aws_db_esdk::DynamoDbTablesEncryptionConfig;
12use aws_sdk_dynamodb::types::AttributeValue;
13use std::collections::HashMap;
14use std::fs::File;
15use std::io::Read;
16use std::io::Write;
17use std::path::Path;
18
19/*
20 This example sets up DynamoDb Encryption for the AWS SDK client
21 using the raw RSA Keyring. This keyring uses an RSA key pair to
22 encrypt and decrypt records. This keyring accepts PEM encodings of
23 the key pair as UTF-8 interpreted bytes. The client uses the public key
24 to encrypt items it adds to the table and uses the private key to decrypt
25 existing table items it retrieves.
26
27 This example loads a key pair from PEM files with paths defined in
28 - EXAMPLE_RSA_PRIVATE_KEY_FILENAME
29 - EXAMPLE_RSA_PUBLIC_KEY_FILENAME
30 If you do not provide these files, running this example through this
31 class' main method will generate these files for you. These files will
32 be generated in the directory where the example is run.
33 In practice, users of this library should not generate new key pairs
34 like this, and should instead retrieve an existing key from a secure
35 key management system (e.g. an HSM).
36 You may also provide your own key pair by placing PEM files in the
37 directory where the example is run or modifying the paths in the code
38 below. These files must be valid PEM encodings of the key pair as UTF-8
39 encoded bytes. If you do provide your own key pair, or if a key pair
40 already exists, this class' main method will not generate a new key pair.
41
42 This example loads a key pair from disk, encrypts a test item, and puts the
43 encrypted item to the provided DynamoDb table. Then, it gets the
44 item from the table and decrypts it.
45
46 Running this example requires access to the DDB Table whose name
47 is provided in CLI arguments.
48 This table must be configured with the following
49 primary key configuration:
50 - Partition key is named "partition_key" with type (S)
51 - Sort key is named "sort_key" with type (S)
52*/
53
54const EXAMPLE_RSA_PRIVATE_KEY_FILENAME: &str = "RawRsaKeyringExamplePrivateKey.pem";
55const EXAMPLE_RSA_PUBLIC_KEY_FILENAME: &str = "RawRsaKeyringExamplePublicKey.pem";
56
57pub async fn put_item_get_item() -> Result<(), crate::BoxError> {
58 let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME;
59
60 // You may provide your own RSA key pair in the files located at
61 // - EXAMPLE_RSA_PRIVATE_KEY_FILENAME
62 // - EXAMPLE_RSA_PUBLIC_KEY_FILENAME
63 // If these files are not present, this will generate a pair for you
64 if should_generate_new_rsa_key_pair()? {
65 generate_rsa_key_pair()?;
66 }
67
68 // 1. Load key pair from UTF-8 encoded PEM files.
69 // You may provide your own PEM files to use here.
70 // If you do not, the main method in this class will generate PEM
71 // files for example use. Do not use these files for any other purpose.
72
73 let mut file = File::open(Path::new(EXAMPLE_RSA_PUBLIC_KEY_FILENAME))?;
74 let mut public_key_utf8_bytes = Vec::new();
75 file.read_to_end(&mut public_key_utf8_bytes)?;
76
77 let mut file = File::open(Path::new(EXAMPLE_RSA_PRIVATE_KEY_FILENAME))?;
78 let mut private_key_utf8_bytes = Vec::new();
79 file.read_to_end(&mut private_key_utf8_bytes)?;
80
81 // 2. Create the keyring.
82 // The DynamoDb encryption client uses this to encrypt and decrypt items.
83 let mpl_config = MaterialProvidersConfig::builder().build()?;
84 let mpl = mpl_client::Client::from_conf(mpl_config)?;
85 let raw_rsa_keyring = mpl
86 .create_raw_rsa_keyring()
87 .key_name("my-rsa-key-name")
88 .key_namespace("my-key-namespace")
89 .padding_scheme(PaddingScheme::OaepSha256Mgf1)
90 .public_key(public_key_utf8_bytes)
91 .private_key(private_key_utf8_bytes)
92 .send()
93 .await?;
94
95 // 3. Configure which attributes are encrypted and/or signed when writing new items.
96 // For each attribute that may exist on the items we plan to write to our DynamoDbTable,
97 // we must explicitly configure how they should be treated during item encryption:
98 // - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
99 // - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
100 // - DO_NOTHING: The attribute is not encrypted and not included in the signature
101 let attribute_actions_on_encrypt = HashMap::from([
102 ("partition_key".to_string(), CryptoAction::SignOnly), // Our partition attribute must be SIGN_ONLY
103 ("sort_key".to_string(), CryptoAction::SignOnly), // Our sort attribute must be SIGN_ONLY
104 ("sensitive_data".to_string(), CryptoAction::EncryptAndSign),
105 ]);
106
107 // 4. Configure which attributes we expect to be included in the signature
108 // when reading items. There are two options for configuring this:
109 //
110 // - (Recommended) Configure `allowedUnsignedAttributesPrefix`:
111 // When defining your DynamoDb schema and deciding on attribute names,
112 // choose a distinguishing prefix (such as ":") for all attributes that
113 // you do not want to include in the signature.
114 // This has two main benefits:
115 // - It is easier to reason about the security and authenticity of data within your item
116 // when all unauthenticated data is easily distinguishable by their attribute name.
117 // - If you need to add new unauthenticated attributes in the future,
118 // you can easily make the corresponding update to your `attributeActionsOnEncrypt`
119 // and immediately start writing to that new attribute, without
120 // any other configuration update needed.
121 // Once you configure this field, it is not safe to update it.
122 //
123 // - Configure `allowedUnsignedAttributes`: You may also explicitly list
124 // a set of attributes that should be considered unauthenticated when encountered
125 // on read. Be careful if you use this configuration. Do not remove an attribute
126 // name from this configuration, even if you are no longer writing with that attribute,
127 // as old items may still include this attribute, and our configuration needs to know
128 // to continue to exclude this attribute from the signature scope.
129 // If you add new attribute names to this field, you must first deploy the update to this
130 // field to all readers in your host fleet before deploying the update to start writing
131 // with that new attribute.
132 //
133 // For this example, we currently authenticate all attributes. To make it easier to
134 // add unauthenticated attributes in the future, we define a prefix ":" for such attributes.
135 const UNSIGNED_ATTR_PREFIX: &str = ":";
136
137 // 5. Create the DynamoDb Encryption configuration for the table we will be writing to.
138 let table_config = DynamoDbTableEncryptionConfig::builder()
139 .logical_table_name(ddb_table_name)
140 .partition_key_name("partition_key")
141 .sort_key_name("sort_key")
142 .attribute_actions_on_encrypt(attribute_actions_on_encrypt)
143 .keyring(raw_rsa_keyring)
144 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
145 .build()?;
146
147 let table_configs = DynamoDbTablesEncryptionConfig::builder()
148 .table_encryption_configs(HashMap::from([(ddb_table_name.to_string(), table_config)]))
149 .build()?;
150
151 // 6. Create a new AWS SDK DynamoDb client using the config above
152 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
153 let dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
154 .interceptor(DbEsdkInterceptor::new(table_configs)?)
155 .build();
156 let ddb = aws_sdk_dynamodb::Client::from_conf(dynamo_config);
157
158 // 7. Put an item into our table using the above client.
159 // Before the item gets sent to DynamoDb, it will be encrypted
160 // client-side, according to our configuration.
161 let item = HashMap::from([
162 (
163 "partition_key".to_string(),
164 AttributeValue::S("rawRsaKeyringItem".to_string()),
165 ),
166 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
167 (
168 "sensitive_data".to_string(),
169 AttributeValue::S("encrypt and sign me!".to_string()),
170 ),
171 ]);
172
173 ddb.put_item()
174 .table_name(ddb_table_name)
175 .set_item(Some(item.clone()))
176 .send()
177 .await?;
178
179 // 8. Get the item back from our table using the same client.
180 // The client will decrypt the item client-side, and return
181 // back the original item.
182
183 let key_to_get = HashMap::from([
184 (
185 "partition_key".to_string(),
186 AttributeValue::S("rawRsaKeyringItem".to_string()),
187 ),
188 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
189 ]);
190
191 let resp = ddb
192 .get_item()
193 .table_name(ddb_table_name)
194 .set_key(Some(key_to_get))
195 // In this example we configure a strongly consistent read
196 // because we perform a read immediately after a write (for demonstrative purposes).
197 // By default, reads are only eventually consistent.
198 // Read our docs to determine which read consistency to use for your application:
199 // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
200 .consistent_read(true)
201 .send()
202 .await?;
203
204 assert_eq!(resp.item, Some(item));
205 println!("raw_rsa_keyring successful.");
206 Ok(())
207}
208
209fn exists(f: &str) -> bool {
210 Path::new(f).exists()
211}
212fn should_generate_new_rsa_key_pair() -> Result<bool, String> {
213 // If a key pair already exists: do not overwrite existing key pair
214 if exists(EXAMPLE_RSA_PRIVATE_KEY_FILENAME) && exists(EXAMPLE_RSA_PUBLIC_KEY_FILENAME) {
215 Ok(false)
216 }
217 // If only one file is present: throw exception
218 else if exists(EXAMPLE_RSA_PRIVATE_KEY_FILENAME) && !exists(EXAMPLE_RSA_PUBLIC_KEY_FILENAME) {
219 Err("Missing public key file at ".to_string() + EXAMPLE_RSA_PUBLIC_KEY_FILENAME)
220 }
221 // If a key pair already exists: do not overwrite existing key pair
222 else if exists(EXAMPLE_RSA_PRIVATE_KEY_FILENAME) && !exists(EXAMPLE_RSA_PUBLIC_KEY_FILENAME) {
223 Err("Missing private key file at ".to_string() + EXAMPLE_RSA_PRIVATE_KEY_FILENAME)
224 }
225 // If neither file is present, generate a new key pair
226 else {
227 Ok(true)
228 }
229}
230
231fn generate_rsa_key_pair() -> Result<(), crate::BoxError> {
232 use aws_lc_rs::encoding::AsDer;
233 use aws_lc_rs::encoding::Pkcs8V1Der;
234 use aws_lc_rs::encoding::PublicKeyX509Der;
235 use aws_lc_rs::rsa::KeySize;
236 use aws_lc_rs::rsa::PrivateDecryptingKey;
237
238 // Safety check: Validate neither file is present
239 if exists(EXAMPLE_RSA_PRIVATE_KEY_FILENAME) || exists(EXAMPLE_RSA_PUBLIC_KEY_FILENAME) {
240 return Err(crate::BoxError(
241 "generate_rsa_key_pair will not overwrite existing PEM files".to_string(),
242 ));
243 }
244
245 // This code will generate a new RSA key pair for example use.
246 // The public and private key will be written to the files:
247 // - public: EXAMPLE_RSA_PUBLIC_KEY_FILENAME
248 // - private: EXAMPLE_RSA_PRIVATE_KEY_FILENAME
249 // This example uses aws-lc-rs's KeyPairGenerator to generate the key pair.
250 // In practice, you should not generate this in your code, and should instead
251 // retrieve this key from a secure key management system (e.g. HSM)
252 // This key is created here for example purposes only.
253
254 let private_key = PrivateDecryptingKey::generate(KeySize::Rsa2048)?;
255 let public_key = private_key.public_key();
256
257 let public_key = AsDer::<PublicKeyX509Der>::as_der(&public_key)?;
258 let public_key = pem::Pem::new("RSA PUBLIC KEY", public_key.as_ref());
259 let public_key = pem::encode(&public_key);
260
261 let private_key = AsDer::<Pkcs8V1Der>::as_der(&private_key)?;
262 let private_key = pem::Pem::new("RSA PRIVATE KEY", private_key.as_ref());
263 let private_key = pem::encode(&private_key);
264
265 std::fs::OpenOptions::new()
266 .write(true)
267 .create(true)
268 .truncate(true)
269 .open(Path::new(EXAMPLE_RSA_PRIVATE_KEY_FILENAME))?
270 .write_all(private_key.as_bytes())?;
271
272 std::fs::OpenOptions::new()
273 .write(true)
274 .create(true)
275 .truncate(true)
276 .open(Path::new(EXAMPLE_RSA_PUBLIC_KEY_FILENAME))?
277 .write_all(public_key.as_bytes())?;
278
279 Ok(())
280}