76 lines
3.2 KiB
Python
Executable File
76 lines
3.2 KiB
Python
Executable File
#===========================================================================================
|
|
# _____ _____ _ _ _ __ __ ___ ____ _____ _____ ____ _ _
|
|
# |_ _| ____| \ | | / \ | \/ |/ _ \| _ \_ _| ____/ ___| | | |
|
|
# | | | _| | \| | / _ \ | |\/| | | | | |_) || | | _|| | | |_| |
|
|
# | | | |___| |\ |/ ___ \| | | | |_| | _ < | | | |__| |___| _ |
|
|
# |_| |_____|_| \_/_/ \_\_| |_|\___/|_| \_\|_| |_____\____|_| |_|
|
|
#
|
|
# RSA Server Keys Manager V1 (Python 3.6)
|
|
#
|
|
# Sources:
|
|
# https://nitratine.net/blog/post/asymmetric-encryption-and-decryption-in-python/
|
|
# https://stackoverflow.com/questions/8933237/how-to-find-if-directory-exists-in-python
|
|
# https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-nested-directory
|
|
#
|
|
#===========================================================================================
|
|
|
|
# Basic cryptography tools
|
|
from cryptography.hazmat.backends import default_backend
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
|
|
# To format created key obj into text to be used/exported
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
# To get user home directory
|
|
from pathlib import Path
|
|
|
|
# Home made RSA Keys lib
|
|
from utils.rsa_tenamortech_utils import *
|
|
|
|
def check_for_existing_keys():
|
|
keys_dir_path = Path.home() / '.auth_server_test' / 'server'
|
|
private_key_path = keys_dir_path / 'id_rsa'
|
|
public_key_path = keys_dir_path / 'id_rsa.pub'
|
|
|
|
# =========================================================
|
|
# Private key file exists ? Load it
|
|
# --- Public key file exists ? Load it
|
|
# --- No Public key file ? Generate it from the Private
|
|
# No Private key file ? Generate both keys (save as well)
|
|
# =========================================================
|
|
|
|
if(private_key_path.exists()):
|
|
print('Find private key from file [ OK ]')
|
|
# Load private key from file
|
|
print('Load private key from file [ ... ]')
|
|
with open(str(private_key_path), "rb") as key_file:
|
|
private_key = serialization.load_pem_private_key(
|
|
key_file.read(),
|
|
password=None,
|
|
backend=default_backend()
|
|
)
|
|
print('Load private key from file [ OK ]')
|
|
if(public_key_path.exists()):
|
|
print('Find public key from file [ OK ]')
|
|
# Load public key from file
|
|
print('Load public key from file [ ... ]')
|
|
with open(str(public_key_path), "rb") as key_file:
|
|
public_key = serialization.load_pem_public_key(
|
|
key_file.read(),
|
|
backend=default_backend()
|
|
)
|
|
print('Load public key from file [ OK ]')
|
|
# Return keys
|
|
return (private_key,public_key)
|
|
else:
|
|
print('Find public key from file [ FAIL ]')
|
|
print('Generating public key from private key [ ... ]')
|
|
public_key = generate_public_key(0,private_key)
|
|
print('Generating public key from private key [ OK ]')
|
|
return (private_key,public_key)
|
|
else:
|
|
print('Find private key from file [ FAIL ]')
|
|
print('New keys will be generated')
|
|
return generate_keys(0)
|
|
#check_for_existing_keys()
|