33
33
import org .bouncycastle .operator .OutputEncryptor ;
34
34
import org .bouncycastle .operator .jcajce .JcaAlgorithmParametersConverter ;
35
35
import org .bouncycastle .util .Store ;
36
+ import org .jetbrains .annotations .Nullable ;
36
37
37
38
import javax .crypto .spec .OAEPParameterSpec ;
38
39
import javax .crypto .spec .PSource ;
@@ -87,15 +88,14 @@ private static void updateMailcapCommandMap() {
87
88
/**
88
89
* Encrypts a MIME message and yields a new S/MIME encrypted MIME message.
89
90
*
90
- * @param session The {@link Session} that is used in conjunction with the
91
- * original {@link MimeMessage} .
91
+ * @param session The {@link Session} that is used in conjunction with the original {@link MimeMessage}.
92
+ * @param messageId Optional MessageID that should be preserved on the encrypted MimeMessage result .
92
93
* @param mimeMessage The original {@link MimeMessage} to be encrypted.
93
- * @param certificate The {@link X509Certificate} used to obtain the
94
- * {@link PublicKey} to encrypt the original message with.
94
+ * @param certificate The {@link X509Certificate} used to obtain the {@link PublicKey} to encrypt the original message with.
95
95
* @return The new S/MIME encrypted {@link MimeMessage}.
96
96
*/
97
- public static MimeMessage encrypt (Session session , MimeMessage mimeMessage , X509Certificate certificate ) {
98
- return encrypt (session , mimeMessage , certificate , DEFAULT_KEY_ENCAPSULATION_ALGORITHM , DEFAULT_CIPHER );
97
+ public static MimeMessage encrypt (Session session , @ Nullable String messageId , MimeMessage mimeMessage , X509Certificate certificate ) {
98
+ return encrypt (session , mimeMessage , messageId , certificate , DEFAULT_KEY_ENCAPSULATION_ALGORITHM , DEFAULT_CIPHER );
99
99
}
100
100
101
101
/**
@@ -104,16 +104,17 @@ public static MimeMessage encrypt(Session session, MimeMessage mimeMessage, X509
104
104
* @param session The {@link Session} that is used in conjunction with the
105
105
* original {@link MimeMessage}.
106
106
* @param mimeMessage The original {@link MimeMessage} to be encrypted.
107
+ * @param messageId Optional MessageID that should be preserved on the encrypted MimeMessage result.
107
108
* @param certificate The {@link X509Certificate} used to obtain the
108
109
* {@link PublicKey} to encrypt the original message with.
109
110
* @param keyEncapsulationAlgorithm Algorithm used to encapsulate the symmetric encryption key.
110
111
* Currently, RSA RSA-OAEP with various SHA digest lengths are supported.
111
112
* @param cmsAlgorithm Encryption algorithm for symmetric content encryption.
112
113
* @return The new S/MIME encrypted {@link MimeMessage}.
113
114
*/
114
- public static MimeMessage encrypt (Session session , MimeMessage mimeMessage , X509Certificate certificate , KeyEncapsulationAlgorithm keyEncapsulationAlgorithm , ASN1ObjectIdentifier cmsAlgorithm ) {
115
+ public static MimeMessage encrypt (Session session , MimeMessage mimeMessage , @ Nullable String messageId , X509Certificate certificate , KeyEncapsulationAlgorithm keyEncapsulationAlgorithm , ASN1ObjectIdentifier cmsAlgorithm ) {
115
116
try {
116
- MimeMessage encryptedMimeMessage = new MimeMessage (session );
117
+ MimeMessage encryptedMimeMessage = new SmimeMessageIdFixingMimeMessage (session , messageId );
117
118
copyHeaders (mimeMessage , encryptedMimeMessage );
118
119
119
120
SMIMEEnvelopedGenerator generator = prepareGenerator (certificate , keyEncapsulationAlgorithm );
@@ -408,34 +409,31 @@ private static JcaCertStore getCertificateStore(SmimeKey smimeKey) throws Certif
408
409
/**
409
410
* Signs a MIME message and yields a new S/MIME signed MIME message.
410
411
*
411
- * @param session The {@link Session} that is used in conjunction with the
412
- * original {@link MimeMessage} .
412
+ * @param session The {@link Session} that is used in conjunction with the original {@link MimeMessage}.
413
+ * @param messageId Optional MessageID that should be preserved on the signed MimeMessage.
413
414
* @param mimeMessage The original {@link MimeMessage} or {@link SMTPMessage} to be signed.
414
- * @param smimeKey The {@link SmimeKey} used to obtain the {@link PrivateKey} to
415
- * sign the original message with.
415
+ * @param smimeKey The {@link SmimeKey} used to obtain the {@link PrivateKey} to sign the original message with.
416
416
* @return The new S/MIME signed {@link MimeMessage} or {@link SMTPMessage}.
417
417
*/
418
- public static <T extends MimeMessage > T sign (Session session , T mimeMessage , SmimeKey smimeKey ) {
419
- return sign (session , mimeMessage , smimeKey , DEFAULT_SIGNATURE_ALGORITHM_NAME );
418
+ public static <T extends MimeMessage > T sign (Session session , @ Nullable String messageId , T mimeMessage , SmimeKey smimeKey ) {
419
+ return sign (session , messageId , mimeMessage , smimeKey , DEFAULT_SIGNATURE_ALGORITHM_NAME );
420
420
}
421
421
422
422
/**
423
423
* Signs a MIME message and yields a new S/MIME signed MIME message.
424
424
*
425
- * @param session The {@link Session} that is used in conjunction with the
426
- * original {@link MimeMessage} .
425
+ * @param session The {@link Session} that is used in conjunction with the original {@link MimeMessage}.
426
+ * @param messageId Optional MessageID that should be preserved on the signed MimeMessage.
427
427
* @param mimeMessage The original {@link MimeMessage} or {@link SMTPMessage} to be signed.
428
- * @param smimeKey The {@link SmimeKey} used to obtain the {@link PrivateKey} to
429
- * sign the original message with.
430
- * @param algorithmName The name of the signature algorithm to use. Must be an algorithm
431
- * supported by the Bouncy Castle security provider.
428
+ * @param smimeKey The {@link SmimeKey} used to obtain the {@link PrivateKey} to sign the original message with.
429
+ * @param algorithmName The name of the signature algorithm to use. Must be an algorithm supported by the Bouncy Castle security provider.
432
430
* @return The new S/MIME signed {@link MimeMessage} or {@link SMTPMessage}.
433
431
*/
434
- public static <T extends MimeMessage > T sign (Session session , T mimeMessage , SmimeKey smimeKey , String algorithmName ) {
432
+ public static <T extends MimeMessage > T sign (Session session , @ Nullable String messageId , T mimeMessage , SmimeKey smimeKey , String algorithmName ) {
435
433
//noinspection unchecked
436
434
return (mimeMessage instanceof SMTPMessage )
437
- ? sign (mimeMessage , (T ) new SMTPMessage (session ), smimeKey , algorithmName )
438
- : sign (mimeMessage , (T ) new MimeMessage (session ), smimeKey , algorithmName );
435
+ ? sign (mimeMessage , (T ) new SmimeMessageIdFixingSMTPMessage (session , messageId ), smimeKey , algorithmName )
436
+ : sign (mimeMessage , (T ) new SmimeMessageIdFixingMimeMessage (session , messageId ), smimeKey , algorithmName );
439
437
}
440
438
441
439
private static <T extends MimeMessage > T sign (T mimeMessage , T signedMessage , SmimeKey smimeKey , String algorithmName ) {
0 commit comments