This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
paste.go
401 lines (326 loc) · 9.57 KB
/
paste.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"crypto/aes"
"crypto/cipher"
"github.com/DHowett/go-xattr"
"golang.org/x/crypto/scrypt"
"io"
"os"
"path/filepath"
"time"
)
const CURRENT_ENCRYPTION_METHOD string = "2"
type PasteStore interface {
GenerateNewPasteID(bool) (PasteID, error)
New(bool) (*Paste, error)
Get(PasteID, []byte) (*Paste, error)
Save(*Paste) error
Destroy(*Paste) error
EncryptionKeyForPasteWithPassword(*Paste, string) []byte
readStream(*Paste) (*PasteReader, error)
writeStream(*Paste) (*PasteWriter, error)
}
type PasteID string
func (id PasteID) String() string {
return string(id)
}
func PasteIDFromString(s string) PasteID {
return PasteID(s)
}
type PasteEncryptedError struct {
ID PasteID
}
func (e PasteEncryptedError) Error() string {
return "Paste " + e.ID.String() + " is encrypted."
}
type PasteInvalidKeyError PasteEncryptedError
func (e PasteInvalidKeyError) Error() string { return "" }
type PasteNotFoundError struct {
ID PasteID
}
func (e PasteNotFoundError) Error() string {
return "Paste " + e.ID.String() + " was not found."
}
type PasteReader struct {
io.ReadCloser
paste *Paste
}
func (pr *PasteReader) Close() error {
return pr.ReadCloser.Close()
}
type PasteWriter struct {
io.WriteCloser
paste *Paste
}
func (pr *PasteWriter) Close() error {
pr.paste.Save()
return pr.WriteCloser.Close()
}
type Paste struct {
ID PasteID
Language *Language
Encrypted bool
Expiration string
Title string
store PasteStore
mtime time.Time
exptime time.Time
encryptionKey []byte
encryptionSalt []byte
encryptionMethod string
}
func (p *Paste) Save() error {
return p.store.Save(p)
}
func (p *Paste) Destroy() error {
return p.store.Destroy(p)
}
func (p *Paste) Reader() (*PasteReader, error) {
return p.store.readStream(p)
}
func (p *Paste) Writer() (*PasteWriter, error) {
return p.store.writeStream(p)
}
func (p *Paste) LastModified() time.Time {
return p.mtime
}
func (p *Paste) ExpirationTime() time.Time {
return p.exptime
}
func (p *Paste) SetEncryptionKey(key []byte) {
p.encryptionKey = key
p.Encrypted = (key != nil)
}
func (p *Paste) EncryptionKeyWithPassword(password string) []byte {
return p.store.EncryptionKeyForPasteWithPassword(p, password)
}
type PasteCallback func(*Paste)
type FilesystemPasteStore struct {
PasteUpdateCallback PasteCallback
PasteDestroyCallback PasteCallback
path string
}
func noopPasteCallback(p *Paste) {}
func NewFilesystemPasteStore(path string) *FilesystemPasteStore {
return &FilesystemPasteStore{
path: path,
PasteUpdateCallback: PasteCallback(noopPasteCallback),
PasteDestroyCallback: PasteCallback(noopPasteCallback),
}
}
func (store *FilesystemPasteStore) GenerateNewPasteID(encrypted bool) (PasteID, error) {
nbytes, idlen := 4, 5
if encrypted {
nbytes, idlen = 5, 8
}
for {
s, err := generateRandomBase32String(nbytes, idlen)
if _, staterr := os.Stat(store.filenameForID(PasteIDFromString(s))); os.IsNotExist(staterr) {
return PasteIDFromString(s), err
}
}
}
func (store *FilesystemPasteStore) filenameForID(id PasteID) string {
return filepath.Join(store.path, id.String())
}
func (store *FilesystemPasteStore) New(encrypted bool) (p *Paste, err error) {
id, err := store.GenerateNewPasteID(encrypted)
if err != nil {
panic(err)
}
p = &Paste{ID: id, store: store}
if encrypted {
p.encryptionSalt, _ = generateRandomBytes(16)
p.encryptionMethod = CURRENT_ENCRYPTION_METHOD
}
return
}
func putMetadata(fn string, name string, value string) error {
return xattr.Setxattr(fn, "user.paste."+name, []byte(value), 0, 0)
}
func getMetadata(fn string, name string, dflt string) string {
bytes, err := xattr.Getxattr(fn, "user.paste."+name, 0, 0)
if err != nil {
return dflt
}
return string(bytes)
}
func (store *FilesystemPasteStore) Get(id PasteID, key []byte) (p *Paste, err error) {
filename := store.filenameForID(id)
stat, err := os.Stat(filename)
if err != nil {
err = PasteNotFoundError{ID: id}
return
}
paste := &Paste{ID: id, store: store, mtime: stat.ModTime()}
hmac := getMetadata(filename, "hmac", "")
paste.encryptionMethod = getMetadata(filename, "encryption_version", "")
if hmac != "" {
if paste.encryptionMethod == "" {
paste.encryptionMethod = "1"
}
paste.Encrypted = true
salt := getMetadata(filename, "encryption_salt", "")
if salt == "" {
paste.encryptionSalt = []byte(paste.ID.String())
} else {
saltb, e := base32Encoder.DecodeString(salt)
if e != nil {
err = e
return
}
paste.encryptionSalt = saltb
}
err = PasteEncryptedError{ID: id}
if key != nil {
err = nil
hmacBytes, e := base32Encoder.DecodeString(hmac)
if e != nil {
err = e
return
}
MACMessage := encryptionMethodHandlers[paste.encryptionMethod].generateMACMessage(paste)
ok := checkMAC(MACMessage, hmacBytes, key)
if !ok {
err = PasteInvalidKeyError{ID: id}
return
}
paste.encryptionKey = key
err = nil
}
}
paste.Language = LanguageNamed(getMetadata(filename, "language", "text"))
paste.Expiration = getMetadata(filename, "expiration", "")
paste.Title = getMetadata(filename, "title", "")
if paste.Expiration != "" {
if dur, err := ParseDuration(paste.Expiration); err == nil {
paste.exptime = paste.mtime.Add(dur)
}
}
store.PasteUpdateCallback(paste)
p = paste
return
}
func (store *FilesystemPasteStore) Save(p *Paste) error {
filename := store.filenameForID(p.ID)
if err := putMetadata(filename, "language", p.Language.ID); err != nil {
return err
}
if p.Expiration != "" {
if err := putMetadata(filename, "expiration", p.Expiration); err != nil {
return err
}
}
if err := putMetadata(filename, "title", p.Title); err != nil {
return err
}
if p.Encrypted {
MACMessage := encryptionMethodHandlers[p.encryptionMethod].generateMACMessage(p)
hmacBytes := constructMAC([]byte(MACMessage), p.encryptionKey)
hmac := base32Encoder.EncodeToString(hmacBytes)
if err := putMetadata(filename, "hmac", hmac); err != nil {
return err
}
if err := putMetadata(filename, "encryption_version", p.encryptionMethod); err != nil {
return err
}
if err := putMetadata(filename, "encryption_salt", base32Encoder.EncodeToString(p.encryptionSalt)); err != nil {
return err
}
}
store.PasteUpdateCallback(p)
return nil
}
func (store *FilesystemPasteStore) Destroy(p *Paste) error {
err := os.Remove(store.filenameForID(p.ID))
if err != nil {
return err
}
store.PasteDestroyCallback(p)
return nil
}
func (store *FilesystemPasteStore) EncryptionKeyForPasteWithPassword(p *Paste, password string) []byte {
if password == "" {
return nil
}
key, err := scrypt.Key([]byte(password), []byte(p.encryptionSalt), 16384, 8, 1, 32)
if err != nil {
panic(err)
}
return key
}
func (store *FilesystemPasteStore) readStream(p *Paste) (*PasteReader, error) {
filename := store.filenameForID(p.ID)
var r io.ReadCloser
var err error
if r, err = os.Open(filename); err != nil {
return nil, err
}
if p.Encrypted {
r = encryptionMethodHandlers[p.encryptionMethod].encryptedReadWrapper(p, r)
}
return &PasteReader{ReadCloser: r, paste: p}, nil
}
func (store *FilesystemPasteStore) writeStream(p *Paste) (*PasteWriter, error) {
filename := store.filenameForID(p.ID)
var w io.WriteCloser
var err error
if w, err = os.Create(filename); err != nil {
return nil, err
}
// N.B. We always write using the newest encryption method.
if p.Encrypted {
w = encryptionMethodHandlers[p.encryptionMethod].encryptedWriteWrapper(p, w)
}
return &PasteWriter{WriteCloser: w, paste: p}, nil
}
type EncryptionMethodHandlers struct {
generateMACMessage func(*Paste) []byte
encryptedReadWrapper func(*Paste, io.ReadCloser) io.ReadCloser
encryptedWriteWrapper func(*Paste, io.WriteCloser) io.WriteCloser
}
var encryptionMethodHandlers = map[string]EncryptionMethodHandlers{
"": EncryptionMethodHandlers{
generateMACMessage: func(p *Paste) []byte { return []byte{} },
encryptedReadWrapper: func(p *Paste, r io.ReadCloser) io.ReadCloser { return r },
encryptedWriteWrapper: func(p *Paste, w io.WriteCloser) io.WriteCloser { return w },
},
"1": EncryptionMethodHandlers{
generateMACMessage: func(p *Paste) []byte {
return []byte(p.ID.String())
},
encryptedReadWrapper: func(p *Paste, r io.ReadCloser) io.ReadCloser {
blockCipher, _ := aes.NewCipher(p.encryptionKey)
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(blockCipher, iv[:])
streamReader := &cipher.StreamReader{S: stream, R: r}
return &ReadCloser{Reader: streamReader, Closer: r}
},
encryptedWriteWrapper: func(p *Paste, w io.WriteCloser) io.WriteCloser {
blockCipher, _ := aes.NewCipher(p.encryptionKey)
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(blockCipher, iv[:])
streamWriter := &cipher.StreamWriter{S: stream, W: w}
return &WriteCloser{Writer: streamWriter, Closer: w}
},
},
"2": EncryptionMethodHandlers{
generateMACMessage: func(p *Paste) []byte {
return append([]byte(p.ID.String()), p.encryptionSalt...)
},
encryptedReadWrapper: func(p *Paste, r io.ReadCloser) io.ReadCloser {
blockCipher, _ := aes.NewCipher(p.encryptionKey)
var iv [aes.BlockSize]byte
stream := cipher.NewCTR(blockCipher, iv[:])
streamReader := &cipher.StreamReader{S: stream, R: r}
return &ReadCloser{Reader: streamReader, Closer: r}
},
encryptedWriteWrapper: func(p *Paste, w io.WriteCloser) io.WriteCloser {
blockCipher, _ := aes.NewCipher(p.encryptionKey)
var iv [aes.BlockSize]byte
stream := cipher.NewCTR(blockCipher, iv[:])
streamWriter := &cipher.StreamWriter{S: stream, W: w}
return &WriteCloser{Writer: streamWriter, Closer: w}
},
},
}