| 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> |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, | 133 FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, |
| 134 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), | 134 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 135 (LPTSTR) & lpMsgBuf, 0, NULL); | 135 (LPTSTR) & lpMsgBuf, 0, NULL); |
| 136 | 136 |
| 137 xsltTransformError (xsltXPathGetTransformContext (ctxt), NULL, NULL, | 137 xsltTransformError (xsltXPathGetTransformContext (ctxt), NULL, NULL, |
| 138 "exslt:crypto error (line %d). %s", line, | 138 "exslt:crypto error (line %d). %s", line, |
| 139 lpMsgBuf); | 139 lpMsgBuf); |
| 140 LocalFree (lpMsgBuf); | 140 LocalFree (lpMsgBuf); |
| 141 } | 141 } |
| 142 | 142 |
| 143 HCRYPTHASH | 143 static HCRYPTHASH |
| 144 exsltCryptoCryptoApiCreateHash (xmlXPathParserContextPtr ctxt, | 144 exsltCryptoCryptoApiCreateHash (xmlXPathParserContextPtr ctxt, |
| 145 HCRYPTPROV hCryptProv, ALG_ID algorithm, | 145 HCRYPTPROV hCryptProv, ALG_ID algorithm, |
| 146 const char *msg, unsigned int msglen, | 146 const char *msg, unsigned int msglen, |
| 147 char *dest, unsigned int destlen) | 147 char *dest, unsigned int destlen) |
| 148 { | 148 { |
| 149 HCRYPTHASH hHash = 0; | 149 HCRYPTHASH hHash = 0; |
| 150 DWORD dwHashLen = destlen; | 150 DWORD dwHashLen = destlen; |
| 151 | 151 |
| 152 if (!CryptCreateHash (hCryptProv, algorithm, 0, 0, &hHash)) { | 152 if (!CryptCreateHash (hCryptProv, algorithm, 0, 0, &hHash)) { |
| 153 exsltCryptoCryptoApiReportError (ctxt, __LINE__); | 153 exsltCryptoCryptoApiReportError (ctxt, __LINE__); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 hHash = exsltCryptoCryptoApiCreateHash (ctxt, hCryptProv, | 196 hHash = exsltCryptoCryptoApiCreateHash (ctxt, hCryptProv, |
| 197 algorithm, msg, msglen, | 197 algorithm, msg, msglen, |
| 198 dest, HASH_DIGEST_LENGTH); | 198 dest, HASH_DIGEST_LENGTH); |
| 199 if (0 != hHash) { | 199 if (0 != hHash) { |
| 200 CryptDestroyHash (hHash); | 200 CryptDestroyHash (hHash); |
| 201 } | 201 } |
| 202 | 202 |
| 203 CryptReleaseContext (hCryptProv, 0); | 203 CryptReleaseContext (hCryptProv, 0); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void | 206 static void |
| 207 exsltCryptoCryptoApiRc4Encrypt (xmlXPathParserContextPtr ctxt, | 207 exsltCryptoCryptoApiRc4Encrypt (xmlXPathParserContextPtr ctxt, |
| 208 const unsigned char *key, | 208 const unsigned char *key, |
| 209 const unsigned char *msg, int msglen, | 209 const unsigned char *msg, int msglen, |
| 210 unsigned char *dest, int destlen) { | 210 unsigned char *dest, int destlen) { |
| 211 HCRYPTPROV hCryptProv; | 211 HCRYPTPROV hCryptProv; |
| 212 HCRYPTKEY hKey; | 212 HCRYPTKEY hKey; |
| 213 HCRYPTHASH hHash; | 213 HCRYPTHASH hHash; |
| 214 DWORD dwDataLen; | 214 DWORD dwDataLen; |
| 215 unsigned char hash[HASH_DIGEST_LENGTH]; | 215 unsigned char hash[HASH_DIGEST_LENGTH]; |
| 216 | 216 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 247 | 247 |
| 248 fail: | 248 fail: |
| 249 if (0 != hHash) { | 249 if (0 != hHash) { |
| 250 CryptDestroyHash (hHash); | 250 CryptDestroyHash (hHash); |
| 251 } | 251 } |
| 252 | 252 |
| 253 CryptDestroyKey (hKey); | 253 CryptDestroyKey (hKey); |
| 254 CryptReleaseContext (hCryptProv, 0); | 254 CryptReleaseContext (hCryptProv, 0); |
| 255 } | 255 } |
| 256 | 256 |
| 257 void | 257 static void |
| 258 exsltCryptoCryptoApiRc4Decrypt (xmlXPathParserContextPtr ctxt, | 258 exsltCryptoCryptoApiRc4Decrypt (xmlXPathParserContextPtr ctxt, |
| 259 const unsigned char *key, | 259 const unsigned char *key, |
| 260 const unsigned char *msg, int msglen, | 260 const unsigned char *msg, int msglen, |
| 261 unsigned char *dest, int destlen) { | 261 unsigned char *dest, int destlen) { |
| 262 HCRYPTPROV hCryptProv; | 262 HCRYPTPROV hCryptProv; |
| 263 HCRYPTKEY hKey; | 263 HCRYPTKEY hKey; |
| 264 HCRYPTHASH hHash; | 264 HCRYPTHASH hHash; |
| 265 DWORD dwDataLen; | 265 DWORD dwDataLen; |
| 266 unsigned char hash[HASH_DIGEST_LENGTH]; | 266 unsigned char hash[HASH_DIGEST_LENGTH]; |
| 267 | 267 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 #if defined(HAVE_GCRYPT) | 310 #if defined(HAVE_GCRYPT) |
| 311 | 311 |
| 312 #define HAVE_CRYPTO | 312 #define HAVE_CRYPTO |
| 313 #define PLATFORM_HASH exsltCryptoGcryptHash | 313 #define PLATFORM_HASH exsltCryptoGcryptHash |
| 314 #define PLATFORM_RC4_ENCRYPT exsltCryptoGcryptRc4Encrypt | 314 #define PLATFORM_RC4_ENCRYPT exsltCryptoGcryptRc4Encrypt |
| 315 #define PLATFORM_RC4_DECRYPT exsltCryptoGcryptRc4Decrypt | 315 #define PLATFORM_RC4_DECRYPT exsltCryptoGcryptRc4Decrypt |
| 316 #define PLATFORM_MD4 GCRY_MD_MD4 | 316 #define PLATFORM_MD4 GCRY_MD_MD4 |
| 317 #define PLATFORM_MD5 GCRY_MD_MD5 | 317 #define PLATFORM_MD5 GCRY_MD_MD5 |
| 318 #define PLATFORM_SHA1 GCRY_MD_SHA1 | 318 #define PLATFORM_SHA1 GCRY_MD_SHA1 |
| 319 | 319 |
| 320 #ifdef HAVE_SYS_TYPES_H | 320 #ifdef HAVE_SYS_TYPES_H |
| 321 # include <sys/types.h> | 321 # include <sys/types.h> |
| 322 #endif | 322 #endif |
| 323 #ifdef HAVE_STDINT_H | 323 #ifdef HAVE_STDINT_H |
| 324 # include <stdint.h> | 324 # include <stdint.h> |
| 325 #endif | 325 #endif |
| 326 | 326 |
| 327 #ifdef HAVE_SYS_SELECT_H | 327 #ifdef HAVE_SYS_SELECT_H |
| 328 #include <sys/select.h> /* needed by gcrypt.h 4 Jul 04 */ | 328 #include <sys/select.h> /* needed by gcrypt.h 4 Jul 04 */ |
| 329 #endif | 329 #endif |
| 330 #include <gcrypt.h> | 330 #include <gcrypt.h> |
| 331 | 331 |
| 332 static void | 332 static void |
| 333 exsltCryptoGcryptInit (void) { | 333 exsltCryptoGcryptInit (void) { |
| 334 static int gcrypt_init; | 334 static int gcrypt_init; |
| 335 xmlLockLibrary (); | 335 xmlLockLibrary (); |
| 336 | 336 |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 * | 588 * |
| 589 * computes the sha1 hash of a string and returns as hex | 589 * computes the sha1 hash of a string and returns as hex |
| 590 */ | 590 */ |
| 591 static void | 591 static void |
| 592 exsltCryptoRc4EncryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { | 592 exsltCryptoRc4EncryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
| 593 | 593 |
| 594 int key_len = 0, key_size = 0; | 594 int key_len = 0, key_size = 0; |
| 595 int str_len = 0, bin_len = 0, hex_len = 0; | 595 int str_len = 0, bin_len = 0, hex_len = 0; |
| 596 xmlChar *key = NULL, *str = NULL, *padkey = NULL; | 596 xmlChar *key = NULL, *str = NULL, *padkey = NULL; |
| 597 xmlChar *bin = NULL, *hex = NULL; | 597 xmlChar *bin = NULL, *hex = NULL; |
| 598 xsltTransformContextPtr tctxt = NULL; |
| 598 | 599 |
| 599 if ((nargs < 1) || (nargs > 3)) { | 600 if (nargs != 2) { |
| 600 xmlXPathSetArityError (ctxt); | 601 xmlXPathSetArityError (ctxt); |
| 601 return; | 602 return; |
| 602 } | 603 } |
| 604 tctxt = xsltXPathGetTransformContext(ctxt); |
| 603 | 605 |
| 604 str = xmlXPathPopString (ctxt); | 606 str = xmlXPathPopString (ctxt); |
| 605 str_len = xmlUTF8Strlen (str); | 607 str_len = xmlUTF8Strlen (str); |
| 606 | 608 |
| 607 if (str_len == 0) { | 609 if (str_len == 0) { |
| 608 xmlXPathReturnEmptyString (ctxt); | 610 xmlXPathReturnEmptyString (ctxt); |
| 609 xmlFree (str); | 611 xmlFree (str); |
| 610 return; | 612 return; |
| 611 } | 613 } |
| 612 | 614 |
| 613 key = xmlXPathPopString (ctxt); | 615 key = xmlXPathPopString (ctxt); |
| 614 key_len = xmlUTF8Strlen (str); | 616 key_len = xmlUTF8Strlen (key); |
| 615 | 617 |
| 616 if (key_len == 0) { | 618 if (key_len == 0) { |
| 617 xmlXPathReturnEmptyString (ctxt); | 619 xmlXPathReturnEmptyString (ctxt); |
| 618 xmlFree (key); | 620 xmlFree (key); |
| 619 xmlFree (str); | 621 xmlFree (str); |
| 620 return; | 622 return; |
| 621 } | 623 } |
| 622 | 624 |
| 623 padkey = xmlMallocAtomic (RC4_KEY_LENGTH); | 625 padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1); |
| 626 if (padkey == NULL) { |
| 627 » xsltTransformError(tctxt, NULL, tctxt->inst, |
| 628 » "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n"); |
| 629 » tctxt->state = XSLT_STATE_STOPPED; |
| 630 » xmlXPathReturnEmptyString (ctxt); |
| 631 » goto done; |
| 632 } |
| 633 memset(padkey, 0, RC4_KEY_LENGTH + 1); |
| 634 |
| 624 key_size = xmlUTF8Strsize (key, key_len); | 635 key_size = xmlUTF8Strsize (key, key_len); |
| 636 if ((key_size > RC4_KEY_LENGTH) || (key_size < 0)) { |
| 637 xsltTransformError(tctxt, NULL, tctxt->inst, |
| 638 "exsltCryptoRc4EncryptFunction: key size too long or key broken\n"); |
| 639 tctxt->state = XSLT_STATE_STOPPED; |
| 640 xmlXPathReturnEmptyString (ctxt); |
| 641 goto done; |
| 642 } |
| 625 memcpy (padkey, key, key_size); | 643 memcpy (padkey, key, key_size); |
| 626 memset (padkey + key_size, '\0', sizeof (padkey)); | |
| 627 | 644 |
| 628 /* encrypt it */ | 645 /* encrypt it */ |
| 629 bin_len = str_len; | 646 bin_len = str_len; |
| 630 bin = xmlStrdup (str); | 647 bin = xmlStrdup (str); |
| 631 if (bin == NULL) { | 648 if (bin == NULL) { |
| 649 xsltTransformError(tctxt, NULL, tctxt->inst, |
| 650 "exsltCryptoRc4EncryptFunction: Failed to allocate string\n"); |
| 651 tctxt->state = XSLT_STATE_STOPPED; |
| 632 xmlXPathReturnEmptyString (ctxt); | 652 xmlXPathReturnEmptyString (ctxt); |
| 633 goto done; | 653 goto done; |
| 634 } | 654 } |
| 635 PLATFORM_RC4_ENCRYPT (ctxt, padkey, str, str_len, bin, bin_len); | 655 PLATFORM_RC4_ENCRYPT (ctxt, padkey, str, str_len, bin, bin_len); |
| 636 | 656 |
| 637 /* encode it */ | 657 /* encode it */ |
| 638 hex_len = str_len * 2 + 1; | 658 hex_len = str_len * 2 + 1; |
| 639 hex = xmlMallocAtomic (hex_len); | 659 hex = xmlMallocAtomic (hex_len); |
| 640 if (hex == NULL) { | 660 if (hex == NULL) { |
| 661 xsltTransformError(tctxt, NULL, tctxt->inst, |
| 662 "exsltCryptoRc4EncryptFunction: Failed to allocate result\n"); |
| 663 tctxt->state = XSLT_STATE_STOPPED; |
| 641 xmlXPathReturnEmptyString (ctxt); | 664 xmlXPathReturnEmptyString (ctxt); |
| 642 goto done; | 665 goto done; |
| 643 } | 666 } |
| 644 | 667 |
| 645 exsltCryptoBin2Hex (bin, str_len, hex, hex_len); | 668 exsltCryptoBin2Hex (bin, str_len, hex, hex_len); |
| 646 xmlXPathReturnString (ctxt, hex); | 669 xmlXPathReturnString (ctxt, hex); |
| 647 | 670 |
| 648 done: | 671 done: |
| 649 if (key != NULL) | 672 if (key != NULL) |
| 650 xmlFree (key); | 673 xmlFree (key); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 663 * | 686 * |
| 664 * computes the sha1 hash of a string and returns as hex | 687 * computes the sha1 hash of a string and returns as hex |
| 665 */ | 688 */ |
| 666 static void | 689 static void |
| 667 exsltCryptoRc4DecryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { | 690 exsltCryptoRc4DecryptFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
| 668 | 691 |
| 669 int key_len = 0, key_size = 0; | 692 int key_len = 0, key_size = 0; |
| 670 int str_len = 0, bin_len = 0, ret_len = 0; | 693 int str_len = 0, bin_len = 0, ret_len = 0; |
| 671 xmlChar *key = NULL, *str = NULL, *padkey = NULL, *bin = | 694 xmlChar *key = NULL, *str = NULL, *padkey = NULL, *bin = |
| 672 NULL, *ret = NULL; | 695 NULL, *ret = NULL; |
| 696 xsltTransformContextPtr tctxt = NULL; |
| 673 | 697 |
| 674 if ((nargs < 1) || (nargs > 3)) { | 698 if (nargs != 2) { |
| 675 xmlXPathSetArityError (ctxt); | 699 xmlXPathSetArityError (ctxt); |
| 676 return; | 700 return; |
| 677 } | 701 } |
| 702 tctxt = xsltXPathGetTransformContext(ctxt); |
| 678 | 703 |
| 679 str = xmlXPathPopString (ctxt); | 704 str = xmlXPathPopString (ctxt); |
| 680 str_len = xmlUTF8Strlen (str); | 705 str_len = xmlUTF8Strlen (str); |
| 681 | 706 |
| 682 if (str_len == 0) { | 707 if (str_len == 0) { |
| 683 xmlXPathReturnEmptyString (ctxt); | 708 xmlXPathReturnEmptyString (ctxt); |
| 684 xmlFree (str); | 709 xmlFree (str); |
| 685 return; | 710 return; |
| 686 } | 711 } |
| 687 | 712 |
| 688 key = xmlXPathPopString (ctxt); | 713 key = xmlXPathPopString (ctxt); |
| 689 key_len = xmlUTF8Strlen (str); | 714 key_len = xmlUTF8Strlen (key); |
| 690 | 715 |
| 691 if (key_len == 0) { | 716 if (key_len == 0) { |
| 692 xmlXPathReturnEmptyString (ctxt); | 717 xmlXPathReturnEmptyString (ctxt); |
| 693 xmlFree (key); | 718 xmlFree (key); |
| 694 xmlFree (str); | 719 xmlFree (str); |
| 695 return; | 720 return; |
| 696 } | 721 } |
| 697 | 722 |
| 698 padkey = xmlMallocAtomic (RC4_KEY_LENGTH); | 723 padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1); |
| 724 if (padkey == NULL) { |
| 725 » xsltTransformError(tctxt, NULL, tctxt->inst, |
| 726 » "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n"); |
| 727 » tctxt->state = XSLT_STATE_STOPPED; |
| 728 » xmlXPathReturnEmptyString (ctxt); |
| 729 » goto done; |
| 730 } |
| 731 memset(padkey, 0, RC4_KEY_LENGTH + 1); |
| 699 key_size = xmlUTF8Strsize (key, key_len); | 732 key_size = xmlUTF8Strsize (key, key_len); |
| 733 if ((key_size > RC4_KEY_LENGTH) || (key_size < 0)) { |
| 734 xsltTransformError(tctxt, NULL, tctxt->inst, |
| 735 "exsltCryptoRc4EncryptFunction: key size too long or key broken\n"); |
| 736 tctxt->state = XSLT_STATE_STOPPED; |
| 737 xmlXPathReturnEmptyString (ctxt); |
| 738 goto done; |
| 739 } |
| 700 memcpy (padkey, key, key_size); | 740 memcpy (padkey, key, key_size); |
| 701 memset (padkey + key_size, '\0', sizeof (padkey)); | |
| 702 | 741 |
| 703 /* decode hex to binary */ | 742 /* decode hex to binary */ |
| 704 bin_len = str_len; | 743 bin_len = str_len; |
| 705 bin = xmlMallocAtomic (bin_len); | 744 bin = xmlMallocAtomic (bin_len); |
| 745 if (bin == NULL) { |
| 746 xsltTransformError(tctxt, NULL, tctxt->inst, |
| 747 "exsltCryptoRc4EncryptFunction: Failed to allocate string\n"); |
| 748 tctxt->state = XSLT_STATE_STOPPED; |
| 749 xmlXPathReturnEmptyString (ctxt); |
| 750 goto done; |
| 751 } |
| 706 ret_len = exsltCryptoHex2Bin (str, str_len, bin, bin_len); | 752 ret_len = exsltCryptoHex2Bin (str, str_len, bin, bin_len); |
| 707 | 753 |
| 708 /* decrypt the binary blob */ | 754 /* decrypt the binary blob */ |
| 709 ret = xmlMallocAtomic (ret_len); | 755 ret = xmlMallocAtomic (ret_len); |
| 756 if (ret == NULL) { |
| 757 xsltTransformError(tctxt, NULL, tctxt->inst, |
| 758 "exsltCryptoRc4EncryptFunction: Failed to allocate result\n"); |
| 759 tctxt->state = XSLT_STATE_STOPPED; |
| 760 xmlXPathReturnEmptyString (ctxt); |
| 761 goto done; |
| 762 } |
| 710 PLATFORM_RC4_DECRYPT (ctxt, padkey, bin, ret_len, ret, ret_len); | 763 PLATFORM_RC4_DECRYPT (ctxt, padkey, bin, ret_len, ret, ret_len); |
| 711 | 764 |
| 712 xmlXPathReturnString (ctxt, ret); | 765 xmlXPathReturnString (ctxt, ret); |
| 713 | 766 |
| 767 done: |
| 714 if (key != NULL) | 768 if (key != NULL) |
| 715 xmlFree (key); | 769 xmlFree (key); |
| 716 if (str != NULL) | 770 if (str != NULL) |
| 717 xmlFree (str); | 771 xmlFree (str); |
| 718 if (padkey != NULL) | 772 if (padkey != NULL) |
| 719 xmlFree (padkey); | 773 xmlFree (padkey); |
| 720 if (bin != NULL) | 774 if (bin != NULL) |
| 721 xmlFree (bin); | 775 xmlFree (bin); |
| 722 } | 776 } |
| 723 | 777 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 740 exsltCryptoSha1Function); | 794 exsltCryptoSha1Function); |
| 741 xsltRegisterExtModuleFunction ((const xmlChar *) "rc4_encrypt", | 795 xsltRegisterExtModuleFunction ((const xmlChar *) "rc4_encrypt", |
| 742 EXSLT_CRYPTO_NAMESPACE, | 796 EXSLT_CRYPTO_NAMESPACE, |
| 743 exsltCryptoRc4EncryptFunction); | 797 exsltCryptoRc4EncryptFunction); |
| 744 xsltRegisterExtModuleFunction ((const xmlChar *) "rc4_decrypt", | 798 xsltRegisterExtModuleFunction ((const xmlChar *) "rc4_decrypt", |
| 745 EXSLT_CRYPTO_NAMESPACE, | 799 EXSLT_CRYPTO_NAMESPACE, |
| 746 exsltCryptoRc4DecryptFunction); | 800 exsltCryptoRc4DecryptFunction); |
| 747 } | 801 } |
| 748 | 802 |
| 749 #else | 803 #else |
| 804 /** |
| 805 * exsltCryptoRegister: |
| 806 * |
| 807 * Registers the EXSLT - Crypto module |
| 808 */ |
| 750 void | 809 void |
| 751 exsltCryptoRegister (void) { | 810 exsltCryptoRegister (void) { |
| 752 } | 811 } |
| 753 | 812 |
| 754 #endif /* defined(HAVE_CRYPTO) */ | 813 #endif /* defined(HAVE_CRYPTO) */ |
| 755 | 814 |
| 756 #endif /* EXSLT_CRYPTO_ENABLED */ | 815 #endif /* EXSLT_CRYPTO_ENABLED */ |
| OLD | NEW |