| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2015 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 """Set of helpers to generate signed X.509v3 certificates. | 6 """Set of helpers to generate signed X.509v3 certificates. |
| 7 | 7 |
| 8 This works by shelling out calls to the 'openssl req' and 'openssl ca' | 8 This works by shelling out calls to the 'openssl req' and 'openssl ca' |
| 9 commands, and passing the appropriate command line flags and configuration file | 9 commands, and passing the appropriate command line flags and configuration file |
| 10 (.cnf). | 10 (.cnf). |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 # January 1st, 2016 12:00 UTC | 36 # January 1st, 2016 12:00 UTC |
| 37 JANUARY_1_2016_UTC = '160101120000Z' | 37 JANUARY_1_2016_UTC = '160101120000Z' |
| 38 | 38 |
| 39 # January 1st, 2021 12:00 UTC | 39 # January 1st, 2021 12:00 UTC |
| 40 JANUARY_1_2021_UTC = '210101120000Z' | 40 JANUARY_1_2021_UTC = '210101120000Z' |
| 41 | 41 |
| 42 # The default time tests should use when verifying. | 42 # The default time tests should use when verifying. |
| 43 DEFAULT_TIME = MARCH_2_2015_UTC | 43 DEFAULT_TIME = MARCH_2_2015_UTC |
| 44 | 44 |
| 45 KEY_PURPOSE_ANY = 'anyExtendedKeyUsage' |
| 46 KEY_PURPOSE_SERVER_AUTH = 'serverAuth' |
| 47 KEY_PURPOSE_CLIENT_AUTH = 'clientAuth' |
| 48 |
| 49 DEFAULT_KEY_PURPOSE = KEY_PURPOSE_SERVER_AUTH |
| 50 |
| 45 # Counters used to generate unique (but readable) path names. | 51 # Counters used to generate unique (but readable) path names. |
| 46 g_cur_path_id = {} | 52 g_cur_path_id = {} |
| 47 | 53 |
| 48 # Output paths used: | 54 # Output paths used: |
| 49 # - g_out_dir: where any temporary files (keys, cert req, signing db etc) are | 55 # - g_out_dir: where any temporary files (keys, cert req, signing db etc) are |
| 50 # saved to. | 56 # saved to. |
| 51 # - g_out_pem: the path to the final output (which is a .pem file) | 57 # - g_out_pem: the path to the final output (which is a .pem file) |
| 52 # | 58 # |
| 53 # See init() for how these are assigned, based on the name of the calling | 59 # See init() for how these are assigned, based on the name of the calling |
| 54 # script. | 60 # script. |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 450 |
| 445 cert_data = self.cert.get_cert_pem() | 451 cert_data = self.cert.get_cert_pem() |
| 446 block_name = 'TRUST_ANCHOR_UNCONSTRAINED' | 452 block_name = 'TRUST_ANCHOR_UNCONSTRAINED' |
| 447 if self.constrained: | 453 if self.constrained: |
| 448 block_name = 'TRUST_ANCHOR_CONSTRAINED' | 454 block_name = 'TRUST_ANCHOR_CONSTRAINED' |
| 449 | 455 |
| 450 # Use a different block name in the .pem file, depending on the anchor type. | 456 # Use a different block name in the .pem file, depending on the anchor type. |
| 451 return cert_data.replace('CERTIFICATE', block_name) | 457 return cert_data.replace('CERTIFICATE', block_name) |
| 452 | 458 |
| 453 | 459 |
| 454 def write_test_file(description, chain, trust_anchor, utc_time, verify_result, | 460 def write_test_file(description, chain, trust_anchor, utc_time, key_purpose, |
| 455 errors, out_pem=None): | 461 verify_result, errors, out_pem=None): |
| 456 """Writes a test file that contains all the inputs necessary to run a | 462 """Writes a test file that contains all the inputs necessary to run a |
| 457 verification on a certificate chain""" | 463 verification on a certificate chain.""" |
| 458 | 464 |
| 459 # Prepend the script name that generated the file to the description. | 465 # Prepend the script name that generated the file to the description. |
| 460 test_data = '[Created by: %s]\n\n%s\n' % (sys.argv[0], description) | 466 test_data = '[Created by: %s]\n\n%s\n' % (sys.argv[0], description) |
| 461 | 467 |
| 462 # Write the certificate chain to the output file. | 468 # Write the certificate chain to the output file. |
| 463 for cert in chain: | 469 for cert in chain: |
| 464 test_data += '\n' + cert.get_cert_pem() | 470 test_data += '\n' + cert.get_cert_pem() |
| 465 | 471 |
| 466 test_data += '\n' + trust_anchor.get_pem() | 472 test_data += '\n' + trust_anchor.get_pem() |
| 467 test_data += '\n' + text_data_to_pem('TIME', utc_time) | 473 test_data += '\n' + text_data_to_pem('TIME', utc_time) |
| 468 | 474 |
| 469 verify_result_string = 'SUCCESS' if verify_result else 'FAIL' | 475 verify_result_string = 'SUCCESS' if verify_result else 'FAIL' |
| 470 test_data += '\n' + text_data_to_pem('VERIFY_RESULT', verify_result_string) | 476 test_data += '\n' + text_data_to_pem('VERIFY_RESULT', verify_result_string) |
| 471 | 477 |
| 478 test_data += '\n' + text_data_to_pem('KEY_PURPOSE', key_purpose) |
| 479 |
| 472 if errors is not None: | 480 if errors is not None: |
| 473 test_data += '\n' + text_data_to_pem('ERRORS', errors) | 481 test_data += '\n' + text_data_to_pem('ERRORS', errors) |
| 474 | 482 |
| 475 write_string_to_file(test_data, out_pem if out_pem else g_out_pem) | 483 write_string_to_file(test_data, out_pem if out_pem else g_out_pem) |
| 476 | 484 |
| 477 | 485 |
| 478 def write_string_to_file(data, path): | 486 def write_string_to_file(data, path): |
| 479 with open(path, 'w') as f: | 487 with open(path, 'w') as f: |
| 480 f.write(data) | 488 f.write(data) |
| 481 | 489 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 512 | 520 |
| 513 | 521 |
| 514 def create_intermediate_certificate(name, issuer): | 522 def create_intermediate_certificate(name, issuer): |
| 515 return Certificate(name, TYPE_CA, issuer) | 523 return Certificate(name, TYPE_CA, issuer) |
| 516 | 524 |
| 517 | 525 |
| 518 def create_end_entity_certificate(name, issuer): | 526 def create_end_entity_certificate(name, issuer): |
| 519 return Certificate(name, TYPE_END_ENTITY, issuer) | 527 return Certificate(name, TYPE_END_ENTITY, issuer) |
| 520 | 528 |
| 521 init(sys.argv[0]) | 529 init(sys.argv[0]) |
| OLD | NEW |