Back to index

opendkim  2.6.4
t-test86.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_test86_c_id[] = "@(#)$Id: t-test86.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 "t-testdata.h"
00033 #include "../dkim-strl.h"
00034 
00035 #define       MAXHEADER     4096
00036 
00037 #define       SIG1 "v=1; a=rsa-sha256; c=relaxed/simple; d=sendmail.com; s=test; t=1172620939; bh=yHBAX+3IwxTZIynBuB/5tlsBInJq9n8qz5fgAycHi80=; h=Received:Received:Received:From:To:Date:Subject:Message-ID; b=HLU6+LztURsYvmqEEHX74Vx9dR7tRtUDIlgRws7WCk5D8HqHx9Z2sSWkPqlkbh+meXydZWexg42oxOE94p6BLa5rDhJopSvlHBeZeCLp0U+JIkk7TlLWv82K+2Tbykx1b8bYuriFafkyPYhm+SFHs0zirGDJz71dYnTMc229znM="
00038 #define SIG2 "v=1; a=rsa-sha256; c=relaxed/simple; d=sendmail.com; s=test;\r\n\tt=1172620939; bh=yHBAX+3IwxTZIynBuB/5tlsBInJq9n8qz5fgAycHi80=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID; b=HLU\r\n\t6+LztURsYvmqEEHX74Vx9dR7tRtUDIlgRws7WCk5D8HqHx9Z2sSWkPqlkbh+meXydZW\r\n\texg42oxOE94p6BLa5rDhJopSvlHBeZeCLp0U+JIkk7TlLWv82K+2Tbykx1b8bYuriFa\r\n\tfkyPYhm+SFHs0zirGDJz71dYnTMc229znM="
00039 
00040 #define       DKIM_TEST_POLICY     "dkim=discardable"
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 #define       GOODQUERY     "_adsp._domainkey.sendmail.com"
00068        if (excheck)
00069               *qstatus = NOERROR;
00070        else if (strcmp(query, GOODQUERY) != 0)
00071               *qstatus = NXDOMAIN;
00072        else
00073               strlcpy(buf, DKIM_TEST_POLICY, buflen);
00074 
00075        return 0;
00076 }
00077 
00078 /*
00079 **  MAIN -- program mainline
00080 **
00081 **  Parameters:
00082 **     The usual.
00083 **
00084 **  Return value:
00085 **     Exit status.
00086 */
00087 
00088 int
00089 main(int argc, char **argv)
00090 {
00091 # ifdef TEST_KEEP_FILES
00092        u_int flags;
00093 # endif /* TEST_KEEP_FILES */
00094        int presult;
00095        dkim_policy_t pcode;
00096        DKIM_STAT status;
00097        dkim_query_t qtype = DKIM_QUERY_FILE;
00098        DKIM *dkim;
00099        DKIM_LIB *lib;
00100        unsigned char hdr[MAXHEADER + 1];
00101 
00102 #ifdef USE_GNUTLS
00103        (void) gnutls_global_init();
00104 #endif /* USE_GNUTLS */
00105 
00106        /* instantiate the library */
00107        lib = dkim_init(NULL, NULL);
00108        assert(lib != NULL);
00109 
00110        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00111        {
00112               printf("*** relaxed/simple rsa-sha256 verifying subdomain with i=/d= mismatch and \"discardable\" policy SKIPPED\n");
00113               dkim_close(lib);
00114               return 0;
00115        }
00116 
00117        printf("*** relaxed/simple rsa-sha256 verifying subdomain with i=/d= mismatch and \"discardable\" policy\n");
00118 
00119 #ifdef TEST_KEEP_FILES
00120        /* set flags */
00121        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00122        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00123                            sizeof flags);
00124 #endif /* TEST_KEEP_FILES */
00125 
00126        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00127                            &qtype, sizeof qtype);
00128        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00129                            KEYFILE, strlen(KEYFILE));
00130 
00131        dkim = dkim_verify(lib, JOBID, NULL, &status);
00132        assert(dkim != NULL);
00133 
00134        (void) dkim_set_policy_lookup(lib, policy_lookup);
00135 
00136        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00137        status = dkim_header(dkim, hdr, strlen(hdr));
00138        assert(status == DKIM_STAT_OK);
00139 
00140        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00141        assert(status == DKIM_STAT_OK);
00142 
00143        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00144        assert(status == DKIM_STAT_OK);
00145 
00146        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00147        assert(status == DKIM_STAT_OK);
00148 
00149        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00150        assert(status == DKIM_STAT_OK);
00151 
00152 #define       XHEADER05     "From: Murray S. Kucherawy <msk@eng.sendmail.com>"
00153        status = dkim_header(dkim, XHEADER05, strlen(XHEADER05));
00154        assert(status == DKIM_STAT_OK);
00155 
00156        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00157        assert(status == DKIM_STAT_OK);
00158 
00159        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00160        assert(status == DKIM_STAT_OK);
00161 
00162        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00163        assert(status == DKIM_STAT_OK);
00164 
00165        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00166        assert(status == DKIM_STAT_OK);
00167 
00168        status = dkim_eoh(dkim);
00169        assert(status == DKIM_STAT_OK);
00170 
00171        status = dkim_body(dkim, BODY00, strlen(BODY00));
00172        assert(status == DKIM_STAT_OK);
00173 
00174        status = dkim_body(dkim, BODY01, strlen(BODY01));
00175        assert(status == DKIM_STAT_OK);
00176 
00177        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00178        assert(status == DKIM_STAT_OK);
00179        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00180        assert(status == DKIM_STAT_OK);
00181        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00182        assert(status == DKIM_STAT_OK);
00183        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00184        assert(status == DKIM_STAT_OK);
00185        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00186        assert(status == DKIM_STAT_OK);
00187 
00188        status = dkim_body(dkim, BODY02, strlen(BODY02));
00189        assert(status == DKIM_STAT_OK);
00190 
00191        status = dkim_body(dkim, BODY03, strlen(BODY03));
00192        assert(status == DKIM_STAT_OK);
00193 
00194        status = dkim_body(dkim, BODY04, strlen(BODY04));
00195        assert(status == DKIM_STAT_OK);
00196 
00197        status = dkim_body(dkim, BODY03, strlen(BODY03));
00198        assert(status == DKIM_STAT_OK);
00199 
00200        status = dkim_body(dkim, BODY03, strlen(BODY03));
00201        assert(status == DKIM_STAT_OK);
00202 
00203        status = dkim_body(dkim, BODY05, strlen(BODY05));
00204        assert(status == DKIM_STAT_OK);
00205 
00206        status = dkim_body(dkim, BODY03, strlen(BODY03));
00207        assert(status == DKIM_STAT_OK);
00208 
00209        status = dkim_body(dkim, BODY03, strlen(BODY03));
00210        assert(status == DKIM_STAT_OK);
00211 
00212        status = dkim_eom(dkim, NULL);
00213        assert(status == DKIM_STAT_OK);
00214 
00215        status = dkim_policy(dkim, &pcode, NULL, NULL);
00216        assert(status == DKIM_STAT_OK);
00217 
00218        presult = dkim_getpresult(dkim);
00219        assert(presult == DKIM_PRESULT_NONE);
00220 
00221        status = dkim_free(dkim);
00222        assert(status == DKIM_STAT_OK);
00223 
00224        dkim_close(lib);
00225 
00226        return 0;
00227 }