Re: [xslt] [PATCH]exslt crypto support done with dynamic library loading
- From: joel reed <joelwreed comcast net>
- To: The Gnome XSLT library mailing-list <xslt gnome org>
- Subject: Re: [xslt] [PATCH]exslt crypto support done with dynamic library loading
- Date: Fri, 28 May 2004 19:16:07 -0400
On Fri, May 28, 2004 at 05:04:51PM -0400, joel reed wrote:
> I reworked my exslt crypto namespace patch to dynamically load
> openssl to implement MD5 and SHA1 hashing like so:
a few notes on this patch i forgot to mention 1st posting:
1) i removed crypto:ripemd160
2) i don't _think_ anyone wanted dynamic module loading on Win32,
and my experience with compiling libltdl with MS Visual C++, leads
me to believe we should avoid this requirement if at all possible.
3) i tested the win32 crypto code separately, but not once i dropped
it into libexslt. i'll do that next week.
4) if there was an unregister or shutdown hook for extensions the
ltdl module loading init/teardown could be done much better i think.
jr
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:crypto="http://exslt.org/crypto"
> extension-element-prefixes="crypto">
>
> <xsl:template match="editor/plaintext">
> md5sum=<xsl:value-of select="crypto:md5sum(.)"/>
> sha1sum=<xsl:value-of select="crypto:sha1sum(.)"/>
> </xsl:template>
>
> </xsl:stylesheet>
>
> This patch uses ltdl per Aleksey's advice (http://mail.gnome.org/archives/xml/2004-May/msg00103.html).
> If it looks good, a libltdl directory would also need to be created
> and populated with the command "libtoolize --ltdl" per http://www.gnu.org/software/libtool/manual.html#TOC52
>
> finally, on win32, the patch uses CryptoApi, which _seems_ to be the
> best win32 solution.
>
> comments?
>
> jr
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/configure.in libxslt-1.1.7/configure.in
> --- libxslt-1.1.7-orig/configure.in 2004-05-17 00:00:53.000000000 -0400
> +++ libxslt-1.1.7/configure.in 2004-05-28 16:48:30.000000000 -0400
> @@ -69,7 +69,20 @@ AC_ISC_POSIX
> AC_PROG_CC
> AC_STDC_HEADERS
> AC_ARG_PROGRAM
> -AM_PROG_LIBTOOL
> +
> +dnl from http://www.gnu.org/software/libtool/manual.html#SEC45
> +dnl Enable building of the convenience library
> +dnl and set LIBLTDL accordingly
> +AC_LIBLTDL_INSTALLABLE
> +dnl Substitute LTDLINCL and LIBLTDL in the Makefiles
> +AC_SUBST(LTDLINCL)
> +AC_SUBST(LIBLTDL)
> +dnl Check for dlopen support
> +AC_LIBTOOL_DLOPEN
> +dnl Configure libtool
> +AC_PROG_LIBTOOL
> +dnl Configure libltdl
> +AC_CONFIG_SUBDIRS(libltdl)
>
> dnl
> dnl Math detection
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/libexslt/crypto.c libxslt-1.1.7/libexslt/crypto.c
> --- libxslt-1.1.7-orig/libexslt/crypto.c 1969-12-31 19:00:00.000000000 -0500
> +++ libxslt-1.1.7/libexslt/crypto.c 2004-05-28 16:48:30.000000000 -0400
> @@ -0,0 +1,253 @@
> +#define IN_LIBEXSLT
> +#include "libexslt/libexslt.h"
> +
> +#if defined(WIN32) && !defined (__CYGWIN__) && (!__MINGW32__)
> +#include <win32config.h>
> +#else
> +#include "config.h"
> +#endif
> +
> +#include <libxml/tree.h>
> +#include <libxml/xpath.h>
> +#include <libxml/xpathInternals.h>
> +#include <libxml/parser.h>
> +#include <libxml/encoding.h>
> +#include <libxml/uri.h>
> +
> +#include <libxslt/xsltconfig.h>
> +#include <libxslt/xsltutils.h>
> +#include <libxslt/xsltInternals.h>
> +#include <libxslt/extensions.h>
> +
> +#include "exslt.h"
> +
> +#define HASH_DIGEST_LENGTH 32
> +#define MD5_DIGEST_LENGTH 16
> +#define SHA1_DIGEST_LENGTH 20
> +
> +void exsltCryptoDigest2Hex(const unsigned char* hash, int hashlen,
> + unsigned char* hex, int hexlen)
> +{
> + static const char bin2hex[] = { '0', '1', '2', '3',
> + '4', '5', '6', '7',
> + '8', '9', 'a', 'b',
> + 'c', 'd', 'e', 'f' };
> +
> + unsigned char lo, hi;
> + int i, pos;
> + for (i = 0, pos = 0; (i < hashlen && pos < hexlen); i++)
> + {
> + lo = hash[i] & 0xf;
> + hi = hash[i] >> 4;
> + hex[pos++] = bin2hex[hi];
> + hex[pos++] = bin2hex[lo];
> + }
> +
> + hex[pos] = '\0';
> +}
> +
> +#if defined(WIN32)
> +#include <windows.h>
> +#include <wincrypt.h>
> +#pragma comment(lib, "advapi32.lib")
> +
> +void exsltCryptoCryptoApiHash(xmlXPathParserContextPtr ctxt, ALG_ID algorithm,
> + const char* msg, unsigned long msglen, char hash[HASH_DIGEST_LENGTH])
> +{
> + HCRYPTPROV hCryptProv;
> + HCRYPTHASH hHash;
> + DWORD dwHashLen = HASH_DIGEST_LENGTH;
> +
> + if(! CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
> + CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error CryptAcquireContext returned NULL\n");
> + return;
> + }
> +
> + if (!CryptCreateHash(hCryptProv, algorithm, 0, 0, &hHash))
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error CryptCreateHash returned NULL\n");
> +
> + CryptReleaseContext(hCryptProv, 0);
> + return;
> + }
> +
> + if(!CryptHashData(hHash, (const BYTE*)msg, msglen, 0))
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error CryptHashData returned NULL\n");
> + goto fail;
> + }
> +
> + if (!CryptGetHashParam(hHash, HP_HASHVAL, hash, &dwHashLen, 0))
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error CryptGetHashParam returned NULL\n");
> + goto fail;
> + }
> +
> +fail:
> + CryptDestroyHash(hHash);
> + CryptReleaseContext(hCryptProv, 0);
> +}
> +
> +#else /* defined(WIN32) */
> +
> +#include <ltdl.h>
> +
> +#define CRYPTO_LIBRARY_NAME "libssl"
> +
> +typedef unsigned char* (*hash_function_t)(const unsigned char *d, unsigned long n,
> + unsigned char *md);
> +
> +void exsltCryptoOpensslHash(xmlXPathParserContextPtr ctxt, const char* algorithm,
> + const char* msg, unsigned long msglen, char dest[HASH_DIGEST_LENGTH])
> +{
> + lt_dlhandle handle;
> + lt_ptr ptr;
> + hash_function_t hash_function;
> + int rc;
> +
> + rc = lt_dlinit();
> + if (rc)
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error in lt_dlinit\n");
> + return;
> + }
> +
> + handle = lt_dlopenext (CRYPTO_LIBRARY_NAME);
> + if (!handle)
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error in lt_dlopenext\n");
> +
> + lt_dlexit();
> + return;
> + }
> +
> + ptr = lt_dlsym (handle, algorithm);
> + if (!ptr)
> + {
> + xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
> + "exslt:crypto : internal error in lt_dlsym\n");
> + goto fail;
> + }
> +
> + hash_function = (hash_function_t)ptr;
> + hash_function((const unsigned char*)msg, msglen, dest);
> +
> +fail:
> + lt_dlclose(handle);
> + lt_dlexit();
> +}
> +
> +#endif /* defined(WIN32) */
> +
> +/**
> + * exsltCryptoMd5sumFunction:
> + * @ctxt: an XPath parser context
> + * @nargs: the number of arguments
> + *
> + * computes the md5sum of a string
> + */
> +static void
> +exsltCryptoMd5sumFunction (xmlXPathParserContextPtr ctxt, int nargs) {
> +
> + int str_len = 0;
> + xmlChar *str = NULL, *ret = NULL;
> + unsigned char hash[HASH_DIGEST_LENGTH];
> + unsigned char hex[MD5_DIGEST_LENGTH*2+1];
> +
> + if ((nargs < 1) || (nargs > 2)) {
> + xmlXPathSetArityError(ctxt);
> + return;
> + }
> +
> + str = xmlXPathPopString(ctxt);
> + str_len = xmlUTF8Strlen(str);
> +
> + if (str_len == 0) {
> + xmlXPathReturnEmptyString(ctxt);
> + xmlFree(str);
> + return;
> + }
> +
> +#if defined(WIN32)
> + exsltCryptoCryptoApiHash(ctxt, CALG_MD5, str, str_len, hash);
> +#else
> + exsltCryptoOpensslHash(ctxt, "MD5", str, str_len, hash);
> +#endif
> +
> + exsltCryptoDigest2Hex(hash, sizeof(hash)-1, hex, sizeof(hex)-1);
> +
> + ret = xmlStrdup((xmlChar *)hex);
> + xmlXPathReturnString(ctxt, ret);
> +
> + if (str != NULL)
> + xmlFree(str);
> +}
> +
> +/**
> + * exsltCryptoSha1sumFunction:
> + * @ctxt: an XPath parser context
> + * @nargs: the number of arguments
> + *
> + * computes the sha1sum of a string
> + */
> +static void
> +exsltCryptoSha1sumFunction (xmlXPathParserContextPtr ctxt, int nargs) {
> +
> + int str_len = 0;
> + xmlChar *str = NULL, *ret = NULL;
> + unsigned char hash[HASH_DIGEST_LENGTH];
> + unsigned char hex[SHA1_DIGEST_LENGTH*2+1];
> +
> + if ((nargs < 1) || (nargs > 2)) {
> + xmlXPathSetArityError(ctxt);
> + return;
> + }
> +
> + str = xmlXPathPopString(ctxt);
> + str_len = xmlUTF8Strlen(str);
> +
> + if (str_len == 0) {
> + xmlXPathReturnEmptyString(ctxt);
> + xmlFree(str);
> + return;
> + }
> +
> +#if defined(WIN32)
> + exsltCryptoCryptoApiHash(ctxt, CALG_MD5, str, str_len, hash);
> +#else
> + exsltCryptoOpensslHash(ctxt, "SHA1", str, str_len, hash);
> +#endif
> +
> + exsltCryptoDigest2Hex(hash, sizeof(hash)-1, hex, sizeof(hex)-1);
> +
> + ret = xmlStrdup((xmlChar *)hex);
> + xmlXPathReturnString(ctxt, ret);
> +
> + if (str != NULL)
> + xmlFree(str);
> +}
> +
> +/**
> + * exsltCryptoRegister:
> + *
> + * Registers the EXSLT - Crypto module
> + */
> +
> +void
> +exsltCryptoRegister (void) {
> + xsltRegisterExtModuleFunction ((const xmlChar *) "md5sum",
> + EXSLT_CRYPTO_NAMESPACE,
> + exsltCryptoMd5sumFunction);
> + xsltRegisterExtModuleFunction ((const xmlChar *) "sha1sum",
> + EXSLT_CRYPTO_NAMESPACE,
> + exsltCryptoSha1sumFunction);
> +}
> +
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/libexslt/exslt.c libxslt-1.1.7/libexslt/exslt.c
> --- libxslt-1.1.7-orig/libexslt/exslt.c 2003-08-18 18:29:49.000000000 -0400
> +++ libxslt-1.1.7/libexslt/exslt.c 2004-05-28 16:48:30.000000000 -0400
> @@ -32,6 +32,7 @@ const int exsltLibxmlVersion = LIBXML_VE
> void
> exsltRegisterAll (void) {
> exsltCommonRegister();
> + exsltCryptoRegister();
> exsltMathRegister();
> exsltSetsRegister();
> exsltFuncRegister();
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/libexslt/exslt.h libxslt-1.1.7/libexslt/exslt.h
> --- libxslt-1.1.7-orig/libexslt/exslt.h 2003-08-25 07:35:40.000000000 -0400
> +++ libxslt-1.1.7/libexslt/exslt.h 2004-05-28 16:48:30.000000000 -0400
> @@ -22,6 +22,12 @@ EXSLTPUBVAR const int exsltLibxmlVersion
> */
> #define EXSLT_COMMON_NAMESPACE ((const xmlChar *) "http://exslt.org/common")
> /**
> + * EXSLT_CRYPTO_NAMESPACE:
> + *
> + * Namespace for EXSLT crypto functions
> + */
> +#define EXSLT_CRYPTO_NAMESPACE ((const xmlChar *) "http://exslt.org/crypto")
> +/**
> * EXSLT_MATH_NAMESPACE:
> *
> * Namespace for EXSLT math functions
> @@ -66,6 +72,7 @@ EXSLTPUBVAR const int exsltLibxmlVersion
> #define SAXON_NAMESPACE ((const xmlChar *) "http://icl.com/saxon")
>
> EXSLTPUBFUN void EXSLTCALL exsltCommonRegister (void);
> +EXSLTPUBFUN void EXSLTCALL exsltCryptoRegister (void);
> EXSLTPUBFUN void EXSLTCALL exsltMathRegister (void);
> EXSLTPUBFUN void EXSLTCALL exsltSetsRegister (void);
> EXSLTPUBFUN void EXSLTCALL exsltFuncRegister (void);
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/libexslt/Makefile.am libxslt-1.1.7/libexslt/Makefile.am
> --- libxslt-1.1.7-orig/libexslt/Makefile.am 2003-10-23 12:26:34.000000000 -0400
> +++ libxslt-1.1.7/libexslt/Makefile.am 2004-05-28 16:48:30.000000000 -0400
> @@ -1,6 +1,6 @@
> INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/libxslt -I$(top_srcdir)/libexslt \
> -I$(top_builddir) -I$(top_builddir)/libxslt \
> - -I$(top_builddir)/libexslt $(LIBXML_CFLAGS) $(CFLAGS)
> + -I$(top_builddir)/libexslt $(LIBXML_CFLAGS) $(CFLAGS) $(LTDLINCL)
>
> lib_LTLIBRARIES = libexslt.la
>
> @@ -16,6 +16,7 @@ exsltinc_HEADERS =
> libexslt_la_SOURCES = \
> exslt.c \
> common.c \
> + crypto.c \
> math.c \
> sets.c \
> functions.c \
> @@ -26,8 +27,9 @@ libexslt_la_SOURCES =
> dynamic.c
>
> # The following DOES NOT WORK reliably.
> -libexslt_la_LIBADD = $(top_builddir)/libxslt/libxslt.la $(EXTRA_LIBS)
> +libexslt_la_LIBADD = $(top_builddir)/libxslt/libxslt.la $(EXTRA_LIBS) $(LIBLTDL)
> libexslt_la_LDFLAGS = -version-info @LIBEXSLT_VERSION_INFO@
> +libexslt_la_DEPENDENCIES = $(LIBLTDL)
>
> man_MANS = libexslt.3
>
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/win32/Makefile.mingw libxslt-1.1.7/win32/Makefile.mingw
> --- libxslt-1.1.7-orig/win32/Makefile.mingw 2003-12-21 06:01:27.000000000 -0500
> +++ libxslt-1.1.7/win32/Makefile.mingw 2004-05-28 16:48:30.000000000 -0400
> @@ -111,6 +111,7 @@ XSLT_OBJS_A = $(XSLT_INTDIR_A)/attribute
>
> # Libexslt object files.
> EXSLT_OBJS = $(EXSLT_INTDIR)/common.o\
> + $(EXSLT_INTDIR)/crypto.o\
> $(EXSLT_INTDIR)/date.o\
> $(EXSLT_INTDIR)/exslt.o\
> $(EXSLT_INTDIR)/functions.o\
> @@ -123,6 +124,7 @@ EXSLT_SRCS = $(subst .o,.c,$(subst $(EXS
>
> # Static libexslt object files.
> EXSLT_OBJS_A = $(EXSLT_INTDIR_A)/common.o\
> + $(EXSLT_INTDIR_A)/crypto.o\
> $(EXSLT_INTDIR_A)/date.o\
> $(EXSLT_INTDIR_A)/exslt.o\
> $(EXSLT_INTDIR_A)/functions.o\
> diff -up -wbBurN -X /home/jreed/src/lm-3.1.2/lm/do-not-diff libxslt-1.1.7-orig/win32/Makefile.msvc libxslt-1.1.7/win32/Makefile.msvc
> --- libxslt-1.1.7-orig/win32/Makefile.msvc 2004-02-22 17:32:47.000000000 -0500
> +++ libxslt-1.1.7/win32/Makefile.msvc 2004-05-28 16:48:30.000000000 -0400
> @@ -115,6 +115,7 @@ XSLT_OBJS_A = $(XSLT_INTDIR_A)\attribute
>
> # Libexslt object files.
> EXSLT_OBJS = $(EXSLT_INTDIR)\common.obj\
> + $(EXSLT_INTDIR)\crypto.obj\
> $(EXSLT_INTDIR)\date.obj\
> $(EXSLT_INTDIR)\exslt.obj\
> $(EXSLT_INTDIR)\functions.obj\
> @@ -126,6 +127,7 @@ EXSLT_OBJS = $(EXSLT_INTDIR)\common.obj\
>
> # Static libexslt object files.
> EXSLT_OBJS_A = $(EXSLT_INTDIR_A)\common.obj\
> + $(EXSLT_INTDIR_A)\crypto.obj\
> $(EXSLT_INTDIR_A)\date.obj\
> $(EXSLT_INTDIR_A)\exslt.obj\
> $(EXSLT_INTDIR_A)\functions.obj\
> _______________________________________________
> xslt mailing list, project page http://xmlsoft.org/XSLT/
> xslt gnome org
> http://mail.gnome.org/mailman/listinfo/xslt
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]