Skip to content

Commit 2633526

Browse files
committed
v1.3.1
1 parent 52b10e4 commit 2633526

File tree

4 files changed

+91
-92
lines changed

4 files changed

+91
-92
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tiamo/phpas2",
33
"description": "PHPAS2 is a php-based implementation of the EDIINT AS2 standard",
4-
"version": "1.3",
4+
"version": "1.3.1",
55
"authors": [
66
{
77
"name": "Vladyslav K",

src/CryptoHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function calculateMIC($payload, $algo = 'sha256', $includeHeaders
2626
if (! ($payload instanceof MimePart)) {
2727
$payload = MimePart::fromString($payload);
2828
}
29-
// $digest = base64_encode(openssl_digest($payload, $digestAlgorithm, true));
29+
// $digest = base64_encode(openssl_digest($payload, $digestAlgorithm, true));
3030
$digest = base64_encode(hash(
3131
$digestAlgorithm,
3232
$includeHeaders ? $payload : $payload->getBody(),
@@ -109,8 +109,9 @@ public static function verify($data, $caInfo = [])
109109
if ($data instanceof MimePart) {
110110
$data = self::getTempFilename((string) $data);
111111
}
112-
113-
return openssl_pkcs7_verify($data, PKCS7_BINARY | PKCS7_NOSIGS | PKCS7_NOVERIFY, null, $caInfo);
112+
// TODO: implement
113+
// return openssl_pkcs7_verify($data, PKCS7_BINARY | PKCS7_NOSIGS | PKCS7_NOVERIFY, null, $caInfo);
114+
return openssl_pkcs7_verify($data, PKCS7_BINARY | PKCS7_NOSIGS | PKCS7_NOVERIFY);
114115
}
115116

116117
/**

src/Management.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ public function buildMdn(MessageInterface $message, $confirmationText = null, $e
393393

394394
// Parse Message Headers
395395
$messageHeaders = MimePart::fromString($message->getHeaders());
396-
$isSignedRequested = $messageHeaders->hasHeader('disposition-notification-options');
396+
$notificationOptions = $messageHeaders->hasHeader('disposition-notification-options');
397+
398+
// TODO: parse (signed-receipt-protocol, signed-receipt-micalg)
399+
// $notificationOptions = Utils::parseHeader($notificationOptions);
397400

398401
$headers = [
399402
'Message-ID' => '<' . Utils::generateMessageID($receiver) . '>',
@@ -406,7 +409,7 @@ public function buildMdn(MessageInterface $message, $confirmationText = null, $e
406409
'Connection' => 'close',
407410
];
408411

409-
if (! $isSignedRequested) {
412+
if (! $notificationOptions) {
410413
$reportHeaders['Mime-Version'] = '1.0';
411414
$reportHeaders += $headers;
412415
}
@@ -439,7 +442,7 @@ public function buildMdn(MessageInterface $message, $confirmationText = null, $e
439442
], Utils::normalizeHeaders($mdnData)));
440443

441444
// If signed MDN is requested by partner then sign the MDN and attach to report
442-
if ($isSignedRequested) {
445+
if ($notificationOptions) {
443446
$this->getLogger()->debug('Outbound MDN has been signed.');
444447
$x509 = openssl_x509_read($receiver->getCertificate());
445448
$key = openssl_get_privatekey($receiver->getPrivateKey(), $receiver->getPrivateKeyPassPhrase());

src/Server.php

Lines changed: 80 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -114,101 +114,96 @@ public function execute(ServerRequestInterface $request = null)
114114

115115
$micalg = $payload->getParsedHeader('Disposition-Notification-Options', 2, 0);
116116

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-
}
126117

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+
}
144125

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+
// }
158130

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');
164139
}
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));
165143

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;
175150
}
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), '<>');
180173
}
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);
204202
}
205-
206-
$this->storage->saveMessage($message);
207-
$this->getLogger()->debug('AS2 communication successful, message has been saved.', [$messageId]);
208203
}
209204

210-
} catch (\Exception $e) {
211-
205+
$this->storage->saveMessage($message);
206+
$this->getLogger()->debug('AS2 communication successful, message has been saved.', [$messageId]);
212207
}
213208

214209
} catch (\Exception $e) {

0 commit comments

Comments
 (0)