Skip to content

Commit 30a9eee

Browse files
committed
Use SHA256 thumbprints in non-ADFS cert flows
1 parent 6f94eee commit 30a9eee

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

msal4j-sdk/src/integrationtest/java/com.microsoft.aad.msal4j/ClientCredentialsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private ClientAssertion getClientAssertion(String clientId) {
160160
clientId,
161161
(ClientCertificate) certificate,
162162
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
163-
true);
163+
true, false);
164164
}
165165

166166
private void assertAcquireTokenCommon(String clientId, IClientCredential credential, String authority) throws Exception {

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ClientCertificate.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public String publicCertificateHash()
5656
.getHash(publicKeyCertificateChain.get(0).getEncoded()));
5757
}
5858

59+
public String publicCertificateHashSha1()
60+
throws CertificateEncodingException, NoSuchAlgorithmException {
61+
62+
return Base64.getEncoder().encodeToString(ClientCertificate
63+
.getHashSha1(publicKeyCertificateChain.get(0).getEncoded()));
64+
}
65+
5966
public List<String> getEncodedPublicKeyCertificateChain() throws CertificateEncodingException {
6067
List<String> result = new ArrayList<>();
6168

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

122-
private static byte[] getHash(final byte[] inputBytes) throws NoSuchAlgorithmException {
129+
private static byte[] getHashSha1(final byte[] inputBytes) throws NoSuchAlgorithmException {
123130
final MessageDigest md = MessageDigest.getInstance("SHA-1");
124131
md.update(inputBytes);
125132
return md.digest();
126133
}
134+
135+
private static byte[] getHash(final byte[] inputBytes) throws NoSuchAlgorithmException {
136+
final MessageDigest md = MessageDigest.getInstance("SHA-256");
137+
md.update(inputBytes);
138+
return md.digest();
139+
}
127140
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ConfidentialClientApplication.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ private void initClientAuthentication(IClientCredential clientCredential) {
101101
} else if (clientCredential instanceof ClientCertificate) {
102102
this.clientCertAuthentication = true;
103103
this.clientCertificate = (ClientCertificate) clientCredential;
104-
clientAuthentication = buildValidClientCertificateAuthority();
104+
if (Authority.detectAuthorityType(this.authenticationAuthority.canonicalAuthorityUrl()) == AuthorityType.ADFS) {
105+
clientAuthentication = buildValidClientCertificateAuthorityLegacySha1();
106+
} else {
107+
clientAuthentication = buildValidClientCertificateAuthority();
108+
}
105109
} else if (clientCredential instanceof ClientAssertion) {
106110
clientAuthentication = createClientAuthFromClientAssertion((ClientAssertion) clientCredential);
107111
} else {
@@ -127,7 +131,18 @@ private ClientAuthentication buildValidClientCertificateAuthority() {
127131
clientId(),
128132
clientCertificate,
129133
this.authenticationAuthority.selfSignedJwtAudience(),
130-
sendX5c);
134+
sendX5c,
135+
false);
136+
return createClientAuthFromClientAssertion(clientAssertion);
137+
}
138+
139+
private ClientAuthentication buildValidClientCertificateAuthorityLegacySha1() {
140+
ClientAssertion clientAssertion = JwtHelper.buildJwt(
141+
clientId(),
142+
clientCertificate,
143+
this.authenticationAuthority.selfSignedJwtAudience(),
144+
sendX5c,
145+
true);
131146
return createClientAuthFromClientAssertion(clientAssertion);
132147
}
133148

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/JwtHelper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
final class JwtHelper {
2222

2323
static ClientAssertion buildJwt(String clientId, final ClientCertificate credential,
24-
final String jwtAudience, boolean sendX5c) throws MsalClientException {
24+
final String jwtAudience, boolean sendX5c,
25+
boolean useLegacySha1) throws MsalClientException {
2526
if (StringHelper.isBlank(clientId)) {
2627
throw new IllegalArgumentException("clientId is null or empty");
2728
}
@@ -55,7 +56,11 @@ static ClientAssertion buildJwt(String clientId, final ClientCertificate credent
5556
builder.x509CertChain(certs);
5657
}
5758

58-
builder.x509CertThumbprint(new Base64URL(credential.publicCertificateHash()));
59+
if (useLegacySha1) {
60+
builder.x509CertThumbprint(new Base64URL(credential.publicCertificateHashSha1()));
61+
} else {
62+
builder.x509CertSHA256Thumbprint(new Base64URL(credential.publicCertificateHash()));
63+
}
5964

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

0 commit comments

Comments
 (0)