certificates/acme/errors.go

379 lines
12 KiB
Go
Raw Normal View History

2019-05-27 00:41:10 +00:00
package acme
import (
"encoding/json"
2021-03-01 06:49:20 +00:00
"fmt"
2021-03-30 06:16:39 +00:00
"log"
"net/http"
"os"
2021-03-01 06:49:20 +00:00
2019-05-27 00:41:10 +00:00
"github.com/pkg/errors"
2021-03-30 06:16:39 +00:00
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/logging"
2019-05-27 00:41:10 +00:00
)
2021-03-01 06:49:20 +00:00
// ProblemType is the type of the ACME problem.
type ProblemType int
2019-05-27 00:41:10 +00:00
const (
2021-03-03 23:16:25 +00:00
// ErrorAccountDoesNotExistType request specified an account that does not exist
2021-03-01 06:49:20 +00:00
ErrorAccountDoesNotExistType ProblemType = iota
2021-03-03 23:16:25 +00:00
// ErrorAlreadyRevokedType request specified a certificate to be revoked that has already been revoked
2021-03-01 06:49:20 +00:00
ErrorAlreadyRevokedType
2021-03-03 23:16:25 +00:00
// ErrorBadCSRType CSR is unacceptable (e.g., due to a short key)
2021-03-01 06:49:20 +00:00
ErrorBadCSRType
2021-03-03 23:16:25 +00:00
// ErrorBadNonceType client sent an unacceptable anti-replay nonce
2021-03-01 06:49:20 +00:00
ErrorBadNonceType
2021-03-03 23:16:25 +00:00
// ErrorBadPublicKeyType JWS was signed by a public key the server does not support
2021-03-01 06:49:20 +00:00
ErrorBadPublicKeyType
2021-03-03 23:16:25 +00:00
// ErrorBadRevocationReasonType revocation reason provided is not allowed by the server
2021-03-01 06:49:20 +00:00
ErrorBadRevocationReasonType
2021-03-03 23:16:25 +00:00
// ErrorBadSignatureAlgorithmType JWS was signed with an algorithm the server does not support
2021-03-01 06:49:20 +00:00
ErrorBadSignatureAlgorithmType
2021-03-03 23:16:25 +00:00
// ErrorCaaType Authority Authorization (CAA) records forbid the CA from issuing a certificate
2021-03-01 06:49:20 +00:00
ErrorCaaType
2021-03-03 23:16:25 +00:00
// ErrorCompoundType error conditions are indicated in the “subproblems” array.
2021-03-01 06:49:20 +00:00
ErrorCompoundType
2021-03-03 23:16:25 +00:00
// ErrorConnectionType server could not connect to validation target
2021-03-01 06:49:20 +00:00
ErrorConnectionType
2021-03-03 23:16:25 +00:00
// ErrorDNSType was a problem with a DNS query during identifier validation
2021-03-01 06:49:20 +00:00
ErrorDNSType
2021-03-03 23:16:25 +00:00
// ErrorExternalAccountRequiredType request must include a value for the “externalAccountBinding” field
2021-03-01 06:49:20 +00:00
ErrorExternalAccountRequiredType
2021-03-03 23:16:25 +00:00
// ErrorIncorrectResponseType received didn’t match the challenge’s requirements
2021-03-01 06:49:20 +00:00
ErrorIncorrectResponseType
2021-03-03 23:16:25 +00:00
// ErrorInvalidContactType URL for an account was invalid
2021-03-01 06:49:20 +00:00
ErrorInvalidContactType
2021-03-03 23:16:25 +00:00
// ErrorMalformedType request message was malformed
2021-03-01 06:49:20 +00:00
ErrorMalformedType
2021-03-03 23:16:25 +00:00
// ErrorOrderNotReadyType request attempted to finalize an order that is not ready to be finalized
2021-03-01 06:49:20 +00:00
ErrorOrderNotReadyType
2021-03-03 23:16:25 +00:00
// ErrorRateLimitedType request exceeds a rate limit
2021-03-01 06:49:20 +00:00
ErrorRateLimitedType
2021-03-03 23:16:25 +00:00
// ErrorRejectedIdentifierType server will not issue certificates for the identifier
2021-03-01 06:49:20 +00:00
ErrorRejectedIdentifierType
2021-03-03 23:16:25 +00:00
// ErrorServerInternalType server experienced an internal error
2021-03-01 06:49:20 +00:00
ErrorServerInternalType
2021-03-03 23:16:25 +00:00
// ErrorTLSType server received a TLS error during validation
2021-03-01 06:49:20 +00:00
ErrorTLSType
2021-03-03 23:16:25 +00:00
// ErrorUnauthorizedType client lacks sufficient authorization
2021-03-01 06:49:20 +00:00
ErrorUnauthorizedType
2021-03-03 23:16:25 +00:00
// ErrorUnsupportedContactType URL for an account used an unsupported protocol scheme
2021-03-01 06:49:20 +00:00
ErrorUnsupportedContactType
2021-03-03 23:16:25 +00:00
// ErrorUnsupportedIdentifierType identifier is of an unsupported type
2021-03-01 06:49:20 +00:00
ErrorUnsupportedIdentifierType
2021-03-03 23:16:25 +00:00
// ErrorUserActionRequiredType the “instance” URL and take actions specified there
2021-03-01 06:49:20 +00:00
ErrorUserActionRequiredType
2021-03-03 23:16:25 +00:00
// ErrorNotImplementedType operation is not implemented
2021-03-01 06:49:20 +00:00
ErrorNotImplementedType
2019-05-27 00:41:10 +00:00
)
// String returns the string representation of the acme problem type,
// fulfilling the Stringer interface.
2021-03-01 06:49:20 +00:00
func (ap ProblemType) String() string {
2019-05-27 00:41:10 +00:00
switch ap {
2021-03-01 06:49:20 +00:00
case ErrorAccountDoesNotExistType:
2019-05-27 00:41:10 +00:00
return "accountDoesNotExist"
2021-03-01 06:49:20 +00:00
case ErrorAlreadyRevokedType:
2019-05-27 00:41:10 +00:00
return "alreadyRevoked"
2021-03-01 06:49:20 +00:00
case ErrorBadCSRType:
2019-05-27 00:41:10 +00:00
return "badCSR"
2021-03-01 06:49:20 +00:00
case ErrorBadNonceType:
2019-05-27 00:41:10 +00:00
return "badNonce"
2021-03-01 06:49:20 +00:00
case ErrorBadPublicKeyType:
2019-05-27 00:41:10 +00:00
return "badPublicKey"
2021-03-01 06:49:20 +00:00
case ErrorBadRevocationReasonType:
2019-05-27 00:41:10 +00:00
return "badRevocationReason"
2021-03-01 06:49:20 +00:00
case ErrorBadSignatureAlgorithmType:
2019-05-27 00:41:10 +00:00
return "badSignatureAlgorithm"
2021-03-01 06:49:20 +00:00
case ErrorCaaType:
2019-05-27 00:41:10 +00:00
return "caa"
2021-03-01 06:49:20 +00:00
case ErrorCompoundType:
2019-05-27 00:41:10 +00:00
return "compound"
2021-03-01 06:49:20 +00:00
case ErrorConnectionType:
2019-05-27 00:41:10 +00:00
return "connection"
2021-03-01 06:49:20 +00:00
case ErrorDNSType:
2019-05-27 00:41:10 +00:00
return "dns"
2021-03-01 06:49:20 +00:00
case ErrorExternalAccountRequiredType:
2019-05-27 00:41:10 +00:00
return "externalAccountRequired"
2021-03-01 06:49:20 +00:00
case ErrorInvalidContactType:
2019-05-27 00:41:10 +00:00
return "incorrectResponse"
2021-03-01 06:49:20 +00:00
case ErrorMalformedType:
2019-05-27 00:41:10 +00:00
return "malformed"
2021-03-01 06:49:20 +00:00
case ErrorOrderNotReadyType:
2019-05-27 00:41:10 +00:00
return "orderNotReady"
2021-03-01 06:49:20 +00:00
case ErrorRateLimitedType:
2019-05-27 00:41:10 +00:00
return "rateLimited"
2021-03-01 06:49:20 +00:00
case ErrorRejectedIdentifierType:
2019-05-27 00:41:10 +00:00
return "rejectedIdentifier"
2021-03-01 06:49:20 +00:00
case ErrorServerInternalType:
2019-05-27 00:41:10 +00:00
return "serverInternal"
2021-03-01 06:49:20 +00:00
case ErrorTLSType:
2019-05-27 00:41:10 +00:00
return "tls"
2021-03-01 06:49:20 +00:00
case ErrorUnauthorizedType:
2019-05-27 00:41:10 +00:00
return "unauthorized"
2021-03-01 06:49:20 +00:00
case ErrorUnsupportedContactType:
2019-05-27 00:41:10 +00:00
return "unsupportedContact"
2021-03-01 06:49:20 +00:00
case ErrorUnsupportedIdentifierType:
2019-05-27 00:41:10 +00:00
return "unsupportedIdentifier"
2021-03-01 06:49:20 +00:00
case ErrorUserActionRequiredType:
2019-05-27 00:41:10 +00:00
return "userActionRequired"
2021-03-01 06:49:20 +00:00
case ErrorNotImplementedType:
return "notImplemented"
2019-05-27 00:41:10 +00:00
default:
2021-03-03 23:16:25 +00:00
return fmt.Sprintf("unsupported type ACME error type '%d'", int(ap))
2021-03-01 06:49:20 +00:00
}
}
type errorMetadata struct {
details string
status int
typ string
String string
}
var (
officialACMEPrefix = "urn:ietf:params:acme:error:"
errorServerInternalMetadata = errorMetadata{
2021-03-01 07:33:18 +00:00
typ: officialACMEPrefix + ErrorServerInternalType.String(),
details: "The server experienced an internal error",
status: 500,
2021-03-01 06:49:20 +00:00
}
2021-03-01 07:33:18 +00:00
errorMap = map[ProblemType]errorMetadata{
2021-03-01 06:49:20 +00:00
ErrorAccountDoesNotExistType: {
typ: officialACMEPrefix + ErrorAccountDoesNotExistType.String(),
details: "Account does not exist",
status: 400,
},
ErrorAlreadyRevokedType: {
typ: officialACMEPrefix + ErrorAlreadyRevokedType.String(),
2021-11-26 16:27:42 +00:00
details: "Certificate already revoked",
2021-03-01 06:49:20 +00:00
status: 400,
},
ErrorBadCSRType: {
typ: officialACMEPrefix + ErrorBadCSRType.String(),
details: "The CSR is unacceptable",
status: 400,
},
ErrorBadNonceType: {
typ: officialACMEPrefix + ErrorBadNonceType.String(),
details: "Unacceptable anti-replay nonce",
status: 400,
},
ErrorBadPublicKeyType: {
typ: officialACMEPrefix + ErrorBadPublicKeyType.String(),
details: "The jws was signed by a public key the server does not support",
status: 400,
},
ErrorBadRevocationReasonType: {
typ: officialACMEPrefix + ErrorBadRevocationReasonType.String(),
details: "The revocation reason provided is not allowed by the server",
status: 400,
},
ErrorBadSignatureAlgorithmType: {
typ: officialACMEPrefix + ErrorBadSignatureAlgorithmType.String(),
details: "The JWS was signed with an algorithm the server does not support",
status: 400,
},
ErrorCaaType: {
typ: officialACMEPrefix + ErrorCaaType.String(),
details: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
status: 400,
},
ErrorCompoundType: {
typ: officialACMEPrefix + ErrorCompoundType.String(),
details: "Specific error conditions are indicated in the “subproblems” array",
status: 400,
},
ErrorConnectionType: {
typ: officialACMEPrefix + ErrorConnectionType.String(),
details: "The server could not connect to validation target",
status: 400,
},
ErrorDNSType: {
typ: officialACMEPrefix + ErrorDNSType.String(),
details: "There was a problem with a DNS query during identifier validation",
status: 400,
},
ErrorExternalAccountRequiredType: {
typ: officialACMEPrefix + ErrorExternalAccountRequiredType.String(),
details: "The request must include a value for the \"externalAccountBinding\" field",
status: 400,
},
ErrorIncorrectResponseType: {
typ: officialACMEPrefix + ErrorIncorrectResponseType.String(),
details: "Response received didn't match the challenge's requirements",
status: 400,
},
ErrorInvalidContactType: {
typ: officialACMEPrefix + ErrorInvalidContactType.String(),
details: "A contact URL for an account was invalid",
status: 400,
},
ErrorMalformedType: {
typ: officialACMEPrefix + ErrorMalformedType.String(),
details: "The request message was malformed",
status: 400,
},
ErrorOrderNotReadyType: {
typ: officialACMEPrefix + ErrorOrderNotReadyType.String(),
details: "The request attempted to finalize an order that is not ready to be finalized",
status: 400,
},
ErrorRateLimitedType: {
typ: officialACMEPrefix + ErrorRateLimitedType.String(),
details: "The request exceeds a rate limit",
status: 400,
},
ErrorRejectedIdentifierType: {
typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
details: "The server will not issue certificates for the identifier",
status: 400,
},
ErrorNotImplementedType: {
typ: officialACMEPrefix + ErrorRejectedIdentifierType.String(),
details: "The requested operation is not implemented",
status: 501,
},
ErrorTLSType: {
typ: officialACMEPrefix + ErrorTLSType.String(),
details: "The server received a TLS error during validation",
status: 400,
},
ErrorUnauthorizedType: {
typ: officialACMEPrefix + ErrorUnauthorizedType.String(),
details: "The client lacks sufficient authorization",
status: 401,
},
ErrorUnsupportedContactType: {
typ: officialACMEPrefix + ErrorUnsupportedContactType.String(),
details: "A contact URL for an account used an unsupported protocol scheme",
status: 400,
},
ErrorUnsupportedIdentifierType: {
typ: officialACMEPrefix + ErrorUnsupportedIdentifierType.String(),
details: "An identifier is of an unsupported type",
status: 400,
},
ErrorUserActionRequiredType: {
typ: officialACMEPrefix + ErrorUserActionRequiredType.String(),
details: "Visit the “instance” URL and take actions specified there",
status: 400,
},
ErrorServerInternalType: errorServerInternalMetadata,
2019-05-27 00:41:10 +00:00
}
2021-03-01 06:49:20 +00:00
)
2019-05-27 00:41:10 +00:00
2021-03-01 06:49:20 +00:00
// Error represents an ACME
2019-05-27 00:41:10 +00:00
type Error struct {
2021-03-01 06:49:20 +00:00
Type string `json:"type"`
2021-03-05 07:10:46 +00:00
Detail string `json:"detail"`
2021-03-01 06:49:20 +00:00
Subproblems []interface{} `json:"subproblems,omitempty"`
Identifier interface{} `json:"identifier,omitempty"`
Err error `json:"-"`
Status int `json:"-"`
}
2021-03-03 23:16:25 +00:00
// NewError creates a new Error type.
2021-03-01 06:49:20 +00:00
func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
2021-03-11 07:05:46 +00:00
return newError(pt, errors.Errorf(msg, args...))
}
func newError(pt ProblemType, err error) *Error {
2021-03-01 07:33:18 +00:00
meta, ok := errorMap[pt]
2021-03-01 06:49:20 +00:00
if !ok {
meta = errorServerInternalMetadata
return &Error{
2021-03-05 07:10:46 +00:00
Type: meta.typ,
Detail: meta.details,
Status: meta.status,
2021-03-11 07:05:46 +00:00
Err: err,
2021-03-01 06:49:20 +00:00
}
}
return &Error{
2021-03-05 07:10:46 +00:00
Type: meta.typ,
Detail: meta.details,
Status: meta.status,
2021-03-11 07:05:46 +00:00
Err: err,
2021-03-01 06:49:20 +00:00
}
2019-05-27 00:41:10 +00:00
}
2021-03-03 23:16:25 +00:00
// NewErrorISE creates a new ErrorServerInternalType Error.
func NewErrorISE(msg string, args ...interface{}) *Error {
return NewError(ErrorServerInternalType, msg, args...)
}
2021-03-05 07:10:46 +00:00
// WrapError attempts to wrap the internal error.
func WrapError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
2019-05-27 00:41:10 +00:00
switch e := err.(type) {
case nil:
return nil
case *Error:
if e.Err == nil {
2021-03-05 07:10:46 +00:00
e.Err = errors.Errorf(msg+"; "+e.Detail, args...)
2019-05-27 00:41:10 +00:00
} else {
2021-03-01 06:49:20 +00:00
e.Err = errors.Wrapf(e.Err, msg, args...)
2019-05-27 00:41:10 +00:00
}
return e
default:
2021-03-11 07:05:46 +00:00
return newError(typ, errors.Wrapf(err, msg, args...))
2019-05-27 00:41:10 +00:00
}
}
2021-03-05 07:10:46 +00:00
// WrapErrorISE shortcut to wrap an internal server error type.
func WrapErrorISE(err error, msg string, args ...interface{}) *Error {
return WrapError(ErrorServerInternalType, err, msg, args...)
2021-03-01 07:33:18 +00:00
}
2021-03-01 06:49:20 +00:00
// StatusCode returns the status code and implements the StatusCoder interface.
func (e *Error) StatusCode() int {
return e.Status
}
// Error allows AError to implement the error interface.
2019-05-27 00:41:10 +00:00
func (e *Error) Error() string {
2021-03-05 07:10:46 +00:00
return e.Detail
2019-05-27 00:41:10 +00:00
}
// Cause returns the internal error and implements the Causer interface.
func (e *Error) Cause() error {
if e.Err == nil {
2021-03-05 07:10:46 +00:00
return errors.New(e.Detail)
2019-05-27 00:41:10 +00:00
}
return e.Err
}
// ToLog implements the EnableLogger interface.
func (e *Error) ToLog() (interface{}, error) {
b, err := json.Marshal(e)
if err != nil {
return nil, WrapErrorISE(err, "error marshaling acme.Error for logging")
}
return string(b), nil
}
2021-03-30 06:16:39 +00:00
// WriteError writes to w a JSON representation of the given error.
func WriteError(w http.ResponseWriter, err *Error) {
w.Header().Set("Content-Type", "application/problem+json")
w.WriteHeader(err.StatusCode())
// Write errors in the response writer
if rl, ok := w.(logging.ResponseLogger); ok {
rl.WithFields(map[string]interface{}{
"error": err.Err,
})
if os.Getenv("STEPDEBUG") == "1" {
if e, ok := err.Err.(errs.StackTracer); ok {
rl.WithFields(map[string]interface{}{
"stack-trace": fmt.Sprintf("%+v", e),
})
}
}
}
if err := json.NewEncoder(w).Encode(err); err != nil {
log.Println(err)
}
}