oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "crypto/aes"
5 "crypto/cipher"
6 "crypto/ecdh"
7 "crypto/hkdf"
8 "crypto/rand"
9 "crypto/sha256"
10 "encoding/base64"
11 "encoding/binary"
12 "errors"
13 "strings"
14)
15
16// The console never sees a secret's plaintext: `carlos secrets set` seals
17// on the operator's machine against the console's secrets_pubkey, and Alta
18// does the same. This is CARLOS's envelope (platform internal/appconfig/
19// crypto.go), reimplemented because Alta cannot import the platform:
20//
21// v1 (unbound): ephPub(65) || nonce(12) || AES-256-GCM ciphertext
22// v2 (bound): "CSB2" || v1's layout, with (account, app) as GCM
23// associated data and a different HKDF info
24//
25// The console says which to write: GET /config answers secrets_account
26// only while it takes bound (v2) envelopes.
27const (
28 v1Info = "carlos-secrets-v1"
29 v2Info = "carlos-secrets-v2"
30 boundPrefix = "CSB2"
31)
32
33func gcmFor(shared []byte, info string) (cipher.AEAD, error) {
34 key, err := hkdf.Key(sha256.New, shared, nil, info, 32)
35 if err != nil {
36 return nil, err
37 }
38 block, err := aes.NewCipher(key)
39 if err != nil {
40 return nil, err
41 }
42 return cipher.NewGCM(block)
43}
44
45func bindingAAD(account, app string) []byte {
46 var out []byte
47 for _, f := range []string{boundPrefix, account, app} {
48 out = binary.BigEndian.AppendUint32(out, uint32(len(f)))
49 out = append(out, f...)
50 }
51 return out
52}
53
54// Seal seals plaintext for the console's public key. With account empty
55// it writes v1; otherwise v2 bound to (account, app).
56func Seal(pubB64, account, app string, plaintext []byte) (string, error) {
57 if len(plaintext) == 0 {
58 return "", errors.New("console: refusing to seal an empty secret")
59 }
60 raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(pubB64))
61 if err != nil {
62 return "", errors.New("console: secrets_pubkey is not base64")
63 }
64 pub, err := ecdh.P256().NewPublicKey(raw)
65 if err != nil {
66 return "", errors.New("console: secrets_pubkey is not a P-256 point")
67 }
68 eph, err := ecdh.P256().GenerateKey(rand.Reader)
69 if err != nil {
70 return "", err
71 }
72 nonce := make([]byte, 12)
73 if _, err := rand.Read(nonce); err != nil {
74 return "", err
75 }
76 shared, err := eph.ECDH(pub)
77 if err != nil {
78 return "", err
79 }
80 prefix, info, aad := "", v1Info, []byte(nil)
81 if account != "" {
82 prefix, info, aad = boundPrefix, v2Info, bindingAAD(account, app)
83 }
84 gcm, err := gcmFor(shared, info)
85 if err != nil {
86 return "", err
87 }
88 out := append([]byte(prefix), eph.PublicKey().Bytes()...)
89 out = append(out, nonce...)
90 out = gcm.Seal(out, nonce, plaintext, aad)
91 return base64.StdEncoding.EncodeToString(out), nil
92}
93
94// Open is Seal's inverse, for tests (the fake console holds the private
95// half, as a box does).
96func Open(priv *ecdh.PrivateKey, account, app, envelope string) ([]byte, error) {
97 raw, err := base64.StdEncoding.DecodeString(envelope)
98 if err != nil {
99 return nil, err
100 }
101 info, aad := v1Info, []byte(nil)
102 if strings.HasPrefix(string(raw), boundPrefix) {
103 raw = raw[len(boundPrefix):]
104 info, aad = v2Info, bindingAAD(account, app)
105 }
106 if len(raw) < 65+12+16 {
107 return nil, errors.New("console: envelope too short")
108 }
109 eph, err := ecdh.P256().NewPublicKey(raw[:65])
110 if err != nil {
111 return nil, err
112 }
113 shared, err := priv.ECDH(eph)
114 if err != nil {
115 return nil, err
116 }
117 gcm, err := gcmFor(shared, info)
118 if err != nil {
119 return nil, err
120 }
121 return gcm.Open(nil, raw[65:77], raw[77:], aad)
122}
123