1 Index: libexslt/crypto.c
2 ===================================================================
3 --- libexslt/crypto.c (revision 1485)
4 +++ libexslt/crypto.c (working copy)
5 @@ -317,13 +317,13 @@ exsltCryptoCryptoApiRc4Decrypt (xmlXPath
6 #define PLATFORM_MD5 GCRY_MD_MD5
7 #define PLATFORM_SHA1 GCRY_MD_SHA1
9 -#ifdef HAVE_SYS_TYPES_H
10 -# include <sys/types.h>
16 +#ifdef HAVE_SYS_TYPES_H
17 +# include <sys/types.h>
23 #ifdef HAVE_SYS_SELECT_H
24 #include <sys/select.h> /* needed by gcrypt.h 4 Jul 04 */
26 @@ -595,11 +595,13 @@ exsltCryptoRc4EncryptFunction (xmlXPathP
27 int str_len = 0, bin_len = 0, hex_len = 0;
28 xmlChar *key = NULL, *str = NULL, *padkey = NULL;
29 xmlChar *bin = NULL, *hex = NULL;
30 + xsltTransformContextPtr tctxt = NULL;
32 - if ((nargs < 1) || (nargs > 3)) {
34 xmlXPathSetArityError (ctxt);
37 + tctxt = xsltXPathGetTransformContext(ctxt);
39 str = xmlXPathPopString (ctxt);
40 str_len = xmlUTF8Strlen (str);
41 @@ -611,7 +613,7 @@ exsltCryptoRc4EncryptFunction (xmlXPathP
44 key = xmlXPathPopString (ctxt);
45 - key_len = xmlUTF8Strlen (str);
46 + key_len = xmlUTF8Strlen (key);
49 xmlXPathReturnEmptyString (ctxt);
50 @@ -620,15 +622,33 @@ exsltCryptoRc4EncryptFunction (xmlXPathP
54 - padkey = xmlMallocAtomic (RC4_KEY_LENGTH);
55 + padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1);
56 + if (padkey == NULL) {
57 + xsltTransformError(tctxt, NULL, tctxt->inst,
58 + "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n");
59 + tctxt->state = XSLT_STATE_STOPPED;
60 + xmlXPathReturnEmptyString (ctxt);
63 + memset(padkey, 0, RC4_KEY_LENGTH + 1);
65 key_size = xmlUTF8Strsize (key, key_len);
66 + if ((key_size > RC4_KEY_LENGTH) || (key_size < 0)) {
67 + xsltTransformError(tctxt, NULL, tctxt->inst,
68 + "exsltCryptoRc4EncryptFunction: key size too long or key broken\n");
69 + tctxt->state = XSLT_STATE_STOPPED;
70 + xmlXPathReturnEmptyString (ctxt);
73 memcpy (padkey, key, key_size);
74 - memset (padkey + key_size, '\0', sizeof (padkey));
78 bin = xmlStrdup (str);
80 + xsltTransformError(tctxt, NULL, tctxt->inst,
81 + "exsltCryptoRc4EncryptFunction: Failed to allocate string\n");
82 + tctxt->state = XSLT_STATE_STOPPED;
83 xmlXPathReturnEmptyString (ctxt);
86 @@ -638,6 +658,9 @@ exsltCryptoRc4EncryptFunction (xmlXPathP
87 hex_len = str_len * 2 + 1;
88 hex = xmlMallocAtomic (hex_len);
90 + xsltTransformError(tctxt, NULL, tctxt->inst,
91 + "exsltCryptoRc4EncryptFunction: Failed to allocate result\n");
92 + tctxt->state = XSLT_STATE_STOPPED;
93 xmlXPathReturnEmptyString (ctxt);
96 @@ -670,11 +693,13 @@ exsltCryptoRc4DecryptFunction (xmlXPathP
97 int str_len = 0, bin_len = 0, ret_len = 0;
98 xmlChar *key = NULL, *str = NULL, *padkey = NULL, *bin =
100 + xsltTransformContextPtr tctxt = NULL;
102 - if ((nargs < 1) || (nargs > 3)) {
104 xmlXPathSetArityError (ctxt);
107 + tctxt = xsltXPathGetTransformContext(ctxt);
109 str = xmlXPathPopString (ctxt);
110 str_len = xmlUTF8Strlen (str);
111 @@ -686,7 +711,7 @@ exsltCryptoRc4DecryptFunction (xmlXPathP
114 key = xmlXPathPopString (ctxt);
115 - key_len = xmlUTF8Strlen (str);
116 + key_len = xmlUTF8Strlen (key);
119 xmlXPathReturnEmptyString (ctxt);
120 @@ -695,22 +720,51 @@ exsltCryptoRc4DecryptFunction (xmlXPathP
124 - padkey = xmlMallocAtomic (RC4_KEY_LENGTH);
125 + padkey = xmlMallocAtomic (RC4_KEY_LENGTH + 1);
126 + if (padkey == NULL) {
127 + xsltTransformError(tctxt, NULL, tctxt->inst,
128 + "exsltCryptoRc4EncryptFunction: Failed to allocate padkey\n");
129 + tctxt->state = XSLT_STATE_STOPPED;
130 + xmlXPathReturnEmptyString (ctxt);
133 + memset(padkey, 0, RC4_KEY_LENGTH + 1);
134 key_size = xmlUTF8Strsize (key, key_len);
135 + if ((key_size > RC4_KEY_LENGTH) || (key_size < 0)) {
136 + xsltTransformError(tctxt, NULL, tctxt->inst,
137 + "exsltCryptoRc4EncryptFunction: key size too long or key broken\n");
138 + tctxt->state = XSLT_STATE_STOPPED;
139 + xmlXPathReturnEmptyString (ctxt);
142 memcpy (padkey, key, key_size);
143 - memset (padkey + key_size, '\0', sizeof (padkey));
145 /* decode hex to binary */
147 bin = xmlMallocAtomic (bin_len);
149 + xsltTransformError(tctxt, NULL, tctxt->inst,
150 + "exsltCryptoRc4EncryptFunction: Failed to allocate string\n");
151 + tctxt->state = XSLT_STATE_STOPPED;
152 + xmlXPathReturnEmptyString (ctxt);
155 ret_len = exsltCryptoHex2Bin (str, str_len, bin, bin_len);
157 /* decrypt the binary blob */
158 ret = xmlMallocAtomic (ret_len);
160 + xsltTransformError(tctxt, NULL, tctxt->inst,
161 + "exsltCryptoRc4EncryptFunction: Failed to allocate result\n");
162 + tctxt->state = XSLT_STATE_STOPPED;
163 + xmlXPathReturnEmptyString (ctxt);
166 PLATFORM_RC4_DECRYPT (ctxt, padkey, bin, ret_len, ret, ret_len);
168 xmlXPathReturnString (ctxt, ret);