summaryrefslogtreecommitdiff
path: root/lib/ext_safe_renegotiation.c
blob: 1f30ec979b8503544f45f30e16667872c727f93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2009 Free Software Foundation
 *
 * Author: Steve Dispensa (<dispensa@phonefactor.com>)
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

#include <gnutls_int.h>
#include <ext_safe_renegotiation.h>
#include <gnutls_errors.h>

int
_gnutls_safe_renegotiation_recv_params (gnutls_session_t session, 
		const opaque * data, size_t _data_size)
{
  tls_ext_st *ext = &session->security_parameters.extensions;
  int len = data[0];
  ssize_t data_size = _data_size;

  DECR_LEN (data_size, len+1 /* count the first byte and payload */);

  /* It is not legal to receive this extension on a renegotiation and
   * not receive it on the initial negotiation.
   */
  if (session->internals.initial_negotiation_completed != 0 &&
    session->internals.connection_using_safe_renegotiation == 0)
    {
      gnutls_assert();
      return GNUTLS_E_SAFE_RENEGOTIATION_FAILED;
    }

  if (len > sizeof (ext->ri_extension_data))
    {
      gnutls_assert();
      return GNUTLS_E_SAFE_RENEGOTIATION_FAILED;
    }

  if (len > 0)
    memcpy (ext->ri_extension_data, &data[1], len);
  ext->ri_extension_data_len = len;

  /* "safe renegotiation received" means on *this* handshake; "connection using
   * safe renegotiation" means that the initial hello received on the connection
   * indicated safe renegotiation. 
   */
  session->internals.safe_renegotiation_received = 1;
  session->internals.connection_using_safe_renegotiation = 1;

  return 0;
}

int
_gnutls_safe_renegotiation_send_params (gnutls_session_t session, 
		opaque * data, size_t _data_size)
{
  /* The format of this extension is a one-byte length of verify data followed
   * by the verify data itself. Note that the length byte does not include
   * itself; IOW, empty verify data is represented as a length of 0. That means
   * the minimum extension is one byte: 0x00.
   */
  ssize_t data_size = _data_size;
  tls_ext_st *ext = &session->security_parameters.extensions;


  if (session->internals.priorities.disable_safe_renegotiation != 0)
    {
      gnutls_assert();
      return 0;
    }

  data[0] = 0;

  /* Always offer the extension if we're a client */
  if (session->internals.connection_using_safe_renegotiation ||
     session->security_parameters.entity == GNUTLS_CLIENT)
    {
      DECR_LEN (data_size, 1);
      data[0] = ext->client_verify_data_len;

      DECR_LEN (data_size, ext->client_verify_data_len);

      if (ext->client_verify_data_len > 0)
        memcpy(&data[1], 
	     ext->client_verify_data, 
	     ext->client_verify_data_len);

      if (session->security_parameters.entity == GNUTLS_SERVER)
	{
	  data[0] += ext->server_verify_data_len;

	  DECR_LEN (data_size, ext->server_verify_data_len);

	  if (ext->server_verify_data_len > 0)
  	    memcpy(&data[1 + ext->client_verify_data_len],
		 ext->server_verify_data,
		 ext->server_verify_data_len);
	}
    }
  else
    return 0;

  return 1 + data[0]; /* don't forget the length byte */
}

/**
  * gnutls_safe_negotiation_set_initial - Used to enable and disable initial safe renegotiation
  * @session: is a #gnutls_session_t structure.
  * @value: 0 to disable and 1 to enable
  *
  * Used to enable and disable initial safe renegotiation for the current
  * session. By default it is allowed for a client to not advertise safe
  * renegotiation capability but there might be cases where signalling
  * a client of its insecurity by rejecting session might be beneficial.
  * This option has meaning only in server side.
  **/
void
gnutls_safe_negotiation_set_initial (gnutls_session_t session, int value)
{
  session->internals.priorities.initial_safe_renegotiation = value;
}

/**
  * gnutls_safe_negotiation_set - Used to enable and disable safe renegotiation
  * @session: is a #gnutls_session_t structure.
  * @value: 0 to disable and 1 to enable
  *
  * Used to enable and disable safe renegotiation for the current
  * session. Normally you shouldn't cope with this function since the
  * default (enable) is sufficient, but there might be servers that
  * cannot handle or correctly handle the extension.
  **/
void gnutls_safe_renegotiation_set (gnutls_session_t session, int value)
{
	session->internals.priorities.unsafe_renegotiation = 1-value;
}