Browse Source
put most of the parts for simple signup/login in place...not tested now as it is late. Hopefully tomorrow I find the time to setup a small testform and bring this whole thing finally to work. refs #36
release0.1.5
put most of the parts for simple signup/login in place...not tested now as it is late. Hopefully tomorrow I find the time to setup a small testform and bring this whole thing finally to work. refs #36
release0.1.5
14 changed files with 570 additions and 6 deletions
-
3include/application/application.h
-
1include/auth.h
-
49include/auth/storage.h
-
11include/storage.h
-
74src/application/adapter/http/update.c
-
12src/application/application.c
-
78src/application/signup.c
-
103src/auth/storage/hash_pw.c
-
53src/auth/storage/signup.c
-
95src/auth/storage/storage.c
-
23src/storage/put.c
-
50src/storage/update.c
-
5src/user/save.c
-
19src/utils/hash.c
@ -0,0 +1,49 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef __AUTH_STORAGE_H__ |
|||
#define __AUTH_STORAGE_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include <openssl/sha.h> |
|||
|
|||
#include "class.h" |
|||
|
|||
|
|||
#define SALT_SIZE 32 |
|||
#define HASH_SIZE SHA512_DIGEST_LENGTH |
|||
|
|||
|
|||
CLASS(AuthStorage) { |
|||
Storage store; |
|||
}; |
|||
|
|||
/* |
|||
* @TODO In future this should use a more general purpose hash |
|||
* function, which then will be in utils/hash.c |
|||
*/ |
|||
int hash_pw(const char *, const size_t, unsigned char *, unsigned char **); |
|||
|
|||
#endif // __AUTH_STORAGE_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,78 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#define _GNU_SOURCE |
|||
|
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
#include "auth.h" |
|||
#include "user.h" |
|||
#include "application/application.h" |
|||
|
|||
#include "utils/memory.h" |
|||
|
|||
int |
|||
applicationSignup( |
|||
Application this, |
|||
Credential credential, |
|||
User user, |
|||
Session session) |
|||
{ |
|||
unsigned char hash[SALT_SIZE+HASH_SIZE]; |
|||
|
|||
if (NULL != userLoad(user, this->users)) { |
|||
/* |
|||
* if any user is found with this email return false |
|||
* as on signup equal email adresses are not allowed |
|||
* at all. |
|||
*/ |
|||
return 0; |
|||
} |
|||
|
|||
userSave(user, this->users); |
|||
|
|||
if (FALSE == hash_pw( |
|||
CRED_PWD(cred).pass, |
|||
CRED_PWD(cred).npass, |
|||
&hash, |
|||
&(hash+SALT_SIZE))) { |
|||
/* |
|||
* @TODO if we come here we have to delete the previously saved |
|||
* user again... |
|||
*/ |
|||
return 0; |
|||
} |
|||
|
|||
storagePut( |
|||
this->passwords, |
|||
CRED_PWD(cred).user, |
|||
CRED_PWD(cred).nuser, |
|||
hash, |
|||
SALT_SIZE + HASH_SIZE); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,103 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include <openssl/evp.h> |
|||
#include <openssl/rand.h> |
|||
|
|||
#include "class.h" |
|||
#include "storage.h" |
|||
#include "utils/memory.h" |
|||
|
|||
/* |
|||
* I have to hash the passwords, maybe this will move in |
|||
* a separate class in future, but now everything is done |
|||
* here |
|||
*/ |
|||
#define PBKDF2_ITERATIONS 2048 |
|||
|
|||
/* |
|||
* base64 decode via openssl... |
|||
* I do not need this i think, but I keep it...maybe I have |
|||
* use for it later. |
|||
* |
|||
#include <openssl/bio.h> |
|||
#include <openssl/evp.h> |
|||
|
|||
#define B64_SALT "q36MilkD6Ezlt6+G394aPYWrSwAdEhdnK8k=" |
|||
|
|||
BIO_METHOD * BIO_f_base64(void); |
|||
|
|||
void |
|||
base64decode(char * data) { |
|||
BIO * bio, |
|||
* b64; |
|||
FILE * b64_salt = fmemopen(B64_SALT, sizeof(B64_SALT)-1, "r"); |
|||
|
|||
b64 = BIO_new(BIO_f_base64()); |
|||
bio = BIO_new_fp(b64_salt, BIO_NOCLOSE); |
|||
bio = BIO_push(b64, bio); |
|||
|
|||
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); |
|||
|
|||
if (SALT_SIZE != BIO_read(bio, data, SALT_SIZE)) { |
|||
return -1; |
|||
} |
|||
|
|||
BIO_free_all(bio); |
|||
fclose(b64_salt); |
|||
} |
|||
*/ |
|||
|
|||
int |
|||
hash_pw( |
|||
const char * password, |
|||
const size_t npassword, |
|||
unsigned char * hash, |
|||
unsigned char ** salt) |
|||
{ |
|||
if (NULL == *salt) { |
|||
*salt = memMalloc(SALT_SIZE * sizeof(unsigned char)); |
|||
if (0 > RAND_pseudo_bytes(unsigned char *buf, int num)) { |
|||
MEM_FREE(*salt); |
|||
return FALSE; |
|||
} |
|||
} |
|||
|
|||
if (0 == PKCS5_PBKDF2_HMAC( |
|||
password, |
|||
npassword, |
|||
*salt, |
|||
SALT_SIZE, |
|||
PBKDF2_ITERATIONS, |
|||
EVP_sha512(), |
|||
HASH_SIZE, |
|||
hash)) { |
|||
MEM_FREE(*salt); |
|||
return FALSE; |
|||
} |
|||
|
|||
return TRUE; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,53 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
#include "storage.h" |
|||
#include "auth/storage.h" |
|||
#include "auth/credential.h" |
|||
|
|||
int |
|||
authStorageSignup(AuthStorage this, Credential cred) |
|||
{ |
|||
unsigned char hash[SALT_SIZE+HASH_SIZE]; |
|||
|
|||
if (FALSE == hash_pw( |
|||
CRED_PWD(cred).pass, |
|||
CRED_PWD(cred).npass, |
|||
&hash, |
|||
&(hash+SALT_SIZE))) { |
|||
return 0; |
|||
} |
|||
|
|||
storagePut( |
|||
this->store, |
|||
CRED_PWD(cred).user, |
|||
CRED_PWD(cred).nuser, |
|||
hash, |
|||
SALT_SIZE + HASH_SIZE); |
|||
|
|||
return 1; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,95 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include "class.h" |
|||
#include "storage.h" |
|||
#include "auth/storage.h" |
|||
#include "auth/credential.h" |
|||
#include "commons.h" |
|||
|
|||
static |
|||
int |
|||
authStorageCtor(void * _this, va_list * params) |
|||
{ |
|||
AuthStorage this = _this; |
|||
|
|||
this->store = va_arg(*params, Storage); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
authStorageDtor(void * _this) |
|||
{ |
|||
} |
|||
|
|||
static |
|||
int |
|||
authStorageAuthenticate(void * _this, Credential cred) |
|||
{ |
|||
AuthStorage this = _this; |
|||
|
|||
unsigned char current_hash[HASH_SIZE]; |
|||
unsigned char * found_hash = NULL; |
|||
size_t nfound_hash = 0; |
|||
|
|||
if (CRED_PASSWORD != cred->type) { |
|||
return FALSE; |
|||
} |
|||
|
|||
storageGet( |
|||
this->store, |
|||
CRED_PWD(cred).user, |
|||
CRED_PWD(cred).nuser, |
|||
&found_hash, |
|||
&nfound_hash); |
|||
|
|||
if (NULL == found_hash || (SALT_SIZE + HASH_SIZE) != nfound_hash) { |
|||
/* user not found or found hash is invalid */ |
|||
return FALSE; |
|||
} |
|||
|
|||
/* found_hash <=> salt+hash */ |
|||
if (FALSE == hash_pw( |
|||
CRED_PWD(cred).pass, |
|||
CRED_PWD(cred).npass, |
|||
current_hash, |
|||
&found_hash)) { |
|||
MEM_FREE(found_hash); |
|||
return FALSE; |
|||
} |
|||
|
|||
if (0 != memcmp(current_hash, found_hash+SALT_SIZE, HASH_SIZE)) { |
|||
MEM_FREE(found_hash); |
|||
return FALSE; |
|||
} |
|||
|
|||
MEM_FREE(found_hash); |
|||
return TRUE; |
|||
} |
|||
|
|||
INIT_IFACE(Class, authStorageCtor, authStorageDtor, NULL); |
|||
INIT_IFACE(Auth, authStorageAuthenticate); |
|||
CREATE_CLASS(AuthLdap, NULL, IFACE(Class), IFACE(Auth)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,50 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <gdbm.h> |
|||
#include <string.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "storage.h" |
|||
#include "class.h" |
|||
|
|||
#include "utils/memory.h" |
|||
|
|||
StoragePutResult |
|||
storagePut(Storage this, char * _key, size_t nkey, char * data, size_t ndata) |
|||
{ |
|||
datum key = {_key, nkey}; |
|||
datum value = {data, ndata}; |
|||
|
|||
switch (gdbm_store(this->gdbm, key, value, GDBM_REPLACE)) { |
|||
case 0: |
|||
return SPR_OK; |
|||
case -1: |
|||
return SPR_READ_ONLY; |
|||
default: |
|||
return SPR_UNKNOWN; |
|||
} |
|||
|
|||
return SPR_UNKNOWN; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue