2 * Copyright (C) 2005-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 /* Functions for operating in an PSK passwd file are included here */
25 #include <gnutls_int.h>
30 #include "gnutls_errors.h"
31 #include <auth/psk_passwd.h>
33 #include "gnutls_auth.h"
34 #include "gnutls_dh.h"
36 #include <gnutls_str.h>
37 #include <gnutls_datum.h>
38 #include <gnutls_num.h>
42 /* this function parses passwd.psk file. Format is:
43 * string(username):hex(passwd)
46 pwd_put_values (gnutls_datum_t
* psk
, char *str
)
52 p
= strchr (str
, ':');
56 return GNUTLS_E_SRP_PWD_PARSING_ERROR
;
68 if (p
[len
- 1] == '\n' || p
[len
- 1] == ' ')
71 size
= psk
->size
= len
/ 2;
72 psk
->data
= gnutls_malloc (size
);
73 if (psk
->data
== NULL
)
76 return GNUTLS_E_MEMORY_ERROR
;
79 ret
= _gnutls_hex2bin (p
, len
, psk
->data
, &size
);
80 psk
->size
= (unsigned int) size
;
93 /* Randomizes the given password entry. It actually sets a random password.
94 * Returns 0 on success.
97 _randomize_psk (gnutls_datum_t
* psk
)
101 psk
->data
= gnutls_malloc (16);
102 if (psk
->data
== NULL
)
105 return GNUTLS_E_MEMORY_ERROR
;
110 ret
= _gnutls_rnd (GNUTLS_RND_NONCE
, (char *) psk
->data
, 16);
120 /* Returns the PSK key of the given user.
121 * If the user doesn't exist a random password is returned instead.
124 _gnutls_psk_pwd_find_entry (gnutls_session_t session
, char *username
,
125 gnutls_datum_t
* psk
)
127 gnutls_psk_server_credentials_t cred
;
133 cred
= (gnutls_psk_server_credentials_t
)
134 _gnutls_get_cred (session
->key
, GNUTLS_CRD_PSK
, NULL
);
138 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
141 /* if the callback which sends the parameters is
144 if (cred
->pwd_callback
!= NULL
)
146 ret
= cred
->pwd_callback (session
, username
, psk
);
149 { /* the user does not exist */
150 ret
= _randomize_psk (psk
);
162 return GNUTLS_E_SRP_PWD_ERROR
;
168 /* The callback was not set. Proceed.
170 if (cred
->password_file
== NULL
)
173 return GNUTLS_E_SRP_PWD_ERROR
;
176 /* Open the selected password file.
178 fd
= fopen (cred
->password_file
, "r");
182 return GNUTLS_E_SRP_PWD_ERROR
;
185 len
= strlen (username
);
186 while (fgets (line
, sizeof (line
), fd
) != NULL
)
188 /* move to first ':' */
190 while ((line
[i
] != ':') && (line
[i
] != '\0') && (i
< sizeof (line
)))
195 if (strncmp (username
, line
, MAX (i
, len
)) == 0)
197 ret
= pwd_put_values (psk
, line
);
202 return GNUTLS_E_SRP_PWD_ERROR
;
209 /* user was not found. Fake him.
210 * the last index found and randomize the entry.
212 ret
= _randomize_psk (psk
);
224 #endif /* ENABLE PSK */