summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Sanin <aleksey@src.gnome.org>2003-08-08 16:15:39 +0000
committerAleksey Sanin <aleksey@src.gnome.org>2003-08-08 16:15:39 +0000
commit83ee529fb3cf53ea272c0fe50ec471a88f812083 (patch)
treee9193d96e1f72192d78e3284ce6792887b0c4456
parent6311ebf1129cb0deac3dce0f5d98f846a3bd8440 (diff)
downloadxmlsec1-83ee529fb3cf53ea272c0fe50ec471a88f812083.tar.gz
xmlsec1-83ee529fb3cf53ea272c0fe50ec471a88f812083.tar.bz2
xmlsec1-83ee529fb3cf53ea272c0fe50ec471a88f812083.zip
added functions to read keys from memory (patch from Jaohim)
-rw-r--r--ChangeLog5
-rw-r--r--include/xmlsec/keys.h5
-rw-r--r--src/keys.c139
3 files changed, 122 insertions, 27 deletions
diff --git a/ChangeLog b/ChangeLog
index 86b51ea4..180b96f8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Aug 8 09:06:53 2003 Aleksey Sanin <aleksey@aleksey.com>
+
+ * include/xmlsec/keys.h src/keys.c: added xmlsec-core functions to
+ read keys from memory (patch from Joachim)
+
Thu Aug 7 11:38:43 2003 Aleksey Sanin <aleksey@aleksey.com>
* include/xmlsec/openssl/app.h src/openssl/app.c: added functions
diff --git a/include/xmlsec/keys.h b/include/xmlsec/keys.h
index a7f74c9e..681482f3 100644
--- a/include/xmlsec/keys.h
+++ b/include/xmlsec/keys.h
@@ -158,8 +158,13 @@ XMLSEC_EXPORT int xmlSecKeyMatch (xmlSecKeyPtr key,
const xmlChar *name,
xmlSecKeyReqPtr keyReq);
+XMLSEC_EXPORT xmlSecKeyPtr xmlSecKeyReadBuffer (xmlSecKeyDataId dataId,
+ xmlSecBuffer* buffer);
XMLSEC_EXPORT xmlSecKeyPtr xmlSecKeyReadBinaryFile (xmlSecKeyDataId dataId,
const char* filename);
+XMLSEC_EXPORT xmlSecKeyPtr xmlSecKeyReadMemory (xmlSecKeyDataId dataId,
+ const xmlSecByte* data,
+ xmlSecSize dataSize);
/**
diff --git a/src/keys.c b/src/keys.c
index b26eaa9f..6f8d3157 100644
--- a/src/keys.c
+++ b/src/keys.c
@@ -761,6 +761,66 @@ xmlSecKeyGenerateByName(const xmlChar* name, xmlSecSize sizeBits, xmlSecKeyDataT
}
/**
+ * xmlSecKeyReadBuffer:
+ * @dataId: the key value data klass.
+ * @buffer: the buffer that contains the binary data.
+ *
+ * Reads the key value of klass @dataId from a buffer.
+ *
+ * Returns pointer to newly created key or NULL if an error occurs.
+ */
+xmlSecKeyPtr
+xmlSecKeyReadBuffer(xmlSecKeyDataId dataId, xmlSecBuffer* buffer) {
+ xmlSecKeyInfoCtx keyInfoCtx;
+ xmlSecKeyPtr key;
+ int ret;
+
+ xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
+ xmlSecAssert2(buffer != NULL, NULL);
+
+ /* create key data */
+ key = xmlSecKeyCreate();
+ if(key == NULL) {
+ xmlSecError(XMLSEC_ERRORS_HERE,
+ xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
+ "xmlSecKeyCreate",
+ XMLSEC_ERRORS_R_XMLSEC_FAILED,
+ XMLSEC_ERRORS_NO_MESSAGE);
+ return(NULL);
+ }
+
+ ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL);
+ if(ret < 0) {
+ xmlSecError(XMLSEC_ERRORS_HERE,
+ xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
+ "xmlSecKeyInfoCtxInitialize",
+ XMLSEC_ERRORS_R_XMLSEC_FAILED,
+ XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecKeyDestroy(key);
+ return(NULL);
+ }
+
+ keyInfoCtx.keyReq.keyType = xmlSecKeyDataTypeAny;
+ ret = xmlSecKeyDataBinRead(dataId, key,
+ xmlSecBufferGetData(buffer),
+ xmlSecBufferGetSize(buffer),
+ &keyInfoCtx);
+ if(ret < 0) {
+ xmlSecError(XMLSEC_ERRORS_HERE,
+ xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
+ "xmlSecKeyDataBinRead",
+ XMLSEC_ERRORS_R_XMLSEC_FAILED,
+ XMLSEC_ERRORS_NO_MESSAGE);
+ xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
+ xmlSecKeyDestroy(key);
+ return(NULL);
+ }
+ xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
+
+ return(key);
+}
+
+/**
* xmlSecKeyReadBinaryFile:
* @dataId: the key value data klass.
* @filename: the key binary filename.
@@ -771,10 +831,9 @@ xmlSecKeyGenerateByName(const xmlChar* name, xmlSecSize sizeBits, xmlSecKeyDataT
*/
xmlSecKeyPtr
xmlSecKeyReadBinaryFile(xmlSecKeyDataId dataId, const char* filename) {
- xmlSecKeyInfoCtx keyInfoCtx;
+ xmlSecKeyPtr key;
xmlSecBuffer buffer;
xmlSecByte buf[1024];
- xmlSecKeyPtr key;
FILE *f;
int ret;
@@ -823,51 +882,77 @@ xmlSecKeyReadBinaryFile(xmlSecKeyDataId dataId, const char* filename) {
}
}
fclose(f);
-
- /* create key data */
- key = xmlSecKeyCreate();
+
+ key = xmlSecKeyReadBuffer(dataId, &buffer);
if(key == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
- "xmlSecKeyCreate",
+ "xmlSecKeyReadBuffer",
XMLSEC_ERRORS_R_XMLSEC_FAILED,
- XMLSEC_ERRORS_NO_MESSAGE);
+ "filename=%s",
+ xmlSecErrorsSafeString(filename));
xmlSecBufferFinalize(&buffer);
- return(NULL);
+ return(NULL);
}
- ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL);
+ xmlSecBufferFinalize(&buffer);
+ return (key);
+}
+
+/**
+ * xmlSecKeyReadMemory:
+ * @dataId: the key value data klass.
+ * @data: the memory containing the key
+ * @dataSize: the size of the memory block
+ *
+ * Reads the key value of klass @dataId from a memory block @data.
+ *
+ * Returns pointer to newly created key or NULL if an error occurs.
+ */
+xmlSecKeyPtr
+xmlSecKeyReadMemory(xmlSecKeyDataId dataId, const xmlSecByte* data, xmlSecSize dataSize) {
+ xmlSecBuffer buffer;
+ xmlSecKeyPtr key;
+ int ret;
+
+ xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
+ xmlSecAssert2(data != NULL, NULL);
+ xmlSecAssert2(dataSize > 0, NULL);
+
+ /* read file to buffer */
+ ret = xmlSecBufferInitialize(&buffer, 0);
if(ret < 0) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
- "xmlSecKeyInfoCtxInitialize",
+ "xmlSecBufferInitialize",
+ XMLSEC_ERRORS_R_XMLSEC_FAILED,
+ XMLSEC_ERRORS_NO_MESSAGE);
+ return(NULL);
+ }
+
+ if (xmlSecBufferAppend(&buffer, data, dataSize) < 0) {
+ xmlSecError(XMLSEC_ERRORS_HERE,
+ xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
+ "xmlSecBufferAppend",
XMLSEC_ERRORS_R_XMLSEC_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecBufferFinalize(&buffer);
- xmlSecKeyDestroy(key);
- return(NULL);
+ return(NULL);
}
-
- keyInfoCtx.keyReq.keyType = xmlSecKeyDataTypeAny;
- ret = xmlSecKeyDataBinRead(dataId, key,
- xmlSecBufferGetData(&buffer),
- xmlSecBufferGetSize(&buffer),
- &keyInfoCtx);
- if(ret < 0) {
+
+ key = xmlSecKeyReadBuffer(dataId, &buffer);
+ if(key == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
- "xmlSecKeyDataBinRead",
+ "xmlSecKeyReadBuffer",
XMLSEC_ERRORS_R_XMLSEC_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
- xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
xmlSecBufferFinalize(&buffer);
- xmlSecKeyDestroy(key);
- return(NULL);
+ return(NULL);
}
- xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
+
xmlSecBufferFinalize(&buffer);
-
- return(key);
+ return (key);
}
/**
@@ -882,7 +967,7 @@ xmlSecKeyReadBinaryFile(xmlSecKeyDataId dataId, const char* filename) {
*/
xmlSecKeyPtr
xmlSecKeysMngrGetKey(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx) {
- xmlSecKeyPtr key = NULL;
+ xmlSecKeyPtr key;
int ret;
xmlSecAssert2(keyInfoCtx != NULL, NULL);