summaryrefslogtreecommitdiff
path: root/external/libxmlsec/xmlsec1-ooxml.patch.1
blob: a0878a5222d31e84cacfd6cb411c818fd0c70256 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
From 24c1395c547f8a2f0c78a8618c766ecd55909c66 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

---
 include/xmlsec/strings.h    |   3 +
 include/xmlsec/transforms.h |   4 +
 src/strings.c               |   3 +
 src/transforms.c            |  11 +
 src/xpath.c                 | 542 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 563 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..b9a03e5 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,546 @@ xmlSecTransformVisa3DHackExecute(xmlSecTransformPtr transform, int last,
     return(0);
 }
 
+/* OOXML Relationship Transform. */
+typedef struct _xmlSecRelationshipCtx xmlSecRelationshipCtx, *xmlSecRelationshipCtxPtr;
+struct _xmlSecRelationshipCtx
+{
+    xmlSecPtrListPtr sourceIdList;
+};
+#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 int xmlSecRelationshipReadNode(xmlSecTransformPtr transform, xmlNodePtr node, 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; */
+    xmlSecRelationshipReadNode, /* 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;
+    int ret;
+
+    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecTransformRelationshipId), -1);
+    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecRelationshipSize), -1);
+
+    ctx = xmlSecRelationshipGetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
+    /* initialize context */
+    memset(ctx, 0, sizeof(xmlSecRelationshipCtx));
+
+    ctx->sourceIdList = xmlSecPtrListCreate(xmlSecStringListId);
+    if (!ctx->sourceIdList)
+    {
+        xmlSecError(XMLSEC_ERRORS_HERE,
+                    xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+                    "xmlSecPtrListCreate",
+                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                    XMLSEC_ERRORS_NO_MESSAGE);
+        return -1;
+    }
+
+    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->sourceIdList)
+	xmlSecPtrListDestroy(ctx->sourceIdList);
+
+    memset(ctx, 0, sizeof(xmlSecRelationshipCtx));
+}
+
+static int xmlSecRelationshipReadNode(xmlSecTransformPtr transform, xmlNodePtr node, xmlSecTransformCtxPtr transformCtx)
+{
+    xmlSecRelationshipCtxPtr ctx;
+    xmlNodePtr cur;
+    int ret;
+
+    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecTransformRelationshipId), -1);
+    xmlSecAssert2(xmlSecTransformCheckSize(transform, xmlSecRelationshipSize), -1);
+    xmlSecAssert2(node != NULL, -1);
+    xmlSecAssert2(transformCtx != NULL, -1);
+    ctx = xmlSecRelationshipGetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
+    cur = node->children;
+    while (cur)
+    {
+        if (xmlSecCheckNodeName(cur, "RelationshipReference", "http://schemas.openxmlformats.org/package/2006/digital-signature"))
+        {
+            xmlChar* sourceId;
+            xmlChar* tmp;
+
+            sourceId = xmlGetProp(cur, "SourceId");
+            if (sourceId == NULL)
+            {
+                xmlSecError(XMLSEC_ERRORS_HERE,
+                            NULL,
+                            xmlSecErrorsSafeString("SourceId"),
+                            XMLSEC_ERRORS_R_INVALID_NODE_ATTRIBUTE,
+                            "node=%s",
+                            xmlSecErrorsSafeString(xmlSecNodeGetName(node)));
+                return -1;
+            }
+
+            tmp = xmlStrdup(sourceId);
+            if (!tmp)
+            {
+                xmlSecError(XMLSEC_ERRORS_HERE,
+                            xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+                            NULL,
+                            XMLSEC_ERRORS_R_STRDUP_FAILED,
+                            "len=%d", xmlStrlen(sourceId));
+                return -1;
+            }
+
+            ret = xmlSecPtrListAdd(ctx->sourceIdList, tmp);
+            if (ret < 0)
+            {
+                xmlSecError(XMLSEC_ERRORS_HERE,
+                            xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+                            "xmlSecPtrListAdd",
+                            XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                            XMLSEC_ERRORS_NO_MESSAGE);
+                xmlFree(tmp);
+                return -1;
+            }
+
+        }
+
+        cur = cur->next;
+    }
+
+    return 0;
+}
+
+int xmlSecTransformRelationshipProcessElementNode(xmlSecTransformPtr transform, xmlOutputBufferPtr buf, xmlNodePtr cur)
+{
+    xmlAttrPtr attr;
+    int foundTargetMode = 0;
+
+    xmlOutputBufferWriteString(buf, "<");
+    xmlOutputBufferWriteString(buf, (const char *)cur->name);
+
+    if (cur->nsDef)
+    {
+        xmlOutputBufferWriteString(buf, " xmlns=\"");
+        if (cur->nsDef->href)
+            xmlOutputBufferWriteString(buf, cur->nsDef->href);
+        xmlOutputBufferWriteString(buf, "\"");
+    }
+
+    for (attr = cur->properties; attr; attr = attr->next)
+    {
+        xmlOutputBufferWriteString(buf, " ");
+        xmlOutputBufferWriteString(buf, (const char *)attr->name);
+        if (strcmp(attr->name, "TargetMode") == 0)
+            foundTargetMode = 1;
+        xmlOutputBufferWriteString(buf, "=\"");
+        xmlOutputBufferWriteString(buf, (const char *)xmlGetProp(cur, attr->name));
+        xmlOutputBufferWriteString(buf, "\"");
+    }
+
+    if (strcmp(cur->name, "Relationship") == 0 && !foundTargetMode)
+        xmlOutputBufferWriteString(buf, " TargetMode=\"Internal\"");
+
+    xmlOutputBufferWriteString(buf, ">");
+
+    if (cur->children)
+    {
+        int ret = xmlSecTransformRelationshipProcessNodeList(transform, buf, cur->children);
+        if (ret < 0)
+            return -1;
+    }
+
+    xmlOutputBufferWriteString(buf, "</");
+    xmlOutputBufferWriteString(buf, (const char *)cur->name);
+    xmlOutputBufferWriteString(buf, ">");
+
+    return 0;
+}
+
+int xmlSecTransformRelationshipProcessNode(xmlSecTransformPtr transform, xmlOutputBufferPtr buf, xmlNodePtr cur)
+{
+    int ret;
+
+    switch (cur->type)
+    {
+    case XML_ELEMENT_NODE:
+        if (xmlSecCheckNodeName(cur, "Relationship", "http://schemas.openxmlformats.org/package/2006/relationships"))
+        {
+            xmlChar* id = xmlGetProp(cur, "Id");
+            if (id)
+            {
+                int i;
+                int found = 0;
+                xmlSecRelationshipCtxPtr ctx;
+
+                ctx = xmlSecRelationshipGetCtx(transform);
+                for (i = 0; i < xmlSecPtrListGetSize(ctx->sourceIdList); ++i)
+                {
+                    if (strcmp(xmlSecPtrListGetItem(ctx->sourceIdList, i), id) == 0)
+                    {
+                        found = 1;
+                        break;
+                    }
+                }
+
+                if (!found)
+                    return 0;
+            }
+        }
+
+        ret = xmlSecTransformRelationshipProcessElementNode(transform, buf, cur);
+        if (ret < 0)
+            return -1;
+        break;
+    }
+
+    return 0;
+}
+
+/* Sorts Relationship elements by Id value in lexicographical order. */
+static int xmlSecTransformRelationshipCompare(xmlNodePtr node1, xmlNodePtr node2)
+{
+    xmlChar* id1;
+    xmlChar* id2;
+
+    if (node1 == node2)
+        return 0;
+    if (!node1)
+        return -1;
+    if (!node2)
+        return 1;
+
+    id1 = xmlGetProp(node1, "Id");
+    id2 = xmlGetProp(node2, "Id");
+
+    if (!id1)
+        return -1;
+    if (!id2)
+        return 1;
+
+    return xmlStrcmp(id1, id2);
+}
+
+int xmlSecTransformRelationshipProcessNodeList(xmlSecTransformPtr transform, xmlOutputBufferPtr buf, xmlNodePtr cur)
+{
+    xmlListPtr list;
+    int ret;
+
+    list = xmlListCreate(NULL, (xmlListDataCompare)xmlSecTransformRelationshipCompare);
+    if (!list)
+        return -1;
+
+    for (; cur; cur = cur->next)
+    {
+        if (strcmp(cur->name, "Relationship") == 0)
+            xmlListInsert(list, cur);
+        else
+        {
+            ret = xmlSecTransformRelationshipProcessNode(transform, buf, cur);
+            if (ret < 0)
+            {
+                xmlListDelete(list);
+                return -1;
+            }
+        }
+    }
+
+    if (!xmlListEmpty(list))
+    {
+        int size;
+        int i;
 
+        xmlListSort(list);
+        size = xmlListSize(list);
+        for (i = 0; i < size; ++i)
+        {
+            xmlLinkPtr link = xmlListFront(list);
+            xmlNodePtr node = (xmlNodePtr)xmlLinkGetData(link);
 
+            ret = xmlSecTransformRelationshipProcessNode(transform, buf, node);
+            if (ret < 0)
+            {
+                xmlListDelete(list);
+                return -1;
+            }
+
+            xmlListPopFront(list);
+        }
+
+    }
+
+    xmlListDelete(list);
+    return 0;
+}
+
+int xmlSecTransformRelationshipExecute(xmlSecTransformPtr transform, xmlOutputBufferPtr buf, xmlDocPtr doc)
+{
+    if (doc->children != NULL)
+    {
+        int ret = xmlSecTransformRelationshipProcessNodeList(transform, buf, doc->children);
+        if (ret < 0)
+        {
+            xmlSecError(XMLSEC_ERRORS_HERE,
+                        xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+                        "xmlSecTransformRelationshipProcessNodeList",
+                        XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                        XMLSEC_ERRORS_NO_MESSAGE);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+static int xmlSecTransformRelationshipPushXml(xmlSecTransformPtr transform, xmlSecNodeSetPtr nodes, xmlSecTransformCtxPtr transformCtx)
+{
+    xmlOutputBufferPtr buf;
+    xmlSecRelationshipCtxPtr ctx;
+    int ret;
+
+    xmlSecAssert2(nodes != NULL, -1);
+    xmlSecAssert2(nodes->doc != NULL, -1);
+    xmlSecAssert2(transformCtx != NULL, -1);
+
+    ctx = xmlSecRelationshipGetCtx(transform);
+    xmlSecAssert2(ctx != 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 = xmlSecTransformRelationshipExecute(transform, buf, nodes->doc);
+    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