~ubuntu-branches/ubuntu/hardy/pyca/hardy

« back to all changes in this revision

Viewing changes to cgi-bin/ns-revoke.py

  • Committer: Bazaar Package Importer
  • Author(s): Lars Bahner
  • Date: 2003-12-02 19:39:35 UTC
  • Revision ID: james.westby@ubuntu.com-20031202193935-fzzt289mntvy6a8q
Tags: upstream-20031118
ImportĀ upstreamĀ versionĀ 20031118

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
"""
 
4
ns-revoke.py
 
5
(c) by Michael Stroeder <michael@stroeder.com>
 
6
 
 
7
CGI-BIN for revoking client certificates
 
8
Input:
 
9
 
 
10
PATH_INFO
 
11
- Name of CA in openssl.cnf (section [ca] of openssl.cnf)
 
12
QUERY_STRING
 
13
- Serial number of certificate to revoke
 
14
  max. 8 digits hexadecimal (32 Bit)
 
15
 
 
16
Example:
 
17
  ns-revoke.py/Persona?01
 
18
  revokes client certificate with serial 0x01 of CA "Persona"
 
19
 
 
20
The following checks are made to avoid denial of service attacks:
 
21
- The client software must provide the client certificate.
 
22
- The issuer of the client and the server certificates must match
 
23
"""
 
24
 
 
25
Version='0.6.6'
 
26
 
 
27
import sys, os, string, re, pycacnf, htmlbase, openssl, cgissl, certhelper
 
28
 
 
29
from pycacnf import opensslcnf, pyca_section
 
30
 
 
31
# Wir lesen rein gar nix von Standardeingabe => gleich dicht machen
 
32
sys.stdin.close()
 
33
 
 
34
# Path to openssl executable
 
35
openssl.bin_filename = pyca_section.get('OpenSSLExec','/usr/bin/openssl')
 
36
 
 
37
# Ein paar Umgebungsvariablen auslesen, welche der Apache liefert
 
38
request_method  = os.environ.get('REQUEST_METHOD','')
 
39
query_string    = os.environ.get('QUERY_STRING','')
 
40
script_name    = os.environ.get('SCRIPT_NAME','')
 
41
path_info       = os.environ.get('PATH_INFO','')[1:]
 
42
 
 
43
rm = (re.compile('[0-9a-fA-F]+(&yes)*')).match(query_string)
 
44
 
 
45
# Hier die ueblichen Paranoid-Pruefungen der Parameter
 
46
if request_method!='GET':
 
47
  # Skript nicht mit GET aufgerufen
 
48
  htmlbase.PrintErrorMsg('Wrong method.')
 
49
  sys.exit(0)
 
50
 
 
51
# Angabe der CA pruefen
 
52
ca_name = os.environ.get('PATH_INFO','')[1:]
 
53
 
 
54
if not ca_name:
 
55
  htmlbase.PrintErrorMsg('No certificate authority.')
 
56
  sys.exit(0)
 
57
 
 
58
# Name der CA pruefen
 
59
if not opensslcnf.data['ca'].has_key(ca_name):
 
60
  # CA-Definition nicht in openssl-Konfiguration enthalten
 
61
  htmlbase.PrintErrorMsg('Unknown certificate authority "%s".' % ca_name)
 
62
  sys.exit(0)
 
63
 
 
64
ca = opensslcnf.getcadata(ca_name)
 
65
 
 
66
# Abruf eines Zertifikates mittels Seriennummer
 
67
try:
 
68
  serial,yes = string.split(query_string,'_')
 
69
except ValueError:
 
70
  serial = query_string
 
71
 
 
72
serialnumber=string.atoi(serial,16)
 
73
 
 
74
ca_db = openssl.db.OpenSSLcaDatabaseClass(ca.database)
 
75
entry = ca_db.GetEntrybySerial(serialnumber)
 
76
 
 
77
# Kein entsprechender Eintrag gefunden
 
78
if not entry:
 
79
  htmlbase.PrintErrorMsg('Certificate not found.')
 
80
  sys.exit(0)
 
81
 
 
82
# Zertifikat ist ungueltig
 
83
if entry[openssl.db.DB_type]!=openssl.db.DB_TYPE_VAL:
 
84
  htmlbase.PrintErrorMsg('Certificate invalid.')
 
85
  sys.exit(0)
 
86
 
 
87
certfilename = os.path.join(ca.certs,'%s.pem' % (entry[openssl.db.DB_serial]))
 
88
 
 
89
# Does the certificate file exist?
 
90
if not os.path.isfile(certfilename):
 
91
  htmlbase.PrintErrorMsg('Certificate file not found.')
 
92
  sys.exit(0)
 
93
 
 
94
# Kein Zertifikat mit angegebener Nummer gefunden
 
95
if entry==[]:
 
96
  htmlbase.PrintErrorMsg('Certificate not found.')
 
97
  sys.exit(0)
 
98
 
 
99
if entry[openssl.db.DB_type]!=openssl.db.DB_TYPE_VAL:
 
100
  htmlbase.PrintErrorMsg('Certificate invalid.')
 
101
  sys.exit(0)
 
102
 
 
103
ssl_env = cgissl.GetAllSSLEnviron()
 
104
 
 
105
if not ssl_env.has_key('SSL_CLIENT_S_DN'):
 
106
  htmlbase.PrintErrorMsg('No client certificate present.')
 
107
  sys.exit(0)
 
108
 
 
109
cacert = openssl.cert.X509CertificateClass(ca.certificate)
 
110
 
 
111
#if ssl_env['SSL_CLIENT_I_DN']!=ssl_env['SSL_SERVER_I_DN']:
 
112
#  htmlbase.PrintErrorMsg('Wrong issuer of client certificate.')
 
113
#  sys.exit(0)
 
114
 
 
115
if ssl_env['SSL_CLIENT_S_DN']!=entry[openssl.db.DB_name]:
 
116
  htmlbase.PrintErrorMsg('Wrong client certificate.')
 
117
  sys.exit(0)
 
118
 
 
119
cert = openssl.cert.X509CertificateClass(certfilename)
 
120
 
 
121
if query_string[-4:]!='_yes':
 
122
  htmlbase.PrintHeader('Confirmation of certificate revocation.')
 
123
  print """The following certificate will be revoked:
 
124
%s
 
125
Are you really sure that you want to revoke your certificate?
 
126
The following reasons can make revoking necessary:
 
127
<UL>
 
128
  <LI>Your private key was compromised (stolen, the password was sniffed etc.)</LI>
 
129
  <LI>The content of the certificate attributes has become wrong.</LI>
 
130
</UL>
 
131
<A HREF="%s/%s?%s_yes">YES</A>
 
132
"""  % (cert.htmlprint(),script_name,ca_name,serialnumber)
 
133
  htmlbase.PrintFooter()
 
134
  sys.exit(0)
 
135
 
 
136
ca_db.Revoke(serialnumber)
 
137
htmlbase.PrintHeader('Revoked certificate.')
 
138
print 'The following certificate was revoked by you: %s' % (cert.htmlprint())
 
139
  
 
140
sys.exit(0)
 
141