]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/schema_init.c
note about OpenSSL being more liberal than OpenLDAP when there is garbage past the...
[openldap] / servers / slapd / schema_init.c
index 24ac340b3a1150dd546823f3afaa7e24242e7a57..95d108f6a5f294bd53777cfff0a7aa8d0742a708 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2008 The OpenLDAP Foundation.
+ * Copyright 1998-2009 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 #define authzMatch                             octetStringMatch
 
+/* X.509 PMI ldapSyntaxes */
+/* FIXME: need to create temporary OIDs under OpenLDAP's arc;
+ * these are currently hijacked
+ *
+ *     1.3.6.1.4.1.4203.666            OpenLDAP
+ *     1.3.6.1.4.1.4203.666.11         self-contained works
+ *     1.3.6.1.4.1.4203.666.11.10      X.509 PMI
+ *     1.3.6.1.4.1.4203.666.11.10.2    X.509 PMI ldapSyntaxes
+ *     1.3.6.1.4.1.4203.666.11.10.2.1  AttributeCertificate (supported)
+ *     1.3.6.1.4.1.4203.666.11.10.2.2  AttributeCertificateExactAssertion (supported)
+ *     1.3.6.1.4.1.4203.666.11.10.2.3  AttributeCertificateAssertion (not supported)
+ *     1.3.6.1.4.1.4203.666.11.10.2.4  AttCertPath (X-SUBST'ed right now in pmi.schema)
+ *     1.3.6.1.4.1.4203.666.11.10.2.5  PolicySyntax (X-SUBST'ed right now in pmi.schema)
+ *     1.3.6.1.4.1.4203.666.11.10.2.6  RoleSyntax (X-SUBST'ed right now in pmi.schema)
+ */
+#if 0 /* from <draft-ietf-pkix-ldap-schema-02.txt> (expired) */
+#define attributeCertificateSyntaxOID                  "1.2.826.0.1.3344810.7.5"
+#define attributeCertificateExactAssertionSyntaxOID    "1.2.826.0.1.3344810.7.6"
+#define attributeCertificateAssertionSyntaxOID         "1.2.826.0.1.3344810.7.7"
+#else /* from OpenLDAP's experimental oid arc */
+#define X509_PMI_SyntaxOID                             "1.3.6.1.4.1.4203.666.11.10.2"
+#define attributeCertificateSyntaxOID                  X509_PMI_SyntaxOID ".1"
+#define attributeCertificateExactAssertionSyntaxOID    X509_PMI_SyntaxOID ".2"
+#define attributeCertificateAssertionSyntaxOID         X509_PMI_SyntaxOID ".3"
+#endif
+
 unsigned int index_substr_if_minlen = SLAP_INDEX_SUBSTR_IF_MINLEN_DEFAULT;
 unsigned int index_substr_if_maxlen = SLAP_INDEX_SUBSTR_IF_MAXLEN_DEFAULT;
 unsigned int index_substr_any_len = SLAP_INDEX_SUBSTR_ANY_LEN_DEFAULT;
@@ -130,8 +156,8 @@ enum {
 
 enum {
        SLAP_X509_OPT_C_VERSION         = SLAP_X509_OPTION + 0,
-       SLAP_X509_OPT_C_ISSUERUNIQUEID  = SLAP_X509_OPTION + 1,
-       SLAP_X509_OPT_C_SUBJECTUNIQUEID = SLAP_X509_OPTION + 2,
+       SLAP_X509_OPT_C_ISSUERUNIQUEID  = LBER_CLASS_CONTEXT + 1,
+       SLAP_X509_OPT_C_SUBJECTUNIQUEID = LBER_CLASS_CONTEXT + 2,
        SLAP_X509_OPT_C_EXTENSIONS      = SLAP_X509_OPTION + 3
 };
 
@@ -139,6 +165,40 @@ enum {
        SLAP_X509_OPT_CL_CRLEXTENSIONS  = SLAP_X509_OPTION + 0
 };
 
+/*
+GeneralName ::= CHOICE {
+  otherName                 [0] INSTANCE OF OTHER-NAME,
+  rfc822Name                [1] IA5String,
+  dNSName                   [2] IA5String,
+  x400Address               [3] ORAddress,
+  directoryName             [4] Name,
+  ediPartyName              [5] EDIPartyName,
+  uniformResourceIdentifier [6] IA5String,
+  iPAddress                 [7] OCTET STRING,
+  registeredID              [8] OBJECT IDENTIFIER }
+*/
+enum {
+       SLAP_X509_GN_OTHERNAME          = SLAP_X509_OPTION + 0,
+       SLAP_X509_GN_RFC822NAME         = SLAP_X509_OPTION + 1,
+       SLAP_X509_GN_DNSNAME            = SLAP_X509_OPTION + 2,
+       SLAP_X509_GN_X400ADDRESS        = SLAP_X509_OPTION + 3,
+       SLAP_X509_GN_DIRECTORYNAME      = SLAP_X509_OPTION + 4,
+       SLAP_X509_GN_EDIPARTYNAME       = SLAP_X509_OPTION + 5,
+       SLAP_X509_GN_URI                = SLAP_X509_OPTION + 6,
+       SLAP_X509_GN_IPADDRESS          = SLAP_X509_OPTION + 7,
+       SLAP_X509_GN_REGISTEREDID       = SLAP_X509_OPTION + 8
+};
+
+/* X.509 PMI related stuff */
+enum {
+       SLAP_X509AC_V1          = 0,
+       SLAP_X509AC_V2          = 1
+};
+
+enum {
+       SLAP_X509AC_ISSUER      = SLAP_X509_OPTION + 0
+};
+
 /* X.509 certificate validation */
 static int
 certificateValidate( Syntax *syntax, struct berval *in )
@@ -259,10 +319,11 @@ certificateListValidate( Syntax *syntax, struct berval *in )
                        tag = ber_skip_tag( ber, &len );
                }
        }
-       /* Optional Extensions */
+       /* Optional Extensions - Sequence of Sequence */
        if ( tag == SLAP_X509_OPT_CL_CRLEXTENSIONS ) { /* ? */
+               ber_len_t seqlen;
                if ( version != SLAP_X509_V2 ) return LDAP_INVALID_SYNTAX;
-               tag = ber_skip_tag( ber, &len );
+               tag = ber_peek_tag( ber, &seqlen );
                if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
                ber_skip_data( ber, len );
                tag = ber_skip_tag( ber, &len );
@@ -276,10 +337,94 @@ certificateListValidate( Syntax *syntax, struct berval *in )
        ber_skip_data( ber, len );
        tag = ber_skip_tag( ber, &len );
        /* Must be at end now */
+       /* NOTE: OpenSSL tolerates CL with garbage past the end */
        if ( len || tag != LBER_DEFAULT ) return LDAP_INVALID_SYNTAX;
        return LDAP_SUCCESS;
 }
 
