Back to index

opendkim  2.6.4
t-test73.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, 2012, The OpenDKIM Project.  All rights reserved.
00006 */
00007 
00008 #ifndef lint
00009 static char t_test73_c_id[] = "@(#)$Id: t-test73.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 <netinet/in.h>
00017 #include <arpa/inet.h>
00018 #include <arpa/nameser.h>
00019 #include <unistd.h>
00020 #include <assert.h>
00021 #include <string.h>
00022 #include <resolv.h>
00023 #include <stdio.h>
00024 
00025 #ifdef USE_GNUTLS
00026 # include <gnutls/gnutls.h>
00027 #endif /* USE_GNUTLS */
00028 
00029 /* libopendkim includes */
00030 #include "../dkim.h"
00031 #include "t-testdata.h"
00032 
00033 #define       BUFRSZ        1024
00034 #define       MAXADDRESS    256
00035 #define       MAXHEADER     4096
00036 
00037 #ifndef MIN
00038 # define MIN(x,y)    ((x) < (y) ? (x) : (y))
00039 #endif /* ! MIN */
00040 
00041 #define SIG2 "v=1;  a=rsa-sha1; c=simple/simple; d=example.com; s=test;\r\n\tt=1172620939; r=y; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID;\r\n\tb=F/cSOK/4qujIeNhKcC1LjAMFS33ORcsRqoEfNO6g1WXMlK5LW/foFePbUyFbbEbhY\r\n\t 8RhU+7C4R914QI6WW+lYMh11p0z1BGu2HJ4HHOlBivi1DDfZgsRZrEJhBeMngNIN9+\r\n\t 8HbGhTSWWpOBn+jYtfvGJBGtbv3AjgVgNropc7DM="
00042 
00043 size_t alen;
00044 unsigned char *abuf;
00045 unsigned char qbuf[BUFRSZ];
00046 
00047 static int
00048 stub_dns_cancel(void *srv, void *q)
00049 {
00050        return DKIM_DNS_SUCCESS;
00051 }
00052 
00053 static int
00054 stub_dns_query(void *srv, int type, unsigned char *query,
00055                unsigned char *buf, size_t buflen, void **qh)
00056 {
00057        abuf = buf;
00058        alen = buflen;
00059        dkim_strlcpy(qbuf, query, sizeof qbuf);
00060 
00061        return DKIM_DNS_SUCCESS;
00062 }
00063 
00064 static int
00065 stub_dns_waitreply(void *srv, void *qh, struct timeval *to, size_t *bytes,
00066                    int *error, int *dnssec)
00067 {
00068        int n;
00069        int status;
00070        int qdcount;
00071        unsigned char *cp;
00072        unsigned char *eom;
00073        char buf[BUFRSZ + 1];
00074        int elen;
00075        int slen;
00076        int olen;
00077        char *q;
00078        unsigned char *len;
00079        unsigned char *dnptrs[3];
00080        unsigned char **lastdnptr;
00081        HEADER newhdr;
00082 
00083        memset(&newhdr, '\0', sizeof newhdr);
00084        memset(&dnptrs, '\0', sizeof dnptrs);
00085               
00086        newhdr.qdcount = htons(1);
00087        newhdr.ancount = htons(1);
00088        newhdr.rcode = NOERROR;
00089        newhdr.opcode = QUERY;
00090        newhdr.qr = 1;
00091        newhdr.id = 0;
00092 
00093        lastdnptr = &dnptrs[2];
00094        dnptrs[0] = abuf;
00095 
00096        /* copy out the new header */
00097        memcpy(abuf, &newhdr, sizeof newhdr);
00098 
00099        cp = &abuf[HFIXEDSZ];
00100        eom = &abuf[alen];
00101 
00102        /* question section */
00103        elen = dn_comp(qbuf, cp, eom - cp, dnptrs, lastdnptr);
00104        if (elen == -1)
00105               return DKIM_DNS_ERROR;
00106        cp += elen;
00107        PUTSHORT(T_TXT, cp);
00108        PUTSHORT(C_IN, cp);
00109 
00110        /* answer section */
00111        elen = dn_comp(qbuf, cp, eom - cp, dnptrs, lastdnptr);
00112        if (elen == -1)
00113               return DKIM_DNS_ERROR;
00114        cp += elen;
00115        PUTSHORT(T_TXT, cp);
00116        PUTSHORT(C_IN, cp);
00117        PUTLONG(0L, cp);
00118 
00119        len = cp;
00120        cp += INT16SZ;
00121 
00122        slen = strlen(REPORTRECORD);
00123        q = REPORTRECORD;
00124        olen = 0;
00125 
00126        while (slen > 0)
00127        {
00128               elen = MIN(slen, 255);
00129               *cp = (char) elen;
00130               cp++;
00131               olen++;
00132               memcpy(cp, q, elen);
00133               q += elen;
00134               cp += elen;
00135               olen += elen;
00136               slen -= elen;
00137        }
00138 
00139        eom = cp;
00140 
00141        cp = len;
00142        PUTSHORT(olen, cp);
00143 
00144        *bytes = eom - abuf;
00145 
00146        if (dnssec != NULL)
00147               *dnssec = DKIM_DNSSEC_UNKNOWN;
00148 
00149        return DKIM_DNS_SUCCESS;
00150 }
00151 
00152 /*
00153 **  MAIN -- program mainline
00154 **
00155 **  Parameters:
00156 **     The usual.
00157 **
00158 **  Return value:
00159 **     Exit status.
00160 */
00161 
00162 int
00163 main(int argc, char **argv)
00164 {
00165        int hfd;
00166        int bfd;
00167        dkim_policy_t pcode = DKIM_POLICY_NONE;
00168        u_int flags;
00169        DKIM_STAT status;
00170        DKIM *dkim;
00171        DKIM_LIB *lib;
00172        DKIM_SIGINFO *sig;
00173        dkim_query_t qtype = DKIM_QUERY_FILE;
00174        unsigned char hdr[MAXHEADER + 1];
00175        unsigned char addr[MAXADDRESS + 1];
00176        unsigned char opts[BUFRSZ];
00177        unsigned char smtp[BUFRSZ];
00178 
00179 #ifdef USE_GNUTLS
00180        (void) gnutls_global_init();
00181 #endif /* USE_GNUTLS */
00182 
00183        /* instantiate the library */
00184        lib = dkim_init(NULL, NULL);
00185        assert(lib != NULL);
00186 
00187        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00188        {
00189               printf("*** simple/simple rsa-sha256 verifying with extra signature spaces and reportinfo (failure) SKIPPED\n");
00190               dkim_close(lib);
00191               return 0;
00192        }
00193 
00194        printf("*** simple/simple rsa-sha256 verifying with extra signature spaces and reportinfo (failure)\n");
00195 
00196        /* DNS stubs for the reporting data lookup */
00197        dkim_dns_set_query_service(lib, NULL);
00198        dkim_dns_set_query_start(lib, stub_dns_query);
00199        dkim_dns_set_query_cancel(lib, stub_dns_cancel);
00200        dkim_dns_set_query_waitreply(lib, stub_dns_waitreply);
00201 
00202        /* set flags */
00203        flags = DKIM_LIBFLAGS_TMPFILES;
00204 #ifdef TEST_KEEP_FILES
00205        flags |= DKIM_LIBFLAGS_KEEPFILES;
00206 #endif /* TEST_KEEP_FILES */
00207        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00208                            sizeof flags);
00209 
00210        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00211                            &qtype, sizeof qtype);
00212        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00213                            KEYFILE, strlen(KEYFILE));
00214 
00215        dkim = dkim_verify(lib, JOBID, NULL, &status);
00216        assert(dkim != NULL);
00217 
00218        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00219        status = dkim_header(dkim, hdr, strlen(hdr));
00220        assert(status == DKIM_STAT_OK);
00221 
00222        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00223        assert(status == DKIM_STAT_OK);
00224 
00225        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00226        assert(status == DKIM_STAT_OK);
00227 
00228        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00229        assert(status == DKIM_STAT_OK);
00230 
00231        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00232        assert(status == DKIM_STAT_OK);
00233 
00234        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00235        assert(status == DKIM_STAT_OK);
00236 
00237        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00238        assert(status == DKIM_STAT_OK);
00239 
00240        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00241        assert(status == DKIM_STAT_OK);
00242 
00243        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00244        assert(status == DKIM_STAT_OK);
00245 
00246        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00247        assert(status == DKIM_STAT_OK);
00248 
00249        status = dkim_eoh(dkim);
00250        assert(status == DKIM_STAT_OK);
00251 
00252        status = dkim_body(dkim, BODY00, strlen(BODY00));
00253        assert(status == DKIM_STAT_OK);
00254 
00255        status = dkim_body(dkim, BODY01, strlen(BODY01));
00256        assert(status == DKIM_STAT_OK);
00257 
00258        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00259        assert(status == DKIM_STAT_OK);
00260        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00261        assert(status == DKIM_STAT_OK);
00262        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00263        assert(status == DKIM_STAT_OK);
00264        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00265        assert(status == DKIM_STAT_OK);
00266        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00267        assert(status == DKIM_STAT_OK);
00268 
00269        status = dkim_body(dkim, BODY02, strlen(BODY02));
00270        assert(status == DKIM_STAT_OK);
00271 
00272        status = dkim_body(dkim, BODY03, strlen(BODY03));
00273        assert(status == DKIM_STAT_OK);
00274 
00275        status = dkim_body(dkim, BODY04, strlen(BODY04));
00276        assert(status == DKIM_STAT_OK);
00277 
00278        status = dkim_body(dkim, BODY03, strlen(BODY03));
00279        assert(status == DKIM_STAT_OK);
00280 
00281        status = dkim_body(dkim, BODY03, strlen(BODY03));
00282        assert(status == DKIM_STAT_OK);
00283 
00284        status = dkim_body(dkim, BODY05, strlen(BODY05));
00285        assert(status == DKIM_STAT_OK);
00286 
00287        status = dkim_body(dkim, BODY03, strlen(BODY03));
00288        assert(status == DKIM_STAT_OK);
00289 
00290        status = dkim_body(dkim, BODY03, strlen(BODY03));
00291        assert(status == DKIM_STAT_OK);
00292 
00293        status = dkim_eom(dkim, NULL);
00294        assert(status == DKIM_STAT_BADSIG);
00295 
00296        /* set up for the rest */
00297        hfd = -1;
00298        bfd = -1;
00299        sig = dkim_getsignature(dkim);
00300        assert(sig != NULL);
00301 
00302        /* policy query, verify correct result */
00303        status = dkim_policy(dkim, &pcode, NULL, NULL);
00304        assert(status == DKIM_STAT_OK);
00305        assert(pcode == DKIM_POLICY_ALL);
00306 
00307        /* request report info, verify valid descriptors and address */
00308        memset(addr, '\0', sizeof addr);
00309        memset(opts, '\0', sizeof opts);
00310        memset(smtp, '\0', sizeof smtp);
00311        status = dkim_sig_getreportinfo(dkim, sig, &hfd, &bfd,
00312                                        addr, sizeof addr,
00313                                        opts, sizeof opts,
00314                                        smtp, sizeof smtp, NULL);
00315        assert(status == DKIM_STAT_OK);
00316        assert(hfd > 2);
00317        assert(bfd > 2);
00318        assert(strcmp(addr, REPLYADDRESS) == 0);
00319        assert(strcmp(smtp, SMTPTOKEN) == 0);
00320 
00321        /* test descriptors */
00322        status = lseek(hfd, 0, SEEK_CUR);
00323        assert(status >= 0);
00324        status = lseek(bfd, 0, SEEK_CUR);
00325        assert(status >= 0);
00326 
00327        status = dkim_free(dkim);
00328        assert(status == DKIM_STAT_OK);
00329 
00330        dkim_close(lib);
00331 
00332        return 0;
00333 }