package console import ( "crypto/aes" "crypto/cipher" "crypto/ecdh" "crypto/hkdf" "crypto/rand" "crypto/sha256" "encoding/base64" "encoding/binary" "errors" "strings" ) // The console never sees a secret's plaintext: `carlos secrets set` seals // on the operator's machine against the console's secrets_pubkey, and Alta // does the same. This is CARLOS's envelope (platform internal/appconfig/ // crypto.go), reimplemented because Alta cannot import the platform: // // v1 (unbound): ephPub(65) || nonce(12) || AES-256-GCM ciphertext // v2 (bound): "CSB2" || v1's layout, with (account, app) as GCM // associated data and a different HKDF info // // The console says which to write: GET /config answers secrets_account // only while it takes bound (v2) envelopes. const ( v1Info = "carlos-secrets-v1" v2Info = "carlos-secrets-v2" boundPrefix = "CSB2" ) func gcmFor(shared []byte, info string) (cipher.AEAD, error) { key, err := hkdf.Key(sha256.New, shared, nil, info, 32) if err != nil { return nil, err } block, err := aes.NewCipher(key) if err != nil { return nil, err } return cipher.NewGCM(block) } func bindingAAD(account, app string) []byte { var out []byte for _, f := range []string{boundPrefix, account, app} { out = binary.BigEndian.AppendUint32(out, uint32(len(f))) out = append(out, f...) } return out } // Seal seals plaintext for the console's public key. With account empty // it writes v1; otherwise v2 bound to (account, app). func Seal(pubB64, account, app string, plaintext []byte) (string, error) { if len(plaintext) == 0 { return "", errors.New("console: refusing to seal an empty secret") } raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(pubB64)) if err != nil { return "", errors.New("console: secrets_pubkey is not base64") } pub, err := ecdh.P256().NewPublicKey(raw) if err != nil { return "", errors.New("console: secrets_pubkey is not a P-256 point") } eph, err := ecdh.P256().GenerateKey(rand.Reader) if err != nil { return "", err } nonce := make([]byte, 12) if _, err := rand.Read(nonce); err != nil { return "", err } shared, err := eph.ECDH(pub) if err != nil { return "", err } prefix, info, aad := "", v1Info, []byte(nil) if account != "" { prefix, info, aad = boundPrefix, v2Info, bindingAAD(account, app) } gcm, err := gcmFor(shared, info) if err != nil { return "", err } out := append([]byte(prefix), eph.PublicKey().Bytes()...) out = append(out, nonce...) out = gcm.Seal(out, nonce, plaintext, aad) return base64.StdEncoding.EncodeToString(out), nil } // Open is Seal's inverse, for tests (the fake console holds the private // half, as a box does). func Open(priv *ecdh.PrivateKey, account, app, envelope string) ([]byte, error) { raw, err := base64.StdEncoding.DecodeString(envelope) if err != nil { return nil, err } info, aad := v1Info, []byte(nil) if strings.HasPrefix(string(raw), boundPrefix) { raw = raw[len(boundPrefix):] info, aad = v2Info, bindingAAD(account, app) } if len(raw) < 65+12+16 { return nil, errors.New("console: envelope too short") } eph, err := ecdh.P256().NewPublicKey(raw[:65]) if err != nil { return nil, err } shared, err := priv.ECDH(eph) if err != nil { return nil, err } gcm, err := gcmFor(shared, info) if err != nil { return nil, err } return gcm.Open(nil, raw[65:77], raw[77:], aad) }