RFC 2254 : The String Representation of LDAP Search Filters

This patch add support for rfc2254 escaping.

With patched source method
ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags);
, where flags are
XN_FLAG_RFC2253|ASN1_STRFLGS_ESC_2254
can be used in generation of "LDAP search filter".

Roumen


Index: crypto/asn1/a_strex.c
===================================================================
RCS file: /work/repo/mirror/openssl/openssl/crypto/asn1/a_strex.c,v
retrieving revision 1.17
diff -u -r1.17 a_strex.c
--- crypto/asn1/a_strex.c       21 Feb 2007 17:44:53 -0000      1.17
+++ crypto/asn1/a_strex.c       4 Mar 2007 21:18:57 -0000
@@ -112,9 +112,10 @@
  * 4 byte forms.
  */
 
-static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, 
char_io *io_ch, void *arg)
+static int do_esc_char(unsigned long c, unsigned short flags, char *do_quotes, 
char_io *io_ch, void *arg)
 {
-       unsigned char chflgs, chtmp;
+       unsigned short chflgs;
+       unsigned char chtmp;
        char tmphex[HEX_SIZE(long)+3];
 
        if(c > 0xffffffffL)
@@ -143,7 +144,7 @@
                if(!io_ch(arg, &chtmp, 1)) return -1;
                return 2;
        }
-       if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
+       if(chflgs & 
(ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB|ASN1_STRFLGS_ESC_2254)) {
                BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
                if(!io_ch(arg, tmphex, 3)) return -1;
                return 3;
@@ -161,10 +162,11 @@
  */
 
 static int do_buf(unsigned char *buf, int buflen,
-                       int type, unsigned char flags, char *quotes, char_io 
*io_ch, void *arg)
+                       int type, unsigned short flags, char *quotes, char_io 
*io_ch, void *arg)
 {
        int i, outlen, len;
-       unsigned char orflags, *p, *q;
+       unsigned short orflags;
+       unsigned char *p, *q;
        unsigned long c;
        p = buf;
        q = buf + buflen;
@@ -208,12 +210,12 @@
                                 * otherwise each character will be > 0x7f and 
so the 
                                 * character will never be escaped on first and 
last.
                                 */
-                               len = do_esc_char(utfbuf[i], (unsigned 
char)(flags | orflags), quotes, io_ch, arg);
+                               len = do_esc_char(utfbuf[i], (unsigned 
short)(flags | orflags), quotes, io_ch, arg);
                                if(len < 0) return -1;
                                outlen += len;
                        }
                } else {
-                       len = do_esc_char(c, (unsigned char)(flags | orflags), 
quotes, io_ch, arg);
+                       len = do_esc_char(c, (unsigned short)(flags | orflags), 
quotes, io_ch, arg);
                        if(len < 0) return -1;
                        outlen += len;
                }
@@ -293,6 +295,7 @@
 };
 
 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
+                 ASN1_STRFLGS_ESC_2254 | \
                  ASN1_STRFLGS_ESC_QUOTE | \
                  ASN1_STRFLGS_ESC_CTRL | \
                  ASN1_STRFLGS_ESC_MSB)
@@ -309,10 +312,10 @@
        int outlen, len;
        int type;
        char quotes;
-       unsigned char flags;
+       unsigned short flags;
        quotes = 0;
        /* Keep a copy of escape flags */
-       flags = (unsigned char)(lflags & ESC_FLAGS);
+       flags = (unsigned short)(lflags & ESC_FLAGS);
 
        type = str->type;
 
Index: crypto/asn1/asn1.h
===================================================================
RCS file: /work/repo/mirror/openssl/openssl/crypto/asn1/asn1.h,v
retrieving revision 1.149
diff -u -r1.149 asn1.h
--- crypto/asn1/asn1.h  24 Dec 2006 16:22:54 -0000      1.149
+++ crypto/asn1/asn1.h  4 Mar 2007 12:40:36 -0000
@@ -475,6 +475,10 @@
 
 #define ASN1_STRFLGS_DUMP_DER          0x200
 
