~ubuntu-branches/ubuntu/maverick/pyca/maverick

« back to all changes in this revision

Viewing changes to sbin/ca-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
# ca-revoke.py
 
5
# (c) by Michael Stroeder, michael@stroeder.com
 
6
########################################################################
 
7
 
 
8
__version__ = '0.6.6'
 
9
 
 
10
########################################################################
 
11
# This script is used to revoke certificates from the command line
 
12
# ca-revoke.py -h prints usage of parameters.
 
13
########################################################################
 
14
 
 
15
import sys, string, os, smtplib, getopt
 
16
 
 
17
from time import time,localtime,strftime,mktime
 
18
 
 
19
def findoption(options,paramname):
 
20
  for i in options:
 
21
    if i[0]==paramname:
 
22
      return i
 
23
  return ()
 
24
 
 
25
def PrintUsage(ErrorMsg='',ErrorCode=1):
 
26
  script_name = string.split(sys.argv[0],os.sep)[-1]
 
27
  sys.stderr.write("""*** %s *** (C) by Michael Stroeder, 1999
 
28
 
 
29
usage: %s [options]
 
30
 
 
31
Options:
 
32
 
 
33
  -h or --help
 
34
        Print out this message
 
35
 
 
36
  --config=[pathname]
 
37
        Pathname of OpenSSL configuration file.
 
38
        You may also use env variable OPENSSL_CONF.
 
39
        Default: /etc/openssl/openssl.cnf
 
40
 
 
41
  --pycalib=[directory]
 
42
        Specify directory containing the pyCA modules
 
43
        You may also use env variable PYCALIB.
 
44
        Default: /usr/local/pyca/pylib
 
45
 
 
46
  --name=[CA name]
 
47
        Name of CA in section [ca] of OpenSSL config.
 
48
 
 
49
  --serial=[hex number]
 
50
        Serial number of certificate to revoke.
 
51
 
 
52
""" % (script_name,script_name))
 
53
  if ErrorMsg:
 
54
    sys.stderr.write('Error: %s\n' % ErrorMsg)
 
55
  sys.exit(ErrorCode)
 
56
 
 
57
########################################################################
 
58
#                              Main
 
59
########################################################################
 
60
 
 
61
script_name=sys.argv[0]
 
62
 
 
63
try:
 
64
  options,args=getopt.getopt(sys.argv[1:],'h',['help','config=','pycalib=','name=','serial='])
 
65
except getopt.error,e:
 
66
  PrintUsage(str(e))
 
67
 
 
68
if findoption(options,'-h')!=() or findoption(options,'--help')!=():
 
69
  PrintUsage()
 
70
 
 
71
if findoption(options,'--config')!=():
 
72
  opensslcnfname = findoption(options,'--config')[1]
 
73
else:
 
74
  opensslcnfname = os.environ.get('OPENSSL_CONF','/etc/openssl/openssl.cnf')
 
75
 
 
76
if not os.path.isfile(opensslcnfname):
 
77
  PrintUsage('Config file %s not found.' % (opensslcnfname))
 
78
 
 
79
if findoption(options,'--pycalib')!=():
 
80
  pycalib = findoption(options,'--pycalib')[1]
 
81
else:
 
82
  pycalib = os.environ.get('PYCALIB','/usr/local/pyca/pylib')
 
83
 
 
84
if not os.path.exists(pycalib) or not os.path.isdir(pycalib):
 
85
  PrintUsage('Module directory %s not exists or not a directory.' % (pycalib))
 
86
 
 
87
sys.path.append(pycalib)
 
88
 
 
89
try:
 
90
  import openssl,charset
 
91
  from openssl.db import \
 
92
    empty_DN_dict, \
 
93
    DB_type,DB_exp_date,DB_rev_date,DB_serial,DB_file,DB_name,DB_number, \
 
94
    DB_TYPE_REV,DB_TYPE_EXP,DB_TYPE_VAL, \
 
95
    dbtime2tuple,GetEntriesbyDN,SplitDN
 
96
except ImportError:
 
97
  PrintUsage('Required pyCA modules not found in directory %s!' % (pycalib))
 
98
 
 
99
# Read the configuration file
 
100
if os.path.isfile('%s.pickle' % (opensslcnfname)):
 
101
  # Try to read OpenSSL's config file from a pickled copy
 
102
  f=open('%s.pickle' % (opensslcnfname),'rb')
 
103
  try:
 
