Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit b9ba6fe

Browse files
authored
Now throwing on certificate decoding errors (#904)
1 parent 3dec9a9 commit b9ba6fe

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

libraries/bot-connector/src/main/java/com/microsoft/bot/connector/authentication/JwtTokenExtractor.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.auth0.jwt.interfaces.Verification;
1111
import com.microsoft.bot.connector.ExecutorFactory;
1212
import java.io.ByteArrayInputStream;
13+
import java.security.cert.CertificateException;
1314
import java.security.cert.CertificateFactory;
1415
import java.security.cert.X509Certificate;
1516
import java.util.Base64;
@@ -156,12 +157,8 @@ private CompletableFuture<ClaimsIdentity> validateToken(
156157
&& key.certificateChain != null
157158
&& key.certificateChain.size() > 0
158159
) {
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.
163160
X509Certificate cert = decodeCertificate(key.certificateChain.get(0));
164-
if (cert != null && !isCertValid(cert)) {
161+
if (!isCertValid(cert)) {
165162
throw new JWTVerificationException("Signing certificate is not valid");
166163
}
167164
}
@@ -209,24 +206,24 @@ private CompletableFuture<ClaimsIdentity> validateToken(
209206
}
210207

211208
return new ClaimsIdentity(decodedJWT);
212-
} catch (JWTVerificationException ex) {
209+
} catch (JWTVerificationException | CertificateException ex) {
213210
LOGGER.warn(ex.getMessage());
214211
throw new AuthenticationException(ex);
215212
}
216213
}, ExecutorFactory.getExecutor());
217214
}
218215

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));
227220
}
228221

229222
private boolean isCertValid(X509Certificate cert) {
223+
if (cert == null) {
224+
return false;
225+
}
226+
230227
long now = new Date().getTime();
231228
long clockskew = tokenValidationParameters.clockSkew.toMillis();
232229
long startValid = cert.getNotBefore().getTime() - clockskew;

0 commit comments

Comments
 (0)