blob: a133889a8e98372b4ec6a0e35af4fd72992dcc62 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2015-2016 by sysmocom - s.f.m.c. GmbH
3 * Author: Neels Hofmeyr
Harald Weltec0f00072016-04-27 18:32:35 +02004 * All Rights Reserved
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
Harald Weltec0f00072016-04-27 18:32:35 +02007 *
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
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <osmocom/core/utils.h>
24#include <osmocom/core/logging.h>
25#include <osmocom/core/msgb.h>
26#include <osmocom/gsm/tlv.h>
27#include <osmocom/gsm/oap.h>
28
29#include <stdint.h>
30
Harald Welte96e2a002017-06-12 21:44:18 +020031/*! \addtogroup oap
32 * @{
Harald Weltea3389832017-10-16 18:34:14 +020033 * \file oap.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034 * Osmocom Authentication Protocol
Harald Welte96e2a002017-06-12 21:44:18 +020035 */
36
Neels Hofmeyr87e45502017-06-20 00:17:59 +020037/*! Decode OAP message data.
Harald Weltec0f00072016-04-27 18:32:35 +020038 * \param[out] oap_msg Parsed data is written to this instance.
39 * \param[in] data Pointer to the data buffer containing the OAP message.
40 * \param[in] data_len Length of the OAP message data.
41 * \returns 0 on success, a negative cause value on failure.
42 */
43int osmo_oap_decode(struct osmo_oap_message *oap_msg,
44 const uint8_t *const_data, size_t data_len)
45{
46 int rc;
47 uint8_t tag;
48 /* the shift/match functions expect non-const pointers, but we'll
49 * either copy the data or cast pointers back to const before returning
50 * them
51 */
52 uint8_t *data = (uint8_t *)const_data;
53 uint8_t *value;
54 size_t value_len;
55
56 memset(oap_msg, 0, sizeof(*oap_msg));
57
58 /* message type */
59 rc = osmo_shift_v_fixed(&data, &data_len, 1, &value);
60 if (rc < 0)
61 return -GMM_CAUSE_INV_MAND_INFO;
62 oap_msg->message_type = osmo_decode_big_endian(value, 1);
63
64 /* specific parts */
65 while (data_len > 0) {
66 enum osmo_oap_iei iei;
67
68 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
69 if (rc < 0)
70 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
71
72 iei = tag;
73
74 switch (iei) {
75 case OAP_CLIENT_ID_IE:
76 if (value_len != 2) {
77 LOGP(DLOAP, LOGL_NOTICE,
78 "OAP IE type client ID (%d) should be 2 octets, but has %d\n",
79 (int)iei, (int)value_len);
80 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
81 }
82
83 oap_msg->client_id = osmo_decode_big_endian(value, value_len);
84
85 if (oap_msg->client_id == 0) {
86 LOGP(DLOAP, LOGL_NOTICE,
87 "OAP IE type client ID (%d): client ID must be nonzero.\n",
88 (int)iei);
89 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
90 }
91 break;
92
93 case OAP_AUTN_IE:
94 if (value_len != sizeof(oap_msg->autn)) {
95 LOGP(DLOAP, LOGL_NOTICE,
96 "OAP IE type AUTN (%d) should be %d octets, but has %d\n",
97 (int)iei, (int)sizeof(oap_msg->autn), (int)value_len);
98 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
99 }
100 memcpy(oap_msg->autn, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100101 oap_msg->autn_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200102 break;
103
104 case OAP_RAND_IE:
105 if (value_len != sizeof(oap_msg->rand)) {
106 LOGP(DLOAP, LOGL_NOTICE,
107 "OAP IE type RAND (%d) should be %d octets, but has %d\n",
108 (int)iei, (int)sizeof(oap_msg->rand), (int)value_len);
109 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
110 }
111 memcpy(oap_msg->rand, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100112 oap_msg->rand_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200113 break;
114
115 case OAP_XRES_IE:
116 if (value_len != sizeof(oap_msg->xres)) {
117 LOGP(DLOAP, LOGL_NOTICE,
118 "OAP IE type XRES (%d) should be %d octets, but has %d\n",
119 (int)iei, (int)sizeof(oap_msg->xres), (int)value_len);
120 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
121 }
122 memcpy(oap_msg->xres, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100123 oap_msg->xres_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200124 break;
125
126 case OAP_AUTS_IE:
127 if (value_len != sizeof(oap_msg->auts)) {
128 LOGP(DLOAP, LOGL_NOTICE,
129 "OAP IE type AUTS (%d) should be %d octets, but has %d\n",
130 (int)iei, (int)sizeof(oap_msg->auts), (int)value_len);
131 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
132 }
133 memcpy(oap_msg->auts, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100134 oap_msg->auts_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200135 break;
136
137 case OAP_CAUSE_IE:
138 if (value_len > 1) {
139 LOGP(DLOAP, LOGL_ERROR,
140 "OAP cause may not exceed one octet, is %d", (int)value_len);
141 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
142 }
143 oap_msg->cause = *value;
144 break;
145
146 default:
147 LOGP(DLOAP, LOGL_NOTICE,
148 "OAP IE type %d unknown\n", iei);
149 continue;
150 }
151 }
152
153 return 0;
154}
155
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200156/*! Compose OAP message data.
Harald Weltec0f00072016-04-27 18:32:35 +0200157 * \param[out] msg OAP message data is appended to this message buffer.
158 * \param[in] oap_msg Elements to encode in the message data.
159 */
160void osmo_oap_encode(struct msgb *msg, const struct osmo_oap_message *oap_msg)
161{
162 uint8_t u8;
163
164 /* generic part */
165 OSMO_ASSERT(oap_msg->message_type);
166 msgb_v_put(msg, (uint8_t)oap_msg->message_type);
167
168 /* specific parts */
169 if ((u8 = oap_msg->cause))
170 msgb_tlv_put(msg, OAP_CAUSE_IE, sizeof(u8), &u8);
171
172 if (oap_msg->client_id > 0)
173 msgb_tlv_put(msg, OAP_CLIENT_ID_IE, sizeof(oap_msg->client_id),
174 osmo_encode_big_endian(oap_msg->client_id,
175 sizeof(oap_msg->client_id)));
176
177 if (oap_msg->rand_present)
178 msgb_tlv_put(msg, OAP_RAND_IE, sizeof(oap_msg->rand), oap_msg->rand);
179
180 if (oap_msg->autn_present)
181 msgb_tlv_put(msg, OAP_AUTN_IE, sizeof(oap_msg->autn), oap_msg->autn);
182
183 if (oap_msg->auts_present)
184 msgb_tlv_put(msg, OAP_AUTS_IE, sizeof(oap_msg->auts), oap_msg->auts);
185
186 if (oap_msg->xres_present)
187 msgb_tlv_put(msg, OAP_XRES_IE, sizeof(oap_msg->xres), oap_msg->xres);
188
189 msg->l2h = msg->data;
190}
Harald Welte96e2a002017-06-12 21:44:18 +0200191
192/*! @} */