serialization.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ct
  15. import (
  16. "crypto"
  17. "crypto/sha256"
  18. "encoding/json"
  19. "fmt"
  20. "strings"
  21. "time"
  22. "github.com/google/certificate-transparency-go/tls"
  23. "github.com/google/certificate-transparency-go/x509"
  24. )
  25. // SerializeSCTSignatureInput serializes the passed in sct and log entry into
  26. // the correct format for signing.
  27. func SerializeSCTSignatureInput(sct SignedCertificateTimestamp, entry LogEntry) ([]byte, error) {
  28. switch sct.SCTVersion {
  29. case V1:
  30. input := CertificateTimestamp{
  31. SCTVersion: sct.SCTVersion,
  32. SignatureType: CertificateTimestampSignatureType,
  33. Timestamp: sct.Timestamp,
  34. EntryType: entry.Leaf.TimestampedEntry.EntryType,
  35. Extensions: sct.Extensions,
  36. }
  37. switch entry.Leaf.TimestampedEntry.EntryType {
  38. case X509LogEntryType:
  39. input.X509Entry = entry.Leaf.TimestampedEntry.X509Entry
  40. case PrecertLogEntryType:
  41. input.PrecertEntry = &PreCert{
  42. IssuerKeyHash: entry.Leaf.TimestampedEntry.PrecertEntry.IssuerKeyHash,
  43. TBSCertificate: entry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate,
  44. }
  45. case XJSONLogEntryType:
  46. input.JSONEntry = entry.Leaf.TimestampedEntry.JSONEntry
  47. default:
  48. return nil, fmt.Errorf("unsupported entry type %s", entry.Leaf.TimestampedEntry.EntryType)
  49. }
  50. return tls.Marshal(input)
  51. default:
  52. return nil, fmt.Errorf("unknown SCT version %d", sct.SCTVersion)
  53. }
  54. }
  55. // SerializeSTHSignatureInput serializes the passed in STH into the correct
  56. // format for signing.
  57. func SerializeSTHSignatureInput(sth SignedTreeHead) ([]byte, error) {
  58. switch sth.Version {
  59. case V1:
  60. if len(sth.SHA256RootHash) != crypto.SHA256.Size() {
  61. return nil, fmt.Errorf("invalid TreeHash length, got %d expected %d", len(sth.SHA256RootHash), crypto.SHA256.Size())
  62. }
  63. input := TreeHeadSignature{
  64. Version: sth.Version,
  65. SignatureType: TreeHashSignatureType,
  66. Timestamp: sth.Timestamp,
  67. TreeSize: sth.TreeSize,
  68. SHA256RootHash: sth.SHA256RootHash,
  69. }
  70. return tls.Marshal(input)
  71. default:
  72. return nil, fmt.Errorf("unsupported STH version %d", sth.Version)
  73. }
  74. }
  75. // CreateX509MerkleTreeLeaf generates a MerkleTreeLeaf for an X509 cert
  76. func CreateX509MerkleTreeLeaf(cert ASN1Cert, timestamp uint64) *MerkleTreeLeaf {
  77. return &MerkleTreeLeaf{
  78. Version: V1,
  79. LeafType: TimestampedEntryLeafType,
  80. TimestampedEntry: &TimestampedEntry{
  81. Timestamp: timestamp,
  82. EntryType: X509LogEntryType,
  83. X509Entry: &cert,
  84. },
  85. }
  86. }
  87. // CreateJSONMerkleTreeLeaf creates the merkle tree leaf for json data.
  88. func CreateJSONMerkleTreeLeaf(data interface{}, timestamp uint64) *MerkleTreeLeaf {
  89. jsonData, err := json.Marshal(AddJSONRequest{Data: data})
  90. if err != nil {
  91. return nil
  92. }
  93. // Match the JSON serialization implemented by json-c
  94. jsonStr := strings.Replace(string(jsonData), ":", ": ", -1)
  95. jsonStr = strings.Replace(jsonStr, ",", ", ", -1)
  96. jsonStr = strings.Replace(jsonStr, "{", "{ ", -1)
  97. jsonStr = strings.Replace(jsonStr, "}", " }", -1)
  98. jsonStr = strings.Replace(jsonStr, "/", `\/`, -1)
  99. // TODO: Pending google/certificate-transparency#1243, replace with
  100. // ObjectHash once supported by CT server.
  101. return &MerkleTreeLeaf{
  102. Version: V1,
  103. LeafType: TimestampedEntryLeafType,
  104. TimestampedEntry: &TimestampedEntry{
  105. Timestamp: timestamp,
  106. EntryType: XJSONLogEntryType,
  107. JSONEntry: &JSONDataEntry{Data: []byte(jsonStr)},
  108. },
  109. }
  110. }
  111. // MerkleTreeLeafFromRawChain generates a MerkleTreeLeaf from a chain (in DER-encoded form) and timestamp.
  112. func MerkleTreeLeafFromRawChain(rawChain []ASN1Cert, etype LogEntryType, timestamp uint64) (*MerkleTreeLeaf, error) {
  113. // Need at most 3 of the chain
  114. count := 3
  115. if count > len(rawChain) {
  116. count = len(rawChain)
  117. }
  118. chain := make([]*x509.Certificate, count)
  119. for i := range chain {
  120. cert, err := x509.ParseCertificate(rawChain[i].Data)
  121. if x509.IsFatal(err) {
  122. return nil, fmt.Errorf("failed to parse chain[%d] cert: %v", i, err)
  123. }
  124. chain[i] = cert
  125. }
  126. return MerkleTreeLeafFromChain(chain, etype, timestamp)
  127. }
  128. // MerkleTreeLeafFromChain generates a MerkleTreeLeaf from a chain and timestamp.
  129. func MerkleTreeLeafFromChain(chain []*x509.Certificate, etype LogEntryType, timestamp uint64) (*MerkleTreeLeaf, error) {
  130. leaf := MerkleTreeLeaf{
  131. Version: V1,
  132. LeafType: TimestampedEntryLeafType,
  133. TimestampedEntry: &TimestampedEntry{
  134. EntryType: etype,
  135. Timestamp: timestamp,
  136. },
  137. }
  138. if etype == X509LogEntryType {
  139. leaf.TimestampedEntry.X509Entry = &ASN1Cert{Data: chain[0].Raw}
  140. return &leaf, nil
  141. }
  142. if etype != PrecertLogEntryType {
  143. return nil, fmt.Errorf("unknown LogEntryType %d", etype)
  144. }
  145. // Pre-certs are more complicated. First, parse the leaf pre-cert and its
  146. // putative issuer.
  147. if len(chain) < 2 {
  148. return nil, fmt.Errorf("no issuer cert available for precert leaf building")
  149. }
  150. issuer := chain[1]
  151. cert := chain[0]
  152. var preIssuer *x509.Certificate
  153. if IsPreIssuer(issuer) {
  154. // Replace the cert's issuance information with details from the pre-issuer.
  155. preIssuer = issuer
  156. // The issuer of the pre-cert is not going to be the issuer of the final
  157. // cert. Change to use the final issuer's key hash.
  158. if len(chain) < 3 {
  159. return nil, fmt.Errorf("no issuer cert available for pre-issuer")
  160. }
  161. issuer = chain[2]
  162. }
  163. // Next, post-process the DER-encoded TBSCertificate, to remove the CT poison
  164. // extension and possibly update the issuer field.
  165. defangedTBS, err := x509.BuildPrecertTBS(cert.RawTBSCertificate, preIssuer)
  166. if err != nil {
  167. return nil, fmt.Errorf("failed to remove poison extension: %v", err)
  168. }
  169. leaf.TimestampedEntry.EntryType = PrecertLogEntryType
  170. leaf.TimestampedEntry.PrecertEntry = &PreCert{
  171. IssuerKeyHash: sha256.Sum256(issuer.RawSubjectPublicKeyInfo),
  172. TBSCertificate: defangedTBS,
  173. }
  174. return &leaf, nil
  175. }
  176. // MerkleTreeLeafForEmbeddedSCT generates a MerkleTreeLeaf from a chain and an
  177. // SCT timestamp, where the leaf certificate at chain[0] is a certificate that
  178. // contains embedded SCTs. It is assumed that the timestamp provided is from
  179. // one of the SCTs embedded within the leaf certificate.
  180. func MerkleTreeLeafForEmbeddedSCT(chain []*x509.Certificate, timestamp uint64) (*MerkleTreeLeaf, error) {
  181. // For building the leaf for a certificate and SCT where the SCT is embedded
  182. // in the certificate, we need to build the original precertificate TBS
  183. // data. First, parse the leaf cert and its issuer.
  184. if len(chain) < 2 {
  185. return nil, fmt.Errorf("no issuer cert available for precert leaf building")
  186. }
  187. issuer := chain[1]
  188. cert := chain[0]
  189. // Next, post-process the DER-encoded TBSCertificate, to remove the SCTList
  190. // extension.
  191. tbs, err := x509.RemoveSCTList(cert.RawTBSCertificate)
  192. if err != nil {
  193. return nil, fmt.Errorf("failed to remove SCT List extension: %v", err)
  194. }
  195. return &MerkleTreeLeaf{
  196. Version: V1,
  197. LeafType: TimestampedEntryLeafType,
  198. TimestampedEntry: &TimestampedEntry{
  199. EntryType: PrecertLogEntryType,
  200. Timestamp: timestamp,
  201. PrecertEntry: &PreCert{
  202. IssuerKeyHash: sha256.Sum256(issuer.RawSubjectPublicKeyInfo),
  203. TBSCertificate: tbs,
  204. },
  205. },
  206. }, nil
  207. }
  208. // LeafHashForLeaf returns the leaf hash for a Merkle tree leaf.
  209. func LeafHashForLeaf(leaf *MerkleTreeLeaf) ([sha256.Size]byte, error) {
  210. leafData, err := tls.Marshal(*leaf)
  211. if err != nil {
  212. return [sha256.Size]byte{}, fmt.Errorf("failed to tls-encode MerkleTreeLeaf: %s", err)
  213. }
  214. data := append([]byte{TreeLeafPrefix}, leafData...)
  215. leafHash := sha256.Sum256(data)
  216. return leafHash, nil
  217. }
  218. // IsPreIssuer indicates whether a certificate is a pre-cert issuer with the specific
  219. // certificate transparency extended key usage.
  220. func IsPreIssuer(issuer *x509.Certificate) bool {
  221. for _, eku := range issuer.ExtKeyUsage {
  222. if eku == x509.ExtKeyUsageCertificateTransparency {
  223. return true
  224. }
  225. }
  226. return false
  227. }
  228. // RawLogEntryFromLeaf converts a LeafEntry object (which has the raw leaf data
  229. // after JSON parsing) into a RawLogEntry object (i.e. a TLS-parsed structure).
  230. func RawLogEntryFromLeaf(index int64, entry *LeafEntry) (*RawLogEntry, error) {
  231. ret := RawLogEntry{Index: index}
  232. if rest, err := tls.Unmarshal(entry.LeafInput, &ret.Leaf); err != nil {
  233. return nil, fmt.Errorf("failed to unmarshal MerkleTreeLeaf: %v", err)
  234. } else if len(rest) > 0 {
  235. return nil, fmt.Errorf("MerkleTreeLeaf: trailing data %d bytes", len(rest))
  236. }
  237. switch eType := ret.Leaf.TimestampedEntry.EntryType; eType {
  238. case X509LogEntryType:
  239. var certChain CertificateChain
  240. if rest, err := tls.Unmarshal(entry.ExtraData, &certChain); err != nil {
  241. return nil, fmt.Errorf("failed to unmarshal CertificateChain: %v", err)
  242. } else if len(rest) > 0 {
  243. return nil, fmt.Errorf("CertificateChain: trailing data %d bytes", len(rest))
  244. }
  245. ret.Cert = *ret.Leaf.TimestampedEntry.X509Entry
  246. ret.Chain = certChain.Entries
  247. case PrecertLogEntryType:
  248. var precertChain PrecertChainEntry
  249. if rest, err := tls.Unmarshal(entry.ExtraData, &precertChain); err != nil {
  250. return nil, fmt.Errorf("failed to unmarshal PrecertChainEntry: %v", err)
  251. } else if len(rest) > 0 {
  252. return nil, fmt.Errorf("PrecertChainEntry: trailing data %d bytes", len(rest))
  253. }
  254. ret.Cert = precertChain.PreCertificate
  255. ret.Chain = precertChain.CertificateChain
  256. default:
  257. // TODO(pavelkalinnikov): Section 4.6 of RFC6962 implies that unknown types
  258. // are not errors. We should revisit how we process this case.
  259. return nil, fmt.Errorf("unknown entry type: %v", eType)
  260. }
  261. return &ret, nil
  262. }
  263. // ToLogEntry converts RawLogEntry to a LogEntry, which includes an x509-parsed
  264. // (pre-)certificate.
  265. //
  266. // Note that this function may return a valid LogEntry object and a non-nil
  267. // error value, when the error indicates a non-fatal parsing error.
  268. func (rle *RawLogEntry) ToLogEntry() (*LogEntry, error) {
  269. var err error
  270. entry := LogEntry{Index: rle.Index, Leaf: rle.Leaf, Chain: rle.Chain}
  271. switch eType := rle.Leaf.TimestampedEntry.EntryType; eType {
  272. case X509LogEntryType:
  273. entry.X509Cert, err = rle.Leaf.X509Certificate()
  274. if x509.IsFatal(err) {
  275. return nil, fmt.Errorf("failed to parse certificate: %v", err)
  276. }
  277. case PrecertLogEntryType:
  278. var tbsCert *x509.Certificate
  279. tbsCert, err = rle.Leaf.Precertificate()
  280. if x509.IsFatal(err) {
  281. return nil, fmt.Errorf("failed to parse precertificate: %v", err)
  282. }
  283. entry.Precert = &Precertificate{
  284. Submitted: rle.Cert,
  285. IssuerKeyHash: rle.Leaf.TimestampedEntry.PrecertEntry.IssuerKeyHash,
  286. TBSCertificate: tbsCert,
  287. }
  288. default:
  289. return nil, fmt.Errorf("unknown entry type: %v", eType)
  290. }
  291. // err may be non-nil for a non-fatal error.
  292. return &entry, err
  293. }
  294. // LogEntryFromLeaf converts a LeafEntry object (which has the raw leaf data
  295. // after JSON parsing) into a LogEntry object (which includes x509.Certificate
  296. // objects, after TLS and ASN.1 parsing).
  297. //
  298. // Note that this function may return a valid LogEntry object and a non-nil
  299. // error value, when the error indicates a non-fatal parsing error.
  300. func LogEntryFromLeaf(index int64, leaf *LeafEntry) (*LogEntry, error) {
  301. rle, err := RawLogEntryFromLeaf(index, leaf)
  302. if err != nil {
  303. return nil, err
  304. }
  305. return rle.ToLogEntry()
  306. }
  307. // TimestampToTime converts a timestamp in the style of RFC 6962 (milliseconds
  308. // since UNIX epoch) to a Go Time.
  309. func TimestampToTime(ts uint64) time.Time {
  310. secs := int64(ts / 1000)
  311. msecs := int64(ts % 1000)
  312. return time.Unix(secs, msecs*1000000)
  313. }