Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions wfe/wfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func (wfe *WebFrontEndImpl) verifyJWS(
Path: request.RequestURI,
}
if expectedURL.String() != headerURL {
return nil, acme.MalformedProblem(fmt.Sprintf(
return nil, acme.UnauthorizedProblem(fmt.Sprintf(
"JWS header parameter 'url' incorrect. Expected %q, got %q",
expectedURL.String(), headerURL))
}
Expand Down Expand Up @@ -2220,27 +2220,27 @@ func (wfe *WebFrontEndImpl) FinalizeOrder( //nolint:gocyclo,gocognit

// Check that the CSR has the same number of names as the initial order contained
if len(csrDNSs) != len(orderDNSs) {
wfe.sendError(acme.UnauthorizedProblem(
wfe.sendError(acme.BadCSRProblem(
"Order includes different number of DNSnames identifiers than CSR specifies"), response)
return
}
if len(csrIPs) != len(orderIPs) {
wfe.sendError(acme.UnauthorizedProblem(
wfe.sendError(acme.BadCSRProblem(
"Order includes different number of IP address identifiers than CSR specifies"), response)
return
}

// Check that the CSR's names match the order names exactly
for i, name := range orderDNSs {
if name != csrDNSs[i] {
wfe.sendError(acme.UnauthorizedProblem(
wfe.sendError(acme.BadCSRProblem(
fmt.Sprintf("CSR is missing Order domain %q", name)), response)
return
}
}
for i, IP := range orderIPs {
if !csrIPs[i].Equal(IP) {
wfe.sendError(acme.UnauthorizedProblem(
wfe.sendError(acme.BadCSRProblem(
fmt.Sprintf("CSR is missing Order IP %q", IP)), response)
return
}
Expand Down
170 changes: 170 additions & 0 deletions wfe/wfe_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package wfe

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"io"
"log"
"net"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"

"github.com/go-jose/go-jose/v4"

"github.com/letsencrypt/pebble/v2/acme"
"github.com/letsencrypt/pebble/v2/core"
"github.com/letsencrypt/pebble/v2/db"
)

func TestNewAccountRejectsUnsupportedSignatureAlgorithm(t *testing.T) {
Expand Down Expand Up @@ -48,3 +59,162 @@ func TestNewAccountRejectsUnsupportedSignatureAlgorithm(t *testing.T) {
t.Errorf("algorithms = %q, want %q", problem.Algorithms, wantAlgorithms)
}
}

// signedPOST returns a POST to path whose body is a JWS over payload signed
// by key, with the given "url" header and a fresh nonce from wfe. With an
// empty kid the JWK is embedded instead.
func signedPOST(t *testing.T, wfe *WebFrontEndImpl, key *ecdsa.PrivateKey, path, headerURL, kid string, payload []byte) *http.Request {
t.Helper()

signingKey := jose.SigningKey{Algorithm: jose.ES256, Key: key}
opts := &jose.SignerOptions{EmbedJWK: kid == ""}
if kid != "" {
signingKey.Key = jose.JSONWebKey{Key: key, KeyID: kid}
}
opts.WithHeader("nonce", wfe.nonce.createNonce())
opts.WithHeader("url", headerURL)
signer, err := jose.NewSigner(signingKey, opts)
if err != nil {
t.Fatalf("making signer: %v", err)
}
jws, err := signer.Sign(payload)
if err != nil {
t.Fatalf("signing: %v", err)
}

req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(jws.FullSerialize()))
req.TLS = &tls.ConnectionState{}
req.Header.Set("Content-Type", expectedJWSContentType)
req.Header.Set("User-Agent", "pebble-test")
return req
}

// problemOf serves req and returns the status code and problem document.
func problemOf(t *testing.T, wfe *WebFrontEndImpl, req *http.Request) (int, acme.ProblemDetails) {
t.Helper()

recorder := httptest.NewRecorder()
wfe.Handler().ServeHTTP(recorder, req)
var problem acme.ProblemDetails
if err := json.Unmarshal(recorder.Body.Bytes(), &problem); err != nil {
t.Fatalf("unmarshaling problem response %q: %v", recorder.Body.String(), err)
}
return recorder.Code, problem
}

func newTestKey(t *testing.T) *ecdsa.PrivateKey {
t.Helper()

key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("generating key: %v", err)
}
return key
}

func TestURLMismatchIsUnauthorized(t *testing.T) {
t.Parallel()

wfe := &WebFrontEndImpl{
log: log.New(io.Discard, "", 0),
nonce: newNonceMap(),
}
req := signedPOST(t, wfe, newTestKey(t), newAccountPath,
"https://example.com/not-the-new-account-url", "", []byte(`{"termsOfServiceAgreed":true}`))

code, problem := problemOf(t, wfe, req)

// RFC 8555 Section 6.4: a "url" header parameter that is not the request
// URL MUST be rejected as unauthorized.
if code != http.StatusForbidden {
t.Errorf("response status = %d, want %d", code, http.StatusForbidden)
}
if problem.Type != "urn:ietf:params:acme:error:unauthorized" {
t.Errorf("problem type = %q, want unauthorized (detail: %q)", problem.Type, problem.Detail)
}
}

func TestFinalizeWithDifferentIdentifiersIsBadCSR(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
dnsNames []string
ips []net.IP
}{
{name: "another domain", dnsNames: []string{"other.example.com"}, ips: []net.IP{net.ParseIP("192.0.2.1")}},
{name: "one more domain", dnsNames: []string{"example.com", "other.example.com"}, ips: []net.IP{net.ParseIP("192.0.2.1")}},
{name: "another IP address", dnsNames: []string{"example.com"}, ips: []net.IP{net.ParseIP("192.0.2.2")}},
{name: "no IP address", dnsNames: []string{"example.com"}},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

memoryStore := db.NewMemoryStore()
wfe := &WebFrontEndImpl{
log: log.New(io.Discard, "", 0),
nonce: newNonceMap(),
db: memoryStore,
}
accountKey := newTestKey(t)
account := &core.Account{
Account: acme.Account{Status: acme.StatusValid},
Key: &jose.JSONWebKey{Key: accountKey.Public()},
}
if _, err := memoryStore.AddAccount(account); err != nil {
t.Fatalf("adding account: %v", err)
}
// An order for example.com and 192.0.2.1 whose authorizations are
// valid, so that it is ready to finalize.
identifiers := []acme.Identifier{
{Type: acme.IdentifierDNS, Value: "example.com"},
{Type: acme.IdentifierIP, Value: "192.0.2.1"},
}
var authzs []*core.Authorization
for _, ident := range identifiers {
authzs = append(authzs, &core.Authorization{
Authorization: acme.Authorization{Status: acme.StatusValid, Identifier: ident},
ExpiresDate: time.Now().Add(time.Hour),
})
}
order := &core.Order{
ID: "order-1",
AccountID: account.ID,
Order: acme.Order{Identifiers: identifiers},
ExpiresDate: time.Now().Add(time.Hour),
AuthorizationObjects: authzs,
}
if _, err := memoryStore.AddOrder(order); err != nil {
t.Fatalf("adding order: %v", err)
}

csr, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{
DNSNames: tc.dnsNames,
IPAddresses: tc.ips,
}, newTestKey(t))
if err != nil {
t.Fatalf("making CSR: %v", err)
}
payload, err := json.Marshal(map[string]string{"csr": base64.RawURLEncoding.EncodeToString(csr)})
if err != nil {
t.Fatalf("marshaling finalize request: %v", err)
}
path := orderFinalizePath + order.ID
req := signedPOST(t, wfe, accountKey, path, "https://example.com"+path,
"https://example.com"+acctPath+account.ID, payload)

code, problem := problemOf(t, wfe, req)

// RFC 8555 Section 7.4: when the CSR and order identifiers differ,
// the problem document SHOULD use error code "badCSR".
if code != http.StatusBadRequest {
t.Errorf("response status = %d, want %d", code, http.StatusBadRequest)
}
if problem.Type != "urn:ietf:params:acme:error:badCSR" {
t.Errorf("problem type = %q, want badCSR (detail: %q)", problem.Type, problem.Detail)
}
})
}
}
Loading