mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-17 02:48:34 +03:00
The CardKeyProviderCsv class implements a column decryption scheme where columns are protected using a transport key. The CSV files are enrcypted using contrib/csv-encrypt-columns.py. The current implementation has two main problems: - The decryption code in CardKeyProviderCsv is not specific to CSV files. It could be re-used in other formats, for example to decrypt columns (fields) red from a database. So let's split the decryption code in a separate class. - The encryption code in csv-encrypt-columns.py accesses methods and properties in CardKeyProviderCsv. Also having the coresponding encryption code somewhere out of tree may be confusing. Let's improve the design and put encryption and decryption functions in a single class. Let's also make sure the encryption/decryption is covered by unittests. Related: SYS#7725 Change-Id: I180457d4938f526d227c81020e4e03c6b3a57dab
67 lines
2.4 KiB
Python
Executable File
67 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# Utility program to perform column-based encryption of a CSV file holding SIM/UICC
|
|
# related key materials.
|
|
#
|
|
# (C) 2024 by Harald Welte <laforge@osmocom.org>
|
|
#
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
import sys
|
|
import csv
|
|
import argparse
|
|
from Cryptodome.Cipher import AES
|
|
from osmocom.utils import h2b, b2h, Hexstr
|
|
|
|
from pySim.card_key_provider import CardKeyFieldCryptor
|
|
|
|
class CsvColumnEncryptor(CardKeyFieldCryptor):
|
|
def __init__(self, filename: str, transport_keys: dict):
|
|
self.filename = filename
|
|
self.crypt = CardKeyFieldCryptor(transport_keys)
|
|
|
|
def encrypt(self) -> None:
|
|
with open(self.filename, 'r') as infile:
|
|
cr = csv.DictReader(infile)
|
|
cr.fieldnames = [field.upper() for field in cr.fieldnames]
|
|
|
|
with open(self.filename + '.encr', 'w') as outfile:
|
|
cw = csv.DictWriter(outfile, dialect=csv.unix_dialect, fieldnames=cr.fieldnames)
|
|
cw.writeheader()
|
|
|
|
for row in cr:
|
|
for fieldname in cr.fieldnames:
|
|
row[fieldname] = self.crypt.encrypt_field(fieldname, row[fieldname])
|
|
cw.writerow(row)
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('CSVFILE', help="CSV file name")
|
|
parser.add_argument('--csv-column-key', action='append', required=True,
|
|
help='per-CSV-column AES transport key')
|
|
|
|
opts = parser.parse_args()
|
|
|
|
csv_column_keys = {}
|
|
for par in opts.csv_column_key:
|
|
name, key = par.split(':')
|
|
csv_column_keys[name] = key
|
|
|
|
if len(csv_column_keys) == 0:
|
|
print("You must specify at least one key!")
|
|
sys.exit(1)
|
|
|
|
cce = CsvColumnEncryptor(opts.CSVFILE, csv_column_keys)
|
|
cce.encrypt()
|