Back to index

opendkim  2.6.6
t-test74.c
Go to the documentation of this file.
00001 /*
00002 **  Copyright (c) 2005-2008 Sendmail, Inc. and its suppliers.
00003 **    All rights reserved.
00004 **
00005 **  Copyright (c) 2009, 2011, The OpenDKIM Project.  All rights reserved.
00006 */
00007 
00008 #ifndef lint
00009 static char t_test74_c_id[] = "@(#)$Id: t-test74.c,v 1.2 2009/12/08 19:14:27 cm-msk Exp $";
00010 #endif /* !lint */
00011 
00012 #include "build-config.h"
00013 
00014 /* system includes */
00015 #include <sys/types.h>
00016 #include <assert.h>
00017 #include <string.h>
00018 #include <stdio.h>
00019 #include <arpa/nameser.h>
00020 
00021 #ifdef USE_GNUTLS
00022 # include <gnutls/gnutls.h>
00023 #endif /* USE_GNUTLS */
00024 
00025 /* libopendkim includes */
00026 #include "../dkim.h"
00027 #include "t-testdata.h"
00028 
00029 #define       MAXHEADER     4096
00030 
00031 #define SIG1 "v=1; a=rsa-sha1; c=simple/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=V+wiYd1cE70A40eDNUtYwU\r\n\tvqi727NN/vVo/OdC7jG4zvztIXCRjPKDXEfiZdW+6PZ08M4zA3GmLZI2p+IJTza+VDQ\r\n\tbohnvIUSRR6q9+Nddqz1qTCL0gNM5d64xKwrLUesk/3wb/Cvua6Atr+VBMFVCwQmSDf\r\n\trs4brLkqMyFOkCE="
00032 #define SIG2 "v=1; a=rsa-sha256; c=simple/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=yHBAX+3IwxTZIynBuB/5tlsBInJq9n8qz5fgAycHi80=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID; b=Y3y\r\n\tVeA3WZdCZl1sGuOZNC3BBRhtGCOExkZdw5xQoGPvSX/q6AC1SAJvOUWOri95AZAUGs0\r\n\t/bIDzzt23ei9jc+rptlavrl/5ijMrl6ShmvkACk6It62KPkJcDpoGfi5AZkrfX1Ou/z\r\n\tqGg5xJX86Kqd7FgNolMg7PbfyWliK2Yb84="
00033 #define SIG3 "v=1; a=rsa-sha256; c=simple/simple; d=example.com; s=brisbane;\r\n\tt=1172620939; bh=yHBAX+3IwxTZIynBuB/5tlsBInJq9n8qz5fgAycHi80=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID; b=Y3y\r\n\tVeA3WZdCZl1sGuOZNC3BBRhtGCOExkZdw5xQoGPvSX/q6AC1SAJvOUWOri95AZAUGs0\r\n\t/bIDzzt23ei9jc+rptlavrl/5ijMrl6ShmvkACk6It62KPkJcDpoGfi5AZkrfX1Ou/z\r\n\tqGg5xJX86Kqd7FgNolMg7PbfyWliK2Yb84="
00034 
00035 int kl;
00036 
00037 /*
00038 **  KEY_LOOKUP -- key lookup
00039 **
00040 **  Parameters:
00041 **     dkim -- DKIM handle
00042 **     sig -- DKIM_SIGINFO handle
00043 **     buf -- where to write the result
00044 **     buflen -- how much space is available at "buf"
00045 **
00046 **  Return value:
00047 **     A DKIM_STAT_* constant.
00048 */
00049 
00050 DKIM_STAT
00051 key_lookup(DKIM *dkim, DKIM_SIGINFO *sig, unsigned char *buf, size_t buflen)
00052 {
00053        const char *selector;
00054        const char *domain;
00055 
00056        assert(dkim != NULL);
00057        assert(sig != NULL);
00058        assert(buf != NULL);
00059 
00060        selector = dkim_sig_getselector(sig);
00061        assert(selector != NULL);
00062        assert(strcmp(selector, SELECTOR) == 0 ||
00063               strcmp(selector, SELECTOR2) == 0);
00064 
00065        domain = dkim_sig_getdomain(sig);
00066        assert(domain != NULL);
00067        assert(strcmp(domain, DOMAIN) == 0);
00068 
00069        memset(buf, '\0', buflen);
00070 
00071        if (strcmp(selector, SELECTOR) == 0)
00072               strncpy(buf, PUBLICKEY, buflen);
00073        else
00074               strncpy(buf, PUBLICKEY2, buflen);
00075 
00076        kl++;
00077 
00078        return DKIM_STAT_OK;
00079 }
00080 
00081 /*
00082 **  MAIN -- program mainline
00083 **
00084 **  Parameters:
00085 **     The usual.
00086 **
00087 **  Return value:
00088 **     Exit status.
00089 */
00090 
00091 int
00092 main(int argc, char **argv)
00093 {
00094 #ifdef TEST_KEEP_FILES
00095        u_int flags;
00096 #endif /* TEST_KEEP_FILES */
00097        DKIM_STAT status;
00098        DKIM *dkim;
00099        DKIM_LIB *lib;
00100        unsigned char hdr[MAXHEADER + 1];
00101 
00102        kl = 0;
00103 
00104 #ifdef USE_GNUTLS
00105        (void) gnutls_global_init();
00106 #endif /* USE_GNUTLS */
00107 
00108        /* instantiate the library */
00109        lib = dkim_init(NULL, NULL);
00110        assert(lib != NULL);
00111 
00112        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00113        {
00114               printf("*** simple/simple rsa-sha256 verifying with key callback and key reuse SKIPPED\n");
00115               dkim_close(lib);
00116               return 0;
00117        }
00118 
00119        printf("*** simple/simple rsa-sha256 verifying with key callback and key reuse\n");
00120 
00121 #ifdef TEST_KEEP_FILES
00122        /* set flags */
00123        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00124        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00125                            sizeof flags);
00126 #endif /* TEST_KEEP_FILES */
00127 
00128        dkim = dkim_verify(lib, JOBID, NULL, &status);
00129        assert(dkim != NULL);
00130 
00131        (void) dkim_set_key_lookup(lib, key_lookup);
00132 
00133        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG1);
00134        status = dkim_header(dkim, hdr, strlen(hdr));
00135        assert(status == DKIM_STAT_OK);
00136 
00137        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00138        status = dkim_header(dkim, hdr, strlen(hdr));
00139        assert(status == DKIM_STAT_OK);
00140 
00141        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG3);
00142        status = dkim_header(dkim, hdr, strlen(hdr));
00143        assert(status == DKIM_STAT_OK);
00144 
00145        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00146        assert(status == DKIM_STAT_OK);
00147 
00148        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00149        assert(status == DKIM_STAT_OK);
00150 
00151        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00152        assert(status == DKIM_STAT_OK);
00153 
00154        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00155        assert(status == DKIM_STAT_OK);
00156 
00157        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00158        assert(status == DKIM_STAT_OK);
00159 
00160        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00161        assert(status == DKIM_STAT_OK);
00162 
00163        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00164        assert(status == DKIM_STAT_OK);
00165 
00166        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00167        assert(status == DKIM_STAT_OK);
00168 
00169        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00170        assert(status == DKIM_STAT_OK);
00171 
00172        status = dkim_eoh(dkim);
00173        assert(status == DKIM_STAT_OK);
00174 
00175        status = dkim_body(dkim, BODY00, strlen(BODY00));
00176        assert(status == DKIM_STAT_OK);
00177 
00178        status = dkim_body(dkim, BODY01, strlen(BODY01));
00179        assert(status == DKIM_STAT_OK);
00180 
00181        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00182        assert(status == DKIM_STAT_OK);
00183        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00184        assert(status == DKIM_STAT_OK);
00185        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00186        assert(status == DKIM_STAT_OK);
00187        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00188        assert(status == DKIM_STAT_OK);
00189        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00190        assert(status == DKIM_STAT_OK);
00191 
00192        status = dkim_body(dkim, BODY02, strlen(BODY02));
00193        assert(status == DKIM_STAT_OK);
00194 
00195        status = dkim_body(dkim, BODY03, strlen(BODY03));
00196        assert(status == DKIM_STAT_OK);
00197 
00198        status = dkim_body(dkim, BODY04, strlen(BODY04));
00199        assert(status == DKIM_STAT_OK);
00200 
00201        status = dkim_body(dkim, BODY03, strlen(BODY03));
00202        assert(status == DKIM_STAT_OK);
00203 
00204        status = dkim_body(dkim, BODY03, strlen(BODY03));
00205        assert(status == DKIM_STAT_OK);
00206 
00207        status = dkim_body(dkim, BODY05, strlen(BODY05));
00208        assert(status == DKIM_STAT_OK);
00209 
00210        status = dkim_body(dkim, BODY03, strlen(BODY03));
00211        assert(status == DKIM_STAT_OK);
00212 
00213        status = dkim_body(dkim, BODY03, strlen(BODY03));
00214        assert(status == DKIM_STAT_OK);
00215 
00216        status = dkim_eom(dkim, NULL);
00217        assert(status == DKIM_STAT_OK);
00218 
00219        status = dkim_free(dkim);
00220        assert(status == DKIM_STAT_OK);
00221 
00222        assert(kl == 2);
00223 
00224        dkim_close(lib);
00225 
00226        return 0;
00227 }