blob: 266d0d6a50d79ef32376e76ad9844c87525a6e67 [file] [log] [blame]
Harald Welte915e0ef2011-12-07 02:38:42 +01001/* GSM/GPRS/3G authentication testing tool */
2
Harald Weltebc6f56c2012-03-21 17:37:53 +01003/* (C) 2010-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte915e0ef2011-12-07 02:38:42 +01004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
28#include <getopt.h>
29
30#include <osmocom/crypt/auth.h>
31#include <osmocom/core/utils.h>
32
Harald Welte57799ed2012-06-27 15:06:19 +020033static void dump_triplets_dat(struct osmo_auth_vector *vec)
34{
35 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
36 fprintf(stderr, "triplets.dat doesn't support UMTS!\n");
37 return;
38 }
39 printf("imsi,");
40 printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
41 printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
42 printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
43}
44
Harald Welte915e0ef2011-12-07 02:38:42 +010045static void dump_auth_vec(struct osmo_auth_vector *vec)
46{
47 printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand)));
48
49 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
50 printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn)));
51 printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik)));
52 printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck)));
53 printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len));
54 }
55
56 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
57 printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres)));
58 printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc)));
59 }
60}
61
62static struct osmo_sub_auth_data test_aud = {
63 .type = OSMO_AUTH_TYPE_NONE,
64 .algo = OSMO_AUTH_ALG_NONE,
65};
66
Harald Welte5fb795e2012-03-21 08:51:48 +010067static void help()
68{
69 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
70 "-3 --3g\tUse 3G (UMTS) authentication\n"
71 "-a --algorithm\tSpecify name of the algorithm\n"
72 "-k --key\tSpecify Ki / K\n"
73 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +010074 "-O --op\tSpecify OP (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +010075 "-a --amf\tSpecify AMF (only for 3G)\n"
76 "-s --sqn\tSpecify SQN (only for 3G)\n"
Harald Weltecebf3f02012-03-22 16:45:23 +010077 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +020078 "-r --rand\tSpecify random value\n"
79 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Harald Welte5fb795e2012-03-21 08:51:48 +010080}
81
Harald Welte915e0ef2011-12-07 02:38:42 +010082int main(int argc, char **argv)
83{
84 struct osmo_auth_vector _vec;
85 struct osmo_auth_vector *vec = &_vec;
Harald Weltecebf3f02012-03-22 16:45:23 +010086 uint8_t _rand[16], _auts[16];
Harald Welte915e0ef2011-12-07 02:38:42 +010087 int rc, option_index;
88 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +010089 int auts_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +020090 int fmt_triplets_dat = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +010091
Harald Weltebc6f56c2012-03-21 17:37:53 +010092 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +010093 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
94
Harald Weltecebf3f02012-03-22 16:45:23 +010095 memset(_auts, 0, sizeof(_auts));
96
Harald Welte915e0ef2011-12-07 02:38:42 +010097 while (1) {
98 int c;
99 unsigned long ul;
100 static struct option long_options[] = {
101 { "2g", 0, 0, '2' },
102 { "3g", 0, 0, '3' },
103 { "algorithm", 1, 0, 'a' },
104 { "key", 1, 0, 'k' },
105 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100106 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100107 { "amf", 1, 0, 'f' },
108 { "sqn", 1, 0, 's' },
109 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100110 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100111 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100112 { 0, 0, 0, 0 }
113 };
114
115 rc = 0;
116
Harald Welte57799ed2012-06-27 15:06:19 +0200117 c = getopt_long(argc, argv, "23a:k:o:f:s:r:hO:A:I", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +0100118 &option_index);
119
120 if (c == -1)
121 break;
122
123 switch (c) {
124 case '2':
125 test_aud.type = OSMO_AUTH_TYPE_GSM;
126 break;
127 case '3':
128 test_aud.type = OSMO_AUTH_TYPE_UMTS;
129 break;
130 case 'a':
131 rc = osmo_auth_alg_parse(optarg);
132 if (rc < 0)
133 break;
134 test_aud.algo = rc;
135 break;
136 case 'k':
137 switch (test_aud.type) {
138 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100139 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
140 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100141 break;
142 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100143 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
144 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100145 break;
146 default:
147 fprintf(stderr, "please specify 2g/3g first!\n");
148 }
149 break;
150 case 'o':
151 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
152 fprintf(stderr, "Only UMTS has OPC\n");
153 exit(2);
154 }
Harald Welteaae23622011-12-07 11:35:02 +0100155 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
156 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100157 test_aud.u.umts.opc_is_op = 0;
158 break;
159 case 'O':
160 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
161 fprintf(stderr, "Only UMTS has OP\n");
162 exit(2);
163 }
164 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
165 sizeof(test_aud.u.umts.opc));
166 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100167 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100168 case 'A':
169 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
170 fprintf(stderr, "Only UMTS has AUTS\n");
171 exit(2);
172 }
173 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
174 auts_is_set = 1;
175 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100176 case 'f':
177 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
178 fprintf(stderr, "Only UMTS has AMF\n");
179 exit(2);
180 }
Harald Welteaae23622011-12-07 11:35:02 +0100181 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
182 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100183 break;
184 case 's':
185 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
186 fprintf(stderr, "Only UMTS has SQN\n");
187 exit(2);
188 }
189 ul = strtoul(optarg, 0, 10);
Harald Welteaae23622011-12-07 11:35:02 +0100190 test_aud.u.umts.sqn = ul;
Harald Welte915e0ef2011-12-07 02:38:42 +0100191 break;
192 case 'r':
193 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
194 rand_is_set = 1;
195 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200196 case 'I':
197 fmt_triplets_dat = 1;
198 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100199 case 'h':
200 help();
201 exit(0);
202 default:
203 help();
204 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100205 }
206
207 if (rc < 0) {
208 fprintf(stderr, "Error parsing argument of option `%c'\n", c);
209 exit(2);
210 }
211 }
212
213 if (!rand_is_set) {
214 printf("WARNING: We're using really weak random numbers!\n\n");
215 srand(time(NULL));
216 *(uint32_t *)&_rand[0] = rand();
217 *(uint32_t *)(&_rand[4]) = rand();
218 *(uint32_t *)(&_rand[8]) = rand();
219 *(uint32_t *)(&_rand[12]) = rand();
220 }
221
Harald Welte5fb795e2012-03-21 08:51:48 +0100222 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
223 test_aud.algo == OSMO_AUTH_ALG_NONE) {
224 help();
225 exit(2);
226 }
227
Harald Welte915e0ef2011-12-07 02:38:42 +0100228 memset(vec, 0, sizeof(*vec));
229
Harald Weltecebf3f02012-03-22 16:45:23 +0100230 if (!auts_is_set)
231 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
232 else
233 rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100234 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100235 if (!auts_is_set)
236 fprintf(stderr, "error generating auth vector\n");
237 else
238 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100239 exit(1);
240 }
241
Harald Welte57799ed2012-06-27 15:06:19 +0200242 if (fmt_triplets_dat)
243 dump_triplets_dat(vec);
244 else
245 dump_auth_vec(vec);
Harald Welte915e0ef2011-12-07 02:38:42 +0100246
Harald Weltecebf3f02012-03-22 16:45:23 +0100247 if (auts_is_set)
Harald Welteaae23622011-12-07 11:35:02 +0100248 printf("AUTS success: SEQ.MS = %lu\n", test_aud.u.umts.sqn);
Harald Welte915e0ef2011-12-07 02:38:42 +0100249
Harald Weltecebf3f02012-03-22 16:45:23 +0100250 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100251}