@@ -114,101 +114,96 @@ public function execute(ServerRequestInterface $request = null)
114
114
115
115
$ micalg = $ payload ->getParsedHeader ('Disposition-Notification-Options ' , 2 , 0 );
116
116
117
- try {
118
-
119
- // Check if payload is encrypted and if so decrypt it
120
- if ($ payload ->isEncrypted ()) {
121
- $ this ->getLogger ()->debug ('Inbound AS2 message is encrypted. ' );
122
- $ payload = CryptoHelper::decrypt ($ payload , $ receiver ->getCertificate (), $ receiver ->getPrivateKey ());
123
- $ this ->getLogger ()->debug ('The inbound AS2 message data has been decrypted. ' );
124
- $ message ->setEncrypted ();
125
- }
126
117
127
- // Check if message from this partner are expected to be signed
128
- // if ($receiver->getSignatureAlgorithm() && !$payload->isSigned()) {
129
- // throw new \InvalidArgumentException('Incoming message from AS2 partner are defined to be signed');
130
- // }
131
-
132
- // Check if message is signed and if so verify it
133
- if ($ payload ->isSigned ()) {
134
- $ this ->getLogger ()->debug ('Inbound AS2 message is signed. ' );
135
- $ this ->getLogger ()->debug (sprintf ('The sender used the algorithm "%s" to sign the inbound AS2 message. ' ,
136
- $ micalg ));
137
- $ this ->getLogger ()->debug ('Using certificate to verify inbound AS2 message signature. ' );
138
- if (! CryptoHelper::verify ($ payload , $ sender ->getCertificate ())) {
139
- throw new \RuntimeException ('Signature Verification Failed ' );
140
- }
141
- $ this ->getLogger ()->debug ('Digital signature of inbound AS2 message has been verified successful. ' );
142
- $ this ->getLogger ()->debug (sprintf ('Found %s payload attachments in the inbound AS2 message. ' ,
143
- $ payload ->getCountParts () - 1 ));
118
+ // Check if payload is encrypted and if so decrypt it
119
+ if ($ payload ->isEncrypted ()) {
120
+ $ this ->getLogger ()->debug ('Inbound AS2 message is encrypted. ' );
121
+ $ payload = CryptoHelper::decrypt ($ payload , $ receiver ->getCertificate (), $ receiver ->getPrivateKey ());
122
+ $ this ->getLogger ()->debug ('The inbound AS2 message data has been decrypted. ' );
123
+ $ message ->setEncrypted ();
124
+ }
144
125
145
- if (! $ micalg ) {
146
- $ micalg = $ payload ->getParsedHeader ('content-type ' , 0 , 'micalg ' );
147
- }
148
- foreach ($ payload ->getParts () as $ part ) {
149
- if (! $ part ->isPkc7Signature ()) {
150
- $ payload = $ part ;
151
- }
152
- }
153
- // TODO: AS2-Version: 1.1 multiple attachments
154
- // Saving the message mic for sending it in the MDN
155
- $ message ->setMic (CryptoHelper::calculateMIC ($ payload , $ micalg ));
156
- $ message ->setSigned ();
157
- }
126
+ // Check if message from this partner are expected to be signed
127
+ // if ($receiver->getSignatureAlgorithm() && !$payload->isSigned()) {
128
+ // throw new \InvalidArgumentException('Incoming message from AS2 partner are defined to be signed');
129
+ // }
158
130
159
- // Check if the message has been compressed and if so decompress it
160
- if ($ payload ->isCompressed ()) {
161
- $ this ->getLogger ()->debug ('Decompressing the payload ' );
162
- $ payload = CryptoHelper::decompress ($ payload );
163
- $ message ->setCompressed ();
131
+ // Check if message is signed and if so verify it
132
+ if ($ payload ->isSigned ()) {
133
+ $ this ->getLogger ()->debug ('Inbound AS2 message is signed. ' );
134
+ $ this ->getLogger ()->debug (sprintf ('The sender used the algorithm "%s" to sign the inbound AS2 message. ' ,
135
+ $ micalg ));
136
+ $ this ->getLogger ()->debug ('Using certificate to verify inbound AS2 message signature. ' );
137
+ if (! CryptoHelper::verify ($ payload , $ sender ->getCertificate ())) {
138
+ throw new \RuntimeException ('Signature Verification Failed ' );
164
139
}
140
+ $ this ->getLogger ()->debug ('Digital signature of inbound AS2 message has been verified successful. ' );
141
+ $ this ->getLogger ()->debug (sprintf ('Found %s payload attachments in the inbound AS2 message. ' ,
142
+ $ payload ->getCountParts () - 1 ));
165
143
166
- // If this is a MDN, get the Message-Id and check if it exists
167
- if ($ payload ->isReport ()) {
168
- // Get Original Message-Id
169
- $ messageId = null ;
170
- foreach ($ payload ->getParts () as $ part ) {
171
- if ($ part ->getParsedHeader ('content-type ' , 0 , 0 ) == 'message/disposition-notification ' ) {
172
- $ bodyPayload = MimePart::fromString ($ part ->getBody ());
173
- $ messageId = trim ($ bodyPayload ->getParsedHeader ('original-message-id ' , 0 , 0 ), '<> ' );
174
- }
144
+ if (! $ micalg ) {
145
+ $ micalg = $ payload ->getParsedHeader ('content-type ' , 0 , 'micalg ' );
146
+ }
147
+ foreach ($ payload ->getParts () as $ part ) {
148
+ if (! $ part ->isPkc7Signature ()) {
149
+ $ payload = $ part ;
175
150
}
176
- $ this ->getLogger ()->debug ('Asynchronous MDN received for AS2 message ' , [$ messageId ]);
177
- $ message = $ this ->storage ->getMessage ($ messageId );
178
- if (! $ message ) {
179
- throw new \InvalidArgumentException ('Unknown AS2 MDN received. Will not be processed ' );
151
+ }
152
+ // TODO: AS2-Version: 1.1 multiple attachments
153
+ // Saving the message mic for sending it in the MDN
154
+ $ message ->setMic (CryptoHelper::calculateMIC ($ payload , $ micalg ));
155
+ $ message ->setSigned ();
156
+ }
157
+
158
+ // Check if the message has been compressed and if so decompress it
159
+ if ($ payload ->isCompressed ()) {
160
+ $ this ->getLogger ()->debug ('Decompressing the payload ' );
161
+ $ payload = CryptoHelper::decompress ($ payload );
162
+ $ message ->setCompressed ();
163
+ }
164
+
165
+ // If this is a MDN, get the Message-Id and check if it exists
166
+ if ($ payload ->isReport ()) {
167
+ // Get Original Message-Id
168
+ $ messageId = null ;
169
+ foreach ($ payload ->getParts () as $ part ) {
170
+ if ($ part ->getParsedHeader ('content-type ' , 0 , 0 ) == 'message/disposition-notification ' ) {
171
+ $ bodyPayload = MimePart::fromString ($ part ->getBody ());
172
+ $ messageId = trim ($ bodyPayload ->getParsedHeader ('original-message-id ' , 0 , 0 ), '<> ' );
180
173
}
181
- $ this ->manager ->processMdn ($ message , $ payload );
182
- $ this ->storage ->saveMessage ($ message );
183
- $ responseBody = 'AS2 ASYNC MDN has been received ' ;
184
- } else {
185
-
186
- $ message ->setPayload ((string ) $ payload );
187
- $ message ->setStatus (MessageInterface::STATUS_SUCCESS );
188
- // $this->manager->processMessage($message, $payload);
189
-
190
- // if MDN enabled than send notification
191
- if ($ mdnMode = $ receiver ->getMdnMode ()) {
192
- $ mdn = $ this ->manager ->buildMdn ($ message );
193
- $ message ->setMdnStatus (MessageInterface::MDN_STATUS_SENT );
194
- if ($ mdnMode == PartnerInterface::MDN_MODE_SYNC ) {
195
- $ this ->getLogger ()->debug (sprintf ('Synchronous MDN sent as answer to message "%s". ' ,
196
- $ messageId ));
197
- $ responseHeaders = $ mdn ->getHeaders ();
198
- $ responseBody = $ mdn ->getBody ();
199
- } else {
200
- $ this ->getLogger ()->debug (sprintf ('Asynchronous MDN sent as answer to message "%s". ' ,
201
- $ messageId ));
202
- $ this ->manager ->sendMdn ($ message );
203
- }
174
+ }
175
+ $ this ->getLogger ()->debug ('Asynchronous MDN received for AS2 message ' , [$ messageId ]);
176
+ $ message = $ this ->storage ->getMessage ($ messageId );
177
+ if (! $ message ) {
178
+ throw new \InvalidArgumentException ('Unknown AS2 MDN received. Will not be processed ' );
179
+ }
180
+ $ this ->manager ->processMdn ($ message , $ payload );
181
+ $ this ->storage ->saveMessage ($ message );
182
+ $ responseBody = 'AS2 ASYNC MDN has been received ' ;
183
+ } else {
184
+
185
+ $ message ->setPayload ((string ) $ payload );
186
+ $ message ->setStatus (MessageInterface::STATUS_SUCCESS );
187
+ // $this->manager->processMessage($message, $payload);
188
+
189
+ // if MDN enabled than send notification
190
+ if ($ mdnMode = $ receiver ->getMdnMode ()) {
191
+ $ mdn = $ this ->manager ->buildMdn ($ message );
192
+ $ message ->setMdnStatus (MessageInterface::MDN_STATUS_SENT );
193
+ if ($ mdnMode == PartnerInterface::MDN_MODE_SYNC ) {
194
+ $ this ->getLogger ()->debug (sprintf ('Synchronous MDN sent as answer to message "%s". ' ,
195
+ $ messageId ));
196
+ $ responseHeaders = $ mdn ->getHeaders ();
197
+ $ responseBody = $ mdn ->getBody ();
198
+ } else {
199
+ $ this ->getLogger ()->debug (sprintf ('Asynchronous MDN sent as answer to message "%s". ' ,
200
+ $ messageId ));
201
+ $ this ->manager ->sendMdn ($ message );
204
202
}
205
-
206
- $ this ->storage ->saveMessage ($ message );
207
- $ this ->getLogger ()->debug ('AS2 communication successful, message has been saved. ' , [$ messageId ]);
208
203
}
209
204
210
- } catch ( \ Exception $ e ) {
211
-
205
+ $ this -> storage -> saveMessage ( $ message );
206
+ $ this -> getLogger ()-> debug ( ' AS2 communication successful, message has been saved. ' , [ $ messageId ]);
212
207
}
213
208
214
209
} catch (\Exception $ e ) {
0 commit comments