-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp01_test.go
More file actions
262 lines (241 loc) · 8.44 KB
/
Copy pathhttp01_test.go
File metadata and controls
262 lines (241 loc) · 8.44 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package acme
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/go-jose/go-jose/v4"
"github.com/letsencrypt/cactus/ca"
"github.com/letsencrypt/cactus/cert"
cactuslog "github.com/letsencrypt/cactus/log"
"github.com/letsencrypt/cactus/signer"
"github.com/letsencrypt/cactus/storage"
)
// TestHTTP01ChallengeMode brings up the stack in http-01 mode, runs a
// matching HTTP server that serves the keyAuthorization at the right
// path, and confirms the order can be finalized.
func TestHTTP01ChallengeMode(t *testing.T) {
dir := t.TempDir()
fs, _ := storage.New(dir)
seed := make([]byte, signer.SeedSize)
for i := range seed {
seed[i] = 0xAA
}
s, _ := signer.FromSeed(signer.AlgMLDSA44, seed)
l, err := cactuslog.New(context.Background(), cactuslog.Config{
LogID: cert.TrustAnchorID("32473.1"),
CosignerID: cert.TrustAnchorID("32473.1"),
Signer: s,
FS: fs,
FlushPeriod: 25 * time.Millisecond,
})
if err != nil {
t.Fatal(err)
}
defer l.Stop()
issuer, _ := ca.New(l, "32473.1", 1)
srv, _ := New(Config{
Issuer: issuer,
ChallengeMode: ChallengeHTTP01,
})
hsrv := httptest.NewServer(srv.Handler())
defer hsrv.Close()
srv.SetExternalURL(hsrv.URL)
// Stand up a shared challenge HTTP server. The client needs to
// register its keyAuthorization here before triggering the
// challenge.
var mu sync.Mutex
keyauths := map[string]string{} // token -> keyauth
chsrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
const prefix = "/.well-known/acme-challenge/"
if !strings.HasPrefix(r.URL.Path, prefix) {
http.NotFound(w, r)
return
}
token := strings.TrimPrefix(r.URL.Path, prefix)
mu.Lock()
ka, ok := keyauths[token]
mu.Unlock()
if !ok {
http.NotFound(w, r)
return
}
w.Write([]byte(ka))
}))
defer chsrv.Close()
// Override the http-01 fetch URL by pointing the identifier at the
// challenge server. attemptHTTP01 builds "http://<identifier>/...";
// we can put the chsrv host:port in the identifier.
chHost := strings.TrimPrefix(chsrv.URL, "http://")
// Account.
acctKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
acctJWK := &jose.JSONWebKey{Key: acctKey.Public(), Algorithm: "ES256"}
jwkBytes, _ := acctJWK.MarshalJSON()
nonce := nonceFor(t, hsrv.URL)
jws := jwsSign(t, acctKey, acctJWK, "", nonce, hsrv.URL+"/new-account",
mustMarshal(NewAccountReq{TermsOfServiceAgreed: true}))
resp, _ := post(t, hsrv.URL, "/new-account", jws)
kid := resp.Header.Get("Location")
// new-order with chHost as the identifier.
nonce = resp.Header.Get("Replay-Nonce")
jws = jwsSign(t, acctKey, nil, kid, nonce, hsrv.URL+"/new-order",
mustMarshal(NewOrderReq{Identifiers: []Identifier{{Type: "dns", Value: chHost}}}))
resp, body := post(t, hsrv.URL, "/new-order", jws)
var ord OrderResp
if err := json.Unmarshal(body, &ord); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if ord.Status != "pending" {
t.Errorf("order should be pending in http-01 mode, got %q", ord.Status)
}
if len(ord.Authorizations) != 1 {
t.Fatalf("expected 1 authz, got %d", len(ord.Authorizations))
}
// Fetch the authz to find the challenge URL + token.
nonce = resp.Header.Get("Replay-Nonce")
authzPath := strings.TrimPrefix(ord.Authorizations[0], hsrv.URL)
jws = jwsSign(t, acctKey, nil, kid, nonce, ord.Authorizations[0], []byte("{}"))
resp, body = post(t, hsrv.URL, authzPath, jws)
var az AuthzResp
if err := json.Unmarshal(body, &az); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if len(az.Challenges) == 0 {
t.Fatalf("no challenges; body=%s", body)
}
ch := az.Challenges[0]
// Compute keyAuthorization and register on the challenge server.
ka, err := keyAuthorization(ch.Token, jwkBytes)
if err != nil {
t.Fatal(err)
}
mu.Lock()
keyauths[ch.Token] = ka
mu.Unlock()
// Trigger the challenge.
nonce = resp.Header.Get("Replay-Nonce")
chPath := strings.TrimPrefix(ch.URL, hsrv.URL)
jws = jwsSign(t, acctKey, nil, kid, nonce, ch.URL, []byte("{}"))
resp, body = post(t, hsrv.URL, chPath, jws)
if resp.StatusCode != 200 {
t.Fatalf("challenge POST status=%d body=%s", resp.StatusCode, body)
}
var got ChallengeMsg
if err := json.Unmarshal(body, &got); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if got.Status != "valid" {
t.Errorf("challenge status = %q, want valid", got.Status)
}
// The order should now be ready.
nonce = resp.Header.Get("Replay-Nonce")
orderURL := strings.TrimSuffix(ord.Finalize, "/finalize/"+lastSeg(ord.Finalize)) + "/order/" + lastSeg(ord.Finalize)
_ = orderURL
// Skip the GET-as-POST order check; instead just finalize and
// verify we get a valid cert.
csrKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
csrTmpl := &x509.CertificateRequest{
Subject: pkix.Name{CommonName: chHost},
DNSNames: []string{chHost},
}
csrDER, _ := x509.CreateCertificateRequest(rand.Reader, csrTmpl, csrKey)
jws = jwsSign(t, acctKey, nil, kid, nonce, ord.Finalize,
mustMarshal(FinalizeReq{CSR: base64.RawURLEncoding.EncodeToString(csrDER)}))
resp, body = post(t, hsrv.URL, strings.TrimPrefix(ord.Finalize, hsrv.URL), jws)
if resp.StatusCode != 200 {
t.Fatalf("finalize status=%d body=%s", resp.StatusCode, body)
}
var ord2 OrderResp
if err := json.Unmarshal(body, &ord2); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if ord2.Status != "valid" {
t.Errorf("post-finalize order status = %q, want valid", ord2.Status)
}
if ord2.Certificate == "" {
t.Error("missing cert URL")
}
// Cert is fetchable via POST-as-GET (RFC 8555 §6.3).
_, cb := postAsGet(t, hsrv.URL, ord2.Certificate, acctKey, kid, "")
if !strings.Contains(string(cb), "BEGIN CERTIFICATE") {
t.Errorf("not a PEM cert: %q", cb)
}
}
func lastSeg(u string) string {
if i := strings.LastIndex(u, "/"); i >= 0 {
return u[i+1:]
}
return u
}
// TestHTTP01ChallengeRejectsBadResponse confirms a misconfigured client
// (wrong keyAuthorization) gets challenge → invalid.
func TestHTTP01ChallengeRejectsBadResponse(t *testing.T) {
dir := t.TempDir()
fs, _ := storage.New(dir)
seed := make([]byte, signer.SeedSize)
for i := range seed {
seed[i] = 0xBB
}
s, _ := signer.FromSeed(signer.AlgMLDSA44, seed)
l, _ := cactuslog.New(context.Background(), cactuslog.Config{
LogID: cert.TrustAnchorID("32473.1"), CosignerID: cert.TrustAnchorID("32473.1"),
Signer: s, FS: fs, FlushPeriod: 25 * time.Millisecond,
})
defer l.Stop()
issuer, _ := ca.New(l, "32473.1", 1)
srv, _ := New(Config{Issuer: issuer, ChallengeMode: ChallengeHTTP01})
hsrv := httptest.NewServer(srv.Handler())
defer hsrv.Close()
srv.SetExternalURL(hsrv.URL)
// Challenge HTTP server that always returns the wrong body.
chsrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("nope"))
}))
defer chsrv.Close()
chHost := strings.TrimPrefix(chsrv.URL, "http://")
acctKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
acctJWK := &jose.JSONWebKey{Key: acctKey.Public(), Algorithm: "ES256"}
nonce := nonceFor(t, hsrv.URL)
jws := jwsSign(t, acctKey, acctJWK, "", nonce, hsrv.URL+"/new-account",
mustMarshal(NewAccountReq{TermsOfServiceAgreed: true}))
resp, _ := post(t, hsrv.URL, "/new-account", jws)
kid := resp.Header.Get("Location")
nonce = resp.Header.Get("Replay-Nonce")
jws = jwsSign(t, acctKey, nil, kid, nonce, hsrv.URL+"/new-order",
mustMarshal(NewOrderReq{Identifiers: []Identifier{{Type: "dns", Value: chHost}}}))
resp, body := post(t, hsrv.URL, "/new-order", jws)
var ord OrderResp
if err := json.Unmarshal(body, &ord); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
nonce = resp.Header.Get("Replay-Nonce")
authzURL := ord.Authorizations[0]
jws = jwsSign(t, acctKey, nil, kid, nonce, authzURL, []byte("{}"))
resp, body = post(t, hsrv.URL, strings.TrimPrefix(authzURL, hsrv.URL), jws)
var az AuthzResp
if err := json.Unmarshal(body, &az); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if len(az.Challenges) == 0 {
t.Fatalf("no challenges; body=%s", body)
}
ch := az.Challenges[0]
// Don't register a keyauth — the challenge server returns "nope".
nonce = resp.Header.Get("Replay-Nonce")
chPath := strings.TrimPrefix(ch.URL, hsrv.URL)
jws = jwsSign(t, acctKey, nil, kid, nonce, ch.URL, []byte("{}"))
resp, _ = post(t, hsrv.URL, chPath, jws)
if resp.StatusCode != http.StatusForbidden {
t.Errorf("expected 403, got %d", resp.StatusCode)
}
}