2015-10-20 02:03:18 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2018-01-05 04:42:49 -06:00
|
|
|
# Copyright (C) 2013 - 2018, The pgAdmin Development Team
|
2015-10-20 02:03:18 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
#########################################################################
|
2016-01-18 08:48:14 -06:00
|
|
|
|
2015-10-20 02:03:18 -05:00
|
|
|
"""This File Provides Cryptography."""
|
|
|
|
|
|
|
|
import base64
|
2016-05-13 02:51:20 -05:00
|
|
|
import hashlib
|
2015-10-20 02:03:18 -05:00
|
|
|
|
2016-06-21 08:12:14 -05:00
|
|
|
from Crypto import Random
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
2015-11-06 04:23:19 -06:00
|
|
|
padding_string = b'}'
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def encrypt(plaintext, key):
|
|
|
|
"""
|
|
|
|
Encrypt the plaintext with AES method.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
plaintext -- String to be encrypted.
|
|
|
|
key -- Key for encryption.
|
|
|
|
"""
|
|
|
|
|
|
|
|
iv = Random.new().read(AES.block_size)
|
2018-04-11 07:45:59 -05:00
|
|
|
cipher = AES.new(pad(key), AES.MODE_CFB, iv)
|
2017-02-16 05:00:40 -06:00
|
|
|
# If user has entered non ascii password (Python2)
|
|
|
|
# we have to encode it first
|
2018-04-11 07:45:59 -05:00
|
|
|
if hasattr(str, 'decode'):
|
|
|
|
plaintext = plaintext.encode('utf-8')
|
2017-02-16 05:00:40 -06:00
|
|
|
encrypted = base64.b64encode(iv + cipher.encrypt(plaintext))
|
2015-10-20 02:03:18 -05:00
|
|
|
|
2017-02-16 05:00:40 -06:00
|
|
|
return encrypted
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def decrypt(ciphertext, key):
|
|
|
|
"""
|
|
|
|
Decrypt the AES encrypted string.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
ciphertext -- Encrypted string with AES method.
|
|
|
|
key -- key to decrypt the encrypted string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
global padding_string
|
|
|
|
|
|
|
|
ciphertext = base64.b64decode(ciphertext)
|
|
|
|
iv = ciphertext[:AES.block_size]
|
2018-04-11 07:45:59 -05:00
|
|
|
cipher = AES.new(pad(key), AES.MODE_CFB, iv)
|
2015-10-20 02:03:18 -05:00
|
|
|
decrypted = cipher.decrypt(ciphertext[AES.block_size:])
|
|
|
|
|
2016-06-16 14:32:57 -05:00
|
|
|
return decrypted
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
2018-04-04 04:47:01 -05:00
|
|
|
def pad(key):
|
2015-10-20 02:03:18 -05:00
|
|
|
"""Add padding to the key."""
|
|
|
|
|
|
|
|
global padding_string
|
2018-04-04 04:47:01 -05:00
|
|
|
str_len = len(key)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
# Key must be maximum 32 bytes long, so take first 32 bytes
|
|
|
|
if str_len > 32:
|
2018-04-04 04:47:01 -05:00
|
|
|
return key[:32]
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
# If key size id 16, 24 or 32 bytes then padding not require
|
|
|
|
if str_len == 16 or str_len == 24 or str_len == 32:
|
2018-04-04 04:47:01 -05:00
|
|
|
return key
|
2015-10-20 02:03:18 -05:00
|
|
|
|
2017-06-28 08:44:32 -05:00
|
|
|
# Convert bytes to string (python3)
|
|
|
|
if not hasattr(str, 'decode'):
|
|
|
|
padding_string = padding_string.decode()
|
|
|
|
|
2015-10-20 02:03:18 -05:00
|
|
|
# Add padding to make key 32 bytes long
|
2018-04-04 04:47:01 -05:00
|
|
|
return key + ((32 - str_len % 32) * padding_string)
|
2016-05-13 02:51:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
def pqencryptpassword(password, user):
|
|
|
|
"""
|
|
|
|
pqencryptpassword -- to encrypt a password
|
|
|
|
This is intended to be used by client applications that wish to send
|
|
|
|
commands like ALTER USER joe PASSWORD 'pwd'. The password need not
|
|
|
|
be sent in cleartext if it is encrypted on the client side. This is
|
|
|
|
good because it ensures the cleartext password won't end up in logs,
|
|
|
|
pg_stat displays, etc. We export the function so that clients won't
|
|
|
|
be dependent on low-level details like whether the enceyption is MD5
|
|
|
|
or something else.
|
|
|
|
|
|
|
|
Arguments are the cleartext password, and the SQL name of the user it
|
|
|
|
is for.
|
|
|
|
|
|
|
|
Return value is "md5" followed by a 32-hex-digit MD5 checksum..
|
|
|
|
|
|
|
|
Args:
|
|
|
|
password:
|
|
|
|
user:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
m = hashlib.md5()
|
|
|
|
|
|
|
|
# Place salt at the end because it may be known by users trying to crack
|
|
|
|
# the MD5 output.
|
2017-02-16 05:00:40 -06:00
|
|
|
# Handling of non-ascii password (Python2)
|
|
|
|
if hasattr(str, 'decode'):
|
|
|
|
password = password.encode('utf-8')
|
|
|
|
user = user.encode('utf-8')
|
|
|
|
else:
|
|
|
|
password = password.encode()
|
|
|
|
user = user.encode()
|
|
|
|
|
|
|
|
m.update(password)
|
|
|
|
m.update(user)
|
2016-05-13 02:51:20 -05:00
|
|
|
|
|
|
|
return "md5" + m.hexdigest()
|