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.
17 def add_attr(self
, attr_type
, attr_value_type
, attr_value
,
19 self
.attrs
.append((attr_type
, attr_value_type
, attr_value
, attr_modifier
))
24 for n
, attr
in enumerate(self
.attrs
):
25 s
+= 'attrTypeAndValue%i=SEQUENCE:attrTypeAndValueSequence%i_%i\n' % (
29 for n
, attr
in enumerate(self
.attrs
):
30 attr_type
, attr_value_type
, attr_value
, attr_modifier
= attr
31 s
+= '[attrTypeAndValueSequence%i_%i]\n' % (id(self
), n
)
32 # Note the quotes around the string value here, which is necessary for
33 # trailing whitespace to be included by openssl.
34 s
+= 'type=OID:%s\n' % attr_type
37 s
+= attr_modifier
+ ','
38 s
+= '%s:"%s"\n' % (attr_value_type
, attr_value
)
53 s
= 'asn1 = SEQUENCE:rdnSequence\n\n[rdnSequence]\n'
54 for n
, rdn
in enumerate(self
.rdns
):
55 s
+= 'rdn%i = SET:rdnSet%i\n' % (n
, n
)
59 for n
, rdn
in enumerate(self
.rdns
):
60 s
+= '[rdnSet%i]\n%s\n' % (n
, rdn
)
66 out_fn
= os
.path
.join('..', 'names', fn
+ '.pem')
67 conf_tempfile
= tempfile
.NamedTemporaryFile()
68 conf_tempfile
.write(str(s
))
70 der_tmpfile
= tempfile
.NamedTemporaryFile()
71 description_tmpfile
= tempfile
.NamedTemporaryFile()
72 subprocess
.check_call(['openssl', 'asn1parse', '-genconf', conf_tempfile
.name
,
73 '-i', '-out', der_tmpfile
.name
],
74 stdout
=description_tmpfile
)
77 output_file
= open(out_fn
, 'w')
78 description_tmpfile
.seek(0)
79 output_file
.write(description_tmpfile
.read())
80 output_file
.write('-----BEGIN NAME-----\n')
81 output_file
.write(base64
.encodestring(der_tmpfile
.read()))
82 output_file
.write('-----END NAME-----\n')
90 def extra_whitespace(s
):
91 return ' ' + s
.replace(' ', ' ') + ' '
99 for valuetype
in ('PRINTABLESTRING', 'T61STRING', 'UTF8', 'BMPSTRING',
101 for string_mangler
in (unmangled
, extra_whitespace
, case_swap
):
103 n
.add_rdn().add_attr('countryName', 'PRINTABLESTRING', 'US')
104 n
.add_rdn().add_attr('stateOrProvinceName',
106 string_mangler('New York'))
107 n
.add_rdn().add_attr('localityName',
109 string_mangler("ABCDEFGHIJKLMNOPQRSTUVWXYZ "
110 "abcdefghijklmnopqrstuvwxyz "
111 "0123456789 '()+,-./:=?"))
113 n_extra_attr
= copy
.deepcopy(n
)
114 n_extra_attr
.rdns
[-1].add_attr('organizationName',
116 string_mangler('Name of company'))
118 n_dupe_attr
= copy
.deepcopy(n
)
119 n_dupe_attr
.rdns
[-1].add_attr(*n_dupe_attr
.rdns
[-1].attrs
[-1])
121 n_extra_rdn
= copy
.deepcopy(n
)
122 n_extra_rdn
.add_rdn().add_attr('organizationName',
124 string_mangler('Name of company'))
126 filename_base
= 'ascii-' + valuetype
+ '-' + string_mangler
.__name
__
128 generate(n
, filename_base
)
129 generate(n_extra_attr
, filename_base
+ '-extra_attr')
130 generate(n_dupe_attr
, filename_base
+ '-dupe_attr')
131 generate(n_extra_rdn
, filename_base
+ '-extra_rdn')
133 for valuetype
in ('UTF8', 'BMPSTRING', 'UNIVERSALSTRING'):
135 n
.add_rdn().add_attr('countryName', 'PRINTABLESTRING', 'JP')
136 n
.add_rdn().add_attr('localityName',
138 "\xe6\x9d\xb1\xe4\xba\xac",
141 filename_base
= 'unicode_bmp-' + valuetype
+ '-' + 'unmangled'
142 generate(n
, filename_base
)
144 for valuetype
in ('UTF8', 'UNIVERSALSTRING'):
146 n
.add_rdn().add_attr('countryName', 'PRINTABLESTRING', 'JP')
147 n
.add_rdn().add_attr('localityName',
149 "\xf0\x9d\x90\x80\xf0\x9d\x90\x99",
152 filename_base
= 'unicode_supplementary-' + valuetype
+ '-' + 'unmangled'
153 generate(n
, filename_base
)
155 generate("""asn1 = SEQUENCE:rdnSequence
159 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
160 [attrTypeAndValueSequence0_0]
162 value=PRINTABLESTRING:"US"
163 extra=PRINTABLESTRING:"hello world"
164 """, "invalid-AttributeTypeAndValue-extradata")
166 generate("""asn1 = SEQUENCE:rdnSequence
170 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
171 [attrTypeAndValueSequence0_0]
173 """, "invalid-AttributeTypeAndValue-onlyOneElement")
175 generate("""asn1 = SEQUENCE:rdnSequence
179 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
180 [attrTypeAndValueSequence0_0]
181 """, "invalid-AttributeTypeAndValue-empty")
183 generate("""asn1 = SEQUENCE:rdnSequence
187 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
188 [attrTypeAndValueSequence0_0]
189 type=PRINTABLESTRING:"hello world"
190 value=PRINTABLESTRING:"US"
191 """, "invalid-AttributeTypeAndValue-badAttributeType")
193 generate("""asn1 = SEQUENCE:rdnSequence
197 attrTypeAndValue0=SET:attrTypeAndValueSequence0_0
198 [attrTypeAndValueSequence0_0]
200 value=PRINTABLESTRING:"US"
201 """, "invalid-AttributeTypeAndValue-setNotSequence")
203 generate("""asn1 = SEQUENCE:rdnSequence
205 rdn0 = SEQUENCE:rdnSet0
207 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
208 [attrTypeAndValueSequence0_0]
210 value=PRINTABLESTRING:"US"
211 """, "invalid-RDN-sequenceInsteadOfSet")
213 generate("""asn1 = SEQUENCE:rdnSequence
217 """, "invalid-RDN-empty")
219 generate("""asn1 = SET:rdnSequence
223 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
224 [attrTypeAndValueSequence0_0]
226 value=PRINTABLESTRING:"US"
227 """, "invalid-Name-setInsteadOfSequence")
229 generate("""asn1 = SEQUENCE:rdnSequence
231 """, "valid-Name-empty")
233 # Certs with a RDN that is sorted differently due to length of the values, but
234 # which should compare equal when normalized.
235 generate("""asn1 = SEQUENCE:rdnSequence
239 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
240 attrTypeAndValue1=SEQUENCE:attrTypeAndValueSequence0_1
241 [attrTypeAndValueSequence0_0]
242 type=OID:stateOrProvinceName
243 value=PRINTABLESTRING:" state"
244 [attrTypeAndValueSequence0_1]
245 type=OID:localityName
246 value=PRINTABLESTRING:"locality"
247 """, "ascii-PRINTABLESTRING-rdn_sorting_1")
249 generate("""asn1 = SEQUENCE:rdnSequence
253 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
254 attrTypeAndValue1=SEQUENCE:attrTypeAndValueSequence0_1
255 [attrTypeAndValueSequence0_0]
256 type=OID:stateOrProvinceName
257 value=PRINTABLESTRING:"state"
258 [attrTypeAndValueSequence0_1]
259 type=OID:localityName
260 value=PRINTABLESTRING:" locality"
261 """, "ascii-PRINTABLESTRING-rdn_sorting_2")
263 # Certs with a RDN that is sorted differently due to length of the values, and
264 # also contains multiple values with the same type.
265 generate("""asn1 = SEQUENCE:rdnSequence
269 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
270 attrTypeAndValue1=SEQUENCE:attrTypeAndValueSequence0_1
271 attrTypeAndValue2=SEQUENCE:attrTypeAndValueSequence0_2
272 attrTypeAndValue3=SEQUENCE:attrTypeAndValueSequence0_3
273 attrTypeAndValue4=SEQUENCE:attrTypeAndValueSequence0_4
274 [attrTypeAndValueSequence0_0]
275 type=OID:domainComponent
276 value=IA5STRING:" cOm"
277 [attrTypeAndValueSequence0_1]
278 type=OID:domainComponent
279 value=IA5STRING:"eXaMple"
280 [attrTypeAndValueSequence0_2]
281 type=OID:domainComponent
282 value=IA5STRING:"wWw"
283 [attrTypeAndValueSequence0_3]
284 type=OID:localityName
285 value=PRINTABLESTRING:"NEw"
286 [attrTypeAndValueSequence0_4]
287 type=OID:localityName
288 value=PRINTABLESTRING:" yORk "
289 """, "ascii-mixed-rdn_dupetype_sorting_1")
291 generate("""asn1 = SEQUENCE:rdnSequence
295 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
296 attrTypeAndValue1=SEQUENCE:attrTypeAndValueSequence0_1
297 attrTypeAndValue2=SEQUENCE:attrTypeAndValueSequence0_2
298 attrTypeAndValue3=SEQUENCE:attrTypeAndValueSequence0_3
299 attrTypeAndValue4=SEQUENCE:attrTypeAndValueSequence0_4
300 [attrTypeAndValueSequence0_0]
301 type=OID:domainComponent
302 value=IA5STRING:"cOM"
303 [attrTypeAndValueSequence0_1]
304 type=OID:domainComponent
305 value=IA5STRING:"eXampLE"
306 [attrTypeAndValueSequence0_2]
307 type=OID:domainComponent
308 value=IA5STRING:" Www "
309 [attrTypeAndValueSequence0_3]
310 type=OID:localityName
311 value=PRINTABLESTRING:" nEw "
312 [attrTypeAndValueSequence0_4]
313 type=OID:localityName
314 value=PRINTABLESTRING:"yoRK"
315 """, "ascii-mixed-rdn_dupetype_sorting_2")
317 # Minimal valid config. Copy and modify this one when generating new invalid
319 generate("""asn1 = SEQUENCE:rdnSequence
323 attrTypeAndValue0=SEQUENCE:attrTypeAndValueSequence0_0
324 [attrTypeAndValueSequence0_0]
326 value=PRINTABLESTRING:"US"
327 """, "valid-minimal")
329 if __name__
== '__main__':