Skip to content

Commit

Permalink
Merge pull request trusteddomainproject#207 form futatuki/libopendkim…
Browse files Browse the repository at this point in the history
…-expose-nametables

Expose dkim_code_to_name() and dkim_name_to_code() with tables
  • Loading branch information
futatuki committed Apr 26, 2024
2 parents 8e7aede + e421a4d commit 5e99b2f
Show file tree
Hide file tree
Showing 23 changed files with 1,047 additions and 139 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ release, and a summary of the changes in that release.
were not being properly handled.
LIBOPENDKIM: Fix implicit function declaration for snprintf.
Patch from Michael Orlitzky (PR #170).
LIBOPENDKIM: Expose conversion table between internal code already
provided as DKIM_ macros and their literal name in C string.
BUILD: Fix function signature of main() in configure. Patch from
Michael Orlitzky (PR #171).
BUILD: Fix pkgconfig names of Cyrus SASL and OpenLDAP in configure.
Expand Down
2 changes: 1 addition & 1 deletion libopendkim/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ endif
LDADD = ./libopendkim.la

lib_LTLIBRARIES = libopendkim.la
libopendkim_la_SOURCES = base32.c base64.c dkim-atps.c dkim-cache.c dkim-canon.c dkim-dns.c dkim-keys.c dkim-mailparse.c dkim-report.c dkim-tables.c dkim-test.c dkim-util.c dkim.c util.c base64.h dkim-cache.h dkim-canon.h dkim-dns.h dkim-internal.h dkim-keys.h dkim-mailparse.h dkim-report.h dkim-tables.h dkim-test.h dkim-types.h dkim-util.h dkim.h util.h
libopendkim_la_SOURCES = base32.c base64.c dkim-atps.c dkim-cache.c dkim-canon.c dkim-dns.c dkim-keys.c dkim-mailparse.c dkim-report.c dkim-tables.c dkim-test.c dkim-util.c dkim.c util.c base64.h dkim-cache.h dkim-canon.h dkim-dns.h dkim-internal.h dkim-keys.h dkim-mailparse.h dkim-report.h dkim-test.h dkim-types.h dkim-util.h dkim.h util.h
libopendkim_la_CPPFLAGS = $(LIBCRYPTO_CPPFLAGS)
libopendkim_la_CFLAGS = $(LIBCRYPTO_INCDIRS) $(LIBOPENDKIM_INC) $(COV_CFLAGS)
libopendkim_la_LDFLAGS = -no-undefined $(LIBCRYPTO_LIBDIRS) $(COV_LDFLAGS) -version-info $(LIBOPENDKIM_VERSION_INFO)
Expand Down
3 changes: 1 addition & 2 deletions libopendkim/dkim-atps.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "dkim.h"
#include "dkim-internal.h"
#include "dkim-types.h"
#include "dkim-tables.h"
#include "util.h"

#ifdef USE_GNUTLS
Expand Down Expand Up @@ -142,7 +141,7 @@ dkim_atps_check(DKIM *dkim, DKIM_SIGINFO *sig, struct timeval *timeout,
/* confirm it requested a hash we know how to do */
if (strcasecmp(ahash, "none") != 0)
{
hash = dkim_name_to_code(hashes, ahash);
hash = dkim_name_to_code(dkim_table_hashes, ahash);
if (hash == -1)
return DKIM_STAT_INVALID;
}
Expand Down
17 changes: 17 additions & 0 deletions libopendkim/dkim-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ typedef int dkim_key_t;
#define DKIM_KEY_SERVICE 5 /* s */
#define DKIM_KEY_FLAGS 6 /* t */

extern DKIM_NAMETABLE *dkim_table_keyparams;

/*
** DKIM_SETTYPE -- types of sets
*/
Expand All @@ -108,6 +110,8 @@ typedef int dkim_set_t;
#define DKIM_SETTYPE_KEY 1
#define DKIM_SETTYPE_SIGREPORT 2

extern DKIM_NAMETABLE *dkim_table_settypes;

/*
** DKIM_HASHTYPE -- types of hashes
*/
Expand All @@ -116,6 +120,8 @@ typedef int dkim_set_t;
#define DKIM_HASHTYPE_SHA1 0
#define DKIM_HASHTYPE_SHA256 1

extern DKIM_NAMETABLE *dkim_table_hashes;

/*
** DKIM_KEYTYPE -- types of keys
*/
Expand All @@ -124,6 +130,8 @@ typedef int dkim_set_t;
#define DKIM_KEYTYPE_RSA 0
#define DKIM_KEYTYPE_ED25519 1

extern DKIM_NAMETABLE *dkim_table_keytypes;

/*
** DKIM_SET -- a set of parameters and values
*/
Expand Down Expand Up @@ -152,6 +160,15 @@ typedef struct dkim_key DKIM_KEY;
struct dkim_canon;
typedef struct dkim_canon DKIM_CANON;


#ifdef _FFR_CONDITIONAL

/*
** mandatory DKIM tags
*/
extern DKIM_NAMETABLE *dkim_table_mandatory;
#endif /* _FFR_CONDITIONAL */

/* prototypes */
extern DKIM_STAT dkim_process_set __P((DKIM *, dkim_set_t, u_char *, size_t,
void *, _Bool, const char *));
Expand Down
1 change: 0 additions & 1 deletion libopendkim/dkim-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "dkim-report.h"
#include "dkim-internal.h"
#include "dkim-types.h"
#include "dkim-tables.h"
#include "util.h"

/* prototypes */
Expand Down
158 changes: 131 additions & 27 deletions libopendkim/dkim-tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@

/* system includes */
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* libopendkim includes */
#include "dkim-tables.h"
#include "dkim-internal.h"

/* structures */
struct dkim_nametable
{
const char * tbl_name; /* name */
const int tbl_code; /* code */
};

struct dkim_iter_ctx
{
DKIM_NAMETABLE *current; /* current table entry */
_Bool is_eot; /* It is last entry or not */
};

/* lookup tables */
static struct nametable prv_keyparams[] = /* key parameters */
static struct dkim_nametable prv_keyparams[] = /* key parameters */
{
{ "a", DKIM_KEY_ALGORITHM },
{ "n", DKIM_KEY_NOTES },
Expand All @@ -28,17 +41,17 @@ static struct nametable prv_keyparams[] = /* key parameters */
{ "v", DKIM_KEY_VERSION },
{ NULL, -1 }
};
struct nametable *keyparams = prv_keyparams;
DKIM_NAMETABLE *dkim_table_keyparams = prv_keyparams;

static struct nametable prv_keyflags[] = /* key flags */
static struct dkim_nametable prv_keyflags[] = /* key flags */
{
{ "y", DKIM_SIGFLAG_TESTKEY },
{ "s", DKIM_SIGFLAG_NOSUBDOMAIN },
{ NULL, -1 }
};
struct nametable *keyflags = prv_keyflags;
DKIM_NAMETABLE *dkim_table_keyflags = prv_keyflags;

static struct nametable prv_sigparams[] = /* signature parameters */
static struct dkim_nametable prv_sigparams[] = /* signature parameters */
{
{ "a", DKIM_PARAM_SIGNALG },
{ "b", DKIM_PARAM_SIGNATURE },
Expand All @@ -56,49 +69,49 @@ static struct nametable prv_sigparams[] = /* signature parameters */
{ "z", DKIM_PARAM_COPIEDHDRS },
{ NULL, -1 }
};
struct nametable *sigparams = prv_sigparams;
DKIM_NAMETABLE *dkim_table_sigparams = prv_sigparams;

static struct nametable prv_algorithms[] = /* signing algorithms */
static struct dkim_nametable prv_algorithms[] = /* signing algorithms */
{
{ "rsa-sha1", DKIM_SIGN_RSASHA1 },
{ "rsa-sha256", DKIM_SIGN_RSASHA256 },
{ "ed25519-sha256", DKIM_SIGN_ED25519SHA256 },
{ NULL, -1 },
};
struct nametable *algorithms = prv_algorithms;
DKIM_NAMETABLE *dkim_table_algorithms = prv_algorithms;

static struct nametable prv_canonicalizations[] = /* canonicalizations */
static struct dkim_nametable prv_canonicalizations[] = /* canonicalizations */
{
{ "simple", DKIM_CANON_SIMPLE },
{ "relaxed", DKIM_CANON_RELAXED },
{ NULL, -1 },
};
struct nametable *canonicalizations = prv_canonicalizations;
DKIM_NAMETABLE *dkim_table_canonicalizations = prv_canonicalizations;

static struct nametable prv_hashes[] = /* hashes */
static struct dkim_nametable prv_hashes[] = /* hashes */
{
{ "sha1", DKIM_HASHTYPE_SHA1 },
{ "sha256", DKIM_HASHTYPE_SHA256 },
{ NULL, -1 },
};
struct nametable *hashes = prv_hashes;
DKIM_NAMETABLE *dkim_table_hashes = prv_hashes;

static struct nametable prv_keytypes[] = /* key types */
static struct dkim_nametable prv_keytypes[] = /* key types */
{
{ "rsa", DKIM_KEYTYPE_RSA },
{ "ed25519", DKIM_KEYTYPE_ED25519 },
{ NULL, -1 },
};
struct nametable *keytypes = prv_keytypes;
DKIM_NAMETABLE *dkim_table_keytypes = prv_keytypes;

static struct nametable prv_querytypes[] = /* query types */
static struct dkim_nametable prv_querytypes[] = /* query types */
{
{ "dns", DKIM_QUERY_DNS },
{ NULL, -1 },
};
struct nametable *querytypes = prv_querytypes;
DKIM_NAMETABLE *dkim_table_querytypes = prv_querytypes;

static struct nametable prv_results[] = /* result codes */
static struct dkim_nametable prv_results[] = /* result codes */
{
{ "Success", DKIM_STAT_OK },
{ "Bad signature", DKIM_STAT_BADSIG },
Expand All @@ -116,20 +129,21 @@ static struct nametable prv_results[] = /* result codes */
{ "Invalid result", DKIM_STAT_CBINVALID },
{ "Try again later", DKIM_STAT_CBTRYAGAIN },
{ "Multiple DNS replies", DKIM_STAT_MULTIDNSREPLY },
{ "End of the table", DKIM_STAT_ITER_EOT },
{ NULL, -1 },
};
struct nametable *results = prv_results;
DKIM_NAMETABLE *dkim_table_results = prv_results;

static struct nametable prv_settypes[] = /* set types */
static struct dkim_nametable prv_settypes[] = /* set types */
{
{ "key", DKIM_SETTYPE_KEY },
{ "signature", DKIM_SETTYPE_SIGNATURE },
{ "signature reporting", DKIM_SETTYPE_SIGREPORT },
{ NULL, -1 },
};
struct nametable *settypes = prv_settypes;
DKIM_NAMETABLE *dkim_table_settypes = prv_settypes;

static struct nametable prv_sigerrors[] = /* signature parsing errors */
static struct dkim_nametable prv_sigerrors[] = /* signature parsing errors */
{
{ "no signature error", DKIM_SIGERROR_OK },
{ "unsupported signature version", DKIM_SIGERROR_VERSION },
Expand Down Expand Up @@ -182,15 +196,15 @@ static struct nametable prv_sigerrors[] = /* signature parsing errors */
#endif /* _FFR_CONDITIONAL */
{ NULL, -1 },
};
struct nametable *sigerrors = prv_sigerrors;
DKIM_NAMETABLE *dkim_table_sigerrors = prv_sigerrors;

#ifdef _FFR_CONDITIONAL
static struct nametable prv_mandatory[] = /* mandatory DKIM tags */
static struct dkim_nametable prv_mandatory[] = /* mandatory DKIM tags */
{
{ "!cd", 0 },
{ NULL, -1 },
};
struct nametable *mandatory = prv_mandatory;
DKIM_NAMETABLE *dkim_table_mandatory = prv_mandatory;
#endif /* _FFR_CONDITIONAL */

/* ===================================================================== */
Expand All @@ -207,7 +221,7 @@ struct nametable *mandatory = prv_mandatory;
*/

const char *
dkim_code_to_name(struct nametable *tbl, const int code)
dkim_code_to_name(DKIM_NAMETABLE *tbl, const int code)
{
int c;

Expand Down Expand Up @@ -235,7 +249,7 @@ dkim_code_to_name(struct nametable *tbl, const int code)
*/

const int
dkim_name_to_code(struct nametable *tbl, const char *name)
dkim_name_to_code(DKIM_NAMETABLE *tbl, const char *name)
{
int c;

Expand All @@ -250,3 +264,93 @@ dkim_name_to_code(struct nametable *tbl, const char *name)
return tbl[c].tbl_code;
}
}

/*
** DKIM_NAMETABLE_FIRST -- get the first entry of the table and start iteration
**
** Parameters:
** tbl -- name table
** ctx -- iteration context (returned)
** name -- name in the first item in the table (returned)
** code -- code in the first item in the table (returned)
**
** Return value:
** A DKIM_STAT_OK -- retrieve the first item successfully
** A DKIM_STAT_ITER_EOT -- the table has no item.
** A DKIM_STAT_NORESOURCE -- cannot allocate memory for the
** iteration context
**
*/
DKIM_STAT
dkim_nametable_first(DKIM_NAMETABLE *tbl, DKIM_ITER_CTX **ctx,
const char **name, int *code)
{
*ctx = (DKIM_ITER_CTX *)
malloc(sizeof(DKIM_ITER_CTX));
if (*ctx == NULL)
{
return DKIM_STAT_NORESOURCE;
}
if (tbl->tbl_name == NULL)
{
(*ctx)->current = NULL;
(*ctx)->is_eot = TRUE;
return DKIM_STAT_ITER_EOT;
}
*name = tbl->tbl_name;
*code = tbl->tbl_code;
(*ctx)->current = tbl;
(*ctx)->is_eot = (((*ctx)->current)[1].tbl_name == NULL)? TRUE : FALSE;
return DKIM_STAT_OK;
}

/*
** DKIM_NAMETABLE_NEXT -- get the next entry on the iteration the table
**
** Parameters:
** ctx -- iteration context (updated)
** name -- name in the first item in the table (returned)
** code -- code in the first item in the table (returned)
**
** Return value:
** A DKIM_STAT_OK -- retrieve the first item successfully
** A DKIM_STAT_ITER_EOT -- the table has no item.
**
*/
DKIM_STAT
dkim_nametable_next(DKIM_ITER_CTX *ctx, const char **name, int *code)
{
if (ctx->is_eot)
{
return DKIM_STAT_ITER_EOT;
}
ctx->current++;
*name = ctx->current->tbl_name;
*code = ctx->current->tbl_code;
ctx->is_eot = ((ctx->current)[1].tbl_name == NULL)? TRUE : FALSE;
return DKIM_STAT_OK;
}

/*
** DKIM_ITER_CTX_FREE -- release resources associated with
** a nametable iteration context
**
** Parameters:
** ctx -- iteration context
**
** Return value:
** DKIM_STAT_OK -- operation was successful
**
** Note: This function is a placeholder to add some operation associated
** with future changes of the structure of the tables.
**
*/
DKIM_STAT
dkim_iter_ctx_free(DKIM_ITER_CTX *ctx)
{
if (ctx != NULL)
{
free((void *)ctx);
}
return DKIM_STAT_OK;
}
Loading

0 comments on commit 5e99b2f

Please sign in to comment.