|
|
|
@ -2,7 +2,9 @@ package salty |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bufio" |
|
|
|
|
"crypto/ed25519" |
|
|
|
|
"encoding/base64" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
"strings" |
|
|
|
@ -11,17 +13,28 @@ import ( |
|
|
|
|
"github.com/keys-pub/keys" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const privateKeySizeLimit = 1 << 8 // 256 bytes
|
|
|
|
|
var ( |
|
|
|
|
// ErrProtectedKey is an error returned when a private key is protected by a password, but none was provided.
|
|
|
|
|
ErrProtectedKey = errors.New("error: key projtected by a password") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// GenerateKeys creates a new pair of Ed25519 keys and writes the Private Key
|
|
|
|
|
// to the `out io.Writer` and returns the Private and Public Keys.
|
|
|
|
|
// The Private Key written to `out` is Base64 encoded.
|
|
|
|
|
func GenerateKeys(out io.Writer) (*keys.EdX25519Key, string) { |
|
|
|
|
func GenerateKeys(pwd string, out io.Writer) (*keys.EdX25519Key, string) { |
|
|
|
|
k := keys.GenerateEdX25519Key() |
|
|
|
|
|
|
|
|
|
var encodedKey string |
|
|
|
|
|
|
|
|
|
if pwd != "" { |
|
|
|
|
encodedKey = base64.StdEncoding.EncodeToString(keys.EncryptWithPassword(k.Private(), pwd)) |
|
|
|
|
} else { |
|
|
|
|
encodedKey = base64.StdEncoding.EncodeToString(k.Private()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fmt.Fprintf(out, "# created: %s\n", time.Now().Format(time.RFC3339)) |
|
|
|
|
fmt.Fprintf(out, "# public key: %s\n", k.PublicKey().ID().String()) |
|
|
|
|
fmt.Fprintf(out, "%s\n", base64.StdEncoding.EncodeToString(k.Private())) |
|
|
|
|
fmt.Fprintf(out, "%s\n", encodedKey) |
|
|
|
|
|
|
|
|
|
return k, k.PublicKey().ID().String() |
|
|
|
|
} |
|
|
|
@ -31,8 +44,8 @@ func GenerateKeys(out io.Writer) (*keys.EdX25519Key, string) { |
|
|
|
|
// lines are ignored and the private key is the first non-comment / non-blank line.
|
|
|
|
|
// The Private Key is a Base64 decoded.
|
|
|
|
|
// This returns the parsed Ed25519 key on success or nil key and error if it fails.
|
|
|
|
|
func ParseIdentity(r io.Reader) (*keys.EdX25519Key, error) { |
|
|
|
|
scanner := bufio.NewScanner(io.LimitReader(r, privateKeySizeLimit)) |
|
|
|
|
func ParseIdentity(pwd string, r io.Reader) (*keys.EdX25519Key, error) { |
|
|
|
|
scanner := bufio.NewScanner(r) |
|
|
|
|
var n int |
|
|
|
|
for scanner.Scan() { |
|
|
|
|
line := scanner.Text() |
|
|
|
@ -40,11 +53,27 @@ func ParseIdentity(r io.Reader) (*keys.EdX25519Key, error) { |
|
|
|
|
if strings.HasPrefix(line, "#") || line == "" { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
bs, err := base64.StdEncoding.DecodeString(line) |
|
|
|
|
|
|
|
|
|
decodedKey, err := base64.StdEncoding.DecodeString(line) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, fmt.Errorf("error at line %d: %v", n, err) |
|
|
|
|
return nil, fmt.Errorf("error decoding key: %w", err) |
|
|
|
|
} |
|
|
|
|
return keys.NewEdX25519KeyFromPrivateKey(keys.Bytes64(bs)), nil |
|
|
|
|
|
|
|
|
|
var decryptedKey []byte |
|
|
|
|
|
|
|
|
|
if len(decodedKey) > ed25519.PrivateKeySize { |
|
|
|
|
if pwd == "" { |
|
|
|
|
return nil, ErrProtectedKey |
|
|
|
|
} |
|
|
|
|
decryptedKey, err = keys.DecryptWithPassword(decodedKey, pwd) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, fmt.Errorf("error decrypting key: %w", err) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
decryptedKey = decodedKey[:] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return keys.NewEdX25519KeyFromPrivateKey(keys.Bytes64(decryptedKey)), nil |
|
|
|
|
} |
|
|
|
|
if err := scanner.Err(); err != nil { |
|
|
|
|
return nil, fmt.Errorf("failed to read identity file: %v", err) |
|
|
|
|