Guile: Fix `x509-certificate-dn-oid' and related functions.
[gnutls.git] / lib / auth_psk_passwd.c
blob0e4ec44ce74da94d3d9b35ba0401b758756768fc
1 /*
2 * Copyright (C) 2005, 2007 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 /* Functions for operating in an PSK passwd file are included here */
27 #include <gnutls_int.h>
29 #ifdef ENABLE_PSK
31 #include "x509_b64.h"
32 #include "gnutls_errors.h"
33 #include <auth_psk_passwd.h>
34 #include "auth_psk.h"
35 #include "gnutls_auth_int.h"
36 #include "gnutls_dh.h"
37 #include "debug.h"
38 #include <gnutls_str.h>
39 #include <gnutls_datum.h>
40 #include <gnutls_num.h>
43 /* this function parses passwd.psk file. Format is:
44 * string(username):hex(passwd)
46 static int
47 pwd_put_values (gnutls_datum_t * psk, char *str)
49 char *p;
50 int len, ret;
51 size_t size;
53 p = strchr (str, ':');
54 if (p == NULL)
56 gnutls_assert ();
57 return GNUTLS_E_SRP_PWD_PARSING_ERROR;
60 *p = '\0';
61 p++;
63 /* skip username
66 /* read the key
68 len = strlen (p);
69 if (p[len - 1] == '\n' || p[len - 1] == ' ')
70 len--;
72 size = psk->size = len / 2;
73 psk->data = gnutls_malloc (size);
74 if (psk->data == NULL)
76 gnutls_assert ();
77 return GNUTLS_E_MEMORY_ERROR;
80 ret = _gnutls_hex2bin ((opaque *) p, len, psk->data, &size);
81 psk->size = (unsigned int)size;
82 if (ret < 0)
84 gnutls_assert ();
85 return ret;
89 return 0;
94 /* Randomizes the given password entry. It actually sets a random password.
95 * Returns 0 on success.
97 static int
98 _randomize_psk (gnutls_datum_t * psk)
100 psk->data = gnutls_malloc (16);
101 if (psk->data == NULL)
103 gnutls_assert ();
104 return GNUTLS_E_MEMORY_ERROR;
107 psk->size = 16;
108 if (gc_nonce ((char *) psk->data, 16) != GC_OK)
110 gnutls_assert ();
111 return GNUTLS_E_RANDOM_FAILED;
114 return 0;
117 /* Returns the PSK key of the given user.
118 * If the user doesn't exist a random password is returned instead.
121 _gnutls_psk_pwd_find_entry (gnutls_session_t session, char *username,
122 gnutls_datum_t * psk)
124 gnutls_psk_server_credentials_t cred;
125 FILE *fd;
126 char line[2 * 1024];
127 unsigned i, len;
128 int ret;
130 cred = (gnutls_psk_server_credentials_t)
131 _gnutls_get_cred (session->key, GNUTLS_CRD_PSK, NULL);
132 if (cred == NULL)
134 gnutls_assert ();
135 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
138 /* if the callback which sends the parameters is
139 * set, use it.
141 if (cred->pwd_callback != NULL)
143 ret = cred->pwd_callback (session, username, psk);
145 if (ret == 1)
146 { /* the user does not exist */
147 ret = _randomize_psk (psk);
148 if (ret < 0)
150 gnutls_assert ();
151 return ret;
153 return 0;
156 if (ret < 0)
158 gnutls_assert ();
159 return GNUTLS_E_SRP_PWD_ERROR;
162 return 0;
165 /* The callback was not set. Proceed.
167 if (cred->password_file == NULL)
169 gnutls_assert ();
170 return GNUTLS_E_SRP_PWD_ERROR;
173 /* Open the selected password file.
175 fd = fopen (cred->password_file, "r");
176 if (fd == NULL)
178 gnutls_assert ();
179 return GNUTLS_E_SRP_PWD_ERROR;
182 len = strlen (username);
183 while (fgets (line, sizeof (line), fd) != NULL)
185 /* move to first ':' */
186 i = 0;
187 while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line)))
189 i++;
192 if (strncmp (username, line, MAX (i, len)) == 0)
194 ret = pwd_put_values (psk, line);
195 if (ret < 0)
197 gnutls_assert ();
198 return GNUTLS_E_SRP_PWD_ERROR;
200 return 0;
204 /* user was not found. Fake him.
205 * the last index found and randomize the entry.
207 ret = _randomize_psk (psk);
208 if (ret < 0)
210 gnutls_assert ();
211 return ret;
214 return 0;
219 #endif /* ENABLE PSK */