Beispiel #1
0
void ows_layers_storage_fill(ows * o)
{
  PGresult *res, *res_g;
  ows_layer_node *ln;
  bool filled;
  buffer *sql;
  int i, end;

  assert(o);
  assert(o->layers);

  sql = buffer_init();
  buffer_add_str(sql, "SELECT DISTINCT f_table_schema, f_table_name FROM geometry_columns");
  res = ows_psql_exec(o, sql->buf);
  buffer_empty(sql);

  buffer_add_str(sql, "SELECT DISTINCT f_table_schema, f_table_name FROM geography_columns");
  res_g = ows_psql_exec(o, sql->buf);
  buffer_free(sql);

  for (ln = o->layers->first ; ln ; ln = ln->next) {
    filled = false;

    for (i = 0, end = PQntuples(res); i < end; i++) {
      if (    buffer_cmp(ln->layer->storage->schema, (char *) PQgetvalue(res, i, 0))
           && buffer_cmp(ln->layer->storage->table,  (char *) PQgetvalue(res, i, 1))) {
        ows_layer_storage_fill(o, ln->layer, true);
        filled = true;
      }
    }

    for (i = 0, end = PQntuples(res_g); i < end; i++) {
      if (    buffer_cmp(ln->layer->storage->schema, (char *) PQgetvalue(res_g, i, 0))
           && buffer_cmp(ln->layer->storage->table,  (char *) PQgetvalue(res_g, i, 1))) {
        ows_layer_storage_fill(o, ln->layer, false);
        filled = true;
      }
    }

    if (!filled) {
      if (ln->layer->storage) ows_layer_storage_free(ln->layer->storage);
      ln->layer->storage = NULL;
    }
  }

  PQclear(res);
  PQclear(res_g);
}
Beispiel #2
0
static
void test_hmac_test_vectors(void)
{
	test_begin("test_hmac_test_vectors");

	buffer_t *pt, *ct, *key, *res;
	pt = buffer_create_dynamic(pool_datastack_create(), 50);
	key = buffer_create_dynamic(pool_datastack_create(), 20);
	ct = buffer_create_dynamic(pool_datastack_create(), 32);
	res = buffer_create_dynamic(pool_datastack_create(), 32);

	hex_to_binary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", key);
	hex_to_binary("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", pt);
	hex_to_binary("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", res);

	struct dcrypt_context_hmac *hctx;
	if (!dcrypt_ctx_hmac_create("sha256", &hctx, NULL)) {
		test_assert_failed("dcrypt_ctx_hmac_create", __FILE__, __LINE__-1);
	} else {
		dcrypt_ctx_hmac_set_key(hctx, key->data, key->used);
		test_assert(dcrypt_ctx_hmac_init(hctx, NULL));
		test_assert(dcrypt_ctx_hmac_update(hctx, pt->data, pt->used, NULL));
		test_assert(dcrypt_ctx_hmac_final(hctx, ct, NULL));
		test_assert(buffer_cmp(ct, res));
		dcrypt_ctx_hmac_destroy(&hctx);
	}

	test_end();
}
Beispiel #3
0
bool array_cmp_i(const struct array *array1, const struct array *array2)
{
	if (!array_is_created_i(array1) || array1->buffer->used == 0)
		return !array_is_created_i(array2) || array2->buffer->used == 0;

	if (!array_is_created_i(array2))
		return FALSE;

	return buffer_cmp(array1->buffer, array2->buffer);
}
Beispiel #4
0
/*
 * Retrieve the ns_uri associated to the specified ns_prefix
 */
buffer *ows_layer_ns_uri(ows_layer_list * ll, buffer * ns_prefix)
{
    ows_layer_node *ln;

    assert(ll);
    assert(ns_prefix);

    for (ln = ll->first; ln ; ln = ln->next)
        if (buffer_cmp(ln->layer->ns_prefix, ns_prefix->buf))
            return ln->layer->ns_uri;

    return (buffer *) NULL;
}
Beispiel #5
0
/*
 * Retrieve the prefix linked to the specified layer
 */
