source: TI12-security/trunk/python/ndg.security.common/ndg/security/common/openssl.py @ 2140

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.common/ndg/security/common/openssl.py@2140
Revision 2140, 4.2 KB checked in by pjkersha, 14 years ago (diff)

Moved OpenSSLConfig class from MyProxy?.py into a separate module so that it
can be used by the Certificate Authority client code.

Line 
1"""OpenSSL utilities module - contains OpenSSLConfig class for
2parsing OpenSSL configuration files
3
4NERC Data Grid Project
5
6@author P J Kershaw 08/02/07
7
8@copyright (C) 2007 CCLRC & NERC
9
10@license This software may be distributed under the terms of the Q Public
11License, version 1.0 or later.
12"""
13reposID = '$Id:$'
14
15import re, os
16
17#_____________________________________________________________________________       
18class OpenSSLConfigError(Exception):
19    """Exceptions related to OpenSSLConfig class"""   
20
21
22#_____________________________________________________________________________       
23class OpenSSLConfig(object):
24    """Wrapper to OpenSSL Configuration file to allow extraction of
25    required distinguished name used for making certificate requests
26   
27    @cvar __reqDnRE: regular expression pattern for locating required
28    distinguished name from the config file"""
29   
30    __reqDnRE = '\[ req_distinguished_name \].*\['
31   
32    def __init__(self, filePath=None):
33        """Initial OpenSSL configuration optionally setting a file path to
34        read from
35       
36        @param filePath: path to OpenSSL configuration file"""
37       
38        # Content of file
39        self.__fileTxt = None
40        self.__setFilePath(filePath)
41
42           
43    def __setFilePath(self, filePath):
44        """Set property method
45        @param filePath: path for OpenSSL configuration file"""
46        if filePath is not None:
47            if not isinstance(filePath, basestring):
48                raise OpenSSLConfigError, \
49                    "Input OpenSSL config file path must be a string"
50
51            self.__filePath = filePath
52                   
53            try:
54                if not os.access(self.__filePath, os.R_OK):
55                    raise OpenSSLConfigError, "not found or no read access"
56                                         
57            except Exception, e:
58                raise OpenSSLConfigError, \
59                    "OpenSSL config file path is not valid: \"%s\": %s" % \
60                    (self.__filePath, str(e))
61
62
63    def __getFilePath(self):
64        """Get property method
65        @param filePath: file path for OpenSSL configuration file"""
66        return self.__filePath
67
68    filePath = property(fget=__getFilePath,
69                        fset=__setFilePath,
70                        doc="file path for configuration file")
71   
72    def __getFileTxt(self):
73        """Get content of file in call to getReqDN
74        @return string content of file"""
75        return self.__fileTxt
76   
77    def __setFileTxt(self, input):
78        """Set content of file
79        @param input: string content of  file. - Set to None to re-read
80        file content in call to getReqDN"""
81        if input is not None and not isinstance(input, basestring):
82            raise AttributeError, "File text must be string or None type"
83       
84        self.__fileTxt = input
85   
86   
87    fileTxt = property(fset=__setFileTxt,
88                       fget=__getFileTxt,
89                       doc="Content of SSL file")
90   
91    def _read(self):
92        """Read OpenSSL configuration file and return as string
93       
94        @return fileTxt: content of the file"""
95
96        self.__fileTxt = self.__fileTxt or open(self.__filePath).read()
97        return self.__fileTxt
98
99
100    def getReqDN(self):
101        """Read Required DN parameters from the configuration file returning
102        them in a dictionary
103       
104        @return Distinguished Name OU and O defaults in a dictionary"""
105       
106        # Nb. Match over line boundaries
107        try:
108            reqDnTxt = re.findall(self.__reqDnRE, self._read(), re.S)[0]
109
110            # Separate lines
111            reqDnLines = reqDnTxt.split(os.linesep)
112           
113            # Match the '*_default' entries and make a dictionary
114            #
115            # Make sure comment lies are omitted - P J Kershaw 22/07/05
116            return dict([re.split('_default\s*=\s*', line) \
117                         for line in reqDnLines \
118                         if re.match('[^#].*_default\s*=', line)]) 
119        except Exception, e:
120            raise OpenSSLConfigError, \
121                "Error reading content of OpenSSL config file \"%s\: %s" % \
122                                                    (self.__filePath, str(e))
Note: See TracBrowser for help on using the repository browser.