tailnetlock.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ts_omit_tailnetlock
  4. package localapi
  5. import (
  6. "encoding/json"
  7. "io"
  8. "net/http"
  9. "strconv"
  10. "tailscale.com/tka"
  11. "tailscale.com/types/key"
  12. "tailscale.com/types/tkatype"
  13. "tailscale.com/util/httpm"
  14. )
  15. func init() {
  16. Register("tka/affected-sigs", (*Handler).serveTKAAffectedSigs)
  17. Register("tka/cosign-recovery-aum", (*Handler).serveTKACosignRecoveryAUM)
  18. Register("tka/disable", (*Handler).serveTKADisable)
  19. Register("tka/force-local-disable", (*Handler).serveTKALocalDisable)
  20. Register("tka/generate-recovery-aum", (*Handler).serveTKAGenerateRecoveryAUM)
  21. Register("tka/init", (*Handler).serveTKAInit)
  22. Register("tka/log", (*Handler).serveTKALog)
  23. Register("tka/modify", (*Handler).serveTKAModify)
  24. Register("tka/sign", (*Handler).serveTKASign)
  25. Register("tka/status", (*Handler).serveTKAStatus)
  26. Register("tka/submit-recovery-aum", (*Handler).serveTKASubmitRecoveryAUM)
  27. Register("tka/verify-deeplink", (*Handler).serveTKAVerifySigningDeeplink)
  28. Register("tka/wrap-preauth-key", (*Handler).serveTKAWrapPreauthKey)
  29. }
  30. func (h *Handler) serveTKAStatus(w http.ResponseWriter, r *http.Request) {
  31. if !h.PermitRead {
  32. http.Error(w, "lock status access denied", http.StatusForbidden)
  33. return
  34. }
  35. if r.Method != httpm.GET {
  36. http.Error(w, "use GET", http.StatusMethodNotAllowed)
  37. return
  38. }
  39. j, err := json.MarshalIndent(h.b.NetworkLockStatus(), "", "\t")
  40. if err != nil {
  41. http.Error(w, "JSON encoding error", http.StatusInternalServerError)
  42. return
  43. }
  44. w.Header().Set("Content-Type", "application/json")
  45. w.Write(j)
  46. }
  47. func (h *Handler) serveTKASign(w http.ResponseWriter, r *http.Request) {
  48. if !h.PermitWrite {
  49. http.Error(w, "lock sign access denied", http.StatusForbidden)
  50. return
  51. }
  52. if r.Method != httpm.POST {
  53. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  54. return
  55. }
  56. type signRequest struct {
  57. NodeKey key.NodePublic
  58. RotationPublic []byte
  59. }
  60. var req signRequest
  61. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  62. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  63. return
  64. }
  65. if err := h.b.NetworkLockSign(req.NodeKey, req.RotationPublic); err != nil {
  66. http.Error(w, "signing failed: "+err.Error(), http.StatusInternalServerError)
  67. return
  68. }
  69. w.WriteHeader(http.StatusOK)
  70. }
  71. func (h *Handler) serveTKAInit(w http.ResponseWriter, r *http.Request) {
  72. if !h.PermitWrite {
  73. http.Error(w, "lock init access denied", http.StatusForbidden)
  74. return
  75. }
  76. if r.Method != httpm.POST {
  77. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  78. return
  79. }
  80. type initRequest struct {
  81. Keys []tka.Key
  82. DisablementValues [][]byte
  83. SupportDisablement []byte
  84. }
  85. var req initRequest
  86. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  87. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  88. return
  89. }
  90. if !h.b.NetworkLockAllowed() {
  91. http.Error(w, "Tailnet Lock is not supported on your pricing plan", http.StatusForbidden)
  92. return
  93. }
  94. if err := h.b.NetworkLockInit(req.Keys, req.DisablementValues, req.SupportDisablement); err != nil {
  95. http.Error(w, "initialization failed: "+err.Error(), http.StatusInternalServerError)
  96. return
  97. }
  98. j, err := json.MarshalIndent(h.b.NetworkLockStatus(), "", "\t")
  99. if err != nil {
  100. http.Error(w, "JSON encoding error", http.StatusInternalServerError)
  101. return
  102. }
  103. w.Header().Set("Content-Type", "application/json")
  104. w.Write(j)
  105. }
  106. func (h *Handler) serveTKAModify(w http.ResponseWriter, r *http.Request) {
  107. if !h.PermitWrite {
  108. http.Error(w, "network-lock modify access denied", http.StatusForbidden)
  109. return
  110. }
  111. if r.Method != httpm.POST {
  112. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  113. return
  114. }
  115. type modifyRequest struct {
  116. AddKeys []tka.Key
  117. RemoveKeys []tka.Key
  118. }
  119. var req modifyRequest
  120. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  121. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  122. return
  123. }
  124. if err := h.b.NetworkLockModify(req.AddKeys, req.RemoveKeys); err != nil {
  125. http.Error(w, "network-lock modify failed: "+err.Error(), http.StatusInternalServerError)
  126. return
  127. }
  128. w.WriteHeader(204)
  129. }
  130. func (h *Handler) serveTKAWrapPreauthKey(w http.ResponseWriter, r *http.Request) {
  131. if !h.PermitWrite {
  132. http.Error(w, "network-lock modify access denied", http.StatusForbidden)
  133. return
  134. }
  135. if r.Method != httpm.POST {
  136. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  137. return
  138. }
  139. type wrapRequest struct {
  140. TSKey string
  141. TKAKey string // key.NLPrivate.MarshalText
  142. }
  143. var req wrapRequest
  144. if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 12*1024)).Decode(&req); err != nil {
  145. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  146. return
  147. }
  148. var priv key.NLPrivate
  149. if err := priv.UnmarshalText([]byte(req.TKAKey)); err != nil {
  150. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  151. return
  152. }
  153. wrappedKey, err := h.b.NetworkLockWrapPreauthKey(req.TSKey, priv)
  154. if err != nil {
  155. http.Error(w, err.Error(), http.StatusInternalServerError)
  156. return
  157. }
  158. w.WriteHeader(http.StatusOK)
  159. w.Write([]byte(wrappedKey))
  160. }
  161. func (h *Handler) serveTKAVerifySigningDeeplink(w http.ResponseWriter, r *http.Request) {
  162. if !h.PermitRead {
  163. http.Error(w, "signing deeplink verification access denied", http.StatusForbidden)
  164. return
  165. }
  166. if r.Method != httpm.POST {
  167. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  168. return
  169. }
  170. type verifyRequest struct {
  171. URL string
  172. }
  173. var req verifyRequest
  174. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  175. http.Error(w, "invalid JSON for verifyRequest body", http.StatusBadRequest)
  176. return
  177. }
  178. res := h.b.NetworkLockVerifySigningDeeplink(req.URL)
  179. j, err := json.MarshalIndent(res, "", "\t")
  180. if err != nil {
  181. http.Error(w, "JSON encoding error", http.StatusInternalServerError)
  182. return
  183. }
  184. w.Header().Set("Content-Type", "application/json")
  185. w.Write(j)
  186. }
  187. func (h *Handler) serveTKADisable(w http.ResponseWriter, r *http.Request) {
  188. if !h.PermitWrite {
  189. http.Error(w, "network-lock modify access denied", http.StatusForbidden)
  190. return
  191. }
  192. if r.Method != httpm.POST {
  193. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  194. return
  195. }
  196. body := io.LimitReader(r.Body, 1024*1024)
  197. secret, err := io.ReadAll(body)
  198. if err != nil {
  199. http.Error(w, "reading secret", http.StatusBadRequest)
  200. return
  201. }
  202. if err := h.b.NetworkLockDisable(secret); err != nil {
  203. http.Error(w, "network-lock disable failed: "+err.Error(), http.StatusBadRequest)
  204. return
  205. }
  206. w.WriteHeader(http.StatusOK)
  207. }
  208. func (h *Handler) serveTKALocalDisable(w http.ResponseWriter, r *http.Request) {
  209. if !h.PermitWrite {
  210. http.Error(w, "network-lock modify access denied", http.StatusForbidden)
  211. return
  212. }
  213. if r.Method != httpm.POST {
  214. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  215. return
  216. }
  217. // Require a JSON stanza for the body as an additional CSRF protection.
  218. var req struct{}
  219. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  220. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  221. return
  222. }
  223. if err := h.b.NetworkLockForceLocalDisable(); err != nil {
  224. http.Error(w, "network-lock local disable failed: "+err.Error(), http.StatusBadRequest)
  225. return
  226. }
  227. w.WriteHeader(http.StatusOK)
  228. }
  229. func (h *Handler) serveTKALog(w http.ResponseWriter, r *http.Request) {
  230. if r.Method != httpm.GET {
  231. http.Error(w, "use GET", http.StatusMethodNotAllowed)
  232. return
  233. }
  234. limit := 50
  235. if limitStr := r.FormValue("limit"); limitStr != "" {
  236. lm, err := strconv.Atoi(limitStr)
  237. if err != nil {
  238. http.Error(w, "parsing 'limit' parameter: "+err.Error(), http.StatusBadRequest)
  239. return
  240. }
  241. limit = int(lm)
  242. }
  243. updates, err := h.b.NetworkLockLog(limit)
  244. if err != nil {
  245. http.Error(w, "reading log failed: "+err.Error(), http.StatusInternalServerError)
  246. return
  247. }
  248. j, err := json.MarshalIndent(updates, "", "\t")
  249. if err != nil {
  250. http.Error(w, "JSON encoding error", http.StatusInternalServerError)
  251. return
  252. }
  253. w.Header().Set("Content-Type", "application/json")
  254. w.Write(j)
  255. }
  256. func (h *Handler) serveTKAAffectedSigs(w http.ResponseWriter, r *http.Request) {
  257. if r.Method != httpm.POST {
  258. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  259. return
  260. }
  261. keyID, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 2048))
  262. if err != nil {
  263. http.Error(w, "reading body", http.StatusBadRequest)
  264. return
  265. }
  266. sigs, err := h.b.NetworkLockAffectedSigs(keyID)
  267. if err != nil {
  268. http.Error(w, err.Error(), http.StatusInternalServerError)
  269. return
  270. }
  271. j, err := json.MarshalIndent(sigs, "", "\t")
  272. if err != nil {
  273. http.Error(w, "JSON encoding error", http.StatusInternalServerError)
  274. return
  275. }
  276. w.Header().Set("Content-Type", "application/json")
  277. w.Write(j)
  278. }
  279. func (h *Handler) serveTKAGenerateRecoveryAUM(w http.ResponseWriter, r *http.Request) {
  280. if !h.PermitWrite {
  281. http.Error(w, "access denied", http.StatusForbidden)
  282. return
  283. }
  284. if r.Method != httpm.POST {
  285. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  286. return
  287. }
  288. type verifyRequest struct {
  289. Keys []tkatype.KeyID
  290. ForkFrom string
  291. }
  292. var req verifyRequest
  293. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  294. http.Error(w, "invalid JSON for verifyRequest body", http.StatusBadRequest)
  295. return
  296. }
  297. var forkFrom tka.AUMHash
  298. if req.ForkFrom != "" {
  299. if err := forkFrom.UnmarshalText([]byte(req.ForkFrom)); err != nil {
  300. http.Error(w, "decoding fork-from: "+err.Error(), http.StatusBadRequest)
  301. return
  302. }
  303. }
  304. res, err := h.b.NetworkLockGenerateRecoveryAUM(req.Keys, forkFrom)
  305. if err != nil {
  306. http.Error(w, err.Error(), http.StatusInternalServerError)
  307. return
  308. }
  309. w.Header().Set("Content-Type", "application/octet-stream")
  310. w.Write(res.Serialize())
  311. }
  312. func (h *Handler) serveTKACosignRecoveryAUM(w http.ResponseWriter, r *http.Request) {
  313. if !h.PermitWrite {
  314. http.Error(w, "access denied", http.StatusForbidden)
  315. return
  316. }
  317. if r.Method != httpm.POST {
  318. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  319. return
  320. }
  321. body := io.LimitReader(r.Body, 1024*1024)
  322. aumBytes, err := io.ReadAll(body)
  323. if err != nil {
  324. http.Error(w, "reading AUM", http.StatusBadRequest)
  325. return
  326. }
  327. var aum tka.AUM
  328. if err := aum.Unserialize(aumBytes); err != nil {
  329. http.Error(w, "decoding AUM", http.StatusBadRequest)
  330. return
  331. }
  332. res, err := h.b.NetworkLockCosignRecoveryAUM(&aum)
  333. if err != nil {
  334. http.Error(w, err.Error(), http.StatusInternalServerError)
  335. return
  336. }
  337. w.Header().Set("Content-Type", "application/octet-stream")
  338. w.Write(res.Serialize())
  339. }
  340. func (h *Handler) serveTKASubmitRecoveryAUM(w http.ResponseWriter, r *http.Request) {
  341. if !h.PermitWrite {
  342. http.Error(w, "access denied", http.StatusForbidden)
  343. return
  344. }
  345. if r.Method != httpm.POST {
  346. http.Error(w, "use POST", http.StatusMethodNotAllowed)
  347. return
  348. }
  349. body := io.LimitReader(r.Body, 1024*1024)
  350. aumBytes, err := io.ReadAll(body)
  351. if err != nil {
  352. http.Error(w, "reading AUM", http.StatusBadRequest)
  353. return
  354. }
  355. var aum tka.AUM
  356. if err := aum.Unserialize(aumBytes); err != nil {
  357. http.Error(w, "decoding AUM", http.StatusBadRequest)
  358. return
  359. }
  360. if err := h.b.NetworkLockSubmitRecoveryAUM(&aum); err != nil {
  361. http.Error(w, err.Error(), http.StatusInternalServerError)
  362. return
  363. }
  364. w.WriteHeader(http.StatusOK)
  365. }