This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
/
ztls_json.go
66 lines (57 loc) · 1.54 KB
/
ztls_json.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
package ztls
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/zmap/zgrab/ztools/x509"
)
type encodedCertificates struct {
Certificates [][]byte `json:"raw"`
ParsedCertificate *x509.Certificate `json:"certificate"`
ParsedCertificates []*x509.Certificate `json:"chain"`
}
func (ec *encodedCertificates) FromZTLS(c *Certificates) *encodedCertificates {
ec.Certificates = c.Certificates
if len(c.ParsedCertificates) > 0 {
ec.ParsedCertificate = c.ParsedCertificates[0]
}
if len(c.ParsedCertificates) > 1 {
ec.ParsedCertificates = c.ParsedCertificates[1:]
}
return ec
}
func (c *Certificates) FromEncoded(ec *encodedCertificates) *Certificates {
c.Certificates = ec.Certificates
// TODO actually parse the parsed cert
return c
}
func (c *Certificates) MarshalJSON() ([]byte, error) {
ec := new(encodedCertificates).FromZTLS(c)
return json.Marshal(ec)
}
func (c *Certificates) UnmarshalJSON(b []byte) error {
ec := new(encodedCertificates)
if err := json.Unmarshal(b, ec); err != nil {
return err
}
c.FromEncoded(ec)
return nil
}
func (cs CipherSuite) MarshalJSON() ([]byte, error) {
buf := make([]byte, 2)
buf[0] = byte(cs >> 8)
buf[1] = byte(cs)
enc := strings.ToUpper(hex.EncodeToString(buf))
m := make(map[string]interface{}, 3)
m["hex"] = fmt.Sprintf("0x%s", enc)
m["name"] = cs.String()
m["value"] = int(cs)
return json.Marshal(m)
}
func (v TLSVersion) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
m["name"] = v.String()
m["value"] = int(v)
return json.Marshal(m)
}