Procházet zdrojové kódy

Authenticator unmarshal tests

Jonathan Turner před 9 roky
rodič
revize
abf4f261b6
3 změnil soubory, kde provedl 106 přidání a 21 odebrání
  1. 18 10
      types/Authenticator.go
  2. 82 0
      types/Authenticator_test.go
  3. 6 11
      types/AuthorizationData.go

+ 18 - 10
types/Authenticator.go

@@ -1,6 +1,9 @@
 package types
 
 import (
+	"encoding/asn1"
+	"fmt"
+	"github.com/jcmturner/gokrb5/types/asnAppTag"
 	"time"
 )
 
@@ -24,14 +27,19 @@ authorization-data      [8] AuthorizationData OPTIONAL
 
 */
 
-type KDCReq struct {
-	AVNO              int               `asn1:"explicit,tag:0"`
-	CRealm            string            `asn1:"explicit,tag:1"`
-	CName             PrincipalName     `asn1:"explicit,tag:2"`
-	Cksum             Checksum          `asn1:"explicit,optional,tag:3"`
-	Cusec             int               `asn1:"explicit,tag:4"`
-	CTime             time.Time         `asn1:"explicit,tag:5"`
-	SubKey            EncryptionKey     `asn1:"explicit,optional,tag:6"`
-	SeqNumber         int               `asn1:"explicit,optional,tag:7"`
-	AuthorizationData AuthorizationData `asn1:"explicit,optional,tag:8"`
+type Authenticator struct {
+	AVNO              int                      `asn1:"explicit,tag:0"`
+	CRealm            string                   `asn1:"explicit,tag:1"`
+	CName             PrincipalName            `asn1:"explicit,tag:2"`
+	Cksum             Checksum                 `asn1:"explicit,optional,tag:3"`
+	Cusec             int                      `asn1:"explicit,tag:4"`
+	CTime             time.Time                `asn1:"explicit,tag:5"`
+	SubKey            EncryptionKey            `asn1:"explicit,optional,tag:6"`
+	SeqNumber         int                      `asn1:"explicit,optional,tag:7"`
+	AuthorizationData []AuthorizationDataEntry `asn1:"explicit,optional,tag:8"`
+}
+
+func (a *Authenticator) Unmarshal(b []byte) error {
+	_, err := asn1.UnmarshalWithParams(b, a, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.Authenticator))
+	return err
 }

+ 82 - 0
types/Authenticator_test.go

