Use libtasn1 v2.4.
[gnutls.git] / lib / ext_srp.c
blobc6ded6eb52469aa772cc3bb8c07d55ac3e629424
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2010 Free Software
3 * Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
26 #include <gnutls_int.h>
27 #include <ext_srp.h>
29 #ifdef ENABLE_SRP
31 #include "gnutls_auth.h"
32 #include "auth_srp.h"
33 #include "gnutls_errors.h"
34 #include "gnutls_algorithms.h"
35 #include <gnutls_num.h>
37 int
38 _gnutls_srp_recv_params (gnutls_session_t session, const opaque * data,
39 size_t _data_size)
41 uint8_t len;
42 ssize_t data_size = _data_size;
44 if (session->security_parameters.entity == GNUTLS_SERVER)
46 if (data_size > 0)
48 len = data[0];
49 DECR_LEN (data_size, len);
51 if (MAX_SRP_USERNAME < len)
53 gnutls_assert ();
54 return GNUTLS_E_ILLEGAL_SRP_USERNAME;
56 memcpy (session->security_parameters.extensions.srp_username,
57 &data[1], len);
58 /* null terminated */
59 session->security_parameters.extensions.srp_username[len] = 0;
62 return 0;
65 /* returns data_size or a negative number on failure
66 * data is allocated locally
68 int
69 _gnutls_srp_send_params (gnutls_session_t session, opaque * data,
70 size_t data_size)
72 unsigned len;
74 if (_gnutls_kx_priority (session, GNUTLS_KX_SRP) < 0 &&
75 _gnutls_kx_priority (session, GNUTLS_KX_SRP_DSS) < 0 &&
76 _gnutls_kx_priority (session, GNUTLS_KX_SRP_RSA) < 0)
78 /* algorithm was not allowed in this session
80 return 0;
83 /* this function sends the client extension data (username) */
84 if (session->security_parameters.entity == GNUTLS_CLIENT)
86 gnutls_srp_client_credentials_t cred = (gnutls_srp_client_credentials_t)
87 _gnutls_get_cred (session->key, GNUTLS_CRD_SRP, NULL);
89 if (cred == NULL)
90 return 0;
92 if (cred->username != NULL)
93 { /* send username */
94 len = MIN (strlen (cred->username), 255);
96 if (data_size < len + 1)
98 gnutls_assert ();
99 return GNUTLS_E_SHORT_MEMORY_BUFFER;
102 data[0] = (uint8_t) len;
103 memcpy (&data[1], cred->username, len);
104 return len + 1;
106 else if (cred->get_function != NULL)
108 /* Try the callback
110 char *username = NULL, *password = NULL;
112 if (cred->get_function (session, &username, &password) < 0
113 || username == NULL || password == NULL)
115 gnutls_assert ();
116 return GNUTLS_E_ILLEGAL_SRP_USERNAME;
119 len = MIN (strlen (username), 255);
121 if (data_size < len + 1)
123 gnutls_free (username);
124 gnutls_free (password);
125 gnutls_assert ();
126 return GNUTLS_E_SHORT_MEMORY_BUFFER;
129 session->internals.srp_username = username;
130 session->internals.srp_password = password;
132 data[0] = (uint8_t) len;
133 memcpy (&data[1], username, len);
134 return len + 1;
137 return 0;
140 #endif /* ENABLE_SRP */