File: ykparse.c

package info (click to toggle)
libyubikey 1.13-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 1,660 kB
  • sloc: sh: 11,647; ansic: 878; makefile: 71
file content (151 lines) | stat: -rw-r--r-- 4,537 bytes parent folder | download | duplicates (4)
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
/* ykparse.c --- Example command line interface for authentication token.
 *
 * Written by Simon Josefsson <simon@josefsson.org>.
 * Copyright (c) 2006-2012 Yubico AB
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "yubikey.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

int
main (int argc, char *argv[])
{
  uint8_t buf[128];
  uint8_t key[YUBIKEY_KEY_SIZE];
  char *aeskey, *token;
  yubikey_token_st tok;

  /* Parse command line parameters. */
  if (argc < 2)
    {
      printf ("Usage: %s <aeskey> <token>\n", argv[0]);
      printf (" AESKEY:\tHex encoded AES-key.\n");
      printf (" TOKEN:\t\tModHex encoded token.\n");
      return EXIT_FAILURE;
    }

  aeskey = argv[1];
  token = argv[2];

  if (strlen (aeskey) != 32)
    {
      printf ("error: Hex encoded AES-key must be 32 characters.\n");
      return EXIT_FAILURE;
    }

  if (strlen (token) > 32)
    {
      printf ("warning: overlong token, ignoring prefix: %.*s\n",
	      (int) strlen (token) - 32, token);
      token = token + (strlen (token) - 32);
    }

  if (strlen (token) != 32)
    {
      printf ("error: ModHex encoded token must be 32 characters.\n");
      return EXIT_FAILURE;
    }

  /* Debug. */
  printf ("Input:\n");
  printf ("  token: %s\n", token);

  yubikey_modhex_decode ((char *) key, token, YUBIKEY_KEY_SIZE);

  {
    size_t i;
    printf ("          ");
    for (i = 0; i < YUBIKEY_KEY_SIZE; i++)
      printf ("%02x ", key[i] & 0xFF);
    printf ("\n");
  }

  printf ("  aeskey: %s\n", aeskey);

  yubikey_hex_decode ((char *) key, aeskey, YUBIKEY_KEY_SIZE);

  {
    size_t i;
    printf ("          ");
    for (i = 0; i < YUBIKEY_KEY_SIZE; i++)
      printf ("%02x ", key[i] & 0xFF);
    printf ("\n");
  }

  /* Pack up dynamic password, decrypt it and verify checksum */
  yubikey_parse ((uint8_t *) token, key, &tok);

  printf ("Output:\n");
  {
    size_t i;
    printf ("          ");
    for (i = 0; i < YUBIKEY_BLOCK_SIZE; i++)
      printf ("%02x ", ((uint8_t *) & tok)[i] & 0xFF);
    printf ("\n");
  }

  printf ("\nStruct:\n");
  /* Debug */
  {
    size_t i;
    printf ("  uid: ");
    for (i = 0; i < YUBIKEY_UID_SIZE; i++)
      printf ("%02x ", tok.uid[i] & 0xFF);
    printf ("\n");
  }
  printf ("  counter: %d (0x%04x)\n", tok.ctr, tok.ctr);
  printf ("  timestamp (low): %d (0x%04x)\n", tok.tstpl, tok.tstpl);
  printf ("  timestamp (high): %d (0x%02x)\n", tok.tstph, tok.tstph);
  printf ("  session use: %d (0x%02x)\n", tok.use, tok.use);
  printf ("  random: %d (0x%02x)\n", tok.rnd, tok.rnd);
  printf ("  crc: %d (0x%04x)\n", tok.crc, tok.crc);

  printf ("\nDerived:\n");
  printf ("  cleaned counter: %d (0x%04x)\n",
	  yubikey_counter (tok.ctr), yubikey_counter (tok.ctr));
  yubikey_modhex_encode ((char *) buf, (char *) tok.uid, YUBIKEY_UID_SIZE);
  printf ("  modhex uid: %s\n", buf);
  printf ("  triggered by caps lock: %s\n",
	  yubikey_capslock (tok.ctr) ? "yes" : "no");
  printf ("  crc: %04X\n", yubikey_crc16 ((void *) &tok, YUBIKEY_KEY_SIZE));

  printf ("  crc check: ");
  if (yubikey_crc_ok_p ((uint8_t *) & tok))
    {
      printf ("ok\n");
      return EXIT_SUCCESS;
    }

  printf ("fail\n");
  return EXIT_FAILURE;
}