buffer *ows_layer_ns_prefix(ows_layer_list * ll, buffer * layer_name)
{
    ows_layer_node *ln;

    assert(ll);
    assert(layer_name);

    for (ln = ll->first; ln ; ln = ln->next)
        if (buffer_cmp(ln->layer->name, layer_name->buf))
            return ln->layer->ns_prefix;

    return (buffer *) NULL;
}
Beispiel #6
0
/*
 * Check if a given buffer value is or not in the list
 */
bool in_list(const list * l, const buffer * value)
{
    list_node *ln;

    assert(l);
    assert(value);

    for (ln = l->first ; ln ; ln = ln->next)
        if (value->use == ln->value->use)
            if (buffer_cmp(value, ln->value->buf))
                return true;

    return false;
}
Beispiel #7
0
/*
 * Return a list of layer names grouped by prefix
 */
list *ows_layer_list_by_ns_prefix(ows_layer_list * ll, list * layer_name, buffer * ns_prefix)
{
    list *typ;
    list_node *ln;
    buffer *layer_ns_prefix;

    assert(ll);
    assert(layer_name);
    assert(ns_prefix);

    typ = list_init();

    for (ln = layer_name->first; ln ; ln = ln->next) {
        layer_ns_prefix = ows_layer_ns_prefix(ll, ln->value);

        if (buffer_cmp(layer_ns_prefix, ns_prefix->buf))
            list_add_by_copy(typ, ln->value);
    }

    return typ;
}
Beispiel #8
0
/*
 * Parse char by char QUERY_STRING request and return an array key/value
 * (key are all lowercase)
 */
array *cgi_parse_kvp(ows * o, char *query)
{
  int i;
  bool in_key;
  buffer *key;
  buffer *val;
  array *arr;
  char string[2];

  assert(o);
  assert(query);

  key = buffer_init();
  val = buffer_init();
  arr = array_init();
  in_key = true;

  cgi_unescape_url(query);
  cgi_remove_crlf(query);
  cgi_plustospace(query);

  for (i = 0; i < CGI_QUERY_MAX && query[i] ; i++) {

    if (query[i] == '&') {

      in_key = true;

      array_add(arr, key, val);
      key = buffer_init();
      val = buffer_init();

    } else if (query[i] == '=') {
      /* char '=' inside filter key mustn't be taken into account */
      if ((!buffer_case_cmp(key, "filter") || !buffer_case_cmp(key, "outputformat")) && buffer_cmp(val, ""))
        in_key = false;
      else buffer_add(val, query[i]);
    }
    /* Check characters'CGI request */
    else {
      /* to check the regular expression, argument must be a string, not a char */
      string[0] = query[i];
      string[1] = '\0';

      if (in_key) {

        /* if word is key, only letters are allowed */
        if (check_regexp(string, "[A-Za-zà-ÿ]"))
          buffer_add(key, tolower(query[i]));
        else {
          buffer_free(key);
          buffer_free(val);
          array_free(arr);
          ows_error(o, OWS_ERROR_MISSING_PARAMETER_VALUE,
                    "QUERY_STRING contains forbidden characters", "request");
          return NULL;
        }
      } else {
        /* if word is filter key, more characters are allowed */
        if (                                 check_regexp(string, "[A-Za-zà-ÿ0-9.\\=;,():/\\*_ \\-]")
                                             || (buffer_cmp(key, "filter") && check_regexp(string, "[A-Za-zà-ÿ0-9.#\\,():/_<> %\"\'=\\*!\\-]|\\[|\\]")))
          buffer_add(val, query[i]);
        else {
          buffer_free(key);
          buffer_free(val);
          array_free(arr);
          ows_error(o, OWS_ERROR_MISSING_PARAMETER_VALUE,
                    "QUERY_STRING contains forbidden characters", "request");
          return NULL;
        }
      }
    }
  }

  if (i == CGI_QUERY_MAX) {
    buffer_free(key);
    buffer_free(val);
    array_free(arr);
    ows_error(o, OWS_ERROR_REQUEST_HTTP, "QUERY_STRING too long", "request");
    return NULL;
  }

  array_add(arr, key, val);

  return arr;
}
Beispiel #9
0
/*
 * Convert a PostgreSql type to a valid
 * OGC XMLSchema's type
 */