104
    # first try to use the faster cPickle module
 
105
    from cPickle import load
 
106
  except ImportError:
 
107
    from pickle import load
 
108
  opensslcnf=load(f)
 
109
  f.close()
 
110
else:
 
111
  # Parse OpenSSL's config file from source
 
112
  opensslcnf=openssl.cnf.OpenSSLConfigClass(opensslcnfname)
 
113
 
 
114
pyca_section = opensslcnf.data.get('pyca',{})
 
115
ca_names = opensslcnf.sectionkeys.get('ca',[])
 
116
 
 
117
openssl.bin_filename = pyca_section.get('OpenSSLExec','/usr/local/ssl/bin/openssl')
 
118
if not os.path.isfile(openssl.bin_filename):
 
119
  sys.stderr.write('Did not find OpenSSL executable %s.\n' % (openssl.bin_filename))
 
120
  sys.exit(1)
 
121
 
 
122
if findoption(options,'--name')!=():
 
123
  ca_name = findoption(options,'--name')[1]
 
124
  if not ca_name in ca_names:
 
125
    PrintUsage('Wrong CA name.\nCA names listed in the current configuration:\n%s.' % string.join(ca_names,', '))
 
126
else:
 
127
  PrintUsage('You have to provide a name of a CA definition.')
 
128
 
 
129
if findoption(options,'--serial')!=():
 
130
  try:
 
131
    serial = string.atoi(findoption(options,'--serial')[1],16)
 
132
  except ValueError:
 
133
    PrintUsage('No valid serial number.')
 
134
 
 
135
else:
 
136
  PrintUsage('You have to provide the serial number of the certificate you want to revoke.')
 
137
 
 
138
ca = opensslcnf.getcadata(ca_name)
 
139
 
 
140
sys.stdout.write('Searching database %s for certificate %x...\n' % (ca.database,serial))
 
141
ca_db = openssl.db.OpenSSLcaDatabaseClass(ca.database)
 
142
result = ca_db.GetEntrybySerial(serial)
 
143
 
 
144
if result:
 
145
  sys.stdout.write("""Found the following certificate:
 
146
Serial number: %s
 
147
Distinguished Name: %s
 
148
""" % (result[DB_serial],charset.asn12iso(result[DB_name])))
 
149
 
 
150
  if result[DB_type]==DB_TYPE_REV:
 
151
    sys.stdout.write('Certificate already revoked since %s.\n' % strftime('%d.%m.%Y',localtime(mktime(dbtime2tuple(result[DB_rev_date])))))
 
152
    sys.exit(0)
 
153
  elif result[DB_type]==DB_TYPE_EXP:
 
154
    sys.stdout.write('Certificate already expired since %s.\n' % strftime('%d.%m.%Y',localtime(mktime(dbtime2tuple(result[DB_exp_date])))))
 
155
    sys.exit(0)
 
156
  elif result[DB_type]==DB_TYPE_VAL:
 
157
    sys.stdout.write('Valid until %s.\n\nRevoke the certificate? (y/n) ' % strftime('%d.%m.%Y',localtime(mktime(dbtime2tuple(result[DB_exp_date])))))
 
158
    answer = sys.stdin.readline()
 
159
    if string.lower(string.strip(answer))=='y':
 
160
      ca_db.Revoke(serial)
 
161
      sys.stdout.write('Certificate %x in %s marked as revoked.\n' % (serial,ca_name))
 
162
      # CA's private key present <=> we are on the private CA system
 
163
      if os.path.isfile(ca.certificate) and os.path.isfile(ca.private_key):
 
164
        sys.stdout.write('Issue new CRL? (y/n) ')
 
165
        answer = sys.stdin.readline()
 
166
        if string.lower(string.strip(answer))=='y':
 
167
          sys.stdout.write('Issueing new CRL %s.\n' % (ca.crl))
 
168
          rc = os.system('%s ca -config %s -name %s -gencrl -out %s' % \
 
169
                         (openssl.bin_filename,opensslcnfname,ca.sectionname,ca.crl))
 
170
          if rc:
 
171
            sys.stderr.write('Error %d creating CRL %s.\n' % (rc,ca.crl))
 
172
 
 
173
  else:
 
174
    raise ValueError, 'Unknown type field %s in certificate database.' % result[DB_type]
 
175
 
 
176
else:
 
177
  PrintUsage('No certificate found in "%s" with serial %x.\n' % (ca_name,serial))
 
178