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,
25 /* This file contains everything for the Ephemeral Diffie Hellman (DHE)
26 * key exchange. This is used in the handshake procedure of the certificate
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
= {
48 _gnutls_gen_cert_server_certificate
,
49 _gnutls_gen_cert_client_certificate
,
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
,
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
= {
65 _gnutls_gen_cert_server_certificate
,
66 _gnutls_gen_cert_client_certificate
,
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
,
76 _gnutls_proc_cert_client_cert_vrfy
, /* proc client cert vrfy */
77 _gnutls_proc_cert_cert_req
/* proc server cert request */
82 gen_dhe_server_kx (gnutls_session_t session
, opaque
** data
)
86 int ret
= 0, data_size
;
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
);
100 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
103 bits
= _gnutls_dh_get_allowed_prime_bits (session
);
105 /* find the appropriate certificate */
107 _gnutls_get_selected_cert (session
, &apr_cert_list
,
108 &apr_cert_list_length
, &apr_pkey
)) < 0)
115 _gnutls_get_dh_params (cred
->dh_params
, cred
->params_func
, session
);
116 mpis
= _gnutls_dh_params_to_mpi (dh_params
);
120 return GNUTLS_E_NO_TEMPORARY_DH_PARAMS
;
126 if ((ret
= _gnutls_auth_info_set (session
, GNUTLS_CRD_CERTIFICATE
,
127 sizeof (cert_auth_info_st
), 0)) < 0)
133 _gnutls_dh_set_group (session
, g
, p
);
135 ret
= _gnutls_dh_common_print_server_kx (session
, g
, p
, data
, 0);
143 /* Generate the signature. */
146 ddata
.size
= data_size
;
148 if (apr_cert_list_length
> 0)
151 _gnutls_tls_sign_params (session
, &apr_cert_list
[0],
152 apr_pkey
, &ddata
, &signature
)) < 0)
162 return data_size
; /* do not put a signature - ILLEGAL! */
165 *data
= gnutls_realloc_fast (*data
, data_size
+ signature
.size
+ 2);
168 _gnutls_free_datum (&signature
);
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
);
182 proc_dhe_server_kx (gnutls_session_t session
, opaque
* data
,
186 gnutls_datum_t vparams
, signature
;
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)
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);
206 /* VERIFY SIGNATURE */
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
;
219 _gnutls_get_auth_info_gcert (&peer_cert
,
220 session
->security_parameters
.cert_type
,
228 ret
= _gnutls_verify_sig_params (session
, &peer_cert
, &vparams
, &signature
);
230 _gnutls_gcert_deinit (&peer_cert
);
243 proc_dhe_client_kx (gnutls_session_t session
, opaque
* data
,
246 gnutls_certificate_credentials_t cred
;
250 gnutls_dh_params_t dh_params
;
252 cred
= (gnutls_certificate_credentials_t
)
253 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
257 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
261 _gnutls_get_dh_params (cred
->dh_params
, cred
->params_func
, session
);
262 mpis
= _gnutls_dh_params_to_mpi (dh_params
);
266 return GNUTLS_E_NO_TEMPORARY_DH_PARAMS
;
272 ret
= _gnutls_proc_dh_common_client_kx (session
, data
, _data_size
, g
, p
);