-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccount.go
420 lines (386 loc) · 9.89 KB
/
account.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package lastpass
import (
"bytes"
"context"
"crypto"
"crypto/rand"
"crypto/rsa"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
// Account represents a LastPass item.
// An item can be a password, payment card, bank account, etc., or a custom item type.
type Account struct {
ID string
Name string
Username string
Password string
URL string
Group string
// Shared folder name.
// If non-empty, it must have prefix "Shared-".
// Empty means this Account is not in a shared folder.
Share string
Notes string
// Timestamp in seconds (set by LastPass servers).
LastModifiedGMT string
LastTouch string
}
type encryptedAccount struct {
id string
name []byte
username []byte
password []byte
url []byte
group []byte
notes []byte
lastModifiedGMT string
lastTouch string
}
// share represents a LastPass shared folder.
type share struct {
id string
name string
// Account fields within a shared folder are encrypted with this sharing key.
key []byte
// true if the shared folder admin marked shared folder as read-only for our user
readOnly bool
}
// the blob returned by the /getaccts.php endpoint is made up of chunks
type chunk struct {
id uint32
payload []byte
}
// Accounts lists all LastPass accounts.
//
// If Client is not logged in, an *AuthenticationError is returned.
func (c *Client) Accounts(ctx context.Context) ([]*Account, error) {
loggedIn, err := c.loggedIn(ctx)
if err != nil {
return nil, err
}
if !loggedIn {
return nil, &AuthenticationError{"client not logged in"}
}
blob, err := c.FetchEncryptedAccounts(ctx)
if err != nil {
return nil, err
}
return c.ParseEncryptedAccounts(bytes.NewReader(blob))
}
// FetchEncryptedAccounts fetches the user's encrypted accounts from LastPass.
// The returned []byte can be parsed using the ParseEncryptedAccounts method.
func (c *Client) FetchEncryptedAccounts(ctx context.Context) ([]byte, error) {
endpoint := c.baseURL + EndpointGetAccts
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
u.RawQuery = url.Values{
"requestsrc": []string{"cli"},
"mobile": []string{"1"},
"b64": []string{"1"},
"hasplugin": []string{"1.3.3"},
}.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
c.log(ctx, "%s %s\n", req.Method, req.URL)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GET %s: %s", u.String(), res.Status)
}
blobBase64, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return decodeBase64(blobBase64)
}
// ParseEncryptedAccounts parses encrypted accounts into a []*Account.
// The original encrypted accounts data can be obtained from LastPass
// using the FetchEncryptedAccounts method.
func (c *Client) ParseEncryptedAccounts(r io.Reader) ([]*Account, error) {
chunks, err := getCompleteChunks(r)
if err != nil {
return nil, err
}
accts := make([]*Account, 0)
key := c.session.EncryptionKey
var share share
for _, chunk := range chunks {
switch chunk.id {
case chunkIDFromString("ACCT"):
encryptedAccount, err := parseAccount(bytes.NewReader(chunk.payload))
if err != nil {
return nil, err
}
acct, err := decryptAccount(encryptedAccount, key)
if err != nil {
return nil, err
}
if acct.URL == "http://group" {
// ignore "group" accounts since they are made up by LastPass and have no credentials
continue
}
acct.Share = share.name
accts = append(accts, acct)
case chunkIDFromString("SHAR"):
share, err = parseShare(
bytes.NewReader(chunk.payload),
c.session.EncryptionKey,
c.session.OptPrivateKey)
if err != nil {
return nil, err
}
// after SHAR chunk all the following ACCTs are enrypted with the SHAR's sharing key
key = share.key
default:
// the blob contains many other chunks we're currently not interested in
// see https://github.com/lastpass/lastpass-cli/blob/a84aa9629957033082c5930968dda7fbed751dfa/blob.c#L585-L676
}
}
return accts, nil
}
func getCompleteChunks(r io.Reader) ([]*chunk, error) {
chunks, err := extractChunks(r)
if err != nil {
return nil, err
}
if !areComplete(chunks) {
return nil, errors.New("blob is truncated")
}
return chunks, nil
}
// see https://github.com/lastpass/lastpass-cli/blob/8767b5e53192ad4e72d1352db4aa9218e928cbe1/blob.c#L356-L421
func parseAccount(r io.Reader) (*encryptedAccount, error) {
id, err := readItem(r)
if err != nil {
return nil, err
}
nameEncrypted, err := readItem(r)
if err != nil {
return nil, err
}
groupEncrypted, err := readItem(r)
if err != nil {
return nil, err
}
urlHexEncoded, err := readItem(r)
if err != nil {
return nil, err
}
notesEncrypted, err := readItem(r)
if err != nil {
return nil, err
}
for i := 0; i < 2; i++ {
if err = skipItem(r); err != nil {
return nil, err
}
}
usernameEncrypted, err := readItem(r)
if err != nil {
return nil, err
}
passwordEncrypted, err := readItem(r)
if err != nil {
return nil, err
}
for i := 0; i < 3; i++ {
if err = skipItem(r); err != nil {
return nil, err
}
}
lastTouch, err := readItem(r)
if err != nil {
return nil, err
}
for i := 0; i < 18; i++ {
if err = skipItem(r); err != nil {
return nil, err
}
}
lastModifiedGMT, err := readItem(r)
if err != nil {
return nil, err
}
return &encryptedAccount{
string(id),
nameEncrypted,
usernameEncrypted,
passwordEncrypted,
urlHexEncoded,
groupEncrypted,
notesEncrypted,
string(lastModifiedGMT),
string(lastTouch),
}, nil
}
func decryptAccount(encrypted *encryptedAccount, encryptionKey []byte) (*Account, error) {
name, err := decryptItem(encrypted.name, encryptionKey)
if err != nil {
return nil, err
}
username, err := decryptItem(encrypted.username, encryptionKey)
if err != nil {
return nil, err
}
password, err := decryptItem(encrypted.password, encryptionKey)
if err != nil {
return nil, err
}
url, err := decodeHex(encrypted.url)
if err != nil {
return nil, err
}
group, err := decryptItem(encrypted.group, encryptionKey)
if err != nil {
return nil, err
}
notes, err := decryptItem(encrypted.notes, encryptionKey)
if err != nil {
return nil, err
}
return &Account{
encrypted.id,
name,
username,
password,
string(url),
group,
"",
notes,
encrypted.lastModifiedGMT,
encrypted.lastTouch,
}, nil
}
func parseShare(r io.Reader, encryptionKey []byte, privateKey *rsa.PrivateKey) (share, error) {
shareID, err := readItem(r)
if err != nil {
return share{}, err
}
sharingKeyRSAEncryptedHex, err := readItem(r)
if err != nil {
return share{}, err
}
nameEncrypted, err := readItem(r)
if err != nil {
return share{}, err
}
readOnly, err := readItem(r)
if err != nil {
return share{}, err
}
if err = skipItem(r); err != nil {
return share{}, err
}
sharingKeyAESEncrypted, err := readItem(r)
if err != nil {
return share{}, err
}
var sharingKey []byte
if len(sharingKeyAESEncrypted) > 0 {
// The sharing key is only AES encrypted with the regular encryption key.
// The is the default case and happens after the user had already decrypted
// the sharing key with their private key once before (possibly in some other LastPass client).
key, err := decryptItem(sharingKeyAESEncrypted, encryptionKey)
if err != nil {
return share{}, err
}
sharingKey, err = hex.DecodeString(key)
if err != nil {
return share{}, err
}
} else {
// The user who shares the folder with us, encrypted the sharing key with our public key.
// Therefore, we decrypt the sharing key with our private key.
if privateKey == nil {
return share{}, errors.New("account private key is nil - " +
"refer to the following url for more information: " +
"https://support.lastpass.com/help/" +
"why-am-i-seeing-an-error-no-private-key-cannot-decrypt-pending-shares-message-lp010147")
}
sharingKeyRSAEncrypted, err := decodeHex(sharingKeyRSAEncryptedHex)
if err != nil {
return share{}, err
}
key, err := privateKey.Decrypt(rand.Reader, sharingKeyRSAEncrypted, &rsa.OAEPOptions{
// The CLI uses RSA_PKCS1_OAEP_PADDING
// (see https://github.com/lastpass/lastpass-cli/blob/a84aa9629957033082c5930968dda7fbed751dfa/cipher.c#L78).
// As described on https://linux.die.net/man/3/rsa_private_decrypt, RSA_PKCS1_OAEP_PADDING uses SHA1.
Hash: crypto.SHA1,
})
if err != nil {
return share{}, err
}
sharingKey, err = decodeHex(key)
if err != nil {
return share{}, err
}
}
name, err := decryptItem(nameEncrypted, sharingKey)
if err != nil {
return share{}, err
}
// convert "0" to false and "1" to true
readOnlyBool, err := strconv.ParseBool(string(readOnly))
if err != nil {
return share{}, err
}
return share{
id: string(shareID),
name: name,
key: sharingKey,
readOnly: readOnlyBool,
}, nil
}
func areComplete(chunks []*chunk) bool {
if len(chunks) == 0 {
return false
}
lastChunk := chunks[len(chunks)-1]
// ENDM = end marker
return lastChunk.id == chunkIDFromString("ENDM") &&
string(lastChunk.payload) == "OK"
}
func (c *Client) getShare(ctx context.Context, shareName string) (share, error) {
blob, err := c.FetchEncryptedAccounts(ctx)
if err != nil {
return share{}, err
}
chunks, err := getCompleteChunks(bytes.NewReader(blob))
if err != nil {
return share{}, err
}
for _, chunk := range chunks {
if chunk.id == chunkIDFromString("SHAR") {
share, err := parseShare(
bytes.NewReader(chunk.payload),
c.session.EncryptionKey,
c.session.OptPrivateKey)
if err != nil {
return share, err
}
if share.name == shareName {
return share, nil
}
}
}
return share{}, fmt.Errorf("shared folder %s not found", shareName)
}
func (a *Account) isShared() bool {
return strings.HasPrefix(a.Share, "Shared-")
}