diff options
Diffstat (limited to 'src/libcryptobox')
-rw-r--r-- | src/libcryptobox/keypair.c | 42 | ||||
-rw-r--r-- | src/libcryptobox/keypair.h | 11 |
2 files changed, 48 insertions, 5 deletions
diff --git a/src/libcryptobox/keypair.c b/src/libcryptobox/keypair.c index 8f0e8e8f6..e645ef3e0 100644 --- a/src/libcryptobox/keypair.c +++ b/src/libcryptobox/keypair.c @@ -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) diff --git a/src/libcryptobox/keypair.h b/src/libcryptobox/keypair.h index 697e06a3d..f913cfc65 100644 --- a/src/libcryptobox/keypair.h +++ b/src/libcryptobox/keypair.h @@ -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 */ |