Skip to content

Commit c482f9c

Browse files
committed
Fixed key reuse issue
1 parent a60ee49 commit c482f9c

3 files changed

Lines changed: 7 additions & 20 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ void connect() throws IOException, GeneralSecurityException, SpotifyAuthenticati
129129
acc.dump();
130130

131131
Keyexchange.APResponseMessage apResponseMessage = Keyexchange.APResponseMessage.parseFrom(buffer);
132-
keys.computeSharedKey(apResponseMessage.getChallenge().getLoginCryptoChallenge().getDiffieHellman().getGs().toByteArray());
132+
byte[] sharedKey = Utils.toByteArray(keys.computeSharedKey(apResponseMessage.getChallenge().getLoginCryptoChallenge().getDiffieHellman().getGs().toByteArray()));
133133

134134

135135
// Solve challenge
136136

137137
ByteArrayOutputStream data = new ByteArrayOutputStream(0x64);
138138

139139
Mac mac = Mac.getInstance("HmacSHA1");
140-
mac.init(new SecretKeySpec(keys.sharedKeyArray(), "HmacSHA1"));
140+
mac.init(new SecretKeySpec(sharedKey, "HmacSHA1"));
141141
for (int i = 1; i < 6; i++) {
142142
mac.update(acc.array());
143143
mac.update(new byte[]{(byte) i});

core/src/main/java/xyz/gianlu/librespot/core/ZeroconfServer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,14 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
181181
return;
182182
}
183183

184-
keys.computeSharedKey(Base64.getDecoder().decode(clientKeyStr));
185-
184+
byte[] sharedKey = Utils.toByteArray(keys.computeSharedKey(Base64.getDecoder().decode(clientKeyStr)));
186185
byte[] blobBytes = Base64.getDecoder().decode(blobStr);
187186
byte[] iv = Arrays.copyOfRange(blobBytes, 0, 16);
188187
byte[] encrypted = Arrays.copyOfRange(blobBytes, 16, blobBytes.length - 20);
189188
byte[] checksum = Arrays.copyOfRange(blobBytes, blobBytes.length - 20, blobBytes.length);
190189

191190
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
192-
sha1.update(keys.sharedKeyArray());
191+
sha1.update(sharedKey);
193192
byte[] baseKey = Arrays.copyOfRange(sha1.digest(), 0, 16);
194193

195194
Mac hmac = Mac.getInstance("HmacSHA1");

core/src/main/java/xyz/gianlu/librespot/crypto/DiffieHellman.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class DiffieHellman {
2828
private static final Logger LOGGER = Logger.getLogger(DiffieHellman.class);
2929
private final BigInteger privateKey;
3030
private final BigInteger publicKey;
31-
private BigInteger sharedKey = null;
3231

3332
public DiffieHellman(Random random) {
3433
byte[] keyData = new byte[95];
@@ -38,26 +37,15 @@ public DiffieHellman(Random random) {
3837
publicKey = GENERATOR.modPow(privateKey, PRIME);
3938
}
4039

41-
public void computeSharedKey(byte[] remoteKeyBytes) {
42-
if (sharedKey != null) throw new IllegalStateException("Cannot reuse object!");
43-
40+
@NotNull
41+
public BigInteger computeSharedKey(byte[] remoteKeyBytes) {
4442
BigInteger remoteKey = new BigInteger(1, remoteKeyBytes);
45-
sharedKey = remoteKey.modPow(privateKey, PRIME);
43+
BigInteger sharedKey = remoteKey.modPow(privateKey, PRIME);
4644

4745
LOGGER.trace("Computed shared key successfully!");
48-
}
49-
50-
@NotNull
51-
public BigInteger sharedKey() {
52-
if (sharedKey == null) throw new IllegalStateException("Shared key not initialized!");
5346
return sharedKey;
5447
}
5548

56-
@NotNull
57-
public byte[] sharedKeyArray() {
58-
return Utils.toByteArray(sharedKey());
59-
}
60-
6149
@NotNull
6250
public BigInteger privateKey() {
6351
return privateKey;

0 commit comments

Comments
 (0)