Skip to content

Commit ace3571

Browse files
committed
Add method to load symmetric key from memory
1 parent 01b1f02 commit ace3571

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/keys.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,49 @@ static PyObject* PyXmlSec_KeyFromBinaryFile(PyObject* self, PyObject* args, PyOb
257257
return NULL;
258258
}
259259

260+
static const char PyXmlSec_KeyFromBinaryData__doc__[] = \
261+
"Loads (symmetric) key of kind *klass* from *data*.\n\n"
262+
":param klass: the key value data klass\n"
263+
":param data: the key binary data\n"
264+
":return: pointer to newly created key\n";
265+
static PyObject* PyXmlSec_KeyFromBinaryData(PyObject* self, PyObject* args, PyObject* kwargs) {
266+
static char *kwlist[] = { "klass", "data", NULL};
267+
268+
PyXmlSec_KeyData* keydata = NULL;
269+
const char* data = NULL;
270+
Py_ssize_t data_size = 0;
271+
272+
PyXmlSec_Key* key = NULL;
273+
274+
PYXMLSEC_DEBUG("load symmetric key from memory - start");
275+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!s#:from_binary_data", kwlist,
276+
PyXmlSec_KeyDataType, &keydata, &data, &data_size))
277+
{
278+
goto ON_FAIL;
279+
}
280+
281+
if ((key = PyXmlSec_NewKey1((PyTypeObject*)self)) == NULL) goto ON_FAIL;
282+
283+
Py_BEGIN_ALLOW_THREADS;
284+
key->handle = xmlSecKeyReadMemory(keydata->id, (const xmlSecByte*)data, (xmlSecSize)data_size);
285+
Py_END_ALLOW_THREADS;
286+
287+
if (key->handle == NULL) {
288+
PyXmlSec_SetLastError("cannot read key");
289+
goto ON_FAIL;
290+
}
291+
292+
key->is_own = 1;
293+
294+
PYXMLSEC_DEBUG("load symmetric key from memory - ok");
295+
return (PyObject*)key;
296+
297+
ON_FAIL:
298+
PYXMLSEC_DEBUG("load symmetric key from memory - fail");
299+
Py_XDECREF(key);
300+
return NULL;
301+
}
302+
260303
static const char PyXmlSec_KeyCertFromMemory__doc__[] = \
261304
"Loads certificate from memory.\n\n"
262305
":param data: the certificate binary data\n"
@@ -413,6 +456,12 @@ static PyMethodDef PyXmlSec_KeyMethods[] = {
413456
METH_CLASS|METH_VARARGS|METH_KEYWORDS,
414457
PyXmlSec_KeyFromBinaryFile__doc__
415458
},
459+
{
460+
"from_binary_data",
461+
(PyCFunction)PyXmlSec_KeyFromBinaryData,
462+
METH_CLASS|METH_VARARGS|METH_KEYWORDS,
463+
PyXmlSec_KeyFromBinaryData__doc__
464+
},
416465
{
417466
"load_cert_from_memory",
418467
(PyCFunction)PyXmlSec_KeyCertFromMemory,

tests/test_keys.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ def test_from_binary_file_with_bad_args(self):
4646
with self.assertRaises(TypeError):
4747
xmlsec.Key.from_binary_file(klass="", filename=1)
4848

49+
def test_from_binary_data(self):
50+
key = xmlsec.Key.from_binary_data(klass=consts.KeyDataDes, data=self.load("deskey.bin"))
51+
self.assertIsNotNone(key)
52+
53+
def test_from_binary_data_with_bad_args(self):
54+
with self.assertRaises(TypeError):
55+
xmlsec.Key.from_binary_data(klass="", data=1)
56+
4957
def test_load_cert_from_file(self):
5058
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
5159
self.assertIsNotNone(key)

0 commit comments

Comments
 (0)