NegotiationToken.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package gssapi
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/jcmturner/asn1"
  6. "gopkg.in/jcmturner/gokrb5.v2/credentials"
  7. "gopkg.in/jcmturner/gokrb5.v2/messages"
  8. "gopkg.in/jcmturner/gokrb5.v2/types"
  9. )
  10. /*
  11. https://msdn.microsoft.com/en-us/library/ms995330.aspx
  12. NegotiationToken ::= CHOICE {
  13. negTokenInit [0] NegTokenInit, This is the Negotiation token sent from the client to the server.
  14. negTokenResp [1] NegTokenResp
  15. }
  16. NegTokenInit ::= SEQUENCE {
  17. mechTypes [0] MechTypeList,
  18. reqFlags [1] ContextFlags OPTIONAL,
  19. -- inherited from RFC 2478 for backward compatibility,
  20. -- RECOMMENDED to be left out
  21. mechToken [2] OCTET STRING OPTIONAL,
  22. mechListMIC [3] OCTET STRING OPTIONAL,
  23. ...
  24. }
  25. NegTokenResp ::= SEQUENCE {
  26. negState [0] ENUMERATED {
  27. accept-completed (0),
  28. accept-incomplete (1),
  29. reject (2),
  30. request-mic (3)
  31. } OPTIONAL,
  32. -- REQUIRED in the first reply from the target
  33. supportedMech [1] MechType OPTIONAL,
  34. -- present only in the first reply from the target
  35. responseToken [2] OCTET STRING OPTIONAL,
  36. mechListMIC [3] OCTET STRING OPTIONAL,
  37. ...
  38. }
  39. */
  40. // NegTokenInit implements Negotiation Token of type Init
  41. type NegTokenInit struct {
  42. MechTypes []asn1.ObjectIdentifier `asn1:"explicit,tag:0"`
  43. ReqFlags ContextFlags `asn1:"explicit,optional,tag:1"`
  44. MechToken []byte `asn1:"explicit,optional,tag:2"`
  45. MechTokenMIC []byte `asn1:"explicit,optional,tag:3"`
  46. }
  47. // NegTokenResp implements Negotiation Token of type Resp/Targ
  48. type NegTokenResp struct {
  49. NegState asn1.Enumerated `asn1:"explicit,tag:0"`
  50. SupportedMech asn1.ObjectIdentifier `asn1:"explicit,optional,tag:1"`
  51. ResponseToken []byte `asn1:"explicit,optional,tag:2"`
  52. MechListMIC []byte `asn1:"explicit,optional,tag:3"`
  53. }
  54. // NegTokenTarg implements Negotiation Token of type Resp/Targ
  55. type NegTokenTarg NegTokenResp
  56. // UnmarshalNegToken umarshals and returns either a NegTokenInit or a NegTokenResp.
  57. //
  58. // The boolean indicates if the response is a NegTokenInit.
  59. // If error is nil and the boolean is false the response is a NegTokenResp.
  60. func UnmarshalNegToken(b []byte) (bool, interface{}, error) {
  61. var a asn1.RawValue
  62. _, err := asn1.Unmarshal(b, &a)
  63. if err != nil {
  64. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken: %v", err)
  65. }
  66. switch a.Tag {
  67. case 0:
  68. var negToken NegTokenInit
  69. _, err = asn1.Unmarshal(a.Bytes, &negToken)
  70. if err != nil {
  71. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d (Init): %v", a.Tag, err)
  72. }
  73. return true, negToken, nil
  74. case 1:
  75. var negToken NegTokenResp
  76. _, err = asn1.Unmarshal(a.Bytes, &negToken)
  77. if err != nil {
  78. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d (Resp/Targ): %v", a.Tag, err)
  79. }
  80. return false, negToken, nil
  81. default:
  82. return false, nil, errors.New("Unknown choice type for NegotiationToken")
  83. }
  84. }
  85. // Marshal an Init negotiation token
  86. func (n *NegTokenInit) Marshal() ([]byte, error) {
  87. b, err := asn1.Marshal(*n)
  88. if err != nil {
  89. return nil, err
  90. }
  91. nt := asn1.RawValue{
  92. Tag: 0,
  93. Class: 2,
  94. IsCompound: true,
  95. Bytes: b,
  96. }
  97. nb, err := asn1.Marshal(nt)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return nb, nil
  102. }
  103. // Marshal a Resp/Targ negotiation token
  104. func (n *NegTokenResp) Marshal() ([]byte, error) {
  105. b, err := asn1.Marshal(*n)
  106. if err != nil {
  107. return nil, err
  108. }
  109. nt := asn1.RawValue{
  110. Tag: 1,
  111. Class: 2,
  112. IsCompound: true,
  113. Bytes: b,
  114. }
  115. nb, err := asn1.Marshal(nt)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return nb, nil
  120. }
  121. // NewNegTokenInitKrb5 creates new Init negotiation token for Kerberos 5
  122. func NewNegTokenInitKrb5(creds credentials.Credentials, tkt messages.Ticket, sessionKey types.EncryptionKey) (NegTokenInit, error) {
  123. mt, err := NewKRB5APREQMechToken(creds, tkt, sessionKey)
  124. if err != nil {
  125. return NegTokenInit{}, fmt.Errorf("Error getting MechToken; %v", err)
  126. }
  127. return NegTokenInit{
  128. MechTypes: []asn1.ObjectIdentifier{MechTypeOIDKRB5},
  129. MechToken: mt,
  130. }, nil
  131. }