| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- package acme
- import (
- "github.com/pkg/errors"
- )
- // AccountDoesNotExistErr returns a new acme error.
- func AccountDoesNotExistErr(err error) *Error {
- return &Error{
- Type: accountDoesNotExistErr,
- Detail: "Account does not exist",
- Status: 404,
- Err: err,
- }
- }
- // AlreadyRevokedErr returns a new acme error.
- func AlreadyRevokedErr(err error) *Error {
- return &Error{
- Type: alreadyRevokedErr,
- Detail: "Certificate already revoked",
- Status: 400,
- Err: err,
- }
- }
- // BadCSRErr returns a new acme error.
- func BadCSRErr(err error) *Error {
- return &Error{
- Type: badCSRErr,
- Detail: "The CSR is unacceptable",
- Status: 400,
- Err: err,
- }
- }
- // BadNonceErr returns a new acme error.
- func BadNonceErr(err error) *Error {
- return &Error{
- Type: badNonceErr,
- Detail: "Unacceptable anti-replay nonce",
- Status: 400,
- Err: err,
- }
- }
- // BadPublicKeyErr returns a new acme error.
- func BadPublicKeyErr(err error) *Error {
- return &Error{
- Type: badPublicKeyErr,
- Detail: "The jws was signed by a public key the server does not support",
- Status: 400,
- Err: err,
- }
- }
- // BadRevocationReasonErr returns a new acme error.
- func BadRevocationReasonErr(err error) *Error {
- return &Error{
- Type: badRevocationReasonErr,
- Detail: "The revocation reason provided is not allowed by the server",
- Status: 400,
- Err: err,
- }
- }
- // BadSignatureAlgorithmErr returns a new acme error.
- func BadSignatureAlgorithmErr(err error) *Error {
- return &Error{
- Type: badSignatureAlgorithmErr,
- Detail: "The JWS was signed with an algorithm the server does not support",
- Status: 400,
- Err: err,
- }
- }
- // CaaErr returns a new acme error.
- func CaaErr(err error) *Error {
- return &Error{
- Type: caaErr,
- Detail: "Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate",
- Status: 400,
- Err: err,
- }
- }
- // CompoundErr returns a new acme error.
- func CompoundErr(err error) *Error {
- return &Error{
- Type: compoundErr,
- Detail: "Specific error conditions are indicated in the “subproblems” array",
- Status: 400,
- Err: err,
- }
- }
- // ConnectionErr returns a new acme error.
- func ConnectionErr(err error) *Error {
- return &Error{
- Type: connectionErr,
- Detail: "The server could not connect to validation target",
- Status: 400,
- Err: err,
- }
- }
- // DNSErr returns a new acme error.
- func DNSErr(err error) *Error {
- return &Error{
- Type: dnsErr,
- Detail: "There was a problem with a DNS query during identifier validation",
- Status: 400,
- Err: err,
- }
- }
- // ExternalAccountRequiredErr returns a new acme error.
- func ExternalAccountRequiredErr(err error) *Error {
- return &Error{
- Type: externalAccountRequiredErr,
- Detail: "The request must include a value for the \"externalAccountBinding\" field",
- Status: 400,
- Err: err,
- }
- }
- // IncorrectResponseErr returns a new acme error.
- func IncorrectResponseErr(err error) *Error {
- return &Error{
- Type: incorrectResponseErr,
- Detail: "Response received didn't match the challenge's requirements",
- Status: 400,
- Err: err,
- }
- }
- // InvalidContactErr returns a new acme error.
- func InvalidContactErr(err error) *Error {
- return &Error{
- Type: invalidContactErr,
- Detail: "A contact URL for an account was invalid",
- Status: 400,
- Err: err,
- }
- }
- // MalformedErr returns a new acme error.
- func MalformedErr(err error) *Error {
- return &Error{
- Type: malformedErr,
- Detail: "The request message was malformed",
- Status: 400,
- Err: err,
- }
- }
- // OrderNotReadyErr returns a new acme error.
- func OrderNotReadyErr(err error) *Error {
- return &Error{
- Type: orderNotReadyErr,
- Detail: "The request attempted to finalize an order that is not ready to be finalized",
- Status: 400,
- Err: err,
- }
- }
- // RateLimitedErr returns a new acme error.
- func RateLimitedErr(err error) *Error {
- return &Error{
- Type: rateLimitedErr,
- Detail: "The request exceeds a rate limit",
- Status: 400,
- Err: err,
- }
- }
- // RejectedIdentifierErr returns a new acme error.
- func RejectedIdentifierErr(err error) *Error {
- return &Error{
- Type: rejectedIdentifierErr,
- Detail: "The server will not issue certificates for the identifier",
- Status: 400,
- Err: err,
- }
- }
- // ServerInternalErr returns a new acme error.
- func ServerInternalErr(err error) *Error {
- return &Error{
- Type: serverInternalErr,
- Detail: "The server experienced an internal error",
- Status: 500,
- Err: err,
- }
- }
- // TLSErr returns a new acme error.
- func TLSErr(err error) *Error {
- return &Error{
- Type: tlsErr,
- Detail: "The server received a TLS error during validation",
- Status: 400,
- Err: err,
- }
- }
- // UnauthorizedErr returns a new acme error.
- func UnauthorizedErr(err error) *Error {
- return &Error{
- Type: unauthorizedErr,
- Detail: "The client lacks sufficient authorization",
- Status: 401,
- Err: err,
- }
- }
- // UnsupportedContactErr returns a new acme error.
- func UnsupportedContactErr(err error) *Error {
- return &Error{
- Type: unsupportedContactErr,
- Detail: "A contact URL for an account used an unsupported protocol scheme",
- Status: 400,
- Err: err,
- }
- }
- // UnsupportedIdentifierErr returns a new acme error.
- func UnsupportedIdentifierErr(err error) *Error {
- return &Error{
- Type: unsupportedIdentifierErr,
- Detail: "An identifier is of an unsupported type",
- Status: 400,
- Err: err,
- }
- }
- // UserActionRequiredErr returns a new acme error.
- func UserActionRequiredErr(err error) *Error {
- return &Error{
- Type: userActionRequiredErr,
- Detail: "Visit the “instance” URL and take actions specified there",
- Status: 400,
- Err: err,
- }
- }
- // ProbType is the type of the ACME problem.
- type ProbType int
- const (
- // The request specified an account that does not exist
- accountDoesNotExistErr ProbType = iota
- // The request specified a certificate to be revoked that has already been revoked
- alreadyRevokedErr
- // The CSR is unacceptable (e.g., due to a short key)
- badCSRErr
- // The client sent an unacceptable anti-replay nonce
- badNonceErr
- // The JWS was signed by a public key the server does not support
- badPublicKeyErr
- // The revocation reason provided is not allowed by the server
- badRevocationReasonErr
- // The JWS was signed with an algorithm the server does not support
- badSignatureAlgorithmErr
- // Certification Authority Authorization (CAA) records forbid the CA from issuing a certificate
- caaErr
- // Specific error conditions are indicated in the “subproblems” array.
- compoundErr
- // The server could not connect to validation target
- connectionErr
- // There was a problem with a DNS query during identifier validation
- dnsErr
- // The request must include a value for the “externalAccountBinding” field
- externalAccountRequiredErr
- // Response received didn’t match the challenge’s requirements
- incorrectResponseErr
- // A contact URL for an account was invalid
- invalidContactErr
- // The request message was malformed
- malformedErr
- // The request attempted to finalize an order that is not ready to be finalized
- orderNotReadyErr
- // The request exceeds a rate limit
- rateLimitedErr
- // The server will not issue certificates for the identifier
- rejectedIdentifierErr
- // The server experienced an internal error
- serverInternalErr
- // The server received a TLS error during validation
- tlsErr
- // The client lacks sufficient authorization
- unauthorizedErr
- // A contact URL for an account used an unsupported protocol scheme
- unsupportedContactErr
- // An identifier is of an unsupported type
- unsupportedIdentifierErr
- // Visit the “instance” URL and take actions specified there
- userActionRequiredErr
- )
- // String returns the string representation of the acme problem type,
- // fulfilling the Stringer interface.
- func (ap ProbType) String() string {
- switch ap {
- case accountDoesNotExistErr:
- return "accountDoesNotExist"
- case alreadyRevokedErr:
- return "alreadyRevoked"
- case badCSRErr:
- return "badCSR"
- case badNonceErr:
- return "badNonce"
- case badPublicKeyErr:
- return "badPublicKey"
- case badRevocationReasonErr:
- return "badRevocationReason"
- case badSignatureAlgorithmErr:
- return "badSignatureAlgorithm"
- case caaErr:
- return "caa"
- case compoundErr:
- return "compound"
- case connectionErr:
- return "connection"
- case dnsErr:
- return "dns"
- case externalAccountRequiredErr:
- return "externalAccountRequired"
- case incorrectResponseErr:
- return "incorrectResponse"
- case invalidContactErr:
- return "invalidContact"
- case malformedErr:
- return "malformed"
- case orderNotReadyErr:
- return "orderNotReady"
- case rateLimitedErr:
- return "rateLimited"
- case rejectedIdentifierErr:
- return "rejectedIdentifier"
- case serverInternalErr:
- return "serverInternal"
- case tlsErr:
- return "tls"
- case unauthorizedErr:
- return "unauthorized"
- case unsupportedContactErr:
- return "unsupportedContact"
- case unsupportedIdentifierErr:
- return "unsupportedIdentifier"
- case userActionRequiredErr:
- return "userActionRequired"
- default:
- return "unsupported type"
- }
- }
- // Error is an ACME error type complete with problem document.
- type Error struct {
- Type ProbType
- Detail string
- Err error
- Status int
- Sub []*Error
- Identifier *Identifier
- }
- // Wrap attempts to wrap the internal error.
- func Wrap(err error, wrap string) *Error {
- switch e := err.(type) {
- case nil:
- return nil
- case *Error:
- if e.Err == nil {
- e.Err = errors.New(wrap + "; " + e.Detail)
- } else {
- e.Err = errors.Wrap(e.Err, wrap)
- }
- return e
- default:
- return ServerInternalErr(errors.Wrap(err, wrap))
- }
- }
- // Error implements the error interface.
- func (e *Error) Error() string {
- if e.Err == nil {
- return e.Detail
- }
- return e.Err.Error()
- }
- // Cause returns the internal error and implements the Causer interface.
- func (e *Error) Cause() error {
- if e.Err == nil {
- return errors.New(e.Detail)
- }
- return e.Err
- }
- // ToACME returns an acme representation of the problem type.
- func (e *Error) ToACME() *AError {
- ae := &AError{
- Type: "urn:ietf:params:acme:error:" + e.Type.String(),
- Detail: e.Error(),
- Status: e.Status,
- }
- if e.Identifier != nil {
- ae.Identifier = *e.Identifier
- }
- for _, p := range e.Sub {
- ae.Subproblems = append(ae.Subproblems, p.ToACME())
- }
- return ae
- }
- // StatusCode returns the status code and implements the StatusCode interface.
- func (e *Error) StatusCode() int {
- return e.Status
- }
- // AError is the error type as seen in acme request/responses.
- type AError struct {
- Type string `json:"type"`
- Detail string `json:"detail"`
- Identifier interface{} `json:"identifier,omitempty"`
- Subproblems []interface{} `json:"subproblems,omitempty"`
- Status int `json:"-"`
- }
- // Error allows AError to implement the error interface.
- func (ae *AError) Error() string {
- return ae.Detail
- }
- // StatusCode returns the status code and implements the StatusCode interface.
- func (ae *AError) StatusCode() int {
- return ae.Status
- }
|