blob: 4f36dee5bd3b966759bf0b5132ef316720b84fea [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"
Harald Weltecebf3f02012-03-22 16:45:23 +010084 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +020085 "-r --rand\tSpecify random value\n"
86 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010087
88 fprintf(stderr, "\nAvailable algorithms for option -a:\n");
89 for (alg = 1; alg < _OSMO_AUTH_ALG_NUM; alg++)
90 fprintf(stderr, " %s\n",
91 osmo_auth_alg_name(alg));
Harald Welte5fb795e2012-03-21 08:51:48 +010092}
93
Harald Welte915e0ef2011-12-07 02:38:42 +010094int main(int argc, char **argv)
95{
96 struct osmo_auth_vector _vec;
97 struct osmo_auth_vector *vec = &_vec;
Neels Hofmeyr8352d312017-02-02 20:05:14 +010098 uint8_t _rand[16], _auts[14];
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +020099 uint64_t sqn;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200100 unsigned int ind;
Harald Welte915e0ef2011-12-07 02:38:42 +0100101 int rc, option_index;
102 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +0100103 int auts_is_set = 0;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200104 int sqn_is_set = 0;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200105 int ind_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +0200106 int fmt_triplets_dat = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +0100107
Harald Weltebc6f56c2012-03-21 17:37:53 +0100108 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100109 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
110
Harald Weltecebf3f02012-03-22 16:45:23 +0100111 memset(_auts, 0, sizeof(_auts));
112
Harald Welte915e0ef2011-12-07 02:38:42 +0100113 while (1) {
114 int c;
Harald Welte915e0ef2011-12-07 02:38:42 +0100115 static struct option long_options[] = {
116 { "2g", 0, 0, '2' },
117 { "3g", 0, 0, '3' },
118 { "algorithm", 1, 0, 'a' },
119 { "key", 1, 0, 'k' },
120 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100121 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100122 { "amf", 1, 0, 'f' },
123 { "sqn", 1, 0, 's' },
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200124 { "ind", 1, 0, 'i' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100125 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100126 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100127 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100128 { 0, 0, 0, 0 }
129 };
130
131 rc = 0;
132
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200133 c = getopt_long(argc, argv, "23a:k:o:f:s:i:r:hO:A:I", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +0100134 &option_index);
135
136 if (c == -1)
137 break;
138
139 switch (c) {
140 case '2':
141 test_aud.type = OSMO_AUTH_TYPE_GSM;
142 break;
143 case '3':
144 test_aud.type = OSMO_AUTH_TYPE_UMTS;
Neels Hofmeyr4315e012017-08-26 21:40:11 +0200145 test_aud.u.umts.ind_bitlen = 5;
Harald Welte915e0ef2011-12-07 02:38:42 +0100146 break;
147 case 'a':
148 rc = osmo_auth_alg_parse(optarg);
149 if (rc < 0)
150 break;
151 test_aud.algo = rc;
152 break;
153 case 'k':
154 switch (test_aud.type) {
155 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100156 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
157 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100158 break;
159 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100160 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
161 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100162 break;
163 default:
164 fprintf(stderr, "please specify 2g/3g first!\n");
165 }
166 break;
167 case 'o':
168 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
169 fprintf(stderr, "Only UMTS has OPC\n");
170 exit(2);
171 }
Harald Welteaae23622011-12-07 11:35:02 +0100172 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
173 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100174 test_aud.u.umts.opc_is_op = 0;
175 break;
176 case 'O':
177 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
178 fprintf(stderr, "Only UMTS has OP\n");
179 exit(2);
180 }
181 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
182 sizeof(test_aud.u.umts.opc));
183 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100184 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100185 case 'A':
186 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
187 fprintf(stderr, "Only UMTS has AUTS\n");
188 exit(2);
189 }
190 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
191 auts_is_set = 1;
192 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100193 case 'f':
194 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
195 fprintf(stderr, "Only UMTS has AMF\n");
196 exit(2);
197 }
Harald Welteaae23622011-12-07 11:35:02 +0100198 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
199 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100200 break;
201 case 's':
202 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
203 fprintf(stderr, "Only UMTS has SQN\n");
204 exit(2);
205 }
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200206 sqn = strtoull(optarg, 0, 10);
207 sqn_is_set = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100208 break;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200209 case 'i':
210 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
211 fprintf(stderr, "Only UMTS has IND\n");
212 exit(2);
213 }
214 ind = atoi(optarg);
215 ind_is_set = 1;
216 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100217 case 'r':
218 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
219 rand_is_set = 1;
220 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200221 case 'I':
222 fmt_triplets_dat = 1;
223 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100224 case 'h':
225 help();
226 exit(0);
227 default:
228 help();
229 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100230 }
231
232 if (rc < 0) {
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100233 help();
234 fprintf(stderr, "\nError parsing argument of option `%c'\n", c);
Harald Welte915e0ef2011-12-07 02:38:42 +0100235 exit(2);
236 }
237 }
238
239 if (!rand_is_set) {
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200240 int i;
Harald Welte915e0ef2011-12-07 02:38:42 +0100241 printf("WARNING: We're using really weak random numbers!\n\n");
242 srand(time(NULL));
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200243
244 for (i = 0; i < 4; ++i) {
245 uint32_t r;
246 r = rand();
247 memcpy(&_rand[i*4], &r, 4);
248 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100249 }
250
Harald Welte5fb795e2012-03-21 08:51:48 +0100251 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
252 test_aud.algo == OSMO_AUTH_ALG_NONE) {
253 help();
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100254 fprintf(stderr, "\nError: you need to pass at least"
255 " -2 or -3, as well as an algorithm to use.\n");
Harald Welte5fb795e2012-03-21 08:51:48 +0100256 exit(2);
257 }
258
Harald Welte915e0ef2011-12-07 02:38:42 +0100259 memset(vec, 0, sizeof(*vec));
260
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200261 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
262 uint64_t seq_1 = 1LL << test_aud.u.umts.ind_bitlen;
263 uint64_t ind_mask = seq_1 - 1;
264
265 if (sqn_is_set) {
266 /* Before calculating the UMTS auth vector, osmo_auth_gen_vec() increments SEQ.
267 * To end up with the SQN passed in by the user, we need to pass in SEQ-1, and
268 * indicate which IND slot to target. */
269 test_aud.u.umts.sqn = sqn - seq_1;
270 test_aud.u.umts.ind = sqn & ind_mask;
271 }
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200272
273 if (sqn_is_set && ind_is_set) {
274 fprintf(stderr, "Requesting --sqn %"PRIu64" implies IND=%u,"
275 " so no further --ind argument is allowed.\n",
276 sqn, test_aud.u.umts.ind);
277 exit(2);
278 }
279
280 if (ind_is_set) {
281 if (ind >= (1 << test_aud.u.umts.ind_bitlen)) {
282 fprintf(stderr, "Requested --ind %u is too large for IND bitlen of %u\n",
283 ind, test_aud.u.umts.ind_bitlen);
284 exit(2);
285 }
286 test_aud.u.umts.ind = ind;
287 }
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200288 }
289
Harald Weltecebf3f02012-03-22 16:45:23 +0100290 if (!auts_is_set)
291 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
292 else
293 rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100294 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100295 if (!auts_is_set)
296 fprintf(stderr, "error generating auth vector\n");
297 else
298 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100299 exit(1);
300 }
301
Harald Welte57799ed2012-06-27 15:06:19 +0200302 if (fmt_triplets_dat)
303 dump_triplets_dat(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100304 else {
Harald Welte57799ed2012-06-27 15:06:19 +0200305 dump_auth_vec(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100306 if (test_aud.type == OSMO_AUTH_TYPE_UMTS)
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100307 printf("SQN:\t%" PRIu64 "\n", test_aud.u.umts.sqn);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100308 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100309
Harald Weltecebf3f02012-03-22 16:45:23 +0100310 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100311}