OLD | NEW |
1 #define IN_LIBEXSLT | 1 #define IN_LIBEXSLT |
2 #include "libexslt/libexslt.h" | 2 #include "libexslt/libexslt.h" |
3 | 3 |
4 #if defined(WIN32) && !defined (__CYGWIN__) && (!__MINGW32__) | 4 #if defined(WIN32) && !defined (__CYGWIN__) && (!__MINGW32__) |
5 #include <win32config.h> | 5 #include <win32config.h> |
6 #else | 6 #else |
7 #include "config.h" | 7 #include "config.h" |
8 #endif | 8 #endif |
9 | 9 |
10 #include <libxml/tree.h> | 10 #include <libxml/tree.h> |
11 #include <libxml/xpath.h> | 11 #include <libxml/xpath.h> |
12 #include <libxml/xpathInternals.h> | 12 #include <libxml/xpathInternals.h> |
13 #include <libxml/parser.h> | 13 #include <libxml/parser.h> |
14 #include <libxml/encoding.h> | 14 #include <libxml/encoding.h> |
15 #include <libxml/uri.h> | 15 #include <libxml/uri.h> |
16 | 16 |
17 #include <libxslt/xsltconfig.h> | 17 #include <libxslt/xsltconfig.h> |
18 #include <libxslt/xsltutils.h> | 18 #include <libxslt/xsltutils.h> |
19 #include <libxslt/xsltInternals.h> | 19 #include <libxslt/xsltInternals.h> |
20 #include <libxslt/extensions.h> | 20 #include <libxslt/extensions.h> |
21 | 21 |
22 #include "exslt.h" | 22 #include "exslt.h" |
23 | 23 |
24 #ifdef EXSLT_CRYPTO_ENABLED | 24 #ifdef EXSLT_CRYPTO_ENABLED |
25 | 25 |
26 #define HASH_DIGEST_LENGTH 32 | 26 #define HASH_DIGEST_LENGTH 32 |
27 #define MD5_DIGEST_LENGTH 16 | 27 #define MD5_DIGEST_LENGTH 16 |
28 #define SHA1_DIGEST_LENGTH 20 | 28 #define SHA1_DIGEST_LENGTH 20 |
29 | 29 |
30 /* gcrypt rc4 can do 256 bit keys, but cryptoapi limit | 30 /* gcrypt rc4 can do 256 bit keys, but cryptoapi limit |
31 seems to be 128 for the default provider */ | 31 seems to be 128 for the default provider */ |
32 #define RC4_KEY_LENGTH 128 | 32 #define RC4_KEY_LENGTH 128 |
33 | 33 |
34 /* The following routines have been declared static - this should be | 34 /* The following routines have been declared static - this should be |
35 reviewed to consider whether we want to expose them to the API | 35 reviewed to consider whether we want to expose them to the API |
36 exsltCryptoBin2Hex | 36 exsltCryptoBin2Hex |
37 exsltCryptoHex2Bin | 37 exsltCryptoHex2Bin |
38 exsltCryptoGcryptInit | 38 exsltCryptoGcryptInit |
39 exsltCryptoGcryptHash | 39 exsltCryptoGcryptHash |
40 exsltCryptoGcryptRc4Encrypt | 40 exsltCryptoGcryptRc4Encrypt |
41 exsltCryptoGcryptRC4Decrypt | 41 exsltCryptoGcryptRC4Decrypt |
42 */ | 42 */ |
43 | 43 |
44 /** | 44 /** |
45 * exsltCryptoBin2Hex: | 45 * exsltCryptoBin2Hex: |
46 * @bin: binary blob to convert | 46 * @bin: binary blob to convert |
47 * @binlen: length of binary blob | 47 * @binlen: length of binary blob |
48 * @hex: buffer to store hex version of blob | 48 * @hex: buffer to store hex version of blob |
49 * @hexlen: length of buffer to store hex version of blob | 49 * @hexlen: length of buffer to store hex version of blob |
50 * | 50 * |
51 * Helper function which encodes a binary blob as hex. | 51 * Helper function which encodes a binary blob as hex. |
52 */ | 52 */ |
53 static void | 53 static void |
54 exsltCryptoBin2Hex (const unsigned char *bin, int binlen, | 54 exsltCryptoBin2Hex (const unsigned char *bin, int binlen, |
55 unsigned char *hex, int hexlen) { | 55 unsigned char *hex, int hexlen) { |
56 static const char bin2hex[] = { '0', '1', '2', '3', | 56 static const char bin2hex[] = { '0', '1', '2', '3', |
57 '4', '5', '6', '7', | 57 '4', '5', '6', '7', |
58 '8', '9', 'a', 'b', | 58 '8', '9', 'a', 'b', |
59 'c', 'd', 'e', 'f' | 59 'c', 'd', 'e', 'f' |
60 }; | 60 }; |
61 | 61 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 } | 350 } |
351 | 351 |
352 /** | 352 /** |
353 * exsltCryptoGcryptHash: | 353 * exsltCryptoGcryptHash: |
354 * @ctxt: an XPath parser context | 354 * @ctxt: an XPath parser context |
355 * @algorithm: hashing algorithm to use | 355 * @algorithm: hashing algorithm to use |
356 * @msg: text to be hashed | 356 * @msg: text to be hashed |
357 * @msglen: length of text to be hashed | 357 * @msglen: length of text to be hashed |
358 * @dest: buffer to place hash result | 358 * @dest: buffer to place hash result |
359 * | 359 * |
360 * Helper function which hashes a message using MD4, MD5, or SHA1. | 360 * Helper function which hashes a message using MD4, MD5, or SHA1. |
361 * using gcrypt | 361 * using gcrypt |
362 */ | 362 */ |
363 static void | 363 static void |
364 exsltCryptoGcryptHash (xmlXPathParserContextPtr ctxt ATTRIBUTE_UNUSED, | 364 exsltCryptoGcryptHash (xmlXPathParserContextPtr ctxt ATTRIBUTE_UNUSED, |
365 /* changed the enum to int */ | 365 /* changed the enum to int */ |
366 int algorithm, const char *msg, | 366 int algorithm, const char *msg, |
367 unsigned long msglen, | 367 unsigned long msglen, |
368 char dest[HASH_DIGEST_LENGTH]) { | 368 char dest[HASH_DIGEST_LENGTH]) { |
369 exsltCryptoGcryptInit (); | 369 exsltCryptoGcryptInit (); |
370 gcry_md_hash_buffer (algorithm, dest, msg, msglen); | 370 gcry_md_hash_buffer (algorithm, dest, msg, msglen); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 | 450 |
451 #endif /* defined(HAVE_GCRYPT) */ | 451 #endif /* defined(HAVE_GCRYPT) */ |
452 | 452 |
453 #if defined(HAVE_CRYPTO) | 453 #if defined(HAVE_CRYPTO) |
454 | 454 |
455 /** | 455 /** |
456 * exsltCryptoPopString: | 456 * exsltCryptoPopString: |
457 * @ctxt: an XPath parser context | 457 * @ctxt: an XPath parser context |
458 * @nargs: the number of arguments | 458 * @nargs: the number of arguments |
459 * | 459 * |
460 * Helper function which checks for and returns first string argument and its le
ngth | 460 * Helper function which checks for and returns first string argument and its |
| 461 * length in bytes. |
461 */ | 462 */ |
462 static int | 463 static int |
463 exsltCryptoPopString (xmlXPathParserContextPtr ctxt, int nargs, | 464 exsltCryptoPopString (xmlXPathParserContextPtr ctxt, int nargs, |
464 xmlChar ** str) { | 465 xmlChar ** str) { |
465 | 466 |
466 int str_len = 0; | 467 int str_len = 0; |
467 | 468 |
468 if ((nargs < 1) || (nargs > 2)) { | 469 if ((nargs < 1) || (nargs > 2)) { |
469 xmlXPathSetArityError (ctxt); | 470 xmlXPathSetArityError (ctxt); |
470 return 0; | 471 return 0; |
471 } | 472 } |
472 | 473 |
473 *str = xmlXPathPopString (ctxt); | 474 *str = xmlXPathPopString (ctxt); |
474 str_len = xmlUTF8Strlen (*str); | 475 str_len = xmlStrlen (*str); |
475 | 476 |
476 if (str_len == 0) { | 477 if (str_len == 0) { |
477 xmlXPathReturnEmptyString (ctxt); | 478 xmlXPathReturnEmptyString (ctxt); |
478 xmlFree (*str); | 479 xmlFree (*str); |
479 return 0; | 480 return 0; |
480 } | 481 } |
481 | 482 |
482 return str_len; | 483 return str_len; |
483 } | 484 } |
484 | 485 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 /** | 585 /** |
585 * exsltCryptoRc4EncryptFunction: | 586 * exsltCryptoRc4EncryptFunction: |
586 * @ctxt: an XPath parser context | 587 * @ctxt: an XPath parser context |
587 * @nargs: the number of arguments | 588 * @nargs: the number of arguments |
588 * | 589 * |
589 * computes the sha1 hash of a string and returns as hex | 590 * computes the sha1 hash of a string and returns as hex |
590 */ | 591 */ |
591 static void | 592 static void |
592 exsltCryptoRc4EncryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { | 593 exsltCryptoRc4EncryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
593 | 594 |
594 int key_len = 0, key_size = 0; | 595 int key_len = 0; |
595 int str_len = 0, bin_len = 0, hex_len = 0; | 596 int str_len = 0, bin_len = 0, hex_len = 0; |
596 xmlChar *key = NULL, *str = NULL, *padkey = NULL; | 597 xmlChar *key = NULL, *str = NULL, *padkey = NULL; |
597 xmlChar *bin = NULL, *hex = NULL; | 598 xmlChar *bin = NULL, *hex = NULL; |
598 xsltTransformContextPtr tctxt = NULL; | 599 xsltTransformContextPtr tctxt = NULL; |
599 | 600 |
600 if (nargs != 2) { | 601 if (nargs != 2) { |
601 xmlXPathSetArityError (ctxt); | 602 xmlXPathSetArityError (ctxt); |
602 return; | 603 return; |
603 } | 604 } |
604 tctxt = xsltXPathGetTransformContext(ctxt); | 605 tctxt = xsltXPathGetTransformContext(ctxt); |
605 | 606 |
606 str = xmlXPathPopString (ctxt); | 607 str = xmlXPathPopString (ctxt); |
607 str_len = xmlUTF8Strlen (str); | 608 str_len = xmlStrlen (str); |
608 | 609 |
609 if (str_len == 0) { | 610 if (str_len == 0) { |
610 xmlXPathReturnEmptyString (ctxt); | 611 xmlXPathReturnEmptyString (ctxt); |
611 xmlFree (str); | 612 xmlFree (str); |
612 return; | 613 return; |
613 } | 614 } |
614 | 615 |
615 key = xmlXPathPopString (ctxt); | 616 key = xmlXPathPopString (ctxt); |
616 key_len = xmlUTF8Strlen (key); | 617 key_len = xmlStrlen (key); |
617 | 618 |
618 if (key_len == 0) { | 619 if (key_len == 0) { |
619 xmlXPathReturnEmptyString (ctxt); | 620 xmlXPathReturnEmptyString (ctxt); |
620 xmlFree (key); | 621 xmlFree (key); |
621 xmlFree (str); | 622 xmlFree (str); |
622 return; | 623 return; |
623 } | 624 } |
624 | 625 |
625 padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1); | 626 padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1); |
626 if (padkey == NULL) { | 627 if (padkey == NULL) { |
627 xsltTransformError(tctxt, NULL, tctxt->inst, | 628 xsltTransformError(tctxt, NULL, tctxt->inst, |
628 "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n"); | 629 "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n"); |
629 tctxt->state = XSLT_STATE_STOPPED; | 630 tctxt->state = XSLT_STATE_STOPPED; |
630 xmlXPathReturnEmptyString (ctxt); | 631 xmlXPathReturnEmptyString (ctxt); |
631 goto done; | 632 goto done; |
632 } | 633 } |
633 memset(padkey, 0, RC4_KEY_LENGTH + 1); | 634 memset(padkey, 0, RC4_KEY_LENGTH + 1); |
634 | 635 |
635 key_size = xmlUTF8Strsize (key, key_len); | 636 if ((key_len > RC4_KEY_LENGTH) || (key_len < 0)) { |
636 if ((key_size > RC4_KEY_LENGTH) || (key_size < 0)) { | |
637 xsltTransformError(tctxt, NULL, tctxt->inst, | 637 xsltTransformError(tctxt, NULL, tctxt->inst, |
638 "exsltCryptoRc4EncryptFunction: key size too long or key broken\n"); | 638 "exsltCryptoRc4EncryptFunction: key size too long or key broken\n"); |
639 tctxt->state = XSLT_STATE_STOPPED; | 639 tctxt->state = XSLT_STATE_STOPPED; |
640 xmlXPathReturnEmptyString (ctxt); | 640 xmlXPathReturnEmptyString (ctxt); |
641 goto done; | 641 goto done; |
642 } | 642 } |
643 memcpy (padkey, key, key_size); | 643 memcpy (padkey, key, key_len); |
644 | 644 |
645 /* encrypt it */ | 645 /* encrypt it */ |
646 bin_len = str_len; | 646 bin_len = str_len; |
647 bin = xmlStrdup (str); | 647 bin = xmlStrdup (str); |
648 if (bin == NULL) { | 648 if (bin == NULL) { |
649 xsltTransformError(tctxt, NULL, tctxt->inst, | 649 xsltTransformError(tctxt, NULL, tctxt->inst, |
650 "exsltCryptoRc4EncryptFunction: Failed to allocate string\n"); | 650 "exsltCryptoRc4EncryptFunction: Failed to allocate string\n"); |
651 tctxt->state = XSLT_STATE_STOPPED; | 651 tctxt->state = XSLT_STATE_STOPPED; |
652 xmlXPathReturnEmptyString (ctxt); | 652 xmlXPathReturnEmptyString (ctxt); |
653 goto done; | 653 goto done; |
(...skipping 28 matching lines...) Expand all Loading... |
682 /** | 682 /** |
683 * exsltCryptoRc4DecryptFunction: | 683 * exsltCryptoRc4DecryptFunction: |
684 * @ctxt: an XPath parser context | 684 * @ctxt: an XPath parser context |
685 * @nargs: the number of arguments | 685 * @nargs: the number of arguments |
686 * | 686 * |
687 * computes the sha1 hash of a string and returns as hex | 687 * computes the sha1 hash of a string and returns as hex |
688 */ | 688 */ |
689 static void | 689 static void |
690 exsltCryptoRc4DecryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { | 690 exsltCryptoRc4DecryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
691 | 691 |
692 int key_len = 0, key_size = 0; | 692 int key_len = 0; |
693 int str_len = 0, bin_len = 0, ret_len = 0; | 693 int str_len = 0, bin_len = 0, ret_len = 0; |
694 xmlChar *key = NULL, *str = NULL, *padkey = NULL, *bin = | 694 xmlChar *key = NULL, *str = NULL, *padkey = NULL, *bin = |
695 NULL, *ret = NULL; | 695 NULL, *ret = NULL; |
696 xsltTransformContextPtr tctxt = NULL; | 696 xsltTransformContextPtr tctxt = NULL; |
697 | 697 |
698 if (nargs != 2) { | 698 if (nargs != 2) { |
699 xmlXPathSetArityError (ctxt); | 699 xmlXPathSetArityError (ctxt); |
700 return; | 700 return; |
701 } | 701 } |
702 tctxt = xsltXPathGetTransformContext(ctxt); | 702 tctxt = xsltXPathGetTransformContext(ctxt); |
703 | 703 |
704 str = xmlXPathPopString (ctxt); | 704 str = xmlXPathPopString (ctxt); |
705 str_len = xmlUTF8Strlen (str); | 705 str_len = xmlStrlen (str); |
706 | 706 |
707 if (str_len == 0) { | 707 if (str_len == 0) { |
708 xmlXPathReturnEmptyString (ctxt); | 708 xmlXPathReturnEmptyString (ctxt); |
709 xmlFree (str); | 709 xmlFree (str); |
710 return; | 710 return; |
711 } | 711 } |
712 | 712 |
713 key = xmlXPathPopString (ctxt); | 713 key = xmlXPathPopString (ctxt); |
714 key_len = xmlUTF8Strlen (key); | 714 key_len = xmlStrlen (key); |
715 | 715 |
716 if (key_len == 0) { | 716 if (key_len == 0) { |
717 xmlXPathReturnEmptyString (ctxt); | 717 xmlXPathReturnEmptyString (ctxt); |
718 xmlFree (key); | 718 xmlFree (key); |
719 xmlFree (str); | 719 xmlFree (str); |
720 return; | 720 return; |
721 } | 721 } |
722 | 722 |
723 padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1); | 723 padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1); |
724 if (padkey == NULL) { | 724 if (padkey == NULL) { |
725 xsltTransformError(tctxt, NULL, tctxt->inst, | 725 xsltTransformError(tctxt, NULL, tctxt->inst, |
726 "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n"); | 726 "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n"); |
727 tctxt->state = XSLT_STATE_STOPPED; | 727 tctxt->state = XSLT_STATE_STOPPED; |
728 xmlXPathReturnEmptyString (ctxt); | 728 xmlXPathReturnEmptyString (ctxt); |
729 goto done; | 729 goto done; |
730 } | 730 } |
731 memset(padkey, 0, RC4_KEY_LENGTH + 1); | 731 memset(padkey, 0, RC4_KEY_LENGTH + 1); |
732 key_size = xmlUTF8Strsize (key, key_len); | 732 if ((key_len > RC4_KEY_LENGTH) || (key_len < 0)) { |
733 if ((key_size > RC4_KEY_LENGTH) || (key_size < 0)) { | |
734 xsltTransformError(tctxt, NULL, tctxt->inst, | 733 xsltTransformError(tctxt, NULL, tctxt->inst, |
735 "exsltCryptoRc4EncryptFunction: key size too long or key broken\n"); | 734 "exsltCryptoRc4EncryptFunction: key size too long or key broken\n"); |
736 tctxt->state = XSLT_STATE_STOPPED; | 735 tctxt->state = XSLT_STATE_STOPPED; |
737 xmlXPathReturnEmptyString (ctxt); | 736 xmlXPathReturnEmptyString (ctxt); |
738 goto done; | 737 goto done; |
739 } | 738 } |
740 memcpy (padkey, key, key_size); | 739 memcpy (padkey, key, key_len); |
741 | 740 |
742 /* decode hex to binary */ | 741 /* decode hex to binary */ |
743 bin_len = str_len; | 742 bin_len = str_len; |
744 bin = xmlMallocAtomic (bin_len); | 743 bin = xmlMallocAtomic (bin_len); |
745 if (bin == NULL) { | 744 if (bin == NULL) { |
746 xsltTransformError(tctxt, NULL, tctxt->inst, | 745 xsltTransformError(tctxt, NULL, tctxt->inst, |
747 "exsltCryptoRc4EncryptFunction: Failed to allocate string\n"); | 746 "exsltCryptoRc4EncryptFunction: Failed to allocate string\n"); |
748 tctxt->state = XSLT_STATE_STOPPED; | 747 tctxt->state = XSLT_STATE_STOPPED; |
749 xmlXPathReturnEmptyString (ctxt); | 748 xmlXPathReturnEmptyString (ctxt); |
750 goto done; | 749 goto done; |
751 } | 750 } |
752 ret_len = exsltCryptoHex2Bin (str, str_len, bin, bin_len); | 751 ret_len = exsltCryptoHex2Bin (str, str_len, bin, bin_len); |
753 | 752 |
754 /* decrypt the binary blob */ | 753 /* decrypt the binary blob */ |
755 ret = xmlMallocAtomic (ret_len); | 754 ret = xmlMallocAtomic (ret_len + 1); |
756 if (ret == NULL) { | 755 if (ret == NULL) { |
757 xsltTransformError(tctxt, NULL, tctxt->inst, | 756 xsltTransformError(tctxt, NULL, tctxt->inst, |
758 "exsltCryptoRc4EncryptFunction: Failed to allocate result\n"); | 757 "exsltCryptoRc4EncryptFunction: Failed to allocate result\n"); |
759 tctxt->state = XSLT_STATE_STOPPED; | 758 tctxt->state = XSLT_STATE_STOPPED; |
760 xmlXPathReturnEmptyString (ctxt); | 759 xmlXPathReturnEmptyString (ctxt); |
761 goto done; | 760 goto done; |
762 } | 761 } |
763 PLATFORM_RC4_DECRYPT (ctxt, padkey, bin, ret_len, ret, ret_len); | 762 PLATFORM_RC4_DECRYPT (ctxt, padkey, bin, ret_len, ret, ret_len); |
| 763 ret[ret_len] = 0; |
764 | 764 |
765 xmlXPathReturnString (ctxt, ret); | 765 xmlXPathReturnString (ctxt, ret); |
766 | 766 |
767 done: | 767 done: |
768 if (key != NULL) | 768 if (key != NULL) |
769 xmlFree (key); | 769 xmlFree (key); |
770 if (str != NULL) | 770 if (str != NULL) |
771 xmlFree (str); | 771 xmlFree (str); |
772 if (padkey != NULL) | 772 if (padkey != NULL) |
773 xmlFree (padkey); | 773 xmlFree (padkey); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 * | 806 * |
807 * Registers the EXSLT - Crypto module | 807 * Registers the EXSLT - Crypto module |
808 */ | 808 */ |
809 void | 809 void |
810 exsltCryptoRegister (void) { | 810 exsltCryptoRegister (void) { |
811 } | 811 } |
812 | 812 |
813 #endif /* defined(HAVE_CRYPTO) */ | 813 #endif /* defined(HAVE_CRYPTO) */ |
814 | 814 |
815 #endif /* EXSLT_CRYPTO_ENABLED */ | 815 #endif /* EXSLT_CRYPTO_ENABLED */ |
OLD | NEW |