carlos / contra Public

Clone
git clone https://amadan.net/carlos/contra

Plain git — no account needed to clone.

Download

Download this file

1package store
2
3import (
4 "context"
5 "time"
6
7 "gorm.io/gorm"
8
9 "github.com/carlosframework/contra/internal/contra/crypto"
10 "github.com/carlosframework/contra/internal/contra/model"
11)
12
13// ReplaceRecoveryWrap swaps an account's recovery branch for a new one: a new
14// wrap of RK, and the login public key that goes with it.
15//
16// # Why this one deletes
17//
18// Everything else in this package is additive. PROTOCOL.md says the recovery
19// code is the exception, and says why: only its wrap is stored, never the
20// value, so it cannot be *reprinted* — it can only be *regenerated*. A new
21// sheet means a new code, and the old sheet has to stop working, or "reprint"
22// would quietly leave two live keys to the vault with one of them lost
23// somewhere. The old wrap and the old login key go in the same transaction as
24// the new ones arrive.
25//
26// The copy in front of this says so before the person commits. That is not
27// decoration: it is the difference between a feature and a data-loss bug.
28//
29// # What it does not do
30//
31// It never touches the passphrase wrap. PROTOCOL.md's rule is that W_pw and
32// W_rc are never removed — losing a passkey or a device must not strand
33// anybody — so a failure part-way through this must never be able to leave an
34// account with no way in. One transaction, and the passphrase branch is not
35// in it.
36func (s *Store) ReplaceRecoveryWrap(ctx context.Context, accountID, wrapID []byte,
37 kdfParams string, version uint64, kcv []byte, sealed Sealed, loginPublicKey []byte) error {
38 if !sealed.valid() || len(kcv) != 16 || len(wrapID) != 16 || len(accountID) != 16 {
39 return ErrShortField
40 }
41 if len(loginPublicKey) != 65 || loginPublicKey[0] != 0x04 {
42 return ErrShortField
43 }
44
45 loginID, err := NewID()
46 if err != nil {
47 return err
48 }
49
50 return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
51 // Refuse to work on an account that does not exist, rather than
52 // leaving a wrap belonging to nobody.
53 var count int64
54 if err := tx.Model(&model.Account{}).
55 Where("id = ?", accountID).Count(&count).Error; err != nil {
56 return err
57 }
58 if count == 0 {
59 return gorm.ErrRecordNotFound
60 }
61
62 if err := tx.Where("account_id = ? AND kind = ?", accountID, crypto.ObjectWrapRC).
63 Delete(&model.Wrap{}).Error; err != nil {
64 return err
65 }
66 if err := tx.Where("account_id = ? AND branch = ?", accountID, "rc").
67 Delete(&model.LoginKey{}).Error; err != nil {
68 return err
69 }
70
71 now := time.Now().UTC()
72 if err := tx.Create(&model.Wrap{
73 ID: wrapID,
74 AccountID: accountID,
75 Kind: crypto.ObjectWrapRC,
76 FormatVersion: FormatVersion,
77 Version: version,
78 KDFParams: kdfParams,
79 KCV: kcv,
80 Nonce: sealed.Nonce,
81 Ciphertext: sealed.Ciphertext,
82 CreatedAt: now,
83 }).Error; err != nil {
84 return err
85 }
86 return tx.Create(&model.LoginKey{
87 ID: loginID,
88 AccountID: accountID,
89 Branch: "rc",
90 PublicKey: loginPublicKey,
91 CreatedAt: now,
92 }).Error
93 })
94}
95