2 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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 3 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 License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
27 #include "gnutls_errors.h"
28 #include <auth/srp_passwd.h>
29 #include "gnutls_auth.h"
30 #include "gnutls_auth.h"
31 #include "gnutls_srp.h"
33 #include "gnutls_num.h"
35 #include <gnutls_str.h>
36 #include <auth/cert.h>
37 #include <gnutls_datum.h>
38 #include <gnutls_sig.h>
40 #include <gnutls_x509.h>
41 #include <algorithms.h>
43 static int gen_srp_cert_server_kx (gnutls_session_t
, gnutls_buffer_st
*);
44 static int proc_srp_cert_server_kx (gnutls_session_t
, uint8_t *, size_t);
46 const mod_auth_st srp_rsa_auth_struct
= {
48 _gnutls_gen_cert_server_crt
,
50 gen_srp_cert_server_kx
,
51 _gnutls_gen_srp_client_kx
,
56 NULL
, /* certificate */
57 proc_srp_cert_server_kx
,
58 _gnutls_proc_srp_client_kx
,
63 const mod_auth_st srp_dss_auth_struct
= {
65 _gnutls_gen_cert_server_crt
,
67 gen_srp_cert_server_kx
,
68 _gnutls_gen_srp_client_kx
,
73 NULL
, /* certificate */
74 proc_srp_cert_server_kx
,
75 _gnutls_proc_srp_client_kx
,
81 gen_srp_cert_server_kx (gnutls_session_t session
, gnutls_buffer_st
* data
)
84 gnutls_datum_t signature
, ddata
;
85 gnutls_certificate_credentials_t cred
;
86 gnutls_pcert_st
*apr_cert_list
;
87 gnutls_privkey_t apr_pkey
;
88 int apr_cert_list_length
;
89 gnutls_sign_algorithm_t sign_algo
;
90 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
92 ret
= _gnutls_gen_srp_server_kx (session
, data
);
97 ddata
.data
= data
->data
;
98 ddata
.size
= data
->length
;
100 cred
= (gnutls_certificate_credentials_t
)
101 _gnutls_get_cred (session
, GNUTLS_CRD_CERTIFICATE
, NULL
);
105 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
108 /* find the appropriate certificate */
110 _gnutls_get_selected_cert (session
, &apr_cert_list
,
111 &apr_cert_list_length
, &apr_pkey
)) < 0)
118 _gnutls_handshake_sign_data (session
, &apr_cert_list
[0],
119 apr_pkey
, &ddata
, &signature
,
126 if (_gnutls_version_has_selectable_sighash (ver
))
128 const sign_algorithm_st
*aid
;
131 if (sign_algo
== GNUTLS_SIGN_UNKNOWN
)
133 ret
= GNUTLS_E_UNKNOWN_ALGORITHM
;
137 aid
= _gnutls_sign_to_tls_aid (sign_algo
);
141 ret
= GNUTLS_E_UNKNOWN_ALGORITHM
;
145 p
[0] = aid
->hash_algorithm
;
146 p
[1] = aid
->sign_algorithm
;
148 ret
= _gnutls_buffer_append_data(data
, p
, 2);
156 ret
= _gnutls_buffer_append_data_prefix( data
, 16, signature
.data
, signature
.size
);
167 _gnutls_free_datum (&signature
);
172 proc_srp_cert_server_kx (gnutls_session_t session
, uint8_t * data
,
177 gnutls_datum_t vparams
, signature
;
179 cert_auth_info_t info
;
180 gnutls_pcert_st peer_cert
;
182 gnutls_sign_algorithm_t sign_algo
= GNUTLS_SIGN_UNKNOWN
;
183 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
185 ret
= _gnutls_proc_srp_server_kx (session
, data
, _data_size
);
189 data_size
= _data_size
- ret
;
191 info
= _gnutls_get_auth_info (session
);
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 /* VERIFY SIGNATURE */
201 vparams
.size
= ret
; /* all the data minus the signature */
204 p
= &data
[vparams
.size
];
205 if (_gnutls_version_has_selectable_sighash (ver
))
207 sign_algorithm_st aid
;
209 DECR_LEN (data_size
, 1);
210 aid
.hash_algorithm
= *p
++;
211 DECR_LEN (data_size
, 1);
212 aid
.sign_algorithm
= *p
++;
213 sign_algo
= _gnutls_tls_aid_to_sign (&aid
);
214 if (sign_algo
== GNUTLS_SIGN_UNKNOWN
)
216 _gnutls_debug_log("unknown signature %d.%d\n", aid
.sign_algorithm
, aid
.hash_algorithm
);
218 return GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM
;
222 DECR_LEN (data_size
, 2);
223 sigsize
= _gnutls_read_uint16 (p
);
225 DECR_LEN (data_size
, sigsize
);
226 signature
.data
= &p
[2];
227 signature
.size
= sigsize
;
230 _gnutls_get_auth_info_pcert (&peer_cert
,
231 session
->security_parameters
.cert_type
,
241 _gnutls_handshake_verify_data (session
, &peer_cert
, &vparams
, &signature
,
244 gnutls_pcert_deinit (&peer_cert
);
255 #endif /* ENABLE_SRP */