| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/hmac" |
| 5 |
"crypto/sha256" |
| 6 |
"encoding/base64" |
| 7 |
"encoding/hex" |
| 8 |
"encoding/json" |
| 9 |
|
| 10 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 11 |
) |
| 12 |
|
| 13 |
/* Helper function */ |
| 14 |
func getEncryptedName(name string, addrKR, nodeKR *crypto.KeyRing) (string, error) { |
| 15 |
clearTextName := crypto.NewPlainMessageFromString(name) |
| 16 |
|
| 17 |
encName, err := nodeKR.Encrypt(clearTextName, addrKR) |
| 18 |
if err != nil { |
| 19 |
return "", err |
| 20 |
} |
| 21 |
|
| 22 |
encNameString, err := encName.GetArmored() |
| 23 |
if err != nil { |
| 24 |
return "", err |
| 25 |
} |
| 26 |
|
| 27 |
return encNameString, nil |
| 28 |
} |
| 29 |
|
| 30 |
func GetNameHash(name string, hashKey []byte) (string, error) { |
| 31 |
mac := hmac.New(sha256.New, hashKey) |
| 32 |
_, err := mac.Write([]byte(name)) |
| 33 |
if err != nil { |
| 34 |
return "", err |
| 35 |
} |
| 36 |
|
| 37 |
return hex.EncodeToString(mac.Sum(nil)), nil |
| 38 |
} |
| 39 |
|
| 40 |
type MoveLinkReq struct { |
| 41 |
ParentLinkID string |
| 42 |
|
| 43 |
Name string // Encrypted File Name |
| 44 |
OriginalHash string // Old Encrypted File Name Hash |
| 45 |
Hash string // Encrypted File Name Hash by using parent's NodeHashKey |
| 46 |
NodePassphrase string // The passphrase used to unlock the NodeKey, encrypted by the owning Link/Share keyring. |
| 47 |
NodePassphraseSignature string // The signature of the NodePassphrase |
| 48 |
|
| 49 |
SignatureAddress string // Signature email address used to sign passphrase and name |
| 50 |
} |
| 51 |
|
| 52 |
func (moveLinkReq *MoveLinkReq) SetName(name string, addrKR, nodeKR *crypto.KeyRing) error { |
| 53 |
encNameString, err := getEncryptedName(name, addrKR, nodeKR) |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
|
| 58 |
moveLinkReq.Name = encNameString |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
func (moveLinkReq *MoveLinkReq) SetHash(name string, hashKey []byte) error { |
| 63 |
nameHash, err := GetNameHash(name, hashKey) |
| 64 |
if err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
|
| 68 |
moveLinkReq.Hash = nameHash |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
type CreateFileReq struct { |
| 73 |
ParentLinkID string |
| 74 |
|
| 75 |
Name string // Encrypted File Name |
| 76 |
Hash string // Encrypted File Name Hash |
| 77 |
MIMEType string // MIME Type |
| 78 |
|
| 79 |
ContentKeyPacket string // The block's key packet, encrypted with the node key. |
| 80 |
ContentKeyPacketSignature string // Unencrypted signature of the content session key, signed with the NodeKey |
| 81 |
|
| 82 |
NodeKey string // The private NodeKey, used to decrypt any file/folder content. |
| 83 |
NodePassphrase string // The passphrase used to unlock the NodeKey, encrypted by the owning Link/Share keyring. |
| 84 |
NodePassphraseSignature string // The signature of the NodePassphrase |
| 85 |
|
| 86 |
SignatureAddress string // Signature email address used to sign passphrase and name |
| 87 |
} |
| 88 |
|
| 89 |
func (createFileReq *CreateFileReq) SetName(name string, addrKR, nodeKR *crypto.KeyRing) error { |
| 90 |
encNameString, err := getEncryptedName(name, addrKR, nodeKR) |
| 91 |
if err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
|
| 95 |
createFileReq.Name = encNameString |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
func (createFileReq *CreateFileReq) SetHash(name string, hashKey []byte) error { |
| 100 |
nameHash, err := GetNameHash(name, hashKey) |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
|
| 105 |
createFileReq.Hash = nameHash |
| 106 |
|
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func (createFileReq *CreateFileReq) SetContentKeyPacketAndSignature(kr *crypto.KeyRing) (*crypto.SessionKey, error) { |
| 111 |
newSessionKey, err := crypto.GenerateSessionKey() |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
|
| 116 |
encSessionKey, err := kr.EncryptSessionKey(newSessionKey) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
|
| 121 |
sessionKeyPlainMessage := crypto.NewPlainMessage(newSessionKey.Key) |
| 122 |
sessionKeySignature, err := kr.SignDetached(sessionKeyPlainMessage) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
armoredSessionKeySignature, err := sessionKeySignature.GetArmored() |
| 127 |
if err != nil { |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
|
| 131 |
createFileReq.ContentKeyPacket = base64.StdEncoding.EncodeToString(encSessionKey) |
| 132 |
createFileReq.ContentKeyPacketSignature = armoredSessionKeySignature |
| 133 |
return newSessionKey, nil |
| 134 |
} |
| 135 |
|
| 136 |
type CreateFileRes struct { |
| 137 |
ID string // Encrypted Link ID |
| 138 |
RevisionID string // Encrypted Revision ID |
| 139 |
} |
| 140 |
|
| 141 |
type CreateRevisionRes struct { |
| 142 |
ID string // Encrypted Revision ID |
| 143 |
} |
| 144 |
|
| 145 |
type CommitRevisionReq struct { |
| 146 |
ManifestSignature string |
| 147 |
SignatureAddress string |
| 148 |
XAttr string |
| 149 |
} |
| 150 |
|
| 151 |
type RevisionXAttrCommon struct { |
| 152 |
ModificationTime string |
| 153 |
Size int64 |
| 154 |
BlockSizes []int64 |
| 155 |
Digests map[string]string |
| 156 |
} |
| 157 |
|
| 158 |
type RevisionXAttr struct { |
| 159 |
Common RevisionXAttrCommon |
| 160 |
} |
| 161 |
|
| 162 |
func (commitRevisionReq *CommitRevisionReq) SetEncXAttrString(addrKR, nodeKR *crypto.KeyRing, xAttrCommon *RevisionXAttrCommon) error { |
| 163 |
// Source |
| 164 |
// - https://github.com/ProtonMail/WebClients/blob/099a2451b51dea38b5f0e07ec3b8fcce07a88303/packages/shared/lib/interfaces/drive/link.ts#L53 |
| 165 |
// - https://github.com/ProtonMail/WebClients/blob/main/applications/drive/src/app/store/_links/extendedAttributes.ts#L139 |
| 166 |
// XAttr has following JSON structure encrypted by node key: |
| 167 |
// { |
| 168 |
// Common: { |
| 169 |
// ModificationTime: "2021-09-16T07:40:54+0000", |
| 170 |
// Size: 13283, |
| 171 |
// BlockSizes: [1,2,3], |
| 172 |
// Digests: "sha1 string" |
| 173 |
// }, |
| 174 |
// } |
| 175 |
|
| 176 |
jsonByteArr, err := json.Marshal(RevisionXAttr{ |
| 177 |
Common: *xAttrCommon, |
| 178 |
}) |
| 179 |
if err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
|
| 183 |
encXattr, err := nodeKR.Encrypt(crypto.NewPlainMessage(jsonByteArr), addrKR) |
| 184 |
if err != nil { |
| 185 |
return err |
| 186 |
} |
| 187 |
|
| 188 |
encXattrString, err := encXattr.GetArmored() |
| 189 |
if err != nil { |
| 190 |
return err |
| 191 |
} |
| 192 |
|
| 193 |
commitRevisionReq.XAttr = encXattrString |
| 194 |
return nil |
| 195 |
} |
| 196 |
|
| 197 |
type BlockToken struct { |
| 198 |
Index int |
| 199 |
Token string |
| 200 |
} |