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

Side by Side Diff: net/data/verify_certificate_chain_unittest/openssl_conf.py

Issue 1414393008: Add scripts to generate simple test data for certificate verification. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@key_usages
Patch Set: add moar test Created 5 years 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/python
2 # Copyright (c) 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 """This file contains helpers for representing, manipulating, and writing
7 OpenSSL configuration files [1]
8
9 Configuration files are simply a collection of name=value "properties", which
10 are grouped into "sections".
11
12 [1] https://www.openssl.org/docs/manmaster/apps/config.html
13 """
14
15 class Property(object):
16 """Represents a key/value pair in OpenSSL .cnf files.
17
18 Names and values are not quoted in any way, so callers need to pass the text
19 exactly as it should be written to the file (leading and trailing whitespace
20 doesn't matter).
21
22 For instance:
23 baseConstraints = critical, CA:false
24
25 Could be represented by a Property where:
26 name = 'baseConstraints'
27 value = 'critical, CA:false'
28 """
29 def __init__(self, name, value):
30 self.name = name
31 self.value = value
32
33
34 def write_to(self, out):
35 """Outputs this property to .cnf file."""
36 out.write('%s = %s\n' % (self.name, self.value))
37
38
39 class Section(object):
40 """Represents a section in OpenSSL. For instance:
41 [CA_root]
42 preserve = true
43
44 Could be represented by a Section where:
45 name = 'CA_root'
46 properties = [Property('preserve', 'true')]
47 """
48 def __init__(self, name):
49 self.name = name
50 self.properties = []
51
52
53 def ensure_property_name_not_duplicated(self, name):
54 """Raises an exception of there is more than 1 property named |name|."""
55 count = 0
56 for prop in self.properties:
57 if prop.name == name:
58 count += 1
59 if count > 1:
60 raise Exception('Duplicate property: %s' % (name))
61
62
63 def set_property(self, name, value):
64 """Replaces, adds, or removes a Property from the Section:
65
66 - If |value| is None, then this is equivalent to calling
67 remove_property(name)
68 - If there is an existing property matching |name| then its value is
69 replaced with |value|
70 - If there are no properties matching |name| then a new one is added at
71 the end of the section
72
73 It is expected that there is AT MOST 1 property with the given name. If
74 that is not the case then this function will raise an error."""
75
76 if value is None:
77 self.remove_property(name)
78 return
79
80 self.ensure_property_name_not_duplicated(name)
81
82 for prop in self.properties:
83 if prop.name == name:
84 prop.value = value
85 return
86
87 self.add_property(name, value)
88
89
90 def add_property(self, name, value):
91 """Adds a property (allows duplicates)"""
92 self.properties.append(Property(name, value))
93
94
95 def remove_property(self, name):
96 """Removes the property with the indicated name, if it exists.
97
98 It is expected that there is AT MOST 1 property with the given name. If
99 that is not the case then this function will raise an error."""
100 self.ensure_property_name_not_duplicated(name)
101
102 for i in range(len(self.properties)):
103 if self.properties[i].name == name:
104 self.properties.pop(i)
105 return
106
107
108 def write_to(self, out):
109 """Outputs the section in the format used by .cnf files"""
110 out.write('[%s]\n' % (self.name))
111 for prop in self.properties:
112 prop.write_to(out)
113 out.write('\n')
114
115
116 class Config(object):
117 """Represents a .cnf (configuration) file in OpenSSL"""
118 def __init__(self):
119 self.sections = []
120
121
122 def get_section(self, name):
123 """Gets or creates a section with the given name."""
124 for section in self.sections:
125 if section.name == name:
126 return section
127 new_section = Section(name)
128 self.sections.append(new_section)
129 return new_section
130
131
132 def write_to_file(self, path):
133 """Outputs the Config to a .cnf files."""
134 with open(path, 'w') as out:
135 for section in self.sections:
136 section.write_to(out)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698