]> git.datanom.net - securemail.git/blob - user.py
Make backwards compatible with nacl 1.0.x
[securemail.git] / user.py
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
20 import pickle
21 from db import DBInterface as DBI
22 from cryptonize import Cryptonize
23 from nacl.public import PublicKey
24
25 class NoSuchUser(Exception):
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)
35 else:
36 self.pubkeys = {}
37
38 def store(self, key):
39 crypto = Cryptonize()
40 cipher = crypto.symmetric_encrypt(key, pickle.dumps(self))
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:
47 raise NoSuchUser('{0}: User not found'.format(key))
48 plain = crypto.symmetric_decrypt(key, cipher)
49 try:
50 obj = pickle.loads(plain)
51 self.__dict__.update(obj.__dict__)
52 except pickle.UnpicklingError as e:
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
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):
87 return self._email
88
89 @email.setter
90 def email(self, email):
91 self._email = email
92
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
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))
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))
123 c = Cryptonize()
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])
130 message = "Kill all humans æøåÅØÆ"
131 print ("Message to encrypt: {0}".format(message))
132 encrypted = c.asymmetric_encrypt(keypair1[0], u.get_pubkey('test'), message)
133 print ("Message encrypted: {0}".format(encrypted))
134 plaintext = c.asymmetric_decrypt(keypair2[0], keypair1[1], encrypted)
135 print("Message decrypted: {0}".format(plaintext.decode()))
136 except NoSuchUser:
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)
This page took 0.104152 seconds and 6 git commands to generate.