char *ows_psql_to_xsd(buffer * type, ows_version *version)
{
    int wfs_version;

    assert(type);
    assert(version);

    wfs_version = ows_version_get(version);

    if (buffer_case_cmp(type, "geometry")) return "gml:GeometryPropertyType";
    if (buffer_cmp(type, "geography")) return "gml:GeometryPropertyType";
    if (buffer_cmp(type, "int2")) return "short";
    if (buffer_cmp(type, "int4")) return "int";
    if (buffer_cmp(type, "int8")) return "long";
    if (buffer_cmp(type, "float4")) return "float";
    if (buffer_cmp(type, "float8")) return "double";
    if (buffer_cmp(type, "bool")) return "boolean";
    if (buffer_cmp(type, "bytea")) return "byte";
    if (buffer_cmp(type, "date")) return "date";
    if (buffer_cmp(type, "time")) return "time";
    if (buffer_ncmp(type, "numeric", 7)) return "decimal";
    if (buffer_ncmp(type, "timestamp", 9)) return "dateTime"; /* Could be also timestamptz */
    if (buffer_cmp(type, "POINT")) return "gml:PointPropertyType";
    if (buffer_cmp(type, "LINESTRING") && wfs_version == 100) return "gml:LineStringPropertyType";
    if (buffer_cmp(type, "LINESTRING") && wfs_version == 110) return "gml:CurvePropertyType";
    if (buffer_cmp(type, "POLYGON") && wfs_version == 100) return "gml:PolygonPropertyType";
    if (buffer_cmp(type, "POLYGON") && wfs_version == 110) return "gml:SurfacePropertyType";
    if (buffer_cmp(type, "MULTIPOINT")) return "gml:MultiPointPropertyType";
    if (buffer_cmp(type, "MULTILINESTRING") && wfs_version == 100) return "gml:MultiLineStringPropertyType";
    if (buffer_cmp(type, "MULTILINESTRING") && wfs_version == 110) return "gml:MultiCurvePropertyType";
    if (buffer_cmp(type, "MULTIPOLYGON") && wfs_version == 100) return "gml:MultiPolygonPropertyType";
    if (buffer_cmp(type, "MULTIPOLYGON") && wfs_version == 110) return "gml:MultiSurfacePropertyType";
    if (buffer_cmp(type, "GEOMETRYCOLLECTION")) return "gml:MultiGeometryPropertyType";

    return "string";
}
Beispiel #10
0
/*
 * Specifies the list of feature types available from the wfs
 * Used for both 1.0.0 && 1.1.0 versions
 */
