Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file osmo-auc-gen.c |
| 2 | * GSM/GPRS/3G authentication testing tool. */ |
| 3 | /* |
| 4 | * (C) 2010-2012 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 5 | * |
| 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 Engelhardt | a6d8393 | 2013-06-03 01:38:57 +0200 | [diff] [blame] | 29 | #include <time.h> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 30 | #include <getopt.h> |
Harald Welte | b53717f | 2012-08-02 08:42:59 +0200 | [diff] [blame] | 31 | #include <unistd.h> |
Holger Hans Peter Freyther | a652abc | 2013-07-14 09:11:47 +0200 | [diff] [blame] | 32 | #include <inttypes.h> |
| 33 | #include <time.h> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 34 | |
| 35 | #include <osmocom/crypt/auth.h> |
| 36 | #include <osmocom/core/utils.h> |
| 37 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 38 | static 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 Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 50 | static void dump_auth_vec(struct osmo_auth_vector *vec) |
| 51 | { |
Harald Welte | 4f511b6 | 2016-05-18 19:36:42 +0200 | [diff] [blame] | 52 | printf("RAND:\t%s\n", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand))); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 53 | |
| 54 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
Harald Welte | 4f511b6 | 2016-05-18 19:36:42 +0200 | [diff] [blame] | 55 | 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 Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (vec->auth_types & OSMO_AUTH_TYPE_GSM) { |
Harald Welte | 4f511b6 | 2016-05-18 19:36:42 +0200 | [diff] [blame] | 62 | 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 Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | static struct osmo_sub_auth_data test_aud = { |
| 68 | .type = OSMO_AUTH_TYPE_NONE, |
| 69 | .algo = OSMO_AUTH_ALG_NONE, |
| 70 | }; |
| 71 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 72 | static void help() |
| 73 | { |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 74 | int alg; |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 75 | 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 Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 80 | "-O --op\tSpecify OP (only for 3G)\n" |
Holger Hans Peter Freyther | 91ff17c | 2015-05-26 00:11:37 +0800 | [diff] [blame] | 81 | "-f --amf\tSpecify AMF (only for 3G)\n" |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 82 | "-s --sqn\tSpecify SQN (only for 3G)\n" |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 83 | "-A --auts\tSpecify AUTS (only for 3G)\n" |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 84 | "-r --rand\tSpecify random value\n" |
| 85 | "-I --ipsec\tOutput in triplets.dat format for strongswan\n"); |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 86 | |
| 87 | fprintf(stderr, "\nAvailable algorithms for option -a:\n"); |
| 88 | for (alg = 1; alg < _OSMO_AUTH_ALG_NUM; alg++) |
| 89 | fprintf(stderr, " %s\n", |
| 90 | osmo_auth_alg_name(alg)); |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 93 | int main(int argc, char **argv) |
| 94 | { |
| 95 | struct osmo_auth_vector _vec; |
| 96 | struct osmo_auth_vector *vec = &_vec; |
Neels Hofmeyr | 8352d31 | 2017-02-02 20:05:14 +0100 | [diff] [blame] | 97 | uint8_t _rand[16], _auts[14]; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 98 | int rc, option_index; |
| 99 | int rand_is_set = 0; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 100 | int auts_is_set = 0; |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 101 | int fmt_triplets_dat = 0; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 102 | |
Harald Welte | bc6f56c | 2012-03-21 17:37:53 +0100 | [diff] [blame] | 103 | printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n"); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 104 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 105 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 106 | memset(_auts, 0, sizeof(_auts)); |
| 107 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 108 | while (1) { |
| 109 | int c; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 110 | static struct option long_options[] = { |
| 111 | { "2g", 0, 0, '2' }, |
| 112 | { "3g", 0, 0, '3' }, |
| 113 | { "algorithm", 1, 0, 'a' }, |
| 114 | { "key", 1, 0, 'k' }, |
| 115 | { "opc", 1, 0, 'o' }, |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 116 | { "op", 1, 0, 'O' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 117 | { "amf", 1, 0, 'f' }, |
| 118 | { "sqn", 1, 0, 's' }, |
| 119 | { "rand", 1, 0, 'r' }, |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 120 | { "auts", 1, 0, 'A' }, |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 121 | { "help", 0, 0, 'h' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 122 | { 0, 0, 0, 0 } |
| 123 | }; |
| 124 | |
| 125 | rc = 0; |
| 126 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 127 | c = getopt_long(argc, argv, "23a:k:o:f:s:r:hO:A:I", long_options, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 128 | &option_index); |
| 129 | |
| 130 | if (c == -1) |
| 131 | break; |
| 132 | |
| 133 | switch (c) { |
| 134 | case '2': |
| 135 | test_aud.type = OSMO_AUTH_TYPE_GSM; |
| 136 | break; |
| 137 | case '3': |
| 138 | test_aud.type = OSMO_AUTH_TYPE_UMTS; |
| 139 | break; |
| 140 | case 'a': |
| 141 | rc = osmo_auth_alg_parse(optarg); |
| 142 | if (rc < 0) |
| 143 | break; |
| 144 | test_aud.algo = rc; |
| 145 | break; |
| 146 | case 'k': |
| 147 | switch (test_aud.type) { |
| 148 | case OSMO_AUTH_TYPE_GSM: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 149 | rc = osmo_hexparse(optarg, test_aud.u.gsm.ki, |
| 150 | sizeof(test_aud.u.gsm.ki)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 151 | break; |
| 152 | case OSMO_AUTH_TYPE_UMTS: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 153 | rc = osmo_hexparse(optarg, test_aud.u.umts.k, |
| 154 | sizeof(test_aud.u.umts.k)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 155 | break; |
| 156 | default: |
| 157 | fprintf(stderr, "please specify 2g/3g first!\n"); |
| 158 | } |
| 159 | break; |
| 160 | case 'o': |
| 161 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 162 | fprintf(stderr, "Only UMTS has OPC\n"); |
| 163 | exit(2); |
| 164 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 165 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 166 | sizeof(test_aud.u.umts.opc)); |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 167 | test_aud.u.umts.opc_is_op = 0; |
| 168 | break; |
| 169 | case 'O': |
| 170 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 171 | fprintf(stderr, "Only UMTS has OP\n"); |
| 172 | exit(2); |
| 173 | } |
| 174 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 175 | sizeof(test_aud.u.umts.opc)); |
| 176 | test_aud.u.umts.opc_is_op = 1; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 177 | break; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 178 | case 'A': |
| 179 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 180 | fprintf(stderr, "Only UMTS has AUTS\n"); |
| 181 | exit(2); |
| 182 | } |
| 183 | rc = osmo_hexparse(optarg, _auts, sizeof(_auts)); |
| 184 | auts_is_set = 1; |
| 185 | break; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 186 | case 'f': |
| 187 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 188 | fprintf(stderr, "Only UMTS has AMF\n"); |
| 189 | exit(2); |
| 190 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 191 | rc = osmo_hexparse(optarg, test_aud.u.umts.amf, |
| 192 | sizeof(test_aud.u.umts.amf)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 193 | break; |
| 194 | case 's': |
| 195 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 196 | fprintf(stderr, "Only UMTS has SQN\n"); |
| 197 | exit(2); |
| 198 | } |
Neels Hofmeyr | 1447799 | 2017-03-16 00:50:06 +0100 | [diff] [blame] | 199 | test_aud.u.umts.sqn = strtoull(optarg, 0, 10); |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 200 | /* Before calculating the UMTS auth vector, |
| 201 | * osmo_auth_gen_vec() increments the SQN. SQN-1 here |
| 202 | * to end up with the SQN the user requested. */ |
| 203 | test_aud.u.umts.sqn--; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 204 | break; |
| 205 | case 'r': |
| 206 | rc = osmo_hexparse(optarg, _rand, sizeof(_rand)); |
| 207 | rand_is_set = 1; |
| 208 | break; |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 209 | case 'I': |
| 210 | fmt_triplets_dat = 1; |
| 211 | break; |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 212 | case 'h': |
| 213 | help(); |
| 214 | exit(0); |
| 215 | default: |
| 216 | help(); |
| 217 | exit(1); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (rc < 0) { |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 221 | help(); |
| 222 | fprintf(stderr, "\nError parsing argument of option `%c'\n", c); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 223 | exit(2); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (!rand_is_set) { |
Holger Hans Peter Freyther | 17aa6b2 | 2014-06-22 16:53:55 +0200 | [diff] [blame] | 228 | int i; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 229 | printf("WARNING: We're using really weak random numbers!\n\n"); |
| 230 | srand(time(NULL)); |
Holger Hans Peter Freyther | 17aa6b2 | 2014-06-22 16:53:55 +0200 | [diff] [blame] | 231 | |
| 232 | for (i = 0; i < 4; ++i) { |
| 233 | uint32_t r; |
| 234 | r = rand(); |
| 235 | memcpy(&_rand[i*4], &r, 4); |
| 236 | } |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 239 | if (test_aud.type == OSMO_AUTH_TYPE_NONE || |
| 240 | test_aud.algo == OSMO_AUTH_ALG_NONE) { |
| 241 | help(); |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 242 | fprintf(stderr, "\nError: you need to pass at least" |
| 243 | " -2 or -3, as well as an algorithm to use.\n"); |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 244 | exit(2); |
| 245 | } |
| 246 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 247 | memset(vec, 0, sizeof(*vec)); |
| 248 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 249 | if (!auts_is_set) |
| 250 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 251 | else |
| 252 | rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 253 | if (rc < 0) { |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 254 | if (!auts_is_set) |
| 255 | fprintf(stderr, "error generating auth vector\n"); |
| 256 | else |
| 257 | fprintf(stderr, "AUTS from MS seems incorrect\n"); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 258 | exit(1); |
| 259 | } |
| 260 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 261 | if (fmt_triplets_dat) |
| 262 | dump_triplets_dat(vec); |
Neels Hofmeyr | 5fe3d1b | 2017-03-15 01:16:43 +0100 | [diff] [blame] | 263 | else { |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 264 | dump_auth_vec(vec); |
Neels Hofmeyr | 5fe3d1b | 2017-03-15 01:16:43 +0100 | [diff] [blame] | 265 | if (test_aud.type == OSMO_AUTH_TYPE_UMTS) |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 266 | printf("SQN:\t%" PRIu64 "\n", test_aud.u.umts.sqn); |
Neels Hofmeyr | 5fe3d1b | 2017-03-15 01:16:43 +0100 | [diff] [blame] | 267 | } |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 268 | |
Neels Hofmeyr | 5fe3d1b | 2017-03-15 01:16:43 +0100 | [diff] [blame] | 269 | /* After recovering SQN.MS from AUTS, milenage_gen_vec_auts() does |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 270 | * aud->u.umts.sqn++, so to show SQN.MS we need to -1 */ |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 271 | if (auts_is_set) |
Neels Hofmeyr | 5fe3d1b | 2017-03-15 01:16:43 +0100 | [diff] [blame] | 272 | printf("AUTS success: SQN.MS = %" PRIu64 "\n", |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 273 | test_aud.u.umts.sqn - 1); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 274 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 275 | exit(0); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 276 | } |