aboutsummaryrefslogtreecommitdiff
path: root/types/item.go
blob: 8596ce479bb26835982feb42d2b9268a103b26e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package types

import (
	"fmt"

	"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
	StFormatCosignedTreeHeadV1 StFormat = 2
	StFormatConsistencyProofV1 StFormat = 3
	StFormatInclusionProofV1   StFormat = 4
	StFormatSignedChecksumV1   StFormat = 5
)

// StItem references a versioned item based on a given format specifier
type StItem struct {
	Format             StFormat            `tls:"maxval:65535"`
	SignedTreeHeadV1   *SignedTreeHeadV1   `tls:"selector:Format,val:1"`
	CosignedTreeHeadV1 *CosignedTreeHeadV1 `tls:"selector:Format,val:2"`
	ConsistencyProofV1 *ConsistencyProofV1 `tls:"selector:Format,val:3"`
	InclusionProofV1   *InclusionProofV1   `tls:"selector:Format,val:4"`
	SignedChecksumV1   *SignedChecksumV1   `tls:"selector:Format,val:5"`
}

// StItemList is an StItem List that is at most 2^32-1 bytes when serialized.
type StItemList struct {
	Items []StItem `tls:"minlen:0,maxlen:4294967295"`
}

type SignedTreeHeadV1 struct {
	TreeHead  TreeHeadV1
	Signature SignatureV1
}

type CosignedTreeHeadV1 struct {
	SignedTreeHead SignedTreeHeadV1
	Cosignatures   []SignatureV1 `tls:"minlen:0,maxlen:4294967295"`
}

type ConsistencyProofV1 struct {
	LogId           Namespace
	TreeSize1       uint64
	TreeSize2       uint64
	ConsistencyPath []NodeHash `tls:"minlen:0,maxlen:65535"`
}

type InclusionProofV1 struct {
	LogId         Namespace
	TreeSize      uint64
	LeafIndex     uint64
	InclusionPath []NodeHash `tls:"minlen:0,maxlen:65535"`
}

type SignedChecksumV1 struct {
	Data      ChecksumV1
	Signature SignatureV1
}

type ChecksumV1 struct {
	Identifier []byte `tls:"minlen:1,maxlen:128"`
	Checksum   []byte `tls:"minlen:1,maxlen:64"`
}

type TreeHeadV1 struct {
	Timestamp uint64
	TreeSize  uint64
	RootHash  NodeHash
	Extension []byte `tls:"minlen:0,maxlen:65535"`
}

type NodeHash struct {
	Data []byte `tls:"minlen:32,maxlen:255"`
}

type SignatureV1 struct {
	Namespace Namespace
	Signature []byte `tls:"minlen:1,maxlen:65535"`
}

func (f StFormat) String() string {
	switch f {
	case StFormatReserved:
		return "reserved"
	case StFormatSignedTreeHeadV1:
		return "signed_tree_head_v1"
	case StFormatCosignedTreeHeadV1:
		return "cosigned_tree_head_v1"
	case StFormatConsistencyProofV1:
		return "consistency_proof_v1"
	case StFormatInclusionProofV1:
		return "inclusion_proof_v1"
	case StFormatSignedChecksumV1:
		return "signed_checksum_v1"
	default:
		return fmt.Sprintf("unknown StFormat: %d", f)
	}
}

func (i StItem) String() string {
	switch i.Format {
	case StFormatReserved:
		return fmt.Sprintf("Format(%s)", i.Format)
	case StFormatSignedTreeHeadV1:
		return fmt.Sprintf("Format(%s): %+v", i.Format, i.SignedTreeHeadV1)
	case StFormatCosignedTreeHeadV1:
		return fmt.Sprintf("Format(%s): %+v", i.Format, i.CosignedTreeHeadV1)
	case StFormatConsistencyProofV1:
		return fmt.Sprintf("Format(%s): %+v", i.Format, i.ConsistencyProofV1)
	case StFormatInclusionProofV1:
		return fmt.Sprintf("Format(%s): %+v", i.Format, i.InclusionProofV1)
	case StFormatSignedChecksumV1:
		return fmt.Sprintf("Format(%s): %+v", i.Format, i.SignedChecksumV1)
	default:
		return fmt.Sprintf("unknown StItem: %v", i.Format)
	}
}

// Marshal marshals a TLS-encodable item
func Marshal(item interface{}) ([]byte, error) {
	serialized, err := tls.Marshal(item)
	if err != nil {
		return nil, fmt.Errorf("tls.Marshal: %v", err)
	}
	return serialized, nil
}

// Unmarshal unmarshals a TLS-encoded item
func Unmarshal(serialized []byte, out interface{}) error {
	extra, err := tls.Unmarshal(serialized, out)
	if err != nil {
		return fmt.Errorf("tls.Unmarshal: %v", err)
	}
	if len(extra) > 0 {
		return fmt.Errorf("tls.Unmarshal: extra data: %X", extra)
	}
	return nil
}