-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.go
58 lines (45 loc) · 1.24 KB
/
serializer.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
package sct
import "encoding/binary"
func WriteOutHere(sct SignedCertificateTimestamp, here []byte) (int, error) {
if sct.SCTVersion != 0 {
return 0, INVALID_VERSION
}
//Determine Lenth of Serialized Output
ctxlen := len(sct.Extensions)
siglen := len(sct.Signature.Signature)
sctoutput := 1 + 32 + 8 + 2 + ctxlen + 2 + 2 + siglen
if sctoutput > 0xFFFF {
return 0, SCT_TOO_LARGE
}
if len(here) < sctoutput {
return sctoutput, NOT_ENOUGH_BUFFER
}
//Write Version
here[0] = byte(sct.SCTVersion)
//Write LogID
copy(here[1:33], sct.LogID[:])
//Write Timestamp
binary.BigEndian.PutUint64(here[33:41], sct.Timestamp)
//Write Extensions
binary.BigEndian.PutUint16(here[41:43], uint16(ctxlen))
n := 43 + ctxlen
copy(here[43:n], sct.Extensions)
//Write Signature Algorithm
here[n] = byte(sct.Signature.HashAlgorithm)
here[n+1] = byte(sct.Signature.SignatureAlgorithm)
n += 2
//Write Signature
binary.BigEndian.PutUint16(here[n:n+2], uint16(siglen))
n += 2
copy(here[n:n+siglen], sct.Signature.Signature)
return sctoutput, nil
}
func WriteOut(sct SignedCertificateTimestamp) ([]byte, error) {
n, err := WriteOutHere(sct, nil)
if n == 0 {
return nil, err
}
v := make([]byte, n)
_, err = WriteOutHere(sct, v)
return v, err
}