2018-10-21 16:21:43 +00:00
|
|
|
'''
|
2019-06-20 00:59:05 +00:00
|
|
|
Onionr - Private P2P Communication
|
2018-10-21 16:21:43 +00:00
|
|
|
|
|
|
|
This default plugin allows users to encrypt/decrypt messages without using blocks
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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/>.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Imports some useful libraries
|
2019-03-06 22:39:46 +00:00
|
|
|
import logger, config, threading, time, datetime, sys, json
|
2018-10-21 16:21:43 +00:00
|
|
|
from onionrblockapi import Block
|
2019-06-25 08:21:36 +00:00
|
|
|
from onionrutils import stringvalidators
|
2018-10-21 16:21:43 +00:00
|
|
|
import onionrexceptions, onionrusers
|
|
|
|
import locale
|
|
|
|
locale.setlocale(locale.LC_ALL, '')
|
2019-04-25 05:38:15 +00:00
|
|
|
plugin_name = 'encrypt'
|
2018-10-21 16:21:43 +00:00
|
|
|
|
|
|
|
class PlainEncryption:
|
|
|
|
def __init__(self, api):
|
|
|
|
self.api = api
|
|
|
|
return
|
|
|
|
def encrypt(self):
|
|
|
|
# peer, data
|
|
|
|
plaintext = ""
|
|
|
|
encrypted = ""
|
|
|
|
# detect if signing is enabled
|
|
|
|
sign = True
|
|
|
|
try:
|
|
|
|
if sys.argv[3].lower() == 'false':
|
|
|
|
sign = False
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2019-06-25 08:21:36 +00:00
|
|
|
if not stringvalidators.validate_pub_key(sys.argv[2]):
|
2018-10-21 16:21:43 +00:00
|
|
|
raise onionrexceptions.InvalidPubkey
|
|
|
|
except (ValueError, IndexError) as e:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.error("Peer public key not specified", terminal=True)
|
2018-10-21 16:21:43 +00:00
|
|
|
except onionrexceptions.InvalidPubkey:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.error("Invalid public key", terminal=True)
|
2018-10-21 16:21:43 +00:00
|
|
|
else:
|
|
|
|
pubkey = sys.argv[2]
|
|
|
|
# Encrypt if public key is valid
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info("Please enter your message (ctrl-d or -q to stop):", terminal=True)
|
2018-10-21 16:21:43 +00:00
|
|
|
try:
|
|
|
|
for line in sys.stdin:
|
|
|
|
if line == '-q\n':
|
|
|
|
break
|
|
|
|
plaintext += line
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(1)
|
|
|
|
# Build Message to encrypt
|
|
|
|
data = {}
|
|
|
|
myPub = self.api.get_core()._crypto.pubKey
|
|
|
|
if sign:
|
|
|
|
data['sig'] = self.api.get_core()._crypto.edSign(plaintext, key=self.api.get_core()._crypto.privKey, encodeResult=True)
|
|
|
|
data['sig'] = self.api.get_core()._utils.bytesToStr(data['sig'])
|
|
|
|
data['signer'] = myPub
|
|
|
|
data['data'] = plaintext
|
|
|
|
data = json.dumps(data)
|
|
|
|
plaintext = data
|
2019-02-16 04:08:03 +00:00
|
|
|
encrypted = self.api.get_core()._crypto.pubKeyEncrypt(plaintext, pubkey, encodedData=True)
|
2018-10-21 16:21:43 +00:00
|
|
|
encrypted = self.api.get_core()._utils.bytesToStr(encrypted)
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info('Encrypted Message: \n\nONIONR ENCRYPTED DATA %s END ENCRYPTED DATA' % (encrypted,), terminal=True)
|
2018-11-17 07:23:10 +00:00
|
|
|
|
2018-10-26 00:56:02 +00:00
|
|
|
def decrypt(self):
|
2018-10-21 16:21:43 +00:00
|
|
|
plaintext = ""
|
2018-10-26 00:56:02 +00:00
|
|
|
data = ""
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info("Please enter your message (ctrl-d or -q to stop):", terminal=True)
|
2018-10-26 00:56:02 +00:00
|
|
|
try:
|
|
|
|
for line in sys.stdin:
|
|
|
|
if line == '-q\n':
|
|
|
|
break
|
|
|
|
data += line
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(1)
|
|
|
|
if len(data) <= 1:
|
|
|
|
return
|
2018-10-24 04:54:28 +00:00
|
|
|
encrypted = data.replace('ONIONR ENCRYPTED DATA ', '').replace('END ENCRYPTED DATA', '')
|
|
|
|
myPub = self.api.get_core()._crypto.pubKey
|
2019-02-17 20:21:03 +00:00
|
|
|
decrypted = self.api.get_core()._crypto.pubKeyDecrypt(encrypted, privkey=self.api.get_core()._crypto.privKey, encodedData=True)
|
2018-10-24 04:54:28 +00:00
|
|
|
if decrypted == False:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.error("Decryption failed", terminal=True)
|
2018-10-24 04:54:28 +00:00
|
|
|
else:
|
|
|
|
data = json.loads(decrypted)
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info('Decrypted Message: \n\n%s' % data['data'], terminal=True)
|
2018-10-26 00:56:02 +00:00
|
|
|
try:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info("Signing public key: %s" % (data['signer'],), terminal=True)
|
2018-10-26 00:56:02 +00:00
|
|
|
assert self.api.get_core()._crypto.edVerify(data['data'], data['signer'], data['sig']) != False
|
|
|
|
except (AssertionError, KeyError) as e:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.warn("WARNING: THIS MESSAGE HAS A MISSING OR INVALID SIGNATURE", terminal=True)
|
2018-10-26 00:56:02 +00:00
|
|
|
else:
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info("Message has good signature.", terminal=True)
|
2018-10-21 16:21:43 +00:00
|
|
|
return
|
2018-11-11 03:25:40 +00:00
|
|
|
|
2018-10-21 16:21:43 +00:00
|
|
|
def on_init(api, data = None):
|
|
|
|
'''
|
|
|
|
This event is called after Onionr is initialized, but before the command
|
|
|
|
inputted is executed. Could be called when daemon is starting or when
|
|
|
|
just the client is running.
|
|
|
|
'''
|
|
|
|
|
|
|
|
pluginapi = api
|
|
|
|
encrypt = PlainEncryption(pluginapi)
|
|
|
|
api.commands.register(['encrypt'], encrypt.encrypt)
|
|
|
|
api.commands.register(['decrypt'], encrypt.decrypt)
|
2018-11-17 07:23:10 +00:00
|
|
|
|
2018-11-11 03:25:40 +00:00
|
|
|
return
|