/src/xmlsec/src/xmldsig.c
Line | Count | Source |
1 | | /** |
2 | | * XML Security Library (http://www.aleksey.com/xmlsec). |
3 | | * |
4 | | * This is free software; see the Copyright file in the source distribution for precise wording. |
5 | | * |
6 | | * Copyright (C) 2002-2026 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved. |
7 | | */ |
8 | | /** |
9 | | * @addtogroup xmlsec_core_xmldsig |
10 | | * @brief XML Digital Signature functions. |
11 | | * [XML Digital Signature](http://www.w3.org/TR/xmldsig-core/) implementation. |
12 | | */ |
13 | | #include "globals.h" |
14 | | |
15 | | #ifndef XMLSEC_NO_XMLDSIG |
16 | | |
17 | | #include <stdlib.h> |
18 | | #include <stdio.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include <libxml/tree.h> |
22 | | #include <libxml/parser.h> |
23 | | |
24 | | #include <xmlsec/xmlsec.h> |
25 | | #include <xmlsec/buffer.h> |
26 | | #include <xmlsec/xmltree.h> |
27 | | #include <xmlsec/keys.h> |
28 | | #include <xmlsec/keysmngr.h> |
29 | | #include <xmlsec/transforms.h> |
30 | | #include <xmlsec/membuf.h> |
31 | | #include <xmlsec/xmldsig.h> |
32 | | #include <xmlsec/errors.h> |
33 | | |
34 | | #include "cast_helpers.h" |
35 | | #include "transform_helpers.h" |
36 | | |
37 | | /****************************************************************************** |
38 | | * |
39 | | * xmlSecDSigCtx |
40 | | * |
41 | | *****************************************************************************/ |
42 | | static int xmlSecDSigCtxProcessSignatureNode (xmlSecDSigCtxPtr dsigCtx, |
43 | | xmlNodePtr node); |
44 | | static int xmlSecDSigCtxProcessSignedInfoNode (xmlSecDSigCtxPtr dsigCtx, |
45 | | xmlNodePtr node, |
46 | | xmlNodePtr * firstReferenceNode); |
47 | | static int xmlSecDSigCtxProcessKeyInfoNode (xmlSecDSigCtxPtr dsigCtx, |
48 | | xmlNodePtr node); |
49 | | static int xmlSecDSigCtxProcessObjectNode (xmlSecDSigCtxPtr dsigCtx, |
50 | | xmlNodePtr node); |
51 | | static int xmlSecDSigCtxProcessManifestNode (xmlSecDSigCtxPtr dsigCtx, |
52 | | xmlNodePtr node); |
53 | | |
54 | | static int xmlSecDSigCtxProcessReferences (xmlSecDSigCtxPtr dsigCtx, |
55 | | xmlNodePtr firstReferenceNode); |
56 | | |
57 | | |
58 | | static void xmlSecDSigCtxMarkAsSucceeded (xmlSecDSigCtxPtr dsigCtx); |
59 | | static void xmlSecDSigCtxMarkAsFailed (xmlSecDSigCtxPtr dsigCtx, |
60 | | xmlSecDSigFailureReason failureReason); |
61 | | |
62 | | /* The ID attribute in XMLDSig is 'Id' */ |
63 | | static const xmlChar* xmlSecDSigIds[] = { xmlSecAttrId, NULL }; |
64 | | |
65 | | /** |
66 | | * @brief Creates a dsig:Signature processing context. |
67 | | * @details Creates <dsig:Signature/> element processing context. |
68 | | * The caller is responsible for destroying returned object by calling |
69 | | * #xmlSecDSigCtxDestroy function. |
70 | | * |
71 | | * @param keysMngr the pointer to keys manager. |
72 | | * @return pointer to newly allocated context object or NULL if an error |
73 | | * occurs. |
74 | | */ |
75 | | xmlSecDSigCtxPtr |
76 | 10.9k | xmlSecDSigCtxCreate(xmlSecKeysMngrPtr keysMngr) { |
77 | 10.9k | xmlSecDSigCtxPtr dsigCtx; |
78 | 10.9k | int ret; |
79 | | |
80 | 10.9k | dsigCtx = (xmlSecDSigCtxPtr) xmlMalloc(sizeof(xmlSecDSigCtx)); |
81 | 10.9k | if(dsigCtx == NULL) { |
82 | 0 | xmlSecMallocError(sizeof(xmlSecDSigCtx), NULL); |
83 | 0 | return(NULL); |
84 | 0 | } |
85 | | |
86 | 10.9k | ret = xmlSecDSigCtxInitialize(dsigCtx, keysMngr); |
87 | 10.9k | if(ret < 0) { |
88 | 0 | xmlSecInternalError("xmlSecDSigCtxInitialize", NULL); |
89 | 0 | xmlSecDSigCtxDestroy(dsigCtx); |
90 | 0 | return(NULL); |
91 | 0 | } |
92 | 10.9k | return(dsigCtx); |
93 | 10.9k | } |
94 | | |
95 | | /** |
96 | | * @brief Destroys a dsig:Signature processing context. |
97 | | * @details Destroy context object created with #xmlSecDSigCtxCreate function. |
98 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
99 | | */ |
100 | | void |
101 | 10.9k | xmlSecDSigCtxDestroy(xmlSecDSigCtxPtr dsigCtx) { |
102 | 10.9k | xmlSecAssert(dsigCtx != NULL); |
103 | | |
104 | 10.9k | xmlSecDSigCtxFinalize(dsigCtx); |
105 | 10.9k | xmlFree(dsigCtx); |
106 | 10.9k | } |
107 | | |
108 | | /** |
109 | | * @brief Initializes a dsig:Signature processing context. |
110 | | * @details Initializes <dsig:Signature/> element processing context. |
111 | | * The caller is responsible for cleaning up returned object by calling |
112 | | * #xmlSecDSigCtxFinalize function. |
113 | | * |
114 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
115 | | * @param keysMngr the pointer to keys manager. |
116 | | * @return 0 on success or a negative value if an error occurs. |
117 | | */ |
118 | | int |
119 | 10.9k | xmlSecDSigCtxInitialize(xmlSecDSigCtxPtr dsigCtx, xmlSecKeysMngrPtr keysMngr) { |
120 | 10.9k | int ret; |
121 | | |
122 | 10.9k | xmlSecAssert2(dsigCtx != NULL, -1); |
123 | | |
124 | 10.9k | memset(dsigCtx, 0, sizeof(xmlSecDSigCtx)); |
125 | | |
126 | | /* initialize key info */ |
127 | 10.9k | ret = xmlSecKeyInfoCtxInitialize(&(dsigCtx->keyInfoReadCtx), keysMngr); |
128 | 10.9k | if(ret < 0) { |
129 | 0 | xmlSecInternalError("xmlSecKeyInfoCtxInitialize", NULL); |
130 | 0 | return(-1); |
131 | 0 | } |
132 | 10.9k | dsigCtx->keyInfoReadCtx.mode = xmlSecKeyInfoModeRead; |
133 | | |
134 | 10.9k | ret = xmlSecKeyInfoCtxInitialize(&(dsigCtx->keyInfoWriteCtx), keysMngr); |
135 | 10.9k | if(ret < 0) { |
136 | 0 | xmlSecInternalError("xmlSecKeyInfoCtxInitialize", NULL); |
137 | 0 | return(-1); |
138 | 0 | } |
139 | 10.9k | dsigCtx->keyInfoWriteCtx.mode = xmlSecKeyInfoModeWrite; |
140 | | /* it's not wise to write a private key :) */ |
141 | 10.9k | dsigCtx->keyInfoWriteCtx.keyReq.keyType = xmlSecKeyDataTypePublic; |
142 | | |
143 | | /* initialize the signature transform context */ |
144 | 10.9k | ret = xmlSecTransformCtxInitialize(&(dsigCtx->transformCtx)); |
145 | 10.9k | if(ret < 0) { |
146 | 0 | xmlSecInternalError("xmlSecTransformCtxInitialize", NULL); |
147 | 0 | return(-1); |
148 | 0 | } |
149 | | |
150 | | /* references lists from SignedInfo and Manifest elements */ |
151 | 10.9k | ret = xmlSecPtrListInitialize(&(dsigCtx->signedInfoReferences), |
152 | 10.9k | xmlSecDSigReferenceCtxListId); |
153 | 10.9k | if(ret != 0) { |
154 | 0 | xmlSecInternalError("xmlSecPtrListInitialize", NULL); |
155 | 0 | return(-1); |
156 | 0 | } |
157 | 10.9k | ret = xmlSecPtrListInitialize(&(dsigCtx->manifestReferences), |
158 | 10.9k | xmlSecDSigReferenceCtxListId); |
159 | 10.9k | if(ret != 0) { |
160 | 0 | xmlSecInternalError("xmlSecPtrListInitialize", NULL); |
161 | 0 | return(-1); |
162 | 0 | } |
163 | | |
164 | 10.9k | dsigCtx->enabledReferenceUris = xmlSecTransformUriTypeEmpty | xmlSecTransformUriTypeSameDocument; |
165 | 10.9k | return(0); |
166 | 10.9k | } |
167 | | |
168 | | /** |
169 | | * @brief Cleans up a dsig:Signature processing context. |
170 | | * @details Cleans up @p dsigCtx object initialized with #xmlSecDSigCtxInitialize function. |
171 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
172 | | */ |
173 | | void |
174 | 10.9k | xmlSecDSigCtxFinalize(xmlSecDSigCtxPtr dsigCtx) { |
175 | 10.9k | xmlSecAssert(dsigCtx != NULL); |
176 | | |
177 | 10.9k | xmlSecTransformCtxFinalize(&(dsigCtx->transformCtx)); |
178 | 10.9k | xmlSecKeyInfoCtxFinalize(&(dsigCtx->keyInfoReadCtx)); |
179 | 10.9k | xmlSecKeyInfoCtxFinalize(&(dsigCtx->keyInfoWriteCtx)); |
180 | 10.9k | xmlSecPtrListFinalize(&(dsigCtx->signedInfoReferences)); |
181 | 10.9k | xmlSecPtrListFinalize(&(dsigCtx->manifestReferences)); |
182 | | |
183 | 10.9k | if(dsigCtx->enabledReferenceTransforms != NULL) { |
184 | 0 | xmlSecPtrListDestroy(dsigCtx->enabledReferenceTransforms); |
185 | 0 | } |
186 | 10.9k | if(dsigCtx->signKey != NULL) { |
187 | 0 | xmlSecKeyDestroy(dsigCtx->signKey); |
188 | 0 | } |
189 | 10.9k | if(dsigCtx->id != NULL) { |
190 | 774 | xmlFree(dsigCtx->id); |
191 | 774 | } |
192 | 10.9k | memset(dsigCtx, 0, sizeof(xmlSecDSigCtx)); |
193 | 10.9k | } |
194 | | |
195 | | /** |
196 | | * @brief Enables a transform for dsig:Reference processing. |
197 | | * @details Enables @p transformId for <dsig:Reference/> elements processing. |
198 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
199 | | * @param transformId the transform klass. |
200 | | * @return 0 on success or a negative value if an error occurs. |
201 | | */ |
202 | | int |
203 | 0 | xmlSecDSigCtxEnableReferenceTransform(xmlSecDSigCtxPtr dsigCtx, xmlSecTransformId transformId) { |
204 | 0 | int ret; |
205 | |
|
206 | 0 | xmlSecAssert2(dsigCtx != NULL, -1); |
207 | 0 | xmlSecAssert2(dsigCtx->result == NULL, -1); |
208 | 0 | xmlSecAssert2(transformId != xmlSecTransformIdUnknown, -1); |
209 | | |
210 | 0 | if(dsigCtx->enabledReferenceTransforms == NULL) { |
211 | 0 | dsigCtx->enabledReferenceTransforms = xmlSecPtrListCreate(xmlSecTransformIdListId); |
212 | 0 | if(dsigCtx->enabledReferenceTransforms == NULL) { |
213 | 0 | xmlSecInternalError("xmlSecPtrListCreate", NULL); |
214 | 0 | return(-1); |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | 0 | ret = xmlSecPtrListAdd(dsigCtx->enabledReferenceTransforms, (void*)transformId); |
219 | 0 | if(ret < 0) { |
220 | 0 | xmlSecInternalError("xmlSecPtrListAdd", NULL); |
221 | 0 | return(-1); |
222 | 0 | } |
223 | 0 | return(0); |
224 | 0 | } |
225 | | |
226 | | /** |
227 | | * @brief Enables a transform for dsig:SignedInfo processing. |
228 | | * @details Enables @p transformId for <dsig:SignedInfo/> element processing. |
229 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
230 | | * @param transformId the transform klass. |
231 | | * @return 0 on success or a negative value if an error occurs. |
232 | | */ |
233 | | int |
234 | 0 | xmlSecDSigCtxEnableSignatureTransform(xmlSecDSigCtxPtr dsigCtx, xmlSecTransformId transformId) { |
235 | 0 | xmlSecAssert2(dsigCtx != NULL, -1); |
236 | 0 | xmlSecAssert2(dsigCtx->result == NULL, -1); |
237 | 0 | xmlSecAssert2(transformId != xmlSecTransformIdUnknown, -1); |
238 | | |
239 | 0 | return(xmlSecPtrListAdd(&(dsigCtx->transformCtx.enabledTransforms), (void*)transformId)); |
240 | 0 | } |
241 | | |
242 | | /** |
243 | | * @brief Gets the pre-signature buffer for the SignedInfo element. |
244 | | * @details Gets pointer to the buffer with serialized <dsig:SignedInfo/> element |
245 | | * just before signature calculation (valid if and only if |
246 | | * #XMLSEC_DSIG_FLAGS_STORE_SIGNATURE context flag is set). |
247 | | * |
248 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
249 | | * @return the pointer to the pre-signature buffer or NULL if an error occurs. |
250 | | */ |
251 | | xmlSecBufferPtr |
252 | 0 | xmlSecDSigCtxGetPreSignBuffer(xmlSecDSigCtxPtr dsigCtx) { |
253 | 0 | xmlSecAssert2(dsigCtx != NULL, NULL); |
254 | | |
255 | 0 | return((dsigCtx->preSignMemBufMethod != NULL) ? |
256 | 0 | xmlSecTransformMemBufGetBuffer(dsigCtx->preSignMemBufMethod) : NULL); |
257 | 0 | } |
258 | | |
259 | | /** |
260 | | * @brief Signs the data as described in @p tmpl node. |
261 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
262 | | * @param tmpl the pointer to <dsig:Signature/> node with signature template. |
263 | | * @return 0 on success or a negative value if an error occurs. |
264 | | */ |
265 | | int |
266 | 0 | xmlSecDSigCtxSign(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr tmpl) { |
267 | 0 | xmlSecByte* outBuf; |
268 | 0 | xmlSecSize outSize; |
269 | 0 | int outLen; |
270 | 0 | int ret; |
271 | |
|
272 | 0 | xmlSecAssert2(dsigCtx != NULL, -1); |
273 | 0 | xmlSecAssert2(dsigCtx->result == NULL, -1); |
274 | 0 | xmlSecAssert2(tmpl != NULL, -1); |
275 | 0 | xmlSecAssert2(tmpl->doc != NULL, -1); |
276 | | |
277 | | /* add ids for Signature nodes */ |
278 | 0 | dsigCtx->operation = xmlSecTransformOperationSign; |
279 | 0 | dsigCtx->status = xmlSecDSigStatusUnknown; |
280 | 0 | dsigCtx->failureReason = xmlSecDSigFailureReasonUnknown; |
281 | 0 | dsigCtx->keyInfoReadCtx.operation = xmlSecTransformOperationSign; |
282 | 0 | dsigCtx->keyInfoWriteCtx.operation = xmlSecTransformOperationSign; |
283 | 0 | xmlSecAddIDs(tmpl->doc, tmpl, xmlSecDSigIds); |
284 | | |
285 | | /* read signature template */ |
286 | 0 | ret = xmlSecDSigCtxProcessSignatureNode(dsigCtx, tmpl); |
287 | 0 | if(ret < 0) { |
288 | 0 | xmlSecInternalError("xmlSecDSigCtxProcessSignatureNode", NULL); |
289 | 0 | return(-1); |
290 | 0 | } |
291 | 0 | xmlSecAssert2(dsigCtx->signMethod != NULL, -1); |
292 | 0 | xmlSecAssert2(dsigCtx->signValueNode != NULL, -1); |
293 | | |
294 | | /* references processing might change the status */ |
295 | 0 | if(dsigCtx->status != xmlSecDSigStatusUnknown) { |
296 | 0 | return(0); |
297 | 0 | } |
298 | | |
299 | | /* check what we've got */ |
300 | 0 | dsigCtx->result = dsigCtx->transformCtx.result; |
301 | 0 | if((dsigCtx->result == NULL) || (xmlSecBufferGetData(dsigCtx->result) == NULL)) { |
302 | 0 | xmlSecOtherError(XMLSEC_ERRORS_R_INVALID_RESULT, NULL, NULL); |
303 | 0 | return(-1); |
304 | 0 | } |
305 | | |
306 | | /* write signed data to xml */ |
307 | 0 | outBuf = xmlSecBufferGetData(dsigCtx->result); |
308 | 0 | outSize = xmlSecBufferGetSize(dsigCtx->result); |
309 | 0 | XMLSEC_SAFE_CAST_SIZE_TO_INT(outSize, outLen, return(-1), NULL); |
310 | 0 | #if LIBXML_VERSION >= 21300 |
311 | 0 | if(xmlNodeSetContentLen(dsigCtx->signValueNode, outBuf, outLen) < 0) { |
312 | 0 | xmlSecXmlError("xmlNodeSetContentLen", xmlSecNodeGetName(dsigCtx->signValueNode)); |
313 | 0 | return(-1); |
314 | 0 | } |
315 | | #else /* LIBXML_VERSION >= 21300 */ |
316 | | /* libxml2 < 2.13.0: xmlNodeSetContentLen() returns void and cannot report errors */ |
317 | | xmlNodeSetContentLen(dsigCtx->signValueNode, outBuf, outLen); |
318 | | #endif /* LIBXML_VERSION >= 21300 */ |
319 | | |
320 | | /* set success status and we are done */ |
321 | 0 | xmlSecDSigCtxMarkAsSucceeded(dsigCtx); |
322 | 0 | return(0); |
323 | 0 | } |
324 | | |
325 | | /** |
326 | | * @brief Validates the dsig:Signature in the node. |
327 | | * @details Validates signature in the @p node. The verification result is returned |
328 | | * in @p status and @p failureReason members of the @p dsigCtx object. |
329 | | * |
330 | | * Note that some verification failures are reported both by returning a negative value |
331 | | * and by setting @p status to #xmlSecDSigStatusInvalid with a meaningful @p failureReason |
332 | | * (for example, when the key is not found: #xmlSecDSigFailureReasonKeyNotFound). In this |
333 | | * case @p status and @p failureReason remain meaningful even though the return value is |
334 | | * negative. This is intentionally different from a reference digest failure |
335 | | * (#xmlSecDSigFailureReasonReference), where the function returns 0 and the failure is |
336 | | * reported only in @p status. |
337 | | * |
338 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
339 | | * @param node the pointer to the <dsig:Signature/> node. |
340 | | * @return 0 on success (check @p status member of @p dsigCtx to get |
341 | | * the actual signature verification result) or a negative value if an error occurs |
342 | | * (note that @p status and @p failureReason may still hold a meaningful value for |
343 | | * verification failures reported as errors, e.g. #xmlSecDSigFailureReasonKeyNotFound). |
344 | | */ |
345 | | int |
346 | 10.9k | xmlSecDSigCtxVerify(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr node) { |
347 | 10.9k | int ret; |
348 | | |
349 | 10.9k | xmlSecAssert2(dsigCtx != NULL, -1); |
350 | 10.9k | xmlSecAssert2(dsigCtx->result == NULL, -1); |
351 | 10.9k | xmlSecAssert2(node != NULL, -1); |
352 | 10.9k | xmlSecAssert2(node->doc != NULL, -1); |
353 | | |
354 | | /* add ids for Signature nodes */ |
355 | 10.9k | dsigCtx->operation = xmlSecTransformOperationVerify; |
356 | 10.9k | dsigCtx->status = xmlSecDSigStatusUnknown; |
357 | 10.9k | dsigCtx->failureReason = xmlSecDSigFailureReasonUnknown; |
358 | 10.9k | dsigCtx->keyInfoReadCtx.operation = xmlSecTransformOperationVerify; |
359 | 10.9k | dsigCtx->keyInfoWriteCtx.operation = xmlSecTransformOperationVerify; |
360 | 10.9k | xmlSecAddIDs(node->doc, node, xmlSecDSigIds); |
361 | | |
362 | | /* read signature info */ |
363 | 10.9k | ret = xmlSecDSigCtxProcessSignatureNode(dsigCtx, node); |
364 | 10.9k | if(ret < 0) { |
365 | 10.9k | xmlSecInternalError("xmlSecDSigCtxProcessSignatureNode", NULL); |
366 | 10.9k | return(-1); |
367 | 10.9k | } |
368 | 0 | xmlSecAssert2(dsigCtx->signMethod != NULL, -1); |
369 | 0 | xmlSecAssert2(dsigCtx->signValueNode != NULL, -1); |
370 | | |
371 | | /* references processing might change the status */ |
372 | 0 | if(dsigCtx->status != xmlSecDSigStatusUnknown) { |
373 | 0 | return(0); |
374 | 0 | } |
375 | | |
376 | | /* verify SignatureValue node content */ |
377 | 0 | ret = xmlSecTransformVerifyNodeContent(dsigCtx->signMethod, dsigCtx->signValueNode, |
378 | 0 | &(dsigCtx->transformCtx)); |
379 | 0 | if(ret < 0) { |
380 | 0 | xmlSecInternalError("xmlSecTransformVerifyNodeContent", NULL); |
381 | 0 | return(-1); |
382 | 0 | } |
383 | | |
384 | | /* set status and we are done */ |
385 | 0 | if(dsigCtx->signMethod->status == xmlSecTransformStatusOk) { |
386 | 0 | xmlSecDSigCtxMarkAsSucceeded(dsigCtx); |
387 | 0 | } else { |
388 | 0 | xmlSecDSigCtxMarkAsFailed(dsigCtx, xmlSecDSigFailureReasonSignature); |
389 | 0 | } |
390 | 0 | return(0); |
391 | 0 | } |
392 | | |
393 | | static void |
394 | 0 | xmlSecDSigCtxMarkAsSucceeded(xmlSecDSigCtxPtr dsigCtx) { |
395 | 0 | xmlSecAssert(dsigCtx != NULL); |
396 | | |
397 | 0 | dsigCtx->status = xmlSecDSigStatusSucceeded; |
398 | 0 | } |
399 | | |
400 | | static void |
401 | 701 | xmlSecDSigCtxMarkAsFailed(xmlSecDSigCtxPtr dsigCtx, xmlSecDSigFailureReason failureReason) { |
402 | 701 | xmlSecAssert(dsigCtx != NULL); |
403 | | |
404 | 701 | dsigCtx->status = xmlSecDSigStatusInvalid; |
405 | 701 | if(dsigCtx->failureReason == xmlSecDSigFailureReasonUnknown) { |
406 | 701 | dsigCtx->failureReason = failureReason; |
407 | 701 | } |
408 | 701 | } |
409 | | |
410 | | |
411 | | /* |
412 | | * xmlSecDSigCtxProcessSignatureNode: |
413 | | * |
414 | | * The Signature element (http://www.w3.org/TR/xmldsig-core/#sec-Signature) |
415 | | * |
416 | | * The Signature element is the root element of an XML Signature. |
417 | | * Implementation MUST generate laxly schema valid [XML-schema] Signature |
418 | | * elements as specified by the following schema: |
419 | | * The way in which the SignedInfo element is presented to the |
420 | | * canonicalization method is dependent on that method. The following |
421 | | * applies to algorithms which process XML as nodes or characters: |
422 | | * |
423 | | * - XML based canonicalization implementations MUST be provided with |
424 | | * a [XPath] node-set originally formed from the document containing |
425 | | * the SignedInfo and currently indicating the SignedInfo, its descendants, |
426 | | * and the attribute and namespace nodes of SignedInfo and its descendant |
427 | | * elements. |
428 | | * |
429 | | * - Text based canonicalization algorithms (such as CRLF and charset |
430 | | * normalization) should be provided with the UTF-8 octets that represent |
431 | | * the well-formed SignedInfo element, from the first character to the |
432 | | * last character of the XML representation, inclusive. This includes |
433 | | * the entire text of the start and end tags of the SignedInfo element |
434 | | * as well as all descendant markup and character data (i.e., the text) |
435 | | * between those tags. Use of text based canonicalization of SignedInfo |
436 | | * is NOT RECOMMENDED. |
437 | | * |
438 | | * ================================= |
439 | | * we do not support any non XML based C14N |
440 | | * |
441 | | * Schema Definition: |
442 | | * |
443 | | * <element name="Signature" type="ds:SignatureType"/> |
444 | | * <complexType name="SignatureType"> |
445 | | * <sequence> |
446 | | * <element ref="ds:SignedInfo"/> |
447 | | * <element ref="ds:SignatureValue"/> |
448 | | * <element ref="ds:KeyInfo" minOccurs="0"/> |
449 | | * <element ref="ds:Object" minOccurs="0" maxOccurs="unbounded"/> |
450 | | * </sequence> <attribute name="Id" type="ID" use="optional"/> |
451 | | * </complexType> |
452 | | * |
453 | | * DTD: |
454 | | * |
455 | | * <!ELEMENT Signature (SignedInfo, SignatureValue, KeyInfo?, Object*) > |
456 | | * <!ATTLIST Signature |
457 | | * xmlns CDATA #FIXED 'http://www.w3.org/2000/09/xmldsig#' |
458 | | * Id ID #IMPLIED > |
459 | | * |
460 | | */ |
461 | | static int |
462 | 10.9k | xmlSecDSigCtxProcessSignatureNode(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr node) { |
463 | 10.9k | xmlSecTransformDataType firstType; |
464 | 10.9k | xmlNodePtr signedInfoNode = NULL; |
465 | 10.9k | xmlNodePtr keyInfoNode = NULL; |
466 | 10.9k | xmlNodePtr firstReferenceNode = NULL; |
467 | 10.9k | xmlNodePtr cur; |
468 | 10.9k | int ret; |
469 | | |
470 | 10.9k | xmlSecAssert2(dsigCtx != NULL, -1); |
471 | 10.9k | xmlSecAssert2((dsigCtx->operation == xmlSecTransformOperationSign) || (dsigCtx->operation == xmlSecTransformOperationVerify), -1); |
472 | 10.9k | xmlSecAssert2(dsigCtx->status == xmlSecDSigStatusUnknown, -1); |
473 | 10.9k | xmlSecAssert2(dsigCtx->signValueNode == NULL, -1); |
474 | 10.9k | xmlSecAssert2(dsigCtx->signMethod == NULL, -1); |
475 | 10.9k | xmlSecAssert2(dsigCtx->c14nMethod == NULL, -1); |
476 | 10.9k | xmlSecAssert2(node != NULL, -1); |
477 | | |
478 | 10.9k | if(!xmlSecCheckNodeName(node, xmlSecNodeSignature, xmlSecDSigNs)) { |
479 | 0 | xmlSecInvalidNodeError(node, xmlSecNodeSignature, NULL); |
480 | 0 | return(-1); |
481 | 0 | } |
482 | | |
483 | | |
484 | | /* setup transforms */ |
485 | 10.9k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_USE_ASN1_SIGNATURE_VALUES) != 0) { |
486 | 0 | dsigCtx->transformCtx.flags |= XMLSEC_TRANSFORMCTX_FLAGS_SUPPORT_ASN1_SIGNATURE_VALUES; |
487 | 0 | } |
488 | 10.9k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_ALLOW_EMPTY_INPUT) != 0) { |
489 | 0 | dsigCtx->transformCtx.flags |= XMLSEC_TRANSFORMCTX_FLAGS_ALLOW_EMPTY_INPUT; |
490 | 0 | } |
491 | | |
492 | | /* read node data */ |
493 | 10.9k | xmlSecAssert2(dsigCtx->id == NULL, -1); |
494 | | /* xmlGetProp() returns NULL both when the attribute is absent and when |
495 | | * it cannot allocate the return value, so check for presence first to |
496 | | * report the correct error */ |
497 | 10.9k | if(xmlHasProp(node, xmlSecAttrId)) { |
498 | 774 | dsigCtx->id = xmlGetProp(node, xmlSecAttrId); |
499 | 774 | if(dsigCtx->id == NULL) { |
500 | 0 | xmlSecXmlError("xmlGetProp", xmlSecNodeGetName(node)); |
501 | 0 | return(-1); |
502 | 0 | } |
503 | 774 | } |
504 | | |
505 | | /* first node is required SignedInfo */ |
506 | 10.9k | cur = xmlSecGetNextElementNode(node->children); |
507 | 10.9k | if((cur == NULL) || (!xmlSecCheckNodeName(cur, xmlSecNodeSignedInfo, xmlSecDSigNs))) { |
508 | 34 | xmlSecInvalidNodeError(cur, xmlSecNodeSignedInfo, NULL); |
509 | 34 | return(-1); |
510 | 34 | } |
511 | 10.8k | signedInfoNode = cur; |
512 | 10.8k | cur = xmlSecGetNextElementNode(cur->next); |
513 | | |
514 | | /* next node is required SignatureValue */ |
515 | 10.8k | if((cur == NULL) || (!xmlSecCheckNodeName(cur, xmlSecNodeSignatureValue, xmlSecDSigNs))) { |
516 | 3 | xmlSecInvalidNodeError(cur, xmlSecNodeSignatureValue, NULL); |
517 | 3 | return(-1); |
518 | 3 | } |
519 | 10.8k | dsigCtx->signValueNode = cur; |
520 | 10.8k | cur = xmlSecGetNextElementNode(cur->next); |
521 | | |
522 | | /* next node is optional KeyInfo */ |
523 | 10.8k | if((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeKeyInfo, xmlSecDSigNs))) { |
524 | 10.7k | keyInfoNode = cur; |
525 | 10.7k | cur = xmlSecGetNextElementNode(cur->next); |
526 | 10.7k | } else { |
527 | 183 | keyInfoNode = NULL; |
528 | 183 | } |
529 | | |
530 | | /* next nodes are optional Object nodes |
531 | | * |
532 | | * Note: Manifest nodes found in Object elements are intentionally processed here, |
533 | | * before the key is located and before the signature over SignedInfo is verified, |
534 | | * so unauthenticated content can drive reference processing (URI resolution, |
535 | | * document/resource reads, and transform and digest computation for an unbounded |
536 | | * number of references) and the Manifest digests are not enforced. The mitigations |
537 | | * are the XMLSEC_DSIG_FLAGS_IGNORE_MANIFESTS flag (checked below) and the default |
538 | | * enabledReferenceUris restriction (Empty | SameDocument) set in |
539 | | * xmlSecDSigCtxInitialize(). Applications processing untrusted documents should |
540 | | * keep the default URI restrictions or set XMLSEC_DSIG_FLAGS_IGNORE_MANIFESTS. |
541 | | * This ordering and the non-enforced Manifest digests are allowed by XML Signature 1.1 |
542 | | * section 5.1 ("the digests within such a Manifest are checked at the application's |
543 | | * discretion"). */ |
544 | 14.2k | while((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeObject, xmlSecDSigNs))) { |
545 | | /* read manifests from objects */ |
546 | 13.0k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_IGNORE_MANIFESTS) == 0) { |
547 | 13.0k | ret = xmlSecDSigCtxProcessObjectNode(dsigCtx, cur); |
548 | 13.0k | if(ret < 0) { |
549 | 9.72k | xmlSecInternalError("xmlSecDSigCtxProcessObjectNode", NULL); |
550 | 9.72k | return(-1); |
551 | 9.72k | } |
552 | 13.0k | } |
553 | 3.33k | cur = xmlSecGetNextElementNode(cur->next); |
554 | 3.33k | } |
555 | | |
556 | | /* if there is something left, then it's an error */ |
557 | 1.16k | if(cur != NULL) { |
558 | 7 | xmlSecUnexpectedNodeError(cur, NULL); |
559 | 7 | return(-1); |
560 | 7 | } |
561 | | |
562 | | /* process SignedInfo node and prepare transform */ |
563 | 1.15k | ret = xmlSecDSigCtxProcessSignedInfoNode(dsigCtx, signedInfoNode, &firstReferenceNode); |
564 | 1.15k | if(ret < 0) { |
565 | 457 | xmlSecInternalError("xmlSecDSigCtxProcessSignedInfoNode", NULL); |
566 | 457 | return(-1); |
567 | 457 | } |
568 | | |
569 | | /* as the result, we should have sign and c14n methods set */ |
570 | 701 | xmlSecAssert2(dsigCtx->signMethod != NULL, -1); |
571 | 701 | xmlSecAssert2(dsigCtx->c14nMethod != NULL, -1); |
572 | | |
573 | | /* now read key info node |
574 | | * Note: if the key cannot be located then this returns a negative value while also |
575 | | * recording xmlSecDSigStatusInvalid and xmlSecDSigFailureReasonKeyNotFound in |
576 | | * dsigCtx; unlike the reference-digest failure path in xmlSecDSigCtxProcessReferences |
577 | | * (which returns 0), this failure is propagated as a hard error on purpose, yet the |
578 | | * recorded status and failure reason remain meaningful (see xmlSecDSigCtxVerify()). */ |
579 | 701 | ret = xmlSecDSigCtxProcessKeyInfoNode(dsigCtx, keyInfoNode); |
580 | 701 | if(ret < 0) { |
581 | 701 | xmlSecInternalError("xmlSecDSigCtxProcessKeyInfoNode", NULL); |
582 | 701 | return(-1); |
583 | 701 | } |
584 | | /* as the result, we should have a key */ |
585 | 0 | xmlSecAssert2(dsigCtx->signKey != NULL, -1); |
586 | | |
587 | | /* now actually process references and calculate digests */ |
588 | 0 | ret = xmlSecDSigCtxProcessReferences(dsigCtx, firstReferenceNode); |
589 | 0 | if(ret < 0) { |
590 | 0 | xmlSecInternalError("xmlSecDSigCtxProcessReferences", NULL); |
591 | 0 | return(-1); |
592 | 0 | } |
593 | | /* references processing might change the status */ |
594 | 0 | if(dsigCtx->status != xmlSecDSigStatusUnknown) { |
595 | 0 | return(0); |
596 | 0 | } |
597 | | |
598 | | /* if we need to write result to xml node then we need base64 encode result */ |
599 | 0 | if(dsigCtx->operation == xmlSecTransformOperationSign) { |
600 | 0 | xmlSecTransformPtr base64Encode; |
601 | | |
602 | | /* we need to add base64 encode transform */ |
603 | 0 | base64Encode = xmlSecTransformCtxCreateAndAppend(&(dsigCtx->transformCtx), |
604 | 0 | xmlSecTransformBase64Id); |
605 | 0 | if(base64Encode == NULL) { |
606 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend", NULL); |
607 | 0 | return(-1); |
608 | 0 | } |
609 | 0 | base64Encode->operation = xmlSecTransformOperationEncode; |
610 | 0 | } |
611 | | |
612 | 0 | firstType = xmlSecTransformGetDataType(dsigCtx->transformCtx.first, |
613 | 0 | xmlSecTransformModePush, |
614 | 0 | &(dsigCtx->transformCtx)); |
615 | 0 | if((firstType & xmlSecTransformDataTypeXml) != 0) { |
616 | 0 | xmlSecNodeSetPtr nodeset = NULL; |
617 | |
|
618 | 0 | xmlSecAssert2(signedInfoNode != NULL, -1); |
619 | 0 | nodeset = xmlSecNodeSetGetChildren(signedInfoNode->doc, signedInfoNode, 1, 0); |
620 | 0 | if(nodeset == NULL) { |
621 | 0 | xmlSecInternalError("xmlSecNodeSetGetChildren(signedInfoNode)", NULL); |
622 | 0 | return(-1); |
623 | 0 | } |
624 | | |
625 | | /* calculate the signature */ |
626 | 0 | ret = xmlSecTransformCtxXmlExecute(&(dsigCtx->transformCtx), nodeset); |
627 | 0 | if(ret < 0) { |
628 | 0 | xmlSecInternalError("xmlSecTransformCtxXmlExecute", NULL); |
629 | 0 | xmlSecTransformCtxClearNodeRefs(&(dsigCtx->transformCtx), nodeset); |
630 | 0 | xmlSecNodeSetDestroy(nodeset); |
631 | 0 | return(-1); |
632 | 0 | } |
633 | | |
634 | 0 | xmlSecTransformCtxClearNodeRefs(&(dsigCtx->transformCtx), nodeset); |
635 | 0 | xmlSecNodeSetDestroy(nodeset); |
636 | 0 | } else { |
637 | 0 | xmlSecNotImplementedError("The first transform in the SignedInfo canonicalization chain does not accept an XML node-set input"); |
638 | 0 | return(-1); |
639 | 0 | } |
640 | 0 | return(0); |
641 | 0 | } |
642 | | |
643 | | /** |
644 | | * @brief Processes the dsig:SignedInfo element. |
645 | | * @details The SignedInfo Element (http://www.w3.org/TR/xmldsig-core/#sec-SignedInfo) |
646 | | * |
647 | | * The structure of SignedInfo includes the canonicalization algorithm, |
648 | | * a signature algorithm, and one or more references. The SignedInfo element |
649 | | * may contain an optional ID attribute that will allow it to be referenced by |
650 | | * other signatures and objects. |
651 | | * |
652 | | * SignedInfo does not include explicit signature or digest properties (such as |
653 | | * calculation time, cryptographic device serial number, etc.). If an |
654 | | * application needs to associate properties with the signature or digest, |
655 | | * it may include such information in a SignatureProperties element within |
656 | | * an Object element. |
657 | | * |
658 | | * Schema Definition: |
659 | | * @code{.xml} |
660 | | * <element name="SignedInfo" type="ds:SignedInfoType"/> |
661 | | * <complexType name="SignedInfoType"> |
662 | | * <sequence> |
663 | | * <element ref="ds:CanonicalizationMethod"/> |
664 | | * <element ref="ds:SignatureMethod"/> |
665 | | * <element ref="ds:Reference" maxOccurs="unbounded"/> |
666 | | * </sequence> |
667 | | * <attribute name="Id" type="ID" use="optional"/> |
668 | | * </complexType> |
669 | | * |
670 | | * DTD: |
671 | | * |
672 | | * <!ELEMENT SignedInfo (CanonicalizationMethod, SignatureMethod, Reference+) > |
673 | | * <!ATTLIST SignedInfo Id ID #IMPLIED> |
674 | | * @endcode |
675 | | */ |
676 | | static int |
677 | 1.15k | xmlSecDSigCtxProcessSignedInfoNode(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr node, xmlNodePtr * firstReferenceNode) { |
678 | 1.15k | xmlSecSize refNodesCount = 0; |
679 | 1.15k | xmlNodePtr cur; |
680 | | |
681 | 1.15k | xmlSecAssert2(dsigCtx != NULL, -1); |
682 | 1.15k | xmlSecAssert2(dsigCtx->status == xmlSecDSigStatusUnknown, -1); |
683 | 1.15k | xmlSecAssert2(dsigCtx->signMethod == NULL, -1); |
684 | 1.15k | xmlSecAssert2(dsigCtx->c14nMethod == NULL, -1); |
685 | 1.15k | xmlSecAssert2((dsigCtx->operation == xmlSecTransformOperationSign) || (dsigCtx->operation == xmlSecTransformOperationVerify), -1); |
686 | 1.15k | xmlSecAssert2(node != NULL, -1); |
687 | 1.15k | xmlSecAssert2(firstReferenceNode != NULL, -1); |
688 | 1.15k | xmlSecAssert2((*firstReferenceNode) == NULL, -1); |
689 | | |
690 | | /* first node is required CanonicalizationMethod. */ |
691 | 1.15k | cur = xmlSecGetNextElementNode(node->children); |
692 | 1.15k | if((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeCanonicalizationMethod, xmlSecDSigNs))) { |
693 | 920 | dsigCtx->c14nMethod = xmlSecTransformCtxNodeRead(&(dsigCtx->transformCtx), |
694 | 920 | cur, xmlSecTransformUsageC14NMethod); |
695 | 920 | if(dsigCtx->c14nMethod == NULL) { |
696 | 167 | xmlSecInternalError2("xmlSecTransformCtxNodeRead", NULL, |
697 | 167 | "node=%s", xmlSecErrorsSafeString(xmlSecNodeGetName(cur))); |
698 | 167 | return(-1); |
699 | 167 | } |
700 | | /* the C14N node was consumed; move to the next element */ |
701 | 753 | cur = xmlSecGetNextElementNode(cur->next); |
702 | 753 | } else if(dsigCtx->defC14NMethodId != xmlSecTransformIdUnknown) { |
703 | | /* the dsig spec does require the CanonicalizationMethod node |
704 | | * to be present but in some cases the application might decide to |
705 | | * minimize traffic */ |
706 | 0 | dsigCtx->c14nMethod = xmlSecTransformCtxCreateAndAppend(&(dsigCtx->transformCtx), |
707 | 0 | dsigCtx->defC14NMethodId); |
708 | 0 | if(dsigCtx->c14nMethod == NULL) { |
709 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend", NULL); |
710 | 0 | return(-1); |
711 | 0 | } |
712 | 238 | } else { |
713 | | /* if c14n method is not specified in the template and not specified in |
714 | | * the dsig context then it's an error. |
715 | | */ |
716 | 238 | xmlSecInvalidNodeError(cur, xmlSecNodeCanonicalizationMethod, NULL); |
717 | 238 | return(-1); |
718 | 238 | } |
719 | | |
720 | | /* insert membuf if requested */ |
721 | 753 | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_STORE_SIGNATURE) != 0) { |
722 | 0 | xmlSecAssert2(dsigCtx->preSignMemBufMethod == NULL, -1); |
723 | 0 | dsigCtx->preSignMemBufMethod = xmlSecTransformCtxCreateAndAppend(&(dsigCtx->transformCtx), |
724 | 0 | xmlSecTransformMemBufId); |
725 | 0 | if(dsigCtx->preSignMemBufMethod == NULL) { |
726 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend", |
727 | 0 | xmlSecTransformKlassGetName(xmlSecTransformMemBufId)); |
728 | 0 | return(-1); |
729 | 0 | } |
730 | 0 | } |
731 | | |
732 | | /* next node is required SignatureMethod. |
733 | | * Note: 'cur' already points to the element after the CanonicalizationMethod |
734 | | * (or to the first element child if a default C14N method was used), so no advancement here. */ |
735 | 753 | if((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeSignatureMethod, xmlSecDSigNs))) { |
736 | 737 | dsigCtx->signMethod = xmlSecTransformCtxNodeRead(&(dsigCtx->transformCtx), |
737 | 737 | cur, xmlSecTransformUsageSignatureMethod); |
738 | 737 | if(dsigCtx->signMethod == NULL) { |
739 | 25 | xmlSecInternalError("xmlSecTransformCtxNodeRead", |
740 | 25 | xmlSecNodeGetName(cur)); |
741 | 25 | return(-1); |
742 | 25 | } |
743 | 712 | cur = xmlSecGetNextElementNode(cur->next); |
744 | 712 | } else if(dsigCtx->defSignMethodId != xmlSecTransformIdUnknown) { |
745 | | /* the dsig spec does require the SignatureMethod node |
746 | | * to be present but in some cases the application might decide to |
747 | | * minimize traffic */ |
748 | 0 | dsigCtx->signMethod = xmlSecTransformCtxCreateAndAppend(&(dsigCtx->transformCtx), dsigCtx->defSignMethodId); |
749 | 0 | if(dsigCtx->signMethod == NULL) { |
750 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend", NULL); |
751 | 0 | return(-1); |
752 | 0 | } |
753 | 16 | } else { |
754 | | /* if sign method is not specified in the template and not specified in |
755 | | * the dsig context then it's an error. |
756 | | */ |
757 | 16 | xmlSecInvalidNodeError(cur, xmlSecNodeSignatureMethod, NULL); |
758 | 16 | return(-1); |
759 | 16 | } |
760 | 712 | dsigCtx->signMethod->operation = dsigCtx->operation; |
761 | | |
762 | | /* read references */ |
763 | 2.87k | while((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeReference, xmlSecDSigNs))) { |
764 | | /* record first reference node */ |
765 | 2.16k | if((*firstReferenceNode) == NULL) { |
766 | 703 | (*firstReferenceNode) = cur; |
767 | 703 | } |
768 | 2.16k | ++refNodesCount; |
769 | | |
770 | | /* go to next */ |
771 | 2.16k | cur = xmlSecGetNextElementNode(cur->next); |
772 | 2.16k | } |
773 | | |
774 | | /* check that we have at least one Reference */ |
775 | 712 | if(refNodesCount == 0) { |
776 | 9 | xmlSecOtherError(XMLSEC_ERRORS_R_DSIG_NO_REFERENCES, NULL, NULL); |
777 | 9 | return(-1); |
778 | 9 | } |
779 | | |
780 | | /* if there is something left, then it's an error */ |
781 | 703 | if(cur != NULL) { |
782 | 2 | xmlSecUnexpectedNodeError(cur, NULL); |
783 | 2 | return(-1); |
784 | 2 | } |
785 | | |
786 | | /* done */ |
787 | 701 | return(0); |
788 | 703 | } |
789 | | |
790 | | |
791 | | static int |
792 | 0 | xmlSecDSigCtxProcessReferences(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr firstReferenceNode) { |
793 | 0 | xmlSecDSigReferenceCtxPtr dsigRefCtx; |
794 | 0 | xmlNodePtr cur; |
795 | 0 | int ret; |
796 | |
|
797 | 0 | xmlSecAssert2(dsigCtx != NULL, -1); |
798 | 0 | xmlSecAssert2(dsigCtx->status == xmlSecDSigStatusUnknown, -1); |
799 | 0 | xmlSecAssert2((dsigCtx->operation == xmlSecTransformOperationSign) || (dsigCtx->operation == xmlSecTransformOperationVerify), -1); |
800 | 0 | xmlSecAssert2(xmlSecPtrListGetSize(&(dsigCtx->signedInfoReferences)) == 0, -1); |
801 | 0 | xmlSecAssert2(firstReferenceNode != NULL, -1); |
802 | | |
803 | | /* process references */ |
804 | 0 | for(cur = firstReferenceNode; (cur != NULL); cur = xmlSecGetNextElementNode(cur->next)) { |
805 | | /* already checked but we trust none */ |
806 | 0 | if(!xmlSecCheckNodeName(cur, xmlSecNodeReference, xmlSecDSigNs)) { |
807 | 0 | xmlSecInvalidNodeError(cur, xmlSecNodeReference, NULL); |
808 | 0 | return(-1); |
809 | 0 | } |
810 | | |
811 | | /* create reference */ |
812 | 0 | dsigRefCtx = xmlSecDSigReferenceCtxCreate(dsigCtx, xmlSecDSigReferenceOriginSignedInfo); |
813 | 0 | if(dsigRefCtx == NULL) { |
814 | 0 | xmlSecInternalError("xmlSecDSigReferenceCtxCreate", NULL); |
815 | 0 | return(-1); |
816 | 0 | } |
817 | | |
818 | | /* add to the list */ |
819 | 0 | ret = xmlSecPtrListAdd(&(dsigCtx->signedInfoReferences), dsigRefCtx); |
820 | 0 | if(ret < 0) { |
821 | 0 | xmlSecInternalError("xmlSecPtrListAdd", NULL); |
822 | 0 | xmlSecDSigReferenceCtxDestroy(dsigRefCtx); |
823 | 0 | return(-1); |
824 | 0 | } |
825 | | |
826 | | /* process */ |
827 | 0 | ret = xmlSecDSigReferenceCtxProcessNode(dsigRefCtx, cur); |
828 | 0 | if(ret < 0) { |
829 | 0 | xmlSecInternalError("xmlSecDSigReferenceCtxProcessNode", |
830 | 0 | xmlSecNodeGetName(cur)); |
831 | 0 | return(-1); |
832 | 0 | } |
833 | | |
834 | | /* bail out if the current Reference processing failed */ |
835 | 0 | if(dsigRefCtx->status != xmlSecDSigStatusSucceeded) { |
836 | 0 | xmlSecDSigCtxMarkAsFailed(dsigCtx, xmlSecDSigFailureReasonReference); |
837 | 0 | return(0); |
838 | 0 | } |
839 | 0 | } |
840 | | |
841 | | /* done */ |
842 | 0 | return(0); |
843 | 0 | } |
844 | | |
845 | | |
846 | | static int |
847 | 701 | xmlSecDSigCtxProcessKeyInfoNode(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr node) { |
848 | 701 | int ret; |
849 | | |
850 | 701 | xmlSecAssert2(dsigCtx != NULL, -1); |
851 | 701 | xmlSecAssert2(dsigCtx->signMethod != NULL, -1); |
852 | | |
853 | | /* set key requirements */ |
854 | 701 | ret = xmlSecTransformSetKeyReq(dsigCtx->signMethod, &(dsigCtx->keyInfoReadCtx.keyReq)); |
855 | 701 | if(ret < 0) { |
856 | 0 | xmlSecInternalError("xmlSecTransformSetKeyReq", xmlSecTransformGetName(dsigCtx->signMethod)); |
857 | 0 | return(-1); |
858 | 0 | } |
859 | | |
860 | | /* ignore <dsig:KeyInfo /> if the key is already set */ |
861 | 701 | if((dsigCtx->signKey == NULL) && (dsigCtx->keyInfoReadCtx.keysMngr != NULL) && (dsigCtx->keyInfoReadCtx.keysMngr->getKey != NULL)) { |
862 | 701 | dsigCtx->signKey = (dsigCtx->keyInfoReadCtx.keysMngr->getKey)(node, &(dsigCtx->keyInfoReadCtx)); |
863 | 701 | } |
864 | | |
865 | | /* check that we have exactly what we want */ |
866 | 701 | if((dsigCtx->signKey == NULL) || (!xmlSecKeyMatch(dsigCtx->signKey, NULL, &(dsigCtx->keyInfoReadCtx.keyReq)))) { |
867 | 701 | xmlSecOtherError(XMLSEC_ERRORS_R_KEY_NOT_FOUND, NULL, NULL); |
868 | | /* record the failure reason before propagating this as a hard error: status and |
869 | | * failureReason remain meaningful to the application even though a negative value |
870 | | * is returned (see xmlSecDSigCtxVerify()) */ |
871 | 701 | xmlSecDSigCtxMarkAsFailed(dsigCtx, xmlSecDSigFailureReasonKeyNotFound); |
872 | 701 | return(-1); |
873 | 701 | } |
874 | | |
875 | | /* set the key to the transform */ |
876 | 0 | ret = xmlSecTransformSetKey(dsigCtx->signMethod, dsigCtx->signKey); |
877 | 0 | if(ret < 0) { |
878 | 0 | xmlSecInternalError("xmlSecTransformSetKey", xmlSecTransformGetName(dsigCtx->signMethod)); |
879 | 0 | return(-1); |
880 | 0 | } |
881 | | |
882 | | /* if we are signing a document, update <dsig:KeyInfo/> node */ |
883 | 0 | if((node != NULL) && (dsigCtx->operation == xmlSecTransformOperationSign)) { |
884 | 0 | ret = xmlSecKeyInfoNodeWrite(node, dsigCtx->signKey, &(dsigCtx->keyInfoWriteCtx)); |
885 | 0 | if(ret < 0) { |
886 | 0 | xmlSecInternalError("xmlSecKeyInfoNodeWrite", NULL); |
887 | 0 | return(-1); |
888 | 0 | } |
889 | 0 | } |
890 | | |
891 | 0 | return(0); |
892 | 0 | } |
893 | | |
894 | | /** |
895 | | * @brief Processes a dsig:Object element. |
896 | | * @details The Object Element (http://www.w3.org/TR/xmldsig-core/#sec-Object) |
897 | | * |
898 | | * Object is an optional element that may occur one or more times. When |
899 | | * present, this element may contain any data. The Object element may include |
900 | | * optional MIME type, ID, and encoding attributes. |
901 | | * |
902 | | * Schema Definition: |
903 | | * @code{.xml} |
904 | | * <element name="Object" type="ds:ObjectType"/> |
905 | | * <complexType name="ObjectType" mixed="true"> |
906 | | * <sequence minOccurs="0" maxOccurs="unbounded"> |
907 | | * <any namespace="##any" processContents="lax"/> |
908 | | * </sequence> |
909 | | * <attribute name="Id" type="ID" use="optional"/> |
910 | | * <attribute name="MimeType" type="string" use="optional"/> |
911 | | * <attribute name="Encoding" type="anyURI" use="optional"/> |
912 | | * </complexType> |
913 | | * |
914 | | * DTD: |
915 | | * |
916 | | * <!ELEMENT Object (#PCDATA|Signature|SignatureProperties|Manifest %Object.ANY;)* > |
917 | | * <!ATTLIST Object Id ID #IMPLIED |
918 | | * MimeType CDATA #IMPLIED |
919 | | * Encoding CDATA #IMPLIED > |
920 | | * @endcode |
921 | | */ |
922 | | static int |
923 | 13.0k | xmlSecDSigCtxProcessObjectNode(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr node) { |
924 | 13.0k | xmlNodePtr cur; |
925 | 13.0k | int ret; |
926 | | |
927 | 13.0k | xmlSecAssert2(dsigCtx != NULL, -1); |
928 | 13.0k | xmlSecAssert2(dsigCtx->status == xmlSecDSigStatusUnknown, -1); |
929 | 13.0k | xmlSecAssert2(node != NULL, -1); |
930 | | |
931 | | /* We care about Manifest nodes only; ignore everything else. Per the XMLDSig DTD |
932 | | * the Manifest node is a direct child of Object node, so only direct children are scanned. */ |
933 | 13.0k | cur = xmlSecGetNextElementNode(node->children); |
934 | 15.3k | while(cur != NULL) { |
935 | 12.0k | if(xmlSecCheckNodeName(cur, xmlSecNodeManifest, xmlSecDSigNs)) { |
936 | 10.2k | ret = xmlSecDSigCtxProcessManifestNode(dsigCtx, cur); |
937 | 10.2k | if(ret < 0) { |
938 | 9.72k | xmlSecInternalError("xmlSecDSigCtxProcessManifestNode", NULL); |
939 | 9.72k | return(-1); |
940 | 9.72k | } |
941 | 10.2k | } |
942 | 2.30k | cur = xmlSecGetNextElementNode(cur->next); |
943 | 2.30k | } |
944 | 3.33k | return(0); |
945 | 13.0k | } |
946 | | |
947 | | /** |
948 | | * @brief Processes a dsig:Manifest element. |
949 | | * @details The Manifest Element (http://www.w3.org/TR/xmldsig-core/#sec-Manifest) |
950 | | * |
951 | | * The Manifest element provides a list of References. The difference from |
952 | | * the list in SignedInfo is that it is application defined which, if any, of |
953 | | * the digests are actually checked against the objects referenced and what to |
954 | | * do if the object is inaccessible or the digest compare fails. If a Manifest |
955 | | * is pointed to from SignedInfo, the digest over the Manifest itself will be |
956 | | * checked by the core signature validation behavior. The digests within such |
957 | | * a Manifest are checked at the application's discretion. If a Manifest is |
958 | | * referenced from another Manifest, even the overall digest of this two-level-deep |
959 | | * Manifest might not be checked. |
960 | | * |
961 | | * Schema Definition: |
962 | | * @code{.xml} |
963 | | * <element name="Manifest" type="ds:ManifestType"/> |
964 | | * <complexType name="ManifestType"> |
965 | | * <sequence> |
966 | | * <element ref="ds:Reference" maxOccurs="unbounded"/> |
967 | | * </sequence> |
968 | | * <attribute name="Id" type="ID" use="optional"/> |
969 | | * </complexType> |
970 | | * |
971 | | * DTD: |
972 | | * |
973 | | * <!ELEMENT Manifest (Reference+) > |
974 | | * <!ATTLIST Manifest Id ID #IMPLIED > |
975 | | * @endcode |
976 | | */ |
977 | | static int |
978 | 10.2k | xmlSecDSigCtxProcessManifestNode(xmlSecDSigCtxPtr dsigCtx, xmlNodePtr node) { |
979 | 10.2k | xmlSecDSigReferenceCtxPtr dsigRefCtx; |
980 | 10.2k | xmlSecSize refNodesCount = 0; |
981 | 10.2k | xmlNodePtr cur; |
982 | 10.2k | int ret; |
983 | | |
984 | 10.2k | xmlSecAssert2(dsigCtx != NULL, -1); |
985 | 10.2k | xmlSecAssert2(dsigCtx->status == xmlSecDSigStatusUnknown, -1); |
986 | 10.2k | xmlSecAssert2(node != NULL, -1); |
987 | | |
988 | | /* process references */ |
989 | 10.2k | cur = xmlSecGetNextElementNode(node->children); |
990 | 15.9k | while((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeReference, xmlSecDSigNs))) { |
991 | | /* create reference context */ |
992 | 15.3k | dsigRefCtx = xmlSecDSigReferenceCtxCreate(dsigCtx, xmlSecDSigReferenceOriginManifest); |
993 | 15.3k | if(dsigRefCtx == NULL) { |
994 | 0 | xmlSecInternalError("xmlSecDSigReferenceCtxCreate", NULL); |
995 | 0 | return(-1); |
996 | 0 | } |
997 | | |
998 | | /* add to the list */ |
999 | 15.3k | ret = xmlSecPtrListAdd(&(dsigCtx->manifestReferences), dsigRefCtx); |
1000 | 15.3k | if(ret < 0) { |
1001 | 0 | xmlSecInternalError("xmlSecPtrListAdd", NULL); |
1002 | 0 | xmlSecDSigReferenceCtxDestroy(dsigRefCtx); |
1003 | 0 | return(-1); |
1004 | 0 | } |
1005 | | |
1006 | | /* note that a digest mismatch only sets dsigRefCtx->status to xmlSecDSigStatusInvalid |
1007 | | * (the call below still returns 0), and that status is intentionally not used to fail |
1008 | | * the whole signature here; unlike the SignedInfo references (see |
1009 | | * xmlSecDSigCtxProcessReferences), the digests within a Manifest are |
1010 | | * checked at the application's discretion (see the function docs and |
1011 | | * XML Signature 1.1 REC section 5.1). The application can inspect |
1012 | | * the per-reference statuses via dsigCtx->manifestReferences. */ |
1013 | 15.3k | ret = xmlSecDSigReferenceCtxProcessNode(dsigRefCtx, cur); |
1014 | 15.3k | if(ret < 0) { |
1015 | 9.72k | xmlSecInternalError("xmlSecDSigReferenceCtxProcessNode", NULL); |
1016 | 9.72k | return(-1); |
1017 | 9.72k | } |
1018 | | |
1019 | 5.63k | ++refNodesCount; |
1020 | | |
1021 | 5.63k | cur = xmlSecGetNextElementNode(cur->next); |
1022 | 5.63k | } |
1023 | | |
1024 | | /* check that we have at least one Reference */ |
1025 | 561 | if(refNodesCount == 0) { |
1026 | 4 | xmlSecOtherError(XMLSEC_ERRORS_R_DSIG_NO_REFERENCES, NULL, NULL); |
1027 | 4 | return(-1); |
1028 | 4 | } |
1029 | | |
1030 | | /* we should have nothing else here */ |
1031 | 557 | if(cur != NULL) { |
1032 | 1 | xmlSecUnexpectedNodeError(cur, NULL); |
1033 | 1 | return(-1); |
1034 | 1 | } |
1035 | 556 | return(0); |
1036 | 557 | } |
1037 | | |
1038 | | /** |
1039 | | * @brief Gets status as a string. |
1040 | | * @param status the status. |
1041 | | * @return status as a string. |
1042 | | */ |
1043 | | const char* |
1044 | 0 | xmlSecDSigCtxGetStatusString(xmlSecDSigStatus status) { |
1045 | 0 | switch(status) { |
1046 | 0 | case xmlSecDSigStatusSucceeded: |
1047 | 0 | return "OK"; |
1048 | | |
1049 | 0 | case xmlSecDSigStatusInvalid: |
1050 | 0 | return "FAILED"; |
1051 | | |
1052 | | /* unknown status can only be caused by an error in the operation */ |
1053 | 0 | case xmlSecDSigStatusUnknown: |
1054 | 0 | default: |
1055 | 0 | return "ERROR"; |
1056 | 0 | } |
1057 | 0 | } |
1058 | | |
1059 | | /** |
1060 | | * @brief Gets failure reason as a string. |
1061 | | * @param failureReason the failure reason. |
1062 | | * @return failure reason as a string. |
1063 | | */ |
1064 | | const char* |
1065 | 0 | xmlSecDSigCtxGetFailureReasonString(xmlSecDSigFailureReason failureReason) { |
1066 | 0 | switch(failureReason) { |
1067 | 0 | case xmlSecDSigFailureReasonReference: |
1068 | 0 | return "REFERENCE"; |
1069 | | |
1070 | 0 | case xmlSecDSigFailureReasonSignature: |
1071 | 0 | return "SIGNATURE"; |
1072 | | |
1073 | 0 | case xmlSecDSigFailureReasonKeyNotFound: |
1074 | 0 | return "KEY-NOT-FOUND"; |
1075 | | |
1076 | 0 | case xmlSecDSigFailureReasonUnknown: |
1077 | 0 | default: |
1078 | 0 | return "UNKNOWN"; |
1079 | 0 | } |
1080 | 0 | } |
1081 | | |
1082 | | /** |
1083 | | * @brief Prints debug information about the context. |
1084 | | * @details Prints the debug information about @p dsigCtx to @p output. |
1085 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
1086 | | * @param output the pointer to output FILE. |
1087 | | */ |
1088 | | void |
1089 | 0 | xmlSecDSigCtxDebugDump(xmlSecDSigCtxPtr dsigCtx, FILE* output) { |
1090 | 0 | xmlSecAssert(dsigCtx != NULL); |
1091 | 0 | xmlSecAssert(output != NULL); |
1092 | | |
1093 | 0 | if(dsigCtx->operation == xmlSecTransformOperationSign) { |
1094 | 0 | fprintf(output, "= SIGNATURE CONTEXT\n"); |
1095 | 0 | } else { |
1096 | 0 | fprintf(output, "= VERIFICATION CONTEXT\n"); |
1097 | 0 | } |
1098 | 0 | fprintf(output, "== Status: %s\n", xmlSecDSigCtxGetStatusString(dsigCtx->status)); |
1099 | 0 | fprintf(output, "== Failure reason: %s\n", xmlSecDSigCtxGetFailureReasonString(dsigCtx->failureReason)); |
1100 | |
|
1101 | 0 | fprintf(output, "== flags: 0x%08x\n", dsigCtx->flags); |
1102 | 0 | fprintf(output, "== flags2: 0x%08x\n", dsigCtx->flags2); |
1103 | |
|
1104 | 0 | if(dsigCtx->id != NULL) { |
1105 | 0 | fprintf(output, "== Id: \"%s\"\n", (const char*)dsigCtx->id); |
1106 | 0 | } |
1107 | |
|
1108 | 0 | fprintf(output, "== Key Info Read Ctx:\n"); |
1109 | 0 | xmlSecKeyInfoCtxDebugDump(&(dsigCtx->keyInfoReadCtx), output); |
1110 | 0 | fprintf(output, "== Key Info Write Ctx:\n"); |
1111 | 0 | xmlSecKeyInfoCtxDebugDump(&(dsigCtx->keyInfoWriteCtx), output); |
1112 | |
|
1113 | 0 | fprintf(output, "== Signature Transform Ctx:\n"); |
1114 | 0 | xmlSecTransformCtxDebugDump(&(dsigCtx->transformCtx), output); |
1115 | |
|
1116 | 0 | if(dsigCtx->signMethod != NULL) { |
1117 | 0 | fprintf(output, "== Signature Method:\n"); |
1118 | 0 | xmlSecTransformDebugDump(dsigCtx->signMethod, output); |
1119 | 0 | } |
1120 | |
|
1121 | 0 | if(dsigCtx->signKey != NULL) { |
1122 | 0 | fprintf(output, "== Signature Key:\n"); |
1123 | 0 | xmlSecKeyDebugDump(dsigCtx->signKey, output); |
1124 | 0 | } |
1125 | |
|
1126 | 0 | fprintf(output, "== SignedInfo References List:\n"); |
1127 | 0 | xmlSecPtrListDebugDump(&(dsigCtx->signedInfoReferences), output); |
1128 | |
|
1129 | 0 | fprintf(output, "== Manifest References List:\n"); |
1130 | 0 | xmlSecPtrListDebugDump(&(dsigCtx->manifestReferences), output); |
1131 | |
|
1132 | 0 | if((dsigCtx->result != NULL) && |
1133 | 0 | (xmlSecBufferGetData(dsigCtx->result) != NULL)) { |
1134 | |
|
1135 | 0 | fprintf(output, "== Result - start buffer:\n"); |
1136 | 0 | xmlSecBufferDebugHexDump(dsigCtx->result, output); |
1137 | 0 | fprintf(output, "\n== Result - end buffer\n"); |
1138 | 0 | } |
1139 | 0 | if(((dsigCtx->flags & XMLSEC_DSIG_FLAGS_STORE_SIGNATURE) != 0) && |
1140 | 0 | (xmlSecDSigCtxGetPreSignBuffer(dsigCtx) != NULL) && |
1141 | 0 | (xmlSecBufferGetData(xmlSecDSigCtxGetPreSignBuffer(dsigCtx)) != NULL)) { |
1142 | |
|
1143 | 0 | fprintf(output, "== PreSign data - start buffer:\n"); |
1144 | 0 | xmlSecBufferDebugHexDump(xmlSecDSigCtxGetPreSignBuffer(dsigCtx), output); |
1145 | 0 | fprintf(output, "\n== PreSign data - end buffer\n"); |
1146 | 0 | } |
1147 | 0 | } |
1148 | | |
1149 | | /** |
1150 | | * @brief Prints debug information about the context in XML format. |
1151 | | * @details Prints the debug information about @p dsigCtx to @p output in XML format. |
1152 | | * @param dsigCtx the pointer to <dsig:Signature/> processing context. |
1153 | | * @param output the pointer to output FILE. |
1154 | | */ |
1155 | | void |
1156 | 0 | xmlSecDSigCtxDebugXmlDump(xmlSecDSigCtxPtr dsigCtx, FILE* output) { |
1157 | 0 | xmlSecAssert(dsigCtx != NULL); |
1158 | 0 | xmlSecAssert(output != NULL); |
1159 | | |
1160 | 0 | if(dsigCtx->operation == xmlSecTransformOperationSign) { |
1161 | 0 | fprintf(output, "<SignatureContext"); |
1162 | 0 | } else { |
1163 | 0 | fprintf(output, "<VerificationContext"); |
1164 | 0 | } |
1165 | 0 | fprintf(output, " status=\"%s\"", xmlSecDSigCtxGetStatusString(dsigCtx->status)); |
1166 | 0 | fprintf(output, " failureReason=\"%s\"", xmlSecDSigCtxGetFailureReasonString(dsigCtx->failureReason)); |
1167 | 0 | fprintf(output, ">\n"); |
1168 | |
|
1169 | 0 | fprintf(output, "<Flags>%08x</Flags>\n", dsigCtx->flags); |
1170 | 0 | fprintf(output, "<Flags2>%08x</Flags2>\n", dsigCtx->flags2); |
1171 | |
|
1172 | 0 | fprintf(output, "<Id>"); |
1173 | 0 | xmlSecPrintXmlString(output, dsigCtx->id); |
1174 | 0 | fprintf(output, "</Id>\n"); |
1175 | |
|
1176 | 0 | fprintf(output, "<KeyInfoReadCtx>\n"); |
1177 | 0 | xmlSecKeyInfoCtxDebugXmlDump(&(dsigCtx->keyInfoReadCtx), output); |
1178 | 0 | fprintf(output, "</KeyInfoReadCtx>\n"); |
1179 | |
|
1180 | 0 | fprintf(output, "<KeyInfoWriteCtx>\n"); |
1181 | 0 | xmlSecKeyInfoCtxDebugXmlDump(&(dsigCtx->keyInfoWriteCtx), output); |
1182 | 0 | fprintf(output, "</KeyInfoWriteCtx>\n"); |
1183 | |
|
1184 | 0 | fprintf(output, "<SignatureTransformCtx>\n"); |
1185 | 0 | xmlSecTransformCtxDebugXmlDump(&(dsigCtx->transformCtx), output); |
1186 | 0 | fprintf(output, "</SignatureTransformCtx>\n"); |
1187 | |
|
1188 | 0 | if(dsigCtx->signMethod != NULL) { |
1189 | 0 | fprintf(output, "<SignatureMethod>\n"); |
1190 | 0 | xmlSecTransformDebugXmlDump(dsigCtx->signMethod, output); |
1191 | 0 | fprintf(output, "</SignatureMethod>\n"); |
1192 | 0 | } |
1193 | |
|
1194 | 0 | if(dsigCtx->signKey != NULL) { |
1195 | 0 | fprintf(output, "<SignatureKey>\n"); |
1196 | 0 | xmlSecKeyDebugXmlDump(dsigCtx->signKey, output); |
1197 | 0 | fprintf(output, "</SignatureKey>\n"); |
1198 | 0 | } |
1199 | |
|
1200 | 0 | fprintf(output, "<SignedInfoReferences>\n"); |
1201 | 0 | xmlSecPtrListDebugXmlDump(&(dsigCtx->signedInfoReferences), output); |
1202 | 0 | fprintf(output, "</SignedInfoReferences>\n"); |
1203 | |
|
1204 | 0 | fprintf(output, "<ManifestReferences>\n"); |
1205 | 0 | xmlSecPtrListDebugXmlDump(&(dsigCtx->manifestReferences), output); |
1206 | 0 | fprintf(output, "</ManifestReferences>\n"); |
1207 | |
|
1208 | 0 | if((dsigCtx->result != NULL) && (xmlSecBufferGetData(dsigCtx->result) != NULL)) { |
1209 | 0 | fprintf(output, "<Result>\n"); |
1210 | 0 | xmlSecBufferDebugHexDump(dsigCtx->result, output); |
1211 | 0 | fprintf(output, "\n</Result>\n"); |
1212 | 0 | } |
1213 | 0 | if(((dsigCtx->flags & XMLSEC_DSIG_FLAGS_STORE_SIGNATURE) != 0) && |
1214 | 0 | (xmlSecDSigCtxGetPreSignBuffer(dsigCtx) != NULL) && |
1215 | 0 | (xmlSecBufferGetData(xmlSecDSigCtxGetPreSignBuffer(dsigCtx)) != NULL)) { |
1216 | |
|
1217 | 0 | fprintf(output, "<PreSignData>\n"); |
1218 | 0 | xmlSecBufferDebugHexDump(xmlSecDSigCtxGetPreSignBuffer(dsigCtx), output); |
1219 | 0 | fprintf(output, "\n</PreSignData>\n"); |
1220 | 0 | } |
1221 | |
|
1222 | 0 | if(dsigCtx->operation == xmlSecTransformOperationSign) { |
1223 | 0 | fprintf(output, "</SignatureContext>\n"); |
1224 | 0 | } else { |
1225 | 0 | fprintf(output, "</VerificationContext>\n"); |
1226 | 0 | } |
1227 | 0 | } |
1228 | | |
1229 | | /****************************************************************************** |
1230 | | * |
1231 | | * xmlSecDSigReferenceCtx |
1232 | | * |
1233 | | *****************************************************************************/ |
1234 | | /** |
1235 | | * @brief Creates a dsig:Reference processing context. |
1236 | | * @details Creates new <dsig:Reference/> element processing context. Caller is responsible |
1237 | | * for destroying the returned context by calling #xmlSecDSigReferenceCtxDestroy |
1238 | | * function. |
1239 | | * |
1240 | | * @param dsigCtx the pointer to parent <dsig:Signature/> node processing context. |
1241 | | * @param origin the reference origin (<dsig:SignedInfo/> or <dsig:Manifest/> node). |
1242 | | * @return pointer to newly created context or NULL if an error occurs. |
1243 | | */ |
1244 | | xmlSecDSigReferenceCtxPtr |
1245 | 15.3k | xmlSecDSigReferenceCtxCreate(xmlSecDSigCtxPtr dsigCtx, xmlSecDSigReferenceOrigin origin) { |
1246 | 15.3k | xmlSecDSigReferenceCtxPtr dsigRefCtx; |
1247 | 15.3k | int ret; |
1248 | | |
1249 | 15.3k | xmlSecAssert2(dsigCtx != NULL, NULL); |
1250 | | |
1251 | 15.3k | dsigRefCtx = (xmlSecDSigReferenceCtxPtr) xmlMalloc(sizeof(xmlSecDSigReferenceCtx)); |
1252 | 15.3k | if(dsigRefCtx == NULL) { |
1253 | 0 | xmlSecMallocError(sizeof(xmlSecDSigReferenceCtx), NULL); |
1254 | 0 | return(NULL); |
1255 | 0 | } |
1256 | | |
1257 | 15.3k | ret = xmlSecDSigReferenceCtxInitialize(dsigRefCtx, dsigCtx, origin); |
1258 | 15.3k | if(ret < 0) { |
1259 | 0 | xmlSecInternalError("xmlSecDSigReferenceCtxInitialize", NULL); |
1260 | 0 | xmlSecDSigReferenceCtxDestroy(dsigRefCtx); |
1261 | 0 | return(NULL); |
1262 | 0 | } |
1263 | 15.3k | return(dsigRefCtx); |
1264 | 15.3k | } |
1265 | | |
1266 | | /** |
1267 | | * @brief Destroys a dsig:Reference processing context. |
1268 | | * @details Destroy context object created with #xmlSecDSigReferenceCtxCreate function. |
1269 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1270 | | */ |
1271 | | void |
1272 | 15.3k | xmlSecDSigReferenceCtxDestroy(xmlSecDSigReferenceCtxPtr dsigRefCtx) { |
1273 | 15.3k | xmlSecAssert(dsigRefCtx != NULL); |
1274 | | |
1275 | 15.3k | xmlSecDSigReferenceCtxFinalize(dsigRefCtx); |
1276 | 15.3k | xmlFree(dsigRefCtx); |
1277 | 15.3k | } |
1278 | | |
1279 | | /** |
1280 | | * @brief Initializes a dsig:Reference processing context. |
1281 | | * @details Initializes new <dsig:Reference/> element processing context. Caller is responsible |
1282 | | * for cleaning up the returned context by calling #xmlSecDSigReferenceCtxFinalize |
1283 | | * function. |
1284 | | * |
1285 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1286 | | * @param dsigCtx the pointer to parent <dsig:Signature/> node processing context. |
1287 | | * @param origin the reference origin (<dsig:SignedInfo/> or <dsig:Manifest/> node). |
1288 | | * @return 0 on success or a negative value otherwise. |
1289 | | */ |
1290 | | int |
1291 | | xmlSecDSigReferenceCtxInitialize(xmlSecDSigReferenceCtxPtr dsigRefCtx, xmlSecDSigCtxPtr dsigCtx, |
1292 | 15.3k | xmlSecDSigReferenceOrigin origin) { |
1293 | 15.3k | int ret; |
1294 | | |
1295 | 15.3k | xmlSecAssert2(dsigCtx != NULL, -1); |
1296 | 15.3k | xmlSecAssert2(dsigRefCtx != NULL, -1); |
1297 | | |
1298 | 15.3k | memset(dsigRefCtx, 0, sizeof(xmlSecDSigReferenceCtx)); |
1299 | | |
1300 | 15.3k | dsigRefCtx->dsigCtx = dsigCtx; |
1301 | 15.3k | dsigRefCtx->origin = origin; |
1302 | | |
1303 | | /* initialize the reference transform context */ |
1304 | 15.3k | ret = xmlSecTransformCtxInitialize(&(dsigRefCtx->transformCtx)); |
1305 | 15.3k | if(ret < 0) { |
1306 | 0 | xmlSecInternalError("xmlSecTransformCtxInitialize", NULL); |
1307 | 0 | return(-1); |
1308 | 0 | } |
1309 | | |
1310 | | /* inherit the application's transform context settings */ |
1311 | 15.3k | ret = xmlSecTransformCtxCopyUserPref(&(dsigRefCtx->transformCtx), &(dsigCtx->transformCtx)); |
1312 | 15.3k | if(ret < 0) { |
1313 | 0 | xmlSecInternalError("xmlSecTransformCtxCopyUserPref", NULL); |
1314 | 0 | return(-1); |
1315 | 0 | } |
1316 | | |
1317 | | /* re-apply the per-reference settings */ |
1318 | 15.3k | xmlSecPtrListEmpty(&(dsigRefCtx->transformCtx.enabledTransforms)); |
1319 | 15.3k | if(dsigCtx->enabledReferenceTransforms != NULL) { |
1320 | 0 | ret = xmlSecPtrListCopy(&(dsigRefCtx->transformCtx.enabledTransforms), dsigCtx->enabledReferenceTransforms); |
1321 | 0 | if(ret < 0) { |
1322 | 0 | xmlSecInternalError("xmlSecPtrListCopy", NULL); |
1323 | 0 | return(-1); |
1324 | 0 | } |
1325 | 0 | } |
1326 | 15.3k | dsigRefCtx->transformCtx.preExecCallback = dsigCtx->referencePreExecuteCallback; |
1327 | 15.3k | dsigRefCtx->transformCtx.enabledUris = dsigCtx->enabledReferenceUris; |
1328 | 15.3k | dsigRefCtx->transformCtx.userData = dsigCtx->userData; |
1329 | | |
1330 | 15.3k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_USE_VISA3D_HACK) != 0) { |
1331 | 0 | dsigRefCtx->transformCtx.flags |= XMLSEC_TRANSFORMCTX_FLAGS_USE_VISA3D_HACK; |
1332 | 0 | } |
1333 | 15.3k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_USE_ASN1_SIGNATURE_VALUES) != 0) { |
1334 | 0 | dsigRefCtx->transformCtx.flags |= XMLSEC_TRANSFORMCTX_FLAGS_SUPPORT_ASN1_SIGNATURE_VALUES; |
1335 | 0 | } |
1336 | 15.3k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_RELATIONSHIP_LEGACY) != 0) { |
1337 | 0 | dsigRefCtx->transformCtx.flags |= XMLSEC_TRANSFORMCTX_FLAGS_RELATIONSHIP_LEGACY; |
1338 | 0 | } |
1339 | 15.3k | if((dsigCtx->flags & XMLSEC_DSIG_FLAGS_ALLOW_EMPTY_INPUT) != 0) { |
1340 | 0 | dsigRefCtx->transformCtx.flags |= XMLSEC_TRANSFORMCTX_FLAGS_ALLOW_EMPTY_INPUT; |
1341 | 0 | } |
1342 | 15.3k | return(0); |
1343 | 15.3k | } |
1344 | | |
1345 | | /** |
1346 | | * @brief Cleans up a dsig:Reference processing context. |
1347 | | * @details Cleans up context object initialized with #xmlSecDSigReferenceCtxInitialize function. |
1348 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1349 | | */ |
1350 | | void |
1351 | 15.3k | xmlSecDSigReferenceCtxFinalize(xmlSecDSigReferenceCtxPtr dsigRefCtx) { |
1352 | 15.3k | xmlSecAssert(dsigRefCtx != NULL); |
1353 | | |
1354 | 15.3k | xmlSecTransformCtxFinalize(&(dsigRefCtx->transformCtx)); |
1355 | 15.3k | if(dsigRefCtx->id != NULL) { |
1356 | 12.1k | xmlFree(dsigRefCtx->id); |
1357 | 12.1k | } |
1358 | 15.3k | if(dsigRefCtx->uri != NULL) { |
1359 | 1.17k | xmlFree(dsigRefCtx->uri); |
1360 | 1.17k | } |
1361 | 15.3k | if(dsigRefCtx->type != NULL) { |
1362 | 219 | xmlFree(dsigRefCtx->type); |
1363 | 219 | } |
1364 | 15.3k | memset(dsigRefCtx, 0, sizeof(xmlSecDSigReferenceCtx)); |
1365 | 15.3k | } |
1366 | | |
1367 | | /** |
1368 | | * @brief Gets the pre-digest buffer for the Reference element. |
1369 | | * @details Gets the results of <dsig:Reference/> node processing just before digesting |
1370 | | * (valid only if #XMLSEC_DSIG_FLAGS_STORE_SIGNEDINFO_REFERENCES or |
1371 | | * #XMLSEC_DSIG_FLAGS_STORE_MANIFEST_REFERENCES flags of signature context |
1372 | | * are set). |
1373 | | * |
1374 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1375 | | * @return pointer to the buffer or NULL if an error occurs. |
1376 | | */ |
1377 | | xmlSecBufferPtr |
1378 | 0 | xmlSecDSigReferenceCtxGetPreDigestBuffer(xmlSecDSigReferenceCtxPtr dsigRefCtx) { |
1379 | 0 | xmlSecAssert2(dsigRefCtx != NULL, NULL); |
1380 | | |
1381 | 0 | return((dsigRefCtx->preDigestMemBufMethod != NULL) ? |
1382 | 0 | xmlSecTransformMemBufGetBuffer(dsigRefCtx->preDigestMemBufMethod) : NULL); |
1383 | 0 | } |
1384 | | |
1385 | | /** |
1386 | | * @brief Processes a dsig:Reference element. |
1387 | | * @details The Reference Element (http://www.w3.org/TR/xmldsig-core/#sec-Reference) |
1388 | | * |
1389 | | * Reference is an element that may occur one or more times. It specifies |
1390 | | * a digest algorithm and digest value, and optionally an identifier of the |
1391 | | * object being signed, the type of the object, and/or a list of transforms |
1392 | | * to be applied prior to digesting. The identification (URI) and transforms |
1393 | | * describe how the digested content (i.e., the input to the digest method) |
1394 | | * was created. The Type attribute facilitates the processing of referenced |
1395 | | * data. For example, while this specification makes no requirements over |
1396 | | * external data, an application may wish to signal that the referent is a |
1397 | | * Manifest. An optional ID attribute permits a Reference to be referenced |
1398 | | * from elsewhere. |
1399 | | * |
1400 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1401 | | * @param node the pointer to <dsig:Reference/> node. |
1402 | | * @return 0 on success or a negative value otherwise. |
1403 | | */ |
1404 | | int |
1405 | 15.3k | xmlSecDSigReferenceCtxProcessNode(xmlSecDSigReferenceCtxPtr dsigRefCtx, xmlNodePtr node) { |
1406 | 15.3k | xmlSecTransformCtxPtr transformCtx; |
1407 | 15.3k | xmlNodePtr digestValueNode = NULL; |
1408 | 15.3k | xmlNodePtr cur; |
1409 | 15.3k | int ret; |
1410 | | |
1411 | 15.3k | xmlSecAssert2(dsigRefCtx != NULL, -1); |
1412 | 15.3k | xmlSecAssert2(dsigRefCtx->dsigCtx != NULL, -1); |
1413 | 15.3k | xmlSecAssert2(dsigRefCtx->digestMethod == NULL, -1); |
1414 | 15.3k | xmlSecAssert2(dsigRefCtx->preDigestMemBufMethod == NULL, -1); |
1415 | 15.3k | xmlSecAssert2(node != NULL, -1); |
1416 | 15.3k | xmlSecAssert2(node->doc != NULL, -1); |
1417 | | |
1418 | 15.3k | transformCtx = &(dsigRefCtx->transformCtx); |
1419 | | |
1420 | | /* read attributes first. All three are optional, but xmlGetProp() |
1421 | | * returns NULL both when the attribute is absent and when it cannot |
1422 | | * allocate the return value, so each is checked for presence first to |
1423 | | * report the correct error on allocation failure */ |
1424 | 15.3k | if(xmlHasProp(node, xmlSecAttrURI)) { |
1425 | 1.17k | dsigRefCtx->uri = xmlGetProp(node, xmlSecAttrURI); |
1426 | 1.17k | if(dsigRefCtx->uri == NULL) { |
1427 | 0 | xmlSecXmlError("xmlGetProp", xmlSecNodeGetName(node)); |
1428 | 0 | return(-1); |
1429 | 0 | } |
1430 | 1.17k | } |
1431 | 15.3k | if(xmlHasProp(node, xmlSecAttrId)) { |
1432 | 12.1k | dsigRefCtx->id = xmlGetProp(node, xmlSecAttrId); |
1433 | 12.1k | if(dsigRefCtx->id == NULL) { |
1434 | 0 | xmlSecXmlError("xmlGetProp", xmlSecNodeGetName(node)); |
1435 | 0 | return(-1); |
1436 | 0 | } |
1437 | 12.1k | } |
1438 | 15.3k | if(xmlHasProp(node, xmlSecAttrType)) { |
1439 | 219 | dsigRefCtx->type = xmlGetProp(node, xmlSecAttrType); |
1440 | 219 | if(dsigRefCtx->type == NULL) { |
1441 | 0 | xmlSecXmlError("xmlGetProp", xmlSecNodeGetName(node)); |
1442 | 0 | return(-1); |
1443 | 0 | } |
1444 | 219 | } |
1445 | | |
1446 | | /* set start URI (and check that it is enabled!) */ |
1447 | 15.3k | ret = xmlSecTransformCtxSetUri(transformCtx, dsigRefCtx->uri, node); |
1448 | 15.3k | if(ret < 0) { |
1449 | 58 | xmlSecInternalError2("xmlSecTransformCtxSetUri", NULL, |
1450 | 58 | "uri=%s", xmlSecErrorsSafeString(dsigRefCtx->uri)); |
1451 | 58 | return(-1); |
1452 | 58 | } |
1453 | | |
1454 | | /* first is optional Transforms node */ |
1455 | 15.3k | cur = xmlSecGetNextElementNode(node->children); |
1456 | 15.3k | if((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeTransforms, xmlSecDSigNs))) { |
1457 | 10.0k | ret = xmlSecTransformCtxNodesListRead(transformCtx, |
1458 | 10.0k | cur, xmlSecTransformUsageDSigTransform); |
1459 | 10.0k | if(ret < 0) { |
1460 | 2.88k | xmlSecInternalError2("xmlSecTransformCtxNodesListRead", NULL, |
1461 | 2.88k | "node=%s", xmlSecErrorsSafeString(xmlSecNodeGetName(cur))); |
1462 | 2.88k | return(-1); |
1463 | 2.88k | } |
1464 | | |
1465 | 7.20k | cur = xmlSecGetNextElementNode(cur->next); |
1466 | 7.20k | } |
1467 | | |
1468 | | /* insert membuf if requested */ |
1469 | 12.4k | if(((dsigRefCtx->origin == xmlSecDSigReferenceOriginSignedInfo) && |
1470 | 0 | ((dsigRefCtx->dsigCtx->flags & XMLSEC_DSIG_FLAGS_STORE_SIGNEDINFO_REFERENCES) != 0)) || |
1471 | 12.4k | ((dsigRefCtx->origin == xmlSecDSigReferenceOriginManifest) && |
1472 | 12.4k | ((dsigRefCtx->dsigCtx->flags & XMLSEC_DSIG_FLAGS_STORE_MANIFEST_REFERENCES) != 0))) { |
1473 | | |
1474 | | /* preDigestMemBufMethod is known to be NULL: checked at the function entry */ |
1475 | 0 | dsigRefCtx->preDigestMemBufMethod = xmlSecTransformCtxCreateAndAppend( |
1476 | 0 | transformCtx, |
1477 | 0 | xmlSecTransformMemBufId); |
1478 | 0 | if(dsigRefCtx->preDigestMemBufMethod == NULL) { |
1479 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend(xmlSecTransformMemBufId)", NULL); |
1480 | 0 | return(-1); |
1481 | 0 | } |
1482 | 0 | } |
1483 | | |
1484 | | /* next node is required DigestMethod. */ |
1485 | 12.4k | if((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeDigestMethod, xmlSecDSigNs))) { |
1486 | 12.3k | dsigRefCtx->digestMethod = xmlSecTransformCtxNodeRead(&(dsigRefCtx->transformCtx), |
1487 | 12.3k | cur, xmlSecTransformUsageDigestMethod); |
1488 | 12.3k | if(dsigRefCtx->digestMethod == NULL) { |
1489 | 88 | xmlSecInternalError("xmlSecTransformCtxNodeRead", |
1490 | 88 | xmlSecNodeGetName(cur)); |
1491 | 88 | return(-1); |
1492 | 88 | } |
1493 | | |
1494 | 12.2k | cur = xmlSecGetNextElementNode(cur->next); |
1495 | 12.2k | } else if(dsigRefCtx->dsigCtx->defDigestMethodId != xmlSecTransformIdUnknown) { |
1496 | | /* the dsig spec does require the DigestMethod node |
1497 | | * to be present but in some cases the application might decide to |
1498 | | * minimize traffic */ |
1499 | 0 | dsigRefCtx->digestMethod = xmlSecTransformCtxCreateAndAppend(&(dsigRefCtx->transformCtx), dsigRefCtx->dsigCtx->defDigestMethodId); |
1500 | 0 | if(dsigRefCtx->digestMethod == NULL) { |
1501 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend", NULL); |
1502 | 0 | return(-1); |
1503 | 0 | } |
1504 | 44 | } else { |
1505 | | /* if digest method is not specified in the template and not specified in |
1506 | | * the dsig context then it's an error. |
1507 | | */ |
1508 | 44 | xmlSecInvalidNodeError(cur, xmlSecNodeDigestMethod, NULL); |
1509 | 44 | return(-1); |
1510 | 44 | } |
1511 | 12.2k | dsigRefCtx->digestMethod->operation = dsigRefCtx->dsigCtx->operation; |
1512 | | |
1513 | | /* last node is required DigestValue */ |
1514 | 12.2k | if((cur != NULL) && (xmlSecCheckNodeName(cur, xmlSecNodeDigestValue, xmlSecDSigNs))) { |
1515 | 12.2k | digestValueNode = cur; |
1516 | 12.2k | cur = xmlSecGetNextElementNode(cur->next); |
1517 | 12.2k | } else { |
1518 | 5 | xmlSecInvalidNodeError(cur, xmlSecNodeDigestValue, NULL); |
1519 | 5 | return(-1); |
1520 | 5 | } |
1521 | | |
1522 | | /* if we have something else then it's an error */ |
1523 | 12.2k | if(cur != NULL) { |
1524 | 5 | xmlSecUnexpectedNodeError(cur, NULL); |
1525 | 5 | return(-1); |
1526 | 5 | } |
1527 | | |
1528 | | /* if we need to write result to xml node then we need base64 encode result */ |
1529 | 12.2k | if(dsigRefCtx->dsigCtx->operation == xmlSecTransformOperationSign) { |
1530 | 0 | xmlSecTransformPtr base64Encode; |
1531 | | |
1532 | | /* we need to add base64 encode transform */ |
1533 | 0 | base64Encode = xmlSecTransformCtxCreateAndAppend(transformCtx, xmlSecTransformBase64Id); |
1534 | 0 | if(base64Encode == NULL) { |
1535 | 0 | xmlSecInternalError("xmlSecTransformCtxCreateAndAppend", NULL); |
1536 | 0 | return(-1); |
1537 | 0 | } |
1538 | 0 | base64Encode->operation = xmlSecTransformOperationEncode; |
1539 | 0 | } |
1540 | | |
1541 | | /* finally get transform results */ |
1542 | 12.2k | ret = xmlSecTransformCtxExecute(transformCtx, node->doc); |
1543 | 12.2k | if(ret < 0) { |
1544 | 5.60k | xmlSecInternalError("xmlSecTransformCtxExecute", NULL); |
1545 | 5.60k | return(-1); |
1546 | 5.60k | } |
1547 | 6.66k | dsigRefCtx->result = transformCtx->result; |
1548 | | |
1549 | 6.66k | if(dsigRefCtx->dsigCtx->operation == xmlSecTransformOperationSign) { |
1550 | 0 | xmlSecByte* outBuf; |
1551 | 0 | xmlSecSize outSize; |
1552 | 0 | int outLen; |
1553 | |
|
1554 | 0 | if((dsigRefCtx->result == NULL) || (xmlSecBufferGetData(dsigRefCtx->result) == NULL)) { |
1555 | 0 | xmlSecOtherError(XMLSEC_ERRORS_R_INVALID_RESULT, NULL, NULL); |
1556 | 0 | return(-1); |
1557 | 0 | } |
1558 | | |
1559 | | /* write the digest result to xml */ |
1560 | 0 | outBuf = xmlSecBufferGetData(dsigRefCtx->result); |
1561 | 0 | outSize = xmlSecBufferGetSize(dsigRefCtx->result); |
1562 | 0 | XMLSEC_SAFE_CAST_SIZE_TO_INT(outSize, outLen, return(-1), NULL); |
1563 | 0 | #if LIBXML_VERSION >= 21300 |
1564 | 0 | if(xmlNodeSetContentLen(digestValueNode, outBuf, outLen) < 0) { |
1565 | 0 | xmlSecXmlError("xmlNodeSetContentLen", xmlSecNodeGetName(digestValueNode)); |
1566 | 0 | return(-1); |
1567 | 0 | } |
1568 | | #else /* LIBXML_VERSION >= 21300 */ |
1569 | | /* libxml2 < 2.13.0: xmlNodeSetContentLen() returns void and cannot report errors */ |
1570 | | xmlNodeSetContentLen(digestValueNode, outBuf, outLen); |
1571 | | #endif /* LIBXML_VERSION >= 21300 */ |
1572 | | |
1573 | | /* set success status and we are done */ |
1574 | 0 | dsigRefCtx->status = xmlSecDSigStatusSucceeded; |
1575 | 6.66k | } else { |
1576 | | /* verify DigestValue node content */ |
1577 | 6.66k | ret = xmlSecTransformVerifyNodeContent(dsigRefCtx->digestMethod, |
1578 | 6.66k | digestValueNode, transformCtx); |
1579 | 6.66k | if(ret < 0) { |
1580 | 1.03k | xmlSecInternalError("xmlSecTransformVerifyNodeContent", NULL); |
1581 | 1.03k | return(-1); |
1582 | 1.03k | } |
1583 | | |
1584 | | /* set status and we are done */ |
1585 | 5.63k | if(dsigRefCtx->digestMethod->status == xmlSecTransformStatusOk) { |
1586 | 126 | dsigRefCtx->status = xmlSecDSigStatusSucceeded; |
1587 | 5.51k | } else { |
1588 | 5.51k | dsigRefCtx->status = xmlSecDSigStatusInvalid; |
1589 | 5.51k | } |
1590 | 5.63k | } |
1591 | | |
1592 | 5.63k | return(0); |
1593 | 6.66k | } |
1594 | | |
1595 | | /** |
1596 | | * @brief Prints debug information about the reference context. |
1597 | | * @details Prints debug information about @p dsigRefCtx to @p output. |
1598 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1599 | | * @param output the pointer to output FILE. |
1600 | | */ |
1601 | | void |
1602 | 0 | xmlSecDSigReferenceCtxDebugDump(xmlSecDSigReferenceCtxPtr dsigRefCtx, FILE* output) { |
1603 | 0 | xmlSecAssert(dsigRefCtx != NULL); |
1604 | 0 | xmlSecAssert(dsigRefCtx->dsigCtx != NULL); |
1605 | 0 | xmlSecAssert(output != NULL); |
1606 | | |
1607 | 0 | if(dsigRefCtx->dsigCtx->operation == xmlSecTransformOperationSign) { |
1608 | 0 | fprintf(output, "= REFERENCE CALCULATION CONTEXT\n"); |
1609 | 0 | } else { |
1610 | 0 | fprintf(output, "= REFERENCE VERIFICATION CONTEXT\n"); |
1611 | 0 | } |
1612 | 0 | fprintf(output, "== Status: %s\n", xmlSecDSigCtxGetStatusString(dsigRefCtx->status)); |
1613 | |
|
1614 | 0 | if(dsigRefCtx->id != NULL) { |
1615 | 0 | fprintf(output, "== Id: \"%s\"\n", (const char*)dsigRefCtx->id); |
1616 | 0 | } |
1617 | 0 | if(dsigRefCtx->uri != NULL) { |
1618 | 0 | fprintf(output, "== URI: \"%s\"\n", (const char*)dsigRefCtx->uri); |
1619 | 0 | } |
1620 | 0 | if(dsigRefCtx->type != NULL) { |
1621 | 0 | fprintf(output, "== Type: \"%s\"\n", (const char*)dsigRefCtx->type); |
1622 | 0 | } |
1623 | |
|
1624 | 0 | fprintf(output, "== Reference Transform Ctx:\n"); |
1625 | 0 | xmlSecTransformCtxDebugDump(&(dsigRefCtx->transformCtx), output); |
1626 | |
|
1627 | 0 | if(dsigRefCtx->digestMethod != NULL) { |
1628 | 0 | fprintf(output, "== Digest Method:\n"); |
1629 | 0 | xmlSecTransformDebugDump(dsigRefCtx->digestMethod, output); |
1630 | 0 | } |
1631 | |
|
1632 | 0 | if((dsigRefCtx->result != NULL) && |
1633 | 0 | (xmlSecBufferGetData(dsigRefCtx->result) != NULL)) { |
1634 | |
|
1635 | 0 | fprintf(output, "== Result - start buffer:\n"); |
1636 | 0 | xmlSecBufferDebugHexDump(dsigRefCtx->result, output); |
1637 | 0 | fprintf(output, "\n== Result - end buffer\n"); |
1638 | 0 | } |
1639 | |
|
1640 | 0 | if((xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx) != NULL) && |
1641 | 0 | (xmlSecBufferGetData(xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx)) != NULL)) { |
1642 | |
|
1643 | 0 | fprintf(output, "== PreDigest data - start buffer:\n"); |
1644 | 0 | xmlSecBufferDebugHexDump(xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx), output); |
1645 | 0 | fprintf(output, "\n== PreDigest data - end buffer\n"); |
1646 | 0 | } |
1647 | 0 | } |
1648 | | |
1649 | | /** |
1650 | | * @brief Prints debug information about the reference context in XML format. |
1651 | | * @details Prints debug information about @p dsigRefCtx to @p output in XML format. |
1652 | | * @param dsigRefCtx the pointer to <dsig:Reference/> element processing context. |
1653 | | * @param output the pointer to output FILE. |
1654 | | */ |
1655 | | void |
1656 | 0 | xmlSecDSigReferenceCtxDebugXmlDump(xmlSecDSigReferenceCtxPtr dsigRefCtx, FILE* output) { |
1657 | 0 | xmlSecAssert(dsigRefCtx != NULL); |
1658 | 0 | xmlSecAssert(dsigRefCtx->dsigCtx != NULL); |
1659 | 0 | xmlSecAssert(output != NULL); |
1660 | | |
1661 | 0 | if(dsigRefCtx->dsigCtx->operation == xmlSecTransformOperationSign) { |
1662 | 0 | fprintf(output, "<ReferenceCalculationContext"); |
1663 | 0 | } else { |
1664 | 0 | fprintf(output, "<ReferenceVerificationContext"); |
1665 | 0 | } |
1666 | 0 | fprintf(output, " status=\"%s\"", xmlSecDSigCtxGetStatusString(dsigRefCtx->status)); |
1667 | 0 | fprintf(output, ">\n"); |
1668 | |
|
1669 | 0 | fprintf(output, "<Id>"); |
1670 | 0 | xmlSecPrintXmlString(output, dsigRefCtx->id); |
1671 | 0 | fprintf(output, "</Id>\n"); |
1672 | |
|
1673 | 0 | fprintf(output, "<URI>"); |
1674 | 0 | xmlSecPrintXmlString(output, dsigRefCtx->uri); |
1675 | 0 | fprintf(output, "</URI>\n"); |
1676 | |
|
1677 | 0 | fprintf(output, "<Type>"); |
1678 | 0 | xmlSecPrintXmlString(output, dsigRefCtx->type); |
1679 | 0 | fprintf(output, "</Type>\n"); |
1680 | |
|
1681 | 0 | fprintf(output, "<ReferenceTransformCtx>\n"); |
1682 | 0 | xmlSecTransformCtxDebugXmlDump(&(dsigRefCtx->transformCtx), output); |
1683 | 0 | fprintf(output, "</ReferenceTransformCtx>\n"); |
1684 | |
|
1685 | 0 | if(dsigRefCtx->digestMethod != NULL) { |
1686 | 0 | fprintf(output, "<DigestMethod>\n"); |
1687 | 0 | xmlSecTransformDebugXmlDump(dsigRefCtx->digestMethod, output); |
1688 | 0 | fprintf(output, "</DigestMethod>\n"); |
1689 | 0 | } |
1690 | |
|
1691 | 0 | if((dsigRefCtx->result != NULL) && (xmlSecBufferGetData(dsigRefCtx->result) != NULL)) { |
1692 | 0 | fprintf(output, "<Result>"); |
1693 | 0 | xmlSecBufferDebugHexDump(dsigRefCtx->result, output); |
1694 | 0 | fprintf(output, "</Result>\n"); |
1695 | 0 | } |
1696 | |
|
1697 | 0 | if((xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx) != NULL) && |
1698 | 0 | (xmlSecBufferGetData(xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx)) != NULL)) { |
1699 | |
|
1700 | 0 | fprintf(output, "<PreDigestData>"); |
1701 | 0 | xmlSecBufferDebugHexDump(xmlSecDSigReferenceCtxGetPreDigestBuffer(dsigRefCtx), output); |
1702 | 0 | fprintf(output, "</PreDigestData>\n"); |
1703 | 0 | } |
1704 | 0 | if(dsigRefCtx->dsigCtx->operation == xmlSecTransformOperationSign) { |
1705 | 0 | fprintf(output, "</ReferenceCalculationContext>\n"); |
1706 | 0 | } else { |
1707 | 0 | fprintf(output, "</ReferenceVerificationContext>\n"); |
1708 | 0 | } |
1709 | 0 | } |
1710 | | |
1711 | | |
1712 | | /****************************************************************************** |
1713 | | * |
1714 | | * xmlSecDSigReferenceCtxListKlass |
1715 | | * |
1716 | | *****************************************************************************/ |
1717 | | /* the list calls these through the xmlSecPtr*ItemMethod types, see xmlSecDSigReferenceCtxListDestroyItem() below */ |
1718 | | static void |
1719 | 15.3k | xmlSecDSigReferenceCtxListDestroyItem(xmlSecPtr ptr) { |
1720 | 15.3k | xmlSecDSigReferenceCtxDestroy((xmlSecDSigReferenceCtxPtr)ptr); |
1721 | 15.3k | } |
1722 | | |
1723 | | static void |
1724 | 0 | xmlSecDSigReferenceCtxListDebugDumpItem(xmlSecPtr ptr, FILE* output) { |
1725 | 0 | xmlSecDSigReferenceCtxDebugDump((xmlSecDSigReferenceCtxPtr)ptr, output); |
1726 | 0 | } |
1727 | | |
1728 | | static void |
1729 | 0 | xmlSecDSigReferenceCtxListDebugXmlDumpItem(xmlSecPtr ptr, FILE* output) { |
1730 | 0 | xmlSecDSigReferenceCtxDebugXmlDump((xmlSecDSigReferenceCtxPtr)ptr, output); |
1731 | 0 | } |
1732 | | |
1733 | | static xmlSecPtrListKlass xmlSecDSigReferenceCtxListKlass = { |
1734 | | BAD_CAST "dsig-reference-list", |
1735 | | NULL, /* xmlSecPtrDuplicateItemMethod duplicateItem; */ |
1736 | | xmlSecDSigReferenceCtxListDestroyItem, /* xmlSecPtrDestroyItemMethod destroyItem; */ |
1737 | | xmlSecDSigReferenceCtxListDebugDumpItem, /* xmlSecPtrDebugDumpItemMethod debugDumpItem; */ |
1738 | | xmlSecDSigReferenceCtxListDebugXmlDumpItem, /* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */ |
1739 | | }; |
1740 | | |
1741 | | /** |
1742 | | * @brief Gets the dsig:Reference processing contexts list klass. |
1743 | | * @details The <dsig:Reference/> element processing contexts list klass. |
1744 | | * |
1745 | | * @return <dsig:Reference/> element processing context list klass. |
1746 | | */ |
1747 | | xmlSecPtrListId |
1748 | 21.8k | xmlSecDSigReferenceCtxListGetKlass(void) { |
1749 | 21.8k | return(&xmlSecDSigReferenceCtxListKlass); |
1750 | 21.8k | } |
1751 | | |
1752 | | #else /* XMLSEC_NO_XMLDSIG */ |
1753 | | |
1754 | | /* ISO C forbids an empty translation unit */ |
1755 | | typedef int make_iso_compilers_happy; |
1756 | | |
1757 | | #endif /* XMLSEC_NO_XMLDSIG */ |