blob: fee976789523fcfed00a577c23b4cb2c46ad23d6 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file osmo-auc-gen.c
2 * GSM/GPRS/3G authentication testing tool. */
3/*
4 * (C) 2010-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte915e0ef2011-12-07 02:38:42 +01005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <errno.h>
28#include <string.h>
Jan Engelhardta6d83932013-06-03 01:38:57 +020029#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010030#include <getopt.h>
Harald Welteb53717f2012-08-02 08:42:59 +020031#include <unistd.h>
Holger Hans Peter Freythera652abc2013-07-14 09:11:47 +020032#include <inttypes.h>
33#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010034
35#include <osmocom/crypt/auth.h>
36#include <osmocom/core/utils.h>
37
Harald Welte57799ed2012-06-27 15:06:19 +020038static void dump_triplets_dat(struct osmo_auth_vector *vec)
39{
40 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
41 fprintf(stderr, "triplets.dat doesn't support UMTS!\n");
42 return;
43 }
44 printf("imsi,");
45 printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
46 printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
47 printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
48}
49
Harald Welte915e0ef2011-12-07 02:38:42 +010050static void dump_auth_vec(struct osmo_auth_vector *vec)
51{
Harald Welte4f511b62016-05-18 19:36:42 +020052 printf("RAND:\t%s\n", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
Harald Welte915e0ef2011-12-07 02:38:42 +010053
54 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
Harald Welte4f511b62016-05-18 19:36:42 +020055 printf("AUTN:\t%s\n", osmo_hexdump_nospc(vec->autn, sizeof(vec->autn)));
56 printf("IK:\t%s\n", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
57 printf("CK:\t%s\n", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
58 printf("RES:\t%s\n", osmo_hexdump_nospc(vec->res, vec->res_len));
Harald Welte915e0ef2011-12-07 02:38:42 +010059 }
60
61 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
Harald Welte4f511b62016-05-18 19:36:42 +020062 printf("SRES:\t%s\n", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
63 printf("Kc:\t%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
Harald Welte915e0ef2011-12-07 02:38:42 +010064 }
65}
66
67static struct osmo_sub_auth_data test_aud = {
68 .type = OSMO_AUTH_TYPE_NONE,
69 .algo = OSMO_AUTH_ALG_NONE,
70};
71
Harald Welte5fb795e2012-03-21 08:51:48 +010072static void help()
73{
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010074 int alg;
Harald Welte5fb795e2012-03-21 08:51:48 +010075 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
76 "-3 --3g\tUse 3G (UMTS) authentication\n"
77 "-a --algorithm\tSpecify name of the algorithm\n"
78 "-k --key\tSpecify Ki / K\n"
79 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +010080 "-O --op\tSpecify OP (only for 3G)\n"
Holger Hans Peter Freyther91ff17c2015-05-26 00:11:37 +080081 "-f --amf\tSpecify AMF (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +010082 "-s --sqn\tSpecify SQN (only for 3G)\n"
Neels Hofmeyr3cb08272017-08-26 21:45:33 +020083 "-i --ind\tSpecify IND slot for new SQN after AUTS (only for 3G)\n"
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +020084 "-l --ind-len\tSpecify IND bit length (default=5) (only for 3G)\n"
Harald Weltecebf3f02012-03-22 16:45:23 +010085 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +020086 "-r --rand\tSpecify random value\n"
87 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010088
89 fprintf(stderr, "\nAvailable algorithms for option -a:\n");
90 for (alg = 1; alg < _OSMO_AUTH_ALG_NUM; alg++)
91 fprintf(stderr, " %s\n",
92 osmo_auth_alg_name(alg));
Harald Welte5fb795e2012-03-21 08:51:48 +010093}
94
Harald Welte915e0ef2011-12-07 02:38:42 +010095int main(int argc, char **argv)
96{
97 struct osmo_auth_vector _vec;
98 struct osmo_auth_vector *vec = &_vec;
Neels Hofmeyr8352d312017-02-02 20:05:14 +010099 uint8_t _rand[16], _auts[14];
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200100 uint64_t sqn;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200101 unsigned int ind;
Harald Welte915e0ef2011-12-07 02:38:42 +0100102 int rc, option_index;
103 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +0100104 int auts_is_set = 0;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200105 int sqn_is_set = 0;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200106 int ind_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +0200107 int fmt_triplets_dat = 0;
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200108 uint64_t seq_1;
109 uint64_t ind_mask;
Harald Welte915e0ef2011-12-07 02:38:42 +0100110
Harald Weltebc6f56c2012-03-21 17:37:53 +0100111 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100112 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
113
Harald Weltecebf3f02012-03-22 16:45:23 +0100114 memset(_auts, 0, sizeof(_auts));
115
Harald Welte915e0ef2011-12-07 02:38:42 +0100116 while (1) {
117 int c;
Harald Welte915e0ef2011-12-07 02:38:42 +0100118 static struct option long_options[] = {
119 { "2g", 0, 0, '2' },
120 { "3g", 0, 0, '3' },
121 { "algorithm", 1, 0, 'a' },
122 { "key", 1, 0, 'k' },
123 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100124 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100125 { "amf", 1, 0, 'f' },
126 { "sqn", 1, 0, 's' },
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200127 { "ind", 1, 0, 'i' },
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200128 { "ind-len", 1, 0, 'l' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100129 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100130 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100131 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100132 { 0, 0, 0, 0 }
133 };
134
135 rc = 0;
136
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200137 c = getopt_long(argc, argv, "23a:k:o:f:s:i:l:r:hO:A:I", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +0100138 &option_index);
139
140 if (c == -1)
141 break;
142
143 switch (c) {
144 case '2':
145 test_aud.type = OSMO_AUTH_TYPE_GSM;
146 break;
147 case '3':
148 test_aud.type = OSMO_AUTH_TYPE_UMTS;
Neels Hofmeyr4315e012017-08-26 21:40:11 +0200149 test_aud.u.umts.ind_bitlen = 5;
Harald Welte915e0ef2011-12-07 02:38:42 +0100150 break;
151 case 'a':
152 rc = osmo_auth_alg_parse(optarg);
153 if (rc < 0)
154 break;
155 test_aud.algo = rc;
156 break;
157 case 'k':
158 switch (test_aud.type) {
159 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100160 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
161 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100162 break;
163 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100164 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
165 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100166 break;
167 default:
168 fprintf(stderr, "please specify 2g/3g first!\n");
169 }
170 break;
171 case 'o':
172 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
173 fprintf(stderr, "Only UMTS has OPC\n");
174 exit(2);
175 }
Harald Welteaae23622011-12-07 11:35:02 +0100176 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
177 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100178 test_aud.u.umts.opc_is_op = 0;
179 break;
180 case 'O':
181 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
182 fprintf(stderr, "Only UMTS has OP\n");
183 exit(2);
184 }
185 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
186 sizeof(test_aud.u.umts.opc));
187 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100188 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100189 case 'A':
190 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
191 fprintf(stderr, "Only UMTS has AUTS\n");
192 exit(2);
193 }
194 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
195 auts_is_set = 1;
196 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100197 case 'f':
198 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
199 fprintf(stderr, "Only UMTS has AMF\n");
200 exit(2);
201 }
Harald Welteaae23622011-12-07 11:35:02 +0100202 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
203 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100204 break;
205 case 's':
206 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
207 fprintf(stderr, "Only UMTS has SQN\n");
208 exit(2);
209 }
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200210 sqn = strtoull(optarg, 0, 10);
211 sqn_is_set = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100212 break;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200213 case 'i':
214 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
215 fprintf(stderr, "Only UMTS has IND\n");
216 exit(2);
217 }
218 ind = atoi(optarg);
219 ind_is_set = 1;
220 break;
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200221 case 'l':
222 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
223 fprintf(stderr, "Only UMTS has IND bitlen\n");
224 exit(2);
225 }
226 test_aud.u.umts.ind_bitlen = atoi(optarg);
227 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100228 case 'r':
229 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
230 rand_is_set = 1;
231 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200232 case 'I':
233 fmt_triplets_dat = 1;
234 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100235 case 'h':
236 help();
237 exit(0);
238 default:
239 help();
240 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100241 }
242
243 if (rc < 0) {
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100244 help();
245 fprintf(stderr, "\nError parsing argument of option `%c'\n", c);
Harald Welte915e0ef2011-12-07 02:38:42 +0100246 exit(2);
247 }
248 }
249
250 if (!rand_is_set) {
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200251 int i;
Harald Welte915e0ef2011-12-07 02:38:42 +0100252 printf("WARNING: We're using really weak random numbers!\n\n");
253 srand(time(NULL));
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200254
255 for (i = 0; i < 4; ++i) {
256 uint32_t r;
257 r = rand();
258 memcpy(&_rand[i*4], &r, 4);
259 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100260 }
261
Harald Welte5fb795e2012-03-21 08:51:48 +0100262 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
263 test_aud.algo == OSMO_AUTH_ALG_NONE) {
264 help();
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100265 fprintf(stderr, "\nError: you need to pass at least"
266 " -2 or -3, as well as an algorithm to use.\n");
Harald Welte5fb795e2012-03-21 08:51:48 +0100267 exit(2);
268 }
269
Harald Welte915e0ef2011-12-07 02:38:42 +0100270 memset(vec, 0, sizeof(*vec));
271
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200272 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200273 seq_1 = 1LL << test_aud.u.umts.ind_bitlen;
274 ind_mask = seq_1 - 1;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200275
276 if (sqn_is_set) {
277 /* Before calculating the UMTS auth vector, osmo_auth_gen_vec() increments SEQ.
278 * To end up with the SQN passed in by the user, we need to pass in SEQ-1, and
279 * indicate which IND slot to target. */
280 test_aud.u.umts.sqn = sqn - seq_1;
281 test_aud.u.umts.ind = sqn & ind_mask;
282 }
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200283
284 if (sqn_is_set && ind_is_set) {
285 fprintf(stderr, "Requesting --sqn %"PRIu64" implies IND=%u,"
286 " so no further --ind argument is allowed.\n",
287 sqn, test_aud.u.umts.ind);
288 exit(2);
289 }
290
291 if (ind_is_set) {
292 if (ind >= (1 << test_aud.u.umts.ind_bitlen)) {
293 fprintf(stderr, "Requested --ind %u is too large for IND bitlen of %u\n",
294 ind, test_aud.u.umts.ind_bitlen);
295 exit(2);
296 }
297 test_aud.u.umts.ind = ind;
298 }
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200299 }
300
Harald Weltecebf3f02012-03-22 16:45:23 +0100301 if (!auts_is_set)
302 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
303 else
304 rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100305 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100306 if (!auts_is_set)
307 fprintf(stderr, "error generating auth vector\n");
308 else
309 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100310 exit(1);
311 }
312
Harald Welte57799ed2012-06-27 15:06:19 +0200313 if (fmt_triplets_dat)
314 dump_triplets_dat(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100315 else {
Harald Welte57799ed2012-06-27 15:06:19 +0200316 dump_auth_vec(vec);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200317 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100318 printf("SQN:\t%" PRIu64 "\n", test_aud.u.umts.sqn);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200319 printf("IND:\t%u\n", (unsigned int)(test_aud.u.umts.sqn & ind_mask));
Neels Hofmeyr2066a422017-08-26 22:43:50 +0200320 if (auts_is_set)
321 printf("SQN.MS:\t%" PRIu64 "\n", test_aud.u.umts.sqn_ms);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200322 }
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100323 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100324
Harald Weltecebf3f02012-03-22 16:45:23 +0100325 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100326}