blob: 1494a6a637d40173b67c281427c2f8ebde4b5c50 [file] [log] [blame]
Harald Weltec0f00072016-04-27 18:32:35 +02001/* Osmocom Authentication Protocol message encoder/decoder */
2
3/* (C) 2015-2016 by sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
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
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
31/*! \brief Decode OAP message data.
32 * \param[out] oap_msg Parsed data is written to this instance.
33 * \param[in] data Pointer to the data buffer containing the OAP message.
34 * \param[in] data_len Length of the OAP message data.
35 * \returns 0 on success, a negative cause value on failure.
36 */
37int osmo_oap_decode(struct osmo_oap_message *oap_msg,
38 const uint8_t *const_data, size_t data_len)
39{
40 int rc;
41 uint8_t tag;
42 /* the shift/match functions expect non-const pointers, but we'll
43 * either copy the data or cast pointers back to const before returning
44 * them
45 */
46 uint8_t *data = (uint8_t *)const_data;
47 uint8_t *value;
48 size_t value_len;
49
50 memset(oap_msg, 0, sizeof(*oap_msg));
51
52 /* message type */
53 rc = osmo_shift_v_fixed(&data, &data_len, 1, &value);
54 if (rc < 0)
55 return -GMM_CAUSE_INV_MAND_INFO;
56 oap_msg->message_type = osmo_decode_big_endian(value, 1);
57
58 /* specific parts */
59 while (data_len > 0) {
60 enum osmo_oap_iei iei;
61
62 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
63 if (rc < 0)
64 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
65
66 iei = tag;
67
68 switch (iei) {
69 case OAP_CLIENT_ID_IE:
70 if (value_len != 2) {
71 LOGP(DLOAP, LOGL_NOTICE,
72 "OAP IE type client ID (%d) should be 2 octets, but has %d\n",
73 (int)iei, (int)value_len);
74 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
75 }
76
77 oap_msg->client_id = osmo_decode_big_endian(value, value_len);
78
79 if (oap_msg->client_id == 0) {
80 LOGP(DLOAP, LOGL_NOTICE,
81 "OAP IE type client ID (%d): client ID must be nonzero.\n",
82 (int)iei);
83 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
84 }
85 break;
86
87 case OAP_AUTN_IE:
88 if (value_len != sizeof(oap_msg->autn)) {
89 LOGP(DLOAP, LOGL_NOTICE,
90 "OAP IE type AUTN (%d) should be %d octets, but has %d\n",
91 (int)iei, (int)sizeof(oap_msg->autn), (int)value_len);
92 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
93 }
94 memcpy(oap_msg->autn, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +010095 oap_msg->autn_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +020096 break;
97
98 case OAP_RAND_IE:
99 if (value_len != sizeof(oap_msg->rand)) {
100 LOGP(DLOAP, LOGL_NOTICE,
101 "OAP IE type RAND (%d) should be %d octets, but has %d\n",
102 (int)iei, (int)sizeof(oap_msg->rand), (int)value_len);
103 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
104 }
105 memcpy(oap_msg->rand, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100106 oap_msg->rand_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200107 break;
108
109 case OAP_XRES_IE:
110 if (value_len != sizeof(oap_msg->xres)) {
111 LOGP(DLOAP, LOGL_NOTICE,
112 "OAP IE type XRES (%d) should be %d octets, but has %d\n",
113 (int)iei, (int)sizeof(oap_msg->xres), (int)value_len);
114 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
115 }
116 memcpy(oap_msg->xres, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100117 oap_msg->xres_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200118 break;
119
120 case OAP_AUTS_IE:
121 if (value_len != sizeof(oap_msg->auts)) {
122 LOGP(DLOAP, LOGL_NOTICE,
123 "OAP IE type AUTS (%d) should be %d octets, but has %d\n",
124 (int)iei, (int)sizeof(oap_msg->auts), (int)value_len);
125 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
126 }
127 memcpy(oap_msg->auts, value, value_len);
Neels Hofmeyrdd979502016-12-08 17:48:24 +0100128 oap_msg->auts_present = value_len ? 1 : 0;
Harald Weltec0f00072016-04-27 18:32:35 +0200129 break;
130
131 case OAP_CAUSE_IE:
132 if (value_len > 1) {
133 LOGP(DLOAP, LOGL_ERROR,
134 "OAP cause may not exceed one octet, is %d", (int)value_len);
135 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
136 }
137 oap_msg->cause = *value;
138 break;
139
140 default:
141 LOGP(DLOAP, LOGL_NOTICE,
142 "OAP IE type %d unknown\n", iei);
143 continue;
144 }
145 }
146
147 return 0;
148}
149
150/*! \brief Compose OAP message data.
151 * \param[out] msg OAP message data is appended to this message buffer.
152 * \param[in] oap_msg Elements to encode in the message data.
153 */
154void osmo_oap_encode(struct msgb *msg, const struct osmo_oap_message *oap_msg)
155{
156 uint8_t u8;
157
158 /* generic part */
159 OSMO_ASSERT(oap_msg->message_type);
160 msgb_v_put(msg, (uint8_t)oap_msg->message_type);
161
162 /* specific parts */
163 if ((u8 = oap_msg->cause))
164 msgb_tlv_put(msg, OAP_CAUSE_IE, sizeof(u8), &u8);
165
166 if (oap_msg->client_id > 0)
167 msgb_tlv_put(msg, OAP_CLIENT_ID_IE, sizeof(oap_msg->client_id),
168 osmo_encode_big_endian(oap_msg->client_id,
169 sizeof(oap_msg->client_id)));
170
171 if (oap_msg->rand_present)
172 msgb_tlv_put(msg, OAP_RAND_IE, sizeof(oap_msg->rand), oap_msg->rand);
173
174 if (oap_msg->autn_present)
175 msgb_tlv_put(msg, OAP_AUTN_IE, sizeof(oap_msg->autn), oap_msg->autn);
176
177 if (oap_msg->auts_present)
178 msgb_tlv_put(msg, OAP_AUTS_IE, sizeof(oap_msg->auts), oap_msg->auts);
179
180 if (oap_msg->xres_present)
181 msgb_tlv_put(msg, OAP_XRES_IE, sizeof(oap_msg->xres), oap_msg->xres);
182
183 msg->l2h = msg->data;
184}