-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy path__init__.py
139 lines (112 loc) · 4.11 KB
/
__init__.py
1
2
3
4
5
6
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
Module for encrypting and decrypting passwords.
Copyright (C) 2014 Radek Novacek <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import os
import stat
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from binascii import hexlify, unhexlify
__all__ = ['InvalidKeyFile', 'UnwritableKeyFile', 'Password']
class InvalidKeyFile(Exception):
pass
class UnwritableKeyFile(Exception):
pass
class Password(object):
KEYFILE = '/var/lib/virt-who/key'
ENCRYPT = 1
DECRYPT = 0
BLOCKSIZE = 16
@staticmethod
def safe_ord(s):
"""
Returns ord(s) if s is a string else if s is an int, returns the int other exceptions are
raised
:param s: str or int
:return: int
"""
if isinstance(s, int):
return s
return ord(s)
@classmethod
def _pad(cls, s):
padding_amount = cls.BLOCKSIZE - len(s) % cls.BLOCKSIZE
return s + (chr(padding_amount) * padding_amount).encode('ascii')
@classmethod
def _unpad(cls, s):
return s[0:-Password.safe_ord(s[-1])]
@classmethod
def _crypt(cls, op, key, iv, data):
key = unhexlify(key)[:cls.BLOCKSIZE]
iv = unhexlify(iv)[:cls.BLOCKSIZE]
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
if op == Password.ENCRYPT:
crypter = cipher.encryptor()
elif op == Password.DECRYPT:
crypter = cipher.decryptor()
else:
raise ValueError("Unable to perform op '%s'" % op)
value = crypter.update(data) + crypter.finalize()
return value
@classmethod
def encrypt(cls, password):
key, iv = cls._read_or_generate_key_iv()
if isinstance(password, str):
password = password.encode('utf-8')
return cls._crypt(cls.ENCRYPT, key, iv, cls._pad(password))
@classmethod
def decrypt(cls, enc):
try:
key, iv = cls._read_key_iv()
if isinstance(enc, str):
enc = enc.encode('utf-8')
return cls._unpad(cls._crypt(cls.DECRYPT, key, iv, enc)).decode('utf-8')
except TypeError:
raise InvalidKeyFile("Encryption key is invalid")
@classmethod
def _read_key_iv(cls):
try:
with open(cls.KEYFILE, 'r') as f:
key = f.readline().strip()
iv = f.readline().strip()
if not iv or not key:
raise InvalidKeyFile("Invalid format")
return key, iv
except IOError as e:
raise InvalidKeyFile(str(e))
@classmethod
def _can_write(cls):
return os.getuid() == 0
@classmethod
def _read_or_generate_key_iv(cls):
try:
return cls._read_key_iv()
except InvalidKeyFile:
pass
if not cls._can_write():
raise UnwritableKeyFile("Only root can write keyfile")
key = hexlify(cls._generate_key())
iv = hexlify(cls._generate_key())
try:
with open(cls.KEYFILE, 'w') as f:
f.write("%s\n%s\n" % (key.decode(), iv.decode()))
except IOError as e:
raise UnwritableKeyFile(str(e))
os.chmod(cls.KEYFILE, stat.S_IRUSR | stat.S_IWUSR)
return key, iv
@classmethod
def _generate_key(cls):
return os.urandom(cls.BLOCKSIZE)