Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# KeyValue element and namespace tokens from include/xmlsec/strings.h and
# src/strings.c. keyinfo_fuzzer never requests a write, and its corpus has no
# KeyValue children, so these names are mutation-resistant gates.
"http://www.w3.org/2000/09/xmldsig#"
"http://www.w3.org/2009/xmldsig11#"
"http://www.aleksey.com/xmlsec/2002"
"http://www.w3.org/2001/04/xmlenc#"
"KeyInfo"
"KeyValue"
"RSAKeyValue"
"DSAKeyValue"
"ECKeyValue"
"DHKeyValue"
"AESKeyValue"
"HMACKeyValue"
"DESKeyValue"
"Modulus"
"Exponent"
"NamedCurve"
"PublicKey"
"Seed"
"PgenCounter"
3 changes: 3 additions & 0 deletions apps/oss-fuzz/config/xmlsec_keyvalue_fuzzer.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[libfuzzer]
dict = xmlsec_keyvalue_fuzzer.dict
max_len = 65536
5 changes: 5 additions & 0 deletions apps/oss-fuzz/corpus/xmlsec_keyvalue/aes-keyvalue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:xsec="http://www.aleksey.com/xmlsec/2002">
<KeyValue>
<xsec:AESKeyValue>MDEyMzQ1Njc4OUFCQ0RFRg==</xsec:AESKeyValue>
</KeyValue>
</KeyInfo>
5 changes: 5 additions & 0 deletions apps/oss-fuzz/corpus/xmlsec_keyvalue/dsa-keyvalue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyValue>
<DSAKeyValue><P>5llqtXo2G1g61J8WFsP00U9cl5z7emzkxqTre4C8pV/qdT8YRkbxrvHK7Bc7BXdx7abcP46842DcEZ07u7esCzlV9jziQTWsij9d6l8Nra9RRMqz37wDx0hUPRfmWHeEVFHpQkgUlL1lai/Ujs36fbiInXWoR3fWQn6+vjqP5S8=</P><Q>vSPQz7ak24nBo5gwSMfEXkvEblc=</Q><G>CUyqAoEkRHpKrbmVnA8t3vkSEM8Gk3qHmTNunxVdmBVaitqPnf6D7SXxLRQNvuJ8+BAI70U/+iu5EUknE+QzvH2bPP9B9pXl9lNBV88wzjCC+M3kQD2+Bjps+NdJkTjhrJMygi8KPGCDbmfSqf+F1+nBIpDIgKzWxbhUMHd9VfI=</G><Y>JLZHChtL9OjhYpD2tVpN4+Urp9P0R/1jmkQA3vYycSf6GDtGDyESjCuWl2sH4EwIt3yJkhN4CPwGgrrkq8H1S+U7X76nTEaobAmElnBrBeF6uatRwwmCvVm4DCKs1XJNeN1QlsbLikf9aJguzjj8unzyYZRdjMcgjhLtVjeYAC0=</Y><J>ATfG1k4XSpDlWczkblMTpUkvazbCh6NH0ImOzLyjXlhRpihqtb/HBaCGWs3JzAiafW75tk57/9+vj6drV/vl44l14K5piP7VnDSADNCkBq9gCN/wM3u8w7cb8gpgTqC68I2yHzVVh4umjOrrgg==</J></DSAKeyValue>
</KeyValue>
</KeyInfo>
8 changes: 8 additions & 0 deletions apps/oss-fuzz/corpus/xmlsec_keyvalue/ec-keyvalue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyValue>
<ECKeyValue xmlns="http://www.w3.org/2009/xmldsig11#">
<NamedCurve URI="urn:oid:1.2.840.10045.3.1.7"/>
<PublicKey>BP4TDUMXanrPs0QmI8bGK0l7x3mhNCt11nlv3+6F6uWpSZjtZuPHnb+OCwBCcPHKeK1j24raHXg8Il5Xwzx2YNw=</PublicKey>
</ECKeyValue>
</KeyValue>
</KeyInfo>
5 changes: 5 additions & 0 deletions apps/oss-fuzz/corpus/xmlsec_keyvalue/hmac-keyvalue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:xsec="http://www.aleksey.com/xmlsec/2002">
<KeyValue>
<xsec:HMACKeyValue>MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=</xsec:HMACKeyValue>
</KeyValue>
</KeyInfo>
8 changes: 8 additions & 0 deletions apps/oss-fuzz/corpus/xmlsec_keyvalue/rsa-keyvalue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyValue>
<RSAKeyValue>
<Modulus>3iVnhDXlgGiWvV2f21bCP4NAeTkwouWvN9K94SNeV01xzuvPg2GRb+ozF0/YbQ8jj7UDeuIgcLoBrC/jSMtp7gJp6zj3oHhX97NZVv5SBUmBOJRbB8efy3apTvTvWlzJQhO4WVXBRDqmA0dGHPRRuMB6l125wy+WBMWxO6BzUooe9m0OpQXjKokJKFcl9Zd4ht3EqXW8cuHgiyrtYzXTcO63W9+J6dFOi1DTYhKMkK53jMsModlleEUdqUHgTbxK0TBSYMa2sB1rGfcVq+QanOlsRHXAXiZM/BE6uPqlFq+g5osSZbHfH2qC/0G/wKKlWzIx0YPjQSBq/MbroodKLQ==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
</KeyValue>
</KeyInfo>
236 changes: 236 additions & 0 deletions apps/oss-fuzz/xmlsec_keyvalue_target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* xmlsec KeyValue read/write fuzz target.
*
* Threat model: a KeyInfo/KeyValue element is attacker-controlled in any XML
* Signature or XML Encryption document. xmlsec_keyinfo_fuzzer only calls
* xmlSecKeyInfoNodeRead (unknown or AES key id) and never writes, so the
* KeyValue writers in src/keysdata_helpers.c stay dark. This target reads the
* untrusted KeyInfo and writes any key it obtains back through
* xmlSecKeyInfoNodeWrite. It also generates a small symmetric key and writes
* that, which reaches the binary KeyValue writers without a successful parse.
*
* Expected bugs: memory-safety in base64 decode, bignum import and XML
* serialization of key material. External fetches stay disabled.
*/
#include <stdint.h>
#include <stddef.h>
#include <limits.h>

