Skip to content

Commit 6b844cc

Browse files
authored
Merge pull request #25 from lawtonpittenger/main
bug fixes
2 parents 1a9186e + 17b4a38 commit 6b844cc

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

key-import-export/rsa/export_app_with_signature/cfn/template.yaml

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ Resources:
501501
502502
self._hash_algo = 'sha256'
503503
self._other_extensions = {}
504-
self._kms_signature_algo = 'RSASSA_PSS_SHA_256'
504+
self._kms_signature_algo = 'RSASSA_PKCS1_V1_5_SHA_256'
505505
506506
@_writer
507507
def subject(self, value):
@@ -895,7 +895,7 @@ Resources:
895895
896896
# Check if Root Cert ARN & ICA Cert ARN are present, if not, import them
897897
if not APC_ROOT_KEY_ARN:
898-
APC_ROOT_KEY_ARN = import_public_key_to_payment_crypto(root_cert, ica_cert)
898+
APC_ROOT_KEY_ARN, APC_ICA_KEY_ARN = import_public_key_to_payment_crypto(root_cert, ica_cert)
899899
else:
900900
print("Root Key ARN already found:", APC_ROOT_KEY_ARN)
901901
@@ -925,7 +925,7 @@ Resources:
925925
public_key = cert.public_key()
926926
927927
# Export AES_KEY1 using RSA-OAEP with RSA_KEY1 as the wrapping key
928-
enc_aes_key1 = export_aes_key(APC_KEY_ARN, cert_contents, APC_ROOT_KEY_ARN)
928+
enc_aes_key1 = export_aes_key(APC_KEY_ARN, cert_contents, APC_ICA_KEY_ARN)
929929
930930
# Prepend the appropriate key block header to ENC_AES_KEY1
931931
enc_aes_key1_with_header = prepend_key_block_header(enc_aes_key1, DATA_TYPE)
@@ -940,23 +940,27 @@ Resources:
940940
'APC_ROOT_KEY_ARN': APC_ROOT_KEY_ARN,
941941
'KMS_KEY_ARN': KMS_KEY_ARN,
942942
'APC_KEY_ARN': APC_KEY_ARN,
943-
'enc_aes_key1': enc_aes_key1, # This is already a string
944-
'kcv': kcv, # Assume this is already in the correct format
943+
'enc_aes_key1': enc_aes_key1,
944+
'kcv': kcv,
945945
'signature': hex_signature
946946
}
947947
948-
949948
# Optionally store results in S3
950949
if S3_BUCKET:
951950
s3_locations = store_results_in_s3(result, S3_BUCKET, S3_PREFIX)
952951
result['s3_locations'] = s3_locations
953-
952+
953+
response_body = {
954+
'message': 'All operations completed successfully',
955+
'result': result
956+
}
957+
954958
return {
955959
'statusCode': 200,
956-
'body': json.dumps({
957-
'message': 'All operations completed successfully',
958-
'result': result
959-
})
960+
'body': response_body,
961+
'headers': {
962+
'Content-Type': 'application/json'
963+
}
960964
}
961965
except Exception as e:
962966
# Get the full traceback
@@ -1173,7 +1177,7 @@ Resources:
11731177
11741178
intermediateARN = response['Key']['KeyArn']
11751179
1176-
return intermediateARN
1180+
return rootARN, intermediateARN
11771181
11781182
except ClientError as e:
11791183
error_code = e.response['Error']['Code']
@@ -1289,10 +1293,11 @@ Resources:
12891293
12901294
def sign_with_kms(data_to_sign, key_arn):
12911295
kms_client = boto3.client('kms')
1296+
message_bytes = bytes.fromhex(data_to_sign)
12921297
12931298
response = kms_client.sign(
12941299
KeyId=key_arn,
1295-
Message=data_to_sign.encode(),
1300+
Message=message_bytes,
12961301
MessageType='RAW',
12971302
SigningAlgorithm='RSASSA_PKCS1_V1_5_SHA_256'
12981303
)
@@ -1303,15 +1308,37 @@ Resources:
13031308
s3_client = boto3.client('s3')
13041309
s3_locations = {}
13051310
1311+
# Combine the three key ARNs into one file
1312+
key_arns = [
1313+
f"KMS Key ARN: {result['KMS_KEY_ARN']}",
1314+
f"APC Root Key ARN: {result['APC_ROOT_KEY_ARN']}",
1315+
f"APC Key ARN: {result['APC_KEY_ARN']}"
1316+
]
1317+
key_arns_content = "\n".join(key_arns)
1318+
1319+
file_key = f"{prefix}KEY_ARNS.txt"
1320+
try:
1321+
s3_client.put_object(
1322+
Bucket=bucket,
1323+
Key=file_key,
1324+
Body=key_arns_content.encode('utf-8')
1325+
)
1326+
s3_locations['KEY_ARNS'] = f"s3://{bucket}/{file_key}"
1327+
except ClientError as e:
1328+
print(f"Error storing KEY_ARNS in S3: {e}")
1329+
raise
1330+
1331+
# Store the remaining files
13061332
for key, value in result.items():
1333+
if key in ['KMS_KEY_ARN', 'APC_ROOT_KEY_ARN', 'APC_KEY_ARN']:
1334+
# Skip these keys as they are already combined
1335+
continue
1336+
13071337
if value is None:
13081338
print(f"Skipping storing {key} in S3 as the value is None.")
13091339
continue
13101340
1311-
if key in ['APC_ROOT_KEY_ARN', 'KMS_KEY_ARN', 'APC_KEY_ARN']:
1312-
# These are not files, just store them as text
1313-
content = str(value)
1314-
elif key in ['enc_aes_key1', 'kcv']:
1341+
if key in ['enc_aes_key1', 'kcv']:
13151342
# These are already in hex format, store as is
13161343
content = str(value)
13171344
elif key == 'signature':
@@ -1334,7 +1361,10 @@ Resources:
13341361
except ClientError as e:
13351362
print(f"Error storing {key} in S3: {e}")
13361363
raise
1364+
13371365
return s3_locations
1366+
1367+
13381368
Environment:
13391369
Variables:
13401370
CERTIFICATE_S3_URI: !Ref CertificateS3URI

