Neels Hofmeyr | d981efa | 2016-12-08 17:50:03 +0100 | [diff] [blame] | 1 | /* Test Osmocom Authentication Protocol */ |
| 2 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | * (C) 2016 by sysmocom - s.f.m.c. GmbH |
| 4 | * Author: Neels Hofmeyr |
Neels Hofmeyr | d981efa | 2016-12-08 17:50:03 +0100 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Neels Hofmeyr | d981efa | 2016-12-08 17:50:03 +0100 | [diff] [blame] | 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 | |
Max | 2f0b0c9 | 2017-01-12 16:47:13 +0100 | [diff] [blame] | 24 | #include <osmocom/core/application.h> |
Neels Hofmeyr | d981efa | 2016-12-08 17:50:03 +0100 | [diff] [blame] | 25 | #include <osmocom/core/logging.h> |
| 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/gsm/oap.h> |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <stdbool.h> |
| 32 | |
| 33 | static struct msgb *oap_encoded(const struct osmo_oap_message *oap_msg) |
| 34 | { |
| 35 | struct msgb *msgb = msgb_alloc_headroom(1000, 64, __func__); |
| 36 | OSMO_ASSERT(msgb); |
| 37 | osmo_oap_encode(msgb, oap_msg); |
| 38 | return msgb; |
| 39 | } |
| 40 | |
| 41 | static bool encode_decode_makes_same_msg(struct osmo_oap_message *oap_msg) |
| 42 | { |
| 43 | struct osmo_oap_message oap_msg_decoded; |
| 44 | struct msgb *msgb; |
| 45 | int rc; |
| 46 | bool result; |
| 47 | memset(&oap_msg_decoded, 0, sizeof(oap_msg_decoded)); |
| 48 | |
| 49 | msgb = oap_encoded(oap_msg); |
| 50 | printf("encoded message:\n%s\n", |
| 51 | osmo_hexdump((void*)msgb_l2(msgb), msgb_l2len(msgb))); |
| 52 | rc = osmo_oap_decode(&oap_msg_decoded, msgb_l2(msgb), msgb_l2len(msgb)); |
| 53 | |
| 54 | if (rc) { |
| 55 | printf("osmo_oap_decode() returned error: %d\n", rc); |
| 56 | result = false; |
| 57 | goto free_msgb; |
| 58 | } |
| 59 | |
| 60 | rc = memcmp(oap_msg, &oap_msg_decoded, sizeof(oap_msg_decoded)); |
| 61 | if (rc) { |
| 62 | printf("decoded message mismatches encoded message\n"); |
| 63 | printf("original:\n%s\n", |
| 64 | osmo_hexdump((void*)oap_msg, sizeof(*oap_msg))); |
| 65 | printf("en- and decoded:\n%s\n", |
| 66 | osmo_hexdump((void*)&oap_msg_decoded, sizeof(oap_msg_decoded))); |
| 67 | result = false; |
| 68 | goto free_msgb; |
| 69 | } |
| 70 | |
| 71 | printf("ok\n"); |
| 72 | result = true; |
| 73 | |
| 74 | free_msgb: |
| 75 | talloc_free(msgb); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | static void test_oap_messages_dec_enc(void) |
| 80 | { |
| 81 | printf("Testing OAP messages\n"); |
| 82 | |
| 83 | struct osmo_oap_message oap_msg; |
| 84 | |
| 85 | #define CLEAR() memset(&oap_msg, 0, sizeof(oap_msg)) |
| 86 | #define CHECK() OSMO_ASSERT(encode_decode_makes_same_msg(&oap_msg)) |
| 87 | |
| 88 | printf("- Register Request\n"); |
| 89 | CLEAR(); |
| 90 | oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST; |
| 91 | oap_msg.client_id = 0x2342; |
| 92 | CHECK(); |
| 93 | |
| 94 | printf("- Register Error\n"); |
| 95 | CLEAR(); |
| 96 | oap_msg.message_type = OAP_MSGT_REGISTER_ERROR; |
| 97 | oap_msg.cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 98 | CHECK(); |
| 99 | |
| 100 | printf("- Register Result\n"); |
| 101 | CLEAR(); |
| 102 | oap_msg.message_type = OAP_MSGT_REGISTER_RESULT; |
| 103 | CHECK(); |
| 104 | |
| 105 | printf("- Challenge Request, no rand, no autn\n"); |
| 106 | CLEAR(); |
| 107 | oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST; |
| 108 | oap_msg.rand_present = 0; |
| 109 | oap_msg.autn_present = 0; |
| 110 | CHECK(); |
| 111 | |
| 112 | printf("- Challenge Request, with rand, no autn\n"); |
| 113 | CLEAR(); |
| 114 | oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST; |
| 115 | osmo_hexparse("0102030405060708090a0b0c0d0e0f10", |
| 116 | oap_msg.rand, 16); |
| 117 | oap_msg.rand_present = 1; |
| 118 | oap_msg.autn_present = 0; |
| 119 | CHECK(); |
| 120 | |
| 121 | printf("- Challenge Request, no rand, with autn\n"); |
| 122 | CLEAR(); |
| 123 | oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST; |
| 124 | oap_msg.rand_present = 0; |
| 125 | osmo_hexparse("cec4e3848a33000086781158ca40f136", |
| 126 | oap_msg.autn, 16); |
| 127 | oap_msg.autn_present = 1; |
| 128 | CHECK(); |
| 129 | |
| 130 | printf("- Challenge Request, with rand, with autn\n"); |
| 131 | CLEAR(); |
| 132 | oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST; |
| 133 | osmo_hexparse("0102030405060708090a0b0c0d0e0f10", |
| 134 | oap_msg.rand, 16); |
| 135 | oap_msg.rand_present = 1; |
| 136 | osmo_hexparse("cec4e3848a33000086781158ca40f136", |
| 137 | oap_msg.autn, 16); |
| 138 | oap_msg.autn_present = 1; |
| 139 | CHECK(); |
| 140 | |
| 141 | printf("- Challenge Error\n"); |
| 142 | CLEAR(); |
| 143 | oap_msg.message_type = OAP_MSGT_CHALLENGE_ERROR; |
| 144 | oap_msg.cause = GMM_CAUSE_GSM_AUTH_UNACCEPT; |
| 145 | CHECK(); |
| 146 | |
| 147 | printf("- Challenge Result\n"); |
| 148 | CLEAR(); |
| 149 | oap_msg.message_type = OAP_MSGT_CHALLENGE_RESULT; |
| 150 | osmo_hexparse("0102030405060708", |
| 151 | oap_msg.xres, 8); |
| 152 | oap_msg.xres_present = 1; |
| 153 | CHECK(); |
| 154 | |
| 155 | printf("- Sync Request\n"); |
| 156 | CLEAR(); |
| 157 | oap_msg.message_type = OAP_MSGT_SYNC_REQUEST; |
Neels Hofmeyr | 8352d31 | 2017-02-02 20:05:14 +0100 | [diff] [blame] | 158 | osmo_hexparse("102030405060708090a0b0c0d0e0", |
| 159 | oap_msg.auts, 14); |
Neels Hofmeyr | d981efa | 2016-12-08 17:50:03 +0100 | [diff] [blame] | 160 | oap_msg.auts_present = 1; |
| 161 | CHECK(); |
| 162 | |
| 163 | /* Sync Error and Sync Result are not used in OAP */ |
| 164 | } |
| 165 | |
| 166 | const struct log_info_cat default_categories[] = { |
| 167 | }; |
| 168 | |
| 169 | static struct log_info info = { |
| 170 | .cat = default_categories, |
| 171 | .num_cat = ARRAY_SIZE(default_categories), |
| 172 | }; |
| 173 | |
| 174 | int main(int argc, char **argv) |
| 175 | { |
| 176 | osmo_init_logging(&info); |
| 177 | |
| 178 | test_oap_messages_dec_enc(); |
| 179 | |
| 180 | printf("Done.\n"); |
| 181 | return EXIT_SUCCESS; |
| 182 | } |