Skip to content

Commit

Permalink
persisent host keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimnz committed Apr 4, 2024
1 parent 23880c9 commit 25bacac
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions pkg/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package host

import (
"context"
"errors"
"os"
"path/filepath"

"crypto/rand"
"fmt"
Expand Down Expand Up @@ -55,15 +58,48 @@ func New(ctx context.Context, cfg config.Host) (*Host, error) {
cryptoType = libp2pcrypto.ECDSA
}

randomness := rand.Reader
if seed := cfg.Crypto.Seed; seed != 0 {
log.Warn("USING MANUAL SEED - !! WARNING !! INSECURE: ", seed)
randomness = mrand.New(mrand.NewSource(int64(seed)))
// check for existing key in ~/.orbis/keyfile
dirname, err := os.UserHomeDir()
if err != nil {
return nil, err
}
var priv libp2pcrypto.PrivKey
path := filepath.Join(dirname, ".orbis", "keyfile")
_, err = os.Stat(path)
if err == nil {
// read
buf, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading key file: %w", err)
}
priv, err = libp2pcrypto.UnmarshalPrivateKey(buf)
if err != nil {
return nil, fmt.Errorf("unmarshaling priv key: %w", err)
}
} else if errors.Is(err, os.ErrNotExist) {
// generate
randomness := rand.Reader
if seed := cfg.Crypto.Seed; seed != 0 {
log.Warn("USING MANUAL SEED - !! WARNING !! INSECURE: ", seed)
randomness = mrand.New(mrand.NewSource(int64(seed)))
}

priv, _, err := libp2pcrypto.GenerateKeyPairWithReader(cryptoType, cfg.Crypto.Bits, randomness)
if err != nil {
return nil, fmt.Errorf("generate key pair: %w", err)
priv, _, err = libp2pcrypto.GenerateKeyPairWithReader(cryptoType, cfg.Crypto.Bits, randomness)
if err != nil {
return nil, fmt.Errorf("generate key pair: %w", err)
}
// save new key
buf, err := libp2pcrypto.MarshalPrivateKey(priv)
if err != nil {
return nil, fmt.Errorf("marshaling priv key: %w", err)
}
err = os.WriteFile(path, buf, 0600)
if err != nil {
return nil, fmt.Errorf("writing priv key: %w", err)
}
} else {
// error out
return nil, fmt.Errorf("checking key file: %w", err)
}

cpriv, err := crypto.PrivateKeyFromLibP2P(priv)
Expand Down

0 comments on commit 25bacac

Please sign in to comment.