|
16 | 16 | package io.netty.handler.ssl.ocsp; |
17 | 17 |
|
18 | 18 | import io.netty.util.concurrent.Promise; |
| 19 | +import org.bouncycastle.asn1.x500.X500Name; |
| 20 | +import org.bouncycastle.asn1.x509.BasicConstraints; |
| 21 | +import org.bouncycastle.asn1.x509.Extension; |
| 22 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 23 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 24 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder; |
| 25 | +import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; |
19 | 26 | import org.bouncycastle.cert.ocsp.BasicOCSPResp; |
| 27 | +import org.bouncycastle.cert.ocsp.BasicOCSPRespBuilder; |
| 28 | +import org.bouncycastle.cert.ocsp.CertificateID; |
| 29 | +import org.bouncycastle.cert.ocsp.CertificateStatus; |
| 30 | +import org.bouncycastle.cert.ocsp.OCSPException; |
| 31 | +import org.bouncycastle.cert.ocsp.RespID; |
| 32 | +import org.bouncycastle.operator.ContentSigner; |
| 33 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
| 34 | +import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; |
20 | 35 | import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.function.Executable; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.ValueSource; |
21 | 39 |
|
22 | 40 | import javax.net.ssl.HttpsURLConnection; |
23 | 41 | import java.io.IOException; |
| 42 | +import java.math.BigInteger; |
24 | 43 | import java.net.URL; |
| 44 | +import java.security.KeyPair; |
| 45 | +import java.security.KeyPairGenerator; |
| 46 | +import java.security.PrivateKey; |
| 47 | +import java.security.SecureRandom; |
25 | 48 | import java.security.cert.X509Certificate; |
| 49 | +import java.util.Date; |
26 | 50 | import java.util.concurrent.ExecutionException; |
27 | 51 |
|
28 | 52 | import static io.netty.handler.ssl.ocsp.OcspServerCertificateValidator.createDefaultResolver; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
29 | 54 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 55 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
30 | 56 |
|
31 | 57 | class OcspClientTest { |
32 | 58 |
|
33 | | - @Test |
34 | | - void simpleOcspQueryTest() throws IOException, ExecutionException, InterruptedException { |
| 59 | + private static final SecureRandom RANDOM = new SecureRandom(); |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @ValueSource(strings = {"https://apple.com"}) |
| 63 | + void simpleOcspQueryTest(String urlString) throws IOException, ExecutionException, InterruptedException { |
35 | 64 | HttpsURLConnection httpsConnection = null; |
36 | 65 | try { |
37 | | - URL url = new URL("https://netty.io"); |
| 66 | + URL url = new URL(urlString); |
38 | 67 | httpsConnection = (HttpsURLConnection) url.openConnection(); |
39 | 68 | httpsConnection.connect(); |
40 | 69 |
|
@@ -56,4 +85,123 @@ void simpleOcspQueryTest() throws IOException, ExecutionException, InterruptedEx |
56 | 85 | } |
57 | 86 | } |
58 | 87 | } |
| 88 | + |
| 89 | + @Test |
| 90 | + void validateSignatureWithIncludedChainSucceeds() throws Exception { |
| 91 | + final CertAndKey rootIssuer = buildCertificate("CN=SomeRootCA", true, null); |
| 92 | + CertAndKey intermediateIssuer = buildCertificate("CN=SomeIntermediateCA", true, rootIssuer); |
| 93 | + CertAndKey ocspResponder = buildCertificate("CN=SomeOCSPResponder", false, intermediateIssuer); |
| 94 | + |
| 95 | + // Create actual OCSP response with the responder's certificate |
| 96 | + X509CertificateHolder responderHolder = new JcaX509CertificateHolder(ocspResponder.certificate); |
| 97 | + X509CertificateHolder intermediateHolder = new JcaX509CertificateHolder(intermediateIssuer.certificate); |
| 98 | + |
| 99 | + // Create a minimal BasicOCSPResp that contains the certificate chain |
| 100 | + final BasicOCSPResp resp = createBasicOcspResponse( |
| 101 | + ocspResponder, |
| 102 | + new X509CertificateHolder[]{responderHolder, intermediateHolder} |
| 103 | + ); |
| 104 | + |
| 105 | + assertDoesNotThrow(new Executable() { |
| 106 | + @Override |
| 107 | + public void execute() throws Throwable { |
| 108 | + OcspClient.validateSignature(resp, rootIssuer.certificate); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void validateSignatureWithInvalidChainThrows() throws Exception { |
| 115 | + // Build an unrelated responder chain so nothing is signed by the provided issuer (using RSA) |
| 116 | + final CertAndKey issuerBundle = buildCertificate("CN=Issuer", true, null); |
| 117 | + |
| 118 | + // Different CA |
| 119 | + CertAndKey otherRoot = buildCertificate("CN=SomeRootCA", true, null); |
| 120 | + CertAndKey otherIntermediate = buildCertificate("CN=SomeIntermediateCA", true, otherRoot); |
| 121 | + CertAndKey otherResponder = buildCertificate("CN=SomeResponder", false, otherIntermediate); |
| 122 | + |
| 123 | + X509CertificateHolder responderHolder = new JcaX509CertificateHolder(otherResponder.certificate); |
| 124 | + X509CertificateHolder intermediateHolder = new JcaX509CertificateHolder(otherIntermediate.certificate); |
| 125 | + |
| 126 | + // Create actual OCSP response with untrusted chain |
| 127 | + final BasicOCSPResp resp = createBasicOcspResponse( |
| 128 | + otherResponder, |
| 129 | + new X509CertificateHolder[]{responderHolder, intermediateHolder} |
| 130 | + ); |
| 131 | + |
| 132 | + assertThrows(OCSPException.class, new Executable() { |
| 133 | + @Override |
| 134 | + public void execute() throws Throwable { |
| 135 | + OcspClient.validateSignature(resp, issuerBundle.certificate); |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + private static BasicOCSPResp createBasicOcspResponse(CertAndKey responderBundle, |
| 141 | + X509CertificateHolder[] certChain) throws Exception { |
| 142 | + CertAndKey dummyCert = buildCertificate("CN=DummyCert", true, null); |
| 143 | + |
| 144 | + // Create certificate ID for OCSP response |
| 145 | + CertificateID certId = new CertificateID( |
| 146 | + new JcaDigestCalculatorProviderBuilder().build().get(CertificateID.HASH_SHA1), |
| 147 | + new JcaX509CertificateHolder(dummyCert.certificate), |
| 148 | + dummyCert.certificate.getSerialNumber() |
| 149 | + ); |
| 150 | + |
| 151 | + // Create response builder with responder ID based on certificate |
| 152 | + X509CertificateHolder responderHolder = new JcaX509CertificateHolder(responderBundle.certificate); |
| 153 | + RespID respID = new RespID(responderHolder.getSubject()); |
| 154 | + |
| 155 | + BasicOCSPRespBuilder respBuilder = new BasicOCSPRespBuilder(respID); |
| 156 | + |
| 157 | + // Add response for the certificate (status: good) |
| 158 | + respBuilder.addResponse(certId, CertificateStatus.GOOD); |
| 159 | + |
| 160 | + // Build and sign the response with the responder's private key |
| 161 | + ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA") |
| 162 | + .build(responderBundle.keyPair.getPrivate()); |
| 163 | + |
| 164 | + return respBuilder.build(signer, certChain, new Date()); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Build an X.509 certificate with the given subject. If {@code issuer} is {@code null} the certificate is |
| 169 | + * self-signed, otherwise it is issued (signed) by the given issuer. Uses RSA-2048 keys. |
| 170 | + */ |
| 171 | + private static CertAndKey buildCertificate(String subjectDn, boolean isCertificateAuthority, |
| 172 | + CertAndKey issuer) throws Exception { |
| 173 | + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); |
| 174 | + keyPairGenerator.initialize(2048); |
| 175 | + KeyPair keyPair = keyPairGenerator.generateKeyPair(); |
| 176 | + |
| 177 | + X500Name subject = new X500Name(subjectDn); |
| 178 | + boolean selfSigned = issuer == null; |
| 179 | + X500Name issuerName = selfSigned |
| 180 | + ? subject |
| 181 | + : new JcaX509CertificateHolder(issuer.certificate).getSubject(); |
| 182 | + PrivateKey signingKey = selfSigned ? keyPair.getPrivate() : issuer.keyPair.getPrivate(); |
| 183 | + |
| 184 | + Date notBefore = new Date(System.currentTimeMillis() - 86400000L); |
| 185 | + Date notAfter = new Date(System.currentTimeMillis() + 365L * 86400000L); |
| 186 | + |
| 187 | + JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder( |
| 188 | + issuerName, new BigInteger(64, RANDOM), notBefore, notAfter, subject, keyPair.getPublic()); |
| 189 | + builder.addExtension(Extension.basicConstraints, true, new BasicConstraints(isCertificateAuthority)); |
| 190 | + |
| 191 | + ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").build(signingKey); |
| 192 | + X509CertificateHolder holder = builder.build(signer); |
| 193 | + X509Certificate certificate = new JcaX509CertificateConverter().getCertificate(holder); |
| 194 | + |
| 195 | + return new CertAndKey(certificate, keyPair); |
| 196 | + } |
| 197 | + |
| 198 | + private static final class CertAndKey { |
| 199 | + final X509Certificate certificate; |
| 200 | + final KeyPair keyPair; |
| 201 | + |
| 202 | + CertAndKey(X509Certificate certificate, KeyPair keyPair) { |
| 203 | + this.certificate = certificate; |
| 204 | + this.keyPair = keyPair; |
| 205 | + } |
| 206 | + } |
59 | 207 | } |