NegotiationToken.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. type NegTokenTarg NegTokenResp
  56. // Unmarshal and return 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 (n *NegotiationToken) Unmarshal(b []byte) (bool, interface{}, error) {
  61. _, err := asn1.Unmarshal(b, n)
  62. if err != nil {
  63. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken: %v", err)
  64. }
  65. switch n.Choice.Tag {
  66. case 0:
  67. var negToken NegTokenInit
  68. _, err = asn1.Unmarshal(b, &negToken)
  69. if err != nil {
  70. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", n.Choice.Tag, err)
  71. }
  72. return true, negToken, nil
  73. case 1:
  74. var negToken NegTokenResp
  75. _, err = asn1.Unmarshal(b, &negToken)
  76. if err != nil {
  77. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", n.Choice.Tag, err)
  78. }
  79. return false, negToken, nil
  80. default:
  81. return false, nil, errors.New("Unknown choice type for NegotiationToken")
  82. }
  83. }
  84. func (n *NegTokenInit) Marshal() ([]byte, error) {
  85. b, err := asn1.Marshal(*n)
  86. if err != nil {
  87. return nil, err
  88. }
  89. //nt := NegotiationToken{
  90. // Choice: asn1.RawValue{
  91. // Tag: 0,
  92. // Class: 2,
  93. // IsCompound: true,
  94. // Bytes: b,
  95. // },
  96. //}
  97. //nb, err := asn1.Marshal(nt)
  98. //if err != nil {
  99. // return nil, err
  100. //}
  101. return b, nil
  102. }
  103. // Returns marshalled bytes of a NegotiationToken rather than the NegTokenResp
  104. func (n *NegTokenResp) Marshal() ([]byte, error) {
  105. b, err := asn1.Marshal(*n)
  106. if err != nil {
  107. return nil, err
  108. }
  109. nt := NegotiationToken{
  110. Choice: asn1.RawValue{
  111. Tag: 1,
  112. Class: 2,
  113. IsCompound: true,
  114. Bytes: b,
  115. },
  116. }
  117. nb, err := asn1.Marshal(nt)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return nb, nil
  122. }