Line data Source code
1 : /**
2 : * XML Security Library (http://www.aleksey.com/xmlsec).
3 : *
4 : * Creating signature and encryption templates.
5 : *
6 : * This is free software; see Copyright file in the source
7 : * distribution for preciese wording.
8 : *
9 : * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
10 : */
11 : #include "globals.h"
12 :
13 : #include <stdlib.h>
14 : #include <string.h>
15 :
16 : #include <libxml/tree.h>
17 :
18 : #include <xmlsec/xmlsec.h>
19 : #include <xmlsec/xmltree.h>
20 : #include <xmlsec/transforms.h>
21 : #include <xmlsec/strings.h>
22 : #include <xmlsec/base64.h>
23 : #include <xmlsec/templates.h>
24 : #include <xmlsec/errors.h>
25 :
26 :
27 : static xmlNodePtr xmlSecTmplAddReference (xmlNodePtr parentNode,
28 : xmlSecTransformId digestMethodId,
29 : const xmlChar *id,
30 : const xmlChar *uri,
31 : const xmlChar *type);
32 : static int xmlSecTmplPrepareEncData (xmlNodePtr parentNode,
33 : xmlSecTransformId encMethodId);
34 : static int xmlSecTmplNodeWriteNsList (xmlNodePtr parentNode,
35 : const xmlChar** namespaces);
36 : /**************************************************************************
37 : *
38 : * <dsig:Signature/> node
39 : *
40 : **************************************************************************/
41 : /**
42 : * xmlSecTmplSignatureCreate:
43 : * @doc: the pointer to signature document or NULL; in the
44 : * second case, application must later call @xmlSetTreeDoc
45 : * to ensure that all the children nodes have correct
46 : * pointer to XML document.
47 : * @c14nMethodId: the signature canonicalization method.
48 : * @signMethodId: the signature method.
49 : * @id: the node id (may be NULL).
50 : *
51 : * Creates new <dsig:Signature/> node with the mandatory <dsig:SignedInfo/>,
52 : * <dsig:CanonicalizationMethod/>, <dsig:SignatureMethod/> and
53 : * <dsig:SignatureValue/> children and sub-children.
54 : * The application is responsible for inserting the returned node
55 : * in the XML document.
56 : *
57 : * Returns: the pointer to newly created <dsig:Signature/> node or NULL if an
58 : * error occurs.
59 : */
60 : xmlNodePtr
61 0 : xmlSecTmplSignatureCreate(xmlDocPtr doc, xmlSecTransformId c14nMethodId,
62 : xmlSecTransformId signMethodId, const xmlChar *id) {
63 0 : return xmlSecTmplSignatureCreateNsPref(doc, c14nMethodId, signMethodId, id, NULL);
64 : }
65 :
66 : /**
67 : * xmlSecTmplSignatureCreateNsPref:
68 : * @doc: the pointer to signature document or NULL; in the
69 : * second case, application must later call @xmlSetTreeDoc
70 : * to ensure that all the children nodes have correct
71 : * pointer to XML document.
72 : * @c14nMethodId: the signature canonicalization method.
73 : * @signMethodId: the signature method.
74 : * @id: the node id (may be NULL).
75 : * @nsPrefix: the namespace prefix for the signature element (e.g. "dsig"), or NULL
76 : *
77 : * Creates new <dsig:Signature/> node with the mandatory
78 : * <dsig:SignedInfo/>, <dsig:CanonicalizationMethod/>,
79 : * <dsig:SignatureMethod/> and <dsig:SignatureValue/> children and
80 : * sub-children. This method differs from xmlSecTmplSignatureCreate in
81 : * that it will define the http://www.w3.org/2000/09/xmldsig#
82 : * namespace with the given prefix that will be used for all of the
83 : * appropriate child nodes. The application is responsible for
84 : * inserting the returned node in the XML document.
85 : *
86 : * Returns: the pointer to newly created <dsig:Signature/> node or NULL if an
87 : * error occurs.
88 : */
89 : xmlNodePtr
90 0 : xmlSecTmplSignatureCreateNsPref(xmlDocPtr doc, xmlSecTransformId c14nMethodId,
91 : xmlSecTransformId signMethodId, const xmlChar *id,
92 : const xmlChar* nsPrefix) {
93 : xmlNodePtr signNode;
94 : xmlNodePtr signedInfoNode;
95 : xmlNodePtr cur;
96 : xmlNsPtr ns;
97 :
98 0 : xmlSecAssert2(c14nMethodId != NULL, NULL);
99 0 : xmlSecAssert2(c14nMethodId->href != NULL, NULL);
100 0 : xmlSecAssert2(signMethodId != NULL, NULL);
101 0 : xmlSecAssert2(signMethodId->href != NULL, NULL);
102 :
103 : /* create Signature node itself */
104 0 : signNode = xmlNewDocNode(doc, NULL, xmlSecNodeSignature, NULL);
105 0 : if(signNode == NULL) {
106 0 : xmlSecError(XMLSEC_ERRORS_HERE,
107 : NULL,
108 : "xmlNewDocNode",
109 : XMLSEC_ERRORS_R_XML_FAILED,
110 : "node=%s",
111 : xmlSecErrorsSafeString(xmlSecNodeSignature));
112 0 : return(NULL);
113 : }
114 :
115 0 : ns = xmlNewNs(signNode, xmlSecDSigNs, nsPrefix);
116 0 : if(ns == NULL) {
117 0 : xmlSecError(XMLSEC_ERRORS_HERE,
118 : NULL,
119 : "xmlNewNs",
120 : XMLSEC_ERRORS_R_XML_FAILED,
121 : "ns=%s",
122 : xmlSecErrorsSafeString(xmlSecDSigNs));
123 0 : xmlFreeNode(signNode);
124 0 : return(NULL);
125 : }
126 0 : xmlSetNs(signNode, ns);
127 :
128 0 : if(id != NULL) {
129 0 : xmlSetProp(signNode, BAD_CAST "Id", id);
130 : }
131 :
132 : /* add SignedInfo node */
133 0 : signedInfoNode = xmlSecAddChild(signNode, xmlSecNodeSignedInfo, xmlSecDSigNs);
134 0 : if(signedInfoNode == NULL) {
135 0 : xmlSecError(XMLSEC_ERRORS_HERE,
136 : NULL,
137 : "xmlSecAddChild",
138 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
139 : "node=%s",
140 : xmlSecErrorsSafeString(xmlSecNodeSignedInfo));
141 0 : xmlFreeNode(signNode);
142 0 : return(NULL);
143 : }
144 :
145 : /* add SignatureValue node */
146 0 : cur = xmlSecAddChild(signNode, xmlSecNodeSignatureValue, xmlSecDSigNs);
147 0 : if(cur == NULL) {
148 0 : xmlSecError(XMLSEC_ERRORS_HERE,
149 : NULL,
150 : "xmlSecAddChild",
151 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
152 : "node=%s",
153 : xmlSecErrorsSafeString(xmlSecNodeSignatureValue));
154 0 : xmlFreeNode(signNode);
155 0 : return(NULL);
156 : }
157 :
158 : /* add CanonicaizationMethod node to SignedInfo */
159 0 : cur = xmlSecAddChild(signedInfoNode, xmlSecNodeCanonicalizationMethod, xmlSecDSigNs);
160 0 : if(cur == NULL) {
161 0 : xmlSecError(XMLSEC_ERRORS_HERE,
162 : NULL,
163 : "xmlSecAddChild",
164 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
165 : "node=%s",
166 : xmlSecErrorsSafeString(xmlSecNodeCanonicalizationMethod));
167 0 : xmlFreeNode(signNode);
168 0 : return(NULL);
169 : }
170 0 : if(xmlSetProp(cur, xmlSecAttrAlgorithm, c14nMethodId->href) == NULL) {
171 0 : xmlSecError(XMLSEC_ERRORS_HERE,
172 : NULL,
173 : "xmlSetProp",
174 : XMLSEC_ERRORS_R_XML_FAILED,
175 : "name=%s,value=%s",
176 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
177 0 : xmlSecErrorsSafeString(c14nMethodId->href));
178 0 : xmlFreeNode(signNode);
179 0 : return(NULL);
180 : }
181 :
182 : /* add SignatureMethod node to SignedInfo */
183 0 : cur = xmlSecAddChild(signedInfoNode, xmlSecNodeSignatureMethod, xmlSecDSigNs);
184 0 : if(cur == NULL) {
185 0 : xmlSecError(XMLSEC_ERRORS_HERE,
186 : NULL,
187 : "xmlSecAddChild",
188 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
189 : "node=%s",
190 : xmlSecErrorsSafeString(xmlSecNodeSignatureMethod));
191 0 : xmlFreeNode(signNode);
192 0 : return(NULL);
193 : }
194 0 : if(xmlSetProp(cur, xmlSecAttrAlgorithm, signMethodId->href) == NULL) {
195 0 : xmlSecError(XMLSEC_ERRORS_HERE,
196 : NULL,
197 : "xmlSetProp",
198 : XMLSEC_ERRORS_R_XML_FAILED,
199 : "name=%s,value=%s",
200 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
201 0 : xmlSecErrorsSafeString(signMethodId->href));
202 0 : xmlFreeNode(signNode);
203 0 : return(NULL);
204 : }
205 :
206 0 : return(signNode);
207 : }
208 :
209 : /**
210 : * xmlSecTmplSignatureEnsureKeyInfo:
211 : * @signNode: the pointer to <dsig:Signature/> node.
212 : * @id: the node id (may be NULL).
213 : *
214 : * Adds (if necessary) <dsig:KeyInfo/> node to the <dsig:Signature/>
215 : * node @signNode.
216 : *
217 : * Returns: the pointer to newly created <dsig:KeyInfo/> node or NULL if an
218 : * error occurs.
219 : */
220 : xmlNodePtr
221 0 : xmlSecTmplSignatureEnsureKeyInfo(xmlNodePtr signNode, const xmlChar *id) {
222 : xmlNodePtr res;
223 :
224 0 : xmlSecAssert2(signNode != NULL, NULL);
225 :
226 0 : res = xmlSecFindChild(signNode, xmlSecNodeKeyInfo, xmlSecDSigNs);
227 0 : if(res == NULL) {
228 : xmlNodePtr signValueNode;
229 :
230 0 : signValueNode = xmlSecFindChild(signNode, xmlSecNodeSignatureValue, xmlSecDSigNs);
231 0 : if(signValueNode == NULL) {
232 0 : xmlSecError(XMLSEC_ERRORS_HERE,
233 : NULL,
234 : xmlSecErrorsSafeString(xmlSecNodeSignatureValue),
235 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
236 : XMLSEC_ERRORS_NO_MESSAGE);
237 0 : return(NULL);
238 : }
239 :
240 0 : res = xmlSecAddNextSibling(signValueNode, xmlSecNodeKeyInfo, xmlSecDSigNs);
241 0 : if(res == NULL) {
242 0 : xmlSecError(XMLSEC_ERRORS_HERE,
243 : NULL,
244 : "xmlSecAddNextSibling",
245 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
246 : "node=%s",
247 : xmlSecErrorsSafeString(xmlSecNodeKeyInfo));
248 0 : return(NULL);
249 : }
250 : }
251 0 : if(id != NULL) {
252 0 : xmlSetProp(res, xmlSecAttrId, id);
253 : }
254 0 : return(res);
255 : }
256 :
257 : /**
258 : * xmlSecTmplSignatureAddReference:
259 : * @signNode: the pointer to <dsig:Signature/> node.
260 : * @digestMethodId: the reference digest method.
261 : * @id: the node id (may be NULL).
262 : * @uri: the reference node uri (may be NULL).
263 : * @type: the reference node type (may be NULL).
264 : *
265 : * Adds <dsig:Reference/> node with given URI (@uri), Id (@id) and
266 : * Type (@type) attributes and the required children <dsig:DigestMethod/> and
267 : * <dsig:DigestValue/> to the <dsig:SignedInfo/> child of @signNode.
268 : *
269 : * Returns: the pointer to newly created <dsig:Reference/> node or NULL
270 : * if an error occurs.
271 : */
272 : xmlNodePtr
273 0 : xmlSecTmplSignatureAddReference(xmlNodePtr signNode, xmlSecTransformId digestMethodId,
274 : const xmlChar *id, const xmlChar *uri, const xmlChar *type) {
275 : xmlNodePtr signedInfoNode;
276 :
277 0 : xmlSecAssert2(signNode != NULL, NULL);
278 0 : xmlSecAssert2(digestMethodId != NULL, NULL);
279 0 : xmlSecAssert2(digestMethodId->href != NULL, NULL);
280 :
281 0 : signedInfoNode = xmlSecFindChild(signNode, xmlSecNodeSignedInfo, xmlSecDSigNs);
282 0 : if(signedInfoNode == NULL) {
283 0 : xmlSecError(XMLSEC_ERRORS_HERE,
284 : NULL,
285 : xmlSecErrorsSafeString(xmlSecNodeSignedInfo),
286 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
287 : XMLSEC_ERRORS_NO_MESSAGE);
288 0 : return(NULL);
289 : }
290 :
291 0 : return(xmlSecTmplAddReference(signedInfoNode, digestMethodId, id, uri, type));
292 : }
293 :
294 : static xmlNodePtr
295 0 : xmlSecTmplAddReference(xmlNodePtr parentNode, xmlSecTransformId digestMethodId,
296 : const xmlChar *id, const xmlChar *uri, const xmlChar *type) {
297 : xmlNodePtr res;
298 : xmlNodePtr cur;
299 :
300 0 : xmlSecAssert2(parentNode != NULL, NULL);
301 0 : xmlSecAssert2(digestMethodId != NULL, NULL);
302 0 : xmlSecAssert2(digestMethodId->href != NULL, NULL);
303 :
304 : /* add Reference node */
305 0 : res = xmlSecAddChild(parentNode, xmlSecNodeReference, xmlSecDSigNs);
306 0 : if(res == NULL) {
307 0 : xmlSecError(XMLSEC_ERRORS_HERE,
308 : NULL,
309 : "xmlSecAddChild",
310 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
311 : "node=%s",
312 : xmlSecErrorsSafeString(xmlSecNodeReference));
313 0 : return(NULL);
314 : }
315 :
316 : /* set Reference node attributes */
317 0 : if(id != NULL) {
318 0 : xmlSetProp(res, xmlSecAttrId, id);
319 : }
320 0 : if(type != NULL) {
321 0 : xmlSetProp(res, xmlSecAttrType, type);
322 : }
323 0 : if(uri != NULL) {
324 0 : xmlSetProp(res, xmlSecAttrURI, uri);
325 : }
326 :
327 : /* add DigestMethod node and set algorithm */
328 0 : cur = xmlSecAddChild(res, xmlSecNodeDigestMethod, xmlSecDSigNs);
329 0 : if(cur == NULL) {
330 0 : xmlSecError(XMLSEC_ERRORS_HERE,
331 : NULL,
332 : "xmlSecAddChild",
333 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
334 : "node=%s",
335 : xmlSecErrorsSafeString(xmlSecNodeDigestMethod));
336 0 : xmlUnlinkNode(res);
337 0 : xmlFreeNode(res);
338 0 : return(NULL);
339 : }
340 0 : if(xmlSetProp(cur, xmlSecAttrAlgorithm, digestMethodId->href) == NULL) {
341 0 : xmlSecError(XMLSEC_ERRORS_HERE,
342 : NULL,
343 : "xmlSetProp",
344 : XMLSEC_ERRORS_R_XML_FAILED,
345 : "name=%s,value=%s",
346 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
347 0 : xmlSecErrorsSafeString(digestMethodId->href));
348 0 : xmlUnlinkNode(res);
349 0 : xmlFreeNode(res);
350 0 : return(NULL);
351 : }
352 :
353 : /* add DigestValue node */
354 0 : cur = xmlSecAddChild(res, xmlSecNodeDigestValue, xmlSecDSigNs);
355 0 : if(cur == NULL) {
356 0 : xmlSecError(XMLSEC_ERRORS_HERE,
357 : NULL,
358 : "xmlSecAddChild",
359 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
360 : "node=%s",
361 : xmlSecErrorsSafeString(xmlSecNodeDigestValue));
362 0 : xmlUnlinkNode(res);
363 0 : xmlFreeNode(res);
364 0 : return(NULL);
365 : }
366 :
367 0 : return(res);
368 : }
369 :
370 : /**
371 : * xmlSecTmplSignatureAddObject:
372 : * @signNode: the pointer to <dsig:Signature/> node.
373 : * @id: the node id (may be NULL).
374 : * @mimeType: the object mime type (may be NULL).
375 : * @encoding: the object encoding (may be NULL).
376 : *
377 : * Adds <dsig:Object/> node to the <dsig:Signature/> node @signNode.
378 : *
379 : * Returns: the pointer to newly created <dsig:Object/> node or NULL
380 : * if an error occurs.
381 : */
382 : xmlNodePtr
383 0 : xmlSecTmplSignatureAddObject(xmlNodePtr signNode, const xmlChar *id,
384 : const xmlChar *mimeType, const xmlChar *encoding) {
385 : xmlNodePtr res;
386 :
387 0 : xmlSecAssert2(signNode != NULL, NULL);
388 :
389 0 : res = xmlSecAddChild(signNode, xmlSecNodeObject, xmlSecDSigNs);
390 0 : if(res == NULL) {
391 0 : xmlSecError(XMLSEC_ERRORS_HERE,
392 : NULL,
393 : "xmlSecAddChild",
394 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
395 : "node=%s",
396 : xmlSecErrorsSafeString(xmlSecNodeObject));
397 0 : return(NULL);
398 : }
399 0 : if(id != NULL) {
400 0 : xmlSetProp(res, xmlSecAttrId, id);
401 : }
402 0 : if(mimeType != NULL) {
403 0 : xmlSetProp(res, xmlSecAttrMimeType, mimeType);
404 : }
405 0 : if(encoding != NULL) {
406 0 : xmlSetProp(res, xmlSecAttrEncoding, encoding);
407 : }
408 0 : return(res);
409 : }
410 :
411 : /**
412 : * xmlSecTmplSignatureGetSignMethodNode:
413 : * @signNode: the pointer to <dsig:Signature /> node.
414 : *
415 : * Gets pointer to <dsig:SignatureMethod/> child of <dsig:KeyInfo/> node.
416 : *
417 : * Returns: pointer to <dsig:SignatureMethod /> node or NULL if an error occurs.
418 : */
419 : xmlNodePtr
420 0 : xmlSecTmplSignatureGetSignMethodNode(xmlNodePtr signNode) {
421 : xmlNodePtr signedInfoNode;
422 :
423 0 : xmlSecAssert2(signNode != NULL, NULL);
424 :
425 0 : signedInfoNode = xmlSecFindChild(signNode, xmlSecNodeSignedInfo, xmlSecDSigNs);
426 0 : if(signedInfoNode == NULL) {
427 0 : xmlSecError(XMLSEC_ERRORS_HERE,
428 : NULL,
429 : xmlSecErrorsSafeString(xmlSecNodeSignedInfo),
430 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
431 : XMLSEC_ERRORS_NO_MESSAGE);
432 0 : return(NULL);
433 : }
434 0 : return(xmlSecFindChild(signedInfoNode, xmlSecNodeSignatureMethod, xmlSecDSigNs));
435 : }
436 :
437 : /**
438 : * xmlSecTmplSignatureGetC14NMethodNode:
439 : * @signNode: the pointer to <dsig:Signature /> node.
440 : *
441 : * Gets pointer to <dsig:CanonicalizationMethod/> child of <dsig:KeyInfo/> node.
442 : *
443 : * Returns: pointer to <dsig:CanonicalizationMethod /> node or NULL if an error occurs.
444 : */
445 : xmlNodePtr
446 0 : xmlSecTmplSignatureGetC14NMethodNode(xmlNodePtr signNode) {
447 : xmlNodePtr signedInfoNode;
448 :
449 0 : xmlSecAssert2(signNode != NULL, NULL);
450 :
451 0 : signedInfoNode = xmlSecFindChild(signNode, xmlSecNodeSignedInfo, xmlSecDSigNs);
452 0 : if(signedInfoNode == NULL) {
453 0 : xmlSecError(XMLSEC_ERRORS_HERE,
454 : NULL,
455 : xmlSecErrorsSafeString(xmlSecNodeSignedInfo),
456 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
457 : XMLSEC_ERRORS_NO_MESSAGE);
458 0 : return(NULL);
459 : }
460 0 : return(xmlSecFindChild(signedInfoNode, xmlSecNodeCanonicalizationMethod, xmlSecDSigNs));
461 : }
462 :
463 : /**
464 : * xmlSecTmplReferenceAddTransform:
465 : * @referenceNode: the pointer to <dsig:Reference/> node.
466 : * @transformId: the transform method id.
467 : *
468 : * Adds <dsig:Transform/> node to the <dsig:Reference/> node @referenceNode.
469 : *
470 : * Returns: the pointer to newly created <dsig:Transform/> node or NULL if an
471 : * error occurs.
472 : */
473 : xmlNodePtr
474 0 : xmlSecTmplReferenceAddTransform(xmlNodePtr referenceNode, xmlSecTransformId transformId) {
475 : xmlNodePtr transformsNode;
476 : xmlNodePtr res;
477 :
478 0 : xmlSecAssert2(referenceNode != NULL, NULL);
479 0 : xmlSecAssert2(transformId != NULL, NULL);
480 0 : xmlSecAssert2(transformId->href != NULL, NULL);
481 :
482 : /* do we need to create Transforms node first */
483 0 : transformsNode = xmlSecFindChild(referenceNode, xmlSecNodeTransforms, xmlSecDSigNs);
484 0 : if(transformsNode == NULL) {
485 : xmlNodePtr tmp;
486 :
487 0 : tmp = xmlSecGetNextElementNode(referenceNode->children);
488 0 : if(tmp == NULL) {
489 0 : transformsNode = xmlSecAddChild(referenceNode, xmlSecNodeTransforms, xmlSecDSigNs);
490 : } else {
491 0 : transformsNode = xmlSecAddPrevSibling(tmp, xmlSecNodeTransforms, xmlSecDSigNs);
492 : }
493 0 : if(transformsNode == NULL) {
494 0 : xmlSecError(XMLSEC_ERRORS_HERE,
495 : NULL,
496 : "xmlSecAddChild or xmlSecAddPrevSibling",
497 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
498 : "node=%s",
499 : xmlSecErrorsSafeString(xmlSecNodeTransforms));
500 0 : return(NULL);
501 : }
502 : }
503 :
504 0 : res = xmlSecAddChild(transformsNode, xmlSecNodeTransform, xmlSecDSigNs);
505 0 : if(res == NULL) {
506 0 : xmlSecError(XMLSEC_ERRORS_HERE,
507 : NULL,
508 : "xmlSecAddChild",
509 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
510 : "node=%s",
511 : xmlSecErrorsSafeString(xmlSecNodeTransform));
512 0 : return(NULL);
513 : }
514 :
515 0 : if(xmlSetProp(res, xmlSecAttrAlgorithm, transformId->href) == NULL) {
516 0 : xmlSecError(XMLSEC_ERRORS_HERE,
517 : NULL,
518 : "xmlSetProp",
519 : XMLSEC_ERRORS_R_XML_FAILED,
520 : "name=%s,value=%s",
521 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
522 0 : xmlSecErrorsSafeString(transformId->href));
523 0 : xmlUnlinkNode(res);
524 0 : xmlFreeNode(res);
525 0 : return(NULL);
526 : }
527 :
528 0 : return(res);
529 : }
530 :
531 : /**
532 : * xmlSecTmplObjectAddSignProperties:
533 : * @objectNode: the pointer to <dsig:Object/> node.
534 : * @id: the node id (may be NULL).
535 : * @target: the Target (may be NULL).
536 : *
537 : * Adds <dsig:SignatureProperties/> node to the <dsig:Object/> node @objectNode.
538 : *
539 : * Returns: the pointer to newly created <dsig:SignatureProperties/> node or NULL
540 : * if an error occurs.
541 : */
542 : xmlNodePtr
543 0 : xmlSecTmplObjectAddSignProperties(xmlNodePtr objectNode, const xmlChar *id, const xmlChar *target) {
544 : xmlNodePtr res;
545 :
546 0 : xmlSecAssert2(objectNode != NULL, NULL);
547 :
548 0 : res = xmlSecAddChild(objectNode, xmlSecNodeSignatureProperties, xmlSecDSigNs);
549 0 : if(res == NULL) {
550 0 : xmlSecError(XMLSEC_ERRORS_HERE,
551 : NULL,
552 : "xmlSecAddChild",
553 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
554 : "node=%s",
555 : xmlSecErrorsSafeString(xmlSecNodeSignatureProperties));
556 0 : return(NULL);
557 : }
558 0 : if(id != NULL) {
559 0 : xmlSetProp(res, xmlSecAttrId, id);
560 : }
561 0 : if(target != NULL) {
562 0 : xmlSetProp(res, xmlSecAttrTarget, target);
563 : }
564 0 : return(res);
565 : }
566 :
567 : /**
568 : * xmlSecTmplObjectAddManifest:
569 : * @objectNode: the pointer to <dsig:Object/> node.
570 : * @id: the node id (may be NULL).
571 : *
572 : * Adds <dsig:Manifest/> node to the <dsig:Object/> node @objectNode.
573 : *
574 : * Returns: the pointer to newly created <dsig:Manifest/> node or NULL
575 : * if an error occurs.
576 : */
577 : xmlNodePtr
578 0 : xmlSecTmplObjectAddManifest(xmlNodePtr objectNode, const xmlChar *id) {
579 : xmlNodePtr res;
580 :
581 0 : xmlSecAssert2(objectNode != NULL, NULL);
582 :
583 0 : res = xmlSecAddChild(objectNode, xmlSecNodeManifest, xmlSecDSigNs);
584 0 : if(res == NULL) {
585 0 : xmlSecError(XMLSEC_ERRORS_HERE,
586 : NULL,
587 : "xmlSecAddChild",
588 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
589 : "node=%s",
590 : xmlSecErrorsSafeString(xmlSecNodeManifest));
591 0 : return(NULL);
592 : }
593 0 : if(id != NULL) {
594 0 : xmlSetProp(res, xmlSecAttrId, id);
595 : }
596 0 : return(res);
597 : }
598 :
599 : /**
600 : * xmlSecTmplManifestAddReference:
601 : * @manifestNode: the pointer to <dsig:Manifest/> node.
602 : * @digestMethodId: the reference digest method.
603 : * @id: the node id (may be NULL).
604 : * @uri: the reference node uri (may be NULL).
605 : * @type: the reference node type (may be NULL).
606 : *
607 : * Adds <dsig:Reference/> node with specified URI (@uri), Id (@id) and
608 : * Type (@type) attributes and the required children <dsig:DigestMethod/> and
609 : * <dsig:DigestValue/> to the <dsig:Manifest/> node @manifestNode.
610 : *
611 : * Returns: the pointer to newly created <dsig:Reference/> node or NULL
612 : * if an error occurs.
613 : */
614 : xmlNodePtr
615 0 : xmlSecTmplManifestAddReference(xmlNodePtr manifestNode, xmlSecTransformId digestMethodId,
616 : const xmlChar *id, const xmlChar *uri, const xmlChar *type) {
617 0 : return(xmlSecTmplAddReference(manifestNode, digestMethodId, id, uri, type));
618 : }
619 :
620 : /**************************************************************************
621 : *
622 : * <enc:EncryptedData/> node
623 : *
624 : **************************************************************************/
625 : /**
626 : * xmlSecTmplEncDataCreate:
627 : * @doc: the pointer to signature document or NULL; in the later
628 : * case, application must later call @xmlSetTreeDoc to ensure
629 : * that all the children nodes have correct pointer to XML document.
630 : * @encMethodId: the encryption method (may be NULL).
631 : * @id: the Id attribute (optional).
632 : * @type: the Type attribute (optional)
633 : * @mimeType: the MimeType attribute (optional)
634 : * @encoding: the Encoding attribute (optional)
635 : *
636 : * Creates new <enc:EncryptedData /> node for encryption template.
637 : *
638 : * Returns: the pointer newly created <enc:EncryptedData/> node or NULL
639 : * if an error occurs.
640 : */
641 : xmlNodePtr
642 0 : xmlSecTmplEncDataCreate(xmlDocPtr doc, xmlSecTransformId encMethodId,
643 : const xmlChar *id, const xmlChar *type,
644 : const xmlChar *mimeType, const xmlChar *encoding) {
645 : xmlNodePtr encNode;
646 : xmlNsPtr ns;
647 :
648 0 : encNode = xmlNewDocNode(doc, NULL, xmlSecNodeEncryptedData, NULL);
649 0 : if(encNode == NULL) {
650 0 : xmlSecError(XMLSEC_ERRORS_HERE,
651 : NULL,
652 : "xmlNewDocNode",
653 : XMLSEC_ERRORS_R_XML_FAILED,
654 : "node=%s",
655 : xmlSecErrorsSafeString(xmlSecNodeEncryptedData));
656 0 : return(NULL);
657 : }
658 :
659 0 : ns = xmlNewNs(encNode, xmlSecEncNs, NULL);
660 0 : if(ns == NULL) {
661 0 : xmlSecError(XMLSEC_ERRORS_HERE,
662 : NULL,
663 : "xmlNewNs",
664 : XMLSEC_ERRORS_R_XML_FAILED,
665 : "ns=%s",
666 : xmlSecErrorsSafeString(xmlSecEncNs));
667 0 : return(NULL);
668 : }
669 0 : xmlSetNs(encNode, ns);
670 :
671 0 : if(id != NULL) {
672 0 : xmlSetProp(encNode, xmlSecAttrId, id);
673 : }
674 0 : if(type != NULL) {
675 0 : xmlSetProp(encNode, xmlSecAttrType, type);
676 : }
677 0 : if(mimeType != NULL) {
678 0 : xmlSetProp(encNode, xmlSecAttrMimeType, mimeType);
679 : }
680 0 : if(encoding != NULL) {
681 0 : xmlSetProp(encNode, xmlSecAttrEncoding, encoding);
682 : }
683 :
684 0 : if(xmlSecTmplPrepareEncData(encNode, encMethodId) < 0) {
685 0 : xmlFreeNode(encNode);
686 0 : return(NULL);
687 : }
688 0 : return(encNode);
689 : }
690 :
691 : static int
692 0 : xmlSecTmplPrepareEncData(xmlNodePtr parentNode, xmlSecTransformId encMethodId) {
693 : xmlNodePtr cur;
694 :
695 0 : xmlSecAssert2(parentNode != NULL, -1);
696 0 : xmlSecAssert2((encMethodId == NULL) || (encMethodId->href != NULL), -1);
697 :
698 : /* add EncryptionMethod node if requested */
699 0 : if(encMethodId != NULL) {
700 0 : cur = xmlSecAddChild(parentNode, xmlSecNodeEncryptionMethod, xmlSecEncNs);
701 0 : if(cur == NULL) {
702 0 : xmlSecError(XMLSEC_ERRORS_HERE,
703 : NULL,
704 : "xmlSecAddChild",
705 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
706 : "node=%s",
707 : xmlSecErrorsSafeString(xmlSecNodeEncryptionMethod));
708 0 : return(-1);
709 : }
710 0 : if(xmlSetProp(cur, xmlSecAttrAlgorithm, encMethodId->href) == NULL) {
711 0 : xmlSecError(XMLSEC_ERRORS_HERE,
712 : NULL,
713 : "xmlSetProp",
714 : XMLSEC_ERRORS_R_XML_FAILED,
715 : "name=%s,value=%s",
716 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
717 0 : xmlSecErrorsSafeString(encMethodId->href));
718 0 : return(-1);
719 : }
720 : }
721 :
722 : /* and CipherData node */
723 0 : cur = xmlSecAddChild(parentNode, xmlSecNodeCipherData, xmlSecEncNs);
724 0 : if(cur == NULL) {
725 0 : xmlSecError(XMLSEC_ERRORS_HERE,
726 : NULL,
727 : "xmlSecAddChild",
728 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
729 : "node=%s",
730 : xmlSecErrorsSafeString(xmlSecNodeCipherData));
731 0 : return(-1);
732 : }
733 :
734 0 : return(0);
735 : }
736 :
737 :
738 : /**
739 : * xmlSecTmplEncDataEnsureKeyInfo:
740 : * @encNode: the pointer to <enc:EncryptedData/> node.
741 : * @id: the Id attrbibute (optional).
742 : *
743 : * Adds <dsig:KeyInfo/> to the <enc:EncryptedData/> node @encNode.
744 : *
745 : * Returns: the pointer to newly created <dsig:KeyInfo/> node or
746 : * NULL if an error occurs.
747 : */
748 : xmlNodePtr
749 0 : xmlSecTmplEncDataEnsureKeyInfo(xmlNodePtr encNode, const xmlChar* id) {
750 : xmlNodePtr res;
751 :
752 0 : xmlSecAssert2(encNode != NULL, NULL);
753 :
754 0 : res = xmlSecFindChild(encNode, xmlSecNodeKeyInfo, xmlSecDSigNs);
755 0 : if(res == NULL) {
756 : xmlNodePtr cipherDataNode;
757 :
758 0 : cipherDataNode = xmlSecFindChild(encNode, xmlSecNodeCipherData, xmlSecEncNs);
759 0 : if(cipherDataNode == NULL) {
760 0 : xmlSecError(XMLSEC_ERRORS_HERE,
761 : NULL,
762 : xmlSecErrorsSafeString(xmlSecNodeCipherData),
763 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
764 : XMLSEC_ERRORS_NO_MESSAGE);
765 0 : return(NULL);
766 : }
767 :
768 0 : res = xmlSecAddPrevSibling(cipherDataNode, xmlSecNodeKeyInfo, xmlSecDSigNs);
769 0 : if(res == NULL) {
770 0 : xmlSecError(XMLSEC_ERRORS_HERE,
771 : NULL,
772 : "xmlSecAddPrevSibling",
773 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
774 : "node=%s",
775 : xmlSecErrorsSafeString(xmlSecNodeKeyInfo));
776 0 : return(NULL);
777 : }
778 : }
779 0 : if(id != NULL) {
780 0 : xmlSetProp(res, xmlSecAttrId, id);
781 : }
782 0 : return(res);
783 : }
784 :
785 : /**
786 : * xmlSecTmplEncDataEnsureEncProperties:
787 : * @encNode: the pointer to <enc:EncryptedData/> node.
788 : * @id: the Id attribute (optional).
789 : *
790 : * Adds <enc:EncryptionProperties/> node to the <enc:EncryptedData/>
791 : * node @encNode.
792 : *
793 : * Returns: the pointer to newly created <enc:EncryptionProperties/> node or
794 : * NULL if an error occurs.
795 : */
796 : xmlNodePtr
797 0 : xmlSecTmplEncDataEnsureEncProperties(xmlNodePtr encNode, const xmlChar *id) {
798 : xmlNodePtr res;
799 :
800 0 : xmlSecAssert2(encNode != NULL, NULL);
801 :
802 0 : res = xmlSecFindChild(encNode, xmlSecNodeEncryptionProperties, xmlSecEncNs);
803 0 : if(res == NULL) {
804 0 : res = xmlSecAddChild(encNode, xmlSecNodeEncryptionProperties, xmlSecEncNs);
805 0 : if(res == NULL) {
806 0 : xmlSecError(XMLSEC_ERRORS_HERE,
807 : NULL,
808 : "xmlSecAddChild",
809 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
810 : "node=%s",
811 : xmlSecErrorsSafeString(xmlSecNodeEncryptionProperties));
812 0 : return(NULL);
813 : }
814 : }
815 :
816 0 : if(id != NULL) {
817 0 : xmlSetProp(res, xmlSecAttrId, id);
818 : }
819 :
820 0 : return(res);
821 : }
822 :
823 : /**
824 : * xmlSecTmplEncDataAddEncProperty:
825 : * @encNode: the pointer to <enc:EncryptedData/> node.
826 : * @id: the Id attribute (optional).
827 : * @target: the Target attribute (optional).
828 : *
829 : * Adds <enc:EncryptionProperty/> node (and the parent
830 : * <enc:EncryptionProperties/> node if required) to the
831 : * <enc:EncryptedData/> node @encNode.
832 : *
833 : * Returns: the pointer to newly created <enc:EncryptionProperty/> node or
834 : * NULL if an error occurs.
835 : */
836 : xmlNodePtr
837 0 : xmlSecTmplEncDataAddEncProperty(xmlNodePtr encNode, const xmlChar *id, const xmlChar *target) {
838 : xmlNodePtr encProps;
839 : xmlNodePtr res;
840 :
841 0 : xmlSecAssert2(encNode != NULL, NULL);
842 :
843 0 : encProps = xmlSecTmplEncDataEnsureEncProperties(encNode, NULL);
844 0 : if(encProps == NULL) {
845 0 : xmlSecError(XMLSEC_ERRORS_HERE,
846 : NULL,
847 : "xmlSecTmplEncDataEnsureEncProperties",
848 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
849 : XMLSEC_ERRORS_NO_MESSAGE);
850 0 : return(NULL);
851 : }
852 :
853 0 : res = xmlSecAddChild(encProps, xmlSecNodeEncryptionProperty, xmlSecEncNs);
854 0 : if(res == NULL) {
855 0 : xmlSecError(XMLSEC_ERRORS_HERE,
856 : NULL,
857 : "xmlSecAddChild",
858 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
859 : "node=%s",
860 : xmlSecErrorsSafeString(xmlSecNodeEncryptionProperty));
861 0 : return(NULL);
862 : }
863 0 : if(id != NULL) {
864 0 : xmlSetProp(res, xmlSecAttrId, id);
865 : }
866 0 : if(target != NULL) {
867 0 : xmlSetProp(res, xmlSecAttrTarget, target);
868 : }
869 :
870 0 : return(res);
871 : }
872 :
873 : /**
874 : * xmlSecTmplEncDataEnsureCipherValue:
875 : * @encNode: the pointer to <enc:EncryptedData/> node.
876 : *
877 : * Adds <enc:CipherValue/> to the <enc:EncryptedData/> node @encNode.
878 : *
879 : * Returns: the pointer to newly created <enc:CipherValue/> node or
880 : * NULL if an error occurs.
881 : */
882 : xmlNodePtr
883 0 : xmlSecTmplEncDataEnsureCipherValue(xmlNodePtr encNode) {
884 : xmlNodePtr cipherDataNode;
885 : xmlNodePtr res, tmp;
886 :
887 0 : xmlSecAssert2(encNode != NULL, NULL);
888 :
889 0 : cipherDataNode = xmlSecFindChild(encNode, xmlSecNodeCipherData, xmlSecEncNs);
890 0 : if(cipherDataNode == NULL) {
891 0 : xmlSecError(XMLSEC_ERRORS_HERE,
892 : NULL,
893 : xmlSecErrorsSafeString(xmlSecNodeCipherData),
894 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
895 : XMLSEC_ERRORS_NO_MESSAGE);
896 0 : return(NULL);
897 : }
898 :
899 : /* check that we don;t have CipherReference node */
900 0 : tmp = xmlSecFindChild(cipherDataNode, xmlSecNodeCipherReference, xmlSecEncNs);
901 0 : if(tmp != NULL) {
902 0 : xmlSecError(XMLSEC_ERRORS_HERE,
903 : NULL,
904 : xmlSecErrorsSafeString(xmlSecNodeCipherReference),
905 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
906 : XMLSEC_ERRORS_NO_MESSAGE);
907 0 : return(NULL);
908 : }
909 :
910 0 : res = xmlSecFindChild(cipherDataNode, xmlSecNodeCipherValue, xmlSecEncNs);
911 0 : if(res == NULL) {
912 0 : res = xmlSecAddChild(cipherDataNode, xmlSecNodeCipherValue, xmlSecEncNs);
913 0 : if(res == NULL) {
914 0 : xmlSecError(XMLSEC_ERRORS_HERE,
915 : NULL,
916 : "xmlSecAddChild",
917 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
918 : "node=%s",
919 : xmlSecErrorsSafeString(xmlSecNodeCipherValue));
920 0 : return(NULL);
921 : }
922 : }
923 :
924 0 : return(res);
925 : }
926 :
927 : /**
928 : * xmlSecTmplEncDataEnsureCipherReference:
929 : * @encNode: the pointer to <enc:EncryptedData/> node.
930 : * @uri: the URI attribute (may be NULL).
931 : *
932 : * Adds <enc:CipherReference/> node with specified URI attribute @uri
933 : * to the <enc:EncryptedData/> node @encNode.
934 : *
935 : * Returns: the pointer to newly created <enc:CipherReference/> node or
936 : * NULL if an error occurs.
937 : */
938 : xmlNodePtr
939 0 : xmlSecTmplEncDataEnsureCipherReference(xmlNodePtr encNode, const xmlChar *uri) {
940 : xmlNodePtr cipherDataNode;
941 : xmlNodePtr res, tmp;
942 :
943 0 : xmlSecAssert2(encNode != NULL, NULL);
944 :
945 0 : cipherDataNode = xmlSecFindChild(encNode, xmlSecNodeCipherData, xmlSecEncNs);
946 0 : if(cipherDataNode == NULL) {
947 0 : xmlSecError(XMLSEC_ERRORS_HERE,
948 : NULL,
949 : xmlSecErrorsSafeString(xmlSecNodeCipherData),
950 : XMLSEC_ERRORS_R_NODE_NOT_FOUND,
951 : XMLSEC_ERRORS_NO_MESSAGE);
952 0 : return(NULL);
953 : }
954 :
955 : /* check that we don;t have CipherValue node */
956 0 : tmp = xmlSecFindChild(cipherDataNode, xmlSecNodeCipherValue, xmlSecEncNs);
957 0 : if(tmp != NULL) {
958 0 : xmlSecError(XMLSEC_ERRORS_HERE,
959 : NULL,
960 : xmlSecErrorsSafeString(xmlSecNodeCipherValue),
961 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
962 : XMLSEC_ERRORS_NO_MESSAGE);
963 0 : return(NULL);
964 : }
965 :
966 0 : res = xmlSecFindChild(cipherDataNode, xmlSecNodeCipherReference, xmlSecEncNs);
967 0 : if(res == NULL) {
968 0 : res = xmlSecAddChild(cipherDataNode, xmlSecNodeCipherReference, xmlSecEncNs);
969 0 : if(res == NULL) {
970 0 : xmlSecError(XMLSEC_ERRORS_HERE,
971 : NULL,
972 : "xmlSecAddChild",
973 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
974 : "node=%s",
975 : xmlSecErrorsSafeString(xmlSecNodeCipherReference));
976 0 : return(NULL);
977 : }
978 : }
979 :
980 0 : if(uri != NULL) {
981 0 : xmlSetProp(res, xmlSecAttrURI, uri);
982 : }
983 :
984 0 : return(res);
985 : }
986 :
987 : /**
988 : * xmlSecTmplEncDataGetEncMethodNode:
989 : * @encNode: the pointer to <enc:EcnryptedData /> node.
990 : *
991 : * Gets pointer to <enc:EncrytpionMethod/> node.
992 : *
993 : * Returns: pointer to <enc:EncryptionMethod /> node or NULL if an error occurs.
994 : */
995 : xmlNodePtr
996 0 : xmlSecTmplEncDataGetEncMethodNode(xmlNodePtr encNode) {
997 0 : xmlSecAssert2(encNode != NULL, NULL);
998 :
999 0 : return(xmlSecFindChild(encNode, xmlSecNodeEncryptionMethod, xmlSecEncNs));
1000 : }
1001 :
1002 : /**
1003 : * xmlSecTmplCipherReferenceAddTransform:
1004 : * @cipherReferenceNode: the pointer to <enc:CipherReference/> node.
1005 : * @transformId: the transform id.
1006 : *
1007 : * Adds <dsig:Transform/> node (and the parent <dsig:Transforms/> node)
1008 : * with specified transform methods @transform to the <enc:CipherReference/>
1009 : * child node of the <enc:EncryptedData/> node @encNode.
1010 : *
1011 : * Returns: the pointer to newly created <dsig:Transform/> node or
1012 : * NULL if an error occurs.
1013 : */
1014 : xmlNodePtr
1015 0 : xmlSecTmplCipherReferenceAddTransform(xmlNodePtr cipherReferenceNode,
1016 : xmlSecTransformId transformId) {
1017 : xmlNodePtr transformsNode;
1018 : xmlNodePtr res;
1019 :
1020 0 : xmlSecAssert2(cipherReferenceNode != NULL, NULL);
1021 0 : xmlSecAssert2(transformId != NULL, NULL);
1022 0 : xmlSecAssert2(transformId->href != NULL, NULL);
1023 :
1024 0 : transformsNode = xmlSecFindChild(cipherReferenceNode, xmlSecNodeTransforms, xmlSecEncNs);
1025 0 : if(transformsNode == NULL) {
1026 0 : transformsNode = xmlSecAddChild(cipherReferenceNode, xmlSecNodeTransforms, xmlSecEncNs);
1027 0 : if(transformsNode == NULL) {
1028 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1029 : NULL,
1030 : "xmlSecAddChild",
1031 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1032 : "node=%s",
1033 : xmlSecErrorsSafeString(xmlSecNodeTransforms));
1034 0 : return(NULL);
1035 : }
1036 : }
1037 :
1038 0 : res = xmlSecAddChild(transformsNode, xmlSecNodeTransform, xmlSecDSigNs);
1039 0 : if(res == NULL) {
1040 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1041 : NULL,
1042 : "xmlSecAddChild",
1043 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1044 : "node=%s",
1045 : xmlSecErrorsSafeString(xmlSecNodeTransform));
1046 0 : return(NULL);
1047 : }
1048 :
1049 0 : if(xmlSetProp(res, xmlSecAttrAlgorithm, transformId->href) == NULL) {
1050 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1051 : NULL,
1052 : "xmlSetProp",
1053 : XMLSEC_ERRORS_R_XML_FAILED,
1054 : "name=%s,value=%s",
1055 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
1056 0 : xmlSecErrorsSafeString(transformId->href));
1057 0 : xmlUnlinkNode(res);
1058 0 : xmlFreeNode(res);
1059 0 : return(NULL);
1060 : }
1061 :
1062 0 : return(res);
1063 : }
1064 :
1065 :
1066 : /***********************************************************************
1067 : *
1068 : * <enc:EncryptedKey> node
1069 : *
1070 : **********************************************************************/
1071 :
1072 : /**
1073 : * xmlSecTmplReferenceListAddDataReference:
1074 : * @encNode: the pointer to <enc:EncryptedKey/> node.
1075 : * @uri: uri to reference (optional)
1076 : *
1077 : * Adds <enc:DataReference/> and the parent <enc:ReferenceList/> node (if needed).
1078 : *
1079 : * Returns: the pointer to newly created <enc:DataReference/> node or
1080 : * NULL if an error occurs.
1081 : */
1082 : xmlNodePtr
1083 0 : xmlSecTmplReferenceListAddDataReference(xmlNodePtr encNode, const xmlChar *uri) {
1084 : xmlNodePtr refListNode, res;
1085 :
1086 0 : xmlSecAssert2(encNode != NULL, NULL);
1087 :
1088 0 : refListNode = xmlSecFindChild(encNode, xmlSecNodeReferenceList, xmlSecEncNs);
1089 0 : if(refListNode == NULL) {
1090 0 : refListNode = xmlSecAddChild(encNode, xmlSecNodeReferenceList, xmlSecEncNs);
1091 0 : if(refListNode == NULL) {
1092 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1093 : NULL,
1094 : "xmlSecAddChild",
1095 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1096 : "node=%s",
1097 : xmlSecErrorsSafeString(xmlSecNodeReferenceList));
1098 0 : return(NULL);
1099 : }
1100 : }
1101 :
1102 0 : res = xmlSecAddChild(refListNode, xmlSecNodeDataReference, xmlSecEncNs);
1103 0 : if(res == NULL) {
1104 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1105 : NULL,
1106 : "xmlSecAddChild",
1107 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1108 : "node=%s",
1109 : xmlSecErrorsSafeString(xmlSecNodeDataReference));
1110 0 : return(NULL);
1111 : }
1112 :
1113 0 : if(uri != NULL) {
1114 0 : if(xmlSetProp(res, xmlSecAttrURI, uri) == NULL) {
1115 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1116 : NULL,
1117 : "xmlSetProp",
1118 : XMLSEC_ERRORS_R_XML_FAILED,
1119 : "name=%s,value=%s",
1120 : xmlSecErrorsSafeString(xmlSecAttrURI),
1121 : xmlSecErrorsSafeString(uri));
1122 0 : xmlUnlinkNode(res);
1123 0 : xmlFreeNode(res);
1124 0 : return(NULL);
1125 : }
1126 : }
1127 :
1128 0 : return(res);
1129 : }
1130 :
1131 : /**
1132 : * xmlSecTmplReferenceListAddKeyReference:
1133 : * @encNode: the pointer to <enc:EncryptedKey/> node.
1134 : * @uri: uri to reference (optional)
1135 : *
1136 : * Adds <enc:KeyReference/> and the parent <enc:ReferenceList/> node (if needed).
1137 : *
1138 : * Returns: the pointer to newly created <enc:KeyReference/> node or
1139 : * NULL if an error occurs.
1140 : */
1141 : xmlNodePtr
1142 0 : xmlSecTmplReferenceListAddKeyReference(xmlNodePtr encNode, const xmlChar *uri) {
1143 : xmlNodePtr refListNode, res;
1144 :
1145 0 : xmlSecAssert2(encNode != NULL, NULL);
1146 :
1147 0 : refListNode = xmlSecFindChild(encNode, xmlSecNodeReferenceList, xmlSecEncNs);
1148 0 : if(refListNode == NULL) {
1149 0 : refListNode = xmlSecAddChild(encNode, xmlSecNodeReferenceList, xmlSecEncNs);
1150 0 : if(refListNode == NULL) {
1151 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1152 : NULL,
1153 : "xmlSecAddChild",
1154 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1155 : "node=%s",
1156 : xmlSecErrorsSafeString(xmlSecNodeReferenceList));
1157 0 : return(NULL);
1158 : }
1159 : }
1160 :
1161 0 : res = xmlSecAddChild(refListNode, xmlSecNodeKeyReference, xmlSecEncNs);
1162 0 : if(res == NULL) {
1163 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1164 : NULL,
1165 : "xmlSecAddChild",
1166 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1167 : "node=%s",
1168 : xmlSecErrorsSafeString(xmlSecNodeKeyReference));
1169 0 : return(NULL);
1170 : }
1171 :
1172 0 : if(uri != NULL) {
1173 0 : if(xmlSetProp(res, xmlSecAttrURI, uri) == NULL) {
1174 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1175 : NULL,
1176 : "xmlSetProp",
1177 : XMLSEC_ERRORS_R_XML_FAILED,
1178 : "name=%s,value=%s",
1179 : xmlSecErrorsSafeString(xmlSecAttrURI),
1180 : xmlSecErrorsSafeString(uri));
1181 0 : xmlUnlinkNode(res);
1182 0 : xmlFreeNode(res);
1183 0 : return(NULL);
1184 : }
1185 : }
1186 :
1187 0 : return(res);
1188 : }
1189 :
1190 :
1191 : /**************************************************************************
1192 : *
1193 : * <dsig:KeyInfo/> node
1194 : *
1195 : **************************************************************************/
1196 :
1197 : /**
1198 : * xmlSecTmplKeyInfoAddKeyName:
1199 : * @keyInfoNode: the pointer to <dsig:KeyInfo/> node.
1200 : * @name: the key name (optional).
1201 : *
1202 : * Adds <dsig:KeyName/> node to the <dsig:KeyInfo/> node @keyInfoNode.
1203 : *
1204 : * Returns: the pointer to the newly created <dsig:KeyName/> node or
1205 : * NULL if an error occurs.
1206 : */
1207 : xmlNodePtr
1208 0 : xmlSecTmplKeyInfoAddKeyName(xmlNodePtr keyInfoNode, const xmlChar* name) {
1209 : xmlNodePtr res;
1210 :
1211 0 : xmlSecAssert2(keyInfoNode != NULL, NULL);
1212 :
1213 0 : res = xmlSecAddChild(keyInfoNode, xmlSecNodeKeyName, xmlSecDSigNs);
1214 0 : if(res == NULL) {
1215 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1216 : NULL,
1217 : "xmlSecAddChild",
1218 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1219 : "node=%s",
1220 : xmlSecErrorsSafeString(xmlSecNodeKeyName));
1221 0 : return(NULL);
1222 : }
1223 0 : if(name != NULL) {
1224 0 : xmlSecNodeEncodeAndSetContent(res, name);
1225 : }
1226 0 : return(res);
1227 : }
1228 :
1229 : /**
1230 : * xmlSecTmplKeyInfoAddKeyValue:
1231 : * @keyInfoNode: the pointer to <dsig:KeyInfo/> node.
1232 : *
1233 : * Adds <dsig:KeyValue/> node to the <dsig:KeyInfo/> node @keyInfoNode.
1234 : *
1235 : * Returns: the pointer to the newly created <dsig:KeyValue/> node or
1236 : * NULL if an error occurs.
1237 : */
1238 : xmlNodePtr
1239 0 : xmlSecTmplKeyInfoAddKeyValue(xmlNodePtr keyInfoNode) {
1240 : xmlNodePtr res;
1241 :
1242 0 : xmlSecAssert2(keyInfoNode != NULL, NULL);
1243 :
1244 0 : res = xmlSecAddChild(keyInfoNode, xmlSecNodeKeyValue, xmlSecDSigNs);
1245 0 : if(res == NULL) {
1246 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1247 : NULL,
1248 : "xmlSecAddChild",
1249 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1250 : "node=%s",
1251 : xmlSecErrorsSafeString(xmlSecNodeKeyValue));
1252 0 : return(NULL);
1253 : }
1254 :
1255 0 : return(res);
1256 : }
1257 :
1258 : /**
1259 : * xmlSecTmplKeyInfoAddX509Data:
1260 : * @keyInfoNode: the pointer to <dsig:KeyInfo/> node.
1261 : *
1262 : * Adds <dsig:X509Data/> node to the <dsig:KeyInfo/> node @keyInfoNode.
1263 : *
1264 : * Returns: the pointer to the newly created <dsig:X509Data/> node or
1265 : * NULL if an error occurs.
1266 : */
1267 : xmlNodePtr
1268 0 : xmlSecTmplKeyInfoAddX509Data(xmlNodePtr keyInfoNode) {
1269 : xmlNodePtr res;
1270 :
1271 0 : xmlSecAssert2(keyInfoNode != NULL, NULL);
1272 :
1273 0 : res = xmlSecAddChild(keyInfoNode, xmlSecNodeX509Data, xmlSecDSigNs);
1274 0 : if(res == NULL) {
1275 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1276 : NULL,
1277 : "xmlSecAddChild",
1278 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1279 : "node=%s",
1280 : xmlSecErrorsSafeString(xmlSecNodeX509Data));
1281 0 : return(NULL);
1282 : }
1283 :
1284 0 : return(res);
1285 : }
1286 :
1287 : /**
1288 : * xmlSecTmplKeyInfoAddRetrievalMethod:
1289 : * @keyInfoNode: the pointer to <dsig:KeyInfo/> node.
1290 : * @uri: the URI attribute (optional).
1291 : * @type: the Type attribute(optional).
1292 : *
1293 : * Adds <dsig:RetrievalMethod/> node to the <dsig:KeyInfo/> node @keyInfoNode.
1294 : *
1295 : * Returns: the pointer to the newly created <dsig:RetrievalMethod/> node or
1296 : * NULL if an error occurs.
1297 : */
1298 : xmlNodePtr
1299 0 : xmlSecTmplKeyInfoAddRetrievalMethod(xmlNodePtr keyInfoNode, const xmlChar *uri,
1300 : const xmlChar *type) {
1301 : xmlNodePtr res;
1302 :
1303 0 : xmlSecAssert2(keyInfoNode != NULL, NULL);
1304 :
1305 0 : res = xmlSecAddChild(keyInfoNode, xmlSecNodeRetrievalMethod, xmlSecDSigNs);
1306 0 : if(res == NULL) {
1307 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1308 : NULL,
1309 : "xmlSecAddChild",
1310 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1311 : "node=%s",
1312 : xmlSecErrorsSafeString(xmlSecNodeRetrievalMethod));
1313 0 : return(NULL);
1314 : }
1315 :
1316 0 : if(uri != NULL) {
1317 0 : xmlSetProp(res, xmlSecAttrURI, uri);
1318 : }
1319 0 : if(type != NULL) {
1320 0 : xmlSetProp(res, xmlSecAttrType, type);
1321 : }
1322 0 : return(res);
1323 : }
1324 :
1325 : /**
1326 : * xmlSecTmplRetrievalMethodAddTransform:
1327 : * @retrMethodNode: the pointer to <dsig:RetrievalMethod/> node.
1328 : * @transformId: the transform id.
1329 : *
1330 : * Adds <dsig:Transform/> node (and the parent <dsig:Transforms/> node
1331 : * if required) to the <dsig:RetrievalMethod/> node @retrMethod.
1332 : *
1333 : * Returns: the pointer to the newly created <dsig:Transforms/> node or
1334 : * NULL if an error occurs.
1335 : */
1336 : xmlNodePtr
1337 0 : xmlSecTmplRetrievalMethodAddTransform(xmlNodePtr retrMethodNode, xmlSecTransformId transformId) {
1338 : xmlNodePtr transformsNode;
1339 : xmlNodePtr res;
1340 :
1341 0 : xmlSecAssert2(retrMethodNode != NULL, NULL);
1342 0 : xmlSecAssert2(transformId != NULL, NULL);
1343 0 : xmlSecAssert2(transformId->href != NULL, NULL);
1344 :
1345 0 : transformsNode = xmlSecFindChild(retrMethodNode, xmlSecNodeTransforms, xmlSecDSigNs);
1346 0 : if(transformsNode == NULL) {
1347 0 : transformsNode = xmlSecAddChild(retrMethodNode, xmlSecNodeTransforms, xmlSecDSigNs);
1348 0 : if(transformsNode == NULL) {
1349 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1350 : NULL,
1351 : "xmlSecAddChild",
1352 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1353 : "node=%s",
1354 : xmlSecErrorsSafeString(xmlSecNodeTransforms));
1355 0 : return(NULL);
1356 : }
1357 : }
1358 :
1359 0 : res = xmlSecAddChild(transformsNode, xmlSecNodeTransform, xmlSecDSigNs);
1360 0 : if(res == NULL) {
1361 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1362 : NULL,
1363 : "xmlSecAddChild",
1364 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1365 : "node=%s",
1366 : xmlSecErrorsSafeString(xmlSecNodeTransform));
1367 0 : return(NULL);
1368 : }
1369 :
1370 0 : if(xmlSetProp(res, xmlSecAttrAlgorithm, transformId->href) == NULL) {
1371 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1372 : NULL,
1373 : "xmlSetProp",
1374 : XMLSEC_ERRORS_R_XML_FAILED,
1375 : "name=%s,value=%s",
1376 : xmlSecErrorsSafeString(xmlSecAttrAlgorithm),
1377 0 : xmlSecErrorsSafeString(transformId->href));
1378 0 : xmlUnlinkNode(res);
1379 0 : xmlFreeNode(res);
1380 0 : return(NULL);
1381 : }
1382 :
1383 0 : return(res);
1384 : }
1385 :
1386 :
1387 : /**
1388 : * xmlSecTmplKeyInfoAddEncryptedKey:
1389 : * @keyInfoNode: the pointer to <dsig:KeyInfo/> node.
1390 : * @encMethodId: the encryption method (optional).
1391 : * @id: the Id attribute (optional).
1392 : * @type: the Type attribute (optional).
1393 : * @recipient: the Recipient attribute (optional).
1394 : *
1395 : * Adds <enc:EncryptedKey/> node with given attributes to
1396 : * the <dsig:KeyInfo/> node @keyInfoNode.
1397 : *
1398 : * Returns: the pointer to the newly created <enc:EncryptedKey/> node or
1399 : * NULL if an error occurs.
1400 : */
1401 : xmlNodePtr
1402 0 : xmlSecTmplKeyInfoAddEncryptedKey(xmlNodePtr keyInfoNode, xmlSecTransformId encMethodId,
1403 : const xmlChar* id, const xmlChar* type, const xmlChar* recipient) {
1404 : xmlNodePtr encKeyNode;
1405 :
1406 0 : xmlSecAssert2(keyInfoNode != NULL, NULL);
1407 :
1408 : /* we allow multiple encrypted key elements */
1409 0 : encKeyNode = xmlSecAddChild(keyInfoNode, xmlSecNodeEncryptedKey, xmlSecEncNs);
1410 0 : if(encKeyNode == NULL) {
1411 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1412 : NULL,
1413 : "xmlSecAddChild",
1414 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1415 : "node=%s",
1416 : xmlSecErrorsSafeString(xmlSecNodeEncryptedKey));
1417 0 : return(NULL);
1418 : }
1419 :
1420 0 : if(id != NULL) {
1421 0 : xmlSetProp(encKeyNode, xmlSecAttrId, id);
1422 : }
1423 0 : if(type != NULL) {
1424 0 : xmlSetProp(encKeyNode, xmlSecAttrType, type);
1425 : }
1426 0 : if(recipient != NULL) {
1427 0 : xmlSetProp(encKeyNode, xmlSecAttrRecipient, recipient);
1428 : }
1429 :
1430 0 : if(xmlSecTmplPrepareEncData(encKeyNode, encMethodId) < 0) {
1431 0 : xmlUnlinkNode(encKeyNode);
1432 0 : xmlFreeNode(encKeyNode);
1433 0 : return(NULL);
1434 : }
1435 0 : return(encKeyNode);
1436 : }
1437 :
1438 : /***********************************************************************
1439 : *
1440 : * <dsig:X509Data> node
1441 : *
1442 : **********************************************************************/
1443 : /**
1444 : * xmlSecTmplX509DataAddIssuerSerial:
1445 : * @x509DataNode: the pointer to <dsig:X509Data/> node.
1446 : *
1447 : * Adds <dsig:X509IssuerSerial/> node to the given <dsig:X509Data/> node.
1448 : *
1449 : * Returns: the pointer to the newly created <dsig:X509IssuerSerial/> node or
1450 : * NULL if an error occurs.
1451 : */
1452 :
1453 : xmlNodePtr
1454 0 : xmlSecTmplX509DataAddIssuerSerial(xmlNodePtr x509DataNode) {
1455 : xmlNodePtr cur;
1456 :
1457 0 : xmlSecAssert2(x509DataNode != NULL, NULL);
1458 :
1459 0 : cur = xmlSecFindChild(x509DataNode, xmlSecNodeX509IssuerSerial, xmlSecDSigNs);
1460 0 : if(cur != NULL) {
1461 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1462 : NULL,
1463 : xmlSecErrorsSafeString(xmlSecNodeX509IssuerSerial),
1464 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1465 : XMLSEC_ERRORS_NO_MESSAGE);
1466 0 : return(NULL);
1467 : }
1468 :
1469 0 : cur = xmlSecAddChild(x509DataNode, xmlSecNodeX509IssuerSerial, xmlSecDSigNs);
1470 0 : if(cur == NULL) {
1471 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1472 : NULL,
1473 : "xmlSecAddChild",
1474 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1475 : "node=%s",
1476 : xmlSecErrorsSafeString(xmlSecNodeX509IssuerSerial));
1477 0 : return(NULL);
1478 : }
1479 :
1480 0 : return (cur);
1481 : }
1482 :
1483 : /**
1484 : * xmlSecTmplX509IssuerSerialAddIssuerName:
1485 : * @x509IssuerSerialNode: the pointer to <dsig:X509IssuerSerial/> node.
1486 : * @issuerName: the issuer name (optional).
1487 : *
1488 : * Adds <dsig:X509IssuerName/> node to the <dsig:X509IssuerSerial/> node @x509IssuerSerialNode.
1489 : *
1490 : * Returns: the pointer to the newly created <dsig:X509IssuerName/> node or
1491 : * NULL if an error occurs.
1492 : */
1493 : xmlNodePtr
1494 0 : xmlSecTmplX509IssuerSerialAddIssuerName(xmlNodePtr x509IssuerSerialNode, const xmlChar* issuerName) {
1495 : xmlNodePtr res;
1496 :
1497 0 : xmlSecAssert2(x509IssuerSerialNode != NULL, NULL);
1498 :
1499 0 : if(xmlSecFindChild(x509IssuerSerialNode, xmlSecNodeX509IssuerName,
1500 : xmlSecDSigNs) != NULL) {
1501 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1502 : NULL,
1503 : xmlSecErrorsSafeString(xmlSecNodeX509IssuerName),
1504 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1505 : XMLSEC_ERRORS_NO_MESSAGE);
1506 0 : return(NULL);
1507 : }
1508 :
1509 0 : res = xmlSecAddChild(x509IssuerSerialNode, xmlSecNodeX509IssuerName, xmlSecDSigNs);
1510 0 : if(res == NULL) {
1511 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1512 : NULL,
1513 : "xmlSecAddChild",
1514 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1515 : "node=%s",
1516 : xmlSecErrorsSafeString(xmlSecNodeX509IssuerName));
1517 0 : return(NULL);
1518 : }
1519 :
1520 0 : if (issuerName != NULL) {
1521 0 : xmlSecNodeEncodeAndSetContent(res, issuerName);
1522 : }
1523 0 : return(res);
1524 : }
1525 :
1526 : /**
1527 : * xmlSecTmplX509IssuerSerialAddSerialNumber:
1528 : * @x509IssuerSerialNode: the pointer to <dsig:X509IssuerSerial/> node.
1529 : * @serial: the serial number (optional).
1530 : *
1531 : * Adds <dsig:X509SerialNumber/> node to the <dsig:X509IssuerSerial/> node @x509IssuerSerialNode.
1532 : *
1533 : * Returns: the pointer to the newly created <dsig:X509SerialNumber/> node or
1534 : * NULL if an error occurs.
1535 : */
1536 : xmlNodePtr
1537 0 : xmlSecTmplX509IssuerSerialAddSerialNumber(xmlNodePtr x509IssuerSerialNode, const xmlChar* serial) {
1538 : xmlNodePtr res;
1539 :
1540 0 : xmlSecAssert2(x509IssuerSerialNode != NULL, NULL);
1541 :
1542 0 : if(xmlSecFindChild(x509IssuerSerialNode, xmlSecNodeX509SerialNumber,
1543 : xmlSecDSigNs) != NULL) {
1544 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1545 : NULL,
1546 : xmlSecErrorsSafeString(xmlSecNodeX509SerialNumber),
1547 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1548 : XMLSEC_ERRORS_NO_MESSAGE);
1549 0 : return(NULL);
1550 : }
1551 :
1552 0 : res = xmlSecAddChild(x509IssuerSerialNode, xmlSecNodeX509SerialNumber, xmlSecDSigNs);
1553 0 : if(res == NULL) {
1554 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1555 : NULL,
1556 : "xmlSecAddChild",
1557 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1558 : "node=%s",
1559 : xmlSecErrorsSafeString(xmlSecNodeX509SerialNumber));
1560 0 : return(NULL);
1561 : }
1562 :
1563 0 : if (serial != NULL) {
1564 0 : xmlSecNodeEncodeAndSetContent(res, serial);
1565 : }
1566 0 : return(res);
1567 : }
1568 :
1569 : /**
1570 : * xmlSecTmplX509DataAddSubjectName:
1571 : * @x509DataNode: the pointer to <dsig:X509Data/> node.
1572 : *
1573 : * Adds <dsig:X509SubjectName/> node to the given <dsig:X509Data/> node.
1574 : *
1575 : * Returns: the pointer to the newly created <dsig:X509SubjectName/> node or
1576 : * NULL if an error occurs.
1577 : */
1578 :
1579 : xmlNodePtr
1580 0 : xmlSecTmplX509DataAddSubjectName(xmlNodePtr x509DataNode) {
1581 : xmlNodePtr cur;
1582 :
1583 0 : xmlSecAssert2(x509DataNode != NULL, NULL);
1584 :
1585 0 : cur = xmlSecFindChild(x509DataNode, xmlSecNodeX509SubjectName, xmlSecDSigNs);
1586 0 : if(cur != NULL) {
1587 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1588 : NULL,
1589 : xmlSecErrorsSafeString(xmlSecNodeX509SubjectName),
1590 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1591 : XMLSEC_ERRORS_NO_MESSAGE);
1592 0 : return(NULL);
1593 : }
1594 :
1595 0 : cur = xmlSecAddChild(x509DataNode, xmlSecNodeX509SubjectName, xmlSecDSigNs);
1596 0 : if(cur == NULL) {
1597 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1598 : NULL,
1599 : "xmlSecAddChild",
1600 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1601 : "node=%s",
1602 : xmlSecErrorsSafeString(xmlSecNodeX509SubjectName));
1603 0 : return(NULL);
1604 : }
1605 :
1606 0 : return (cur);
1607 : }
1608 :
1609 : /**
1610 : * xmlSecTmplX509DataAddSKI:
1611 : * @x509DataNode: the pointer to <dsig:X509Data/> node.
1612 : *
1613 : * Adds <dsig:X509SKI/> node to the given <dsig:X509Data/> node.
1614 : *
1615 : * Returns: the pointer to the newly created <dsig:X509SKI/> node or
1616 : * NULL if an error occurs.
1617 : */
1618 :
1619 : xmlNodePtr
1620 0 : xmlSecTmplX509DataAddSKI(xmlNodePtr x509DataNode) {
1621 : xmlNodePtr cur;
1622 :
1623 0 : xmlSecAssert2(x509DataNode != NULL, NULL);
1624 :
1625 0 : cur = xmlSecFindChild(x509DataNode, xmlSecNodeX509SKI, xmlSecDSigNs);
1626 0 : if(cur != NULL) {
1627 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1628 : NULL,
1629 : xmlSecErrorsSafeString(xmlSecNodeX509SKI),
1630 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1631 : XMLSEC_ERRORS_NO_MESSAGE);
1632 0 : return(NULL);
1633 : }
1634 :
1635 0 : cur = xmlSecAddChild(x509DataNode, xmlSecNodeX509SKI, xmlSecDSigNs);
1636 0 : if(cur == NULL) {
1637 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1638 : NULL,
1639 : "xmlSecAddChild",
1640 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1641 : "node=%s",
1642 : xmlSecErrorsSafeString(xmlSecNodeX509SKI));
1643 0 : return(NULL);
1644 : }
1645 :
1646 0 : return (cur);
1647 : }
1648 :
1649 :
1650 : /**
1651 : * xmlSecTmplX509DataAddCertificate:
1652 : * @x509DataNode: the pointer to <dsig:X509Data/> node.
1653 : *
1654 : * Adds <dsig:X509Certificate/> node to the given <dsig:X509Data/> node.
1655 : *
1656 : * Returns: the pointer to the newly created <dsig:X509Certificate/> node or
1657 : * NULL if an error occurs.
1658 : */
1659 :
1660 : xmlNodePtr
1661 0 : xmlSecTmplX509DataAddCertificate(xmlNodePtr x509DataNode) {
1662 : xmlNodePtr cur;
1663 :
1664 0 : xmlSecAssert2(x509DataNode != NULL, NULL);
1665 :
1666 0 : cur = xmlSecFindChild(x509DataNode, xmlSecNodeX509Certificate, xmlSecDSigNs);
1667 0 : if(cur != NULL) {
1668 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1669 : NULL,
1670 : xmlSecErrorsSafeString(xmlSecNodeX509Certificate),
1671 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1672 : XMLSEC_ERRORS_NO_MESSAGE);
1673 0 : return(NULL);
1674 : }
1675 :
1676 0 : cur = xmlSecAddChild(x509DataNode, xmlSecNodeX509Certificate, xmlSecDSigNs);
1677 0 : if(cur == NULL) {
1678 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1679 : NULL,
1680 : "xmlSecAddChild",
1681 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1682 : "node=%s",
1683 : xmlSecErrorsSafeString(xmlSecNodeX509Certificate));
1684 0 : return(NULL);
1685 : }
1686 :
1687 0 : return (cur);
1688 : }
1689 :
1690 : /**
1691 : * xmlSecTmplX509DataAddCRL:
1692 : * @x509DataNode: the pointer to <dsig:X509Data/> node.
1693 : *
1694 : * Adds <dsig:X509CRL/> node to the given <dsig:X509Data/> node.
1695 : *
1696 : * Returns: the pointer to the newly created <dsig:X509CRL/> node or
1697 : * NULL if an error occurs.
1698 : */
1699 :
1700 : xmlNodePtr
1701 0 : xmlSecTmplX509DataAddCRL(xmlNodePtr x509DataNode) {
1702 : xmlNodePtr cur;
1703 :
1704 0 : xmlSecAssert2(x509DataNode != NULL, NULL);
1705 :
1706 0 : cur = xmlSecFindChild(x509DataNode, xmlSecNodeX509CRL, xmlSecDSigNs);
1707 0 : if(cur != NULL) {
1708 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1709 : NULL,
1710 : xmlSecErrorsSafeString(xmlSecNodeX509CRL),
1711 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1712 : XMLSEC_ERRORS_NO_MESSAGE);
1713 0 : return(NULL);
1714 : }
1715 :
1716 0 : cur = xmlSecAddChild(x509DataNode, xmlSecNodeX509CRL, xmlSecDSigNs);
1717 0 : if(cur == NULL) {
1718 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1719 : NULL,
1720 : "xmlSecAddChild",
1721 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1722 : "node=%s",
1723 : xmlSecErrorsSafeString(xmlSecNodeX509CRL));
1724 0 : return(NULL);
1725 : }
1726 :
1727 0 : return (cur);
1728 : }
1729 :
1730 : /*************************************************************************
1731 : *
1732 : * <dsig:Transform/> node
1733 : *
1734 : ************************************************************************/
1735 :
1736 : /**
1737 : * xmlSecTmplTransformAddHmacOutputLength:
1738 : * @transformNode: the pointer to <dsig:Transform/> node
1739 : * @bitsLen: the required length in bits
1740 : *
1741 : * Creates <dsig:HMACOutputLength/> child for the HMAC transform
1742 : * node @node.
1743 : *
1744 : * Returns: 0 on success and a negatie value otherwise.
1745 : */
1746 : int
1747 0 : xmlSecTmplTransformAddHmacOutputLength(xmlNodePtr transformNode, xmlSecSize bitsLen) {
1748 : xmlNodePtr cur;
1749 : char buf[32];
1750 :
1751 0 : xmlSecAssert2(transformNode != NULL, -1);
1752 0 : xmlSecAssert2(bitsLen > 0, -1);
1753 :
1754 0 : cur = xmlSecFindChild(transformNode, xmlSecNodeHMACOutputLength, xmlSecDSigNs);
1755 0 : if(cur != NULL) {
1756 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1757 : NULL,
1758 : xmlSecErrorsSafeString(xmlSecNodeHMACOutputLength),
1759 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1760 : XMLSEC_ERRORS_NO_MESSAGE);
1761 0 : return(-1);
1762 : }
1763 :
1764 0 : cur = xmlSecAddChild(transformNode, xmlSecNodeHMACOutputLength, xmlSecDSigNs);
1765 0 : if(cur == NULL) {
1766 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1767 : NULL,
1768 : "xmlSecAddChild",
1769 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1770 : "node=%s",
1771 : xmlSecErrorsSafeString(xmlSecNodeHMACOutputLength));
1772 0 : return(-1);
1773 : }
1774 :
1775 0 : sprintf(buf, "%u", bitsLen);
1776 0 : xmlNodeSetContent(cur, BAD_CAST buf);
1777 0 : return(0);
1778 : }
1779 :
1780 : /**
1781 : * xmlSecTmplTransformAddRsaOaepParam:
1782 : * @transformNode: the pointer to <dsig:Transform/> node.
1783 : * @buf: the OAEP param buffer.
1784 : * @size: the OAEP param buffer size.
1785 : *
1786 : * Creates <enc:OAEPParam/> child node in the @node.
1787 : *
1788 : * Returns: 0 on success or a negative value if an error occurs.
1789 : */
1790 : int
1791 0 : xmlSecTmplTransformAddRsaOaepParam(xmlNodePtr transformNode,
1792 : const xmlSecByte *buf, xmlSecSize size) {
1793 : xmlNodePtr oaepParamNode;
1794 : xmlChar *base64;
1795 :
1796 0 : xmlSecAssert2(transformNode != NULL, -1);
1797 0 : xmlSecAssert2(buf != NULL, -1);
1798 0 : xmlSecAssert2(size > 0, -1);
1799 :
1800 0 : oaepParamNode = xmlSecFindChild(transformNode, xmlSecNodeRsaOAEPparams, xmlSecEncNs);
1801 0 : if(oaepParamNode != NULL) {
1802 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1803 : NULL,
1804 : xmlSecErrorsSafeString(xmlSecNodeRsaOAEPparams),
1805 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1806 : XMLSEC_ERRORS_NO_MESSAGE);
1807 0 : return(-1);
1808 : }
1809 :
1810 0 : oaepParamNode = xmlSecAddChild(transformNode, xmlSecNodeRsaOAEPparams, xmlSecEncNs);
1811 0 : if(oaepParamNode == NULL) {
1812 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1813 : NULL,
1814 : "xmlSecAddChild",
1815 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1816 : "node=%s",
1817 : xmlSecErrorsSafeString(xmlSecNodeRsaOAEPparams));
1818 0 : return(-1);
1819 : }
1820 :
1821 0 : base64 = xmlSecBase64Encode(buf, size, 0);
1822 0 : if(base64 == NULL) {
1823 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1824 : NULL,
1825 : "xmlSecBase64Encode",
1826 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1827 : "size=%d", size);
1828 0 : return(-1);
1829 : }
1830 :
1831 0 : xmlNodeSetContent(oaepParamNode, base64);
1832 0 : xmlFree(base64);
1833 0 : return(0);
1834 : }
1835 :
1836 : /**
1837 : * xmlSecTmplTransformAddXsltStylesheet:
1838 : * @transformNode: the pointer to <dsig:Transform/> node.
1839 : * @xslt: the XSLT transform exspression.
1840 : *
1841 : * Writes the XSLT transform expression to the @node.
1842 : *
1843 : * Returns: 0 on success or a negative value otherwise.
1844 : */
1845 : int
1846 0 : xmlSecTmplTransformAddXsltStylesheet(xmlNodePtr transformNode, const xmlChar *xslt) {
1847 : xmlDocPtr xsltDoc;
1848 : int ret;
1849 :
1850 0 : xmlSecAssert2(transformNode != NULL, -1);
1851 0 : xmlSecAssert2(xslt != NULL, -1);
1852 :
1853 0 : xsltDoc = xmlParseMemory((const char*)xslt, xmlStrlen(xslt));
1854 0 : if(xsltDoc == NULL) {
1855 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1856 : NULL,
1857 : "xmlParseMemory",
1858 : XMLSEC_ERRORS_R_XML_FAILED,
1859 : XMLSEC_ERRORS_NO_MESSAGE);
1860 0 : return(-1);
1861 : }
1862 :
1863 0 : ret = xmlSecReplaceContent(transformNode, xmlDocGetRootElement(xsltDoc));
1864 0 : if(ret < 0) {
1865 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1866 : NULL,
1867 : "xmlSecReplaceContent",
1868 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1869 : XMLSEC_ERRORS_NO_MESSAGE);
1870 0 : xmlFreeDoc(xsltDoc);
1871 0 : return(-1);
1872 : }
1873 :
1874 0 : xmlFreeDoc(xsltDoc);
1875 0 : return(0);
1876 : }
1877 :
1878 : /**
1879 : * xmlSecTmplTransformAddC14NInclNamespaces:
1880 : * @transformNode: the pointer to <dsig:Transform/> node.
1881 : * @prefixList: the white space delimited list of namespace prefixes,
1882 : * where "#default" indicates the default namespace
1883 : * (optional).
1884 : *
1885 : * Adds "inclusive" namespaces to the ExcC14N transform node @node.
1886 : *
1887 : * Returns: 0 if success or a negative value otherwise.
1888 : */
1889 : int
1890 0 : xmlSecTmplTransformAddC14NInclNamespaces(xmlNodePtr transformNode,
1891 : const xmlChar *prefixList) {
1892 : xmlNodePtr cur;
1893 :
1894 0 : xmlSecAssert2(transformNode != NULL, -1);
1895 0 : xmlSecAssert2(prefixList != NULL, -1);
1896 :
1897 0 : cur = xmlSecFindChild(transformNode, xmlSecNodeInclusiveNamespaces, xmlSecNsExcC14N);
1898 0 : if(cur != NULL) {
1899 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1900 : NULL,
1901 : xmlSecErrorsSafeString(xmlSecNodeInclusiveNamespaces),
1902 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1903 : XMLSEC_ERRORS_NO_MESSAGE);
1904 0 : return(-1);
1905 : }
1906 :
1907 0 : cur = xmlSecAddChild(transformNode, xmlSecNodeInclusiveNamespaces, xmlSecNsExcC14N);
1908 0 : if(cur == NULL) {
1909 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1910 0 : xmlSecErrorsSafeString(xmlSecNodeGetName(transformNode)),
1911 : "xmlSecAddChild",
1912 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1913 : "node=%s",
1914 : xmlSecErrorsSafeString(xmlSecNodeInclusiveNamespaces));
1915 0 : return(-1);
1916 : }
1917 :
1918 0 : xmlSetProp(cur, xmlSecAttrPrefixList, prefixList);
1919 0 : return(0);
1920 : }
1921 :
1922 : /**
1923 : * xmlSecTmplTransformAddXPath:
1924 : * @transformNode: the pointer to the <dsig:Transform/> node.
1925 : * @expression: the XPath expression.
1926 : * @nsList: the NULL terminated list of namespace prefix/href pairs
1927 : * (optional).
1928 : *
1929 : * Writes XPath transform infromation to the <dsig:Transform/> node
1930 : * @node.
1931 : *
1932 : * Returns: 0 for success or a negative value otherwise.
1933 : */
1934 : int
1935 0 : xmlSecTmplTransformAddXPath(xmlNodePtr transformNode, const xmlChar *expression,
1936 : const xmlChar **nsList) {
1937 : xmlNodePtr xpathNode;
1938 :
1939 0 : xmlSecAssert2(transformNode != NULL, -1);
1940 0 : xmlSecAssert2(expression != NULL, -1);
1941 :
1942 0 : xpathNode = xmlSecFindChild(transformNode, xmlSecNodeXPath, xmlSecDSigNs);
1943 0 : if(xpathNode != NULL) {
1944 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1945 : NULL,
1946 : xmlSecErrorsSafeString(xmlSecNodeXPath),
1947 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
1948 : XMLSEC_ERRORS_NO_MESSAGE);
1949 0 : return(-1);
1950 : }
1951 :
1952 0 : xpathNode = xmlSecAddChild(transformNode, xmlSecNodeXPath, xmlSecDSigNs);
1953 0 : if(xpathNode == NULL) {
1954 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1955 : NULL,
1956 : "xmlSecAddChild",
1957 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1958 : "node=%s",
1959 : xmlSecErrorsSafeString(xmlSecNodeXPath));
1960 0 : return(-1);
1961 : }
1962 :
1963 0 : xmlSecNodeEncodeAndSetContent(xpathNode, expression);
1964 0 : return((nsList != NULL) ? xmlSecTmplNodeWriteNsList(xpathNode, nsList) : 0);
1965 : }
1966 :
1967 : /**
1968 : * xmlSecTmplTransformAddXPath2:
1969 : * @transformNode: the pointer to the <dsig:Transform/> node.
1970 : * @type: the XPath2 transform type ("union", "intersect" or "subtract").
1971 : * @expression: the XPath expression.
1972 : * @nsList: the NULL terminated list of namespace prefix/href pairs.
1973 : * (optional).
1974 : *
1975 : * Writes XPath2 transform infromation to the <dsig:Transform/> node
1976 : * @node.
1977 : *
1978 : * Returns: 0 for success or a negative value otherwise.
1979 : */
1980 : int
1981 0 : xmlSecTmplTransformAddXPath2(xmlNodePtr transformNode, const xmlChar* type,
1982 : const xmlChar *expression, const xmlChar **nsList) {
1983 : xmlNodePtr xpathNode;
1984 :
1985 0 : xmlSecAssert2(transformNode != NULL, -1);
1986 0 : xmlSecAssert2(type != NULL, -1);
1987 0 : xmlSecAssert2(expression != NULL, -1);
1988 :
1989 0 : xpathNode = xmlSecAddChild(transformNode, xmlSecNodeXPath, xmlSecXPath2Ns);
1990 0 : if(xpathNode == NULL) {
1991 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1992 : NULL,
1993 : "xmlSecAddChild",
1994 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1995 : "node=%s",
1996 : xmlSecErrorsSafeString(xmlSecNodeXPath));
1997 0 : return(-1);
1998 : }
1999 0 : xmlSetProp(xpathNode, xmlSecAttrFilter, type);
2000 :
2001 0 : xmlSecNodeEncodeAndSetContent(xpathNode, expression);
2002 0 : return((nsList != NULL) ? xmlSecTmplNodeWriteNsList(xpathNode, nsList) : 0);
2003 : }
2004 :
2005 : /**
2006 : * xmlSecTmplTransformAddXPointer:
2007 : * @transformNode: the pointer to the <dsig:Transform/> node.
2008 : * @expression: the XPath expression.
2009 : * @nsList: the NULL terminated list of namespace prefix/href pairs.
2010 : * (optional).
2011 : *
2012 : * Writes XPoniter transform infromation to the <dsig:Transform/> node
2013 : * @node.
2014 : *
2015 : * Returns: 0 for success or a negative value otherwise.
2016 : */
2017 : int
2018 0 : xmlSecTmplTransformAddXPointer(xmlNodePtr transformNode, const xmlChar *expression,
2019 : const xmlChar **nsList) {
2020 : xmlNodePtr xpointerNode;
2021 :
2022 0 : xmlSecAssert2(expression != NULL, -1);
2023 0 : xmlSecAssert2(transformNode != NULL, -1);
2024 :
2025 0 : xpointerNode = xmlSecFindChild(transformNode, xmlSecNodeXPointer, xmlSecXPointerNs);
2026 0 : if(xpointerNode != NULL) {
2027 0 : xmlSecError(XMLSEC_ERRORS_HERE,
2028 : NULL,
2029 : xmlSecErrorsSafeString(xmlSecNodeXPointer),
2030 : XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT,
2031 : XMLSEC_ERRORS_NO_MESSAGE);
2032 0 : return(-1);
2033 : }
2034 :
2035 0 : xpointerNode = xmlSecAddChild(transformNode, xmlSecNodeXPointer, xmlSecXPointerNs);
2036 0 : if(xpointerNode == NULL) {
2037 0 : xmlSecError(XMLSEC_ERRORS_HERE,
2038 : NULL,
2039 : "xmlSecAddChild",
2040 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
2041 : "node=%s",
2042 : xmlSecErrorsSafeString(xmlSecNodeXPointer));
2043 0 : return(-1);
2044 : }
2045 :
2046 :
2047 0 : xmlSecNodeEncodeAndSetContent(xpointerNode, expression);
2048 0 : return((nsList != NULL) ? xmlSecTmplNodeWriteNsList(xpointerNode, nsList) : 0);
2049 : }
2050 :
2051 : static int
2052 0 : xmlSecTmplNodeWriteNsList(xmlNodePtr parentNode, const xmlChar** nsList) {
2053 : xmlNsPtr ns;
2054 : const xmlChar *prefix;
2055 : const xmlChar *href;
2056 : const xmlChar **ptr;
2057 :
2058 0 : xmlSecAssert2(parentNode != NULL, -1);
2059 0 : xmlSecAssert2(nsList != NULL, -1);
2060 :
2061 0 : ptr = nsList;
2062 0 : while((*ptr) != NULL) {
2063 0 : if(xmlStrEqual(BAD_CAST "#default", (*ptr))) {
2064 0 : prefix = NULL;
2065 : } else {
2066 0 : prefix = (*ptr);
2067 : }
2068 0 : if((++ptr) == NULL) {
2069 0 : xmlSecError(XMLSEC_ERRORS_HERE,
2070 : NULL,
2071 : NULL,
2072 : XMLSEC_ERRORS_R_INVALID_DATA,
2073 : "unexpected end of ns list");
2074 0 : return(-1);
2075 : }
2076 0 : href = *(ptr++);
2077 :
2078 0 : ns = xmlNewNs(parentNode, href, prefix);
2079 0 : if(ns == NULL) {
2080 0 : xmlSecError(XMLSEC_ERRORS_HERE,
2081 : NULL,
2082 : "xmlNewNs",
2083 : XMLSEC_ERRORS_R_XML_FAILED,
2084 : "href=%s;prefix=%s",
2085 : xmlSecErrorsSafeString(href),
2086 : xmlSecErrorsSafeString(prefix));
2087 0 : return(-1);
2088 : }
2089 : }
2090 0 : return(0);
2091 : }
|