~ubuntu-branches/debian/sid/ca-certificates/sid

« back to all changes in this revision

Viewing changes to mozilla/certdata2pem.py

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Kern
  • Date: 2009-07-09 10:35:39 UTC
  • mfrom: (9.1.4 karmic)
  • Revision ID: james.westby@ubuntu.com-20090709103539-yntccp0wqnf4a361
Tags: 20090709
Fix purge by checking for `/etc/ssl/certs' first.  (Closes: #536331)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# vim:set et sw=4:
 
3
#
 
4
# certdata2pem.py - splits certdata.txt into multiple files
 
5
#
 
6
# Copyright (C) 2009 Philipp Kern <pkern@debian.org>
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program; if not, write to the Free Software
 
20
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
 
21
# USA.
 
22
 
 
23
import base64
 
24
import os.path
 
25
import re
 
26
import sys
 
27
import textwrap
 
28
 
 
29
objects = []
 
30
 
 
31
# Dirty file parser.
 
32
in_data, in_multiline, in_obj = False, False, False
 
33
field, type, value, obj = None, None, None, dict()
 
34
for line in open('certdata.txt', 'r'):
 
35
    # Ignore the file header.
 
36
    if not in_data:
 
37
        if line.startswith('BEGINDATA'):
 
38
            in_data = True
 
39
        continue
 
40
    # Ignore comment lines.
 
41
    if line.startswith('#'):
 
42
        continue
 
43
    # Empty lines are significant if we are inside an object.
 
44
    if in_obj and len(line.strip()) == 0:
 
45
        objects.append(obj)
 
46
        obj = dict()
 
47
        in_obj = False
 
48
        continue
 
49
    if len(line.strip()) == 0:
 
50
        continue
 
51
    if in_multiline:
 
52
        if not line.startswith('END'):
 
53
            if type == 'MULTILINE_OCTAL':
 
54
                line = line.strip()
 
55
                for i in re.finditer(r'\\([0-3][0-7][0-7])', line):
 
56
                    value += chr(int(i.group(1), 8))
 
57
            else:
 
58
                value += line
 
59
            continue
 
60
        obj[field] = value
 
61
        in_multiline = False
 
62
        continue
 
63
    if line.startswith('CKA_CLASS'):
 
64
        in_obj = True
 
65
    line_parts = line.strip().split(' ', 2)
 
66
    if len(line_parts) > 2:
 
67
        field, type = line_parts[0:2]
 
68
        value = ' '.join(line_parts[2:])
 
69
    elif len(line_parts) == 2:
 
70
        field, type = line_parts
 
71
        value = None
 
72
    else:
 
73
        raise NotImplementedError, 'line_parts < 2 not supported.'
 
74
    if type == 'MULTILINE_OCTAL':
 
75
        in_multiline = True
 
76
        value = ""
 
77
        continue
 
78
    obj[field] = value
 
79
if len(obj.items()) > 0:
 
80
    objects.append(obj)
 
81
 
 
82
# Read blacklist.
 
83
blacklist = []
 
84
if os.path.exists('blacklist.txt'):
 
85
    for line in open('blacklist.txt', 'r'):
 
86
        line = line.strip()
 
87
        if line.startswith('#') or len(line) == 0:
 
88
            continue
 
89
        item = line.split('#', 1)[0].strip()
 
90
        blacklist.append(item)
 
91
 
 
92
# Build up trust database.
 
93
trust = dict()
 
94
for obj in objects:
 
95
    if obj['CKA_CLASS'] != 'CKO_NETSCAPE_TRUST':
 
96
        continue
 
97
    if obj['CKA_LABEL'] in blacklist:
 
98
        print "Certificate %s blacklisted, ignoring." % obj['CKA_LABEL']
 
99
    elif obj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NETSCAPE_TRUSTED_DELEGATOR':
 
100
        trust[obj['CKA_LABEL']] = True
 
101
    elif obj['CKA_TRUST_EMAIL_PROTECTION'] == 'CKT_NETSCAPE_TRUSTED_DELEGATOR':
 
102
        trust[obj['CKA_LABEL']] = True
 
103
    elif obj['CKA_TRUST_SERVER_AUTH'] == 'CKT_NETSCAPE_UNTRUSTED':
 
104
        print '!'*74
 
105
        print "UNTRUSTED BUT NOT BLACKLISTED CERTIFICATE FOUND: %s" % obj['CKA_LABEL']
 
106
        print '!'*74
 
107
    else:
 
108
        print "Ignoring certificate %s.  SAUTH=%s, EPROT=%s" % \
 
109
              (obj['CKA_LABEL'], obj['CKA_TRUST_SERVER_AUTH'],
 
110
               obj['CKA_TRUST_EMAIL_PROTECTION'])
 
111
 
 
112
for obj in objects:
 
113
    if obj['CKA_CLASS'] == 'CKO_CERTIFICATE':
 
114
        if not obj['CKA_LABEL'] in trust or not trust[obj['CKA_LABEL']]:
 
115
            continue
 
116
        fname = obj['CKA_LABEL'][1:-1].replace('/', '_')\
 
117
                                      .replace(' ', '_')\
 
118
                                      .replace('(', '=')\
 
119
                                      .replace(')', '=')\
 
120
                                      .replace(',', '_') + '.crt'
 
121
        f = open(fname, 'w')
 
122
        f.write("-----BEGIN CERTIFICATE-----\n")
 
123
        f.write("\n".join(textwrap.wrap(base64.b64encode(obj['CKA_VALUE']), 64)))
 
124
        f.write("\n-----END CERTIFICATE-----\n")
 
125