Back to index

opendkim  2.6.4
t-test46.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_test46_c_id[] = "@(#)$Id: t-test46.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 <stdlib.h>
00019 #include <stdio.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-sha256; c=simple/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=yHBAX+3IwxTZIynBuB/5tlsBInJq9n8qz5fgAycHi81=;\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="
00032 #define SIG2 "v=1; a=rsa-sha256; c=relaxed/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=hNR\r\n\tIcA7ZG6mZL9GPr5E9rJPQBy0DNnPSNAqYmtpbHJjhzWj3fsUKXDCEl8vJki6VuP0hDA\r\n\t4wRRJ6hkD0/u9iY2O+7xwAyuzkC3Z719CuGidnqlJt/1kJ4QW4KlcWJcj2v8SjD475G\r\n\tchVu0268Cz9PTJWSEqg/WZfWLQrji0gmy0="
00033 
00034 struct local_sig
00035 {
00036        const char *  ls_domain;
00037        const char *  ls_signalg;
00038        const char *  ls_timestamp;
00039        const char *  ls_canon;
00040        const char *  ls_version;
00041        const char *  ls_selector;
00042        const char *  ls_bodyhash;
00043        const char *  ls_hdrlist;
00044        const char *  ls_signature;
00045 };
00046 
00047 /*
00048 **  ALLOC_HANDLE -- allocate a local signature handle
00049 **
00050 **  Parameters:
00051 **     ignored -- a (void *) which we don't need
00052 **
00053 **  Return value:
00054 **     Pointer to a local signature handle.
00055 */
00056 
00057 void *
00058 alloc_handle(void *ignored)
00059 {
00060        void *new;
00061 
00062        new = (void *) malloc(sizeof(struct local_sig));
00063        assert(new != NULL);
00064        memset(new, '\0', sizeof(struct local_sig));
00065        return new;
00066 }
00067 
00068 /*
00069 **  TAGVALUES -- process a tag/value pair given a DKIM_SIG handle
00070 **
00071 **  Parameters:
00072 **     user -- (void *) referring to a struct local_sig
00073 **     pcode -- parameter code being reported (DKIM_PARAM_*)
00074 **     param -- pointer to the text form of the tag
00075 **     value -- pointer to the text form of the value
00076 **
00077 **  Return value:
00078 **     None.
00079 */
00080 
00081 void
00082 tagvalues(void *user, dkim_param_t pcode,
00083           const u_char *param, const u_char *value)
00084 {
00085        struct local_sig *ls = (struct local_sig *) user;
00086 
00087        switch (pcode)
00088        {
00089          case DKIM_PARAM_DOMAIN:
00090               ls->ls_domain = value;
00091               break;
00092 
00093          case DKIM_PARAM_SIGNATURE:
00094               ls->ls_signature = value;
00095               break;
00096 
00097          case DKIM_PARAM_HDRLIST:
00098               ls->ls_hdrlist = value;
00099               break;
00100 
00101          case DKIM_PARAM_BODYHASH:
00102               ls->ls_bodyhash = value;
00103               break;
00104 
00105          case DKIM_PARAM_SELECTOR:
00106               ls->ls_selector = value;
00107               break;
00108 
00109          case DKIM_PARAM_VERSION:
00110               ls->ls_version = value;
00111               break;
00112 
00113          case DKIM_PARAM_CANONALG:
00114               ls->ls_canon = value;
00115               break;
00116 
00117          case DKIM_PARAM_TIMESTAMP:
00118               ls->ls_timestamp = value;
00119               break;
00120 
00121          case DKIM_PARAM_SIGNALG:
00122               ls->ls_signalg = value;
00123               break;
00124 
00125          case DKIM_PARAM_UNKNOWN:
00126               assert(0);
00127        } 
00128 }
00129 
00130 /*
00131 **  PRESCREEN -- verify the contents of a local_sig structure
00132 **
00133 **  Parameters:
00134 **     dkim -- DKIM handle
00135 **     sigs -- array of DKIM_SIGINFO pointers
00136 **     nsigs -- how many sigs there were
00137 **
00138 **  Return value:
00139 **     DKIM_CBSTAT_CONTINUE (assuming no assertions fire).
00140 */
00141 
00142 DKIM_CBSTAT
00143 prescreen(DKIM *dkim, DKIM_SIGINFO **sigs, int nsigs)
00144 {
00145        DKIM_SIGINFO *siginfo;
00146 
00147        assert(dkim != NULL);
00148        assert(sigs != NULL);
00149        assert(nsigs == 2);
00150 
00151        /*
00152        **  Arrange to swap order since we know for this test that the second
00153        **  one is good and the first one is bad.
00154        */
00155 
00156        siginfo = sigs[1];
00157        sigs[1] = sigs[0];
00158        sigs[0] = siginfo;
00159 
00160        return DKIM_CBSTAT_CONTINUE;
00161 }
00162 
00163 /*
00164 **  FREE_HANDLE -- deallocate a local signature handle
00165 **
00166 **  Parameters:
00167 **     ignored -- a (void *) which we don't need
00168 **     handle -- pointer to the struct local_sig to be deallocated
00169 **
00170 **  Return value:
00171 **     None.
00172 */
00173 
00174 void
00175 free_handle(void *ignored, void *handle)
00176 {
00177        assert(handle != NULL);
00178 
00179        free(handle);
00180 }
00181 
00182 /*
00183 **  MAIN -- program mainline
00184 **
00185 **  Parameters:
00186 **     The usual.
00187 **
00188 **  Return value:
00189 **     Exit status.
00190 */
00191 
00192 int
00193 main(int argc, char **argv)
00194 {
00195 #ifdef TEST_KEEP_FILES
00196        u_int flags;
00197 #endif /* TEST_KEEP_FILES */
00198        DKIM_STAT status;
00199        DKIM *dkim;
00200        DKIM_LIB *lib;
00201        dkim_query_t qtype = DKIM_QUERY_FILE;
00202        unsigned char hdr[MAXHEADER + 1];
00203 
00204 #ifdef USE_GNUTLS
00205        (void) gnutls_global_init();
00206 #endif /* USE_GNUTLS */
00207 
00208        /* instantiate the library */
00209        lib = dkim_init(NULL, NULL);
00210        assert(lib != NULL);
00211 
00212        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00213        {
00214               printf("*** relaxed/simple rsa-sha256 signature reordering SKIPPED\n");
00215               dkim_close(lib);
00216               return 0;
00217        }
00218 
00219        printf("*** relaxed/simple rsa-sha256 signature reordering\n");
00220 
00221 #ifdef TEST_KEEP_FILES
00222        /* set flags */
00223        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00224        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00225                            sizeof flags);
00226 #endif /* TEST_KEEP_FILES */
00227 
00228        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00229                            &qtype, sizeof qtype);
00230        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00231                            KEYFILE, strlen(KEYFILE));
00232 
00233        dkim = dkim_verify(lib, JOBID, NULL, &status);
00234        assert(dkim != NULL);
00235 
00236        (void) dkim_set_signature_handle(lib, alloc_handle);
00237        (void) dkim_set_signature_handle_free(lib, free_handle);
00238        (void) dkim_set_signature_tagvalues(lib, tagvalues);
00239        (void) dkim_set_prescreen(lib, prescreen);
00240 
00241        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG1);
00242        status = dkim_header(dkim, hdr, strlen(hdr));
00243        assert(status == DKIM_STAT_OK);
00244 
00245        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00246        status = dkim_header(dkim, hdr, strlen(hdr));
00247        assert(status == DKIM_STAT_OK);
00248 
00249        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00250        assert(status == DKIM_STAT_OK);
00251 
00252        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00253        assert(status == DKIM_STAT_OK);
00254 
00255        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00256        assert(status == DKIM_STAT_OK);
00257 
00258        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00259        assert(status == DKIM_STAT_OK);
00260 
00261        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00262        assert(status == DKIM_STAT_OK);
00263 
00264        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00265        assert(status == DKIM_STAT_OK);
00266 
00267        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00268        assert(status == DKIM_STAT_OK);
00269 
00270        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00271        assert(status == DKIM_STAT_OK);
00272 
00273        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00274        assert(status == DKIM_STAT_OK);
00275 
00276        status = dkim_eoh(dkim);
00277        assert(status == DKIM_STAT_OK);
00278 
00279        status = dkim_body(dkim, BODY00, strlen(BODY00));
00280        assert(status == DKIM_STAT_OK);
00281 
00282        status = dkim_body(dkim, BODY01, strlen(BODY01));
00283        assert(status == DKIM_STAT_OK);
00284 
00285        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00286        assert(status == DKIM_STAT_OK);
00287        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00288        assert(status == DKIM_STAT_OK);
00289        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00290        assert(status == DKIM_STAT_OK);
00291        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00292        assert(status == DKIM_STAT_OK);
00293        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00294        assert(status == DKIM_STAT_OK);
00295 
00296        status = dkim_body(dkim, BODY02, strlen(BODY02));
00297        assert(status == DKIM_STAT_OK);
00298 
00299        status = dkim_body(dkim, BODY03, strlen(BODY03));
00300        assert(status == DKIM_STAT_OK);
00301 
00302        status = dkim_body(dkim, BODY04, strlen(BODY04));
00303        assert(status == DKIM_STAT_OK);
00304 
00305        status = dkim_body(dkim, BODY03, strlen(BODY03));
00306        assert(status == DKIM_STAT_OK);
00307 
00308        status = dkim_body(dkim, BODY03, strlen(BODY03));
00309        assert(status == DKIM_STAT_OK);
00310 
00311        status = dkim_body(dkim, BODY05, strlen(BODY05));
00312        assert(status == DKIM_STAT_OK);
00313 
00314        status = dkim_body(dkim, BODY03, strlen(BODY03));
00315        assert(status == DKIM_STAT_OK);
00316 
00317        status = dkim_body(dkim, BODY03, strlen(BODY03));
00318        assert(status == DKIM_STAT_OK);
00319 
00320        status = dkim_eom(dkim, NULL);
00321        assert(status == DKIM_STAT_OK);
00322 
00323        status = dkim_free(dkim);
00324        assert(status == DKIM_STAT_OK);
00325 
00326        dkim_close(lib);
00327 
00328        return 0;
00329 }