-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwords.go
202 lines (189 loc) · 5.17 KB
/
passwords.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
package pgperms
import (
"bytes"
"context"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"regexp"
"strconv"
"github.com/jackc/pgx/v4"
"github.com/xdg-go/pbkdf2"
"github.com/xdg-go/scram"
)
var (
md5Re = regexp.MustCompile(`^md5[0-9a-f]{32}$`)
scramRe = regexp.MustCompile(`^SCRAM-(SHA-1|SHA-256|SHA-512)\$(\d+):([^$]+)\$([^:]+):(.+)$`)
)
// verifyPassword returns whether the hashed password belongs to the given user and password.
func verifyPassword(hashed, username, plain string) bool {
if hashed == plain {
return true
}
if md5Re.MatchString(hashed) {
return hashed == MD5Password(username, plain)
}
if m := scramRe.FindStringSubmatch(hashed); m != nil {
hgf := getScramHash(m[1])
if hgf == nil {
return false
}
iters, err := strconv.ParseInt(m[2], 10, 64)
if err != nil {
return false
}
salt, err := base64.StdEncoding.DecodeString(m[3])
if err != nil {
return false
}
storedKey, err := base64.StdEncoding.DecodeString(m[4])
if err != nil {
return false
}
serverKey, err := base64.StdEncoding.DecodeString(m[5])
if err != nil {
return false
}
digestKey := pbkdf2.Key([]byte(plain), salt, int(iters), 32, hgf)
clientKey := getHMACSum(hgf, digestKey, []byte("Client Key"))
if !bytes.Equal(storedKey, getSum(hgf, clientKey)) {
return false
}
if !bytes.Equal(serverKey, getHMACSum(hgf, digestKey, []byte("Server Key"))) {
return false
}
return true
/*
s, err := hgf.NewServer(func(user string) (scram.StoredCredentials, error) {
return scram.StoredCredentials{
KeyFactors: scram.KeyFactors{
Salt: string(salt),
Iters: int(iters),
},
StoredKey: storedKey,
ServerKey: serverKey,
}, nil
})
if err != nil {
return false
}
c, err := scram.SHA1.NewClient(username, plain, "")
if err != nil {
return false
}
clConv := c.NewConversation()
seConv := s.NewConversation()
var serverMsg string
for {
log.Printf("server -> client: %q", serverMsg)
clMsg, err := clConv.Step(serverMsg)
if err != nil {
log.Printf("Client reject: %q %v", clMsg, err)
return false
}
log.Printf("client -> server: %q", clMsg)
if clConv.Done() {
log.Printf("Client is done. Valid: %v", clConv.Valid())
log.Printf("Server done? %v; Server valid? %v", seConv.Done(), seConv.Valid())
return clConv.Valid()
}
serverMsg, err = seConv.Step(clMsg)
if err != nil {
log.Printf("Server reject: %q %v", serverMsg, err)
return false
}
}
*/
}
return false
}
func getScramHash(name string) scram.HashGeneratorFcn {
switch name {
case "SHA-1":
return scram.SHA1
case "SHA-256":
return scram.SHA256
case "SHA-512":
return scram.SHA512
default:
return nil
}
}
func getHMACSum(hgf scram.HashGeneratorFcn, key, msg []byte) []byte {
h := hmac.New(hgf, key)
_, _ = h.Write(msg)
return h.Sum(nil)
}
func getSum(hgf scram.HashGeneratorFcn, key []byte) []byte {
h := hgf()
_, _ = h.Write(key)
return h.Sum(nil)
}
type PasswordHasher func(username, password string) (string, error)
func SelectPasswordHasher(ctx context.Context, conn *pgx.Conn) (PasswordHasher, error) {
var method string
if err := conn.QueryRow(ctx, "SELECT setting FROM pg_settings WHERE name='password_encryption'").Scan(&method); err != nil {
return nil, err
}
switch method {
case "scram-sha-256":
return func(username, password string) (string, error) {
return ScramSha256Password(password)
}, nil
case "md5":
return func(username, password string) (string, error) {
return MD5Password(username, password), nil
}, nil
default:
return nil, fmt.Errorf("unknown password_encryption %q. File a feature request at https://github.com/SnoozeThis-org/pgperms/issues or don't use plaintext passwords in the config file", method)
}
}
func MD5Password(username, password string) string {
b := md5.Sum([]byte(password + username))
return "md5" + hex.EncodeToString(b[:])
}
func ScramSha256Password(password string) (string, error) {
const iterationCnt = 4096
const keyLen = 32
salt := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return "", err
}
digestKey := pbkdf2.Key([]byte(password), salt, iterationCnt, keyLen, sha256.New)
clientKey := getHMACSum(scram.SHA256, digestKey, []byte("Client Key"))
storedKey := getSum(scram.SHA256, clientKey)
serverKey := getHMACSum(scram.SHA256, digestKey, []byte("Server Key"))
return fmt.Sprintf("SCRAM-SHA-256$%d:%s$%s:%s",
iterationCnt,
base64.StdEncoding.EncodeToString(salt),
base64.StdEncoding.EncodeToString(storedKey),
base64.StdEncoding.EncodeToString(serverKey),
), nil
}
func encryptPasswordsInConfig(ctx context.Context, conn *pgx.Conn, roles map[string]RoleAttributes) error {
var hasher PasswordHasher
for r, ra := range roles {
if ra.Password == nil || md5Re.MatchString(*ra.Password) || scramRe.MatchString(*ra.Password) {
continue
}
if hasher == nil {
var err error
hasher, err = SelectPasswordHasher(ctx, conn)
if err != nil {
return err
}
}
hash, err := hasher(r, *ra.Password)
if err != nil {
return err
}
ra.hashedPassword = hash
roles[r] = ra
}
return nil
}