Jonathan Turner 9 лет назад
Родитель
Сommit
06e3df885c
3 измененных файлов с 67 добавлено и 55 удалено
  1. 0 8
      GSSAPI/MechType.go
  2. 35 41
      GSSAPI/NegotiationToken.go
  3. 32 6
      GSSAPI/NegotiationToken_test.go

+ 0 - 8
GSSAPI/MechType.go

@@ -1,15 +1,7 @@
 package GSSAPI
 
-import (
-	"github.com/jcmturner/asn1"
-)
-
 const (
 	SPNEGO_OIDHex                = "2b0601050502"       //1.3.6.1.5.5.2
 	MechType_Krb5_OIDHex         = "2a864886f712010202" //1.2.840.113554.1.2.2
 	MechType_MSLegacyKrb5_OIDHex = "2a864882f712010202" //1.2.840.48018.1.2.2
 )
-
-type MechType asn1.ObjectIdentifier
-
-type MechTypeList []MechType

+ 35 - 41
GSSAPI/NegotiationToken.go

@@ -48,73 +48,67 @@ type NegotiationToken struct {
 }
 
 type NegTokenInit struct {
-	Body    asn1.RawValue `asn1:"explicit,tag:0"`
+	MechTypes    []asn1.ObjectIdentifier `asn1:"explicit,tag:0"`
+	ReqFlags     ContextFlags            `asn1:"explicit,optional,tag:1"`
+	MechToken    []byte                  `asn1:"explicit,optional,tag:2"`
+	MechTokenMIC []byte                  `asn1:"explicit,optional,tag:3"`
 }
 
 type NegTokenResp struct {
-	Body    NegTokenRespBody `asn1:"explicit,tag:1"`
-}
-
-type NegTokenInitBody struct {
-	MechTypes    MechTypeList `asn1:"explicit,tag:0"`
-	ReqFlags     ContextFlags `asn1:"explicit,optional,tag:1"`
-	MechToken    []byte       `asn1:"explicit,optional,tag:2"`
-	MechTokenMIC []byte       `asn1:"explicit,optional,tag:3"`
-}
-
-type NegTokenRespBody struct {
-	NegState      asn1.Enumerated `asn1:"explicit,optional,tag:0"`
-	SupportedMech MechType        `asn1:"explicit,optional,tag:1"`
-	ResponseToken []byte          `asn1:"explicit,optional,tag:2"`
-	MechListMIC   []byte          `asn1:"explicit,optional,tag:3"`
+	NegState      asn1.Enumerated       `asn1:"explicit,optional,tag:0"`
+	SupportedMech asn1.ObjectIdentifier `asn1:"explicit,optional,tag:1"`
+	ResponseToken []byte                `asn1:"explicit,optional,tag:2"`
+	MechListMIC   []byte                `asn1:"explicit,optional,tag:3"`
 }
 
 // Unmarshal and return either a NegTokenInit or a NegTokenResp.
 //
-// The boolean indicates if the reponse is a NegTokenInit.
+// The boolean indicates if the response is a NegTokenInit.
 // If error is nil and the boolean is false the response is a NegTokenResp.
 func (n *NegotiationToken) Unmarshal(b []byte) (bool, interface{}, error) {
 	_, err := asn1.Unmarshal(b, n)
 	if err != nil {
 		return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken: %v", err)
 	}
-	var negToken interface{}
-	var isInit bool
 	switch n.Choice.Tag {
 	case 0:
-		negToken = NegTokenInit{}
-		isInit = true
+		var negToken NegTokenInit
+		_, err = asn1.Unmarshal(b, &negToken)
+		if err != nil {
+			return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", n.Choice.Tag, err)
+		}
+		return true, negToken, nil
 	case 1:
-		negToken = NegTokenResp{}
+		var negToken NegTokenResp
+		_, err = asn1.Unmarshal(b, &negToken)
+		if err != nil {
+			return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", n.Choice.Tag, err)
+		}
+		return false, negToken, nil
 	default:
 		return false, nil, errors.New("Unknown choice type for NegotiationToken")
 	}
-	_, err = asn1.Unmarshal(b, &negToken)
-	if err != nil {
-		return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d: %v", n.Choice.Tag, err)
-	}
-	return isInit, negToken, nil
+
 }
 
