NegotiationToken.go 3.6 KB

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