-
Notifications
You must be signed in to change notification settings - Fork 3
/
ser.go
178 lines (151 loc) · 5.21 KB
/
ser.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
178
// Implements helper routines useful for de/serialization for both the stationary and plainsight states in order to
// securely JSONify secret store state for storage.
package ghostpass
import (
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/hex"
"encoding/json"
"io/ioutil"
)
// Helper function that converts a stationary persistent store back into a `SecretStore` for interaction.
// Putting the store in stationary mode preserves more state than plainsight mode, so not much decryption is needed.
func StationaryUnmarshal(checksum [32]byte, serialized []byte) (*SecretStore, error) {
// turn the serialized JSON back into a partially initialized state for a SecretStore
var ss struct {
Version int `json:"version"`
StoreState string `json:"state"`
Name string `json:"name"`
Fields map[string]*Field `json:"fields"`
}
err := json.Unmarshal(serialized, &ss)
if err != nil {
return nil, err
}
// no need to decrypt service, since this it's not encrypted. We are also
// not making a copy since we are just mutating the state of the fields
for _, field := range ss.Fields {
// rederive attributes of partial field
if err := field.RederiveAuthPair(checksum[:]); err != nil {
return nil, err
}
}
// return the SecretStore as if nothing changed
return &SecretStore{
Version: ss.Version,
StoreState: ss.StoreState,
Name: ss.Name,
SymmetricKey: checksum[:],
Fields: ss.Fields,
}, nil
}
// Helper routine that prepares a secret store from an exported plainsight
// distribution. Since the state stored on disk does not contain any remnants of the auth
// credentials per field, this unmarshaller rederives that using the given symmetric key.
func PlainsightUnmarshal(checksum [32]byte, encoded []byte) (*SecretStore, error) {
// decode from base64
compressed, err := base64.StdEncoding.DecodeString(string(encoded))
if err != nil {
return nil, err
}
// decompress the compressed input before deserializing
reader, err := zlib.NewReader(bytes.NewReader(compressed))
if err != nil {
return nil, err
}
// parse out serialized JSON plainsight store
serialized, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
// turn the serialized JSON back into a partially initialized state for a SecretStore
var ss struct {
Version int `json:"version"`
StoreState string `json:"state"`
Name string `json:"name"`
Fields map[string][]byte `json:"fields"`
}
if err := json.Unmarshal(serialized, &ss); err != nil {
return nil, err
}
// create new semi-unencrypted mapping
fields := make(map[string]*Field)
for servicekey, secret := range ss.Fields {
// decode hex for key
dec, err := hex.DecodeString(servicekey)
if err != nil {
return nil, err
}
// decrypt service key if store file was plainsight exported
service, err := BoxDecrypt(checksum[:], []byte(dec))
if err != nil {
return nil, err
}
// reinitialize field from compressed secret
field, err := ReconstructField(checksum[:], secret)
if err != nil {
return nil, err
}
// decomprethe string representation for secrets back into a field
fields[string(service)] = field
delete(ss.Fields, servicekey)
}
// return the SecretStore as if nothing changed
return &SecretStore{
Version: ss.Version,
StoreState: StoreStationary,
Name: ss.Name,
SymmetricKey: checksum[:],
Fields: fields,
}, nil
}
// Helper routine that helps prepare a secret store to be plainsight distributable, by
// incorporating indistinguishability to all entries, stripping the symmetric key checksum,
// compressing the final store, and applying one-time pads for deniability (TODO).
func (ss *SecretStore) PlainsightMarshal() ([]byte, error) {
// stores a final compressed mapping for the secret store's fields, where
// keys are encrypted for indistinguishability and a compressed form of the credential pair
// is also created to map against for serialization.
encfields := make(map[string][]byte)
// encrypt all the service keys for indistinguishability
for service, field := range ss.Fields {
// encrypt the service key for indistinguishability
encservice, err := BoxEncrypt(ss.SymmetricKey, []byte(service))
if err != nil {
return nil, err
}
// TODO: if deniable secrets are found, apply one-time pad to mutate secret
secret := field.AuthPair
// store the new encrypted entry
enc := hex.EncodeToString(encservice)
encfields[enc] = secret
}
// serialize into a byte array for compression
data, err := json.Marshal(&struct {
Version int `json:"version"`
StoreState string `json:"state"`
Name string `json:"name"`
Fields map[string][]byte `json:"fields"`
}{
Version: Version,
StoreState: StorePlainsight,
Name: ss.Name,
Fields: encfields,
})
if err != nil {
return nil, err
}
// apply zlib compression
var buf bytes.Buffer
gz := zlib.NewWriter(&buf)
if _, err := gz.Write(data); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
// finalize encoded stream for return
res := base64.StdEncoding.EncodeToString(buf.Bytes())
return []byte(res), nil
}