/
db.c
67 lines (57 loc) · 1.93 KB
/
db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2017 Cossack Labs Limited
*
* This file is a part of Hermes-core.
*
* Hermes-core is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hermes-core 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hermes-core. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <hermes/credential_store/db.h>
#include <string.h>
#include "../../utils/utils.h"
#include <assert.h>
#include <stdio.h>
#define CREDENTIAL_STORE_PATH "db/credential_store"
#define CS_SUCCESS 0
#define CS_FAIL 1
typedef struct db_type{
char path[1024];
}db_t;
uint32_t db_get_public_key(void* cs, const uint8_t* user_id, const size_t user_id_length, uint8_t** pub_key, size_t* pub_key_length){
char fpath[10*1024];
fprintf(stdout, "get public key for user_id=<%.*s>\n", (int)user_id_length, user_id);
db_t* db = (db_t*)cs;
BUILD_TYPED_PATH(fpath, C(db->path), E(user_id, user_id_length));
return read_whole_file(fpath, pub_key, pub_key_length);
}
hm_cs_db_t* db_create(void){
hm_cs_db_t* cs=calloc(1, sizeof(hm_cs_db_t));
assert(cs);
cs->user_data=calloc(1, sizeof(db_t));
assert(cs->user_data);
cs->get_pub=db_get_public_key;
db_t* db = (db_t*)(((hm_cs_db_t*)cs)->user_data);
memcpy(db->path, CREDENTIAL_STORE_PATH, strlen(CREDENTIAL_STORE_PATH)+1);
create_directory(((db_t*)(((hm_cs_db_t*)cs)->user_data))->path);
return cs;
}
uint32_t db_destroy(hm_cs_db_t** cs){
if(!cs || !(*cs)){
return CS_FAIL;
}
free((*cs)->user_data);
free(*cs);
*cs=NULL;
return CS_SUCCESS;
}