File: yubikey

package info (click to toggle)
python-yubiotp 1.0.0.post1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 196 kB
  • sloc: python: 671; makefile: 130
file content (349 lines) | stat: -rwxr-xr-x 10,797 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python

"""
This is a command-line interface to the yubiotp package. Its primary function
is to simulate a YubiKey device for testing purposes. It also includes some
utilities for things like converting to and from modhex.
"""
from __future__ import print_function

import sys
from os.path import expanduser
from optparse import OptionParser, OptionGroup, Option, OptionValueError
from random import choice
from binascii import hexlify, unhexlify

from six.moves import configparser

from yubiotp.modhex import modhex, unmodhex, modhex_to_hex, hex_to_modhex
from yubiotp.otp import YubiKey, decode_otp, encode_otp


def main():
    parser = Handler.global_option_parser()
    parser.disable_interspersed_args()

    opts, args = parser.parse_args()

    if len(args) == 0:
        usage('You must choose an action', parser)

    handlers = {
        'list': ListHandler(),
        'init': InitHandler(),
        'delete': DeleteHandler(),
        'gen': GenHandler(),
        'parse': ParseHandler(),

        'modhex': ModhexHandler(),
    }

    handler = handlers.get(args[0])

    if handler is None:
        usage('Unknown action: {0}'.format(args[0]), parser)
    else:
        handler.run()


def check_hex_value(option, opt, value):
    try:
        unhexlify(value.encode())
    except TypeError as e:
        raise OptionValueError(str(e))

    return value


def check_modhex_value(option, opt, value):
    try:
        unmodhex(value.encode())
    except ValueError as e:
        raise OptionValueError(str(e))

    return value


class YubiKeyOption(Option):
    """
    Custom optparse Option class that adds the 'hex' value type. Values of this
    type are expected to be strings of hex digits, which will be decoded into
    binary strings before being stored.
    """
    TYPES = Option.TYPES + ('hex', 'modhex')
    TYPE_CHECKER = dict(Option.TYPE_CHECKER,
                        hex=check_hex_value,
                        modhex=check_modhex_value)


make_option = YubiKeyOption


class Handler(object):
    name = ''
    options = [
        make_option('-f', '--config', dest='config', default='~/.yubikey', metavar='PATH', help='A config file to store device state. [%default]'),
        make_option('-n', '--name', dest='device_name', default='0', metavar='NAME', help='The device number or name to operate on. [%default]'),
    ]
    args = ''
    description = 'Simulates one or more YubiKey devices from which you can generate tokens. Also parses tokens for verification. Choose an action for more information.'

    def run(self):
        parser = self.make_option_parser()

        opts, args = parser.parse_args()

        self.handle(opts, args)

    def make_option_parser(self):
        usage = '%prog [global opts] {0} [any opts]'.format(self.name)
        if self.args:
            usage = usage + ' ' + self.args

        parser = OptionParser(usage=usage, description=self.description)

        for option in Handler.options:
            parser.add_option(option)

        if len(self.options) > 0:
            group = OptionGroup(parser, self.name)
            for option in self.options:
                group.add_option(option)

            parser.add_option_group(group)

        return parser

    def handle(self, opts, args):
        raise NotImplementedError()

    @classmethod
    def global_option_parser(cls):
        parser = OptionParser(
            usage='%prog [global opts] <list|init|delete|gen|parse|modhex> [any opts] [args]',
            description=cls.description,
        )

        for option in cls.options:
            parser.add_option(option)

        return parser

    @staticmethod
    def random_hex(count):
        return ''.join(choice('0123456789abcdef') for i in range(count * 2))


class ListHandler(Handler):
    name = 'list'
    options = []
    args = ''
    description = 'List all virtual YubiKey devices.'

    def handle(self, opts, args):
        config = configparser.SafeConfigParser()
        config.read([expanduser(opts.config)])
        config.write(sys.stdout)


class InitHandler(Handler):
    name = 'init'
    options = [
        make_option('-p', '--public', dest='public_id', type='modhex', default='', help='A modhex-encoded public ID (up to 16 bytes)'),
        make_option('-k', '--key', dest='key', type='hex', help='A hex-encoded 16-byte AES key. If omitted, one will be generated.'),
        make_option('-u', '--uid', dest='uid', type='hex', help='A hex-encoded 6-byte private ID. If omitted, one will be generated.'),
        make_option('-s', '--session', dest='session', type='int', default=0, help='The initial session counter. [%default]'),
    ]
    description = 'Initialize a new virtual YubiKey.'

    def handle(self, opts, args):
        if opts.key is None:
            opts.key = self.random_hex(16)

        if opts.uid is None:
            opts.uid = self.random_hex(6)

        device = Device(opts.config, opts.device_name)
        device.create(opts.public_id, opts.key, opts.uid, opts.session)
        device.save()


class DeleteHandler(Handler):
    name = 'delete'
    options = []
    args = ''
    description = 'Remove a virtual YubiKey device.'

    def handle(self, opts, args):
        device = Device(opts.config, opts.device_name)
        device.delete()
        device.save()


class GenHandler(Handler):
    name = 'gen'
    options = [
        make_option('-c', '--count', dest='count', type='int', default=1, help='Generate multiple tokens. [%default]'),
        make_option('-i', '--interactive', action='store_true', dest='interactive', help='Generate a token for every line read from stdin until interrupted.'),
    ]
    args = ''
    description = 'Generate one or more tokens from the virtual device. This simulates pressing the YubiKey\'s button.'

    def handle(self, opts, args):
        device = Device(opts.config, opts.device_name)

        for i in range(opts.count):
            print(device.gen_token().decode())

        if opts.interactive:
            try:
                while True:
                    sys.stdin.readline()
                    print(device.gen_token().decode())
            except KeyboardInterrupt:
                pass

        device.save()


class ParseHandler(Handler):
    name = 'parse'
    options = []
    args = 'token ...'
    description = 'Parse tokens generated by the selected virtual device and display its fields.'

    def handle(self, opts, args):
        device = Device(opts.config, opts.device_name)
        key = device.get_config('key', unhex=True)

        for token in args[1:]:
            try:
                public_id, otp = decode_otp(token.encode(), key)
            except ValueError as e:
                print(e)
            else:
                print('public_id: {0}'.format(public_id.decode()))
                print('uid: {0}'.format(hexlify(otp.uid).decode()))
                print('session: {0}'.format(otp.session))
                print('timestamp: 0x{0:x}'.format(otp.timestamp))
                print('counter: {0}'.format(otp.counter))
                print('random: 0x{0:x}'.format(otp.rand))
                print()


class ModhexHandler(Handler):
    name = 'modhex'
    options = [
        make_option('-d', '--decode', action='store_true', dest='decode', help='Decode from modhex. Default is to encode to modhex.'),
        make_option('-H', '--hex', action='store_true', dest='hex', help='Encode to or decode from a string of hex digits. Default is a raw string.'),
    ]
    args = 'input ...'
    description = 'Encode (default) or decode a modhex string.'

    def handle(self, opts, args):
        for arg in args[1:]:
            try:
                if opts.decode:
                    if opts.hex:
                        print(modhex_to_hex(arg.encode()).decode())
                    else:
                        print(unmodhex(arg.encode()).decode())
                else:
                    if opts.hex:
                        print(hex_to_modhex(arg.encode()).decode())
                    else:
                        print(modhex(arg.encode()).decode())
            except ValueError as e:
                print(e, file=sys.stderr)


def usage(message, parser=None):
    print(message)
    print()

    if parser is not None:
        parser.print_help()

    sys.exit(1)


class Device(object):
    def __init__(self, config_path, name):
        self.config_path = expanduser(config_path)
        self.name = name
        self.section_name = 'device_{0}'.format(name)

        self.config = self._load_config()
        self.yubikey = None

    def _load_config(self):
        config = configparser.SafeConfigParser()
        config.read([self.config_path])

        return config

    def create(self, public_id, key, uid, session):
        if len(key) != 32:
            raise ValueError('AES keys must be exactly 16 bytes')

        try:
            self.config.add_section(self.section_name)
        except configparser.DuplicateSectionError:
            usage('A device named "{0}" already exists.'.format(self.name))
        else:
            self.set_config('public_id', public_id)
            self.set_config('key', key)
            self.set_config('uid', uid)
            self.set_config('session', session)

    def delete(self):
        try:
            self.config.remove_section(self.section_name)
        except configparser.NoSectionError:
            usage('The device named "{0}" does not exist.'.format(self.name))

    def gen_token(self):
        self.ensure_yubikey()

        otp = self.yubikey.generate()
        key = self.get_config('key', unhex=True)
        public_id = self.get_config('public_id').encode()

        token = encode_otp(otp, key, public_id=public_id)

        return token

    def ensure_yubikey(self):
        if self.yubikey is None:
            try:
                uid = self.get_config('uid', unhex=True)
                session = int(self.get_config('session'))

                self.get_config('key')
            except Exception as e:
                usage('The device named "{0}" does not exist or is corrupt. ({1})'.format(self.name, e))
            else:
                self.yubikey = YubiKey(uid=uid, session=session)

        return self.yubikey

    def save(self):
        if self.yubikey is not None:
            self.set_config('session', self.yubikey.session + 1)

        with open(self.config_path, 'w') as f:
            self.config.write(f)

    def get_config(self, key, unhex=False):
        value = self.config.get(self.section_name, key)
        if unhex:
            value = unhexlify(value.encode())

        return value

    def set_config(self, key, value):
        self.config.set(self.section_name, key, str(value))


if __name__ == '__main__':
    main()