From e18586ddf0c8b5bddef570dcc080c71a603bafe5 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 13 Jul 2024 21:22:59 +0200 Subject: [PATCH] pySim.globalplatform: Add 'http' submodule for GP Amd B RAM over HTTPS This implements the first parts of the "GlobalPlatform Remote Application Management over HTTP Card Specification v2.3 - Amendment B, Versoin 1.2". Specifically, this patch covers the TLV definitions for the OTA message used for HTTPS session triggering. This also adds some more unit test coverage to pySim.cat, based on real-world data that was captured nested inside the HTTPS Administration session triggering parameters. Change-Id: Ia7d7bd6df41bdf1249011bad9a9a38b7669edc54 --- pySim/cat.py | 15 ++++++ pySim/global_platform/http.py | 93 +++++++++++++++++++++++++++++++++++ tests/test_tlvs.py | 1 + 3 files changed, 109 insertions(+) create mode 100644 pySim/global_platform/http.py diff --git a/pySim/cat.py b/pySim/cat.py index d2a3a66a..73f0a762 100644 --- a/pySim/cat.py +++ b/pySim/cat.py @@ -267,6 +267,9 @@ class SsString(COMPR_TLV_IE, tag=0x89): # TS 102 223 Section 8.15 class TextString(COMPR_TLV_IE, tag=0x8D): + _test_de_encode = [ + ( '8d090470617373776f7264', {'dcs': 4, 'text_string': '70617373776f7264'} ), + ] _construct = Struct('dcs'/Int8ub, # TS 03.38 'text_string'/HexAdapter(GreedyBytes)) @@ -444,6 +447,9 @@ class BrowserTerminationCause(COMPR_TLV_IE, tag=0xB4): # TS 102 223 Section 8.52 class BearerDescription(COMPR_TLV_IE, tag=0xB5): + _test_de_encode = [ + ( 'b50103', {'bearer_parameters': '', 'bearer_type': 'default'} ), + ] # TS 31.111 Section 8.52.1 BearerParsCs = Struct('data_rate'/Int8ub, 'bearer_service'/Int8ub, @@ -507,11 +513,17 @@ class ChannelStatus(COMPR_TLV_IE, tag = 0xB8): # TS 102 223 Section 8.58 class OtherAddress(COMPR_TLV_IE, tag = 0xBE): + _test_de_encode = [ + ( 'be052101020304', {'address': '01020304', 'type_of_address': 'ipv4'} ), + ] _construct = Struct('type_of_address'/Enum(Int8ub, ipv4=0x21, ipv6=0x57), 'address'/HexAdapter(GreedyBytes)) # TS 102 223 Section 8.59 class UiccTransportLevel(COMPR_TLV_IE, tag = 0xBC): + _test_de_encode = [ + ( 'bc03028000', {'port_number': 32768, 'protocol_type': 'tcp_uicc_client_remote'} ), + ] _construct = Struct('protocol_type'/Enum(Int8ub, udp_uicc_client_remote=1, tcp_uicc_client_remote=2, tcp_uicc_server=3, udp_uicc_client_local=4, tcp_uicc_client_local=5, direct_channel=6), @@ -558,6 +570,9 @@ class RemoteEntityAddress(COMPR_TLV_IE, tag=0xC9): # TS 102 223 Section 8.70 class NetworkAccessName(COMPR_TLV_IE, tag=0xC7): + _test_de_encode = [ + ( 'c704036e6161', '036e6161' ), + ] _construct = HexAdapter(GreedyBytes) # TS 102 223 Section 8.72 diff --git a/pySim/global_platform/http.py b/pySim/global_platform/http.py new file mode 100644 index 00000000..f0e98aa0 --- /dev/null +++ b/pySim/global_platform/http.py @@ -0,0 +1,93 @@ +"""GlobalPlatform Remote Application Management over HTTP Card Specification v2.3 - Amendment B. +Also known as SCP81 for SIM/USIM/UICC/eUICC/eSIM OTA. +""" + +# (C) 2024 by Harald Welte +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from construct import Struct, Int8ub, Int16ub, Bytes, GreedyBytes, GreedyString, BytesInteger +from construct import this, len_, Rebuild, Const +from construct import Optional as COptional + +from pySim.tlv import BER_TLV_IE +from pySim import cat + + +# Table 3-3 + Section 3.8.1 +class RasConnectionParams(BER_TLV_IE, tag=0x84, nested=cat.OpenChannel.nested_collection_cls.possible_nested): + pass + +# Table 3-3 + Section 3.8.2 +class SecurityParams(BER_TLV_IE, tag=0x85): + _test_de_encode = [ + ( '850804deadbeef020040', {'kid': 64,'kvn': 0, 'psk_id': b'\xde\xad\xbe\xef', 'sha_type': None} ) + ] + _construct = Struct('_psk_id_len'/Rebuild(Int8ub, len_(this.psk_id)), 'psk_id'/Bytes(this._psk_id_len), + '_kid_kvn_len'/Const(2, Int8ub), 'kvn'/Int8ub, 'kid'/Int8ub, + 'sha_type'/COptional(Int8ub)) + +# Table 3-3 + ? +class ExtendedSecurityParams(BER_TLV_IE, tag=0xA5): + _construct = GreedyBytes + +# Table 3-3 + Section 3.8.3 +class SessionRetryPolicyParams(BER_TLV_IE, tag=0x86): + _construct = Struct('retry_counter'/Int16ub, + 'retry_waiting_delay'/BytesInteger(5), + 'retry_report_failure'/COptional(GreedyBytes)) + +# Table 3-3 + Section 3.8.4 +class AdminHostParam(BER_TLV_IE, tag=0x8A): + _test_de_encode = [ + ( '8a0a61646d696e2e686f7374', 'admin.host' ), + ] + _construct = GreedyString('utf-8') + +# Table 3-3 + Section 3.8.5 +class AgentIdParam(BER_TLV_IE, tag=0x8B): + _construct = GreedyString('utf-8') + +# Table 3-3 + Section 3.8.6 +class AdminUriParam(BER_TLV_IE, tag=0x8C): + _test_de_encode = [ + ( '8c1668747470733a2f2f61646d696e2e686f73742f757269', 'https://admin.host/uri' ), + ] + _construct = GreedyString('utf-8') + +# Table 3-3 +class HttpPostParams(BER_TLV_IE, tag=0x89, nested=[AdminHostParam, AgentIdParam, AdminUriParam]): + pass + +# Table 3-3 +class AdmSessionParams(BER_TLV_IE, tag=0x83, nested=[RasConnectionParams, SecurityParams, + ExtendedSecurityParams, SessionRetryPolicyParams, + HttpPostParams]): + pass + +# Table 3-3 + Section 3.11.4 +class RasFqdn(BER_TLV_IE, tag=0xD6): + _construct = GreedyBytes # FIXME: DNS String + +# Table 3-3 + Section 3.11.7 +class DnsConnectionParams(BER_TLV_IE, tag=0xFA, nested=cat.OpenChannel.nested_collection_cls.possible_nested): + pass + +# Table 3-3 +class DnsResolutionParams(BER_TLV_IE, tag=0xB3, nested=[RasFqdn, DnsConnectionParams]): + pass + +# Table 3-3 +class AdmSessTriggerParams(BER_TLV_IE, tag=0x81, nested=[AdmSessionParams, DnsResolutionParams]): + pass diff --git a/tests/test_tlvs.py b/tests/test_tlvs.py index a0dd1076..429b9669 100755 --- a/tests/test_tlvs.py +++ b/tests/test_tlvs.py @@ -31,6 +31,7 @@ import pySim.sysmocom_sja2 import pySim.gsm_r import pySim.cdma_ruim import pySim.global_platform +import pySim.global_platform.http if 'unittest.util' in __import__('sys').modules: # Show full diff in self.assertEqual.