aboutsummaryrefslogtreecommitdiff
path: root/handler.go
blob: 7b986c1d914fc33685683e576ae8be94fbe239ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package stfe

import (
	"context"
	"fmt"

	"encoding/json"
	"io/ioutil"
	"net/http"

	"github.com/golang/glog"
	"github.com/google/certificate-transparency-go/tls"
	"github.com/google/trillian"
)

// appHandler implements the http.Handler interface, and contains a reference
// to an STFE server instance as well as a function that uses it.
type appHandler struct {
	instance *instance // STFE server instance
	endpoint string    // e.g., add-entry
	method   string    // e.g., GET
	handler  func(context.Context, *instance, http.ResponseWriter, *http.Request) (int, error)
}

// ServeHTTP docdoc
func (a appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx, cancel := context.WithDeadline(r.Context(), a.instance.timesource.Now().Add(a.instance.deadline))
	defer cancel()

	if r.Method != a.method {
		glog.Warningf("%s: got HTTP %s, wanted HTTP %s", a.instance.prefix+a.endpoint, r.Method, a.method)
		a.sendHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s", r.Method))
		return
	}

	statusCode, err := a.handler(ctx, a.instance, w, r)
	if err != nil {
		glog.Warningf("handler error %s/%s: %v", a.instance.prefix, a.endpoint, err)
		a.sendHTTPError(w, statusCode, err)
	}
}

// sendHTTPError replies to a request with an error message and a status code.
func (a appHandler) sendHTTPError(w http.ResponseWriter, statusCode int, err error) {
	http.Error(w, http.StatusText(statusCode), statusCode)
}

func addEntry(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) {
	glog.Info("in addEntry")
	var request AddEntryRequest
	if err := unpackRequest(r, &request); err != nil {
		return http.StatusBadRequest, err
	}

	item, err := verifyAddEntryRequest(request)
	if err != nil {
		return http.StatusBadRequest, err
	}
	glog.Infof("got item: %s", item)

	serializedItem, err := tls.Marshal(*item)
	if err != nil {
		return http.StatusInternalServerError, fmt.Errorf("tls marshal failed: %v", err)
	}
	trillianRequest := trillian.QueueLeafRequest{
		LogId: i.logID,
		Leaf: &trillian.LogLeaf{
			LeafValue: serializedItem,
			//TODO: add appendix here w/ chain + signature
		},
	}

	trillianResponse, err := i.client.QueueLeaf(ctx, &trillianRequest)
	if err != nil {
		return http.StatusInternalServerError, fmt.Errorf("backend QueueLeaf request failed: %v", err)
	}
	if trillianResponse == nil {
		return http.StatusInternalServerError, fmt.Errorf("missing QueueLeaf response")
	}
	// TODO: check that we got gRPC OK as specified in Trillian's API doc

	queuedLeaf := trillianResponse.QueuedLeaf
	glog.Infof("Queued leaf: %v", queuedLeaf.Leaf.LeafValue)
	// TODO: respond with an SDI

	return http.StatusOK, nil
}

// verifyAddEntryRequest
func verifyAddEntryRequest(r AddEntryRequest) (*StItem, error) {
	item, err := StItemFromB64(r.Item)
	if err != nil {
		return nil, fmt.Errorf("failed decoding StItem: %v", err)
	}
	if item.Format != StFormatChecksumV1 {
		return nil, fmt.Errorf("invalid StItem format: %s", item.Format)
	}
	// TODO: verify checksum length
	// TODO: verify r.Signature and r.Certificate
	return item, nil
}

// unpackRequest tries to unpack a json-encoded HTTP POST request into `unpack`
func unpackRequest(r *http.Request, unpack interface{}) error {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return fmt.Errorf("failed reading request body: %v", err)
	}
	if err := json.Unmarshal(body, &unpack); err != nil {
		return fmt.Errorf("failed parsing json body: %v", err)
	}
	return nil
}

// getEntries provides with a list of entries from the Trillian backend
func getEntries(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) {
	glog.Info("in getEntries")
	return http.StatusOK, nil // TODO
}

// getAnchors provides a list of configured trust anchors
func getAnchors(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) {
	glog.Info("in getAnchors")
	return http.StatusOK, nil // TODO
}

// getProofByHash provides an inclusion proof based on a given leaf hash
func getProofByHash(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) {
	glog.Info("in getProofByHash")
	return http.StatusOK, nil // TODO
}

// getConsistencyProof provides a consistency proof between two STHs
func getConsistencyProof(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) {
	glog.Info("in getConsistencyProof")
	return http.StatusOK, nil // TODO
}

// getSth provides the most recent STH
func getSth(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) {
	glog.Info("in getSth")
	return http.StatusOK, nil // TODO
}