Back to index

opendkim  2.6.6
t-test115.c
Go to the documentation of this file.
00001 /*
00002 **  Copyright (c) 2005-2009 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_test115_c_id[] = "@(#)$Id: t-test115.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 SIG "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=nok;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00031 #define SIG2 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=badv;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00032 #define SIG3 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=sha256only;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00033 #define SIG4 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=badh;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00034 #define SIG5 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=badk;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00035 #define SIG6 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=nop;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00036 #define SIG7 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=emptyp;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00037 #define SIG8 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=corruptp;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
00038 
00039 /*
00040 **  MAIN -- program mainline
00041 **
00042 **  Parameters:
00043 **     The usual.
00044 **
00045 **  Return value:
00046 **     Exit status.
00047 */
00048 
00049 int
00050 main(int argc, char **argv)
00051 {
00052 #ifdef TEST_KEEP_FILES
00053        u_int flags;
00054 #endif /* TEST_KEEP_FILES */
00055        int nsigs;
00056        DKIM_STAT status;
00057        DKIM *dkim;
00058        DKIM_LIB *lib;
00059        DKIM_SIGINFO **sigs;
00060        dkim_query_t qtype = DKIM_QUERY_FILE;
00061        unsigned char hdr[MAXHEADER + 1];
00062 
00063        printf("*** detection of various key anomalies\n");
00064 
00065 #ifdef USE_GNUTLS
00066        (void) gnutls_global_init();
00067 #endif /* USE_GNUTLS */
00068 
00069        /* instantiate the library */
00070        lib = dkim_init(NULL, NULL);
00071        assert(lib != NULL);
00072 
00073 #ifdef TEST_KEEP_FILES
00074        /* set flags */
00075        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00076        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00077                            sizeof flags);
00078 #endif /* TEST_KEEP_FILES */
00079 
00080        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00081                            &qtype, sizeof qtype);
00082        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00083                            KEYFILE, strlen(KEYFILE));
00084 
00085        dkim = dkim_verify(lib, JOBID, NULL, &status);
00086        assert(dkim != NULL);
00087 
00088        /* key with no k= */
00089        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG);
00090        status = dkim_header(dkim, hdr, strlen(hdr));
00091        assert(status == DKIM_STAT_OK);
00092 
00093        /* key with bad v= */
00094        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00095        status = dkim_header(dkim, hdr, strlen(hdr));
00096        assert(status == DKIM_STAT_OK);
00097 
00098        /* key allowing sha256 only */
00099        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG3);
00100        status = dkim_header(dkim, hdr, strlen(hdr));
00101        assert(status == DKIM_STAT_OK);
00102 
00103        /* key with bogus h= */
00104        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG4);
00105        status = dkim_header(dkim, hdr, strlen(hdr));
00106        assert(status == DKIM_STAT_OK);
00107 
00108        /* key with bogus k= */
00109        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG5);
00110        status = dkim_header(dkim, hdr, strlen(hdr));
00111        assert(status == DKIM_STAT_OK);
00112 
00113        /* key with missing p= */
00114        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG6);
00115        status = dkim_header(dkim, hdr, strlen(hdr));
00116        assert(status == DKIM_STAT_OK);
00117 
00118        /* key with empty p= (revoked) */
00119        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG7);
00120        status = dkim_header(dkim, hdr, strlen(hdr));
00121        assert(status == DKIM_STAT_OK);
00122 
00123        /* key with corrupt p= */
00124        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG8);
00125        status = dkim_header(dkim, hdr, strlen(hdr));
00126        assert(status == DKIM_STAT_OK);
00127 
00128        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00129        assert(status == DKIM_STAT_OK);
00130 
00131        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00132        assert(status == DKIM_STAT_OK);
00133 
00134        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00135        assert(status == DKIM_STAT_OK);
00136 
00137        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00138        assert(status == DKIM_STAT_OK);
00139 
00140        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00141        assert(status == DKIM_STAT_OK);
00142 
00143        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00144        assert(status == DKIM_STAT_OK);
00145 
00146        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00147        assert(status == DKIM_STAT_OK);
00148 
00149        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00150        assert(status == DKIM_STAT_OK);
00151 
00152        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00153        assert(status == DKIM_STAT_OK);
00154 
00155        status = dkim_eoh(dkim);
00156        assert(status == DKIM_STAT_OK);
00157 
00158        status = dkim_getsiglist(dkim, &sigs, &nsigs);
00159        assert(status == DKIM_STAT_OK);
00160        assert(nsigs == 8);
00161 
00162        assert(dkim_sig_geterror(sigs[0]) == DKIM_SIGERROR_BADSIG);
00163        assert(dkim_sig_geterror(sigs[1]) == DKIM_SIGERROR_KEYVERSION);
00164        if (dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00165               assert(dkim_sig_geterror(sigs[2]) == DKIM_SIGERROR_KEYHASHMISMATCH);
00166        else
00167               assert(dkim_sig_geterror(sigs[2]) == DKIM_SIGERROR_KEYUNKNOWNHASH);
00168        assert(dkim_sig_geterror(sigs[3]) == DKIM_SIGERROR_KEYUNKNOWNHASH);
00169        assert(dkim_sig_geterror(sigs[4]) == DKIM_SIGERROR_KEYTYPEUNKNOWN);
00170        assert(dkim_sig_geterror(sigs[5]) == DKIM_SIGERROR_DNSSYNTAX);
00171        assert(dkim_sig_geterror(sigs[6]) == DKIM_SIGERROR_KEYREVOKED);
00172        assert(dkim_sig_geterror(sigs[7]) == DKIM_SIGERROR_KEYDECODE);
00173 
00174        status = dkim_free(dkim);
00175        assert(status == DKIM_STAT_OK);
00176 
00177        dkim_close(lib);
00178 
00179        return 0;
00180 }