pidgin: a2067547: Add a function for converting a PurpleCe...

qulogic at pidgin.im qulogic at pidgin.im
Mon Feb 27 18:12:14 EST 2012


----------------------------------------------------------------------
Revision: a20675477f20a92c7670991cd6273519c8f700e6
Parent:   7e4ca181c2e2d12fcfa9c5a3d5a0318a36dc5144
Author:   qulogic at pidgin.im
Date:     02/25/12 22:01:41
Branch:   im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/a20675477f20a92c7670991cd6273519c8f700e6

Changelog: 

Add a function for converting a PurpleCertificate to a string
suitable for display to the user.

This is basically just a copy of purple_certificate_display_x509
right now, but it will eventually lead somewhere...

Changes against parent 7e4ca181c2e2d12fcfa9c5a3d5a0318a36dc5144

  patched  ChangeLog.API
  patched  libpurple/certificate.c
  patched  libpurple/certificate.h
  patched  libpurple/plugins/ssl/ssl-gnutls.c
  patched  libpurple/plugins/ssl/ssl-nss.c

-------------- next part --------------
============================================================
--- libpurple/plugins/ssl/ssl-gnutls.c	109587e7087179a687b2378e342d299a73a53e68
+++ libpurple/plugins/ssl/ssl-gnutls.c	c8153f2143ed5f5467eec1a8a21d7bac666735b1
@@ -1173,6 +1173,55 @@ x509_get_der_data(PurpleCertificate *crt
 	return data;
 }
 
+static gchar *
+x509_display_string(PurpleCertificate *crt)
+{
+	gchar *sha_asc;
+	GByteArray *sha_bin;
+	gchar *cn;
+	time_t activation, expiration;
+	gchar *activ_str, *expir_str;
+	gchar *text;
+
+	/* Pull out the SHA1 checksum */
+	sha_bin = x509_sha1sum(crt);
+	sha_asc = purple_base16_encode_chunked(sha_bin->data, sha_bin->len);
+
+	/* Get the cert Common Name */
+	/* TODO: Will break on CA certs */
+	cn = x509_common_name(crt);
+
+	/* Get the certificate times */
+	/* TODO: Check the times against localtime */
+	/* TODO: errorcheck? */
+	if (!x509_times(crt, &activation, &expiration)) {
+		purple_debug_error("certificate",
+				   "Failed to get certificate times!\n");
+		activation = expiration = 0;
+	}
+	activ_str = g_strdup(ctime(&activation));
+	expir_str = g_strdup(ctime(&expiration));
+
+	/* Make messages */
+	text = g_strdup_printf(_("Common name: %s\n\n"
+	                         "Fingerprint (SHA1): %s\n\n"
+	                         "Activation date: %s\n"
+	                         "Expiration date: %s\n"),
+	                       cn ? cn : "(null)",
+	                       sha_asc ? sha_asc : "(null)",
+	                       activ_str ? activ_str : "(null)",
+	                       expir_str ? expir_str : "(null)");
+
+	/* Cleanup */
+	g_free(cn);
+	g_free(sha_asc);
+	g_free(activ_str);
+	g_free(expir_str);
+	g_byte_array_free(sha_bin, TRUE);
+
+	return text;
+}
+
 /* X.509 certificate operations provided by this plugin */
 static PurpleCertificateScheme x509_gnutls = {
 	"x509",                          /* Scheme name */
@@ -1190,8 +1239,8 @@ static PurpleCertificateScheme x509_gnut
 	x509_times,                      /* Activation/Expiration time */
 	x509_importcerts_from_file,      /* Multiple certificates import function */
 	x509_get_der_data,               /* Binary DER data */
+	x509_display_string,             /* Display representation */
 
-	NULL,
 	NULL
 
 };
============================================================
--- libpurple/plugins/ssl/ssl-nss.c	e7a5c12e7b2e9b258b2210c47774f507764dc5e7
+++ libpurple/plugins/ssl/ssl-nss.c	dcfbd33bb18f95d2105964fdb55c5b49ffefa1e5
@@ -953,6 +953,55 @@ x509_get_der_data(PurpleCertificate *crt
 	return data;
 }
 
+static gchar *
+x509_display_string(PurpleCertificate *crt)
+{
+	gchar *sha_asc;
+	GByteArray *sha_bin;
+	gchar *cn;
+	time_t activation, expiration;
+	gchar *activ_str, *expir_str;
+	gchar *text;
+
+	/* Pull out the SHA1 checksum */
+	sha_bin = x509_sha1sum(crt);
+	sha_asc = purple_base16_encode_chunked(sha_bin->data, sha_bin->len);
+
+	/* Get the cert Common Name */
+	/* TODO: Will break on CA certs */
+	cn = x509_common_name(crt);
+
+	/* Get the certificate times */
+	/* TODO: Check the times against localtime */
+	/* TODO: errorcheck? */
+	if (!x509_times(crt, &activation, &expiration)) {
+		purple_debug_error("certificate",
+				   "Failed to get certificate times!\n");
+		activation = expiration = 0;
+	}
+	activ_str = g_strdup(ctime(&activation));
+	expir_str = g_strdup(ctime(&expiration));
+
+	/* Make messages */
+	text = g_strdup_printf(_("Common name: %s\n\n"
+	                         "Fingerprint (SHA1): %s\n\n"
+	                         "Activation date: %s\n"
+	                         "Expiration date: %s\n"),
+	                       cn ? cn : "(null)",
+	                       sha_asc ? sha_asc : "(null)",
+	                       activ_str ? activ_str : "(null)",
+	                       expir_str ? expir_str : "(null)");
+
+	/* Cleanup */
+	g_free(cn);
+	g_free(sha_asc);
+	g_free(activ_str);
+	g_free(expir_str);
+	g_byte_array_free(sha_bin, TRUE);
+
+	return text;
+}
+
 static PurpleCertificateScheme x509_nss = {
 	"x509",                          /* Scheme name */
 	N_("X.509 Certificates"),        /* User-visible scheme name */
@@ -969,8 +1018,8 @@ static PurpleCertificateScheme x509_nss 
 	x509_times,                      /* Activation/Expiration time */
 	x509_importcerts_from_file,      /* Multiple certificate import function */
 	x509_get_der_data,               /* Binary DER data */
+	x509_display_string,             /* Display representation */
 
-	NULL,
 	NULL
 };
 
