diff options
| -rw-r--r-- | cmd/eno/cobra.go | 4 | ||||
| -rw-r--r-- | cmd/eno/cobra_ssh.go | 22 | ||||
| -rw-r--r-- | internal/crud/cli_edit.go | 16 | ||||
| -rw-r--r-- | internal/crud/cli_new.go | 33 | ||||
| -rw-r--r-- | internal/crypt/rawDecrypt.go | 6 | ||||
| -rw-r--r-- | internal/crypt/sshKey.go | 4 | ||||
| -rw-r--r-- | internal/utils/cli.go | 13 |
7 files changed, 65 insertions, 33 deletions
diff --git a/cmd/eno/cobra.go b/cmd/eno/cobra.go index 4a7443e..cf373d5 100644 --- a/cmd/eno/cobra.go +++ b/cmd/eno/cobra.go @@ -70,6 +70,7 @@ var encryptCmd = &cobra.Command{ } encrypted, _, err := crypt.EncryptBytesSSH(fileBytes, pubKeys) if err != nil { + clear(fileBytes) cmd.PrintErrln("Error:", err) os.Exit(1) } @@ -91,6 +92,8 @@ var encryptCmd = &cobra.Command{ encrypted, err := crypt.EncryptBytesPass(fileBytes, password) if err != nil { + clear(fileBytes) + clear(password) cmd.PrintErrln("Error:", err) os.Exit(1) } @@ -166,6 +169,7 @@ var decryptCmd = &cobra.Command{ plaintext, err := crypt.DecryptBytesPass(fileBytes, password) if err != nil { + clear(password) cmd.PrintErrln("Error:", err) os.Exit(1) } diff --git a/cmd/eno/cobra_ssh.go b/cmd/eno/cobra_ssh.go index f684e95..084960a 100644 --- a/cmd/eno/cobra_ssh.go +++ b/cmd/eno/cobra_ssh.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "crypto" "errors" "fmt" @@ -39,8 +38,6 @@ var sshInitCmd = &cobra.Command{ Short: "setup ssh for Eno notes", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { - scanner := bufio.NewScanner(os.Stdin) - if Conf.SSHInitialized { cmd.PrintErrln("SSH is already initialized") ok, err := utils.AskUserYesNo("Reinitialize SSH?", false) @@ -70,22 +67,22 @@ var sshInitCmd = &cobra.Command{ if privateKeyPath == "" { fmt.Print("Enter path to SSH private key: ") - if !scanner.Scan() { + if !utils.StdinScanner.Scan() { cmd.PrintErrln("failed to read input") os.Exit(1) } - privateKeyPath = expandPath(strings.TrimSpace(scanner.Text())) + privateKeyPath = expandPath(strings.TrimSpace(utils.StdinScanner.Text())) } publicKeyPath := privateKeyPath + ".pub" if _, err := os.Stat(publicKeyPath); os.IsNotExist(err) { cmd.PrintErrln("public key not found at " + publicKeyPath) fmt.Print("Enter path to SSH public key: ") - if !scanner.Scan() { + if !utils.StdinScanner.Scan() { cmd.PrintErrln("failed to read input") os.Exit(1) } - publicKeyPath = strings.TrimSpace(scanner.Text()) + publicKeyPath = strings.TrimSpace(utils.StdinScanner.Text()) publicKeyPath = expandPath(publicKeyPath) } @@ -110,14 +107,16 @@ var sshInitCmd = &cobra.Command{ fmt.Printf("Enter the ssh key passphrase: ") sshPassword, err := utils.TakeSecureUserInput() if err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } crypt.TryMlock(sshPassword) defer clear(sshPassword) - privKey, err = ssh.ParseRawPrivateKeyWithPassphrase(privKeyBytes, []byte(sshPassword)) + privKey, err = ssh.ParseRawPrivateKeyWithPassphrase(privKeyBytes, sshPassword) } else { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } @@ -125,6 +124,7 @@ var sshInitCmd = &cobra.Command{ signer, ok := privKey.(crypto.Signer) if !ok { + clear(privKeyBytes) cmd.PrintErrln("failed to extract public key from private key") os.Exit(1) } @@ -132,30 +132,36 @@ var sshInitCmd = &cobra.Command{ givenPubKey, _, _, _, err := ssh.ParseAuthorizedKey(givenPubKeyBytes) if err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } extractedPubKey, err := ssh.NewPublicKey(extractedPubKeyRaw) if err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } if ssh.FingerprintSHA256(extractedPubKey) != ssh.FingerprintSHA256(givenPubKey) { + clear(privKeyBytes) cmd.PrintErrln("public key file does not match private key") os.Exit(1) } if err := updateConfig("DefaultSSHPrivPath", privateKeyPath); err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } if err := updateConfig("DefaultSSHPubPath", publicKeyPath); err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } if err := updateConfig("SSHInitialized", true); err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } diff --git a/internal/crud/cli_edit.go b/internal/crud/cli_edit.go index b7b76f0..03c84df 100644 --- a/internal/crud/cli_edit.go +++ b/internal/crud/cli_edit.go @@ -50,7 +50,9 @@ func EditNotePass(notePath, editorString string) error { crypt.TryMlock(newContent) defer clear(newContent) - os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } encrypted, err := crypt.EncryptBytesPass(newContent, password) if err != nil { return err } @@ -66,11 +68,15 @@ func EditNotePass(notePath, editorString string) error { func deferredCleanTmpFile(tmpFile *os.File, oldContentLen int) { tmpFileInfo, err := tmpFile.Stat() if err != nil { - os.WriteFile(tmpFile.Name(), make([]byte, oldContentLen), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, oldContentLen), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } return } - os.WriteFile(tmpFile.Name(), make([]byte, int(tmpFileInfo.Size())), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, int(tmpFileInfo.Size())), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } } func EditNoteSSH(notePath, privKeyPath, editorString string) error { @@ -151,7 +157,9 @@ func EditNoteSSH(notePath, privKeyPath, editorString string) error { crypt.TryMlock(newContent) defer clear(newContent) - os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } encrypted, _, err := crypt.EncryptBytesSSH(newContent, pubKeys) if err != nil { diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go index a7408d7..6a2787d 100644 --- a/internal/crud/cli_new.go +++ b/internal/crud/cli_new.go @@ -1,7 +1,6 @@ package crud import ( - "bufio" "bytes" "fmt" "os" @@ -29,12 +28,12 @@ func NewNotePass(output string) error { crypt.TryMlock(passwordConfirm) defer clear(passwordConfirm) - if string(password) != string(passwordConfirm) { + if !bytes.Equal(password, passwordConfirm) { return fmt.Errorf("passphrases do not match") } encrypted, err := crypt.EncryptBytesPass( - []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), + append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...), password, ) if err != nil { @@ -46,6 +45,7 @@ func NewNotePass(output string) error { if err != nil { return err } + defer file.Close() _, err = file.Write(encrypted) if err != nil { @@ -77,14 +77,14 @@ func NewNotePassEdit(output, editorString string) error { crypt.TryMlock(passwordConfirm) defer clear(passwordConfirm) - if string(password) != string(passwordConfirm) { + if !bytes.Equal(password, passwordConfirm) { return fmt.Errorf("passphrases do not match") } noteInfo, err := os.Stat(output) preExisting := err == nil - initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) encrypted, err := crypt.EncryptBytesPass(initialContent, password) if err != nil { return err @@ -140,7 +140,9 @@ func NewNotePassEdit(output, editorString string) error { crypt.TryMlock(newContent) defer clear(newContent) - os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } encrypted, err = crypt.EncryptBytesPass(newContent, password) if err != nil { @@ -183,13 +185,12 @@ func PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, error) { if addMore { for { fmt.Print("Enter a public key, or path to a public key file (comma-separated for multiple): ") - scanner := bufio.NewScanner(os.Stdin) - scanner.Scan() - if err := scanner.Err(); err != nil { + utils.StdinScanner.Scan() + if err := utils.StdinScanner.Err(); err != nil { return nil, false, fmt.Errorf("failed to read public key: %v", err) } - input := strings.TrimSpace(scanner.Text()) + input := strings.TrimSpace(utils.StdinScanner.Text()) if input == "" { continue } @@ -236,7 +237,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { return fmt.Errorf("at least one recipient public key is required") } - tmptxt := []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^") + tmptxt := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) encrypted, _, err := crypt.EncryptBytesSSH(tmptxt, pubKeys) if err != nil { @@ -248,6 +249,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { if err != nil { return err } + defer file.Close() _, err = file.Write(encrypted) if err != nil { @@ -274,7 +276,7 @@ func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath noteInfo, err := os.Stat(output) preExisting := err == nil - initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) encrypted, noteKey, err := crypt.EncryptBytesSSH(initialContent, pubKeys) if err != nil { return err @@ -286,6 +288,9 @@ func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath if err != nil { return err } + if err != nil { + return err + } _, err = file.Write(encrypted) if err != nil { @@ -337,7 +342,9 @@ func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath crypt.TryMlock(newContent) defer clear(newContent) - os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } encrypted, _, err = crypt.EncryptBytesSSH(newContent, pubKeys) if err != nil { diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go index d1f0193..ede063c 100644 --- a/internal/crypt/rawDecrypt.go +++ b/internal/crypt/rawDecrypt.go @@ -25,7 +25,7 @@ func DecryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) { return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err) } - if headerLen < 0 || idx+1+headerLen > len(inputBytes) { + if headerLen <= 0 || idx+1+headerLen > len(inputBytes) { return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen) } @@ -73,7 +73,7 @@ func DecryptBytesSSH(inputBytes []byte, privKeyPath string) ([]byte, error) { return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err) } - if headerLen < 0 || idx+1+headerLen > len(inputBytes) { + if headerLen <= 0 || idx+1+headerLen > len(inputBytes) { return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen) } @@ -137,7 +137,7 @@ func DecryptBytesSSHWithKey(inputBytes []byte, noteKey []byte) ([]byte, error) { return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err) } - if headerLen < 0 || idx+1+headerLen > len(inputBytes) { + if headerLen <= 0 || idx+1+headerLen > len(inputBytes) { return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen) } diff --git a/internal/crypt/sshKey.go b/internal/crypt/sshKey.go index b0909b1..2062ebd 100644 --- a/internal/crypt/sshKey.go +++ b/internal/crypt/sshKey.go @@ -43,7 +43,9 @@ func edwardsToMontgomery(edPub []byte) []byte { // Ed25519 seed -> X25519 private key. func ed25519SeedToX25519Priv(seed []byte) []byte { h := sha512.Sum512(seed) - scalar := h[:32] + scalar := make([]byte, 32) + copy(scalar, h[:32]) + clear(h[:]) scalar[0] &= 248 scalar[31] &= 127 diff --git a/internal/utils/cli.go b/internal/utils/cli.go index 46eef78..ba7a07c 100644 --- a/internal/utils/cli.go +++ b/internal/utils/cli.go @@ -7,20 +7,25 @@ import ( "strings" ) +var StdinScanner = bufio.NewScanner(os.Stdin) + +func init() { + StdinScanner.Buffer(make([]byte, 0, 512*1024), 512*1024) +} + func AskUserYesNo(prompt string, defaultVal bool) (bool, error) { suffix := "[y/N]" if defaultVal { suffix = "[Y/n]" } - scanner := bufio.NewScanner(os.Stdin) fmt.Printf("%s %s ", prompt, suffix) - scanner.Scan() - if err := scanner.Err(); err != nil { + StdinScanner.Scan() + if err := StdinScanner.Err(); err != nil { return false, fmt.Errorf("failed to read confirmation: %w", err) } - input := strings.TrimSpace(scanner.Text()) + input := strings.TrimSpace(StdinScanner.Text()) if input == "" { return defaultVal, nil } |