NegotiationToken.go 3.9 KB

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