KRBError.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Kerberos 5 message types and methods.
  2. package messages
  3. import (
  4. "fmt"
  5. "github.com/jcmturner/asn1"
  6. "github.com/jcmturner/gokrb5/iana/asnAppTag"
  7. "github.com/jcmturner/gokrb5/iana/errorcode"
  8. "github.com/jcmturner/gokrb5/iana/msgtype"
  9. "github.com/jcmturner/gokrb5/types"
  10. "time"
  11. )
  12. // RFC 4120 KRB_ERROR: https://tools.ietf.org/html/rfc4120#section-5.9.1.
  13. type KRBError struct {
  14. PVNO int `asn1:"explicit,tag:0"`
  15. MsgType int `asn1:"explicit,tag:1"`
  16. CTime time.Time `asn1:"generalized,optional,explicit,tag:2"`
  17. Cusec int `asn1:"optional,explicit,tag:3"`
  18. STime time.Time `asn1:"generalized,explicit,tag:4"`
  19. Susec int `asn1:"explicit,tag:5"`
  20. ErrorCode int `asn1:"explicit,tag:6"`
  21. CRealm string `asn1:"generalstring,optional,explicit,tag:7"`
  22. CName types.PrincipalName `asn1:"optional,explicit,tag:8"`
  23. Realm string `asn1:"generalstring,explicit,tag:9"`
  24. SName types.PrincipalName `asn1:"explicit,tag:10"`
  25. EText string `asn1:"generalstring,optional,explicit,tag:11"`
  26. EData []byte `asn1:"optional,explicit,tag:12"`
  27. }
  28. // Unmarshal bytes b into the KRBError struct.
  29. func (k *KRBError) Unmarshal(b []byte) error {
  30. _, err := asn1.UnmarshalWithParams(b, k, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.KRBError))
  31. if err != nil {
  32. return err
  33. }
  34. expectedMsgType := msgtype.KRB_ERROR
  35. if k.MsgType != expectedMsgType {
  36. return fmt.Errorf("Message ID does not indicate a KRB_ERROR. Expected: %v; Actual: %v", expectedMsgType, k.MsgType)
  37. }
  38. return nil
  39. }
  40. // Error method implementing error interface on KRBError struct.
  41. func (k KRBError) Error() string {
  42. return fmt.Sprintf("KRB Error: %s - %s", errorcode.ErrorCodeLookup(k.ErrorCode), k.EText)
  43. }
  44. func processReplyError(b []byte, err error) error {
  45. switch err.(type) {
  46. case asn1.StructuralError:
  47. var krberr KRBError
  48. tmperr := krberr.Unmarshal(b)
  49. if tmperr != nil {
  50. return err
  51. }
  52. return krberr
  53. default:
  54. return err
  55. }
  56. }