WrapToken_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package gssapi
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "gopkg.in/jcmturner/gokrb5.v4/iana/keyusage"
  8. "gopkg.in/jcmturner/gokrb5.v4/types"
  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. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  62. var wt WrapToken
  63. err := wt.Unmarshal(challenge, true)
  64. assert.Nil(t, err, "Unexpected error occurred.")
  65. assert.Equal(t, getChallengeReference(), &wt, "Token not decoded as expected.")
  66. }
  67. func TestUnmarshalFailure_Challenge(t *testing.T) {
  68. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  69. var wt WrapToken
  70. err := wt.Unmarshal(challenge, false)
  71. assert.NotNil(t, err, "Expected error did not occur: a message from the acceptor cannot be expected to be sent from the initiator.")
  72. assert.Nil(t, wt.Payload, "Token fields should not have been initialised")
  73. assert.Nil(t, wt.CheckSum, "Token fields should not have been initialised")
  74. assert.Equal(t, byte(0x00), wt.Flags, "Token fields should not have been initialised")
  75. assert.Equal(t, uint16(0), wt.EC, "Token fields should not have been initialised")
  76. assert.Equal(t, uint16(0), wt.RRC, "Token fields should not have been initialised")
  77. assert.Equal(t, uint64(0), wt.SndSeqNum, "Token fields should not have been initialised")
  78. }
  79. func TestUnmarshal_ChallengeReply(t *testing.T) {
  80. response, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  81. var wt WrapToken
  82. err := wt.Unmarshal(response, false)
  83. assert.Nil(t, err, "Unexpected error occurred.")
  84. assert.Equal(t, getResponseReference(), &wt, "Token not decoded as expected.")
  85. }
  86. func TestUnmarshalFailure_ChallengeReply(t *testing.T) {
  87. response, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  88. var wt WrapToken
  89. err := wt.Unmarshal(response, true)
  90. assert.NotNil(t, err, "Expected error did not occur: a message from the initiator cannot be expected to be sent from the acceptor.")
  91. assert.Nil(t, wt.Payload, "Token fields should not have been initialised")
  92. assert.Nil(t, wt.CheckSum, "Token fields should not have been initialised")
  93. assert.Equal(t, byte(0x00), wt.Flags, "Token fields should not have been initialised")
  94. assert.Equal(t, uint16(0), wt.EC, "Token fields should not have been initialised")
  95. assert.Equal(t, uint16(0), wt.RRC, "Token fields should not have been initialised")
  96. assert.Equal(t, uint64(0), wt.SndSeqNum, "Token fields should not have been initialised")
  97. }
  98. func TestChallengeChecksumVerification(t *testing.T) {
  99. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  100. var wt WrapToken
  101. wt.Unmarshal(challenge, true)
  102. challengeOk, cErr := wt.VerifyCheckSum(getSessionKey(), acceptorSeal)
  103. assert.Nil(t, cErr, "Error occurred during checksum verification.")
  104. assert.True(t, challengeOk, "Checksum verification failed.")
  105. }
  106. func TestResponseChecksumVerification(t *testing.T) {
  107. reply, _ := hex.DecodeString(testChallengeReplyFromInitiator)
  108. var wt WrapToken
  109. wt.Unmarshal(reply, false)
  110. replyOk, rErr := wt.VerifyCheckSum(getSessionKey(), initiatorSeal)
  111. assert.Nil(t, rErr, "Error occurred during checksum verification.")
  112. assert.True(t, replyOk, "Checksum verification failed.")
  113. }
  114. func TestChecksumVerificationFailure(t *testing.T) {
  115. challenge, _ := hex.DecodeString(testChallengeFromAcceptor)
  116. var wt WrapToken
  117. wt.Unmarshal(challenge, true)
  118. // Test a failure with the correct key but wrong keyusage:
  119. challengeOk, cErr := wt.VerifyCheckSum(getSessionKey(), initiatorSeal)
  120. assert.NotNil(t, cErr, "Expected error did not occur.")
  121. assert.False(t, challengeOk, "Checksum verification succeeded when it should have failed.")
  122. wrongKeyVal, _ := hex.DecodeString("14f9bde6b50ec508201a97f74c4effff")
  123. badKey := types.EncryptionKey{
  124. KeyType: sessionKeyType,
  125. KeyValue: wrongKeyVal,
  126. }
  127. // Test a failure with the wrong key but correct keyusage:
  128. wrongKeyOk, wkErr := wt.VerifyCheckSum(badKey, acceptorSeal)
  129. assert.NotNil(t, wkErr, "Expected error did not occur.")
  130. assert.False(t, wrongKeyOk, "Checksum verification succeeded when it should have failed.")
  131. }
  132. func TestMarshal_Challenge(t *testing.T) {
  133. bytes, _ := getChallengeReference().Marshal()
  134. assert.Equal(t, testChallengeFromAcceptor, hex.EncodeToString(bytes),
  135. "Marshalling did not yield the expected result.")
  136. }
  137. func TestMarshal_ChallengeReply(t *testing.T) {
  138. bytes, _ := getResponseReference().Marshal()
  139. assert.Equal(t, testChallengeReplyFromInitiator, hex.EncodeToString(bytes),
  140. "Marshalling did not yield the expected result.")
  141. }
  142. func TestMarshal_Failures(t *testing.T) {
  143. noChkSum := getResponseReferenceNoChkSum()
  144. chkBytes, chkErr := noChkSum.Marshal()
  145. assert.Nil(t, chkBytes, "No bytes should be returned.")
  146. assert.NotNil(t, chkErr, "Expected an error as no checksum was set")
  147. noPayload := getResponseReference()
  148. noPayload.Payload = nil
  149. pldBytes, pldErr := noPayload.Marshal()
  150. assert.Nil(t, pldBytes, "No bytes should be returned.")
  151. assert.NotNil(t, pldErr, "Expected an error as no checksum was set")
  152. }
  153. func TestNewInitiatorTokenSignatureAndMarshalling(t *testing.T) {
  154. token, tErr := NewInitiatorToken([]byte{0x01, 0x01, 0x00, 0x00}, getSessionKey())
  155. assert.Nil(t, tErr, "Unexepected error.")
  156. assert.Equal(t, getResponseReference(), token, "Token failed to be marshalled to the expected bytes.")
  157. }