File: generate-chains.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (175 lines) | stat: -rwxr-xr-x 7,922 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import sys
sys.path += ['../..']

import gencerts

def add_excluded_name_constraints(cert, num_dns, num_ip, num_dirnames, num_uri):
  cert.get_extensions().set_property('nameConstraints', '@nameConstraints_info')
  constraints = cert.config.get_section('nameConstraints_info')
  for i in range(num_dns):
    constraints.set_property('excluded;DNS.%i' % (i + 1), 'x%i.test' % i)
  for i in range(num_ip):
    b,c = divmod(i, 256)
    a,b = divmod(b, 256)
    constraints.set_property('excluded;IP.%i' % (i + 1),
                             '11.%i.%i.%i/255.255.255.255' % (a, b, c))
  for i in range(num_dirnames):
    section_name = 'nameConstraints_dirname_x%i' % (i + 1)
    dirname = cert.config.get_section(section_name)
    dirname.set_property('commonName', '"x%i' % i)
    constraints.set_property('excluded;dirName.%i' % (i + 1), section_name)
  for i in range(num_uri):
    constraints.set_property('excluded;URI.%i' % (i + 1), 'http://xest/%i' % i)


def add_permitted_name_constraints(
    cert, num_dns, num_ip, num_dirnames, num_uri):
  cert.get_extensions().set_property('nameConstraints', '@nameConstraints_info')
  constraints = cert.config.get_section('nameConstraints_info')
  for i in range(num_dns):
    constraints.set_property('permitted;DNS.%i' % (i + 1), 't%i.test' % i)
  for i in range(num_ip):
    b,c = divmod(i, 256)
    a,b = divmod(b, 256)
    constraints.set_property('permitted;IP.%i' % (i + 1),
                             '10.%i.%i.%i/255.255.255.255' % (a, b, c))
  for i in range(num_dirnames):
    section_name = 'nameConstraints_dirname_p%i' % (i + 1)
    dirname = cert.config.get_section(section_name)
    dirname.set_property('commonName', '"t%i' % i)
    constraints.set_property('permitted;dirName.%i' % (i + 1), section_name)
  for i in range(num_uri):
    constraints.set_property('permitted;URI.%i' % (i + 1),
                               'http://test/%i' % i)


def add_sans(cert, num_dns, num_ip, num_dirnames, num_uri):
  cert.get_extensions().set_property('subjectAltName', '@san_info')
  sans = cert.config.get_section('san_info')
  for i in range(num_dns):
    sans.set_property('DNS.%i' % (i + 1), 't%i.test' % i)
  for i in range(num_ip):
    b,c = divmod(i, 256)
    a,b = divmod(b, 256)
    sans.set_property('IP.%i' % (i + 1), '10.%i.%i.%i' % (a, b, c))
  for i in range(num_dirnames):
    section_name = 'san_dirname%i' % (i + 1)
    dirname = cert.config.get_section(section_name)
    dirname.set_property('commonName', '"t%i' % i)
    sans.set_property('dirName.%i' % (i + 1), section_name)
  for i in range(num_uri):
    sans.set_property('URI.%i' % (i + 1), 'http://test/%i' % i)


# Self-signed root certificate.
root = gencerts.create_self_signed_root_certificate('Root')

# Use the same keys for all the chains. Fewer key files to check in, and also
# gives stability against re-ordering of the calls to |make_chain|.
intermediate_key = gencerts.get_or_generate_rsa_key(
    2048, gencerts.create_key_path('Intermediate'))
target_key = gencerts.get_or_generate_rsa_key(
    2048, gencerts.create_key_path('t0'))

def make_chain(name, doc, excluded, permitted, sans):
  # Intermediate certificate.
  intermediate = gencerts.create_intermediate_certificate('Intermediate', root)
  intermediate.set_key(intermediate_key)
  add_excluded_name_constraints(intermediate, **excluded)
  add_permitted_name_constraints(intermediate, **permitted)

  # Target certificate.
  target = gencerts.create_end_entity_certificate('t0', intermediate)
  target.set_key(target_key)
  add_sans(target, **sans)

  chain = [target, intermediate, root]
  gencerts.write_chain(doc, chain, '%s.pem' % name)


make_chain(
    'ok-all-types',
    "A chain containing a large number of name constraints and names,\n"
    "but below the limit.",
    excluded=dict(num_dns=418, num_ip=418, num_dirnames=418, num_uri=1025),
    permitted=dict(num_dns=418, num_ip=418, num_dirnames=418, num_uri=1025),
    sans=dict(num_dns=418, num_ip=418, num_dirnames=417, num_uri=1025))

