Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 1 | /* GSM/GPRS/3G authentication testing tool */ |
| 2 | |
| 3 | /* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 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> |
| 28 | #include <getopt.h> |
| 29 | |
| 30 | #include <osmocom/crypt/auth.h> |
| 31 | #include <osmocom/core/utils.h> |
| 32 | |
| 33 | static void dump_auth_vec(struct osmo_auth_vector *vec) |
| 34 | { |
| 35 | printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand))); |
| 36 | |
| 37 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 38 | printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn))); |
| 39 | printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik))); |
| 40 | printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck))); |
| 41 | printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len)); |
| 42 | } |
| 43 | |
| 44 | if (vec->auth_types & OSMO_AUTH_TYPE_GSM) { |
| 45 | printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres))); |
| 46 | printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc))); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static struct osmo_sub_auth_data test_aud = { |
| 51 | .type = OSMO_AUTH_TYPE_NONE, |
| 52 | .algo = OSMO_AUTH_ALG_NONE, |
| 53 | }; |
| 54 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame^] | 55 | static void help() |
| 56 | { |
| 57 | printf( "-2 --2g\tUse 2G (GSM) authentication\n" |
| 58 | "-3 --3g\tUse 3G (UMTS) authentication\n" |
| 59 | "-a --algorithm\tSpecify name of the algorithm\n" |
| 60 | "-k --key\tSpecify Ki / K\n" |
| 61 | "-o --opc\tSpecify OPC (only for 3G)\n" |
| 62 | "-a --amf\tSpecify AMF (only for 3G)\n" |
| 63 | "-s --sqn\tSpecify SQN (only for 3G)\n" |
| 64 | "-r --rand\tSpecify random value\n"); |
| 65 | } |
| 66 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 67 | int main(int argc, char **argv) |
| 68 | { |
| 69 | struct osmo_auth_vector _vec; |
| 70 | struct osmo_auth_vector *vec = &_vec; |
| 71 | uint8_t _rand[16]; |
| 72 | int rc, option_index; |
| 73 | int rand_is_set = 0; |
| 74 | |
| 75 | printf("osmo-auc-gen (C) 2011 by Harald Welte\n"); |
| 76 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 77 | |
| 78 | while (1) { |
| 79 | int c; |
| 80 | unsigned long ul; |
| 81 | static struct option long_options[] = { |
| 82 | { "2g", 0, 0, '2' }, |
| 83 | { "3g", 0, 0, '3' }, |
| 84 | { "algorithm", 1, 0, 'a' }, |
| 85 | { "key", 1, 0, 'k' }, |
| 86 | { "opc", 1, 0, 'o' }, |
| 87 | { "amf", 1, 0, 'f' }, |
| 88 | { "sqn", 1, 0, 's' }, |
| 89 | { "rand", 1, 0, 'r' }, |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame^] | 90 | { "help", 0, 0, 'h' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 91 | { 0, 0, 0, 0 } |
| 92 | }; |
| 93 | |
| 94 | rc = 0; |
| 95 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame^] | 96 | c = getopt_long(argc, argv, "23a:k:o:f:s:r:h", long_options, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 97 | &option_index); |
| 98 | |
| 99 | if (c == -1) |
| 100 | break; |
| 101 | |
| 102 | switch (c) { |
| 103 | case '2': |
| 104 | test_aud.type = OSMO_AUTH_TYPE_GSM; |
| 105 | break; |
| 106 | case '3': |
| 107 | test_aud.type = OSMO_AUTH_TYPE_UMTS; |
| 108 | break; |
| 109 | case 'a': |
| 110 | rc = osmo_auth_alg_parse(optarg); |
| 111 | if (rc < 0) |
| 112 | break; |
| 113 | test_aud.algo = rc; |
| 114 | break; |
| 115 | case 'k': |
| 116 | switch (test_aud.type) { |
| 117 | case OSMO_AUTH_TYPE_GSM: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 118 | rc = osmo_hexparse(optarg, test_aud.u.gsm.ki, |
| 119 | sizeof(test_aud.u.gsm.ki)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 120 | break; |
| 121 | case OSMO_AUTH_TYPE_UMTS: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 122 | rc = osmo_hexparse(optarg, test_aud.u.umts.k, |
| 123 | sizeof(test_aud.u.umts.k)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 124 | break; |
| 125 | default: |
| 126 | fprintf(stderr, "please specify 2g/3g first!\n"); |
| 127 | } |
| 128 | break; |
| 129 | case 'o': |
| 130 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 131 | fprintf(stderr, "Only UMTS has OPC\n"); |
| 132 | exit(2); |
| 133 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 134 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 135 | sizeof(test_aud.u.umts.opc)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 136 | break; |
| 137 | case 'f': |
| 138 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 139 | fprintf(stderr, "Only UMTS has AMF\n"); |
| 140 | exit(2); |
| 141 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 142 | rc = osmo_hexparse(optarg, test_aud.u.umts.amf, |
| 143 | sizeof(test_aud.u.umts.amf)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 144 | break; |
| 145 | case 's': |
| 146 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 147 | fprintf(stderr, "Only UMTS has SQN\n"); |
| 148 | exit(2); |
| 149 | } |
| 150 | ul = strtoul(optarg, 0, 10); |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 151 | test_aud.u.umts.sqn = ul; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 152 | break; |
| 153 | case 'r': |
| 154 | rc = osmo_hexparse(optarg, _rand, sizeof(_rand)); |
| 155 | rand_is_set = 1; |
| 156 | break; |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame^] | 157 | case 'h': |
| 158 | help(); |
| 159 | exit(0); |
| 160 | default: |
| 161 | help(); |
| 162 | exit(1); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (rc < 0) { |
| 166 | fprintf(stderr, "Error parsing argument of option `%c'\n", c); |
| 167 | exit(2); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (!rand_is_set) { |
| 172 | printf("WARNING: We're using really weak random numbers!\n\n"); |
| 173 | srand(time(NULL)); |
| 174 | *(uint32_t *)&_rand[0] = rand(); |
| 175 | *(uint32_t *)(&_rand[4]) = rand(); |
| 176 | *(uint32_t *)(&_rand[8]) = rand(); |
| 177 | *(uint32_t *)(&_rand[12]) = rand(); |
| 178 | } |
| 179 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame^] | 180 | if (test_aud.type == OSMO_AUTH_TYPE_NONE || |
| 181 | test_aud.algo == OSMO_AUTH_ALG_NONE) { |
| 182 | help(); |
| 183 | exit(2); |
| 184 | } |
| 185 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 186 | memset(vec, 0, sizeof(*vec)); |
| 187 | |
| 188 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 189 | if (rc < 0) { |
| 190 | fprintf(stderr, "error generating auth vector\n"); |
| 191 | exit(1); |
| 192 | } |
| 193 | |
| 194 | dump_auth_vec(vec); |
| 195 | #if 0 |
| 196 | const uint8_t auts[14] = { 0x87, 0x11, 0xa0, 0xec, 0x9e, 0x16, 0x37, 0xdf, |
| 197 | 0x17, 0xf8, 0x0b, 0x38, 0x4e, 0xe4 }; |
| 198 | |
| 199 | rc = osmo_auth_gen_vec_auts(vec, &test_aud, auts, _rand, _rand); |
| 200 | if (rc < 0) { |
| 201 | printf("AUTS failed\n"); |
| 202 | } else { |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 203 | printf("AUTS success: SEQ.MS = %lu\n", test_aud.u.umts.sqn); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 204 | } |
| 205 | #endif |
| 206 | exit(0); |
| 207 | |
| 208 | } |