api_byte_diff_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "encoding/asn1"
  10. "errors"
  11. "fmt"
  12. "net/url"
  13. "testing"
  14. "github.com/stretchr/testify/require"
  15. "github.com/smallstep/pkcs7"
  16. smallscep "github.com/smallstep/scep"
  17. "go.step.sm/crypto/minica"
  18. "github.com/smallstep/certificates/authority/provisioner"
  19. "github.com/smallstep/certificates/scep"
  20. )
  21. // oidSCEPfailInfoText mirrors the attribute the CA signs into failure CertReps.
  22. var oidSCEPfailInfoText = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 24, 1}
  23. type noopSignAuthority struct{}
  24. func (noopSignAuthority) SignWithContext(_ context.Context, _ *x509.CertificateRequest, _ provisioner.SignOptions, _ ...provisioner.SignOption) ([]*x509.Certificate, error) {
  25. return nil, errors.New("not implemented")
  26. }
  27. func (noopSignAuthority) LoadProvisionerByName(string) (provisioner.Interface, error) {
  28. return &provisioner.SCEP{}, nil
  29. }
  30. // TestCreateFailureResponseWireUnchanged proves the client-visible CertRep is
  31. // unchanged when the real error is passed as failError: the signed attributes
  32. // carry the same failInfo and infoText as before, and the internal failError
  33. // text never appears in the DER bytes.
  34. func TestCreateFailureResponseWireUnchanged(t *testing.T) {
  35. ca, err := minica.New(minica.WithGetSignerFunc(func() (crypto.Signer, error) {
  36. return rsa.GenerateKey(rand.Reader, 2048)
  37. }))
  38. require.NoError(t, err)
  39. auth, err := scep.New(noopSignAuthority{}, scep.Options{
  40. Roots: []*x509.Certificate{ca.Root},
  41. Intermediates: []*x509.Certificate{ca.Intermediate},
  42. SignerCert: ca.Intermediate,
  43. Signer: ca.Signer,
  44. })
  45. require.NoError(t, err)
  46. ctx := scep.NewProvisionerContext(scep.NewContext(context.Background(), auth), &provisioner.SCEP{})
  47. msg := &scep.PKIMessage{
  48. TransactionID: smallscep.TransactionID("txn-123"),
  49. MessageType: smallscep.PKCSReq,
  50. SenderNonce: smallscep.SenderNonce("0123456789abcdef"),
  51. }
  52. webhookErr := &url.Error{Op: "Post", URL: "https://hooks.example.com/validate?token=secret", Err: errors.New("connection refused")}
  53. cases := []struct {
  54. name string
  55. infoText string
  56. failError error
  57. // skipByteCheck when failError text is also the infoText (already on the wire).
  58. skipByteCheck bool
  59. }{
  60. {"challenge invalid", "webhook server did not allow request", errors.New("webhook server did not allow request"), true},
  61. {"challenge validation error", "failed validating challenge password", webhookErr, false},
  62. {"unexpected message type", "unexpected message type: (19)", fmt.Errorf("unexpected message type: (%s)", smallscep.PKCSReq), false},
  63. {"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},
  64. }
  65. for _, tc := range cases {
  66. t.Run(tc.name, func(t *testing.T) {
  67. resp, err := createFailureResponse(ctx, nil, msg, smallscep.BadRequest, tc.infoText, tc.failError)
  68. require.NoError(t, err)
  69. // The internal error is returned for logging, not for the wire.
  70. require.ErrorIs(t, resp.Error, tc.failError)
  71. // The CertRep still parses as a FAILURE with failInfo badRequest.
  72. parsed, err := smallscep.ParsePKIMessage(resp.Data)
  73. require.NoError(t, err)
  74. require.Equal(t, smallscep.FAILURE, parsed.PKIStatus)
  75. require.Equal(t, smallscep.BadRequest, parsed.FailInfo)
  76. // The signed failInfoText attribute is the fixed client-facing sentence.
  77. p7, err := pkcs7.Parse(resp.Data)
  78. require.NoError(t, err)
  79. var text string
  80. require.NoError(t, p7.UnmarshalSignedAttribute(oidSCEPfailInfoText, &text))
  81. require.Equal(t, tc.infoText, text)
  82. // The internal failError text is not in the DER bytes.
  83. if !tc.skipByteCheck {
  84. require.NotContains(t, string(resp.Data), tc.failError.Error())
  85. require.False(t, bytes.Contains(resp.Data, []byte("token=secret")))
  86. }
  87. })
  88. }
  89. }