- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
35 lines
1.3 KiB
Python
Executable File
35 lines
1.3 KiB
Python
Executable File
import base64
|
|
|
|
from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
|
|
from cryptography.hazmat.primitives import hashes, serialization
|
|
from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa
|
|
|
|
|
|
class UnsupportedKeyType(ValueError):
|
|
pass
|
|
|
|
|
|
def parse_public_key(pem):
|
|
if not isinstance(pem, str):
|
|
raise ValueError("PEM must be a string")
|
|
try:
|
|
key = serialization.load_pem_public_key(pem.encode("utf-8"))
|
|
except (ValueError, UnsupportedAlgorithm):
|
|
raise ValueError("Invalid PEM public key") from None
|
|
if isinstance(key, rsa.RSAPublicKey) and key.key_size >= 2048:
|
|
return key
|
|
if isinstance(key, ec.EllipticCurvePublicKey) and isinstance(key.curve, ec.SECP256R1):
|
|
return key
|
|
raise UnsupportedKeyType("Unsupported public key type")
|
|
|
|
def verify_implant_signature(public_key, signature, nonce):
|
|
try:
|
|
signed = base64.b64decode(signature, validate=True)
|
|
if isinstance(public_key, ec.EllipticCurvePublicKey):
|
|
public_key.verify(signed, nonce.encode("ascii"), ec.ECDSA(hashes.SHA256()))
|
|
else:
|
|
public_key.verify(signed, nonce.encode("ascii"), padding.PKCS1v15(), hashes.SHA256())
|
|
except (InvalidSignature, ValueError):
|
|
return False
|
|
return True
|