From e48a02646f60b6fdff8db5c4105896756584ac63 Mon Sep 17 00:00:00 2001 From: ishaan Date: Sun, 19 Jul 2026 18:33:40 +0530 Subject: reorganize --- internal/crud/cli_edit.go | 24 +-- internal/crud/cli_new.go | 384 ------------------------------------------ internal/crud/cli_new_pass.go | 161 ++++++++++++++++++ internal/crud/cli_new_ssh.go | 234 +++++++++++++++++++++++++ 4 files changed, 396 insertions(+), 407 deletions(-) delete mode 100644 internal/crud/cli_new.go create mode 100644 internal/crud/cli_new_pass.go create mode 100644 internal/crud/cli_new_ssh.go (limited to 'internal/crud') diff --git a/internal/crud/cli_edit.go b/internal/crud/cli_edit.go index 50bb8d2..0683439 100644 --- a/internal/crud/cli_edit.go +++ b/internal/crud/cli_edit.go @@ -3,7 +3,6 @@ package crud import ( "fmt" "os" - "strconv" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/editor" @@ -90,28 +89,7 @@ func EditNoteSSH(notePath, privKeyPath, editorString string) error { 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) + headers, _, err := crypt.ParseFileHeaders(ciphertext) if err != nil { return fmt.Errorf("failed to read file headers: %v", err) } diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go deleted file mode 100644 index 8e56490..0000000 --- a/internal/crud/cli_new.go +++ /dev/null @@ -1,384 +0,0 @@ -package crud - -import ( - "bytes" - "fmt" - "os" - "strings" - - "gitlab.com/whiteboardguy/eno/internal/crypt" - "gitlab.com/whiteboardguy/eno/internal/editor" - "gitlab.com/whiteboardguy/eno/internal/utils" -) - -func NewNotePass(output string) error { - fmt.Printf("Enter the passphrase: ") - password, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - crypt.TryMlock(password) - defer clear(password) - - fmt.Printf("Confirm the passphrase: ") - passwordConfirm, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - crypt.TryMlock(passwordConfirm) - defer clear(passwordConfirm) - - if !bytes.Equal(password, passwordConfirm) { - return fmt.Errorf("passphrases do not match") - } - - encrypted, err := crypt.EncryptBytesPass( - append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...), - password, - ) - if err != nil { - return err - } - - fmt.Println(output) - file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return err - } - defer file.Close() - - _, err = file.Write(encrypted) - if err != nil { - return err - } - - file.Sync() - - // utils.ClearTerm() - fmt.Println("New note saved at:", file.Name()) - - return nil -} - -func NewNotePassEdit(output, editorString string) error { - fmt.Printf("Enter the passphrase: ") - password, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - crypt.TryMlock(password) - defer clear(password) - - fmt.Printf("Confirm the passphrase: ") - passwordConfirm, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - crypt.TryMlock(passwordConfirm) - defer clear(passwordConfirm) - - if !bytes.Equal(password, passwordConfirm) { - return fmt.Errorf("passphrases do not match") - } - - noteInfo, err := os.Stat(output) - preExisting := err == nil - - initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) - encrypted, err := crypt.EncryptBytesPass(initialContent, password) - if err != nil { - return err - } - - file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - 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 := utils.SecureTempFile() - if err != nil { - return err - } - - plaintext, err := crypt.DecryptBytesPass(ciphertext, password) - 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) - - 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 - } - - 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 PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, error) { - useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true) - if err != nil { - return nil, false, 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 nil, false, fmt.Errorf("failed to read confirmation: %v", err) - } - - pubKeys := [][]byte{} - - if useDefault { - pubKeys = append(pubKeys, defaultPubKeyBytes) - } - - if addMore { - for { - fmt.Println("Enter public keys (\033[1mcomma-separated for multiple\033[0m):") - fmt.Println("Example: ~/keys/bob.pub, ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AA..., bob@github, james@gitlab") - fmt.Print("Public Keys: ") - 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(utils.StdinScanner.Text()) - if input == "" { - continue - } - - for part := range strings.SplitSeq(input, ",") { - part = strings.TrimSpace(part) - if part == "" { - continue - } - - if strings.Contains(part, "@") && !strings.HasPrefix(part, "ssh-") { - parts := strings.Split(part, "@") - if len(parts) != 2 { - return nil, false, fmt.Errorf("unsupported source or invalid key format: %s", part) - } - - username, service := parts[0], parts[1] - body, err := utils.GetPubKeyFromService(username, service) - if err != nil { - return nil, false, fmt.Errorf("failed to get public key from %s: %v", part, err) - } - selectedKeys, err := selectServiceKeys(part, body) - if err != nil { - fmt.Printf("Warning: %s: %v\n", service, err) - continue - } - for _, k := range selectedKeys { - pubKeys = append(pubKeys, bytes.TrimSpace(k)) - } - 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 nil, false, 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 nil, false, fmt.Errorf("failed to read confirmation: %v", err) - } - if !more { - break - } - } - } - - return pubKeys, useDefault, nil -} - -func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { - pubKeys, _, err := PromptRecipientKeys(defaultPubKeyBytes) - if err != nil { - return err - } - if len(pubKeys) == 0 { - return fmt.Errorf("at least one recipient public key is required") - } - - tmptxt := append(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.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return err - } - defer file.Close() - - _, 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 { - pubKeys, useDefault, err := PromptRecipientKeys(defaultPubKeyBytes) - if err != nil { - return err - } - if len(pubKeys) == 0 { - return fmt.Errorf("at least one recipient public key is required") - } - - noteInfo, err := os.Stat(output) - preExisting := err == nil - - initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) - encrypted, noteKey, err := crypt.EncryptBytesSSH(initialContent, pubKeys) - if err != nil { - return err - } - crypt.TryMlock(noteKey) - defer clear(noteKey) - - file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - 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 := utils.SecureTempFile() - if err != nil { - return err - } - - var plaintext []byte - if useDefault { - plaintext, err = crypt.DecryptBytesSSH(ciphertext, defaultPrivKeyPath) - } else { - plaintext, err = crypt.DecryptBytesSSHWithKey(ciphertext, noteKey) - } - 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) - - 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 { - 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 -} diff --git a/internal/crud/cli_new_pass.go b/internal/crud/cli_new_pass.go new file mode 100644 index 0000000..c18ddc6 --- /dev/null +++ b/internal/crud/cli_new_pass.go @@ -0,0 +1,161 @@ +package crud + +import ( + "bytes" + "fmt" + "os" + + "gitlab.com/whiteboardguy/eno/internal/crypt" + "gitlab.com/whiteboardguy/eno/internal/editor" + "gitlab.com/whiteboardguy/eno/internal/utils" +) + +func NewNotePass(output string) error { + fmt.Printf("Enter the passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(password) + defer clear(password) + + fmt.Printf("Confirm the passphrase: ") + passwordConfirm, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(passwordConfirm) + defer clear(passwordConfirm) + + if !bytes.Equal(password, passwordConfirm) { + return fmt.Errorf("passphrases do not match") + } + + encrypted, err := crypt.EncryptBytesPass( + append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...), + password, + ) + if err != nil { + return err + } + + fmt.Println(output) + file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer file.Close() + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + + // utils.ClearTerm() + fmt.Println("New note saved at:", file.Name()) + + return nil +} + +func NewNotePassEdit(output, editorString string) error { + fmt.Printf("Enter the passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(password) + defer clear(password) + + fmt.Printf("Confirm the passphrase: ") + passwordConfirm, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(passwordConfirm) + defer clear(passwordConfirm) + + if !bytes.Equal(password, passwordConfirm) { + return fmt.Errorf("passphrases do not match") + } + + noteInfo, err := os.Stat(output) + preExisting := err == nil + + initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) + encrypted, err := crypt.EncryptBytesPass(initialContent, password) + if err != nil { + return err + } + + file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + 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 := utils.SecureTempFile() + if err != nil { + return err + } + + plaintext, err := crypt.DecryptBytesPass(ciphertext, password) + 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) + + 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 + } + + 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 +} diff --git a/internal/crud/cli_new_ssh.go b/internal/crud/cli_new_ssh.go new file mode 100644 index 0000000..5479490 --- /dev/null +++ b/internal/crud/cli_new_ssh.go @@ -0,0 +1,234 @@ +package crud + +import ( + "bytes" + "fmt" + "os" + "strings" + + "gitlab.com/whiteboardguy/eno/internal/crypt" + "gitlab.com/whiteboardguy/eno/internal/editor" + "gitlab.com/whiteboardguy/eno/internal/utils" +) + +func PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, error) { + useDefault, err := utils.AskUserYesNo("Do you want to encrypt the note with your own default Public Key?", true) + if err != nil { + return nil, false, 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 nil, false, fmt.Errorf("failed to read confirmation: %v", err) + } + + pubKeys := [][]byte{} + + if useDefault { + pubKeys = append(pubKeys, defaultPubKeyBytes) + } + + if addMore { + for { + fmt.Println("Enter public keys (\033[1mcomma-separated for multiple\033[0m):") + fmt.Println("Example: ~/keys/bob.pub, ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AA..., bob@github, james@gitlab") + fmt.Print("Public Keys: ") + 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(utils.StdinScanner.Text()) + if input == "" { + continue + } + + for part := range strings.SplitSeq(input, ",") { + part = strings.TrimSpace(part) + if part == "" { + continue + } + + if strings.Contains(part, "@") && !strings.HasPrefix(part, "ssh-") { + parts := strings.Split(part, "@") + if len(parts) != 2 { + return nil, false, fmt.Errorf("unsupported source or invalid key format: %s", part) + } + + username, service := parts[0], parts[1] + body, err := utils.GetPubKeyFromService(username, service) + if err != nil { + return nil, false, fmt.Errorf("failed to get public key from %s: %v", part, err) + } + selectedKeys, err := selectServiceKeys(part, body) + if err != nil { + fmt.Printf("Warning: %s: %v\n", service, err) + continue + } + for _, k := range selectedKeys { + pubKeys = append(pubKeys, bytes.TrimSpace(k)) + } + 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 nil, false, 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 nil, false, fmt.Errorf("failed to read confirmation: %v", err) + } + if !more { + break + } + } + } + + return pubKeys, useDefault, nil +} + +func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { + pubKeys, _, err := PromptRecipientKeys(defaultPubKeyBytes) + if err != nil { + return err + } + if len(pubKeys) == 0 { + return fmt.Errorf("at least one recipient public key is required") + } + + tmptxt := append(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.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + return err + } + defer file.Close() + + _, 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 { + pubKeys, useDefault, err := PromptRecipientKeys(defaultPubKeyBytes) + if err != nil { + return err + } + if len(pubKeys) == 0 { + return fmt.Errorf("at least one recipient public key is required") + } + + noteInfo, err := os.Stat(output) + preExisting := err == nil + + initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) + encrypted, noteKey, err := crypt.EncryptBytesSSH(initialContent, pubKeys) + if err != nil { + return err + } + crypt.TryMlock(noteKey) + defer clear(noteKey) + + file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + 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 := utils.SecureTempFile() + if err != nil { + return err + } + + var plaintext []byte + if useDefault { + plaintext, err = crypt.DecryptBytesSSH(ciphertext, defaultPrivKeyPath) + } else { + plaintext, err = crypt.DecryptBytesSSHWithKey(ciphertext, noteKey) + } + 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) + + 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 { + 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 +} -- cgit v1.3