AAD 只含租户,避免任何设置变更使已存密码失效;旧行在解密时按历史版本回退,无需重填授权码。
This commit is contained in:
@@ -163,7 +163,9 @@ func (s *Store) seal(secret string, revision int64) ([]byte, error) {
|
|||||||
if _, err = rand.Read(nonce); err != nil {
|
if _, err = rand.Read(nonce); err != nil {
|
||||||
return nil, ErrUnavailable
|
return nil, ErrUnavailable
|
||||||
}
|
}
|
||||||
return aead.Seal(nonce, nonce, []byte(secret), []byte(fmt.Sprintf("weekly-mail:%s:%d", s.tenant, revision))), nil
|
// The AAD must not follow the settings revision: rotating recipients would otherwise
|
||||||
|
// make a previously saved password undecryptable.
|
||||||
|
return aead.Seal(nonce, nonce, []byte(secret), []byte("weekly-mail:"+s.tenant)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) unseal(sealed []byte, revision int64) (string, error) {
|
func (s *Store) unseal(sealed []byte, revision int64) (string, error) {
|
||||||
@@ -182,11 +184,19 @@ func (s *Store) unseal(sealed []byte, revision int64) (string, error) {
|
|||||||
if err != nil || len(sealed) < aead.NonceSize() {
|
if err != nil || len(sealed) < aead.NonceSize() {
|
||||||
return "", ErrUnavailable
|
return "", ErrUnavailable
|
||||||
}
|
}
|
||||||
raw, err := aead.Open(nil, sealed[:aead.NonceSize()], sealed[aead.NonceSize():], []byte(fmt.Sprintf("weekly-mail:%s:%d", s.tenant, revision)))
|
nonce, body := sealed[:aead.NonceSize()], sealed[aead.NonceSize():]
|
||||||
if err != nil {
|
raw, err := aead.Open(nil, nonce, body, []byte("weekly-mail:"+s.tenant))
|
||||||
return "", ErrUnavailable
|
if err == nil {
|
||||||
|
return string(raw), nil
|
||||||
}
|
}
|
||||||
return string(raw), nil
|
// Legacy rows were sealed with the revision in the AAD; recover them without forcing
|
||||||
|
// the operator to paste the authorization code again.
|
||||||
|
for candidate := int64(1); candidate <= 8; candidate++ {
|
||||||
|
if plain, openErr := aead.Open(nil, nonce, body, []byte(fmt.Sprintf("weekly-mail:%s:%d", s.tenant, candidate))); openErr == nil {
|
||||||
|
return string(plain), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", ErrUnavailable
|
||||||
}
|
}
|
||||||
|
|
||||||
func validAddress(value string) bool {
|
func validAddress(value string) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user