|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "path" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/bwesterb/mtc" |
| 14 | + "github.com/bwesterb/mtc/ca" |
| 15 | +) |
| 16 | + |
| 17 | +type Server struct { |
| 18 | + server *http.Server |
| 19 | +} |
| 20 | + |
| 21 | +func NewServer(caPath string, listenAddr string) *Server { |
| 22 | + |
| 23 | + mux := http.NewServeMux() |
| 24 | + mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(path.Join(caPath, "www"))))) |
| 25 | + mux.HandleFunc("/ca/queue", handleCaQueue(caPath)) |
| 26 | + mux.HandleFunc("/ca/cert", handleCaCert(caPath)) |
| 27 | + |
| 28 | + return &Server{ |
| 29 | + server: &http.Server{ |
| 30 | + Handler: mux, |
| 31 | + Addr: listenAddr, |
| 32 | + WriteTimeout: 15 * time.Second, |
| 33 | + ReadTimeout: 15 * time.Second, |
| 34 | + }} |
| 35 | +} |
| 36 | + |
| 37 | +func (s *Server) ListenAndServe() error { |
| 38 | + if err := s.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 39 | + return err |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func (s *Server) Shutdown(ctx context.Context) error { |
| 45 | + return s.server.Shutdown(ctx) |
| 46 | +} |
| 47 | + |
| 48 | +func assertionFromRequestUnchecked(r *http.Request) (*ca.QueuedAssertion, error) { |
| 49 | + |
| 50 | + var ( |
| 51 | + checksum []byte |
| 52 | + err error |
| 53 | + ) |
| 54 | + |
| 55 | + checksumParam := r.URL.Query().Get("checksum") |
| 56 | + if checksumParam != "" { |
| 57 | + checksum, err = hex.DecodeString(checksumParam) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + } |
| 62 | + var a mtc.Assertion |
| 63 | + switch r.Method { |
| 64 | + case http.MethodPost: |
| 65 | + body, err := io.ReadAll(r.Body) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + defer r.Body.Close() |
| 70 | + |
| 71 | + err = a.UnmarshalBinary(body) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + default: |
| 76 | + return nil, fmt.Errorf("unsupported HTTP method: %v", r.Method) |
| 77 | + } |
| 78 | + |
| 79 | + return &ca.QueuedAssertion{ |
| 80 | + Assertion: a, |
| 81 | + Checksum: checksum, |
| 82 | + }, nil |
| 83 | +} |
| 84 | + |
| 85 | +func assertionFromRequest(r *http.Request) (*ca.QueuedAssertion, error) { |
| 86 | + qa, err := assertionFromRequestUnchecked(r) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + err = qa.Check() |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + return qa, nil |
| 97 | +} |
| 98 | + |
| 99 | +func handleCaQueue(path string) func(w http.ResponseWriter, r *http.Request) { |
| 100 | + return func(w http.ResponseWriter, r *http.Request) { |
| 101 | + h, err := ca.Open(path) |
| 102 | + if err != nil { |
| 103 | + http.Error(w, "failed to open CA", http.StatusInternalServerError) |
| 104 | + return |
| 105 | + } |
| 106 | + defer h.Close() |
| 107 | + qa, err := assertionFromRequest(r) |
| 108 | + if err != nil { |
| 109 | + http.Error(w, "invalid assertion", http.StatusBadRequest) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + err = h.Queue(qa.Assertion, qa.Checksum) |
| 114 | + if err != nil { |
| 115 | + http.Error(w, "failed to queue assertion", http.StatusInternalServerError) |
| 116 | + return |
| 117 | + } |
| 118 | + w.WriteHeader(http.StatusOK) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func handleCaCert(path string) func(w http.ResponseWriter, r *http.Request) { |
| 123 | + return func(w http.ResponseWriter, r *http.Request) { |
| 124 | + h, err := ca.Open(path) |
| 125 | + if err != nil { |
| 126 | + http.Error(w, "failed to open CA", http.StatusInternalServerError) |
| 127 | + return |
| 128 | + } |
| 129 | + defer h.Close() |
| 130 | + qa, err := assertionFromRequest(r) |
| 131 | + if err != nil { |
| 132 | + http.Error(w, "invalid assertion", http.StatusBadRequest) |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + cert, err := h.CertificateFor(qa.Assertion) |
| 137 | + if err != nil { |
| 138 | + http.Error(w, "failed to get certificate for assertion", http.StatusBadRequest) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + buf, err := cert.MarshalBinary() |
| 143 | + if err != nil { |
| 144 | + http.Error(w, "failed to marshal certificate", http.StatusInternalServerError) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + _, err = w.Write(buf) |
| 149 | + if err != nil { |
| 150 | + http.Error(w, "failed to write response", http.StatusInternalServerError) |
| 151 | + return |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments