]> source.dussan.org Git - rspamd.git/commitdiff
Add routines to print pubkey components
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 6 Feb 2016 14:09:21 +0000 (14:09 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 6 Feb 2016 14:09:21 +0000 (14:09 +0000)
src/libcryptobox/keypair.c
src/libcryptobox/keypair.h

index 8f0e8e8f6f5b0619978902628ce7bbed796efb9b..e645ef3e07152d9c23868215a5ca1df51fc588fb 100644 (file)
@@ -146,6 +146,8 @@ rspamd_cryptobox_keypair_alloc (enum rspamd_cryptobox_keypair_type type,
                abort ();
        }
 
+       memset (kp, 0, size);
+
        return kp;
 }
 
@@ -153,7 +155,7 @@ static struct rspamd_cryptobox_pubkey *
 rspamd_cryptobox_pubkey_alloc (enum rspamd_cryptobox_keypair_type type,
                enum rspamd_cryptobox_mode alg)
 {
-       struct rspamd_cryptobox_pubkey *kp;
+       struct rspamd_cryptobox_pubkey *pk;
        guint size = 0;
 
        if (alg == RSPAMD_CRYPTOBOX_MODE_25519) {
@@ -173,13 +175,15 @@ rspamd_cryptobox_pubkey_alloc (enum rspamd_cryptobox_keypair_type type,
                }
        }
 
-       g_assert (size >= sizeof (*kp));
+       g_assert (size >= sizeof (*pk));
 
-       if (posix_memalign ((void **)&kp, 32, size) != 0) {
+       if (posix_memalign ((void **)&pk, 32, size) != 0) {
                abort ();
        }
 
-       return kp;
+       memset (pk, 0, size);
+
+       return pk;
 }
 
 
@@ -553,7 +557,8 @@ rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp, guint how)
                rspamd_keypair_print_component (p, len, res, how, "Private key");
        }
        if ((how & RSPAMD_KEYPAIR_ID_SHORT)) {
-               rspamd_keypair_print_component (kp->id, 5, res, how, "Short key ID");
+               rspamd_keypair_print_component (kp->id, RSPAMD_KEYPAIR_SHORT_ID_LEN,
+                               res, how, "Short key ID");
        }
        if ((how & RSPAMD_KEYPAIR_ID)) {
                rspamd_keypair_print_component (kp->id, sizeof (kp->id), res, how, "Key ID");
@@ -562,6 +567,33 @@ rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp, guint how)
        return res;
 }
 
+GString *
+rspamd_pubkey_print (struct rspamd_cryptobox_pubkey *pk, guint how)
+{
+       GString *res;
+       guint len;
+       gpointer p;
+
+       g_assert (pk != NULL);
+
+       res = g_string_sized_new (63);
+
+       if ((how & RSPAMD_KEYPAIR_PUBKEY)) {
+               p = rspamd_cryptobox_pubkey_pk (pk, &len);
+               rspamd_keypair_print_component (p, len, res, how, "Public key");
+       }
+       if ((how & RSPAMD_KEYPAIR_ID_SHORT)) {
+               rspamd_keypair_print_component (pk->id, RSPAMD_KEYPAIR_SHORT_ID_LEN,
+                               res, how, "Short key ID");
+       }
+       if ((how & RSPAMD_KEYPAIR_ID)) {
+               rspamd_keypair_print_component (pk->id, sizeof (pk->id), res, how,
+                               "Key ID");
+       }
+
+       return res;
+}
+
 const guchar *
 rspamd_keypair_component (struct rspamd_cryptobox_keypair *kp,
                guint ncomp, guint *len)
index 697e06a3d83ad5bbef2163a8279324156673aa4f..f913cfc658a4531bdf8c310fd2ae361d7be6790b 100644 (file)
@@ -169,6 +169,8 @@ const guchar * rspamd_pubkey_get_id (struct rspamd_cryptobox_pubkey *pk);
 const guchar * rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk,
                guint *len);
 
+/** Short ID characters count */
+#define RSPAMD_KEYPAIR_SHORT_ID_LEN 5
 /** Print pubkey */
 #define RSPAMD_KEYPAIR_PUBKEY 0x1
 /** Print secret key */
@@ -191,6 +193,15 @@ const guchar * rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk,
 GString *rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp,
                guint how);
 
+/**
+ * Print pubkey encoding it if needed
+ * @param key key to print
+ * @param how flags that specifies printing behaviour
+ * @return newly allocated string with keypair
+ */
+GString *rspamd_pubkey_print (struct rspamd_cryptobox_pubkey *pk,
+               guint how);
+
 /** Get keypair pubkey ID */
 #define RSPAMD_KEYPAIR_COMPONENT_ID 0
 /** Get keypair public key */