authentik.enterprise.endpoints.connectors.agent.http
1from base64 import urlsafe_b64encode 2from json import dumps 3from secrets import token_bytes 4 5from cryptography.hazmat.backends import default_backend 6from cryptography.hazmat.primitives import hashes, serialization 7from cryptography.hazmat.primitives.asymmetric import ec 8from cryptography.hazmat.primitives.ciphers import Cipher 9from cryptography.hazmat.primitives.ciphers.algorithms import AES 10from cryptography.hazmat.primitives.ciphers.modes import GCM 11from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHash 12from django.http import HttpResponse 13from jwcrypto.common import base64url_decode, base64url_encode 14 15from authentik.endpoints.connectors.agent.models import AgentDeviceConnection 16 17 18def length_prefixed(data: bytes) -> bytes: 19 length = len(data) 20 return length.to_bytes(4, "big") + data 21 22 23def build_apu(public_key: ec.EllipticCurvePublicKey): 24 # X9.63 representation: 0x04 || X || Y 25 public_numbers = public_key.public_numbers() 26 27 x_bytes = public_numbers.x.to_bytes(32, "big") 28 y_bytes = public_numbers.y.to_bytes(32, "big") 29 30 x963 = bytes([0x04]) + x_bytes + y_bytes 31 32 result = length_prefixed(b"APPLE") + length_prefixed(x963) 33 34 return result 35 36 37def encrypt_token_with_a256_gcm( 38 body: dict, 39 device_encryption_key: str, 40 apv: bytes, 41 typ: str = "platformsso-login-response+jwt", 42) -> str: 43 ephemeral_key = ec.generate_private_key(curve=ec.SECP256R1()) 44 device_public_key = serialization.load_pem_public_key( 45 device_encryption_key.encode(), backend=default_backend() 46 ) 47 48 shared_secret_z = ephemeral_key.exchange(ec.ECDH(), device_public_key) 49 50 apu = build_apu(ephemeral_key.public_key()) 51 52 jwe_header = { 53 "enc": "A256GCM", 54 "kid": "ephemeralKey", 55 "epk": { 56 "x": base64url_encode( 57 ephemeral_key.public_key().public_numbers().x.to_bytes(32, "big") 58 ), 59 "y": base64url_encode( 60 ephemeral_key.public_key().public_numbers().y.to_bytes(32, "big") 61 ), 62 "kty": "EC", 63 "crv": "P-256", 64 }, 65 "typ": typ, 66 "alg": "ECDH-ES", 67 "apu": base64url_encode(apu), 68 "apv": base64url_encode(apv), 69 } 70 71 party_u_info = length_prefixed(apu) 72 party_v_info = length_prefixed(apv) 73 supp_pub_info = (256).to_bytes(4, "big") 74 75 other_info = length_prefixed(b"A256GCM") + party_u_info + party_v_info + supp_pub_info 76 77 ckdf = ConcatKDFHash( 78 algorithm=hashes.SHA256(), 79 length=32, 80 otherinfo=other_info, 81 ) 82 83 derived_key = ckdf.derive(shared_secret_z) 84 85 nonce = token_bytes(12) 86 87 header_json = dumps(jwe_header, separators=(",", ":")).encode() 88 aad = urlsafe_b64encode(header_json).rstrip(b"=") 89 90 cipher = Cipher(AES(derived_key), GCM(nonce)) 91 encryptor = cipher.encryptor() 92 encryptor.authenticate_additional_data(aad) 93 ciphertext = encryptor.update(dumps(body).encode()) + encryptor.finalize() 94 95 # base64url encoding 96 protected_b64 = urlsafe_b64encode(header_json).rstrip(b"=") 97 iv_b64 = urlsafe_b64encode(nonce).rstrip(b"=") 98 ciphertext_b64 = urlsafe_b64encode(ciphertext).rstrip(b"=") 99 tag_b64 = urlsafe_b64encode(encryptor.tag).rstrip(b"=") 100 101 jwe_compact = b".".join( 102 [ 103 protected_b64, 104 b"", 105 iv_b64, 106 ciphertext_b64, 107 tag_b64, 108 ] 109 ) 110 return jwe_compact.decode() 111 112 113class JWEResponse(HttpResponse): 114 115 def __init__( 116 self, 117 data: dict, 118 device: AgentDeviceConnection, 119 apv: str, 120 typ: str = "platformsso-login-response+jwt", 121 ): 122 super().__init__( 123 content=encrypt_token_with_a256_gcm( 124 data, device.apple_encryption_key, base64url_decode(apv), typ=typ 125 ), 126 content_type=f"application/{typ}", 127 )
def
length_prefixed(data: bytes) -> bytes:
def
build_apu( public_key: cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey):
24def build_apu(public_key: ec.EllipticCurvePublicKey): 25 # X9.63 representation: 0x04 || X || Y 26 public_numbers = public_key.public_numbers() 27 28 x_bytes = public_numbers.x.to_bytes(32, "big") 29 y_bytes = public_numbers.y.to_bytes(32, "big") 30 31 x963 = bytes([0x04]) + x_bytes + y_bytes 32 33 result = length_prefixed(b"APPLE") + length_prefixed(x963) 34 35 return result
def
encrypt_token_with_a256_gcm( body: dict, device_encryption_key: str, apv: bytes, typ: str = 'platformsso-login-response+jwt') -> str:
38def encrypt_token_with_a256_gcm( 39 body: dict, 40 device_encryption_key: str, 41 apv: bytes, 42 typ: str = "platformsso-login-response+jwt", 43) -> str: 44 ephemeral_key = ec.generate_private_key(curve=ec.SECP256R1()) 45 device_public_key = serialization.load_pem_public_key( 46 device_encryption_key.encode(), backend=default_backend() 47 ) 48 49 shared_secret_z = ephemeral_key.exchange(ec.ECDH(), device_public_key) 50 51 apu = build_apu(ephemeral_key.public_key()) 52 53 jwe_header = { 54 "enc": "A256GCM", 55 "kid": "ephemeralKey", 56 "epk": { 57 "x": base64url_encode( 58 ephemeral_key.public_key().public_numbers().x.to_bytes(32, "big") 59 ), 60 "y": base64url_encode( 61 ephemeral_key.public_key().public_numbers().y.to_bytes(32, "big") 62 ), 63 "kty": "EC", 64 "crv": "P-256", 65 }, 66 "typ": typ, 67 "alg": "ECDH-ES", 68 "apu": base64url_encode(apu), 69 "apv": base64url_encode(apv), 70 } 71 72 party_u_info = length_prefixed(apu) 73 party_v_info = length_prefixed(apv) 74 supp_pub_info = (256).to_bytes(4, "big") 75 76 other_info = length_prefixed(b"A256GCM") + party_u_info + party_v_info + supp_pub_info 77 78 ckdf = ConcatKDFHash( 79 algorithm=hashes.SHA256(), 80 length=32, 81 otherinfo=other_info, 82 ) 83 84 derived_key = ckdf.derive(shared_secret_z) 85 86 nonce = token_bytes(12) 87 88 header_json = dumps(jwe_header, separators=(",", ":")).encode() 89 aad = urlsafe_b64encode(header_json).rstrip(b"=") 90 91 cipher = Cipher(AES(derived_key), GCM(nonce)) 92 encryptor = cipher.encryptor() 93 encryptor.authenticate_additional_data(aad) 94 ciphertext = encryptor.update(dumps(body).encode()) + encryptor.finalize() 95 96 # base64url encoding 97 protected_b64 = urlsafe_b64encode(header_json).rstrip(b"=") 98 iv_b64 = urlsafe_b64encode(nonce).rstrip(b"=") 99 ciphertext_b64 = urlsafe_b64encode(ciphertext).rstrip(b"=") 100 tag_b64 = urlsafe_b64encode(encryptor.tag).rstrip(b"=") 101 102 jwe_compact = b".".join( 103 [ 104 protected_b64, 105 b"", 106 iv_b64, 107 ciphertext_b64, 108 tag_b64, 109 ] 110 ) 111 return jwe_compact.decode()
class
JWEResponse(django.http.response.HttpResponse):
114class JWEResponse(HttpResponse): 115 116 def __init__( 117 self, 118 data: dict, 119 device: AgentDeviceConnection, 120 apv: str, 121 typ: str = "platformsso-login-response+jwt", 122 ): 123 super().__init__( 124 content=encrypt_token_with_a256_gcm( 125 data, device.apple_encryption_key, base64url_decode(apv), typ=typ 126 ), 127 content_type=f"application/{typ}", 128 )
An HTTP response class with a string as content.
This content can be read, appended to, or replaced.
JWEResponse( data: dict, device: authentik.endpoints.connectors.agent.models.AgentDeviceConnection, apv: str, typ: str = 'platformsso-login-response+jwt')
116 def __init__( 117 self, 118 data: dict, 119 device: AgentDeviceConnection, 120 apv: str, 121 typ: str = "platformsso-login-response+jwt", 122 ): 123 super().__init__( 124 content=encrypt_token_with_a256_gcm( 125 data, device.apple_encryption_key, base64url_decode(apv), typ=typ 126 ), 127 content_type=f"application/{typ}", 128 )