// Package relay is the platform STUN/TURN relay: the app-facing credential // contract (spec §2), key derivation (§4), and the server (§3). package relay import ( "crypto/hmac" "crypto/sha256" "encoding/base64" "fmt" "io" "regexp" "strconv" "strings" "time" "golang.org/x/crypto/hkdf" ) const ( MaxCredentialLife = 4 * time.Hour hkdfSalt = "carlos-relay-v1" ) var ( expiryRe = regexp.MustCompile(`^[0-9]+$`) accountRe = regexp.MustCompile(`^[a-z0-9]+$`) appRe = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) incarnationRe = regexp.MustCompile(`^[0-9a-f]{16}$`) kidRe = regexp.MustCompile(`^[a-z0-9]{1,8}$`) ) // DeriveKey is HKDF-SHA256(master, salt "carlos-relay-v1", // info account||0x00||app||0x00||incarnation, 32). func DeriveKey(master []byte, account, app, incarnation string) []byte { info := []byte(account + "\x00" + app + "\x00" + incarnation) r := hkdf.New(sha256.New, master, []byte(hkdfSalt), info) key := make([]byte, 32) if _, err := io.ReadFull(r, key); err != nil { panic("relay: hkdf: " + err.Error()) // cannot fail for 32 bytes } return key } // ValidKid reports whether s is usable as a key id: 1-8 lowercase letters or // digits, the same alphabet ParseUsername and ParseMasters hold a kid to. // Exported so a WRITER of masters — `carlos relay keygen`, and the console // parsing its own CARLOS_RELAY_MASTERS_ — refuses a typo where the // operator can still fix it, instead of minting usernames every relay // silently declines. func ValidKid(s string) bool { return kidRe.MatchString(s) } func Suffix(account, app, incarnation, kid string) string { return account + ":" + app + ":" + incarnation + ":" + kid } // Credential is base64(HMAC-SHA256(key, username)), the TURN password. func Credential(key []byte, username string) string { h := hmac.New(sha256.New, key) h.Write([]byte(username)) return base64.StdEncoding.EncodeToString(h.Sum(nil)) } type Username struct { Expiry time.Time Account, App, Incarnation, Kid string } // ParseUsername splits "::::" and // validates each field's alphabet. It does not check expiry against now; // the auth handler does that with its own clock. func ParseUsername(s string) (Username, error) { f := strings.Split(s, ":") if len(f) != 5 { return Username{}, fmt.Errorf("relay: username has %d fields, want 5", len(f)) } if !expiryRe.MatchString(f[0]) { return Username{}, fmt.Errorf("relay: bad expiry %q", f[0]) } exp, err := strconv.ParseInt(f[0], 10, 64) if err != nil { return Username{}, fmt.Errorf("relay: bad expiry %q", f[0]) } switch { case !accountRe.MatchString(f[1]): return Username{}, fmt.Errorf("relay: bad account %q", f[1]) case !appRe.MatchString(f[2]): return Username{}, fmt.Errorf("relay: bad app %q", f[2]) case !incarnationRe.MatchString(f[3]): return Username{}, fmt.Errorf("relay: bad incarnation %q", f[3]) case !kidRe.MatchString(f[4]): return Username{}, fmt.Errorf("relay: bad kid %q", f[4]) } return Username{Expiry: time.Unix(exp, 0).UTC(), Account: f[1], App: f[2], Incarnation: f[3], Kid: f[4]}, nil }