wrapToken_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package gssapi
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/jcmturner/gokrb5/v8/iana/keyusage"
  7. "github.com/jcmturner/gokrb5/v8/types"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. const (
  11. // What a kerberized server might send
  12. testChallengeFromAcceptor = "050401ff000c000000000000575e85d601010000853b728d5268525a1386c19f"
  13. // What an initiator client could reply
  14. testChallengeReplyFromInitiator = "050400ff000c000000000000000000000101000079a033510b6f127212242b97"
  15. // session key used to sign the tokens above
  16. sessionKey = "14f9bde6b50ec508201a97f74c4e5bd3"
  17. sessionKeyType = 17
  18. acceptorSeal = keyusage.GSSAPI_ACCEPTOR_SEAL
  19. initiatorSeal = keyusage.GSSAPI_INITIATOR_SEAL
  20. )
  21. func getSessionKey() types.EncryptionKey {
  22. key, _ := hex.DecodeString(sessionKey)
  23. return types.EncryptionKey{
  24. KeyType: sessionKeyType,
  25. KeyValue: key,
  26. }
  27. }
  28. func getChallengeReference() *WrapToken {
  29. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  30. return &WrapToken{
  31. Flags: 0x01,
  32. EC: 12,
  33. RRC: 0,
  34. SndSeqNum: binary.BigEndian.Uint64(challenge[8:16]),
  35. Payload: []byte{0x01, 0x01, 0x00, 0x00},
  36. CheckSum: challenge[20:32],
  37. }
  38. }
  39. func getChallengeReferenceNoChksum() *WrapToken {
  40. c := getChallengeReference()
  41. c.CheckSum = nil
  42. return c
  43. }
  44. func getResponseReference() *WrapToken {
  45. response, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  46. return &WrapToken{
  47. Flags: 0x00,
  48. EC: 12,
  49. RRC: 0,
  50. SndSeqNum: 0,
  51. Payload: []byte{0x01, 0x01, 0x00, 0x00},
  52. CheckSum: response[20:32],
  53. }
  54. }
  55. func getResponseReferenceNoChkSum() *WrapToken {
  56. r := getResponseReference()
  57. r.CheckSum = nil
  58. return r
  59. }
  60. func TestUnmarshal_Challenge(t *testing.T) {
  61. t.Parallel()
  62. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  63. var wt WrapToken
  64. err := wt.Unmarshal(challenge, true)
  65. assert.Nil(t, err, "Unexpected error occurred.")
  66. assert.Equal(t, getChallengeReference(), &wt, "Token not decoded as expected.")
  67. }
  68. func TestUnmarshalFailure_Challenge(t *testing.T) {
  69. t.Parallel()
  70. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  71. var wt WrapToken
  72. err := wt.Unmarshal(challenge, false)
  73. assert.NotNil(t, err, "Expected error did not occur: a message from the acceptor cannot be expected to be sent from the initiator.")
  74. assert.Nil(t, wt.Payload, "Token fields should not have been initialised")
  75. assert.Nil(t, wt.CheckSum, "Token fields should not have been initialised")
  76. assert.Equal(t, byte(0x00), wt.Flags, "Token fields should not have been initialised")
  77. assert.Equal(t, uint16(0), wt.EC, "Token fields should not have been initialised")
  78. assert.Equal(t, uint16(0), wt.RRC, "Token fields should not have been initialised")
  79. assert.Equal(t, uint64(0), wt.SndSeqNum, "Token fields should not have been initialised")
  80. }
  81. func TestUnmarshal_ChallengeReply(t *testing.T) {
  82. t.Parallel()
  83. response, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  84. var wt WrapToken
  85. err := wt.Unmarshal(response, false)
  86. assert.Nil(t, err, "Unexpected error occurred.")
  87. assert.Equal(t, getResponseReference(), &wt, "Token not decoded as expected.")
  88. }
  89. func TestUnmarshalFailure_ChallengeReply(t *testing.T) {
  90. t.Parallel()
  91. response, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  92. var wt WrapToken
  93. err := wt.Unmarshal(response, true)
  94. assert.NotNil(t, err, "Expected error did not occur: a message from the initiator cannot be expected to be sent from the acceptor.")
  95. assert.Nil(t, wt.Payload, "Token fields should not have been initialised")
  96. assert.Nil(t, wt.CheckSum, "Token fields should not have been initialised")
  97. assert.Equal(t, byte(0x00), wt.Flags, "Token fields should not have been initialised")
  98. assert.Equal(t, uint16(0), wt.EC, "Token fields should not have been initialised")
  99. assert.Equal(t, uint16(0), wt.RRC, "Token fields should not have been initialised")
  100. assert.Equal(t, uint64(0), wt.SndSeqNum, "Token fields should not have been initialised")
  101. }
  102. func TestChallengeChecksumVerification(t *testing.T) {
  103. t.Parallel()
  104. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  105. var wt WrapToken
  106. wt.Unmarshal(challenge, true)
  107. challengeOk, cErr := wt.Verify(getSessionKey(), acceptorSeal)
  108. assert.Nil(t, cErr, "Error occurred during checksum verification.")
  109. assert.True(t, challengeOk, "Checksum verification failed.")
  110. }
  111. func TestResponseChecksumVerification(t *testing.T) {
  112. t.Parallel()
  113. reply, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  114. var wt WrapToken
  115. wt.Unmarshal(reply, false)
  116. replyOk, rErr := wt.Verify(getSessionKey(), initiatorSeal)
  117. assert.Nil(t, rErr, "Error occurred during checksum verification.")
  118. assert.True(t, replyOk, "Checksum verification failed.")
  119. }
  120. func TestChecksumVerificationFailure(t *testing.T) {
  121. t.Parallel()
  122. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  123. var wt WrapToken
  124. wt.Unmarshal(challenge, true)
  125. // Test a failure with the correct key but wrong keyusage:
  126. challengeOk, cErr := wt.Verify(getSessionKey(), initiatorSeal)
  127. assert.NotNil(t, cErr, "Expected error did not occur.")
  128. assert.False(t, challengeOk, "Checksum verification succeeded when it should have failed.")
  129. wrongKeyVal, _ := hex.DecodeString("14f9bde6b50ec508201a97f74c4effff")
  130. badKey := types.EncryptionKey{
  131. KeyType: sessionKeyType,
  132. KeyValue: wrongKeyVal,
  133. }
  134. // Test a failure with the wrong key but correct keyusage:
  135. wrongKeyOk, wkErr := wt.Verify(badKey, acceptorSeal)
  136. assert.NotNil(t, wkErr, "Expected error did not occur.")
  137. assert.False(t, wrongKeyOk, "Checksum verification succeeded when it should have failed.")
  138. }
  139. func TestMarshal_Challenge(t *testing.T) {
  140. t.Parallel()
  141. bytes, _ := getChallengeReference().Marshal()
  142. assert.Equal(t, testChallengeFromAcceptor, hex.EncodeToString(bytes),
  143. "Marshalling did not yield the expected result.")
  144. }
  145. func TestMarshal_ChallengeReply(t *testing.T) {
  146. t.Parallel()
  147. bytes, _ := getResponseReference().Marshal()
  148. assert.Equal(t, testChallengeReplyFromInitiator, hex.EncodeToString(bytes),
  149. "Marshalling did not yield the expected result.")
  150. }
  151. func TestMarshal_Failures(t *testing.T) {
  152. t.Parallel()
  153. noChkSum := getResponseReferenceNoChkSum()
  154. chkBytes, chkErr := noChkSum.Marshal()
  155. assert.Nil(t, chkBytes, "No bytes should be returned.")
  156. assert.NotNil(t, chkErr, "Expected an error as no checksum was set")
  157. noPayload := getResponseReference()
  158. noPayload.Payload = nil
  159. pldBytes, pldErr := noPayload.Marshal()
  160. assert.Nil(t, pldBytes, "No bytes should be returned.")
  161. assert.NotNil(t, pldErr, "Expected an error as no checksum was set")
  162. }
  163. func TestNewInitiatorTokenSignatureAndMarshalling(t *testing.T) {
  164. t.Parallel()
  165. token, tErr := NewInitiatorWrapToken([]byte{0x01, 0x01, 0x00, 0x00}, getSessionKey())
  166. assert.Nil(t, tErr, "Unexpected error.")
  167. assert.Equal(t, getResponseReference(), token, "Token failed to be marshalled to the expected bytes.")
  168. }