Improve password hash handling
This adds support for more hash algorithms. Also a stored password will be updated to SSHA512 upon a successful bind. It will also automatically hash a cleartext password if the `userpassword` field is modified with a cleartext one. Hashes supported: * SSHA * SSHA256 * SSHA512pull/14/head
parent
dbd9003714
commit
f05e41c9aa
@ -1,59 +1,53 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"errors"
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha1"
|
"github.com/jsimonetti/pwscheme/ssha"
|
||||||
"encoding/base64"
|
"github.com/jsimonetti/pwscheme/ssha256"
|
||||||
"fmt"
|
"github.com/jsimonetti/pwscheme/ssha512"
|
||||||
"strings"
|
)
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
const (
|
||||||
|
SSHA = "{SSHA}"
|
||||||
|
SSHA256 = "{SSHA256}"
|
||||||
|
SSHA512 = "{SSHA512}"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encode encodes the []byte of raw password
|
// Encode encodes the string to ssha512
|
||||||
func SSHAEncode(rawPassPhrase []byte) string {
|
func SSHAEncode(rawPassPhrase string) (string, error) {
|
||||||
hash := makeSSHAHash(rawPassPhrase, makeSalt())
|
return ssha512.Generate(rawPassPhrase, 16)
|
||||||
b64 := base64.StdEncoding.EncodeToString(hash)
|
|
||||||
return fmt.Sprintf("{ssha}%s", b64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matches matches the encoded password and the raw password
|
// Matches matches the encoded password and the raw password
|
||||||
func SSHAMatches(encodedPassPhrase string, rawPassPhrase []byte) bool {
|
func SSHAMatches(encodedPassPhrase string, rawPassPhrase string) (bool, error) {
|
||||||
if !strings.EqualFold(encodedPassPhrase[:6], "{ssha}") {
|
hashType, err := determineHashType(encodedPassPhrase)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
bhash, err := base64.StdEncoding.DecodeString(encodedPassPhrase[6:])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false, errors.New("invalid password hash stored")
|
||||||
}
|
}
|
||||||
salt := bhash[20:]
|
|
||||||
|
|
||||||
newssha := makeSSHAHash(rawPassPhrase, salt)
|
|
||||||
|
|
||||||
if bytes.Compare(newssha, bhash) != 0 {
|
switch hashType {
|
||||||
return false
|
case SSHA:
|
||||||
|
return ssha.Validate(rawPassPhrase, encodedPassPhrase)
|
||||||
|
case SSHA256:
|
||||||
|
return ssha256.Validate(rawPassPhrase, encodedPassPhrase)
|
||||||
|
case SSHA512:
|
||||||
|
return ssha512.Validate(rawPassPhrase, encodedPassPhrase)
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// makeSalt make a 32 byte array containing random bytes.
|
return false, errors.New("no matching hash type found")
|
||||||
func makeSalt() []byte {
|
|
||||||
sbytes := make([]byte, 32)
|
|
||||||
_, err := rand.Read(sbytes)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Could not read random bytes: %s", err)
|
|
||||||
}
|
|
||||||
return sbytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeSSHAHash make hasing using SHA-1 with salt. This is not the final output though. You need to append {SSHA} string with base64 of this hash.
|
func determineHashType(hash string) (string, error) {
|
||||||
func makeSSHAHash(passphrase, salt []byte) []byte {
|
if len(hash) >= 7 && string(hash[0:6]) == SSHA {
|
||||||
sha := sha1.New()
|
return SSHA, nil
|
||||||
sha.Write(passphrase)
|
}
|
||||||
sha.Write(salt)
|
if len(hash) >= 10 && string(hash[0:9]) == SSHA256 {
|
||||||
|
return SSHA256, nil
|
||||||
|
}
|
||||||
|
if len(hash) >= 10 && string(hash[0:9]) == SSHA512 {
|
||||||
|
return SSHA512, nil
|
||||||
|
}
|
||||||
|
|
||||||
h := sha.Sum(nil)
|
return "", errors.New("no valid hash found")
|
||||||
return append(h, salt...)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue