Line data Source code
1 : /**
2 : * XML Security Library (http://www.aleksey.com/xmlsec).
3 : *
4 : * Keys.
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/list.h>
21 : #include <xmlsec/keys.h>
22 : #include <xmlsec/keysmngr.h>
23 : #include <xmlsec/transforms.h>
24 : #include <xmlsec/keyinfo.h>
25 : #include <xmlsec/errors.h>
26 :
27 : /**************************************************************************
28 : *
29 : * xmlSecKeyUseWith
30 : *
31 : *************************************************************************/
32 : /**
33 : * xmlSecKeyUseWithInitialize:
34 : * @keyUseWith: the pointer to information about key application/user.
35 : *
36 : * Initializes @keyUseWith object.
37 : *
38 : * Returns: 0 on success or a negative value if an error occurs.
39 : */
40 : int
41 0 : xmlSecKeyUseWithInitialize(xmlSecKeyUseWithPtr keyUseWith) {
42 0 : xmlSecAssert2(keyUseWith != NULL, -1);
43 :
44 0 : memset(keyUseWith, 0, sizeof(xmlSecKeyUseWith));
45 0 : return(0);
46 : }
47 :
48 : /**
49 : * xmlSecKeyUseWithFinalize:
50 : * @keyUseWith: the pointer to information about key application/user.
51 : *
52 : * Finalizes @keyUseWith object.
53 : */
54 : void
55 0 : xmlSecKeyUseWithFinalize(xmlSecKeyUseWithPtr keyUseWith) {
56 0 : xmlSecAssert(keyUseWith != NULL);
57 :
58 0 : xmlSecKeyUseWithReset(keyUseWith);
59 0 : memset(keyUseWith, 0, sizeof(xmlSecKeyUseWith));
60 : }
61 :
62 : /**
63 : * xmlSecKeyUseWithReset:
64 : * @keyUseWith: the pointer to information about key application/user.
65 : *
66 : * Resets the @keyUseWith to its state after initialization.
67 : */
68 : void
69 0 : xmlSecKeyUseWithReset(xmlSecKeyUseWithPtr keyUseWith) {
70 0 : xmlSecAssert(keyUseWith != NULL);
71 :
72 0 : xmlSecKeyUseWithSet(keyUseWith, NULL, NULL);
73 : }
74 :
75 : /**
76 : * xmlSecKeyUseWithCopy:
77 : * @dst: the pointer to destination object.
78 : * @src: the pointer to source object.
79 : *
80 : * Copies information from @dst to @src.
81 : *
82 : * Returns: 0 on success or a negative value if an error occurs.
83 : */
84 : int
85 0 : xmlSecKeyUseWithCopy(xmlSecKeyUseWithPtr dst, xmlSecKeyUseWithPtr src) {
86 0 : xmlSecAssert2(dst != NULL, -1);
87 0 : xmlSecAssert2(src != NULL, -1);
88 :
89 0 : return(xmlSecKeyUseWithSet(dst, src->application, src->identifier));
90 : }
91 :
92 : /**
93 : * xmlSecKeyUseWithCreate:
94 : * @application: the application value.
95 : * @identifier: the identifier value.
96 : *
97 : * Creates new xmlSecKeyUseWith object. The caller is responsible for destroying
98 : * returned object with @xmlSecKeyUseWithDestroy function.
99 : *
100 : * Returns: pointer to newly created object or NULL if an error occurs.
101 : */
102 : xmlSecKeyUseWithPtr
103 0 : xmlSecKeyUseWithCreate(const xmlChar* application, const xmlChar* identifier) {
104 : xmlSecKeyUseWithPtr keyUseWith;
105 : int ret;
106 :
107 : /* Allocate a new xmlSecKeyUseWith and fill the fields. */
108 0 : keyUseWith = (xmlSecKeyUseWithPtr)xmlMalloc(sizeof(xmlSecKeyUseWith));
109 0 : if(keyUseWith == NULL) {
110 0 : xmlSecError(XMLSEC_ERRORS_HERE,
111 : NULL,
112 : NULL,
113 : XMLSEC_ERRORS_R_MALLOC_FAILED,
114 : "sizeof(xmlSecKeyUseWith)=%d",
115 : sizeof(xmlSecKeyUseWith));
116 0 : return(NULL);
117 : }
118 0 : memset(keyUseWith, 0, sizeof(xmlSecKeyUseWith));
119 :
120 0 : ret = xmlSecKeyUseWithInitialize(keyUseWith);
121 0 : if(ret < 0) {
122 0 : xmlSecError(XMLSEC_ERRORS_HERE,
123 : NULL,
124 : "xmlSecKeyUseWithInitialize",
125 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
126 : XMLSEC_ERRORS_NO_MESSAGE);
127 0 : xmlSecKeyUseWithDestroy(keyUseWith);
128 0 : return(NULL);
129 : }
130 :
131 0 : ret = xmlSecKeyUseWithSet(keyUseWith, application, identifier);
132 0 : if(ret < 0) {
133 0 : xmlSecError(XMLSEC_ERRORS_HERE,
134 : NULL,
135 : "xmlSecKeyUseWithSet",
136 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
137 : XMLSEC_ERRORS_NO_MESSAGE);
138 0 : xmlSecKeyUseWithDestroy(keyUseWith);
139 0 : return(NULL);
140 : }
141 :
142 0 : return(keyUseWith);
143 : }
144 :
145 : /**
146 : * xmlSecKeyUseWithDuplicate:
147 : * @keyUseWith: the pointer to information about key application/user.
148 : *
149 : * Duplicates @keyUseWith object. The caller is responsible for destroying
150 : * returned object with @xmlSecKeyUseWithDestroy function.
151 : *
152 : * Returns: pointer to newly created object or NULL if an error occurs.
153 : */
154 : xmlSecKeyUseWithPtr
155 0 : xmlSecKeyUseWithDuplicate(xmlSecKeyUseWithPtr keyUseWith) {
156 : int ret;
157 :
158 : xmlSecKeyUseWithPtr newKeyUseWith;
159 :
160 0 : xmlSecAssert2(keyUseWith != NULL, NULL);
161 :
162 0 : newKeyUseWith = xmlSecKeyUseWithCreate(NULL, NULL);
163 0 : if(newKeyUseWith == NULL) {
164 0 : xmlSecError(XMLSEC_ERRORS_HERE,
165 : NULL,
166 : "xmlSecKeyUseWithCreate",
167 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
168 : XMLSEC_ERRORS_NO_MESSAGE);
169 0 : return(NULL);
170 : }
171 :
172 0 : ret = xmlSecKeyUseWithCopy(newKeyUseWith, keyUseWith);
173 0 : if(ret < 0) {
174 0 : xmlSecError(XMLSEC_ERRORS_HERE,
175 : NULL,
176 : "xmlSecKeyUseWithCopy",
177 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
178 : XMLSEC_ERRORS_NO_MESSAGE);
179 0 : xmlSecKeyUseWithDestroy(keyUseWith);
180 0 : return(NULL);
181 : }
182 :
183 0 : return(newKeyUseWith);
184 : }
185 :
186 : /**
187 : * xmlSecKeyUseWithDestroy:
188 : * @keyUseWith: the pointer to information about key application/user.
189 : *
190 : * Destroys @keyUseWith created with @xmlSecKeyUseWithCreate or @xmlSecKeyUseWithDuplicate
191 : * functions.
192 : */
193 : void
194 0 : xmlSecKeyUseWithDestroy(xmlSecKeyUseWithPtr keyUseWith) {
195 0 : xmlSecAssert(keyUseWith != NULL);
196 :
197 0 : xmlSecKeyUseWithFinalize(keyUseWith);
198 0 : xmlFree(keyUseWith);
199 : }
200 :
201 : /**
202 : * xmlSecKeyUseWithSet:
203 : * @keyUseWith: the pointer to information about key application/user.
204 : * @application: the new application value.
205 : * @identifier: the new identifier value.
206 : *
207 : * Sets @application and @identifier in the @keyUseWith.
208 : *
209 : * Returns: 0 on success or a negative value if an error occurs.
210 : */
211 : int
212 0 : xmlSecKeyUseWithSet(xmlSecKeyUseWithPtr keyUseWith, const xmlChar* application, const xmlChar* identifier) {
213 0 : xmlSecAssert2(keyUseWith != NULL, -1);
214 :
215 0 : if(keyUseWith->application != NULL) {
216 0 : xmlFree(keyUseWith->application);
217 0 : keyUseWith->application = NULL;
218 : }
219 0 : if(keyUseWith->identifier != NULL) {
220 0 : xmlFree(keyUseWith->identifier);
221 0 : keyUseWith->identifier = NULL;
222 : }
223 :
224 0 : if(application != NULL) {
225 0 : keyUseWith->application = xmlStrdup(application);
226 0 : if(keyUseWith->application == NULL) {
227 0 : xmlSecError(XMLSEC_ERRORS_HERE,
228 : NULL,
229 : NULL,
230 : XMLSEC_ERRORS_R_MALLOC_FAILED,
231 : "xmlStrlen(application)=%d",
232 : xmlStrlen(application));
233 0 : return(-1);
234 : }
235 : }
236 0 : if(identifier != NULL) {
237 0 : keyUseWith->identifier = xmlStrdup(identifier);
238 0 : if(keyUseWith->identifier == NULL) {
239 0 : xmlSecError(XMLSEC_ERRORS_HERE,
240 : NULL,
241 : NULL,
242 : XMLSEC_ERRORS_R_MALLOC_FAILED,
243 : "xmlStrlen(identifier)=%d",
244 : xmlStrlen(identifier));
245 0 : return(-1);
246 : }
247 : }
248 :
249 0 : return(0);
250 : }
251 :
252 : /**
253 : * xmlSecKeyUseWithDebugDump:
254 : * @keyUseWith: the pointer to information about key application/user.
255 : * @output: the pointer to output FILE.
256 : *
257 : * Prints xmlSecKeyUseWith debug information to a file @output.
258 : */
259 : void
260 0 : xmlSecKeyUseWithDebugDump(xmlSecKeyUseWithPtr keyUseWith, FILE* output) {
261 0 : xmlSecAssert(keyUseWith != NULL);
262 0 : xmlSecAssert(output != NULL);
263 :
264 0 : fprintf(output, "=== KeyUseWith: application=\"%s\",identifier=\"%s\"\n",
265 0 : (keyUseWith->application) ? keyUseWith->application : BAD_CAST "",
266 0 : (keyUseWith->identifier) ? keyUseWith->identifier : BAD_CAST "");
267 : }
268 :
269 : /**
270 : * xmlSecKeyUseWithDebugXmlDump:
271 : * @keyUseWith: the pointer to information about key application/user.
272 : * @output: the pointer to output FILE.
273 : *
274 : * Prints xmlSecKeyUseWith debug information to a file @output in XML format.
275 : */
276 : void
277 0 : xmlSecKeyUseWithDebugXmlDump(xmlSecKeyUseWithPtr keyUseWith, FILE* output) {
278 0 : xmlSecAssert(keyUseWith != NULL);
279 0 : xmlSecAssert(output != NULL);
280 :
281 0 : fprintf(output, "<KeyUseWith>\n");
282 :
283 0 : fprintf(output, "<Application>");
284 0 : xmlSecPrintXmlString(output, keyUseWith->application);
285 0 : fprintf(output, "</Application>");
286 :
287 0 : fprintf(output, "<Identifier>");
288 0 : xmlSecPrintXmlString(output, keyUseWith->identifier);
289 0 : fprintf(output, "</Identifier>");
290 :
291 0 : fprintf(output, "</KeyUseWith>\n");
292 : }
293 :
294 : /***********************************************************************
295 : *
296 : * KeyUseWith list
297 : *
298 : **********************************************************************/
299 : static xmlSecPtrListKlass xmlSecKeyUseWithPtrListKlass = {
300 : BAD_CAST "key-use-with-list",
301 : (xmlSecPtrDuplicateItemMethod)xmlSecKeyUseWithDuplicate, /* xmlSecPtrDuplicateItemMethod duplicateItem; */
302 : (xmlSecPtrDestroyItemMethod)xmlSecKeyUseWithDestroy, /* xmlSecPtrDestroyItemMethod destroyItem; */
303 : (xmlSecPtrDebugDumpItemMethod)xmlSecKeyUseWithDebugDump, /* xmlSecPtrDebugDumpItemMethod debugDumpItem; */
304 : (xmlSecPtrDebugDumpItemMethod)xmlSecKeyUseWithDebugXmlDump, /* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */
305 : };
306 :
307 : /**
308 : * xmlSecKeyUseWithPtrListGetKlass:
309 : *
310 : * The key data list klass.
311 : *
312 : * Returns: pointer to the key data list klass.
313 : */
314 : xmlSecPtrListId
315 0 : xmlSecKeyUseWithPtrListGetKlass(void) {
316 0 : return(&xmlSecKeyUseWithPtrListKlass);
317 : }
318 :
319 : /**************************************************************************
320 : *
321 : * xmlSecKeyReq - what key are we looking for?
322 : *
323 : *************************************************************************/
324 : /**
325 : * xmlSecKeyReqInitialize:
326 : * @keyReq: the pointer to key requirements object.
327 : *
328 : * Initialize key requirements object. Caller is responsible for
329 : * cleaning it with #xmlSecKeyReqFinalize function.
330 : *
331 : * Returns: 0 on success or a negative value if an error occurs.
332 : */
333 : int
334 0 : xmlSecKeyReqInitialize(xmlSecKeyReqPtr keyReq) {
335 : int ret;
336 :
337 0 : xmlSecAssert2(keyReq != NULL, -1);
338 :
339 0 : memset(keyReq, 0, sizeof(xmlSecKeyReq));
340 :
341 0 : keyReq->keyUsage = xmlSecKeyUsageAny; /* by default you can do whatever you want with the key */
342 0 : ret = xmlSecPtrListInitialize(&keyReq->keyUseWithList, xmlSecKeyUseWithPtrListId);
343 0 : if(ret < 0) {
344 0 : xmlSecError(XMLSEC_ERRORS_HERE,
345 : NULL,
346 : "xmlSecPtrListInitialize",
347 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
348 : XMLSEC_ERRORS_NO_MESSAGE);
349 0 : return(-1);
350 : }
351 :
352 :
353 0 : return(0);
354 : }
355 :
356 : /**
357 : * xmlSecKeyReqFinalize:
358 : * @keyReq: the pointer to key requirements object.
359 : *
360 : * Cleans the key requirements object initialized with #xmlSecKeyReqInitialize
361 : * function.
362 : */
363 : void
364 0 : xmlSecKeyReqFinalize(xmlSecKeyReqPtr keyReq) {
365 0 : xmlSecAssert(keyReq != NULL);
366 :
367 0 : xmlSecPtrListFinalize(&keyReq->keyUseWithList);
368 0 : memset(keyReq, 0, sizeof(xmlSecKeyReq));
369 : }
370 :
371 : /**
372 : * xmlSecKeyReqReset:
373 : * @keyReq: the pointer to key requirements object.
374 : *
375 : * Resets key requirements object for new key search.
376 : */
377 : void
378 0 : xmlSecKeyReqReset(xmlSecKeyReqPtr keyReq) {
379 0 : xmlSecAssert(keyReq != NULL);
380 :
381 0 : xmlSecPtrListEmpty(&keyReq->keyUseWithList);
382 0 : keyReq->keyId = NULL;
383 0 : keyReq->keyType = 0;
384 0 : keyReq->keyUsage = xmlSecKeyUsageAny;
385 0 : keyReq->keyBitsSize = 0;
386 : }
387 :
388 : /**
389 : * xmlSecKeyReqCopy:
390 : * @dst: the pointer to destination object.
391 : * @src: the pointer to source object.
392 : *
393 : * Copies key requirements from @src object to @dst object.
394 : *
395 : * Returns: 0 on success and a negative value if an error occurs.
396 : */
397 : int
398 0 : xmlSecKeyReqCopy(xmlSecKeyReqPtr dst, xmlSecKeyReqPtr src) {
399 : int ret;
400 :
401 0 : xmlSecAssert2(dst != NULL, -1);
402 0 : xmlSecAssert2(src != NULL, -1);
403 :
404 0 : dst->keyId = src->keyId;
405 0 : dst->keyType = src->keyType;
406 0 : dst->keyUsage = src->keyUsage;
407 0 : dst->keyBitsSize = src->keyBitsSize;
408 :
409 0 : ret = xmlSecPtrListCopy(&dst->keyUseWithList, &src->keyUseWithList);
410 0 : if(ret < 0) {
411 0 : xmlSecError(XMLSEC_ERRORS_HERE,
412 : NULL,
413 : "xmlSecPtrListCopy",
414 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
415 : XMLSEC_ERRORS_NO_MESSAGE);
416 0 : return(-1);
417 : }
418 :
419 0 : return(0);
420 : }
421 :
422 : /**
423 : * xmlSecKeyReqMatchKey:
424 : * @keyReq: the pointer to key requirements object.
425 : * @key: the pointer to key.
426 : *
427 : * Checks whether @key matches key requirements @keyReq.
428 : *
429 : * Returns: 1 if key matches requirements, 0 if not and a negative value
430 : * if an error occurs.
431 : */
432 : int
433 0 : xmlSecKeyReqMatchKey(xmlSecKeyReqPtr keyReq, xmlSecKeyPtr key) {
434 0 : xmlSecAssert2(keyReq != NULL, -1);
435 0 : xmlSecAssert2(xmlSecKeyIsValid(key), -1);
436 :
437 0 : if((keyReq->keyType != xmlSecKeyDataTypeUnknown) && ((xmlSecKeyGetType(key) & keyReq->keyType) == 0)) {
438 0 : return(0);
439 : }
440 0 : if((keyReq->keyUsage != xmlSecKeyDataUsageUnknown) && ((keyReq->keyUsage & key->usage) == 0)) {
441 0 : return(0);
442 : }
443 :
444 0 : return(xmlSecKeyReqMatchKeyValue(keyReq, xmlSecKeyGetValue(key)));
445 : }
446 :
447 : /**
448 : * xmlSecKeyReqMatchKeyValue:
449 : * @keyReq: the pointer to key requirements.
450 : * @value: the pointer to key value.
451 : *
452 : * Checks whether @keyValue matches key requirements @keyReq.
453 : *
454 : * Returns: 1 if key value matches requirements, 0 if not and a negative value
455 : * if an error occurs.
456 : */
457 : int
458 0 : xmlSecKeyReqMatchKeyValue(xmlSecKeyReqPtr keyReq, xmlSecKeyDataPtr value) {
459 0 : xmlSecAssert2(keyReq != NULL, -1);
460 0 : xmlSecAssert2(value != NULL, -1);
461 :
462 0 : if((keyReq->keyId != xmlSecKeyDataIdUnknown) &&
463 0 : (!xmlSecKeyDataCheckId(value, keyReq->keyId))) {
464 :
465 0 : return(0);
466 : }
467 0 : if((keyReq->keyBitsSize > 0) &&
468 0 : (xmlSecKeyDataGetSize(value) > 0) &&
469 0 : (xmlSecKeyDataGetSize(value) < keyReq->keyBitsSize)) {
470 :
471 0 : return(0);
472 : }
473 0 : return(1);
474 : }
475 :
476 : /**
477 : * xmlSecKeyReqDebugDump:
478 : * @keyReq: the pointer to key requirements object.
479 : * @output: the pointer to output FILE.
480 : *
481 : * Prints debug information about @keyReq into @output.
482 : */
483 : void
484 0 : xmlSecKeyReqDebugDump(xmlSecKeyReqPtr keyReq, FILE* output) {
485 0 : xmlSecAssert(keyReq != NULL);
486 0 : xmlSecAssert(output != NULL);
487 :
488 0 : fprintf(output, "=== KeyReq:\n");
489 0 : fprintf(output, "==== keyId: %s\n",
490 0 : (xmlSecKeyDataKlassGetName(keyReq->keyId)) ?
491 0 : xmlSecKeyDataKlassGetName(keyReq->keyId) :
492 : BAD_CAST "NULL");
493 0 : fprintf(output, "==== keyType: 0x%08x\n", keyReq->keyType);
494 0 : fprintf(output, "==== keyUsage: 0x%08x\n", keyReq->keyUsage);
495 0 : fprintf(output, "==== keyBitsSize: %d\n", keyReq->keyBitsSize);
496 0 : xmlSecPtrListDebugDump(&(keyReq->keyUseWithList), output);
497 : }
498 :
499 : /**
500 : * xmlSecKeyReqDebugXmlDump:
501 : * @keyReq: the pointer to key requirements object.
502 : * @output: the pointer to output FILE.
503 : *
504 : * Prints debug information about @keyReq into @output in XML format.
505 : */
506 : void
507 0 : xmlSecKeyReqDebugXmlDump(xmlSecKeyReqPtr keyReq, FILE* output) {
508 0 : xmlSecAssert(keyReq != NULL);
509 0 : xmlSecAssert(output != NULL);
510 :
511 0 : fprintf(output, "<KeyReq>\n");
512 :
513 0 : fprintf(output, "<KeyId>");
514 0 : xmlSecPrintXmlString(output, xmlSecKeyDataKlassGetName(keyReq->keyId));
515 0 : fprintf(output, "</KeyId>\n");
516 :
517 0 : fprintf(output, "<KeyType>0x%08x</KeyType>\n", keyReq->keyType);
518 0 : fprintf(output, "<KeyUsage>0x%08x</KeyUsage>\n", keyReq->keyUsage);
519 0 : fprintf(output, "<KeyBitsSize>%d</KeyBitsSize>\n", keyReq->keyBitsSize);
520 0 : xmlSecPtrListDebugXmlDump(&(keyReq->keyUseWithList), output);
521 0 : fprintf(output, "</KeyReq>\n");
522 : }
523 :
524 :
525 : /**************************************************************************
526 : *
527 : * xmlSecKey
528 : *
529 : *************************************************************************/
530 : /**
531 : * xmlSecKeyCreate:
532 : *
533 : * Allocates and initializes new key. Caller is responsible for
534 : * freeing returned object with #xmlSecKeyDestroy function.
535 : *
536 : * Returns: the pointer to newly allocated @xmlSecKey structure
537 : * or NULL if an error occurs.
538 : */
539 : xmlSecKeyPtr
540 0 : xmlSecKeyCreate(void) {
541 : xmlSecKeyPtr key;
542 :
543 : /* Allocate a new xmlSecKey and fill the fields. */
544 0 : key = (xmlSecKeyPtr)xmlMalloc(sizeof(xmlSecKey));
545 0 : if(key == NULL) {
546 0 : xmlSecError(XMLSEC_ERRORS_HERE,
547 : NULL,
548 : NULL,
549 : XMLSEC_ERRORS_R_MALLOC_FAILED,
550 : "sizeof(xmlSecKey)=%d",
551 : sizeof(xmlSecKey));
552 0 : return(NULL);
553 : }
554 0 : memset(key, 0, sizeof(xmlSecKey));
555 0 : key->usage = xmlSecKeyUsageAny;
556 0 : return(key);
557 : }
558 :
559 : /**
560 : * xmlSecKeyEmpty:
561 : * @key: the pointer to key.
562 : *
563 : * Clears the @key data.
564 : */
565 : void
566 0 : xmlSecKeyEmpty(xmlSecKeyPtr key) {
567 0 : xmlSecAssert(key != NULL);
568 :
569 0 : if(key->value != NULL) {
570 0 : xmlSecKeyDataDestroy(key->value);
571 : }
572 0 : if(key->name != NULL) {
573 0 : xmlFree(key->name);
574 : }
575 0 : if(key->dataList != NULL) {
576 0 : xmlSecPtrListDestroy(key->dataList);
577 : }
578 :
579 0 : memset(key, 0, sizeof(xmlSecKey));
580 : }
581 :
582 : /**
583 : * xmlSecKeyDestroy:
584 : * @key: the pointer to key.
585 : *
586 : * Destroys the key created using #xmlSecKeyCreate function.
587 : */
588 : void
589 0 : xmlSecKeyDestroy(xmlSecKeyPtr key) {
590 0 : xmlSecAssert(key != NULL);
591 :
592 0 : xmlSecKeyEmpty(key);
593 0 : xmlFree(key);
594 : }
595 :
596 : /**
597 : * xmlSecKeyCopy:
598 : * @keyDst: the destination key.
599 : * @keySrc: the source key.
600 : *
601 : * Copies key data from @keySrc to @keyDst.
602 : *
603 : * Returns: 0 on success or a negative value if an error occurs.
604 : */
605 : int
606 0 : xmlSecKeyCopy(xmlSecKeyPtr keyDst, xmlSecKeyPtr keySrc) {
607 0 : xmlSecAssert2(keyDst != NULL, -1);
608 0 : xmlSecAssert2(keySrc != NULL, -1);
609 :
610 : /* empty destination */
611 0 : xmlSecKeyEmpty(keyDst);
612 :
613 : /* copy everything */
614 0 : if(keySrc->name != NULL) {
615 0 : keyDst->name = xmlStrdup(keySrc->name);
616 0 : if(keyDst->name == NULL) {
617 0 : xmlSecError(XMLSEC_ERRORS_HERE,
618 : NULL,
619 : NULL,
620 : XMLSEC_ERRORS_R_STRDUP_FAILED,
621 0 : "len=%d", xmlStrlen(keySrc->name));
622 0 : return(-1);
623 : }
624 : }
625 :
626 0 : if(keySrc->value != NULL) {
627 0 : keyDst->value = xmlSecKeyDataDuplicate(keySrc->value);
628 0 : if(keyDst->value == NULL) {
629 0 : xmlSecError(XMLSEC_ERRORS_HERE,
630 : NULL,
631 : "xmlSecKeyDataDuplicate",
632 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
633 : XMLSEC_ERRORS_NO_MESSAGE);
634 0 : return(-1);
635 : }
636 : }
637 :
638 0 : if(keySrc->dataList != NULL) {
639 0 : keyDst->dataList = xmlSecPtrListDuplicate(keySrc->dataList);
640 0 : if(keyDst->dataList == NULL) {
641 0 : xmlSecError(XMLSEC_ERRORS_HERE,
642 : NULL,
643 : "xmlSecPtrListDuplicate",
644 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
645 : XMLSEC_ERRORS_NO_MESSAGE);
646 0 : return(-1);
647 : }
648 : }
649 :
650 0 : keyDst->usage = keySrc->usage;
651 0 : keyDst->notValidBefore = keySrc->notValidBefore;
652 0 : keyDst->notValidAfter = keySrc->notValidAfter;
653 0 : return(0);
654 : }
655 :
656 : /**
657 : * xmlSecKeyDuplicate:
658 : * @key: the pointer to the #xmlSecKey structure.
659 : *
660 : * Creates a duplicate of the given @key.
661 : *
662 : * Returns: the pointer to newly allocated #xmlSecKey structure
663 : * or NULL if an error occurs.
664 : */
665 : xmlSecKeyPtr
666 0 : xmlSecKeyDuplicate(xmlSecKeyPtr key) {
667 : xmlSecKeyPtr newKey;
668 : int ret;
669 :
670 0 : xmlSecAssert2(key != NULL, NULL);
671 :
672 0 : newKey = xmlSecKeyCreate();
673 0 : if(newKey == NULL) {
674 0 : xmlSecError(XMLSEC_ERRORS_HERE,
675 : NULL,
676 : "xmlSecKeyCreate",
677 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
678 : XMLSEC_ERRORS_NO_MESSAGE);
679 0 : return(NULL);
680 : }
681 :
682 0 : ret = xmlSecKeyCopy(newKey, key);
683 0 : if(ret < 0) {
684 0 : xmlSecError(XMLSEC_ERRORS_HERE,
685 : NULL,
686 : "xmlSecKeyCopy",
687 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
688 : XMLSEC_ERRORS_NO_MESSAGE);
689 0 : xmlSecKeyDestroy(newKey);
690 0 : return(NULL);
691 : }
692 :
693 0 : return(newKey);
694 : }
695 :
696 : /**
697 : * xmlSecKeyMatch:
698 : * @key: the pointer to key.
699 : * @name: the pointer to key name (may be NULL).
700 : * @keyReq: the pointer to key requirements.
701 : *
702 : * Checks whether the @key matches the given criteria.
703 : *
704 : * Returns: 1 if the key satisfies the given criteria or 0 otherwise.
705 : */
706 : int
707 0 : xmlSecKeyMatch(xmlSecKeyPtr key, const xmlChar *name, xmlSecKeyReqPtr keyReq) {
708 0 : xmlSecAssert2(xmlSecKeyIsValid(key), -1);
709 0 : xmlSecAssert2(keyReq != NULL, -1);
710 :
711 0 : if((name != NULL) && (!xmlStrEqual(xmlSecKeyGetName(key), name))) {
712 0 : return(0);
713 : }
714 0 : return(xmlSecKeyReqMatchKey(keyReq, key));
715 : }
716 :
717 : /**
718 : * xmlSecKeyGetType:
719 : * @key: the pointer to key.
720 : *
721 : * Gets @key type.
722 : *
723 : * Returns: key type.
724 : */
725 : xmlSecKeyDataType
726 0 : xmlSecKeyGetType(xmlSecKeyPtr key) {
727 : xmlSecKeyDataPtr data;
728 :
729 0 : xmlSecAssert2(key != NULL, xmlSecKeyDataTypeUnknown);
730 :
731 0 : data = xmlSecKeyGetValue(key);
732 0 : if(data == NULL) {
733 0 : return(xmlSecKeyDataTypeUnknown);
734 : }
735 0 : return(xmlSecKeyDataGetType(data));
736 : }
737 :
738 : /**
739 : * xmlSecKeyGetName:
740 : * @key: the pointer to key.
741 : *
742 : * Gets key name (see also #xmlSecKeySetName function).
743 : *
744 : * Returns: key name.
745 : */
746 : const xmlChar*
747 0 : xmlSecKeyGetName(xmlSecKeyPtr key) {
748 0 : xmlSecAssert2(key != NULL, NULL);
749 :
750 0 : return(key->name);
751 : }
752 :
753 : /**
754 : * xmlSecKeySetName:
755 : * @key: the pointer to key.
756 : * @name: the new key name.
757 : *
758 : * Sets key name (see also #xmlSecKeyGetName function).
759 : *
760 : * Returns: 0 on success or a negative value if an error occurs.
761 : */
762 : int
763 0 : xmlSecKeySetName(xmlSecKeyPtr key, const xmlChar* name) {
764 0 : xmlSecAssert2(key != NULL, -1);
765 :
766 0 : if(key->name != NULL) {
767 0 : xmlFree(key->name);
768 0 : key->name = NULL;
769 : }
770 :
771 0 : if(name != NULL) {
772 0 : key->name = xmlStrdup(name);
773 0 : if(key->name == NULL) {
774 0 : xmlSecError(XMLSEC_ERRORS_HERE,
775 : NULL,
776 : NULL,
777 : XMLSEC_ERRORS_R_STRDUP_FAILED,
778 : "len=%d", xmlStrlen(name));
779 0 : return(-1);
780 : }
781 : }
782 :
783 0 : return(0);
784 : }
785 :
786 : /**
787 : * xmlSecKeyGetValue:
788 : * @key: the pointer to key.
789 : *
790 : * Gets key value (see also #xmlSecKeySetValue function).
791 : *
792 : * Returns: key value (crypto material).
793 : */
794 : xmlSecKeyDataPtr
795 0 : xmlSecKeyGetValue(xmlSecKeyPtr key) {
796 0 : xmlSecAssert2(key != NULL, NULL);
797 :
798 0 : return(key->value);
799 : }
800 :
801 : /**
802 : * xmlSecKeySetValue:
803 : * @key: the pointer to key.
804 : * @value: the new value.
805 : *
806 : * Sets key value (see also #xmlSecKeyGetValue function).
807 : *
808 : * Returns: 0 on success or a negative value if an error occurs.
809 : */
810 : int
811 0 : xmlSecKeySetValue(xmlSecKeyPtr key, xmlSecKeyDataPtr value) {
812 0 : xmlSecAssert2(key != NULL, -1);
813 :
814 0 : if(key->value != NULL) {
815 0 : xmlSecKeyDataDestroy(key->value);
816 0 : key->value = NULL;
817 : }
818 0 : key->value = value;
819 :
820 0 : return(0);
821 : }
822 :
823 : /**
824 : * xmlSecKeyGetData:
825 : * @key: the pointer to key.
826 : * @dataId: the requested data klass.
827 : *
828 : * Gets key's data.
829 : *
830 : * Returns: additional data associated with the @key (see also
831 : * #xmlSecKeyAdoptData function).
832 : */
833 : xmlSecKeyDataPtr
834 0 : xmlSecKeyGetData(xmlSecKeyPtr key, xmlSecKeyDataId dataId) {
835 :
836 0 : xmlSecAssert2(key != NULL, NULL);
837 0 : xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
838 :
839 : /* special cases */
840 0 : if(dataId == xmlSecKeyDataValueId) {
841 0 : return(key->value);
842 0 : } else if(key->dataList != NULL) {
843 : xmlSecKeyDataPtr tmp;
844 : xmlSecSize pos, size;
845 :
846 0 : size = xmlSecPtrListGetSize(key->dataList);
847 0 : for(pos = 0; pos < size; ++pos) {
848 0 : tmp = (xmlSecKeyDataPtr)xmlSecPtrListGetItem(key->dataList, pos);
849 0 : if((tmp != NULL) && (tmp->id == dataId)) {
850 0 : return(tmp);
851 : }
852 : }
853 : }
854 0 : return(NULL);
855 : }
856 :
857 : /**
858 : * xmlSecKeyEnsureData:
859 : * @key: the pointer to key.
860 : * @dataId: the requested data klass.
861 : *
862 : * If necessary, creates key data of @dataId klass and adds to @key.
863 : *
864 : * Returns: pointer to key data or NULL if an error occurs.
865 : */
866 : xmlSecKeyDataPtr
867 0 : xmlSecKeyEnsureData(xmlSecKeyPtr key, xmlSecKeyDataId dataId) {
868 : xmlSecKeyDataPtr data;
869 : int ret;
870 :
871 0 : xmlSecAssert2(key != NULL, NULL);
872 0 : xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
873 :
874 0 : data = xmlSecKeyGetData(key, dataId);
875 0 : if(data != NULL) {
876 0 : return(data);
877 : }
878 :
879 0 : data = xmlSecKeyDataCreate(dataId);
880 0 : if(data == NULL) {
881 0 : xmlSecError(XMLSEC_ERRORS_HERE,
882 : NULL,
883 : "xmlSecKeyDataCreate",
884 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
885 : "dataId=%s",
886 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)));
887 0 : return(NULL);
888 : }
889 :
890 0 : ret = xmlSecKeyAdoptData(key, data);
891 0 : if(ret < 0) {
892 0 : xmlSecError(XMLSEC_ERRORS_HERE,
893 : NULL,
894 : "xmlSecKeyAdoptData",
895 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
896 : "dataId=%s",
897 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)));
898 0 : xmlSecKeyDataDestroy(data);
899 0 : return(NULL);
900 : }
901 :
902 0 : return(data);
903 : }
904 :
905 : /**
906 : * xmlSecKeyAdoptData:
907 : * @key: the pointer to key.
908 : * @data: the pointer to key data.
909 : *
910 : * Adds @data to the @key. The @data object will be destroyed
911 : * by @key.
912 : *
913 : * Returns: 0 on success or a negative value otherwise.
914 : */
915 : int
916 0 : xmlSecKeyAdoptData(xmlSecKeyPtr key, xmlSecKeyDataPtr data) {
917 : xmlSecKeyDataPtr tmp;
918 : xmlSecSize pos, size;
919 :
920 0 : xmlSecAssert2(key != NULL, -1);
921 0 : xmlSecAssert2(xmlSecKeyDataIsValid(data), -1);
922 :
923 : /* special cases */
924 0 : if(data->id == xmlSecKeyDataValueId) {
925 0 : if(key->value != NULL) {
926 0 : xmlSecKeyDataDestroy(key->value);
927 : }
928 0 : key->value = data;
929 0 : return(0);
930 : }
931 :
932 0 : if(key->dataList == NULL) {
933 0 : key->dataList = xmlSecPtrListCreate(xmlSecKeyDataListId);
934 0 : if(key->dataList == NULL) {
935 0 : xmlSecError(XMLSEC_ERRORS_HERE,
936 : NULL,
937 : "xmlSecPtrListCreate",
938 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
939 : XMLSEC_ERRORS_NO_MESSAGE);
940 0 : return(-1);
941 : }
942 : }
943 :
944 :
945 0 : size = xmlSecPtrListGetSize(key->dataList);
946 0 : for(pos = 0; pos < size; ++pos) {
947 0 : tmp = (xmlSecKeyDataPtr)xmlSecPtrListGetItem(key->dataList, pos);
948 0 : if((tmp != NULL) && (tmp->id == data->id)) {
949 0 : return(xmlSecPtrListSet(key->dataList, data, pos));
950 : }
951 : }
952 :
953 0 : return(xmlSecPtrListAdd(key->dataList, data));
954 : }
955 :
956 : /**
957 : * xmlSecKeyDebugDump:
958 : * @key: the pointer to key.
959 : * @output: the pointer to output FILE.
960 : *
961 : * Prints the information about the @key to the @output.
962 : */
963 : void
964 0 : xmlSecKeyDebugDump(xmlSecKeyPtr key, FILE *output) {
965 0 : xmlSecAssert(xmlSecKeyIsValid(key));
966 0 : xmlSecAssert(output != NULL);
967 :
968 0 : fprintf(output, "== KEY\n");
969 0 : fprintf(output, "=== method: %s\n",
970 0 : (key->value->id->dataNodeName != NULL) ?
971 0 : (char*)(key->value->id->dataNodeName) : "NULL");
972 :
973 0 : fprintf(output, "=== key type: ");
974 0 : if((xmlSecKeyGetType(key) & xmlSecKeyDataTypeSymmetric) != 0) {
975 0 : fprintf(output, "Symmetric\n");
976 0 : } else if((xmlSecKeyGetType(key) & xmlSecKeyDataTypePrivate) != 0) {
977 0 : fprintf(output, "Private\n");
978 0 : } else if((xmlSecKeyGetType(key) & xmlSecKeyDataTypePublic) != 0) {
979 0 : fprintf(output, "Public\n");
980 : } else {
981 0 : fprintf(output, "Unknown\n");
982 : }
983 :
984 0 : if(key->name != NULL) {
985 0 : fprintf(output, "=== key name: %s\n", key->name);
986 : }
987 0 : fprintf(output, "=== key usage: %d\n", key->usage);
988 0 : if(key->notValidBefore < key->notValidAfter) {
989 0 : fprintf(output, "=== key not valid before: %ld\n", (unsigned long)key->notValidBefore);
990 0 : fprintf(output, "=== key not valid after: %ld\n", (unsigned long)key->notValidAfter);
991 : }
992 0 : if(key->value != NULL) {
993 0 : xmlSecKeyDataDebugDump(key->value, output);
994 : }
995 0 : if(key->dataList != NULL) {
996 0 : xmlSecPtrListDebugDump(key->dataList, output);
997 : }
998 : }
999 :
1000 : /**
1001 : * xmlSecKeyDebugXmlDump:
1002 : * @key: the pointer to key.
1003 : * @output: the pointer to output FILE.
1004 : *
1005 : * Prints the information about the @key to the @output in XML format.
1006 : */
1007 : void
1008 0 : xmlSecKeyDebugXmlDump(xmlSecKeyPtr key, FILE *output) {
1009 0 : xmlSecAssert(xmlSecKeyIsValid(key));
1010 0 : xmlSecAssert(output != NULL);
1011 :
1012 0 : fprintf(output, "<KeyInfo>\n");
1013 :
1014 0 : fprintf(output, "<KeyMethod>");
1015 0 : xmlSecPrintXmlString(output, key->value->id->dataNodeName);
1016 0 : fprintf(output, "</KeyMethod>\n");
1017 :
1018 0 : fprintf(output, "<KeyType>");
1019 0 : if((xmlSecKeyGetType(key) & xmlSecKeyDataTypeSymmetric) != 0) {
1020 0 : fprintf(output, "Symmetric\n");
1021 0 : } else if((xmlSecKeyGetType(key) & xmlSecKeyDataTypePrivate) != 0) {
1022 0 : fprintf(output, "Private\n");
1023 0 : } else if((xmlSecKeyGetType(key) & xmlSecKeyDataTypePublic) != 0) {
1024 0 : fprintf(output, "Public\n");
1025 : } else {
1026 0 : fprintf(output, "Unknown\n");
1027 : }
1028 0 : fprintf(output, "</KeyType>\n");
1029 :
1030 0 : fprintf(output, "<KeyName>");
1031 0 : xmlSecPrintXmlString(output, key->name);
1032 0 : fprintf(output, "</KeyName>\n");
1033 :
1034 0 : if(key->notValidBefore < key->notValidAfter) {
1035 0 : fprintf(output, "<KeyValidity notValidBefore=\"%ld\" notValidAfter=\"%ld\"/>\n",
1036 0 : (unsigned long)key->notValidBefore,
1037 0 : (unsigned long)key->notValidAfter);
1038 : }
1039 :
1040 0 : if(key->value != NULL) {
1041 0 : xmlSecKeyDataDebugXmlDump(key->value, output);
1042 : }
1043 0 : if(key->dataList != NULL) {
1044 0 : xmlSecPtrListDebugXmlDump(key->dataList, output);
1045 : }
1046 :
1047 0 : fprintf(output, "</KeyInfo>\n");
1048 : }
1049 :
1050 : /**
1051 : * xmlSecKeyGenerate:
1052 : * @dataId: the requested key klass (rsa, dsa, aes, ...).
1053 : * @sizeBits: the new key size (in bits!).
1054 : * @type: the new key type (session, permanent, ...).
1055 : *
1056 : * Generates new key of requested klass @dataId and @type.
1057 : *
1058 : * Returns: pointer to newly created key or NULL if an error occurs.
1059 : */
1060 : xmlSecKeyPtr
1061 0 : xmlSecKeyGenerate(xmlSecKeyDataId dataId, xmlSecSize sizeBits, xmlSecKeyDataType type) {
1062 : xmlSecKeyPtr key;
1063 : xmlSecKeyDataPtr data;
1064 : int ret;
1065 :
1066 0 : xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
1067 :
1068 0 : data = xmlSecKeyDataCreate(dataId);
1069 0 : if(data == NULL) {
1070 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1071 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1072 : "xmlSecKeyDataCreate",
1073 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1074 : XMLSEC_ERRORS_NO_MESSAGE);
1075 0 : return(NULL);
1076 : }
1077 :
1078 0 : ret = xmlSecKeyDataGenerate(data, sizeBits, type);
1079 0 : if(ret < 0) {
1080 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1081 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1082 : "xmlSecKeyDataGenerate",
1083 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1084 : "size=%d;type=%d", sizeBits, type);
1085 0 : xmlSecKeyDataDestroy(data);
1086 0 : return(NULL);
1087 : }
1088 :
1089 0 : key = xmlSecKeyCreate();
1090 0 : if(key == NULL) {
1091 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1092 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1093 : "xmlSecKeyCreate",
1094 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1095 : XMLSEC_ERRORS_NO_MESSAGE);
1096 0 : xmlSecKeyDataDestroy(data);
1097 0 : return(NULL);
1098 : }
1099 :
1100 0 : ret = xmlSecKeySetValue(key, data);
1101 0 : if(ret < 0) {
1102 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1103 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1104 : "xmlSecKeySetValue",
1105 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1106 : XMLSEC_ERRORS_NO_MESSAGE);
1107 0 : xmlSecKeyDataDestroy(data);
1108 0 : xmlSecKeyDestroy(key);
1109 0 : return(NULL);
1110 : }
1111 :
1112 0 : return(key);
1113 : }
1114 :
1115 : /**
1116 : * xmlSecKeyGenerateByName:
1117 : * @name: the requested key klass name (rsa, dsa, aes, ...).
1118 : * @sizeBits: the new key size (in bits!).
1119 : * @type: the new key type (session, permanent, ...).
1120 : *
1121 : * Generates new key of requested @klass and @type.
1122 : *
1123 : * Returns: pointer to newly created key or NULL if an error occurs.
1124 : */
1125 : xmlSecKeyPtr
1126 0 : xmlSecKeyGenerateByName(const xmlChar* name, xmlSecSize sizeBits, xmlSecKeyDataType type) {
1127 : xmlSecKeyDataId dataId;
1128 :
1129 0 : xmlSecAssert2(name != NULL, NULL);
1130 :
1131 0 : dataId = xmlSecKeyDataIdListFindByName(xmlSecKeyDataIdsGet(), name, xmlSecKeyDataUsageAny);
1132 0 : if(dataId == xmlSecKeyDataIdUnknown) {
1133 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1134 : NULL,
1135 : xmlSecErrorsSafeString(name),
1136 : XMLSEC_ERRORS_R_KEY_DATA_NOT_FOUND,
1137 : XMLSEC_ERRORS_NO_MESSAGE);
1138 0 : return(NULL);
1139 : }
1140 :
1141 0 : return(xmlSecKeyGenerate(dataId, sizeBits, type));
1142 : }
1143 :
1144 : /**
1145 : * xmlSecKeyReadBuffer:
1146 : * @dataId: the key value data klass.
1147 : * @buffer: the buffer that contains the binary data.
1148 : *
1149 : * Reads the key value of klass @dataId from a buffer.
1150 : *
1151 : * Returns: pointer to newly created key or NULL if an error occurs.
1152 : */
1153 : xmlSecKeyPtr
1154 0 : xmlSecKeyReadBuffer(xmlSecKeyDataId dataId, xmlSecBuffer* buffer) {
1155 : xmlSecKeyInfoCtx keyInfoCtx;
1156 : xmlSecKeyPtr key;
1157 : int ret;
1158 :
1159 0 : xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
1160 0 : xmlSecAssert2(buffer != NULL, NULL);
1161 :
1162 : /* create key data */
1163 0 : key = xmlSecKeyCreate();
1164 0 : if(key == NULL) {
1165 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1166 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1167 : "xmlSecKeyCreate",
1168 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1169 : XMLSEC_ERRORS_NO_MESSAGE);
1170 0 : return(NULL);
1171 : }
1172 :
1173 0 : ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL);
1174 0 : if(ret < 0) {
1175 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1176 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1177 : "xmlSecKeyInfoCtxInitialize",
1178 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1179 : XMLSEC_ERRORS_NO_MESSAGE);
1180 0 : xmlSecKeyDestroy(key);
1181 0 : return(NULL);
1182 : }
1183 :
1184 0 : keyInfoCtx.keyReq.keyType = xmlSecKeyDataTypeAny;
1185 0 : ret = xmlSecKeyDataBinRead(dataId, key,
1186 0 : xmlSecBufferGetData(buffer),
1187 : xmlSecBufferGetSize(buffer),
1188 : &keyInfoCtx);
1189 0 : if(ret < 0) {
1190 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1191 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1192 : "xmlSecKeyDataBinRead",
1193 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1194 : XMLSEC_ERRORS_NO_MESSAGE);
1195 0 : xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
1196 0 : xmlSecKeyDestroy(key);
1197 0 : return(NULL);
1198 : }
1199 0 : xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
1200 :
1201 0 : return(key);
1202 : }
1203 :
1204 : /**
1205 : * xmlSecKeyReadBinaryFile:
1206 : * @dataId: the key value data klass.
1207 : * @filename: the key binary filename.
1208 : *
1209 : * Reads the key value of klass @dataId from a binary file @filename.
1210 : *
1211 : * Returns: pointer to newly created key or NULL if an error occurs.
1212 : */
1213 : xmlSecKeyPtr
1214 0 : xmlSecKeyReadBinaryFile(xmlSecKeyDataId dataId, const char* filename) {
1215 : xmlSecKeyPtr key;
1216 : xmlSecBuffer buffer;
1217 : int ret;
1218 :
1219 0 : xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
1220 0 : xmlSecAssert2(filename != NULL, NULL);
1221 :
1222 : /* read file to buffer */
1223 0 : ret = xmlSecBufferInitialize(&buffer, 0);
1224 0 : if(ret < 0) {
1225 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1226 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1227 : "xmlSecBufferInitialize",
1228 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1229 : XMLSEC_ERRORS_NO_MESSAGE);
1230 0 : return(NULL);
1231 : }
1232 :
1233 0 : ret = xmlSecBufferReadFile(&buffer, filename);
1234 0 : if(ret < 0) {
1235 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1236 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1237 : "xmlSecBufferReadFile",
1238 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1239 : "filename=%s",
1240 : xmlSecErrorsSafeString(filename));
1241 0 : xmlSecBufferFinalize(&buffer);
1242 0 : return(NULL);
1243 : }
1244 :
1245 0 : key = xmlSecKeyReadBuffer(dataId, &buffer);
1246 0 : if(key == NULL) {
1247 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1248 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1249 : "xmlSecKeyReadBuffer",
1250 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1251 : "filename=%s",
1252 : xmlSecErrorsSafeString(filename));
1253 0 : xmlSecBufferFinalize(&buffer);
1254 0 : return(NULL);
1255 : }
1256 :
1257 0 : xmlSecBufferFinalize(&buffer);
1258 0 : return (key);
1259 : }
1260 :
1261 : /**
1262 : * xmlSecKeyReadMemory:
1263 : * @dataId: the key value data klass.
1264 : * @data: the memory containing the key
1265 : * @dataSize: the size of the memory block
1266 : *
1267 : * Reads the key value of klass @dataId from a memory block @data.
1268 : *
1269 : * Returns: pointer to newly created key or NULL if an error occurs.
1270 : */
1271 : xmlSecKeyPtr
1272 0 : xmlSecKeyReadMemory(xmlSecKeyDataId dataId, const xmlSecByte* data, xmlSecSize dataSize) {
1273 : xmlSecBuffer buffer;
1274 : xmlSecKeyPtr key;
1275 : int ret;
1276 :
1277 0 : xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, NULL);
1278 0 : xmlSecAssert2(data != NULL, NULL);
1279 0 : xmlSecAssert2(dataSize > 0, NULL);
1280 :
1281 : /* read file to buffer */
1282 0 : ret = xmlSecBufferInitialize(&buffer, 0);
1283 0 : if(ret < 0) {
1284 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1285 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1286 : "xmlSecBufferInitialize",
1287 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1288 : XMLSEC_ERRORS_NO_MESSAGE);
1289 0 : return(NULL);
1290 : }
1291 :
1292 0 : if (xmlSecBufferAppend(&buffer, data, dataSize) < 0) {
1293 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1294 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1295 : "xmlSecBufferAppend",
1296 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1297 : XMLSEC_ERRORS_NO_MESSAGE);
1298 0 : xmlSecBufferFinalize(&buffer);
1299 0 : return(NULL);
1300 : }
1301 :
1302 0 : key = xmlSecKeyReadBuffer(dataId, &buffer);
1303 0 : if(key == NULL) {
1304 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1305 0 : xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(dataId)),
1306 : "xmlSecKeyReadBuffer",
1307 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1308 : XMLSEC_ERRORS_NO_MESSAGE);
1309 0 : xmlSecBufferFinalize(&buffer);
1310 0 : return(NULL);
1311 : }
1312 :
1313 0 : xmlSecBufferFinalize(&buffer);
1314 0 : return (key);
1315 : }
1316 :
1317 : /**
1318 : * xmlSecKeysMngrGetKey:
1319 : * @keyInfoNode: the pointer to <dsig:KeyInfo/> node.
1320 : * @keyInfoCtx: the pointer to <dsig:KeyInfo/> node processing context.
1321 : *
1322 : * Reads the <dsig:KeyInfo/> node @keyInfoNode and extracts the key.
1323 : *
1324 : * Returns: the pointer to key or NULL if the key is not found or
1325 : * an error occurs.
1326 : */
1327 : xmlSecKeyPtr
1328 0 : xmlSecKeysMngrGetKey(xmlNodePtr keyInfoNode, xmlSecKeyInfoCtxPtr keyInfoCtx) {
1329 : xmlSecKeyPtr key;
1330 : int ret;
1331 :
1332 0 : xmlSecAssert2(keyInfoCtx != NULL, NULL);
1333 :
1334 :
1335 : /* first try to read data from <dsig:KeyInfo/> node */
1336 0 : key = xmlSecKeyCreate();
1337 0 : if(key == NULL) {
1338 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1339 : NULL,
1340 : "xmlSecKeyCreate",
1341 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1342 : XMLSEC_ERRORS_NO_MESSAGE);
1343 0 : return(NULL);
1344 : }
1345 :
1346 0 : if(keyInfoNode != NULL) {
1347 0 : ret = xmlSecKeyInfoNodeRead(keyInfoNode, key, keyInfoCtx);
1348 0 : if(ret < 0) {
1349 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1350 : NULL,
1351 : "xmlSecKeyInfoNodeRead",
1352 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1353 : "node=%s",
1354 0 : xmlSecErrorsSafeString(xmlSecNodeGetName(keyInfoNode)));
1355 0 : xmlSecKeyDestroy(key);
1356 0 : return(NULL);
1357 : }
1358 :
1359 0 : if((xmlSecKeyGetValue(key) != NULL) &&
1360 0 : (xmlSecKeyMatch(key, NULL, &(keyInfoCtx->keyReq)) != 0)) {
1361 0 : return(key);
1362 : }
1363 : }
1364 0 : xmlSecKeyDestroy(key);
1365 :
1366 : /* if we have keys manager, try it */
1367 0 : if(keyInfoCtx->keysMngr != NULL) {
1368 0 : key = xmlSecKeysMngrFindKey(keyInfoCtx->keysMngr, NULL, keyInfoCtx);
1369 0 : if(key == NULL) {
1370 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1371 : NULL,
1372 : "xmlSecKeysMngrFindKey",
1373 : XMLSEC_ERRORS_R_XMLSEC_FAILED,
1374 : XMLSEC_ERRORS_NO_MESSAGE);
1375 0 : return(NULL);
1376 : }
1377 0 : if(xmlSecKeyGetValue(key) != NULL) {
1378 0 : return(key);
1379 : }
1380 0 : xmlSecKeyDestroy(key);
1381 : }
1382 :
1383 0 : xmlSecError(XMLSEC_ERRORS_HERE,
1384 : NULL,
1385 : NULL,
1386 : XMLSEC_ERRORS_R_KEY_NOT_FOUND,
1387 : XMLSEC_ERRORS_NO_MESSAGE);
1388 0 : return(NULL);
1389 : }
1390 :
1391 : /***********************************************************************
1392 : *
1393 : * Keys list
1394 : *
1395 : **********************************************************************/
1396 : static xmlSecPtrListKlass xmlSecKeyPtrListKlass = {
1397 : BAD_CAST "keys-list",
1398 : (xmlSecPtrDuplicateItemMethod)xmlSecKeyDuplicate, /* xmlSecPtrDuplicateItemMethod duplicateItem; */
1399 : (xmlSecPtrDestroyItemMethod)xmlSecKeyDestroy, /* xmlSecPtrDestroyItemMethod destroyItem; */
1400 : (xmlSecPtrDebugDumpItemMethod)xmlSecKeyDebugDump, /* xmlSecPtrDebugDumpItemMethod debugDumpItem; */
1401 : (xmlSecPtrDebugDumpItemMethod)xmlSecKeyDebugXmlDump,/* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */
1402 : };
1403 :
1404 : /**
1405 : * xmlSecKeyPtrListGetKlass:
1406 : *
1407 : * The keys list klass.
1408 : *
1409 : * Returns: keys list id.
1410 : */
1411 : xmlSecPtrListId
1412 0 : xmlSecKeyPtrListGetKlass(void) {
1413 0 : return(&xmlSecKeyPtrListKlass);
1414 : }
1415 :
|