============================================================
--- ChangeLog.API	d352199fd25651ed253c13b210eae739cb92800a
+++ ChangeLog.API	a6c0ef933e0535d55645fad5b4ee96b3899f12d9
@@ -9,6 +9,7 @@ version 3.0.0 (??/??/????):
 		* purple_account_set_ui_data
 		* purple_account_register_completed
 		* purple_certificate_get_der_data
+		* purple_certificate_get_display_string
 		* purple_conv_chat_cb_get_alias
 		* purple_conv_chat_cb_get_flags
 		* purple_conv_chat_cb_is_buddy
============================================================
--- libpurple/certificate.c	084505c8e55c07db86a7d6ac74b929759762434a
+++ libpurple/certificate.c	e0501c91e2fd7e3bae14f5836d7d7bd239615f28
@@ -518,6 +518,24 @@ gchar *
 }
 
 gchar *
+purple_certificate_get_display_string(PurpleCertificate *crt)
+{
+	PurpleCertificateScheme *scheme;
+	gchar *str;
+
+	g_return_val_if_fail(crt, NULL);
+	g_return_val_if_fail(crt->scheme, NULL);
+
+	scheme = crt->scheme;
+
+	g_return_val_if_fail(scheme->get_display_string, NULL);
+
+	str = (scheme->get_display_string)(crt);
+
+	return str;
+}
+
+gchar *
 purple_certificate_pool_mkpath(PurpleCertificatePool *pool, const gchar *id)
 {
 	gchar *path;
@@ -2168,43 +2186,10 @@ purple_certificate_display_x509(PurpleCe
 void
 purple_certificate_display_x509(PurpleCertificate *crt)
 {
-	gchar *sha_asc;
-	GByteArray *sha_bin;
-	gchar *cn;
-	time_t activation, expiration;
-	gchar *activ_str, *expir_str;
 	gchar *secondary;
 
-	/* Pull out the SHA1 checksum */
-	sha_bin = purple_certificate_get_fingerprint_sha1(crt);
-	/* Now decode it for display */
-	sha_asc = purple_base16_encode_chunked(sha_bin->data,
-					       sha_bin->len);
-
-	/* Get the cert Common Name */
-	/* TODO: Will break on CA certs */
-	cn = purple_certificate_get_subject_name(crt);
-
-	/* Get the certificate times */
-	/* TODO: Check the times against localtime */
-	/* TODO: errorcheck? */
-	if (!purple_certificate_get_times(crt, &activation, &expiration)) {
-		purple_debug_error("certificate",
-				   "Failed to get certificate times!\n");
-		activation = expiration = 0;
-	}
-	activ_str = g_strdup(ctime(&activation));
-	expir_str = g_strdup(ctime(&expiration));
-
 	/* Make messages */
-	secondary = g_strdup_printf(_("Common name: %s\n\n"
-								  "Fingerprint (SHA1): %s\n\n"
-								  "Activation date: %s\n"
-								  "Expiration date: %s\n"),
-								cn ? cn : "(null)",
-								sha_asc ? sha_asc : "(null)",
-								activ_str ? activ_str : "(null)",
-								expir_str ? expir_str : "(null)");
+	secondary = purple_certificate_get_display_string(crt);
 
 	/* Make a semi-pretty display */
 	purple_notify_info(
@@ -2214,12 +2199,7 @@ purple_certificate_display_x509(PurpleCe
 		secondary);
 
 	/* Cleanup */
-	g_free(cn);
 	g_free(secondary);
-	g_free(sha_asc);
-	g_free(activ_str);
-	g_free(expir_str);
-	g_byte_array_free(sha_bin, TRUE);
 }
 
 void purple_certificate_add_ca_search_path(const char *path)
============================================================
--- libpurple/certificate.h	e37ec24bfa59f7b09286122c35d8c0ba888e61e9
+++ libpurple/certificate.h	0b5281a4695170e23041f630804d4909d70241a4
@@ -261,8 +261,16 @@ struct _PurpleCertificateScheme
 	 */
 	GByteArray * (* get_der_data)(PurpleCertificate *crt);
 
+	/**
+	 * Retrieves a string representation of the certificate suitable for display
+	 *
+	 * @param crt   Certificate instance
+	 * @return User-displayable string representation of certificate - must be
+	 *         freed using g_free().
+	 */
+	gchar * (* get_display_string)(PurpleCertificate *crt);
+
 	void (*_purple_reserved1)(void);
-	void (*_purple_reserved2)(void);
 };
 
 /** A set of operations used to provide logic for verifying a Certificate's
@@ -577,6 +585,17 @@ purple_certificate_get_der_data(PurpleCe
 GByteArray *
 purple_certificate_get_der_data(PurpleCertificate *crt);
 
+/**
+ * Retrieves a string suitable for displaying a certificate to the user.
+ *
+ * @param crt Certificate instance
+ *
+ * @return String representing the certificate that may be displayed to the user
+ *         - must be freed using g_free().
+ */
+char *
+purple_certificate_get_display_string(PurpleCertificate *crt);
+
 /*@}*/
 
 /*****************************************************************************/


More information about the Commits mailing list