Back to index

opendkim  2.6.2
t-test42.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_test42_c_id[] = "@(#)$Id: t-test42.c,v 1.2.38.1 2010/10/28 04:25:38 cm-msk Exp $";
00010 #endif /* !lint */
00011 
00012 #include "build-config.h"
00013 
00014 #include "build-config.h"
00015 
00016 /* system includes */
00017 #include <sys/types.h>
00018 #include <arpa/nameser.h>
00019 #ifdef HAVE_STDBOOL_H
00020 # include <stdbool.h>
00021 #endif /* HAVE_STDBOOL_H */
00022 #include <assert.h>
00023 #include <string.h>
00024 #include <stdio.h>
00025 
00026 #ifdef USE_GNUTLS
00027 # include <gnutls/gnutls.h>
00028 #endif /* USE_GNUTLS */
00029 
00030 /* libopendkim includes */
00031 #include "../dkim.h"
00032 #include "../dkim-strl.h"
00033 #include "t-testdata.h"
00034 
00035 #define       MAXHEADER     4096
00036 
00037 #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="
00038 
00039 int pl;
00040 int kl;
00041 
00042 /*
00043 **  POLICY_LOOKUP -- policy lookup
00044 **
00045 **  Parameters:
00046 **     dkim -- DKIM handle
00047 **     query -- string to query
00048 **     excheck -- existence check?
00049 **     buf -- where to write the result
00050 **     buflen -- how much space is available at "buf"
00051 **     qstatus -- query status (returned)
00052 **
00053 **  Return value:
00054 **     0 -- operation completed
00055 **     -1 -- error
00056 */
00057 
00058 int
00059 policy_lookup(DKIM *dkim, unsigned char *query, _Bool excheck,
00060               unsigned char *buf, size_t buflen, int *qstatus)
00061 {
00062        assert(dkim != NULL);
00063        assert(query != NULL);
00064        assert(buf != NULL);
00065        assert(qstatus != NULL);
00066 
00067        pl = 1;
00068 
00069        strlcpy(buf, DKIM_POLICY_DEFAULTTXT, buflen);
00070        *qstatus = NOERROR;
00071 
00072        return 0;
00073 }
00074 
00075 /*
00076 **  KEY_LOOKUP -- key lookup
00077 **
00078 **  Parameters:
00079 **     dkim -- DKIM handle
00080 **     sig -- DKIM_SIGINFO handle
00081 **     buf -- where to write the result
00082 **     buflen -- how much space is available at "buf"
00083 **
00084 **  Return value:
00085 **     A DKIM_STAT_* constant.
00086 */
00087 
00088 DKIM_STAT
00089 key_lookup(DKIM *dkim, DKIM_SIGINFO *sig, unsigned char *buf, size_t buflen)
00090 {
00091        const char *selector;
00092        const char *domain;
00093 
00094        assert(dkim != NULL);
00095        assert(sig != NULL);
00096        assert(buf != NULL);
00097 
00098        selector = dkim_sig_getselector(sig);
00099        assert(selector != NULL);
00100        assert(strcmp(selector, SELECTOR) == 0);
00101 
00102        domain = dkim_sig_getdomain(sig);
00103        assert(domain != NULL);
00104        assert(strcmp(domain, DOMAIN) == 0);
00105 
00106        memset(buf, '\0', buflen);
00107        strncpy(buf, PUBLICKEY, buflen);
00108 
00109        kl = 1;
00110 
00111        return DKIM_STAT_OK;
00112 }
00113 
00114 /*
00115 **  MAIN -- program mainline
00116 **
00117 **  Parameters:
00118 **     The usual.
00119 **
00120 **  Return value:
00121 **     Exit status.
00122 */
00123 
00124 int
00125 main(int argc, char **argv)
00126 {
00127 #ifdef TEST_KEEP_FILES
00128        u_int flags;
00129 #endif /* TEST_KEEP_FILES */
00130        int testpolicy;
00131        int suspicious;
00132        DKIM_STAT status;
00133        dkim_policy_t pcode;
00134        DKIM *dkim;
00135        DKIM_LIB *lib;
00136        unsigned char hdr[MAXHEADER + 1];
00137 
00138        pl = 0;
00139        kl = 0;
00140 
00141 #ifdef USE_GNUTLS
00142        (void) gnutls_global_init();
00143 #endif /* USE_GNUTLS */
00144 
00145        /* instantiate the library */
00146        lib = dkim_init(NULL, NULL);
00147        assert(lib != NULL);
00148 
00149        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00150        {
00151               printf("*** simple/simple rsa-sha256 verifying with key/policy callbacks SKIPPED\n");
00152               dkim_close(lib);
00153               return 0;
00154        }
00155 
00156        printf("*** simple/simple rsa-sha256 verifying with key/policy callbacks\n");
00157 
00158 #ifdef TEST_KEEP_FILES
00159        /* set flags */
00160        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00161        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00162                            sizeof flags);
00163 #endif /* TEST_KEEP_FILES */
00164 
00165        dkim = dkim_verify(lib, JOBID, NULL, &status);
00166        assert(dkim != NULL);
00167 
00168        (void) dkim_set_key_lookup(lib, key_lookup);
00169        (void) dkim_set_policy_lookup(lib, policy_lookup);
00170 
00171        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00172        status = dkim_header(dkim, hdr, strlen(hdr));
00173        assert(status == DKIM_STAT_OK);
00174 
00175        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00176        assert(status == DKIM_STAT_OK);
00177 
00178        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00179        assert(status == DKIM_STAT_OK);
00180 
00181        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00182        assert(status == DKIM_STAT_OK);
00183 
00184        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00185        assert(status == DKIM_STAT_OK);
00186 
00187        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00188        assert(status == DKIM_STAT_OK);
00189 
00190        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00191        assert(status == DKIM_STAT_OK);
00192 
00193        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00194        assert(status == DKIM_STAT_OK);
00195 
00196        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00197        assert(status == DKIM_STAT_OK);
00198 
00199        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00200        assert(status == DKIM_STAT_OK);
00201 
00202        status = dkim_eoh(dkim);
00203        assert(status == DKIM_STAT_OK);
00204 
00205        status = dkim_body(dkim, BODY00, strlen(BODY00));
00206        assert(status == DKIM_STAT_OK);
00207 
00208        status = dkim_body(dkim, BODY01, strlen(BODY01));
00209        assert(status == DKIM_STAT_OK);
00210 
00211        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00212        assert(status == DKIM_STAT_OK);
00213        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00214        assert(status == DKIM_STAT_OK);
00215        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00216        assert(status == DKIM_STAT_OK);
00217        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00218        assert(status == DKIM_STAT_OK);
00219        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00220        assert(status == DKIM_STAT_OK);
00221 
00222        status = dkim_body(dkim, BODY02, strlen(BODY02));
00223        assert(status == DKIM_STAT_OK);
00224 
00225        status = dkim_body(dkim, BODY03, strlen(BODY03));
00226        assert(status == DKIM_STAT_OK);
00227 
00228        status = dkim_body(dkim, BODY04, strlen(BODY04));
00229        assert(status == DKIM_STAT_OK);
00230 
00231        status = dkim_body(dkim, BODY03, strlen(BODY03));
00232        assert(status == DKIM_STAT_OK);
00233 
00234        status = dkim_body(dkim, BODY03, strlen(BODY03));
00235        assert(status == DKIM_STAT_OK);
00236 
00237        status = dkim_body(dkim, BODY05, strlen(BODY05));
00238        assert(status == DKIM_STAT_OK);
00239 
00240        status = dkim_body(dkim, BODY03, strlen(BODY03));
00241        assert(status == DKIM_STAT_OK);
00242 
00243        status = dkim_body(dkim, BODY03, strlen(BODY03));
00244        assert(status == DKIM_STAT_OK);
00245 
00246        status = dkim_eom(dkim, NULL);
00247        assert(status == DKIM_STAT_OK);
00248 
00249        testpolicy = 0;
00250        suspicious = 0;
00251        status = dkim_policy(dkim, &pcode, NULL, NULL);
00252        assert(status == DKIM_STAT_OK);
00253        assert(pcode == DKIM_POLICY_UNKNOWN);
00254 
00255        status = dkim_free(dkim);
00256        assert(status == DKIM_STAT_OK);
00257 
00258        assert(pl == 1);
00259        assert(kl == 1);
00260 
00261        dkim_close(lib);
00262 
00263        return 0;
00264 }