gen_cert_header.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import binascii
  5. def _file_byte_generator(filename):
  6. with open(filename, "rb") as f:
  7. contents = f.read()
  8. # Treat empty files the same as a file containing a lone 0;
  9. # a single-element array will fail cert verifcation just as an
  10. # empty array would.
  11. if not contents:
  12. return ['\0']
  13. return contents
  14. def _create_header(array_name, cert_bytes):
  15. hexified = ["0x" + binascii.hexlify(byte) for byte in cert_bytes]
  16. substs = { 'array_name': array_name, 'bytes': ', '.join(hexified) }
  17. return "const uint8_t %(array_name)s[] = {\n%(bytes)s\n};\n" % substs
  18. # Create functions named the same as the data arrays that we're going to
  19. # write to the headers, so we don't have to duplicate the names like so:
  20. #
  21. # def arrayName(header, cert_filename):
  22. # header.write(_create_header("arrayName", cert_filename))
  23. array_names = [
  24. 'marketplaceProdPublicRoot',
  25. 'marketplaceProdReviewersRoot',
  26. 'marketplaceDevPublicRoot',
  27. 'marketplaceDevReviewersRoot',
  28. 'marketplaceStageRoot',
  29. 'trustedAppPublicRoot',
  30. 'trustedAppTestRoot',
  31. 'xpcshellRoot',
  32. 'addonsPublicRoot',
  33. 'addonsStageRoot',
  34. 'privilegedPackageRoot',
  35. ]
  36. for n in array_names:
  37. # Make sure the lambda captures the right string.
  38. globals()[n] = lambda header, cert_filename, name=n: header.write(_create_header(name, _file_byte_generator(cert_filename)))