NegotiationToken.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Negotiation Token - Init
  40. type NegTokenInit struct {
  41. MechTypes []asn1.ObjectIdentifier `asn1:"explicit,tag:0"`
  42. ReqFlags ContextFlags `asn1:"explicit,optional,tag:1"`
  43. MechToken []byte `asn1:"explicit,optional,tag:2"`
  44. MechTokenMIC []byte `asn1:"explicit,optional,tag:3"`
  45. }
  46. // Negotiation Token - Resp/Targ
  47. type NegTokenResp struct {
  48. NegState asn1.Enumerated `asn1:"explicit,tag:0"`
  49. SupportedMech asn1.ObjectIdentifier `asn1:"explicit,optional,tag:1"`
  50. ResponseToken []byte `asn1:"explicit,optional,tag:2"`
  51. MechListMIC []byte `asn1:"explicit,optional,tag:3"`
  52. }
  53. type NegTokenTarg NegTokenResp
  54. // Unmarshal and return either a NegTokenInit or a NegTokenResp.
  55. //
  56. // The boolean indicates if the response is a NegTokenInit.
  57. // If error is nil and the boolean is false the response is a NegTokenResp.
  58. func UnmarshalNegToken(b []byte) (bool, interface{}, error) {
  59. var a asn1.RawValue
  60. _, err := asn1.Unmarshal(b, &a)
  61. if err != nil {
  62. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken: %v", err)
  63. }
  64. switch a.Tag {
  65. case 0:
  66. var negToken NegTokenInit
  67. _, err = asn1.Unmarshal(a.Bytes, &negToken)
  68. if err != nil {
  69. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", a.Tag, err)
  70. }
  71. return true, negToken, nil
  72. case 1:
  73. var negToken NegTokenResp
  74. _, err = asn1.Unmarshal(a.Bytes, &negToken)
  75. if err != nil {
  76. return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", a.Tag, err)
  77. }
  78. return false, negToken, nil
  79. default:
  80. return false, nil, errors.New("Unknown choice type for NegotiationToken")
  81. }
  82. }
  83. // Marshal an Init negotiation token
  84. func (n *NegTokenInit) Marshal() ([]byte, error) {
  85. b, err := asn1.Marshal(*n)
  86. if err != nil {
  87. return nil, err
  88. }
  89. nt := asn1.RawValue{
  90. Tag: 0,
  91. Class: 2,
  92. IsCompound: true,
  93. Bytes: b,
  94. }
  95. nb, err := asn1.Marshal(nt)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return nb, nil
  100. }
  101. // Marshal a Resp/Targ negotiation token
  102. func (n *NegTokenResp) Marshal() ([]byte, error) {
  103. b, err := asn1.Marshal(*n)
  104. if err != nil {
  105. return nil, err
  106. }
  107. nt := asn1.RawValue{
  108. Tag: 1,
  109. Class: 2,
  110. IsCompound: true,
  111. Bytes: b,
  112. }
  113. nb, err := asn1.Marshal(nt)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return nb, nil
  118. }
  119. // Create new Init negotiation token for Kerberos 5
  120. func NewNegTokenInitKrb5(c config.Config, cname types.PrincipalName, tkt types.Ticket, sessionKey types.EncryptionKey) (NegTokenInit, error) {
  121. mt, err := NewKRB5APREQMechToken(c, cname, tkt, sessionKey)
  122. if err != nil {
  123. return NegTokenInit{}, fmt.Errorf("Error getting MechToken; %v", err)
  124. }
  125. return NegTokenInit{
  126. MechTypes: []asn1.ObjectIdentifier{MechTypeOID_Krb5},
  127. MechToken: mt,
  128. }, nil
  129. }