+/* This flag determines to do RC2254 escaping.
+ */
+#define ASN1_STRFLGS_ESC_2254          0x400
+
 /* All the string flags consistent with RFC2253,
  * escaping control characters isn't essential in
  * RFC2253 but it is advisable anyway.
Index: crypto/asn1/charmap.h
===================================================================
RCS file: /work/repo/mirror/openssl/openssl/crypto/asn1/charmap.h,v
retrieving revision 1.1
diff -u -r1.1 charmap.h
--- crypto/asn1/charmap.h       28 Jul 2000 01:58:11 -0000      1.1
+++ crypto/asn1/charmap.h       4 Mar 2007 12:49:05 -0000
@@ -2,13 +2,13 @@
  * Mask of various character properties
  */
 
-static unsigned char char_type[] = {
+static unsigned short char_type[] = {
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
-120, 0, 1,40, 0, 0, 0,16,16,16, 0,25,25,16,16,16,
+120, 0, 1,40, 0, 0, 0,16,1040,1040,1040,25,25,16,16,16,
 16,16,16,16,16,16,16,16,16,16,16, 9, 9,16, 9,16,
  0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
-16,16,16,16,16,16,16,16,16,16,16, 0, 1, 0, 0, 0,
+16,16,16,16,16,16,16,16,16,16,16, 0,1025, 0, 0, 0,
  0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
 16,16,16,16,16,16,16,16,16,16,16, 0, 0, 0, 0, 2
 };
Index: crypto/asn1/charmap.pl
===================================================================
RCS file: /work/repo/mirror/openssl/openssl/crypto/asn1/charmap.pl,v
retrieving revision 1.1
diff -u -r1.1 charmap.pl
--- crypto/asn1/charmap.pl      28 Jul 2000 01:58:11 -0000      1.1
+++ crypto/asn1/charmap.pl      4 Mar 2007 12:48:50 -0000
@@ -15,6 +15,7 @@
 my $PSTRING_CHAR = 0x10;       # Valid PrintableString character
 my $RFC2253_FIRST_ESC = 0x20; # Escaped with \ if first character
 my $RFC2253_LAST_ESC = 0x40;  # Escaped with \ if last character
+my $RFC2254_ESC = 0x400;       # Character escaped \XX
 
 for($i = 0; $i < 128; $i++) {
        # Set the RFC2253 escape characters (control)
@@ -41,7 +42,7 @@
 $arr[ord(",")] |= $NOESC_QUOTE | $RFC2253_ESC;
 $arr[ord("+")] |= $NOESC_QUOTE | $RFC2253_ESC;
 $arr[ord("\"")] |= $RFC2253_ESC;
-$arr[ord("\\")] |= $RFC2253_ESC;
+$arr[ord("\\")] |= $RFC2253_ESC | $RFC2254_ESC;
 $arr[ord("<")] |= $NOESC_QUOTE | $RFC2253_ESC;
 $arr[ord(">")] |= $NOESC_QUOTE | $RFC2253_ESC;
 $arr[ord(";")] |= $NOESC_QUOTE | $RFC2253_ESC;
@@ -50,8 +51,9 @@
 
 $arr[ord(" ")] |= $PSTRING_CHAR;
 $arr[ord("'")] |= $PSTRING_CHAR;
-$arr[ord("(")] |= $PSTRING_CHAR;
-$arr[ord(")")] |= $PSTRING_CHAR;
+$arr[ord("(")] |= $PSTRING_CHAR | $RFC2254_ESC;
+$arr[ord(")")] |= $PSTRING_CHAR | $RFC2254_ESC;
+$arr[ord("*")] |= $PSTRING_CHAR | $RFC2254_ESC;
 $arr[ord("+")] |= $PSTRING_CHAR;
 $arr[ord(",")] |= $PSTRING_CHAR;
 $arr[ord("-")] |= $PSTRING_CHAR;
@@ -68,7 +70,7 @@
  * Mask of various character properties
  */
 
-static unsigned char char_type[] = {
+static unsigned short char_type[] = {
 EOF
 
 for($i = 0; $i < 128; $i++) {

Reply via email to