| 1 | package operator |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "math" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 10 | "github.com/sqids/sqids-go" |
| 11 | ) |
| 12 | |
| 13 | const OwnerLimit = 8 << 10 |
| 14 | |
| 15 | var teamCodec, teamCodecError = sqids.New() |
| 16 | |
| 17 | func TeamSQID(id int64) (string, error) { |
| 18 | if id <= 0 || teamCodecError != nil { |
| 19 | return "", ErrRefused |
| 20 | } |
| 21 | s, e := teamCodec.Encode([]uint64{uint64(id)}) |
| 22 | if e != nil || len(s) == 0 || len(s) > 64 { |
| 23 | return "", ErrRefused |
| 24 | } |
| 25 | return s, nil |
| 26 | } |
| 27 | func validTeam(id int64, s string) bool { |
| 28 | expected, e := TeamSQID(id) |
| 29 | return e == nil && s == expected |
| 30 | } |
| 31 | |
| 32 | func validTeamSQID(s string) bool { |
| 33 | if teamCodecError != nil || len(s) == 0 || len(s) > 64 { |
| 34 | return false |
| 35 | } |
| 36 | ids := teamCodec.Decode(s) |
| 37 | return len(ids) == 1 && ids[0] > 0 && ids[0] <= math.MaxInt64 && validTeam(int64(ids[0]), s) |
| 38 | } |
| 39 | func PersonRef(s string) bool { |
| 40 | if len(s) != 29 || !strings.HasPrefix(s, "pr_") { |
| 41 | return false |
| 42 | } |
| 43 | for _, c := range s[3:] { |
| 44 | if !(c >= 'a' && c <= 'z' || c >= '2' && c <= '7') { |
| 45 | return false |
| 46 | } |
| 47 | } |
| 48 | return true |
| 49 | } |
| 50 | |
| 51 | type OwnerReceipt struct { |
| 52 | Version int `json:"version"` |
| 53 | Purpose string `json:"purpose"` |
| 54 | OperationID string `json:"operation_id"` |
| 55 | Issuer string `json:"issuer"` |
| 56 | Directory string `json:"directory"` |
| 57 | TeamID int64 `json:"team_id"` |
| 58 | TeamSQID string `json:"team_sqid"` |
| 59 | PersonRef string `json:"person_ref"` |
| 60 | Role string `json:"role"` |
| 61 | Issued int64 `json:"issued_at"` |
| 62 | Expires int64 `json:"expires_at"` |
| 63 | } |
| 64 | |
| 65 | func (p OwnerReceipt) valid() bool { |
| 66 | return p.Version == 1 && p.Purpose == "owner-seed" && CanonicalToken(p.OperationID) && Origin(p.Issuer) && Origin(p.Directory) && validTeam(p.TeamID, p.TeamSQID) && PersonRef(p.PersonRef) && p.Role == "owner" && p.Issued > 0 && p.Expires > p.Issued && p.Expires-p.Issued <= 300 |
| 67 | } |
| 68 | |
| 69 | func SignOwner(k *family.Keypair, p OwnerReceipt) ([]byte, error) { |
| 70 | if !signingKey(k) || !p.valid() { |
| 71 | return nil, ErrRefused |
| 72 | } |
| 73 | payload, e := encode(p) |
| 74 | if e != nil { |
| 75 | return nil, e |
| 76 | } |
| 77 | sig, e := family.Sign(k, OwnerContext, payload) |
| 78 | if e != nil { |
| 79 | return nil, ErrUnavailable |
| 80 | } |
| 81 | out, e := encode(envelope{1, keyID(k.SignPub()), base64.RawURLEncoding.EncodeToString(payload), base64.RawURLEncoding.EncodeToString(sig)}) |
| 82 | if e != nil || len(out) > OwnerLimit { |
| 83 | return nil, ErrRefused |
| 84 | } |
| 85 | return out, nil |
| 86 | } |
| 87 | |
| 88 | type OwnerVerifier struct { |
| 89 | key []byte |
| 90 | kid, issuer, directory string |
| 91 | } |
| 92 | |
| 93 | func NewOwnerVerifier(publicKey, issuer, directory string) (*OwnerVerifier, error) { |
| 94 | k, e := public(publicKey) |
| 95 | if e != nil || !Origin(issuer) || !Origin(directory) || issuer == directory { |
| 96 | return nil, ErrRefused |
| 97 | } |
| 98 | return &OwnerVerifier{k, keyID(k), issuer, directory}, nil |
| 99 | } |
| 100 | func (v *OwnerVerifier) Verify(raw []byte, now time.Time) (OwnerReceipt, error) { |
| 101 | if v == nil || len(raw) == 0 || len(raw) > OwnerLimit { |
| 102 | return OwnerReceipt{}, ErrRefused |
| 103 | } |
| 104 | var env envelope |
| 105 | if strictCanonical(raw, &env) != nil || env.Version != 1 || env.KeyID != v.kid { |
| 106 | return OwnerReceipt{}, ErrRefused |
| 107 | } |
| 108 | p, ok := canonical(env.Payload, -1) |
| 109 | sig, ok2 := canonical(env.Signature, 64) |
| 110 | if !ok || !ok2 || len(p) > OwnerLimit || !family.Verify(v.key, OwnerContext, p, sig) { |
| 111 | return OwnerReceipt{}, ErrRefused |
| 112 | } |
| 113 | var receipt OwnerReceipt |
| 114 | if strictCanonical(p, &receipt) != nil || !receipt.valid() || receipt.Issuer != v.issuer || receipt.Directory != v.directory || now.Unix() <= 0 || receipt.Issued > now.Unix() || now.Unix() >= receipt.Expires { |
| 115 | return OwnerReceipt{}, ErrRefused |
| 116 | } |
| 117 | return receipt, nil |
| 118 | } |
| 119 | |