summaryrefslogtreecommitdiff
path: root/egg
diff options
context:
space:
mode:
authorNiels De Graef <nielsdegraef@gmail.com>2022-07-06 09:57:11 +0200
committerNiels De Graef <nielsdegraef@gmail.com>2022-07-07 13:40:25 +0200
commit8927be67708f5b8fcb66e3d65b2de4b8fe77b236 (patch)
tree00a2a9ef54cad54d797402f92b923ce8c7fd2ecb /egg
parent8f8926a0fa911ba9b4a299966f1c097b8954ca2b (diff)
downloadgcr-8927be67708f5b8fcb66e3d65b2de4b8fe77b236.tar.gz
Use GDateTime instead of GDate
... and remove some functions that still use `time_t`
Diffstat (limited to 'egg')
-rw-r--r--egg/egg-asn1x.c180
-rw-r--r--egg/egg-asn1x.h17
-rw-r--r--egg/test-asn1.c128
3 files changed, 66 insertions, 259 deletions
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index b7d9d11..b6522c9 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -1968,7 +1968,7 @@ two_to_four_digit_year (int year)
static gboolean
parse_utc_time (const gchar *time, gsize n_time,
- struct tm* when, gint *offset)
+ struct tm* when, gint32 *offset)
{
const char *p, *e;
int year;
@@ -2077,7 +2077,7 @@ parse_utc_time (const gchar *time, gsize n_time,
static gboolean
parse_general_time (const gchar *time, gsize n_time,
- struct tm* when, gint *offset)
+ struct tm* when, gint32 *offset)
{
const char *p, *e;
@@ -2181,48 +2181,36 @@ static gboolean
anode_read_time (GNode *node,
GBytes *data,
struct tm *when,
- glong *value)
+ gint32 *offset)
{
- const gchar *buf;
+ const char *buf;
gboolean ret;
- gint offset = 0;
- gint flags;
- gint type;
+ int flags;
+ int type;
gsize len;
g_assert (data != NULL);
g_assert (when != NULL);
- g_assert (value != NULL);
+ g_assert (offset != NULL);
flags = anode_def_flags (node);
type = anode_def_type (node);
buf = g_bytes_get_data (data, &len);
if (type == EGG_ASN1X_GENERALIZED_TIME)
- ret = parse_general_time (buf, len, when, &offset);
+ ret = parse_general_time (buf, len, when, offset);
else if (type == EGG_ASN1X_UTC_TIME)
- ret = parse_utc_time (buf, len, when, &offset);
+ ret = parse_utc_time (buf, len, when, offset);
else if (flags & FLAG_GENERALIZED)
- ret = parse_general_time (buf, len, when, &offset);
+ ret = parse_general_time (buf, len, when, offset);
else if (flags & FLAG_UTC)
- ret = parse_utc_time (buf, len, when, &offset);
+ ret = parse_utc_time (buf, len, when, offset);
else
g_return_val_if_reached (FALSE);
if (!ret)
return anode_failure (node, "invalid time content");
- /* In order to work with 32 bit time_t. */
- if (sizeof (time_t) <= 4 && when->tm_year >= 138) {
- *value = (time_t)2145914603; /* 2037-12-31 23:23:23 */
-
- /* Convert to seconds since epoch */
- } else {
- *value = timegm (when);
- g_return_val_if_fail (*value >= 0, FALSE);
- *value += offset;
- }
-
return TRUE;
}
@@ -3619,77 +3607,51 @@ egg_asn1x_set_bits_as_ulong (GNode *node,
anode_take_value (node, g_bytes_new_take (data, len));
}
-glong
-egg_asn1x_get_time_as_long (GNode *node)
+GDateTime *
+egg_asn1x_get_time_as_date_time (GNode *node)
{
struct tm when;
GBytes *data;
- glong time;
- gint type;
+ int type;
+ gint32 offset;
+ GTimeZone *timezone;
+ GDateTime *ret;
- g_return_val_if_fail (node, -1);
- type = anode_def_type (node);
-
- /* Time is often represented as a choice, so work than in here */
- if (type == EGG_ASN1X_CHOICE) {
- node = egg_asn1x_get_choice (node);
- if (node == NULL)
- return -1;
- g_return_val_if_fail (anode_def_type (node) == EGG_ASN1X_TIME ||
- anode_def_type (node) == EGG_ASN1X_UTC_TIME ||
- anode_def_type (node) == EGG_ASN1X_GENERALIZED_TIME, -1);
- return egg_asn1x_get_time_as_long (node);
- }
-
- g_return_val_if_fail (type == EGG_ASN1X_TIME ||
- type == EGG_ASN1X_UTC_TIME ||
- type == EGG_ASN1X_GENERALIZED_TIME, -1);
-
- data = anode_get_value (node);
- if (data == NULL)
- return -1;
-
- if (!anode_read_time (node, data, &when, &time))
- g_return_val_if_reached (-1); /* already validated */
- return time;
-}
-
-gboolean
-egg_asn1x_get_time_as_date (GNode *node,
- GDate *date)
-{
- struct tm when;
- GBytes *data;
- glong time;
- gint type;
-
- g_return_val_if_fail (node, FALSE);
+ g_return_val_if_fail (node, NULL);
type = anode_def_type (node);
/* Time is often represented as a choice, so work than in here */
if (type == EGG_ASN1X_CHOICE) {
node = egg_asn1x_get_choice (node);
if (node == NULL)
- return FALSE;
+ return NULL;
g_return_val_if_fail (anode_def_type (node) == EGG_ASN1X_TIME ||
anode_def_type (node) == EGG_ASN1X_UTC_TIME ||
- anode_def_type (node) == EGG_ASN1X_GENERALIZED_TIME, FALSE);
- return egg_asn1x_get_time_as_date (node, date);
+ anode_def_type (node) == EGG_ASN1X_GENERALIZED_TIME, NULL);
+ return egg_asn1x_get_time_as_date_time (node);
}
g_return_val_if_fail (type == EGG_ASN1X_TIME ||
type == EGG_ASN1X_UTC_TIME ||
- type == EGG_ASN1X_GENERALIZED_TIME, FALSE);
+ type == EGG_ASN1X_GENERALIZED_TIME, NULL);
data = anode_get_value (node);
if (data == NULL)
- return FALSE;
-
- if (!anode_read_time (node, data, &when, &time))
- g_return_val_if_reached (FALSE); /* already validated */
+ return NULL;
- g_date_set_dmy (date, when.tm_mday, when.tm_mon + 1, when.tm_year + 1900);
- return TRUE;
+ if (!anode_read_time (node, data, &when, &offset))
+ g_return_val_if_reached (NULL); /* already validated */
+
+ timezone = g_time_zone_new_offset (offset);
+ ret = g_date_time_new (timezone,
+ when.tm_year + 1900,
+ when.tm_mon + 1,
+ when.tm_mday,
+ when.tm_hour,
+ when.tm_min,
+ when.tm_sec);
+ g_time_zone_unref (timezone);
+ return ret;
}
gchar*
@@ -3985,9 +3947,9 @@ static gboolean
anode_validate_time (GNode *node,
GBytes *value)
{
- glong time;
struct tm when;
- return anode_read_time (node, value, &when, &time);
+ gint32 offset;
+ return anode_read_time (node, value, &when, &offset);
}
static gboolean
@@ -4719,72 +4681,6 @@ egg_asn1x_destroy (gpointer data)
}
/* --------------------------------------------------------------------------------
- * TIME PARSING
- */
-
-glong
-egg_asn1x_parse_time_general (const gchar *time, gssize n_time)
-{
- gboolean ret;
- glong value;
- struct tm when;
- gint offset = 0;
-
- g_return_val_if_fail (time, -1);
-
- if (n_time < 0)
- n_time = strlen (time);
-
- ret = parse_general_time (time, n_time, &when, &offset);
- if (!ret)
- return -1;
-
- /* In order to work with 32 bit time_t. */
- if (sizeof (time_t) <= 4 && when.tm_year >= 138) {
- value = (time_t)2145914603; /* 2037-12-31 23:23:23 */
-
- /* Convert to seconds since epoch */
- } else {
- value = timegm (&when);
- g_return_val_if_fail (*time >= 0, FALSE);
- value += offset;
- }
-
- return value;
-}
-
-glong
-egg_asn1x_parse_time_utc (const gchar *time, gssize n_time)
-{
- gboolean ret;
- glong value;
- struct tm when;
- gint offset = 0;
-
- g_return_val_if_fail (time, -1);
-
- if (n_time < 0)
- n_time = strlen (time);
-
- ret = parse_utc_time (time, n_time, &when, &offset);
- if (!ret)
- return -1;
-
- /* In order to work with 32 bit time_t. */
- if (sizeof (time_t) <= 4 && when.tm_year >= 138) {
- value = (time_t)2145914603; /* 2037-12-31 23:23:23 */
-
- /* Convert to seconds since epoch */
- } else {
- value = timegm (&when);
- g_return_val_if_fail (*time >= 0, FALSE);
- value += offset;
- }
-
- return value;
-}
-
-/* --------------------------------------------------------------------------------
* BASIC RAW ELEMENT INFO
*/
diff --git a/egg/egg-asn1x.h b/egg/egg-asn1x.h
index aab3f4e..f0d6622 100644
--- a/egg/egg-asn1x.h
+++ b/egg/egg-asn1x.h
@@ -239,16 +239,7 @@ gboolean egg_asn1x_set_string_as_utf8 (GNode *node,
gchar * egg_asn1x_get_bmpstring_as_utf8 (GNode *node);
-glong egg_asn1x_get_time_as_long (GNode *node);
-
-gboolean egg_asn1x_set_time_as_long (GNode *node,
- glong time);
-
-gboolean egg_asn1x_get_time_as_date (GNode *node,
- GDate *date);
-
-gboolean egg_asn1x_set_time_as_date (GNode *node,
- GDate *date);
+GDateTime * egg_asn1x_get_time_as_date_time (GNode *node);
GQuark egg_asn1x_get_oid_as_quark (GNode *node);
@@ -262,12 +253,6 @@ gboolean egg_asn1x_set_oid_as_string (GNode *node,
void egg_asn1x_destroy (gpointer asn);
-glong egg_asn1x_parse_time_general (const gchar *time,
- gssize n_time);
-
-glong egg_asn1x_parse_time_utc (const gchar *time,
- gssize n_time);
-
gssize egg_asn1x_element_length (const guchar *data,
gsize n_data);
diff --git a/egg/test-asn1.c b/egg/test-asn1.c
index c4750b1..9f64a3d 100644
--- a/egg/test-asn1.c
+++ b/egg/test-asn1.c
@@ -569,7 +569,7 @@ test_generalized_time (void)
{
GBytes *bytes;
GNode *asn;
- glong value;
+ GDateTime *value;
asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
g_assert (asn);
@@ -578,22 +578,26 @@ test_generalized_time (void)
g_assert_cmpint (EGG_ASN1X_GENERALIZED_TIME, ==, egg_asn1x_type (asn));
/* Shouldn't succeed */
- value = egg_asn1x_get_time_as_long (asn);
- g_assert (value == -1);
+ value = egg_asn1x_get_time_as_date_time (asn);
+ g_assert_null (value);
/* Should work */
bytes = g_bytes_new_static (TGENERALIZED, XL (TGENERALIZED));
if (!egg_asn1x_decode (asn, bytes))
g_assert_not_reached ();
g_bytes_unref (bytes);
- value = egg_asn1x_get_time_as_long (asn);
- g_assert (value == 1185368728);
+ value = egg_asn1x_get_time_as_date_time (asn);
+ g_assert_nonnull (value);
+ g_assert_cmpint (g_date_time_get_day_of_month (value), ==, 25);
+ g_assert_cmpint (g_date_time_get_month (value), ==, 7);
+ g_assert_cmpint (g_date_time_get_year (value), ==, 2007);
+ g_clear_pointer (&value, g_date_time_unref);
egg_asn1x_clear (asn);
/* Shouldn't succeed */
- value = egg_asn1x_get_time_as_long (asn);
- g_assert (value == -1);
+ value = egg_asn1x_get_time_as_date_time (asn);
+ g_assert_null (value);
egg_asn1x_destroy (asn);
}
@@ -601,13 +605,10 @@ test_generalized_time (void)
static void
test_time_get_missing (void)
{
- GDate date;
GNode *asn;
asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
- if (egg_asn1x_get_time_as_date (asn, &date))
- g_assert_not_reached ();
- g_assert (egg_asn1x_get_time_as_long (asn) == -1);
+ g_assert_null (egg_asn1x_get_time_as_date_time (asn));
egg_asn1x_destroy (asn);
}
@@ -2058,103 +2059,30 @@ test_oid_get_no_value (void)
egg_asn1x_destroy (asn);
}
-typedef struct _TimeTestData {
- gchar *value;
- time_t ref;
-} TimeTestData;
-
-static const TimeTestData generalized_time_test_data[] = {
- { "20070725130528Z", 1185368728 },
- { "20070725130528.2134Z", 1185368728 },
- { "20070725140528-0100", 1185368728 },
- { "20070725040528+0900", 1185368728 },
- { "20070725013528+1130", 1185368728 },
- { "20070725Z", 1185321600 },
- { "20070725+0000", 1185321600 },
-
- /* Bad ones */
- { "200707", -1 },
-
- { NULL, 0 }
-};
-
-static const TimeTestData utc_time_test_data[] = {
- /* Test the Y2K style wrap arounds */
- { "070725130528Z", 1185368728 }, /* The year 2007 */
- { "020725130528Z", 1027602328 }, /* The year 2002 */
- { "970725130528Z", 869835928 }, /* The year 1997 */
- { "370725130528Z", 2132139928 }, /* The year 2037 */
-
- /* Test the time zones and other formats */
- { "070725130528.2134Z", 1185368728 },
- { "070725140528-0100", 1185368728 },
- { "070725040528+0900", 1185368728 },
- { "070725013528+1130", 1185368728 },
- { "070725Z", 1185321600 },
- { "070725+0000", 1185321600 },
-
- /* Bad ones */
- { "0707", -1 },
-
- { NULL, 0 }
-};
-
-static void
-test_general_time (Test* test, gconstpointer unused)
-{
- time_t when;
- const TimeTestData *data;
-
- for (data = generalized_time_test_data; data->value; ++data) {
- when = egg_asn1x_parse_time_general (data->value, -1);
- if (data->ref != when) {
- printf ("%s", data->value);
- printf ("%s != ", ctime (&when));
- printf ("%s\n", ctime (&data->ref));
- fflush (stdout);
- }
-
- g_assert ("decoded time doesn't match reference" && data->ref == when);
- }
-}
-
-static void
-test_utc_time (Test* test, gconstpointer unused)
-{
- time_t when;
- const TimeTestData *data;
-
- for (data = utc_time_test_data; data->value; ++data) {
- when = egg_asn1x_parse_time_utc (data->value, -1);
- if (data->ref != when) {
- printf ("%s", data->value);
- printf ("%s != ", ctime (&when));
- printf ("%s\n", ctime (&data->ref));
- fflush (stdout);
- }
-
- g_assert ("decoded time doesn't match reference" && data->ref == when);
- }
-}
-
static void
test_read_time (Test* test, gconstpointer unused)
{
- glong time;
+ GDateTime *val;
- time = egg_asn1x_get_time_as_long (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notBefore", NULL));
- g_assert_cmpint (time, ==, 820454400);
+ val = egg_asn1x_get_time_as_date_time (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notBefore", NULL));
+ g_assert_nonnull (val);
+ g_assert_cmpint (g_date_time_to_unix (val), ==, 820454400);
+ g_clear_pointer (&val, g_date_time_unref);
}
static void
test_read_date (Test* test, gconstpointer unused)
{
- GDate date;
- if (!egg_asn1x_get_time_as_date (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notAfter", NULL), &date))
- g_assert_not_reached ();
- g_assert_cmpint (date.day, ==, 31);
- g_assert_cmpint (date.month, ==, 12);
- g_assert_cmpint (date.year, ==, 2020);
+ GDateTime *datetime;
+
+ datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notAfter", NULL));
+ g_assert_nonnull (datetime);
+ g_assert_cmpint (g_date_time_get_day_of_month (datetime), ==, 31);
+ g_assert_cmpint (g_date_time_get_month (datetime), ==, 12);
+ g_assert_cmpint (g_date_time_get_year (datetime), ==, 2020);
+ g_assert_cmpint (g_date_time_get_utc_offset (datetime), ==, 0);
+
+ g_clear_pointer (&datetime, g_date_time_unref);
}
static void
@@ -2545,8 +2473,6 @@ main (int argc, char **argv)
g_test_add ("/asn1/write_value", Test, NULL, setup, test_write_value, teardown);
g_test_add ("/asn1/element_length_content", Test, NULL, setup, test_element_length_content, teardown);
g_test_add ("/asn1/read_element", Test, NULL, setup, test_read_element, teardown);
- g_test_add ("/asn1/general_time", Test, NULL, setup, test_general_time, teardown);
- g_test_add ("/asn1/utc_time", Test, NULL, setup, test_utc_time, teardown);
g_test_add ("/asn1/read_time", Test, NULL, setup, test_read_time, teardown);
g_test_add ("/asn1/read_date", Test, NULL, setup, test_read_date, teardown);
g_test_add ("/asn1/create_by_oid", Test, NULL, setup, test_create_by_oid, teardown);