|
| 1 | +// Copyright 2026 oliverpool |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package acmez_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "crypto/ecdsa" |
| 20 | + "crypto/elliptic" |
| 21 | + "crypto/rand" |
| 22 | + "crypto/x509" |
| 23 | + "encoding/pem" |
| 24 | + "io" |
| 25 | + "log" |
| 26 | + "log/slog" |
| 27 | + "net" |
| 28 | + "net/http" |
| 29 | + "net/http/httptest" |
| 30 | + "strconv" |
| 31 | + "sync/atomic" |
| 32 | + "testing" |
| 33 | + |
| 34 | + "code.pfad.fr/check" |
| 35 | + "github.com/letsencrypt/pebble/v2/ca" |
| 36 | + "github.com/letsencrypt/pebble/v2/db" |
| 37 | + "github.com/letsencrypt/pebble/v2/va" |
| 38 | + "github.com/letsencrypt/pebble/v2/wfe" |
| 39 | + "github.com/mholt/acmez/v3" |
| 40 | + "github.com/mholt/acmez/v3/acme" |
| 41 | +) |
| 42 | + |
| 43 | +func newHttpSolver(t *testing.T) (port int, solver acmez.Solver) { |
| 44 | + hsolver := &httpSolver{} |
| 45 | + s := httptest.NewServer(hsolver) |
| 46 | + t.Cleanup(s.Close) |
| 47 | + |
| 48 | + hostPort := s.Listener.Addr().String() |
| 49 | + _, sport, err := net.SplitHostPort(hostPort) |
| 50 | + check.Equal(t, nil, err) |
| 51 | + port, err = strconv.Atoi(sport) |
| 52 | + check.Equal(t, nil, err) |
| 53 | + |
| 54 | + return port, hsolver |
| 55 | +} |
| 56 | + |
| 57 | +type httpSolver struct { |
| 58 | + challenge atomic.Pointer[acme.Challenge] |
| 59 | +} |
| 60 | + |
| 61 | +// ServeHTTP implements http.Handler. |
| 62 | +func (h *httpSolver) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 63 | + chal := h.challenge.Load() |
| 64 | + if chal == nil || r.URL.Path != chal.HTTP01ResourcePath() || r.Method != http.MethodGet { |
| 65 | + http.NotFound(w, r) |
| 66 | + return |
| 67 | + } |
| 68 | + w.Header().Add("Content-Type", "text/plain") |
| 69 | + w.Write([]byte(chal.KeyAuthorization)) |
| 70 | +} |
| 71 | + |
| 72 | +// Present implements Solver. |
| 73 | +func (h *httpSolver) Present(ctx context.Context, chal acme.Challenge) error { |
| 74 | + h.challenge.Store(&chal) |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// CleanUp implements Solver. |
| 79 | +func (h *httpSolver) CleanUp(context.Context, acme.Challenge) error { |
| 80 | + h.challenge.Store(nil) |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func TestAlreadyReplaced(t *testing.T) { |
| 85 | + solverPort, solver := newHttpSolver(t) |
| 86 | + client := newAcmeClient(t, solverPort, 0) |
| 87 | + c := acmez.Client{ |
| 88 | + Client: client, |
| 89 | + ChallengeSolvers: map[string]acmez.Solver{ |
| 90 | + "http-01": solver, |
| 91 | + }, |
| 92 | + } |
| 93 | + privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 94 | + check.Equal(t, nil, err) |
| 95 | + account, err := c.NewAccount(t.Context(), acme.Account{ |
| 96 | + TermsOfServiceAgreed: true, |
| 97 | + PrivateKey: privateKey, |
| 98 | + }) |
| 99 | + check.Equal(t, nil, err) |
| 100 | + |
| 101 | + certKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 102 | + check.Equal(t, nil, err) |
| 103 | + sans := []string{"127.0.0.1"} |
| 104 | + |
| 105 | + // initial cert |
| 106 | + initialCerts, err := c.ObtainCertificateForSANs(t.Context(), account, certKey, sans) |
| 107 | + check.Equal(t, nil, err) |
| 108 | + block, _ := pem.Decode(initialCerts[0].ChainPEM) |
| 109 | + toReplace, err := x509.ParseCertificate(block.Bytes) |
| 110 | + check.Equal(t, nil, err) |
| 111 | + |
| 112 | + { |
| 113 | + // initial relacement |
| 114 | + csr, err := acmez.NewCSR(certKey, sans) |
| 115 | + check.Equal(t, nil, err) |
| 116 | + params, err := acmez.OrderParametersFromCSR(account, csr) |
| 117 | + check.Equal(t, nil, err) |
| 118 | + params.Replaces = toReplace |
| 119 | + _, err = c.ObtainCertificate(t.Context(), params) |
| 120 | + check.Equal(t, nil, err) |
| 121 | + } |
| 122 | + |
| 123 | + { |
| 124 | + // second replacement (of the same certificate) |
| 125 | + csr, err := acmez.NewCSR(certKey, sans) |
| 126 | + check.Equal(t, nil, err) |
| 127 | + params, err := acmez.OrderParametersFromCSR(account, csr) |
| 128 | + check.Equal(t, nil, err) |
| 129 | + params.Replaces = toReplace |
| 130 | + _, err = c.ObtainCertificate(t.Context(), params) |
| 131 | + check.Equal(t, nil, err) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func newAcmeClient(t *testing.T, httpPort, tlsPort int) *acme.Client { |
| 136 | + s := newPebbleServer(t, httpPort, tlsPort) |
| 137 | + return &acme.Client{ |
| 138 | + Directory: s.URL + wfe.DirectoryPath, |
| 139 | + HTTPClient: s.Client(), |
| 140 | + // Logger: slog.New(slog.NewTextHandler(t.Output(), nil)), |
| 141 | + Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func newPebbleServer(t *testing.T, httpPort, tlsPort int) *httptest.Server { |
| 146 | + t.Setenv("PEBBLE_VA_NOSLEEP", "1") // https://github.com/letsencrypt/pebble/blob/23ab0beb482ac4760d7f3064141128a74d0d9430/va/va.go#L51 |
| 147 | + // logger := log.New(t.Output(), "test", log.Llongfile) |
| 148 | + logger := log.New(io.Discard, "test", log.Llongfile) |
| 149 | + db := db.NewMemoryStore() |
| 150 | + keyAlg := "rsa" |
| 151 | + alternateRoots := 0 |
| 152 | + chainLength := 1 |
| 153 | + profiles := map[string]ca.Profile{ |
| 154 | + "default": { |
| 155 | + Description: "The default profile", |
| 156 | + ValidityPeriod: 0, // Will be overridden by the CA's default |
| 157 | + }, |
| 158 | + } |
| 159 | + ca := ca.New(logger, db, "", keyAlg, alternateRoots, chainLength, profiles) |
| 160 | + va := va.New(logger, httpPort, tlsPort, true, "", db) |
| 161 | + wfeImpl := wfe.New(logger, db, va, ca, true, false, 0, 0) |
| 162 | + |
| 163 | + s := httptest.NewUnstartedServer(wfeImpl.Handler()) |
| 164 | + s.StartTLS() |
| 165 | + t.Cleanup(s.Close) |
| 166 | + return s |
| 167 | +} |
0 commit comments