Add E2EE recovery and device rotation

Add signed, encrypted recovery archives with revision checks, account-certified device enrollment and key rotation, and historical signer lookup. Include the database migration, API endpoints, tests, and updated E2EE documentation.
This commit is contained in:
Artemy
2026-10-24 16:54:56 +07:00
parent 2d99c57a3b
commit a049a80e04
15 changed files with 2259 additions and 130 deletions
@@ -0,0 +1,20 @@
using Govor.Domain.Models.Users;
using Govor.Domain.Models.Users.Crypto;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Govor.Domain.Configurations;
public class E2eeRecoveryVaultConfiguration : IEntityTypeConfiguration<E2eeRecoveryVault>
{
public void Configure(EntityTypeBuilder<E2eeRecoveryVault> builder)
{
builder.HasKey(v => v.UserId);
var b = builder;
b.Property(v => v.Revision).IsConcurrencyToken();
b.Property(v => v.IdentityKey).HasMaxLength(200);
b.Property(v => v.Signature).HasMaxLength(100);
b.Property(v => v.Ciphertext).HasMaxLength(4_000_000);
b.HasOne<User>().WithOne().HasForeignKey<E2eeRecoveryVault>(v => v.UserId).OnDelete(DeleteBehavior.Cascade);
}
}
@@ -9,6 +9,10 @@ public class UserCryptoSessionConfiguration : IEntityTypeConfiguration<UserCrypt
public void Configure(EntityTypeBuilder<UserCryptoSession> builder)
{
builder.HasKey(ucs => ucs.Id);
builder.Property(c => c.Generation).IsConcurrencyToken();
builder.Property(c => c.AccountIdentityKey).HasMaxLength(200);
builder.Property(c => c.AccountSignature).HasMaxLength(100);
builder.Property(c => c.PreviousFingerprint).HasMaxLength(64);
builder.HasOne(ucs => ucs.UserSession)
.WithOne(us => us.CryptoSession)
@@ -34,4 +38,4 @@ public class UserCryptoSessionConfiguration : IEntityTypeConfiguration<UserCrypt
builder.Property(ucs => ucs.PublicIdentityKey)
.IsRequired();
}
}
}
+2
View File
@@ -14,6 +14,7 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
public virtual DbSet<UserSession> UserSessions { get; set; }
public virtual DbSet<UserPushToken> UserPushTokens { get; set; }
public virtual DbSet<UserCryptoSession> UserCryptoSessions { get; set; }
public virtual DbSet<E2eeRecoveryVault> E2eeRecoveryVaults { get; set; }
public virtual DbSet<SignedPreKey> SignedPreKeys { get; set; }
public virtual DbSet<OneTimePreKey> OneTimePreKeys { get; set; }
public virtual DbSet<Friendship> Friendships { get; set; }
@@ -41,6 +42,7 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new E2eeRecoveryVaultConfiguration());
modelBuilder.ApplyConfiguration(new UserSessionConfiguration());
modelBuilder.ApplyConfiguration(new FriendshipConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,87 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Govor.Domain.Migrations
{
/// <inheritdoc />
public partial class E2eeRecoveryAndRotation : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "AccountIdentityKey",
table: "UserCryptoSessions",
type: "character varying(200)",
maxLength: 200,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "AccountSignature",
table: "UserCryptoSessions",
type: "character varying(100)",
maxLength: 100,
nullable: true);
migrationBuilder.AddColumn<long>(
name: "Generation",
table: "UserCryptoSessions",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddColumn<string>(
name: "PreviousFingerprint",
table: "UserCryptoSessions",
type: "character varying(64)",
maxLength: 64,
nullable: true);
migrationBuilder.CreateTable(
name: "E2eeRecoveryVaults",
columns: table => new
{
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Revision = table.Column<long>(type: "bigint", nullable: false),
IdentityKey = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
Ciphertext = table.Column<string>(type: "character varying(4000000)", maxLength: 4000000, nullable: false),
Signature = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_E2eeRecoveryVaults", x => x.UserId);
table.ForeignKey(
name: "FK_E2eeRecoveryVaults_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "E2eeRecoveryVaults");
migrationBuilder.DropColumn(
name: "AccountIdentityKey",
table: "UserCryptoSessions");
migrationBuilder.DropColumn(
name: "AccountSignature",
table: "UserCryptoSessions");
migrationBuilder.DropColumn(
name: "Generation",
table: "UserCryptoSessions");
migrationBuilder.DropColumn(
name: "PreviousFingerprint",
table: "UserCryptoSessions");
}
}
}
@@ -797,6 +797,35 @@ namespace Govor.Domain.Migrations
b.ToTable("Admins");
});
modelBuilder.Entity("Govor.Domain.Models.Users.Crypto.E2eeRecoveryVault", b =>
{
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.Property<string>("Ciphertext")
.IsRequired()
.HasMaxLength(4000000)
.HasColumnType("character varying(4000000)");
b.Property<string>("IdentityKey")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<long>("Revision")
.IsConcurrencyToken()
.HasColumnType("bigint");
b.Property<string>("Signature")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("UserId");
b.ToTable("E2eeRecoveryVaults");
});
modelBuilder.Entity("Govor.Domain.Models.Users.Crypto.OneTimePreKey", b =>
{
b.Property<Guid>("Id")
@@ -858,6 +887,22 @@ namespace Govor.Domain.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("AccountIdentityKey")
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<string>("AccountSignature")
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<long>("Generation")
.IsConcurrencyToken()
.HasColumnType("bigint");
b.Property<string>("PreviousFingerprint")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<byte[]>("PublicIdentityKey")
.IsRequired()
.HasColumnType("bytea");
@@ -1234,6 +1279,15 @@ namespace Govor.Domain.Migrations
b.Navigation("User");
});
modelBuilder.Entity("Govor.Domain.Models.Users.Crypto.E2eeRecoveryVault", b =>
{
b.HasOne("Govor.Domain.Models.Users.User", null)
.WithOne()
.HasForeignKey("Govor.Domain.Models.Users.Crypto.E2eeRecoveryVault", "UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Govor.Domain.Models.Users.Crypto.OneTimePreKey", b =>
{
b.HasOne("Govor.Domain.Models.Users.Crypto.UserCryptoSession", "UserCryptoSession")
@@ -0,0 +1,10 @@
namespace Govor.Domain.Models.Users.Crypto;
public sealed class E2eeRecoveryVault
{
public Guid UserId { get; set; }
public long Revision { get; set; }
public string IdentityKey { get; set; } = "";
public string Ciphertext { get; set; } = "";
public string Signature { get; set; } = "";
}
@@ -8,6 +8,10 @@ public class UserCryptoSession
public UserSession UserSession { get; set; }
public byte[] PublicIdentityKey { get; set; }
public string? AccountIdentityKey { get; set; }
public string? AccountSignature { get; set; }
public long Generation { get; set; }
public string? PreviousFingerprint { get; set; }
public SignedPreKey SignedPreKey { get; set; }
public ICollection<OneTimePreKey> OneTimePreKeys { get; set; }