Skip to content

fix(chisel): convert seed to private key file EE-5099 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ var serverHelp = `
of man-in-the-middle attacks (defaults to the CHISEL_KEY environment
variable, otherwise a new key is generate each run).

--private-key-file, An optional path to a PEM-encoded TLS private key. When
this flag is set, the --key option is omitted, and the provided private key
is used to secure all communications. A private key file can be generated by:
openssl ecparam -name prime256v1 -genkey -out private_key.pem

--authfile, An optional path to a users.json file. This file should
be an object with users defined like:
{
Expand Down Expand Up @@ -170,6 +175,7 @@ func server(args []string) {

config := &chserver.Config{}
flags.StringVar(&config.KeySeed, "key", "", "")
flags.StringVar(&config.PrivateKeyFile, "private-key-file", "", "")
flags.StringVar(&config.AuthFile, "authfile", "", "")
flags.StringVar(&config.Auth, "auth", "", "")
flags.DurationVar(&config.KeepAlive, "keepalive", 25*time.Second, "")
Expand Down
38 changes: 26 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"time"

Expand All @@ -22,14 +23,15 @@ import (

// Config is the configuration for the chisel service
type Config struct {
KeySeed string
AuthFile string
Auth string
Proxy string
Socks5 bool
Reverse bool
KeepAlive time.Duration
TLS TLSConfig
KeySeed string
AuthFile string
Auth string
Proxy string
Socks5 bool
Reverse bool
KeepAlive time.Duration
TLS TLSConfig
PrivateKeyFile string
}

// Server respresent a chisel service
Expand Down Expand Up @@ -73,11 +75,23 @@ func NewServer(c *Config) (*Server, error) {
server.users.AddUser(u)
}
}
//generate private key (optionally using seed)
key, err := ccrypto.GenerateKey(c.KeySeed)
if err != nil {
log.Fatal("Failed to generate key")

var key []byte
var err error
if c.PrivateKeyFile != "" {
//read private key from the file specified by the --private-key-file flag
key, err = os.ReadFile(c.PrivateKeyFile)
if err != nil {
log.Fatal("Failed to read private key from disk")
}
} else {
//generate private key (optionally using seed)
key, err = ccrypto.GenerateKey(c.KeySeed)
if err != nil {
log.Fatal("Failed to generate key")
}
}

//convert into ssh.PrivateKey
private, err := ssh.ParsePrivateKey(key)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion share/ccrypto/determ_rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ func (d *determRand) Read(b []byte) (int, error) {
for n < l {
next, out := hash(d.next)
n += copy(b[n:], out)
d.next = next

// In Golang 1.20, ecdsa.GenerateKey() introduced a function called
// MaybeReadRand() which reads 1 byte from the determRand reader
// with 50% chance. As a result, GenerateKey() generates
// nondeterministic keys.
// The following conditional check neutralizes this effect.
if l > 1 {
d.next = next
}
}
return n, nil
}
Expand Down