2 * This file is part of DisOrder
3 * Copyright (C) 2007 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
41 #include "configuration.h"
45 /** @brief Hash function used in signing HMAC */
46 #define ALGO GCRY_MD_SHA1
48 /** @brief Size of key to use */
51 /** @brief Signing key */
52 static uint8_t signing_key
[HASHSIZE
];
54 /** @brief Previous signing key */
55 static uint8_t old_signing_key
[HASHSIZE
];
57 /** @brief Signing key validity limit or 0 if none */
58 static time_t signing_key_validity_limit
;
60 /** @brief Hash of revoked cookies */
63 /** @brief Callback to expire revocation list */
64 static int revoked_cleanup_callback(const char *key
, void *value
,
66 if(*(time_t *)value
< *(time_t *)u
)
67 hash_remove(revoked
, key
);
71 /** @brief Generate a new key */
72 static void newkey(void) {
76 memcpy(old_signing_key
, signing_key
, HASHSIZE
);
77 gcry_randomize(signing_key
, HASHSIZE
, GCRY_STRONG_RANDOM
);
78 signing_key_validity_limit
= now
+ config
->cookie_key_lifetime
;
79 /* Now is a good time to clean up the revocation list... */
81 hash_foreach(revoked
, revoked_cleanup_callback
, &now
);
84 /** @brief Sign @p subject with @p key and return the base64 of the result
85 * @param key Key to sign with (@ref HASHSIZE bytes)
86 * @param subject Subject string
87 * @return Base64-encoded signature or NULL
89 static char *sign(const uint8_t *key
,
90 const char *subject
) {
96 if((e
= gcry_md_open(&h
, ALGO
, GCRY_MD_FLAG_HMAC
))) {
97 error(0, "gcry_md_open: %s", gcry_strerror(e
));
100 if((e
= gcry_md_setkey(h
, key
, HASHSIZE
))) {
101 error(0, "gcry_md_setkey: %s", gcry_strerror(e
));
105 gcry_md_write(h
, subject
, strlen(subject
));
106 sig
= gcry_md_read(h
, ALGO
);
107 sig64
= mime_to_base64(sig
, HASHSIZE
);
112 /** @brief Create a login cookie
113 * @param user Username
114 * @return Cookie or NULL
116 char *make_cookie(const char *user
) {
117 const char *password
;
119 char *b
, *bp
, *c
, *g
;
121 /* semicolons aren't allowed in usernames */
122 if(strchr(user
, ';')) {
123 error(0, "make_cookie for username with semicolon");
126 /* look up the password */
127 password
= trackdb_get_password(user
);
129 error(0, "make_cookie for nonexistent user");
132 /* make sure we have a valid signing key */
134 if(now
>= signing_key_validity_limit
)
136 /* construct the subject */
137 byte_xasprintf(&b
, "%jx;%s;", (intmax_t)now
+ config
->cookie_login_lifetime
,
138 urlencodestring(user
));
139 byte_xasprintf(&bp
, "%s%s", b
, password
);
141 if(!(g
= sign(signing_key
, bp
)))
143 /* put together the final cookie */
144 byte_xasprintf(&c
, "%s%s", b
, g
);
148 /** @brief Verify a cookie
149 * @param cookie Cookie to verify
150 * @return Verified user or NULL
152 char *verify_cookie(const char *cookie
) {
156 char *user
, *bp
, *sig
;
157 const char *password
;
159 /* check the revocation list */
160 if(revoked
&& hash_find(revoked
, cookie
)) {
161 error(0, "attempt to log in with revoked cookie");
164 /* parse the cookie */
166 t
= strtoimax(cookie
, &c1
, 16);
168 error(errno
, "error parsing cookie timestamp");
172 error(0, "invalid cookie timestamp");
175 /* There'd better be two semicolons */
176 c2
= strchr(c1
+ 1, ';');
178 error(0, "invalid cookie syntax");
181 /* Extract the username */
182 user
= xstrndup(c1
+ 1, c2
- (c1
+ 1));
186 error(0, "cookie has expired");
189 /* look up the password */
190 password
= trackdb_get_password(user
);
192 error(0, "verify_cookie for nonexistent user");
195 /* construct the expected subject. We re-encode the timestamp and the
197 byte_xasprintf(&bp
, "%jx;%s;%s", t
, urlencodestring(user
), password
);
198 /* Compute the expected signature. NB we base64 the expected signature and
199 * compare that rather than exposing our base64 parser to the cookie. */
200 if(!(sig
= sign(signing_key
, bp
)))
202 if(!strcmp(sig
, c2
+ 1))
204 /* that didn't match, try the old key */
205 if(!(sig
= sign(old_signing_key
, bp
)))
207 if(!strcmp(sig
, c2
+ 1))
209 /* that didn't match either */
210 error(0, "cookie signature does not match");
214 /** @brief Revoke a cookie
215 * @param cookie Cookie to revoke
217 * Further attempts to log in with @p cookie will fail.
219 void revoke_cookie(const char *cookie
) {
223 /* find the cookie's expiry time */
225 when
= (time_t)strtoimax(cookie
, &ptr
, 16);
226 /* reject bogus cookies */
231 /* make sure the revocation list exists */
233 revoked
= hash_new(sizeof(time_t));
234 /* add the cookie to it; its value is the expiry time */
235 hash_add(revoked
, cookie
, &when
, HASH_INSERT
);