forked from lsh123/xmlsec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh docs and cleanup css (lsh123#407)
- Loading branch information
Showing
37 changed files
with
662 additions
and
5,444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,24 +26,24 @@ | |
<div class="titlepage"><div><div><h3 class="title"> | ||
<a name="xmlsec-example-decrypt3"></a>decrypt3.c</h3></div></div></div> | ||
<div class="informalexample"><pre class="programlisting"> | ||
/** | ||
/** | ||
* XML Security Library example: Decrypting an encrypted file using a custom keys manager. | ||
* | ||
* | ||
* Decrypts encrypted XML file using a custom files based keys manager. | ||
* We assume that key's name in <dsig:KeyName/> element is just | ||
* We assume that key's name in <dsig:KeyName/> element is just | ||
* key's file name in the current folder. | ||
* | ||
* Usage: | ||
* ./decrypt3 <xml-enc> | ||
* | ||
* Usage: | ||
* ./decrypt3 <xml-enc> | ||
* | ||
* Example: | ||
* ./decrypt3 encrypt1-res.xml | ||
* ./decrypt3 encrypt2-res.xml | ||
* | ||
* This is free software; see Copyright file in the source | ||
* distribution for preciese wording. | ||
* | ||
* Copyright (C) 2002-2016 Aleksey Sanin <[email protected]>. All Rights Reserved. | ||
* | ||
* Copyright (C) 2002-2022 Aleksey Sanin <[email protected]>. All Rights Reserved. | ||
*/ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
@@ -68,12 +68,12 @@ | |
xmlSecKeysMngrPtr create_files_keys_mngr(void); | ||
int decrypt_file(xmlSecKeysMngrPtr mngr, const char* enc_file); | ||
|
||
int | ||
int | ||
main(int argc, char **argv) { | ||
xmlSecKeysMngrPtr mngr; | ||
#ifndef XMLSEC_NO_XSLT | ||
xsltSecurityPrefsPtr xsltSecPrefs = NULL; | ||
#endif /* XMLSEC_NO_XSLT */ | ||
#endif /* XMLSEC_NO_XSLT */ | ||
|
||
assert(argv); | ||
|
||
|
@@ -89,20 +89,20 @@ | |
xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; | ||
xmlSubstituteEntitiesDefault(1); | ||
#ifndef XMLSEC_NO_XSLT | ||
xmlIndentTreeOutput = 1; | ||
xmlIndentTreeOutput = 1; | ||
#endif /* XMLSEC_NO_XSLT */ | ||
|
||
/* Init libxslt */ | ||
#ifndef XMLSEC_NO_XSLT | ||
/* disable everything */ | ||
xsltSecPrefs = xsltNewSecurityPrefs(); | ||
xsltSecPrefs = xsltNewSecurityPrefs(); | ||
xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid); | ||
xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid); | ||
xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid); | ||
xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid); | ||
xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid); | ||
xsltSetDefaultSecurityPrefs(xsltSecPrefs); | ||
#endif /* XMLSEC_NO_XSLT */ | ||
xsltSetDefaultSecurityPrefs(xsltSecPrefs); | ||
#endif /* XMLSEC_NO_XSLT */ | ||
|
||
/* Init xmlsec library */ | ||
if(xmlSecInit() < 0) { | ||
|
@@ -118,15 +118,15 @@ | |
|
||
/* Load default crypto engine if we are supporting dynamic | ||
* loading for xmlsec-crypto libraries. Use the crypto library | ||
* name ("openssl", "nss", etc.) to load corresponding | ||
* name ("openssl", "nss", etc.) to load corresponding | ||
* xmlsec-crypto library. | ||
*/ | ||
#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING | ||
if(xmlSecCryptoDLLoadLibrary(NULL) < 0) { | ||
fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n" | ||
"that you have it installed and check shared libraries path\n" | ||
"(LD_LIBRARY_PATH and/or LTDL_LIBRARY_PATH) environment variables.\n"); | ||
return(-1); | ||
return(-1); | ||
} | ||
#endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */ | ||
|
||
|
@@ -149,29 +149,29 @@ | |
} | ||
|
||
if(decrypt_file(mngr, argv[1]) < 0) { | ||
xmlSecKeysMngrDestroy(mngr); | ||
xmlSecKeysMngrDestroy(mngr); | ||
return(-1); | ||
} | ||
} | ||
|
||
/* destroy keys manager */ | ||
xmlSecKeysMngrDestroy(mngr); | ||
|
||
/* Shutdown xmlsec-crypto library */ | ||
xmlSecCryptoShutdown(); | ||
|
||
/* Shutdown crypto library */ | ||
xmlSecCryptoAppShutdown(); | ||
|
||
/* Shutdown xmlsec library */ | ||
xmlSecShutdown(); | ||
|
||
/* Shutdown libxslt/libxml */ | ||
#ifndef XMLSEC_NO_XSLT | ||
xsltFreeSecurityPrefs(xsltSecPrefs); | ||
xsltCleanupGlobals(); | ||
xsltCleanupGlobals(); | ||
#endif /* XMLSEC_NO_XSLT */ | ||
xmlCleanupParser(); | ||
|
||
return(0); | ||
} | ||
|
||
|
@@ -180,33 +180,33 @@ | |
* @mngr: the pointer to keys manager. | ||
* @enc_file: the encrypted XML file name. | ||
* | ||
* Decrypts the XML file #enc_file using DES key from #key_file and | ||
* Decrypts the XML file #enc_file using DES key from #key_file and | ||
* prints results to stdout. | ||
* | ||
* Returns 0 on success or a negative value if an error occurs. | ||
*/ | ||
int | ||
int | ||
decrypt_file(xmlSecKeysMngrPtr mngr, const char* enc_file) { | ||
xmlDocPtr doc = NULL; | ||
xmlNodePtr node = NULL; | ||
xmlSecEncCtxPtr encCtx = NULL; | ||
int res = -1; | ||
|
||
assert(mngr); | ||
assert(enc_file); | ||
|
||
/* load template */ | ||
doc = xmlParseFile(enc_file); | ||
if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){ | ||
fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file); | ||
goto done; | ||
goto done; | ||
} | ||
|
||
/* find start node */ | ||
node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs); | ||
if(node == NULL) { | ||
fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file); | ||
goto done; | ||
goto done; | ||
} | ||
|
||
/* create encryption context */ | ||
|
@@ -221,7 +221,7 @@ | |
fprintf(stderr,"Error: decryption failed\n"); | ||
goto done; | ||
} | ||
|
||
/* print decrypted data to stdout */ | ||
if(encCtx->resultReplaced != 0) { | ||
fprintf(stdout, "Decrypted XML data:\n"); | ||
|
@@ -230,38 +230,38 @@ | |
fprintf(stdout, "Decrypted binary data (" XMLSEC_SIZE_FMT " bytes):\n", | ||
xmlSecBufferGetSize(encCtx->result)); | ||
if(xmlSecBufferGetData(encCtx->result) != NULL) { | ||
fwrite(xmlSecBufferGetData(encCtx->result), | ||
1, | ||
fwrite(xmlSecBufferGetData(encCtx->result), | ||
1, | ||
xmlSecBufferGetSize(encCtx->result), | ||
stdout); | ||
} | ||
} | ||
fprintf(stdout, "\n"); | ||
|
||
/* success */ | ||
res = 0; | ||
|
||
done: | ||
done: | ||
/* cleanup */ | ||
if(encCtx != NULL) { | ||
xmlSecEncCtxDestroy(encCtx); | ||
} | ||
|
||
if(doc != NULL) { | ||
xmlFreeDoc(doc); | ||
xmlFreeDoc(doc); | ||
} | ||
return(res); | ||
} | ||
|
||
/** | ||
* create_files_keys_mngr: | ||
* | ||
* Creates a files based keys manager: we assume that key name is | ||
* | ||
* Creates a files based keys manager: we assume that key name is | ||
* the key file name, | ||
* | ||
* Returns pointer to newly created keys manager or NULL if an error occurs. | ||
*/ | ||
xmlSecKeysMngrPtr | ||
xmlSecKeysMngrPtr | ||
create_files_keys_mngr(void) { | ||
xmlSecKeyStorePtr keysStore; | ||
xmlSecKeysMngrPtr mngr; | ||
|
@@ -272,7 +272,7 @@ | |
fprintf(stderr, "Error: failed to create keys store.\n"); | ||
return(NULL); | ||
} | ||
|
||
/* create keys manager */ | ||
mngr = xmlSecKeysMngrCreate(); | ||
if(mngr == NULL) { | ||
|
@@ -288,7 +288,7 @@ | |
xmlSecKeysMngrDestroy(mngr); | ||
return(NULL); | ||
} | ||
|
||
/* initialize crypto library specific data in keys manager */ | ||
if(xmlSecCryptoKeysMngrInit(mngr) < 0) { | ||
fprintf(stderr, "Error: failed to initialize crypto data in keys manager.\n"); | ||
|
@@ -303,19 +303,19 @@ | |
|
||
/**************************************************************************** | ||
* | ||
* Files Keys Store: we assume that key's name (content of the | ||
* <dsig:KeyName/> element is a name of the file with a key (in the | ||
* Files Keys Store: we assume that key's name (content of the | ||
* <dsig:KeyName/> element is a name of the file with a key (in the | ||
* current folder). | ||
* Attention: this probably not a good solution for high traffic systems. | ||
* | ||
* | ||
***************************************************************************/ | ||
static xmlSecKeyPtr files_keys_store_find_key (xmlSecKeyStorePtr store, | ||
const xmlChar* name, | ||
xmlSecKeyInfoCtxPtr keyInfoCtx); | ||
static xmlSecKeyStoreKlass files_keys_store_klass = { | ||
sizeof(xmlSecKeyStoreKlass), | ||
sizeof(xmlSecKeyStore), | ||
BAD_CAST "files-based-keys-store", /* const xmlChar* name; */ | ||
BAD_CAST "files-based-keys-store", /* const xmlChar* name; */ | ||
NULL, /* xmlSecKeyStoreInitializeMethod initialize; */ | ||
NULL, /* xmlSecKeyStoreFinalizeMethod finalize; */ | ||
files_keys_store_find_key, /* xmlSecKeyStoreFindKeyMethod findKey; */ | ||
|
@@ -327,13 +327,13 @@ | |
|
||
/** | ||
* files_keys_store_get_klass: | ||
* | ||
* | ||
* The files based keys store klass: we assume that key name is the | ||
* key file name, | ||
* | ||
* Returns files based keys store klass. | ||
*/ | ||
xmlSecKeyStoreId | ||
xmlSecKeyStoreId | ||
files_keys_store_get_klass(void) { | ||
return(&files_keys_store_klass); | ||
} | ||
|
@@ -343,7 +343,7 @@ | |
* @store: the pointer to simple keys store. | ||
* @name: the desired key name. | ||
* @keyInfoCtx: the pointer to <dsig:KeyInfo/> node processing context. | ||
* | ||
* | ||
* Lookups key in the @store. The caller is responsible for destroying | ||
* returned key with #xmlSecKeyDestroy function. | ||
* | ||
|
@@ -353,16 +353,16 @@ | |
files_keys_store_find_key(xmlSecKeyStorePtr store, const xmlChar* name, xmlSecKeyInfoCtxPtr keyInfoCtx) { | ||
xmlSecKeyPtr key; | ||
const xmlChar* p; | ||
|
||
assert(store); | ||
assert(keyInfoCtx); | ||
|
||
/* it's possible to do not have the key name or desired key type | ||
/* it's possible to do not have the key name or desired key type | ||
* but we could do nothing in this case */ | ||
if((name == NULL) || (keyInfoCtx->keyReq.keyId == xmlSecKeyDataIdUnknown)){ | ||
return(NULL); | ||
} | ||
|
||
/* we don't want to open files in a folder other than "current"; | ||
* to prevent it limit the characters in the key name to alpha/digit, | ||
* '.', '-' or '_'. | ||
|
@@ -372,7 +372,7 @@ | |
return(NULL); | ||
} | ||
} | ||
|
||
if((keyInfoCtx->keyReq.keyId == xmlSecKeyDataDsaId) || (keyInfoCtx->keyReq.keyId == xmlSecKeyDataRsaId)) { | ||
/* load key from a pem file, if key is not found then it's an error (is it?) */ | ||
key = xmlSecCryptoAppKeyLoad((const char*)name, xmlSecKeyDataFormatPem, NULL, NULL, NULL); | ||
|
@@ -393,7 +393,7 @@ | |
if(xmlSecKeySetName(key, name) < 0) { | ||
fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", (const char*)name); | ||
xmlSecKeyDestroy(key); | ||
return(NULL); | ||
return(NULL); | ||
} | ||
|
||
return(key); | ||
|
Oops, something went wrong.