[gcr] gcr: Add support for encoding certificate requests as PEM



commit 2edc1fc3bdb4e014bd9188ea5ef776fda21849b0
Author: Stef Walter <stefw collabora co uk>
Date:   Wed Nov 23 13:01:20 2011 +0100

    gcr: Add support for encoding certificate requests as PEM
    
     * Add textual argument to gcr_certificate_request_encode()

 gcr/gcr-certificate-request.c        |   28 +++++++++++++++++++++++++---
 gcr/gcr-certificate-request.h        |    1 +
 gcr/tests/frob-certificate-request.c |   14 ++++----------
 3 files changed, 30 insertions(+), 13 deletions(-)
---
diff --git a/gcr/gcr-certificate-request.c b/gcr/gcr-certificate-request.c
index dce8d7d..477c443 100644
--- a/gcr/gcr-certificate-request.c
+++ b/gcr/gcr-certificate-request.c
@@ -26,6 +26,7 @@
 #include "gcr-oids.h"
 #include "gcr-subject-public-key.h"
 
+#include <egg/egg-armor.h>
 #include <egg/egg-asn1x.h>
 #include <egg/egg-asn1-defs.h>
 #include <egg/egg-dn.h>
@@ -571,20 +572,28 @@ gcr_certificate_request_complete_finish (GcrCertificateRequest *self,
 /**
  * gcr_certificate_request_encode:
  * @self: a certificate request
+ * @textual: whether to encode output as text
  * @length: location to place length of returned data
  *
  * Encode the certificate request. It must have been completed with
  * gcr_certificate_request_complete() or gcr_certificate_request_complete_async()
  *
- * The output is a DER encoded certificate request.
+ * If @textual is %FALSE, the output is a DER encoded certificate request.
+ *
+ * If @textual is %TRUE, the output is encoded as text. For PKCS\#10 requests this
+ * is done using the OpenSSL style PEM encoding.
  *
  * Returns: (transfer full) (array length=length): the encoded certificate request
  */
 guchar *
 gcr_certificate_request_encode (GcrCertificateRequest *self,
+                                gboolean textual,
                                 gsize *length)
 {
 	EggBytes *bytes;
+	gpointer encoded;
+	gpointer data;
+	gsize size;
 
 	g_return_val_if_fail (GCR_IS_CERTIFICATE_REQUEST (self), NULL);
 	g_return_val_if_fail (length != NULL, NULL);
@@ -596,6 +605,19 @@ gcr_certificate_request_encode (GcrCertificateRequest *self,
 		return NULL;
 	}
 
-	*length = egg_bytes_get_size (bytes);
-	return g_byte_array_free (egg_bytes_unref_to_array (bytes), FALSE);
+	size = egg_bytes_get_size (bytes);
+	encoded = g_byte_array_free (egg_bytes_unref_to_array (bytes), FALSE);
+
+	if (textual) {
+		data = egg_armor_write (encoded, size,
+		                        g_quark_from_static_string ("CERTIFICATE REQUEST"),
+		                        NULL, length);
+		g_free (encoded);
+		encoded = data;
+
+	} else {
+		*length = size;
+	}
+
+	return encoded;
 }
diff --git a/gcr/gcr-certificate-request.h b/gcr/gcr-certificate-request.h
index c2f715c..53e1284 100644
--- a/gcr/gcr-certificate-request.h
+++ b/gcr/gcr-certificate-request.h
@@ -68,6 +68,7 @@ gboolean                     gcr_certificate_request_complete_finish  (GcrCertif
                                                                        GError **error);
 
 guchar *                     gcr_certificate_request_encode           (GcrCertificateRequest *self,
+                                                                       gboolean textual,
                                                                        gsize *length);
 
 G_END_DECLS
diff --git a/gcr/tests/frob-certificate-request.c b/gcr/tests/frob-certificate-request.c
index 78c0815..ecf8584 100644
--- a/gcr/tests/frob-certificate-request.c
+++ b/gcr/tests/frob-certificate-request.c
@@ -71,8 +71,8 @@ test_request (const gchar *uri)
 	GcrCertificateRequest *req;
 	GError *error = NULL;
 	GckObject *key;
-	guchar *data, *output;
-	gsize n_data, n_output;
+	guchar *data;
+	gsize n_data;
 
 	key = load_key_for_uri (uri);
 	if (key == NULL)
@@ -85,16 +85,10 @@ test_request (const gchar *uri)
 	gcr_certificate_request_complete (req, NULL, &error);
 	g_assert_no_error (error);
 
-	data = gcr_certificate_request_encode (req, &n_data);
-
-	output = egg_armor_write (data, n_data,
-	                          g_quark_from_static_string ("CERTIFICATE REQUEST"),
-	                          NULL, &n_output);
+	data = gcr_certificate_request_encode (req, TRUE, &n_data);
 
+	write (1, data, n_data);
 	g_free (data);
-
-	write (1, output, n_output);
-	g_free (output);
 }
 
 int



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