diff options
| author | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-01-12 15:39:59 +0000 |
|---|---|---|
| committer | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2010-01-12 15:39:59 +0000 |
| commit | a237cd105702f66b9ecf6727059d0bdcec096bae (patch) | |
| tree | 4ebf15409be61594d126d9ab173870421a1b7234 | |
| parent | d42c16e2391e0f03adc75d199ec40b92cbf9c1d6 (diff) | |
| download | lasso-a237cd105702f66b9ecf6727059d0bdcec096bae.tar.gz lasso-a237cd105702f66b9ecf6727059d0bdcec096bae.tar.xz lasso-a237cd105702f66b9ecf6727059d0bdcec096bae.zip | |
Core: add simple function to load key from any format
| -rw-r--r-- | lasso/xml/private.h | 3 | ||||
| -rw-r--r-- | lasso/xml/tools.c | 58 |
2 files changed, 60 insertions, 1 deletions
diff --git a/lasso/xml/private.h b/lasso/xml/private.h index 53d9daef..582fcd63 100644 --- a/lasso/xml/private.h +++ b/lasso/xml/private.h @@ -120,7 +120,6 @@ time_t lasso_iso_8601_gmt_to_time_t(char *xsdtime); LassoPemFileType lasso_get_pem_file_type(const char *file); xmlSecKeyPtr lasso_get_public_key_from_pem_file(const char *file); -xmlSecKeyPtr lasso_load_private_key_file(const char *file); xmlSecKeyPtr lasso_get_public_key_from_pem_cert_file(const char *file); xmlSecKeysMngr* lasso_load_certs_from_pem_certs_chain_file (const char *file); @@ -221,6 +220,8 @@ static inline void message(GLogLevelFlags level, const char *format, ...) char * lasso_get_relaystate_from_query(const char *query); char * lasso_url_add_parameters(char *url, gboolean free, ...); +xmlSecKey* lasso_xmlsec_load_private_key_from_buffer(const char *buffer, size_t length, const char *password); +xmlSecKey* lasso_xmlsec_load_private_key(const char *filename_or_buffer, const char *password); #ifdef __cplusplus } diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c index 08938c15..b20de580 100644 --- a/lasso/xml/tools.c +++ b/lasso/xml/tools.c @@ -1691,3 +1691,61 @@ cleanup: return new_url; } + +/** + * lasso_xmlsec_load_private_key_from_buffer: + * @buffer: a buffer containing a key in any format + * @length: length of the buffer + * @password: eventually a password + */ +xmlSecKey* +lasso_xmlsec_load_private_key_from_buffer(const char *buffer, size_t length, const char *password) { + int i = 0; + xmlSecKeyDataFormat key_formats[] = { + xmlSecKeyDataFormatDer, + xmlSecKeyDataFormatCertDer, + xmlSecKeyDataFormatPkcs8Der, + xmlSecKeyDataFormatCertPem, + xmlSecKeyDataFormatPkcs8Pem, + xmlSecKeyDataFormatPem, + xmlSecKeyDataFormatBinary, + 0 + }; + xmlSecKey *private_key = NULL; + + xmlSecErrorsDefaultCallbackEnableOutput(FALSE); + for (i = 0; key_formats[i] && private_key == NULL; i++) { + private_key = xmlSecCryptoAppKeyLoadMemory((xmlSecByte*)buffer, length, + key_formats[i], password, NULL, NULL); + } + /* special lasso metadata hack */ + if (! private_key) { + xmlChar *out; + int len; + out = xmlMalloc(length*4); + len = xmlSecBase64Decode(BAD_CAST buffer, out, length*4); + private_key = xmlSecCryptoAppKeyLoadMemory((xmlSecByte*)buffer, length, + xmlSecKeyDataFormatDer, password, NULL, NULL); + xmlFree(out); + } + xmlSecErrorsDefaultCallbackEnableOutput(TRUE); + + return private_key; +} + + +xmlSecKey* +lasso_xmlsec_load_private_key(const char *filename_or_buffer, const char *password) { + char *buffer; + size_t length; + + if (! filename_or_buffer) + return NULL; + + if (g_file_get_contents(filename_or_buffer, &buffer, &length, NULL)) { + return lasso_xmlsec_load_private_key_from_buffer(buffer, length, password); + } else { + return lasso_xmlsec_load_private_key_from_buffer(filename_or_buffer, strlen(filename_or_buffer), password); + } + +} |
