secret.c (2892B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2026 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet 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 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file secret.c 23 */ 24 25 #include "secret.h" 26 27 #include <gnunet/gnunet_util_lib.h> 28 #include <string.h> 29 30 #define SECRET_APP_ID "org.gnunet.Messenger" 31 32 const SecretSchema * 33 _secret_schema(void) 34 { 35 static const SecretSchema schema = { 36 "org.gnunet.chat.AccountSecret", SECRET_SCHEMA_NONE, 37 { 38 { "name", SECRET_SCHEMA_ATTRIBUTE_STRING }, 39 { "app_id", SECRET_SCHEMA_ATTRIBUTE_STRING }, 40 { "NULL", 0 }, 41 } 42 }; 43 return &schema; 44 } 45 46 char* 47 _secret_description(const char *name) 48 { 49 char *desc; 50 51 GNUNET_asprintf( 52 &desc, 53 "GNUnet Messenger account secret for identity %s", 54 name 55 ); 56 57 return desc; 58 } 59 60 char* 61 secret_lookup(const char *name, 62 uint32_t *secret_len) 63 { 64 GError *error = NULL; 65 gchar *password; 66 67 password = secret_password_lookup_sync( 68 _secret_schema(), 69 NULL, 70 &error, 71 "name", name, 72 "app_id", SECRET_APP_ID, 73 NULL 74 ); 75 76 if (error) 77 { 78 return NULL; 79 } 80 else if (password) 81 { 82 *secret_len = g_utf8_strlen(password, -1); 83 return password; 84 } 85 else 86 { 87 *secret_len = 0; 88 return NULL; 89 } 90 } 91 92 bool 93 secret_store(const char *name, 94 const char *secret, 95 uint32_t secret_len) 96 { 97 GError *error = NULL; 98 gboolean result; 99 100 if (strlen(secret) != secret_len) 101 return false; 102 103 result = secret_password_store_sync ( 104 _secret_schema(), 105 SECRET_COLLECTION_DEFAULT, 106 _secret_description(name), 107 secret, 108 NULL, 109 &error, 110 "name", name, 111 "app_id", SECRET_APP_ID, 112 NULL 113 ); 114 115 if (error) 116 return false; 117 else 118 return result; 119 } 120 121 bool 122 secret_delete(const char *name) 123 { 124 GError *error = NULL; 125 gboolean result; 126 127 result = secret_password_clear_sync( 128 _secret_schema(), 129 NULL, 130 &error, 131 "name", name, 132 "app_id", SECRET_APP_ID, 133 NULL 134 ); 135 136 if (error) 137 return false; 138 else 139 return result; 140 } 141 142 void 143 secret_wipe(char *secret) 144 { 145 gchar *password = secret; 146 147 if (password) 148 secret_password_wipe(password); 149 } 150 151 void 152 secret_free(char *secret) 153 { 154 gchar *password = secret; 155 156 if (password) 157 secret_password_free(password); 158 }