Skip to content

Commit d979aa5

Browse files
committed
Started extracting MDN creation on reception; #140
1 parent 627e174 commit d979aa5

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

phase2-lib/src/main/java/com/helger/phase2/processor/receiver/net/AS2ReceiverHandler.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.helger.phase2.cert.ECertificatePartnershipType;
6464
import com.helger.phase2.cert.ICertificateFactory;
6565
import com.helger.phase2.crypto.ICryptoHelper;
66+
import com.helger.phase2.crypto.MIC;
6667
import com.helger.phase2.disposition.AS2DispositionException;
6768
import com.helger.phase2.disposition.DispositionType;
6869
import com.helger.phase2.exception.AS2Exception;
@@ -406,7 +407,7 @@ protected void sendSyncMDN (@Nonnull final String sClientInfo,
406407
final IAS2Session aSession = m_aReceiverModule.getSession ();
407408

408409
// Main MDN creation
409-
final IMessageMDN aMdn = AS2Helper.createMDN (aSession, aMsg, aDisposition, sText);
410+
final IMessageMDN aMdn = AS2Helper.createSyncMDN (aSession, aMsg, aDisposition, sText);
410411

411412
if (aMsg.isRequestingAsynchMDN ())
412413
{
@@ -570,6 +571,18 @@ public void handleIncomingMessage (@Nonnull final String sClientInfo,
570571
// message
571572
decrypt (aMsg, aResHelper);
572573

574+
// TODO Calculate MIC before decompress #140
575+
MIC aBeforeDecompressMIC;
576+
try
577+
{
578+
aBeforeDecompressMIC = AS2Helper.createMICOnReception (aMsg);
579+
}
580+
catch (final Exception ex)
581+
{
582+
// Ignore error
583+
aBeforeDecompressMIC = null;
584+
}
585+
573586
if (aCryptoHelper.isCompressed (aMsg.getContentType ()))
574587
{
575588
if (LOGGER.isTraceEnabled ())
@@ -697,7 +710,7 @@ public void handleIncomingMessage (@Nonnull final String sClientInfo,
697710

698711
// Just send a HTTP OK
699712
HTTPHelper.sendSimpleHTTPResponse (aResponseHandler, CHttp.HTTP_OK);
700-
LOGGER.info ("sent HTTP OK " + sClientInfo + aMsg.getLoggingText ());
713+
LOGGER.info ("Sent HTTP OK " + sClientInfo + aMsg.getLoggingText ());
701714
}
702715
}
703716
catch (final Exception ex)
@@ -718,7 +731,7 @@ public void handleIncomingMessage (@Nonnull final String sClientInfo,
718731
{
719732
// close and delete the temporary shared stream if it exists
720733
final TempSharedFileInputStream sis = aMsg.getTempSharedFileInputStream ();
721-
if (null != sis)
734+
if (sis != null)
722735
{
723736
try
724737
{
@@ -767,11 +780,10 @@ public void handle (@Nonnull final AbstractActiveNetModule aOwner, @Nonnull fina
767780
}
768781
else
769782
{
770-
if (aMsgDataSource instanceof ByteArrayDataSource)
783+
if (aMsgDataSource instanceof final ByteArrayDataSource aBADS)
771784
{
772785
LOGGER.info ("received " +
773-
AS2IOHelper.getTransferRate (((ByteArrayDataSource) aMsgDataSource).directGetBytes ().length,
774-
aSW) +
786+
AS2IOHelper.getTransferRate (aBADS.directGetBytes ().length, aSW) +
775787
" from " +
776788
sClientInfo +
777789
aMsg.getLoggingText ());

phase2-lib/src/main/java/com/helger/phase2/util/AS2Helper.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,38 @@ public static void createMDNData (@Nonnull final IAS2Session aSession,
239239
// (size));
240240
}
241241

242+
@Nullable
243+
public static MIC createMICOnReception (@Nonnull final AS2Message aMsg) throws Exception
244+
{
245+
final Partnership aPartnership = aMsg.partnership ();
246+
247+
final String sDispositionOptions = aMsg.getHeader (CHttpHeader.DISPOSITION_NOTIFICATION_OPTIONS);
248+
final DispositionOptions aDispositionOptions = DispositionOptions.createFromString (sDispositionOptions);
249+
250+
ECryptoAlgorithmSign eSigningAlgorithm = aDispositionOptions.getFirstMICAlg ();
251+
if (eSigningAlgorithm == null)
252+
{
253+
// Try from partnership (#93)
254+
final String sSigningAlgorithm = aPartnership.getSigningAlgorithm ();
255+
eSigningAlgorithm = ECryptoAlgorithmSign.getFromIDOrNull (sSigningAlgorithm);
256+
if (eSigningAlgorithm == null)
257+
{
258+
LOGGER.warn ("The partnership signing algorithm name '" + sSigningAlgorithm + "' is unknown.");
259+
}
260+
}
261+
262+
if (eSigningAlgorithm == null)
263+
return null;
264+
265+
// If the source message was signed or encrypted, include the headers -
266+
// see message sending for details
267+
final boolean bIncludeHeadersInMIC = aPartnership.getSigningAlgorithm () != null ||
268+
aPartnership.getEncryptAlgorithm () != null ||
269+
aPartnership.getCompressionType () != null;
270+
271+
return getCryptoHelper ().calculateMIC (aMsg.getData (), eSigningAlgorithm, bIncludeHeadersInMIC);
272+
}
273+
242274
/**
243275
* Create a new MDN
244276
*
@@ -255,10 +287,10 @@ public static void createMDNData (@Nonnull final IAS2Session aSession,
255287
* In case of an error
256288
*/
257289
@Nonnull
258-
public static IMessageMDN createMDN (@Nonnull final IAS2Session aSession,
259-
@Nonnull final AS2Message aMsg,
260-
@Nonnull final DispositionType aDisposition,
261-
@Nonnull final String sText) throws Exception
290+
public static IMessageMDN createSyncMDN (@Nonnull final IAS2Session aSession,
291+
@Nonnull final AS2Message aMsg,
292+
@Nonnull final DispositionType aDisposition,
293+
@Nonnull final String sText) throws Exception
262294
{
263295
ValueEnforcer.notNull (aSession, "AS2Session");
264296
ValueEnforcer.notNull (aMsg, "AS2Message");
@@ -324,29 +356,8 @@ public static IMessageMDN createMDN (@Nonnull final IAS2Session aSession,
324356
final String sDispositionOptions = aMsg.getHeader (CHttpHeader.DISPOSITION_NOTIFICATION_OPTIONS);
325357
final DispositionOptions aDispositionOptions = DispositionOptions.createFromString (sDispositionOptions);
326358

327-
ECryptoAlgorithmSign eSigningAlgorithm = aDispositionOptions.getFirstMICAlg ();
328-
if (eSigningAlgorithm == null)
329-
{
330-
// Try from partnership (#93)
331-
final String sSigningAlgorithm = aPartnership.getSigningAlgorithm ();
332-
eSigningAlgorithm = ECryptoAlgorithmSign.getFromIDOrNull (sSigningAlgorithm);
333-
if (eSigningAlgorithm == null)
334-
{
335-
LOGGER.warn ("The partnership signing algorithm name '" + sSigningAlgorithm + "' is unknown.");
336-
}
337-
}
338-
339-
MIC aMIC = null;
340-
if (eSigningAlgorithm != null)
341-
{
342-
// If the source message was signed or encrypted, include the headers -
343-
// see message sending for details
344-
final boolean bIncludeHeadersInMIC = aPartnership.getSigningAlgorithm () != null ||
345-
aPartnership.getEncryptAlgorithm () != null ||
346-
aPartnership.getCompressionType () != null;
347-
348-
aMIC = getCryptoHelper ().calculateMIC (aMsg.getData (), eSigningAlgorithm, bIncludeHeadersInMIC);
349-
}
359+
// Calculate MIC
360+
final MIC aMIC = createMICOnReception (aMsg);
350361
if (aMIC != null)
351362
aMDN.attrs ().putIn (AS2MessageMDN.MDNA_MIC, aMIC.getAsAS2String ());
352363

0 commit comments

Comments
 (0)