forked from oleiade/trousseau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trousseau.go
177 lines (146 loc) · 3.66 KB
/
trousseau.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package trousseau
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/oleiade/serrure/aes"
"github.com/oleiade/serrure/openpgp"
)
type Trousseau struct {
// Crypto public configuration attributes
CryptoType CryptoType `json:"crypto_type"`
CryptoAlgorithm CryptoAlgorithm `json:"crypto_algorithm"`
// Encrypted data private attribute
Data []byte `json:"_data"`
// Crypto algorithm to decryption
cryptoMapping map[CryptoAlgorithm]interface{}
}
func OpenTrousseau(fp string) (*Trousseau, error) {
var trousseau *Trousseau
var content []byte
var err error
content, err = ioutil.ReadFile(fp)
if err != nil {
return nil, err
}
err = json.Unmarshal(content, &trousseau)
if err != nil {
// Check if the content of the file matches with a legacy
// data store file format. Raise a proper error accordingly.
contentVersion := DiscoverVersion(content, VersionDiscoverClosures)
if contentVersion != "" {
return nil, fmt.Errorf("outdated data store file format detected: %s. "+
"You are currently using incompatible version: %s. "+
"Please upgrade the data store by using the upgrade command.",
contentVersion, TROUSSEAU_VERSION)
}
return nil, err
}
return trousseau, nil
}
func FromBytes(d []byte) (*Trousseau, error) {
var trousseau *Trousseau
var err error
err = json.Unmarshal(d, &trousseau)
if err != nil {
// Check if the content of the file matches with a legacy
// data store file format. Raise a proper error accordingly.
contentVersion := DiscoverVersion(d, VersionDiscoverClosures)
if contentVersion != "" {
return nil, fmt.Errorf("outdated data store file format detected: %s. "+
"You are currently using incompatible version: %s. "+
"Please upgrade the data store by using the upgrade command.",
contentVersion, TROUSSEAU_VERSION)
}
return nil, err
}
return trousseau, err
}
func (t *Trousseau) Decrypt() (*Store, error) {
var store Store
switch t.CryptoAlgorithm {
case GPG_ENCRYPTION:
passphrase, err := GetPassphrase()
if err != nil {
ErrorLogger.Fatal(err)
}
d, err := openpgp.NewOpenPGPDecrypter(GnupgSecring(), passphrase)
if err != nil {
return nil, err
}
pd, err := d.Decrypt(t.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(pd, &store)
if err != nil {
return nil, err
}
case AES_256_ENCRYPTION:
passphrase, err := GetPassphrase()
if err != nil {
ErrorLogger.Fatal(err)
}
d := aes.NewAES256Decrypter(passphrase)
pd, err := d.Decrypt(t.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(pd, &store)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("Invalid encryption method provided")
}
return &store, nil
}
func (t *Trousseau) Encrypt(store *Store) error {
switch t.CryptoAlgorithm {
case GPG_ENCRYPTION:
pd, err := json.Marshal(*store)
if err != nil {
return err
}
e, err := openpgp.NewOpenPGPEncrypter(GnupgPubring(), store.Meta.Recipients)
if err != nil {
return err
}
t.Data, err = e.Encrypt(pd)
if err != nil {
return err
}
case AES_256_ENCRYPTION:
pd, err := json.Marshal(*store)
if err != nil {
return err
}
passphrase, err := GetPassphrase()
if err != nil {
ErrorLogger.Fatal(err)
}
d, err := aes.NewAES256Encrypter(passphrase, nil)
if err != nil {
return err
}
t.Data, err = d.Encrypt(pd)
if err != nil {
return err
}
default:
return fmt.Errorf("Invalid encryption method provided")
}
return nil
}
func (t *Trousseau) Write(fp string) error {
jsonData, err := json.Marshal(t)
if err != nil {
return err
}
err = ioutil.WriteFile(fp, jsonData, os.FileMode(0600))
if err != nil {
return err
}
return nil
}