forked from henrybear327/Proton-API-Bridge
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcrypto_block_test.go
More file actions
233 lines (211 loc) · 6.6 KB
/
Copy pathcrypto_block_test.go
File metadata and controls
233 lines (211 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package proton_api_bridge
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"io"
"strings"
"testing"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/gopenpgp/v3/crypto"
proton "github.com/rclone/go-proton-api"
)
func hashB64(b []byte) string {
h := sha256.New()
h.Write(b)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// helper: makes an addrKR (a normal signing/encryption keypair) for tests.
func newAddrKR(t *testing.T) *crypto.KeyRing {
t.Helper()
pgp := crypto.PGP()
k, err := pgp.KeyGeneration().AddUserId("test", "test@example.com").New().GenerateKey()
if err != nil {
t.Fatalf("addr key gen: %v", err)
}
kr, err := crypto.NewKeyRing(k)
if err != nil {
t.Fatalf("addr keyring: %v", err)
}
return kr
}
// helper: produces a fresh nodeKR.
func newNodeKR(t *testing.T) *crypto.KeyRing {
t.Helper()
pgp := crypto.PGP()
k, err := pgp.KeyGeneration().AddUserId("node", "node@example.com").New().GenerateKey()
if err != nil {
t.Fatalf("node key gen: %v", err)
}
kr, err := crypto.NewKeyRing(k)
if err != nil {
t.Fatalf("node keyring: %v", err)
}
return kr
}
// TestBlockEncryptDecryptRoundTrip mirrors the upload + download crypto flow.
// It is the regression test for: "gopenpgp: error in reading data message:
// openpgp: invalid data: tag byte does not have MSB set" on download.
func TestBlockEncryptDecryptRoundTrip(t *testing.T) {
pgp := crypto.PGP()
addrKR := newAddrKR(t)
nodeKR := newNodeKR(t)
// Build a fresh session key (as createFileUploadDraft does).
sessionKey, err := pgp.GenerateSessionKey()
if err != nil {
t.Fatalf("gen session key: %v", err)
}
// Pretend block payload — anything that wouldn't itself happen to look
// like a valid PGP packet header.
plaintext := []byte(strings.Repeat("hello proton drive ", 1000))
// --- Upload path: encrypt block + sign + encrypt the signature ---
sessionEnc, err := pgp.Encryption().SessionKey(sessionKey).New()
if err != nil {
t.Fatalf("session enc: %v", err)
}
encMsg, err := sessionEnc.Encrypt(plaintext)
if err != nil {
t.Fatalf("encrypt block: %v", err)
}
encData := encMsg.Bytes()
signHandle, err := pgp.Sign().SigningKeys(addrKR).Detached().New()
if err != nil {
t.Fatalf("sign handle: %v", err)
}
rawSig, err := signHandle.Sign(plaintext, crypto.Bytes)
if err != nil {
t.Fatalf("sign: %v", err)
}
sigEncHandle, err := pgp.Encryption().Recipients(nodeKR).New()
if err != nil {
t.Fatalf("sig enc handle: %v", err)
}
encSig, err := sigEncHandle.Encrypt(rawSig)
if err != nil {
t.Fatalf("encrypt sig: %v", err)
}
encSigArmor, err := encSig.Armor()
if err != nil {
t.Fatalf("armor sig: %v", err)
}
// --- Download path: feed exactly what the server would store back into
// decryptBlockIntoBuffer and check the round-trip. ---
var out bytes.Buffer
err = decryptBlockIntoBuffer(
sessionKey,
addrKR,
nodeKR,
hashB64(encData),
encSigArmor,
&out,
io.NopCloser(bytes.NewReader(encData)),
)
if err != nil {
t.Fatalf("decryptBlockIntoBuffer: %v", err)
}
if !bytes.Equal(out.Bytes(), plaintext) {
t.Fatalf("round-trip plaintext mismatch (len got %d want %d)", out.Len(), len(plaintext))
}
}
// readFirstPacket parses the first OpenPGP packet from b.
func readFirstPacket(t *testing.T, b []byte) packet.Packet {
t.Helper()
p, err := packet.Read(bytes.NewReader(b))
if err != nil {
t.Fatalf("read packet: %v", err)
}
return p
}
// TestBlockEncryptCryptoRefresh checks that the upload path produces Proton
// Drive's new crypto-refresh (RFC 9580) file-content format: a v6 PKESK content
// key packet and a v2 SEIPD data packet encrypted with AES-256-GCM. It also
// round-trips the block back through decryptBlockIntoBuffer to confirm the new
// format is still readable by the download path.
func TestBlockEncryptCryptoRefresh(t *testing.T) {
pgp := crypto.PGP()
addrKR := newAddrKR(t)
nodeKR := newNodeKR(t)
// Build the content key packet the way go-proton-api does for an upload.
// This yields the v6 session key and populates ContentKeyPacket with the
// v6 PKESK.
var req proton.CreateFileReq
sessionKey, err := req.SetContentKeyPacketAndSignature(nodeKR)
if err != nil {
t.Fatalf("SetContentKeyPacketAndSignature: %v", err)
}
// The content key packet must be a v6 PKESK.
contentKeyPacket, err := base64.StdEncoding.DecodeString(req.ContentKeyPacket)
if err != nil {
t.Fatalf("decode content key packet: %v", err)
}
ek, ok := readFirstPacket(t, contentKeyPacket).(*packet.EncryptedKey)
if !ok {
t.Fatalf("content key packet: not a PKESK packet")
}
if ek.Version != 6 {
t.Fatalf("content key packet: got PKESK version %d, want 6", ek.Version)
}
plaintext := []byte(strings.Repeat("hello proton drive ", 1000))
// --- Upload path: encrypt block (new format) + sign + encrypt signature ---
sessionEnc, err := protonDrivePGP().Encryption().SessionKey(sessionKey).New()
if err != nil {
t.Fatalf("session enc: %v", err)
}
encMsg, err := sessionEnc.Encrypt(plaintext)
if err != nil {
t.Fatalf("encrypt block: %v", err)
}
encData := encMsg.Bytes()
// The block data packet must be a v2 SEIPD using AES-256-GCM.
se, ok := readFirstPacket(t, encData).(*packet.SymmetricallyEncrypted)
if !ok {
t.Fatalf("block data: got %T, want *packet.SymmetricallyEncrypted", readFirstPacket(t, encData))
}
if se.Version != 2 {
t.Fatalf("block data: got SEIPD version %d, want 2", se.Version)
}
if se.Mode != packet.AEADModeGCM {
t.Fatalf("block data: got AEAD mode %d, want GCM (%d)", se.Mode, packet.AEADModeGCM)
}
if se.Cipher != packet.CipherAES256 {
t.Fatalf("block data: got cipher %d, want AES-256 (%d)", se.Cipher, packet.CipherAES256)
}
// Signature path is unchanged (default profile).
signHandle, err := pgp.Sign().SigningKeys(addrKR).Detached().New()
if err != nil {
t.Fatalf("sign handle: %v", err)
}
rawSig, err := signHandle.Sign(plaintext, crypto.Bytes)
if err != nil {
t.Fatalf("sign: %v", err)
}
sigEncHandle, err := pgp.Encryption().Recipients(nodeKR).New()
if err != nil {
t.Fatalf("sig enc handle: %v", err)
}
encSig, err := sigEncHandle.Encrypt(rawSig)
if err != nil {
t.Fatalf("encrypt sig: %v", err)
}
encSigArmor, err := encSig.Armor()
if err != nil {
t.Fatalf("armor sig: %v", err)
}
// --- Download path: the new format must still decrypt cleanly. ---
var out bytes.Buffer
err = decryptBlockIntoBuffer(
sessionKey,
addrKR,
nodeKR,
hashB64(encData),
encSigArmor,
&out,
io.NopCloser(bytes.NewReader(encData)),
)
if err != nil {
t.Fatalf("decryptBlockIntoBuffer: %v", err)
}
if !bytes.Equal(out.Bytes(), plaintext) {
t.Fatalf("round-trip plaintext mismatch (len got %d want %d)", out.Len(), len(plaintext))
}
}