forked from henrybear327/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlink_file_types.go
More file actions
212 lines (171 loc) · 6.14 KB
/
Copy pathlink_file_types.go
File metadata and controls
212 lines (171 loc) · 6.14 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
package proton
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"github.com/ProtonMail/gopenpgp/v3/crypto"
)
/* Helper function */
func getEncryptedName(name string, addrKR, nodeKR *crypto.KeyRing) (string, error) {
// Names are currently always encrypted to a v4 parent node key, but
// Proton Drive forbids AEAD for names regardless of the recipient key's
// preferences, so use the format-pinning helper to stay correct if
// parent keys ever become v6.
encName, err := EncryptMessageNonAead(nodeKR, []byte(name), addrKR)
if err != nil {
return "", err
}
return encName.Armor()
}
func GetNameHash(name string, hashKey []byte) (string, error) {
mac := hmac.New(sha256.New, hashKey)
_, err := mac.Write([]byte(name))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
type MoveLinkReq struct {
ParentLinkID string
Name string // Encrypted File Name
Hash string // Encrypted File Name Hash by using parent's NodeHashKey
NameSignatureEmail string // Signature email used to sign the name. Required.
NodePassphrase string // The passphrase used to unlock the NodeKey, encrypted by the owning Link/Share keyring.
// NodePassphraseSignature is the signature of the NodePassphrase.
// It is required when moving an anonymously created node and must be
// signed using the SignatureEmail. Omitted when empty.
NodePassphraseSignature string `json:",omitempty"`
// SignatureEmail is the email used to sign the NodePassphrase.
// Required when moving an anonymously created node. Omitted when empty.
SignatureEmail string `json:",omitempty"`
// OriginalHash is the current name hash before the move and is used to
// prevent race conditions. Omitted when empty.
OriginalHash string `json:",omitempty"`
}
func (moveLinkReq *MoveLinkReq) SetName(name string, addrKR, nodeKR *crypto.KeyRing) error {
encNameString, err := getEncryptedName(name, addrKR, nodeKR)
if err != nil {
return err
}
moveLinkReq.Name = encNameString
return nil
}
func (moveLinkReq *MoveLinkReq) SetHash(name string, hashKey []byte) error {
nameHash, err := GetNameHash(name, hashKey)
if err != nil {
return err
}
moveLinkReq.Hash = nameHash
return nil
}
type CreateFileReq struct {
ParentLinkID string
Name string // Encrypted File Name
Hash string // Encrypted File Name Hash
MIMEType string // MIME Type
ContentKeyPacket string // The block's key packet, encrypted with the node key.
ContentKeyPacketSignature string // Unencrypted signature of the content session key, signed with the NodeKey
NodeKey string // The private NodeKey, used to decrypt any file/folder content.
NodePassphrase string // The passphrase used to unlock the NodeKey, encrypted by the owning Link/Share keyring.
NodePassphraseSignature string // The signature of the NodePassphrase
SignatureAddress string // Signature email address used to sign passphrase and name
}
func (createFileReq *CreateFileReq) SetName(name string, addrKR, nodeKR *crypto.KeyRing) error {
encNameString, err := getEncryptedName(name, addrKR, nodeKR)
if err != nil {
return err
}
createFileReq.Name = encNameString
return nil
}
func (createFileReq *CreateFileReq) SetHash(name string, hashKey []byte) error {
nameHash, err := GetNameHash(name, hashKey)
if err != nil {
return err
}
createFileReq.Hash = nameHash
return nil
}
func (createFileReq *CreateFileReq) SetContentKeyPacketAndSignature(kr *crypto.KeyRing) (*crypto.SessionKey, error) {
// New files use the pre-crypto-refresh content format: a non-v6 session
// key wrapped in a v3 PKESK, giving v1 SEIPD data blocks. The official
// Proton clients (web included) do not yet create or reliably read files
// in the crypto-refresh format, so originating it makes the file
// undecryptable in the Proton apps. Revisions of existing crypto-refresh
// files are unaffected: they reuse the file's stored (v6) content key,
// which selects the v2 SEIPD block format automatically.
newSessionKey, err := crypto.PGP().GenerateSessionKey()
if err != nil {
return nil, err
}
encSessionKey, err := encryptSessionKey(kr, newSessionKey)
if err != nil {
return nil, err
}
armoredSessionKeySignature, err := signDetachedArmored(kr, newSessionKey.Key)
if err != nil {
return nil, err
}
createFileReq.ContentKeyPacket = base64.StdEncoding.EncodeToString(encSessionKey)
createFileReq.ContentKeyPacketSignature = armoredSessionKeySignature
return newSessionKey, nil
}
type CreateFileRes struct {
ID string // Encrypted Link ID
RevisionID string // Encrypted Revision ID
}
type CreateRevisionRes struct {
ID string // Encrypted Revision ID
}
type CommitRevisionReq struct {
ManifestSignature string
SignatureAddress string
XAttr string
}
type RevisionXAttrCommon struct {
ModificationTime string
Size int64
BlockSizes []int64
Digests map[string]string
}
type RevisionXAttr struct {
Common RevisionXAttrCommon
}
func (commitRevisionReq *CommitRevisionReq) SetEncXAttrString(addrKR, nodeKR *crypto.KeyRing, xAttrCommon *RevisionXAttrCommon) error {
// Source
// - https://github.com/ProtonMail/WebClients/blob/099a2451b51dea38b5f0e07ec3b8fcce07a88303/packages/shared/lib/interfaces/drive/link.ts#L53
// - https://github.com/ProtonMail/WebClients/blob/main/applications/drive/src/app/store/_links/extendedAttributes.ts#L139
// XAttr has following JSON structure encrypted by node key:
// {
// Common: {
// ModificationTime: "2021-09-16T07:40:54+0000",
// Size: 13283,
// BlockSizes: [1,2,3],
// Digests: "sha1 string"
// },
// }
jsonByteArr, err := json.Marshal(RevisionXAttr{
Common: *xAttrCommon,
})
if err != nil {
return err
}
// The xattr is encrypted to the node key, which for files is a v6 key;
// AEAD must not be used here (see EncryptMessageNonAead).
encXattr, err := EncryptMessageNonAead(nodeKR, jsonByteArr, addrKR)
if err != nil {
return err
}
encXattrString, err := encXattr.Armor()
if err != nil {
return err
}
commitRevisionReq.XAttr = encXattrString
return nil
}
type BlockToken struct {
Index int
Token string
}