Fix memory leaks.
[gnutls.git] / lib / auth_dhe.c
blobef10802889530022923ca4e8772344782c6a6e54
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file contains everything for the Ephemeral Diffie Hellman (DHE)
26 * key exchange. This is used in the handshake procedure of the certificate
27 * authentication.
30 #include "gnutls_int.h"
31 #include "gnutls_auth_int.h"
32 #include "gnutls_errors.h"
33 #include "gnutls_dh.h"
34 #include "gnutls_num.h"
35 #include "gnutls_sig.h"
36 #include <gnutls_datum.h>
37 #include <auth_cert.h>
38 #include <gnutls_x509.h>
39 #include <gnutls_state.h>
40 #include <auth_dh_common.h>
42 static int gen_dhe_server_kx (gnutls_session_t, opaque **);
43 static int proc_dhe_server_kx (gnutls_session_t, opaque *, size_t);
44 static int proc_dhe_client_kx (gnutls_session_t, opaque *, size_t);
46 const mod_auth_st dhe_rsa_auth_struct = {
47 "DHE_RSA",
48 _gnutls_gen_cert_server_certificate,
49 _gnutls_gen_cert_client_certificate,
50 gen_dhe_server_kx,
51 _gnutls_gen_dh_common_client_kx,
52 _gnutls_gen_cert_client_cert_vrfy, /* gen client cert vrfy */
53 _gnutls_gen_cert_server_cert_req, /* server cert request */
55 _gnutls_proc_cert_server_certificate,
56 _gnutls_proc_cert_client_certificate,
57 proc_dhe_server_kx,
58 proc_dhe_client_kx,
59 _gnutls_proc_cert_client_cert_vrfy, /* proc client cert vrfy */
60 _gnutls_proc_cert_cert_req /* proc server cert request */
63 const mod_auth_st dhe_dss_auth_struct = {
64 "DHE_DSS",
65 _gnutls_gen_cert_server_certificate,
66 _gnutls_gen_cert_client_certificate,
67 gen_dhe_server_kx,
68 _gnutls_gen_dh_common_client_kx,
69 _gnutls_gen_cert_client_cert_vrfy, /* gen client cert vrfy */
70 _gnutls_gen_cert_server_cert_req, /* server cert request */
72 _gnutls_proc_cert_server_certificate,
73 _gnutls_proc_cert_client_certificate,
74 proc_dhe_server_kx,
75 proc_dhe_client_kx,
76 _gnutls_proc_cert_client_cert_vrfy, /* proc client cert vrfy */
77 _gnutls_proc_cert_cert_req /* proc server cert request */
81 static int
82 gen_dhe_server_kx (gnutls_session_t session, opaque ** data)
84 mpi_t g, p;
85 const mpi_t *mpis;
86 int ret = 0, data_size;
87 int bits;
88 gnutls_cert *apr_cert_list;
89 gnutls_privkey *apr_pkey;
90 int apr_cert_list_length;
91 gnutls_datum_t signature, ddata;
92 gnutls_certificate_credentials_t cred;
93 gnutls_dh_params_t dh_params;
95 cred = (gnutls_certificate_credentials_t)
96 _gnutls_get_cred (session->key, GNUTLS_CRD_CERTIFICATE, NULL);
97 if (cred == NULL)
99 gnutls_assert ();
100 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
103 bits = _gnutls_dh_get_allowed_prime_bits (session);
105 /* find the appropriate certificate */
106 if ((ret =
107 _gnutls_get_selected_cert (session, &apr_cert_list,
108 &apr_cert_list_length, &apr_pkey)) < 0)
110 gnutls_assert ();
111 return ret;
114 dh_params =
115 _gnutls_get_dh_params (cred->dh_params, cred->params_func, session);
116 mpis = _gnutls_dh_params_to_mpi (dh_params);
117 if (mpis == NULL)
119 gnutls_assert ();
120 return GNUTLS_E_NO_TEMPORARY_DH_PARAMS;
123 p = mpis[0];
124 g = mpis[1];
126 if ((ret = _gnutls_auth_info_set (session, GNUTLS_CRD_CERTIFICATE,
127 sizeof (cert_auth_info_st), 0)) < 0)
129 gnutls_assert ();
130 return ret;
133 _gnutls_dh_set_group (session, g, p);
135 ret = _gnutls_dh_common_print_server_kx (session, g, p, data, 0);
136 if (ret < 0)
138 gnutls_assert ();
139 return ret;
141 data_size = ret;
143 /* Generate the signature. */
145 ddata.data = *data;
146 ddata.size = data_size;
148 if (apr_cert_list_length > 0)
150 if ((ret =
151 _gnutls_tls_sign_params (session, &apr_cert_list[0],
152 apr_pkey, &ddata, &signature)) < 0)
154 gnutls_assert ();
155 gnutls_free (*data);
156 return ret;
159 else
161 gnutls_assert ();
162 return data_size; /* do not put a signature - ILLEGAL! */
165 *data = gnutls_realloc_fast (*data, data_size + signature.size + 2);
166 if (*data == NULL)
168 _gnutls_free_datum (&signature);
169 gnutls_assert ();
170 return GNUTLS_E_MEMORY_ERROR;
173 _gnutls_write_datum16 (&(*data)[data_size], signature);
174 data_size += signature.size + 2;
176 _gnutls_free_datum (&signature);
178 return data_size;
181 static int
182 proc_dhe_server_kx (gnutls_session_t session, opaque * data,
183 size_t _data_size)
185 int sigsize;
186 gnutls_datum_t vparams, signature;
187 int ret;
188 cert_auth_info_t info = _gnutls_get_auth_info (session);
189 ssize_t data_size = _data_size;
190 gnutls_cert peer_cert;
192 if (info == NULL || info->ncerts == 0)
194 gnutls_assert ();
195 /* we need this in order to get peer's certificate */
196 return GNUTLS_E_INTERNAL_ERROR;
199 ret = _gnutls_proc_dh_common_server_kx (session, data, _data_size, 0);
200 if (ret < 0)
202 gnutls_assert ();
203 return ret;
206 /* VERIFY SIGNATURE */
208 vparams.size = ret;
209 vparams.data = data;
211 DECR_LEN (data_size, 2);
212 sigsize = _gnutls_read_uint16 (&data[vparams.size]);
214 DECR_LEN (data_size, sigsize);
215 signature.data = &data[vparams.size + 2];
216 signature.size = sigsize;
218 if ((ret =
219 _gnutls_get_auth_info_gcert (&peer_cert,
220 session->security_parameters.cert_type,
221 info,
222 CERT_NO_COPY)) < 0)
224 gnutls_assert ();
225 return ret;
228 ret = _gnutls_verify_sig_params (session, &peer_cert, &vparams, &signature);
230 _gnutls_gcert_deinit (&peer_cert);
231 if (ret < 0)
233 gnutls_assert ();
234 return ret;
237 return ret;
242 static int
243 proc_dhe_client_kx (gnutls_session_t session, opaque * data,
244 size_t _data_size)
246 gnutls_certificate_credentials_t cred;
247 int ret;
248 mpi_t p, g;
249 const mpi_t *mpis;
250 gnutls_dh_params_t dh_params;
252 cred = (gnutls_certificate_credentials_t)
253 _gnutls_get_cred (session->key, GNUTLS_CRD_CERTIFICATE, NULL);
254 if (cred == NULL)
256 gnutls_assert ();
257 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
260 dh_params =
261 _gnutls_get_dh_params (cred->dh_params, cred->params_func, session);
262 mpis = _gnutls_dh_params_to_mpi (dh_params);
263 if (mpis == NULL)
265 gnutls_assert ();
266 return GNUTLS_E_NO_TEMPORARY_DH_PARAMS;
269 p = mpis[0];
270 g = mpis[1];
272 ret = _gnutls_proc_dh_common_client_kx (session, data, _data_size, g, p);
274 return ret;