NegotiationToken.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 asn1.RawValue
  41. type NegTokenInit struct {
  42. MechTypes MechTypeList `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. type NegTokenResp struct {
  48. NegState asn1.Enumerated `asn1:"explicit,optional,tag:0"`
  49. SupportedMech MechType `asn1:"explicit,optional,tag:1"`
  50. ResponseToken []byte `asn1:"explicit,optional,tag:2"`
  51. MechListMIC []byte `asn1:"explicit,optional,tag:3"`
  52. }
  53. // Unmarshal and return either a NegTokenInit or a NegTokenResp.
  54. //
  55. // The boolean indicates if the reponse is a NegTokenInit.
  56. // If error is nil and the boolean is false the response is a NegTokenResp.
  57. func (n *NegotiationToken) Unmarshal(b []byte) (bool, interface{}, error) {
  58. _, err := asn1.Unmarshal(b, n)
  59. if err != nil {
  60. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken: %v", err)
  61. }
  62. var negToken interface{}
  63. var isInit bool
  64. switch n.Tag {
  65. case 0:
  66. negToken = &NegTokenInit{}
  67. isInit = true
  68. case 1:
  69. negToken = &NegTokenResp{}
  70. default:
  71. return false, nil, errors.New("Unknown choice type for NegotiationToken")
  72. }
  73. _, err = asn1.Unmarshal(n.Bytes, negToken)
  74. if err != nil {
  75. return nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", n.Tag, err)
  76. }
  77. return isInit, negToken, nil
  78. }
  79. // Returns marshalled bytes of a NegotiationToken rather than the NegTokenInit
  80. func (n *NegTokenInit) Marshal() ([]byte, error) {
  81. b, err := asn1.Marshal(*n)
  82. if err != nil {
  83. return nil, err
  84. }
  85. nt := NegotiationToken{
  86. Tag: 0,
  87. Class: 2,
  88. IsCompound: true,
  89. Bytes: b,
  90. }
  91. nb, err := asn1.Marshal(nt)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return nb, nil
  96. }
  97. // Returns marshalled bytes of a NegotiationToken rather than the NegTokenResp
  98. func (n *NegTokenResp) Marshal() ([]byte, error) {
  99. b, err := asn1.Marshal(*n)
  100. if err != nil {
  101. return nil, err
  102. }
  103. nt := NegotiationToken{
  104. Tag: 1,
  105. Class: 2,
  106. IsCompound: true,
  107. Bytes: b,
  108. }
  109. nb, err := asn1.Marshal(nt)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return nb, nil
  114. }