*/
static void *
rspamd_cryptobox_keypair_sk(struct rspamd_cryptobox_keypair *kp,
- unsigned int *len)
+ size_t *len)
{
g_assert(kp != NULL);
static void *
rspamd_cryptobox_keypair_pk(struct rspamd_cryptobox_keypair *kp,
- unsigned int *len)
+ size_t *len)
{
g_assert(kp != NULL);
static void *
rspamd_cryptobox_pubkey_pk(const struct rspamd_cryptobox_pubkey *kp,
- unsigned int *len)
+ size_t *len)
{
g_assert(kp != NULL);
void rspamd_cryptobox_keypair_dtor(struct rspamd_cryptobox_keypair *kp)
{
void *sk;
- unsigned int len = 0;
+ size_t len = 0;
sk = rspamd_cryptobox_keypair_sk(kp, &len);
g_assert(sk != NULL && len > 0);
{
struct rspamd_cryptobox_keypair *kp;
void *pk, *sk;
- unsigned int size;
+ size_t size;
kp = rspamd_cryptobox_keypair_alloc(type);
kp->type = type;
{
unsigned char *decoded;
gsize dlen, expected_len;
- unsigned int pklen;
+ size_t pklen;
struct rspamd_cryptobox_pubkey *pk;
unsigned char *pk_data;
{
unsigned char *decoded;
gsize dlen, expected_len;
- unsigned int pklen;
+ size_t pklen;
struct rspamd_cryptobox_pubkey *pk;
unsigned char *pk_data;
gsize len,
enum rspamd_cryptobox_keypair_type type)
{
- unsigned int pklen;
+ size_t pklen;
struct rspamd_cryptobox_pubkey *pk;
unsigned char *pk_data;
unsigned int *len)
{
unsigned char *ret = NULL;
- unsigned int rlen;
+ size_t rlen;
ret = rspamd_cryptobox_pubkey_pk(pk, &rlen);
static void
rspamd_keypair_print_component(unsigned char *data, gsize datalen,
- GString *res, unsigned int how, const char *description)
+ GString *res, unsigned int how, const char *description,
+ enum rspamd_cryptobox_keypair_encoding encoding)
{
int olen, b32_len;
rspamd_printf_gstring(res, "%s: ", description);
}
- if (how & RSPAMD_KEYPAIR_BASE32) {
+ if (encoding == RSPAMD_KEYPAIR_ENCODING_ZBASE32) {
b32_len = (datalen * 8 / 5) + 2;
g_string_set_size(res, res->len + b32_len);
res->len -= b32_len;
res->str[res->len] = '\0';
}
}
- else if (how & RSPAMD_KEYPAIR_HEX) {
+ else if (encoding == RSPAMD_KEYPAIR_ENCODING_HEX) {
rspamd_printf_gstring(res, "%*xs", (int) datalen, data);
}
+ else if (encoding == RSPAMD_KEYPAIR_ENCODING_BASE64) {
+ rspamd_printf_gstring(res, "%*Bs", (int) datalen, data);
+ }
else {
g_string_append_len(res, data, datalen);
}
}
GString *
-rspamd_keypair_print(struct rspamd_cryptobox_keypair *kp, unsigned int how)
+rspamd_keypair_print(struct rspamd_cryptobox_keypair *kp,
+ enum rspamd_cryptobox_keypair_encoding encoding,
+ unsigned int how)
{
GString *res;
- unsigned int len;
+ size_t len;
gpointer p;
g_assert(kp != NULL);
if ((how & RSPAMD_KEYPAIR_PUBKEY)) {
p = rspamd_cryptobox_keypair_pk(kp, &len);
- rspamd_keypair_print_component(p, len, res, how, "Public key");
+ rspamd_keypair_print_component(p, len, res, how, "Public key", encoding);
}
if ((how & RSPAMD_KEYPAIR_PRIVKEY)) {
p = rspamd_cryptobox_keypair_sk(kp, &len);
- rspamd_keypair_print_component(p, len, res, how, "Private key");
+ rspamd_keypair_print_component(p, len, res, how, "Private key", encoding);
}
if ((how & RSPAMD_KEYPAIR_ID_SHORT)) {
rspamd_keypair_print_component(kp->id, RSPAMD_KEYPAIR_SHORT_ID_LEN,
- res, how, "Short key ID");
+ res, how, "Short key ID", encoding);
}
if ((how & RSPAMD_KEYPAIR_ID)) {
- rspamd_keypair_print_component(kp->id, sizeof(kp->id), res, how, "Key ID");
+ rspamd_keypair_print_component(kp->id, sizeof(kp->id), res, how, "Key ID", encoding);
}
return res;
}
GString *
-rspamd_pubkey_print(struct rspamd_cryptobox_pubkey *pk, unsigned int how)
+rspamd_pubkey_print(struct rspamd_cryptobox_pubkey *pk,
+ enum rspamd_cryptobox_keypair_encoding encoding,
+ unsigned int how)
{
GString *res;
- unsigned int len;
+ size_t len;
gpointer p;
g_assert(pk != NULL);
if ((how & RSPAMD_KEYPAIR_PUBKEY)) {
p = rspamd_cryptobox_pubkey_pk(pk, &len);
- rspamd_keypair_print_component(p, len, res, how, "Public key");
+ rspamd_keypair_print_component(p, len, res, how, "Public key", encoding);
}
if ((how & RSPAMD_KEYPAIR_ID_SHORT)) {
rspamd_keypair_print_component(pk->id, RSPAMD_KEYPAIR_SHORT_ID_LEN,
- res, how, "Short key ID");
+ res, how, "Short key ID", encoding);
}
if ((how & RSPAMD_KEYPAIR_ID)) {
rspamd_keypair_print_component(pk->id, sizeof(pk->id), res, how,
- "Key ID");
+ "Key ID", encoding);
}
return res;
rspamd_keypair_component(struct rspamd_cryptobox_keypair *kp,
unsigned int ncomp, unsigned int *len)
{
- unsigned int rlen = 0;
+ size_t rlen = 0;
const unsigned char *ret = NULL;
g_assert(kp != NULL);
const ucl_object_t *privkey, *pubkey, *elt;
const char *str;
enum rspamd_cryptobox_keypair_type type = RSPAMD_KEYPAIR_KEX;
- gboolean is_hex = FALSE;
+ enum rspamd_cryptobox_keypair_encoding encoding = RSPAMD_KEYPAIR_ENCODING_DEFAULT;
struct rspamd_cryptobox_keypair *kp;
- unsigned int len;
- gsize ucl_len;
- int dec_len;
+ size_t len;
+ size_t ucl_len;
+ size_t dec_len;
gpointer target;
if (ucl_object_type(obj) != UCL_OBJECT) {
str = ucl_object_tostring(elt);
if (g_ascii_strcasecmp(str, "hex") == 0) {
- is_hex = TRUE;
+ encoding = RSPAMD_KEYPAIR_ENCODING_HEX;
+ }
+ else if (g_ascii_strcasecmp(str, "base64") == 0) {
+ encoding = RSPAMD_KEYPAIR_ENCODING_BASE64;
}
/* TODO: handle errors */
}
target = rspamd_cryptobox_keypair_sk(kp, &len);
str = ucl_object_tolstring(privkey, &ucl_len);
- if (is_hex) {
+ switch (encoding) {
+ case RSPAMD_KEYPAIR_ENCODING_HEX:
dec_len = rspamd_decode_hex_buf(str, ucl_len, target, len);
- }
- else {
+ break;
+ case RSPAMD_KEYPAIR_ENCODING_ZBASE32:
dec_len = rspamd_decode_base32_buf(str, ucl_len, target, len, RSPAMD_BASE32_DEFAULT);
+ break;
+ case RSPAMD_KEYPAIR_ENCODING_BASE64:
+ dec_len = rspamd_cryptobox_base64_decode(str, ucl_len, target, &len);
+ break;
+ case RSPAMD_KEYPAIR_ENCODING_BINARY:
+ if (len >= ucl_len) {
+ memcpy(target, str, ucl_len);
+ dec_len = ucl_len;
+ }
+ else {
+ memcpy(target, str, len);
+ dec_len = len;
+ }
+ break;
}
+
if (dec_len != (int) len) {
rspamd_keypair_unref(kp);
target = rspamd_cryptobox_keypair_pk(kp, &len);
str = ucl_object_tolstring(pubkey, &ucl_len);
- if (is_hex) {
+ switch (encoding) {
+ case RSPAMD_KEYPAIR_ENCODING_HEX:
dec_len = rspamd_decode_hex_buf(str, ucl_len, target, len);
- }
- else {
+ break;
+ case RSPAMD_KEYPAIR_ENCODING_ZBASE32:
dec_len = rspamd_decode_base32_buf(str, ucl_len, target, len, RSPAMD_BASE32_DEFAULT);
+ break;
+ case RSPAMD_KEYPAIR_ENCODING_BASE64:
+ dec_len = rspamd_cryptobox_base64_decode(str, ucl_len, target, &len);
+ break;
+ case RSPAMD_KEYPAIR_ENCODING_BINARY:
+ if (len >= ucl_len) {
+ memcpy(target, str, ucl_len);
+ dec_len = ucl_len;
+ }
+ else {
+ memcpy(target, str, len);
+ dec_len = len;
+ }
+ break;
}
if (dec_len != (int) len) {
ucl_object_t *
rspamd_keypair_to_ucl(struct rspamd_cryptobox_keypair *kp,
+ enum rspamd_cryptobox_keypair_encoding encoding,
enum rspamd_keypair_dump_flags flags)
{
ucl_object_t *ucl_out, *elt;
- int how = 0;
GString *keypair_out;
- const char *encoding;
+ const char *encoding_str = NULL;
g_assert(kp != NULL);
- if (flags & RSPAMD_KEYPAIR_DUMP_HEX) {
- how |= RSPAMD_KEYPAIR_HEX;
- encoding = "hex";
- }
- else {
- how |= RSPAMD_KEYPAIR_BASE32;
- encoding = "base32";
- }
-
if (flags & RSPAMD_KEYPAIR_DUMP_FLATTENED) {
ucl_out = ucl_object_typed_new(UCL_OBJECT);
elt = ucl_out;
ucl_object_insert_key(ucl_out, elt, "keypair", 0, false);
}
+ if (encoding == RSPAMD_KEYPAIR_ENCODING_HEX) {
+ encoding_str = "hex";
+ }
+ else if (encoding == RSPAMD_KEYPAIR_ENCODING_BASE64) {
+ encoding_str = "base64";
+ }
+ /* XXX: maybe support binary here ? */
/* pubkey part */
- keypair_out = rspamd_keypair_print(kp,
- RSPAMD_KEYPAIR_PUBKEY | how);
+ keypair_out = rspamd_keypair_print(kp, encoding,
+ RSPAMD_KEYPAIR_PUBKEY | flags);
ucl_object_insert_key(elt,
ucl_object_fromlstring(keypair_out->str, keypair_out->len),
"pubkey", 0, false);
if (!(flags & RSPAMD_KEYPAIR_DUMP_NO_SECRET)) {
/* privkey part */
- keypair_out = rspamd_keypair_print(kp,
- RSPAMD_KEYPAIR_PRIVKEY | how);
+ keypair_out = rspamd_keypair_print(kp, encoding,
+ RSPAMD_KEYPAIR_PRIVKEY | flags);
ucl_object_insert_key(elt,
ucl_object_fromlstring(keypair_out->str, keypair_out->len),
"privkey", 0, false);
}
keypair_out = rspamd_keypair_print(kp,
- RSPAMD_KEYPAIR_ID | how);
+ encoding,
+ RSPAMD_KEYPAIR_ID | flags);
ucl_object_insert_key(elt,
ucl_object_fromlstring(keypair_out->str, keypair_out->len),
"id", 0, false);
g_string_free(keypair_out, TRUE);
- ucl_object_insert_key(elt,
- ucl_object_fromstring(encoding),
- "encoding", 0, false);
+ if (encoding_str) {
+ ucl_object_insert_key(elt,
+ ucl_object_fromstring(encoding_str),
+ "encoding", 0, false);
+ }
ucl_object_insert_key(elt,
ucl_object_fromstring("curve25519"),
#define RSPAMD_KEYPAIR_ID 0x4
/** Print short key id */
#define RSPAMD_KEYPAIR_ID_SHORT 0x8
-/** Encode output with base 32 */
-#define RSPAMD_KEYPAIR_BASE32 0x10
/** Human readable output */
#define RSPAMD_KEYPAIR_HUMAN 0x20
-#define RSPAMD_KEYPAIR_HEX 0x40
-
-/**
- * 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_keypair_print(struct rspamd_cryptobox_keypair *kp,
- unsigned int 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,
- unsigned int how);
/** Get keypair pubkey ID */
#define RSPAMD_KEYPAIR_COMPONENT_ID 0
enum rspamd_keypair_dump_flags {
RSPAMD_KEYPAIR_DUMP_DEFAULT = 0,
- RSPAMD_KEYPAIR_DUMP_HEX = 1u << 0u,
RSPAMD_KEYPAIR_DUMP_NO_SECRET = 1u << 1u,
RSPAMD_KEYPAIR_DUMP_FLATTENED = 1u << 2u,
};
+enum rspamd_cryptobox_keypair_encoding {
+ RSPAMD_KEYPAIR_ENCODING_ZBASE32 = 0,
+ RSPAMD_KEYPAIR_ENCODING_HEX = 1,
+ RSPAMD_KEYPAIR_ENCODING_BASE64 = 2,
+ RSPAMD_KEYPAIR_ENCODING_BINARY = 3,
+ RSPAMD_KEYPAIR_ENCODING_DEFAULT = RSPAMD_KEYPAIR_ENCODING_ZBASE32,
+};
+
+
+/**
+ * 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,
+ enum rspamd_cryptobox_keypair_encoding encoding,
+ unsigned int how);
+/**
+ * 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_keypair_print(struct rspamd_cryptobox_keypair *kp,
+ enum rspamd_cryptobox_keypair_encoding encoding,
+ unsigned int how);
/**
* Converts keypair to ucl object
* @param kp
* @return
*/
+
ucl_object_t *rspamd_keypair_to_ucl(struct rspamd_cryptobox_keypair *kp,
+ enum rspamd_cryptobox_keypair_encoding encoding,
enum rspamd_keypair_dump_flags flags);