summaryrefslogtreecommitdiff
path: root/external/libxmlsec/xmlsec1-ooxml.patch.1
blob: af92fbbe7b3e63cc6ebe826fb24cc6e25a5db7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
From 1770428d30a77e7c5e3344687369d83e04201f0b Mon Sep 17 00:00:00 2001
From: Miklos Vajna <vmiklos@collabora.co.uk>
Date: Mon, 25 Jan 2016 09:50:03 +0100
Subject: [PATCH] OOXML Relationship Transform skeleton

---
 include/xmlsec/strings.h    |   3 +
 include/xmlsec/transforms.h |   4 +
 src/strings.c               |   3 +
 src/transforms.c            |  11 ++
 src/xpath.c                 | 279 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 300 insertions(+)

diff --git a/include/xmlsec/strings.h b/include/xmlsec/strings.h
index 07afb9d..9c72d1b 100644
--- a/include/xmlsec/strings.h
+++ b/include/xmlsec/strings.h
@@ -551,6 +551,9 @@ XMLSEC_EXPORT_VAR const xmlChar xmlSecXPath2FilterUnion[];
 XMLSEC_EXPORT_VAR const xmlChar xmlSecNameXPointer[];
 XMLSEC_EXPORT_VAR const xmlChar xmlSecNodeXPointer[];
 
