Jonathan Turner 9 лет назад
Родитель
Сommit
3984e88f67
3 измененных файлов с 46 добавлено и 20 удалено
  1. 38 0
      asn1tools/tools.go
  2. 7 6
      types/Ticket.go
  3. 1 14
      types/asnAppTag/constants.go

+ 38 - 0
asn1tools/tools.go

@@ -0,0 +1,38 @@
+package asn1tools
+
+// ASN1 Length octets.
+// There are two forms: short (for lengths between 0 and 127), and long definite (for lengths between 0 and 2^1008 -1).
+// Short form. One octet. Bit 8 has value "0" and bits 7-1 give the length.
+// Long form. Two to 127 octets. Bit 8 of first octet has value "1" and bits 7-1 give the number of additional length octets. Second and following octets give the length, base 256, most significant digit first.
+func MarshalLengthBytes(l int) []byte {
+	if l <= 127 {
+		return []byte{byte(l)}
+	}
+	var b []byte
+	p := 1
+	for i := 1; i < 127; {
+		b = append([]byte{byte((l % (p * 256)) / p)}, b...)
+		p = p * 256
+		l = l - l%p
+		if l <= 0 {
+			break
+		}
+	}
+	return append([]byte{byte(128 + len(b))}, b...)
+}
+
+// The Marshal method of golang's asn1 package does not enable you to configure to wrap the output in an application tag.
+// This method adds that wrapping tag
+func AddASNAppTag(b []byte, tag int) []byte {
+	// The ASN1 wrapping consists of 2 bytes:
+	// 1st byte -> Identifier Octet - Application Tag
+	// 2nd byte -> The length (this will be the size indicated in the input bytes + 2 for the additional bytes we add here.
+	// Application Tag:
+	//| Byte:       | 8                            | 7                          | 6                                         | 5 | 4 | 3 | 2 | 1             |
+	//| Value:      | 0                            | 1                          | 1                                         | From the RFC spec 4120        |
+	//| Explanation | Defined by the ASN1 encoding rules for an application tag | A value of 1 indicates a constructed type | The ASN Application tag value |
+	// Therefore the value of the byte is an integer = ( Application tag value + 96 )
+	b = append(MarshalLengthBytes(int(b[1])+2), b...)
+	b = append([]byte{byte(96 + tag)}, b...)
+	return b
+}

+ 7 - 6
types/Ticket.go

@@ -2,12 +2,13 @@ package types
 
 import (
 	"encoding/asn1"
+	"encoding/hex"
 	"fmt"
-	"github.com/jcmturner/gokrb5/types/asnAppTag"
 	jtasn1 "github.com/jcmturner/asn1"
-	"time"
+	"github.com/jcmturner/gokrb5/asn1tools"
+	"github.com/jcmturner/gokrb5/types/asnAppTag"
 	"os"
-	"encoding/hex"
+	"time"
 )
 
 // Reference: https://www.ietf.org/rfc/rfc4120.txt
@@ -49,7 +50,7 @@ func (t *Ticket) Marshal() ([]byte, error) {
 	if err != nil {
 		return nil, err
 	}
-	b = asnAppTag.AddASNAppTag(b, asnAppTag.Ticket)
+	b = asn1tools.AddASNAppTag(b, asnAppTag.Ticket)
 	return b, nil
 }
 
@@ -101,7 +102,7 @@ func MarshalTicketSequence(tkts []Ticket) (asn1.RawValue, error) {
 	for i, t := range tkts {
 		b, err := t.Marshal()
 		if err != nil {
-			return raw, fmt.Errorf("Error marshalling ticket number %d in seqence of tickets", i +1)
+			return raw, fmt.Errorf("Error marshalling ticket number %d in seqence of tickets", i+1)
 		}
 		btkts = append(btkts, b...)
 	}
@@ -112,7 +113,7 @@ func MarshalTicketSequence(tkts []Ticket) (asn1.RawValue, error) {
 	//| Byte:       | 8                            | 7                          | 6                                         | 5 | 4 | 3 | 2 | 1             |
 	//| Value:      | 0                            | 1                          | 1                                         | From the RFC spec 4120        |
 	//| Explanation | Defined by the ASN1 encoding rules for an application tag | A value of 1 indicates a constructed type | The ASN Application tag value |
-	btkts = append([]byte{byte(len(btkts))}, btkts...)
+	btkts = append(asn1tools.MarshalLengthBytes(len(btkts)), btkts...)
 	fmt.Fprintf(os.Stderr, "mar: %+v", btkts)
 	raw.Bytes = btkts
 	return raw, nil

+ 1 - 14
types/asnAppTag/constants.go

@@ -20,18 +20,5 @@ const (
 	EncKrbCredPart = 29
 	KRBError       = 30
 )
-// The Marshal method of golang's asn1 package does not enable you to configure to wrap the output in an application tag.
-// This method adds that wrapping tag
-func AddASNAppTag(b []byte, tag int) []byte {
-	// The ASN1 wrapping consists of 2 bytes:
-	// 1st byte -> Identifier Octet - Application Tag
-	// 2nd byte -> The length (this will be the size indicated in the input bytes + 2 for the additional bytes we add here.
-	// Application Tag:
-	//| Byte:       | 8                            | 7                          | 6                                         | 5 | 4 | 3 | 2 | 1             |
-	//| Value:      | 0                            | 1                          | 1                                         | From the RFC spec 4120        |
-	//| Explanation | Defined by the ASN1 encoding rules for an application tag | A value of 1 indicates a constructed type | The ASN Application tag value |
-	// Therefore the value of the byte is an integer = ( Application tag value + 96 )
-	b = append([]byte{byte(96 + tag), byte(b[1] + 2)}, b...)
-	return b
-}
+
 //TODO review if we want to consolidate with the MsgTypes in the dictionary