Onionr/onionr/onionrcommands/pubkeymanager.py

83 lines
3.5 KiB
Python
Raw Normal View History

2019-03-08 01:08:06 +00:00
'''
Onionr - Private P2P Communication
2019-03-08 01:08:06 +00:00
This module defines user ID-related CLI commands
2019-03-08 01:08:06 +00:00
'''
'''
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 3 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, see <https://www.gnu.org/licenses/>.
'''
2019-03-15 00:18:35 +00:00
import sys, getpass
2019-03-29 17:37:51 +00:00
import logger, onionrexceptions
from onionrutils import stringvalidators, bytesconverter
from onionrusers import onionrusers, contactmanager
2019-08-05 23:09:04 +00:00
import config
2019-07-20 00:01:16 +00:00
from coredb import keydb
2019-07-27 20:29:15 +00:00
import keymanager, onionrcrypto
import unpaddedbase32
2019-07-27 20:29:15 +00:00
from etc import onionrvalues
DETERMINISTIC_REQUIREMENT = onionrvalues.PASSWORD_LENGTH
2019-08-05 23:09:04 +00:00
def add_ID():
2019-07-27 20:29:15 +00:00
key_manager = keymanager.KeyManager()
2019-03-08 01:08:06 +00:00
try:
sys.argv[2]
assert sys.argv[2] == 'true'
except (IndexError, AssertionError) as e:
2019-07-27 20:29:15 +00:00
newID = key_manager.addKey()[0]
2019-03-08 01:08:06 +00:00
else:
logger.warn('Deterministic keys require random and long passphrases.', terminal=True)
logger.warn('If a good passphrase is not used, your key can be easily stolen.', terminal=True)
logger.warn('You should use a series of hard to guess words, see this for reference: https://www.xkcd.com/936/', terminal=True)
try:
pass1 = getpass.getpass(prompt='Enter at least %s characters: ' % (DETERMINISTIC_REQUIREMENT,))
pass2 = getpass.getpass(prompt='Confirm entry: ')
except KeyboardInterrupt:
sys.exit(42)
2019-07-27 20:29:15 +00:00
if onionrcrypto.cryptoutils.safe_compare(pass1, pass2):
2019-03-08 01:08:06 +00:00
try:
logger.info('Generating deterministic key. This can take a while.', terminal=True)
2019-07-27 20:29:15 +00:00
newID, privKey = onionrcrypto.generate_deterministic(pass1)
2019-03-08 01:08:06 +00:00
except onionrexceptions.PasswordStrengthError:
2019-07-27 20:29:15 +00:00
logger.error('Passphrase must use at least %s characters.' % (DETERMINISTIC_REQUIREMENT,), terminal=True)
2019-03-08 01:08:06 +00:00
sys.exit(1)
else:
logger.error('Passwords do not match.', terminal=True)
2019-03-08 01:08:06 +00:00
sys.exit(1)
try:
2019-07-27 20:29:15 +00:00
key_manager.addKey(pubKey=newID,
privKey=privKey)
except ValueError:
logger.error('That ID is already available, you can change to it with the change-id command.', terminal=True)
return
logger.info('Added ID: %s' % (bytesconverter.bytes_to_str(newID),), terminal=True)
2019-03-08 01:08:06 +00:00
2019-08-05 23:09:04 +00:00
def change_ID():
2019-07-27 20:29:15 +00:00
key_manager = keymanager.KeyManager()
2019-03-08 01:08:06 +00:00
try:
key = sys.argv[2]
key = unpaddedbase32.repad(key.encode()).decode()
2019-03-08 01:08:06 +00:00
except IndexError:
logger.warn('Specify pubkey to use', terminal=True)
2019-03-08 01:08:06 +00:00
else:
if stringvalidators.validate_pub_key(key):
2019-07-27 20:29:15 +00:00
if key in key_manager.getPubkeyList():
2019-08-05 23:09:04 +00:00
config.set('general.public_key', key)
config.save()
logger.info('Set active key to: %s' % (key,), terminal=True)
logger.info('Restart Onionr if it is running.', terminal=True)
2019-03-08 01:08:06 +00:00
else:
logger.warn('That key does not exist', terminal=True)
2019-03-08 01:08:06 +00:00
else:
logger.warn('Invalid key %s' % (key,), terminal=True)