+static int
+integerBitAndMatch(
+ int *matchp,
+ slap_mask_t flags,
+ Syntax *syntax,
+ MatchingRule *mr,
+ struct berval *value,
+ void *assertedValue )
+{
+ long lValue, lAssertedValue;
+
+ /* safe to assume integers are NUL terminated? */
+ lValue = strtoul(value->bv_val, NULL, 10);
+ if(( lValue == LONG_MIN || lValue == LONG_MAX) && errno == ERANGE )
+ return LDAP_CONSTRAINT_VIOLATION;
+
+ lAssertedValue = strtol(((struct berval *)assertedValue)->bv_val, NULL, 10);
+ if(( lAssertedValue == LONG_MIN || lAssertedValue == LONG_MAX) && errno == ERANGE )
+ return LDAP_CONSTRAINT_VIOLATION;
+
+ *matchp = (lValue & lAssertedValue);
+ return LDAP_SUCCESS;
+}
+
+static int
+integerBitOrMatch(
+ int *matchp,
+ slap_mask_t flags,
+ Syntax *syntax,
+ MatchingRule *mr,
+ struct berval *value,
+ void *assertedValue )
+{
+ long lValue, lAssertedValue;
+
+ /* safe to assume integers are NUL terminated? */
+ lValue = strtoul(value->bv_val, NULL, 10);
+ if(( lValue == LONG_MIN || lValue == LONG_MAX) && errno == ERANGE )
+ return LDAP_CONSTRAINT_VIOLATION;
+
+ lAssertedValue = strtol(((struct berval *)assertedValue)->bv_val, NULL, 10);
+ if(( lAssertedValue == LONG_MIN || lAssertedValue == LONG_MAX) && errno == ERANGE )
+ return LDAP_CONSTRAINT_VIOLATION;
+
+ *matchp = (lValue | lAssertedValue);
+ return LDAP_SUCCESS;
+}
+
+#ifdef HAVE_TLS
+#include <openssl/x509.h>
+#include <openssl/err.h>
+char digit[] = "0123456789";
+
+/*
+ * Next function returns a string representation of a ASN1_INTEGER.
+ * It works for unlimited lengths.
+ */
+
+static struct berval *
+asn1_integer2str(ASN1_INTEGER *a, struct berval *bv)
+{
+ char buf[256];
+ char *p;
+
+ /* We work backwards, make it fill from the end of buf */
+ p = buf + sizeof(buf) - 1;
+ *p = '\0';
+
+ if ( a == NULL || a->length == 0 ) {
+ *--p = '0';
+ } else {
+ int i;
+ int n = a->length;
+ int base = 0;
+ unsigned int *copy;
+
+ /* We want to preserve the original */
+ copy = ch_malloc(n*sizeof(unsigned int));
+ for (i = 0; i<n; i++) {
+ copy[i] = a->data[i];
+ }
+
+ /*
+ * base indicates the index of the most significant
+ * byte that might be nonzero. When it goes off the
+ * end, we now there is nothing left to do.
+ */
+ while (base < n) {
+ unsigned int carry;
+
+ carry = 0;
+ for (i = base; i<n; i++ ) {
+ copy[i] += carry*256;
+ carry = copy[i] % 10;
+ copy[i] /= 10;
+ }
+ if (p <= buf+1) {
+ /*
+ * Way too large, we need to leave
+ * room for sign if negative
+ */
+ free(copy);
+ return NULL;
+ }
+ *--p = digit[carry];
+ if (copy[base] == 0)
+ base++;
+ }
+ free(copy);
+ }
+
+ if ( a->type == V_ASN1_NEG_INTEGER ) {
+ *--p = '-';
+ }
+
+ return ber_str2bv( p, 0, 1, bv );
+}
+
+/*
+ * Given a certificate in DER format, extract the corresponding
+ * assertion value for certificateExactMatch
+ */
+static int
+certificateExactConvert(
+ struct berval * in,
+ struct berval * out )
+{
+ X509 *xcert;
+ unsigned char *p = in->bv_val;
+ struct berval serial;
+ struct berval issuer_dn;
+
+ xcert = d2i_X509(NULL, &p, in->bv_len);
+ if ( !xcert ) {
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONFIG, ENTRY,
+ "certificateExactConvert: error parsing cert: %s\n",
+ ERR_error_string(ERR_get_error(),NULL), 0, 0 );
+#else
+ Debug( LDAP_DEBUG_ARGS, "certificateExactConvert: "
+ "error parsing cert: %s\n",
+ ERR_error_string(ERR_get_error(),NULL), NULL, NULL );
+#endif
+ return LDAP_INVALID_SYNTAX;
+ }
+
+ if ( !asn1_integer2str(xcert->cert_info->serialNumber, &serial) ) {
+ X509_free(xcert);
+ return LDAP_INVALID_SYNTAX;
+ }
+ if ( dnX509normalize(X509_get_issuer_name(xcert), &issuer_dn ) != LDAP_SUCCESS ) {
+ X509_free(xcert);
+ ber_memfree(serial.bv_val);
+ return LDAP_INVALID_SYNTAX;
+ }
+
+ X509_free(xcert);
+
+ out->bv_len = serial.bv_len + issuer_dn.bv_len + sizeof(" $ ");
+ out->bv_val = ch_malloc(out->bv_len);
+ p = out->bv_val;
+ AC_MEMCPY(p, serial.bv_val, serial.bv_len);
+ p += serial.bv_len;
+ AC_MEMCPY(p, " $ ", sizeof(" $ ")-1);
+ p += 3;
+ AC_MEMCPY(p, issuer_dn.bv_val, issuer_dn.bv_len);
+ p += issuer_dn.bv_len;
+ *p++ = '\0';
+
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONFIG, ARGS,
+ "certificateExactConvert: \n %s\n", out->bv_val, 0, 0 );
+#else
+ Debug( LDAP_DEBUG_ARGS, "certificateExactConvert "
+ "\n\t\"%s\"\n",
+ out->bv_val, NULL, NULL );
+#endif
+
+ ber_memfree(serial.bv_val);
+ ber_memfree(issuer_dn.bv_val);
+
+ return LDAP_SUCCESS;
+}
+
+static int
+serial_and_issuer_parse(
+ struct berval *assertion,
+ struct berval *serial,
+ struct berval *issuer_dn
+)
+{
+ char *begin;
+ char *end;
+ char *p;
+ struct berval bv;
+
+ begin = assertion->bv_val;
+ end = assertion->bv_val+assertion->bv_len-1;
+ for (p=begin; p<=end && *p != '$'; p++)
+ ;
+ if ( p > end )
+ return LDAP_INVALID_SYNTAX;
+
+ /* p now points at the $ sign, now use begin and end to delimit the
+ serial number */
+ while (ASCII_SPACE(*begin))
+ begin++;
+ end = p-1;
+ while (ASCII_SPACE(*end))
+ end--;
+
+ bv.bv_len = end-begin+1;
+ bv.bv_val = begin;
+ ber_dupbv(serial, &bv);
+
+ /* now extract the issuer, remember p was at the dollar sign */
+ if ( issuer_dn ) {
+ begin = p+1;
+ end = assertion->bv_val+assertion->bv_len-1;
+ while (ASCII_SPACE(*begin))
+ begin++;
+ /* should we trim spaces at the end too? is it safe always? */
+
+ bv.bv_len = end-begin+1;
+ bv.bv_val = begin;
+ dnNormalize2( NULL, &bv, issuer_dn );
+ }
+
+ return LDAP_SUCCESS;
+}
+
+static int
+certificateExactMatch(
+ int *matchp,
+ slap_mask_t flags,
+ Syntax *syntax,
+ MatchingRule *mr,
+ struct berval *value,
+ void *assertedValue )
+{
+ X509 *xcert;
+ unsigned char *p = value->bv_val;
+ struct berval serial;
+ struct berval issuer_dn;
+ struct berval asserted_serial;
+ struct berval asserted_issuer_dn;
+ int ret;
+
+ xcert = d2i_X509(NULL, &p, value->bv_len);
+ if ( !xcert ) {
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONFIG, ENTRY,
+ "certificateExactMatch: error parsing cert: %s\n",
+ ERR_error_string(ERR_get_error(),NULL), 0, 0 );
+#else
+ Debug( LDAP_DEBUG_ARGS, "certificateExactMatch: "
+ "error parsing cert: %s\n",
+ ERR_error_string(ERR_get_error(),NULL), NULL, NULL );
+#endif
+ return LDAP_INVALID_SYNTAX;
+ }
+
+ asn1_integer2str(xcert->cert_info->serialNumber, &serial);
+ dnX509normalize(X509_get_issuer_name(xcert), &issuer_dn);
+
+ X509_free(xcert);
+
+ serial_and_issuer_parse(assertedValue,
+ &asserted_serial,
+ &asserted_issuer_dn);
+
+ ret = integerMatch(
+ matchp,
+ flags,
+ slap_schema.si_syn_integer,
+ slap_schema.si_mr_integerMatch,
+ &serial,
+ &asserted_serial);
+ if ( ret == LDAP_SUCCESS ) {
+ if ( *matchp == 0 ) {
+ /* We need to normalize everything for dnMatch */
+ ret = dnMatch(
+ matchp,
+ flags,
+ slap_schema.si_syn_distinguishedName,
+ slap_schema.si_mr_distinguishedNameMatch,
+ &issuer_dn,
+ &asserted_issuer_dn);
+ }
+ }
+
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONFIG, ARGS, "certificateExactMatch "
+ "%d\n\t\"%s $ %s\"\n",
+ *matchp, serial.bv_val, issuer_dn.bv_val );
+ LDAP_LOG( CONFIG, ARGS, "\t\"%s $ %s\"\n",
+ asserted_serial.bv_val, asserted_issuer_dn.bv_val,
+ 0 );
+#else
+ Debug( LDAP_DEBUG_ARGS, "certificateExactMatch "
+ "%d\n\t\"%s $ %s\"\n",
+ *matchp, serial.bv_val, issuer_dn.bv_val );
+ Debug( LDAP_DEBUG_ARGS, "\t\"%s $ %s\"\n",
+ asserted_serial.bv_val, asserted_issuer_dn.bv_val,
+ NULL );
+#endif
+
+ ber_memfree(serial.bv_val);
+ ber_memfree(issuer_dn.bv_val);
+ ber_memfree(asserted_serial.bv_val);
+ ber_memfree(asserted_issuer_dn.bv_val);
+
+ return ret;
+}
+
+/*
+ * Index generation function
+ * We just index the serials, in most scenarios the issuer DN is one of
+ * a very small set of values.
+ */
+static int certificateExactIndexer(
+ slap_mask_t use,
+ slap_mask_t flags,
+ Syntax *syntax,
+ MatchingRule *mr,
+ struct berval *prefix,
+ BerVarray values,
+ BerVarray *keysp )
+{
+ int i;
+ BerVarray keys;
+ X509 *xcert;
+ unsigned char *p;
+ struct berval serial;
+
+ /* we should have at least one value at this point */
+ assert( values != NULL && values[0].bv_val != NULL );
+
+ for( i=0; values[i].bv_val != NULL; i++ ) {
+ /* empty -- just count them */
+ }
+
+ keys = ch_malloc( sizeof( struct berval ) * (i+1) );
+
+ for( i=0; values[i].bv_val != NULL; i++ ) {
+ p = values[i].bv_val;
+ xcert = d2i_X509(NULL, &p, values[i].bv_len);
+ if ( !xcert ) {
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONFIG, ENTRY,
+ "certificateExactIndexer: error parsing cert: %s\n",
+ ERR_error_string(ERR_get_error(),NULL), 0, 0);
+#else
+ Debug( LDAP_DEBUG_ARGS, "certificateExactIndexer: "
+ "error parsing cert: %s\n",
+ ERR_error_string(ERR_get_error(),NULL),
+ NULL, NULL );
+#endif
+ /* Do we leak keys on error? */
+ return LDAP_INVALID_SYNTAX;
+ }
+
+ asn1_integer2str(xcert->cert_info->serialNumber, &serial);
+ X509_free(xcert);
+ integerNormalize( slap_schema.si_syn_integer,
+ &serial,
+ &keys[i] );
+ ber_memfree(serial.bv_val);
+#ifdef NEW_LOGGING
+ LDAP_LOG( CONFIG, ENTRY,
+ "certificateExactIndexer: returning: %s\n", keys[i].bv_val, 0, 0);
+#else
+ Debug( LDAP_DEBUG_ARGS, "certificateExactIndexer: "
+ "returning: %s\n",
+ keys[i].bv_val,
+ NULL, NULL );
+#endif
+ }
+
+ keys[i].bv_val = NULL;
+ *keysp = keys;
+ return LDAP_SUCCESS;
+}
+
+/* Index generation function */
+/* We think this is always called with a value in matching rule syntax */
+static int certificateExactFilter(
+ slap_mask_t use,
+ slap_mask_t flags,
+ Syntax *syntax,
+ MatchingRule *mr,
+ struct berval *prefix,
+ void * assertValue,
+ BerVarray *keysp )
+{
+ BerVarray keys;
+ struct berval asserted_serial;
+
+ serial_and_issuer_parse(assertValue,
+ &asserted_serial,
+ NULL);
+
+ keys = ch_malloc( sizeof( struct berval ) * 2 );
+ integerNormalize( syntax, &asserted_serial, &keys[0] );
+ keys[1].bv_val = NULL;
+ *keysp = keys;
+
+ ber_memfree(asserted_serial.bv_val);
+ return LDAP_SUCCESS;
+}
+#endif
+