This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
/
hkpfront.go
160 lines (140 loc) · 3.69 KB
/
hkpfront.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
package hkpfront
import (
"crypto/tls"
"net"
"net/http"
"sync"
"time"
"github.com/andres-erbsen/clock"
"github.com/yahoo/coname"
"github.com/yahoo/coname/proto"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/net/context"
)
// HKPFront implements a unverified GnuPG-compatible HKP frontend for the
// verified keyserver.
type HKPFront struct {
Lookup func(context.Context, *proto.LookupRequest) (*proto.LookupProof, error)
Clk clock.Clock
InsecureSkipVerify bool
// Config must be set and valid if InsecureSkipVerify is not set
Config *proto.Config
// this is needed due to https://github.com/golang/go/issues/14374
TLSConfig *tls.Config
ln net.Listener
sr http.Server
connStateMu sync.Mutex
connState map[net.Conn]http.ConnState
stopOnce sync.Once
stop chan struct{}
waitStop sync.WaitGroup // server + all open connections
}
func (h *HKPFront) Start(ln net.Listener) {
h.stop = make(chan struct{})
h.connState = make(map[net.Conn]http.ConnState)
h.sr = http.Server{
Addr: ln.Addr().String(),
Handler: h,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 4096,
ConnState: h.updateConnState,
TLSConfig: h.TLSConfig,
}
h.ln = ln
h.waitStop.Add(1)
go h.run()
}
func (h *HKPFront) run() {
defer h.waitStop.Done()
h.sr.Serve(h.ln)
}
func (h *HKPFront) Stop() {
h.stopOnce.Do(func() {
close(h.stop)
h.sr.SetKeepAlivesEnabled(false)
h.ln.Close()
h.connStateMu.Lock()
for c, s := range h.connState {
if s == http.StateIdle {
c.Close()
}
}
h.connStateMu.Unlock()
h.waitStop.Wait()
})
}
func (h *HKPFront) updateConnState(c net.Conn, s http.ConnState) {
h.connStateMu.Lock()
defer h.connStateMu.Unlock()
h.connState[c] = s
switch s {
case http.StateNew:
h.waitStop.Add(1)
case http.StateIdle:
select {
case <-h.stop:
c.Close()
default:
}
case http.StateClosed, http.StateHijacked:
h.waitStop.Done()
delete(h.connState, c)
}
}
func (h *HKPFront) ServeHTTP(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if r.Method != "GET" || r.URL.Path != "/pks/lookup" || len(q["op"]) != 1 || q["op"][0] != "get" || len(q["search"]) != 1 {
http.Error(w, `this server only supports queries of the form "/pks/lookup?op=get&search=<EMAIL>"`, 501)
return
}
user := q["search"][0]
ctx := context.Background()
var requiredSignatures *proto.QuorumExpr
if !h.InsecureSkipVerify {
realm, err := coname.GetRealmByUser(h.Config, user)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
switch realm.VerificationPolicy.PolicyType.(type) {
case *proto.AuthorizationPolicy_Quorum:
requiredSignatures = realm.VerificationPolicy.PolicyType.(*proto.AuthorizationPolicy_Quorum).Quorum
default:
}
}
pf, err := h.Lookup(ctx, &proto.LookupRequest{UserId: user, QuorumRequirement: requiredSignatures})
if err != nil {
http.Error(w, err.Error(), 503)
return
}
if !h.InsecureSkipVerify {
coname.VerifyLookup(h.Config, user, pf, h.Clk.Now())
}
if pf.Profile == nil || pf.Profile.Keys == nil {
http.Error(w, `No results found: No keys found: unknown email`, 404)
return
}
pgpKey, present := pf.Profile.Keys["pgp"]
if !present {
http.Error(w, `No results found: No keys found: the email is known to the keyserver, but the profile does not include an OpenPGP key`, 404)
return
}
if _, mr := q["mr"]; mr {
w.Header().Set("Content-Type", "application/pgp-keys")
}
aw, err := armor.Encode(w, "PGP PUBLIC KEY BLOCK", nil)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
_, err = aw.Write(pgpKey)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := aw.Close(); err != nil {
http.Error(w, err.Error(), 500)
return
}
}