Skip to content

Commit

Permalink
Re-add PublicKey to configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander committed Nov 3, 2023
1 parent e5e8c84 commit af15f8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
3 changes: 1 addition & 2 deletions cmd/yggdrasil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func main() {
return
}

privateKey := ed25519.PrivateKey(cfg.PrivateKey)
publicKey := privateKey.Public().(ed25519.PublicKey)
publicKey := ed25519.PublicKey(cfg.PublicKey)

switch {
case *getaddr:
Expand Down
21 changes: 16 additions & 5 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
type NodeConfig struct {
PrivateKey KeyBytes `comment:"Your private key. DO NOT share this with anyone!"`
PrivateKeyPath string `json:",omitempty"`
PublicKey KeyBytes `comment:"Your public key. This is safe to share."`
Certificate *tls.Certificate `json:"-"`
Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tls://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."`
InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ \"tls://a.b.c.d:e\" ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."`
Expand Down Expand Up @@ -128,7 +129,8 @@ func (cfg *NodeConfig) UnmarshalHJSON(b []byte) error {

func (cfg *NodeConfig) postprocessConfig() error {
if cfg.PrivateKeyPath != "" {
cfg.PrivateKey = nil
cfg.PrivateKey = cfg.PrivateKey[:0]
cfg.PublicKey = cfg.PublicKey[:0]
f, err := os.ReadFile(cfg.PrivateKeyPath)
if err != nil {
return err
Expand All @@ -137,6 +139,13 @@ func (cfg *NodeConfig) postprocessConfig() error {
return err
}
}
if len(cfg.PrivateKey) < ed25519.SeedSize {
return fmt.Errorf("private key is not long enough")
}
privateKey := ed25519.NewKeyFromSeed(cfg.PrivateKey[:ed25519.SeedSize])
publicKey := privateKey.Public().(ed25519.PublicKey)
cfg.PrivateKey = append(cfg.PrivateKey[:0], privateKey[:ed25519.SeedSize]...)
cfg.PublicKey = append(cfg.PublicKey[:0], publicKey...)
switch {
case cfg.Certificate == nil:
// No self-signed certificate has been generated yet.
Expand Down Expand Up @@ -173,7 +182,7 @@ func (cfg *NodeConfig) GenerateSelfSignedCertificate() error {
}

func (cfg *NodeConfig) MarshalPEMCertificate() ([]byte, error) {
privateKey := ed25519.PrivateKey(cfg.PrivateKey)
privateKey := ed25519.NewKeyFromSeed(cfg.PrivateKey[:ed25519.SeedSize])
publicKey := privateKey.Public().(ed25519.PublicKey)

cert := &x509.Certificate{
Expand Down Expand Up @@ -201,15 +210,17 @@ func (cfg *NodeConfig) MarshalPEMCertificate() ([]byte, error) {
}

func (cfg *NodeConfig) NewPrivateKey() {
_, spriv, err := ed25519.GenerateKey(nil)
spub, spriv, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
}
cfg.PrivateKey = KeyBytes(spriv)
cfg.PrivateKey = append(cfg.PrivateKey[:0], spriv[:ed25519.SeedSize]...)
cfg.PublicKey = append(cfg.PublicKey[:0], spub...)
}

func (cfg *NodeConfig) MarshalPEMPrivateKey() ([]byte, error) {
b, err := x509.MarshalPKCS8PrivateKey(ed25519.PrivateKey(cfg.PrivateKey))
privateKey := ed25519.NewKeyFromSeed(cfg.PrivateKey[:ed25519.SeedSize])
b, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("failed to marshal PKCS8 key: %w", err)
}
Expand Down

0 comments on commit af15f8a

Please sign in to comment.