4747import java .security .cert .X509Certificate ;
4848import java .util .Collection ;
4949import java .util .Collections ;
50- import java .util .Enumeration ;
5150import java .util .LinkedHashMap ;
5251import java .util .Locale ;
5352import java .util .Map ;
108107import com .helger .http .CHttpHeader ;
109108import com .helger .io .file .FileHelper ;
110109import com .helger .mail .cte .EContentTransferEncoding ;
111- import com .helger .phase2 .exception .AS2Exception ;
112110import com .helger .phase2 .util .AS2HttpHelper ;
113111import com .helger .phase2 .util .AS2IOHelper ;
114112import com .helger .phase2 .util .AS2ResourceHelper ;
@@ -260,13 +258,23 @@ public KeyStore loadKeyStore (@Nonnull final IKeyStoreType aKeyStoreType,
260258 return aKeyStore ;
261259 }
262260
263- public boolean isEncrypted (@ Nonnull final MimeBodyPart aPart ) throws MessagingException
261+ public boolean isEncrypted (@ Nonnull final MimeBodyPart aPart )
264262 {
265263 ValueEnforcer .notNull (aPart , "Part" );
266264
265+ String sContentType ;
266+ try
267+ {
268+ sContentType = aPart .getContentType ();
269+ }
270+ catch (final MessagingException ex )
271+ {
272+ sContentType = null ;
273+ }
274+
267275 // Content-Type is sthg like this if encrypted:
268276 // application/pkcs7-mime; name=smime.p7m; smime-type=enveloped-data
269- final ContentType aContentType = AS2HttpHelper .parseContentType (aPart . getContentType () );
277+ final ContentType aContentType = AS2HttpHelper .parseContentType (sContentType );
270278 if (aContentType == null )
271279 return false ;
272280
@@ -278,19 +286,28 @@ public boolean isEncrypted (@Nonnull final MimeBodyPart aPart) throws MessagingE
278286 return sSmimeType != null && sSmimeType .equalsIgnoreCase ("enveloped-data" );
279287 }
280288
281- public boolean isSigned (@ Nonnull final MimeBodyPart aPart ) throws MessagingException
289+ public boolean isSigned (@ Nonnull final MimeBodyPart aPart )
282290 {
283291 ValueEnforcer .notNull (aPart , "Part" );
284292
285- final ContentType aContentType = AS2HttpHelper .parseContentType (aPart .getContentType ());
293+ String sContentType ;
294+ try
295+ {
296+ sContentType = aPart .getContentType ();
297+ }
298+ catch (final MessagingException ex )
299+ {
300+ sContentType = null ;
301+ }
302+ final ContentType aContentType = AS2HttpHelper .parseContentType (sContentType );
286303 if (aContentType == null )
287304 return false ;
288305
289306 final String sBaseType = aContentType .getBaseType ();
290307 return sBaseType .equalsIgnoreCase ("multipart/signed" );
291308 }
292309
293- public boolean isCompressed (@ Nonnull final String sContentType ) throws AS2Exception
310+ public boolean isCompressed (@ Nonnull final String sContentType )
294311 {
295312 ValueEnforcer .notNull (sContentType , "ContentType" );
296313
@@ -332,23 +349,33 @@ public MIC calculateMIC (@Nonnull final MimeBodyPart aPart,
332349 aMessageDigest = new LoggingMessageDigest (aMessageDigest );
333350 }
334351
352+ final int nHeadersIncluded ;
353+ final StringBuilder aHeadersIncluded = new StringBuilder ();
335354 if (bIncludeHeaders )
336355 {
337356 // Start hashing the header
338- final Enumeration <String > aHeaderLines = aPart .getAllHeaderLines ();
339- while ( aHeaderLines . hasMoreElements () )
357+ final ICommonsList <String > aHeaderLines = new CommonsArrayList <> ( aPart .getAllHeaderLines () );
358+ for ( final String sHeaderLine : aHeaderLines )
340359 {
341- final String sHeaderLine = aHeaderLines .nextElement ();
342-
343360 aMessageDigest .update (AS2IOHelper .getAllAsciiBytes (sHeaderLine ));
344361 aMessageDigest .update (EOL_BYTES );
345362
346363 if (LOGGER .isDebugEnabled ())
347364 LOGGER .debug ("Using header line '" + sHeaderLine + "' for MIC calculation" );
365+
366+ // For debug logging only
367+ if (aHeadersIncluded .length () > 0 )
368+ aHeadersIncluded .append (':' );
369+ aHeadersIncluded .append (sHeaderLine .substring (0 , sHeaderLine .indexOf (':' )));
348370 }
349371
350372 // The CRLF separator between header and content
351373 aMessageDigest .update (EOL_BYTES );
374+ nHeadersIncluded = aHeaderLines .size ();
375+ }
376+ else
377+ {
378+ nHeadersIncluded = -1 ;
352379 }
353380
354381 final String sMICEncoding = aPart .getEncoding ();
@@ -372,8 +399,14 @@ public MIC calculateMIC (@Nonnull final MimeBodyPart aPart,
372399 // Perform Base64 encoding and append algorithm ID
373400 final MIC ret = new MIC (aMIC , eDigestAlgorithm );
374401
375- if (LOGGER .isDebugEnabled ())
376- LOGGER .debug (" Calculated MIC = " + ret .getAsAS2String ());
402+ LOGGER .info (" Calculated MIC[" +
403+ eDigestAlgorithm +
404+ "][" +
405+ nHeadersIncluded +
406+ " headers; " +
407+ aHeadersIncluded .toString () +
408+ "] = " +
409+ ret .getAsAS2String ());
377410
378411 return ret ;
379412 }
@@ -715,11 +748,9 @@ public MimeBodyPart verify (@Nonnull final MimeBodyPart aPart,
715748 // Get only once and check
716749 // Throws "ParseException" if it is not a MIME message
717750 final Object aContent = aPart .getContent ();
718- if (!(aContent instanceof MimeMultipart ))
751+ if (!(aContent instanceof final MimeMultipart aMainPart ))
719752 throw new IllegalStateException ("Expected Part content to be MimeMultipart but it isn't. It is " +
720753 ClassHelper .getClassName (aContent ));
721- final MimeMultipart aMainPart = (MimeMultipart ) aContent ;
722-
723754 // SMIMESignedParser uses "7bit" as the default - AS2 wants "binary"
724755 final SMIMESignedParser aSignedParser = new SMIMESignedParser (new JcaDigestCalculatorProviderBuilder ().setProvider (m_sSecurityProviderName )
725756 .build (),
0 commit comments