/src/gnutls/lib/ext/cookie.c
Line | Count | Source |
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_parse_params(gnutls_session_t session, |
33 | | gnutls_buffer_st *extdata); |
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 = |
44 | | GNUTLS_EXT_MANDATORY, /* force parsing prior to EXT_TLS extensions */ |
45 | | .server_parse_point = |
46 | | GNUTLS_EXT_MANDATORY, /* force parsing prior to EXT_TLS extensions */ |
47 | | .parse_func = cookie_parse_params, |
48 | | .recv_func = NULL, |
49 | | .send_func = cookie_send_params, |
50 | | .pack_func = NULL, |
51 | | .unpack_func = NULL, |
52 | | .deinit_func = _gnutls_hello_ext_default_deinit, |
53 | | .cannot_be_overriden = 0 |
54 | | }; |
55 | | |
56 | | /* Only client sends this extension. */ |
57 | | static int cookie_parse_params(gnutls_session_t session, |
58 | | gnutls_buffer_st *extdata) |
59 | 0 | { |
60 | 0 | if (session->security_parameters.entity == GNUTLS_SERVER) { |
61 | | /* we don't support it */ |
62 | 0 | return 0; |
63 | 0 | } else { /* client */ |
64 | 0 | if (_gnutls_ext_get_msg(session) == GNUTLS_EXT_FLAG_HRR) { |
65 | 0 | gnutls_datum_t cookie; |
66 | 0 | int ret; |
67 | |
|
68 | 0 | ret = _gnutls_buffer_pop_datum_prefix16(extdata, |
69 | 0 | &cookie); |
70 | 0 | if (ret < 0) |
71 | 0 | return gnutls_assert_val(ret); |
72 | | |
73 | 0 | ret = _gnutls_hello_ext_set_datum( |
74 | 0 | session, GNUTLS_EXTENSION_COOKIE, &cookie); |
75 | 0 | if (ret < 0) |
76 | 0 | return gnutls_assert_val(ret); |
77 | | |
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | 0 | return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION); |
82 | 0 | } |
83 | | |
84 | 0 | return 0; |
85 | 0 | } |
86 | | |
87 | | /* returns data_size or a negative number on failure |
88 | | */ |
89 | | static int cookie_send_params(gnutls_session_t session, |
90 | | gnutls_buffer_st *extdata) |
91 | 0 | { |
92 | 0 | gnutls_datum_t tmp; |
93 | 0 | int ret; |
94 | | |
95 | | /* this function sends the client extension data (dnsname) */ |
96 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
97 | 0 | ret = _gnutls_hello_ext_get_datum( |
98 | 0 | session, GNUTLS_EXTENSION_COOKIE, &tmp); |
99 | 0 | if (ret < 0) |
100 | 0 | return 0; |
101 | | |
102 | 0 | ret = _gnutls_buffer_append_data_prefix16(extdata, tmp.data, |
103 | 0 | tmp.size); |
104 | 0 | if (ret < 0) |
105 | 0 | return gnutls_assert_val(ret); |
106 | | |
107 | 0 | return 0; |
108 | 0 | } |
109 | | |
110 | 0 | return 0; |
111 | 0 | } |