2023-05-15 08:51:32 +08:00
|
|
|
import hashlib
|
2025-07-23 23:29:46 +08:00
|
|
|
import os
|
2025-07-18 09:26:29 +08:00
|
|
|
from typing import Union
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-01-24 20:22:01 +08:00
|
|
|
from Crypto.Cipher import AES
|
2023-05-15 08:51:32 +08:00
|
|
|
from Crypto.PublicKey import RSA
|
2023-08-12 00:57:00 +08:00
|
|
|
from Crypto.Random import get_random_bytes
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
from extensions.ext_redis import redis_client
|
|
|
|
from extensions.ext_storage import storage
|
2024-09-13 22:42:08 +08:00
|
|
|
from libs import gmpy2_pkcs10aep_cipher
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
def generate_key_pair(tenant_id: str) -> str:
|
2023-05-15 08:51:32 +08:00
|
|
|
private_key = RSA.generate(2048)
|
|
|
|
public_key = private_key.publickey()
|
|
|
|
|
|
|
|
pem_private = private_key.export_key()
|
|
|
|
pem_public = public_key.export_key()
|
|
|
|
|
2025-07-23 23:29:46 +08:00
|
|
|
filepath = os.path.join("privkeys", tenant_id, "private.pem")
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
storage.save(filepath, pem_private)
|
|
|
|
|
|
|
|
return pem_public.decode()
|
|
|
|
|
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
prefix_hybrid = b"HYBRID:"
|
|
|
|
|
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
def encrypt(text: str, public_key: Union[str, bytes]) -> bytes:
|
2023-05-15 08:51:32 +08:00
|
|
|
if isinstance(public_key, str):
|
|
|
|
public_key = public_key.encode()
|
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
aes_key = get_random_bytes(16)
|
|
|
|
cipher_aes = AES.new(aes_key, AES.MODE_EAX)
|
|
|
|
|
|
|
|
ciphertext, tag = cipher_aes.encrypt_and_digest(text.encode())
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
rsa_key = RSA.import_key(public_key)
|
2024-01-24 20:22:01 +08:00
|
|
|
cipher_rsa = gmpy2_pkcs10aep_cipher.new(rsa_key)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
enc_aes_key: bytes = cipher_rsa.encrypt(aes_key)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
|
|
|
encrypted_data = enc_aes_key + cipher_aes.nonce + tag + ciphertext
|
|
|
|
|
|
|
|
return prefix_hybrid + encrypted_data
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
def get_decrypt_decoding(tenant_id: str) -> tuple[RSA.RsaKey, object]:
|
2025-07-23 23:29:46 +08:00
|
|
|
filepath = os.path.join("privkeys", tenant_id, "private.pem")
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2025-07-25 11:32:48 +09:00
|
|
|
cache_key = f"tenant_privkey:{hashlib.sha3_256(filepath.encode()).hexdigest()}"
|
2023-05-15 08:51:32 +08:00
|
|
|
private_key = redis_client.get(cache_key)
|
|
|
|
if not private_key:
|
|
|
|
try:
|
|
|
|
private_key = storage.load(filepath)
|
|
|
|
except FileNotFoundError:
|
2025-07-25 11:32:48 +09:00
|
|
|
raise PrivkeyNotFoundError(f"Private key not found, tenant_id: {tenant_id}")
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
redis_client.setex(cache_key, 120, private_key)
|
|
|
|
|
|
|
|
rsa_key = RSA.import_key(private_key)
|
2024-01-24 20:22:01 +08:00
|
|
|
cipher_rsa = gmpy2_pkcs10aep_cipher.new(rsa_key)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
return rsa_key, cipher_rsa
|
|
|
|
|
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
def decrypt_token_with_decoding(encrypted_text: bytes, rsa_key: RSA.RsaKey, cipher_rsa) -> str:
|
2023-08-12 00:57:00 +08:00
|
|
|
if encrypted_text.startswith(prefix_hybrid):
|
2024-08-15 17:53:12 +08:00
|
|
|
encrypted_text = encrypted_text[len(prefix_hybrid) :]
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2024-08-15 17:53:12 +08:00
|
|
|
enc_aes_key = encrypted_text[: rsa_key.size_in_bytes()]
|
|
|
|
nonce = encrypted_text[rsa_key.size_in_bytes() : rsa_key.size_in_bytes() + 16]
|
|
|
|
tag = encrypted_text[rsa_key.size_in_bytes() + 16 : rsa_key.size_in_bytes() + 32]
|
|
|
|
ciphertext = encrypted_text[rsa_key.size_in_bytes() + 32 :]
|
2023-08-12 00:57:00 +08:00
|
|
|
|
|
|
|
aes_key = cipher_rsa.decrypt(enc_aes_key)
|
|
|
|
|
|
|
|
cipher_aes = AES.new(aes_key, AES.MODE_EAX, nonce=nonce)
|
|
|
|
decrypted_text = cipher_aes.decrypt_and_verify(ciphertext, tag)
|
|
|
|
else:
|
|
|
|
decrypted_text = cipher_rsa.decrypt(encrypted_text)
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
return decrypted_text.decode()
|
|
|
|
|
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
def decrypt(encrypted_text: bytes, tenant_id: str) -> str:
|
2024-01-02 23:42:00 +08:00
|
|
|
rsa_key, cipher_rsa = get_decrypt_decoding(tenant_id)
|
|
|
|
|
2025-07-18 09:26:29 +08:00
|
|
|
return decrypt_token_with_decoding(encrypted_text=encrypted_text, rsa_key=rsa_key, cipher_rsa=cipher_rsa)
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
class PrivkeyNotFoundError(Exception):
|
|
|
|
pass
|