oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "crypto/aes"
5 "crypto/cipher"
6 "crypto/rand"
7 "crypto/sha256"
8 "encoding/base64"
9 "encoding/binary"
10 "encoding/hex"
11 "errors"
12 "strings"
13
14 "github.com/google/uuid"
15)
16
17const contentEncoding = 1
18const contentUpdateKind byte = 1
19const contentSnapshotKind byte = 2
20const contentKeyCheckKind byte = 3
21
22// contentReportKind seals an import report (doc_import_reports). Its own
23// kind byte, so a report's ciphertext can never open as an update or a
24// snapshot of the same document, nor one of those as a report.
25const contentReportKind byte = 4
26
27var developmentContentKey = sha256.Sum256([]byte("docs-development-content-key-not-a-secret"))
28var errContentKey = errors.New("docs: invalid content key")
29var errContentAuthentication = errors.New("docs: content authentication failed")
30var errContentEncoding = errors.New("docs: unsupported content encoding")
31
32type contentCipher struct {
33 aead cipher.AEAD
34 fingerprint string
35}
36
37func newContentCipher(encoded, env string) (*contentCipher, error) {
38 encoded = strings.TrimSpace(encoded)
39 var key []byte
40 if encoded == "" && env == "development" {
41 key = developmentContentKey[:]
42 } else {
43 var err error
44 key, err = base64.StdEncoding.Strict().DecodeString(encoded)
45 if err != nil || len(key) != 32 {
46 return nil, errContentKey
47 }
48 }
49 fingerprint := sha256.Sum256(key)
50 devFingerprint := sha256.Sum256(developmentContentKey[:])
51 if env != "development" && fingerprint == devFingerprint {
52 return nil, errContentKey
53 }
54 block, err := aes.NewCipher(key)
55 if err != nil {
56 return nil, errContentKey
57 }
58 aead, err := cipher.NewGCM(block)
59 if err != nil {
60 return nil, err
61 }
62 return &contentCipher{aead: aead, fingerprint: hex.EncodeToString(fingerprint[:])}, nil
63}
64
65func contentAAD(kind byte, guid string, seq int64, abi uint32) ([]byte, error) {
66 id, err := uuid.Parse(guid)
67 if err != nil || seq < 0 {
68 return nil, errContentAuthentication
69 }
70 aad := append([]byte("oficina/docs/content/v1"), kind)
71 aad = append(aad, id[:]...)
72 aad = binary.BigEndian.AppendUint64(aad, uint64(seq))
73 aad = binary.BigEndian.AppendUint32(aad, abi)
74 return aad, nil
75}
76
77func (c *contentCipher) seal(kind byte, guid string, seq int64, abi uint32, plaintext []byte) ([]byte, error) {
78 aad, err := contentAAD(kind, guid, seq, abi)
79 if err != nil {
80 return nil, err
81 }
82 nonce := make([]byte, c.aead.NonceSize())
83 if _, err := rand.Read(nonce); err != nil {
84 return nil, err
85 }
86 return c.aead.Seal(nonce, nonce, plaintext, aad), nil
87}
88
89func (c *contentCipher) open(kind byte, guid string, seq int64, abi uint32, sealed []byte) ([]byte, error) {
90 aad, err := contentAAD(kind, guid, seq, abi)
91 if err != nil || len(sealed) < c.aead.NonceSize()+c.aead.Overhead() {
92 return nil, errContentAuthentication
93 }
94 n := c.aead.NonceSize()
95 plaintext, err := c.aead.Open(nil, sealed[:n], sealed[n:], aad)
96 if err != nil {
97 return nil, errContentAuthentication
98 }
99 return plaintext, nil
100}
101