+/* X.509 PMI Attribute Certificate Validate */
+static int
+attributeCertificateValidate( Syntax *syntax, struct berval *in )
+{
+       BerElementBuffer berbuf;
+       BerElement *ber = (BerElement *)&berbuf;
+       ber_tag_t tag;
+       ber_len_t len;
+       ber_int_t version;
+       int cont = 0;
+
+       ber_init2( ber, in, LBER_USE_DER );
+       
+       tag = ber_skip_tag( ber, &len );        /* Signed wrapper */
+       if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
+
+       tag = ber_skip_tag( ber, &len );        /* Sequence */
+       if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
+
+       tag = ber_peek_tag( ber, &len );        /* Version */
+       if ( tag != LBER_INTEGER ) return LDAP_INVALID_SYNTAX;
+       tag = ber_get_int( ber, &version );     /* X.509 only allows v2 */
+       if ( version != SLAP_X509AC_V2 ) return LDAP_INVALID_SYNTAX;
+
+       tag = ber_skip_tag( ber, &len );        /* Holder */
+       if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
+       ber_skip_data( ber, len );
+
+       tag = ber_skip_tag( ber, &len );        /* Issuer */
+       if ( tag != SLAP_X509AC_ISSUER ) return LDAP_INVALID_SYNTAX;
+       ber_skip_data( ber, len );
+
+       tag = ber_skip_tag( ber, &len );        /* Signature */
+       if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
+       ber_skip_data( ber, len );
+
+       tag = ber_skip_tag( ber, &len );        /* Serial number */
+       if ( tag != LBER_INTEGER ) return LDAP_INVALID_SYNTAX;
+       ber_skip_data( ber, len );
+
+       tag = ber_skip_tag( ber, &len );        /* AttCertValidityPeriod */
+       if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
+       ber_skip_data( ber, len );
+
+       tag = ber_skip_tag( ber, &len );        /* Attributes */
+       if ( tag != LBER_SEQUENCE ) return LDAP_INVALID_SYNTAX;
+       ber_skip_data( ber, len );
+
+       tag = ber_peek_tag( ber, &len );
+
+       if ( tag == LBER_BITSTRING ) {  /* issuerUniqueID */
+               tag = ber_skip_tag( ber, &len );
+               ber_skip_data( ber, len );
+               tag = ber_peek_tag( ber, &len );
+       }
+
+       if ( tag == LBER_SEQUENCE ) {   /* extensions or signatureAlgorithm */
+               tag = ber_skip_tag( ber, &len );
+               ber_skip_data( ber, len );
+               cont++;
+               tag = ber_peek_tag( ber, &len );
+       }
+
+       if ( tag == LBER_SEQUENCE ) {   /* signatureAlgorithm */
+               tag = ber_skip_tag( ber, &len );
+               ber_skip_data( ber, len );
+               cont++;
+               tag = ber_peek_tag( ber, &len );
+       }
+
+       if ( tag == LBER_BITSTRING ) {  /* Signature */
+               tag = ber_skip_tag( ber, &len );
+               ber_skip_data( ber, len );
+               cont++;
+               tag = ber_peek_tag( ber, &len );
+       }
+
+       /* Must be at end now */
+       if ( len != 0 || tag != LBER_DEFAULT || cont < 2 ) return LDAP_INVALID_SYNTAX;
+
+       return LDAP_SUCCESS;
+}
+
 int
 octetStringMatch(
        int *matchp,
@@ -962,12 +1107,7 @@ bitStringValidate(
   ...
       
  *
- * Note: normalization strips any leading "0"s, unless the
- * bit string is exactly "'0'B", so the normalized example,
- * in slapd, would result in
- * 
- * 1.3.6.1.4.1.1466.0=#04024869,o=test,c=gb#'101'B
- * 
+ * Note:
  * RFC 4514 clarifies that SHARP, i.e. "#", doesn't have to
  * be escaped except when at the beginning of a value, the
  * definition of Name and Optional UID appears to be flawed,
@@ -991,11 +1131,11 @@ bitStringValidate(
  *
  * in fact "com#'1'B" is a valid IA5 string.
  *
- * As a consequence, current slapd code assumes that the
- * presence of portions of a BitString at the end of the string 
- * representation of a NameAndOptionalUID means a BitString
- * is expected, and cause an error otherwise.  This is quite
- * arbitrary, and might change in the future.
+ * As a consequence, current slapd code takes the presence of
+ * #<valid BitString> at the end of the string representation
+ * of a NameAndOptionalUID to mean this is indeed a BitString.
+ * This is quite arbitrary - it has changed the past and might
+ * change in the future.
  */
 
 
@@ -1066,7 +1206,8 @@ nameUIDPretty(
 
                        if ( rc == LDAP_SUCCESS ) {
                                ber_dupbv_x( &dnval, val, ctx );
-                               dnval.bv_len -= uidval.bv_len + 1;
+                               uidval.bv_val--;
+                               dnval.bv_len -= ++uidval.bv_len;
                                dnval.bv_val[dnval.bv_len] = '\0';
 
                        } else {
@@ -1083,36 +1224,18 @@ nameUIDPretty(
                }
 
                if( !BER_BVISNULL( &uidval ) ) {
-                       int     i, c, got1;
                        char    *tmp;
 
                        tmp = slap_sl_realloc( out->bv_val, out->bv_len 
-                               + STRLENOF( "#" ) + uidval.bv_len + 1,
+                               + uidval.bv_len + 1,
                                ctx );
                        if( tmp == NULL ) {
                                ber_memfree_x( out->bv_val, ctx );
                                return LDAP_OTHER;
                        }
                        out->bv_val = tmp;
-                       out->bv_val[out->bv_len++] = '#';
-                       out->bv_val[out->bv_len++] = '\'';
-
-                       got1 = uidval.bv_len < sizeof("'0'B"); 
-                       for( i = 1; i < uidval.bv_len - 2; i++ ) {
-                               c = uidval.bv_val[i];
-                               switch(c) {
-                                       case '0':
-                                               if( got1 ) out->bv_val[out->bv_len++] = c;
-                                               break;
-                                       case '1':
-                                               got1 = 1;
-                                               out->bv_val[out->bv_len++] = c;
-                                               break;
-                               }
-                       }
-
-                       out->bv_val[out->bv_len++] = '\'';
-                       out->bv_val[out->bv_len++] = 'B';
+                       memcpy( out->bv_val + out->bv_len, uidval.bv_val, uidval.bv_len );
+                       out->bv_len += uidval.bv_len;
                        out->bv_val[out->bv_len] = '\0';
                }
        }
@@ -1543,8 +1666,8 @@ UTF8StringNormalize(
        void *ctx )
 {
        struct berval tmp, nvalue;
-       int flags;
-       int i, wasspace;
+       int flags, wasspace;
+       ber_len_t i;
 
        assert( SLAP_MR_IS_VALUE_OF_SYNTAX( use ) != 0 );
 
@@ -1619,7 +1742,7 @@ directoryStringSubstringsMatch(
        int match = 0;
        SubstringsAssertion *sub = assertedValue;
        struct berval left = *value;
-       int i;
+       ber_len_t i;
        int priorspace=0;
 
        if ( !BER_BVISNULL( &sub->sa_initial ) ) {
@@ -2008,7 +2131,7 @@ postalAddressValidate(
        struct berval *in )
 {
        struct berval bv = *in;
-       int c;
+       ber_len_t c;
 
        for ( c = 0; c < in->bv_len; c++ ) {
                if ( in->bv_val[c] == '\\' ) {
@@ -2044,7 +2167,7 @@ postalAddressNormalize(
        void *ctx )
 {
        BerVarray lines = NULL, nlines = NULL;
-       int l, c;
+       ber_len_t l, c;
        int rc = LDAP_SUCCESS;
        MatchingRule *xmr = NULL;
        char *p;
@@ -2101,7 +2224,7 @@ postalAddressNormalize(
        }
        *--p = '\0';
 
-       assert( p - normalized->bv_val == normalized->bv_len );
+       assert( p == &normalized->bv_val[normalized->bv_len] );
 
 done:;
        if ( nlines != NULL ) {
@@ -2693,6 +2816,7 @@ UUIDNormalize(
 
                } else {
                        slap_sl_free( normalized->bv_val, ctx );
+                       BER_BVZERO( normalized );
                        return LDAP_INVALID_SYNTAX;
                }
 
@@ -2851,9 +2975,8 @@ static int
 checkNum( struct berval *in, struct berval *out )
 {
        /* parse serialNumber */
-       int neg = 0;
+       ber_len_t neg = 0, extra = 0;
        char first = '\0';
-       int extra = 0;
 
        out->bv_val = in->bv_val;
        out->bv_len = 0;
@@ -2911,7 +3034,7 @@ serialNumberAndIssuerCheck(
        struct berval *is,
        void *ctx )
 {
-       int n;
+       ber_len_t n;
 
        if( in->bv_len < 3 ) return LDAP_INVALID_SYNTAX;
 
@@ -3059,7 +3182,7 @@ serialNumberAndIssuerCheck(
                        ber_dupbv_x( &ni, is, ctx );
 
                } else {
-                       ber_int_t src, dst;
+                       ber_len_t src, dst;
 
                        ni.bv_len = is->bv_len - numdquotes;
                        ni.bv_val = ber_memalloc_x( ni.bv_len + 1, ctx );
@@ -3164,7 +3287,7 @@ serialNumberAndIssuerPretty(
        p = lutil_strncopy( p, ni.bv_val, ni.bv_len );
        p = lutil_strcopy( p, /*{*/ "\" }" );
 
-       assert( p - out->bv_val == out->bv_len );
+       assert( p == &out->bv_val[out->bv_len] );
 
 done:;
        Debug( LDAP_DEBUG_TRACE, "<<< serialNumberAndIssuerPretty: <%s> => <%s>\n",
@@ -3186,8 +3309,7 @@ slap_bin2hex(
        unsigned char *ptr, zero = '\0';
        char *sptr;
        int first;
-       int i;
-       ber_len_t len, nlen;
+       ber_len_t i, len, nlen;
 
        assert( in != NULL );
        assert( !BER_BVISNULL( in ) );
@@ -3240,7 +3362,7 @@ slap_bin2hex(
        *sptr++ = 'H';
        *sptr = '\0';
 
-       assert( sptr - out->bv_val == nlen );
+       assert( sptr == &out->bv_val[nlen] );
 
        out->bv_len = nlen;
 
@@ -3327,7 +3449,7 @@ serialNumberAndIssuerNormalize(
        p = lutil_strncopy( p, ni.bv_val, ni.bv_len );
        p = lutil_strcopy( p, /*{*/ "\" }" );
 
-       assert( p - out->bv_val == out->bv_len );
+       assert( p == &out->bv_val[out->bv_len] );
 
 func_leave:
        Debug( LDAP_DEBUG_TRACE, "<<< serialNumberAndIssuerNormalize: <%s> => <%s>\n",
@@ -3440,7 +3562,8 @@ done:
 static int
 checkTime( struct berval *in, struct berval *out )
 {
-       int i;
+       int rc;
+       ber_len_t i;
        char buf[STRLENOF("YYYYmmddHHMMSSZ") + 1];
        struct berval bv;
 
@@ -3493,12 +3616,12 @@ checkTime( struct berval *in, struct berval *out )
                return -1;
        }
 
-       i = generalizedTimeValidate( NULL, &bv );
-       if ( i == LDAP_SUCCESS && out != NULL ) {
+       rc = generalizedTimeValidate( NULL, &bv );
+       if ( rc == LDAP_SUCCESS && out != NULL ) {
                out->bv_len = bv.bv_len;
        }
 
-       return i != LDAP_SUCCESS;
+       return rc != LDAP_SUCCESS;
 }
 
 static int
@@ -3645,7 +3768,7 @@ issuerAndThisUpdateCheck(
                ber_dupbv_x( &ni, is, ctx );
 
        } else {
-               ber_int_t src, dst;
+               ber_len_t src, dst;
 
                ni.bv_len = is->bv_len - numdquotes;
                ni.bv_val = ber_memalloc_x( ni.bv_len + 1, ctx );
@@ -3752,7 +3875,7 @@ issuerAndThisUpdatePretty(
        p = lutil_strncopy( p, tu.bv_val, tu.bv_len );
        p = lutil_strcopy( p, /*{*/ "\" }" );
 
-       assert( p - out->bv_val == out->bv_len );
+       assert( p == &out->bv_val[out->bv_len] );
 
 done:;
        Debug( LDAP_DEBUG_TRACE, "<<< issuerAndThisUpdatePretty: <%s> => <%s>\n",
@@ -3818,7 +3941,7 @@ issuerAndThisUpdateNormalize(
        p = lutil_strncopy( p, tu2.bv_val, tu2.bv_len );
        p = lutil_strcopy( p, /*{*/ "\" }" );
 
-       assert( p - out->bv_val == out->bv_len );
+       assert( p == &out->bv_val[out->bv_len] );
 
 func_leave:
        Debug( LDAP_DEBUG_TRACE, "<<< issuerAndThisUpdateNormalize: <%s> => <%s>\n",
@@ -3924,150 +4047,837 @@ done:
        return rc;
 }
 
+/* X.509 PMI serialNumberAndIssuerSerialCheck
+
+AttributeCertificateExactAssertion     ::= SEQUENCE {
+   serialNumber              CertificateSerialNumber,
+   issuer                    AttCertIssuer }
+
+CertificateSerialNumber ::= INTEGER
+
+AttCertIssuer ::=    [0] SEQUENCE {
+issuerName                     GeneralNames OPTIONAL,
+baseCertificateID         [0] IssuerSerial OPTIONAL,
+objectDigestInfo          [1] ObjectDigestInfo OPTIONAL }
+-- At least one component shall be present
+
+GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
+
+GeneralName ::= CHOICE {
+  otherName                 [0] INSTANCE OF OTHER-NAME,
+  rfc822Name                [1] IA5String,
+  dNSName                   [2] IA5String,
+  x400Address               [3] ORAddress,
+  directoryName             [4] Name,
+  ediPartyName              [5] EDIPartyName,
+  uniformResourceIdentifier [6] IA5String,
+  iPAddress                 [7] OCTET STRING,
+  registeredID              [8] OBJECT IDENTIFIER }
+
+IssuerSerial ::= SEQUENCE {
+   issuer       GeneralNames,
+   serial       CertificateSerialNumber,
+   issuerUID UniqueIdentifier OPTIONAL }
+
+ObjectDigestInfo ::= SEQUENCE {
+   digestedObjectType ENUMERATED {
+      publicKey           (0),
+      publicKeyCert       (1),
+      otherObjectTypes    (2) },
+   otherObjectTypeID      OBJECT IDENTIFIER OPTIONAL,
+   digestAlgorithm        AlgorithmIdentifier,
+   objectDigest           BIT STRING }
+
+ * The way I interpret it, an assertion should look like
+
+ { serialNumber 'dd'H,
+   issuer { issuerName { directoryName:rdnSequence:"cn=yyy" }, -- optional
+            baseCertificateID { serial '1d'H,
+                                issuer { directoryName:rdnSequence:"cn=zzz" },
+                                issuerUID <value>              -- optional
+                              },                               -- optional
+            objectDigestInfo { ... }                           -- optional
+          }
+ }
+ * with issuerName, baseCertificateID and objectDigestInfo optional,
+ * at least one present; the way it's currently implemented, it is
+
+ { serialNumber 'dd'H,
+   issuer { baseCertificateID { serial '1d'H,
+                                issuer { directoryName:rdnSequence:"cn=zzz" }
+                              }
+          }
+ }
+
+ * with all the above parts mandatory.
+ */
 static int
-hexValidate(
-       Syntax *syntax,
-       struct berval *in )
+serialNumberAndIssuerSerialCheck(
+       struct berval *in,
+       struct berval *sn,
+       struct berval *is,
+       struct berval *i_sn,    /* contain serial of baseCertificateID */
+       void *ctx )
 {
-       int     i;
-
-       assert( in != NULL );
-       assert( !BER_BVISNULL( in ) );
-
-       for ( i = 0; i < in->bv_len; i++ ) {
-               if ( !ASCII_HEX( in->bv_val[ i ] ) ) {
-                       return LDAP_INVALID_SYNTAX;
-               }
-       }
+       /* Parse GSER format */ 
+       enum {
+               HAVE_NONE = 0x0,
+               HAVE_SN = 0x1,
+               HAVE_ISSUER = 0x2,
+               HAVE_ALL = ( HAVE_SN | HAVE_ISSUER )
+       } have = HAVE_NONE, have2 = HAVE_NONE;
+       int numdquotes = 0;
+       struct berval x = *in;
+       struct berval ni;
 
-       return LDAP_SUCCESS;
-}
+       if ( in->bv_len < 3 ) return LDAP_INVALID_SYNTAX;
 
-/* Normalize a SID as used inside a CSN:
- * three-digit numeric string */
-static int
-hexNormalize(
-       slap_mask_t usage,
-       Syntax *syntax,
-       MatchingRule *mr,
-       struct berval *val,
-       struct berval *normalized,
-       void *ctx )
-{
-       int     i;
+       /* no old format */
+       if ( in->bv_val[0] != '{' && in->bv_val[in->bv_len-1] != '}' ) return LDAP_INVALID_SYNTAX;
 
-       assert( val != NULL );
-       assert( normalized != NULL );
+       x.bv_val++;
+       x.bv_len -= 2;
 
-       ber_dupbv_x( normalized, val, ctx );
+       do {
 
-       for ( i = 0; i < normalized->bv_len; i++ ) {
-               if ( !ASCII_HEX( normalized->bv_val[ i ] ) ) {
-                       ber_memfree_x( normalized->bv_val, ctx );
-                       BER_BVZERO( normalized );
-                       return LDAP_INVALID_SYNTAX;
+               /* eat leading spaces */
+               for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                       /* empty */;
                }
 
-               normalized->bv_val[ i ] = TOLOWER( normalized->bv_val[ i ] );
-       }
+               /* should be at issuer or serialNumber NamedValue */
+               if ( strncasecmp( x.bv_val, "issuer", STRLENOF("issuer") ) == 0 ) {
+                       if ( have & HAVE_ISSUER ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
 
-       return LDAP_SUCCESS;
-}
+                       /* parse IssuerSerial */
+                       x.bv_val += STRLENOF("issuer");
+                       x.bv_len -= STRLENOF("issuer");
 
-static int
-sidValidate (
-       Syntax *syntax,
-       struct berval *in )
-{
-       assert( in != NULL );
-       assert( !BER_BVISNULL( in ) );
+                       if ( x.bv_val[0] != ' ' ) return LDAP_INVALID_SYNTAX;
+                       x.bv_val++;
+                       x.bv_len--;
 
-       if ( in->bv_len != 3 ) {
-               return LDAP_INVALID_SYNTAX;
-       }
+                       /* eat leading spaces */
+                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                               /* empty */;
+                       }
 
-       return hexValidate( NULL, in );
-}
+                       if ( x.bv_val[0] != '{' /*}*/ ) return LDAP_INVALID_SYNTAX;
+                       x.bv_val++;
+                       x.bv_len--;
 
-/* Normalize a SID as used inside a CSN:
- * three-digit numeric string */
-static int
-sidNormalize(
-       slap_mask_t usage,
-       Syntax *syntax,
-       MatchingRule *mr,
-       struct berval *val,
-       struct berval *normalized,
-       void *ctx )
-{
-       if ( val->bv_len != 3 ) {
-               return LDAP_INVALID_SYNTAX;
-       }
+                       /* eat leading spaces */
+                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                               /* empty */;
+                       }
 
-       return hexNormalize( 0, NULL, NULL, val, normalized, ctx );
-}
+                       if ( strncasecmp( x.bv_val, "baseCertificateID ", STRLENOF("baseCertificateID ") ) != 0 ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+                       x.bv_val += STRLENOF("baseCertificateID ");
+                       x.bv_len -= STRLENOF("baseCertificateID ");
 
-static int
-sidPretty(
-       Syntax *syntax,
-       struct berval *val,
-       struct berval *out,
-       void *ctx )
-{
-       return sidNormalize( SLAP_MR_VALUE_OF_SYNTAX, NULL, NULL, val, out, ctx );
-}
+                       /* eat leading spaces */
+                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                               /* empty */;
+                       }
 
-/* Normalize a SID as used inside a CSN, either as-is
- * (assertion value) or extracted from the CSN
- * (attribute value) */
-static int
-csnSidNormalize(
-       slap_mask_t usage,
-       Syntax *syntax,
-       MatchingRule *mr,
-       struct berval *val,
-       struct berval *normalized,
-       void *ctx )
-{
-       struct berval   bv;
-       char            *ptr,
-                       buf[ 4 ];
+                       if ( x.bv_val[0] != '{' /*}*/ ) return LDAP_INVALID_SYNTAX;
+                       x.bv_val++;
+                       x.bv_len--;
 
+                       do {
+                               /* eat leading spaces */
+                               for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                                       /* empty */;
+                               }
 
-       if ( BER_BVISEMPTY( val ) ) {
-               return LDAP_INVALID_SYNTAX;
-       }
+                               /* parse issuer of baseCertificateID */
+                               if ( strncasecmp( x.bv_val, "issuer ", STRLENOF("issuer ") ) == 0 ) {
+                                       if ( have2 & HAVE_ISSUER ) {
+                                               return LDAP_INVALID_SYNTAX;
+                                       }
 
-       if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX(usage) ) {
-               return sidNormalize( 0, NULL, NULL, val, normalized, ctx );
-       }
+                                       x.bv_val += STRLENOF("issuer ");
+                                       x.bv_len -= STRLENOF("issuer ");
 
-       assert( SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) != 0 );
+                                       /* eat leading spaces */
+                                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                                               /* empty */;
+                                       }
 
-       ptr = ber_bvchr( val, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
-               return LDAP_INVALID_SYNTAX;
-       }
+                                       if ( x.bv_val[0] != '{' /*}*/ ) return LDAP_INVALID_SYNTAX;
+                                       x.bv_val++;
+                                       x.bv_len--;
 
-       bv.bv_val = ptr + 1;
-       bv.bv_len = val->bv_len - ( ptr + 1 - val->bv_val );
+                                       /* eat leading spaces */
+                                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                                               /* empty */;
+                                       }
 
-       ptr = ber_bvchr( &bv, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
-               return LDAP_INVALID_SYNTAX;
-       }
+                                       if ( strncasecmp( x.bv_val, "directoryName:rdnSequence:", STRLENOF("directoryName:rdnSequence:") ) != 0 ) {
+                                               return LDAP_INVALID_SYNTAX;
+                                       }
+                                       x.bv_val += STRLENOF("directoryName:rdnSequence:");
+                                       x.bv_len -= STRLENOF("directoryName:rdnSequence:");
+
+                                       if ( x.bv_val[0] != '"' ) return LDAP_INVALID_SYNTAX;
+                                       x.bv_val++;
+                                       x.bv_len--;
+
+                                       is->bv_val = x.bv_val;
+                                       is->bv_len = 0;
+
+                                       for ( ; is->bv_len < x.bv_len; ) {
+                                               if ( is->bv_val[is->bv_len] != '"' ) {
+                                                       is->bv_len++;
+                                                       continue;
+                                               }
+                                               if ( is->bv_val[is->bv_len + 1] == '"' ) {
+                                                       /* double dquote */
+                                                       is->bv_len += 2;
+                                                       continue;
+                                               }
+                                               break;
+                                       }
+                                       x.bv_val += is->bv_len + 1;
+                                       x.bv_len -= is->bv_len + 1;
 
-       bv.bv_val = ptr + 1;
-       bv.bv_len = val->bv_len - ( ptr + 1 - val->bv_val );
-               
-       ptr = ber_bvchr( &bv, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
-               return LDAP_INVALID_SYNTAX;
-       }
+                                       /* eat leading spaces */
+                                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                                               /* empty */;
+                                       }
 
-       bv.bv_len = ptr - bv.bv_val;
+                                       if ( x.bv_val[0] != /*{*/ '}' ) return LDAP_INVALID_SYNTAX;
+                                       x.bv_val++;
+                                       x.bv_len--;
 
-       if ( bv.bv_len == 2 ) {
+                                       have2 |= HAVE_ISSUER;
+
+                               } else if ( strncasecmp( x.bv_val, "serial ", STRLENOF("serial ") ) == 0 ) {
+                                       if ( have2 & HAVE_SN ) {
+                                               return LDAP_INVALID_SYNTAX;
+                                       }
+
+                                       x.bv_val += STRLENOF("serial ");
+                                       x.bv_len -= STRLENOF("serial ");
+
+                                       /* eat leading spaces */
+                                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len--) {
+                                               /* empty */;
+                                       }
+
+                                       if ( checkNum( &x, i_sn ) ) {
+                                               return LDAP_INVALID_SYNTAX;
+                                       }
+
+                                       x.bv_val += i_sn->bv_len;
+                                       x.bv_len -= i_sn->bv_len;
+
+                                       have2 |= HAVE_SN;
+
+                               } else {
+                                       return LDAP_INVALID_SYNTAX;
+                               }
+
+                               /* eat leading spaces */
+                               for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                                       /* empty */;
+                               }
+
+                               if ( have2 == HAVE_ALL ) {
+                                       break;
+                               }
+
+                               if ( x.bv_val[0] != ',' ) return LDAP_INVALID_SYNTAX;
+                               x.bv_val++;
+                               x.bv_len--;
+                       } while ( 1 );
+
+                       if ( x.bv_val[0] != /*{*/ '}' ) return LDAP_INVALID_SYNTAX;
+                       x.bv_val++;
+                       x.bv_len--;
+
+                       /* eat leading spaces */
+                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                               /* empty */;
+                       }
+
+                       if ( x.bv_val[0] != /*{*/ '}' ) return LDAP_INVALID_SYNTAX;
+                       x.bv_val++;
+                       x.bv_len--;
+
+                       have |= HAVE_ISSUER;
+
+               } else if ( strncasecmp( x.bv_val, "serialNumber", STRLENOF("serialNumber") ) == 0 ) {
+                       if ( have & HAVE_SN ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+
+                       /* parse serialNumber */
+                       x.bv_val += STRLENOF("serialNumber");
+                       x.bv_len -= STRLENOF("serialNumber");
+
+                       if ( x.bv_val[0] != ' ' ) return LDAP_INVALID_SYNTAX;
+                       x.bv_val++;
+                       x.bv_len--;
+
+                       /* eat leading spaces */
+                       for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                               /* empty */;
+                       }
+                       
+                       if ( checkNum( &x, sn ) ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+
+                       x.bv_val += sn->bv_len;
+                       x.bv_len -= sn->bv_len;
+
+                       have |= HAVE_SN;
+
+               } else {
+                       return LDAP_INVALID_SYNTAX;
+               }
+
+               /* eat spaces */
+               for ( ; (x.bv_val[0] == ' ') && x.bv_len; x.bv_val++, x.bv_len-- ) {
+                       /* empty */;
+               }
+
+               if ( have == HAVE_ALL ) {
+                       break;
+               }
+
+               if ( x.bv_val[0] != ',' ) {
+                       return LDAP_INVALID_SYNTAX;
+               }
+               x.bv_val++ ;
+               x.bv_len--;
+       } while ( 1 );
+
+       /* should have no characters left... */
+       if( x.bv_len ) return LDAP_INVALID_SYNTAX;
+
+       if ( numdquotes == 0 ) {
+               ber_dupbv_x( &ni, is, ctx );
+
+       } else {
+               ber_len_t src, dst;
+
+               ni.bv_len = is->bv_len - numdquotes;
+               ni.bv_val = ber_memalloc_x( ni.bv_len + 1, ctx );
+               for ( src = 0, dst = 0; src < is->bv_len; src++, dst++ ) {
+                       if ( is->bv_val[src] == '"' ) {
+                               src++;
+                       }
+                       ni.bv_val[dst] = is->bv_val[src];
+               }
+               ni.bv_val[dst] = '\0';
+       }
+
+       *is = ni;
+
+       /* need to handle double dquotes here */
+       return 0;
+}
+
+/* X.509 PMI serialNumberAndIssuerSerialValidate */
+static int
+serialNumberAndIssuerSerialValidate(
+       Syntax *syntax,
+       struct berval *in )
+{
+       int rc;
+       struct berval sn, i, i_sn;
+
+       Debug( LDAP_DEBUG_TRACE, ">>> serialNumberAndIssuerSerialValidate: <%s>\n",
+               in->bv_val, 0, 0 );
+
+       rc = serialNumberAndIssuerSerialCheck( in, &sn, &i, &i_sn, NULL );
+       if ( rc ) {
+               goto done;
+       }
+
+       /* validate DN -- doesn't handle double dquote */ 
+       rc = dnValidate( NULL, &i );
+       if ( rc ) {
+               rc = LDAP_INVALID_SYNTAX;
+       }
+
+       if ( in->bv_val[0] == '{' && in->bv_val[in->bv_len-1] == '}' ) {
+               slap_sl_free( i.bv_val, NULL );
+       }
+
+done:;
+       Debug( LDAP_DEBUG_TRACE, "<<< serialNumberAndIssuerSerialValidate: <%s> err=%d\n",
+               in->bv_val, rc, 0 );
+
+       return rc;
+}
+
+/* X.509 PMI serialNumberAndIssuerSerialPretty */
+static int
+serialNumberAndIssuerSerialPretty(
+       Syntax *syntax,
+       struct berval *in,
+       struct berval *out,
+       void *ctx )
+{
+       struct berval sn, i, i_sn, ni = BER_BVNULL;
+       char *p;
+       int rc;
+
+       assert( in != NULL );
+       assert( out != NULL );
+
+       Debug( LDAP_DEBUG_TRACE, ">>> serialNumberAndIssuerSerialPretty: <%s>\n",
+               in->bv_val, 0, 0 );
+
+       rc = serialNumberAndIssuerSerialCheck( in, &sn, &i, &i_sn, ctx );
+       if ( rc ) {
+               goto done;
+       }
+
+       rc = dnPretty( syntax, &i, &ni, ctx );
+
+       if ( in->bv_val[0] == '{' && in->bv_val[in->bv_len-1] == '}' ) {
+               slap_sl_free( i.bv_val, ctx );
+       }
+
+       if ( rc ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto done;
+       }
+
+       /* make room from sn + "$" */
+       out->bv_len = STRLENOF("{ serialNumber , issuer { baseCertificateID { issuer { directoryName:rdnSequence:\"\" }, serial  } } }")
+               + sn.bv_len + ni.bv_len + i_sn.bv_len;
+       out->bv_val = slap_sl_malloc( out->bv_len + 1, ctx );
+
+       if ( out->bv_val == NULL ) {
+               out->bv_len = 0;
+               rc = LDAP_OTHER;
+               goto done;
+       }
+
+       p = out->bv_val;
+       p = lutil_strcopy( p, "{ serialNumber " );
+       p = lutil_strncopy( p, sn.bv_val, sn.bv_len );
+       p = lutil_strcopy( p, ", issuer { baseCertificateID { issuer { directoryName:rdnSequence:\"" );
+       p = lutil_strncopy( p, ni.bv_val, ni.bv_len );
+       p = lutil_strcopy( p, "\" }, serial " );
+       p = lutil_strncopy( p, i_sn.bv_val, i_sn.bv_len );
+       p = lutil_strcopy( p, " } } }" );
+
+       assert( p == &out->bv_val[out->bv_len] );
+
+done:;
+       Debug( LDAP_DEBUG_TRACE, "<<< serialNumberAndIssuerSerialPretty: <%s> => <%s>\n",
+               in->bv_val, rc == LDAP_SUCCESS ? out->bv_val : "(err)", 0 );
+
+       slap_sl_free( ni.bv_val, ctx );
+
+       return rc; 
+}
+
+/* X.509 PMI serialNumberAndIssuerSerialNormalize */
+/*
+ * This routine is called by attributeCertificateExactNormalize
+ * when attributeCertificateExactNormalize receives a search 
+ * string instead of a attribute certificate. This routine 
+ * checks if the search value is valid and then returns the 
+ * normalized value
+ */
+static int
+serialNumberAndIssuerSerialNormalize(
+       slap_mask_t usage,
+       Syntax *syntax,
+       MatchingRule *mr,
+       struct berval *in,
+       struct berval *out,
+       void *ctx )
+{
+       struct berval i, ni = BER_BVNULL,
+               sn, sn2 = BER_BVNULL, sn3 = BER_BVNULL,
+               i_sn, i_sn2 = BER_BVNULL, i_sn3 = BER_BVNULL;
+       char sbuf2[SLAP_SN_BUFLEN], i_sbuf2[SLAP_SN_BUFLEN],
+               sbuf3[SLAP_SN_BUFLEN], i_sbuf3[SLAP_SN_BUFLEN];
+       char *p;
+       int rc;
+
+       assert( in != NULL );
+       assert( out != NULL );
+
+       Debug( LDAP_DEBUG_TRACE, ">>> serialNumberAndIssuerSerialNormalize: <%s>\n",
+               in->bv_val, 0, 0 );
+
+       rc = serialNumberAndIssuerSerialCheck( in, &sn, &i, &i_sn, ctx );
+       if ( rc ) {
+               goto func_leave;
+       }
+
+       rc = dnNormalize( usage, syntax, mr, &i, &ni, ctx );
+
+       if ( in->bv_val[0] == '{' && in->bv_val[in->bv_len-1] == '}' ) {
+               slap_sl_free( i.bv_val, ctx );
+       }
+
+       if ( rc ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto func_leave;
+       }
+
+       /* Convert sn to canonical hex */
+       sn2.bv_val = sbuf2;
+       sn2.bv_len = sn.bv_len;
+       if ( sn.bv_len > sizeof( sbuf2 ) ) {
+               sn2.bv_val = slap_sl_malloc( sn.bv_len, ctx );
+       }
+       if ( lutil_str2bin( &sn, &sn2, ctx ) ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto func_leave;
+       }
+
+        /* Convert i_sn to canonical hex */
+       i_sn2.bv_val = i_sbuf2;
+       i_sn2.bv_len = i_sn.bv_len;
+       if ( i_sn.bv_len > sizeof( i_sbuf2 ) ) {
+               i_sn2.bv_val = slap_sl_malloc( i_sn.bv_len, ctx );
+       }
+       if ( lutil_str2bin( &i_sn, &i_sn2, ctx ) ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto func_leave;
+       }
+
+       sn3.bv_val = sbuf3;
+       sn3.bv_len = sizeof(sbuf3);
+       if ( slap_bin2hex( &sn2, &sn3, ctx ) ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto func_leave;
+       }
+
+       i_sn3.bv_val = i_sbuf3;
+       i_sn3.bv_len = sizeof(i_sbuf3);
+       if ( slap_bin2hex( &i_sn2, &i_sn3, ctx ) ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto func_leave;
+       }
+
+       out->bv_len = STRLENOF("{ serialNumber , issuer { baseCertificateID { issuer { directoryName:rdnSequence:\"\" }, serial  } } }")
+               + sn3.bv_len + ni.bv_len + i_sn3.bv_len;
+       out->bv_val = slap_sl_malloc( out->bv_len + 1, ctx );
+
+       if ( out->bv_val == NULL ) {
+               out->bv_len = 0;
+               rc = LDAP_OTHER;
+               goto func_leave;
+       }
+
+       p = out->bv_val;
+
+       p = lutil_strcopy( p, "{ serialNumber " );
+       p = lutil_strncopy( p, sn3.bv_val, sn3.bv_len );
+       p = lutil_strcopy( p, ", issuer { baseCertificateID { issuer { directoryName:rdnSequence:\"" );
+       p = lutil_strncopy( p, ni.bv_val, ni.bv_len );
+       p = lutil_strcopy( p, "\" }, serial " );
+       p = lutil_strncopy( p, i_sn3.bv_val, i_sn3.bv_len );
+       p = lutil_strcopy( p, " } } }" );
+
+       assert( p == &out->bv_val[out->bv_len] );
+
+func_leave:
+       Debug( LDAP_DEBUG_TRACE, "<<< serialNumberAndIssuerSerialNormalize: <%s> => <%s>\n",
+               in->bv_val, rc == LDAP_SUCCESS ? out->bv_val : "(err)", 0 );
+
+       if ( sn2.bv_val != sbuf2 ) {
+               slap_sl_free( sn2.bv_val, ctx );
+       }
+
+       if ( i_sn2.bv_val != i_sbuf2 ) {
+               slap_sl_free( i_sn2.bv_val, ctx );
+       }
+
+       if ( sn3.bv_val != sbuf3 ) {
+               slap_sl_free( sn3.bv_val, ctx );
+       }
+
+       if ( i_sn3.bv_val != i_sbuf3 ) {
+               slap_sl_free( i_sn3.bv_val, ctx );
+       }
+
+       slap_sl_free( ni.bv_val, ctx );
+
+       return rc;
+}
+
+/* X.509 PMI attributeCertificateExactNormalize */
+static int
+attributeCertificateExactNormalize(
+       slap_mask_t usage,
+       Syntax *syntax,
+       MatchingRule *mr,
+       struct berval *val,
+       struct berval *normalized,
+       void *ctx )
+{
+       BerElementBuffer berbuf;
+       BerElement *ber = (BerElement *)&berbuf;
+       ber_tag_t tag;
+       ber_len_t len;
+       char issuer_serialbuf[SLAP_SN_BUFLEN], serialbuf[SLAP_SN_BUFLEN];
+       struct berval sn, i_sn, sn2, i_sn2;
+       struct berval issuer_dn = BER_BVNULL, bvdn;
+       char *p;
+       int rc = LDAP_INVALID_SYNTAX;
+
+       if ( BER_BVISEMPTY( val ) ) {
+               goto done;
+       }
+
+       if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX(usage) ) {
+               return serialNumberAndIssuerSerialNormalize( 0, NULL, NULL, val, normalized, ctx );
+       }
+
+       assert( SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) != 0 );
+
+       ber_init2( ber, val, LBER_USE_DER );
+       tag = ber_skip_tag( ber, &len );        /* Signed Sequence */
+       tag = ber_skip_tag( ber, &len );        /* Sequence */
+       tag = ber_skip_tag( ber, &len );        /* (Mandatory) version; must be v2(1) */
+       ber_skip_data( ber, len );
+       tag = ber_skip_tag( ber, &len );        /* Holder Sequence */
+       ber_skip_data( ber, len );
+
+       /* Issuer */
+       tag = ber_skip_tag( ber, &len );        /* Sequence */
+                                               /* issuerName (GeneralNames sequence; optional)? */
+       tag = ber_skip_tag( ber, &len );        /* baseCertificateID (sequence; optional)? */
+       tag = ber_skip_tag( ber, &len );        /* GeneralNames (sequence) */
+       tag = ber_skip_tag( ber, &len );        /* directoryName (we only accept this form of GeneralName) */
+       if ( tag != SLAP_X509_GN_DIRECTORYNAME ) { 
+               rc = LDAP_INVALID_SYNTAX; 
+               goto done;
+       }
+       tag = ber_peek_tag( ber, &len );        /* sequence of RDN */
+       len = ber_ptrlen( ber );
+       bvdn.bv_val = val->bv_val + len;
+       bvdn.bv_len = val->bv_len - len;
+       rc = dnX509normalize( &bvdn, &issuer_dn );
+       if ( rc != LDAP_SUCCESS ) goto done;
+       
+       tag = ber_skip_tag( ber, &len );        /* sequence of RDN */
+       ber_skip_data( ber, len ); 
+       tag = ber_skip_tag( ber, &len );        /* serial number */
+       if ( tag != LBER_INTEGER ) {
+               rc = LDAP_INVALID_SYNTAX; 
+               goto done;
+       }
+       i_sn.bv_val = (char *)ber->ber_ptr;
+       i_sn.bv_len = len;
+       i_sn2.bv_val = issuer_serialbuf;
+       i_sn2.bv_len = sizeof(issuer_serialbuf);
+       if ( slap_bin2hex( &i_sn, &i_sn2, ctx ) ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto done;
+       }
+       ber_skip_data( ber, len );
+
+                                               /* issuerUID (bitstring; optional)? */
+                                               /* objectDigestInfo (sequence; optional)? */
+
+       tag = ber_skip_tag( ber, &len );        /* Signature (sequence) */
+       ber_skip_data( ber, len );
+       tag = ber_skip_tag( ber, &len );        /* serial number */ 
+       if ( tag != LBER_INTEGER ) {
+               rc = LDAP_INVALID_SYNTAX; 
+               goto done;
+       }
+       sn.bv_val = (char *)ber->ber_ptr;
+       sn.bv_len = len;
+       sn2.bv_val = serialbuf;
+       sn2.bv_len = sizeof(serialbuf);
+       if ( slap_bin2hex( &sn, &sn2, ctx ) ) {
+               rc = LDAP_INVALID_SYNTAX;
+               goto done;
+       }
+       ber_skip_data( ber, len );
+
+       normalized->bv_len = STRLENOF( "{ serialNumber , issuer { baseCertificateID { issuer { directoryName:rdnSequence:\"\" }, serial  } } }" )
+               + sn2.bv_len + issuer_dn.bv_len + i_sn2.bv_len;
+       normalized->bv_val = ch_malloc( normalized->bv_len + 1 );
+
+       p = normalized->bv_val;
+
+       p = lutil_strcopy( p, "{ serialNumber " );
+       p = lutil_strncopy( p, sn2.bv_val, sn2.bv_len );
+       p = lutil_strcopy( p, ", issuer { baseCertificateID { issuer { directoryName:rdnSequence:\"" );
+       p = lutil_strncopy( p, issuer_dn.bv_val, issuer_dn.bv_len );
+       p = lutil_strcopy( p, "\" }, serial " );
+       p = lutil_strncopy( p, i_sn2.bv_val, i_sn2.bv_len );
+       p = lutil_strcopy( p, " } } }" );
+
+       Debug( LDAP_DEBUG_TRACE, "attributeCertificateExactNormalize: %s\n",
+               normalized->bv_val, NULL, NULL );
+
+       rc = LDAP_SUCCESS;
+
+done:
+       if ( issuer_dn.bv_val ) ber_memfree( issuer_dn.bv_val );
+       if ( i_sn2.bv_val != issuer_serialbuf ) ber_memfree_x( i_sn2.bv_val, ctx );
+       if ( sn2.bv_val != serialbuf ) ber_memfree_x( sn2.bv_val, ctx );
+
+       return rc;
+}
+
+
+static int
+hexValidate(
+       Syntax *syntax,
+       struct berval *in )
+{
+       ber_len_t       i;
+
+       assert( in != NULL );
+       assert( !BER_BVISNULL( in ) );
+
+       for ( i = 0; i < in->bv_len; i++ ) {
+               if ( !ASCII_HEX( in->bv_val[ i ] ) ) {
+                       return LDAP_INVALID_SYNTAX;
+               }
+       }
+
+       return LDAP_SUCCESS;
+}
+
+/* Normalize a SID as used inside a CSN:
+ * three-digit numeric string */
+static int
+hexNormalize(
+       slap_mask_t usage,
+       Syntax *syntax,
+       MatchingRule *mr,
+       struct berval *val,
+       struct berval *normalized,
+       void *ctx )
+{
+       ber_len_t       i;
+
+       assert( val != NULL );
+       assert( normalized != NULL );
+
+       ber_dupbv_x( normalized, val, ctx );
+
+       for ( i = 0; i < normalized->bv_len; i++ ) {
+               if ( !ASCII_HEX( normalized->bv_val[ i ] ) ) {
+                       ber_memfree_x( normalized->bv_val, ctx );
+                       BER_BVZERO( normalized );
+                       return LDAP_INVALID_SYNTAX;
+               }
+
+               normalized->bv_val[ i ] = TOLOWER( normalized->bv_val[ i ] );
+       }
+
+       return LDAP_SUCCESS;
+}
+
+static int
+sidValidate (
+       Syntax *syntax,
+       struct berval *in )
+{
+       assert( in != NULL );
+       assert( !BER_BVISNULL( in ) );
+
+       if ( in->bv_len != 3 ) {
+               return LDAP_INVALID_SYNTAX;
+       }
+
+       return hexValidate( NULL, in );
+}
+
+/* Normalize a SID as used inside a CSN:
+ * three-digit numeric string */
+static int
+sidNormalize(
+       slap_mask_t usage,
+       Syntax *syntax,
+       MatchingRule *mr,
+       struct berval *val,
+       struct berval *normalized,
+       void *ctx )
+{
+       if ( val->bv_len != 3 ) {
+               return LDAP_INVALID_SYNTAX;
+       }
+
+       return hexNormalize( 0, NULL, NULL, val, normalized, ctx );
+}
+
+static int
+sidPretty(
+       Syntax *syntax,
+       struct berval *val,
+       struct berval *out,
+       void *ctx )
+{
+       return sidNormalize( SLAP_MR_VALUE_OF_SYNTAX, NULL, NULL, val, out, ctx );
+}
+
+/* Normalize a SID as used inside a CSN, either as-is
+ * (assertion value) or extracted from the CSN
+ * (attribute value) */
+static int
+csnSidNormalize(
+       slap_mask_t usage,
+       Syntax *syntax,
+       MatchingRule *mr,
+       struct berval *val,
+       struct berval *normalized,
+       void *ctx )
+{
+       struct berval   bv;
+       char            *ptr,
+                       buf[ 4 ];
+
+
+       if ( BER_BVISEMPTY( val ) ) {
+               return LDAP_INVALID_SYNTAX;
+       }
+
+       if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX(usage) ) {
+               return sidNormalize( 0, NULL, NULL, val, normalized, ctx );
+       }
+
+       assert( SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) != 0 );
+
+       ptr = ber_bvchr( val, '#' );
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
+               return LDAP_INVALID_SYNTAX;
+       }
+
+       bv.bv_val = ptr + 1;
+       bv.bv_len = val->bv_len - ( ptr + 1 - val->bv_val );
+
+       ptr = ber_bvchr( &bv, '#' );
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
+               return LDAP_INVALID_SYNTAX;
+       }
+
+       bv.bv_val = ptr + 1;
+       bv.bv_len = val->bv_len - ( ptr + 1 - val->bv_val );
+               
+       ptr = ber_bvchr( &bv, '#' );
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
+               return LDAP_INVALID_SYNTAX;
+       }
+
+       bv.bv_len = ptr - bv.bv_val;
+
+       if ( bv.bv_len == 2 ) {
                /* OpenLDAP 2.3 SID */
                buf[ 0 ] = '0';
                buf[ 1 ] = bv.bv_val[ 0 ];
@@ -4100,7 +4910,7 @@ csnValidate(
        bv = *in;
 
        ptr = ber_bvchr( &bv, '#' );
-       if ( ptr == NULL || ptr - bv.bv_val == bv.bv_len ) {
+       if ( ptr == NULL || ptr == &bv.bv_val[bv.bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4120,7 +4930,7 @@ csnValidate(
        bv.bv_len = in->bv_len - ( bv.bv_val - in->bv_val );
 
        ptr = ber_bvchr( &bv, '#' );
-       if ( ptr == NULL || ptr - in->bv_val == in->bv_len ) {
+       if ( ptr == NULL || ptr == &in->bv_val[in->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4138,7 +4948,7 @@ csnValidate(
        bv.bv_len = in->bv_len - ( bv.bv_val - in->bv_val );
 
        ptr = ber_bvchr( &bv, '#' );
-       if ( ptr == NULL || ptr - in->bv_val == in->bv_len ) {
+       if ( ptr == NULL || ptr == &in->bv_val[in->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4178,7 +4988,7 @@ csnNormalize21(
        struct berval   bv;
        char            buf[ STRLENOF( "YYYYmmddHHMMSS.uuuuuuZ#SSSSSS#SID#ssssss" ) + 1 ];
        char            *ptr;
-       int             i;
+       ber_len_t       i;
 
        assert( SLAP_MR_IS_VALUE_OF_SYNTAX( usage ) != 0 );
        assert( !BER_BVISEMPTY( val ) );
@@ -4186,7 +4996,7 @@ csnNormalize21(
        gt = *val;
 
        ptr = ber_bvchr( &gt, '#' );
-       if ( ptr == NULL || ptr - gt.bv_val == gt.bv_len ) {
+       if ( ptr == NULL || ptr == &gt.bv_val[gt.bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4203,7 +5013,7 @@ csnNormalize21(
        cnt.bv_len = val->bv_len - ( cnt.bv_val - val->bv_val );
 
        ptr = ber_bvchr( &cnt, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4223,7 +5033,7 @@ csnNormalize21(
        sid.bv_len = val->bv_len - ( sid.bv_val - val->bv_val );
                
        ptr = ber_bvchr( &sid, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4261,7 +5071,7 @@ csnNormalize21(
        }
        *ptr = '\0';
 
-       assert( ptr - bv.bv_val == bv.bv_len );
+       assert( ptr == &bv.bv_val[bv.bv_len] );
 
        if ( csnValidate( syntax, &bv ) != LDAP_SUCCESS ) {
                return LDAP_INVALID_SYNTAX;
@@ -4286,7 +5096,7 @@ csnNormalize23(
        struct berval   bv;
        char            buf[ STRLENOF( "YYYYmmddHHMMSS.uuuuuuZ#SSSSSS#SID#ssssss" ) + 1 ];
        char            *ptr;
-       int             i;
+       ber_len_t       i;
 
        assert( SLAP_MR_IS_VALUE_OF_SYNTAX( usage ) != 0 );
        assert( !BER_BVISEMPTY( val ) );
@@ -4294,7 +5104,7 @@ csnNormalize23(
        gt = *val;
 
        ptr = ber_bvchr( &gt, '#' );
-       if ( ptr == NULL || ptr - gt.bv_val == gt.bv_len ) {
+       if ( ptr == NULL || ptr == &gt.bv_val[gt.bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4307,7 +5117,7 @@ csnNormalize23(
        cnt.bv_len = val->bv_len - ( cnt.bv_val - val->bv_val );
 
        ptr = ber_bvchr( &cnt, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4320,7 +5130,7 @@ csnNormalize23(
        sid.bv_len = val->bv_len - ( sid.bv_val - val->bv_val );
                
        ptr = ber_bvchr( &sid, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4353,7 +5163,7 @@ csnNormalize23(
        }
        *ptr = '\0';
 
-       assert( ptr - bv.bv_val == bv.bv_len );
+       assert( ptr == &bv.bv_val[bv.bv_len] );
        if ( csnValidate( syntax, &bv ) != LDAP_SUCCESS ) {
                return LDAP_INVALID_SYNTAX;
        }
@@ -4375,7 +5185,7 @@ csnNormalize(
 {
        struct berval   cnt, sid, mod;
        char            *ptr;
-       int             i;
+       ber_len_t       i;
 
        assert( val != NULL );
        assert( normalized != NULL );
@@ -4403,7 +5213,7 @@ csnNormalize(
        }
 
        ptr = ber_bvchr( val, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4415,7 +5225,7 @@ csnNormalize(
        cnt.bv_len = val->bv_len - ( cnt.bv_val - val->bv_val );
 
        ptr = ber_bvchr( &cnt, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -4427,7 +5237,7 @@ csnNormalize(
        sid.bv_len = val->bv_len - ( sid.bv_val - val->bv_val );
                
        ptr = ber_bvchr( &sid, '#' );
-       if ( ptr == NULL || ptr - val->bv_val == val->bv_len ) {
+       if ( ptr == NULL || ptr == &val->bv_val[val->bv_len] ) {
                return LDAP_INVALID_SYNTAX;
        }
 
@@ -5185,6 +5995,10 @@ static slap_syntax_defs_rec syntax_defs[] = {
                X_BINARY X_NOT_H_R ")",
                SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER,
                NULL, sequenceValidate, NULL},
+       {"( " attributeCertificateSyntaxOID " DESC 'X.509 AttributeCertificate' "
+               X_BINARY X_NOT_H_R ")",
+               SLAP_SYNTAX_BINARY|SLAP_SYNTAX_BER,
+               NULL, attributeCertificateValidate, NULL},
 #if 0  /* need to go __after__ printableString */
        {"( 1.3.6.1.4.1.1466.115.121.1.11 DESC 'Country String' )",
                0, "1.3.6.1.4.1.1466.115.121.1.44",
@@ -5318,6 +6132,12 @@ static slap_syntax_defs_rec syntax_defs[] = {
                SLAP_SYNTAX_HIDE, NULL, NULL, NULL},
        {"( 1.3.6.1.1.15.7 DESC 'Algorithm Identifier' )",
                SLAP_SYNTAX_HIDE, NULL, NULL, NULL},
+       {"( " attributeCertificateExactAssertionSyntaxOID " DESC 'AttributeCertificate Exact Assertion' )",
+               SLAP_SYNTAX_HIDE, NULL,
+               serialNumberAndIssuerSerialValidate,
+               serialNumberAndIssuerSerialPretty},
+       {"( " attributeCertificateAssertionSyntaxOID " DESC 'AttributeCertificate Assertion' )",
+               SLAP_SYNTAX_HIDE, NULL, NULL, NULL},
 
 #ifdef SLAPD_AUTHPASSWD
        /* needs updating */
@@ -5357,12 +6177,20 @@ char *certificateListExactMatchSyntaxes[] = {
        "1.3.6.1.4.1.1466.115.121.1.9" /* certificateList */,
        NULL
 };
+char *attributeCertificateExactMatchSyntaxes[] = {
+       attributeCertificateSyntaxOID  /* attributeCertificate */,
+       NULL
+};
+
 #ifdef LDAP_COMP_MATCH
 char *componentFilterMatchSyntaxes[] = {
        "1.3.6.1.4.1.1466.115.121.1.8" /* certificate */,
+       "1.3.6.1.4.1.1466.115.121.1.9" /* certificateList */,
+       attributeCertificateSyntaxOID /* attributeCertificate */,
        NULL
 };
 #endif
+
 char *directoryStringSyntaxes[] = {
        "1.3.6.1.4.1.1466.115.121.1.44" /* printableString */,
        NULL
@@ -5394,8 +6222,6 @@ char *objectIdentifierFirstComponentMatchSyntaxes[] = {
  * 2.5.13.33*  keywordMatch
  * 2.5.13.36+  certificatePairExactMatch
  * 2.5.13.37+  certificatePairMatch
- * 2.5.13.38+  certificateListExactMatch
- * 2.5.13.39+  certificateListMatch
  * 2.5.13.40+  algorithmIdentifierMatch
  * 2.5.13.41*  storedPrefixMatch
  * 2.5.13.42   attributeCertificateMatch
@@ -5702,13 +6528,26 @@ static slap_mrule_defs_rec mrule_defs[] = {
 
        {"( 2.5.13.38 NAME 'certificateListExactMatch' "
                "SYNTAX 1.3.6.1.1.15.5 )",
-               SLAP_MR_EQUALITY | SLAP_MR_EXT | SLAP_MR_HIDE, certificateListExactMatchSyntaxes,
+               SLAP_MR_EQUALITY | SLAP_MR_EXT, certificateListExactMatchSyntaxes,
                NULL, certificateListExactNormalize, octetStringMatch,
                octetStringIndexer, octetStringFilter,
                NULL },
 
        {"( 2.5.13.39 NAME 'certificateListMatch' "
                "SYNTAX 1.3.6.1.1.15.6 )",
+               SLAP_MR_EQUALITY | SLAP_MR_EXT, NULL,
+               NULL, NULL, NULL, NULL, NULL,
+               NULL },
+
+       {"( 2.5.13.45 NAME 'attributeCertificateExactMatch' "
+               "SYNTAX " attributeCertificateExactAssertionSyntaxOID " )",
+               SLAP_MR_EQUALITY | SLAP_MR_EXT | SLAP_MR_HIDE, attributeCertificateExactMatchSyntaxes,
+               NULL, attributeCertificateExactNormalize, octetStringMatch,
+               octetStringIndexer, octetStringFilter,
+               NULL },
+
+       {"( 2.5.13.46 NAME 'attributeCertificateMatch' "
+               "SYNTAX " attributeCertificateAssertionSyntaxOID " )",
                SLAP_MR_EQUALITY | SLAP_MR_EXT | SLAP_MR_HIDE, NULL,
                NULL, NULL, NULL, NULL, NULL,
                NULL },
@@ -5789,7 +6628,7 @@ static slap_mrule_defs_rec mrule_defs[] = {
        {"( 1.3.6.1.4.1.4203.666.11.2.3 NAME 'CSNOrderingMatch' "
                "SYNTAX 1.3.6.1.4.1.4203.666.11.2.1 )",
                SLAP_MR_HIDE | SLAP_MR_ORDERING | SLAP_MR_ORDERED_INDEX, NULL,
-               NULL, NULL, csnOrderingMatch,
+               NULL, csnNormalize, csnOrderingMatch,
                NULL, NULL,
                "CSNMatch" },