|
1 | 1 | package action |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "crypto/rand" |
5 | | - "crypto/rsa" |
6 | | - "crypto/x509" |
7 | | - "encoding/pem" |
8 | | - "errors" |
9 | 4 | "fmt" |
10 | | - "io/ioutil" |
11 | 5 |
|
12 | | - log "github.com/Sirupsen/logrus" |
13 | 6 | "github.com/malnick/cryptorious/config" |
14 | 7 | "github.com/malnick/cryptorious/vault" |
| 8 | + gc "github.com/rthornton128/goncurses" |
15 | 9 | ) |
16 | 10 |
|
17 | | -func Encrypt(key string, vs *vault.VaultSet, c config.Config) error { |
18 | | - pubkey, err := createPublicKey(c.PublicKeyPath) |
| 11 | +// Encrypt accepts a key and cryptorious config and returns an error |
| 12 | +// if found during the encryption process |
| 13 | +func Encrypt(key string, c config.Config) error { |
| 14 | + thisVault, err := vault.New(c.VaultPath) |
19 | 15 | if err != nil { |
20 | 16 | return err |
21 | 17 | } |
22 | 18 |
|
23 | | - thisVault, err := vault.New(c.VaultPath) |
| 19 | + clr, err := cleartextFromCurses() |
24 | 20 | if err != nil { |
25 | 21 | return err |
26 | 22 | } |
27 | 23 |
|
28 | | - if len(vs.Password) > 0 { |
29 | | - if encoded, err := encryptValue(pubkey, vs.Password); err == nil { |
30 | | - vs.Password = string(encoded) |
31 | | - } else { |
32 | | - return err |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - if len(vs.SecureNote) > 0 { |
37 | | - if encoded, err := encryptValue(pubkey, vs.SecureNote); err == nil { |
38 | | - vs.SecureNote = string(encoded) |
39 | | - } else { |
40 | | - return err |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - if len(vs.Username) > 0 { |
45 | | - vs.Username = vs.Username |
46 | | - } |
47 | | - |
48 | | - if err := thisVault.Update(key, vs); err != nil { |
| 24 | + vs, err := clr.Encrypt(c.KMSClient, c.KMSKeyARN) |
| 25 | + if err != nil { |
49 | 26 | return err |
50 | 27 | } |
51 | 28 |
|
52 | | - return nil |
| 29 | + return thisVault.Add(key, vs) |
53 | 30 | } |
54 | 31 |
|
55 | | -func encryptValue(pubkey interface{}, value string) ([]byte, error) { |
56 | | - // Encode the passed in value |
57 | | - log.Debugf("Encoding value: %s", value) |
58 | | - encodedValue, err := rsa.EncryptPKCS1v15(rand.Reader, pubkey.(*rsa.PublicKey), []byte(value)) |
59 | | - return encodedValue, err |
60 | | -} |
| 32 | +func cleartextFromCurses() (*vault.CleartextEntry, error) { |
| 33 | + clr := &vault.CleartextEntry{} |
61 | 34 |
|
62 | | -func createPublicKey(path string) (*rsa.PublicKey, error) { |
63 | | - pubData, err := ioutil.ReadFile(path) |
| 35 | + username, err := getValuesFor("Username") |
64 | 36 | if err != nil { |
65 | | - return nil, err |
| 37 | + return clr, err |
66 | 38 | } |
67 | | - log.Debug("using public key file: ", path) |
68 | | - log.Debug(string(pubData)) |
| 39 | + clr.Username = username |
69 | 40 |
|
70 | | - pubkey, err := createPublicKeyBlockCipher(pubData) |
| 41 | + password, err := getValuesFor("Password") |
71 | 42 | if err != nil { |
72 | | - return nil, err |
| 43 | + return clr, err |
73 | 44 | } |
| 45 | + clr.Password = password |
74 | 46 |
|
75 | | - return pubkey.(*rsa.PublicKey), nil |
| 47 | + note, err := getValuesFor("Secure Note") |
| 48 | + if err != nil { |
| 49 | + return clr, err |
| 50 | + } |
| 51 | + clr.SecureNote = note |
| 52 | + |
| 53 | + return clr, nil |
76 | 54 | } |
77 | 55 |
|
78 | | -func createPublicKeyBlockCipher(pubData []byte) (interface{}, error) { |
79 | | - // Create block cipher from RSA key |
80 | | - block, _ := pem.Decode(pubData) |
81 | | - // Ensure key is PEM encoded |
82 | | - if block == nil { |
83 | | - return nil, errors.New(fmt.Sprintf("Bad key data: %s, not PEM encoded", string(pubData))) |
84 | | - } |
85 | | - // Ensure this is actually a RSA pub key |
86 | | - if got, want := block.Type, "RSA PUBLIC KEY"; got != want { |
87 | | - return nil, errors.New(fmt.Sprintf("Unknown key type %q, want %q", got, want)) |
88 | | - } |
89 | | - // Lastly, create the public key using the new block |
90 | | - pubkey, err := x509.ParsePKIXPublicKey(block.Bytes) |
| 56 | +func getValuesFor(key string) (string, error) { |
| 57 | + stdscr, _ := gc.Init() |
| 58 | + defer gc.End() |
| 59 | + |
| 60 | + prompt := fmt.Sprintf("Enter %s: ", key) |
| 61 | + row, col := stdscr.MaxYX() |
| 62 | + row, col = (row/2)-1, (col-len(prompt))/2 |
| 63 | + stdscr.MovePrint(row, col, prompt) |
| 64 | + |
| 65 | + /* GetString will only retieve the specified number of characters. Any |
| 66 | + attempts by the user to enter more characters will elicit an audiable |
| 67 | + beep */ |
| 68 | + var value string |
| 69 | + value, err := stdscr.GetString(10000) |
91 | 70 | if err != nil { |
92 | | - return nil, err |
| 71 | + return value, err |
93 | 72 | } |
94 | | - return pubkey, nil |
| 73 | + |
| 74 | + // stdscr.Refresh() |
| 75 | + stdscr.GetChar() |
| 76 | + stdscr.Erase() |
| 77 | + |
| 78 | + return value, nil |
95 | 79 | } |
0 commit comments