summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJannis Hoffmann <jannis@fehcom.de>2024-07-08 17:55:05 +0200
committerJannis Hoffmann <jannis@fehcom.de>2024-07-08 17:55:05 +0200
commit1001ed6c2ae98a5b9ed707b7710eb3ae87a0a7ce (patch)
treef30f7780a24fc6143e49658d696ba8f661bdfcdc /src
parente8046bd96422c3bedaa0f0e693cac28d3458d4bd (diff)
Downgraded qmail-dkim from C++ to C. Made DKIMContext opaque.
Added functions DKIMContextNew and DKIMContextFree. Changed fields of DKIMContext.
Diffstat (limited to 'src')
-rw-r--r--src/dkim.cpp42
-rw-r--r--src/include/dkim.h28
-rw-r--r--src/meson.build2
-rw-r--r--src/qmail-dkim.c (renamed from src/qmail-dkim.cpp)52
4 files changed, 59 insertions, 65 deletions
diff --git a/src/dkim.cpp b/src/dkim.cpp
index 8f07519..ff21423 100644
--- a/src/dkim.cpp
+++ b/src/dkim.cpp
@@ -27,8 +27,6 @@
#include "dkimverify.h"
-#define DKIMID ('D' | 'K' << 8 | 'I' << 16 | 'M' << 24)
-
/* taken from removed file "ressource.h" */
#ifdef VERSION
#define VERSION_STRING VERSION
@@ -36,21 +34,19 @@
#define VERSION_STRING "1.4.0"
#endif
+struct DKIMContext {
+ CDKIMSign *signObject;
+ CDKIMVerify *verifyObject;
+};
-static void InitContext(DKIMContext *pContext, bool bSign, void *pObject)
+DKIMContext *DKIMContextNew()
{
- pContext->reserved1 = DKIMID;
- pContext->reserved2 = bSign ? 1 : 0;
- pContext->reserved3 = pObject;
+ return new DKIMContext;
}
-static void *ValidateContext(DKIMContext *pContext, bool bSign)
+void DKIMContextFree(DKIMContext *ctx)
{
- if (pContext->reserved1 != DKIMID) return NULL;
-
- if (pContext->reserved2 != (unsigned int)(bSign ? 1 : 0)) return NULL;
-
- return pContext->reserved3;
+ delete ctx;
}
int DKIM_CALL DKIMSignInit(DKIMContext *pSignContext, DKIMSignOptions *pOptions)
@@ -64,13 +60,13 @@ int DKIM_CALL DKIMSignInit(DKIMContext *pSignContext, DKIMSignOptions *pOptions)
if (nRet != DKIM_SUCCESS) delete pSign;
}
- if (nRet == DKIM_SUCCESS) InitContext(pSignContext, true, pSign);
+ if (nRet == DKIM_SUCCESS) pSignContext->signObject = pSign;
return nRet;
}
int DKIM_CALL DKIMSignProcess(DKIMContext *pSignContext, char *szBuffer, int nBufLength)
{
- CDKIMSign *pSign = (CDKIMSign *)ValidateContext(pSignContext, true);
+ CDKIMSign *pSign = pSignContext->signObject;
if (pSign) return pSign->Process(szBuffer, nBufLength, false);
return DKIM_INVALID_CONTEXT;
@@ -79,7 +75,7 @@ int DKIM_CALL DKIMSignProcess(DKIMContext *pSignContext, char *szBuffer, int nBu
int DKIM_CALL DKIMSignGetSig2(
DKIMContext *pSignContext, char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature)
{
- CDKIMSign *pSign = (CDKIMSign *)ValidateContext(pSignContext, true);
+ CDKIMSign *pSign = pSignContext->signObject;
if (pSign) return pSign->GetSig2(szRSAPrivKey, szECCPrivKey, pszSignature);
return DKIM_INVALID_CONTEXT;
@@ -87,11 +83,11 @@ int DKIM_CALL DKIMSignGetSig2(
void DKIM_CALL DKIMSignFree(DKIMContext *pSignContext)
{
- CDKIMSign *pSign = (CDKIMSign *)ValidateContext(pSignContext, true);
+ CDKIMSign *pSign = pSignContext->signObject;
if (pSign) {
delete pSign;
- pSignContext->reserved3 = NULL;
+ pSignContext->signObject = NULL;
}
}
@@ -106,7 +102,7 @@ int DKIM_CALL DKIMVerifyInit(DKIMContext *pVerifyContext, DKIMVerifyOptions *pOp
if (nRet != DKIM_SUCCESS) delete pVerify;
}
- if (nRet == DKIM_SUCCESS) InitContext(pVerifyContext, false, pVerify);
+ if (nRet == DKIM_SUCCESS) pVerifyContext->verifyObject = pVerify;
return nRet;
}
@@ -114,7 +110,7 @@ int DKIM_CALL DKIMVerifyInit(DKIMContext *pVerifyContext, DKIMVerifyOptions *pOp
int DKIM_CALL DKIMVerifyProcess(DKIMContext *pVerifyContext, const char *const szBuffer, int nBufLength)
{
- CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false);
+ CDKIMVerify *pVerify = pVerifyContext->verifyObject;
if (pVerify) return pVerify->Process(szBuffer, nBufLength, false);
@@ -123,7 +119,7 @@ int DKIM_CALL DKIMVerifyProcess(DKIMContext *pVerifyContext, const char *const s
int DKIM_CALL DKIMVerifyResults(DKIMContext *pVerifyContext)
{
- CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false);
+ CDKIMVerify *pVerify = pVerifyContext->verifyObject;
if (pVerify) return pVerify->GetResults();
return DKIM_INVALID_CONTEXT;
@@ -134,7 +130,7 @@ int DKIM_CALL DKIMVerifyGetDetails(
{
szPractices[0] = '\0';
- CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false);
+ CDKIMVerify *pVerify = pVerifyContext->verifyObject;
if (pVerify) {
strcpy(szPractices, pVerify->GetPractices());
@@ -147,11 +143,11 @@ int DKIM_CALL DKIMVerifyGetDetails(
void DKIM_CALL DKIMVerifyFree(DKIMContext *pVerifyContext)
{
- CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false);
+ CDKIMVerify *pVerify = pVerifyContext->verifyObject;
if (pVerify) {
delete pVerify;
- pVerifyContext->reserved3 = NULL;
+ pVerifyContext->verifyObject = NULL;
}
}
diff --git a/src/include/dkim.h b/src/include/dkim.h
index a74b785..64e2c59 100644
--- a/src/include/dkim.h
+++ b/src/include/dkim.h
@@ -89,11 +89,7 @@ typedef int (*DKIMHEADERCALLBACK)(const char *szHeader);
// This function is called to retrieve a TXT record from DNS
typedef int (*DKIMDNSCALLBACK)(const char *szFQDN, char *szBuffer, int nBufLen);
-typedef struct DKIMContext_t {
- unsigned int reserved1;
- unsigned int reserved2;
- void *reserved3;
-} DKIMContext;
+struct DKIMContext;
typedef struct DKIMSignOptions_t {
int nCanon; // canonization
@@ -132,18 +128,22 @@ typedef struct DKIMVerifyDetails_t {
int nResult;
} DKIMVerifyDetails;
-int DKIM_CALL DKIMSignInit(DKIMContext *pSignContext, DKIMSignOptions *pOptions);
-int DKIM_CALL DKIMSignProcess(DKIMContext *pSignContext, char *szBuffer, int nBufLength);
+struct DKIMContext *DKIMContextNew();
+void DKIMContextFree(struct DKIMContext *);
+
+int DKIM_CALL DKIMSignInit(struct DKIMContext *pSignContext, DKIMSignOptions *pOptions);
+int DKIM_CALL DKIMSignProcess(struct DKIMContext *pSignContext, char *szBuffer, int nBufLength);
int DKIM_CALL DKIMSignGetSig2(
- DKIMContext *pSignContext, char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature);
-void DKIM_CALL DKIMSignFree(DKIMContext *pSignContext);
+ struct DKIMContext *pSignContext, char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature);
+void DKIM_CALL DKIMSignFree(struct DKIMContext *pSignContext);
-int DKIM_CALL DKIMVerifyInit(DKIMContext *pVerifyContext, DKIMVerifyOptions *pOptions);
-int DKIM_CALL DKIMVerifyProcess(DKIMContext *pVerifyContext, const char *szBuffer, int nBufLength);
-int DKIM_CALL DKIMVerifyResults(DKIMContext *pVerifyContext);
+int DKIM_CALL DKIMVerifyInit(struct DKIMContext *pVerifyContext, DKIMVerifyOptions *pOptions);
+int DKIM_CALL DKIMVerifyProcess(
+ struct DKIMContext *pVerifyContext, const char *szBuffer, int nBufLength);
+int DKIM_CALL DKIMVerifyResults(struct DKIMContext *pVerifyContext);
int DKIM_CALL DKIMVerifyGetDetails(
- DKIMContext *pVerifyContext, int *nSigCount, DKIMVerifyDetails **pDetails, char *szPractices);
-void DKIM_CALL DKIMVerifyFree(DKIMContext *pVerifyContext);
+ struct DKIMContext *pVerifyContext, int *nSigCount, DKIMVerifyDetails **pDetails, char *szPractices);
+void DKIM_CALL DKIMVerifyFree(struct DKIMContext *pVerifyContext);
// const char *DKIM_CALL DKIMVersion();
diff --git a/src/meson.build b/src/meson.build
index 1f33050..6b40c42 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -298,7 +298,7 @@ if get_option('build-dkim')
dependencies : [dnsresolv_dep, ssl_dep, crypto_dep],
build_by_default : false)
- executable('qmail-dkim', 'qmail-dkim.cpp',
+ executable('qmail-dkim', 'qmail-dkim.c',
include_directories : inc,
install : true,
link_with : dkim_slib,
diff --git a/src/qmail-dkim.cpp b/src/qmail-dkim.c
index 1a05e6c..8b28ab1 100644
--- a/src/qmail-dkim.cpp
+++ b/src/qmail-dkim.c
@@ -1,19 +1,19 @@
/*****************************************************************************
-* Copyright 2005 Alt-N Technologies, Ltd.
+* Copyright 2005 Alt-N Technologies, Ltd.
*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
*
-* http://www.apache.org/licenses/LICENSE-2.0
+* http://www.apache.org/licenses/LICENSE-2.0
*
-* This code incorporates intellectual property owned by Yahoo! and licensed
+* This code incorporates intellectual property owned by Yahoo! and licensed
* pursuant to the Yahoo! DomainKeys Patent License Agreement.
*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
* limitations under the License.
*
* Changes done by ¢feh@fehcom.de obeying the above license
@@ -28,6 +28,7 @@
*****************************************************************************/
#include <unistd.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -35,10 +36,6 @@
#include "dkim.h"
-extern "C" {
-#include "dns.h"
-}
-
// change these to your selector name, domain name, etc
#define MYRSASELECTOR "default"
#define MYECCSELECTOR "eddy"
@@ -48,7 +45,7 @@ extern "C" {
#define strnicmp strncasecmp
#define FDLOG stderr /* writing to another FD requires a method */
-int DKIM_CALL SignThisHeader(const char *szHeader)
+static int DKIM_CALL SignThisHeader(const char *szHeader)
{
if (strnicmp(szHeader, "X-", 2) == 0) {
return 0;
@@ -56,12 +53,12 @@ int DKIM_CALL SignThisHeader(const char *szHeader)
return 1;
}
-int DKIM_CALL SelectorCallback(const char *szFQDN, char *szBuffer, int nBufLen)
+static int DKIM_CALL SelectorCallback(const char *szFQDN, char *szBuffer, int nBufLen)
{
return 0;
}
-void usage()
+static void usage()
{
char version[] = "1.4.0";
fprintf(FDLOG, "qmail-dkim %s \n", version);
@@ -113,7 +110,7 @@ int main(int argc, char *argv[])
int BufLen;
char szSignature[8192];
time_t t;
- DKIMContext ctxt;
+ struct DKIMContext *ctxt = DKIMContextNew();
DKIMSignOptions opts = {0};
opts.nHash = DKIM_HASH_SHA256; // default
@@ -276,12 +273,12 @@ int main(int argc, char *argv[])
exit(1);
}
- n = DKIMSignInit(&ctxt, &opts);
+ n = DKIMSignInit(ctxt, &opts);
while (1) {
BufLen = fread(Buffer, 1, sizeof(Buffer), MsgFP);
if (BufLen > 0) {
- DKIMSignProcess(&ctxt, Buffer, BufLen);
+ DKIMSignProcess(ctxt, Buffer, BufLen);
} else {
break;
}
@@ -292,11 +289,11 @@ int main(int argc, char *argv[])
/** Do the actual signing **/
- n = DKIMSignGetSig2(&ctxt, RSAPrivKey, ECCPrivKey, &pSig);
+ n = DKIMSignGetSig2(ctxt, RSAPrivKey, ECCPrivKey, &pSig);
strcpy(szSignature, pSig);
- DKIMSignFree(&ctxt);
+ DKIMSignFree(ctxt);
FILE *in = fopen(MsgFile, "rb");
FILE *out = fopen(OutFile, "wb+");
@@ -335,24 +332,24 @@ int main(int argc, char *argv[])
DKIMVerifyOptions vopts = {0};
vopts.pfnSelectorCallback = NULL; //SelectorCallback;
- n = DKIMVerifyInit(&ctxt, &vopts);
+ n = DKIMVerifyInit(ctxt, &vopts);
while (1) {
BufLen = fread(Buffer, 1, sizeof(Buffer), in);
if (BufLen > 0) {
- DKIMVerifyProcess(&ctxt, Buffer, BufLen);
+ DKIMVerifyProcess(ctxt, Buffer, BufLen);
} else {
break;
}
}
- n = DKIMVerifyResults(&ctxt);
+ n = DKIMVerifyResults(ctxt);
int nSigCount = 0;
DKIMVerifyDetails *pDetails;
char szPolicy[512];
- n = DKIMVerifyGetDetails(&ctxt, &nSigCount, &pDetails, szPolicy);
+ n = DKIMVerifyGetDetails(ctxt, &nSigCount, &pDetails, szPolicy);
for (int i = 0; i < nSigCount; i++) {
const char s[] = "pass";
@@ -371,7 +368,8 @@ int main(int argc, char *argv[])
printf(" Fail %s \n", error);
}
}
- DKIMVerifyFree(&ctxt);
+ DKIMVerifyFree(ctxt);
}
+ DKIMContextFree(ctxt);
return 0;
}