int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlerror.h>

#include <xmlsec/xmlsec.h>
#include <xmlsec/xmltree.h>
#include <xmlsec/keys.h>
#include <xmlsec/keyinfo.h>
#include <xmlsec/keysmngr.h>
#include <xmlsec/parser.h>
#include <xmlsec/errors.h>
#include <xmlsec/strings.h>

#include <xmlsec/openssl/app.h>
#include <xmlsec/openssl/crypto.h>

static int g_initialized = 0;
static int g_init_failed = 0;
static xmlSecKeysMngrPtr g_mngr = NULL;

static void ignore_error(void* ctx, const char* msg, ...) {
(void)ctx;
(void)msg;
}

static void ignore_xmlsec_error(const char* file, int line, const char* func,
const char* errorObject, const char* errorSubject,
int reason, const char* msg) {
(void)file;
(void)line;
(void)func;
(void)errorObject;
(void)errorSubject;
(void)reason;
(void)msg;
}

static int do_init(void) {
xmlInitParser();
if (xmlSecInit() < 0) {
return -1;
}
if (xmlSecCheckVersion() != 1) {
xmlSecShutdown();
return -1;
}
if (xmlSecOpenSSLAppInit(NULL) < 0) {
xmlSecShutdown();
return -1;
}
if (xmlSecOpenSSLInit() < 0) {
xmlSecOpenSSLShutdown();
xmlSecOpenSSLAppShutdown();
xmlSecShutdown();
return -1;
}
xmlSetGenericErrorFunc(NULL, &ignore_error);
xmlSecErrorsSetCallback(&ignore_xmlsec_error);

g_mngr = xmlSecKeysMngrCreate();
if (g_mngr == NULL) {
goto fail;
}
if (xmlSecOpenSSLAppDefaultKeysMngrInit(g_mngr) < 0) {
xmlSecKeysMngrDestroy(g_mngr);
g_mngr = NULL;
goto fail;
}
return 0;

fail:
xmlSecOpenSSLShutdown();
xmlSecOpenSSLAppShutdown();
xmlSecShutdown();
return -1;
}
static void write_key(xmlSecKeyPtr key) {
xmlSecKeyInfoCtxPtr ctx;
xmlDocPtr doc;
xmlNodePtr node;

if (key == NULL || xmlSecKeyGetValue(key) == NULL) {
return;
}
doc = xmlNewDoc(BAD_CAST "1.0");
if (doc == NULL) {
return;
}
node = xmlNewDocNode(doc, NULL, xmlSecNodeKeyInfo, NULL);
if (node == NULL) {
xmlFreeDoc(doc);
return;
}
xmlDocSetRootElement(doc, node);
xmlSetNs(node, xmlNewNs(node, xmlSecDSigNs, NULL));

ctx = xmlSecKeyInfoCtxCreate(g_mngr);
if (ctx == NULL) {
xmlFreeDoc(doc);
return;
}
ctx->mode = xmlSecKeyInfoModeWrite;
ctx->keyReq.keyId = xmlSecKeyDataIdUnknown;
ctx->keyReq.keyType = xmlSecKeyDataTypeAny;
ctx->keyReq.keyUsage = xmlSecKeyUsageAny;
(void)xmlSecKeyInfoNodeWrite(node, key, ctx);
xmlSecKeyInfoCtxDestroy(ctx);
xmlFreeDoc(doc);
}

