aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/crud/cli_edit.go16
-rw-r--r--internal/crud/cli_new.go33
-rw-r--r--internal/crypt/rawDecrypt.go6
-rw-r--r--internal/crypt/sshKey.go4
-rw-r--r--internal/utils/cli.go13
5 files changed, 47 insertions, 25 deletions
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
}