Fixed leaks in key generation and other cleanups. Patch by Tomas Mraz.
[gnutls.git] / lib / auth_anon.c
blobfce66d13fd0d3b732956a71dc0b87d3ddefc5a32
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2009, 2010 Free
3 * Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
26 /* This file contains the Anonymous Diffie-Hellman key exchange part of
27 * the anonymous authentication. The functions here are used in the
28 * handshake.
31 #include <gnutls_int.h>
33 #ifdef ENABLE_ANON
35 #include "gnutls_auth.h"
36 #include "gnutls_errors.h"
37 #include "gnutls_dh.h"
38 #include "auth_anon.h"
39 #include "gnutls_num.h"
40 #include "gnutls_mpi.h"
41 #include <gnutls_state.h>
42 #include <auth_dh_common.h>
44 static int gen_anon_server_kx (gnutls_session_t, opaque **);
45 static int proc_anon_client_kx (gnutls_session_t, opaque *, size_t);
46 static int proc_anon_server_kx (gnutls_session_t, opaque *, size_t);
48 const mod_auth_st anon_auth_struct = {
49 "ANON",
50 NULL,
51 NULL,
52 gen_anon_server_kx,
53 _gnutls_gen_dh_common_client_kx, /* this can be shared */
54 NULL,
55 NULL,
57 NULL,
58 NULL, /* certificate */
59 proc_anon_server_kx,
60 proc_anon_client_kx,
61 NULL,
62 NULL
65 static int
66 gen_anon_server_kx (gnutls_session_t session, opaque ** data)
68 bigint_t g, p;
69 const bigint_t *mpis;
70 int ret;
71 gnutls_dh_params_t dh_params;
72 gnutls_anon_server_credentials_t cred;
74 cred = (gnutls_anon_server_credentials_t)
75 _gnutls_get_cred (session->key, GNUTLS_CRD_ANON, NULL);
76 if (cred == NULL)
78 gnutls_assert ();
79 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
82 dh_params =
83 _gnutls_get_dh_params (cred->dh_params, cred->params_func, session);
84 mpis = _gnutls_dh_params_to_mpi (dh_params);
85 if (mpis == NULL)
87 gnutls_assert ();
88 return GNUTLS_E_NO_TEMPORARY_DH_PARAMS;
91 p = mpis[0];
92 g = mpis[1];
94 if ((ret =
95 _gnutls_auth_info_set (session, GNUTLS_CRD_ANON,
96 sizeof (anon_auth_info_st), 1)) < 0)
98 gnutls_assert ();
99 return ret;
102 _gnutls_dh_set_group (session, g, p);
104 ret = _gnutls_dh_common_print_server_kx (session, g, p, data, 0);
105 if (ret < 0)
107 gnutls_assert ();
110 return ret;
114 static int
115 proc_anon_client_kx (gnutls_session_t session, opaque * data,
116 size_t _data_size)
118 gnutls_anon_server_credentials_t cred;
119 int ret;
120 bigint_t p, g;
121 gnutls_dh_params_t dh_params;
122 const bigint_t *mpis;
124 cred = (gnutls_anon_server_credentials_t)
125 _gnutls_get_cred (session->key, GNUTLS_CRD_ANON, NULL);
126 if (cred == NULL)
128 gnutls_assert ();
129 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
132 dh_params =
133 _gnutls_get_dh_params (cred->dh_params, cred->params_func, session);
134 mpis = _gnutls_dh_params_to_mpi (dh_params);
135 if (mpis == NULL)
137 gnutls_assert ();
138 return GNUTLS_E_NO_TEMPORARY_DH_PARAMS;
141 p = mpis[0];
142 g = mpis[1];
144 ret = _gnutls_proc_dh_common_client_kx (session, data, _data_size, g, p);
146 return ret;
151 proc_anon_server_kx (gnutls_session_t session, opaque * data,
152 size_t _data_size)
155 int ret;
157 /* set auth_info */
158 if ((ret =
159 _gnutls_auth_info_set (session, GNUTLS_CRD_ANON,
160 sizeof (anon_auth_info_st), 1)) < 0)
162 gnutls_assert ();
163 return ret;
166 ret = _gnutls_proc_dh_common_server_kx (session, data, _data_size, 0);
167 if (ret < 0)
169 gnutls_assert ();
170 return ret;
173 return 0;
176 #endif /* ENABLE_ANON */