Back to index

opendkim  2.6.4
t-test72.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_test72_c_id[] = "@(#)$Id: t-test72.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 XHEADER             "Cc: user@example.com"
00031 
00032 /*
00033 **  MESSAGE -- run the message's content
00034 **
00035 **  Parameters:
00036 **     dkim -- DKIM handle
00037 **     hdr -- signature header content (may be NULL)
00038 **     hdrlen -- signature header size
00039 **
00040 **  Return value:
00041 **     None.
00042 */
00043 
00044 void
00045 message(DKIM *dkim, char *hdr)
00046 {
00047        int status;
00048 
00049        assert(dkim != NULL);
00050 
00051        if (hdr != NULL)
00052        {
00053               status = dkim_header(dkim, hdr, strlen(hdr));
00054               assert(status == DKIM_STAT_OK);
00055        }
00056 
00057        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00058        assert(status == DKIM_STAT_OK);
00059 
00060        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00061        assert(status == DKIM_STAT_OK);
00062 
00063        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00064        assert(status == DKIM_STAT_OK);
00065 
00066        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00067        assert(status == DKIM_STAT_OK);
00068 
00069        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00070        assert(status == DKIM_STAT_OK);
00071 
00072        status = dkim_header(dkim, XHEADER, strlen(XHEADER));
00073        assert(status == DKIM_STAT_OK);
00074 
00075        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00076        assert(status == DKIM_STAT_OK);
00077 
00078        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00079        assert(status == DKIM_STAT_OK);
00080 
00081        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00082        assert(status == DKIM_STAT_OK);
00083 
00084        status = dkim_eoh(dkim);
00085        assert(status == DKIM_STAT_OK);
00086 
00087        status = dkim_body(dkim, BODY00, strlen(BODY00));
00088        assert(status == DKIM_STAT_OK);
00089 
00090        status = dkim_body(dkim, BODY01, strlen(BODY01));
00091        assert(status == DKIM_STAT_OK);
00092 
00093        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00094        assert(status == DKIM_STAT_OK);
00095        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00096        assert(status == DKIM_STAT_OK);
00097        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00098        assert(status == DKIM_STAT_OK);
00099        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00100        assert(status == DKIM_STAT_OK);
00101        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00102        assert(status == DKIM_STAT_OK);
00103 
00104        status = dkim_body(dkim, BODY02, strlen(BODY02));
00105        assert(status == DKIM_STAT_OK);
00106 
00107        status = dkim_body(dkim, BODY03, strlen(BODY03));
00108        assert(status == DKIM_STAT_OK);
00109 
00110        status = dkim_body(dkim, BODY04, strlen(BODY04));
00111        assert(status == DKIM_STAT_OK);
00112 
00113        status = dkim_body(dkim, BODY03, strlen(BODY03));
00114        assert(status == DKIM_STAT_OK);
00115 
00116        status = dkim_body(dkim, BODY03, strlen(BODY03));
00117        assert(status == DKIM_STAT_OK);
00118 
00119        status = dkim_body(dkim, BODY05, strlen(BODY05));
00120        assert(status == DKIM_STAT_OK);
00121 
00122        status = dkim_body(dkim, BODY03, strlen(BODY03));
00123        assert(status == DKIM_STAT_OK);
00124 
00125        status = dkim_body(dkim, BODY03, strlen(BODY03));
00126        assert(status == DKIM_STAT_OK);
00127 
00128        status = dkim_eom(dkim, NULL);
00129        assert(status == DKIM_STAT_OK);
00130 }
00131 
00132 /*
00133 **  MAIN -- program mainline
00134 **
00135 **  Parameters:
00136 **     The usual.
00137 **
00138 **  Return value:
00139 **     Exit status.
00140 */
00141 
00142 int
00143 main(int argc, char **argv)
00144 {
00145 #ifdef TEST_KEEP_FILES
00146        u_int flags;
00147 #endif /* TEST_KEEP_FILES */
00148        DKIM_STAT status;
00149        uint64_t fixed_time;
00150        DKIM *dkim;
00151        DKIM_LIB *lib;
00152        dkim_sigkey_t key;
00153        dkim_query_t qtype = DKIM_QUERY_FILE;
00154        unsigned char hdr[MAXHEADER + 1];
00155 
00156 #ifdef USE_GNUTLS
00157        (void) gnutls_global_init();
00158 #endif /* USE_GNUTLS */
00159 
00160        /* instantiate the library */
00161        lib = dkim_init(NULL, NULL);
00162        assert(lib != NULL);
00163 
00164        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00165        {
00166               printf("*** simple/simple rsa-sha256 signing with subsequent verifying SKIPPED\n");
00167               dkim_close(lib);
00168               return 0;
00169        }
00170 
00171        printf("*** simple/simple rsa-sha256 \"b=\" wrap test\n");
00172 
00173 #ifdef TEST_KEEP_FILES
00174        /* set flags */
00175        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00176        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00177                            sizeof flags);
00178 #endif /* TEST_KEEP_FILES */
00179 
00180        key = KEY;
00181 
00182        dkim = dkim_sign(lib, JOBID, NULL, key, SELECTOR, DOMAIN,
00183                         DKIM_CANON_SIMPLE, DKIM_CANON_SIMPLE,
00184                         DKIM_SIGN_RSASHA256, -1L, &status);
00185        assert(dkim != NULL);
00186 
00187        /* fix signing time */
00188        fixed_time = 1172620939;
00189        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FIXEDTIME,
00190                            &fixed_time, sizeof fixed_time);
00191 
00192        (void) message(dkim, NULL);
00193 
00194        snprintf(hdr, sizeof hdr, "%s: ", DKIM_SIGNHEADER);
00195        status = dkim_getsighdr(dkim, hdr + strlen(DKIM_SIGNHEADER) + 2,
00196                                sizeof hdr - strlen(DKIM_SIGNHEADER) - 2,
00197                                strlen(DKIM_SIGNHEADER) + 2);
00198        assert(status == DKIM_STAT_OK);
00199 
00200        status = dkim_free(dkim);
00201        assert(status == DKIM_STAT_OK);
00202 
00203        /* verify the message using the signature we just obtained */
00204        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00205                            &qtype, sizeof qtype);
00206        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00207                            KEYFILE, strlen(KEYFILE));
00208 
00209        dkim = dkim_verify(lib, JOBID, NULL, &status);
00210        assert(dkim != NULL);
00211 
00212        (void) message(dkim, hdr);
00213 
00214        status = dkim_free(dkim);
00215        assert(status == DKIM_STAT_OK);
00216 
00217        dkim_close(lib);
00218 
00219        return 0;
00220 }