Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check incoming BDEW requests wether an EMT/MAKO certificate is used #235

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
*/
public class BDEWCompatibilityValidator implements IAS4ProfileValidator
{

public static final String EMT_MAK = "EMT.MAK";

public BDEWCompatibilityValidator ()
{}

Expand Down Expand Up @@ -430,6 +433,38 @@ public void validateInitiatorIdentity (@Nonnull final Ebms3UserMessage aUserMsg,
@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata,
@Nonnull final ErrorList aErrorList)
{

X509Certificate aTlsClientEndCert = null;
if (aMessageMetadata.hasRemoteTlsCerts ())
{
aTlsClientEndCert = aMessageMetadata.remoteTlsCerts ().getFirstOrNull ();

final X500Name aTlsName = new X500Name (aTlsClientEndCert.getSubjectX500Principal ().getName ());
final RDN aTlsCnRDN = aTlsName.getRDNs (BCStyle.CN)[0];
final String cn = IETFUtils.valueToString (aTlsCnRDN.getFirst ().getValue ());

if (!cn.contains (EMT_MAK))
{
aErrorList.add (_createError ("TLS certificate '" +
aTlsClientEndCert.getSubjectX500Principal()
+ "' is not an EMT/MAKO certificate"));
}
}

if (aSignatureCert != null)
{
final X500Name aTlsName = new X500Name (aSignatureCert.getSubjectX500Principal ().getName ());
final RDN aSigCnRDN = aTlsName.getRDNs (BCStyle.CN)[0];
final String cn = IETFUtils.valueToString (aSigCnRDN.getFirst ().getValue ());

if (!cn.contains (EMT_MAK))
{
aErrorList.add (_createError ("Signature certificate '" +
aSignatureCert.getSubjectX500Principal()
+ "' is not an EMT/MAKO certificate"));
}
}

final Ebms3PartyInfo aInitatorPartyInfo = aUserMsg.getPartyInfo ();
if (aInitatorPartyInfo != null)
{
Expand Down Expand Up @@ -458,9 +493,8 @@ public void validateInitiatorIdentity (@Nonnull final Ebms3UserMessage aUserMsg,
}
}

if (aMessageMetadata.hasRemoteTlsCerts ())
if (aTlsClientEndCert != null)
{
final X509Certificate aTlsClientEndCert = aMessageMetadata.remoteTlsCerts ().getFirstOrNull ();

final X500Name aTlsName = new X500Name (aTlsClientEndCert.getSubjectX500Principal ().getName ());
final RDN aTlsOuRDN = aTlsName.getRDNs (BCStyle.OU)[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.helger.phase4.ebms3header.Ebms3SignalMessage;
import com.helger.phase4.ebms3header.Ebms3To;
import com.helger.phase4.ebms3header.Ebms3UserMessage;
import com.helger.phase4.messaging.EAS4MessageMode;
import com.helger.phase4.messaging.IAS4IncomingMessageMetadata;
import com.helger.phase4.messaging.domain.MessageHelperMethods;
import com.helger.phase4.model.EMEP;
import com.helger.phase4.model.EMEPBinding;
Expand All @@ -43,14 +45,22 @@
import com.helger.phase4.model.pmode.leg.PModeLegErrorHandling;
import com.helger.phase4.model.pmode.leg.PModeLegProtocol;
import com.helger.phase4.model.pmode.leg.PModeLegSecurity;
import com.helger.phase4.servlet.AS4IncomingMessageMetadata;
import com.helger.phase4.soap.ESoapVersion;
import com.helger.phase4.wss.EWSSVersion;
import com.helger.photon.app.mock.PhotonAppWebTestRule;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;

import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Locale;
import java.util.UUID;

Expand Down Expand Up @@ -86,6 +96,7 @@ public void before ()
"http://localhost:8080",
IPModeIDProvider.DEFAULT_DYNAMIC,
true);
Security.addProvider(new BouncyCastleProvider());
}

@Test
Expand Down Expand Up @@ -854,4 +865,28 @@ public void testValidateSignalMessageNoMessageID ()
assertTrue (m_aErrorList.containsAny (x -> x.getErrorText (LOCALE).contains ("MessageInfo/MessageId is missing")));
}

@SuppressWarnings("unchecked")
@Test
public void testValidateInitiatorIdentityNonEmtMakoTls () throws CertificateException, NoSuchProviderException
{
final Ebms3UserMessage aUserMessage = new Ebms3UserMessage ();
final AS4IncomingMessageMetadata incomingMessageMetadata = AS4IncomingMessageMetadata.createForRequest();
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509", "BC");
final Collection<X509Certificate> certificates = (Collection<X509Certificate>) certificateFactory.generateCertificates(BDEWCompatibilityValidator.class.getResourceAsStream("nonemtmako.cert"));
incomingMessageMetadata.setRemoteTlsCerts(certificates.toArray(new X509Certificate[0]));
VALIDATOR.validateInitiatorIdentity (aUserMessage, null, incomingMessageMetadata, m_aErrorList);
assertTrue (m_aErrorList.containsAny (x -> x.getErrorText (LOCALE).contains ("is not an EMT/MAKO certificate")));
}

@Test
public void testValidateInitiatorIdentityNonEmtMakoSig () throws CertificateException, NoSuchProviderException
{
final Ebms3UserMessage aUserMessage = new Ebms3UserMessage ();
final AS4IncomingMessageMetadata incomingMessageMetadata = AS4IncomingMessageMetadata.createForRequest();
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509", "BC");
final X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(BDEWCompatibilityValidator.class.getResourceAsStream("nonemtmako.cert"));
VALIDATOR.validateInitiatorIdentity (aUserMessage, certificate, incomingMessageMetadata, m_aErrorList);
assertTrue (m_aErrorList.containsAny (x -> x.getErrorText (LOCALE).contains ("is not an EMT/MAKO certificate")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-----BEGIN CERTIFICATE-----
MIIB2DCCAX+gAwIBAgIUeO7Nd7BLDfycbhdx/+sBjsKdVBIwCgYIKoZIzj0EAwIw
azEKMAgGA1UEBRMBMTELMAkGA1UEBhMCREUxDjAMBgNVBAgMBUR1bW15MQ4wDAYD
VQQHDAVEdW1teTEWMBQGA1UECwwNOTk5OTk5OTk5OTk5OTEYMBYGA1UEAwwPZHVt
bXkuRU1ULmR1bW15MCAXDTI0MDQwOTExMjc1NFoYDzIxMjQwNDA5MTEyNzU0WjBr
MQowCAYDVQQFEwExMQswCQYDVQQGEwJERTEOMAwGA1UECAwFRHVtbXkxDjAMBgNV
BAcMBUR1bW15MRYwFAYDVQQLDA05OTk5OTk5OTk5OTk5MRgwFgYDVQQDDA9kdW1t
eS5FTVQuZHVtbXkwWjAUBgcqhkjOPQIBBgkrJAMDAggBAQcDQgAEKuLVYNOqwaap
gNoYgT4RcADtU+LT8/0IFikiPhSBiauV7WDSu11fep+8P4zeMGMRNfPSvnsIhyDF
Nat0oVWznDAKBggqhkjOPQQDAgNHADBEAiAcX3Eal5L/WoN9kkYOKO33z2QbR35I
uEJz8yleJrLXDAIgX5N1KWU+0egFfu+UeioKOVGCx2rJv0OUJGgVrn6sIqU=
-----END CERTIFICATE-----