LCOV - code coverage report
Current view: top level - sgx/src/untrusted - keyvalue_store.c (source / functions) Hit Total Coverage
Test: powHSM firmware Lines: 82 91 90.1 %
Date: 2025-11-18 03:05:18 Functions: 7 7 100.0 %

          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             : #define CHECK_POINTER_OR_RETURN(ptr, retval)        \
      37             :     if (!ptr) {                                     \
      38             :         LOG("NULL pointer given for <%s>\n", #ptr); \
      39             :         return retval;                              \
      40             :     }
      41             : 
      42             : // Sanitizes a key by allowing only [a-zA-Z0-9]. If one or more invalid
      43             : // characters are found, Replace them with a single hyphen.
      44          92 : static void sanitize_key(char* key, char* sanitized_key) {
      45          92 :     if (!key || !sanitized_key)
      46           0 :         return;
      47             : 
      48          92 :     size_t key_len = strlen(key);
      49             : 
      50             :     // Truncate key if it's too long
      51          92 :     if (key_len > KVSTORE_MAX_KEY_LEN) {
      52           3 :         key_len = KVSTORE_MAX_KEY_LEN;
      53             :     }
      54             : 
      55          92 :     bool prev_char_valid = false;
      56          92 :     size_t sanitized_key_len = 0;
      57        1835 :     for (size_t i = 0; i < key_len; i++) {
      58        1743 :         if (isalnum(key[i])) {
      59        1492 :             sanitized_key[sanitized_key_len++] = key[i];
      60        1492 :             prev_char_valid = true;
      61         251 :         } else if (prev_char_valid) {
      62         131 :             sanitized_key[sanitized_key_len++] = '-';
      63         131 :             prev_char_valid = false;
      64             :         }
      65             :     }
      66          92 :     sanitized_key[sanitized_key_len] = '\0';
      67             : }
      68             : 
      69          92 : static char* filename_for(char* key) {
      70             :     char sanitized_key[KVSTORE_MAX_KEY_LEN + 1];
      71          92 :     sanitize_key(key, sanitized_key);
      72          92 :     size_t filename_size =
      73          92 :         strlen(KVSTORE_PREFIX) + strlen(KVSTORE_SUFFIX) + strlen(sanitized_key);
      74          92 :     char* filename = malloc(filename_size + 1);
      75          92 :     strcpy(filename, "");
      76          92 :     strcat(filename, KVSTORE_PREFIX);
      77          92 :     strcat(filename, sanitized_key);
      78          92 :     strcat(filename, KVSTORE_SUFFIX);
      79          92 :     return filename;
      80             : }
      81             : 
      82          89 : static FILE* open_file_for(char* key, char* mode, size_t* file_size) {
      83          89 :     char* filename = filename_for(key);
      84             :     struct stat fst;
      85          89 :     stat(filename, &fst);
      86          89 :     if (file_size)
      87          62 :         *file_size = fst.st_size;
      88          89 :     FILE* file = fopen(filename, mode);
      89          89 :     free(filename);
      90          89 :     return file;
      91             : }
      92             : 
      93          30 : bool kvstore_save(char* key, uint8_t* data, size_t data_size) {
      94          30 :     CHECK_POINTER_OR_RETURN(key, false);
      95          29 :     CHECK_POINTER_OR_RETURN(data, false);
      96          28 :     LOG("Attempting to write data for %s...\n", key);
      97          28 :     if (!data_size) {
      98           1 :         LOG("Invalid zero-length data given for key <%s>\n", key);
      99           1 :         return false;
     100             :     }
     101             : 
     102          27 :     FILE* file = open_file_for(key, "wb", NULL);
     103          27 :     if (!file) {
     104           0 :         LOG("Could not open file for key <%s>\n", key);
     105           0 :         return false;
     106             :     }
     107             : 
     108          27 :     if (fwrite(data, sizeof(data[0]), data_size, file) != data_size) {
     109           0 :         LOG("Error writing secret payload for key <%s>\n", key);
     110           0 :         fclose(file);
     111           0 :         return false;
     112             :     };
     113             : 
     114          27 :     fclose(file);
     115          27 :     return true;
     116             : }
     117             : 
     118          40 : bool kvstore_exists(char* key) {
     119          40 :     CHECK_POINTER_OR_RETURN(key, false);
     120          39 :     LOG("Attempting to determine existence for key <%s>...\n", key);
     121          39 :     size_t file_size = 0;
     122          39 :     FILE* file = open_file_for(key, "rb", &file_size);
     123          39 :     if (file) {
     124          31 :         fclose(file);
     125          31 :         return true;
     126             :     }
     127           8 :     return false;
     128             : }
     129             : 
     130          25 : size_t kvstore_get(char* key, uint8_t* data_buf, size_t buffer_size) {
     131          25 :     CHECK_POINTER_OR_RETURN(key, 0);
     132          24 :     CHECK_POINTER_OR_RETURN(data_buf, 0);
     133          23 :     LOG("Attempting to read data for key <%s>...\n", key);
     134          23 :     size_t file_size = 0;
     135          23 :     FILE* file = open_file_for(key, "rb", &file_size);
     136          23 :     if (!file) {
     137           1 :         LOG("Could not open file for key <%s>\n", key);
     138           1 :         return 0;
     139             :     }
     140             : 
     141          22 :     if (file_size > buffer_size) {
     142           1 :         LOG("Payload too big for destination for key <%s>\n", key);
     143           1 :         fclose(file);
     144           1 :         return 0;
     145             :     }
     146             : 
     147          21 :     if (!file_size) {
     148           1 :         LOG("Invalid zero-length secret stored for key <%s>\n", key);
     149           1 :         fclose(file);
     150           1 :         return 0;
     151             :     }
     152             : 
     153          20 :     if (fread(data_buf, sizeof(data_buf[0]), file_size, file) != file_size) {
     154           0 :         LOG("Could not read payload for key <%s>\n", key);
     155           0 :         fclose(file);
     156           0 :         return 0;
     157             :     };
     158             : 
     159          20 :     fclose(file);
     160          20 :     return file_size;
     161             : }
     162             : 
     163           4 : bool kvstore_remove(char* key) {
     164           4 :     CHECK_POINTER_OR_RETURN(key, false);
     165           3 :     char* filename = filename_for(key);
     166           3 :     int result = remove(filename);
     167           3 :     if (result)
     168           1 :         LOG("Error removing file for key <%s>\n", key);
     169           3 :     free(filename);
     170           3 :     return !result;
     171             : }

Generated by: LCOV version 1.16