key-import-export/rsa/export_app_with_signature/lambdas/csrbuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def __init__(self, subject, KMS_ARN):
120120

121121
self._hash_algo = 'sha256'
122122
self._other_extensions = {}
123-
self._kms_signature_algo = 'RSASSA_PSS_SHA_256'
123+
self._kms_signature_algo = 'RSASSA_PKCS1_V1_5_SHA_256'
124124

125125
@_writer
126126
def subject(self, value):

key-import-export/rsa/export_app_with_signature/lambdas/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def lambda_handler(event, context):
9999

100100
# Check if Root Cert ARN & ICA Cert ARN are present, if not, import them
101101
if not APC_ROOT_KEY_ARN:
102-
APC_ROOT_KEY_ARN = import_public_key_to_payment_crypto(root_cert, ica_cert)
102+
APC_ROOT_KEY_ARN, APC_ICA_KEY_ARN = import_public_key_to_payment_crypto(root_cert, ica_cert)
103103
else:
104104
print("Root Key ARN already found:", APC_ROOT_KEY_ARN)
105105

@@ -129,7 +129,7 @@ def lambda_handler(event, context):
129129
public_key = cert.public_key()
130130

131131
# Export AES_KEY1 using RSA-OAEP with RSA_KEY1 as the wrapping key
132-
enc_aes_key1 = export_aes_key(APC_KEY_ARN, cert_contents, APC_ROOT_KEY_ARN)
132+
enc_aes_key1 = export_aes_key(APC_KEY_ARN, cert_contents, APC_ICA_KEY_ARN)
133133

134134
# Prepend the appropriate key block header to ENC_AES_KEY1
135135
enc_aes_key1_with_header = prepend_key_block_header(enc_aes_key1, DATA_TYPE)
@@ -381,7 +381,7 @@ def import_public_key_to_payment_crypto(root_cert, ica_cert):
381381

382382
intermediateARN = response['Key']['KeyArn']
383383

384-
return intermediateARN
384+
return rootARN, intermediateARN
385385

386386
except ClientError as e:
387387
error_code = e.response['Error']['Code']
@@ -497,10 +497,11 @@ def prepend_key_block_header(enc_aes_key, DATA_TYPE):
497497

498498
def sign_with_kms(data_to_sign, key_arn):
499499
kms_client = boto3.client('kms')
500+
message_bytes = bytes.fromhex(data_to_sign)
500501

501502
response = kms_client.sign(
502503
KeyId=key_arn,
503-
Message=data_to_sign.encode(),
504+
Message=message_bytes,
504505
MessageType='RAW',
505506
SigningAlgorithm='RSASSA_PKCS1_V1_5_SHA_256'
506507
)

0 commit comments

Comments
 (0)