]>
Commit | Line | Data |
---|---|---|
8c4f590c MR |
1 | # -*- coding: utf-8 -*- |
2 | ||
3 | # Copyright (c) 2018 Michael Rasmussen <mir@datanom.net> | |
4 | ||
5 | # This file is part of SecureMail. | |
6 | ||
7 | # SecureMail is free software: you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
9 | # the Free Software Foundation, either version 3 of the License, or | |
10 | # (at your option) any later version. | |
11 | # | |
12 | # SecureMail is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | # | |
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with SecureMail. If not, see <https://www.gnu.org/licenses/>. | |
19 | ||
d65fab5a | 20 | import pickle |
8c4f590c MR |
21 | from db import DBInterface as DBI |
22 | from cryptonize import Cryptonize | |
d65fab5a | 23 | from nacl.public import PublicKey |
8c4f590c | 24 | |
d65fab5a | 25 | class NoSuchUser(Exception): |
8c4f590c MR |
26 | pass |
27 | ||
28 | class User: | |
29 | """ | |
30 | Class implementing the backend users | |
31 | """ | |
32 | def __init__(self, key=None): | |
33 | if key is not None: | |
34 | self.load(key) | |
d65fab5a MR |
35 | else: |
36 | self.pubkeys = {} | |
8c4f590c MR |
37 | |
38 | def store(self, key): | |
39 | crypto = Cryptonize() | |
481494d3 | 40 | cipher = crypto.symmetric_encrypt(key, pickle.dumps(self)) |
8c4f590c MR |
41 | DBI.store_user(crypto.generate_hash(key), cipher) |
42 | ||
43 | def load(self, key): | |
44 | crypto = Cryptonize() | |
45 | cipher = DBI.load_user(crypto.generate_hash(key)) | |
46 | if cipher is None: | |
d65fab5a | 47 | raise NoSuchUser('{0}: User not found'.format(key)) |
8c4f590c MR |
48 | plain = crypto.symmetric_decrypt(key, cipher) |
49 | try: | |
50 | obj = pickle.loads(plain) | |
d65fab5a | 51 | self.__dict__.update(obj.__dict__) |
8c4f590c | 52 | except pickle.UnpicklingError as e: |
d65fab5a MR |
53 | raise e |
54 | ||
55 | def add_pubkey(self, email, key): | |
56 | if email not in self.pubkeys: | |
57 | self.pubkeys[email] = key.encode() | |
58 | else: | |
59 | raise KeyError('{0}: Exists'.format(email)) | |
60 | ||
61 | def update_pubkey(self, email, key): | |
62 | self.pubkeys[email] = key.encode() | |
63 | ||
64 | def delete_pubkey(self, email): | |
65 | if email in self.pubkeys: | |
66 | del self.pubkeys[email] | |
67 | ||
68 | def get_pubkey(self, email): | |
69 | if email in self.pubkeys: | |
70 | key = self.pubkeys[email] | |
71 | key = PublicKey(key) | |
72 | else: | |
73 | key = None | |
74 | ||
75 | return key | |
76 | ||
8c4f590c MR |
77 | @property |
78 | def name(self): | |
79 | return self._name | |
80 | ||
81 | @name.setter | |
82 | def name(self, name): | |
83 | self._name = name | |
84 | ||
85 | @property | |
86 | def email(self): | |
d65fab5a | 87 | return self._email |
8c4f590c MR |
88 | |
89 | @email.setter | |
90 | def email(self, email): | |
91 | self._email = email | |
92 | ||
d65fab5a MR |
93 | @property |
94 | def pubkeys(self): | |
95 | return self._pubkeys | |
96 | ||
97 | @pubkeys.setter | |
98 | def pubkeys(self, pubkeys): | |
99 | if type(pubkeys) is not type({}): | |
100 | raise ValueError('Not dictionary') | |
101 | self._pubkeys = pubkeys | |
8c4f590c MR |
102 | |
103 | if __name__ == '__main__': | |
104 | try: | |
105 | u = User('test') | |
106 | for attr, value in u.__dict__.items(): | |
107 | print ('{0}: {1}'.format(attr, value)) | |
d65fab5a MR |
108 | print ('{0} - {1} - {2}'.format(u.name, u.email, u.pubkeys)) |
109 | key = '' | |
110 | for i in range(40): | |
111 | key += '{0}'.format(i) | |
112 | u = User() | |
113 | u.name = 'testname1' | |
114 | u.email = 'testname1@securemail.icu' | |
115 | u.pubkeys = {'test': 'some test', 'test1': 'some test 1'} | |
116 | try: | |
117 | u.store(key) | |
118 | except: | |
119 | u = User(key) | |
120 | for attr, value in u.__dict__.items(): | |
121 | print ('{0}: {1}'.format(attr, value)) | |
122 | print ('{0} - {1} - {2}'.format(u.name, u.email, u.pubkeys)) | |
8c4f590c | 123 | c = Cryptonize() |
d65fab5a MR |
124 | keypair1 = c.get_key_pair() |
125 | keypair2 = c.get_key_pair() | |
126 | try: | |
127 | u.add_pubkey('test', keypair2[1]) | |
128 | except KeyError: | |
129 | u.update_pubkey('test', keypair2[1]) | |
d65fab5a | 130 | message = "Kill all humans æøåÅØÆ" |
b49f84de | 131 | print ("Message to encrypt: {0}".format(message)) |
462ce28c | 132 | encrypted = c.asymmetric_encrypt(keypair1[0], u.get_pubkey('test'), message) |
b49f84de | 133 | print ("Message encrypted: {0}".format(encrypted)) |
462ce28c | 134 | plaintext = c.asymmetric_decrypt(keypair2[0], keypair1[1], encrypted) |
b49f84de | 135 | print("Message decrypted: {0}".format(plaintext.decode())) |
d65fab5a | 136 | except NoSuchUser: |
8c4f590c MR |
137 | u = User() |
138 | u.name = 'testname' | |
139 | u.email = 'testname@securemail.icu' | |
140 | u.store('test') | |
141 | except Exception as e: | |
142 | print (e) |