forked from micromdm/scep
/
transport.go
180 lines (164 loc) · 4.19 KB
/
transport.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package scepserver
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/go-kit/kit/endpoint"
kitlog "github.com/go-kit/kit/log"
kithttp "github.com/go-kit/kit/transport/http"
)
// ServiceHandler is an HTTP Handler for a SCEP endpoint.
func ServiceHandler(ctx context.Context, svc Service, logger kitlog.Logger) http.Handler {
opts := []kithttp.ServerOption{
kithttp.ServerErrorLogger(logger),
kithttp.ServerBefore(updateContext),
}
scepHandler := kithttp.NewServer(
makeSCEPEndpoint(svc),
decodeSCEPRequest,
encodeSCEPResponse,
opts...,
)
mux := http.NewServeMux()
mux.Handle("/scep", scepHandler)
return mux
}
func updateContext(ctx context.Context, r *http.Request) context.Context {
q := r.URL.Query()
if _, ok := q["operation"]; ok {
ctx = context.WithValue(ctx, "operation", q.Get("operation"))
}
return ctx
}
// EncodeSCEPRequest encodes a SCEP http request
func EncodeSCEPRequest(ctx context.Context, r *http.Request, request interface{}) error {
req := request.(SCEPRequest)
params := r.URL.Query()
params.Set("operation", req.Operation)
switch r.Method {
case "GET":
if len(req.Message) > 0 {
return errors.New("only POSTPKIOperation supported")
}
case "POST":
var buf bytes.Buffer
_, err := buf.Write(req.Message)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(&buf)
default:
return errors.New("method not supported")
}
r.URL.RawQuery = params.Encode()
return nil
}
// DecodeSCEPRequest decodes an HTTP request to the SCEP server
// extracting the Operation and Message.
func decodeSCEPRequest(ctx context.Context, r *http.Request) (interface{}, error) {
msg, err := message(r)
if err != nil {
return nil, err
}
request := SCEPRequest{
Message: msg,
}
return request, nil
}
// extract message from request
func message(r *http.Request) ([]byte, error) {
switch r.Method {
case "GET":
var msg string
q := r.URL.Query()
if _, ok := q["message"]; ok {
msg = q.Get("message")
}
return []byte(msg), nil
case "POST":
return ioutil.ReadAll(r.Body)
default:
return nil, errors.New("method not supported")
}
}
// EncodeSCEPResponse writes a SCEP response back to the SCEP client.
func encodeSCEPResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
resp := response.(SCEPResponse)
if resp.Err != nil {
fmt.Println(resp.Err)
return resp.Err
}
w.Header().Set("Content-Type", contentHeader(ctx, resp.CACertNum))
w.Write(resp.Data)
return nil
}
// DecodeSCEPResponse decodes a SCEP response
func DecodeSCEPResponse(ctx context.Context, r *http.Response) (interface{}, error) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
resp := SCEPResponse{
Data: data,
}
header := r.Header.Get("Content-Type")
if header == certChainHeader {
// TODO decode the response instead of just passing []byte around
// 0 or 1
resp.CACertNum = 2
}
return resp, nil
}
const (
certChainHeader = "application/x-x509-ca-ra-cert"
leafHeader = "application/x-x509-ca-cert"
pkiOpHeader = "application/x-pki-message"
)
func contentHeader(ctx context.Context, certNum int) string {
op := ctx.Value("operation")
switch op {
case "GetCACert":
if certNum > 1 {
return certChainHeader
}
return leafHeader
case "PKIOperation":
return pkiOpHeader
default:
return "text/plain"
}
}
func makeSCEPEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
op := ctx.Value("operation")
if op == nil {
return SCEPResponse{Err: errors.New("unknown operation")}, nil
}
req := request.(SCEPRequest)
switch op {
case "GetCACaps":
caps, err := svc.GetCACaps(ctx)
if err != nil {
return SCEPResponse{Err: err}, nil
}
return SCEPResponse{Data: caps}, nil
case "GetCACert":
cert, certNum, err := svc.GetCACert(ctx)
if err != nil {
return SCEPResponse{Err: err, CACertNum: certNum}, nil
}
return SCEPResponse{Data: cert}, nil
case "PKIOperation":
resp, err := svc.PKIOperation(ctx, req.Message)
if err != nil {
return SCEPResponse{Err: err}, nil
}
return SCEPResponse{Data: resp}, nil
default:
return nil, errors.New("operation not implemented")
}
}
}