| 1 |
package gssapi |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"math" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"gopkg.in/jcmturner/gokrb5.v5/credentials" |
| 10 |
"gopkg.in/jcmturner/gokrb5.v5/iana/msgtype" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v5/messages" |
| 12 |
"gopkg.in/jcmturner/gokrb5.v5/testdata" |
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/types" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
MechTokenHex = "6082026306092a864886f71201020201006e8202523082024ea003020105a10302010ea20703050000000000a382015d6182015930820155a003020105a10d1b0b544553542e474f4b524235a2233021a003020101a11a30181b04485454501b10686f73742e746573742e676f6b726235a382011830820114a003020112a103020103a28201060482010230621d868c97f30bf401e03bbffcd724bd9d067dce2afc31f71a356449b070cdafcc1ff372d0eb1e7a708b50c0152f3996c45b1ea312a803907fb97192d39f20cdcaea29876190f51de6e2b4a4df0460122ed97f363434e1e120b0e76c172b4424a536987152ac0b73013ab88af4b13a3fcdc63f739039dd46d839709cf5b51bb0ce6cb3af05fab3844caac280929955495235e9d0424f8a1fb9b4bd4f6bba971f40b97e9da60b9dabfcf0b1feebfca02c9a19b327a0004aa8e19192726cf347561fa8ac74afad5d6a264e50cf495b93aac86c77b2bc2d184234f6c2767dbea431485a25687b9044a20b601e968efaefffa1fc5283ff32aa6a53cb6c5cdd2eddcb26a481d73081d4a003020112a103020103a281c70481c4a1b29e420324f7edf9efae39df7bcaaf196a3160cf07e72f52a4ef8a965721b2f3343719c50699046e4fcc18ca26c2bfc7e4a9eddfc9d9cfc57ff2f6bdbbd1fc40ac442195bc669b9a0dbba12563b3e4cac9f4022fc01b8aa2d1ab84815bb078399ff7f4d5f9815eef896a0c7e3c049e6fd9932b97096cdb5861425b9d81753d0743212ded1a0fb55a00bf71a46be5ce5e1c8a5cc327b914347d9efcb6cb31ca363b1850d95c7b6c4c3cc6301615ad907318a0c5379d343610fab17eca9c7dc0a5a60658" |
| 18 |
AuthChksum = "100000000000000000000000000000000000000030000000" |
| 19 |
) |
| 20 |
|
| 21 |
func TestMechToken_Unmarshal(t *testing.T) { |
| 22 |
t.Parallel() |
| 23 |
b, err := hex.DecodeString(MechTokenHex) |
| 24 |
if err != nil { |
| 25 |
t.Fatalf("Error decoding MechToken hex: %v", err) |
| 26 |
} |
| 27 |
var mt MechToken |
| 28 |
err = mt.Unmarshal(b) |
| 29 |
if err != nil { |
| 30 |
t.Fatalf("Error unmarshalling MechToken: %v", err) |
| 31 |
} |
| 32 |
assert.Equal(t, MechTypeOIDKRB5, mt.OID, "MechToken OID not as expected.") |
| 33 |
assert.Equal(t, []byte{1, 0}, mt.TokID, "TokID not as expected") |
| 34 |
assert.Equal(t, msgtype.KRB_AP_REQ, mt.APReq.MsgType, "MechToken AP_REQ does not have the right message type.") |
| 35 |
assert.Equal(t, int32(0), mt.KRBError.ErrorCode, "KRBError in MechToken does not indicate no error.") |
| 36 |
assert.Equal(t, int32(18), mt.APReq.Authenticator.EType, "Authenticator within AP_REQ does not have the etype expected.") |
| 37 |
} |
| 38 |
|
| 39 |
func TestMechToken_newAuthenticatorChksum(t *testing.T) { |
| 40 |
t.Parallel() |
| 41 |
b, err := hex.DecodeString(AuthChksum) |
| 42 |
if err != nil { |
| 43 |
t.Fatalf("Error decoding MechToken hex: %v", err) |
| 44 |
} |
| 45 |
cb := newAuthenticatorChksum([]int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}) |
| 46 |
assert.Equal(t, b, cb, "SPNEGO Authenticator checksum not as expected") |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func TestMechToken_newAuthenticatorWithSubkeyGeneration(t *testing.T) { |
| 51 |
t.Parallel() |
| 52 |
creds := credentials.NewCredentials("hftsai", testdata.TEST_REALM) |
| 53 |
creds.CName.NameString = testdata.TEST_PRINCIPALNAME_NAMESTRING |
| 54 |
var etypeID int32 = 18 |
| 55 |
keyLen := 32 |
| 56 |
a, err := NewAuthenticator(creds, []int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}) |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("Error creating authenticator: %v", err) |
| 59 |
} |
| 60 |
a.GenerateSeqNumberAndSubKey(etypeID, keyLen) |
| 61 |
assert.Equal(t, int32(32771), a.Cksum.CksumType, "Checksum type in authenticator for SPNEGO mechtoken not as expected.") |
| 62 |
assert.Equal(t, etypeID, a.SubKey.KeyType, "Subkey not of the expected type.") |
| 63 |
assert.Equal(t, keyLen, len(a.SubKey.KeyValue), "Subkey value not of the right length") |
| 64 |
var nz bool |
| 65 |
for _, b := range a.SubKey.KeyValue { |
| 66 |
if b != byte(0) { |
| 67 |
nz = true |
| 68 |
} |
| 69 |
} |
| 70 |
assert.True(t, nz, "subkey not initialised") |
| 71 |
assert.Condition(t, assert.Comparison(func() bool { |
| 72 |
return a.SeqNumber > 0 |
| 73 |
}), "Sequence number is not greater than zero") |
| 74 |
assert.Condition(t, assert.Comparison(func() bool { |
| 75 |
return a.SeqNumber <= math.MaxUint32 |
| 76 |
})) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func TestMechToken_newAuthenticator(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
creds := credentials.NewCredentials("hftsai", testdata.TEST_REALM) |
| 83 |
creds.CName.NameString = testdata.TEST_PRINCIPALNAME_NAMESTRING |
| 84 |
a, err := NewAuthenticator(creds, []int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("Error creating authenticator: %v", err) |
| 87 |
} |
| 88 |
assert.Equal(t, int32(32771), a.Cksum.CksumType, "Checksum type in authenticator for SPNEGO mechtoken not as expected.") |
| 89 |
assert.Equal(t, int32(0), a.SubKey.KeyType, "Subkey not of the expected type.") |
| 90 |
assert.Nil(t, a.SubKey.KeyValue, "Subkey should not be set.") |
| 91 |
|
| 92 |
assert.Condition(t, assert.Comparison(func() bool { |
| 93 |
return a.SeqNumber > 0 |
| 94 |
}), "Sequence number is not greater than zero") |
| 95 |
assert.Condition(t, assert.Comparison(func() bool { |
| 96 |
return a.SeqNumber <= math.MaxUint32 |
| 97 |
})) |
| 98 |
} |
| 99 |
|
| 100 |
func TestNewAPREQMechToken_and_Marshal(t *testing.T) { |
| 101 |
t.Parallel() |
| 102 |
creds := credentials.NewCredentials("hftsai", testdata.TEST_REALM) |
| 103 |
creds.CName.NameString = testdata.TEST_PRINCIPALNAME_NAMESTRING |
| 104 |
|
| 105 |
var tkt messages.Ticket |
| 106 |
v := "encode_krb5_ticket" |
| 107 |
b, err := hex.DecodeString(testdata.TestVectors[v]) |
| 108 |
if err != nil { |
| 109 |
t.Fatalf("Test vector read error of %s: %v\n", v, err) |
| 110 |
} |
| 111 |
err = tkt.Unmarshal(b) |
| 112 |
if err != nil { |
| 113 |
t.Fatalf("Unmarshal error of %s: %v\n", v, err) |
| 114 |
} |
| 115 |
|
| 116 |
key := types.EncryptionKey{ |
| 117 |
KeyType: 18, |
| 118 |
KeyValue: make([]byte, 32), |
| 119 |
} |
| 120 |
|
| 121 |
mt, err := NewAPREQMechToken(creds, tkt, key, []int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}, []int{}) |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("Error creating MechToken: %v", err) |
| 124 |
} |
| 125 |
mb, err := mt.Marshal() |
| 126 |
if err != nil { |
| 127 |
t.Fatalf("Error unmarshalling MechToken: %v", err) |
| 128 |
} |
| 129 |
err = mt.Unmarshal(mb) |
| 130 |
if err != nil { |
| 131 |
t.Fatalf("Error unmarshalling MechToken: %v", err) |
| 132 |
} |
| 133 |
assert.Equal(t, MechTypeOIDKRB5, mt.OID, "MechToken OID not as expected.") |
| 134 |
assert.Equal(t, []byte{1, 0}, mt.TokID, "TokID not as expected") |
| 135 |
assert.Equal(t, msgtype.KRB_AP_REQ, mt.APReq.MsgType, "MechToken AP_REQ does not have the right message type.") |
| 136 |
assert.Equal(t, int32(0), mt.KRBError.ErrorCode, "KRBError in MechToken does not indicate no error.") |
| 137 |
assert.Equal(t, testdata.TEST_REALM, mt.APReq.Ticket.Realm, "Realm in ticket within the AP_REQ of the MechToken not as expected.") |
| 138 |
assert.Equal(t, testdata.TEST_PRINCIPALNAME_NAMESTRING, mt.APReq.Ticket.SName.NameString, "SName in ticket within the AP_REQ of the MechToken not as expected.") |
| 139 |
assert.Equal(t, int32(18), mt.APReq.Authenticator.EType, "Authenticator within AP_REQ does not have the etype expected.") |
| 140 |
} |