Back to index

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