Skip to content

Commit 69abc8b

Browse files
committed
updated raw rsa keyring to get keys from user files
1 parent a60d67e commit 69abc8b

File tree

4 files changed

+99
-23
lines changed

4 files changed

+99
-23
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ __pycache__
3131

3232
# PyTest
3333
.pytest_cache
34+
test_keys/
3435

3536
# PyCharm
3637
.idea/

examples/src/keyrings/raw_aes_keyring_example.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def encrypt_and_decrypt_with_keyring():
7979
}
8080

8181
# 4. Generate a 256-bit AES key to use with your keyring.
82+
# In practice, you should get this key from a secure key management system such as an HSM.
83+
8284
# Here, the input to secrets.token_bytes() = 32 bytes = 256 bits
8385
static_key = secrets.token_bytes(32)
8486

@@ -98,7 +100,7 @@ def encrypt_and_decrypt_with_keyring():
98100
input=keyring_input
99101
)
100102

101-
# 6. Encrypt the data for the encryptionContext
103+
# 6. Encrypt the data with the encryptionContext
102104
ciphertext, _ = client.encrypt(
103105
source=EXAMPLE_DATA,
104106
keyring=raw_aes_keyring,

examples/src/keyrings/raw_rsa_keyring_example.py

+29-14
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@
5858
EXAMPLE_DATA: bytes = b"Hello World"
5959

6060

61-
def should_generate_new_rsa_key_pair(public_key, private_key):
62-
"""Returns True if user doesn't provide keys, and we need to generate them and
63-
returns False if the user has already provided both public and private keys
64-
Raises an AssertionError if the user only provides one of private_key and public_key
61+
def should_generate_new_rsa_key_pair(public_key_file_name, private_key_file_name):
62+
"""Returns True if user doesn't provide keys, and we need to generate them;
63+
Returns False if the user has already provided both public and private keys
64+
Raises a ValueError if the user only provides one of private_key and public_key
6565
66-
Usage: should_generate_new_rsa_key_pair(public_key, private_key)
66+
Usage: should_generate_new_rsa_key_pair(public_key_file_name, private_key_file_name)
6767
"""
68-
# If only one of public_key and private_key is provided, raise an Assertion Error
69-
if (public_key and not private_key) or (not public_key and private_key):
70-
raise AssertionError("Either both public and private keys should be provided! Or no keys \
68+
# If only one of public_key and private_key files is provided, raise a ValueError
69+
if (public_key_file_name and not private_key_file_name)\
70+
or (not public_key_file_name and private_key_file_name):
71+
raise ValueError("Either both public and private keys should be provided! Or no keys \
7172
should be provided and the example can create the keys for you!")
7273

7374
# If no keys are provided, we should generate a new rsa key pair, so return True
74-
if not public_key and not private_key:
75+
if not public_key_file_name and not private_key_file_name:
7576
return True
7677

7778
# If both keys are already provided, return False
@@ -139,12 +140,12 @@ def create_rsa_keyring(public_key, private_key):
139140
return raw_rsa_keyring
140141

141142

142-
def encrypt_and_decrypt_with_keyring(public_key=None, private_key=None):
143+
def encrypt_and_decrypt_with_keyring(public_key_file_name=None, private_key_file_name=None):
143144
"""Demonstrate an encrypt/decrypt cycle using a Raw RSA keyring
144145
with user defined keys. If no keys are present, generate new RSA
145146
public and private keys and use them to create a Raw RSA keyring
146147
147-
Usage: encrypt_and_decrypt_with_keyring(public_key, private_key)
148+
Usage: encrypt_and_decrypt_with_keyring(public_key_file_name, private_key_file_name)
148149
"""
149150
# 1. Instantiate the encryption SDK client.
150151
# This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
@@ -173,17 +174,31 @@ def encrypt_and_decrypt_with_keyring(public_key=None, private_key=None):
173174

174175
# Check if we need to generate an RSA key pair
175176
should_generate_new_rsa_key_pair_bool = \
176-
should_generate_new_rsa_key_pair(public_key=public_key, private_key=private_key)
177+
should_generate_new_rsa_key_pair(public_key_file_name=public_key_file_name,
178+
private_key_file_name=private_key_file_name)
177179

178180
# If user doesn't provide the keys, that is, if should_generate_new_rsa_key_pair_bool is True
179181
# generate a new RSA public and private key pair
180182
if should_generate_new_rsa_key_pair_bool:
181183
public_key, private_key = generate_rsa_keys()
184+
else:
185+
# If user provides the keys, read the keys from the files
186+
with open(public_key_file_name, "r", encoding='utf-8') as f:
187+
public_key = f.read()
188+
189+
# Convert the public key from a string to bytes
190+
public_key = bytes(public_key, 'utf-8')
191+
192+
with open(private_key_file_name, "r", encoding='utf-8') as f:
193+
private_key = f.read()
194+
195+
# Convert the private key from a string to bytes
196+
private_key = bytes(private_key, 'utf-8')
182197

183198
# Create the keyring
184199
raw_rsa_keyring = create_rsa_keyring(public_key=public_key, private_key=private_key)
185200

186-
# 4. Encrypt the data for the encryptionContext
201+
# 4. Encrypt the data with the encryptionContext
187202
ciphertext, _ = client.encrypt(
188203
source=EXAMPLE_DATA,
189204
keyring=raw_rsa_keyring,
@@ -212,7 +227,7 @@ def encrypt_and_decrypt_with_keyring(public_key=None, private_key=None):
212227
assert plaintext_bytes == EXAMPLE_DATA
213228

214229
# The next part of the example creates a new RSA keyring (for Bob) to demonstrate that
215-
# decryption of the original ciphertext is not possible with a different keyring (Bob's)
230+
# decryption of the original ciphertext is not possible with a different keyring (Bob's).
216231
# (This is an example for demonstration; you do not need to do this in your own code.)
217232

218233
# 9. Create a new Raw RSA keyring for Bob

examples/test/keyrings/test_i_raw_rsa_keyring_example.py

+66-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
"""Test suite for the Raw RSA keyring example."""
4+
import os
5+
46
import pytest
57

68
from ...src.keyrings.raw_rsa_keyring_example import encrypt_and_decrypt_with_keyring, generate_rsa_keys
@@ -20,39 +22,95 @@ def test_encrypt_and_decrypt_with_keyring_with_user_defined_keys():
2022
"""Test function for encrypt and decrypt using the Raw RSA Keyring example.
2123
2224
Here user provides the public and private keys. To test this, we create the
23-
keys using the generate_rsa_keys function
25+
keys using the generate_rsa_keys function and write them to the file.
26+
Then we call the encrypt_and_decrypt_with_keyring function and pass them
2427
"""
28+
# Generate the user keys for testing
2529
user_public_key, user_private_key = generate_rsa_keys()
26-
encrypt_and_decrypt_with_keyring(public_key=user_public_key, private_key=user_private_key)
30+
31+
# Convert the keys to strings
32+
user_public_key = user_public_key.decode('utf-8')
33+
user_private_key = user_private_key.decode('utf-8')
34+
35+
test_keys_directory = 'test_keys'
36+
if not os.path.exists(test_keys_directory):
37+
os.makedirs(test_keys_directory)
38+
39+
# Define the file names for the keys
40+
user_public_key_file_name = test_keys_directory + '/user_public_key_file_name.pem'
41+
user_private_key_file_name = test_keys_directory + '/user_private_key_file_name.pem'
42+
43+
# Write the public key to the file
44+
with open(user_public_key_file_name, "w", encoding="utf-8") as f:
45+
f.write(user_public_key)
46+
47+
# Write the private key to the file
48+
with open(user_private_key_file_name, "w", encoding="utf-8") as f:
49+
f.write(user_private_key)
50+
51+
encrypt_and_decrypt_with_keyring(public_key_file_name=user_public_key_file_name,
52+
private_key_file_name=user_private_key_file_name)
2753

2854

2955
def test_encrypt_and_decrypt_fails_if_user_provides_only_public_key():
3056
"""Test function for encrypt and decrypt using the Raw RSA Keyring example.
3157
32-
Here user provides only the public key. The program should throw an Assertion error
58+
Here user provides only the public key. The program should throw an Value error
3359
as this example requires the user to either provide both private and public keys to
3460
test both encryption and decryption, or not provide any keys and the example generates both
3561
"""
62+
# Generate the user keys for testing
3663
user_public_key, user_private_key = generate_rsa_keys()
64+
65+
# Convert the public key to string
66+
user_public_key = user_public_key.decode('utf-8')
67+
68+
test_keys_directory = 'test_keys'
69+
if not os.path.exists(test_keys_directory):
70+
os.makedirs(test_keys_directory)
71+
72+
# Define the file name for the public key
73+
user_public_key_file_name = test_keys_directory + '/user_public_key_file_name.pem'
74+
75+
# Write the public key to the file
76+
with open(user_public_key_file_name, "w", encoding="utf-8") as f:
77+
f.write(user_public_key)
78+
3779
try:
38-
encrypt_and_decrypt_with_keyring(public_key=user_public_key)
80+
encrypt_and_decrypt_with_keyring(public_key_file_name=user_public_key_file_name)
3981

4082
raise AssertionError("encrypt_and_decrypt_with_keyring should raise an error")
41-
except AssertionError:
83+
except ValueError:
4284
pass
4385

4486

4587
def test_encrypt_and_decrypt_fails_if_user_provides_only_private_key():
4688
"""Test function for encrypt and decrypt using the Raw RSA Keyring example.
4789
48-
Here user provides only the private key. The program should throw an Assertion error
90+
Here user provides only the private key. The program should throw an Value error
4991
as this example requires the user to either provide both private and public keys to
5092
test both encryption and decryption, or not provide any keys and the example generates both
5193
"""
94+
# Generate the user keys for testing
5295
user_public_key, user_private_key = generate_rsa_keys()
96+
97+
# Convert the private key to string
98+
user_private_key = user_private_key.decode('utf-8')
99+
100+
test_keys_directory = 'test_keys'
101+
if not os.path.exists(test_keys_directory):
102+
os.makedirs(test_keys_directory)
103+
104+
# Define the file name for the private key
105+
user_private_key_file_name = test_keys_directory + '/user_private_key_file_name.pem'
106+
107+
# Write the private key to the file
108+
with open(user_private_key_file_name, "w", encoding="utf-8") as f:
109+
f.write(user_private_key)
110+
53111
try:
54-
encrypt_and_decrypt_with_keyring(private_key=user_private_key)
112+
encrypt_and_decrypt_with_keyring(private_key_file_name=user_private_key_file_name)
55113

56114
raise AssertionError("encrypt_and_decrypt_with_keyring should raise an error")
57-
except AssertionError:
115+
except ValueError:
58116
pass

0 commit comments

Comments
 (0)