|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""This file contains methods to use for testing multi-threading for Raw RSA keyring.""" |
| 4 | +from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders |
| 5 | +from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig |
| 6 | +from aws_cryptographic_materialproviders.mpl.models import CreateRawRsaKeyringInput, PaddingScheme |
| 7 | +from aws_cryptographic_materialproviders.mpl.references import IKeyring |
| 8 | +from cryptography.hazmat.backends import default_backend as crypto_default_backend |
| 9 | +from cryptography.hazmat.primitives import serialization as crypto_serialization |
| 10 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 11 | + |
| 12 | + |
| 13 | +def generate_rsa_keys(): |
| 14 | + """Generates a 4096-bit RSA public and private key pair |
| 15 | +
|
| 16 | + Usage: generate_rsa_keys() |
| 17 | + """ |
| 18 | + ssh_rsa_exponent = 65537 |
| 19 | + bit_strength = 4096 |
| 20 | + key = rsa.generate_private_key( |
| 21 | + backend=crypto_default_backend(), |
| 22 | + public_exponent=ssh_rsa_exponent, |
| 23 | + key_size=bit_strength |
| 24 | + ) |
| 25 | + |
| 26 | + # This example choses a particular type of encoding, format and encryption_algorithm |
| 27 | + # Users can choose the PublicFormat, PrivateFormat and encryption_algorithm that align most |
| 28 | + # with their use-cases |
| 29 | + public_key = key.public_key().public_bytes( |
| 30 | + encoding=crypto_serialization.Encoding.PEM, |
| 31 | + format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo |
| 32 | + ) |
| 33 | + private_key = key.private_bytes( |
| 34 | + encoding=crypto_serialization.Encoding.PEM, |
| 35 | + format=crypto_serialization.PrivateFormat.TraditionalOpenSSL, |
| 36 | + encryption_algorithm=crypto_serialization.NoEncryption() |
| 37 | + ) |
| 38 | + |
| 39 | + return public_key, private_key |
| 40 | + |
| 41 | + |
| 42 | +def create_keyring(public_key, private_key): |
| 43 | + """Demonstrate how to create a Raw RSA keyring using the key pair. |
| 44 | +
|
| 45 | + Usage: create_keyring(public_key, private_key) |
| 46 | + """ |
| 47 | + key_name_space = "Some managed raw keys" |
| 48 | + key_name = "My 4096-bit RSA wrapping key" |
| 49 | + |
| 50 | + mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders( |
| 51 | + config=MaterialProvidersConfig() |
| 52 | + ) |
| 53 | + |
| 54 | + keyring_input: CreateRawRsaKeyringInput = CreateRawRsaKeyringInput( |
| 55 | + key_namespace=key_name_space, |
| 56 | + key_name=key_name, |
| 57 | + padding_scheme=PaddingScheme.OAEP_SHA256_MGF1, |
| 58 | + public_key=public_key, |
| 59 | + private_key=private_key |
| 60 | + ) |
| 61 | + |
| 62 | + keyring: IKeyring = mat_prov.create_raw_rsa_keyring( |
| 63 | + input=keyring_input |
| 64 | + ) |
| 65 | + |
| 66 | + return keyring |
0 commit comments