123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- package errors
- import (
- "crypto/x509"
- "encoding/json"
- "fmt"
- )
- type Error struct {
- ErrorCode int `json:"code"`
- Message string `json:"message"`
- }
- type Category int
- type Reason int
- const (
-
- Success Category = 1000 * iota
-
- CertificateError
-
- PrivateKeyError
-
- IntermediatesError
-
- RootError
-
-
- PolicyError
-
- DialError
-
- APIClientError
-
- OCSPError
-
- CSRError
-
- CTError
-
- CertStoreError
- )
- const (
- None Reason = iota
- )
- const (
- BundleExpiringBit int = 1 << iota
- BundleNotUbiquitousBit
- )
- const (
- Unknown Reason = iota
- ReadFailed
- DecodeFailed
- ParseFailed
- )
- const (
-
-
- SelfSigned Reason = 100 * (iota + 1)
-
-
-
- VerifyFailed
-
- BadRequest
-
-
-
- MissingSerial
- )
- const (
- certificateInvalid = 10 * (iota + 1)
- unknownAuthority
- )
- const (
-
-
-
- Encrypted Reason = 100 * (iota + 1)
-
-
-
- NotRSAOrECC
-
-
- KeyMismatch
-
-
- GenerationFailed
-
-
-
- Unavailable
- )
- const (
-
-
- NoKeyUsages Reason = 100 * (iota + 1)
-
-
- InvalidPolicy
-
-
- InvalidRequest
-
- UnknownProfile
- UnmatchedWhitelist
- )
- const (
-
-
- AuthenticationFailure Reason = 100 * (iota + 1)
-
- JSONError
-
- IOError
-
- ClientHTTPError
-
-
- ServerRequestFailed
- )
- const (
-
-
- IssuerMismatch Reason = 100 * (iota + 1)
-
-
- InvalidStatus
- )
- const (
-
-
- PrecertSubmissionFailed = 100 * (iota + 1)
-
-
- CTClientConstructionFailed
-
-
- PrecertMissingPoison
-
-
-
- PrecertInvalidPoison
- )
- const (
-
- InsertionFailed = 100 * (iota + 1)
-
-
- RecordNotFound
- )
- func (e *Error) Error() string {
- marshaled, err := json.Marshal(e)
- if err != nil {
- panic(err)
- }
- return string(marshaled)
- }
- func New(category Category, reason Reason) *Error {
- errorCode := int(category) + int(reason)
- var msg string
- switch category {
- case OCSPError:
- switch reason {
- case ReadFailed:
- msg = "No certificate provided"
- case IssuerMismatch:
- msg = "Certificate not issued by this issuer"
- case InvalidStatus:
- msg = "Invalid revocation status"
- }
- case CertificateError:
- switch reason {
- case Unknown:
- msg = "Unknown certificate error"
- case ReadFailed:
- msg = "Failed to read certificate"
- case DecodeFailed:
- msg = "Failed to decode certificate"
- case ParseFailed:
- msg = "Failed to parse certificate"
- case SelfSigned:
- msg = "Certificate is self signed"
- case VerifyFailed:
- msg = "Unable to verify certificate"
- case BadRequest:
- msg = "Invalid certificate request"
- case MissingSerial:
- msg = "Missing serial number in request"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category CertificateError.",
- reason))
- }
- case PrivateKeyError:
- switch reason {
- case Unknown:
- msg = "Unknown private key error"
- case ReadFailed:
- msg = "Failed to read private key"
- case DecodeFailed:
- msg = "Failed to decode private key"
- case ParseFailed:
- msg = "Failed to parse private key"
- case Encrypted:
- msg = "Private key is encrypted."
- case NotRSAOrECC:
- msg = "Private key algorithm is not RSA or ECC"
- case KeyMismatch:
- msg = "Private key does not match public key"
- case GenerationFailed:
- msg = "Failed to new private key"
- case Unavailable:
- msg = "Private key is unavailable"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category PrivateKeyError.",
- reason))
- }
- case IntermediatesError:
- switch reason {
- case Unknown:
- msg = "Unknown intermediate certificate error"
- case ReadFailed:
- msg = "Failed to read intermediate certificate"
- case DecodeFailed:
- msg = "Failed to decode intermediate certificate"
- case ParseFailed:
- msg = "Failed to parse intermediate certificate"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category IntermediatesError.",
- reason))
- }
- case RootError:
- switch reason {
- case Unknown:
- msg = "Unknown root certificate error"
- case ReadFailed:
- msg = "Failed to read root certificate"
- case DecodeFailed:
- msg = "Failed to decode root certificate"
- case ParseFailed:
- msg = "Failed to parse root certificate"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category RootError.",
- reason))
- }
- case PolicyError:
- switch reason {
- case Unknown:
- msg = "Unknown policy error"
- case NoKeyUsages:
- msg = "Invalid policy: no key usage available"
- case InvalidPolicy:
- msg = "Invalid or unknown policy"
- case InvalidRequest:
- msg = "Policy violation request"
- case UnknownProfile:
- msg = "Unknown policy profile"
- case UnmatchedWhitelist:
- msg = "Request does not match policy whitelist"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category PolicyError.",
- reason))
- }
- case DialError:
- switch reason {
- case Unknown:
- msg = "Failed to dial remote server"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category DialError.",
- reason))
- }
- case APIClientError:
- switch reason {
- case AuthenticationFailure:
- msg = "API client authentication failure"
- case JSONError:
- msg = "API client JSON config error"
- case ClientHTTPError:
- msg = "API client HTTP error"
- case IOError:
- msg = "API client IO error"
- case ServerRequestFailed:
- msg = "API client error: Server request failed"
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error reason %d under category APIClientError.",
- reason))
- }
- case CSRError:
- switch reason {
- case Unknown:
- msg = "CSR parsing failed due to unknown error"
- case ReadFailed:
- msg = "CSR file read failed"
- case ParseFailed:
- msg = "CSR Parsing failed"
- case DecodeFailed:
- msg = "CSR Decode failed"
- case BadRequest:
- msg = "CSR Bad request"
- default:
- panic(fmt.Sprintf("Unsupported CF-SSL error reason %d under category APIClientError.", reason))
- }
- case CTError:
- switch reason {
- case Unknown:
- msg = "Certificate transparency parsing failed due to unknown error"
- case PrecertSubmissionFailed:
- msg = "Certificate transparency precertificate submission failed"
- case PrecertMissingPoison:
- msg = "Precertificate is missing CT poison extension"
- case PrecertInvalidPoison:
- msg = "Precertificate contains an invalid CT poison extension"
- default:
- panic(fmt.Sprintf("Unsupported CF-SSL error reason %d under category CTError.", reason))
- }
- case CertStoreError:
- switch reason {
- case Unknown:
- msg = "Certificate store action failed due to unknown error"
- default:
- panic(fmt.Sprintf("Unsupported CF-SSL error reason %d under category CertStoreError.", reason))
- }
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error type: %d.",
- category))
- }
- return &Error{ErrorCode: errorCode, Message: msg}
- }
- func Wrap(category Category, reason Reason, err error) *Error {
- errorCode := int(category) + int(reason)
- if err == nil {
- panic("Wrap needs a supplied error to initialize.")
- }
-
- switch err.(type) {
- case *Error:
- panic("Unable to wrap a wrapped error.")
- }
- switch category {
- case CertificateError:
-
-
- if reason == VerifyFailed {
- switch errorType := err.(type) {
- case x509.CertificateInvalidError:
- errorCode += certificateInvalid + int(errorType.Reason)
- case x509.UnknownAuthorityError:
- errorCode += unknownAuthority
- }
- }
- case PrivateKeyError, IntermediatesError, RootError, PolicyError, DialError,
- APIClientError, CSRError, CTError, CertStoreError, OCSPError:
-
- default:
- panic(fmt.Sprintf("Unsupported CFSSL error type: %d.",
- category))
- }
- return &Error{ErrorCode: errorCode, Message: err.Error()}
- }
|