static void wfs_feature_type_list(ows * o)
{
  ows_layer_node *ln;
  ows_geobbox *gb;

  int srid_int;
  buffer *srid;
  buffer *srs;
  list_node *keyword, *l_srid;
  int s;
  bool writable, retrievable;

  assert(o);

  writable = false;
  retrievable = false;

  fprintf(o->output, " <FeatureTypeList>\n");

  /* print global operations */

  if (    ows_layer_list_retrievable(o->layers)
          || ows_layer_list_writable(o->layers))
    fprintf(o->output, "  <Operations>\n");

  if (ows_layer_list_retrievable(o->layers)) {
    if (ows_version_get(o->request->version) == 100)
      fprintf(o->output, "   <Query/>\n");
    else if (ows_version_get(o->request->version) == 110)
      fprintf(o->output, " <Operation>Query</Operation>\n");

    retrievable = true;
  }

  if (ows_layer_list_writable(o->layers)) {
    if (ows_version_get(o->request->version) == 100) {
      fprintf(o->output, "   <Insert/>\n");
      fprintf(o->output, "   <Update/>\n");
      fprintf(o->output, "   <Delete/>\n");
    } else if (ows_version_get(o->request->version) == 110) {
      fprintf(o->output, "   <Operation>Insert</Operation>\n");
      fprintf(o->output, "   <Operation>Update</Operation>\n");
      fprintf(o->output, "   <Operation>Delete</Operation>\n");
    }

    writable = true;
  }

  if (    ows_layer_list_retrievable(o->layers)
          || ows_layer_list_writable(o->layers))
    fprintf(o->output, "  </Operations>\n");

  for (ln = o->layers->first ; ln ; ln = ln->next) {
    /* print each feature type */
    if (ows_layer_match_table(o, ln->layer->name)) {

      fprintf(o->output, "<FeatureType xmlns:%s=\"%s\">\n",
              ln->layer->ns_prefix->buf, ln->layer->ns_uri->buf);

      /* name */
      if (ln->layer->name) {
        for (s = 0; s < ln->layer->depth; s++) fprintf(o->output, " ");

        fprintf(o->output, " <Name>");
        buffer_flush(ows_layer_uri_to_prefix(o->layers, ln->layer->name), o->output);
        fprintf(o->output, "</Name>\n");
      }

      /* title */
      if (ln->layer->title) {
        for (s = 0; s < ln->layer->depth; s++) fprintf(o->output, " ");

        fprintf(o->output, " <Title>");
        buffer_flush(ln->layer->title, o->output);
        fprintf(o->output, "</Title>\n");
      }

      /* abstract */
      if (ln->layer->abstract) {
        for (s = 0; s < ln->layer->depth; s++) fprintf(o->output, " ");

        fprintf(o->output, " <Abstract>");
        buffer_flush(ln->layer->abstract, o->output);
        fprintf(o->output, "</Abstract>\n");
      }

      /* keywords */
      if (ln->layer->keywords) {
        for (s = 0; s < ln->layer->depth; s++) fprintf(o->output, " ");

        fprintf(o->output, " <Keywords>");

        for (keyword = ln->layer->keywords->first ; keyword ; keyword = keyword->next) {
          if (ows_version_get(o->request->version) == 100) {
            fprintf(o->output, "%s", keyword->value->buf);
            if (keyword->next) fprintf(o->output, ",");
          } else if (ows_version_get(o->request->version) == 110) {
            fprintf(o->output, "  <Keyword>");
            fprintf(o->output, "%s", keyword->value->buf);
            fprintf(o->output, "</Keyword>");
          }
        }

        fprintf(o->output, "</Keywords>\n");
      }

      /* SRS */
      srid = buffer_init();
      srid_int = ows_srs_get_srid_from_layer(o, ln->layer->name);
      buffer_add_int(srid, srid_int);
      srs = ows_srs_get_from_a_srid(o, srid_int);

      if (srs->use) {
        if (ows_version_get(o->request->version) == 100) {
          fprintf(o->output, " <SRS>");
          buffer_flush(srs, o->output);
          fprintf(o->output, "</SRS>\n");
        } else if (ows_version_get(o->request->version) == 110) {
          fprintf(o->output, " <DefaultSRS>urn:ogc:def:crs:EPSG::%s</DefaultSRS>\n", srid->buf);

          if (ln->layer->srid) {
            for (l_srid = ln->layer->srid->first; l_srid; l_srid = l_srid->next) {
              if (!buffer_cmp(srid, l_srid->value->buf)) {
                fprintf(o->output, " <OtherSRS>urn:ogc:def:crs:EPSG::%s</OtherSRS>\n", l_srid->value->buf);
              }
            }
          }
        }
      } else {
        if (ows_version_get(o->request->version) == 100)
          fprintf(o->output, " <SRS></SRS>\n");
        else if (ows_version_get(o->request->version) == 110)
          fprintf(o->output, " <NoSRS/>");
      }
      /* Operations */
      if (retrievable != ln->layer->retrievable || writable != ln->layer->writable) {
        fprintf(o->output, "  <Operations>\n");

        if (retrievable == false && ln->layer->retrievable == true) {
          if (ows_version_get(o->request->version) == 100)
            fprintf(o->output, "   <Query/>\n");
          else if (ows_version_get(o->request->version) == 110)
            fprintf(o->output, "   <Operation>Query</Operation>\n");
        }

        if (writable == false && ln->layer->writable == true) {
          if (ows_version_get(o->request->version) == 100) {
            fprintf(o->output, "   <Insert/>\n");
            fprintf(o->output, "   <Update/>\n");
            fprintf(o->output, "   <Delete/>\n");
          } else if (ows_version_get(o->request->version) == 110) {
            fprintf(o->output, "   <Operation>Insert</Operation>\n");
            fprintf(o->output, "   <Operation>Update</Operation>\n");
            fprintf(o->output, "   <Operation>Delete</Operation>\n");
          }
        }

        fprintf(o->output, "  </Operations>\n");
      }

      /* Boundaries */
      if (!ln->layer->geobbox) {
        gb = ows_geobbox_compute(o, ln->layer->name);
      } else {
        gb = ows_geobbox_init();
        gb->west = ln->layer->geobbox->west;
        gb->east = ln->layer->geobbox->east;
        gb->south = ln->layer->geobbox->south;
        gb->north = ln->layer->geobbox->north;
      }
      assert(gb);

      for (s = 0; s < ln->layer->depth; s++) fprintf(o->output, " ");

      if (ows_version_get(o->request->version) == 100)
        fprintf(o->output, " <LatLongBoundingBox");
      else if (ows_version_get(o->request->version) == 110)
        fprintf(o->output, " <ows:WGS84BoundingBox>");

      if (gb->east != DBL_MIN) {
        if (ows_version_get(o->request->version) == 100) {
          if (gb->west < gb->east)
            fprintf(o->output, " minx='%.*f'", o->degree_precision, gb->west);
          else
            fprintf(o->output, " minx='%.*f'", o->degree_precision, gb->east);

          if (gb->north < gb->south)
            fprintf(o->output, " miny='%.*f'", o->degree_precision, gb->north);
          else
            fprintf(o->output, " miny='%.*f'", o->degree_precision, gb->south);

          if (gb->west < gb->east)
            fprintf(o->output, " maxx='%.*f'", o->degree_precision, gb->east);
          else
            fprintf(o->output, " maxx='%.*f'", o->degree_precision, gb->west);

          if (gb->north < gb->south)
            fprintf(o->output, " maxy='%.*f'", o->degree_precision, gb->south);
          else
            fprintf(o->output, " maxy='%.*f'", o->degree_precision, gb->north);

          fprintf(o->output, " />\n");
        } else if (ows_version_get(o->request->version) == 110) {
          fprintf(o->output, " <ows:LowerCorner>%.*f %.*f</ows:LowerCorner>",
                  o->degree_precision, gb->west, o->degree_precision, gb->south);
          fprintf(o->output, " <ows:UpperCorner>%.*f %.*f</ows:UpperCorner>",
                  o->degree_precision, gb->east, o->degree_precision, gb->north);
        }
      } else {
        if (ows_version_get(o->request->version) == 100) {
          fprintf(o->output, " minx='0' miny='0' maxx='0' maxy='0'/>\n");
        } else if (ows_version_get(o->request->version) == 110) {
          fprintf(o->output, " <ows:LowerCorner>0 0</ows:LowerCorner>");
          fprintf(o->output, " <ows:UpperCorner>0 0</ows:UpperCorner>");
        }
      }

      if (ows_version_get(o->request->version) == 110)
        fprintf(o->output, " </ows:WGS84BoundingBox>\n");

      buffer_free(srid);
      buffer_free(srs);
      ows_geobbox_free(gb);

      fprintf(o->output, "</FeatureType>\n");
    }
  }

  fprintf(o->output, " </FeatureTypeList>\n");
}
Beispiel #11
0
static
void test_cipher_test_vectors(void)
{
	static struct {
		const char *key;
		const char *iv;
		const char *pt;
		const char *ct;
	} vectors[] =
	{
		{ "2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090a0b0c0d0e0f", "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d" },
		{ "2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b2" }
	};


	test_begin("test_cipher_test_vectors");

	buffer_t *key,*iv,*pt,*ct,*res_enc,*res_dec;

	key = buffer_create_dynamic(pool_datastack_create(), 16);
	iv = buffer_create_dynamic(pool_datastack_create(), 16);
	pt = buffer_create_dynamic(pool_datastack_create(), 16);
	ct = buffer_create_dynamic(pool_datastack_create(), 16);

	res_enc = buffer_create_dynamic(pool_datastack_create(), 32);
	res_dec = buffer_create_dynamic(pool_datastack_create(), 32);

	for(size_t i = 0; i < N_ELEMENTS(vectors); i++) {
		struct dcrypt_context_symmetric *ctx;

		buffer_set_used_size(key, 0);
		buffer_set_used_size(iv, 0);
		buffer_set_used_size(pt, 0);
		buffer_set_used_size(ct, 0);
		buffer_set_used_size(res_enc, 0);
		buffer_set_used_size(res_dec, 0);

		hex_to_binary(vectors[i].key, key);
		hex_to_binary(vectors[i].iv, iv);
		hex_to_binary(vectors[i].pt, pt);
		hex_to_binary(vectors[i].ct, ct);

		if (!dcrypt_ctx_sym_create("AES-128-CBC", DCRYPT_MODE_ENCRYPT, &ctx, NULL)) {
			test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
			continue;
		}

		dcrypt_ctx_sym_set_padding(ctx, FALSE);

		dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
		dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);

		test_assert_idx(dcrypt_ctx_sym_init(ctx, NULL), i);

		test_assert_idx(dcrypt_ctx_sym_update(ctx, pt->data, pt->used, res_enc, NULL), i);
		test_assert_idx(dcrypt_ctx_sym_final(ctx, res_enc, NULL), i);

		test_assert_idx(buffer_cmp(ct, res_enc), i);

		dcrypt_ctx_sym_destroy(&ctx);

		if (!dcrypt_ctx_sym_create("AES-128-CBC", DCRYPT_MODE_DECRYPT, &ctx, NULL)) {
			test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
			continue;
		}

		dcrypt_ctx_sym_set_padding(ctx, FALSE);

		dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
		dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);

		test_assert_idx(dcrypt_ctx_sym_init(ctx, NULL), i);
		test_assert_idx(dcrypt_ctx_sym_update(ctx, res_enc->data, res_enc->used, res_dec, NULL), i);
		test_assert_idx(dcrypt_ctx_sym_final(ctx, res_dec, NULL), i);

		test_assert_idx(buffer_cmp(pt, res_dec), i);

		dcrypt_ctx_sym_destroy(&ctx);
	}

	test_end();
}
Beispiel #12
0
static
void test_cipher_aead_test_vectors(void)
{
	struct dcrypt_context_symmetric *ctx;
	const char *error = NULL;

	test_begin("test_cipher_aead_test_vectors");

	if (!dcrypt_ctx_sym_create("aes-128-gcm", DCRYPT_MODE_ENCRYPT, &ctx, &error)) {
		test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
		return;
	}

	buffer_t *key, *iv, *aad, *pt, *ct, *tag, *tag_res, *res;

	key = buffer_create_dynamic(pool_datastack_create(), 16);
	iv = buffer_create_dynamic(pool_datastack_create(), 16);
	aad = buffer_create_dynamic(pool_datastack_create(), 16);
	pt = buffer_create_dynamic(pool_datastack_create(), 16);
	ct = buffer_create_dynamic(pool_datastack_create(), 16);
	tag = buffer_create_dynamic(pool_datastack_create(), 16);
	res = buffer_create_dynamic(pool_datastack_create(), 16);
	tag_res = buffer_create_dynamic(pool_datastack_create(), 16);

	hex_to_binary("feffe9928665731c6d6a8f9467308308", key);
	hex_to_binary("cafebabefacedbaddecaf888", iv);
	hex_to_binary("d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255", pt);
	hex_to_binary("42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985", ct);
	hex_to_binary("4d5c2af327cd64a62cf35abd2ba6fab4", tag);

	dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
	dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
	dcrypt_ctx_sym_set_aad(ctx, aad->data, aad->used);
	test_assert(dcrypt_ctx_sym_init(ctx, &error));
	test_assert(dcrypt_ctx_sym_update(ctx, pt->data, pt->used, res, &error));
	test_assert(dcrypt_ctx_sym_final(ctx, res, &error));
	test_assert(dcrypt_ctx_sym_get_tag(ctx, tag_res));

	test_assert(buffer_cmp(ct, res) == TRUE);
	test_assert(buffer_cmp(tag, tag_res) == TRUE);

	dcrypt_ctx_sym_destroy(&ctx);

	if (!dcrypt_ctx_sym_create("aes-128-gcm", DCRYPT_MODE_DECRYPT, &ctx, &error)) {
		test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
	} else {

		buffer_set_used_size(res, 0);

		dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
		dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
		dcrypt_ctx_sym_set_aad(ctx, aad->data, aad->used);
		dcrypt_ctx_sym_set_tag(ctx, tag->data, tag->used);
		test_assert(dcrypt_ctx_sym_init(ctx, &error));
		test_assert(dcrypt_ctx_sym_update(ctx, ct->data, ct->used, res, &error));
		test_assert(dcrypt_ctx_sym_final(ctx, res, &error));

		test_assert(buffer_cmp(pt, res) == TRUE);

		dcrypt_ctx_sym_destroy(&ctx);
	}

	test_end();
}
Beispiel #13
0
/*
 * Retrieve columns name and type of a table related a given layer
 */
