-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
extensions.go
77 lines (69 loc) · 2.13 KB
/
extensions.go
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
package sunlight
import (
"errors"
"golang.org/x/crypto/cryptobyte"
)
// Extensions is the CTExtensions field of SignedCertificateTimestamp and
// TimestampedEntry, according to c2sp.org/sunlight.
type Extensions struct {
LeafIndex int64
}
func MarshalExtensions(e Extensions) ([]byte, error) {
// enum {
// leaf_index(0), (255)
// } ExtensionType;
//
// struct {
// ExtensionType extension_type;
// opaque extension_data<0..2^16-1>;
// } Extension;
//
// Extension CTExtensions<0..2^16-1>;
//
// uint8 uint40[5];
// uint40 LeafIndex;
b := &cryptobyte.Builder{}
b.AddUint8(0 /* extension_type = leaf_index */)
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
if e.LeafIndex < 0 || e.LeafIndex >= 1<<40 {
b.SetError(errors.New("leaf_index out of range"))
return
}
addUint40(b, uint64(e.LeafIndex))
})
return b.Bytes()
}
// ParseExtensions parse a CTExtensions field, ignoring unknown extensions.
// It is an error if the leaf_index extension is missing.
func ParseExtensions(extensions []byte) (Extensions, error) {
b := cryptobyte.String(extensions)
for !b.Empty() {
var extensionType uint8
var extension cryptobyte.String
if !b.ReadUint8(&extensionType) || !b.ReadUint16LengthPrefixed(&extension) {
return Extensions{}, errors.New("invalid extension")
}
if extensionType == 0 /* leaf_index */ {
var e Extensions
if !readUint40(&extension, &e.LeafIndex) || !extension.Empty() {
return Extensions{}, errors.New("invalid leaf_index extension")
}
return e, nil
}
}
return Extensions{}, errors.New("missing leaf_index extension")
}
// addUint40 appends a big-endian, 40-bit value to the byte string.
func addUint40(b *cryptobyte.Builder, v uint64) {
b.AddBytes([]byte{byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
}
// readUint40 decodes a big-endian, 40-bit value into out and advances over it.
// It reports whether the read was successful.
func readUint40(s *cryptobyte.String, out *int64) bool {
var v []byte
if !s.ReadBytes(&v, 5) {
return false
}
*out = int64(v[0])<<32 | int64(v[1])<<24 | int64(v[2])<<16 | int64(v[3])<<8 | int64(v[4])
return true
}