]> source.dussan.org Git - rspamd.git/commitdiff
Add routine to print keys.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Jan 2015 18:06:34 +0000 (18:06 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Jan 2015 18:06:34 +0000 (18:06 +0000)
src/libutil/http.c
src/libutil/http.h

index c00d2c40cfaae8132a685ded7a0609791d827692..9a473893203ca9a893c6bf051620f44665efa3f5 100644 (file)
@@ -1697,6 +1697,56 @@ rspamd_http_connection_gen_key (void)
        return (gpointer)kp;
 }
 
+static void
+rspamd_http_print_key_component (guchar *data, gsize datalen,
+               GString *res, guint how, const gchar *description)
+{
+       gchar *b32;
+
+       if (how & RSPAMD_KEYPAIR_HUMAN) {
+               g_string_append_printf (res, "%s: ", description);
+       }
+
+       if (how & RSPAMD_KEYPAIR_BASE32) {
+               b32 = rspamd_encode_base32 (data, datalen);
+               g_string_append_printf (res, "%s", b32);
+               g_free (b32);
+       }
+       else {
+               g_string_append_len (res, data, datalen);
+       }
+
+       if (how & RSPAMD_KEYPAIR_HUMAN) {
+               g_string_append_c (res, '\n');
+       }
+}
+
+GString *
+rspamd_http_connection_print_key (gpointer key, guint how)
+{
+       struct rspamd_http_keypair *kp = (struct rspamd_http_keypair *)key;
+       GString *res;
+
+       g_assert (key != NULL);
+
+       res = g_string_new (NULL);
+
+       if ((how & RSPAMD_KEYPAIR_PUBKEY)) {
+               rspamd_http_print_key_component (kp->pk, sizeof (kp->pk), res, how,
+                               "Public key");
+       }
+       if ((how & RSPAMD_KEYPAIR_PRIVKEY)) {
+               rspamd_http_print_key_component (kp->sk, sizeof (kp->sk), res, how,
+                               "Private key");
+       }
+       if ((how & RSPAMD_KEYPAIR_ID)) {
+               rspamd_http_print_key_component (kp->sk, sizeof (kp->sk), res, how,
+                               "Key ID");
+       }
+
+       return res;
+}
+
 void
 rspamd_http_connection_set_key (struct rspamd_http_connection *conn,
                gpointer key)
index 589b382457d9fe642b04b44f26a3c615ad685a96..5183d43746b018b6afeab6b9fff05494e0d917f7 100644 (file)
@@ -169,6 +169,25 @@ gpointer rspamd_http_connection_gen_key (void);
  */
 void rspamd_http_connection_set_key (struct rspamd_http_connection *conn,
                gpointer key);
+
+/** Print pubkey */
+#define RSPAMD_KEYPAIR_PUBKEY 0x1
+/** Print secret key */
+#define RSPAMD_KEYPAIR_PRIVKEY 0x2
+/** Print key id */
+#define RSPAMD_KEYPAIR_ID 0x4
+/** Encode output with base 32 */
+#define RSPAMD_KEYPAIR_BASE32 0x8
+/** Human readable output */
+#define RSPAMD_KEYPAIR_HUMAN 0x16
+/**
+ * Print keypair encoding it if needed
+ * @param key key to print
+ * @param how flags that specifies printing behaviour
+ * @return newly allocated string with keypair
+ */
+GString *rspamd_http_connection_print_key (gpointer key, guint how);
+
 /**
  * Release key pointed by an opaque pointer
  * @param key opaque key structure