diff options
Diffstat (limited to 'internal/crud')
| -rw-r--r-- | internal/crud/cli_edit.go | 101 | ||||
| -rw-r--r-- | internal/crud/cli_new.go | 258 |
2 files changed, 348 insertions, 11 deletions
diff --git a/internal/crud/cli_edit.go b/internal/crud/cli_edit.go index 5e2cb8d..0213a81 100644 --- a/internal/crud/cli_edit.go +++ b/internal/crud/cli_edit.go @@ -3,13 +3,14 @@ package crud import ( "fmt" "os" + "strconv" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/editor" "gitlab.com/whiteboardguy/eno/internal/utils" ) -func EditNote(notePath, editorString string) error { +func EditNotePass(notePath, editorString string) error { noteInfo, err := os.Stat(notePath) if err != nil && !os.IsNotExist(err) { return err @@ -29,7 +30,7 @@ func EditNote(notePath, editorString string) error { tmpFile, err := os.CreateTemp("", "eno-*") if err != nil { return err } - plaintext, err := crypt.DecryptRawBytes(ciphertext, password) + plaintext, err := crypt.DecryptBytesPass(ciphertext, password) if err != nil { return err } crypt.TryMlock(plaintext) defer clear(plaintext) @@ -51,13 +52,13 @@ func EditNote(notePath, editorString string) error { os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) - encrypted, err := crypt.EncryptRawBytes(newContent, password, crypt.Pass) + encrypted, err := crypt.EncryptBytesPass(newContent, password) if err != nil { return err } err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm()) // utils.ClearTerm() - fmt.Println("Saved:", noteInfo.Name()) + fmt.Println("Saved:", notePath) return err } @@ -71,3 +72,95 @@ func deferredCleanTmpFile(tmpFile *os.File, oldContentLen int) { os.WriteFile(tmpFile.Name(), make([]byte, int(tmpFileInfo.Size())), 0600) } + +func EditNoteSSH(notePath, privKeyPath, editorString string) error { + noteInfo, err := os.Stat(notePath) + if err != nil && !os.IsNotExist(err) { + return err + } + + ciphertext, err := os.ReadFile(notePath) + if err != nil { + return err + } + + idx := -1 + for i, b := range ciphertext { + if b == ':' { + idx = i + break + } + } + if idx == -1 { + return fmt.Errorf("invalid file format: no header length prefix found") + } + + headerLen, err := strconv.Atoi(string(ciphertext[:idx])) + if err != nil { + return fmt.Errorf("failed to parse header length: %v", err) + } + + if headerLen < 0 || idx+1+headerLen > len(ciphertext) { + return fmt.Errorf("invalid header length %d", headerLen) + } + + headerBytes := ciphertext[idx+1 : idx+1+headerLen] + headers, err := crypt.GetFileHeaders(headerBytes) + if err != nil { + return fmt.Errorf("failed to read file headers: %v", err) + } + + if headers.Auth != crypt.SSH { + return fmt.Errorf("note uses %s auth, not SSH", headers.Auth) + } + + pubKeys := make([][]byte, len(headers.Recipients)) + for i, recipient := range headers.Recipients { + pubKeys[i] = recipient.PubKeyBytes + } + + tmpFile, err := os.CreateTemp("", "eno-*") + if err != nil { + return err + } + + plaintext, err := crypt.DecryptBytesSSH(ciphertext, privKeyPath) + if err != nil { + return err + } + crypt.TryMlock(plaintext) + defer clear(plaintext) + + defer deferredCleanTmpFile(tmpFile, len(plaintext)) + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write(plaintext) + if err != nil { + return err + } + + err = editor.OpenWithEditor(tmpFile.Name(), editorString) + if err != nil { + return err + } + + newContent, err := os.ReadFile(tmpFile.Name()) + if err != nil { + return err + } + crypt.TryMlock(newContent) + defer clear(newContent) + + os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + + encrypted, err := crypt.EncryptBytesSSH(newContent, pubKeys) + if err != nil { + return err + } + + err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm()) + + fmt.Println("Saved:", notePath) + + return err +} diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go index 2b6e507..c8303a0 100644 --- a/internal/crud/cli_new.go +++ b/internal/crud/cli_new.go @@ -1,15 +1,18 @@ package crud import ( + "bufio" + "bytes" "fmt" "os" + "strings" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/editor" "gitlab.com/whiteboardguy/eno/internal/utils" ) -func NewNote(output string) error { +func NewNotePass(output string) error { fmt.Printf("Enter the passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { @@ -30,10 +33,9 @@ func NewNote(output string) error { return fmt.Errorf("passphrases do not match") } - encrypted, err := crypt.EncryptRawBytes( + encrypted, err := crypt.EncryptBytesPass( []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), password, - crypt.Pass, ) if err != nil { return err @@ -58,7 +60,7 @@ func NewNote(output string) error { return nil } -func NewNoteEdit(output, editorString string) error { +func NewNotePassEdit(output, editorString string) error { fmt.Printf("Enter the passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { @@ -83,7 +85,7 @@ func NewNoteEdit(output, editorString string) error { preExisting := err == nil initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") - encrypted, err := crypt.EncryptRawBytes(initialContent, password, crypt.Pass) + encrypted, err := crypt.EncryptBytesPass(initialContent, password) if err != nil { return err } @@ -111,7 +113,7 @@ func NewNoteEdit(output, editorString string) error { return err } - plaintext, err := crypt.DecryptRawBytes(ciphertext, password) + plaintext, err := crypt.DecryptBytesPass(ciphertext, password) if err != nil { return err } @@ -140,7 +142,249 @@ func NewNoteEdit(output, editorString string) error { os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) - encrypted, err = crypt.EncryptRawBytes(newContent, password, crypt.Pass) + encrypted, err = crypt.EncryptBytesPass(newContent, password) + if err != nil { + return err + } + + perm := os.FileMode(0600) + if preExisting { + perm = noteInfo.Mode().Perm() + } + + err = os.WriteFile(output, encrypted, perm) + + fmt.Println("New note saved at:", output) + + return err +} + +func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { + useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true) + if err != nil { + return fmt.Errorf("failed to read confirmation: %v", err) + } + if !useDefault { + fmt.Println("Warning: you will not be able to open or edit this note again.") + fmt.Println(" make sure you've used the '--edit' flag.") + } + + addMore, err := utils.AskUserYesNo("Do you want to add more public keys?", false) + if err != nil { + return fmt.Errorf("failed to read confirmation: %v", err) + } + + pubKeys := [][]byte{} + + if useDefault { + pubKeys = append(pubKeys, defaultPubKeyBytes) + } + + 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 { + return fmt.Errorf("failed to read public key: %v", err) + } + + input := strings.TrimSpace(scanner.Text()) + if input == "" { + continue + } + + for part := range strings.SplitSeq(input, ",") { + part = strings.TrimSpace(part) + if part == "" { + continue + } + + var keyBytes []byte + if strings.HasPrefix(part, "ssh-") { + keyBytes = []byte(part) + } else { + var err error + keyBytes, err = os.ReadFile(part) + if err != nil { + return fmt.Errorf("failed to read public key file %s: %v", part, err) + } + } + + pubKeys = append(pubKeys, bytes.TrimSpace(keyBytes)) + } + + more, err := utils.AskUserYesNo("Add more public keys?", false) + if err != nil { + return fmt.Errorf("failed to read confirmation: %v", err) + } + if !more { + break + } + } + } + + if len(pubKeys) == 0 { + return fmt.Errorf("at least one recipient public key is required") + } + + tmptxt := []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^") + + encrypted, err := crypt.EncryptBytesSSH(tmptxt, pubKeys) + if err != nil { + return err + } + + fmt.Println(output) + file, err := os.Create(output) + if err != nil { + return err + } + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + + // utils.ClearTerm() + fmt.Println("New note saved at:", file.Name()) + + return nil +} + +func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath string, editorString string) error { + useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true) + if err != nil { + return fmt.Errorf("failed to read confirmation: %v", err) + } + if !useDefault { + fmt.Println("Warning: you will not be able to open or edit this note again.") + fmt.Println(" make sure you've used the '--edit' flag.") + } + + addMore, err := utils.AskUserYesNo("Do you want to add more public keys?", false) + if err != nil { + return fmt.Errorf("failed to read confirmation: %v", err) + } + + pubKeys := [][]byte{} + + if useDefault { + pubKeys = append(pubKeys, defaultPubKeyBytes) + } + + 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 { + return fmt.Errorf("failed to read public key: %v", err) + } + + input := strings.TrimSpace(scanner.Text()) + if input == "" { + continue + } + + for part := range strings.SplitSeq(input, ",") { + part = strings.TrimSpace(part) + if part == "" { + continue + } + + var keyBytes []byte + if strings.HasPrefix(part, "ssh-") { + keyBytes = []byte(part) + } else { + var err error + keyBytes, err = os.ReadFile(part) + if err != nil { + return fmt.Errorf("failed to read public key file %s: %v", part, err) + } + } + + pubKeys = append(pubKeys, bytes.TrimSpace(keyBytes)) + } + + more, err := utils.AskUserYesNo("Add more public keys?", false) + if err != nil { + return fmt.Errorf("failed to read confirmation: %v", err) + } + if !more { + break + } + } + } + + if len(pubKeys) == 0 { + return fmt.Errorf("at least one recipient public key is required") + } + + noteInfo, err := os.Stat(output) + preExisting := err == nil + + initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + encrypted, err := crypt.EncryptBytesSSH(initialContent, pubKeys) + if err != nil { + return err + } + + file, err := os.Create(output) + if err != nil { + return err + } + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + file.Close() + + ciphertext, err := os.ReadFile(output) + if err != nil { + return err + } + + tmpFile, err := os.CreateTemp("", "eno-*") + if err != nil { + return err + } + + plaintext, err := crypt.DecryptBytesSSH(ciphertext, defaultPrivKeyPath) + if err != nil { + return err + } + crypt.TryMlock(plaintext) + defer clear(plaintext) + + defer deferredCleanTmpFile(tmpFile, len(plaintext)) + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write(plaintext) + if err != nil { + return err + } + + err = editor.OpenWithEditor(tmpFile.Name(), editorString) + if err != nil { + return err + } + + newContent, err := os.ReadFile(tmpFile.Name()) + if err != nil { + return err + } + crypt.TryMlock(newContent) + defer clear(newContent) + + os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + + encrypted, err = crypt.EncryptBytesSSH(newContent, pubKeys) if err != nil { return err } |