Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SHA256 thumbprints in non-ADFS cert flows #840

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private ClientAssertion getClientAssertion(String clientId) {
clientId,
(ClientCertificate) certificate,
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
true);
true, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct in understanding that the existing tests cover the following?

  • AAD + sha2
  • ADFS + sha1

}

private void assertAcquireTokenCommon(String clientId, IClientCredential credential, String authority) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public String publicCertificateHash()
.getHash(publicKeyCertificateChain.get(0).getEncoded()));
}

public String publicCertificateHashSha1()
throws CertificateEncodingException, NoSuchAlgorithmException {

return Base64.getEncoder().encodeToString(ClientCertificate
.getHashSha1(publicKeyCertificateChain.get(0).getEncoded()));
}

public List<String> getEncodedPublicKeyCertificateChain() throws CertificateEncodingException {
List<String> result = new ArrayList<>();

Expand Down Expand Up @@ -119,9 +126,15 @@ static ClientCertificate create(final PrivateKey key, final X509Certificate publ
return new ClientCertificate(key, Arrays.asList(publicKeyCertificate));
}

private static byte[] getHash(final byte[] inputBytes) throws NoSuchAlgorithmException {
private static byte[] getHashSha1(final byte[] inputBytes) throws NoSuchAlgorithmException {
final MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(inputBytes);
return md.digest();
}

private static byte[] getHash(final byte[] inputBytes) throws NoSuchAlgorithmException {
Avery-Dunn marked this conversation as resolved.
Show resolved Hide resolved
final MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(inputBytes);
return md.digest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ private void initClientAuthentication(IClientCredential clientCredential) {
} else if (clientCredential instanceof ClientCertificate) {
this.clientCertAuthentication = true;
this.clientCertificate = (ClientCertificate) clientCredential;
clientAuthentication = buildValidClientCertificateAuthority();
if (Authority.detectAuthorityType(this.authenticationAuthority.canonicalAuthorityUrl()) == AuthorityType.ADFS) {
clientAuthentication = buildValidClientCertificateAuthorityLegacySha1();
} else {
clientAuthentication = buildValidClientCertificateAuthority();
}
} else if (clientCredential instanceof ClientAssertion) {
clientAuthentication = createClientAuthFromClientAssertion((ClientAssertion) clientCredential);
} else {
Expand All @@ -127,7 +131,18 @@ private ClientAuthentication buildValidClientCertificateAuthority() {
clientId(),
clientCertificate,
this.authenticationAuthority.selfSignedJwtAudience(),
sendX5c);
sendX5c,
false);
return createClientAuthFromClientAssertion(clientAssertion);
}

private ClientAuthentication buildValidClientCertificateAuthorityLegacySha1() {
ClientAssertion clientAssertion = JwtHelper.buildJwt(
clientId(),
clientCertificate,
this.authenticationAuthority.selfSignedJwtAudience(),
sendX5c,
true);
return createClientAuthFromClientAssertion(clientAssertion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
final class JwtHelper {

static ClientAssertion buildJwt(String clientId, final ClientCertificate credential,
final String jwtAudience, boolean sendX5c) throws MsalClientException {
final String jwtAudience, boolean sendX5c,
boolean useLegacySha1) throws MsalClientException {
if (StringHelper.isBlank(clientId)) {
throw new IllegalArgumentException("clientId is null or empty");
}
Expand Down Expand Up @@ -55,7 +56,11 @@ static ClientAssertion buildJwt(String clientId, final ClientCertificate credent
builder.x509CertChain(certs);
}

builder.x509CertThumbprint(new Base64URL(credential.publicCertificateHash()));
if (useLegacySha1) {
builder.x509CertThumbprint(new Base64URL(credential.publicCertificateHashSha1()));
} else {
builder.x509CertSHA256Thumbprint(new Base64URL(credential.publicCertificateHash()));
}

jwt = new SignedJWT(builder.build(), claimsSet);
final RSASSASigner signer = new RSASSASigner(credential.privateKey());
Expand Down