build / windows (push) Skipped
build / other_os (push) Skipped
build / mac_amd64 (push) Skipped
build / mac_arm64 (push) Skipped
build / linux (push) Skipped
build / go1.26 (push) Skipped
build / linux_386 (push) Skipped
build / lint (push) Skipped
build / android-all (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/386 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/amd64 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v6 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v7 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm64 (push) Skipped
Build & Push Docker Images / Merge & Push Final Docker Image (push) Waiting for needed jobs
Replace Proton-API-Bridge and go-proton-api with a client in backend/protondrive written against Proton's current Drive API, following the behaviour of Proton's official Drive SDK: - volume-based and v2 routes only - upload block verification, signed manifests and extended attributes - current content encryption (v4 node keys, v6 PKESK, SEIPDv2) - browser (session fork) sign-in, with username and password still supported - the volume's change events, used to keep the metadata cache current and for ChangeNotify, polled at most every 30 s - an in-memory fake Proton server (protontest) so the tests run without an account Uses ProtonMail/gopenpgp/v3 and ProtonMail/go-srp directly; no cgo. Written with heavy AI assistance (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
340 lines
12 KiB
Go
340 lines
12 KiB
Go
package protontest
|
|
|
|
// The Drive cryptography of the fake, written from Proton's Drive SDK
|
|
// (client/js/src/crypto/driveCrypto.ts and the nodes, shares and upload
|
|
// crypto services) independently of the protondrive backend, so a bug
|
|
// in one is unlikely to be mirrored in the other.
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
|
"github.com/ProtonMail/gopenpgp/v3/constants"
|
|
"github.com/ProtonMail/gopenpgp/v3/crypto"
|
|
"github.com/ProtonMail/gopenpgp/v3/profile"
|
|
)
|
|
|
|
// driveKeyName is the user ID of every node and share key, as the SDK
|
|
// generates them.
|
|
const driveKeyName = "Drive key"
|
|
|
|
// aeadPGP returns a handle whose keys advertise SEIPDv2 and whose
|
|
// session-key encryption uses AES-256 GCM, as the SDK's file content
|
|
// does (openpgp.js aeadProtect with Proton's GCM preference).
|
|
//
|
|
// Everything that is not file content goes through encryptArmored,
|
|
// which never produces SEIPDv2 whatever the recipient advertises.
|
|
func aeadPGP() *crypto.PGPHandle {
|
|
p := profile.Default()
|
|
p.AeadEncryption = &packet.AEADConfig{DefaultMode: packet.AEADModeGCM}
|
|
return crypto.PGPWithProfile(p)
|
|
}
|
|
|
|
// signTime returns when the fake generates keys and makes signatures: a
|
|
// day before the local clock, so that what it writes is valid at the
|
|
// server clock whatever skew within a day a test sets.
|
|
func signTime() int64 {
|
|
return time.Now().Add(-24 * time.Hour).Unix()
|
|
}
|
|
|
|
// signTimeOf returns when the fake signs with signer: signTime, or
|
|
// signer's creation time if later, as for a key a test supplied which
|
|
// was generated less than a day ago.
|
|
func signTimeOf(signer *crypto.Key) int64 {
|
|
return max(signTime(), signer.GetEntity().PrimaryKey.CreationTime.Unix())
|
|
}
|
|
|
|
// newPassphrase returns base64 of 32 random bytes, the SDK's
|
|
// generatePassphrase.
|
|
func newPassphrase() []byte {
|
|
return []byte(base64.StdEncoding.EncodeToString(randomBytes(32)))
|
|
}
|
|
|
|
// lockedKey is a generated key with its passphrase and armored locked
|
|
// form.
|
|
type lockedKey struct {
|
|
key *crypto.Key
|
|
passphrase []byte
|
|
armored string
|
|
}
|
|
|
|
// newDriveKey generates a node or share key locked with a new
|
|
// passphrase. aead makes the key advertise SEIPDv2, as file node keys
|
|
// do; folder and share keys do not.
|
|
func newDriveKey(aead bool) lockedKey {
|
|
pgp := crypto.PGP()
|
|
if aead {
|
|
pgp = aeadPGP()
|
|
}
|
|
key, err := pgp.KeyGeneration().AddUserId(driveKeyName, "").GenerationTime(signTime()).New().GenerateKey()
|
|
mustCrypto("generate drive key", err)
|
|
passphrase := newPassphrase()
|
|
return lockedKey{key: key, passphrase: passphrase, armored: lockKey(key, passphrase)}
|
|
}
|
|
|
|
// sealedPassphrase is a node or share passphrase as the server stores it.
|
|
type sealedPassphrase struct {
|
|
armored string // encrypted to the parent key
|
|
signature string // armored detached signature of the plaintext
|
|
}
|
|
|
|
// sealPassphrase encrypts passphrase to parent without AEAD and signs it
|
|
// with signer in a detached signature, the SDK's encryptPassphrase.
|
|
func sealPassphrase(passphrase []byte, parent, signer *crypto.Key) sealedPassphrase {
|
|
return sealedPassphrase{
|
|
armored: encryptArmored(passphrase, parent, nil, false),
|
|
signature: signArmored(passphrase, signer),
|
|
}
|
|
}
|
|
|
|
// encryptArmored encrypts data to key without AEAD, signed inline by
|
|
// signer if not nil, compressed if compress is set.
|
|
//
|
|
// Encrypting to recipients picks SEIPDv2 whenever every recipient key
|
|
// advertises it, as file node keys do, so the session key is generated
|
|
// and wrapped separately: the SDK's ignoreSEIPDv2FeatureFlag.
|
|
func encryptArmored(data []byte, key, signer *crypto.Key, compress bool) string {
|
|
sk, err := crypto.GenerateSessionKeyAlgo(constants.AES256)
|
|
mustCrypto("session key", err)
|
|
kh, err := crypto.PGP().Encryption().Recipient(key).New()
|
|
mustCrypto("encrypt session key", err)
|
|
keyPacket, err := kh.EncryptSessionKey(sk)
|
|
mustCrypto("encrypt session key", err)
|
|
b := crypto.PGP().Encryption().SessionKey(sk)
|
|
if signer != nil {
|
|
b = b.SigningKey(signer).SignTime(signTimeOf(signer))
|
|
}
|
|
if compress {
|
|
b = b.Compress()
|
|
}
|
|
eh, err := b.New()
|
|
mustCrypto("encrypt", err)
|
|
msg, err := eh.Encrypt(data)
|
|
mustCrypto("encrypt", err)
|
|
armored, err := crypto.NewPGPMessage(append(keyPacket, msg.DataPacket...)).Armor()
|
|
mustCrypto("armor message", err)
|
|
return armored
|
|
}
|
|
|
|
// signArmored returns an armored detached signature of data by signer.
|
|
func signArmored(data []byte, signer *crypto.Key) string {
|
|
sh, err := crypto.PGP().Sign().SigningKey(signer).Detached().SignTime(signTimeOf(signer)).New()
|
|
mustCrypto("sign", err)
|
|
sig, err := sh.Sign(data, crypto.Armor)
|
|
mustCrypto("sign", err)
|
|
return string(sig)
|
|
}
|
|
|
|
// signBinary returns a binary detached signature of data by signer.
|
|
func signBinary(data []byte, signer *crypto.Key) []byte {
|
|
sh, err := crypto.PGP().Sign().SigningKey(signer).Detached().SignTime(signTimeOf(signer)).New()
|
|
mustCrypto("sign", err)
|
|
sig, err := sh.Sign(data, crypto.Bytes)
|
|
mustCrypto("sign", err)
|
|
return sig
|
|
}
|
|
|
|
// newHashKey returns a new folder hash key and its armored form,
|
|
// encrypted to and signed by nodeKey, the SDK's generateHashKey.
|
|
func newHashKey(nodeKey *crypto.Key) (hashKey []byte, armored string) {
|
|
hashKey = newPassphrase()
|
|
return hashKey, encryptArmored(hashKey, nodeKey, nodeKey, false)
|
|
}
|
|
|
|
// lookupHash returns the hex HMAC-SHA256 of name keyed with the parent
|
|
// folder's hash key, the SDK's generateLookupHash.
|
|
func lookupHash(parentHashKey []byte, name string) string {
|
|
mac := hmac.New(sha256.New, parentHashKey)
|
|
mac.Write([]byte(name))
|
|
return hex.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
// contentKey is a file's content session key with its key packet.
|
|
type contentKey struct {
|
|
sk *crypto.SessionKey
|
|
packet string // base64 PKESK to the node key
|
|
signature string // armored detached signature of the session key by the node key
|
|
aead bool
|
|
}
|
|
|
|
// newContentKey generates a content session key for nodeKey, the SDK's
|
|
// generateContentKey. With aead the key packet is a v6 PKESK and blocks
|
|
// are SEIPDv2; without, the legacy v3 PKESK and SEIPDv1.
|
|
func newContentKey(nodeKey *crypto.Key, aead bool) contentKey {
|
|
token := randomBytes(32)
|
|
sk := crypto.NewSessionKeyFromTokenWithAead(token, constants.AES256, aead)
|
|
eh, err := crypto.PGP().Encryption().Recipient(nodeKey).New()
|
|
mustCrypto("content key packet", err)
|
|
keyPacket, err := eh.EncryptSessionKey(sk)
|
|
mustCrypto("content key packet", err)
|
|
return contentKey{
|
|
sk: sk,
|
|
packet: base64.StdEncoding.EncodeToString(keyPacket),
|
|
signature: signArmored(sk.Key, nodeKey),
|
|
aead: aead,
|
|
}
|
|
}
|
|
|
|
// pgp returns the handle for encrypting with ck.
|
|
func (ck contentKey) pgp() *crypto.PGPHandle {
|
|
if ck.aead {
|
|
return aeadPGP()
|
|
}
|
|
return crypto.PGP()
|
|
}
|
|
|
|
// encryptBlock encrypts one block of plaintext with the content key and
|
|
// returns the data packet and the armored block signature: a detached
|
|
// signature by signer encrypted to nodeKey with the content key, the
|
|
// SDK's encryptBlock.
|
|
func (ck contentKey) encryptBlock(plain []byte, nodeKey, signer *crypto.Key) (data []byte, encSignature string) {
|
|
eh, err := ck.pgp().Encryption().SessionKey(ck.sk).New()
|
|
mustCrypto("encrypt block", err)
|
|
msg, err := eh.Encrypt(plain)
|
|
mustCrypto("encrypt block", err)
|
|
|
|
sigHandle, err := ck.pgp().Encryption().Recipient(nodeKey).SessionKey(ck.sk).New()
|
|
mustCrypto("encrypt block signature", err)
|
|
sigMsg, err := sigHandle.Encrypt(signBinary(plain, signer))
|
|
mustCrypto("encrypt block signature", err)
|
|
encSignature, err = sigMsg.Armor()
|
|
mustCrypto("armor block signature", err)
|
|
return msg.DataPacket, encSignature
|
|
}
|
|
|
|
// keyRing returns a key ring of keys, which must not be empty.
|
|
func keyRing(keys ...*crypto.Key) *crypto.KeyRing {
|
|
kr, err := crypto.NewKeyRing(nil)
|
|
mustCrypto("key ring", err)
|
|
for _, k := range keys {
|
|
mustCrypto("key ring", kr.AddKey(k))
|
|
}
|
|
return kr
|
|
}
|
|
|
|
// errSignature wraps every signature verification failure.
|
|
var errSignature = errors.New("protontest: bad signature")
|
|
|
|
// decryptArmored decrypts an armored message with key. If verify is not
|
|
// nil the embedded signature must be valid for one of its keys at time
|
|
// at, or now if at is zero.
|
|
func decryptArmored(armored string, key *crypto.Key, verify *crypto.KeyRing, at time.Time) ([]byte, error) {
|
|
b := crypto.PGP().Decryption().DecryptionKey(key)
|
|
if verify != nil {
|
|
b = b.VerificationKeys(verify)
|
|
if !at.IsZero() {
|
|
b = b.VerifyTime(at.Unix())
|
|
}
|
|
}
|
|
dh, err := b.New()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := dh.Decrypt([]byte(armored), crypto.Armor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if verify != nil {
|
|
if err := res.SignatureError(); err != nil {
|
|
return res.Bytes(), fmt.Errorf("%w: %v", errSignature, err)
|
|
}
|
|
}
|
|
return res.Bytes(), nil
|
|
}
|
|
|
|
// verifyDetached checks a detached signature, armored or binary per
|
|
// encoding, of data against verify at time at, or now if at is zero.
|
|
func verifyDetached(data, sig []byte, encoding int8, verify *crypto.KeyRing, at time.Time) error {
|
|
b := crypto.PGP().Verify().VerificationKeys(verify)
|
|
if !at.IsZero() {
|
|
b = b.VerifyTime(at.Unix())
|
|
}
|
|
vh, err := b.New()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res, err := vh.VerifyDetached(data, sig, encoding)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: %v", errSignature, err)
|
|
}
|
|
if err := res.SignatureError(); err != nil {
|
|
return fmt.Errorf("%w: %v", errSignature, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// unlockFunc unlocks an armored private key with a passphrase.
|
|
type unlockFunc func(armoredKey string, passphrase []byte) (*crypto.Key, error)
|
|
|
|
// unlockDriveKey decrypts a node or share key: the passphrase with
|
|
// parent, its detached signature against verify, then the key itself
|
|
// with unlock. A bad signature is returned alongside the unlocked key.
|
|
// Signatures are checked at time at, or now if it is zero.
|
|
func unlockDriveKey(armoredKey string, sealed sealedPassphrase, parent *crypto.Key, verify *crypto.KeyRing, unlock unlockFunc, at time.Time) (*crypto.Key, error) {
|
|
passphrase, err := decryptArmored(sealed.armored, parent, nil, at)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("decrypt passphrase: %w", err)
|
|
}
|
|
key, err := unlock(armoredKey, passphrase)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unlock key: %w", err)
|
|
}
|
|
if err := verifyDetached(passphrase, []byte(sealed.signature), crypto.Armor, verify, at); err != nil {
|
|
return key, fmt.Errorf("passphrase: %w", err)
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
// decryptContentKey decrypts a base64 content key packet with nodeKey
|
|
// and checks its signature against nodeKey and verify at time at.
|
|
func decryptContentKey(packet64, signature string, nodeKey *crypto.Key, verify *crypto.KeyRing, at time.Time) (*crypto.SessionKey, error) {
|
|
keyPacket, err := base64.StdEncoding.DecodeString(packet64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("content key packet: %w", err)
|
|
}
|
|
dh, err := crypto.PGP().Decryption().DecryptionKey(nodeKey).New()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sk, err := dh.DecryptSessionKey(keyPacket)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("content key packet: %w", err)
|
|
}
|
|
kr := keyRing(nodeKey)
|
|
for _, k := range verify.GetKeys() {
|
|
mustCrypto("key ring", kr.AddKey(k))
|
|
}
|
|
if err := verifyDetached(sk.Key, []byte(signature), crypto.Armor, kr, at); err != nil {
|
|
return sk, fmt.Errorf("content key packet: %w", err)
|
|
}
|
|
return sk, nil
|
|
}
|
|
|
|
// decryptBlock decrypts a block's data packet with sk and checks its
|
|
// encrypted signature, decrypted with nodeKey, against verify at time
|
|
// at.
|
|
func decryptBlock(data []byte, encSignature string, sk *crypto.SessionKey, nodeKey *crypto.Key, verify *crypto.KeyRing, at time.Time) ([]byte, error) {
|
|
dh, err := crypto.PGP().Decryption().SessionKey(sk).New()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := dh.Decrypt(data, crypto.Bytes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("decrypt block: %w", err)
|
|
}
|
|
sig, err := decryptArmored(encSignature, nodeKey, nil, at)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("decrypt block signature: %w", err)
|
|
}
|
|
if err := verifyDetached(res.Bytes(), sig, crypto.Bytes, verify, at); err != nil {
|
|
return res.Bytes(), fmt.Errorf("block: %w", err)
|
|
}
|
|
return res.Bytes(), nil
|
|
}
|