Skip to content

Commit

Permalink
Merge pull request #1809 from ivassile/JBEAP-23946
Browse files Browse the repository at this point in the history
[JBEAP-23946] CVE-2022-3143 wildfly-elytron: possible timing attacks via use of unsafe comparator
  • Loading branch information
Skyllarr committed Dec 1, 2022
2 parents 59b0f66 + 1e386fd commit 9f6dca3
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.security.Provider;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -219,7 +218,7 @@ public boolean verifyCertificate(X509Certificate certificate, Attributes attribu
for (int i = 0; i < size; i++) {
Object attrCertificate = attribute.get(i);
if (attrCertificate != null){
if (Arrays.equals(certificate.getEncoded(), (byte[]) attrCertificate)) {
if (MessageDigest.isEqual(certificate.getEncoded(), (byte[]) attrCertificate)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.security.Provider;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import java.util.function.Supplier;

import org.wildfly.common.Assert;
Expand Down Expand Up @@ -56,7 +56,7 @@ public boolean verify(final Evidence evidence) {
if (evidence instanceof X509PeerCertificateChainEvidence) {
final X509PeerCertificateChainEvidence peerCertificateChainEvidence = (X509PeerCertificateChainEvidence) evidence;
try {
return getAlgorithm().equals(peerCertificateChainEvidence.getAlgorithm()) && Arrays.equals(getFirstCertificate().getEncoded(), peerCertificateChainEvidence.getFirstCertificate().getEncoded());
return getAlgorithm().equals(peerCertificateChainEvidence.getAlgorithm()) && MessageDigest.isEqual(getFirstCertificate().getEncoded(), peerCertificateChainEvidence.getFirstCertificate().getEncoded());
} catch (CertificateEncodingException e) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.wildfly.common.math.HashMath.multiHashOrdered;

import java.security.MessageDigest;
import java.util.Arrays;

class RawDigestPassword extends RawPassword implements DigestPassword {
Expand Down Expand Up @@ -62,6 +63,6 @@ public boolean equals(final Object obj) {
return false;
}
RawDigestPassword other = (RawDigestPassword) obj;
return Arrays.equals(digest, other.digest) && username.equals(other.username) && realm.equals(other.realm) && getAlgorithm().equals(other.getAlgorithm());
return MessageDigest.isEqual(digest, other.digest) && username.equals(other.username) && realm.equals(other.realm) && getAlgorithm().equals(other.getAlgorithm());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.wildfly.common.math.HashMath.multiHashOrdered;

import java.security.MessageDigest;
import java.util.Arrays;

class RawSaltedSimpleDigestPassword extends RawPassword implements SaltedSimpleDigestPassword {
Expand Down Expand Up @@ -56,6 +57,7 @@ public boolean equals(final Object obj) {
return false;
}
RawSaltedSimpleDigestPassword other = (RawSaltedSimpleDigestPassword) obj;
return getAlgorithm().equals(other.getAlgorithm()) && Arrays.equals(digest, other.digest) && Arrays.equals(salt, other.salt);
return getAlgorithm().equals(other.getAlgorithm()) && MessageDigest.isEqual(digest, other.digest)
&& Arrays.equals(salt, other.salt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.wildfly.common.math.HashMath.multiHashOrdered;

import java.security.MessageDigest;
import java.util.Arrays;

class RawScramDigestPassword extends RawPassword implements ScramDigestPassword {
Expand Down Expand Up @@ -54,14 +55,16 @@ public RawScramDigestPassword clone() {
}

public int hashCode() {
return multiHashOrdered(multiHashOrdered(multiHashOrdered(Arrays.hashCode(digest), Arrays.hashCode(salt)), iterationCount), getAlgorithm().hashCode());
return multiHashOrdered(multiHashOrdered(multiHashOrdered(Arrays.hashCode(digest),
Arrays.hashCode(salt)), iterationCount), getAlgorithm().hashCode());
}

public boolean equals(final Object obj) {
if (! (obj instanceof RawScramDigestPassword)) {
return false;
}
RawScramDigestPassword other = (RawScramDigestPassword) obj;
return iterationCount == other.iterationCount && getAlgorithm().equals(other.getAlgorithm()) && Arrays.equals(digest, other.digest) && Arrays.equals(salt, other.salt);
return iterationCount == other.iterationCount && getAlgorithm().equals(other.getAlgorithm())
&& MessageDigest.isEqual(digest, other.digest) && Arrays.equals(salt, other.salt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.wildfly.common.math.HashMath.multiHashOrdered;

import java.security.MessageDigest;
import java.util.Arrays;

class RawSimpleDigestPassword extends RawPassword implements SimpleDigestPassword {
Expand Down Expand Up @@ -50,6 +51,6 @@ public boolean equals(final Object obj) {
return false;
}
RawSimpleDigestPassword other = (RawSimpleDigestPassword) obj;
return getAlgorithm().equals(other.getAlgorithm()) && Arrays.equals(digest, other.digest);
return getAlgorithm().equals(other.getAlgorithm()) && MessageDigest.isEqual(digest, other.digest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import static org.wildfly.common.math.HashMath.multiHashOrdered;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Objects;


/**
* A {@link PasswordSpec} for a password represented by a Digest Response as seen in Digest-MD5 SASL/HTTP mechanism.
*
Expand Down Expand Up @@ -70,7 +72,7 @@ public byte[] getDigest() {
public boolean equals(Object other) {
if (! (other instanceof DigestPasswordSpec)) return false;
DigestPasswordSpec o = (DigestPasswordSpec) other;
return Objects.equals(username, o.username) && Objects.equals(realm, o.realm) && Arrays.equals(digest, o.digest);
return Objects.equals(username, o.username) && Objects.equals(realm, o.realm) && MessageDigest.isEqual(digest, o.digest);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package org.wildfly.security.password.spec;

import java.security.MessageDigest;
import java.util.Arrays;

import org.wildfly.common.Assert;

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ public byte[] getDigest() {

@Override
public boolean equals(Object other) {
return other instanceof HashPasswordSpec && Arrays.equals(digest, ((HashPasswordSpec)other).digest);
return other instanceof HashPasswordSpec && MessageDigest.isEqual(digest, ((HashPasswordSpec)other).digest);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ <S extends KeySpec> S getKeySpec(Class<S> keySpecType) throws InvalidKeySpecExce
boolean verify(char[] guess) throws InvalidKeyException {
try {
byte[] guessDigest = userRealmPasswordDigest(getMessageDigest(algorithm), username, realm, guess);
return Arrays.equals(digest, guessDigest);
return MessageDigest.isEqual(digest, guessDigest);
} catch (NoSuchAlgorithmException e) {
throw log.invalidKeyNoSuchMessageDigestAlgorithm(algorithm);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public boolean equals(final Object obj) {
return false;
}
DigestPasswordImpl other = (DigestPasswordImpl) obj;
return Arrays.equals(digest, other.digest) && username.equals(other.username) && realm.equals(other.realm) && algorithm.equals(other.algorithm);
return MessageDigest.isEqual(digest, other.digest) && username.equals(other.username) && realm.equals(other.realm) && algorithm.equals(other.algorithm);
}

private void readObject(ObjectInputStream ignored) throws NotSerializableException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.ObjectInputStream;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
Expand Down Expand Up @@ -210,7 +211,9 @@ public boolean equals(final Object obj) {
return false;
}
MaskedPasswordImpl other = (MaskedPasswordImpl) obj;
return iterationCount == other.iterationCount && Arrays.equals(initialKeyMaterial, other.initialKeyMaterial) && Arrays.equals(salt, other.salt) && Arrays.equals(maskedPasswordBytes, other.maskedPasswordBytes) && Arrays.equals(initializationVector, other.initializationVector) && algorithm.equals(other.algorithm);
return iterationCount == other.iterationCount && Arrays.equals(initialKeyMaterial, other.initialKeyMaterial)
&& MessageDigest.isEqual(salt, other.salt) && MessageDigest.isEqual(maskedPasswordBytes, other.maskedPasswordBytes)
&& MessageDigest.isEqual(initializationVector, other.initializationVector) && algorithm.equals(other.algorithm);
}

Object writeReplace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <S extends KeySpec> S getKeySpec(Class<S> keySpecType) throws InvalidKeySpecExce
@Override
boolean verify(char[] guess) throws InvalidKeyException {
try {
return Arrays.equals(digest, digestOf(algorithm, salt, guess));
return MessageDigest.isEqual(digest, digestOf(algorithm, salt, guess));
} catch (NoSuchAlgorithmException e) {
throw log.invalidKeyNoSuchMessageDigestAlgorithm(algorithm);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public boolean equals(final Object obj) {
return false;
}
SaltedSimpleDigestPasswordImpl other = (SaltedSimpleDigestPasswordImpl) obj;
return algorithm.equals(other.algorithm) && Arrays.equals(digest, other.digest) && Arrays.equals(salt, other.salt);
return algorithm.equals(other.algorithm) && MessageDigest.isEqual(digest, other.digest) && Arrays.equals(salt, other.salt);
}

private void readObject(ObjectInputStream ignored) throws NotSerializableException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
Expand Down Expand Up @@ -193,7 +194,7 @@ boolean verify(char[] guess) throws InvalidKeyException {
if (guess.length == 0) return false;
try {
byte[] output = scramDigest(this.getAlgorithm(), getNormalizedPasswordBytes(guess), this.getSalt(), this.getIterationCount());
return Arrays.equals(this.digest, output);
return MessageDigest.isEqual(this.digest, output);
} catch (NoSuchAlgorithmException nsae) {
throw new InvalidKeyException(nsae);
}
Expand Down Expand Up @@ -308,7 +309,7 @@ public boolean equals(final Object obj) {
return false;
}
ScramDigestPasswordImpl other = (ScramDigestPasswordImpl) obj;
return iterationCount == other.iterationCount && algorithm.equals(other.algorithm) && Arrays.equals(digest, other.digest) && Arrays.equals(salt, other.salt);
return iterationCount == other.iterationCount && algorithm.equals(other.algorithm) && MessageDigest.isEqual(digest, other.digest) && Arrays.equals(salt, other.salt);
}

private void readObject(ObjectInputStream ignored) throws NotSerializableException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static MessageDigest getMessageDigest(String algorithm) throws NoSuchAlgorithmEx

boolean verify(final char[] guess) throws InvalidKeyException {
try {
return Arrays.equals(digest, getDigestOf(algorithm, guess));
return MessageDigest.isEqual(digest, getDigestOf(algorithm, guess));
} catch (NoSuchAlgorithmException e) {
throw log.invalidKeyNoSuchMessageDigestAlgorithm(algorithm);
}
Expand All @@ -128,7 +128,7 @@ public boolean equals(final Object obj) {
return false;
}
SimpleDigestPasswordImpl other = (SimpleDigestPasswordImpl) obj;
return algorithm.equals(other.algorithm) && Arrays.equals(digest, other.digest);
return algorithm.equals(other.algorithm) && MessageDigest.isEqual(digest, other.digest);
}

private void readObject(ObjectInputStream ignored) throws NotSerializableException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ boolean verify(final char[] guess) throws InvalidKeyException {
} catch (NoSuchAlgorithmException e) {
throw log.invalidKeyCannotVerifyPassword(e);
}
return Arrays.equals(getHash(), test);
return MessageDigest.isEqual(getHash(), test);
}

@Override
Expand Down Expand Up @@ -292,7 +292,7 @@ public boolean equals(final Object obj) {
return false;
}
SunUnixMD5CryptPasswordImpl other = (SunUnixMD5CryptPasswordImpl) obj;
return iterationCount == other.iterationCount && algorithm.equals(other.algorithm) && Arrays.equals(hash, other.hash) && Arrays.equals(salt, other.salt);
return iterationCount == other.iterationCount && algorithm.equals(other.algorithm) && MessageDigest.isEqual(hash, other.hash) && Arrays.equals(salt, other.salt);
}

private void readObject(ObjectInputStream ignored) throws NotSerializableException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ boolean verify(final char[] guess) throws InvalidKeyException {
} catch (NoSuchAlgorithmException e) {
throw log.invalidKeyCannotVerifyPassword(e);
}
return Arrays.equals(getHash(), test);
return MessageDigest.isEqual(getHash(), test);
}

@Override
Expand Down Expand Up @@ -238,7 +238,7 @@ public boolean equals(final Object obj) {
return false;
}
UnixMD5CryptPasswordImpl other = (UnixMD5CryptPasswordImpl) obj;
return Arrays.equals(hash, other.hash) && Arrays.equals(salt, other.salt);
return MessageDigest.isEqual(hash, other.hash) && Arrays.equals(salt, other.salt);
}

private void readObject(ObjectInputStream ignored) throws NotSerializableException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ boolean verify(final char[] guess) throws InvalidKeyException {
try {
byte[] password = getNormalizedPasswordBytes(guess);
byte[] encodedGuess = doEncode(algorithm, password, salt, iterationCount);
return Arrays.equals(getHash(), encodedGuess);
return MessageDigest.isEqual(getHash(), encodedGuess);
} catch (NoSuchAlgorithmException e) {
throw log.invalidKeyCannotVerifyPassword(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.UnknownHostException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.util.Arrays;

import javax.security.auth.x500.X500Principal;
Expand Down Expand Up @@ -189,7 +190,7 @@ public boolean equals(final Object obj) {
}

public boolean equals(final OtherName other) {
return other != null && Arrays.equals(encodedName, other.getName());
return other != null && MessageDigest.isEqual(encodedName, other.getName());
}

public int hashCode() {
Expand Down Expand Up @@ -352,7 +353,7 @@ public boolean equals(final Object obj) {
}

public boolean equals(final X400Address other) {
return other != null && Arrays.equals(encodedName, other.getName());
return other != null && MessageDigest.isEqual(encodedName, other.getName());
}

public int hashCode() {
Expand Down Expand Up @@ -471,7 +472,7 @@ public boolean equals(final Object obj) {
}

public boolean equals(final EDIPartyName other) {
return other != null && Arrays.equals(encodedName, other.getName());
return other != null && MessageDigest.isEqual(encodedName, other.getName());
}

public int hashCode() {
Expand Down Expand Up @@ -603,7 +604,7 @@ public boolean equals(final IPAddress other) {
}
return true;
} else {
return Arrays.equals(address, other.getName());
return MessageDigest.isEqual(address, other.getName());
}
}
return false;
Expand Down

0 comments on commit 9f6dca3

Please sign in to comment.