1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*****************************************************************************
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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
* limitations under the License.
*
* Changes done by ¢feh@fehcom.de obeying the above license
*
* Comment: Awful mixture of C and C++ making use of the worst parts of it.
* Style: Partial Hungarian notation (see Torvalds comments)
* C++: Obsolete classes, allocators, virtual constructors w/o destructors
* C: Stdio interface routines
* OpenSSL: Brain demaged EVP_Digest calls with memory leaks.
* Network: Sigh, exchanged internal DNS routines by fehQlibs resolver
*
*****************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "dkim.h"
extern "C" {
#include "dns.h"
}
// change these to your selector name, domain name, etc
#define MYRSASELECTOR "default"
#define MYECCSELECTOR "eddy"
#define MYDOMAIN "" //"bardenhagen.com"
#define MYIDENTITY "" //"dkimtest@bardenhagen.com"
#define strnicmp strncasecmp
#define FDLOG stderr /* writing to another FD requires a method */
int DKIM_CALL SignThisHeader(const char *szHeader)
{
if (strnicmp(szHeader, "X-", 2) == 0) {
return 0;
}
return 1;
}
int DKIM_CALL SelectorCallback(const char *szFQDN, char *szBuffer, int nBufLen)
{
return 0;
}
void usage()
{
char version[] = "1.4.0";
fprintf(FDLOG, "qmail-dkim %s \n", version);
fprintf(
FDLOG,
"Usage: qmail-dkim [-h|-v|-s] [tags] <msgfile> [<RSAkeyfile> <outfile> <Ed25519keyfile>]\n\n");
fprintf(FDLOG, "Options:\n\t-h show this help\n");
fprintf(FDLOG, "\t-s sign the message \n");
fprintf(FDLOG, "\t-v verify the message\n");
fprintf(FDLOG, "\t-V verify the message and write result to output file (Pass/Fail)\n\n");
fprintf(FDLOG, "These tags are available:\n");
fprintf(
FDLOG,
"\t-c<canonicalization> - r=relaxed [DEFAULT], s=simple, t=relaxed/simple, u=simple/relaxed\n");
fprintf(
FDLOG,
"\t-d<sdid> - Signing Domain Identifier (if not provided it will be determined from "
"the sender/from header)\n");
fprintf(
FDLOG,
"\t-i<auid> - Agent User Identifier, usually the sender's email address "
"(optional)\n");
fprintf(FDLOG, "\t-l - include body length tag (optional)\n");
fprintf(FDLOG, "\t-q - include query method tag\n");
fprintf(FDLOG, "\t-t - include a timestamp tag (optional)\n");
fprintf(
FDLOG,
"\t-x<expire_time> - the expire time in seconds since epoch (optional, DEFAULT = current "
"time + 604800)\n");
fprintf(FDLOG, "\t-y<selector> - set RSA selector (DEFAULT: default)\n");
fprintf(FDLOG, "\t-Y<selector> - set Ed25519 selector (DEFAULT: default)\n");
fprintf(
FDLOG,
"\t-z<hash> - set signature algorithm type (1=rsa-sha1, 2=rsa-sha256, 3=both, "
"4=ed25519, 5=hybrid)\n");
}
int main(int argc, char *argv[])
{
int n;
const char *RSAKeyFile = "rsa.pem";
const char *ECCKeyFile = "ed25519.pem";
const char *MsgFile = "test.msg";
const char *OutFile = "signed.msg";
int nKeyLen;
char RSAPrivKey[4196]; // storge for private key FILE including header and DER envelope
char ECCPrivKey[128];
char Buffer[1000];
int BufLen;
char szSignature[8192];
time_t t;
DKIMContext ctxt;
DKIMSignOptions opts = {0};
opts.nHash = DKIM_HASH_SHA256; // default
time(&t);
opts.nCanon = DKIM_SIGN_RELAXED;
opts.nIncludeBodyLengthTag = 0;
opts.nIncludeQueryMethod = 0;
opts.nIncludeTimeStamp = 0;
opts.expireTime = t + 604800; // expires in 1 week
strcpy(opts.szSelector, MYRSASELECTOR);
strcpy(opts.szSelectorE, MYECCSELECTOR);
strcpy(opts.szDomain, MYDOMAIN);
strcpy(opts.szIdentity, MYIDENTITY);
opts.pfnHeaderCallback = SignThisHeader;
strcpy(opts.szRequiredHeaders, "NonExistant");
opts.nIncludeCopiedHeaders = 0;
int nArgParseState = 0;
bool bSign = true;
bool bRes = false;
if (argc < 2) {
usage();
exit(1);
}
for (n = 1; n < argc; n++) {
if (argv[n][0] == '-' && strlen(argv[n]) > 1) {
switch (argv[n][1]) {
case 'c': // canonicalization
if (argv[n][2] == 'r') {
opts.nCanon = DKIM_SIGN_RELAXED;
} else if (argv[n][2] == 's') {
opts.nCanon = DKIM_SIGN_SIMPLE;
} else if (argv[n][2] == 't') {
opts.nCanon = DKIM_SIGN_RELAXED_SIMPLE;
} else if (argv[n][2] == 'u') {
opts.nCanon = DKIM_SIGN_SIMPLE_RELAXED;
}
break;
case 'd': strncpy(opts.szDomain, (const char *)(argv[n] + 2), sizeof(opts.szDomain) - 1); break;
case 'l': // body length tag
opts.nIncludeBodyLengthTag = 1;
break;
case 'h': usage(); return 0;
case 'i': // identity
if (argv[n][2] == '-') {
opts.szIdentity[0] = '\0';
} else {
strncpy(opts.szIdentity, argv[n] + 2, sizeof(opts.szIdentity) - 1);
}
break;
case 'q': // query method tag
opts.nIncludeQueryMethod = 1;
break;
case 's': // sign with and use potentially Ed25519 private key
bSign = true;
break;
case 't': // timestamp tag
opts.nIncludeTimeStamp = 1;
break;
case 'v': // verify
bSign = false;
break;
case 'V': // verify and write result to OutFile
bSign = false;
bRes = true;
break;
case 'x': // expire time
if (argv[n][2] == '-') {
opts.expireTime = 0;
} else {
opts.expireTime = t + atoi(argv[n] + 2);
}
break;
case 'y': strncpy(opts.szSelector, argv[n] + 2, sizeof(opts.szSelector) - 1); break;
case 'Y': strncpy(opts.szSelectorE, argv[n] + 2, sizeof(opts.szSelectorE) - 1); break;
case 'z': // sign w/ sha1, sha256, both, ed25519, hybrid
opts.nHash = atoi(&argv[n][2]);
}
} else {
switch (nArgParseState) {
case 0: MsgFile = argv[n]; break;
case 1: RSAKeyFile = argv[n]; break;
case 2: OutFile = argv[n]; break;
case 3: ECCKeyFile = argv[n]; break;
}
nArgParseState++;
}
}
/** Go for DKIM signing ... **/
if (bSign) {
if (opts.nHash != 4) {
FILE *RSAPrivKeyFP = fopen(RSAKeyFile, "r");
if (RSAPrivKeyFP == NULL) {
#ifdef SHOWLOG
fprintf(FDLOG, " qmail-dkim: can't open private key file (%s) \n", RSAKeyFile);
#endif
exit(1);
}
nKeyLen = fread(
RSAPrivKey,
1,
sizeof(RSAPrivKey),
RSAPrivKeyFP); // we read sizeof(RSAPrivKey) members with size of 1 byte each; sigh
#ifdef SHOWLOG
fprintf(FDLOG, " qmail-dkim: private key file (%s) - length %i \n", RSAKeyFile, nKeyLen);
#endif
if (nKeyLen >= sizeof(RSAPrivKey)) { /* (TC9) on return, we get the number of members read! */
#ifdef SHOWLOG
fprintf(
FDLOG,
" qmail-dkim: private key buffer isn't big enough for private key length %i \n",
nKeyLen);
#endif
exit(1);
}
RSAPrivKey[nKeyLen] = '\0';
fclose(RSAPrivKeyFP);
}
/** Ed25519 signing **/
if (opts.nHash == 4 || opts.nHash == 5) {
FILE *ECCPrivKeyFP = fopen(ECCKeyFile, "r");
if (ECCPrivKeyFP == NULL) {
#ifdef SHOWLOG
fprintf(FDLOG, " qmail-dkim: can't open Ed25519 private key file (%s) \n", ECCKeyFile);
#endif
exit(1);
}
nKeyLen = fread(ECCPrivKey, 1, sizeof(ECCPrivKey), ECCPrivKeyFP);
#ifdef SHOWLOG
fprintf(FDLOG, " qmail-dkim: Ed25519 private key file (%s) - length %i \n", ECCKeyFile, nKeyLen);
#endif
if (nKeyLen >= sizeof(ECCPrivKey)) {
#ifdef SHOWLOG
fprintf(
FDLOG,
" qmail-dkim: ECC private key buffer isn't big enough for ECC private key length %i \n",
nKeyLen);
#endif
exit(1);
}
ECCPrivKey[nKeyLen] = '\0';
fclose(ECCPrivKeyFP);
}
/** Input message for signing **/
FILE *MsgFP = fopen(MsgFile, "rb");
if (MsgFP == NULL) {
#ifdef SHOWLOG
fprintf(FDLOG, " qmail-dkim: can't open msg file (%s) \n", MsgFile);
#endif
exit(1);
}
n = DKIMSignInit(&ctxt, &opts);
while (1) {
BufLen = fread(Buffer, 1, sizeof(Buffer), MsgFP);
if (BufLen > 0) {
DKIMSignProcess(&ctxt, Buffer, BufLen);
} else {
break;
}
}
fclose(MsgFP);
char *pSig = NULL;
/** Do the actual signing **/
n = DKIMSignGetSig2(&ctxt, RSAPrivKey, ECCPrivKey, &pSig);
strcpy(szSignature, pSig);
DKIMSignFree(&ctxt);
FILE *in = fopen(MsgFile, "rb");
FILE *out = fopen(OutFile, "wb+");
#ifdef SHOWLOG
fprintf(FDLOG, " outfile written %s \n", OutFile);
#endif
fwrite(szSignature, 1, strlen(szSignature), out);
fwrite("\r\n", 1, 2, out);
while (1) {
BufLen = fread(Buffer, 1, sizeof(Buffer), in);
if (BufLen > 0) {
fwrite(Buffer, 1, BufLen, out);
} else {
break;
}
}
fclose(in);
}
/** Now go for verification **/
else
{
FILE *in = fopen(MsgFile, "rb");
if (in == NULL) {
//#ifdef SHOWLOG
fprintf(FDLOG, " qmail-dkim: can't open input file\n");
//#endif
return 0; // bad option -- no CTX set up yet
}
DKIMVerifyOptions vopts = {0};
vopts.pfnSelectorCallback = NULL; //SelectorCallback;
n = DKIMVerifyInit(&ctxt, &vopts);
while (1) {
BufLen = fread(Buffer, 1, sizeof(Buffer), in);
if (BufLen > 0) {
DKIMVerifyProcess(&ctxt, Buffer, BufLen);
} else {
break;
}
}
n = DKIMVerifyResults(&ctxt);
int nSigCount = 0;
DKIMVerifyDetails *pDetails;
char szPolicy[512];
n = DKIMVerifyGetDetails(&ctxt, &nSigCount, &pDetails, szPolicy);
for (int i = 0; i < nSigCount; i++) {
const char s[] = "pass";
const char f[] = "fail";
const char *error = DKIM_ErrorResult(pDetails[i].nResult);
if (!bRes) fprintf(FDLOG, " Signature #%d: ", i + 1);
if (pDetails[i].nResult >= 0) {
if (bRes) {
_DKIM_ReportResult(OutFile, s, 0);
} else
printf(" Pass\n");
} else { // fail
if (bRes) {
_DKIM_ReportResult(OutFile, f, error);
} else
printf(" Fail %s \n", error);
}
}
DKIMVerifyFree(&ctxt);
}
return 0;
}
|