/src/gnutls/lib/hello_ext_lib.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2017 Red Hat, Inc.  | 
3  |  |  *  | 
4  |  |  * Author: Nikos Mavrogiannopoulos  | 
5  |  |  *  | 
6  |  |  * This file is part of GnuTLS.  | 
7  |  |  *  | 
8  |  |  * The GnuTLS 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.  | 
12  |  |  *  | 
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.  | 
17  |  |  *  | 
18  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
19  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
20  |  |  *  | 
21  |  |  */  | 
22  |  |  | 
23  |  | /* Internal API functions to be used by extension handlers.  | 
24  |  |  */  | 
25  |  |  | 
26  |  | #include "gnutls_int.h"  | 
27  |  | #include "hello_ext.h"  | 
28  |  | #include "hello_ext_lib.h"  | 
29  |  |  | 
30  |  | void _gnutls_hello_ext_default_deinit(gnutls_ext_priv_data_t priv)  | 
31  | 0  | { | 
32  | 0  |   gnutls_free(priv);  | 
33  | 0  | }  | 
34  |  |  | 
35  |  | /* When this is used, the deinitialization function must be set to default:  | 
36  |  |  * _gnutls_hello_ext_default_deinit.  | 
37  |  |  *  | 
38  |  |  * This also prevents and errors on duplicate entries.  | 
39  |  |  */  | 
40  |  | int _gnutls_hello_ext_set_datum(gnutls_session_t session, extensions_t id,  | 
41  |  |         const gnutls_datum_t *data)  | 
42  | 0  | { | 
43  | 0  |   gnutls_ext_priv_data_t epriv;  | 
44  |  | 
  | 
45  | 0  |   if (_gnutls_hello_ext_get_priv(session, id, &epriv) >= 0)  | 
46  | 0  |     return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);  | 
47  |  |  | 
48  | 0  |   if (data->size >= UINT16_MAX)  | 
49  | 0  |     return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);  | 
50  |  |  | 
51  | 0  |   epriv = gnutls_malloc(data->size + 2);  | 
52  | 0  |   if (epriv == NULL)  | 
53  | 0  |     return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
54  |  |  | 
55  | 0  |   _gnutls_write_uint16(data->size, epriv);  | 
56  | 0  |   memcpy(((uint8_t *)epriv) + 2, data->data, data->size);  | 
57  |  | 
  | 
58  | 0  |   _gnutls_hello_ext_set_priv(session, id, epriv);  | 
59  |  | 
  | 
60  | 0  |   return 0;  | 
61  | 0  | }  | 
62  |  |  | 
63  |  | int _gnutls_hello_ext_get_datum(gnutls_session_t session, extensions_t id,  | 
64  |  |         gnutls_datum_t *data /* constant contents */)  | 
65  | 0  | { | 
66  | 0  |   gnutls_ext_priv_data_t epriv;  | 
67  | 0  |   int ret;  | 
68  |  | 
  | 
69  | 0  |   ret = _gnutls_hello_ext_get_priv(session, id, &epriv);  | 
70  | 0  |   if (ret < 0 || epriv == NULL)  | 
71  | 0  |     return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
72  |  |  | 
73  | 0  |   data->size = _gnutls_read_uint16(epriv);  | 
74  | 0  |   data->data = ((uint8_t *)epriv) + 2;  | 
75  |  | 
  | 
76  | 0  |   return 0;  | 
77  | 0  | }  | 
78  |  |  | 
79  |  | int _gnutls_hello_ext_get_resumed_datum(  | 
80  |  |   gnutls_session_t session, extensions_t id,  | 
81  |  |   gnutls_datum_t *data /* constant contents */)  | 
82  | 0  | { | 
83  | 0  |   gnutls_ext_priv_data_t epriv;  | 
84  | 0  |   int ret;  | 
85  |  | 
  | 
86  | 0  |   ret = _gnutls_hello_ext_get_resumed_priv(session, id, &epriv);  | 
87  | 0  |   if (ret < 0 || epriv == NULL)  | 
88  | 0  |     return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
89  |  |  | 
90  | 0  |   data->size = _gnutls_read_uint16(epriv);  | 
91  | 0  |   data->data = ((uint8_t *)epriv) + 2;  | 
92  |  | 
  | 
93  | 0  |   return 0;  | 
94  | 0  | }  | 
95  |  |  | 
96  |  | int _gnutls_hello_ext_default_pack(gnutls_ext_priv_data_t epriv,  | 
97  |  |            gnutls_buffer_st *ps)  | 
98  | 0  | { | 
99  | 0  |   size_t size;  | 
100  |  | 
  | 
101  | 0  |   size = _gnutls_read_uint16(epriv);  | 
102  |  | 
  | 
103  | 0  |   return _gnutls_buffer_append_data(ps, epriv, size + 2);  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | int _gnutls_hello_ext_default_unpack(gnutls_buffer_st *ps,  | 
107  |  |              gnutls_ext_priv_data_t *epriv)  | 
108  | 0  | { | 
109  | 0  |   gnutls_datum_t data;  | 
110  | 0  |   uint8_t *store;  | 
111  | 0  |   int ret;  | 
112  |  | 
  | 
113  | 0  |   ret = _gnutls_buffer_pop_datum_prefix16(ps, &data);  | 
114  | 0  |   if (ret < 0)  | 
115  | 0  |     return gnutls_assert_val(ret);  | 
116  |  |  | 
117  | 0  |   store = gnutls_calloc(1, data.size + 2);  | 
118  | 0  |   if (store == NULL)  | 
119  | 0  |     return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);  | 
120  |  |  | 
121  | 0  |   _gnutls_write_uint16(data.size, store);  | 
122  | 0  |   memcpy(store + 2, data.data, data.size);  | 
123  |  | 
  | 
124  | 0  |   *epriv = store;  | 
125  | 0  |   return 0;  | 
126  | 0  | }  |