NegotiationToken.go 3.4 KB

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