File: generate_sslroots.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (244 lines) | stat: -rw-r--r-- 9,322 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env vpython3

# -*- coding:utf-8 -*-
# Copyright (c) 2023 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
import argparse
import logging
from pathlib import Path
import tempfile
from typing import Tuple, Any, List, ByteString
from datetime import datetime, timezone
from hashlib import sha256
from urllib.request import urlopen
from asn1crypto import pem, x509

_GENERATED_FILE = 'ssl_roots.h'

def main():
  parser = argparse.ArgumentParser(
      description='This is a tool to transform a crt file '
      f'into a C/C++ header: {_GENERATED_FILE}.')

  parser.add_argument('source_path_or_url',
                      help='File path or URL to PEM storage file. '
                      'The supported cert files are: '
                      '- Google: https://pki.goog/roots.pem; '
                      '- Mozilla: https://curl.se/ca/cacert.pem')
  parser.add_argument('-v',
                      '--verbose',
                      dest='verbose',
                      action='store_true',
                      help='Print output while running')
  parser.add_argument('-f',
                      '--full_cert',
                      dest='full_cert',
                      action='store_true',
                      help='Add public key and certificate name. '
                      'Default is to skip and reduce generated file size.')
  args = parser.parse_args()
  logging.basicConfig(level=logging.DEBUG if args.verbose else logging.WARNING)

  with tempfile.TemporaryDirectory() as temp_dir:
    cert_file = Path(temp_dir) / "cacert.pem"

    if args.source_path_or_url.startswith(
        'https://') or args.source_path_or_url.startswith('http://'):
      _DownloadCertificatesStore(args.source_path_or_url, cert_file)
      destination_dir = Path.cwd()
    else:
      source_path = Path(args.source_path_or_url)
      cert_file.write_bytes(source_path.read_bytes())
      destination_dir = source_path.parent

    logging.debug('Stored certificate from %s into %s', args.source_path_or_url,
                  cert_file)

    header_file = destination_dir / _GENERATED_FILE

    digest, certificates = _LoadCertificatesStore(cert_file)
    _GenerateCHeader(header_file, args.source_path_or_url, digest, certificates,
                     args.full_cert)

    logging.debug('Did generate %s from %s [%s]', header_file,
                  args.source_path_or_url, digest)


def _DownloadCertificatesStore(pem_url: str, destination_file: Path):
  with urlopen(pem_url) as response:
    pem_file = response.read()
    logging.info('Got response with status [%d]: %s', response.status, pem_url)

  if destination_file.parent.exists():
    logging.debug('Creating directory and it\'s parents %s',
                  destination_file.parent)
    destination_file.parent.mkdir(parents=True, exist_ok=True)
  if destination_file.exists():
    logging.debug('Unlink existing file %s', destination_file)
    destination_file.unlink(missing_ok=True)

  destination_file.write_bytes(pem_file)
  logging.info('Stored downloaded %d bytes pem file to `%s`', len(pem_file),
               destination_file)


def _LoadCertificatesStore(
    source_file: Path) -> Tuple[str, List[x509.Certificate]]:
  pem_bytes = source_file.read_bytes()

  certificates = [
      x509.Certificate.load(der)
      for type, _, der in pem.unarmor(pem_bytes, True) if type == 'CERTIFICATE'
  ]
  digest = f'sha256:{sha256(pem_bytes).hexdigest()}'
  logging.debug('Loaded %d certificates from %s [%s] ', len(certificates),
                source_file, digest)
  return digest, certificates


