Back to index

opendkim  2.6.4
t-test54.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_test54_c_id[] = "@(#)$Id: t-test54.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 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;\r\n\tb=hNRIcA7ZG6mZL9GPr5E9rJPQBy0DNnPSNAqYmtpbHJjhzWj3fsUKXDCEl8vJki6Vu\r\n\t P0hDA4wRRJ6hkD0/u9iY2O+7xwAyuzkC3Z719CuGidnqlJt/1kJ4QW4KlcWJcj2v8S\r\n\t jD475GchVu0268Cz9PTJWSEqg/WZfWLQrji0gmy0="
00031 
00032 #define       SKIPHEADER    "Return-Path: <msk@sendmail.com>"
00033 
00034 char *signheaders[] =
00035 {
00036        "Received",
00037        "From",
00038        "To",
00039        "Date",
00040        "Subject",
00041        "Message-ID",
00042        NULL
00043 };
00044 
00045 /*
00046 **  MAIN -- program mainline
00047 **
00048 **  Parameters:
00049 **     The usual.
00050 **
00051 **  Return value:
00052 **     Exit status.
00053 */
00054 
00055 int
00056 main(int argc, char **argv)
00057 {
00058 #ifdef TEST_KEEP_FILES
00059        u_int flags;
00060 #endif /* TEST_KEEP_FILES */
00061        DKIM_STAT status;
00062        uint64_t fixed_time;
00063        DKIM *dkim;
00064        DKIM_LIB *lib;
00065        dkim_sigkey_t key;
00066        unsigned char hdr[MAXHEADER + 1];
00067 
00068 #ifdef USE_GNUTLS
00069        (void) gnutls_global_init();
00070 #endif /* USE_GNUTLS */
00071 
00072        /* instantiate the library */
00073        lib = dkim_init(NULL, NULL);
00074        assert(lib != NULL);
00075 
00076        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00077        {
00078               printf("*** relaxed/simple rsa-sha256 signing with header selection SKIPPED\n");
00079               dkim_close(lib);
00080               return 0;
00081        }
00082 
00083        printf("*** relaxed/simple rsa-sha256 signing with header selection\n");
00084 
00085 #ifdef TEST_KEEP_FILES
00086        /* set flags */
00087        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00088        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00089                            sizeof flags);
00090 #endif /* TEST_KEEP_FILES */
00091 
00092        key = KEY;
00093 
00094        dkim = dkim_sign(lib, JOBID, NULL, key, SELECTOR, DOMAIN,
00095                         DKIM_CANON_RELAXED, DKIM_CANON_SIMPLE,
00096                         DKIM_SIGN_RSASHA256, -1L, &status);
00097        assert(dkim != NULL);
00098 
00099        /* fix signing time */
00100        fixed_time = 1172620939;
00101        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FIXEDTIME,
00102                            &fixed_time, sizeof fixed_time);
00103 
00104        /* arrange to sign specific headers */
00105        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_SIGNHDRS,
00106                            signheaders, sizeof(char **));
00107 
00108        status = dkim_header(dkim, SKIPHEADER, strlen(SKIPHEADER));
00109        assert(status == DKIM_STAT_OK);
00110 
00111        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00112        assert(status == DKIM_STAT_OK);
00113 
00114        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00115        assert(status == DKIM_STAT_OK);
00116 
00117        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00118        assert(status == DKIM_STAT_OK);
00119 
00120        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00121        assert(status == DKIM_STAT_OK);
00122 
00123        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00124        assert(status == DKIM_STAT_OK);
00125 
00126        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00127        assert(status == DKIM_STAT_OK);
00128 
00129        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00130        assert(status == DKIM_STAT_OK);
00131 
00132        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00133        assert(status == DKIM_STAT_OK);
00134 
00135 #define       XHEADER1      "X-ExtraHeader-1: value1"
00136 #define       XHEADER2      "X-ExtraHeader-2: value2"
00137 
00138        status = dkim_header(dkim, XHEADER1, strlen(XHEADER1));
00139        assert(status == DKIM_STAT_OK);
00140 
00141        status = dkim_header(dkim, XHEADER2, strlen(XHEADER2));
00142        assert(status == DKIM_STAT_OK);
00143 
00144        status = dkim_eoh(dkim);
00145        assert(status == DKIM_STAT_OK);
00146 
00147        status = dkim_body(dkim, BODY00, strlen(BODY00));
00148        assert(status == DKIM_STAT_OK);
00149 
00150        status = dkim_body(dkim, BODY01, strlen(BODY01));
00151        assert(status == DKIM_STAT_OK);
00152 
00153        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00154        assert(status == DKIM_STAT_OK);
00155        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00156        assert(status == DKIM_STAT_OK);
00157        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00158        assert(status == DKIM_STAT_OK);
00159        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00160        assert(status == DKIM_STAT_OK);
00161        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00162        assert(status == DKIM_STAT_OK);
00163 
00164        status = dkim_body(dkim, BODY02, strlen(BODY02));
00165        assert(status == DKIM_STAT_OK);
00166 
00167        status = dkim_body(dkim, BODY03, strlen(BODY03));
00168        assert(status == DKIM_STAT_OK);
00169 
00170        status = dkim_body(dkim, BODY04, strlen(BODY04));
00171        assert(status == DKIM_STAT_OK);
00172 
00173        status = dkim_body(dkim, BODY03, strlen(BODY03));
00174        assert(status == DKIM_STAT_OK);
00175 
00176        status = dkim_body(dkim, BODY03, strlen(BODY03));
00177        assert(status == DKIM_STAT_OK);
00178 
00179        status = dkim_body(dkim, BODY05, strlen(BODY05));
00180        assert(status == DKIM_STAT_OK);
00181 
00182        status = dkim_body(dkim, BODY03, strlen(BODY03));
00183        assert(status == DKIM_STAT_OK);
00184 
00185        status = dkim_body(dkim, BODY03, strlen(BODY03));
00186        assert(status == DKIM_STAT_OK);
00187 
00188        status = dkim_eom(dkim, NULL);
00189        assert(status == DKIM_STAT_OK);
00190 
00191        memset(hdr, '\0', sizeof hdr);
00192        status = dkim_getsighdr(dkim, hdr, sizeof hdr,
00193                                strlen(DKIM_SIGNHEADER) + 2);
00194        assert(status == DKIM_STAT_OK);
00195        assert(strcmp(SIG2, hdr) == 0);
00196 
00197        status = dkim_free(dkim);
00198        assert(status == DKIM_STAT_OK);
00199 
00200        dkim_close(lib);
00201 
00202        return 0;
00203 }