public void RefreshTLSUser(string userId, byte[] staticPublicKey) { Guard.NotNull(userId, staticPublicKey); var user = new TLSUser(userId, staticPublicKey); this._usersById[userId] = user; }
void NewDynamicEncryptionSecret(TLSUser user) { var random = this.ixdsCryptoService.GetRandom(32).Result.X; var newKeyPair = this.ixdsCryptoService.GenerateCurve25519KeyPairExact(random).Result; var newDynamicKeyId = this._ratchetTimer.GetNextTicks(user.DynamicPrivateDecryptionKeys.Count > 0 ? user.DynamicPrivateDecryptionKeys.Keys.Max() : 0); Debug.Assert(user.LatestDynamicPublicKey != null && user.LatestDynamicPublicKeyId != 0, "The client always sends a dynamic public key, so we must have it."); var newDynamicSecret = this.ixdsCryptoService.CalculateAndHashSharedSecret(newKeyPair.PrivateKey, user.LatestDynamicPublicKey); user.DynamicSecret = new DynamicSecret(recipientId: null, dynamicSharedSecret: newDynamicSecret, dynamicPublicKey: newKeyPair.PublicKey, dynamicPublicKeyId: newDynamicKeyId, privateKeyHint: user.LatestDynamicPublicKeyId) { UseCount = 0 }; user.DynamicPrivateDecryptionKeys[newDynamicKeyId] = newKeyPair.PrivateKey; this._idsByPrivateKeyHint[newDynamicKeyId] = user.UserId; RemoveExcessKeys(user); }
// TODO: Review this, compare it with TLSCLient.RemovePreviousKeys and when key cleanup is done // This may not work correctly. void RemoveExcessKeys(TLSUser user) { var excess = user.DynamicPrivateDecryptionKeys.Keys.OrderByDescending(k => k).Skip(KeepLatestDynamicPrivateKeys); foreach (var keyId in excess) { user.DynamicPrivateDecryptionKeys.Remove(keyId); this._idsByPrivateKeyHint.Remove(keyId); } }
public TLSClientRatchet(string myId, byte[] myPrivateKey, TLSUser server, IXDSSecService ixdsCryptoService) { Guard.NotNull(myId, myPrivateKey, server, ixdsCryptoService); Guard.NotNull(server.StaticPublicKey); this.MyId = myId; this._myIdBytes = Encoding.UTF8.GetBytes(this.MyId); this._server = server; this.ixdsCryptoService = ixdsCryptoService; this._server.AuthSecret = this.ixdsCryptoService.CalculateAndHashSharedSecret(myPrivateKey, this._server.StaticPublicKey); }