Harald Welte | 21caf32 | 2022-07-16 14:06:46 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # without this, pylint will fail when inner classes are used |
| 4 | # within the 'nested' kwarg of our TlvMeta metaclass on python 3.7 :( |
| 5 | # pylint: disable=undefined-variable |
| 6 | |
| 7 | """ |
| 8 | APDU commands of 3GPP TS 31.102 V16.6.0 |
| 9 | """ |
| 10 | from typing import Dict |
| 11 | |
| 12 | from construct import * |
| 13 | from construct import Optional as COptional |
| 14 | from pySim.filesystem import * |
| 15 | from pySim.construct import * |
| 16 | from pySim.ts_31_102 import SUCI_TlvDataObject |
| 17 | |
| 18 | from pySim.apdu import ApduCommand, ApduCommandSet |
| 19 | |
| 20 | # Copyright (C) 2022 Harald Welte <laforge@osmocom.org> |
| 21 | # |
| 22 | # This program is free software: you can redistribute it and/or modify |
| 23 | # it under the terms of the GNU General Public License as published by |
| 24 | # the Free Software Foundation, either version 2 of the License, or |
| 25 | # (at your option) any later version. |
| 26 | # |
| 27 | # This program is distributed in the hope that it will be useful, |
| 28 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 | # GNU General Public License for more details. |
| 31 | # |
| 32 | # You should have received a copy of the GNU General Public License |
| 33 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 34 | # |
| 35 | |
| 36 | # Mapping between USIM Service Number and its description |
| 37 | |
| 38 | from pySim.apdu import ApduCommand, ApduCommandSet |
| 39 | |
| 40 | # TS 31.102 Section 7.1 |
| 41 | class UsimAuthenticateEven(ApduCommand, n='AUTHENTICATE', ins=0x88, cla=['0X', '4X', '6X']): |
| 42 | _apdu_case = 4 |
| 43 | _construct_p2 = BitStruct('scope'/Enum(Flag, mf=0, df_adf_specific=1), |
| 44 | BitsInteger(4), |
| 45 | 'authentication_context'/Enum(BitsInteger(3), gsm=0, umts=1, |
| 46 | vgcs_vbs=2, gba=4)) |
| 47 | _cs_cmd_gsm_3g = Struct('_rand_len'/Int8ub, 'rand'/HexAdapter(Bytes(this._rand_len)), |
| 48 | '_autn_len'/COptional(Int8ub), 'autn'/If(this._autn_len, HexAdapter(Bytes(this._autn_len)))) |
| 49 | _cs_cmd_vgcs = Struct('_vsid_len'/Int8ub, 'vservice_id'/HexAdapter(Bytes(this._vsid_len)), |
| 50 | '_vkid_len'/Int8ub, 'vk_id'/HexAdapter(Bytes(this._vkid_len)), |
| 51 | '_vstk_rand_len'/Int8ub, 'vstk_rand'/HexAdapter(Bytes(this._vstk_rand_len))) |
| 52 | _cmd_gba_bs = Struct('_rand_len'/Int8ub, 'rand'/HexAdapter(Bytes(this._rand_len)), |
| 53 | '_autn_len'/Int8ub, 'autn'/HexAdapter(Bytes(this._autn_len))) |
| 54 | _cmd_gba_naf = Struct('_naf_id_len'/Int8ub, 'naf_id'/HexAdapter(Bytes(this._naf_id_len)), |
| 55 | '_impi_len'/Int8ub, 'impi'/HexAdapter(Bytes(this._impi_len))) |
| 56 | _cs_cmd_gba = Struct('tag'/Int8ub, 'body'/Switch(this.tag, { 0xDD: 'bootstrap'/_cmd_gba_bs, |
| 57 | 0xDE: 'naf_derivation'/_cmd_gba_naf })) |
| 58 | _cs_rsp_gsm = Struct('_len_sres'/Int8ub, 'sres'/HexAdapter(Bytes(this._len_sres)), |
| 59 | '_len_kc'/Int8ub, 'kc'/HexAdapter(Bytes(this._len_kc))) |
| 60 | _rsp_3g_ok = Struct('_len_res'/Int8ub, 'res'/HexAdapter(Bytes(this._len_res)), |
| 61 | '_len_ck'/Int8ub, 'ck'/HexAdapter(Bytes(this._len_ck)), |
| 62 | '_len_ik'/Int8ub, 'ik'/HexAdapter(Bytes(this._len_ik)), |
| 63 | '_len_kc'/COptional(Int8ub), 'kc'/If(this._len_kc, HexAdapter(Bytes(this._len_kc)))) |
| 64 | _rsp_3g_sync = Struct('_len_auts'/Int8ub, 'auts'/HexAdapter(Bytes(this._len_auts))) |
| 65 | _cs_rsp_3g = Struct('tag'/Int8ub, 'body'/Switch(this.tag, { 0xDB: 'success'/_rsp_3g_ok, |
| 66 | 0xDC: 'sync_fail'/_rsp_3g_sync})) |
| 67 | _cs_rsp_vgcs = Struct(Const(b'\xDB'), '_vstk_len'/Int8ub, 'vstk'/HexAdapter(Bytes(this._vstk_len))) |
| 68 | _cs_rsp_gba_naf = Struct(Const(b'\xDB'), '_ks_ext_naf_len'/Int8ub, 'ks_ext_naf'/HexAdapter(Bytes(this._ks_ext_naf_len))) |
| 69 | def _decode_cmd(self) -> Dict: |
| 70 | r = {} |
| 71 | r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big')) |
| 72 | r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big')) |
| 73 | auth_ctx = r['p2']['authentication_context'] |
| 74 | if auth_ctx in ['gsm', 'umts']: |
| 75 | r['body'] = parse_construct(self._cs_cmd_gsm_3g, self.cmd_data) |
| 76 | elif auth_ctx == 'vgcs_vbs': |
| 77 | r['body'] = parse_construct(self._cs_cmd_vgcs, self.cmd_data) |
| 78 | elif auth_ctx == 'gba': |
| 79 | r['body'] = parse_construct(self._cs_cmd_gba, self.cmd_data) |
| 80 | else: |
| 81 | raise ValueError('Unsupported authentication_context: %s' % auth_ctx) |
| 82 | return r |
| 83 | |
| 84 | def _decode_rsp(self) -> Dict: |
| 85 | r = {} |
| 86 | auth_ctx = self.cmd_dict['p2']['authentication_context'] |
| 87 | if auth_ctx == 'gsm': |
| 88 | r['body'] = parse_construct(self._cs_rsp_gsm, self.rsp_data) |
| 89 | elif auth_ctx == 'umts': |
| 90 | r['body'] = parse_construct(self._cs_rsp_3g, self.rsp_data) |
| 91 | elif auth_ctx == 'vgcs_vbs': |
| 92 | r['body'] = parse_construct(self._cs_rsp_vgcs, self.rsp_data) |
| 93 | elif auth_ctx == 'gba': |
| 94 | if self.cmd_dict['body']['tag'] == 0xDD: |
| 95 | r['body'] = parse_construct(self._cs_rsp_3g, self.rsp_data) |
| 96 | else: |
| 97 | r['body'] = parse_construct(self._cs_rsp_gba_naf, self.rsp_data) |
| 98 | else: |
| 99 | raise ValueError('Unsupported authentication_context: %s' % auth_ctx) |
| 100 | return r |
| 101 | |
| 102 | class UsimAuthenticateOdd(ApduCommand, n='AUTHENTICATE', ins=0x89, cla=['0X', '4X', '6X']): |
| 103 | _apdu_case = 4 |
| 104 | _construct_p2 = BitStruct('scope'/Enum(Flag, mf=0, df_adf_specific=1), |
| 105 | BitsInteger(4), |
| 106 | 'authentication_context'/Enum(BitsInteger(3), mbms=5, local_key=6)) |
| 107 | # TS 31.102 Section 7.5 |
| 108 | class UsimGetIdentity(ApduCommand, n='GET IDENTITY', ins=0x78, cla=['8X', 'CX', 'EX']): |
| 109 | _apdu_case = 4 |
| 110 | _construct_p2 = BitStruct('scope'/Enum(Flag, mf=0, df_adf_specific=1), |
Harald Welte | 1de62c4 | 2023-06-06 18:08:54 +0200 | [diff] [blame] | 111 | 'identity_context'/Enum(BitsInteger(7), suci=1, suci_5g_nswo=2)) |
Harald Welte | 21caf32 | 2022-07-16 14:06:46 +0200 | [diff] [blame] | 112 | _tlv_rsp = SUCI_TlvDataObject |
| 113 | |
| 114 | ApduCommands = ApduCommandSet('TS 31.102', cmds=[UsimAuthenticateEven, UsimAuthenticateOdd, |
| 115 | UsimGetIdentity]) |