[gcr/nielsdg/certificate-gdatetime: 3/6] egg: Drop usage of timegm in favor of GDateTime




commit 194df5463acef43f07ada8bfb05e20aeffb94a42
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Wed Jul 6 21:44:30 2022 +0200

    egg: Drop usage of timegm in favor of GDateTime

 egg/egg-asn1x.c     |  1 -
 egg/egg-timegm.c    | 55 -----------------------------------------------------
 egg/egg-timegm.h    | 30 -----------------------------
 egg/meson.build     |  1 -
 gck/gck-password.c  |  2 --
 gck/gck-slot.c      | 55 +++++++++++++++++++++++++++--------------------------
 gck/gck.h           |  2 +-
 gck/meson.build     |  1 -
 gck/test-gck-slot.c |  3 ++-
 gcr/gcr-record.c    |  2 --
 10 files changed, 31 insertions(+), 121 deletions(-)
---
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index b6522c9c..47f2392c 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -48,7 +48,6 @@
 
 #include "egg-asn1x.h"
 #include "egg-asn1-defs.h"
-#include "egg-timegm.h"
 
 #include <stdlib.h>
 #include <string.h>
diff --git a/egg/meson.build b/egg/meson.build
index 6888a5cd..b577fd34 100644
--- a/egg/meson.build
+++ b/egg/meson.build
@@ -16,7 +16,6 @@ libegg_sources = [
   'egg-secure-memory.c',
   'egg-symkey.c',
   'egg-testing.c',
-  'egg-timegm.c',
 ]
 
 libegg_deps = [
diff --git a/gck/gck-password.c b/gck/gck-password.c
index 3c102e09..4564fa53 100644
--- a/gck/gck-password.c
+++ b/gck/gck-password.c
@@ -25,8 +25,6 @@
 #include "gck.h"
 #include "gck-private.h"
 
-#include "egg/egg-timegm.h"
-
 #include <string.h>
 
 /**
diff --git a/gck/gck-slot.c b/gck/gck-slot.c
index 7c795cbc..a06f7808 100644
--- a/gck/gck-slot.c
+++ b/gck/gck-slot.c
@@ -25,8 +25,6 @@
 #include "gck.h"
 #include "gck-private.h"
 
-#include "egg/egg-timegm.h"
-
 #include <string.h>
 #include <time.h>
 
@@ -233,12 +231,12 @@ gck_slot_info_free (GckSlotInfo *slot_info)
  * @hardware_version_minor: The minor version of the hardware.
  * @firmware_version_major: The major version of the firmware.
  * @firmware_version_minor: The minor version of the firmware.
- * @utc_time: If the token has a hardware clock, this is set to the number of seconds since the epoch.
+ * @utc_time: If the token has a hardware clock, this is the UTC #GDateTime
  *
- * Represents information about a PKCS11 token.
+ * Represents information about a PKCS#11 token.
  *
- * This is analogous to a CK_TOKEN_INFO structure, but the
- * strings are far more usable.
+ * This is analogous to a `CK_TOKEN_INFO` structure, but the fields are far
+ * more usable.
  *
  * When you're done with this structure it should be released with
  * gck_token_info_free().
@@ -266,6 +264,8 @@ gck_token_info_copy (GckTokenInfo *token_info)
        token_info->manufacturer_id = g_strdup (token_info->manufacturer_id);
        token_info->model = g_strdup (token_info->model);
        token_info->serial_number = g_strdup (token_info->serial_number);
+       if (token_info->utc_time != NULL)
+               token_info->utc_time = g_date_time_add (token_info->utc_time, 0);
        return token_info;
 }
 
@@ -280,6 +280,7 @@ gck_token_info_free (GckTokenInfo *token_info)
 {
        if (!token_info)
                return;
+       g_clear_pointer (&token_info->utc_time, g_date_time_unref);
        g_free (token_info->label);
        g_free (token_info->manufacturer_id);
        g_free (token_info->model);
@@ -559,10 +560,6 @@ GckTokenInfo*
 _gck_token_info_from_pkcs11 (CK_TOKEN_INFO_PTR info)
 {
        GckTokenInfo *token_info;
-       gchar *string;
-       /* Must be zero-filled, because strptime will leave tm_isdst
-        * unchanged */
-       struct tm tm = { 0 };
 
        token_info = g_new0 (GckTokenInfo, 1);
        token_info->label = gck_string_from_chars (info->label, sizeof (info->label));
@@ -589,14 +586,23 @@ _gck_token_info_from_pkcs11 (CK_TOKEN_INFO_PTR info)
 
        /* Parse the time into seconds since epoch */
        if (info->flags & CKF_CLOCK_ON_TOKEN) {
-               string = g_strndup ((gchar*)info->utcTime, MIN (14, sizeof (info->utcTime)));
-               if (!strptime (string, "%Y%m%d%H%M%S", &tm))
-                       token_info->utc_time = -1;
-               else
-                       token_info->utc_time = timegm (&tm);
+               char *string;
+               GTimeZone *tz;
+
+               /* The original string has format "%Y%m%d%H%M%S", which just
+                * needs a separator still to be iso8601 confirmant */
+               if (sizeof (info->utcTime) != 14)
+                       token_info->utc_time = NULL;
+               string = g_strdup_printf ("%.8s %.6s",
+                                         (char *) info->utcTime,
+                                         ((char *) info->utcTime) + 8);
+               tz = g_time_zone_new_utc ();
+               token_info->utc_time = g_date_time_new_from_iso8601 (string, tz);
+               g_time_zone_unref (tz);
+
                g_free (string);
        } else {
-               token_info->utc_time = -1;
+               token_info->utc_time = NULL;
        }
 
        return token_info;
@@ -605,11 +611,6 @@ _gck_token_info_from_pkcs11 (CK_TOKEN_INFO_PTR info)
 void
 _gck_token_info_to_pkcs11 (GckTokenInfo *token_info, CK_TOKEN_INFO_PTR info)
 {
-       gchar buffer[64];
-       struct tm tm;
-       time_t tim;
-       gsize len;
-
        if (!gck_string_to_chars (info->label,
                                  sizeof (info->label),
                                  token_info->label))
@@ -645,12 +646,13 @@ _gck_token_info_to_pkcs11 (GckTokenInfo *token_info, CK_TOKEN_INFO_PTR info)
 
        /* Parse the time into seconds since epoch */
        if (token_info->flags & CKF_CLOCK_ON_TOKEN) {
-               tim = token_info->utc_time;
-               if (!gmtime_r (&tim, &tm))
-                       g_return_if_reached ();
-               len = strftime (buffer, sizeof (buffer), "%Y%m%d%H%M%S", &tm);
-               g_return_if_fail (len == sizeof (info->utcTime));
+               char *buffer;
+
+               buffer = g_date_time_format (token_info->utc_time,
+                                            "%Y%m%d%H%M%S");
+               g_return_if_fail (strlen (buffer) == sizeof (info->utcTime));
                memcpy (info->utcTime, buffer, sizeof (info->utcTime));
+               g_free (buffer);
        } else {
                memset (info->utcTime, 0, sizeof (info->utcTime));
        }
@@ -769,7 +771,6 @@ gck_slot_get_mechanism_info (GckSlot *self, gulong mech_type)
        GckMechanismInfo *mechinfo;
        GckModule *module = NULL;
        CK_MECHANISM_INFO info;
-       struct tm;
        CK_RV rv;
 
        g_return_val_if_fail (GCK_IS_SLOT (self), NULL);
diff --git a/gck/gck.h b/gck/gck.h
index 3b65ed2e..7f51346e 100644
--- a/gck/gck.h
+++ b/gck/gck.h
@@ -604,7 +604,7 @@ struct _GckTokenInfo {
        guint8 hardware_version_minor;
        guint8 firmware_version_major;
        guint8 firmware_version_minor;
-       gint64 utc_time;
+       GDateTime *utc_time;
 };
 
 #define             GCK_TYPE_TOKEN_INFO                     (gck_token_info_get_type ())
diff --git a/gck/meson.build b/gck/meson.build
index aa6e9417..8a0e87b9 100644
--- a/gck/meson.build
+++ b/gck/meson.build
@@ -76,7 +76,6 @@ gck_cflags = [
   '-DP11_KIT_API_SUBJECT_TO_CHANGE',
   '-DPKCS11_REGISTRY_DIR="@0@"'.format(get_option('prefix') / get_option('libdir') / 'pkcs11'),
   '-D_XOPEN_SOURCE',   # Needed for strptime()
-  '-D_DEFAULT_SOURCE', # Needed for timegm
 ]
 
 gck_symbolmap = meson.current_source_dir() / 'libgck.map'
diff --git a/gck/test-gck-slot.c b/gck/test-gck-slot.c
index 6d09c427..d03f3191 100644
--- a/gck/test-gck-slot.c
+++ b/gck/test-gck-slot.c
@@ -112,7 +112,8 @@ test_slot_info (Test *test, gconstpointer unused)
                        g_assert_cmpint (175, ==, token->hardware_version_minor);
                        g_assert_cmpint (85, ==, token->firmware_version_major);
                        g_assert_cmpint (185, ==, token->firmware_version_minor);
-                       g_assert_cmpint (927623999, ==, token->utc_time);
+                       g_assert_nonnull (token->utc_time);
+                       g_assert_cmpint (927623999, ==, g_date_time_to_unix (token->utc_time));
 
                        gck_token_info_free (token);
                }
diff --git a/gcr/gcr-record.c b/gcr/gcr-record.c
index d5cad5ab..414396ef 100644
--- a/gcr/gcr-record.c
+++ b/gcr/gcr-record.c
@@ -23,8 +23,6 @@
 
 #include "gcr-record.h"
 
-#include "egg/egg-timegm.h"
-
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]