Improve.
[gnutls.git] / lib / ext_srp.c
blob40f4993487b2679e469cbe1e2267403aa33fa0ff
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005 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 #include <gnutls_int.h>
26 #include <ext_srp.h>
28 #ifdef ENABLE_SRP
30 #include "gnutls_auth.h"
31 #include "auth_srp.h"
32 #include "gnutls_errors.h"
33 #include "gnutls_algorithms.h"
34 #include <gnutls_num.h>
36 int
37 _gnutls_srp_recv_params (gnutls_session_t session, const opaque * data,
38 size_t _data_size)
40 uint8_t len;
41 ssize_t data_size = _data_size;
43 if (session->security_parameters.entity == GNUTLS_SERVER)
45 if (data_size > 0)
47 len = data[0];
48 DECR_LEN (data_size, len);
50 if (MAX_SRP_USERNAME < len)
52 gnutls_assert ();
53 return GNUTLS_E_ILLEGAL_SRP_USERNAME;
55 memcpy (session->security_parameters.extensions.srp_username,
56 &data[1], len);
57 /* null terminated */
58 session->security_parameters.extensions.srp_username[len] = 0;
61 return 0;
64 /* returns data_size or a negative number on failure
65 * data is allocated locally
67 int
68 _gnutls_srp_send_params (gnutls_session_t session, opaque * data,
69 size_t data_size)
71 unsigned len;
73 if (_gnutls_kx_priority (session, GNUTLS_KX_SRP) < 0 &&
74 _gnutls_kx_priority (session, GNUTLS_KX_SRP_DSS) < 0 &&
75 _gnutls_kx_priority (session, GNUTLS_KX_SRP_RSA) < 0)
77 /* algorithm was not allowed in this session
79 return 0;
82 /* this function sends the client extension data (username) */
83 if (session->security_parameters.entity == GNUTLS_CLIENT)
85 gnutls_srp_client_credentials_t cred = (gnutls_srp_client_credentials_t)
86 _gnutls_get_cred (session->key, GNUTLS_CRD_SRP, NULL);
88 if (cred == NULL)
89 return 0;
91 if (cred->username != NULL)
92 { /* send username */
93 len = MIN (strlen (cred->username), 255);
95 if (data_size < len + 1)
97 gnutls_assert ();
98 return GNUTLS_E_SHORT_MEMORY_BUFFER;
101 data[0] = (uint8_t) len;
102 memcpy (&data[1], cred->username, len);
103 return len + 1;
105 else if (cred->get_function != NULL)
107 /* Try the callback
109 char *username = NULL, *password = NULL;
111 if (cred->get_function (session, &username, &password) < 0
112 || username == NULL || password == NULL)
114 gnutls_assert ();
115 return GNUTLS_E_ILLEGAL_SRP_USERNAME;
118 len = MIN (strlen (username), 255);
120 if (data_size < len + 1)
122 gnutls_free (username);
123 gnutls_free (password);
124 gnutls_assert ();
125 return GNUTLS_E_SHORT_MEMORY_BUFFER;
128 session->internals.srp_username = username;
129 session->internals.srp_password = password;
131 data[0] = (uint8_t) len;
132 memcpy (&data[1], username, len);
133 return len + 1;
136 return 0;
139 #endif /* ENABLE_SRP */