Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 1 | # coding=utf-8 |
Harald Welte | 9d0f1f0 | 2021-04-03 09:19:11 +0200 | [diff] [blame] | 2 | """Obtaining card parameters (mostly key data) from external source. |
| 3 | |
| 4 | This module contains a base class and a concrete implementation of |
| 5 | obtaining card key material (or other card-individual parameters) from |
| 6 | an external data source. |
| 7 | |
| 8 | This is used e.g. to keep PIN/PUK data in some file on disk, avoiding |
| 9 | the need of manually entering the related card-individual data on every |
| 10 | operation with pySim-shell. |
| 11 | """ |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 12 | |
Harald Welte | 90d3b97 | 2021-04-03 08:56:21 +0200 | [diff] [blame] | 13 | # (C) 2021 by Sysmocom s.f.m.c. GmbH |
| 14 | # All Rights Reserved |
| 15 | # |
| 16 | # Author: Philipp Maier |
| 17 | # |
| 18 | # This program is free software: you can redistribute it and/or modify |
| 19 | # it under the terms of the GNU General Public License as published by |
| 20 | # the Free Software Foundation, either version 2 of the License, or |
| 21 | # (at your option) any later version. |
| 22 | # |
| 23 | # This program is distributed in the hope that it will be useful, |
| 24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | # GNU General Public License for more details. |
| 27 | # |
| 28 | # You should have received a copy of the GNU General Public License |
| 29 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 30 | |
Harald Welte | 90d3b97 | 2021-04-03 08:56:21 +0200 | [diff] [blame] | 31 | from typing import List, Dict, Optional |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 32 | |
Vadim Yanitskiy | 5ef1650 | 2021-05-02 01:52:56 +0200 | [diff] [blame] | 33 | import abc |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 34 | import csv |
| 35 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 36 | card_key_providers = [] # type: List['CardKeyProvider'] |
| 37 | |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 38 | |
Vadim Yanitskiy | 5ef1650 | 2021-05-02 01:52:56 +0200 | [diff] [blame] | 39 | class CardKeyProvider(abc.ABC): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 40 | """Base class, not containing any concrete implementation.""" |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 41 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 42 | VALID_FIELD_NAMES = ['ICCID', 'ADM1', |
| 43 | 'IMSI', 'PIN1', 'PIN2', 'PUK1', 'PUK2'] |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 44 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 45 | # check input parameters, but do nothing concrete yet |
| 46 | def _verify_get_data(self, fields: List[str] = [], key: str = 'ICCID', value: str = "") -> Dict[str, str]: |
| 47 | """Verify multiple fields for identified card. |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 48 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 49 | Args: |
| 50 | fields : list of valid field names such as 'ADM1', 'PIN1', ... which are to be obtained |
| 51 | key : look-up key to identify card data, such as 'ICCID' |
| 52 | value : value for look-up key to identify card data |
| 53 | Returns: |
| 54 | dictionary of {field, value} strings for each requested field from 'fields' |
| 55 | """ |
| 56 | for f in fields: |
| 57 | if (f not in self.VALID_FIELD_NAMES): |
| 58 | raise ValueError("Requested field name '%s' is not a valid field name, valid field names are: %s" % |
| 59 | (f, str(self.VALID_FIELD_NAMES))) |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 60 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 61 | if (key not in self.VALID_FIELD_NAMES): |
| 62 | raise ValueError("Key field name '%s' is not a valid field name, valid field names are: %s" % |
| 63 | (key, str(self.VALID_FIELD_NAMES))) |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 64 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 65 | return {} |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 66 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 67 | def get_field(self, field: str, key: str = 'ICCID', value: str = "") -> Optional[str]: |
| 68 | """get a single field from CSV file using a specified key/value pair""" |
| 69 | fields = [field] |
| 70 | result = self.get(fields, key, value) |
| 71 | return result.get(field) |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 72 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 73 | @abc.abstractmethod |
| 74 | def get(self, fields: List[str], key: str, value: str) -> Dict[str, str]: |
| 75 | """Get multiple card-individual fields for identified card. |
Harald Welte | 9d0f1f0 | 2021-04-03 09:19:11 +0200 | [diff] [blame] | 76 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 77 | Args: |
| 78 | fields : list of valid field names such as 'ADM1', 'PIN1', ... which are to be obtained |
| 79 | key : look-up key to identify card data, such as 'ICCID' |
| 80 | value : value for look-up key to identify card data |
| 81 | Returns: |
| 82 | dictionary of {field, value} strings for each requested field from 'fields' |
| 83 | """ |
| 84 | |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 85 | |
Harald Welte | 4442b3d | 2021-04-03 09:00:16 +0200 | [diff] [blame] | 86 | class CardKeyProviderCsv(CardKeyProvider): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 87 | """Card key provider implementation that allows to query against a specified CSV file""" |
| 88 | csv_file = None |
| 89 | filename = None |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 90 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 91 | def __init__(self, filename: str): |
| 92 | """ |
| 93 | Args: |
| 94 | filename : file name (path) of CSV file containing card-individual key/data |
| 95 | """ |
| 96 | self.csv_file = open(filename, 'r') |
| 97 | if not self.csv_file: |
| 98 | raise RuntimeError("Could not open CSV file '%s'" % filename) |
| 99 | self.filename = filename |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 100 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 101 | def get(self, fields: List[str], key: str, value: str) -> Dict[str, str]: |
| 102 | super()._verify_get_data(fields, key, value) |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 103 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 104 | self.csv_file.seek(0) |
| 105 | cr = csv.DictReader(self.csv_file) |
| 106 | if not cr: |
| 107 | raise RuntimeError( |
| 108 | "Could not open DictReader for CSV-File '%s'" % self.filename) |
| 109 | cr.fieldnames = [field.upper() for field in cr.fieldnames] |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 110 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 111 | rc = {} |
| 112 | for row in cr: |
| 113 | if row[key] == value: |
| 114 | for f in fields: |
| 115 | if f in row: |
| 116 | rc.update({f: row[f]}) |
| 117 | else: |
| 118 | raise RuntimeError("CSV-File '%s' lacks column '%s'" % |
| 119 | (self.filename, f)) |
| 120 | return rc |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 121 | |
| 122 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 123 | def card_key_provider_register(provider: CardKeyProvider, provider_list=card_key_providers): |
| 124 | """Register a new card key provider. |
Harald Welte | 9d0f1f0 | 2021-04-03 09:19:11 +0200 | [diff] [blame] | 125 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 126 | Args: |
| 127 | provider : the to-be-registered provider |
| 128 | provider_list : override the list of providers from the global default |
| 129 | """ |
| 130 | if not isinstance(provider, CardKeyProvider): |
| 131 | raise ValueError("provider is not a card data provier") |
| 132 | provider_list.append(provider) |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 133 | |
| 134 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 135 | def card_key_provider_get(fields, key: str, value: str, provider_list=card_key_providers) -> Dict[str, str]: |
| 136 | """Query all registered card data providers for card-individual [key] data. |
Harald Welte | 9d0f1f0 | 2021-04-03 09:19:11 +0200 | [diff] [blame] | 137 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 138 | Args: |
| 139 | fields : list of valid field names such as 'ADM1', 'PIN1', ... which are to be obtained |
| 140 | key : look-up key to identify card data, such as 'ICCID' |
| 141 | value : value for look-up key to identify card data |
| 142 | provider_list : override the list of providers from the global default |
| 143 | Returns: |
| 144 | dictionary of {field, value} strings for each requested field from 'fields' |
| 145 | """ |
| 146 | for p in provider_list: |
| 147 | if not isinstance(p, CardKeyProvider): |
| 148 | raise ValueError( |
| 149 | "provider list contains element which is not a card data provier") |
| 150 | result = p.get(fields, key, value) |
| 151 | if result: |
| 152 | return result |
| 153 | return {} |
Philipp Maier | 2b11c32 | 2021-03-17 12:37:39 +0100 | [diff] [blame] | 154 | |
| 155 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 156 | def card_key_provider_get_field(field: str, key: str, value: str, provider_list=card_key_providers) -> Optional[str]: |
| 157 | """Query all registered card data providers for a single field. |
Harald Welte | 9d0f1f0 | 2021-04-03 09:19:11 +0200 | [diff] [blame] | 158 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 159 | Args: |
| 160 | field : name valid field such as 'ADM1', 'PIN1', ... which is to be obtained |
| 161 | key : look-up key to identify card data, such as 'ICCID' |
| 162 | value : value for look-up key to identify card data |
| 163 | provider_list : override the list of providers from the global default |
| 164 | Returns: |
| 165 | dictionary of {field, value} strings for the requested field |
| 166 | """ |
| 167 | for p in provider_list: |
| 168 | if not isinstance(p, CardKeyProvider): |
| 169 | raise ValueError( |
| 170 | "provider list contains element which is not a card data provier") |
| 171 | result = p.get_field(field, key, value) |
| 172 | if result: |
| 173 | return result |
| 174 | return None |