NegotiationToken.go 3.5 KB

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