1010import eu .webeid .ocsp .protocol .OcspResponseValidator ;
1111import eu .webeid .security .exceptions .AuthTokenException ;
1212import eu .webeid .ocsp .exceptions .UserCertificateOCSPCheckFailedException ;
13+ import eu .webeid .ocsp .exceptions .UserCertificateOCSPException ;
1314import eu .webeid .security .util .DateAndTime ;
1415import eu .webeid .ocsp .service .OcspServiceProvider ;
1516import eu .webeid .ocsp .service .OcspService ;
@@ -50,13 +51,13 @@ public class OcspCertificateRevocationChecker implements CertificateRevocationCh
5051
5152 public static final Duration DEFAULT_TIME_SKEW = Duration .ofMinutes (15 );
5253 public static final Duration DEFAULT_THIS_UPDATE_AGE = Duration .ofMinutes (2 );
54+ public static final Duration DEFAULT_NEXT_UPDATE_AGE = Duration .ofMinutes (15 );
5355
5456 private static final Logger LOG = LoggerFactory .getLogger (OcspCertificateRevocationChecker .class );
5557
5658 private final OcspClient ocspClient ;
5759 private final OcspServiceProvider ocspServiceProvider ;
5860 private final Duration allowedOcspResponseTimeSkew ;
59- private final Duration maxOcspResponseThisUpdateAge ;
6061
6162 static {
6263 if (Security .getProvider (BouncyCastleProvider .PROVIDER_NAME ) == null ) {
@@ -66,12 +67,10 @@ public class OcspCertificateRevocationChecker implements CertificateRevocationCh
6667
6768 public OcspCertificateRevocationChecker (OcspClient ocspClient ,
6869 OcspServiceProvider ocspServiceProvider ,
69- Duration allowedOcspResponseTimeSkew ,
70- Duration maxOcspResponseThisUpdateAge ) {
70+ Duration allowedOcspResponseTimeSkew ) {
7171 this .ocspClient = requireNonNull (ocspClient , "ocspClient" );
7272 this .ocspServiceProvider = requireNonNull (ocspServiceProvider , "ocspServiceProvider" );
7373 this .allowedOcspResponseTimeSkew = requirePositiveDuration (allowedOcspResponseTimeSkew , "allowedOcspResponseTimeSkew" );
74- this .maxOcspResponseThisUpdateAge = requirePositiveDuration (maxOcspResponseThisUpdateAge , "maxOcspResponseThisUpdateAge" );
7574 }
7675
7776 /**
@@ -86,22 +85,16 @@ public List<RevocationInfo> validateCertificateNotRevoked(X509Certificate subjec
8685 requireNonNull (subjectCertificate , "subjectCertificate" );
8786 requireNonNull (issuerCertificate , "issuerCertificate" );
8887
89- URI ocspResponderUri = null ;
90- try {
91- OcspService ocspService = ocspServiceProvider .getService (subjectCertificate , issuerCertificate );
92- ocspResponderUri = requireNonNull (ocspService .getAccessLocation (), "ocspResponderUri" );
93-
94- final CertificateID certificateId = getCertificateId (subjectCertificate , issuerCertificate );
95-
96- final OCSPReq request = new OcspRequestBuilder ()
97- .withCertificateId (certificateId )
98- .enableOcspNonce (ocspService .doesSupportNonce ())
99- .build ();
88+ final OcspService ocspService = ocspServiceProvider .getService (subjectCertificate , issuerCertificate );
89+ final CertificateID certificateId = getCertificateId (subjectCertificate , issuerCertificate );
90+ final URI ocspResponderUri = ocspService .getAccessLocation ();
91+ final OCSPReq request = getOcspRequest (certificateId , ocspService );
10092
101- if (!ocspService .doesSupportNonce ()) {
102- LOG .debug ("Disabling OCSP nonce extension" );
103- }
93+ if (!ocspService .doesSupportNonce ()) {
94+ LOG .debug ("Disabling OCSP nonce extension" );
95+ }
10496
97+ try {
10598 LOG .debug ("Sending OCSP request" );
10699 final OCSPResp response = requireNonNull (ocspClient .request (ocspResponderUri , request ), "OCSPResp" );
107100 if (response .getStatus () != OCSPResponseStatus .SUCCESSFUL ) {
@@ -113,20 +106,35 @@ public List<RevocationInfo> validateCertificateNotRevoked(X509Certificate subjec
113106 }
114107 LOG .debug ("OCSP response received successfully" );
115108
116- verifyOcspResponse (basicResponse , ocspService , certificateId , issuerCertificate , maxOcspResponseThisUpdateAge );
109+ // Check the nonce first to fail fast on replayed or mismatched responses. The nonce is only
110+ // trustworthy after signature verification below, which still runs; both checks must pass.
117111 if (ocspService .doesSupportNonce ()) {
118112 checkNonce (request , basicResponse , ocspResponderUri );
119113 }
114+ verifyOcspResponse (basicResponse , ocspService , certificateId , issuerCertificate );
120115 LOG .debug ("OCSP response verified successfully" );
121116
122117 return List .of (new RevocationInfo (ocspResponderUri , Map .of (RevocationInfo .KEY_OCSP_RESPONSE , response )));
123118
124- } catch (OCSPException | CertificateException | OperatorCreationException | IOException | OCSPClientException e ) {
119+ } catch (OCSPException | CertificateException | OperatorCreationException | OCSPClientException e ) {
125120 throw new UserCertificateOCSPCheckFailedException (e , ocspResponderUri );
126121 }
127122 }
128123
129- protected void verifyOcspResponse (BasicOCSPResp basicResponse , OcspService ocspService , CertificateID requestCertificateId , X509Certificate issuerCertificate , Duration maxOcspResponseThisUpdateAge ) throws AuthTokenException , OCSPException , CertificateException , OperatorCreationException {
124+ protected static OCSPReq getOcspRequest (CertificateID certificateId , OcspService ocspService ) throws UserCertificateOCSPException {
125+ final OCSPReq request ;
126+ try {
127+ request = new OcspRequestBuilder ()
128+ .withCertificateId (certificateId )
129+ .enableOcspNonce (ocspService .doesSupportNonce ())
130+ .build ();
131+ } catch (OCSPException e ) {
132+ throw new UserCertificateOCSPException ("Unable to create OCSP request" , e );
133+ }
134+ return request ;
135+ }
136+
137+ protected void verifyOcspResponse (BasicOCSPResp basicResponse , OcspService ocspService , CertificateID requestCertificateId , X509Certificate issuerCertificate ) throws AuthTokenException , OCSPException , CertificateException , OperatorCreationException {
130138 // The verification algorithm follows RFC 2560, https://www.ietf.org/rfc/rfc2560.txt.
131139 //
132140 // 3.2. Signed Response Acceptance Requirements
@@ -142,12 +150,18 @@ protected void verifyOcspResponse(BasicOCSPResp basicResponse, OcspService ocspS
142150 + "received " + basicResponse .getResponses ().length + " responses instead" , ocspService .getAccessLocation ());
143151 }
144152 final SingleResp certStatusResponse = basicResponse .getResponses ()[0 ];
145- if (!requestCertificateId .equals (certStatusResponse .getCertID ())) {
146- throw new UserCertificateOCSPCheckFailedException ("OCSP responded with certificate ID that differs from the requested ID" ,
147- ocspService .getAccessLocation ());
148- }
153+ OcspResponseValidator .validateCertificateId (requestCertificateId , certStatusResponse .getCertID (), ocspService .getAccessLocation ());
149154
150- // 2. The signature on the response is valid.
155+ // Steps 3 and 4 are performed BEFORE step 2. The responder certificate is
156+ // attacker-controlled until validated; verifying the response signature first lets
157+ // an untrusted key drive expensive or unsafe crypto (e.g., a huge RSA public exponent)
158+ // that later rejection can't undo.
159+
160+ // 3. The identity of the signer matches the intended recipient of the
161+ // request.
162+ //
163+ // 4. The signer is currently authorized to provide a response for the
164+ // certificate in question.
151165
152166 // We assume that the responder includes its certificate in the certs field of the response
153167 // that helps us to verify it. According to RFC 2560 this field is optional, but including it
@@ -158,13 +172,6 @@ protected void verifyOcspResponse(BasicOCSPResp basicResponse, OcspService ocspS
158172 }
159173 // The first certificate is the responder certificate, other certificates, if given, are the certificate's chain.
160174 final X509CertificateHolder responderCert = basicResponse .getCerts ()[0 ];
161- OcspResponseValidator .validateResponseSignature (basicResponse , responderCert , ocspService .getAccessLocation ());
162-
163- // 3. The identity of the signer matches the intended recipient of the
164- // request.
165- //
166- // 4. The signer is currently authorized to provide a response for the
167- // certificate in question.
168175
169176 // Use the clock instance so that the date can be mocked in tests.
170177 final Date now = DateAndTime .DefaultClock .getInstance ().now ();
@@ -175,14 +182,18 @@ protected void verifyOcspResponse(BasicOCSPResp basicResponse, OcspService ocspS
175182 throw new UserCertificateOCSPCheckFailedException (e , ocspService .getAccessLocation ());
176183 }
177184
185+ // 2. The signature on the response is valid.
186+
187+ OcspResponseValidator .validateResponseSignature (basicResponse , responderCert , ocspService .getAccessLocation ());
188+
178189 // 5. The time at which the status being indicated is known to be
179190 // correct (thisUpdate) is sufficiently recent.
180191 //
181192 // 6. When available, the time at or before which newer information will
182193 // be available about the status of the certificate (nextUpdate) is
183194 // greater than the current time.
184195
185- OcspResponseValidator .validateCertificateStatusUpdateTime (certStatusResponse , allowedOcspResponseTimeSkew , maxOcspResponseThisUpdateAge , ocspService .getAccessLocation ());
196+ OcspResponseValidator .validateCertificateStatusUpdateTime (certStatusResponse , allowedOcspResponseTimeSkew , ocspService . getMaxThisUpdateAge (), ocspService . getMaxNextUpdateAge () , ocspService .getAccessLocation ());
186197
187198 // Now we can accept the signed response as valid and validate the certificate status.
188199 OcspResponseValidator .validateSubjectCertificateStatus (certStatusResponse , ocspService .getAccessLocation ());
@@ -202,11 +213,15 @@ protected static void checkNonce(OCSPReq request, BasicOCSPResp response, URI oc
202213 }
203214 }
204215
205- protected static CertificateID getCertificateId (X509Certificate subjectCertificate , X509Certificate issuerCertificate ) throws CertificateEncodingException , IOException , OCSPException {
206- final BigInteger serial = subjectCertificate .getSerialNumber ();
207- final DigestCalculator digestCalculator = DigestCalculatorImpl .sha1 ();
208- return new CertificateID (digestCalculator ,
209- new X509CertificateHolder (issuerCertificate .getEncoded ()), serial );
216+ protected static CertificateID getCertificateId (X509Certificate subjectCertificate , X509Certificate issuerCertificate ) throws UserCertificateOCSPException {
217+ try {
218+ final BigInteger serial = subjectCertificate .getSerialNumber ();
219+ final DigestCalculator digestCalculator = DigestCalculatorImpl .sha1 ();
220+ return new CertificateID (digestCalculator ,
221+ new X509CertificateHolder (issuerCertificate .getEncoded ()), serial );
222+ } catch (CertificateEncodingException | IOException | OCSPException e ) {
223+ throw new UserCertificateOCSPException ("Unable to compute certificateId for subject certificate" , e );
224+ }
210225 }
211226
212227 protected static String ocspStatusToString (int status ) {
@@ -227,8 +242,4 @@ protected OcspClient getOcspClient() {
227242 protected OcspServiceProvider getOcspServiceProvider () {
228243 return ocspServiceProvider ;
229244 }
230-
231- protected Duration getMaxOcspResponseThisUpdateAge () {
232- return maxOcspResponseThisUpdateAge ;
233- }
234245}
0 commit comments