blob: eb520532ab250b49bf861959703a39253664c460 [file] [log] [blame]
Neels Hofmeyrd739f092015-10-12 11:57:34 +02001/* Osmocom Authentication Protocol message encoder/decoder */
2
3/* (C) 2015 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 Affero General Public License as published by
10 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <openbsc/oap_messages.h>
24
25#include <openbsc/debug.h>
26#include <openbsc/gprs_utils.h>
27#include <openbsc/utils.h>
28
29#include <osmocom/gsm/tlv.h>
30#include <osmocom/core/msgb.h>
31
32#include <stdint.h>
33
34
35int oap_decode(const uint8_t *const_data, size_t data_len,
36 struct oap_message *oap_msg)
37{
38 int rc;
39 uint8_t tag;
40 /* the shift/match functions expect non-const pointers, but we'll
41 * either copy the data or cast pointers back to const before returning
42 * them
43 */
44 uint8_t *data = (uint8_t *)const_data;
45 uint8_t *value;
46 size_t value_len;
47
48 memset(oap_msg, 0, sizeof(*oap_msg));
49
50 /* message type */
51 rc = gprs_shift_v_fixed(&data, &data_len, 1, &value);
52 if (rc < 0)
53 return -GMM_CAUSE_INV_MAND_INFO;
54 oap_msg->message_type = decode_big_endian(value, 1);
55
56 /* specific parts */
57 while (data_len > 0) {
58 enum oap_iei iei;
59
60 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
61 if (rc < 0)
62 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
63
64 iei = tag;
65
66 switch (iei) {
67 case OAP_CLIENT_ID_IE:
68 if (value_len != 2) {
69 LOGP(DGPRS, LOGL_NOTICE,
70 "OAP IE type client ID (%d) should be 2 octets, but has %d\n",
71 (int)iei, (int)value_len);
72 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
73 }
74
75 oap_msg->client_id = decode_big_endian(value, value_len);
76
77 if (oap_msg->client_id == 0) {
78 LOGP(DGPRS, LOGL_NOTICE,
79 "OAP IE type client ID (%d): client ID must be nonzero.\n",
80 (int)iei);
81 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
82 }
83 break;
84
85 case OAP_AUTN_IE:
86 if (value_len != sizeof(oap_msg->autn)) {
87 LOGP(DGPRS, LOGL_NOTICE,
88 "OAP IE type AUTN (%d) should be %d octets, but has %d\n",
89 (int)iei, (int)sizeof(oap_msg->autn), (int)value_len);
90 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
91 }
92 memcpy(oap_msg->autn, value, value_len);
93 oap_msg->autn_present = value_len;
94 break;
95
96 case OAP_RAND_IE:
97 if (value_len != sizeof(oap_msg->rand)) {
98 LOGP(DGPRS, LOGL_NOTICE,
99 "OAP IE type RAND (%d) should be %d octets, but has %d\n",
100 (int)iei, (int)sizeof(oap_msg->rand), (int)value_len);
101 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
102 }
103 memcpy(oap_msg->rand, value, value_len);
104 oap_msg->rand_present = value_len;
105 break;
106
107 case OAP_XRES_IE:
108 if (value_len != sizeof(oap_msg->xres)) {
109 LOGP(DGPRS, LOGL_NOTICE,
110 "OAP IE type XRES (%d) should be %d octets, but has %d\n",
111 (int)iei, (int)sizeof(oap_msg->xres), (int)value_len);
112 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
113 }
114 memcpy(oap_msg->xres, value, value_len);
115 oap_msg->xres_present = value_len;
116 break;
117
118 case OAP_AUTS_IE:
119 if (value_len != sizeof(oap_msg->auts)) {
120 LOGP(DGPRS, LOGL_NOTICE,
121 "OAP IE type AUTS (%d) should be %d octets, but has %d\n",
122 (int)iei, (int)sizeof(oap_msg->auts), (int)value_len);
123 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
124 }
125 memcpy(oap_msg->auts, value, value_len);
126 oap_msg->auts_present = value_len;
127 break;
128
129 case OAP_CAUSE_IE:
130 if (value_len > 1) {
131 LOGP(DGPRS, LOGL_ERROR,
132 "OAP cause may not exceed one octet, is %d", (int)value_len);
133 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
134 }
135 oap_msg->cause = *value;
136 break;
137
138 default:
139 LOGP(DGPRS, LOGL_NOTICE,
140 "OAP IE type %d unknown\n", iei);
141 continue;
142 }
143 }
144
145 return 0;
146}
147
148void oap_encode(struct msgb *msg, const struct oap_message *oap_msg)
149{
150 uint8_t u8;
151
152 /* generic part */
153 OSMO_ASSERT(oap_msg->message_type);
154 msgb_v_put(msg, (uint8_t)oap_msg->message_type);
155
156 /* specific parts */
157 if ((u8 = oap_msg->cause))
158 msgb_tlv_put(msg, OAP_CAUSE_IE, sizeof(u8), &u8);
159
160 if (oap_msg->client_id > 0)
161 msgb_tlv_put(msg, OAP_CLIENT_ID_IE, sizeof(oap_msg->client_id),
162 encode_big_endian(oap_msg->client_id, sizeof(oap_msg->client_id)));
163
164 if (oap_msg->rand_present)
165 msgb_tlv_put(msg, OAP_RAND_IE, sizeof(oap_msg->rand), oap_msg->rand);
166
167 if (oap_msg->autn_present)
168 msgb_tlv_put(msg, OAP_AUTN_IE, sizeof(oap_msg->autn), oap_msg->autn);
169
170 if (oap_msg->auts_present)
171 msgb_tlv_put(msg, OAP_AUTS_IE, sizeof(oap_msg->auts), oap_msg->auts);
172
173 if (oap_msg->xres_present)
174 msgb_tlv_put(msg, OAP_XRES_IE, sizeof(oap_msg->xres), oap_msg->xres);
175
176 msg->l2h = msg->data;
177}
178