s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
dkim.cpp
Go to the documentation of this file.
1/*****************************************************************************
2* Copyright 2005 Alt-N Technologies, Ltd.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* This code incorporates intellectual property owned by Yahoo! and licensed
11* pursuant to the Yahoo! DomainKeys Patent License Agreement.
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*
19* Changes done by ¢feh@fehcom.de obeying the above license
20*
21*****************************************************************************/
22#include <string.h>
23#include "dkim.h"
24#include "dkimsign.h"
25#include "dkimverify.h"
26
27#define DKIMID ('D' | 'K'<<8 | 'I'<<16 | 'M'<<24)
28/* taken from removed file "ressource.h" */
29#ifdef VERSION
30#define VERSION_STRING VERSION
31#else
32#define VERSION_STRING "1.4.0"
33#endif
34
35static void InitContext(DKIMContext* pContext,bool bSign,void* pObject)
36{
37 pContext->reserved1 = DKIMID;
38 pContext->reserved2 = bSign ? 1 : 0;
39 pContext->reserved3 = pObject;
40}
41
42static void* ValidateContext(DKIMContext* pContext,bool bSign)
43{
44 if (pContext->reserved1 != DKIMID)
45 return NULL;
46
47 if (pContext->reserved2 != (unsigned int)(bSign ? 1 : 0))
48 return NULL;
49
50 return pContext->reserved3;
51}
52
54{
55 int nRet = DKIM_OUT_OF_MEMORY;
56
57 CDKIMSign* pSign = new CDKIMSign;
58
59 if (pSign) {
60 nRet = pSign->Init(pOptions);
61 if (nRet != DKIM_SUCCESS)
62 delete pSign;
63 }
64
65 if (nRet == DKIM_SUCCESS) { InitContext(pSignContext,true,pSign); }
66 return nRet;
67}
68
69int DKIM_CALL DKIMSignProcess(DKIMContext* pSignContext,char* szBuffer,int nBufLength)
70{
71 CDKIMSign* pSign = (CDKIMSign*)ValidateContext(pSignContext,true);
72
73 if (pSign) { return pSign->Process(szBuffer,nBufLength,false); }
75}
76
77int DKIM_CALL DKIMSignGetSig2(DKIMContext* pSignContext,char* szRSAPrivKey,char* szECCPrivKey,char** pszSignature)
78{
79 CDKIMSign* pSign = (CDKIMSign*)ValidateContext(pSignContext,true);
80
81 if (pSign) { return pSign->GetSig2(szRSAPrivKey,szECCPrivKey,pszSignature); }
83}
84
86{
87 CDKIMSign* pSign = (CDKIMSign*)ValidateContext(pSignContext,true);
88
89 if (pSign) {
90 delete pSign;
91 pSignContext->reserved3 = NULL;
92 }
93}
94
96{
97 int nRet = DKIM_OUT_OF_MEMORY;
98
99 CDKIMVerify* pVerify = new CDKIMVerify;
100
101 if (pVerify) {
102 nRet = pVerify->Init(pOptions);
103 if (nRet != DKIM_SUCCESS)
104 delete pVerify;
105 }
106
107 if (nRet == DKIM_SUCCESS) {
108 InitContext(pVerifyContext,false,pVerify);
109 }
110
111 return nRet;
112}
113
114
115int DKIM_CALL DKIMVerifyProcess(DKIMContext* pVerifyContext,const char* const szBuffer,int nBufLength)
116{
117 CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false);
118
119 if (pVerify) {
120 return pVerify->Process(szBuffer,nBufLength,false);
121 }
122
124}
125
127{
128 CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false);
129
130 if (pVerify) {
131 return pVerify->GetResults();
132 }
134}
135
136int DKIM_CALL DKIMVerifyGetDetails(DKIMContext* pVerifyContext,int* nSigCount,DKIMVerifyDetails** pDetails,char* szPractices)
137{
138 szPractices[0] = '\0';
139
140 CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false);
141
142 if (pVerify) {
143 strcpy(szPractices,pVerify->GetPractices());
144 return pVerify->GetDetails(nSigCount,pDetails);
145 }
146
148}
149
150
152{
153 CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false);
154
155 if (pVerify) {
156 delete pVerify;
157 pVerifyContext->reserved3 = NULL;
158 }
159}
160
162{
163 return VERSION_STRING;
164}
165
166static const char* DKIMErrorStrings[-1-DKIM_MAX_ERROR] = {
167 "DKIM_FAIL",
168 "DKIM_BAD_SYNTAX",
169 "DKIM_SIGNATURE_BAD",
170 "DKIM_SIGNATURE_BAD_BUT_TESTING",
171 "DKIM_SIGNATURE_EXPIRED",
172 "DKIM_SELECTOR_INVALID",
173 "DKIM_SELECTOR_GRANULARITY_MISMATCH",
174 "DKIM_SELECTOR_KEY_REVOKED",
175 "DKIM_SELECTOR_DOMAIN_NAME_TOO_LONG",
176 "DKIM_SELECTOR_DNS_TEMP_FAILURE",
177 "DKIM_SELECTOR_DNS_PERM_FAILURE",
178 "DKIM_SELECTOR_PUBLIC_KEY_INVALID",
179 "DKIM_NO_SIGNATURES",
180 "DKIM_NO_VALID_SIGNATURES",
181 "DKIM_BODY_HASH_MISMATCH",
182 "DKIM_SELECTOR_ALGORITHM_MISMATCH",
183 "DKIM_STAT_INCOMPAT",
184 "DKIM_UNSIGNED_FROM",
185 "DKIM_OUT_OF_MEMORY",
186 "DKIM_INVALID_CONTEXT",
187 "DKIM_NO_SENDER",
188 "DKIM_BAD_PRIVATE_KEY",
189 "DKIM_BUFFER_TOO_SMALL",
190};
191
192const char* DKIM_CALL DKIMGetErrorString(int ErrorCode) {
193 if (ErrorCode >= 0 || ErrorCode <= DKIM_MAX_ERROR)
194 return "Unknown";
195 else
196 return DKIMErrorStrings[-1-ErrorCode];
197}
int Process(const char *szBuffer, int nBufLength, bool bEOF)
Definition: dkimbase.cpp:108
int GetSig2(char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature)
Definition: dkimsign.cpp:717
int Init(DKIMSignOptions *pOptions)
Definition: dkimsign.cpp:169
int Init(DKIMVerifyOptions *pOptions)
Definition: dkimverify.cpp:549
const char * GetPractices()
Definition: dkimverify.h:126
int GetResults(void)
Definition: dkimverify.cpp:572
int GetDetails(int *nSigCount, DKIMVerifyDetails **pDetails)
void DKIM_CALL DKIMSignFree(DKIMContext *pSignContext)
Definition: dkim.cpp:85
int DKIM_CALL DKIMSignInit(DKIMContext *pSignContext, DKIMSignOptions *pOptions)
Definition: dkim.cpp:53
int DKIM_CALL DKIMVerifyGetDetails(DKIMContext *pVerifyContext, int *nSigCount, DKIMVerifyDetails **pDetails, char *szPractices)
Definition: dkim.cpp:136
void DKIM_CALL DKIMVerifyFree(DKIMContext *pVerifyContext)
Definition: dkim.cpp:151
int DKIM_CALL DKIMSignGetSig2(DKIMContext *pSignContext, char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature)
Definition: dkim.cpp:77
#define DKIMID
Definition: dkim.cpp:27
int DKIM_CALL DKIMSignProcess(DKIMContext *pSignContext, char *szBuffer, int nBufLength)
Definition: dkim.cpp:69
#define VERSION_STRING
Definition: dkim.cpp:32
int DKIM_CALL DKIMVerifyProcess(DKIMContext *pVerifyContext, const char *const szBuffer, int nBufLength)
Definition: dkim.cpp:115
const char *DKIM_CALL DKIMGetErrorString(int ErrorCode)
Definition: dkim.cpp:192
int DKIM_CALL DKIMVerifyInit(DKIMContext *pVerifyContext, DKIMVerifyOptions *pOptions)
Definition: dkim.cpp:95
int DKIM_CALL DKIMVerifyResults(DKIMContext *pVerifyContext)
Definition: dkim.cpp:126
const char *DKIM_CALL DKIMVersion()
Definition: dkim.cpp:161
#define DKIM_CALL
Definition: dkim.h:22
#define DKIM_INVALID_CONTEXT
Definition: dkim.h:67
#define DKIM_OUT_OF_MEMORY
Definition: dkim.h:66
#define DKIM_MAX_ERROR
Definition: dkim.h:71
#define DKIM_SUCCESS
Definition: dkim.h:47
unsigned int reserved2
Definition: dkim.h:92
void * reserved3
Definition: dkim.h:93
unsigned int reserved1
Definition: dkim.h:91