Version 1.7.10.
[gnutls.git] / lib / gnutls_supplemental.c
blob13ae5bf7da15cbfbe7823da31cd2eaef65078cc6
1 /*
2 * Copyright (C) 2007 Free Software Foundation
4 * Author: Simon Josefsson
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file contains support functions for 'TLS Handshake Message for
26 * Supplemental Data' (RFC 4680).
28 * The idea here is simple. gnutls_handshake() in gnuts_handshake.c
29 * will call _gnutls_gen_supplemental and _gnutls_parse_supplemental
30 * when some extension requested that supplemental data be sent or
31 * received. Extension request this by setting the flags
32 * do_recv_supplemental or do_send_supplemental in the session.
34 * The functions in this file iterate through the _gnutls_supplemental
35 * array, and calls the send/recv functions for each respective data
36 * type.
38 * The receive function of each data type is responsible for decoding
39 * its own data. If the extension did not expect to receive
40 * supplemental data, it should return GNUTLS_E_UNEXPECTED_PACKET.
41 * Otherwise, it just parse the data as normal.
43 * The send function needs to append the 2-byte data format type, and
44 * append the 2-byte length of its data, and the data. If it doesn't
45 * want to send any data, it is fine to return without doing anything.
48 #include "gnutls_int.h"
49 #include "gnutls_supplemental.h"
50 #include "gnutls_errors.h"
51 #include "gnutls_num.h"
52 #include "ext_authz.h"
54 typedef int (*supp_recv_func) (gnutls_session_t session,
55 const opaque *data,
56 size_t data_size);
57 typedef int (*supp_send_func) (gnutls_session_t session,
58 gnutls_buffer *buf);
60 typedef struct
62 const char *name;
63 gnutls_supplemental_data_format_type_t type;
64 supp_recv_func supp_recv_func;
65 supp_send_func supp_send_func;
66 } gnutls_supplemental_entry;
68 gnutls_supplemental_entry _gnutls_supplemental[] = {
69 { "authz_data",
70 GNUTLS_SUPPLEMENTAL_AUTHZ_DATA,
71 _gnutls_authz_supp_recv_params,
72 _gnutls_authz_supp_send_params },
73 { 0, 0, 0, 0 }
76 const char *
77 gnutls_supplemental_get_name (gnutls_supplemental_data_format_type_t type)
79 gnutls_supplemental_entry *p;
81 for(p = _gnutls_supplemental; p->name != NULL; p++)
82 if(p->type == type)
83 return p->name;
85 return NULL;
88 static supp_recv_func
89 get_supp_func_recv (gnutls_supplemental_data_format_type_t type)
91 gnutls_supplemental_entry *p;
93 for(p = _gnutls_supplemental; p->name != NULL; p++)
94 if(p->type == type)
95 return p->supp_recv_func;
97 return NULL;
101 _gnutls_gen_supplemental (gnutls_session_t session, gnutls_buffer *buf)
103 gnutls_supplemental_entry *p;
104 int ret;
106 /* Make room for 3 byte length field. */
107 ret = _gnutls_buffer_append (buf, "\0\0\0", 3);
108 if (ret < 0)
110 gnutls_assert ();
111 return ret;
114 for(p = _gnutls_supplemental; p->name; p++)
116 supp_send_func supp_send = p->supp_send_func;
117 size_t sizepos = buf->length;
118 int ret;
120 /* Make room for supplement type and length byte length field. */
121 ret = _gnutls_buffer_append (buf, "\0\0\0\0", 4);
122 if (ret < 0)
124 gnutls_assert ();
125 return ret;
128 ret = supp_send (session, buf);
129 if (ret < 0)
131 gnutls_assert ();
132 return ret;
135 /* If data were added, store type+length, otherwise reset. */
136 if (buf->length > sizepos + 4)
138 buf->data[sizepos] = 0;
139 buf->data[sizepos + 1] = p->type;
140 buf->data[sizepos + 2] = ((buf->length - sizepos - 4) >> 8) & 0xFF;
141 buf->data[sizepos + 3] = (buf->length - sizepos -4) & 0xFF;
143 else
144 buf->length -= 4;
147 buf->data[0] = ((buf->length - 3) >> 16) & 0xFF;
148 buf->data[1] = ((buf->length - 3) >> 8) & 0xFF;
149 buf->data[2] = (buf->length - 3) & 0xFF;
151 _gnutls_debug_log ("EXT[%x]: Sending %d bytes of supplemental data\n",
152 session, buf->length);
154 return buf->length;
158 _gnutls_parse_supplemental (gnutls_session_t session,
159 const uint8_t * data,
160 int datalen)
162 const opaque *p = data;
163 ssize_t dsize = datalen;
164 size_t total_size;
166 DECR_LEN (dsize, 3);
167 total_size = _gnutls_read_uint24 (p);
168 p += 3;
170 if (dsize != total_size)
172 gnutls_assert();
173 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
178 uint16_t supp_data_type;
179 uint16_t supp_data_length;
180 supp_recv_func recv_func;
182 DECR_LEN (dsize, 2);
183 supp_data_type = _gnutls_read_uint16 (p);
184 p += 2;
186 DECR_LEN (dsize, 2);
187 supp_data_length = _gnutls_read_uint16 (p);
188 p += 2;
190 _gnutls_debug_log ("EXT[%x]: Got supplemental type=%02x length=%d\n",
191 session, supp_data_type, supp_data_length);
193 recv_func = get_supp_func_recv (supp_data_type);
194 if (recv_func)
196 int ret = recv_func (session, p, supp_data_length);
197 if (ret < 0)
199 gnutls_assert ();
200 return ret;
203 else
205 gnutls_assert ();
206 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
209 DECR_LEN (dsize, supp_data_length);
210 p += supp_data_length;
212 while (dsize > 0);
214 return 0;