| 1 | //go:build linux |
| 2 | |
| 3 | package home |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "crypto/sha256" |
| 9 | "encoding/base64" |
| 10 | "encoding/hex" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/home/internal/home/directorydata" |
| 16 | "amadan.net/oficina/home/internal/home/seal" |
| 17 | "amadan.net/oficina/home/internal/operator" |
| 18 | "amadan.net/rastrillo/rastrillo/assertion" |
| 19 | json "github.com/go-json-experiment/json" |
| 20 | ) |
| 21 | |
| 22 | // RunOperatorCommand has no serving startup, migration, mailer or logger. |
| 23 | func RunOperatorCommand(ctx context.Context, path string) (code int) { |
| 24 | code = 3 |
| 25 | defer func() { |
| 26 | if recover() != nil { |
| 27 | code = 3 |
| 28 | } |
| 29 | }() |
| 30 | if ctx == nil || !filepath.IsAbs(path) || filepath.Clean(path) != path || path == "/" { |
| 31 | return 2 |
| 32 | } |
| 33 | material, err := loadOperatorMaterial() |
| 34 | defer clear(material.MAC) |
| 35 | if err != nil { |
| 36 | return 2 |
| 37 | } |
| 38 | ctx, cancel := context.WithTimeout(ctx, 10*time.Minute) |
| 39 | defer cancel() |
| 40 | result, err := operator.RunCommand(ctx, path, material) |
| 41 | if err == operator.ErrRefused { |
| 42 | return 2 |
| 43 | } |
| 44 | if err != nil { |
| 45 | return 3 |
| 46 | } |
| 47 | if result.Write(ctx) != nil { |
| 48 | return 3 |
| 49 | } |
| 50 | return 0 |
| 51 | } |
| 52 | |
| 53 | func loadOperatorMaterial() (operator.CommandMaterial, error) { |
| 54 | var material operator.CommandMaterial |
| 55 | role := os.Getenv("HOME_ROLE") |
| 56 | if role != "credential" && role != "directory" { |
| 57 | return material, operator.ErrRefused |
| 58 | } |
| 59 | origins, err := LoadOrigins() |
| 60 | if err != nil { |
| 61 | return material, operator.ErrRefused |
| 62 | } |
| 63 | raw, err := exchangeJSON("HOME_OPERATOR_MATERIAL", 16384) |
| 64 | if err != nil || raw == "" { |
| 65 | return material, operator.ErrRefused |
| 66 | } |
| 67 | dek, err := key32("HOME_DEK") |
| 68 | if err != nil { |
| 69 | return material, operator.ErrRefused |
| 70 | } |
| 71 | defer clear(dek[:]) |
| 72 | sealer, err := seal.New(dek[:]) |
| 73 | if err != nil { |
| 74 | return material, operator.ErrRefused |
| 75 | } |
| 76 | check, err := operatorSchemaCheck(role) |
| 77 | if err != nil { |
| 78 | return material, err |
| 79 | } |
| 80 | material = operator.CommandMaterial{Role: role, Sealer: sealer, CheckSchema: check} |
| 81 | if role == "credential" { |
| 82 | var cfg credentialOperatorMaterial |
| 83 | if json.Unmarshal([]byte(raw), &cfg, json.RejectUnknownMembers(true)) != nil || cfg.Version != 1 || cfg.Role != role { |
| 84 | return material, operator.ErrRefused |
| 85 | } |
| 86 | material.MAC, err = operatorMAC(cfg.MAC) |
| 87 | if err != nil || bytes.Equal(material.MAC, dek[:]) { |
| 88 | return material, operator.ErrRefused |
| 89 | } |
| 90 | pepper, err := key32("HOME_PEPPER") |
| 91 | if err != nil || bytes.Equal(pepper[:], dek[:]) || bytes.Equal(pepper[:], material.MAC) { |
| 92 | return material, operator.ErrRefused |
| 93 | } |
| 94 | defer clear(pepper[:]) |
| 95 | resolve, err := NewOperatorPersonResolver(pepper[:]) |
| 96 | if err != nil { |
| 97 | return material, operator.ErrRefused |
| 98 | } |
| 99 | owner, err := decodeHomeKeypair(cfg.Owner) |
| 100 | if err != nil { |
| 101 | return material, operator.ErrRefused |
| 102 | } |
| 103 | browser, err := decodeHomeKeypair(os.Getenv("HOME_ASSERTION_KEY")) |
| 104 | if err != nil { |
| 105 | return material, operator.ErrRefused |
| 106 | } |
| 107 | material.OtherKeys = [][]byte{owner.SignPub(), browser.SignPub()} |
| 108 | material.Origin = origins.Credential |
| 109 | material.Build = func(engine *operator.Engine) (operator.CommandExecutor, error) { |
| 110 | return operator.NewCredential(engine, origins.Directory, owner, [][]byte{browser.SignPub()}, resolve) |
| 111 | } |
| 112 | } else { |
| 113 | var cfg directoryOperatorMaterial |
| 114 | if json.Unmarshal([]byte(raw), &cfg, json.RejectUnknownMembers(true)) != nil || cfg.Version != 1 || cfg.Role != role || len(cfg.Roster) == 0 || len(cfg.Roster) > 8 { |
| 115 | return material, operator.ErrRefused |
| 116 | } |
| 117 | material.MAC, err = operatorMAC(cfg.MAC) |
| 118 | if err != nil || bytes.Equal(material.MAC, dek[:]) { |
| 119 | return material, operator.ErrRefused |
| 120 | } |
| 121 | owner, err := operator.NewOwnerVerifier(cfg.Owner, origins.Credential, origins.Directory) |
| 122 | if err != nil { |
| 123 | return material, operator.ErrRefused |
| 124 | } |
| 125 | ownerPub, err := operatorPublic(cfg.Owner) |
| 126 | if err != nil { |
| 127 | return material, operator.ErrRefused |
| 128 | } |
| 129 | signer, err := decodeHomeKeypair(cfg.Provisioning) |
| 130 | if err != nil { |
| 131 | return material, operator.ErrRefused |
| 132 | } |
| 133 | browserRaw, err := exchangeJSON("HOME_ASSERTION_KEYS", 4096) |
| 134 | var encoded map[string]string |
| 135 | if err != nil || json.Unmarshal([]byte(browserRaw), &encoded) != nil || len(encoded) == 0 || len(encoded) > 8 { |
| 136 | return material, operator.ErrRefused |
| 137 | } |
| 138 | browser := map[string][]byte{} |
| 139 | material.OtherKeys = [][]byte{ownerPub, signer.SignPub()} |
| 140 | for id, value := range encoded { |
| 141 | key, err := base64.StdEncoding.Strict().DecodeString(value) |
| 142 | if err != nil || base64.StdEncoding.EncodeToString(key) != value { |
| 143 | return material, operator.ErrRefused |
| 144 | } |
| 145 | browser[id] = key |
| 146 | material.OtherKeys = append(material.OtherKeys, key) |
| 147 | } |
| 148 | if _, err := assertion.NewVerifier(origins.Credential, origins.Directory, browser); err != nil { |
| 149 | return material, operator.ErrRefused |
| 150 | } |
| 151 | rosterRuntime, err := loadRosterRuntime(role, browser) |
| 152 | if err != nil { |
| 153 | return material, operator.ErrRefused |
| 154 | } |
| 155 | ids := map[string]bool{} |
| 156 | matched := rosterRuntime.mode != "serve" |
| 157 | for _, key := range cfg.Roster { |
| 158 | pub, err := operatorPublic(key.Public) |
| 159 | sum := sha256.Sum256(pub) |
| 160 | if err != nil || key.ID != hex.EncodeToString(sum[:]) || ids[key.ID] { |
| 161 | return material, operator.ErrRefused |
| 162 | } |
| 163 | ids[key.ID] = true |
| 164 | material.OtherKeys = append(material.OtherKeys, pub) |
| 165 | if rosterRuntime.mode == "serve" && bytes.Equal(pub, rosterRuntime.signer.SignPub()) { |
| 166 | matched = true |
| 167 | } |
| 168 | } |
| 169 | if !matched { |
| 170 | return material, operator.ErrRefused |
| 171 | } |
| 172 | var apps []string |
| 173 | for _, app := range directoryApps() { |
| 174 | if app.integrated { |
| 175 | apps = append(apps, app.slug) |
| 176 | } |
| 177 | } |
| 178 | policy := directorydata.Config{Apps: apps, ValidateRoute: directoryRouteAllowed} |
| 179 | material.Origin = origins.Directory |
| 180 | material.Build = func(engine *operator.Engine) (operator.CommandExecutor, error) { |
| 181 | return operator.NewDirectory(engine, owner, signer, cfg.Roster, policy) |
| 182 | } |
| 183 | } |
| 184 | if !distinctOperatorKeys(material.OtherKeys) { |
| 185 | return material, operator.ErrRefused |
| 186 | } |
| 187 | return material, nil |
| 188 | } |
| 189 | |