diff options
| -rw-r--r-- | type.go | 76 | ||||
| -rw-r--r-- | type_test.go | 56 | 
2 files changed, 131 insertions, 1 deletions
| @@ -1,3 +1,77 @@  package stfe -// Leaf definition and such goes here +import ( +	"fmt" + +	"encoding/base64" + +	"github.com/google/certificate-transparency-go/tls" +) + +// StFormat defines a particular StItem type that is versioned +type StFormat tls.Enum + +const ( +	StFormatReserved           StFormat = 0 +	StFormatSignedTreeHeadV1   StFormat = 1 +	StFormatSignedDebugInfoV1  StFormat = 2 +	StFormatConsistencyProofV1 StFormat = 3 +	StFormatInclusionProofV1   StFormat = 4 +	StFormatChecksumV1                  = 5 +) + +func (f StFormat) String() string { +	switch f { +	case StFormatReserved: +		return "reserved" +	case StFormatSignedTreeHeadV1: +		return "signed_tree_head_v1" +	case StFormatSignedDebugInfoV1: +		return "signed_debug_info_v1" +	case StFormatConsistencyProofV1: +		return "consistency_proof_v1" +	case StFormatInclusionProofV1: +		return "inclusion_proof_v1" +	case StFormatChecksumV1: +		return "checksum_v1" +	default: +		return fmt.Sprintf("Unknown StFormat: %d", f) +	} +} + +// StItem references a versioned item based on a given format specifier. +type StItem struct { +	Format     StFormat    `tls:"maxval:65535"` +	ChecksumV1 *ChecksumV1 `tls:"selector:Format,val:5"` +	// TODO: add more items +} + +func (i StItem) String() string { +	switch i.Format { +	case StFormatChecksumV1: +		return fmt.Sprintf("%s %s", i.Format, *i.ChecksumV1) +	default: +		return fmt.Sprintf("unknown StItem: %s", i.Format) +	} +} + +// ChecksumV1 associates a package name with an arbitrary checksum value +type ChecksumV1 struct { +	Package  []byte `tls:"minlen:0,maxlen:255"` +	Checksum []byte `tls:"minlen:32,maxlen:255"` +} + +// NewChecksumV1 creates a new StItem of type checksum_v1 +func NewChecksumV1(name string, checksum []byte) (StItem, error) { +	return StItem{ +		Format: StFormatChecksumV1, +		ChecksumV1: &ChecksumV1{ +			Package:  []byte(name), +			Checksum: checksum, +		}, +	}, nil // TODO: error handling +} + +func (i ChecksumV1) String() string { +	return fmt.Sprintf("%v %v", string(i.Package), base64.StdEncoding.EncodeToString(i.Checksum)) +} diff --git a/type_test.go b/type_test.go new file mode 100644 index 0000000..bcd66e6 --- /dev/null +++ b/type_test.go @@ -0,0 +1,56 @@ +package stfe + +import ( +	"fmt" + +	"crypto/sha256" + +	"github.com/google/certificate-transparency-go/tls" +) + +func ExampleNewChecksumV1() { +	name := "foobar-1.2.3" +	hasher := sha256.New() +	hasher.Write([]byte(name)) +	checksum := hasher.Sum(nil) // hash of package name + +	item, err := NewChecksumV1(name, checksum) +	if err != nil { +		fmt.Printf("failed creating checksum item: %v", err) +		return +	} +	fmt.Printf("%s\n", item) +	// Output: checksum_v1 foobar-1.2.3 UOeWe84malBvj2FLtQlr66WA0gUEa5GPR9I7LsYm114= +} + +func ExampleMarshalChecksumV1() { +	item, err := NewChecksumV1("foobar-1.2.3", make([]byte, 32)) +	if err != nil { +		fmt.Printf("failed creating checksum item: %v", err) +		return +	} + +	b, err := tls.Marshal(item) +	if err != nil { +		fmt.Printf("tls.Marshal() failed: %v", err) +		return +	} +	fmt.Printf("%v\n", b) +	// Output: [0 5 12 102 111 111 98 97 114 45 49 46 50 46 51 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] +} + +func ExampleUnmarshalChecksumV1() { +	b := []byte{0, 5, 12, 102, 111, 111, 98, 97, 114, 45, 49, 46, 50, 46, 51, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + +	var item StItem +	extra, err := tls.Unmarshal(b, &item) +	if err != nil { +		fmt.Printf("tls.Unmarshal() failed: %v (%v)", err, extra) +		return +	} else if len(extra) > 0 { +		fmt.Printf("tls.Unmarshal() found extra data: %v", extra) +		return +	} +	fmt.Printf("%v\n", item) +	// Output: checksum_v1 foobar-1.2.3 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= +} | 
