|
10 | 10 | import com.auth0.jwt.interfaces.Verification; |
11 | 11 | import com.microsoft.bot.connector.ExecutorFactory; |
12 | 12 | import java.io.ByteArrayInputStream; |
| 13 | +import java.security.cert.CertificateException; |
13 | 14 | import java.security.cert.CertificateFactory; |
14 | 15 | import java.security.cert.X509Certificate; |
15 | 16 | import java.util.Base64; |
@@ -156,12 +157,8 @@ private CompletableFuture<ClaimsIdentity> validateToken( |
156 | 157 | && key.certificateChain != null |
157 | 158 | && key.certificateChain.size() > 0 |
158 | 159 | ) { |
159 | | - // Note that decodeCertificate will return null if the cert could not |
160 | | - // be decoded. This would likely be the case if it were in an unexpected |
161 | | - // encoding. Going to err on the side of ignoring this check. |
162 | | - // May want to reconsider this and throw on null cert. |
163 | 160 | X509Certificate cert = decodeCertificate(key.certificateChain.get(0)); |
164 | | - if (cert != null && !isCertValid(cert)) { |
| 161 | + if (!isCertValid(cert)) { |
165 | 162 | throw new JWTVerificationException("Signing certificate is not valid"); |
166 | 163 | } |
167 | 164 | } |
@@ -209,24 +206,24 @@ private CompletableFuture<ClaimsIdentity> validateToken( |
209 | 206 | } |
210 | 207 |
|
211 | 208 | return new ClaimsIdentity(decodedJWT); |
212 | | - } catch (JWTVerificationException ex) { |
| 209 | + } catch (JWTVerificationException | CertificateException ex) { |
213 | 210 | LOGGER.warn(ex.getMessage()); |
214 | 211 | throw new AuthenticationException(ex); |
215 | 212 | } |
216 | 213 | }, ExecutorFactory.getExecutor()); |
217 | 214 | } |
218 | 215 |
|
219 | | - private X509Certificate decodeCertificate(String certStr) { |
220 | | - try { |
221 | | - byte[] decoded = Base64.getDecoder().decode(certStr); |
222 | | - return (X509Certificate) CertificateFactory |
223 | | - .getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded)); |
224 | | - } catch (Throwable t) { |
225 | | - return null; |
226 | | - } |
| 216 | + private X509Certificate decodeCertificate(String certStr) throws CertificateException { |
| 217 | + byte[] decoded = Base64.getDecoder().decode(certStr); |
| 218 | + return (X509Certificate) CertificateFactory |
| 219 | + .getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded)); |
227 | 220 | } |
228 | 221 |
|
229 | 222 | private boolean isCertValid(X509Certificate cert) { |
| 223 | + if (cert == null) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
230 | 227 | long now = new Date().getTime(); |
231 | 228 | long clockskew = tokenValidationParameters.clockSkew.toMillis(); |
232 | 229 | long startValid = cert.getNotBefore().getTime() - clockskew; |
|
0 commit comments