-// Returns marshalled bytes of a NegotiationToken rather than the NegTokenInit
 func (n *NegTokenInit) Marshal() ([]byte, error) {
 	b, err := asn1.Marshal(*n)
 	if err != nil {
 		return nil, err
 	}
-	nt := NegotiationToken{
-		Choice: asn1.RawValue{
-			Tag:        0,
-			Class:      2,
-			IsCompound: true,
-			Bytes:      b,
-		},
-	}
-	nb, err := asn1.Marshal(nt)
-	if err != nil {
-		return nil, err
-	}
-	return nb, nil
+	//nt := NegotiationToken{
+	//	Choice: asn1.RawValue{
+	//		Tag:        0,
+	//		Class:      2,
+	//		IsCompound: true,
+	//		Bytes:      b,
+	//	},
+	//}
+	//nb, err := asn1.Marshal(nt)
+	//if err != nil {
+	//	return nil, err
+	//}
+	return b, nil
 }
 
 // Returns marshalled bytes of a NegotiationToken rather than the NegTokenResp

+ 32 - 6
GSSAPI/NegotiationToken_test.go

@@ -1,9 +1,10 @@
 package GSSAPI
 
 import (
-	"testing"
 	"encoding/hex"
+	"github.com/jcmturner/asn1"
 	"github.com/stretchr/testify/assert"
+	"testing"
 )
 
 const (
@@ -12,16 +13,41 @@ const (
 
 func TestUnmarshal_negTokenInit(t *testing.T) {
 	b, err := hex.DecodeString(test_negTokenInit)
-	if err != nil{
+	if err != nil {
 		t.Fatalf("Error converting hex string test data to bytes: %v", err)
 	}
 	var n NegotiationToken
-
 	isInit, nt, err := n.Unmarshal(b)
 	if err != nil {
 		t.Fatalf("Error unmarshalling negotiation token: %v", err)
 	}
-	nInit := nt.(NegTokenInit)
+	assert.IsType(t, NegTokenInit{}, nt, "Not the expected type NegTokenInit")
 	assert.True(t, isInit, "Boolean indicating type is negTokenInit is not true")
-	assert.Equal(t, 4, len(nInit.Body.MechTypes))
-}
+	nInit := nt.(NegTokenInit)
+	assert.Equal(t, 4, len(nInit.MechTypes))
+	expectMechTypes := []asn1.ObjectIdentifier{
+		[]int{1, 2, 840, 113554, 1, 2, 2},
+		[]int{1, 3, 5, 1, 5, 2},
+		[]int{1, 2, 840, 48018, 1, 2, 2},
+		[]int{1, 3, 6, 1, 5, 2, 5},
+	}
+	assert.Equal(t, expectMechTypes, nInit.MechTypes, "MechTypes list in NegTokenInit not as expected")
+}
+
+func TestMarshal_negTokenInit(t *testing.T) {
+	b, err := hex.DecodeString(test_negTokenInit)
+	if err != nil {
+		t.Fatalf("Error converting hex string test data to bytes: %v", err)
+	}
+	var n NegotiationToken
+	_, nt, err := n.Unmarshal(b)
+	if err != nil {
+		t.Fatalf("Error unmarshalling negotiation token: %v", err)
+	}
+	nInit := nt.(NegTokenInit)
+	mb, err := nInit.Marshal()
+	if err != nil {
+		t.Fatalf("Error marshalling negotiation init token: %v", err)
+	}
+	assert.Equal(t, b, mb, "Marshalled bytes not as expected for NegTokenInit")
+}