Guile: Fix `x509-certificate-dn-oid' and related functions.
[gnutls.git] / lib / ext_srp.c
blobe8bd84e0c1bf630596869d0b2fbf315768d83f43
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation
4 * Author: Nikos Mavroyanopoulos
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_int.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 (_gnutls_kx_priority (session, GNUTLS_KX_SRP) < 0 &&
44 _gnutls_kx_priority (session, GNUTLS_KX_SRP_DSS) < 0 &&
45 _gnutls_kx_priority (session, GNUTLS_KX_SRP_RSA) < 0)
47 /* algorithm was not allowed in this session
49 return 0;
52 if (session->security_parameters.entity == GNUTLS_SERVER)
54 if (data_size > 0)
56 len = data[0];
57 DECR_LEN (data_size, len);
59 if (MAX_SRP_USERNAME < len)
61 gnutls_assert ();
62 return GNUTLS_E_ILLEGAL_SRP_USERNAME;
64 memcpy (session->security_parameters.extensions.srp_username,
65 &data[1], len);
66 /* null terminated */
67 session->security_parameters.extensions.srp_username[len] = 0;
70 return 0;
73 /* returns data_size or a negative number on failure
74 * data is allocated locally
76 int
77 _gnutls_srp_send_params (gnutls_session_t session, opaque * data,
78 size_t data_size)
80 unsigned len;
82 if (_gnutls_kx_priority (session, GNUTLS_KX_SRP) < 0 &&
83 _gnutls_kx_priority (session, GNUTLS_KX_SRP_DSS) < 0 &&
84 _gnutls_kx_priority (session, GNUTLS_KX_SRP_RSA) < 0)
86 /* algorithm was not allowed in this session
88 return 0;
91 /* this function sends the client extension data (username) */
92 if (session->security_parameters.entity == GNUTLS_CLIENT)
94 gnutls_srp_client_credentials_t cred = (gnutls_srp_client_credentials_t)
95 _gnutls_get_cred (session->key, GNUTLS_CRD_SRP, NULL);
97 if (cred == NULL)
98 return 0;
100 if (cred->username != NULL)
101 { /* send username */
102 len = MIN (strlen (cred->username), 255);
104 if (data_size < len + 1)
106 gnutls_assert ();
107 return GNUTLS_E_SHORT_MEMORY_BUFFER;
110 data[0] = (uint8_t) len;
111 memcpy (&data[1], cred->username, len);
112 return len + 1;
114 else if (cred->get_function != NULL)
116 /* Try the callback
118 char *username = NULL, *password = NULL;
120 if (cred->get_function (session,
121 session->internals.handshake_restarted,
122 &username, &password) < 0
123 || username == NULL || password == NULL)
126 if (session->internals.handshake_restarted)
128 gnutls_assert ();
129 return GNUTLS_E_ILLEGAL_SRP_USERNAME;
132 return 0;
135 len = MIN (strlen (username), 255);
137 if (data_size < len + 1)
139 gnutls_free (username);
140 gnutls_free (password);
141 gnutls_assert ();
142 return GNUTLS_E_SHORT_MEMORY_BUFFER;
145 session->internals.srp_username = username;
146 session->internals.srp_password = password;
148 data[0] = (uint8_t) len;
149 memcpy (&data[1], username, len);
150 return len + 1;
153 return 0;
156 #endif /* ENABLE_SRP */