Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/pskc/scripts/pskc2csv.py
blob: f482d5bc18c2726c2a5600cf9935e35098e421fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# pskc2csv.py - script to convert a PSKC file to CSV
#
# Copyright (C) 2014-2018 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Script to convert a PSKC file to CSV."""

import argparse
import base64
import csv
import getpass
import operator
import sys
from binascii import b2a_hex

import pskc
from pskc.scripts.util import (
    OutputFile, VersionAction, get_key, get_password)


epilog = '''
supported columns:
  id, serial, secret, counter, time_offset, time_interval, interval,
  time_drift, issuer, manufacturer, response_length, algorithm
And any other properties of pskc.key.Key instances.

Report bugs to <python-pskc-users@lists.arthurdejong.org>.
'''.strip()

# set up command line parser
parser = argparse.ArgumentParser(
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description='Convert a PSKC file to CSV.', epilog=epilog)
parser.add_argument(
    'input', metavar='FILE', help='the PSKC file to read')
parser.add_argument(
    '-V', '--version', action=VersionAction)
parser.add_argument(
    '-o', '--output', metavar='FILE',
    help='write CSV to file instead of stdout')
parser.add_argument(
    '-c', '--columns', metavar='COL:LABEL,COL,..',
    type=lambda x: [column.split(':', 1) for column in x.split(',')],
    help='list of columns with optional labels to export',
    default='serial,secret,algorithm,response_length,time_interval')
parser.add_argument(
    '-p', '--password', '--passwd', metavar='PASS/FILE',
    help='password to use for decryption (or read from a file)')
parser.add_argument(
    '-s', '--secret', metavar='KEY/FILE',
    help='hex encoded encryption key or file containing the binary key')
encodings = {
    'hex': b2a_hex,
    'base32': base64.b32encode,
    'base64': base64.b64encode,
}
parser.add_argument(
    '-e', '--secret-encoding', choices=sorted(encodings.keys()),
    help='encoding used for outputting key material',
    default='hex')


def get_column(key, column, encoding):
    """Return a string value for the given column."""
    value = operator.attrgetter(column)(key)
    if column == 'secret':
        # Python 3 compatible construct for outputting a string
        return str(encodings[encoding](value).decode())
    return value


def main():
    """Convert a PSKC file to CSV."""
    # parse command-line arguments
    args = parser.parse_args()
    # open and parse input PSKC file
    pskcfile = pskc.PSKC(args.input)
    if args.secret:
        pskcfile.encryption.key = get_key(args.secret)
    elif args.password:
        pskcfile.encryption.derive_key(get_password(args.password))
    elif sys.stdin.isatty() and pskcfile.encryption.is_encrypted:
        # prompt for a password
        prompt = 'Password: '
        if pskcfile.encryption.key_name:
            prompt = '%s: ' % pskcfile.encryption.key_name
        passwd = getpass.getpass(prompt)
        pskcfile.encryption.derive_key(passwd)
    # open output CSV file, write header and keys
    with OutputFile(args.output) as output:
        csvfile = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
        csvfile.writerow([column[-1] for column in args.columns])
        for key in pskcfile.keys:
            csvfile.writerow([
                get_column(key, column[0], args.secret_encoding)
                for column in args.columns])