myTeamSpeak: announce the account ID to servers
Signs clientinit with the account's Ed25519 key over the connection challenge, honours the per-bookmark switch, sends updatemytsid on sign-in/out, and connects without the ID when myTeamSpeak is unreachable. Verified live against server.lixko.eu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -118,7 +118,25 @@ public final class MyTeamSpeak {
|
||||
}
|
||||
|
||||
/** A signed-in session: the bearer token later requests carry, and who it belongs to. */
|
||||
record Session(String id, AccountInfo info, byte[] keyPackage) {
|
||||
record Session(String id, AccountInfo info, byte[] keyPackage, byte[] userId, long createdAt,
|
||||
byte[] publicKey, byte[] encryptedPrivateKey, byte[] publicSignature) {
|
||||
}
|
||||
|
||||
/** Reads only the server identity; does not download cloud items. */
|
||||
MyTeamSpeakIdentity serverIdentity(Credentials credentials) throws IOException, MyTsException {
|
||||
Session session = signIn(credentials);
|
||||
byte[] itemKey = null;
|
||||
try {
|
||||
if (session.keyPackage == null) throw new MyTsException("The account has no encryption key", 0);
|
||||
itemKey = MyTsCrypto.unwrapItemKey(credentials.accountKey(), session.keyPackage);
|
||||
return new MyTeamSpeakIdentity(session.userId, session.createdAt, session.publicKey,
|
||||
session.encryptedPrivateKey, session.publicSignature, itemKey);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new MyTsException("Could not unlock the myTeamSpeak server identity", e);
|
||||
} finally {
|
||||
if (itemKey != null) java.util.Arrays.fill(itemKey, (byte) 0);
|
||||
signOut(session.id);
|
||||
}
|
||||
}
|
||||
|
||||
/** Signs in; the caller ends the session with {@link #signOut}. */
|
||||
@@ -129,6 +147,7 @@ public final class MyTeamSpeak {
|
||||
byte[] keyPackage = null;
|
||||
String session = "", username = "", uuid = "";
|
||||
byte[] userId = null;
|
||||
byte[] publicKey = null, encryptedPrivateKey = null, publicSignature = null;
|
||||
long createdAt = 0;
|
||||
int error = 0;
|
||||
while (reply.next()) {
|
||||
@@ -142,8 +161,17 @@ public final class MyTeamSpeak {
|
||||
ProtobufReader identity = reply.readMessage();
|
||||
while (identity.next()) {
|
||||
switch (identity.fieldNumber()) {
|
||||
case 1 -> publicKey = identity.readBytes();
|
||||
case 2 -> {
|
||||
ProtobufReader key = identity.readMessage();
|
||||
while (key.next()) {
|
||||
if (key.fieldNumber() == 1) encryptedPrivateKey = key.readBytes();
|
||||
else key.skip();
|
||||
}
|
||||
}
|
||||
case 3 -> createdAt = identity.readVarint();
|
||||
case 4 -> userId = identity.readBytes();
|
||||
case 5 -> publicSignature = identity.readBytes();
|
||||
default -> identity.skip();
|
||||
}
|
||||
}
|
||||
@@ -152,7 +180,8 @@ public final class MyTeamSpeak {
|
||||
}
|
||||
}
|
||||
if (error != LOGIN_OK) throw loginError(error);
|
||||
return new Session(session, AccountInfo.of(username, uuid, userId, createdAt), keyPackage);
|
||||
return new Session(session, AccountInfo.of(username, uuid, userId, createdAt), keyPackage,
|
||||
userId, createdAt, publicKey, encryptedPrivateKey, publicSignature);
|
||||
}
|
||||
|
||||
private List<SyncItem> pull(String session, byte[] itemKey) throws IOException, MyTsException {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package eu.lixko.ts3j.client.myts;
|
||||
|
||||
import Punisher.NaCl.Internal.Ed25519Ref10.GroupElementP3;
|
||||
import Punisher.NaCl.Internal.Ed25519Ref10.GroupOperations;
|
||||
import Punisher.NaCl.Internal.Ed25519Ref10.ScalarOperations;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** Account identity used to authenticate to voice servers. Never persisted or logged. */
|
||||
public final class MyTeamSpeakIdentity implements AutoCloseable {
|
||||
private final byte[] scalar;
|
||||
private final byte[] publicKey;
|
||||
private final Map<String, String> parameters;
|
||||
private boolean closed;
|
||||
|
||||
MyTeamSpeakIdentity(byte[] id, long created, byte[] publicKey, byte[] encryptedKey,
|
||||
byte[] certificate, byte[] itemKey) throws GeneralSecurityException {
|
||||
if (id == null || id.length != 33 || publicKey == null || publicKey.length != 32
|
||||
|| encryptedKey == null || encryptedKey.length != 112
|
||||
|| certificate == null || certificate.length != 176)
|
||||
throw new GeneralSecurityException("Incomplete myTeamSpeak server identity");
|
||||
this.publicKey = publicKey.clone();
|
||||
scalar = MyTsCrypto.decryptItem(encryptedKey, itemKey);
|
||||
if (!MessageDigest.isEqual(point(scalar), publicKey)) {
|
||||
Arrays.fill(scalar, (byte) 0);
|
||||
throw new GeneralSecurityException("myTeamSpeak private key does not match its public key");
|
||||
}
|
||||
Base64.Encoder b64 = Base64.getEncoder();
|
||||
parameters = new LinkedHashMap<>();
|
||||
parameters.put("myTeamspeakId", b64.encodeToString(id));
|
||||
parameters.put("acTime", Long.toUnsignedString(Long.reverseBytes(created)));
|
||||
parameters.put("userPubKey", b64.encodeToString(publicKey));
|
||||
parameters.put("pubSign", b64.encodeToString(Arrays.copyOf(certificate, 64)));
|
||||
parameters.put("pubSignCert", b64.encodeToString(Arrays.copyOfRange(certificate, 64, 176)));
|
||||
}
|
||||
|
||||
/** Fresh signature for a handshake; closing the identity disables subsequent announcements. */
|
||||
public synchronized Map<String, String> authenticate(byte[] challenge) {
|
||||
if (closed) return Map.of();
|
||||
if (challenge.length != 32) throw new IllegalArgumentException("Expected a 32-byte connection challenge");
|
||||
byte[] message = Arrays.copyOf(challenge, 45);
|
||||
System.arraycopy("MyTeamSpeakID".getBytes(StandardCharsets.US_ASCII), 0, message, 32, 13);
|
||||
byte[] r = null, k = null, s = new byte[32];
|
||||
try {
|
||||
MessageDigest hash = MessageDigest.getInstance("SHA-512");
|
||||
hash.update(scalar);
|
||||
r = hash.digest(message);
|
||||
ScalarOperations.sc_reduce(r);
|
||||
byte[] R = point(r);
|
||||
hash.update(R);
|
||||
hash.update(publicKey);
|
||||
k = hash.digest(message);
|
||||
ScalarOperations.sc_reduce(k);
|
||||
ScalarOperations.sc_muladd(s, k, scalar, r);
|
||||
byte[] signature = Arrays.copyOf(R, 64);
|
||||
System.arraycopy(s, 0, signature, 32, 32);
|
||||
Map<String, String> result = new LinkedHashMap<>(parameters);
|
||||
result.put("authSign", Base64.getEncoder().encodeToString(signature));
|
||||
return result;
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new IllegalStateException("SHA-512 unavailable", e);
|
||||
} finally {
|
||||
if (r != null) Arrays.fill(r, (byte) 0);
|
||||
if (k != null) Arrays.fill(k, (byte) 0);
|
||||
Arrays.fill(s, (byte) 0);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] point(byte[] scalar) {
|
||||
GroupElementP3 point = new GroupElementP3();
|
||||
GroupOperations.ge_scalarmult_base(point, scalar, 0);
|
||||
byte[] encoded = new byte[32];
|
||||
GroupOperations.ge_p3_tobytes(encoded, 0, point);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
closed = true;
|
||||
Arrays.fill(scalar, (byte) 0);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,14 @@ public final class MyTeamSpeakLogin {
|
||||
private final MyTeamSpeak service;
|
||||
private MyTeamSpeak.Credentials credentials;
|
||||
private String username = "";
|
||||
private MyTeamSpeakIdentity serverIdentity;
|
||||
private final Object identityLoadLock = new Object();
|
||||
private long revision;
|
||||
|
||||
/** Changes whenever the signed-in account changes, including sign-out. */
|
||||
public synchronized long revision() {
|
||||
return revision;
|
||||
}
|
||||
|
||||
MyTeamSpeakLogin(File file, MyTeamSpeak service) {
|
||||
this.file = file;
|
||||
@@ -57,7 +65,10 @@ public final class MyTeamSpeakLogin {
|
||||
MyTeamSpeak.Credentials fresh = MyTeamSpeak.Credentials.derive(email.trim(), password);
|
||||
MyTeamSpeak.Account account = service.download(fresh);
|
||||
synchronized (this) {
|
||||
if (serverIdentity != null) serverIdentity.close();
|
||||
serverIdentity = null;
|
||||
credentials = fresh;
|
||||
revision++;
|
||||
username = account.username();
|
||||
write();
|
||||
}
|
||||
@@ -104,8 +115,40 @@ public final class MyTeamSpeakLogin {
|
||||
return service;
|
||||
}
|
||||
|
||||
/** Blocking account request on first use; cached in memory until sign-out. */
|
||||
public MyTeamSpeakIdentity serverIdentity() throws IOException, MyTsException {
|
||||
synchronized (identityLoadLock) {
|
||||
MyTeamSpeak.Credentials current;
|
||||
synchronized (this) {
|
||||
if (credentials == null) return null;
|
||||
if (serverIdentity != null) return serverIdentity;
|
||||
current = credentials;
|
||||
}
|
||||
MyTeamSpeakIdentity loaded;
|
||||
try {
|
||||
loaded = service.serverIdentity(current);
|
||||
} catch (MyTsException e) {
|
||||
synchronized (this) {
|
||||
if (e.credentialsRejected() && credentials == current) signOut();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
synchronized (this) {
|
||||
if (credentials != current) {
|
||||
loaded.close();
|
||||
return null;
|
||||
}
|
||||
serverIdentity = loaded;
|
||||
return loaded;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void signOut() {
|
||||
if (serverIdentity != null) serverIdentity.close();
|
||||
serverIdentity = null;
|
||||
credentials = null;
|
||||
revision++;
|
||||
username = "";
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.delete();
|
||||
|
||||
@@ -20,6 +20,8 @@ import eu.lixko.ts3j.client.chatlog.ChatLogStore;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
import eu.lixko.ts3j.client.contacts.Contact;
|
||||
import eu.lixko.ts3j.client.contacts.ContactStore;
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakIdentity;
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin;
|
||||
import eu.lixko.ts3j.client.net.avatar.Avatar;
|
||||
import eu.lixko.ts3j.client.net.avatar.AvatarNaming;
|
||||
import eu.lixko.ts3j.client.net.avatar.AvatarRepository;
|
||||
@@ -168,6 +170,51 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
private volatile long lastMutedTalkNanos;
|
||||
/** The server's error id for why the last connect was refused; 0 when it was not. */
|
||||
private volatile int connectErrorId;
|
||||
private volatile MyTeamSpeakLogin myTeamSpeak;
|
||||
private volatile boolean myTeamSpeakFeatures = true;
|
||||
private final Object myTeamSpeakUpdateLock = new Object();
|
||||
|
||||
public void setMyTeamSpeakLogin(MyTeamSpeakLogin login) {
|
||||
myTeamSpeak = login;
|
||||
}
|
||||
|
||||
public void setMyTeamSpeakFeatures(boolean enabled) {
|
||||
myTeamSpeakFeatures = enabled;
|
||||
}
|
||||
|
||||
public boolean isMyTeamSpeakFeaturesEnabled() {
|
||||
return myTeamSpeakFeatures;
|
||||
}
|
||||
|
||||
/** Refreshes account authentication after sign-in/out while connected. */
|
||||
public void updateMyTeamSpeak() {
|
||||
LocalTeamspeakClientSocket socket = client;
|
||||
ExecutorService executor = actions;
|
||||
if (!connected || socket == null || executor == null) return;
|
||||
try {
|
||||
executor.execute(() -> {
|
||||
synchronized (myTeamSpeakUpdateLock) {
|
||||
try {
|
||||
var login = myTeamSpeak;
|
||||
var account = myTeamSpeakFeatures && login != null ? login.serverIdentity() : null;
|
||||
if (client != socket || !connected) return;
|
||||
socket.setMyTeamSpeakAuthenticator(account == null ? null : account::authenticate);
|
||||
SingleCommand command = new SingleCommand("updatemytsid", ProtocolRole.CLIENT);
|
||||
socket.addMyTeamSpeakParameters(command);
|
||||
if (command.toMap().isEmpty()) {
|
||||
for (String name : new String[] {"myTeamspeakId", "acTime", "userPubKey", "pubSign", "pubSignCert", "authSign"})
|
||||
command.add(new CommandSingleParameter(name, ""));
|
||||
}
|
||||
socket.executeCommand(command).complete();
|
||||
} catch (Exception e) {
|
||||
ui.onError("Could not update myTeamSpeak identity: " + messageChain(e));
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (RejectedExecutionException ignored) {
|
||||
// The connection closed while scheduling the update.
|
||||
}
|
||||
}
|
||||
|
||||
public TeamspeakConnection(Settings settings, AudioBackend audio, ConnectionListener ui,
|
||||
SoundNotifier sounds, ContactStore contacts) {
|
||||
@@ -289,6 +336,16 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
connected = false;
|
||||
safeCleanup();
|
||||
try {
|
||||
var login = myTeamSpeak;
|
||||
long loginRevision = login == null ? 0 : login.revision();
|
||||
MyTeamSpeakIdentity account = null;
|
||||
try {
|
||||
if (myTeamSpeakFeatures && login != null) account = login.serverIdentity();
|
||||
} catch (Exception e) {
|
||||
// The server connection does not depend on myTeamSpeak being reachable.
|
||||
ui.onError("Connecting without myTeamSpeak identity: " + messageChain(e));
|
||||
}
|
||||
if (connectAttempt != attempt) return;
|
||||
identity = withIdentity;
|
||||
serverHost = address;
|
||||
serverPort = port;
|
||||
@@ -321,6 +378,7 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
microphone.setMutedTalkListener(this::onTalkingWhileMuted);
|
||||
|
||||
client = new LocalTeamspeakClientSocket();
|
||||
client.setMyTeamSpeakAuthenticator(account == null ? null : account::authenticate);
|
||||
eventExecutor = Executors.newSingleThreadExecutor(r -> {
|
||||
Thread t = new Thread(r, "ts3j-events");
|
||||
t.setDaemon(true);
|
||||
@@ -374,6 +432,7 @@ public final class TeamspeakConnection implements TS3Listener {
|
||||
icons.retryFailed();
|
||||
avatars.retryFailed();
|
||||
connected = true;
|
||||
if (login != null && login.revision() != loginRevision) updateMyTeamSpeak();
|
||||
connectedAtMs = System.currentTimeMillis();
|
||||
leaving = false;
|
||||
disconnectAnnounced = false;
|
||||
|
||||
@@ -152,6 +152,7 @@ public final class ServerSession {
|
||||
if (target == null) return null;
|
||||
Bookmark b = new Bookmark(title, target.address(), target.port(), target.nickname(), target.password());
|
||||
b.identityId = identityId;
|
||||
b.myTeamSpeakFeatures = conn.isMyTeamSpeakFeaturesEnabled();
|
||||
b.channel = channel == null ? "" : channel;
|
||||
return b;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user