RecodeCertificate.py 3.71 KB
Newer Older
Tobias Dussa's avatar
Tobias Dussa committed
1
2
3
4
5
6
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from OpenSSL import crypto, SSL 
from os.path import basename, dirname, isfile, splitext
from sys import exit
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
from tkinter import Button, E, Entry, Label, LEFT, Tk, W, filedialog, messagebox, simpledialog


# Define password query dialog
class _QueryPasswordDialog(simpledialog._QueryDialog):
    def body(self, master):
        w = Label(master, text=self.prompt, justify=LEFT)
        w.grid(row=0, padx=5, sticky=W)

        self.entry = Entry(master, name="entry")
        self.entry.grid(row=1, padx=5, sticky=W+E)

        self.toggle = Button(master, text="⚷", command=self.toggleShow)
        self.toggle.grid(row=1, padx=5, column=1, sticky=W+E)

        if self.initialvalue is not None:
            self.entry.insert(0, self.initialvalue)
            self.entry.select_range(0, END)

        return self.entry

    def toggleShow(self):
        if self.entry["show"] == "":
            self.entry["show"] = "●"
        else:
            self.entry["show"] = ""

class _QueryPassword(_QueryPasswordDialog):
    def __init__(self, *args, **kw):
        if "show" in kw:
            self.__show = kw["show"]
            del kw["show"]
        else:
            self.__show = "●"
        _QueryPasswordDialog.__init__(self, *args, **kw)

    def body(self, master):
        entry = _QueryPasswordDialog.body(self, master)
        if self.__show is not None:
            entry.configure(show=self.__show)
        return entry

49
50
51
    def getresult(self):
        return self.entry.get()

52
53
54
def askpassword(title, prompt, **kw):
    d = _QueryPassword(title, prompt, **kw)
    return d.result
Tobias Dussa's avatar
Tobias Dussa committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


# Create and hide root window
root = Tk()
root.withdraw()


# Read input file
while True:
    infile = filedialog.askopenfilename(title='P12-Eingabedatei',
                                        filetypes=(('PKCS12-Dateien', ('*.p12', '*.P12', '*.pfx', '*.PFX')),
                                                   ('alle Dateien', '*.*')))
    if not(infile):
        messagebox.showinfo('Abbruch', 'Die Dateiauswahl wurde abgebrochen!')
        exit(0)

    # Verify file exists; if so, break
    if isfile(infile):
        break


# Read password
while True:
78
    password = askpassword('Passworteingabe', 'Passwort des geheimen Schlüssels:')
Tobias Dussa's avatar
Tobias Dussa committed
79
80
81
82
83
84
85
86
    if password is None:
        messagebox.showinfo('Abbruch', 'Die Passworteingabe wurde abgebrochen!')
        exit(0)

    # Break the loop if the password decrypts the private key, shout out
    # otherwise
    try:
        with open(infile, 'rb') as pkcs12file:
Tobias Dussa's avatar
Tobias Dussa committed
87
            p12 = crypto.load_pkcs12(pkcs12file.read(), password.encode('utf-8'))
Tobias Dussa's avatar
Tobias Dussa committed
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
        break
    except crypto.Error:
        messagebox.showinfo('Fehler!', 'Zertifikat konnte nicht geöffnet werden!')


# Read and write output file
while True:
    outfile = filedialog.asksaveasfilename(title='PKCS12-Ausgabedatei',
                                           initialdir=dirname(infile),
                                           initialfile=splitext(basename(infile))[0]+'-neu.p12',
                                           defaultextension='.p12',
                                           filetypes=(('PKCS12-Dateien', ('*.p12', '*.P12', '*.pfx', '*.PFX')),
                                                      ('alle Dateien', '*.*')))
    if not(outfile):
        messagebox.showinfo('Abbruch', 'Die Dateiauswahl wurde abgebrochen!')
        exit(0)

    try:
        with open(outfile, 'wb') as file:
            file.write(p12.export(password.encode('utf-8')))
        break
    except:
        messagebox.showinfo('Fehler!', 'Kann PKCS12-Datei {} nicht schreiben!'.format(outfile))


# All done
messagebox.showinfo('Zertifikat exportiert!', 'Zertifikat erfolgreich in PKCS12-Datei {} exportiert!'.format(outfile))