/src/gnutls/lib/ext/cookie.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 | | /* This file contains the code for the Max Record Size TLS extension. |
24 | | */ |
25 | | |
26 | | #include "gnutls_int.h" |
27 | | #include "errors.h" |
28 | | #include "num.h" |
29 | | #include "hello_ext_lib.h" |
30 | | #include <ext/cookie.h> |
31 | | |
32 | | static int cookie_recv_params(gnutls_session_t session, |
33 | | const uint8_t * data, size_t data_size); |
34 | | static int cookie_send_params(gnutls_session_t session, |
35 | | gnutls_buffer_st * extdata); |
36 | | |
37 | | const hello_ext_entry_st ext_mod_cookie = { |
38 | | .name = "Cookie", |
39 | | .tls_id = 44, |
40 | | .gid = GNUTLS_EXTENSION_COOKIE, |
41 | | .validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_CLIENT_HELLO | |
42 | | GNUTLS_EXT_FLAG_HRR | GNUTLS_EXT_FLAG_IGNORE_CLIENT_REQUEST, |
43 | | .client_parse_point = GNUTLS_EXT_MANDATORY, /* force parsing prior to EXT_TLS extensions */ |
44 | | .server_parse_point = GNUTLS_EXT_MANDATORY, /* force parsing prior to EXT_TLS extensions */ |
45 | | .recv_func = cookie_recv_params, |
46 | | .send_func = cookie_send_params, |
47 | | .pack_func = NULL, |
48 | | .unpack_func = NULL, |
49 | | .deinit_func = _gnutls_hello_ext_default_deinit, |
50 | | .cannot_be_overriden = 0 |
51 | | }; |
52 | | |
53 | | /* Only client sends this extension. */ |
54 | | static int |
55 | | cookie_recv_params(gnutls_session_t session, |
56 | | const uint8_t * data, size_t data_size) |
57 | 0 | { |
58 | 0 | size_t csize; |
59 | 0 | int ret; |
60 | 0 | gnutls_datum_t tmp; |
61 | |
|
62 | 0 | if (session->security_parameters.entity == GNUTLS_SERVER) { |
63 | | /* we don't support it */ |
64 | 0 | return 0; |
65 | 0 | } else { /* client */ |
66 | 0 | if (_gnutls_ext_get_msg(session) == GNUTLS_EXT_FLAG_HRR) { |
67 | 0 | DECR_LEN(data_size, 2); |
68 | | |
69 | 0 | csize = _gnutls_read_uint16(data); |
70 | 0 | data += 2; |
71 | |
|
72 | 0 | DECR_LEN(data_size, csize); |
73 | | |
74 | 0 | if (data_size != 0) |
75 | 0 | return |
76 | 0 | gnutls_assert_val |
77 | 0 | (GNUTLS_E_UNEXPECTED_PACKET_LENGTH); |
78 | | |
79 | 0 | tmp.data = (void *)data; |
80 | 0 | tmp.size = csize; |
81 | |
|
82 | 0 | ret = |
83 | 0 | _gnutls_hello_ext_set_datum(session, |
84 | 0 | GNUTLS_EXTENSION_COOKIE, |
85 | 0 | &tmp); |
86 | 0 | if (ret < 0) |
87 | 0 | return gnutls_assert_val(ret); |
88 | | |
89 | 0 | return 0; |
90 | 0 | } |
91 | | |
92 | 0 | return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION); |
93 | 0 | } |
94 | | |
95 | 0 | return 0; |
96 | 0 | } |
97 | | |
98 | | /* returns data_size or a negative number on failure |
99 | | */ |
100 | | static int |
101 | | cookie_send_params(gnutls_session_t session, gnutls_buffer_st * extdata) |
102 | 0 | { |
103 | 0 | gnutls_datum_t tmp; |
104 | 0 | int ret; |
105 | | |
106 | | /* this function sends the client extension data (dnsname) */ |
107 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
108 | 0 | ret = |
109 | 0 | _gnutls_hello_ext_get_datum(session, |
110 | 0 | GNUTLS_EXTENSION_COOKIE, &tmp); |
111 | 0 | if (ret < 0) |
112 | 0 | return 0; |
113 | | |
114 | 0 | ret = |
115 | 0 | _gnutls_buffer_append_data_prefix(extdata, 16, tmp.data, |
116 | 0 | tmp.size); |
117 | 0 | if (ret < 0) |
118 | 0 | return gnutls_assert_val(ret); |
119 | | |
120 | 0 | return 0; |
121 | 0 | } |
122 | | |
123 | 0 | return 0; |
124 | 0 | } |