Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: net/data/verify_name_match_unittest/scripts/generate-names.py

Issue 1125333005: RFC 2459 name comparison. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review changes for Nick, plus some fixes Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import copy
7 import os
8 import subprocess
9 import tempfile
10
11
12 class RDN:
13 def __init__(self):
14 self.attrs = []
15
16 def add_attr(self, attr_type, attr_value_type, attr_value):
17 self.attrs.append((attr_type, attr_value_type, attr_value))
18 return self
19
20 def __str__(self):
21 s = ''
22 for n, attr in enumerate(self.attrs):
23 s += 'attrTypeAndValue%i=SEQUENCE:attrTypeAndValueSequence%i_%i\n' % (
24 n, id(self), n)
25
26 s += '\n'
27 for n, attr in enumerate(self.attrs):
28 s += '[attrTypeAndValueSequence%i_%i]\n' % (id(self), n)
29 # Note the quotes around the string value here, which is necessary for
30 # trailing whitespace to be included by openssl.
31 s += 'type=OID:%s\nvalue=%s:"%s"\n' % attr
32
33 return s
34
35
36 class NameGenerator:
37 def __init__(self):
38 self.rdns = []
39
40 def add_rdn(self):
41 rdn = RDN()
42 self.rdns.append(rdn)
43 return rdn
44
45 def __str__(self):
46 s = 'asn1 = SEQUENCE:rdnSequence\n\n[rdnSequence]\n'
47 for n, rdn in enumerate(self.rdns):
48 s += 'rdn%i = SET:rdnSet%i\n' % (n, n)
49
50 s += '\n'
51
52 for n, rdn in enumerate(self.rdns):
53 s += '[rdnSet%i]\n%s\n' % (n, rdn)
54
55 return s
56
57
58 def generate(s, fn):
59 outfn = os.path.join('..', 'names', fn)
60 f = tempfile.NamedTemporaryFile()
61 f.write(str(s))
62 f.flush()
63 subprocess.check_call(['openssl', 'asn1parse', '-genconf', f.name, '-noout',
64 '-out', outfn])
65 f.close()
66
67
68 def unmangled(s):
69 return s
70
71
72 def extra_whitespace(s):
73 return ' ' + s.replace(' ', ' ') + ' '
74
75
76 def case_swap(s):
77 return s.swapcase()
78
79
80 def main():
81 for valuetype in ('PRINTABLESTRING', 'T61STRING', 'UTF8', 'BMPSTRING',
82 'UNIVERSALSTRING'):
83 for string_mangler in (unmangled, extra_whitespace, case_swap):
84 n=NameGenerator()
85 n.add_rdn().add_attr('countryName', 'PRINTABLESTRING', 'US')
86 n.add_rdn().add_attr('stateOrProvinceName',
87 valuetype,
88 string_mangler('New York'))
89 n.add_rdn().add_attr('localityName',
90 valuetype,
91 string_mangler("ABCDEFGHIJKLMNOPQRSTUVWXYZ "
92 "abcdefghijklmnopqrstuvwxyz "
93 "0123456789 '()+,-./:=?"))
94
95 n_extra_attr = copy.deepcopy(n)
96 n_extra_attr.rdns[-1].add_attr('organizationName',
97 valuetype,
98 string_mangler('Name of company'))
99
100 n_extra_rdn = copy.deepcopy(n)
101 n_extra_rdn.add_rdn().add_attr('organizationName',
102 valuetype,
103 string_mangler('Name of company'))
104
105 filename_base = 'ascii-' + valuetype + '-' + string_mangler.__name__
106
107 generate(n, filename_base + '.der')
108 generate(n_extra_attr, filename_base + '-extra_attr.der')
109 generate(n_extra_rdn, filename_base + '-extra_rdn.der')
110
111 generate("""asn1 = SEQUENCE:rdnSequence
112 [rdnSequence]
113 rdn0 = SET:rdnSet0
114 [rdnSet0]
115 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
116 [attrTypeAndValueSequence0_0]
117 type=OID:countryName
118 value=PRINTABLESTRING:"US"
119 extra=PRINTABLESTRING:"hello world"
120 """, "invalid-AttributeTypeAndValue-extradata.der")
121
122 generate("""asn1 = SEQUENCE:rdnSequence
123 [rdnSequence]
124 rdn0 = SET:rdnSet0
125 [rdnSet0]
126 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
127 [attrTypeAndValueSequence0_0]
128 type=OID:countryName
129 """, "invalid-AttributeTypeAndValue-onlyOneElement.der")
130
131 generate("""asn1 = SEQUENCE:rdnSequence
132 [rdnSequence]
133 rdn0 = SET:rdnSet0
134 [rdnSet0]
135 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
136 [attrTypeAndValueSequence0_0]
137 """, "invalid-AttributeTypeAndValue-empty.der")
138
139 generate("""asn1 = SEQUENCE:rdnSequence
140 [rdnSequence]
141 rdn0 = SET:rdnSet0
142 [rdnSet0]
143 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
144 [attrTypeAndValueSequence0_0]
145 type=PRINTABLESTRING:"hello world"
146 value=PRINTABLESTRING:"US"
147 """, "invalid-AttributeTypeAndValue-badAttributeType.der")
148
149 generate("""asn1 = SEQUENCE:rdnSequence
150 [rdnSequence]
151 rdn0 = SET:rdnSet0
152 [rdnSet0]
153 attrTypeAndValue0=SET:attrTypeAndValueSequence0_0
154 [attrTypeAndValueSequence0_0]
155 type=OID:countryName
156 value=PRINTABLESTRING:"US"
157 """, "invalid-AttributeTypeAndValue-setNotSequence.der")
158
159 generate("""asn1 = SEQUENCE:rdnSequence
160 [rdnSequence]
161 rdn0 = SEQUENCE:rdnSet0
162 [rdnSet0]
163 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
164 [attrTypeAndValueSequence0_0]
165 type=OID:countryName
166 value=PRINTABLESTRING:"US"
167 """, "invalid-RDN-sequenceInsteadOfSet.der")
168
169 generate("""asn1 = SEQUENCE:rdnSequence
170 [rdnSequence]
171 rdn0 = SET:rdnSet0
172 [rdnSet0]
173 """, "invalid-RDN-empty.der")
174
175 generate("""asn1 = SET:rdnSequence
176 [rdnSequence]
177 rdn0 = SET:rdnSet0
178 [rdnSet0]
179 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
180 [attrTypeAndValueSequence0_0]
181 type=OID:countryName
182 value=PRINTABLESTRING:"US"
183 """, "invalid-Name-setInsteadOfSequence.der")
184
185 generate("""asn1 = SEQUENCE:rdnSequence
186 [rdnSequence]
187 """, "invalid-Name-empty.der")
188
189 # Minimal valid config. Copy and modify this one when generating new invalid
190 # configs.
191 generate("""asn1 = SEQUENCE:rdnSequence
192 [rdnSequence]
193 rdn0 = SET:rdnSet0
194 [rdnSet0]
195 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
196 [attrTypeAndValueSequence0_0]
197 type=OID:countryName
198 value=PRINTABLESTRING:"US"
199 """, "valid-minimal.der")
200
201 if __name__ == '__main__':
202 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698