blob: 86abeda6a25e5aeb1c05f91fe92e778d3ce7f36d [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file osmo-auc-gen.c
2 * GSM/GPRS/3G authentication testing tool. */
3/*
Harald Welte0ea9b912023-05-30 12:05:56 +02004 * (C) 2010-2023 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 *
Harald Welte915e0ef2011-12-07 02:38:42 +010018 */
19
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <errno.h>
24#include <string.h>
Jan Engelhardta6d83932013-06-03 01:38:57 +020025#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010026#include <getopt.h>
Harald Welteb53717f2012-08-02 08:42:59 +020027#include <unistd.h>
Holger Hans Peter Freythera652abc2013-07-14 09:11:47 +020028#include <inttypes.h>
29#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010030
31#include <osmocom/crypt/auth.h>
32#include <osmocom/core/utils.h>
Harald Welte53b4bbb2021-09-16 21:36:16 +020033#include <osmocom/core/base64.h>
Max4b2b0cc2017-07-10 14:32:48 +020034#include <osmocom/gsm/gsm_utils.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010035
Harald Welte53b4bbb2021-09-16 21:36:16 +020036static void print_base64(const char *fmt, const uint8_t *data, unsigned int len)
37{
38 uint8_t outbuf[256];
39 size_t olen;
40
41 OSMO_ASSERT(osmo_base64_encode(outbuf, sizeof(outbuf), &olen, data, len) == 0);
42 OSMO_ASSERT(sizeof(outbuf) > olen);
43 outbuf[olen] = '\0';
44 printf(fmt, outbuf);
45}
46
Harald Welte57799ed2012-06-27 15:06:19 +020047static void dump_triplets_dat(struct osmo_auth_vector *vec)
48{
49 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
50 fprintf(stderr, "triplets.dat doesn't support UMTS!\n");
51 return;
52 }
53 printf("imsi,");
54 printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
55 printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
56 printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
57}
58
Harald Welte915e0ef2011-12-07 02:38:42 +010059static void dump_auth_vec(struct osmo_auth_vector *vec)
60{
Harald Welte4f511b62016-05-18 19:36:42 +020061 printf("RAND:\t%s\n", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
Harald Welte915e0ef2011-12-07 02:38:42 +010062
63 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
Harald Welte53b4bbb2021-09-16 21:36:16 +020064 uint8_t inbuf[sizeof(vec->rand) + sizeof(vec->autn)];
65
Harald Welte4f511b62016-05-18 19:36:42 +020066 printf("AUTN:\t%s\n", osmo_hexdump_nospc(vec->autn, sizeof(vec->autn)));
67 printf("IK:\t%s\n", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
68 printf("CK:\t%s\n", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
69 printf("RES:\t%s\n", osmo_hexdump_nospc(vec->res, vec->res_len));
Harald Welte53b4bbb2021-09-16 21:36:16 +020070
71 memcpy(inbuf, vec->rand, sizeof(vec->rand));
72 memcpy(inbuf + sizeof(vec->rand), vec->autn, sizeof(vec->autn));
73 print_base64("IMS nonce:\t%s\n", inbuf, sizeof(inbuf));
74 print_base64("IMS res:\t%s\n", vec->res, vec->res_len);
Harald Welte915e0ef2011-12-07 02:38:42 +010075 }
76
77 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
Harald Welte4f511b62016-05-18 19:36:42 +020078 printf("SRES:\t%s\n", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
79 printf("Kc:\t%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
Harald Welte915e0ef2011-12-07 02:38:42 +010080 }
81}
82
Harald Welte0ea9b912023-05-30 12:05:56 +020083static struct osmo_sub_auth_data2 test_aud = {
Harald Welte915e0ef2011-12-07 02:38:42 +010084 .type = OSMO_AUTH_TYPE_NONE,
85 .algo = OSMO_AUTH_ALG_NONE,
86};
87
Harald Weltee61d4592022-11-03 11:05:58 +010088static void help(void)
Harald Welte5fb795e2012-03-21 08:51:48 +010089{
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010090 int alg;
Harald Welte5fb795e2012-03-21 08:51:48 +010091 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
92 "-3 --3g\tUse 3G (UMTS) authentication\n"
93 "-a --algorithm\tSpecify name of the algorithm\n"
94 "-k --key\tSpecify Ki / K\n"
95 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +010096 "-O --op\tSpecify OP (only for 3G)\n"
Holger Hans Peter Freyther91ff17c2015-05-26 00:11:37 +080097 "-f --amf\tSpecify AMF (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +010098 "-s --sqn\tSpecify SQN (only for 3G)\n"
Neels Hofmeyr3cb08272017-08-26 21:45:33 +020099 "-i --ind\tSpecify IND slot for new SQN after AUTS (only for 3G)\n"
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200100 "-l --ind-len\tSpecify IND bit length (default=5) (only for 3G)\n"
Harald Weltecebf3f02012-03-22 16:45:23 +0100101 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +0200102 "-r --rand\tSpecify random value\n"
103 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100104
105 fprintf(stderr, "\nAvailable algorithms for option -a:\n");
106 for (alg = 1; alg < _OSMO_AUTH_ALG_NUM; alg++)
107 fprintf(stderr, " %s\n",
108 osmo_auth_alg_name(alg));
Harald Welte5fb795e2012-03-21 08:51:48 +0100109}
110
Harald Welte915e0ef2011-12-07 02:38:42 +0100111int main(int argc, char **argv)
112{
113 struct osmo_auth_vector _vec;
114 struct osmo_auth_vector *vec = &_vec;
Neels Hofmeyr8352d312017-02-02 20:05:14 +0100115 uint8_t _rand[16], _auts[14];
Pau Espin Pedrolc30a76b2017-11-16 16:37:55 +0100116 uint64_t sqn = 0;
Neels Hofmeyrb2e41cc2017-09-12 03:25:43 +0200117 unsigned int ind = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +0100118 int rc, option_index;
119 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +0100120 int auts_is_set = 0;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200121 int sqn_is_set = 0;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200122 int ind_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +0200123 int fmt_triplets_dat = 0;
Neels Hofmeyrb2e41cc2017-09-12 03:25:43 +0200124 uint64_t ind_mask = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +0100125
Harald Weltebc6f56c2012-03-21 17:37:53 +0100126 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100127 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
128
Harald Weltecebf3f02012-03-22 16:45:23 +0100129 memset(_auts, 0, sizeof(_auts));
130
Harald Welte915e0ef2011-12-07 02:38:42 +0100131 while (1) {
132 int c;
Harald Welte915e0ef2011-12-07 02:38:42 +0100133 static struct option long_options[] = {
134 { "2g", 0, 0, '2' },
135 { "3g", 0, 0, '3' },
136 { "algorithm", 1, 0, 'a' },
137 { "key", 1, 0, 'k' },
138 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100139 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100140 { "amf", 1, 0, 'f' },
141 { "sqn", 1, 0, 's' },
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200142 { "ind", 1, 0, 'i' },
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200143 { "ind-len", 1, 0, 'l' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100144 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100145 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100146 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100147 { 0, 0, 0, 0 }
148 };
149
150 rc = 0;
151
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200152 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 +0100153 &option_index);
154
155 if (c == -1)
156 break;
157
158 switch (c) {
159 case '2':
160 test_aud.type = OSMO_AUTH_TYPE_GSM;
161 break;
162 case '3':
163 test_aud.type = OSMO_AUTH_TYPE_UMTS;
Neels Hofmeyr4315e012017-08-26 21:40:11 +0200164 test_aud.u.umts.ind_bitlen = 5;
Harald Welte915e0ef2011-12-07 02:38:42 +0100165 break;
166 case 'a':
167 rc = osmo_auth_alg_parse(optarg);
168 if (rc < 0)
169 break;
170 test_aud.algo = rc;
171 break;
172 case 'k':
173 switch (test_aud.type) {
174 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100175 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
176 sizeof(test_aud.u.gsm.ki));
Harald Welte0ea9b912023-05-30 12:05:56 +0200177 if (rc != sizeof(test_aud.u.gsm.ki)) {
178 fprintf(stderr, "Invalid Ki length %d\n", rc);
179 exit(2);
180 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100181 break;
182 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100183 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
184 sizeof(test_aud.u.umts.k));
Harald Welte0ea9b912023-05-30 12:05:56 +0200185 /* 3GPP TS 33.102 6.3.7: "The authentication key (K) shall have a length of
186 * 128 bits or 256 bits." */
187 if (rc != 16 && rc != 32) {
188 fprintf(stderr, "Invalid K length %d\n", rc);
189 exit(2);
190 }
191 test_aud.u.umts.k_len = rc;
Harald Welte915e0ef2011-12-07 02:38:42 +0100192 break;
193 default:
194 fprintf(stderr, "please specify 2g/3g first!\n");
195 }
196 break;
197 case 'o':
198 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
199 fprintf(stderr, "Only UMTS has OPC\n");
200 exit(2);
201 }
Harald Welteaae23622011-12-07 11:35:02 +0100202 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
203 sizeof(test_aud.u.umts.opc));
Harald Welte0ea9b912023-05-30 12:05:56 +0200204 if (rc != 16 && rc != 32) {
205 fprintf(stderr, "Invalid OPC length %d\n", rc);
206 exit(2);
207 }
208 test_aud.u.umts.opc_len = rc;
Harald Weltea72e47b2012-03-21 09:03:16 +0100209 test_aud.u.umts.opc_is_op = 0;
210 break;
211 case 'O':
212 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
213 fprintf(stderr, "Only UMTS has OP\n");
214 exit(2);
215 }
216 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
217 sizeof(test_aud.u.umts.opc));
Harald Welte0ea9b912023-05-30 12:05:56 +0200218 if (rc != 16 && rc != 32) {
219 fprintf(stderr, "Invalid OP length %d\n", rc);
220 exit(2);
221 }
222 test_aud.u.umts.opc_len = rc;
Harald Weltea72e47b2012-03-21 09:03:16 +0100223 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100224 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100225 case 'A':
226 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
227 fprintf(stderr, "Only UMTS has AUTS\n");
228 exit(2);
229 }
230 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
231 auts_is_set = 1;
232 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100233 case 'f':
234 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
235 fprintf(stderr, "Only UMTS has AMF\n");
236 exit(2);
237 }
Harald Welteaae23622011-12-07 11:35:02 +0100238 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
239 sizeof(test_aud.u.umts.amf));
Harald Welte0ea9b912023-05-30 12:05:56 +0200240 if (rc != 2) {
241 fprintf(stderr, "Invalid AMF length %d\n", rc);
242 exit(2);
243 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100244 break;
245 case 's':
246 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
247 fprintf(stderr, "Only UMTS has SQN\n");
248 exit(2);
249 }
Harald Welteb48f4382021-09-17 07:56:09 +0200250 sqn = strtoull(optarg, 0, 0);
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200251 sqn_is_set = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100252 break;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200253 case 'i':
254 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
255 fprintf(stderr, "Only UMTS has IND\n");
256 exit(2);
257 }
258 ind = atoi(optarg);
259 ind_is_set = 1;
260 break;
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200261 case 'l':
262 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
263 fprintf(stderr, "Only UMTS has IND bitlen\n");
264 exit(2);
265 }
266 test_aud.u.umts.ind_bitlen = atoi(optarg);
267 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100268 case 'r':
269 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
Harald Welte0ea9b912023-05-30 12:05:56 +0200270 if (rc != sizeof(_rand)) {
271 fprintf(stderr, "Invalid RAND length %d\n", rc);
272 exit(2);
273 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100274 rand_is_set = 1;
275 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200276 case 'I':
277 fmt_triplets_dat = 1;
278 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100279 case 'h':
280 help();
281 exit(0);
282 default:
283 help();
284 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100285 }
286
287 if (rc < 0) {
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100288 help();
289 fprintf(stderr, "\nError parsing argument of option `%c'\n", c);
Harald Welte915e0ef2011-12-07 02:38:42 +0100290 exit(2);
291 }
292 }
293
Harald Welte278a6c82019-12-03 21:35:12 +0100294 if (argc > optind) {
295 fprintf(stderr, "Unsupported positional arguments in command line\n");
296 exit(2);
297 }
298
Harald Welte915e0ef2011-12-07 02:38:42 +0100299 if (!rand_is_set) {
Max4b2b0cc2017-07-10 14:32:48 +0200300 rc = osmo_get_rand_id(_rand, 16);
301 if (rc < 0) {
302 fprintf(stderr, "\nError: unable to obtain secure random numbers: %s!\n",
303 strerror(-rc));
304 exit(3);
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200305 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100306 }
307
Harald Welte5fb795e2012-03-21 08:51:48 +0100308 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
309 test_aud.algo == OSMO_AUTH_ALG_NONE) {
310 help();
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100311 fprintf(stderr, "\nError: you need to pass at least"
312 " -2 or -3, as well as an algorithm to use.\n");
Harald Welte5fb795e2012-03-21 08:51:48 +0100313 exit(2);
314 }
315
Harald Welte915e0ef2011-12-07 02:38:42 +0100316 memset(vec, 0, sizeof(*vec));
317
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200318 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
Neels Hofmeyrb2e41cc2017-09-12 03:25:43 +0200319 uint64_t seq_1 = 1LL << test_aud.u.umts.ind_bitlen;
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200320 ind_mask = seq_1 - 1;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200321
322 if (sqn_is_set) {
323 /* Before calculating the UMTS auth vector, osmo_auth_gen_vec() increments SEQ.
324 * To end up with the SQN passed in by the user, we need to pass in SEQ-1, and
325 * indicate which IND slot to target. */
326 test_aud.u.umts.sqn = sqn - seq_1;
327 test_aud.u.umts.ind = sqn & ind_mask;
328 }
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200329
330 if (sqn_is_set && ind_is_set) {
331 fprintf(stderr, "Requesting --sqn %"PRIu64" implies IND=%u,"
332 " so no further --ind argument is allowed.\n",
333 sqn, test_aud.u.umts.ind);
334 exit(2);
335 }
336
337 if (ind_is_set) {
338 if (ind >= (1 << test_aud.u.umts.ind_bitlen)) {
339 fprintf(stderr, "Requested --ind %u is too large for IND bitlen of %u\n",
340 ind, test_aud.u.umts.ind_bitlen);
341 exit(2);
342 }
343 test_aud.u.umts.ind = ind;
344 }
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200345 }
346
Harald Weltecebf3f02012-03-22 16:45:23 +0100347 if (!auts_is_set)
Harald Welte0ea9b912023-05-30 12:05:56 +0200348 rc = osmo_auth_gen_vec2(vec, &test_aud, _rand);
Harald Weltecebf3f02012-03-22 16:45:23 +0100349 else
Harald Welte0ea9b912023-05-30 12:05:56 +0200350 rc = osmo_auth_gen_vec_auts2(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100351 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100352 if (!auts_is_set)
353 fprintf(stderr, "error generating auth vector\n");
354 else
355 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100356 exit(1);
357 }
358
Harald Welte57799ed2012-06-27 15:06:19 +0200359 if (fmt_triplets_dat)
360 dump_triplets_dat(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100361 else {
Harald Welte57799ed2012-06-27 15:06:19 +0200362 dump_auth_vec(vec);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200363 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100364 printf("SQN:\t%" PRIu64 "\n", test_aud.u.umts.sqn);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200365 printf("IND:\t%u\n", (unsigned int)(test_aud.u.umts.sqn & ind_mask));
Neels Hofmeyr2066a422017-08-26 22:43:50 +0200366 if (auts_is_set)
367 printf("SQN.MS:\t%" PRIu64 "\n", test_aud.u.umts.sqn_ms);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200368 }
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100369 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100370
Harald Weltecebf3f02012-03-22 16:45:23 +0100371 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100372}