+XMLSEC_EXPORT_VAR const xmlChar xmlSecNameRelationship[];
+XMLSEC_EXPORT_VAR const xmlChar xmlSecHrefRelationship[];
+
 /*************************************************************************
  *
  * Xslt strings
diff --git a/include/xmlsec/transforms.h b/include/xmlsec/transforms.h
index 4008cae..b0e31e4 100644
--- a/include/xmlsec/transforms.h
+++ b/include/xmlsec/transforms.h
@@ -961,6 +961,10 @@ XMLSEC_EXPORT int		xmlSecTransformXPointerSetExpr		(xmlSecTransformPtr transform
 									 const xmlChar* expr,
 									 xmlSecNodeSetType nodeSetType,
 									 xmlNodePtr hereNode);
+
+#define xmlSecTransformRelationshipId xmlSecTransformRelationshipGetKlass()
+XMLSEC_EXPORT xmlSecTransformId xmlSecTransformRelationshipGetKlass (void);
+
 #ifndef XMLSEC_NO_XSLT
 /**
  * xmlSecTransformXsltId:
diff --git a/src/strings.c b/src/strings.c
index 9897198..546e993 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -543,6 +543,9 @@ const xmlChar xmlSecXPath2FilterUnion[]		= "union";
 const xmlChar xmlSecNameXPointer[]		= "xpointer";
 const xmlChar xmlSecNodeXPointer[]		= "XPointer";
 
+const xmlChar xmlSecNameRelationship[] = "relationship";
+const xmlChar xmlSecHrefRelationship[] = "http://schemas.openxmlformats.org/package/2006/RelationshipTransform";
+
 /*************************************************************************
  *
  * Xslt strings
diff --git a/src/transforms.c b/src/transforms.c
index 2ed3fe8..9e5ad27 100644
--- a/src/transforms.c
+++ b/src/transforms.c
@@ -271,6 +271,17 @@ xmlSecTransformIdsRegisterDefault(void) {
 	return(-1);
     }
 
+    if (xmlSecTransformIdsRegister(xmlSecTransformRelationshipId) < 0)
+    {
+	xmlSecError(XMLSEC_ERRORS_HERE,
+		    NULL,
+		    "xmlSecTransformIdsRegister",
+		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+		    "name=%s",
+		    xmlSecErrorsSafeString(xmlSecTransformKlassGetName(xmlSecTransformRelationshipId)));
+	return -1;
+    }
+
 #ifndef XMLSEC_NO_XSLT
     if(xmlSecTransformIdsRegister(xmlSecTransformXsltId) < 0) {
 	xmlSecError(XMLSEC_ERRORS_HERE,
diff --git a/src/xpath.c b/src/xpath.c
index 8b0b4f8..ddcd95d 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -17,6 +17,7 @@
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
 #include <libxml/xpointer.h>
+#include <libxml/c14n.h>
 
 #include <xmlsec/xmlsec.h>
 #include <xmlsec/xmltree.h>
@@ -1144,5 +1145,283 @@ xmlSecTransformVisa3DHackExecute(xmlSecTransformPtr transform, int last,
     return(0);
 }
 
+/* OOXML Relationship Transform. */
+typedef struct _xmlSecRelationshipCtx xmlSecRelationshipCtx, *xmlSecRelationshipCtxPtr;
+struct _xmlSecRelationshipCtx
+{
+    xmlParserCtxtPtr parserCtx;
+};
+#define xmlSecRelationshipSize (sizeof(xmlSecTransform) + sizeof(xmlSecRelationshipCtx))
+#define xmlSecRelationshipGetCtx(transform) ((xmlSecRelationshipCtxPtr)(((xmlSecByte*)(transform)) + sizeof(xmlSecTransform)))
+
+static int xmlSecRelationshipInitialize (xmlSecTransformPtr transform);
+static void xmlSecRelationshipFinalize (xmlSecTransformPtr transform);
+static int xmlSecTransformRelationshipPopBin(xmlSecTransformPtr transform, xmlSecByte* data, xmlSecSize maxDataSize, xmlSecSize* dataSize, xmlSecTransformCtxPtr transformCtx);
+static int xmlSecTransformRelationshipPushXml(xmlSecTransformPtr transform, xmlSecNodeSetPtr nodes, xmlSecTransformCtxPtr transformCtx);
+
+static xmlSecTransformKlass xmlSecRelationshipKlass =
+{
+    /* klass/object sizes */
+    sizeof(xmlSecTransformKlass), /* xmlSecSize klassSize */
+    xmlSecRelationshipSize, /* xmlSecSize objSize */
+    xmlSecNameRelationship, /* const xmlChar* name; */
+    xmlSecHrefRelationship, /* const xmlChar* href; */
+    xmlSecTransformUsageDSigTransform, /* xmlSecAlgorithmUsage usage; */
+    xmlSecRelationshipInitialize, /* xmlSecTransformInitializeMethod initialize; */
+    xmlSecRelationshipFinalize, /* xmlSecTransformFinalizeMethod finalize; */
+    NULL, /* xmlSecTransformNodeReadMethod readNode; */
+    NULL, /* xmlSecTransformNodeWriteMethod writeNode; */
+    NULL, /* xmlSecTransformSetKeyReqMethod setKeyReq; */
+    NULL, /* xmlSecTransformSetKeyMethod setKey; */
+    NULL, /* xmlSecTransformValidateMethod validate; */
+    xmlSecTransformDefaultGetDataType, /* xmlSecTransformGetDataTypeMethod getDataType; */
+    NULL, /* xmlSecTransformPushBinMethod pushBin; */
+    xmlSecTransformRelationshipPopBin, /* xmlSecTransformPopBinMethod popBin; */
+    xmlSecTransformRelationshipPushXml, /* xmlSecTransformPushXmlMethod pushXml; */
+    NULL, /* xmlSecTransformPopXmlMethod popXml; */
+    NULL, /* xmlSecTransformExecuteMethod execute; */
+    NULL, /* void* reserved0; */
+    NULL, /* void* reserved1; */
+};
+
+xmlSecTransformId xmlSecTransformRelationshipGetKlass(void)
+{
+    return &xmlSecRelationshipKlass;
+}
+
+static int xmlSecRelationshipInitialize(xmlSecTransformPtr transform)
+{
+    xmlSecRelationshipCtxPtr ctx;
+
+    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecTransformRelationshipId), -1);
+    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecRelationshipSize), -1);
+
+    ctx = xmlSecRelationshipGetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
+    /* initialize context */
+    memset(ctx, 0, sizeof(xmlSecRelationshipCtx));
+    return 0;
+}
+
+static void xmlSecRelationshipFinalize(xmlSecTransformPtr transform)
+{
+    xmlSecRelationshipCtxPtr ctx;
+
+    xmlSecAssert(xmlSecTransformCheckId(transform, xmlSecTransformRelationshipId));
+    xmlSecAssert(xmlSecTransformCheckSize(transform, xmlSecRelationshipSize));
+
+    ctx = xmlSecRelationshipGetCtx(transform);
+    xmlSecAssert(ctx != NULL);
+
+    if (ctx->parserCtx != NULL)
+	xmlFreeParserCtxt(ctx->parserCtx);
+
+    memset(ctx, 0, sizeof(xmlSecRelationshipCtx));
+}
+
+static int xmlSecTransformRelationshipPushXml(xmlSecTransformPtr transform, xmlSecNodeSetPtr nodes, xmlSecTransformCtxPtr transformCtx)
+{
+    xmlOutputBufferPtr buf;
+    int ret;
+
+    xmlSecAssert2(nodes != NULL, -1);
+    xmlSecAssert2(nodes->doc != NULL, -1);
+    xmlSecAssert2(transformCtx != NULL, -1);
+
+    /* check/update current transform status */
+    switch(transform->status)
+    {
+    case xmlSecTransformStatusNone:
+	transform->status = xmlSecTransformStatusWorking;
+	break;
+    case xmlSecTransformStatusWorking:
+    case xmlSecTransformStatusFinished:
+	return(0);
+    default:
+	xmlSecError(XMLSEC_ERRORS_HERE,
+		    xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+		    NULL,
+		    XMLSEC_ERRORS_R_INVALID_STATUS,
+		    "status=%d", transform->status);
+	return(-1);
+    }
+    xmlSecAssert2(transform->status == xmlSecTransformStatusWorking, -1);
+
+    /* prepare output buffer: next transform or ourselves */
+    if(transform->next != NULL)
+    {
+	buf = xmlSecTransformCreateOutputBuffer(transform->next, transformCtx);
+	if(buf == NULL)
+	{
+	    xmlSecError(XMLSEC_ERRORS_HERE,
+			xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			"xmlSecTransformCreateOutputBuffer",
+			XMLSEC_ERRORS_R_XMLSEC_FAILED,
+			XMLSEC_ERRORS_NO_MESSAGE);
+	    return(-1);
+	}
+    } else
+    {
+	buf = xmlSecBufferCreateOutputBuffer(&(transform->outBuf));
+	if (buf == NULL)
+	{
+	    xmlSecError(XMLSEC_ERRORS_HERE,
+			xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			"xmlSecBufferCreateOutputBuffer",
+			XMLSEC_ERRORS_R_XMLSEC_FAILED,
+			XMLSEC_ERRORS_NO_MESSAGE);
+	    return(-1);
+	}
+    }
 
+    ret = xmlC14NExecute(nodes->doc, (xmlC14NIsVisibleCallback)xmlSecNodeSetContains, nodes, XML_C14N_1_0, NULL, 0, buf);
+    if (ret < 0)
+    {
+	xmlSecError(XMLSEC_ERRORS_HERE,
+		    xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+		    "xmlC14NExecute",
+		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+		    XMLSEC_ERRORS_NO_MESSAGE);
+	xmlOutputBufferClose(buf);
+	return(-1);
+    }
 
+    ret = xmlOutputBufferClose(buf);
+    if (ret < 0)
+    {
+	xmlSecError(XMLSEC_ERRORS_HERE,
+		    xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+		    "xmlOutputBufferClose",
+		    XMLSEC_ERRORS_R_XML_FAILED,
+		    XMLSEC_ERRORS_NO_MESSAGE);
+	return(-1);
+    }
+    transform->status = xmlSecTransformStatusFinished;
+    return(0);
+}
+
+static int xmlSecTransformRelationshipPopBin(xmlSecTransformPtr transform, xmlSecByte* data, xmlSecSize maxDataSize, xmlSecSize* dataSize, xmlSecTransformCtxPtr transformCtx)
+{
+    xmlSecBufferPtr out;
+    int ret;
+
+    xmlSecAssert2(data != NULL, -1);
+    xmlSecAssert2(dataSize != NULL, -1);
+    xmlSecAssert2(transformCtx != NULL, -1);
+
+    out = &(transform->outBuf);
+    if (transform->status == xmlSecTransformStatusNone)
+    {
+	xmlOutputBufferPtr buf;
+
+	xmlSecAssert2(transform->inNodes == NULL, -1);
+
+	/* todo: isn't it an error? */
+	if (transform->prev == NULL)
+	{
+	    (*dataSize) = 0;
+	    transform->status = xmlSecTransformStatusFinished;
+	    return(0);
+	}
+
+	/* get xml data from previous transform */
+	ret = xmlSecTransformPopXml(transform->prev, &(transform->inNodes), transformCtx);
+	if (ret < 0)
+	{
+	    xmlSecError(XMLSEC_ERRORS_HERE,
+			xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			"xmlSecTransformPopXml",
+			XMLSEC_ERRORS_R_XMLSEC_FAILED,
+			XMLSEC_ERRORS_NO_MESSAGE);
+	    return(-1);
+	}
+
+	/* dump everything to internal buffer */
+	buf = xmlSecBufferCreateOutputBuffer(out);
+	if (buf == NULL)
+	{
+	    xmlSecError(XMLSEC_ERRORS_HERE,
+			xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			"xmlSecBufferCreateOutputBuffer",
+			XMLSEC_ERRORS_R_XMLSEC_FAILED,
+			XMLSEC_ERRORS_NO_MESSAGE);
+	    return(-1);
+	}
+
+        ret = xmlC14NExecute(transform->inNodes->doc, (xmlC14NIsVisibleCallback)xmlSecNodeSetContains, transform->inNodes, XML_C14N_1_0, NULL, 0, buf);
+        if (ret < 0)
+	{
+            xmlSecError(XMLSEC_ERRORS_HERE,
+			xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			"xmlSecTransformC14NExecute",
+			XMLSEC_ERRORS_R_XMLSEC_FAILED,
+			XMLSEC_ERRORS_NO_MESSAGE);
+	    xmlOutputBufferClose(buf);
+	    return(-1);
+	}
+	ret = xmlOutputBufferClose(buf);
+	if (ret < 0)
+	{
+	    xmlSecError(XMLSEC_ERRORS_HERE,
+			xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			"xmlOutputBufferClose",
+			XMLSEC_ERRORS_R_XML_FAILED,
+			XMLSEC_ERRORS_NO_MESSAGE);
+	    return(-1);
+	}
+	transform->status = xmlSecTransformStatusWorking;
+    }
+
+    if (transform->status == xmlSecTransformStatusWorking)
+    {
+	xmlSecSize outSize;
+
+	/* return chunk after chunk */
+	outSize = xmlSecBufferGetSize(out);
+	if (outSize > maxDataSize)
+	{
+	    outSize = maxDataSize;
+	}
+	if (outSize > XMLSEC_TRANSFORM_BINARY_CHUNK)
+	{
+	    outSize = XMLSEC_TRANSFORM_BINARY_CHUNK;
+	}
+	if (outSize > 0)
+	{
+	    xmlSecAssert2(xmlSecBufferGetData(&(transform->outBuf)), -1);
+
+	    memcpy(data, xmlSecBufferGetData(&(transform->outBuf)), outSize);
+	    ret = xmlSecBufferRemoveHead(&(transform->outBuf), outSize);
+            if (ret < 0)
+	    {
+		xmlSecError(XMLSEC_ERRORS_HERE,
+			    xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+			    "xmlSecBufferRemoveHead",
+			    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+			    "size=%d", outSize);
+		return(-1);
+	    }
+	}
+	else if (xmlSecBufferGetSize(out) == 0)
+	    transform->status = xmlSecTransformStatusFinished;
+	(*dataSize) = outSize;
+    }
+    else if (transform->status == xmlSecTransformStatusFinished)
+    {
+	/* the only way we can get here is if there is no output */
+	xmlSecAssert2(xmlSecBufferGetSize(out) == 0, -1);
+	(*dataSize) = 0;
+    }
+    else
+    {
+	xmlSecError(XMLSEC_ERRORS_HERE,
+		    xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+		    NULL,
+		    XMLSEC_ERRORS_R_INVALID_STATUS,
+		    "status=%d", transform->status);
+	return(-1);
+    }
+
+    return(0);
+}
-- 
2.6.2