From 13f3cd21cbc78aaa4b2f70d4614937db7a86a145 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 17 Apr 2014 07:14:00 +0200 Subject: gcr: Move key size code to gcr-subject-public-key.c Refactor things to key size calculation code is in one place --- gcr/gcr-subject-public-key.c | 55 ++++++++++++++++++++++++++++++++++++++++ gcr/gcr-subject-public-key.h | 2 ++ ui/gcr-key-renderer.c | 60 +++++--------------------------------------- 3 files changed, 63 insertions(+), 54 deletions(-) diff --git a/gcr/gcr-subject-public-key.c b/gcr/gcr-subject-public-key.c index 5b7dc28..359307c 100644 --- a/gcr/gcr-subject-public-key.c +++ b/gcr/gcr-subject-public-key.c @@ -785,6 +785,24 @@ calculate_rsa_key_size (GBytes *data) return key_size; } +static guint +attributes_rsa_key_size (GckAttributes *attrs) +{ + const GckAttribute *attr; + gulong bits; + + attr = gck_attributes_find (attrs, CKA_MODULUS); + + /* Calculate the bit length, and remove the complement */ + if (attr != NULL) + return (attr->length / 2) * 2 * 8; + + if (gck_attributes_find_ulong (attrs, CKA_MODULUS_BITS, &bits)) + return (gint)bits; + + return 0; +} + static guint calculate_dsa_params_size (GNode *params) { @@ -808,6 +826,24 @@ calculate_dsa_params_size (GNode *params) return key_size; } +static guint +attributes_dsa_key_size (GckAttributes *attrs) +{ + const GckAttribute *attr; + gulong bits; + + attr = gck_attributes_find (attrs, CKA_PRIME); + + /* Calculate the bit length, and remove the complement */ + if (attr != NULL) + return (attr->length / 2) * 2 * 8; + + if (gck_attributes_find_ulong (attrs, CKA_PRIME_BITS, &bits)) + return (gint)bits; + + return 0; +} + guint _gcr_subject_public_key_calculate_size (GNode *subject_public_key) { @@ -840,3 +876,22 @@ _gcr_subject_public_key_calculate_size (GNode *subject_public_key) return key_size; } + +guint +_gcr_subject_public_key_attributes_size (GckAttributes *attrs) +{ + gulong key_type; + + if (!gck_attributes_find_ulong (attrs, CKA_KEY_TYPE, &key_type)) + return 0; + + switch (key_type) { + case CKK_RSA: + return attributes_rsa_key_size (attrs); + case CKK_DSA: + return attributes_dsa_key_size (attrs); + default: + g_message ("unsupported key algorithm: %lu", key_type); + return 0; + } +} diff --git a/gcr/gcr-subject-public-key.h b/gcr/gcr-subject-public-key.h index 4a45bd0..9e7c1d9 100644 --- a/gcr/gcr-subject-public-key.h +++ b/gcr/gcr-subject-public-key.h @@ -48,6 +48,8 @@ GNode * _gcr_subject_public_key_load_finish (GAsyncResult *result, guint _gcr_subject_public_key_calculate_size (GNode *subject_public_key); +guint _gcr_subject_public_key_attributes_size (GckAttributes *attributes); + G_END_DECLS #endif /* GCR_CERTIFICATE_H */ diff --git a/ui/gcr-key-renderer.c b/ui/gcr-key-renderer.c index a4dbdb9..1383d31 100644 --- a/ui/gcr-key-renderer.c +++ b/ui/gcr-key-renderer.c @@ -114,53 +114,6 @@ calculate_fingerprint (GcrKeyRenderer *self, return gcr_fingerprint_from_attributes (attrs, algorithm, n_fingerprint); } -static gint -calculate_rsa_key_size (GckAttributes *attrs) -{ - const GckAttribute *attr; - gulong bits; - - attr = gck_attributes_find (attrs, CKA_MODULUS); - - /* Calculate the bit length, and remove the complement */ - if (attr != NULL) - return (attr->length / 2) * 2 * 8; - - if (gck_attributes_find_ulong (attrs, CKA_MODULUS_BITS, &bits)) - return (gint)bits; - - return -1; -} - -static guint -calculate_dsa_key_size (GckAttributes *attrs) -{ - const GckAttribute *attr; - gulong bits; - - attr = gck_attributes_find (attrs, CKA_PRIME); - - /* Calculate the bit length, and remove the complement */ - if (attr != NULL) - return (attr->length / 2) * 2 * 8; - - if (gck_attributes_find_ulong (attrs, CKA_PRIME_BITS, &bits)) - return (gint)bits; - - return -1; -} - -static gint -calculate_key_size (GckAttributes *attrs, gulong key_type) -{ - if (key_type == CKK_RSA) - return calculate_rsa_key_size (attrs); - else if (key_type == CKK_DSA) - return calculate_dsa_key_size (attrs); - else - return -1; -} - static void on_subject_public_key (GObject *source, GAsyncResult *result, @@ -362,7 +315,7 @@ gcr_key_renderer_real_render (GcrRenderer *renderer, GcrViewer *viewer) gchar *display; gulong klass; gulong key_type; - gint size; + guint size; self = GCR_KEY_RENDERER (renderer); @@ -415,9 +368,9 @@ gcr_key_renderer_real_render (GcrRenderer *renderer, GcrViewer *viewer) _gcr_display_view_append_content (view, renderer, text, NULL); - size = calculate_key_size (attrs, key_type); - if (size >= 0) { - display = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%d bit", "%d bits", size), size); + size = _gcr_subject_public_key_attributes_size (attrs); + if (size > 0) { + display = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%u bit", "%u bits", size), size); _gcr_display_view_append_content (view, renderer, _("Strength"), display); g_free (display); } @@ -432,11 +385,10 @@ gcr_key_renderer_real_render (GcrRenderer *renderer, GcrViewer *viewer) text = _("Unknown"); _gcr_display_view_append_value (view, renderer, _("Algorithm"), text, FALSE); - size = calculate_key_size (attrs, key_type); - if (size < 0) + if (size == 0) display = g_strdup (_("Unknown")); else - display = g_strdup_printf ("%d", size); + display = g_strdup_printf ("%u", size); _gcr_display_view_append_value (view, renderer, _("Size"), display, FALSE); g_free (display); -- cgit v1.2.3