NegotiationToken.go 3.8 KB

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