static void read_keyinfo(xmlNodePtr node, xmlSecKeyDataId keyId) {
xmlSecKeyPtr key;
xmlSecKeyInfoCtxPtr ctx;

key = xmlSecKeyCreate();
if (key == NULL) {
return;
}
ctx = xmlSecKeyInfoCtxCreate(g_mngr);
if (ctx == NULL) {
xmlSecKeyDestroy(key);
return;
}
ctx->mode = xmlSecKeyInfoModeRead;
ctx->keyReq.keyId = keyId;
ctx->keyReq.keyType = xmlSecKeyDataTypeAny;
ctx->keyReq.keyUsage = xmlSecKeyUsageAny;
ctx->retrievalMethodCtx.enabledUris =
xmlSecTransformUriTypeEmpty | xmlSecTransformUriTypeSameDocument;
(void)xmlSecKeyInfoNodeRead(node, key, ctx);
xmlSecKeyInfoCtxDestroy(ctx);
write_key(key);
xmlSecKeyDestroy(key);
}

static void write_generated_symmetric(uint8_t selector) {
xmlSecKeyDataId id;
xmlSecSize sizeBits;
xmlSecKeyPtr key;

/* Ids are function-call macros, not compile-time constants. */
switch (selector % 4) {
case 0: id = xmlSecOpenSSLKeyDataAesId; sizeBits = 128; break;
case 1: id = xmlSecOpenSSLKeyDataAesId; sizeBits = 256; break;
case 2: id = xmlSecOpenSSLKeyDataHmacId; sizeBits = 256; break;
default: id = xmlSecOpenSSLKeyDataHmacId; sizeBits = 160; break;
}
key = xmlSecKeyGenerate(id, sizeBits, xmlSecKeyDataTypeSymmetric);
if (key == NULL) {
return;
}
write_key(key);
xmlSecKeyDestroy(key);
}

int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
xmlDocPtr doc = NULL;
xmlNodePtr root = NULL;
xmlNodePtr node = NULL;
xmlSecKeyDataId ids[8];
size_t nids = 0;
size_t i;

if (!g_initialized) {
g_init_failed = (do_init() < 0);
g_initialized = 1;
}
/* Bound the document so one input cannot stall the process. */
if (g_init_failed || size < 2 || size > 65536) {
return 0;
}

write_generated_symmetric(data[0]);

/* Parse the whole buffer so corpus files are valid KeyInfo documents.
data[0] still selects the generated symmetric key. */
doc = xmlReadMemory((const char*)data, (int)size, "fuzz.xml", NULL,
xmlSecParserGetDefaultOptions() | XML_PARSE_NONET);
if (doc == NULL) {
return 0;
}
root = xmlDocGetRootElement(doc);
if (root == NULL) {
xmlFreeDoc(doc);
return 0;
}
if (xmlSecCheckNodeName(root, xmlSecNodeKeyInfo, xmlSecDSigNs)) {
node = root;
} else {
node = xmlSecFindNode(root, xmlSecNodeKeyInfo, xmlSecDSigNs);
}
if (node == NULL) {
xmlFreeDoc(doc);
return 0;
}

ids[nids++] = xmlSecKeyDataIdUnknown;
ids[nids++] = xmlSecOpenSSLKeyDataRsaId;
#ifndef XMLSEC_NO_DSA
ids[nids++] = xmlSecOpenSSLKeyDataDsaId;
#endif
#ifndef XMLSEC_NO_EC
ids[nids++] = xmlSecOpenSSLKeyDataEcId;
#endif
#ifndef XMLSEC_NO_DH
ids[nids++] = xmlSecOpenSSLKeyDataDhId;
#endif
ids[nids++] = xmlSecOpenSSLKeyDataAesId;
ids[nids++] = xmlSecOpenSSLKeyDataHmacId;
for (i = 0; i < nids; i++) {
read_keyinfo(node, ids[i]);
}
xmlFreeDoc(doc);
return 0;
}

Loading