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

Respond with TLS alert "bad_certificate" if certificate parsing fails #1710

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions tls/src/main/java/org/bouncycastle/tls/TlsServerProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ protected void receive13ClientCertificate(ByteArrayInputStream buf)
.setCertificateType(tlsServerContext.getSecurityParametersHandshake().getClientCertificateType())
.setMaxChainLength(tlsServer.getMaxCertificateChainLength());

Certificate clientCertificate = Certificate.parse(options, tlsServerContext, buf, null);
Certificate clientCertificate = TlsUtils.parseCertificate(options, tlsServerContext, buf, null);

assertEmpty(buf);

Expand Down Expand Up @@ -1499,7 +1499,7 @@ protected void receiveCertificateMessage(ByteArrayInputStream buf)
.setCertificateType(tlsServerContext.getSecurityParametersHandshake().getClientCertificateType())
.setMaxChainLength(tlsServer.getMaxCertificateChainLength());

Certificate clientCertificate = Certificate.parse(options, tlsServerContext, buf, null);
Certificate clientCertificate = TlsUtils.parseCertificate(options, tlsServerContext, buf, null);

assertEmpty(buf);

Expand Down
21 changes: 19 additions & 2 deletions tls/src/main/java/org/bouncycastle/tls/TlsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bouncycastle.asn1.rosstandart.RosstandartObjectIdentifiers;
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.tls.Certificate.ParseOptions;
import org.bouncycastle.tls.crypto.Tls13Verifier;
import org.bouncycastle.tls.crypto.TlsAgreement;
import org.bouncycastle.tls.crypto.TlsCertificate;
Expand Down Expand Up @@ -5172,6 +5173,22 @@ private static boolean isSafeRenegotiationServerCertificate(TlsClientContext cli
return false;
}

static Certificate parseCertificate(ParseOptions options, TlsContext context, ByteArrayInputStream buf,
ByteArrayOutputStream endPointHashOutput) throws TlsFatalAlert {
try
{
return Certificate.parse(options, context, buf, endPointHashOutput);
}
catch (TlsFatalAlert e)
{
throw e;
}
catch (IOException e)
{
throw new TlsFatalAlert(AlertDescription.bad_certificate, e);
}
}

static TlsAuthentication receiveServerCertificate(TlsClientContext clientContext, TlsClient client,
ByteArrayInputStream buf, Hashtable serverExtensions) throws IOException
{
Expand All @@ -5188,7 +5205,7 @@ static TlsAuthentication receiveServerCertificate(TlsClientContext clientContext
.setCertificateType(securityParameters.getServerCertificateType())
.setMaxChainLength(client.getMaxCertificateChainLength());

Certificate serverCertificate = Certificate.parse(options, clientContext, buf, endPointHash);
Certificate serverCertificate = parseCertificate(options, clientContext, buf, endPointHash);

TlsProtocol.assertEmpty(buf);

Expand Down Expand Up @@ -5229,7 +5246,7 @@ static TlsAuthentication receive13ServerCertificate(TlsClientContext clientConte
.setCertificateType(securityParameters.getServerCertificateType())
.setMaxChainLength(client.getMaxCertificateChainLength());

Certificate serverCertificate = Certificate.parse(options, clientContext, buf, null);
Certificate serverCertificate = parseCertificate(options, clientContext, buf, null);

TlsProtocol.assertEmpty(buf);

Expand Down