Commit 7e84a43a authored by Shashank Kumar's avatar Shashank Kumar

Encryption tools added

- Create key pair
- Display and manage key pair
- Encrypt a message
- Decrypt a message
parent 67647ae4
......@@ -8,7 +8,6 @@ before_script:
- apt-get install -y ffmpeg git
- pip3 install pipenv
- pipenv install --skip-lock
- pipenv install --skip-lock git+http://github.com/kivy/kivy.git#egg=kivy-1.10.1
pylint:
type: test
......
......@@ -11,6 +11,7 @@ ipdb = "==0.11"
[packages]
cython = "==0.28"
kivy = "==1.10.1"
python-gnupg = "==0.4.3"
requests = "==2.19.1"
......
{
"_meta": {
"hash": {
"sha256": "7e795eb3ea979205c92d0da17b18b772855d81c1f510165db414636c3e46eb46"
"sha256": "9c2f51827b73133cef042216d7ac81263a29b082dd28e6799c096cbaa9417ef4"
},
"pipfile-spec": 6,
"requires": {
......@@ -66,6 +66,14 @@
"index": "pypi",
"version": "==0.28"
},
"docutils": {
"hashes": [
"sha256:02aec4bd92ab067f6ff27a38a38a41173bf01bed8f89157768c1573f53e474a6",
"sha256:51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274",
"sha256:7a4bd47eaf6596e1295ecb11361139febe29b084a87bf005bf899f9a42edc3c6"
],
"version": "==0.14"
},
"idna": {
"hashes": [
"sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e",
......@@ -73,6 +81,39 @@
],
"version": "==2.7"
},
"kivy": {
"hashes": [
"sha256:05985f3e16065a476293695cef39f0b8faa81353475692b8c60ca2530ba08175",
"sha256:44a64f72182dd40aceef2041259359513dafeeecc0891d2d23056799d51babb0",
"sha256:48a0d3edf8fbc9206ebb7abae8d7e2017c5d5f81cf6e76c2e5510193d607420e",
"sha256:6bbeccc9c52a264fb53b7aaa344d865f054da09cc08e15b482754c2d4e3a5cfd",
"sha256:7131a24e30b80f9b7b6e7a1c124f0b5002ef58de35856aff38762b216a4e77fb",
"sha256:77cd45a1aee9061c7474020def1b7b31eb4665e5424c45c23a6a7e4f5afee6b9",
"sha256:7ce9e88b75de47a3f1d52cbe6924c18cafc83fa102e54f6794d241746e93fdff",
"sha256:8f912c0742c7f9582676106ae5a526c3f38bc01a196edd26186c74975b08fad0",
"sha256:952d7483b4f29ebeabb612897cdfdcdd12c33f1bdb51fb0082d36dfcf543ec0b",
"sha256:b0ec00fd3a6dbe56c31510151e8bb14f54390dbc31c5047d61aa90d069c5ec04",
"sha256:c4ad43fb04c899be27baa5a6e382c79620fc3602213221cb97da2280f345b2d6",
"sha256:cb6f052d69748a7db70970e5a8ee3d96506751fcfcc8bf979424fcd788473037",
"sha256:d1113ee88eaff5127ecc05121570f1f2adec21d7f1699ef1c6c2bf4396e51578"
],
"index": "pypi",
"version": "==1.10.1"
},
"kivy-garden": {
"hashes": [
"sha256:9b7d9de5efacbcd0c4b3dd873b30622a86093c9965aa47b523c7a32f3eb34610",
"sha256:c256f42788421273a08fbb0a228f0fb0e80dd86b629fb8c0920507f645be6c72"
],
"version": "==0.1.4"
},
"pygments": {
"hashes": [
"sha256:78f3f434bcc5d6ee09020f92ba487f95ba50f1e3ef83ae96b9d5ffa1bab25c5d",
"sha256:dbae1046def0efb574852fab9e90209b23f556367b5a320c0bcb871c77c3e8cc"
],
"version": "==2.2.0"
},
"python-gnupg": {
"hashes": [
"sha256:2d158dfc6b54927752b945ebe57e6a0c45da27747fa3b9ae66eccc0d2147ac0d",
......@@ -131,10 +172,13 @@
"sha256:03481e81d558d30d230bc12999e3edffe392d244349a90f4ef9b88425fac74ba",
"sha256:0b136648de27201056c1869a6c0d4e23f464750fd9a9ba9750b8336a244429ed",
"sha256:104ab3934abaf5be871a583541e8829d6c19ce7bde2923b2751e0d3ca44db60a",
"sha256:10a46017fef60e16694a30627319f38a2b9b52e90182dddb6e37dcdab0f4bf95",
"sha256:15b111b6a0f46ee1a485414a52a7ad1d703bdf984e9ed3c288a4414d3871dcbd",
"sha256:198626739a79b09fa0a2f06e083ffd12eb55449b5f8bfdbeed1df4910b2ca640",
"sha256:1c383d2ef13ade2acc636556fd544dba6e14fa30755f26812f54300e401f98f2",
"sha256:23d341cdd4a0371820eb2b0bd6b88f5003a7438bbedb33688cd33b8eae59affd",
"sha256:28b2191e7283f4f3568962e373b47ef7f0392993bb6660d079c62bd50fe9d162",
"sha256:2a5b73210bad5279ddb558d9a2bfedc7f4bf6ad7f3c988641d83c40293deaec1",
"sha256:2eb564bbf7816a9d68dd3369a510be3327f1c618d2357fa6b1216994c2e3d508",
"sha256:337ded681dd2ef9ca04ef5d93cfc87e52e09db2594c296b4a0a3662cb1b41249",
"sha256:3a2184c6d797a125dca8367878d3b9a178b6fdd05fdc2d35d758c3006a1cd694",
......@@ -261,10 +305,10 @@
},
"parso": {
"hashes": [
"sha256:8105449d86d858e53ce3e0044ede9dd3a395b1c9716c696af8aa3787158ab806",
"sha256:d250235e52e8f9fc5a80cc2a5f804c9fefd886b2e67a2b1099cf085f403f8e33"
"sha256:35704a43a3c113cce4de228ddb39aab374b8004f4f2407d070b6a2ca784ce8a2",
"sha256:895c63e93b94ac1e1690f5fdd40b65f07c8171e3e53cbd7793b5b96c0e0a7f24"
],
"version": "==0.3.0"
"version": "==0.3.1"
},
"pexpect": {
"hashes": [
......
[
{"application_settings": 1},
{"blog": 1},
{"cli": 1},
{"communication": 1},
{"application_settings": []},
{"blog": []},
{"cli": []},
{"communication": []},
{"encryption":[
{
"tutorials": 0
"tutorials": []
},
{
"tools": [
......@@ -27,9 +27,9 @@
}
]
},
{"how_to_use": 1},
{"profile_settings": 1},
{"theme_settings": 1},
{"vcs": 1},
{"way_ahead": 1}
{"how_to_use": []},
{"profile_settings": []},
{"theme_settings": []},
{"vcs": []},
{"way_ahead": []}
]
......@@ -52,11 +52,7 @@ Step 4: Install application dependencies (this might take a while, grab a cup of
`$ pipenv install`
Step 5: Install Kivy
`$ pipenv install --skip-lock git+http://github.com/kivy/kivy.git#egg=kivy-1.10.1`
Step 6: Run New Contributor Wizard
Step 5: Run New Contributor Wizard
`$ pipenv run python main.py`
......
'''
Modules containing Encryption classes
'''
from copy import copy as cp
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
......@@ -37,10 +35,11 @@ class Encryption(BoxLayout):
self.ids[option_to_enable + '_box'].canvas.before.children[0].rgba = [1, 1, 1, 1]
self.ids[option_to_enable].color = (0, 0, 0, 1)
all_options_items_copy = cp(self.all_options_items)
all_options_items_copy.remove(option_to_enable)
# removing enabled option from the list in order to disable all other options
options_to_disable = self.all_options_items[:]
options_to_disable.remove(option_to_enable)
for option in all_options_items_copy:
for option in options_to_disable:
self.ids[option + '_box'].canvas.before.children[0].rgba = [0, 0, 0, 1]
self.ids[option].color = (1, 1, 1, 1)
self.ids['encryption_content_box'].remove_widget(self.all_options[option])
......
......@@ -4,25 +4,27 @@ This modules helps display and manage key pairs
import logging
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.popup import Popup
from modules.encryption.tools.all_tools.exceptions import GPGError
from modules.encryption.tools.all_tools.services import create_key_pair as ckp
from modules.encryption.tools.all_tools.services import create_gpg_key_pair
from modules.encryption.tools.all_tools.validations import (
validate_email,
validate_name,
validate_comment,
validate_expire_date
validate_email, validate_name, validate_comment, validate_expire_date
)
Builder.load_file('./ui/encryption/all_tools/create_key_pair.kv')
class CreateKeyPairPopup(Popup):
'''
CreateKeyPairPopup displays popup to inform about key creation progress
'''
pass
class CreateKeyPair(BoxLayout):
'''
CreateKeyPair helps integrate UI with helper functions
......@@ -48,7 +50,7 @@ class CreateKeyPair(BoxLayout):
args[0]
)
self.ids[label].text = original_text
self.ids[label].color = [1, 1, 1, 1]
self.ids[label].color = [0, 0, 0, 1]
Clock.schedule_once(replace_label, 2)
def validate(self):
......@@ -99,25 +101,17 @@ class CreateKeyPair(BoxLayout):
return validation_successful
def create_key_pair(self):
def creating_key_pair(self):
'''
Processing user input to create key pair
'''
content_for_popup = BoxLayout(size_hint=(1, 0.8))
content_for_popup.add_widget(Label(text='Key creation in progress'))
popup = Popup(title='Test popup', content=content_for_popup,
auto_dismiss=False, size_hint=(None, None), size=(400, 300))
popup.children[0].children[0].orientation = 'vertical'
popup.children[0].children[0].add_widget(BoxLayout(size_hint=(1, 0.2)))
creating_key_pair_popup = CreateKeyPairPopup()
def add_button(*args):
'''
Replace original popup text and add button
Add Key fingerprint to label and button
'''
popup.children[0].children[0].children[1].children[0].text = args[0]
popup.children[0].children[0].children[0].add_widget(
Button(text='Okay', on_press=popup.dismiss)
)
creating_key_pair_popup.ids['create_gpg_key_pair_popup_label'].text = args[0]
if self.validate():
name = self.ids['input_name'].text
......@@ -126,8 +120,8 @@ class CreateKeyPair(BoxLayout):
expire_date = self.ids['input_expire_date'].text
try:
popup.open()
key_fingerprint = ckp(
creating_key_pair_popup.open()
key_fingerprint = create_gpg_key_pair(
name=name,
email=email,
comment=comment,
......
......@@ -2,14 +2,138 @@
This modules helps decrypt a message using private key
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder
from modules.encryption.tools.all_tools.utils import extract_key_uid_info
from modules.encryption.tools.all_tools.exceptions import GPGError
from modules.encryption.tools.all_tools.services import (
list_gpg_all_keys, decrypt_message
)
Builder.load_file('./ui/encryption/all_tools/decrypt_a_message.kv')
class KeyColumnBox(GridLayout):
'''
KeyColumnBox helps contain widgets and display them at the center of the box
'''
class DecryptKeyBox(BoxLayout):
'''
DecryptKeyBox helps contain widgets and display them at the center of the box
'''
class KeyInfoLabel(Label):
'''
KeyInfoLabel helps display key information
'''
class DisplayAndManageKeyPair(BoxLayout):
'''
DisplayAndManageKeyPair helps integrate UI with helper functions
'''
class DecryptMessagePopup(Popup):
'''
DecryptMessagePopup helps take encrypted message and passphrase and display
decrypted message on a popup
'''
class NoKeyPresentDecrypt(BoxLayout):
'''
NoKeyPresentDecrypt will be displayed in case if no key is present
'''
class DecryptAMessage(BoxLayout):
'''
DecryptAMessage integrated UI with helper functions
'''
pass
def __init__(self, **kwargs):
super(DecryptAMessage, self).__init__(**kwargs)
all_keys = list_gpg_all_keys()
self.populate_with_keys(all_keys['all_private_keys'])
def populate_with_keys(self, all_private_keys):
'''
populate_with_keys displays all the available private keys that can be
used to decrypt message
'''
if all_private_keys.items():
for fingerprint, key_info in all_private_keys.items():
user_info = extract_key_uid_info(key_info['uids'][0])
key_column_box = KeyColumnBox()
key_box = DecryptKeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.decrypt_message_popup
)
key_column_box.add_widget(key_box)
key_info_label = KeyInfoLabel(
text='\n'.join(user_info.values())
)
key_column_box.add_widget(key_info_label)
# pylint: disable=invalid-name
FINGERPRINT_LENGTH_BY_HALF = len(fingerprint) // 2
fingerprint_text = fingerprint[:FINGERPRINT_LENGTH_BY_HALF] + '\n' +\
fingerprint[FINGERPRINT_LENGTH_BY_HALF:]
key_info_label = KeyInfoLabel(text=fingerprint_text)
key_column_box.add_widget(key_info_label)
self.ids['scroll_view_box'].add_widget(key_column_box)
else:
self.ids['scroll_view_box'].clear_widgets()
self.ids['scroll_view_box'].add_widget(NoKeyPresentDecrypt())
@staticmethod
def decrypt_message_popup(*args):
'''
decrypt_message_popup allows user to decrypt a message using passphrase
and encrypted message
'''
decrypt_message_popup_object = DecryptMessagePopup()
def get_decrypted_message(*args):
'''
get_decrypted_message receives all the data from input box in order to
fetch decrypted message
'''
if args:
encrypted_message = decrypt_message_popup_object.ids['encrypted_message'].text
passphrase = decrypt_message_popup_object.ids['passphrase_input'].text
decrypt_message_object = decrypt_message_popup_object.ids['decrypted_message']
if not passphrase:
decrypt_message_object.text = 'Enter Passphrase'
elif encrypted_message:
try:
decrypted_message = decrypt_message(encrypted_message, passphrase)
if decrypted_message:
decrypt_message_object.text = decrypted_message
else:
decrypt_message_object.text = 'Incorrect Passphrase'
except GPGError as error:
decrypt_message_object.text = error.message
else:
decrypt_message_object.text = ''
decrypt_message_popup_object.ids['encrypted_message'].bind(
text=get_decrypted_message
)
decrypt_message_popup_object.ids['passphrase_input'].bind(
text=get_decrypted_message
)
if args:
decrypt_message_popup_object.open()
......@@ -2,14 +2,142 @@
This modules helps display and manage key pairs
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder
from modules.encryption.tools.all_tools.utils import extract_key_uid_info
from modules.encryption.tools.all_tools.services import (
list_gpg_all_keys, export_single_key
)
Builder.load_file('./ui/encryption/all_tools/display_and_manage_key_pair.kv')
class KeyColumnBox(GridLayout):
'''
KeyColumnBox helps contain widgets and display them at the center of the box
'''
class KeyBox(BoxLayout):
'''
KeyBox helps contain widgets and display them at the center of the box
'''
class KeyInfoLabel(Label):
'''
KeyInfoLabel helps display key information
'''
class PublicKeyPopup(Popup):
'''
PublicKeyPopup helps display Public Key in UTF-8 inside a popup
'''
class PrivateKeyPopup(Popup):
'''
PrivateKeyPopup helps display Private Key in UTF-8 inside a popup
'''
class NoKeyPresent(BoxLayout):
'''
NoKeyPresent will be displayed in case if no key is present
'''
class DisplayAndManageKeyPair(BoxLayout):
'''
DisplayAndManageKeyPair helps integrate UI with helper functions
'''
pass
def __init__(self, **kwargs):
super(DisplayAndManageKeyPair, self).__init__(**kwargs)
all_keys = list_gpg_all_keys()
self.populate_with_keys(
all_keys['all_public_keys'],
all_keys['all_private_keys']
)
def populate_with_keys(self, all_public_keys, all_private_keys):
'''
populate_with_keys displays all the available public and private keys
'''
if all_public_keys.items():
for fingerprint, key_info in all_public_keys.items():
user_info = extract_key_uid_info(key_info['uids'][0])
key_column_box = KeyColumnBox()
key_info_label = KeyInfoLabel(
text='\n'.join(user_info.values())
)
key_column_box.add_widget(key_info_label)
# Spliting fingerprint to multiple lines to make it better on the UI
# pylint: disable=invalid-name
FINGERPRINT_LENGTH_BY_HALF = len(fingerprint) // 2
fingerprint_text = fingerprint[:FINGERPRINT_LENGTH_BY_HALF] + '\n' +\
fingerprint[FINGERPRINT_LENGTH_BY_HALF:]
key_info_label = KeyInfoLabel(text=fingerprint_text)
key_column_box.add_widget(key_info_label)
key_box = KeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.display_public_key
)
key_column_box.add_widget(key_box)
if fingerprint in all_private_keys.keys():
key_box = KeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.display_private_key
)
key_column_box.add_widget(key_box)
self.ids['scroll_view_box'].add_widget(key_column_box)
else:
self.ids['scroll_view_box'].clear_widgets()
self.ids['scroll_view_box'].add_widget(NoKeyPresent())
@staticmethod
def display_public_key(*args):
'''
display_public_key helps display public key in plain text to the correspoding
key fingerprint
'''
fingerprint = args[0].id
public_key_popup = PublicKeyPopup()
public_key = export_single_key(fingerprint)
public_key_popup.ids['public_key_text_display'].text = public_key
public_key_popup.open()
@staticmethod
def display_private_key(*args):
'''
display_private_key helps display private key in plain text to the correspoding
key fingerprint and passphrase
'''
fingerprint = args[0].id
def get_private_key(*args):
'''
get_private_key calls helper function to get private key in plain text
'''
passphrase = args[1]
if passphrase:
private_key = export_single_key(fingerprint, passphrase)
if private_key:
private_key_popup.ids['private_key_text_display'].text = private_key
else:
private_key_popup.ids['private_key_text_display'].text = 'Incorrect Passphrase'
private_key_popup = PrivateKeyPopup()
private_key_popup.ids['private_key_passphrase_input'].bind(
text=get_private_key
)
private_key_popup.open()
......@@ -2,14 +2,122 @@
This module helps encrypt a message using public key
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.lang import Builder
from modules.encryption.tools.all_tools.utils import extract_key_uid_info
from modules.encryption.tools.all_tools.services import (
list_gpg_all_keys, encrypt_message
)
Builder.load_file('./ui/encryption/all_tools/encrypt_a_message.kv')
class KeyColumnBox(GridLayout):
'''
KeyColumnBox helps contain widgets and display them at the center of the box
'''
class EncryptKeyBox(BoxLayout):
'''
EncryptKeyBox helps contain widgets and display them at the center of the box
'''
class KeyInfoLabel(Label):
'''
KeyInfoLabel helps display key information
'''
class DisplayAndManageKeyPair(BoxLayout):
'''
DisplayAndManageKeyPair helps integrate UI with helper functions
'''
class EncryptMessagePopup(Popup):
'''
EncryptMessagePopup helps encrypt plain text into encrypted message
'''
class NoKeyPresentEncrypt(BoxLayout):
'''
NoKeyPresentEncrypt will be displayed in case if no key is present
'''
class EncryptAMessage(BoxLayout):
'''
DEncryptAMessage integrated UI with helper functions
EncryptAMessage integrated UI with helper functions
'''
pass
def __init__(self, **kwargs):
super(EncryptAMessage, self).__init__(**kwargs)
all_keys = list_gpg_all_keys()
self.populate_with_keys(all_keys['all_public_keys'])
def populate_with_keys(self, all_public_keys):
'''
populate_with_keys displays all available public keys
'''
if all_public_keys.items():
for fingerprint, key_info in all_public_keys.items():
user_info = extract_key_uid_info(key_info['uids'][0])
key_column_box = KeyColumnBox()
key_box = EncryptKeyBox()
key_box.ids['Key_button'].id = fingerprint
key_box.ids['Key_button'].bind(
on_press=self.encrypt_message_popup
)
key_column_box.add_widget(key_box)
key_info_label = KeyInfoLabel(
text='\n'.join(user_info.values())
)
key_column_box.add_widget(key_info_label)
# Spliting fingerprint to multiple lines to make it better on the UI
# pylint: disable=invalid-name
FINGERPRINT_LENGTH_BY_HALF = len(fingerprint) // 2
fingerprint_text = fingerprint[:FINGERPRINT_LENGTH_BY_HALF] + '\n' +\
fingerprint[FINGERPRINT_LENGTH_BY_HALF:]
key_info_label = KeyInfoLabel(text=fingerprint_text)
key_column_box.add_widget(key_info_label)
self.ids['scroll_view_box'].add_widget(key_column_box)
else:
self.ids['scroll_view_box'].clear_widgets()
self.ids['scroll_view_box'].add_widget(NoKeyPresentEncrypt())
@staticmethod
def encrypt_message_popup(*args):
'''
encrypt_message_popup allows user to encrypt a message from plain text
'''
fingerprint = args[0].id
encrypt_message_popup_object = EncryptMessagePopup()
def get_encrypted_message(*args):
'''
get_encrypted_message receives all the data from input box in order to
fetch encrypted message
'''
plain_message = args[1]
if plain_message:
encrypted_message = encrypt_message(fingerprint, plain_message)
encrypt_message_popup_object.ids['encrypted_message'].text = encrypted_message
else:
encrypt_message_popup_object.ids['encrypted_message'].text = ''
encrypt_message_popup_object.ids['plain_message'].bind(
text=get_encrypted_message
)
encrypt_message_popup_object.open()
......@@ -6,6 +6,7 @@ import gnupg
from modules.encryption.tools.all_tools.exceptions import GPGError
GNUPG_HOME = 'gnupg_home'
IS_KEY_PRIVATE = True
def create_gnupg_object():
......@@ -17,7 +18,7 @@ def create_gnupg_object():
return gpg
def create_key_pair(name, email, comment, expire_date, passphrase=''):
def create_gpg_key_pair(name, email, comment, expire_date, passphrase=''):
'''
Creating fresh public and private keys and returning key's fingerprint
'''
......@@ -38,55 +39,51 @@ def create_key_pair(name, email, comment, expire_date, passphrase=''):
key = gpg.gen_key(input_data)
if key.fingerprint:
return key.fingerprint
else:
raise GPGError(message=getattr(key, 'stderr'))
raise GPGError(message=getattr(key, 'stderr'))
def list_all_keys():
def list_gpg_all_keys():
'''
Returning all public and private keys available in keyring
'''
gpg = create_gnupg_object()
# a key_map return a dictionary mapping key and subkey fingerprints to the
# corresponding key’s dictionary
return {
'all_public_keys': gpg.list_keys().key_map,
'all_private_keys': gpg.list_keys(True).key_map
'all_private_keys': gpg.list_keys(IS_KEY_PRIVATE).key_map
}
def export_single_key(keyid, passphrase=None):
def export_single_key(key_id, passphrase=None):
'''
Returning public or private key in ascii format