OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/cert/ct_signed_certificate_timestamp_log_param.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/base64.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/values.h" | |
14 #include "net/cert/ct_verify_result.h" | |
15 #include "net/cert/signed_certificate_timestamp.h" | |
16 | |
17 namespace net { | |
18 | |
19 namespace { | |
20 | |
21 // Converts a numeric |origin| to text describing the SCT's origin | |
22 std::string OriginToString(ct::SignedCertificateTimestamp::Origin origin) { | |
eroman
2013/11/27 22:33:46
[optional] Since this only returns string literals
Eran M. (Google)
2013/11/29 11:14:44
Done.
| |
23 switch (origin) { | |
24 case ct::SignedCertificateTimestamp::SCT_EMBEDDED: | |
25 return "embedded_in_certificate"; | |
26 case ct::SignedCertificateTimestamp::SCT_FROM_TLS_EXTENSION: | |
27 return "tls_extension"; | |
28 case ct::SignedCertificateTimestamp::SCT_FROM_OCSP_RESPONSE: | |
29 return "ocsp"; | |
30 } | |
31 | |
32 return "unknown"; | |
33 } | |
34 | |
35 // Converts a numeric |hash_algorithm| to its textual representation | |
36 std::string HashAlgorithmToString( | |
eroman
2013/11/27 22:33:46
Same comment as above.
Eran M. (Google)
2013/11/29 11:14:44
Done.
| |
37 ct::DigitallySigned::HashAlgorithm hash_algorithm) { | |
38 switch (hash_algorithm) { | |
39 case ct::DigitallySigned::HASH_ALGO_NONE: | |
40 return "NONE"; | |
41 case ct::DigitallySigned::HASH_ALGO_MD5: | |
42 return "MD5"; | |
43 case ct::DigitallySigned::HASH_ALGO_SHA1: | |
44 return "SHA1"; | |
45 case ct::DigitallySigned::HASH_ALGO_SHA224: | |
46 return "SHA224"; | |
47 case ct::DigitallySigned::HASH_ALGO_SHA256: | |
48 return "SHA256"; | |
49 case ct::DigitallySigned::HASH_ALGO_SHA384: | |
50 return "SHA384"; | |
51 case ct::DigitallySigned::HASH_ALGO_SHA512: | |
52 return "SHA512"; | |
53 } | |
54 | |
55 return "unknown"; | |
56 } | |
57 | |
58 // Converts a numeric |signature_algorithm| to its textual representation | |
59 std::string SignatureAlgorithmToString( | |
eroman
2013/11/27 22:33:46
Same comment as above.
Eran M. (Google)
2013/11/29 11:14:44
Done.
| |
60 ct::DigitallySigned::SignatureAlgorithm signature_algorithm) { | |
61 switch (signature_algorithm) { | |
62 case ct::DigitallySigned::SIG_ALGO_ANONYMOUS: | |
63 return "ANONYMOUS"; | |
64 case ct::DigitallySigned::SIG_ALGO_RSA: | |
65 return "RSA"; | |
66 case ct::DigitallySigned::SIG_ALGO_DSA: | |
67 return "DSA"; | |
68 case ct::DigitallySigned::SIG_ALGO_ECDSA: | |
69 return "ECDSA"; | |
70 } | |
71 | |
72 return "unknown"; | |
73 } | |
74 | |
75 // Base64 encode the given |data| string and put it in |dict| with the | |
eroman
2013/11/27 22:33:46
s/data/value.
Eran M. (Google)
2013/11/29 11:14:44
Done.
Eran M. (Google)
2013/11/29 11:14:44
Done.
| |
76 // description |description|. | |
77 void SetBinaryData( | |
78 const char* key, | |
79 const std::string& value, | |
80 base::DictionaryValue* dict) { | |
81 std::string b64_value; | |
82 base::Base64Encode(value, &b64_value); | |
83 | |
84 dict->SetString(key, b64_value); | |
85 } | |
86 | |
eroman
2013/11/27 22:33:46
delete blank line.
Eran M. (Google)
2013/11/29 11:14:44
Done.
| |
87 | |
88 // Returns a dictionary where each key is a field of the SCT and its value | |
89 // is this field's value in the SCT. This dictionary is meant to be used for | |
90 // outputting a de-serialized SCT to the NetLog. | |
91 base::DictionaryValue* SCTToDictionary( | |
92 const ct::SignedCertificateTimestamp& sct) { | |
93 base::DictionaryValue* out = new base::DictionaryValue(); | |
94 | |
95 out->SetString("origin", OriginToString(sct.origin)); | |
96 out->SetInteger("version", sct.version); | |
97 | |
98 SetBinaryData("log_id", sct.log_id, out); | |
99 base::TimeDelta time_since_unix_epoch = | |
100 sct.timestamp - base::Time::UnixEpoch(); | |
101 out->SetString("timestamp", | |
102 base::Int64ToString(time_since_unix_epoch.InMilliseconds())); | |
103 SetBinaryData("extensions", sct.extensions, out); | |
104 | |
105 out->SetString("hash_algorithm", | |
106 HashAlgorithmToString(sct.signature.hash_algorithm)); | |
107 out->SetString("signature_algorithm", | |
108 SignatureAlgorithmToString(sct.signature.signature_algorithm)); | |
109 SetBinaryData( | |
110 "signature_data", sct.signature.signature_data, out); | |
111 | |
112 return out; | |
113 } | |
114 | |
115 // Given a list of SCTs, return a ListValue instance where each item in the | |
116 // list is a dictionary created by SCTToDictionary. | |
117 base::ListValue* SCTListToPrintableValues( | |
118 const ct::SCTList& sct_list) { | |
119 base::ListValue* output_scts = new base::ListValue(); | |
120 for (ct::SCTList::const_iterator it = sct_list.begin(); | |
121 it != sct_list.end(); | |
122 ++it) | |
123 output_scts->Append(SCTToDictionary(*(it->get()))); | |
124 | |
125 return output_scts; | |
126 } | |
127 | |
128 } // namespace | |
129 | |
130 base::Value* NetLogSignedCertificateTimestampCallback( | |
131 const ct::CTVerifyResult* ct_result, NetLog::LogLevel log_level) { | |
132 base::DictionaryValue* dict = new base::DictionaryValue(); | |
133 | |
134 dict->Set("verified_scts", | |
135 SCTListToPrintableValues(ct_result->verified_scts)); | |
136 | |
137 dict->Set("unverified_scts", | |
138 SCTListToPrintableValues(ct_result->unverified_scts)); | |
139 | |
140 dict->Set("unknown_logs_scts", | |
141 SCTListToPrintableValues(ct_result->unknown_logs_scts)); | |
142 | |
143 return dict; | |
144 } | |
145 | |
146 base::Value* NetLogRawSignedCertificateTimestampCallback( | |
147 const std::string* embedded_scts, | |
148 const std::string* sct_list_from_ocsp, | |
149 const std::string* sct_list_from_tls_extension, | |
150 NetLog::LogLevel log_level) { | |
151 base::DictionaryValue* dict = new base::DictionaryValue(); | |
152 | |
153 SetBinaryData("embedded_scts", *embedded_scts, dict); | |
154 SetBinaryData("scts_from_ocsp_response", *sct_list_from_ocsp, dict); | |
155 SetBinaryData("scts_from_tls_extension", *sct_list_from_tls_extension, dict); | |
156 | |
157 return dict; | |
158 } | |
159 | |
160 } // namespace net | |
OLD | NEW |