|
15 | 15 | */ |
16 | 16 | package io.netty.handler.ssl.ocsp; |
17 | 17 |
|
| 18 | +import io.netty.pkitesting.CertificateBuilder; |
| 19 | +import io.netty.pkitesting.X509Bundle; |
18 | 20 | import io.netty.util.concurrent.Promise; |
| 21 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 22 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder; |
19 | 23 | 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; |
20 | 32 | import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.ValueSource; |
21 | 35 |
|
22 | 36 | import javax.net.ssl.HttpsURLConnection; |
23 | 37 | import java.io.IOException; |
24 | 38 | import java.net.URL; |
25 | 39 | import java.security.cert.X509Certificate; |
| 40 | +import java.util.Date; |
26 | 41 | import java.util.concurrent.ExecutionException; |
27 | 42 |
|
28 | 43 | import static io.netty.handler.ssl.ocsp.OcspServerCertificateValidator.createDefaultResolver; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
29 | 45 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 46 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
30 | 47 |
|
31 | 48 | class OcspClientTest extends AbstractOcspTest { |
32 | 49 |
|
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 { |
35 | 53 | HttpsURLConnection httpsConnection = null; |
36 | 54 | try { |
37 | | - URL url = new URL("https://netty.io"); |
| 55 | + URL url = new URL(urlString); |
38 | 56 | httpsConnection = (HttpsURLConnection) url.openConnection(); |
39 | 57 | httpsConnection.connect(); |
40 | 58 |
|
@@ -55,4 +73,108 @@ void simpleOcspQueryTest() throws IOException, ExecutionException, InterruptedEx |
55 | 73 | } |
56 | 74 | } |
57 | 75 | } |
| 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 | + } |
58 | 180 | } |