Back to index

opendkim  2.6.4
t-test129.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_test129_c_id[] = "@(#)$Id: t-test129.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 
00020 #ifdef USE_GNUTLS
00021 # include <gnutls/gnutls.h>
00022 #endif /* USE_GNUTLS */
00023 
00024 /* libopendkim includes */
00025 #include "../dkim.h"
00026 #include "t-testdata.h"
00027 
00028 #define       MAXHEADER     4096
00029 
00030 #define       AUTHRESHDR    "Authentication-Results"
00031 #define       AUTHRESVAL    "example.net; dkim=pass header.i=@example.com"
00032 
00033 #define JOBID1              "testing1"
00034 #define JOBID2              "testing2"
00035 #define JOBID3              "testing3"
00036 
00037 #ifndef FALSE
00038 # define FALSE              0
00039 #endif /* ! FALSE */
00040 #ifndef TRUE
00041 # define TRUE        1
00042 #endif /* ! TRUE */
00043 
00044 #define SIG2 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID;\r\n\tb=Q4G/ki/5soDXGxs43JfV+qEKDr5X3GgTDNeZqWL3zLLC5DXWWzmnKRcU8NH4Wsfkh\r\n\t o5tMo4NRmqnB2eZtozsyXdHo2ekUPLxuAQJomM4JHaPTfsraHwkibQIkPpW5hf/Rc2\r\n\t 0QgP48iQBjxqcOSn/Vwk5QDup4Qj1vgOxBqTqwdg="
00045 
00046 /* list of headers to sign */
00047 const u_char *signhdrs[] =
00048 {
00049        "from",
00050        "sender",
00051        "reply-to",
00052        "subject",
00053        "date",
00054        "message-id",
00055        "to",
00056        "cc",
00057        "mime-version",
00058        "content-type",
00059        "content-transfer-encoding",
00060        "content-id",
00061        "content-description",
00062        "resent-date",
00063        "resent-from",
00064        "resent-sender",
00065        "resent-to",
00066        "resent-cc",
00067        "resent-message-id",
00068        "in-reply-to",
00069        "references",
00070        "list-id",
00071        "list-help",
00072        "list-unsubscribe",
00073        "list-subscribe",
00074        "list-post",
00075        "list-owner",
00076        "list-archive",
00077        "authentication-results",
00078        NULL
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        uint64_t fixed_time;
00099        dkim_sigkey_t key;
00100        DKIM *dkim;
00101        DKIM *resign;
00102        DKIM_LIB *lib;
00103        dkim_query_t qtype = DKIM_QUERY_FILE;
00104        unsigned char hdr[MAXHEADER + 1];
00105        unsigned char hdr2[MAXHEADER + 1];
00106 
00107        key = KEY;
00108 
00109 #ifdef USE_GNUTLS
00110        (void) gnutls_global_init();
00111 #endif /* USE_GNUTLS */
00112 
00113        /* instantiate the library */
00114        lib = dkim_init(NULL, NULL);
00115        assert(lib != NULL);
00116 
00117        if (!dkim_libfeature(lib, DKIM_FEATURE_RESIGN))
00118        {
00119               printf("*** relaxed/simple rsa-sha1 re-signing without header binding SKIPPED\n");
00120               dkim_close(lib);
00121               return 0;
00122        }
00123 
00124        printf("*** relaxed/simple rsa-sha1 re-signing without header binding\n");
00125 
00126 #ifdef TEST_KEEP_FILES
00127        /* set flags */
00128        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00129        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00130                            sizeof flags);
00131 #endif /* TEST_KEEP_FILES */
00132 
00133        /* test mode */
00134        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00135                            &qtype, sizeof qtype);
00136        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00137                            KEYFILE, strlen(KEYFILE));
00138 
00139        /* fix signing time */
00140        fixed_time = 1172620939;
00141        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FIXEDTIME,
00142                            &fixed_time, sizeof fixed_time);
00143 
00144        /* restrict signed headers */
00145        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_SIGNHDRS,
00146                            signhdrs, sizeof(u_char **));
00147 
00148        dkim = dkim_verify(lib, JOBID1, NULL, &status);
00149        assert(dkim != NULL);
00150 
00151        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00152        status = dkim_header(dkim, hdr, strlen(hdr));
00153        assert(status == DKIM_STAT_OK);
00154 
00155        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00156        assert(status == DKIM_STAT_OK);
00157 
00158        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00159        assert(status == DKIM_STAT_OK);
00160 
00161        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00162        assert(status == DKIM_STAT_OK);
00163 
00164        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00165        assert(status == DKIM_STAT_OK);
00166 
00167        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00168        assert(status == DKIM_STAT_OK);
00169 
00170        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00171        assert(status == DKIM_STAT_OK);
00172 
00173        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00174        assert(status == DKIM_STAT_OK);
00175 
00176        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00177        assert(status == DKIM_STAT_OK);
00178 
00179        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00180        assert(status == DKIM_STAT_OK);
00181 
00182        resign = dkim_sign(lib, JOBID2, NULL, key, SELECTOR, DOMAIN,
00183                           DKIM_CANON_RELAXED, DKIM_CANON_SIMPLE,
00184                           DKIM_SIGN_RSASHA1, -1L, &status);
00185        assert(resign != NULL);
00186 
00187        status = dkim_resign(dkim, resign, TRUE);
00188        assert(status == DKIM_STAT_INVALID);
00189 
00190        status = dkim_resign(resign, dkim, FALSE);
00191        assert(status == DKIM_STAT_OK);
00192 
00193        status = dkim_eoh(dkim);
00194        assert(status == DKIM_STAT_OK);
00195 
00196        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00197        status = dkim_header(resign, hdr, strlen(hdr));
00198        assert(status == DKIM_STAT_OK);
00199 
00200        status = dkim_header(resign, HEADER01, strlen(HEADER01));
00201        assert(status == DKIM_STAT_OK);
00202 
00203        status = dkim_header(resign, HEADER02, strlen(HEADER02));
00204        assert(status == DKIM_STAT_OK);
00205 
00206        status = dkim_header(resign, HEADER03, strlen(HEADER03));
00207        assert(status == DKIM_STAT_OK);
00208 
00209        status = dkim_header(resign, HEADER04, strlen(HEADER04));
00210        assert(status == DKIM_STAT_OK);
00211 
00212        status = dkim_header(resign, HEADER05, strlen(HEADER05));
00213        assert(status == DKIM_STAT_OK);
00214 
00215        status = dkim_header(resign, HEADER06, strlen(HEADER06));
00216        assert(status == DKIM_STAT_OK);
00217 
00218        status = dkim_header(resign, HEADER07, strlen(HEADER07));
00219        assert(status == DKIM_STAT_OK);
00220 
00221        status = dkim_header(resign, HEADER08, strlen(HEADER08));
00222        assert(status == DKIM_STAT_OK);
00223 
00224        status = dkim_body(dkim, BODY00, strlen(BODY00));
00225        assert(status == DKIM_STAT_OK);
00226 
00227        status = dkim_body(resign, BODY00, strlen(BODY00));
00228        assert(status == DKIM_STAT_INVALID);
00229 
00230        status = dkim_body(dkim, BODY01, strlen(BODY01));
00231        assert(status == DKIM_STAT_OK);
00232 
00233        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00234        assert(status == DKIM_STAT_OK);
00235        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00236        assert(status == DKIM_STAT_OK);
00237        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00238        assert(status == DKIM_STAT_OK);
00239        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00240        assert(status == DKIM_STAT_OK);
00241        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00242        assert(status == DKIM_STAT_OK);
00243 
00244        status = dkim_body(dkim, BODY02, strlen(BODY02));
00245        assert(status == DKIM_STAT_OK);
00246 
00247        status = dkim_body(dkim, BODY03, strlen(BODY03));
00248        assert(status == DKIM_STAT_OK);
00249 
00250        status = dkim_body(dkim, BODY04, strlen(BODY04));
00251        assert(status == DKIM_STAT_OK);
00252 
00253        status = dkim_body(dkim, BODY03, strlen(BODY03));
00254        assert(status == DKIM_STAT_OK);
00255 
00256        status = dkim_body(dkim, BODY03, strlen(BODY03));
00257        assert(status == DKIM_STAT_OK);
00258 
00259        status = dkim_body(dkim, BODY05, strlen(BODY05));
00260        assert(status == DKIM_STAT_OK);
00261 
00262        status = dkim_body(dkim, BODY03, strlen(BODY03));
00263        assert(status == DKIM_STAT_OK);
00264 
00265        status = dkim_body(dkim, BODY03, strlen(BODY03));
00266        assert(status == DKIM_STAT_OK);
00267 
00268        status = dkim_eom(resign, NULL);
00269        assert(status == DKIM_STAT_INVALID);
00270 
00271        status = dkim_eom(dkim, NULL);
00272        assert(status == DKIM_STAT_OK);
00273 
00274        snprintf(hdr, sizeof hdr, "%s: %s", AUTHRESHDR, AUTHRESVAL);
00275        status = dkim_header(resign, hdr, strlen(hdr));
00276        assert(status == DKIM_STAT_OK);
00277 
00278        status = dkim_eoh(resign);
00279        assert(status == DKIM_STAT_OK);
00280 
00281        status = dkim_eom(resign, NULL);
00282        assert(status == DKIM_STAT_OK);
00283 
00284        memset(hdr, '\0', sizeof hdr);
00285        status = dkim_getsighdr(resign, hdr, sizeof hdr,
00286                                strlen(DKIM_SIGNHEADER) + 2);
00287        assert(status == DKIM_STAT_OK);
00288 
00289        status = dkim_free(dkim);
00290        assert(status == DKIM_STAT_INVALID);
00291 
00292        status = dkim_free(resign);
00293        assert(status == DKIM_STAT_OK);
00294 
00295        status = dkim_free(dkim);
00296        assert(status == DKIM_STAT_OK);
00297 
00298        /* now see if that one was valid */
00299        dkim = dkim_verify(lib, JOBID3, NULL, &status);
00300        assert(dkim != NULL);
00301 
00302        snprintf(hdr2, sizeof hdr2, "%s: %s", DKIM_SIGNHEADER, hdr);
00303        status = dkim_header(dkim, hdr2, strlen(hdr2));
00304        assert(status == DKIM_STAT_OK);
00305 
00306        snprintf(hdr2, sizeof hdr2, "%s: %s", DKIM_SIGNHEADER, SIG2);
00307        status = dkim_header(dkim, hdr2, strlen(hdr2));
00308        assert(status == DKIM_STAT_OK);
00309 
00310        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00311        assert(status == DKIM_STAT_OK);
00312 
00313        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00314        assert(status == DKIM_STAT_OK);
00315 
00316        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00317        assert(status == DKIM_STAT_OK);
00318 
00319        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00320        assert(status == DKIM_STAT_OK);
00321 
00322        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00323        assert(status == DKIM_STAT_OK);
00324 
00325        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00326        assert(status == DKIM_STAT_OK);
00327 
00328        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00329        assert(status == DKIM_STAT_OK);
00330 
00331        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00332        assert(status == DKIM_STAT_OK);
00333 
00334        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00335        assert(status == DKIM_STAT_OK);
00336 
00337        snprintf(hdr, sizeof hdr, "%s: %s", AUTHRESHDR, AUTHRESVAL);
00338        status = dkim_header(dkim, hdr, strlen(hdr));
00339        assert(status == DKIM_STAT_OK);
00340 
00341        status = dkim_eoh(dkim);
00342        assert(status == DKIM_STAT_OK);
00343 
00344        status = dkim_body(dkim, BODY00, strlen(BODY00));
00345        assert(status == DKIM_STAT_OK);
00346 
00347        status = dkim_body(dkim, BODY01, strlen(BODY01));
00348        assert(status == DKIM_STAT_OK);
00349 
00350        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00351        assert(status == DKIM_STAT_OK);
00352        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00353        assert(status == DKIM_STAT_OK);
00354        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00355        assert(status == DKIM_STAT_OK);
00356        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00357        assert(status == DKIM_STAT_OK);
00358        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00359        assert(status == DKIM_STAT_OK);
00360 
00361        status = dkim_body(dkim, BODY02, strlen(BODY02));
00362        assert(status == DKIM_STAT_OK);
00363 
00364        status = dkim_body(dkim, BODY03, strlen(BODY03));
00365        assert(status == DKIM_STAT_OK);
00366 
00367        status = dkim_body(dkim, BODY04, strlen(BODY04));
00368        assert(status == DKIM_STAT_OK);
00369 
00370        status = dkim_body(dkim, BODY03, strlen(BODY03));
00371        assert(status == DKIM_STAT_OK);
00372 
00373        status = dkim_body(dkim, BODY03, strlen(BODY03));
00374        assert(status == DKIM_STAT_OK);
00375 
00376        status = dkim_body(dkim, BODY05, strlen(BODY05));
00377        assert(status == DKIM_STAT_OK);
00378 
00379        status = dkim_body(dkim, BODY03, strlen(BODY03));
00380        assert(status == DKIM_STAT_OK);
00381 
00382        status = dkim_body(dkim, BODY03, strlen(BODY03));
00383        assert(status == DKIM_STAT_OK);
00384 
00385        status = dkim_eom(dkim, NULL);
00386        assert(status == DKIM_STAT_OK);
00387 
00388        status = dkim_free(dkim);
00389        assert(status == DKIM_STAT_OK);
00390 
00391        dkim_close(lib);
00392 
00393        return 0;
00394 }