5353import jakarta .mail .internet .MimePart ;
5454import java .io .IOException ;
5555import java .math .BigInteger ;
56- import java .security .GeneralSecurityException ;
5756import java .security .PrivateKey ;
5857import java .security .PublicKey ;
5958import java .security .Security ;
@@ -84,6 +83,7 @@ public final class SmimeUtil {
8483 }
8584 }
8685
86+ @ SuppressWarnings ("unused" )
8787 private SmimeUtil () {
8888 }
8989
@@ -143,23 +143,20 @@ public static MimeBodyPart encrypt(MimeBodyPart mimeBodyPart, X509Certificate ce
143143 try {
144144 SMIMEEnvelopedGenerator generator = prepareGenerator (certificate );
145145 OutputEncryptor encryptor = prepareEncryptor ();
146-
147- MimeBodyPart encryptedMimeBodyPart = generator .generate (mimeBodyPart , encryptor );
148- return encryptedMimeBodyPart ;
146+
147+ return generator .generate (mimeBodyPart , encryptor );
149148
150149 } catch (Exception e ) {
151150 throw handledException (e );
152151 }
153152 }
154153
155154 private static void copyHeaders (MimeBodyPart fromBodyPart , MimeMessage toMessage ) throws MessagingException {
156- @ SuppressWarnings ("unchecked" )
157155 Enumeration <Header > headers = fromBodyPart .getAllHeaders ();
158156 copyHeaders (headers , toMessage );
159157 }
160158
161159 private static void copyHeaders (MimeMessage fromMessage , MimeMessage toMessage ) throws MessagingException {
162- @ SuppressWarnings ("unchecked" )
163160 Enumeration <Header > headers = fromMessage .getAllHeaders ();
164161 copyHeaders (headers , toMessage );
165162 }
@@ -186,7 +183,7 @@ private static OutputEncryptor prepareEncryptor() throws CMSException {
186183 }
187184
188185 /**
189- * Decrypts a S/MIME encrypted MIME message and yields a new MIME message.
186+ * Decrypts an S/MIME encrypted MIME message and yields a new MIME message.
190187 *
191188 * @param session
192189 * The {@link Session} that is used in conjunction with the
@@ -215,7 +212,7 @@ public static MimeMessage decrypt(Session session, MimeMessage mimeMessage, Smim
215212 }
216213
217214 /**
218- * Decrypts a S/MIME encrypted MIME body part and yields a new MIME body
215+ * Decrypts an S/MIME encrypted MIME body part and yields a new MIME body
219216 * part.
220217 *
221218 * @param mimeBodyPart
@@ -235,7 +232,7 @@ public static MimeBodyPart decrypt(MimeBodyPart mimeBodyPart, SmimeKey smimeKey)
235232 }
236233
237234 /**
238- * Decrypts a S/MIME encrypted MIME multipart and yields a new MIME body
235+ * Decrypts an S/MIME encrypted MIME multipart and yields a new MIME body
239236 * part.
240237 *
241238 * @param mimeMultipart
@@ -274,7 +271,6 @@ private static byte[] decryptContent(SMIMEEnveloped smimeEnveloped, SmimeKey smi
274271 }
275272
276273 private static void copyHeaderLines (MimeMessage fromMessage , MimeMessage toMessage ) throws MessagingException {
277- @ SuppressWarnings ("unchecked" )
278274 Enumeration <String > headerLines = fromMessage .getAllHeaderLines ();
279275 while (headerLines .hasMoreElements ()) {
280276 String nextElement = headerLines .nextElement ();
@@ -327,8 +323,7 @@ private static SignerInfoGenerator getInfoGenerator(SmimeKey smimeKey) throws Op
327323
328324 PrivateKey privateKey = smimeKey .getPrivateKey ();
329325 X509Certificate certificate = smimeKey .getCertificate ();
330- SignerInfoGenerator infoGenerator = builder .build ("SHA256withRSA" , privateKey , certificate );
331- return infoGenerator ;
326+ return builder .build ("SHA256withRSA" , privateKey , certificate );
332327 }
333328
334329 private static ASN1EncodableVector getSignedAttributes (SmimeKey smimeKey ) {
@@ -351,19 +346,18 @@ private static IssuerAndSerialNumber getIssuerAndSerialNumber(SmimeKey smimeKey)
351346 X509Certificate certificate = smimeKey .getCertificate ();
352347 BigInteger serialNumber = certificate .getSerialNumber ();
353348 X500Name issuerName = new X500Name (certificate .getIssuerDN ().getName ());
354- IssuerAndSerialNumber issuerAndSerialNumber = new IssuerAndSerialNumber (issuerName , serialNumber );
355- return issuerAndSerialNumber ;
349+ return new IssuerAndSerialNumber (issuerName , serialNumber );
356350 }
357351
358352 private static JcaCertStore getCertificateStore (SmimeKey smimeKey ) throws CertificateEncodingException {
359353 Certificate [] certificateChain = smimeKey .getCertificateChain ();
360354 X509Certificate certificate = smimeKey .getCertificate ();
361355
362- List <Certificate > certificateList = null ;
356+ final List <Certificate > certificateList ;
363357 if (certificateChain != null && certificateChain .length > 0 ) {
364358 certificateList = Arrays .asList (certificateChain );
365359 } else {
366- certificateList = new ArrayList <Certificate >();
360+ certificateList = new ArrayList <>();
367361 certificateList .add (certificate );
368362 }
369363 return new JcaCertStore (certificateList );
@@ -383,6 +377,7 @@ private static JcaCertStore getCertificateStore(SmimeKey smimeKey) throws Certif
383377 * @return The new S/MIME signed {@link MimeMessage} or {@link SMTPMessage}.
384378 */
385379 public static <T extends MimeMessage > T sign (Session session , T mimeMessage , SmimeKey smimeKey ) {
380+ //noinspection unchecked
386381 return (mimeMessage instanceof SMTPMessage )
387382 ? sign (mimeMessage , (T ) new SMTPMessage (session ), smimeKey )
388383 : sign (mimeMessage , (T ) new MimeMessage (session ), smimeKey );
@@ -411,7 +406,7 @@ private static MimeBodyPart extractMimeBodyPart(MimeMessage mimeMessage) throws
411406 }
412407
413408 /**
414- * Checks the signature on a S/MIME signed MIME multipart.
409+ * Checks the signature on an S/MIME signed MIME multipart.
415410 *
416411 * @param mimeMultipart
417412 * The {@link MimeMultipart} to be checked.
@@ -427,7 +422,7 @@ public static boolean checkSignature(MimeMultipart mimeMultipart) {
427422 }
428423
429424 /**
430- * Checks the signature on a S/MIME signed MIME part (i.e. MIME message).
425+ * Checks the signature on an S/MIME signed MIME part (i.e. MIME message).
431426 *
432427 * @param mimePart
433428 * The {@link MimePart} to be checked.
@@ -451,8 +446,7 @@ public static boolean checkSignature(MimePart mimePart) {
451446 /**
452447 * Checks a SMIMESigned to make sure that the signature matches.
453448 */
454- private static boolean checkSignature (SMIMESigned smimeSigned ) throws MessagingException , IOException ,
455- GeneralSecurityException {
449+ private static boolean checkSignature (SMIMESigned smimeSigned ) {
456450 try {
457451 boolean returnValue = true ;
458452
@@ -547,7 +541,7 @@ private static SignerInformationVerifier getVerifier(X509Certificate certificate
547541 }
548542
549543 /**
550- * Returns the signed MIME body part of a S/MIME signed MIME multipart.
544+ * Returns the signed MIME body part of an S/MIME signed MIME multipart.
551545 *
552546 * @param mimeMultipart
553547 * The {@link MimeMultipart} to be stripped off.
@@ -563,7 +557,7 @@ public static MimeBodyPart getSignedContent(MimeMultipart mimeMultipart) {
563557 }
564558
565559 /**
566- * Returns the signed MIME body part of a S/MIME signed MIME part (i.e. MIME
560+ * Returns the signed MIME body part of an S/MIME signed MIME part (i.e. MIME
567561 * message).
568562 *
569563 * @param mimePart
0 commit comments