blob: 54128ff08d827fd7857110e694c857fa895b1860 [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>
Jan Engelhardta6d83932013-06-03 01:38:57 +020028#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010029#include <getopt.h>
Harald Welteb53717f2012-08-02 08:42:59 +020030#include <unistd.h>
Holger Hans Peter Freythera652abc2013-07-14 09:11:47 +020031#include <inttypes.h>
32#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010033
34#include <osmocom/crypt/auth.h>
35#include <osmocom/core/utils.h>
36
Harald Welte57799ed2012-06-27 15:06:19 +020037static void dump_triplets_dat(struct osmo_auth_vector *vec)
38{
39 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
40 fprintf(stderr, "triplets.dat doesn't support UMTS!\n");
41 return;
42 }
43 printf("imsi,");
44 printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
45 printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
46 printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
47}
48
Harald Welte915e0ef2011-12-07 02:38:42 +010049static void dump_auth_vec(struct osmo_auth_vector *vec)
50{
51 printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand)));
52
53 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
54 printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn)));
55 printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik)));
56 printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck)));
57 printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len));
58 }
59
60 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
61 printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres)));
62 printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc)));
63 }
64}
65
66static struct osmo_sub_auth_data test_aud = {
67 .type = OSMO_AUTH_TYPE_NONE,
68 .algo = OSMO_AUTH_ALG_NONE,
69};
70
Harald Welte5fb795e2012-03-21 08:51:48 +010071static void help()
72{
73 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
74 "-3 --3g\tUse 3G (UMTS) authentication\n"
75 "-a --algorithm\tSpecify name of the algorithm\n"
76 "-k --key\tSpecify Ki / K\n"
77 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +010078 "-O --op\tSpecify OP (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +010079 "-a --amf\tSpecify AMF (only for 3G)\n"
80 "-s --sqn\tSpecify SQN (only for 3G)\n"
Harald Weltecebf3f02012-03-22 16:45:23 +010081 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +020082 "-r --rand\tSpecify random value\n"
83 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Harald Welte5fb795e2012-03-21 08:51:48 +010084}
85
Harald Welte915e0ef2011-12-07 02:38:42 +010086int main(int argc, char **argv)
87{
88 struct osmo_auth_vector _vec;
89 struct osmo_auth_vector *vec = &_vec;
Harald Weltecebf3f02012-03-22 16:45:23 +010090 uint8_t _rand[16], _auts[16];
Harald Welte915e0ef2011-12-07 02:38:42 +010091 int rc, option_index;
92 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +010093 int auts_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +020094 int fmt_triplets_dat = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +010095
Harald Weltebc6f56c2012-03-21 17:37:53 +010096 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +010097 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
98
Harald Weltecebf3f02012-03-22 16:45:23 +010099 memset(_auts, 0, sizeof(_auts));
100
Harald Welte915e0ef2011-12-07 02:38:42 +0100101 while (1) {
102 int c;
103 unsigned long ul;
104 static struct option long_options[] = {
105 { "2g", 0, 0, '2' },
106 { "3g", 0, 0, '3' },
107 { "algorithm", 1, 0, 'a' },
108 { "key", 1, 0, 'k' },
109 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100110 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100111 { "amf", 1, 0, 'f' },
112 { "sqn", 1, 0, 's' },
113 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100114 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100115 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100116 { 0, 0, 0, 0 }
117 };
118
119 rc = 0;
120
Harald Welte57799ed2012-06-27 15:06:19 +0200121 c = getopt_long(argc, argv, "23a:k:o:f:s:r:hO:A:I", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +0100122 &option_index);
123
124 if (c == -1)
125 break;
126
127 switch (c) {
128 case '2':
129 test_aud.type = OSMO_AUTH_TYPE_GSM;
130 break;
131 case '3':
132 test_aud.type = OSMO_AUTH_TYPE_UMTS;
133 break;
134 case 'a':
135 rc = osmo_auth_alg_parse(optarg);
136 if (rc < 0)
137 break;
138 test_aud.algo = rc;
139 break;
140 case 'k':
141 switch (test_aud.type) {
142 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100143 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
144 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100145 break;
146 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100147 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
148 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100149 break;
150 default:
151 fprintf(stderr, "please specify 2g/3g first!\n");
152 }
153 break;
154 case 'o':
155 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
156 fprintf(stderr, "Only UMTS has OPC\n");
157 exit(2);
158 }
Harald Welteaae23622011-12-07 11:35:02 +0100159 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
160 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100161 test_aud.u.umts.opc_is_op = 0;
162 break;
163 case 'O':
164 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
165 fprintf(stderr, "Only UMTS has OP\n");
166 exit(2);
167 }
168 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
169 sizeof(test_aud.u.umts.opc));
170 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100171 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100172 case 'A':
173 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
174 fprintf(stderr, "Only UMTS has AUTS\n");
175 exit(2);
176 }
177 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
178 auts_is_set = 1;
179 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100180 case 'f':
181 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
182 fprintf(stderr, "Only UMTS has AMF\n");
183 exit(2);
184 }
Harald Welteaae23622011-12-07 11:35:02 +0100185 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
186 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100187 break;
188 case 's':
189 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
190 fprintf(stderr, "Only UMTS has SQN\n");
191 exit(2);
192 }
193 ul = strtoul(optarg, 0, 10);
Harald Welteaae23622011-12-07 11:35:02 +0100194 test_aud.u.umts.sqn = ul;
Harald Welte915e0ef2011-12-07 02:38:42 +0100195 break;
196 case 'r':
197 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
198 rand_is_set = 1;
199 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200200 case 'I':
201 fmt_triplets_dat = 1;
202 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100203 case 'h':
204 help();
205 exit(0);
206 default:
207 help();
208 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100209 }
210
211 if (rc < 0) {
212 fprintf(stderr, "Error parsing argument of option `%c'\n", c);
213 exit(2);
214 }
215 }
216
217 if (!rand_is_set) {
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200218 int i;
Harald Welte915e0ef2011-12-07 02:38:42 +0100219 printf("WARNING: We're using really weak random numbers!\n\n");
220 srand(time(NULL));
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200221
222 for (i = 0; i < 4; ++i) {
223 uint32_t r;
224 r = rand();
225 memcpy(&_rand[i*4], &r, 4);
226 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100227 }
228
Harald Welte5fb795e2012-03-21 08:51:48 +0100229 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
230 test_aud.algo == OSMO_AUTH_ALG_NONE) {
231 help();
232 exit(2);
233 }
234
Harald Welte915e0ef2011-12-07 02:38:42 +0100235 memset(vec, 0, sizeof(*vec));
236
Harald Weltecebf3f02012-03-22 16:45:23 +0100237 if (!auts_is_set)
238 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
239 else
240 rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100241 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100242 if (!auts_is_set)
243 fprintf(stderr, "error generating auth vector\n");
244 else
245 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100246 exit(1);
247 }
248
Harald Welte57799ed2012-06-27 15:06:19 +0200249 if (fmt_triplets_dat)
250 dump_triplets_dat(vec);
251 else
252 dump_auth_vec(vec);
Harald Welte915e0ef2011-12-07 02:38:42 +0100253
Harald Weltecebf3f02012-03-22 16:45:23 +0100254 if (auts_is_set)
Holger Hans Peter Freythera652abc2013-07-14 09:11:47 +0200255 printf("AUTS success: SEQ.MS = %" PRIu64 "\n", test_aud.u.umts.sqn);
Harald Welte915e0ef2011-12-07 02:38:42 +0100256
Harald Weltecebf3f02012-03-22 16:45:23 +0100257 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100258}