forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
link_file_types.go
200 lines (160 loc) · 5.15 KB
/
link_file_types.go
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
package proton
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"github.com/ProtonMail/gopenpgp/v2/crypto"
)
/* Helper function */
func getEncryptedName(name string, addrKR, nodeKR *crypto.KeyRing) (string, error) {
clearTextName := crypto.NewPlainMessageFromString(name)
encName, err := nodeKR.Encrypt(clearTextName, addrKR)
if err != nil {
return "", err
}
encNameString, err := encName.GetArmored()
if err != nil {
return "", err
}
return encNameString, nil
}
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
OriginalHash string // Old Encrypted File Name Hash
Hash string // Encrypted File Name Hash by using parent's NodeHashKey
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 (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) {
newSessionKey, err := crypto.GenerateSessionKey()
if err != nil {
return nil, err
}
encSessionKey, err := kr.EncryptSessionKey(newSessionKey)
if err != nil {
return nil, err
}
sessionKeyPlainMessage := crypto.NewPlainMessage(newSessionKey.Key)
sessionKeySignature, err := kr.SignDetached(sessionKeyPlainMessage)
if err != nil {
return nil, err
}
armoredSessionKeySignature, err := sessionKeySignature.GetArmored()
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
}
encXattr, err := nodeKR.Encrypt(crypto.NewPlainMessage(jsonByteArr), addrKR)
if err != nil {
return err
}
encXattrString, err := encXattr.GetArmored()
if err != nil {
return err
}
commitRevisionReq.XAttr = encXattrString
return nil
}
type BlockToken struct {
Index int
Token string
}