static void ows_storage_fill_attributes(ows * o, ows_layer * l)
{
  buffer *sql;
  PGresult *res;
  buffer *b, *t;
  int i, end;
  list_node *ln;

  assert(o);
  assert(l);
  assert(l->storage);

  sql = buffer_init();

  buffer_add_str(sql, "SELECT a.attname AS field, t.typname AS type ");
  buffer_add_str(sql, "FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n WHERE n.nspname = '");
  buffer_copy(sql, l->storage->schema);
  buffer_add_str(sql, "' AND c.relname = '");
  buffer_copy(sql, l->storage->table);
  buffer_add_str(sql, "' AND c.relnamespace = n.oid AND a.attrelid = c.oid AND a.atttypid = t.oid");
  if (l->include_items) {
    buffer_add_str(sql, " AND a.attname IN (");
    for (ln = l->include_items->first ; ln ; ln = ln->next) {
      buffer_add_str(sql, "'");
      buffer_copy(sql, ln->value);
      buffer_add_str(sql, "', ");
    }
    if (l->include_items->first && l->storage->pkey) {
      buffer_add_str(sql, "'");
      buffer_copy(sql, l->storage->pkey );
      buffer_add_str(sql, "',");
    }

    buffer_add_str(sql, " '');");
  } else {
    buffer_add_str(sql, " AND a.attnum > 0;");
  }

  res = ows_psql_exec(o, sql->buf);
  buffer_free(sql);

  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
    PQclear(res);
    ows_error(o, OWS_ERROR_REQUEST_SQL_FAILED, "Unable to access pg_* tables.", "fill_attributes");
    return;
  }

  for (i = 0, end = PQntuples(res); i < end; i++) {
    b = buffer_init();
    t = buffer_init();
    buffer_add_str(b, PQgetvalue(res, i, 0));
    buffer_add_str(t, PQgetvalue(res, i, 1));

    /* If the column is a geometry, get its real geometry type */
    if (buffer_cmp(t, "geometry")) {
      PGresult *geom_res;
      buffer *geom_sql = buffer_init();
      buffer_add_str(geom_sql, "SELECT type from geometry_columns where f_table_schema='");
      buffer_copy(geom_sql, l->storage->schema);
      buffer_add_str(geom_sql,"' and f_table_name='");
      buffer_copy(geom_sql, l->storage->table);
      buffer_add_str(geom_sql,"' and f_geometry_column='");
      buffer_copy(geom_sql, b);
      buffer_add_str(geom_sql,"';");

      geom_res = ows_psql_exec(o, geom_sql->buf);
      buffer_free(geom_sql);

      if (PQresultStatus(geom_res) != PGRES_TUPLES_OK || PQntuples(geom_res) == 0) {
        PQclear(res);
        PQclear(geom_res);
        ows_error(o, OWS_ERROR_REQUEST_SQL_FAILED,
                  "Unable to access geometry_columns table, try Populate_Geometry_Columns()", "fill_attributes");
        return;
      }

      buffer_empty(t);
      buffer_add_str(t, PQgetvalue(geom_res, 0, 0));
      PQclear(geom_res);
    }

    array_add(l->storage->attributes, b, t);
  }
  PQclear(res);

}
Beispiel #14
0
static void test_buffer_truncate_bits(void)
{
	buffer_t *buf;
	test_begin("buffer_test_truncate_bits");

	struct {
		buffer_t input;
		size_t bits;
		buffer_t output;
	} test_cases[] = {
                { { "\xff\xff\xff", 3, {0} },  0, { "",  0, {0} } },
                { { "\xff\xff\xff", 3, {0} },  1, { "\x01", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  2, { "\x03", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  3, { "\x07", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  4, { "\x0f", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  5, { "\x1f", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  6, { "\x3f", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  7, { "\x7f", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  8, { "\xff", 1, {0} } },
                { { "\xff\xff\xff", 3, {0} },  9, { "\x01\xff", 2, {0} } },
                { { "\xff\xff\xff", 3, {0} }, 10, { "\x03\xff", 2, {0} } },
                { { "\xff\xff\xff", 3, {0} }, 11, { "\x07\xff", 2, {0} } },
                { { "\xff\xff\xff", 3, {0} }, 12, { "\x0f\xff", 2, {0} } },
                { { "\xff\xff\xff", 3, {0} }, 13, { "\x1f\xff", 2, {0} } },
                { { "\xff\xff\xff", 3, {0} }, 14, { "\x3f\xff", 2, {0} } },
                { { "\xff\xff\xff", 3, {0} }, 15, { "\x7f\xff", 2, {0} } },
                { { "0123456789", 10, {0} }, 16, { "01",  2, {0} } },
                { { "0123456789", 10, {0} }, 24, { "012",  3, {0} } },
                { { "0123456789", 10, {0} }, 32, { "0123",  4, {0} } },
                { { "0123456789", 10, {0} }, 40, { "01234",  5, {0} } },
                { { "0123456789", 10, {0} }, 48, { "012345",  6, {0} } },
                { { "0123456789", 10, {0} }, 56, { "0123456",  7, {0} } },
                { { "0123456789", 10, {0} }, 64, { "01234567",  8, {0} } },
                { { "0123456789", 10, {0} }, 72, { "012345678",  9, {0} } },
		{ { "0123456789", 10, {0} }, 80, { "0123456789", 10, {0} } },

		{ { "\x58\x11\xed\x02\x4d\x87\x4a\xe2\x5c\xb2\xfa\x69\xf0\xa9\x46\x2e\x04\xca\x5d\x82", 20, {0} },
		  13,
		  { "\x0b\x02", 2, {0} }
		},

		/* special test cases for auth policy */

		{ { "\x34\x40\xc8\xc9\x3a\xb6\xe7\xc4\x3f\xc1\xc3\x4d\xd5\x56\xa3\xea\xfb\x5a\x33\x57\xac\x11\x39\x2c\x71\xcb\xee\xbb\xc8\x66\x2f\x64", 32, {0} },
		  12,
		  { "\x03\x44", 2, {0} }
		},

		{ { "\x49\xe5\x8a\x88\x76\xd3\x25\x68\xc9\x89\x4a\xe0\x64\xe4\x04\xf4\xf9\x13\xec\x88\x97\x47\x30\x7f\x3f\xcd\x8f\x74\x4f\x40\xd1\x25", 32, {0} },
                  12,
                  { "\x04\x9e", 2, {0} }
                },

		{ { "\x08\x3c\xdc\x14\x61\x80\x1c\xe8\x43\x81\x98\xfa\xc0\x64\x04\x7a\xa2\x73\x25\x6e\xe6\x4b\x85\x42\xd0\xe2\x78\xd7\x91\xb4\x89\x3f", 32, {0} },
                  12,
                  { "\x00\x83", 2, {0} }
                },

	};

	buf = t_buffer_create(10);

	for(size_t i = 0; i < N_ELEMENTS(test_cases); i++) {
		buffer_set_used_size(buf, 0);
		buffer_copy(buf, 0, &test_cases[i].input, 0, (size_t)-1);
		buffer_truncate_rshift_bits(buf, test_cases[i].bits);
		test_assert_idx(buffer_cmp(buf, &test_cases[i].output) == TRUE, i);
	}

	test_end();
}