This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passmanager.go
285 lines (269 loc) · 7.06 KB
/
passmanager.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
package pw2go
import (
"database/sql"
"errors"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
// Password Struct with the name of the service and its password
type Password struct {
service string
value string
}
// PasswordManager Struct with database info
type PasswordManager struct {
dbname string
mastertable string
passtable string
masterhash string
services map[string]bool
db *sql.DB
}
const (
// BADMASTER Message for wrong password input
BADMASTER = "Bad master password"
// BADINPUT Message for bad user input
BADINPUT = "Empty fields are not allowed"
// SETMASTERERROR Message for SetMaster error
SETMASTERERROR = "Master hash already present"
// ALREADYPRESENT Message for password already present
ALREADYPRESENT = "Password already present for this service"
// NOTFOUND Message for service not found
NOTFOUND = "Service not found"
// KLEN AES key length
KLEN = 16
)
// Create new password manager
func NewPassManager(dbname, mastertable, passtable string) *PasswordManager {
return &PasswordManager{dbname: dbname, mastertable: mastertable, passtable: passtable}
}
// NonFatalError checks whether an error message shoud cause a panic or not
func (pm *PasswordManager) NonFatalError(msg *string) bool {
return map[string]bool{
BADMASTER: true,
ALREADYPRESENT: true,
NOTFOUND: true,
BADINPUT: true,
}[*msg]
}
// Init Initialize password manager object
func (pm *PasswordManager) Init() error {
var err error
pm.db, err = sql.Open("sqlite3", fmt.Sprintf("./%s.db", pm.dbname))
if err != nil {
return err
}
query := `CREATE TABLE IF NOT EXISTS %s (
masterhash VARCHAR(512) PRIMARY KEY
);`
if _, err := pm.db.Exec(fmt.Sprintf(query, pm.mastertable)); err != nil {
return err
}
query = `CREATE TABLE IF NOT EXISTS %s (
service VARCHAR(128) PRIMARY KEY,
value VARCHAR(1024) NOT NULL,
nonce VARCHAR(64) NOT NULL
);`
if _, err := pm.db.Exec(fmt.Sprintf(query, pm.passtable)); err != nil {
return err
}
rows, err := pm.db.Query(fmt.Sprintf("SELECT * FROM %s LIMIT 1;", pm.mastertable))
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
if err := rows.Scan(&pm.masterhash); err != nil {
pm.masterhash = ""
return err
}
}
pm.services = make(map[string]bool)
rows, err = pm.db.Query(fmt.Sprintf("SELECT service FROM %s;", pm.passtable))
if err != nil {
return err
}
var serv string
for rows.Next() {
if err = rows.Scan(&serv); err != nil {
return err
}
pm.services[serv] = true
}
return nil
}
// Update table containing a single row with a single value
func setSingleValueTable(tx *sql.Tx, table, value string) error {
if _, err := tx.Exec(fmt.Sprintf("DELETE FROM %s;", table)); err != nil {
return err
}
stmt, err := tx.Prepare(fmt.Sprintf("INSERT INTO %s VALUES(?);", table))
if err != nil {
return err
}
defer stmt.Close()
if _, err := stmt.Exec(value); err != nil {
return err
}
return nil
}
// IsMasterSet checks if a master password was set
func (pm *PasswordManager) IsMasterSet() bool {
return len(pm.masterhash) != 0
}
// MapServices loops over the list of services and executes a function
func (pm *PasswordManager) MapServices(mapper func(string)) {
for service, _ := range pm.services {
mapper(service)
}
}
// SetMaster sets master password (if unset)
func (pm *PasswordManager) SetMaster(master string) error {
if pm.masterhash != "" {
return errors.New(SETMASTERERROR)
}
if master == "" {
return errors.New(BADINPUT)
}
pm.masterhash = sha256sum(master)
tx, err := pm.db.Begin()
if err != nil {
return err
}
if err := setSingleValueTable(tx, pm.mastertable, pm.masterhash); err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
// AddPassword adds a password to database
func (pm *PasswordManager) AddPassword(service, password, master string) error {
if pm.masterhash != sha256sum(master) {
return errors.New(BADMASTER)
}
if pm.services[service] {
return errors.New(ALREADYPRESENT)
}
if service == "" || password == "" {
return errors.New(BADINPUT)
}
cipherobj, err := encryptAESGCM(password, master, service, KLEN)
if err != nil {
return err
}
tx, err := pm.db.Begin()
stmt, err := tx.Prepare(fmt.Sprintf("INSERT INTO %s VALUES(?, ?, ?);", pm.passtable))
if err != nil {
tx.Rollback()
return err
}
if _, err := stmt.Exec(service, cipherobj.ciphertext, cipherobj.nonce); err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err == nil {
pm.services[service] = true
}
return err
}
// GetPassword retrieves a service's password from database
func (pm *PasswordManager) GetPassword(service, master string) (string, error) {
if pm.masterhash != sha256sum(master) {
return "", errors.New(BADMASTER)
}
if !pm.services[service] {
return "", errors.New(NOTFOUND)
}
stmt, err := pm.db.Prepare(fmt.Sprintf("SELECT value, nonce FROM %s WHERE service = ?;", pm.passtable))
if err != nil {
return "", err
}
rows, err := stmt.Query(service)
if err != nil {
return "", err
}
if !rows.Next() {
return "", errors.New(NOTFOUND)
}
var cipherobj AESCipher
if err := rows.Scan(&cipherobj.ciphertext, &cipherobj.nonce); err != nil {
return "", err
}
return decryptAESGCM(&cipherobj, master, service, KLEN)
}
// RemovePassword delete password from database
// This does not require master confirmation by design (forgetting stuff is allowed)
func (pm *PasswordManager) RemovePassword(service string) error {
if !pm.services[service] {
return errors.New(NOTFOUND)
}
stmt, err := pm.db.Prepare(fmt.Sprintf("DELETE FROM %s WHERE service = ?;", pm.passtable))
if err != nil {
return err
}
if _, err := stmt.Exec(service); err != nil {
return err
}
delete(pm.services, service)
return nil
}
// ChangeMaster changes the master password
func (pm *PasswordManager) ChangeMaster(oldmaster, newmaster string) error {
if newmaster == "" {
return errors.New(BADINPUT)
}
// Check old master
if pm.masterhash != sha256sum(oldmaster) {
return errors.New(BADMASTER)
}
// Set new master
tx, err := pm.db.Begin()
if err != nil {
return err
}
hash := sha256sum(newmaster)
if err := setSingleValueTable(tx, pm.mastertable, hash); err != nil {
tx.Rollback()
return err
}
rows, err := tx.Query(fmt.Sprintf("SELECT * FROM %s;", pm.passtable))
if err != nil {
tx.Rollback()
return err
}
// Update all values in DB
var service, plainval string
var cipherobj AESCipher
stmt, err := tx.Prepare(fmt.Sprintf("UPDATE %s SET value = ?, nonce = ? WHERE service = ?;", pm.passtable))
if err != nil {
tx.Rollback()
return err
}
for rows.Next() {
rows.Scan(&service, &cipherobj.ciphertext, &cipherobj.nonce)
plainval, err = decryptAESGCM(&cipherobj, oldmaster, service, KLEN)
if err != nil {
tx.Rollback()
return err
}
cipherobj, err = encryptAESGCM(plainval, newmaster, service, KLEN)
if err != nil {
tx.Rollback()
return err
}
if _, err := stmt.Exec(cipherobj.ciphertext, cipherobj.nonce, service); err != nil {
tx.Rollback()
return err
}
}
err = tx.Commit()
if err == nil {
pm.masterhash = hash
}
return err
}
// Close database connection
func (pm *PasswordManager) Close() {
pm.db.Close()
}