/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 |
41 | | _gnutls_hello_ext_set_datum(gnutls_session_t session, |
42 | | extensions_t id, const gnutls_datum_t * data) |
43 | 0 | { |
44 | 0 | gnutls_ext_priv_data_t epriv; |
45 | |
|
46 | 0 | if (_gnutls_hello_ext_get_priv(session, id, &epriv) >= 0) |
47 | 0 | return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER); |
48 | | |
49 | 0 | if (data->size >= UINT16_MAX) |
50 | 0 | return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER); |
51 | | |
52 | 0 | epriv = gnutls_malloc(data->size + 2); |
53 | 0 | if (epriv == NULL) |
54 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
55 | | |
56 | 0 | _gnutls_write_uint16(data->size, epriv); |
57 | 0 | memcpy(((uint8_t *) epriv) + 2, data->data, data->size); |
58 | |
|
59 | 0 | _gnutls_hello_ext_set_priv(session, id, epriv); |
60 | |
|
61 | 0 | return 0; |
62 | 0 | } |
63 | | |
64 | | int |
65 | | _gnutls_hello_ext_get_datum(gnutls_session_t session, |
66 | | extensions_t id, |
67 | | gnutls_datum_t * data /* constant contents */ ) |
68 | 0 | { |
69 | 0 | gnutls_ext_priv_data_t epriv; |
70 | 0 | int ret; |
71 | |
|
72 | 0 | ret = _gnutls_hello_ext_get_priv(session, id, &epriv); |
73 | 0 | if (ret < 0 || epriv == NULL) |
74 | 0 | return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; |
75 | | |
76 | 0 | data->size = _gnutls_read_uint16(epriv); |
77 | 0 | data->data = ((uint8_t *) epriv) + 2; |
78 | |
|
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | | int |
83 | | _gnutls_hello_ext_get_resumed_datum(gnutls_session_t session, |
84 | | extensions_t id, |
85 | | gnutls_datum_t * |
86 | | data /* constant contents */ ) |
87 | 0 | { |
88 | 0 | gnutls_ext_priv_data_t epriv; |
89 | 0 | int ret; |
90 | |
|
91 | 0 | ret = _gnutls_hello_ext_get_resumed_priv(session, id, &epriv); |
92 | 0 | if (ret < 0 || epriv == NULL) |
93 | 0 | return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; |
94 | | |
95 | 0 | data->size = _gnutls_read_uint16(epriv); |
96 | 0 | data->data = ((uint8_t *) epriv) + 2; |
97 | |
|
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | int |
102 | | _gnutls_hello_ext_default_pack(gnutls_ext_priv_data_t epriv, |
103 | | gnutls_buffer_st * ps) |
104 | 0 | { |
105 | 0 | size_t size; |
106 | |
|
107 | 0 | size = _gnutls_read_uint16(epriv); |
108 | |
|
109 | 0 | return _gnutls_buffer_append_data(ps, epriv, size + 2); |
110 | 0 | } |
111 | | |
112 | | int |
113 | | _gnutls_hello_ext_default_unpack(gnutls_buffer_st * ps, |
114 | | gnutls_ext_priv_data_t * epriv) |
115 | 0 | { |
116 | 0 | gnutls_datum_t data; |
117 | 0 | uint8_t *store; |
118 | 0 | int ret; |
119 | |
|
120 | 0 | ret = _gnutls_buffer_pop_datum_prefix16(ps, &data); |
121 | 0 | if (ret < 0) |
122 | 0 | return gnutls_assert_val(ret); |
123 | | |
124 | 0 | store = gnutls_calloc(1, data.size + 2); |
125 | 0 | if (store == NULL) |
126 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
127 | | |
128 | 0 | _gnutls_write_uint16(data.size, store); |
129 | 0 | memcpy(store + 2, data.data, data.size); |
130 | |
|
131 | 0 | *epriv = store; |
132 | 0 | return 0; |
133 | 0 | } |