krb5Token.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package GSSAPI
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/jcmturner/asn1"
  7. "github.com/jcmturner/gokrb5/asn1tools"
  8. "github.com/jcmturner/gokrb5/config"
  9. "github.com/jcmturner/gokrb5/iana/chksumtype"
  10. "github.com/jcmturner/gokrb5/messages"
  11. "github.com/jcmturner/gokrb5/types"
  12. "os"
  13. )
  14. const (
  15. TOK_ID_KRB_AP_REQ = "0100"
  16. TOK_ID_KRB_AP_REP = "0200"
  17. TOK_ID_KRB_ERROR = "0300"
  18. GSS_C_DELEG_FLAG = 1
  19. GSS_C_MUTUAL_FLAG = 2
  20. GSS_C_REPLAY_FLAG = 4
  21. GSS_C_SEQUENCE_FLAG = 8
  22. GSS_C_CONF_FLAG = 16
  23. GSS_C_INTEG_FLAG = 32
  24. )
  25. func NewKRB5APREQMechToken(APReq messages.APReq) ([]byte, error) {
  26. tb, _ := hex.DecodeString(TOK_ID_KRB_AP_REQ)
  27. b, _ := asn1.Marshal(MechTypeOID_Krb5)
  28. b = append(b, tb...)
  29. tb, err := APReq.Marshal()
  30. if err != nil {
  31. return []byte{}, fmt.Errorf("Could not marshal AP_REQ: %v", err)
  32. }
  33. b = append(b, tb...)
  34. fmt.Fprintf(os.Stderr, "len %v\n", len(b))
  35. return asn1tools.AddASNAppTag(b, 0), nil
  36. }
  37. func newAuthenticator(c config.Config, username string) {
  38. //RFC 4121 Section 4.1.1
  39. auth := types.NewAuthenticator(c.LibDefaults.Default_realm, username)
  40. auth.Cksum = types.Checksum{
  41. CksumType: chksumtype.GSSAPI,
  42. Checksum: newAuthenticatorChksum([]int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}),
  43. }
  44. }
  45. func newAuthenticatorChksum(flags []int) []byte {
  46. a := make([]byte, 24)
  47. for i := range flags {
  48. if i == GSS_C_DELEG_FLAG {
  49. x := make([]byte, 28-len(a))
  50. a = append(a, x...)
  51. }
  52. setAuthenticatorChksumFlag(a, uint32(i))
  53. }
  54. return a
  55. }
  56. func setAuthenticatorChksumFlag(a []byte, i uint32) {
  57. f := binary.LittleEndian.Uint32(a[20:24])
  58. f |= i
  59. binary.LittleEndian.PutUint32(a[20:24], f)
  60. }
  61. /*
  62. The authenticator checksum field SHALL have the following format:
  63. Octet Name Description
  64. -----------------------------------------------------------------
  65. 0..3 Lgth Number of octets in Bnd field; Represented
  66. in little-endian order; Currently contains
  67. hex value 10 00 00 00 (16).
  68. 4..19 Bnd Channel binding information, as described in
  69. section 4.1.1.2.
  70. 20..23 Flags Four-octet context-establishment flags in
  71. little-endian order as described in section
  72. 4.1.1.1.
  73. 24..25 DlgOpt The delegation option identifier (=1) in
  74. little-endian order [optional]. This field
  75. and the next two fields are present if and
  76. only if GSS_C_DELEG_FLAG is set as described
  77. in section 4.1.1.1.
  78. 26..27 Dlgth The length of the Deleg field in little-endian order [optional].
  79. 28..(n-1) Deleg A KRB_CRED message (n = Dlgth + 28) [optional].
  80. n..last Exts Extensions [optional].
  81. */