Browse Source

scep/api: log the real challenge error via failError

The challenge-validation failure path passed the fixed sentence
'failed validating challenge password' as both infoText and failError,
discarding the underlying error. Keep infoText unchanged on the wire and
pass the real error as failError, so writeResponse logs the actual cause.
A test proves the CertRep attributes are unchanged and the internal error
never appears in the DER bytes.
Christian Mejlak 1 month ago
parent
commit
93cfcc2503
2 changed files with 108 additions and 2 deletions
  1. 3 2
      scep/api/api.go
  2. 105 0
      scep/api/api_byte_diff_test.go

+ 3 - 2
scep/api/api.go

@@ -390,8 +390,9 @@ func PKIOperation(ctx context.Context, req request) (Response, error) {
 			if errors.Is(err, provisioner.ErrSCEPChallengeInvalid) {
 				return createFailureResponse(ctx, csr, msg, smallscep.BadRequest, err.Error(), err)
 			}
-			scepErr := errors.New("failed validating challenge password")
-			return createFailureResponse(ctx, csr, msg, smallscep.BadRequest, scepErr.Error(), scepErr)
+			// Keep the client-facing infoText fixed, but pass the real error
+			// as failError so writeResponse logs the actual cause.
+			return createFailureResponse(ctx, csr, msg, smallscep.BadRequest, "failed validating challenge password", err)
 		}
 		signCSROpts = append(signCSROpts, challengeOptions...)
 	} else {

+ 105 - 0
scep/api/api_byte_diff_test.go

@@ -0,0 +1,105 @@
+package api
+
+import (
+	"bytes"
+	"context"
+	"crypto"
+	"crypto/rand"
+	"crypto/rsa"
+	"crypto/x509"
+	"encoding/asn1"
+	"errors"
+	"fmt"
+	"net/url"
+	"testing"
+
+	"github.com/smallstep/certificates/authority/provisioner"
+	"github.com/smallstep/certificates/scep"
+	"github.com/smallstep/pkcs7"
+	smallscep "github.com/smallstep/scep"
+	"github.com/stretchr/testify/require"
+	"go.step.sm/crypto/minica"
+)
+
+// oidSCEPfailInfoText mirrors the attribute the CA signs into failure CertReps.
+var oidSCEPfailInfoText = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 24, 1}
+
+type noopSignAuthority struct{}
+
+func (noopSignAuthority) SignWithContext(_ context.Context, _ *x509.CertificateRequest, _ provisioner.SignOptions, _ ...provisioner.SignOption) ([]*x509.Certificate, error) {
+	return nil, errors.New("not implemented")
+}
+
+func (noopSignAuthority) LoadProvisionerByName(string) (provisioner.Interface, error) {
+	return &provisioner.SCEP{}, nil
+}
+
+// TestCreateFailureResponseWireUnchanged proves the client-visible CertRep is
+// unchanged when the real error is passed as failError: the signed attributes
+// carry the same failInfo and infoText as before, and the internal failError
+// text never appears in the DER bytes.
+func TestCreateFailureResponseWireUnchanged(t *testing.T) {
+	ca, err := minica.New(minica.WithGetSignerFunc(func() (crypto.Signer, error) {
+		return rsa.GenerateKey(rand.Reader, 2048)
+	}))
+	require.NoError(t, err)
+
+	auth, err := scep.New(noopSignAuthority{}, scep.Options{
+		Roots:         []*x509.Certificate{ca.Root},
+		Intermediates: []*x509.Certificate{ca.Intermediate},
+		SignerCert:    ca.Intermediate,
+		Signer:        ca.Signer,
+	})
+	require.NoError(t, err)
+
+	ctx := scep.NewProvisionerContext(scep.NewContext(context.Background(), auth), &provisioner.SCEP{})
+	msg := &scep.PKIMessage{
+		TransactionID: smallscep.TransactionID("txn-123"),
+		MessageType:   smallscep.PKCSReq,
+		SenderNonce:   smallscep.SenderNonce("0123456789abcdef"),
+	}
+
+	webhookErr := &url.Error{Op: "Post", URL: "https://hooks.example.com/validate?token=secret", Err: errors.New("connection refused")}
+
+	cases := []struct {
+		name      string
+		infoText  string
+		failError error
+		// skipByteCheck when failError text is also the infoText (already on the wire).
+		skipByteCheck bool
+	}{
+		{"challenge invalid", "webhook server did not allow request", errors.New("webhook server did not allow request"), true},
+		{"challenge validation error", "failed validating challenge password", webhookErr, false},
+		{"unexpected message type", "unexpected message type: (19)", fmt.Errorf("unexpected message type: (%s)", smallscep.PKCSReq), false},
+		{"signing failure", "internal server error; please see the certificate authority logs for more info", fmt.Errorf("error when signing new certificate: %w", errors.New("boom")), false},
+	}
+
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			resp, err := createFailureResponse(ctx, nil, msg, smallscep.BadRequest, tc.infoText, tc.failError)
+			require.NoError(t, err)
+
+			// The internal error is returned for logging, not for the wire.
+			require.ErrorIs(t, resp.Error, tc.failError)
+
+			// The CertRep still parses as a FAILURE with failInfo badRequest.
+			parsed, err := smallscep.ParsePKIMessage(resp.Data)
+			require.NoError(t, err)
+			require.Equal(t, smallscep.FAILURE, parsed.PKIStatus)
+			require.Equal(t, smallscep.BadRequest, parsed.FailInfo)
+
+			// The signed failInfoText attribute is the fixed client-facing sentence.
+			p7, err := pkcs7.Parse(resp.Data)
+			require.NoError(t, err)
+			var text string
+			require.NoError(t, p7.UnmarshalSignedAttribute(oidSCEPfailInfoText, &text))
+			require.Equal(t, tc.infoText, text)
+
+			// The internal failError text is not in the DER bytes.
+			if !tc.skipByteCheck {
+				require.NotContains(t, string(resp.Data), tc.failError.Error())
+				require.False(t, bytes.Contains(resp.Data, []byte("token=secret")))
+			}
+		})
+	}
+}