Explorar o código

Fix: use constant time compare for secret value

Rod Hynes %!s(int64=10) %!d(string=hai) anos
pai
achega
cfb7a53098
Modificáronse 1 ficheiros con 10 adicións e 4 borrados
  1. 10 4
      psiphon/server/sshService.go

+ 10 - 4
psiphon/server/sshService.go

@@ -20,6 +20,7 @@
 package server
 
 import (
+	"crypto/subtle"
 	"encoding/json"
 	"errors"
 	"fmt"
@@ -169,12 +170,17 @@ func (sshServer *sshServer) passwordCallback(conn ssh.ConnMetadata, password []b
 		return nil, psiphon.ContextError(fmt.Errorf("invalid password payload for %q", conn.User()))
 	}
 
-	if conn.User() == sshServer.config.SSHUserName &&
-		sshPasswordPayload.SshPassword == sshServer.config.SSHPassword {
-		return nil, nil
+	userOk := (subtle.ConstantTimeCompare(
+		[]byte(conn.User()), []byte(sshServer.config.SSHUserName)) == 1)
+
+	passwordOk := (subtle.ConstantTimeCompare(
+		[]byte(sshPasswordPayload.SshPassword), []byte(sshServer.config.SSHPassword)) == 1)
+
+	if !userOk || !passwordOk {
+		return nil, psiphon.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
 	}
 
-	return nil, psiphon.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
+	return nil, nil
 }
 
 func (sshServer *sshServer) authLogCallback(conn ssh.ConnMetadata, method string, err error) {