make_chain(
    'toomany-all-types',
    "A chain containing a large number of different types of name\n"
    "constraints and names, above the limit.",
    excluded=dict(num_dns=419, num_ip=419, num_dirnames=419, num_uri=0),
    permitted=dict(num_dns=419, num_ip=419, num_dirnames=419, num_uri=0),
    sans=dict(num_dns=419, num_ip=419, num_dirnames=418, num_uri=0))

make_chain(
    'toomany-dns-excluded',
    "A chain containing a large number of excluded DNS name\n"
    "constraints and DNS names, above the limit.",
    excluded=dict(num_dns=1025, num_ip=0, num_dirnames=0, num_uri=0),
    permitted=dict(num_dns=0, num_ip=0, num_dirnames=0, num_uri=0),
    sans=dict(num_dns=1024, num_ip=0, num_dirnames=0, num_uri=0))
make_chain(
    'toomany-ips-excluded',
    "A chain containing a large number of excluded IP name\n"
    "constraints and IP names, above the limit.",
    excluded=dict(num_dns=0, num_ip=1025, num_dirnames=0, num_uri=0),
    permitted=dict(num_dns=0, num_ip=0, num_dirnames=0, num_uri=0),
    sans=dict(num_dns=0, num_ip=1024, num_dirnames=0, num_uri=0))
make_chain(
    'toomany-dirnames-excluded',
    "A chain containing a large number of excluded directory name\n"
    "constraints and directory names, above the limit.",
    excluded=dict(num_dns=0, num_ip=0, num_dirnames=1025, num_uri=0),
    permitted=dict(num_dns=0, num_ip=0, num_dirnames=0, num_uri=0),
    sans=dict(num_dns=0, num_ip=0, num_dirnames=1024, num_uri=0))

make_chain(
    'toomany-dns-permitted',
    "A chain containing a large number of permitted DNS name\n"
    "constraints and DNS names, above the limit.",
    excluded=dict(num_dns=0, num_ip=0, num_dirnames=0, num_uri=0),
    permitted=dict(num_dns=1025, num_ip=0, num_dirnames=0, num_uri=0),
    sans=dict(num_dns=1024, num_ip=0, num_dirnames=0, num_uri=0))
make_chain(
    'toomany-ips-permitted',
    "A chain containing a large number of permitted IP name\n"
    "constraints and IP names, above the limit.",
    excluded=dict(num_dns=0, num_ip=0, num_dirnames=0, num_uri=0),
    permitted=dict(num_dns=0, num_ip=1025, num_dirnames=0, num_uri=0),
    sans=dict(num_dns=0, num_ip=1024, num_dirnames=0, num_uri=0))
make_chain(
    'toomany-dirnames-permitted',
    "A chain containing a large number of permitted directory name\n"
    "constraints and directory names, above the limit.",
    excluded=dict(num_dns=0, num_ip=0, num_dirnames=0, num_uri=0),
    permitted=dict(num_dns=0, num_ip=0, num_dirnames=1025, num_uri=0),
    sans=dict(num_dns=0, num_ip=0, num_dirnames=1024, num_uri=0))

make_chain(
    'ok-different-types-dns',
    "A chain containing a large number of name constraints and names,\n"
    "but of different types, thus not triggering the limit.",
    excluded=dict(num_dns=0, num_ip=1025, num_dirnames=1025, num_uri=1025),
    permitted=dict(num_dns=0, num_ip=1025, num_dirnames=1025, num_uri=1025),
    sans=dict(num_dns=1025, num_ip=0, num_dirnames=0, num_uri=0))
make_chain(
    'ok-different-types-ips',
    "A chain containing a large number of name constraints and names,\n"
    "but of different types, thus not triggering the limit.",
    excluded=dict(num_dns=1025, num_ip=0, num_dirnames=1025, num_uri=1025),
    permitted=dict(num_dns=1025, num_ip=0, num_dirnames=1025, num_uri=1025),
    sans=dict(num_dns=0, num_ip=1025, num_dirnames=0, num_uri=0))
make_chain(
    'ok-different-types-dirnames',
    "A chain containing a large number of name constraints and names,\n"
    "but of different types, thus not triggering the limit.",
    excluded=dict(num_dns=1025, num_ip=1025, num_dirnames=0, num_uri=1025),
    permitted=dict(num_dns=1025, num_ip=1025, num_dirnames=0, num_uri=1025),
    sans=dict(num_dns=0, num_ip=0, num_dirnames=1025, num_uri=0))