| 1 | package models |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "log" |
| 7 | ) |
| 8 | |
| 9 | type Entry struct { |
| 10 | Data Data `json:"data"` |
| 11 | MessageType string `json:"message_type"` |
| 12 | cachedJSON []byte |
| 13 | cachedJSONLite []byte |
| 14 | } |
| 15 | |
| 16 | // Clone returns a new copy of the Entry. |
| 17 | func (e *Entry) Clone() Entry { |
| 18 | return Entry{ |
| 19 | Data: e.Data, |
| 20 | MessageType: e.MessageType, |
| 21 | cachedJSON: e.cachedJSON, |
| 22 | cachedJSONLite: e.cachedJSONLite, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // JSON returns the json encoded Entry as byte slice and caches it for later access. |
| 27 | func (e *Entry) JSON() []byte { |
| 28 | if len(e.cachedJSON) > 0 { |
| 29 | return e.cachedJSON |
| 30 | } |
| 31 | e.cachedJSON = e.entryToJSONBytes() |
| 32 | |
| 33 | return e.cachedJSON |
| 34 | } |
| 35 | |
| 36 | // JSONNoCache returns the json encoded Entry as byte slice without caching it. |
| 37 | func (e *Entry) JSONNoCache() []byte { |
| 38 | return e.entryToJSONBytes() |
| 39 | } |
| 40 | |
| 41 | // JSONLite does the same as JSON() but removes the chain and cert's DER representation. |
| 42 | func (e *Entry) JSONLite() []byte { |
| 43 | if len(e.cachedJSONLite) > 0 { |
| 44 | return e.cachedJSONLite |
| 45 | } |
| 46 | e.cachedJSONLite = e.JSONLiteNoCache() |
| 47 | |
| 48 | return e.cachedJSONLite |
| 49 | } |
| 50 | |
| 51 | // JSONLiteNoCache does the same as JSONNoCache() but removes the chain and cert's DER representation. |
| 52 | func (e *Entry) JSONLiteNoCache() []byte { |
| 53 | newEntry := e.Clone() |
| 54 | newEntry.Data.Chain = nil |
| 55 | newEntry.Data.LeafCert.AsDER = "" |
| 56 | |
| 57 | return newEntry.entryToJSONBytes() |
| 58 | } |
| 59 | |
| 60 | // JSONDomains returns the json encoded domains (DomainsEntry) as byte slice. |
| 61 | func (e *Entry) JSONDomains() []byte { |
| 62 | domainsEntry := DomainsEntry{ |
| 63 | Data: e.Data.LeafCert.AllDomains, |
| 64 | MessageType: "dns_entries", |
| 65 | } |
| 66 | |
| 67 | domainsEntryBytes, err := json.Marshal(domainsEntry) |
| 68 | if err != nil { |
| 69 | log.Println(err) |
| 70 | } |
| 71 | |
| 72 | return domainsEntryBytes |
| 73 | } |
| 74 | |
| 75 | // entryToJSONBytes encodes an Entry to a JSON byte slice. |
| 76 | func (e *Entry) entryToJSONBytes() []byte { |
| 77 | buf := bytes.Buffer{} |
| 78 | enc := json.NewEncoder(&buf) |
| 79 | enc.SetEscapeHTML(false) |
| 80 | |
| 81 | err := enc.Encode(e) |
| 82 | if err != nil { |
| 83 | log.Println(err) |
| 84 | } |
| 85 | |
| 86 | return buf.Bytes() |
| 87 | } |
| 88 | |
| 89 | type Data struct { |
| 90 | CertIndex uint64 `json:"cert_index"` |
| 91 | CertLink string `json:"cert_link"` |
| 92 | Chain []LeafCert `json:"chain,omitempty"` |
| 93 | LeafCert LeafCert `json:"leaf_cert"` |
| 94 | Seen float64 `json:"seen"` |
| 95 | Source Source `json:"source"` |
| 96 | UpdateType string `json:"update_type"` |
| 97 | } |
| 98 | |
| 99 | const ( |
| 100 | SourceIsTiled = "tiled" |
| 101 | SourceIsRFC6962 = "rfc6962" |
| 102 | ) |
| 103 | |
| 104 | type Source struct { |
| 105 | Name string `json:"name"` |
| 106 | URL string `json:"url"` |
| 107 | Timestamp float64 `json:"timestamp"` |
| 108 | Operator string `json:"-"` |
| 109 | NormalizedURL string `json:"-"` |
| 110 | Type string `json:"type"` |
| 111 | } |
| 112 | |
| 113 | type LeafCert struct { |
| 114 | AllDomains []string `json:"all_domains"` |
| 115 | AsDER string `json:"as_der,omitempty"` |
| 116 | Extensions Extensions `json:"extensions"` |
| 117 | Fingerprint string `json:"fingerprint"` |
| 118 | SHA1 string `json:"sha1"` |
| 119 | SHA256 string `json:"sha256"` |
| 120 | NotAfter int64 `json:"not_after"` |
| 121 | NotBefore int64 `json:"not_before"` |
| 122 | SerialNumber string `json:"serial_number"` |
| 123 | SignatureAlgorithm string `json:"signature_algorithm"` |
| 124 | Subject Subject `json:"subject"` |
| 125 | Issuer Subject `json:"issuer"` |
| 126 | IsCA bool `json:"is_ca"` |
| 127 | } |
| 128 | |
| 129 | type Subject struct { |
| 130 | C *string `json:"C"` |
| 131 | CN *string `json:"CN"` |
| 132 | L *string `json:"L"` |
| 133 | O *string `json:"O"` |
| 134 | OU *string `json:"OU"` |
| 135 | ST *string `json:"ST"` |
| 136 | Aggregated *string `json:"aggregated"` |
| 137 | EmailAddress *string `json:"email_address"` |
| 138 | } |
| 139 | |
| 140 | type Extensions struct { |
| 141 | AuthorityInfoAccess *string `json:"authorityInfoAccess,omitempty"` |
| 142 | AuthorityKeyIdentifier *string `json:"authorityKeyIdentifier,omitempty"` |
| 143 | BasicConstraints *string `json:"basicConstraints,omitempty"` |
| 144 | CertificatePolicies *string `json:"certificatePolicies,omitempty"` |
| 145 | CtlSignedCertificateTimestamp *string `json:"ctlSignedCertificateTimestamp,omitempty"` |
| 146 | ExtendedKeyUsage *string `json:"extendedKeyUsage,omitempty"` |
| 147 | KeyUsage *string `json:"keyUsage,omitempty"` |
| 148 | SubjectAltName *string `json:"subjectAltName,omitempty"` |
| 149 | SubjectKeyIdentifier *string `json:"subjectKeyIdentifier,omitempty"` |
| 150 | CTLPoisonByte bool `json:"ctlPoisonByte,omitempty"` |
| 151 | } |
| 152 | |
| 153 | type DomainsEntry struct { |
| 154 | Data []string `json:"data"` |
| 155 | MessageType string `json:"message_type"` |
| 156 | } |