2 * This file is part of DisOrder
3 * Copyright (C) 2007, 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/cookies.c
21 * @brief Cookie support
37 #include "configuration.h"
41 /** @brief Hash function used in signing HMAC */
42 #define ALGO GCRY_MD_SHA1
44 /** @brief Size of key to use */
47 /** @brief Signing key */
48 static uint8_t signing_key[HASHSIZE];
50 /** @brief Previous signing key */
51 static uint8_t old_signing_key[HASHSIZE];
53 /** @brief Signing key validity limit or 0 if none */
54 static time_t signing_key_validity_limit;
56 /** @brief Hash of revoked cookies */
59 /** @brief Callback to expire revocation list */
60 static int revoked_cleanup_callback(const char *key, void *value,
62 if(*(time_t *)value < *(time_t *)u)
63 hash_remove(revoked, key);
67 /** @brief Generate a new key */
68 static void newkey(void) {
72 memcpy(old_signing_key, signing_key, HASHSIZE);
73 gcry_randomize(signing_key, HASHSIZE, GCRY_STRONG_RANDOM);
74 signing_key_validity_limit = now + config->cookie_key_lifetime;
75 /* Now is a good time to clean up the revocation list... */
77 hash_foreach(revoked, revoked_cleanup_callback, &now);
80 /** @brief Base64 mapping table for cookies
82 * Stupid Safari cannot cope with quoted cookies, so cookies had better not
83 * need quoting. We use $ to separate the parts of the cookie and +%# to where
84 * MIME uses +/=; see @ref base64.c. See http_separator() for the characters
87 static const char cookie_base64_table[] =
88 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+%#";
90 /** @brief Sign @p subject with @p key and return the base64 of the result
91 * @param key Key to sign with (@ref HASHSIZE bytes)
92 * @param subject Subject string
93 * @return Base64-encoded signature or NULL
95 static char *sign(const uint8_t *key,
96 const char *subject) {
102 if((e = gcry_md_open(&h, ALGO, GCRY_MD_FLAG_HMAC))) {
103 error(0, "gcry_md_open: %s", gcry_strerror(e));
106 if((e = gcry_md_setkey(h, key, HASHSIZE))) {
107 error(0, "gcry_md_setkey: %s", gcry_strerror(e));
111 gcry_md_write(h, subject, strlen(subject));
112 sig = gcry_md_read(h, ALGO);
113 sig64 = generic_to_base64(sig, HASHSIZE, cookie_base64_table);
118 /** @brief Create a login cookie
119 * @param user Username
120 * @return Cookie or NULL
122 char *make_cookie(const char *user) {
123 const char *password;
125 char *b, *bp, *c, *g;
127 /* dollar signs aren't allowed in usernames */
128 if(strchr(user, '$')) {
129 error(0, "make_cookie for username with dollar sign");
132 /* look up the password */
133 password = trackdb_get_password(user);
135 error(0, "make_cookie for nonexistent user");
138 /* make sure we have a valid signing key */
140 if(now >= signing_key_validity_limit)
142 /* construct the subject */
143 byte_xasprintf(&b, "%jx$%s$", (intmax_t)now + config->cookie_login_lifetime,
144 urlencodestring(user));
145 byte_xasprintf(&bp, "%s%s", b, password);
147 if(!(g = sign(signing_key, bp)))
149 /* put together the final cookie */
150 byte_xasprintf(&c, "%s%s", b, g);
154 /** @brief Verify a cookie
155 * @param cookie Cookie to verify
156 * @param rights Where to store rights value
157 * @return Verified user or NULL
159 char *verify_cookie(const char *cookie, rights_type *rights) {
163 char *user, *bp, *sig;
164 const char *password;
167 /* check the revocation list */
168 if(revoked && hash_find(revoked, cookie)) {
169 error(0, "attempt to log in with revoked cookie");
172 /* parse the cookie */
174 t = strtoimax(cookie, &c1, 16);
176 error(errno, "error parsing cookie timestamp");
180 error(0, "invalid cookie timestamp");
183 /* There'd better be two dollar signs */
184 c2 = strchr(c1 + 1, '$');
186 error(0, "invalid cookie syntax");
189 /* Extract the username */
190 user = xstrndup(c1 + 1, c2 - (c1 + 1));
194 error(0, "cookie has expired");
197 /* look up the password */
198 k = trackdb_getuserinfo(user);
200 error(0, "verify_cookie for nonexistent user");
203 password = kvp_get(k, "password");
204 if(!password) password = "";
205 if(parse_rights(kvp_get(k, "rights"), rights, 1))
207 /* construct the expected subject. We re-encode the timestamp and the
209 byte_xasprintf(&bp, "%jx$%s$%s", t, urlencodestring(user), password);
210 /* Compute the expected signature. NB we base64 the expected signature and
211 * compare that rather than exposing our base64 parser to the cookie. */
212 if(!(sig = sign(signing_key, bp)))
214 if(!strcmp(sig, c2 + 1))
216 /* that didn't match, try the old key */
217 if(!(sig = sign(old_signing_key, bp)))
219 if(!strcmp(sig, c2 + 1))
221 /* that didn't match either */
222 error(0, "cookie signature does not match");
226 /** @brief Revoke a cookie
227 * @param cookie Cookie to revoke
229 * Further attempts to log in with @p cookie will fail.
231 void revoke_cookie(const char *cookie) {
235 /* find the cookie's expiry time */
237 when = (time_t)strtoimax(cookie, &ptr, 16);
238 /* reject bogus cookies */
243 /* make sure the revocation list exists */
245 revoked = hash_new(sizeof(time_t));
246 /* add the cookie to it; its value is the expiry time */
247 hash_add(revoked, cookie, &when, HASH_INSERT);