@@ -0,0 +1,82 @@
+package types
+
+import (
+	"encoding/hex"
+	"fmt"
+	"github.com/jcmturner/gokrb5/testdata"
+	"github.com/stretchr/testify/assert"
+	"testing"
+	"time"
+)
+
+const (
+	tf     = "20060102150405"
+	trealm = "ATHENA.MIT.EDU"
+)
+
+func unmarshal(t *testing.T, v string) Authenticator {
+	var a Authenticator
+	//t.Logf("Starting unmarshal tests of %s", v)
+	b, err := hex.DecodeString(testdata.TestVectors[v])
+	if err != nil {
+		t.Fatalf("Test vector read error of %s: %v\n", v, err)
+	}
+	err = a.Unmarshal(b)
+	if err != nil {
+		t.Fatalf("Unmarshal error of %s: %v\n", v, err)
+	}
+	return a
+}
+func TestUnmarshalAuthenticator(t *testing.T) {
+	a := unmarshal(t, "encode_krb5_authenticator")
+	//Parse the test time value into a time.Time type
+	tt, _ := time.Parse(tf, "19940610060317")
+
+	assert.Equal(t, 5, a.AVNO, "Authenticator version number not as expected")
+	assert.Equal(t, trealm, a.CRealm, "CRealm not as expected")
+	assert.Equal(t, 1, a.CName.NameType, "CName NameType not as expected")
+	assert.Equal(t, 2, len(a.CName.NameString), "CName does not have the expected number of NameStrings")
+	assert.Equal(t, "hftsai", a.CName.NameString[0], "CName first entry not as expected")
+	assert.Equal(t, "extra", a.CName.NameString[1], "CName second entry not as expected")
+	assert.Equal(t, 1, a.Cksum.CksumType, "Checksum type not as expected")
+	assert.Equal(t, []byte("1234"), a.Cksum.Checksum, "Checsum not as expected")
+	assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected")
+	assert.Equal(t, tt, a.CTime, "Client time not as expected")
+	assert.Equal(t, 1, a.SubKey.KeyType, "Subkey type not as expected")
+	assert.Equal(t, []byte("12345678"), a.SubKey.KeyValue, "Subkey value not as expected")
+	assert.Equal(t, 2, len(a.AuthorizationData), "Number of Authorization data items not as expected")
+	for i, entry := range a.AuthorizationData {
+		assert.Equal(t, 1, entry.ADType, fmt.Sprintf("Authorization type of entry %d not as expected", i+1))
+		assert.Equal(t, []byte("foobar"), entry.ADData, fmt.Sprintf("Authorization data of entry %d not as expected", i+1))
+	}
+}
+
+func TestUnmarshalAuthenticator_optionalsempty(t *testing.T) {
+	a := unmarshal(t, "encode_krb5_authenticator(optionalsempty)")
+	//Parse the test time value into a time.Time type
+	tt, _ := time.Parse(tf, "19940610060317")
+
+	assert.Equal(t, 5, a.AVNO, "Authenticator version number not as expected")
+	assert.Equal(t, trealm, a.CRealm, "CRealm not as expected")
+	assert.Equal(t, 1, a.CName.NameType, "CName NameType not as expected")
+	assert.Equal(t, 2, len(a.CName.NameString), "CName does not have the expected number of NameStrings")
+	assert.Equal(t, "hftsai", a.CName.NameString[0], "CName first entry not as expected")
+	assert.Equal(t, "extra", a.CName.NameString[1], "CName second entry not as expected")
+	assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected")
+	assert.Equal(t, tt, a.CTime, "Client time not as expected")
+}
+
+func TestUnmarshalAuthenticator_optionalsNULL(t *testing.T) {
+	a := unmarshal(t, "encode_krb5_authenticator(optionalsNULL)")
+	//Parse the test time value into a time.Time type
+	tt, _ := time.Parse(tf, "19940610060317")
+
+	assert.Equal(t, 5, a.AVNO, "Authenticator version number not as expected")
+	assert.Equal(t, trealm, a.CRealm, "CRealm not as expected")
+	assert.Equal(t, 1, a.CName.NameType, "CName NameType not as expected")
+	assert.Equal(t, 2, len(a.CName.NameString), "CName does not have the expected number of NameStrings")
+	assert.Equal(t, "hftsai", a.CName.NameString[0], "CName first entry not as expected")
+	assert.Equal(t, "extra", a.CName.NameString[1], "CName second entry not as expected")
+	assert.Equal(t, 123456, a.Cusec, "Client microseconds not as expected")
+	assert.Equal(t, tt, a.CTime, "Client time not as expected")
+}

+ 6 - 11
types/AuthorizationData.go

@@ -3,8 +3,6 @@ package types
 // Reference: https://www.ietf.org/rfc/rfc4120.txt
 // Section: 5.2.6
 
-import "encoding/asn1"
-
 /*
 AuthorizationData
 
@@ -74,9 +72,12 @@ the ASN.1 structure that follows the subsection heading.
 */
 
 type AuthorizationData struct {
-	ADType int
-	// TODO may make the following a slice of AuthorizationDataEntry
-	ADData []byte
+	Entries []AuthorizationDataEntry
+}
+
+type AuthorizationDataEntry struct {
+	ADType int    `asn1:"explicit,tag:0"`
+	ADData []byte `asn1:"explicit,tag:1"`
 }
 
 type ADIfRelevant struct {
@@ -98,9 +99,3 @@ type ADKDCIssued struct {
 type ADMandatoryForKDC struct {
 	AuthorizationData
 }
-
-func (a *AuthorizationData) GetData() (string, error) {
-	var b []byte
-	_, err := asn1.Unmarshal(a.ADData, &b)
-	return string(b), err
-}