OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 This utility takes a JSON input that describes a CRLSet and produces a | 7 This utility takes a JSON input that describes a CRLSet and produces a |
8 CRLSet from it. | 8 CRLSet from it. |
9 | 9 |
10 The input is taken on stdin and is a dict with the following keys: | 10 The input is taken on stdin and is a dict with the following keys: |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 iterator.step_into() # enter TBSCertificate | 131 iterator.step_into() # enter TBSCertificate |
132 iterator.step_over() # over version | 132 iterator.step_over() # over version |
133 iterator.step_over() # over serial | 133 iterator.step_over() # over serial |
134 iterator.step_over() # over signature algorithm | 134 iterator.step_over() # over signature algorithm |
135 iterator.step_over() # over issuer name | 135 iterator.step_over() # over issuer name |
136 iterator.step_over() # over validity | 136 iterator.step_over() # over validity |
137 iterator.step_over() # over subject name | 137 iterator.step_over() # over subject name |
138 return iterator.contents() | 138 return iterator.contents() |
139 | 139 |
140 | 140 |
| 141 def der_cert_to_spki_hash(der_cert): |
| 142 """Gets the SHA-256 hash of the subjectPublicKeyInfo of a DER encoded cert |
| 143 |
| 144 Args: |
| 145 der_cert: A string containing the DER-encoded certificate |
| 146 |
| 147 Returns: |
| 148 The SHA-256 hash of the certificate, as a byte sequence |
| 149 """ |
| 150 return hashlib.sha256(_der_cert_to_spki(der_cert)).digest() |
| 151 |
| 152 |
141 def pem_cert_file_to_spki_hash(pem_filename): | 153 def pem_cert_file_to_spki_hash(pem_filename): |
142 """Gets the SHA-256 hash of the subjectPublicKeyInfo of a cert in a file | 154 """Gets the SHA-256 hash of the subjectPublicKeyInfo of a cert in a file |
143 | 155 |
144 Args: | 156 Args: |
145 pem_filename: A file containing a PEM-encoded certificate. | 157 pem_filename: A file containing a PEM-encoded certificate. |
146 | 158 |
147 Returns: | 159 Returns: |
148 The SHA-256 hash of the first certificate in the file, as a byte sequence | 160 The SHA-256 hash of the first certificate in the file, as a byte sequence |
149 """ | 161 """ |
150 return hashlib.sha256( | 162 return der_cert_to_spki_hash(_pem_cert_to_binary(pem_filename)) |
151 _der_cert_to_spki(_pem_cert_to_binary(pem_filename))).digest() | |
152 | 163 |
153 | 164 |
154 def main(): | 165 def main(): |
155 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) | 166 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) |
156 parser.add_option('-o', '--output', | 167 parser.add_option('-o', '--output', |
157 help='Specifies the output file. The default is stdout.') | 168 help='Specifies the output file. The default is stdout.') |
158 options, _ = parser.parse_args() | 169 options, _ = parser.parse_args() |
159 outfile = sys.stdout | 170 outfile = sys.stdout |
160 if options.output and options.output != '-': | 171 if options.output and options.output != '-': |
161 outfile = open(options.output, 'wb') | 172 outfile = open(options.output, 'wb') |
(...skipping 29 matching lines...) Expand all Loading... |
191 raw_serial.insert(0, chr(serial & 0xff)) | 202 raw_serial.insert(0, chr(serial & 0xff)) |
192 serial >>= 8 | 203 serial >>= 8 |
193 | 204 |
194 outfile.write(struct.pack('<B', len(raw_serial))) | 205 outfile.write(struct.pack('<B', len(raw_serial))) |
195 outfile.write(''.join(raw_serial)) | 206 outfile.write(''.join(raw_serial)) |
196 return 0 | 207 return 0 |
197 | 208 |
198 | 209 |
199 if __name__ == '__main__': | 210 if __name__ == '__main__': |
200 sys.exit(main()) | 211 sys.exit(main()) |
OLD | NEW |