Debian Codegraph @

link_types.go

raw
1 package proton
2
3 import (
4 "encoding/base64"
5 "encoding/json"
6 "errors"
7
8 "github.com/ProtonMail/gopenpgp/v2/crypto"
9 )
10
11 type LinkWalkFunc func([]string, Link, *crypto.KeyRing) error
12
13 // Link holds the tree structure, for the clients, they represent the files and folders of a given volume.
14 // They have a ParentLinkID that points to parent folders.
15 // Links also hold the file name (encrypted) and a hash of the name for name collisions.
16 // Link data is encrypted with its owning Share keyring.
17 type Link struct {
18 LinkID string // Encrypted file/folder ID
19 ParentLinkID string // Encrypted parent folder ID (LinkID). Root link has null ParentLinkID.
20
21 Type LinkType
22 Name string // Encrypted file name
23 NameSignatureEmail string // Signature email for link name
24 Hash string // HMAC of name encrypted with parent hash key
25 Size int64
26 State LinkState
27 MIMEType string
28
29 CreateTime int64 // Link creation time
30 ModifyTime int64 // Link modification time (on API, real modify date is stored in XAttr)
31 ExpirationTime int64 // Link expiration time
32
33 NodeKey string // The private NodeKey, used to decrypt any file/folder content.
34 NodePassphrase string // The passphrase used to unlock the NodeKey, encrypted by the owning Link/Share keyring.
35 NodePassphraseSignature string
36 SignatureEmail string // Signature email for the NodePassphraseSignature
37
38 FileProperties *FileProperties
39 FolderProperties *FolderProperties
40 }
41
42 type LinkState int
43
44 const (
45 LinkStateDraft LinkState = iota
46 LinkStateActive
47 LinkStateTrashed
48 LinkStateDeleted
49 LinkStateRestoring
50 )
51
52 func (l Link) GetName(parentNodeKR, addrKR *crypto.KeyRing) (string, error) {
53 encName, err := crypto.NewPGPMessageFromArmored(l.Name)
54 if err != nil {
55 return "", err
56 }
57
58 decName, err := parentNodeKR.Decrypt(encName, addrKR, crypto.GetUnixTime())
59 if err != nil {
60 return "", err
61 }
62
63 return decName.GetString(), nil
64 }
65
66 func (l Link) GetKeyRing(parentNodeKR, addrKR *crypto.KeyRing) (*crypto.KeyRing, error) {
67 enc, err := crypto.NewPGPMessageFromArmored(l.NodePassphrase)
68 if err != nil {
69 return nil, err
70 }
71
72 dec, err := parentNodeKR.Decrypt(enc, nil, crypto.GetUnixTime())
73 if err != nil {
74 return nil, err
75 }
76
77 sig, err := crypto.NewPGPSignatureFromArmored(l.NodePassphraseSignature)
78 if err != nil {
79 return nil, err
80 }
81
82 if err := addrKR.VerifyDetached(dec, sig, crypto.GetUnixTime()); err != nil {
83 return nil, err
84 }
85
86 lockedKey, err := crypto.NewKeyFromArmored(l.NodeKey)
87 if err != nil {
88 return nil, err
89 }
90
91 unlockedKey, err := lockedKey.Unlock(dec.GetBinary())
92 if err != nil {
93 return nil, err
94 }
95
96 return crypto.NewKeyRing(unlockedKey)
97 }
98
99 func (l Link) GetHashKey(parentNodeKey, addrKRs *crypto.KeyRing) ([]byte, error) {
100 if l.Type != LinkTypeFolder {
101 return nil, errors.New("link is not a folder")
102 }
103
104 enc, err := crypto.NewPGPMessageFromArmored(l.FolderProperties.NodeHashKey)
105 if err != nil {
106 return nil, err
107 }
108
109 _, ok := enc.GetSignatureKeyIDs()
110 var dec *crypto.PlainMessage
111 if ok {
112 dec, err = parentNodeKey.Decrypt(enc, addrKRs, crypto.GetUnixTime())
113 if err != nil {
114 return nil, err
115 }
116 } else {
117 dec, err = parentNodeKey.Decrypt(enc, nil, 0)
118 if err != nil {
119 return nil, err
120 }
121 }
122
123 return dec.GetBinary(), nil
124 }
125
126 func (l Link) GetSessionKey(nodeKR *crypto.KeyRing) (*crypto.SessionKey, error) {
127 if l.Type != LinkTypeFile {
128 return nil, errors.New("link is not a file")
129 }
130
131 dec, err := base64.StdEncoding.DecodeString(l.FileProperties.ContentKeyPacket)
132 if err != nil {
133 return nil, err
134 }
135
136 key, err := nodeKR.DecryptSessionKey(dec)
137 if err != nil {
138 return nil, err
139 }
140
141 sig, err := crypto.NewPGPSignatureFromArmored(l.FileProperties.ContentKeyPacketSignature)
142 if err != nil {
143 return nil, err
144 }
145
146 if err := nodeKR.VerifyDetached(crypto.NewPlainMessage(key.Key), sig, crypto.GetUnixTime()); err != nil {
147 return nil, err
148 }
149
150 return key, nil
151 }
152
153 type FileProperties struct {
154 ContentKeyPacket string // The block's key packet, encrypted with the node key.
155 ContentKeyPacketSignature string // Signature of the content key packet. Signature of the session key, signed with the NodeKey.
156 ActiveRevision RevisionMetadata // The active revision of the file.
157 }
158
159 type FolderProperties struct {
160 NodeHashKey string // HMAC key used to hash the folder's children names.
161 }
162
163 type LinkType int
164
165 const (
166 LinkTypeFolder LinkType = iota + 1
167 LinkTypeFile
168 )
169
170 type RevisionMetadata struct {
171 ID string // Encrypted Revision ID
172 CreateTime int64 // Unix timestamp of the revision creation time
173 Size int64 // Size of the revision in bytes
174 ManifestSignature string // Signature of the revision manifest, signed with user's address key of the share.
175 SignatureEmail string // Email of the user that signed the revision.
176 State RevisionState // State of revision
177 XAttr string // modification time and size from the file system
178 Thumbnail Bool // Whether the revision has a thumbnail
179 ThumbnailHash string // Hash of the thumbnail
180 }
181
182 func (revisionMetadata *RevisionMetadata) GetDecXAttrString(addrKR, nodeKR *crypto.KeyRing) (*RevisionXAttrCommon, error) {
183 if revisionMetadata.XAttr == "" {
184 return nil, nil
185 }
186
187 // decrypt the modification time and size
188 XAttrMsg, err := crypto.NewPGPMessageFromArmored(revisionMetadata.XAttr)
189 if err != nil {
190 return nil, err
191 }
192
193 decXAttr, err := nodeKR.Decrypt(XAttrMsg, addrKR, crypto.GetUnixTime())
194 if err != nil {
195 return nil, err
196 }
197
198 var data RevisionXAttr
199 err = json.Unmarshal(decXAttr.Data, &data)
200 if err != nil {
201 // TODO: if Unmarshal fails, maybe it's because the file system is missing the field?
202 return nil, err
203 }
204
205 return &data.Common, nil
206 }
207
208 // Revisions are only for files, they represent “versions” of files.
209 // Each file can have 1 active revision and n obsolete revisions.
210 type Revision struct {
211 RevisionMetadata
212
213 Blocks []Block
214 }
215
216 func (revision *Revision) GetDecXAttrString(addrKR, nodeKR *crypto.KeyRing) (*RevisionXAttrCommon, error) {
217 if revision.XAttr == "" {
218 return nil, nil
219 }
220
221 // decrypt the modification time and size
222 XAttrMsg, err := crypto.NewPGPMessageFromArmored(revision.XAttr)
223 if err != nil {
224 return nil, err
225 }
226
227 decXAttr, err := nodeKR.Decrypt(XAttrMsg, addrKR, crypto.GetUnixTime())
228 if err != nil {
229 return nil, err
230 }
231
232 var data RevisionXAttr
233 err = json.Unmarshal(decXAttr.Data, &data)
234 if err != nil {
235 // TODO: if Unmarshal fails, maybe it's because the file system is missing the field?
236 return nil, err
237 }
238
239 return &data.Common, nil
240 }
241
242 type RevisionState int
243
244 const (
245 RevisionStateDraft RevisionState = iota
246 RevisionStateActive
247 RevisionStateObsolete
248 RevisionStateDeleted
249 )
250
251 type CheckAvailableHashesReq struct {
252 Hashes []string
253 }
254
255 type PendingHashData struct {
256 Hash []string
257 RevisionID []string
258 LinkID []string
259 }
260 type CheckAvailableHashesRes struct {
261 AvailableHashes []string
262 PendingHashesData []PendingHashData
263 }