@@ -65,12 +65,15 @@ def decompress_message(compressed_data):
65
65
raise DecompressionError ("Decompression failed with cause: {}" .format (e )) from e
66
66
67
67
68
- def encrypt_message (data_to_encrypt , enc_alg , encryption_cert ):
68
+ def encrypt_message (
69
+ data_to_encrypt , enc_alg , encryption_cert , key_enc_alg = "rsaes_pkcs1v15"
70
+ ):
69
71
"""Function encrypts data and returns the generated ASN.1
70
72
71
73
:param data_to_encrypt: A byte string of the data to be encrypted
72
74
:param enc_alg: The algorithm to be used for encrypting the data
73
75
:param encryption_cert: The certificate to be used for encrypting the data
76
+ :param key_enc_alg: The algo for the key encryption: rsaes_pkcs1v15 (default) or rsaes_oaep
74
77
75
78
:return: A CMS ASN.1 byte string of the encrypted data.
76
79
"""
@@ -136,7 +139,12 @@ def encrypt_message(data_to_encrypt, enc_alg, encryption_cert):
136
139
raise AS2Exception ("Unsupported Encryption Algorithm" )
137
140
138
141
# Encrypt the key and build the ASN.1 message
139
- encrypted_key = asymmetric .rsa_pkcs1v15_encrypt (encryption_cert , key )
142
+ if key_enc_alg == "rsaes_pkcs1v15" :
143
+ encrypted_key = asymmetric .rsa_pkcs1v15_encrypt (encryption_cert , key )
144
+ elif key_enc_alg == "rsaes_oaep" :
145
+ encrypted_key = asymmetric .rsa_oaep_encrypt (encryption_cert , key )
146
+ else :
147
+ raise AS2Exception (f"Unsupported Key Encryption Scheme: { key_enc_alg } " )
140
148
141
149
return cms .ContentInfo (
142
150
{
@@ -163,7 +171,11 @@ def encrypt_message(data_to_encrypt, enc_alg, encryption_cert):
163
171
}
164
172
),
165
173
"key_encryption_algorithm" : cms .KeyEncryptionAlgorithm (
166
- {"algorithm" : cms .KeyEncryptionAlgorithmId ("rsa" )}
174
+ {
175
+ "algorithm" : cms .KeyEncryptionAlgorithmId (
176
+ key_enc_alg
177
+ )
178
+ }
167
179
),
168
180
"encrypted_key" : cms .OctetString (encrypted_key ),
169
181
}
@@ -199,47 +211,52 @@ def decrypt_message(encrypted_data, decryption_key):
199
211
key_enc_alg = recipient_info ["key_encryption_algorithm" ]["algorithm" ].native
200
212
encrypted_key = recipient_info ["encrypted_key" ].native
201
213
202
- if cms . KeyEncryptionAlgorithmId ( key_enc_alg ) == cms . KeyEncryptionAlgorithmId (
203
- "rsa"
204
- ):
205
- try :
214
+ try :
215
+ if cms . KeyEncryptionAlgorithmId (
216
+ key_enc_alg
217
+ ) == cms . KeyEncryptionAlgorithmId ( "rsaes_pkcs1v15" ) :
206
218
key = asymmetric .rsa_pkcs1v15_decrypt (decryption_key [0 ], encrypted_key )
207
- except Exception as e :
208
- raise DecryptionError (
209
- "Failed to decrypt the payload: Could not extract decryption key."
210
- ) from e
211
-
212
- alg = cms_content ["content" ]["encrypted_content_info" ][
213
- "content_encryption_algorithm"
214
- ]
215
- encapsulated_data = cms_content ["content" ]["encrypted_content_info" ][
216
- "encrypted_content"
217
- ].native
218
219
219
- try :
220
- if alg ["algorithm" ].native == "rc4" :
221
- decrypted_content = symmetric .rc4_decrypt (key , encapsulated_data )
222
- elif alg .encryption_cipher == "tripledes" :
223
- cipher = "tripledes_192_cbc"
224
- decrypted_content = symmetric .tripledes_cbc_pkcs5_decrypt (
225
- key , encapsulated_data , alg .encryption_iv
226
- )
227
- elif alg .encryption_cipher == "aes" :
228
- decrypted_content = symmetric .aes_cbc_pkcs7_decrypt (
229
- key , encapsulated_data , alg .encryption_iv
230
- )
231
- elif alg .encryption_cipher == "rc2" :
232
- decrypted_content = symmetric .rc2_cbc_pkcs5_decrypt (
233
- key , encapsulated_data , alg ["parameters" ]["iv" ].native
234
- )
235
- else :
236
- raise AS2Exception ("Unsupported Encryption Algorithm" )
237
- except Exception as e :
238
- raise DecryptionError (
239
- "Failed to decrypt the payload: {}" .format (e )
240
- ) from e
241
- else :
242
- raise AS2Exception ("Unsupported Encryption Algorithm" )
220
+ elif cms .KeyEncryptionAlgorithmId (
221
+ key_enc_alg
222
+ ) == cms .KeyEncryptionAlgorithmId ("rsaes_oaep" ):
223
+ key = asymmetric .rsa_oaep_decrypt (decryption_key [0 ], encrypted_key )
224
+ else :
225
+ raise AS2Exception (
226
+ f"Unsupported Key Encryption Algorithm { key_enc_alg } "
227
+ )
228
+ except Exception as e :
229
+ raise DecryptionError (
230
+ "Failed to decrypt the payload: Could not extract decryption key."
231
+ ) from e
232
+
233
+ alg = cms_content ["content" ]["encrypted_content_info" ][
234
+ "content_encryption_algorithm"
235
+ ]
236
+ encapsulated_data = cms_content ["content" ]["encrypted_content_info" ][
237
+ "encrypted_content"
238
+ ].native
239
+
240
+ try :
241
+ if alg ["algorithm" ].native == "rc4" :
242
+ decrypted_content = symmetric .rc4_decrypt (key , encapsulated_data )
243
+ elif alg .encryption_cipher == "tripledes" :
244
+ cipher = "tripledes_192_cbc"
245
+ decrypted_content = symmetric .tripledes_cbc_pkcs5_decrypt (
246
+ key , encapsulated_data , alg .encryption_iv
247
+ )
248
+ elif alg .encryption_cipher == "aes" :
249
+ decrypted_content = symmetric .aes_cbc_pkcs7_decrypt (
250
+ key , encapsulated_data , alg .encryption_iv
251
+ )
252
+ elif alg .encryption_cipher == "rc2" :
253
+ decrypted_content = symmetric .rc2_cbc_pkcs5_decrypt (
254
+ key , encapsulated_data , alg ["parameters" ]["iv" ].native
255
+ )
256
+ else :
257
+ raise AS2Exception ("Unsupported Encryption Algorithm" )
258
+ except Exception as e :
259
+ raise DecryptionError ("Failed to decrypt the payload: {}" .format (e )) from e
243
260
else :
244
261
raise DecryptionError ("Encrypted data not found in ASN.1 " )
245
262
0 commit comments