corrected copyright notices
[gnutls.git] / lib / auth / srp_rsa.c
blob4f6eb30a716e6733533398d1b8a109d5ca0d0995
1 /*
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>
25 #ifdef ENABLE_SRP
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"
32 #include "debug.h"
33 #include "gnutls_num.h"
34 #include <auth/srp.h>
35 #include <gnutls_str.h>
36 #include <auth/cert.h>
37 #include <gnutls_datum.h>
38 #include <gnutls_sig.h>
39 #include <auth/srp.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 = {
47 "SRP",
48 _gnutls_gen_cert_server_crt,
49 NULL,
50 gen_srp_cert_server_kx,
51 _gnutls_gen_srp_client_kx,
52 NULL,
53 NULL,
55 _gnutls_proc_crt,
56 NULL, /* certificate */
57 proc_srp_cert_server_kx,
58 _gnutls_proc_srp_client_kx,
59 NULL,
60 NULL
63 const mod_auth_st srp_dss_auth_struct = {
64 "SRP",
65 _gnutls_gen_cert_server_crt,
66 NULL,
67 gen_srp_cert_server_kx,
68 _gnutls_gen_srp_client_kx,
69 NULL,
70 NULL,
72 _gnutls_proc_crt,
73 NULL, /* certificate */
74 proc_srp_cert_server_kx,
75 _gnutls_proc_srp_client_kx,
76 NULL,
77 NULL
80 static int
81 gen_srp_cert_server_kx (gnutls_session_t session, gnutls_buffer_st* data)
83 ssize_t ret;
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);
94 if (ret < 0)
95 return ret;
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);
102 if (cred == NULL)
104 gnutls_assert ();
105 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
108 /* find the appropriate certificate */
109 if ((ret =
110 _gnutls_get_selected_cert (session, &apr_cert_list,
111 &apr_cert_list_length, &apr_pkey)) < 0)
113 gnutls_assert ();
114 return ret;
117 if ((ret =
118 _gnutls_handshake_sign_data (session, &apr_cert_list[0],
119 apr_pkey, &ddata, &signature,
120 &sign_algo)) < 0)
122 gnutls_assert ();
123 return ret;
126 if (_gnutls_version_has_selectable_sighash (ver))
128 const sign_algorithm_st *aid;
129 uint8_t p[2];
131 if (sign_algo == GNUTLS_SIGN_UNKNOWN)
133 ret = GNUTLS_E_UNKNOWN_ALGORITHM;
134 goto cleanup;
137 aid = _gnutls_sign_to_tls_aid (sign_algo);
138 if (aid == NULL)
140 gnutls_assert();
141 ret = GNUTLS_E_UNKNOWN_ALGORITHM;
142 goto cleanup;
145 p[0] = aid->hash_algorithm;
146 p[1] = aid->sign_algorithm;
148 ret = _gnutls_buffer_append_data(data, p, 2);
149 if (ret < 0)
151 gnutls_assert();
152 goto cleanup;
156 ret = _gnutls_buffer_append_data_prefix( data, 16, signature.data, signature.size);
158 if (ret < 0)
160 gnutls_assert();
161 goto cleanup;
164 ret = data->length;
166 cleanup:
167 _gnutls_free_datum (&signature);
168 return ret;
171 static int
172 proc_srp_cert_server_kx (gnutls_session_t session, uint8_t * data,
173 size_t _data_size)
175 ssize_t ret;
176 int sigsize;
177 gnutls_datum_t vparams, signature;
178 ssize_t data_size;
179 cert_auth_info_t info;
180 gnutls_pcert_st peer_cert;
181 uint8_t *p;
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);
186 if (ret < 0)
187 return ret;
189 data_size = _data_size - ret;
191 info = _gnutls_get_auth_info (session);
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 /* VERIFY SIGNATURE */
201 vparams.size = ret; /* all the data minus the signature */
202 vparams.data = data;
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);
217 gnutls_assert ();
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;
229 ret =
230 _gnutls_get_auth_info_pcert (&peer_cert,
231 session->security_parameters.cert_type,
232 info);
234 if (ret < 0)
236 gnutls_assert ();
237 return ret;
240 ret =
241 _gnutls_handshake_verify_data (session, &peer_cert, &vparams, &signature,
242 sign_algo);
244 gnutls_pcert_deinit (&peer_cert);
245 if (ret < 0)
247 gnutls_assert ();
248 return ret;
251 return 0;
255 #endif /* ENABLE_SRP */