GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix OCSP signature validation when responder is not the issuer (#1607… · netty/netty@41c4825

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler-ssl-ocsp/src
    • main/java/io/netty/handler/ssl/ocsp
      • OcspClient.java
    • test/java/io/netty/handler/ssl/ocsp
      • OcspClientTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler-ssl-ocsp/src/main/java/io/netty/handler/ssl/ocsp/OcspClient.java‎

Copy file name to clipboardExpand all lines: handler-ssl-ocsp/src/main/java/io/netty/handler/ssl/ocsp/OcspClient.java
+87-4Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.bouncycastle.asn1.x509.Extension;
4343
import org.bouncycastle.asn1.x509.Extensions;
4444
import org.bouncycastle.cert.X509CertificateHolder;
45+
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
4546
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
4647
import org.bouncycastle.cert.ocsp.BasicOCSPResp;
4748
import org.bouncycastle.cert.ocsp.CertificateID;
@@ -55,9 +56,22 @@
5556

5657
import java.net.InetAddress;
5758
import java.net.URL;
59+
import java.security.InvalidAlgorithmParameterException;
60+
import java.security.NoSuchAlgorithmException;
5861
import java.security.SecureRandom;
62+
import java.security.cert.CertPathBuilder;
63+
import java.security.cert.CertPathBuilderException;
64+
import java.security.cert.CertStore;
5965
import java.security.cert.CertificateEncodingException;
66+
import java.security.cert.CertificateException;
67+
import java.security.cert.CollectionCertStoreParameters;
68+
import java.security.cert.PKIXBuilderParameters;
69+
import java.security.cert.TrustAnchor;
70+
import java.security.cert.X509CertSelector;
6071
import java.security.cert.X509Certificate;
72+
import java.util.ArrayList;
73+
import java.util.Collections;
74+
import java.util.List;
6175

6276
import static io.netty.handler.codec.http.HttpMethod.POST;
6377
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
@@ -264,14 +278,83 @@ private static void validateNonce(BasicOCSPResp basicResponse, DEROctetString en
264278
/**
265279
* Validate OCSP response signature
266280
*/
267-
private static void validateSignature(BasicOCSPResp resp, X509Certificate certificate) throws OCSPException {
281+
static void validateSignature(BasicOCSPResp resp, X509Certificate issuerCertificate) throws OCSPException {
268282
try {
269-
ContentVerifierProvider verifier = new JcaContentVerifierProviderBuilder().build(certificate);
270-
if (!resp.isSignatureValid(verifier)) {
271-
throw new OCSPException("OCSP signature is not valid");
283+
X509CertificateHolder[] certs = resp.getCerts();
284+
JcaContentVerifierProviderBuilder providerBuilder = new JcaContentVerifierProviderBuilder();
285+
286+
// If responder certificate is included, validate the chain
287+
if (certs != null && certs.length > 0) {
288+
289+
// Use the first included certificate to verify the OCSP response signature.
290+
X509CertificateHolder responderCert = certs[0];
291+
292+
// Verify OCSP response signature using responder cert
293+
ContentVerifierProvider responderVerifier = providerBuilder.build(responderCert);
294+
295+
if (!resp.isSignatureValid(responderVerifier)) {
296+
throw new OCSPException("OCSP response signature is not valid");
297+
}
298+
299+
// Build chain from responder certificate to issuer using CertPathBuilder
300+
validateCertificateChain(responderCert, certs, issuerCertificate);
301+
} else {
302+
// Validate signature using issuer certificate
303+
ContentVerifierProvider issuerVerifier = providerBuilder.build(issuerCertificate);
304+
305+
if (!resp.isSignatureValid(issuerVerifier)) {
306+
throw new OCSPException("OCSP response signature is not valid");
307+
}
272308
}
273309
} catch (OperatorCreationException e) {
274310
throw new OCSPException("Error validating OCSP-Signature", e);
311+
} catch (CertificateException e) {
312+
throw new OCSPException("Error while processing certificates for OCSP signature validation", e);
313+
}
314+
}
315+
316+
/**
317+
* Validates that a certificate chain can be built from the responder certificate to the issuer.
318+
* Uses Java's CertPathBuilder to construct and validate the chain.
319+
*/
320+
private static void validateCertificateChain(X509CertificateHolder responderCert,
321+
X509CertificateHolder[] allCerts,
322+
X509Certificate issuerCertificate) throws OCSPException {
323+
try {
324+
// Convert BouncyCastle certificate holders to Java X509Certificates
325+
List<X509Certificate> certList = new ArrayList<>(allCerts.length);
326+
for (X509CertificateHolder certHolder : allCerts) {
327+
certList.add(new JcaX509CertificateConverter().getCertificate(certHolder));
328+
}
329+
330+
// Create a CertStore with all the certificates from the OCSP response
331+
CertStore certStore = CertStore.getInstance("Collection",
332+
new CollectionCertStoreParameters(certList));
333+
334+
// Set up the target certificate selector for the responder certificate
335+
X509CertSelector targetConstraints = new X509CertSelector();
336+
targetConstraints.setCertificate(new JcaX509CertificateConverter().getCertificate(responderCert));
337+
338+
// Set up trust anchor with the issuer certificate
339+
TrustAnchor trustAnchor = new TrustAnchor(issuerCertificate, null);
340+
341+
// Build PKIX parameters
342+
PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(
343+
Collections.singleton(trustAnchor), targetConstraints);
344+
pkixParams.addCertStore(certStore);
345+
pkixParams.setRevocationEnabled(false); // Don't check revocation when validating OCSP response
346+
347+
// Build and validate the certificate path
348+
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
349+
builder.build(pkixParams);
350+
351+
// If we reach here, the chain is valid
352+
} catch (CertPathBuilderException e) {
353+
throw new OCSPException("OCSP responder certificate is not trusted by issuer: " + e.getMessage(), e);
354+
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException e) {
355+
throw new OCSPException("Error setting up certificate path validation", e);
356+
} catch (CertificateException e) {
357+
throw new OCSPException("Error converting certificates for path validation", e);
275358
}
276359
}
277360

Collapse file

‎handler-ssl-ocsp/src/test/java/io/netty/handler/ssl/ocsp/OcspClientTest.java‎

Copy file name to clipboardExpand all lines: handler-ssl-ocsp/src/test/java/io/netty/handler/ssl/ocsp/OcspClientTest.java
+125-3Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,44 @@
1515
*/
1616
package io.netty.handler.ssl.ocsp;
1717

18+
import io.netty.pkitesting.CertificateBuilder;
19+
import io.netty.pkitesting.X509Bundle;
1820
import io.netty.util.concurrent.Promise;
21+
import org.bouncycastle.cert.X509CertificateHolder;
22+
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
1923
import org.bouncycastle.cert.ocsp.BasicOCSPResp;
24+
import org.bouncycastle.cert.ocsp.BasicOCSPRespBuilder;
25+
import org.bouncycastle.cert.ocsp.CertificateID;
26+
import org.bouncycastle.cert.ocsp.CertificateStatus;
27+
import org.bouncycastle.cert.ocsp.OCSPException;
28+
import org.bouncycastle.cert.ocsp.RespID;
29+
import org.bouncycastle.operator.ContentSigner;
30+
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
31+
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
2032
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.ValueSource;
2135

2236
import javax.net.ssl.HttpsURLConnection;
2337
import java.io.IOException;
2438
import java.net.URL;
2539
import java.security.cert.X509Certificate;
40+
import java.util.Date;
2641
import java.util.concurrent.ExecutionException;
2742

2843
import static io.netty.handler.ssl.ocsp.OcspServerCertificateValidator.createDefaultResolver;
44+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2945
import static org.junit.jupiter.api.Assertions.assertNull;
46+
import static org.junit.jupiter.api.Assertions.assertThrows;
3047

3148
class OcspClientTest extends AbstractOcspTest {
3249

33-
@Test
34-
void simpleOcspQueryTest() throws IOException, ExecutionException, InterruptedException {
50+
@ParameterizedTest
51+
@ValueSource(strings = {"https://netty.io", "https://apple.com"})
52+
void simpleOcspQueryTest(String urlString) throws IOException, ExecutionException, InterruptedException {
3553
HttpsURLConnection httpsConnection = null;
3654
try {
37-
URL url = new URL("https://netty.io");
55+
URL url = new URL(urlString);
3856
httpsConnection = (HttpsURLConnection) url.openConnection();
3957
httpsConnection.connect();
4058

@@ -55,4 +73,108 @@ void simpleOcspQueryTest() throws IOException, ExecutionException, InterruptedEx
5573
}
5674
}
5775
}
76+
77+
@Test
78+
void validateSignatureWithIncludedChainSucceeds() throws Exception {
79+
X509Bundle rootIssuer = new CertificateBuilder()
80+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
81+
.subject("CN=SomeRootCA")
82+
.setIsCertificateAuthority(true)
83+
.buildSelfSigned();
84+
85+
X509Bundle intermediateIssuer = new CertificateBuilder()
86+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
87+
.subject("CN=SomeIntermediateCA")
88+
.setIsCertificateAuthority(true)
89+
.buildIssuedBy(rootIssuer);
90+
91+
X509Bundle ocspResponder = new CertificateBuilder()
92+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
93+
.subject("CN=SomeOCSPResponder")
94+
.buildIssuedBy(intermediateIssuer);
95+
96+
// Create actual OCSP response with the responder's certificate
97+
X509CertificateHolder responderHolder = new JcaX509CertificateHolder(ocspResponder.getCertificate());
98+
X509CertificateHolder intermediateHolder = new JcaX509CertificateHolder(intermediateIssuer.getCertificate());
99+
100+
// Create a minimal BasicOCSPResp that contains the certificate chain
101+
BasicOCSPResp resp = createBasicOcspResponse(
102+
ocspResponder,
103+
new X509CertificateHolder[]{responderHolder, intermediateHolder}
104+
);
105+
106+
assertDoesNotThrow(() -> OcspClient.validateSignature(resp, rootIssuer.getCertificate()));
107+
}
108+
109+
@Test
110+
void validateSignatureWithInvalidChainThrows() throws Exception {
111+
// Build an unrelated responder chain so nothing is signed by the provided issuer (using RSA)
112+
X509Bundle issuerBundle = new CertificateBuilder()
113+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
114+
.subject("CN=Issuer")
115+
.setIsCertificateAuthority(true)
116+
.buildSelfSigned();
117+
118+
// Different CA
119+
X509Bundle otherRoot = new CertificateBuilder()
120+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
121+
.subject("CN=SomeRootCA")
122+
.setIsCertificateAuthority(true)
123+
.buildSelfSigned();
124+
125+
X509Bundle otherIntermediate = new CertificateBuilder()
126+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
127+
.subject("CN=SomeIntermediateCA")
128+
.setIsCertificateAuthority(true)
129+
.buildIssuedBy(otherRoot);
130+
131+
X509Bundle otherResponder = new CertificateBuilder()
132+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
133+
.subject("CN=SomeResponder")
134+
.buildIssuedBy(otherIntermediate);
135+
136+
X509CertificateHolder responderHolder = new JcaX509CertificateHolder(otherResponder.getCertificate());
137+
X509CertificateHolder intermediateHolder = new JcaX509CertificateHolder(otherIntermediate.getCertificate());
138+
139+
// Create actual OCSP response with untrusted chain
140+
BasicOCSPResp resp = createBasicOcspResponse(
141+
otherResponder,
142+
new X509CertificateHolder[]{responderHolder, intermediateHolder}
143+
);
144+
145+
assertThrows(OCSPException.class, () ->
146+
OcspClient.validateSignature(resp, issuerBundle.getCertificate())
147+
);
148+
}
149+
150+
private static BasicOCSPResp createBasicOcspResponse(X509Bundle responderBundle,
151+
X509CertificateHolder[] certChain) throws Exception {
152+
X509Bundle dummyCert = new CertificateBuilder()
153+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
154+
.subject("CN=DummyCert")
155+
.setIsCertificateAuthority(true)
156+
.buildSelfSigned();
157+
158+
// Create certificate ID for OCSP response
159+
CertificateID certId = new CertificateID(
160+
new JcaDigestCalculatorProviderBuilder().build().get(CertificateID.HASH_SHA1),
161+
new JcaX509CertificateHolder(dummyCert.getCertificate()),
162+
dummyCert.getCertificate().getSerialNumber()
163+
);
164+
165+
// Create response builder with responder ID based on certificate
166+
X509CertificateHolder responderHolder = new JcaX509CertificateHolder(responderBundle.getCertificate());
167+
RespID respID = new RespID(responderHolder.getSubject());
168+
169+
BasicOCSPRespBuilder respBuilder = new BasicOCSPRespBuilder(respID);
170+
171+
// Add response for the certificate (status: good)
172+
respBuilder.addResponse(certId, CertificateStatus.GOOD);
173+
174+
// Build and sign the response with the responder's private key
175+
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA")
176+
.build(responderBundle.getKeyPair().getPrivate());
177+
178+
return respBuilder.build(signer, certChain, new Date());
179+
}
58180
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.