/usr/share/pyca/pylib/openssl/cert.py is in pyca 20031119-0.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #######################################################################
# openssl.cert.py Version 0.6.6
# (c) by Michael Stroeder, michael.stroeder@propack-data.de
########################################################################
# Module for accessing certificate files
########################################################################
import sys, string, os, time
import openssl, charset, certhelper
certformats = ['pem','der','txt','net']
X509v1_certattrlist = ['CN','Email','OU','O','L','ST','C']
########################################################################
# functions used throughout this module
########################################################################
def GuessFormatbyExt(certfilename):
ext = string.lower(os.path.splitext(certfilename)[1])
if ext in certformats:
return ext
else:
return 'pem'
def GetCertValues(certfilename,inform='',command='x509'):
command = string.lower(command)
if command == 'x509':
opensslcommand = '%s x509 -in %s -fingerprint -inform %s -noout -subject -issuer -dates -serial -hash' % (openssl.bin_filename,certfilename,inform)
elif command == 'crl':
opensslcommand = '%s crl -in %s -inform %s -noout -issuer -lastupdate -nextupdate -hash' % (openssl.bin_filename,certfilename,inform)
result={}
hash=''
pipe = os.popen(opensslcommand)
s=pipe.readline()
while s:
try:
name, value = string.split(s,'=',1)
except ValueError:
hash = string.strip(s)
result[name]=string.strip(value)
s=pipe.readline()
rc = pipe.close()
if rc and rc!=256:
raise IOError,"Error %s: %s" % (rc,opensslcommand)
return hash, result
########################################################################
# X509CertificateClass
########################################################################
class X509CertificateClass:
def __init__(self,certfilename,inform=''):
self.filename = certfilename
if not inform:
self.format = GuessFormatbyExt(self.filename)
else:
self.format = inform
self.hash,certattrs = GetCertValues(self.filename,self.format,'x509')
self.issuer = openssl.db.SplitDN(certattrs.get('issuer',{}))
self.subject = openssl.db.SplitDN(certattrs.get('subject',{}))
self.serial = string.atoi(certattrs.get('serial','-1'),16)
self.notBefore = certattrs.get('notBefore','')
if self.notBefore:
self.notBefore_secs = time.mktime(time.strptime(self.notBefore,'%b %d %H:%M:%S %Y GMT'))
else:
self.notBefore_secs = 0
self.notAfter = certattrs.get('notAfter','')
if self.notAfter:
self.notAfter_secs = time.mktime(time.strptime(self.notAfter,'%b %d %H:%M:%S %Y GMT'))
else:
self.notAfter_secs = 0
# Fingerprint suchen
certattrs_keys = certattrs.keys()
self.fingerprint = {}
for i in certattrs_keys:
if i[-11:]=='Fingerprint':
self.fingerprint[string.upper(string.split(i)[0])] = string.split(certattrs[i],':')
# get serial number of certificate
def serial(self):
return string.atoi(self.serial)
def getfingerprint(self,digest='md5',delimiter=':'):
digest = string.upper(digest)
if not digest in ['MD2','MD5','MDC2','RMD160','SHA','SHA1']:
raise ValueError, 'Illegal parameter for digest: %s' % digest
elif self.fingerprint.has_key(digest):
result = self.fingerprint[digest]
elif digest=='MD5':
return certhelper.MD5Fingerprint(certhelper.pem2der(open(self.filename,'r').read()),delimiter)
elif digest=='SHA1':
return certhelper.SHA1Fingerprint(certhelper.pem2der(open(self.filename,'r').read()),delimiter)
else:
opensslcommand = '%s x509 -in %s -inform %s -outform DER | %s %s' % (
openssl.bin_filename,
self.filename,
self.format,
openssl.bin_filename,
string.lower(digest)
)
f = os.popen(opensslcommand)
rawdigest = string.strip(f.read())
rc = f.close()
if rc and rc!=256:
raise IOError,"Error %s: %s" % (rc,opensslcommand)
result = []
for i in range(len(rawdigest)/2):
result.append(rawdigest[2*i:2*(i+1)])
self.fingerprint[digest] = result
return string.upper(string.join(result,delimiter))
# return certificate in a string, desired format given in outform
def readcertfile(self,outform='PEM'):
if not (string.lower(outform) in certformats):
raise ValueError,'invalid certificate output format'
f = os.popen('%s x509 -in %s -inform %s -outform %s' % (openssl.bin_filename,self.filename,self.format,outform))
buf = f.read()
result = []
while buf:
result.append(buf)
buf = f.read()
rc = f.close()
if rc and rc!=256:
raise IOError,"Error %s: %s" % (rc,opensslcommand)
return string.join(result)
# Return a string containing the cert attributes
def asciiprint(self):
# Convert character sets
subjectdatalist = []
issuerdatalist = []
for attr in X509v1_certattrlist:
subjectdatalist.append('%-5s: %s' % (attr,string.strip(charset.asn12iso(self.subject.get(attr,'')))))
issuerdatalist.append('%-5s: %s' % (attr,string.strip(charset.asn12iso(self.issuer.get(attr,'')))))
return """This certificate belongs to:
%s
This certificate was issued by:
%s
Serial Number: %s
This certificate is valid
from %s until %s.
Certificate Fingerprint:
SHA-1: %s
MD5 : %s
""" % ( \
string.join(subjectdatalist,'\n'),
string.join(issuerdatalist,'\n'),
self.serial,
self.notBefore,
self.notAfter,
self.getfingerprint('sha1'),
self.getfingerprint('md5'),
)
# Return a string containing a nice formatted <TABLE> with cert info
def htmlprint(self):
# Convert character sets
subjectdatalist = []
issuerdatalist = []
for attr in X509v1_certattrlist:
subjectdatalist.append(string.strip(charset.asn12html4(self.subject.get(attr,''))))
issuerdatalist.append(string.strip(charset.asn12html4(self.issuer.get(attr,''))))
return """
<TABLE BORDER=1 WIDTH="100%%" CELLPADDING="5%%">
<TR>
<TD WIDTH="50%%">
<DL>
<DT><STRONG>This certificate belongs to:</STRONG></DT>
<DD>%s</DD>
</DL>
</TD>
<TD>
<DL>
<DT><STRONG>This certificate was issued by:</STRONG></DT>
<DD>%s</DD>
</DL>
</TD>
</TR>
<TR>
<TD COLSPAN=2>
<DL>
<DT><STRONG>Serial Number:</STRONG><DT>
<DD>%s</DD>
<DT><STRONG>This certificate is valid from %s until %s.</STRONG></DT>
<DT><STRONG>Certificate Fingerprint:</STRONG></DT>
<DD><PRE>SHA-1: %s<BR>MD5: %s</PRE></DD>
</DL>
</TD>
</TR>
</TABLE>
""" % ( \
string.join(subjectdatalist,'<BR>'),
string.join(issuerdatalist,'<BR>'),
self.serial,
self.notBefore,
self.notAfter,
self.getfingerprint('sha1'),
self.getfingerprint('md5'),
)
########################################################################
# CRLClass
########################################################################
class CRLClass:
def __init__(self,crlfilename,inform=''):
self.filename = crlfilename
if not inform:
self.format = GuessFormatbyExt(self.filename)
else:
self.format = inform
self.hash,certattrs = GetCertValues(self.filename,self.format,'crl')
self.issuer = openssl.db.SplitDN(certattrs.get('issuer',openssl.db.empty_DN_dict))
self.hash = certattrs.get('hash','')
self.lastUpdate = certattrs.get('lastUpdate','')
if self.lastUpdate:
self.lastUpdate_secs = time.mktime(time.strptime(self.lastUpdate,'%b %d %H:%M:%S %Y GMT'))
else:
self.lastUpdate_secs = 0
self.nextUpdate = certattrs.get('nextUpdate','')
if self.nextUpdate:
self.nextUpdate_secs = time.mktime(time.strptime(self.nextUpdate,'%b %d %H:%M:%S %Y GMT'))
else:
self.notAfter_secs = 0
# return certificate in a string, desired format given in outform
def readcertfile(self,outform='PEM'):
if not (string.lower(outform) in certformats):
raise ValueError,'invalid certificate output format'
f = os.popen('%s crl -in %s -inform %s -outform %s' % (openssl.bin_filename,self.filename,self.format,outform))
buf = f.read()
result = []
while buf:
result.append(buf)
buf = f.read()
rc = f.close()
if rc and rc!=256:
raise IOError,"Error %s: %s" % (rc,opensslcommand)
return string.join(result)
########################################################################
# SPKACClass
########################################################################
class SPKACClass:
def __init__(self,spkacfilename,inform=''):
self.filename = spkacfilename
self.data = {}
spkacfile = open(self.filename,'r')
s = spkacfile.readline()
while s:
try:
attr,value=string.split(s,'=',1)
self.data[string.strip(attr)]=string.strip(value)
except ValueError:
pass
s = spkacfile.readline()
|