| OLD | NEW | 
|---|
| (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   Would be 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 WriteTo(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   Is a section named 'CA_root' containing 1 property. | 
|  | 45   """ | 
|  | 46   def __init__(self, name): | 
|  | 47     self.name = name | 
|  | 48     self.properties = [] | 
|  | 49 | 
|  | 50 | 
|  | 51   def SetProperty(self, name, value): | 
|  | 52     """If value is None then the property is deleted. Otherwise overwrites or | 
|  | 53     adds it""" | 
|  | 54     if value is None: | 
|  | 55       self.RemoveProperty(name) | 
|  | 56       return | 
|  | 57 | 
|  | 58     for prop in self.properties: | 
|  | 59       if prop.name == name: | 
|  | 60         prop.value = value | 
|  | 61         return | 
|  | 62 | 
|  | 63     self.AddProperty(name, value) | 
|  | 64 | 
|  | 65 | 
|  | 66   def AddProperty(self, name, value): | 
|  | 67     """Adds a property (allows duplicates)""" | 
|  | 68     self.properties.append(Property(name, value)) | 
|  | 69 | 
|  | 70 | 
|  | 71   def RemoveProperty(self, name): | 
|  | 72     """Removes the property with the indicated name, if it exists.""" | 
|  | 73     for i in range(len(self.properties)): | 
|  | 74       if self.properties[i].name == name: | 
|  | 75         self.properties.pop(i) | 
|  | 76         return | 
|  | 77 | 
|  | 78 | 
|  | 79   def WriteTo(self, out): | 
|  | 80     """Outputs the section in the format used by .cnf files""" | 
|  | 81     out.write('[%s]\n' % (self.name)) | 
|  | 82     for prop in self.properties: | 
|  | 83       prop.WriteTo(out) | 
|  | 84     out.write('\n') | 
|  | 85 | 
|  | 86 | 
|  | 87 class Config(object): | 
|  | 88   """Represents a .cnf (configuration) file in OpenSSL""" | 
|  | 89   def __init__(self): | 
|  | 90     self.sections = [] | 
|  | 91 | 
|  | 92 | 
|  | 93   def GetSection(self, name): | 
|  | 94     """Gets or creates a section with the given name.""" | 
|  | 95     for section in self.sections: | 
|  | 96       if section.name == name: | 
|  | 97         return section | 
|  | 98     new_section = Section(name) | 
|  | 99     self.sections.append(new_section) | 
|  | 100     return new_section | 
|  | 101 | 
|  | 102 | 
|  | 103   def WriteToFile(self, path): | 
|  | 104     """Outputs the Config to a .cnf files.""" | 
|  | 105     with open(path, 'w') as out: | 
|  | 106       for section in self.sections: | 
|  | 107         section.WriteTo(out) | 
| OLD | NEW | 
|---|