2019-10-10 15:37:27 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package caddyauth
|
|
|
|
|
|
|
|
import (
|
2019-10-30 14:56:27 -05:00
|
|
|
"crypto/subtle"
|
2022-09-05 14:32:58 -05:00
|
|
|
"encoding/base64"
|
2019-10-30 14:56:27 -05:00
|
|
|
|
2019-10-10 15:37:27 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"golang.org/x/crypto/scrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy.RegisterModule(BcryptHash{})
|
|
|
|
caddy.RegisterModule(ScryptHash{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// BcryptHash implements the bcrypt hash.
|
|
|
|
type BcryptHash struct{}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (BcryptHash) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http.authentication.hashes.bcrypt",
|
|
|
|
New: func() caddy.Module { return new(BcryptHash) },
|
2019-10-10 15:37:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare compares passwords.
|
|
|
|
func (BcryptHash) Compare(hashed, plaintext, _ []byte) (bool, error) {
|
|
|
|
err := bcrypt.CompareHashAndPassword(hashed, plaintext)
|
|
|
|
if err == bcrypt.ErrMismatchedHashAndPassword {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:51:05 -05:00
|
|
|
// Hash hashes plaintext using a random salt.
|
|
|
|
func (BcryptHash) Hash(plaintext, _ []byte) ([]byte, error) {
|
|
|
|
return bcrypt.GenerateFromPassword(plaintext, 14)
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:32:58 -05:00
|
|
|
// FakeHash returns a fake hash.
|
|
|
|
func (BcryptHash) FakeHash() []byte {
|
|
|
|
// hashed with the following command:
|
|
|
|
// caddy hash-password --plaintext "antitiming" --algorithm "bcrypt"
|
|
|
|
return []byte("$2a$14$X3ulqf/iGxnf1k6oMZ.RZeJUoqI9PX2PM4rS5lkIKJXduLGXGPrt6")
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:37:27 -05:00
|
|
|
// ScryptHash implements the scrypt KDF as a hash.
|
2022-09-05 14:32:58 -05:00
|
|
|
//
|
|
|
|
// DEPRECATED, please use 'bcrypt' instead.
|
2019-10-10 15:37:27 -05:00
|
|
|
type ScryptHash struct {
|
2019-12-23 14:45:35 -05:00
|
|
|
// scrypt's N parameter. If unset or 0, a safe default is used.
|
|
|
|
N int `json:"N,omitempty"`
|
|
|
|
|
|
|
|
// scrypt's r parameter. If unset or 0, a safe default is used.
|
|
|
|
R int `json:"r,omitempty"`
|
|
|
|
|
|
|
|
// scrypt's p parameter. If unset or 0, a safe default is used.
|
|
|
|
P int `json:"p,omitempty"`
|
|
|
|
|
|
|
|
// scrypt's key length parameter (in bytes). If unset or 0, a
|
|
|
|
// safe default is used.
|
2019-10-10 15:37:27 -05:00
|
|
|
KeyLength int `json:"key_length,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (ScryptHash) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http.authentication.hashes.scrypt",
|
|
|
|
New: func() caddy.Module { return new(ScryptHash) },
|
2019-10-10 15:37:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision sets up s.
|
2022-09-05 14:32:58 -05:00
|
|
|
func (s *ScryptHash) Provision(ctx caddy.Context) error {
|
2019-10-10 15:37:27 -05:00
|
|
|
s.SetDefaults()
|
2022-09-05 14:32:58 -05:00
|
|
|
ctx.Logger(s).Warn("use of 'scrypt' is deprecated, please use 'bcrypt' instead")
|
2019-10-10 15:37:27 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDefaults sets safe default parameters, but does
|
|
|
|
// not overwrite existing values. Each default parameter
|
|
|
|
// is set independently; it does not check to ensure
|
|
|
|
// that r*p < 2^30. The defaults chosen are those as
|
|
|
|
// recommended in 2019 by
|
|
|
|
// https://godoc.org/golang.org/x/crypto/scrypt.
|
|
|
|
func (s *ScryptHash) SetDefaults() {
|
|
|
|
if s.N == 0 {
|
|
|
|
s.N = 32768
|
|
|
|
}
|
|
|
|
if s.R == 0 {
|
|
|
|
s.R = 8
|
|
|
|
}
|
|
|
|
if s.P == 0 {
|
|
|
|
s.P = 1
|
|
|
|
}
|
|
|
|
if s.KeyLength == 0 {
|
|
|
|
s.KeyLength = 32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare compares passwords.
|
|
|
|
func (s ScryptHash) Compare(hashed, plaintext, salt []byte) (bool, error) {
|
|
|
|
ourHash, err := scrypt.Key(plaintext, salt, s.N, s.R, s.P, s.KeyLength)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if hashesMatch(hashed, ourHash) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:51:05 -05:00
|
|
|
// Hash hashes plaintext using the given salt.
|
|
|
|
func (s ScryptHash) Hash(plaintext, salt []byte) ([]byte, error) {
|
|
|
|
return scrypt.Key(plaintext, salt, s.N, s.R, s.P, s.KeyLength)
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:32:58 -05:00
|
|
|
// FakeHash returns a fake hash.
|
|
|
|
func (ScryptHash) FakeHash() []byte {
|
|
|
|
// hashed with the following command:
|
|
|
|
// caddy hash-password --plaintext "antitiming" --salt "fakesalt" --algorithm "scrypt"
|
|
|
|
bytes, _ := base64.StdEncoding.DecodeString("kFbjiVemlwK/ZS0tS6/UQqEDeaNMigyCs48KEsGUse8=")
|
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:56:27 -05:00
|
|
|
func hashesMatch(pwdHash1, pwdHash2 []byte) bool {
|
|
|
|
return subtle.ConstantTimeCompare(pwdHash1, pwdHash2) == 1
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:37:27 -05:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ Comparer = (*BcryptHash)(nil)
|
|
|
|
_ Comparer = (*ScryptHash)(nil)
|
2020-10-31 11:51:05 -05:00
|
|
|
_ Hasher = (*BcryptHash)(nil)
|
|
|
|
_ Hasher = (*ScryptHash)(nil)
|
2019-10-10 15:37:27 -05:00
|
|
|
_ caddy.Provisioner = (*ScryptHash)(nil)
|
|
|
|
)
|