2023-03-15 16:05:24 +00:00
|
|
|
package pairing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
2023-03-20 18:39:28 +00:00
|
|
|
"io/ioutil"
|
2023-03-20 22:01:28 +00:00
|
|
|
"net"
|
2023-03-15 16:05:24 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-03-20 18:39:28 +00:00
|
|
|
const (
|
|
|
|
// Session names
|
|
|
|
sessionChallenge = "challenge"
|
|
|
|
sessionBlocked = "blocked"
|
|
|
|
)
|
|
|
|
|
2023-03-15 16:05:24 +00:00
|
|
|
type ChallengeError struct {
|
|
|
|
Text string
|
2023-03-20 20:16:38 +00:00
|
|
|
HTTPCode int
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ce *ChallengeError) Error() string {
|
2023-03-20 20:16:38 +00:00
|
|
|
return fmt.Sprintf("%s : %d", ce.Text, ce.HTTPCode)
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeCookieStore() (*sessions.CookieStore, error) {
|
|
|
|
auth := make([]byte, 64)
|
|
|
|
_, err := rand.Read(auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := make([]byte, 32)
|
|
|
|
_, err = rand.Read(enc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessions.NewCookieStore(auth, enc), nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:31:39 +00:00
|
|
|
// ChallengeGiver is responsible for generating challenges and checking challenge responses
|
2023-03-15 16:05:24 +00:00
|
|
|
type ChallengeGiver struct {
|
|
|
|
cookieStore *sessions.CookieStore
|
|
|
|
encryptor *PayloadEncryptor
|
|
|
|
logger *zap.Logger
|
2023-03-20 22:01:28 +00:00
|
|
|
authedIP net.IP
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChallengeGiver(e *PayloadEncryptor, logger *zap.Logger) (*ChallengeGiver, error) {
|
|
|
|
cs, err := makeCookieStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ChallengeGiver{
|
|
|
|
cookieStore: cs,
|
2023-03-20 15:31:39 +00:00
|
|
|
encryptor: e.Renew(),
|
2023-03-15 16:05:24 +00:00
|
|
|
logger: logger,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) getIP(r *http.Request) (net.IP, error) {
|
2023-03-20 22:01:28 +00:00
|
|
|
h, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
cg.logger.Error("getIP: h, _, err := net.SplitHostPort(r.RemoteAddr)", zap.Error(err), zap.String("r.RemoteAddr", r.RemoteAddr))
|
|
|
|
return nil, &ChallengeError{"error", http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
return net.ParseIP(h), nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) registerClientIP(r *http.Request) error {
|
|
|
|
IP, err := cg.getIP(r)
|
2023-03-20 22:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-22 12:58:09 +00:00
|
|
|
cg.authedIP = IP
|
2023-03-20 22:01:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) validateClientIP(r *http.Request) error {
|
2023-03-20 22:01:28 +00:00
|
|
|
// If we haven't registered yet register the IP
|
|
|
|
if cg.authedIP == nil || len(cg.authedIP) == 0 {
|
|
|
|
err := cg.registerClientIP(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then compare the current req RemoteIP with the authed IP
|
2023-03-22 12:58:09 +00:00
|
|
|
IP, err := cg.getIP(r)
|
2023-03-20 22:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
if !cg.authedIP.Equal(IP) {
|
2023-03-20 22:01:28 +00:00
|
|
|
cg.logger.Error(
|
|
|
|
"request RemoteAddr does not match authedIP: expected '%s', received '%s'",
|
|
|
|
zap.String("expected", cg.authedIP.String()),
|
2023-03-22 12:58:09 +00:00
|
|
|
zap.String("received", IP.String()),
|
2023-03-20 22:01:28 +00:00
|
|
|
)
|
|
|
|
return &ChallengeError{"forbidden", http.StatusForbidden}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) getSession(r *http.Request) (*sessions.Session, error) {
|
2023-03-15 16:05:24 +00:00
|
|
|
s, err := cg.cookieStore.Get(r, sessionChallenge)
|
|
|
|
if err != nil {
|
2023-03-20 15:31:39 +00:00
|
|
|
cg.logger.Error("checkChallengeResponse: cg.cookieStore.Get(r, sessionChallenge)", zap.Error(err), zap.String("sessionChallenge", sessionChallenge))
|
2023-03-20 18:39:28 +00:00
|
|
|
return nil, &ChallengeError{"error", http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) generateNewChallenge(s *sessions.Session, w http.ResponseWriter, r *http.Request) ([]byte, error) {
|
2023-03-20 18:39:28 +00:00
|
|
|
challenge := make([]byte, 64)
|
|
|
|
_, err := rand.Read(challenge)
|
|
|
|
if err != nil {
|
|
|
|
cg.logger.Error("regenerateNewChallenge: _, err = rand.Read(challenge)", zap.Error(err))
|
|
|
|
return nil, &ChallengeError{"error", http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Values[sessionChallenge] = challenge
|
|
|
|
err = s.Save(r, w)
|
|
|
|
if err != nil {
|
|
|
|
cg.logger.Error("regenerateNewChallenge: err = s.Save(r, w)", zap.Error(err))
|
|
|
|
return nil, &ChallengeError{"error", http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
|
|
|
|
return challenge, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) block(s *sessions.Session, w http.ResponseWriter, r *http.Request) error {
|
2023-03-20 18:39:28 +00:00
|
|
|
s.Values[sessionBlocked] = true
|
|
|
|
err := s.Save(r, w)
|
|
|
|
if err != nil {
|
|
|
|
cg.logger.Error("block: err = s.Save(r, w)", zap.Error(err))
|
2023-03-15 16:05:24 +00:00
|
|
|
return &ChallengeError{"error", http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:39:28 +00:00
|
|
|
return &ChallengeError{"forbidden", http.StatusForbidden}
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) checkChallengeResponse(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
err := cg.validateClientIP(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-03-20 22:01:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
s, err := cg.getSession(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-03-20 18:39:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:05:24 +00:00
|
|
|
blocked, ok := s.Values[sessionBlocked].(bool)
|
|
|
|
if ok && blocked {
|
|
|
|
return &ChallengeError{"forbidden", http.StatusForbidden}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the request header doesn't include a challenge don't punish the client, just throw a 403
|
2023-03-22 12:58:09 +00:00
|
|
|
clientChallengeResp := r.Header.Get(sessionChallenge)
|
|
|
|
if clientChallengeResp == "" {
|
2023-03-15 16:05:24 +00:00
|
|
|
return &ChallengeError{"forbidden", http.StatusForbidden}
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
dcr, err := cg.encryptor.decryptPlain(base58.Decode(clientChallengeResp))
|
2023-03-15 16:05:24 +00:00
|
|
|
if err != nil {
|
2023-03-22 12:58:09 +00:00
|
|
|
cg.logger.Error("checkChallengeResponse: cg.encryptor.decryptPlain(base58.Decode(clientChallengeResp))", zap.Error(err), zap.String("clientChallengeResp", clientChallengeResp))
|
2023-03-15 16:05:24 +00:00
|
|
|
return &ChallengeError{"error", http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the challenge is not in the session store don't punish the client, just throw a 403
|
|
|
|
challenge, ok := s.Values[sessionChallenge].([]byte)
|
|
|
|
if !ok {
|
|
|
|
return &ChallengeError{"forbidden", http.StatusForbidden}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only if we have both a challenge in the session store and in the request header
|
|
|
|
// do we entertain blocking the client. Because then we know someone is trying to be sneaky.
|
2023-03-22 12:58:09 +00:00
|
|
|
if !bytes.Equal(dcr, challenge) {
|
2023-03-20 18:39:28 +00:00
|
|
|
return cg.block(s, w, r)
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
2023-03-20 18:39:28 +00:00
|
|
|
|
2023-03-20 22:01:28 +00:00
|
|
|
// If every is ok, generate a new challenge for the next req
|
2023-03-22 12:58:09 +00:00
|
|
|
_, err = cg.generateNewChallenge(s, w, r)
|
|
|
|
return err
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:58:09 +00:00
|
|
|
func (cg *ChallengeGiver) getChallenge(w http.ResponseWriter, r *http.Request) ([]byte, error) {
|
2023-03-20 22:01:28 +00:00
|
|
|
err := cg.validateClientIP(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-20 20:16:38 +00:00
|
|
|
s, err := cg.getSession(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
challenge, ok := s.Values[sessionChallenge].([]byte)
|
|
|
|
if !ok {
|
2023-03-20 20:16:38 +00:00
|
|
|
challenge, err = cg.generateNewChallenge(s, w, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
2023-03-16 14:15:07 +00:00
|
|
|
return challenge, nil
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 15:31:39 +00:00
|
|
|
// ChallengeTaker is responsible for storing and performing server challenges
|
2023-03-15 16:05:24 +00:00
|
|
|
type ChallengeTaker struct {
|
2023-03-20 15:31:39 +00:00
|
|
|
encryptor *PayloadEncryptor
|
|
|
|
serverChallenge []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChallengeTaker(e *PayloadEncryptor) *ChallengeTaker {
|
|
|
|
return &ChallengeTaker{
|
|
|
|
encryptor: e.Renew(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:39:28 +00:00
|
|
|
func (ct *ChallengeTaker) SetChallenge(resp *http.Response) error {
|
|
|
|
challenge, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-20 15:31:39 +00:00
|
|
|
ct.serverChallenge = challenge
|
2023-03-20 18:39:28 +00:00
|
|
|
return nil
|
2023-03-20 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ChallengeTaker) DoChallenge(req *http.Request) error {
|
|
|
|
if ct.serverChallenge != nil {
|
|
|
|
ec, err := ct.encryptor.encryptPlain(ct.serverChallenge)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set(sessionChallenge, base58.Encode(ec))
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-15 16:05:24 +00:00
|
|
|
}
|