Line data Source code
1 : /**
2 : * The MIT License (MIT)
3 : *
4 : * Copyright (c) 2021 RSK Labs Ltd
5 : *
6 : * Permission is hereby granted, free of charge, to any person obtaining a copy
7 : * of this software and associated documentation files (the "Software"), to
8 : * deal in the Software without restriction, including without limitation the
9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 : * sell copies of the Software, and to permit persons to whom the Software is
11 : * furnished to do so, subject to the following conditions:
12 : *
13 : * The above copyright notice and this permission notice shall be included in
14 : * all copies or substantial portions of the Software.
15 : *
16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 : * IN THE SOFTWARE.
23 : */
24 :
25 : #include <sys/stat.h>
26 : #include <ctype.h>
27 : #include <stdio.h>
28 : #include <string.h>
29 : #include "log.h"
30 : #include "keyvalue_store.h"
31 :
32 : #define KVSTORE_PREFIX "./kvstore-"
33 : #define KVSTORE_SUFFIX ".dat"
34 : #define KVSTORE_MAX_KEY_LEN 150
35 :
36 : // Sanitizes a key by allowing only [a-zA-Z0-9]. If one or more invalid
37 : // characters are found, Replace them with a single hyphen.
38 76 : static void sanitize_key(char* key, char* sanitized_key) {
39 76 : if (!key || !sanitized_key)
40 0 : return;
41 :
42 76 : size_t key_len = strlen(key);
43 :
44 : // Truncate key if it's too long
45 76 : if (key_len > KVSTORE_MAX_KEY_LEN) {
46 0 : key_len = KVSTORE_MAX_KEY_LEN;
47 : }
48 :
49 76 : bool prev_char_valid = false;
50 76 : size_t sanitized_key_len = 0;
51 1182 : for (size_t i = 0; i < key_len; i++) {
52 1106 : if (isalnum(key[i])) {
53 880 : sanitized_key[sanitized_key_len++] = key[i];
54 880 : prev_char_valid = true;
55 226 : } else if (prev_char_valid) {
56 106 : sanitized_key[sanitized_key_len++] = '-';
57 106 : prev_char_valid = false;
58 : }
59 : }
60 76 : sanitized_key[sanitized_key_len] = '\0';
61 : }
62 :
63 76 : static char* filename_for(char* key) {
64 : char sanitized_key[KVSTORE_MAX_KEY_LEN + 1];
65 76 : sanitize_key(key, sanitized_key);
66 76 : size_t filename_size =
67 76 : strlen(KVSTORE_PREFIX) + strlen(KVSTORE_SUFFIX) + strlen(sanitized_key);
68 76 : char* filename = malloc(filename_size + 1);
69 76 : strcpy(filename, "");
70 76 : strcat(filename, KVSTORE_PREFIX);
71 76 : strcat(filename, sanitized_key);
72 76 : strcat(filename, KVSTORE_SUFFIX);
73 76 : return filename;
74 : }
75 :
76 74 : static FILE* open_file_for(char* key, char* mode, size_t* file_size) {
77 74 : char* filename = filename_for(key);
78 : struct stat fst;
79 74 : stat(filename, &fst);
80 74 : if (file_size)
81 51 : *file_size = fst.st_size;
82 74 : FILE* file = fopen(filename, mode);
83 74 : free(filename);
84 74 : return file;
85 : }
86 :
87 23 : bool kvstore_save(char* key, uint8_t* data, size_t data_size) {
88 23 : LOG("Attempting to write data for %s...\n", key);
89 23 : if (!data_size) {
90 0 : LOG("Invalid zero-length data given for key <%s>\n", key);
91 0 : return false;
92 : }
93 :
94 23 : FILE* file = open_file_for(key, "wb", NULL);
95 23 : if (!file) {
96 0 : LOG("Could not open file for key <%s>\n", key);
97 0 : return false;
98 : }
99 :
100 23 : if (fwrite(data, sizeof(data[0]), data_size, file) != data_size) {
101 0 : LOG("Error writing secret payload for key <%s>\n", key);
102 0 : fclose(file);
103 0 : return false;
104 : };
105 :
106 23 : fclose(file);
107 23 : return true;
108 : }
109 :
110 33 : bool kvstore_exists(char* key) {
111 33 : LOG("Attempting to determine existence for key <%s>...\n", key);
112 33 : size_t file_size = 0;
113 33 : FILE* file = open_file_for(key, "rb", &file_size);
114 33 : if (file) {
115 26 : fclose(file);
116 26 : return true;
117 : }
118 7 : return false;
119 : }
120 :
121 18 : size_t kvstore_get(char* key, uint8_t* data_buf, size_t buffer_size) {
122 18 : LOG("Attempting to read data for key <%s>...\n", key);
123 18 : size_t file_size = 0;
124 18 : FILE* file = open_file_for(key, "rb", &file_size);
125 18 : if (!file) {
126 0 : LOG("Could not open file for key <%s>\n", key);
127 0 : return 0;
128 : }
129 :
130 18 : if (file_size > buffer_size) {
131 0 : LOG("Payload too big for destination for key <%s>\n", key);
132 0 : fclose(file);
133 0 : return 0;
134 : }
135 :
136 18 : if (!file_size) {
137 0 : LOG("Invalid zero-length secret stored for key <%s>\n", key);
138 0 : fclose(file);
139 0 : return 0;
140 : }
141 :
142 18 : if (fread(data_buf, sizeof(data_buf[0]), file_size, file) != file_size) {
143 0 : LOG("Could not read payload for key <%s>\n", key);
144 0 : fclose(file);
145 0 : return 0;
146 : };
147 :
148 18 : fclose(file);
149 18 : return file_size;
150 : }
151 :
152 2 : bool kvstore_remove(char* key) {
153 2 : char* filename = filename_for(key);
154 2 : int result = remove(filename);
155 2 : if (result)
156 0 : LOG("Error removing file for key <%s>\n", key);
157 2 : free(filename);
158 2 : return !result;
159 : }
|