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