-
Notifications
You must be signed in to change notification settings - Fork 4
/
vault.go
221 lines (189 loc) · 4.59 KB
/
vault.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
package onepassword
import (
"database/sql"
"encoding/json"
"fmt"
"os/user"
"path"
"github.com/mpage/onepassword/crypto"
_ "github.com/mattn/go-sqlite3"
)
const (
DefaultProfile = "default"
// Relative to user's home dir
RelativeVaultPath = "Library/Containers/2BUA8C4S2C.com.agilebits.onepassword-osx-helper/Data/Library/Data/OnePassword.sqlite"
)
// A Vault is a read-only interface to the 1Password SQLite database.
type Vault struct {
db *sql.DB
profileId int
masterKP *crypto.KeyPair // Encrypts item keypairs
overviewKP *crypto.KeyPair // Encrypts overviews
categories map[string]string // For uuid -> name
}
type VaultConfig struct {
DBPath string // Path to the sqlite file
Profile string // Name of 1p profile
}
func resolveDefaultDBPath() string {
u, err := user.Current()
if err != nil {
panic(fmt.Sprintf("Cannot resolve current user: %s", err))
}
return path.Join(u.HomeDir, RelativeVaultPath)
}
var DefaultVaultConfig = VaultConfig{
DBPath: resolveDefaultDBPath(),
Profile: DefaultProfile,
}
func getCategories(db *sql.DB, profileId int) (map[string]string, error) {
cats := make(map[string]string)
err := transact(db, func(tx *sql.Tx) (e error) {
rows, e := tx.Query(
"SELECT uuid, singular_name" +
" FROM categories" +
" WHERE profile_id = ?",
profileId)
if e != nil {
return
}
defer rows.Close()
// Fill in cats
for rows.Next() {
var uuid, name string
e = rows.Scan(&uuid, &name)
if e != nil {
return
}
cats[uuid] = name
}
return rows.Err()
})
if err != nil {
cats = nil
}
return cats, err
}
func NewVault(masterPass string, cfg VaultConfig) (*Vault, error) {
db, err := sql.Open("sqlite3", cfg.DBPath)
if err != nil {
return nil, err
}
// Lookup profile
var profileId, nIters int
var salt, masterKeyBlob, overviewKeyBlob []byte
err = transact(db, func(tx *sql.Tx) error {
row := tx.QueryRow(
"SELECT id, iterations, master_key_data, overview_key_data, salt" +
" FROM profiles" +
" WHERE profile_name = ?",
cfg.Profile)
e := row.Scan(&profileId, &nIters, &masterKeyBlob, &overviewKeyBlob, &salt)
if e == sql.ErrNoRows {
e = fmt.Errorf("no profile named %q", cfg.Profile)
}
return e
})
if err != nil {
db.Close()
return nil, err
}
// Decrypt master/overview keypairs
derKP := crypto.ComputeDerivedKeys(masterPass, salt, nIters)
mkp, err := crypto.DecryptMasterKeys(masterKeyBlob, derKP)
if err != nil {
db.Close()
return nil, err
}
okp, err := crypto.DecryptMasterKeys(overviewKeyBlob, derKP)
if err != nil {
db.Close()
return nil, err
}
// Get category index
cats, err := getCategories(db, profileId)
if err != nil {
db.Close()
return nil, err
}
v := &Vault{
db: db,
profileId: profileId,
masterKP: mkp,
overviewKP: okp,
categories: cats,
}
return v, nil
}
// An ItemPredicate acts as a query to the 1Password database. It returns true
// if an Item in the database is deemed a match. Otherwise it returns false.
type ItemPredicate func(*Item) bool
// LookupItems finds items in the 1Password database that match the supplied predicate.
func (v *Vault) LookupItems(pred ItemPredicate) ([]Item, error) {
var items []Item
err := transact(v.db, func(tx *sql.Tx) (e error) {
rows, e := tx.Query(
"SELECT id, category_uuid, key_data, overview_data" +
" FROM items" +
" WHERE profile_id = ? AND trashed = 0",
v.profileId)
if e != nil {
return
}
defer rows.Close()
// Figure out matches
for rows.Next() {
var itemId int
var catUuid string
var itemKeyBlob, opdata []byte
e = rows.Scan(&itemId, &catUuid, &itemKeyBlob, &opdata)
if e != nil {
return
}
// Decrypt the overview
var overview []byte
overview, e = crypto.DecryptOPData01(opdata, v.overviewKP)
if e != nil {
return
}
var item Item
e = json.Unmarshal(overview, &item)
if e != nil {
return
}
item.Category = Category{catUuid, v.categories[catUuid]}
// Decrypt the item key
var kp *crypto.KeyPair
kp, e = crypto.DecryptItemKey(itemKeyBlob, v.masterKP)
if e != nil {
return
}
// Decrypt the item details
detRow := tx.QueryRow(
"SELECT data FROM item_details" +
" WHERE item_id = ?", itemId)
var detailsCT []byte
e = detRow.Scan(&detailsCT)
if e != nil {
return
}
var details []byte
details, e = crypto.DecryptOPData01(detailsCT, kp)
if e != nil {
return
}
item.Details = details
if pred(&item) {
items = append(items, item)
}
}
return rows.Err()
})
if err != nil {
items = nil
}
return items, err
}
func (v *Vault) Close() {
v.db.Close()
}