diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-15 07:18:20 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-15 07:18:20 +0530 |
| commit | 4c64c1f923d2e1b8b5925b0d2d7f7fbd7b38e6a2 (patch) | |
| tree | a1f5821ec99f428434ac4ec88fabe0325e885153 | |
| parent | ec61b98520abce8d2352172e796191b1829ab5f4 (diff) | |
fix password-protected private key decrypt failure
| -rw-r--r-- | internal/crypt/sshKey.go | 84 |
1 files changed, 65 insertions, 19 deletions
diff --git a/internal/crypt/sshKey.go b/internal/crypt/sshKey.go index 1328e59..b0909b1 100644 --- a/internal/crypt/sshKey.go +++ b/internal/crypt/sshKey.go @@ -10,8 +10,11 @@ import ( "crypto/sha512" "fmt" "os" + "os/signal" + "syscall" "filippo.io/edwards25519/field" + "github.com/charmbracelet/x/term" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/curve25519" "golang.org/x/crypto/ssh" @@ -49,6 +52,35 @@ func ed25519SeedToX25519Priv(seed []byte) []byte { return scalar } +func promptSSHKeyPassphrase(privKeyPath string) []byte { + fd := os.Stdin.Fd() + oldState, err := term.GetState(fd) + if err != nil { + fmt.Fprintf(os.Stderr, "eno: failed to get terminal state: %v\n", err) + return nil + } + + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) + defer signal.Stop(sigCh) + + go func() { + <-sigCh + term.Restore(fd, oldState) + os.Exit(130) + }() + + fmt.Fprintf(os.Stderr, "Enter passphrase for SSH key %s: ", privKeyPath) + passphrase, err := term.ReadPassword(fd) + fmt.Fprintln(os.Stderr) + if err != nil { + fmt.Fprintf(os.Stderr, "eno: failed to read passphrase: %v\n", err) + return nil + } + + return passphrase +} + // Wraps noteKey once per PubKey. Returns WrappedKey for each. func WrapNoteKeyForRecipients(noteKey []byte, pubKeys [][]byte) ([]WrappedKey, error) { recipients := make([]WrappedKey, 0, len(pubKeys)) @@ -104,25 +136,39 @@ func WrapNoteKeyForRecipients(noteKey []byte, pubKeys [][]byte) ([]WrappedKey, e return recipients, nil } -// Decrypts the note key using the private key at privKeyPath. -func UnwrapNoteKey(wrapped WrappedKey, privKeyPath string) ([]byte, error) { +func parsePrivateKeyFile(privKeyPath string) (*ed25519.PrivateKey, []byte, error) { pemBytes, err := os.ReadFile(privKeyPath) if err != nil { - return nil, fmt.Errorf("read SSH key: %w", err) + return nil, nil, fmt.Errorf("read SSH key: %w", err) } TryMlock(pemBytes) - defer clear(pemBytes) parsed, err := ssh.ParseRawPrivateKey(pemBytes) if err != nil { - return nil, fmt.Errorf("parse SSH priv key: %w", err) + if _, ok := err.(*ssh.PassphraseMissingError); ok { + passphrase := promptSSHKeyPassphrase(privKeyPath) + if passphrase == nil { + return nil, pemBytes, fmt.Errorf("parse SSH priv key: %w", err) + } + defer clear(passphrase) + parsed, err = ssh.ParseRawPrivateKeyWithPassphrase(pemBytes, passphrase) + if err != nil { + return nil, pemBytes, fmt.Errorf("parse SSH priv key: %w", err) + } + } else { + return nil, pemBytes, fmt.Errorf("parse SSH priv key: %w", err) + } } edPriv, ok := parsed.(*ed25519.PrivateKey) if !ok { - return nil, fmt.Errorf("only Ed25519 SSH keys supported") + return nil, pemBytes, fmt.Errorf("only Ed25519 SSH keys supported") } + return edPriv, pemBytes, nil +} + +func unwrapNoteKeyWithKey(wrapped WrappedKey, edPriv *ed25519.PrivateKey) ([]byte, error) { seed := edPriv.Seed() TryMlock(seed) defer clear(seed) @@ -155,24 +201,24 @@ func UnwrapNoteKey(wrapped WrappedKey, privKeyPath string) ([]byte, error) { return noteKey, nil } -// Finds the recipient matching privKeyPath in case of multiple recipients, returns note key. -func FindAndUnwrapNoteKey(recipients []WrappedKey, privKeyPath string) ([]byte, error) { - pemBytes, err := os.ReadFile(privKeyPath) +// Decrypts the note key using the private key at privKeyPath. +func UnwrapNoteKey(wrapped WrappedKey, privKeyPath string) ([]byte, error) { + edPriv, pemBytes, err := parsePrivateKeyFile(privKeyPath) if err != nil { - return nil, fmt.Errorf("read SSH key: %w", err) + return nil, err } - TryMlock(pemBytes) defer clear(pemBytes) - parsed, err := ssh.ParseRawPrivateKey(pemBytes) - if err != nil { - return nil, fmt.Errorf("parse SSH priv key: %w", err) - } + return unwrapNoteKeyWithKey(wrapped, edPriv) +} - edPriv, ok := parsed.(*ed25519.PrivateKey) - if !ok { - return nil, fmt.Errorf("only Ed25519 SSH keys supported") +// Finds the recipient matching privKeyPath in case of multiple recipients, returns note key. +func FindAndUnwrapNoteKey(recipients []WrappedKey, privKeyPath string) ([]byte, error) { + edPriv, pemBytes, err := parsePrivateKeyFile(privKeyPath) + if err != nil { + return nil, err } + defer clear(pemBytes) pubKey := edPriv.Public().(ed25519.PublicKey) sshPub, err := ssh.NewPublicKey(pubKey) @@ -183,7 +229,7 @@ func FindAndUnwrapNoteKey(recipients []WrappedKey, privKeyPath string) ([]byte, for _, recipient := range recipients { if bytes.Equal(recipient.PubKeyBytes, ownPubKeyBytes) { - return UnwrapNoteKey(recipient, privKeyPath) + return unwrapNoteKeyWithKey(recipient, edPriv) } } |