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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/* Chrysalide - Outil d'analyse de fichiers binaires
* secstorage.c - équivalent Python du fichier "core/secstorage.c"
*
* Copyright (C) 2025 Cyrille Bagard
*
* This file is part of Chrysalide.
*
* Chrysalide 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.
*
* Chrysalide 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "secstorage.h"
#include <core/secstorage.h>
#include "../access.h"
#include "../convert.h"
#include "../helpers.h"
/* Détermine si une clef de chiffrement protégée est en place. */
static PyObject *py_secstorage_has_secret_storage_key(PyObject *, PyObject *);
/* Définit un mot de passe pour protéger une clef maître. */
static PyObject *py_secstorage_set_secret_storage_password(PyObject *, PyObject *);
/* Détermine si la clef de chiffrement maître est vérouillée. */
static PyObject *py_secstorage_is_secret_storage_locked(PyObject *, PyObject *);
/* Déverrouille la clef de chiffrement maître. */
static PyObject *py_secstorage_unlock_secret_storage(PyObject *, PyObject *);
/* Verrouille la clef de chiffrement maître. */
static PyObject *py_secstorage_lock_secret_storage(PyObject *, PyObject *);
/* Chiffre des données avec la clef de chiffrement maître. */
static PyObject *py_secstorage_encrypt_secret_storage_data(PyObject *, PyObject *);
/* Déchiffre des données avec la clef de chiffrement maître. */
static PyObject *py_secstorage_decrypt_secret_storage_data(PyObject *, PyObject *);
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Détermine si un mot de passe est actuellement en place. *
* *
* Retour : Bilan de l'analyse. *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_has_secret_storage_key(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
GSettings *settings; /* Configuration à considérer */
int ret; /* Bilan de lecture des args. */
bool status; /* Bilan de situation */
#define SECSTORAGE_HAS_SECRET_STORAGE_KEY_METHOD PYTHON_METHOD_DEF \
( \
has_secret_storage_key, "/, settings=None", \
METH_VARARGS, py_secstorage, \
"Indicate if a master key used for protecting secrets seems to have"\
" been defined.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default.\n" \
"\n" \
"The result is a boolean status: *True* if the master key seems" \
" to exist, *False* otherwise." \
)
ret = PyArg_ParseTuple(args, "O&", convert_to_gsettings, &settings);
if (!ret) return NULL;
status = has_secret_storage_key(settings);
result = status ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Définit un mot de passe pour protéger une clef maître. *
* *
* Retour : Bilan de la mise en place. *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_set_secret_storage_password(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
GSettings *settings; /* Configuration à considérer */
const char *passwd; /* Mot de passe associé */
int ret; /* Bilan de lecture des args. */
bool status; /* Bilan de situation */
#define SECSTORAGE_SET_SECRET_STORAGE_PASSWORD_METHOD PYTHON_METHOD_DEF \
( \
set_secret_storage_password, "/, settings=None, password=''", \
METH_VARARGS, py_secstorage, \
"Create a master key used for protecting secrets. This key is" \
" itself protected by the provided password.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default. The supplied" \
" *password* has to be a string.\n" \
"\n" \
"The result is a boolean status: *True* if the operation successed,"\
" *False* otherwise." \
)
settings = NULL;
passwd = "";
ret = PyArg_ParseTuple(args, "|O&s", convert_to_gsettings, &settings, &passwd);
if (!ret) return NULL;
status = set_secret_storage_password(settings, passwd);
result = status ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Détermine si la clef de chiffrement maître est vérouillée. *
* *
* Retour : Bilan de la détermination. *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_is_secret_storage_locked(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
GSettings *settings; /* Configuration à considérer */
int ret; /* Bilan de lecture des args. */
bool status; /* Bilan de situation */
#define SECSTORAGE_IS_SECRET_STORAGE_LOCKED_METHOD PYTHON_METHOD_DEF \
( \
is_secret_storage_locked, "/, settings=None", \
METH_VARARGS, py_secstorage, \
"Indicate if the master key used for protecting secrets is" \
" currently decrypted in memory.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default.\n" \
"\n" \
"The result is a boolean status: *True* if the master key is" \
" unlocked and ready for use, *False* otherwise." \
)
settings = NULL;
ret = PyArg_ParseTuple(args, "|O&", convert_to_gsettings, &settings);
if (!ret) return NULL;
status = is_secret_storage_locked(settings);
result = status ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Déverrouille la clef de chiffrement maître. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_unlock_secret_storage(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
GSettings *settings; /* Configuration à considérer */
const char *passwd; /* Mot de passe associé */
int ret; /* Bilan de lecture des args. */
bool status; /* Bilan de situation */
#define SECSTORAGE_UNLOCK_SECRET_STORAGE_METHOD PYTHON_METHOD_DEF \
( \
unlock_secret_storage, "/, settings=None, password=''", \
METH_VARARGS, py_secstorage, \
"Decrypt in memory the master key used for protecting secrets.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default. The supplied" \
" *password* is the primary password used to protect this key.\n" \
"\n" \
"The result is a boolean status: *True* if the operation successed" \
" or if the master key is already unlocked, *False* otherwise." \
)
settings = NULL;
passwd = "";
ret = PyArg_ParseTuple(args, "|O&s", convert_to_gsettings, &settings, &passwd);
if (!ret) return NULL;
status = unlock_secret_storage(settings, passwd);
result = status ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Verrouille la clef de chiffrement maître. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_lock_secret_storage(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
GSettings *settings; /* Configuration à considérer */
int ret; /* Bilan de lecture des args. */
#define SECSTORAGE_LOCK_SECRET_STORAGE_METHOD PYTHON_METHOD_DEF \
( \
lock_secret_storage, "/, settings=None", \
METH_VARARGS, py_secstorage, \
"Clear from memory the master key used for protecting secrets.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default." \
)
settings = NULL;
ret = PyArg_ParseTuple(args, "|O&", convert_to_gsettings, &settings);
if (!ret) return NULL;
lock_secret_storage(settings);
result = Py_None;
Py_INCREF(result);
return result;
}
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Chiffre des données avec la clef de chiffrement maître. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_encrypt_secret_storage_data(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
const char *data_in; /* Données d'entrée à chiffrer */
Py_ssize_t size_in; /* Quantité de ces données */
GSettings *settings; /* Configuration à considérer */
int ret; /* Bilan de lecture des args. */
sized_binary_t in; /* Données à chiffer */
bool status; /* Bilan de situation */
sized_binary_t out; /* Données chiffrées */
#define SECSTORAGE_ENCRYPT_SECRET_STORAGE_DATA_METHOD PYTHON_METHOD_DEF \
( \
encrypt_secret_storage_data, "data, /, settings=None", \
METH_VARARGS, py_secstorage, \
"Encrypt data using an unlocked the master key.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default." \
"\n" \
"The result is either encrypted data as bytes in case of success," \
" or *None* in case of failure." \
)
settings = NULL;
ret = PyArg_ParseTuple(args, "s#|O&", &data_in, &size_in, convert_to_gsettings, &settings);
if (!ret) return NULL;
in.static_data = data_in;
in.size = size_in;
status = encrypt_secret_storage_data(settings, &in, &out);
if (status)
{
result = PyBytes_FromStringAndSize(out.static_data, out.size);
exit_sized_binary(&out);
}
else
{
result = Py_None;
Py_INCREF(result);
}
return result;
}
/******************************************************************************
* *
* Paramètres : self = objet Python concerné par l'appel. *
* args = arguments fournis à l'appel. *
* *
* Description : Déchiffre des données avec la clef de chiffrement maître. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
static PyObject *py_secstorage_decrypt_secret_storage_data(PyObject *self, PyObject *args)
{
PyObject *result; /* Conversion à retourner */
const char *data_in; /* Données d'entrée à chiffrer */
Py_ssize_t size_in; /* Quantité de ces données */
GSettings *settings; /* Configuration à considérer */
int ret; /* Bilan de lecture des args. */
sized_binary_t in; /* Données à chiffer */
bool status; /* Bilan de situation */
sized_binary_t out; /* Données chiffrées */
#define SECSTORAGE_DECRYPT_SECRET_STORAGE_DATA_METHOD PYTHON_METHOD_DEF \
( \
decrypt_secret_storage_data, "data, /, settings=None", \
METH_VARARGS, py_secstorage, \
"Decrypt data using an unlocked the master key.\n" \
"\n" \
"The *settings* arguement must point to a GSettings intance; the" \
" main configuration settings are used by default." \
"\n" \
"The result is either decrypted data as bytes in case of success," \
" or *None* in case of failure." \
)
settings = NULL;
ret = PyArg_ParseTuple(args, "s#|O&", &data_in, &size_in, convert_to_gsettings, &settings);
if (!ret) return NULL;
in.static_data = data_in;
in.size = size_in;
status = decrypt_secret_storage_data(settings, &in, &out);
if (status)
{
result = PyBytes_FromStringAndSize(out.static_data, out.size);
exit_sized_binary(&out);
}
else
{
result = Py_None;
Py_INCREF(result);
}
return result;
}
/******************************************************************************
* *
* Paramètres : - *
* *
* Description : Définit une extension du module 'core' à compléter. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool populate_core_module_with_secstorage(void)
{
bool result; /* Bilan à retourner */
PyObject *module; /* Module à recompléter */
static PyMethodDef py_secstorage_methods[] = {
SECSTORAGE_HAS_SECRET_STORAGE_KEY_METHOD,
SECSTORAGE_SET_SECRET_STORAGE_PASSWORD_METHOD,
SECSTORAGE_IS_SECRET_STORAGE_LOCKED_METHOD,
SECSTORAGE_UNLOCK_SECRET_STORAGE_METHOD,
SECSTORAGE_LOCK_SECRET_STORAGE_METHOD,
SECSTORAGE_ENCRYPT_SECRET_STORAGE_DATA_METHOD,
SECSTORAGE_DECRYPT_SECRET_STORAGE_DATA_METHOD,
{ NULL }
};
module = get_access_to_python_module("pychrysalide.core");
result = register_python_module_methods(module, py_secstorage_methods);
return result;
}
|