def _GenerateCHeader(header_file: Path, source: str, source_digest: str,
                     certificates: List[x509.Certificate], full_cert: bool):
  header_file.parent.mkdir(parents=True, exist_ok=True)
  with header_file.open('w') as output:
    output.write(_CreateOutputHeader(source, source_digest))

    named_certificates = [(cert,
                           f'kCertificateWithFingerprint_{cert.sha256.hex()}')
                          for cert in certificates]

    names = list(map(lambda x: x[1], named_certificates))
    unique_names = list(set(names))
    if len(names) != len(unique_names):
      raise RuntimeError(
          f'There are {len(names) - len(unique_names)} non-unique '
          'certificate names generated. Generator script must be '
          'fixed to handle collision.')

    for cert, name in named_certificates:

      output.write(_CreateCertificateMetadataHeader(cert))

      if full_cert:
        output.write(
            _CArrayConstantDefinition('unsigned char',
                                      f'{name}_subject_name',
                                      _CreateHexList(cert.subject.dump()),
                                      max_items_per_line=16))
        output.write('\n')
        output.write(
            _CArrayConstantDefinition('unsigned char',
                                      f'{name}_public_key',
                                      _CreateHexList(cert.public_key.dump()),
                                      max_items_per_line=16))
        output.write('\n')

      output.write(
          _CArrayConstantDefinition('unsigned char',
                                    f'{name}_certificate',
                                    _CreateHexList(cert.dump()),
                                    max_items_per_line=16))
      output.write('\n\n')

    if full_cert:
      output.write(
          _CArrayConstantDefinition('unsigned char* const',
                                    'kSSLCertSubjectNameList',
                                    [f'{name}_subject_name' for name in names]))
      output.write('\n\n')

      output.write(
          _CArrayConstantDefinition('unsigned char* const',
                                    'kSSLCertPublicKeyList',
                                    [f'{name}_public_key' for name in names]))
      output.write('\n\n')

    output.write(
        _CArrayConstantDefinition('unsigned char* const',
                                  'kSSLCertCertificateList',
                                  [f'{name}_certificate' for name in names]))
    output.write('\n\n')

    output.write(
        _CArrayConstantDefinition(
            'size_t', 'kSSLCertCertificateSizeList',
            [f'{len(cert.dump())}' for cert, _ in named_certificates]))
    output.write('\n\n')

    output.write(_CreateOutputFooter())


def _CreateHexList(items: ByteString) -> List[str]:
  """
  Produces list of strings each item is hex literal of byte of source sequence
  """
  return [f'0x{item:02X}' for item in items]


def _CArrayConstantDefinition(type_name: str,
                              array_name: str,
                              items: List[Any],
                              max_items_per_line: int = 1) -> str:
  """
  Produces C array definition like: `const type_name array_name = { items };`
  """
  return (f'const {type_name} {array_name}[{len(items)}]='
          f'{_CArrayInitializerList(items, max_items_per_line)};')


def _CArrayInitializerList(items: List[Any],
                           max_items_per_line: int = 1) -> str:
  """
  Produces C initializer list like: `{\\nitems[0], \\n ...}`
  """
  return '{\n' + '\n'.join([
      ','.join(items[i:i + max_items_per_line]) + ','
      for i in range(0, len(items), max_items_per_line)
  ]) + '\n}'


def _CreateCertificateMetadataHeader(cert: x509.Certificate) -> str:
  return (f'/* subject: {cert.subject.human_friendly} */\n'
          f'/* issuer: {cert.issuer.human_friendly} */\n'
          f'/* link: https://crt.sh/?q={cert.sha256.hex()} */\n')


def _CreateOutputHeader(source_path_or_url: str, source_digest: str) -> str:
  now_utc = datetime.now(timezone.utc).replace(microsecond=0)
  output = (
      '/*\n'
      f' *  Copyright {now_utc.year} The WebRTC Project Authors. All rights '
      'reserved.\n'
      ' *\n'
      ' *  Use of this source code is governed by a BSD-style license\n'
      ' *  that can be found in the LICENSE file in the root of the '
      'source\n'
      ' *  tree. An additional intellectual property rights grant can be '
      'found\n'
      ' *  in the file PATENTS.  All contributing project authors may\n'
      ' *  be found in the AUTHORS file in the root of the source tree.\n'
      ' */\n\n'
      '#ifndef RTC_BASE_SSL_ROOTS_H_\n'
      '#define RTC_BASE_SSL_ROOTS_H_\n\n'
      '// This file is the root certificates in C form.\n\n'
      f'// It was generated at {now_utc.isoformat()} by the following script:\n'
      '// `tools_webrtc/sslroots/generate_sslroots.py '
      f'{source_path_or_url}`\n\n'
      '// clang-format off\n'
      '// Don\'t bother formatting generated code,\n'
      '// also it would breaks subject/issuer lines.\n\n'
      f'// Source bundle `{source_path_or_url}` digest is [{source_digest}]\n\n'
  )
  return output


def _CreateOutputFooter():
  return '// clang-format on\n\n#endif  // RTC_BASE_SSL_ROOTS_H_\n'


if __name__ == '__main__':
  main()