~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to shared/cdiff.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
372
372
 
373
373
    if(del || xchg) {
374
374
 
375
 
        if(!(fh = fopen(ctx->open_db, "r"))) {
 
375
        if(!(fh = fopen(ctx->open_db, "rb"))) {
376
376
            logg("!cdiff_cmd_close: Can't open file %s for reading\n", ctx->open_db);
377
377
            return -1;
378
378
        }
383
383
            return -1;
384
384
        }
385
385
 
386
 
        if(!(tmpfh = fopen(tmp, "w"))) {
 
386
        if(!(tmpfh = fopen(tmp, "wb"))) {
387
387
            logg("!cdiff_cmd_close: Can't open file %s for writing\n", tmp);
388
388
            fclose(fh);
389
389
            free(tmp);
467
467
 
468
468
    if(add) {
469
469
 
470
 
        if(!(fh = fopen(ctx->open_db, "a"))) {
 
470
        if(!(fh = fopen(ctx->open_db, "ab"))) {
471
471
            logg("!cdiff_cmd_close: Can't open file %s for appending\n", ctx->open_db);
472
472
            return -1;
473
473
        }
538
538
        return -1;
539
539
    }
540
540
 
541
 
    if(!(src = fopen(srcdb, "r"))) {
 
541
    if(!(src = fopen(srcdb, "rb"))) {
542
542
        logg("!cdiff_cmd_move: Can't open %s for reading\n", srcdb);
543
543
        free(start_str);
544
544
        free(end_str);
555
555
        return -1;
556
556
    }
557
557
 
558
 
    if(!(dst = fopen(dstdb, "a"))) {
 
558
    if(!(dst = fopen(dstdb, "ab"))) {
559
559
        logg("!cdiff_cmd_move: Can't open %s for appending\n", dstdb);
560
560
        free(start_str);
561
561
        free(end_str);
576
576
        return -1;
577
577
    }
578
578
 
579
 
    if(!(tmp = fopen(tmpdb, "w"))) {
 
579
    if(!(tmp = fopen(tmpdb, "wb"))) {
580
580
        logg("!cdiff_cmd_move: Can't open file %s for writing\n", tmpdb);
581
581
        free(start_str);
582
582
        free(end_str);
773
773
    return 0;
774
774
}
775
775
 
776
 
static void pss_mgf(unsigned char *in, unsigned int inlen, unsigned char *out, unsigned int outlen)
777
 
{
778
 
        SHA256_CTX ctx;
779
 
        unsigned int i, laps;
780
 
        unsigned char cnt[4], digest[PSS_DIGEST_LENGTH];
781
 
 
782
 
 
783
 
    laps = (outlen + PSS_DIGEST_LENGTH - 1) / PSS_DIGEST_LENGTH;
784
 
 
785
 
    for(i = 0; i < laps; i++) {
786
 
        cnt[0] = (unsigned char) 0;
787
 
        cnt[1] = (unsigned char) 0;
788
 
        cnt[2] = (unsigned char) (i / 256);
789
 
        cnt[3] = (unsigned char) i;
790
 
 
791
 
        sha256_init(&ctx);
792
 
        sha256_update(&ctx, in, inlen);
793
 
        sha256_update(&ctx, cnt, sizeof(cnt));
794
 
        sha256_final(&ctx, digest);
795
 
 
796
 
        if(i != laps - 1)
797
 
            memcpy(&out[i * PSS_DIGEST_LENGTH], digest, PSS_DIGEST_LENGTH);
798
 
        else
799
 
            memcpy(&out[i * PSS_DIGEST_LENGTH], digest, outlen - i * PSS_DIGEST_LENGTH);
800
 
    }
801
 
}
802
 
 
803
 
static int pss_versig(const unsigned char *sha256, const char *dsig)
804
 
{
805
 
        mp_int n, e;
806
 
        SHA256_CTX ctx;
807
 
        unsigned char *pt, digest1[PSS_DIGEST_LENGTH], digest2[PSS_DIGEST_LENGTH], *salt;
808
 
        unsigned int plen = PSS_NBITS / 8, hlen, slen, i;
809
 
        unsigned char dblock[PSS_NBITS / 8 - PSS_DIGEST_LENGTH - 1];
810
 
        unsigned char mblock[PSS_NBITS / 8 - PSS_DIGEST_LENGTH - 1];
811
 
        unsigned char fblock[8 + 2 * PSS_DIGEST_LENGTH];
812
 
 
813
 
 
814
 
    hlen = slen = PSS_DIGEST_LENGTH;
815
 
    mp_init(&n);
816
 
    mp_read_radix(&n, PSS_NSTR, 10);
817
 
    mp_init(&e);
818
 
    mp_read_radix(&e, PSS_ESTR, 10);
819
 
    if(!(pt = cli_decodesig(dsig, plen, e, n))) {
820
 
        mp_clear(&n);
821
 
        mp_clear(&e);
822
 
        return -1;
823
 
    }
824
 
    mp_clear(&n);
825
 
    mp_clear(&e);
826
 
 
827
 
    if(pt[plen - 1] != 0xbc) {
828
 
        /* cli_dbgmsg("cli_versigpss: Incorrect signature syntax (0xbc)\n"); */
829
 
        free(pt);
830
 
        return -1;
831
 
    }
832
 
 
833
 
    memcpy(mblock, pt, plen - hlen - 1);
834
 
    memcpy(digest2, &pt[plen - hlen - 1], hlen);
835
 
    free(pt);
836
 
 
837
 
    pss_mgf(digest2, hlen, dblock, plen - hlen - 1);
838
 
 
839
 
    for(i = 0; i < plen - hlen - 1; i++)
840
 
        dblock[i] ^= mblock[i];
841
 
 
842
 
    dblock[0] &= (0xff >> 1);
843
 
 
844
 
    salt = memchr(dblock, 0x01, sizeof(dblock));
845
 
    if(!salt) {
846
 
        /* cli_dbgmsg("cli_versigpss: Can't find salt\n"); */
847
 
        return -1;
848
 
    }
849
 
    salt++;
850
 
 
851
 
    if((unsigned int) (dblock + sizeof(dblock) - salt) != slen) {
852
 
        /* cli_dbgmsg("cli_versigpss: Bad salt size\n"); */
853
 
        return -1;
854
 
    }
855
 
 
856
 
    memset(fblock, 0, 8);
857
 
    memcpy(&fblock[8], sha256, hlen);
858
 
    memcpy(&fblock[8 + hlen], salt, slen);
859
 
 
860
 
    sha256_init(&ctx);
861
 
    sha256_update(&ctx, fblock, sizeof(fblock));
862
 
    sha256_final(&ctx, digest1);
863
 
 
864
 
    if(memcmp(digest1, digest2, hlen)) {
865
 
        /* cli_dbgmsg("cli_versigpss: Signature doesn't match.\n"); */
866
 
        return -1;
867
 
    }
868
 
 
869
 
    return 0;
870
 
}
871
 
 
872
776
int cdiff_apply(int fd, unsigned short mode)
873
777
{
874
778
        struct cdiff_ctx ctx;
977
881
        }
978
882
        sha256_final(&sha256ctx, digest);
979
883
 
980
 
        if(pss_versig(digest, dsig)) {
 
884
        if(cli_versig2(digest, dsig, PSS_NSTR, PSS_ESTR) != CL_SUCCESS) {
981
885
            logg("!cdiff_apply: Incorrect digital signature\n");
982
886
            close(desc);
983
887
            free(line);
1075
979
 
1076
980
    } else { /* .script */
1077
981
 
1078
 
        if(!(fh = fdopen(desc, "r"))) {
 
982
        if(!(fh = fdopen(desc, "rb"))) {
1079
983
            logg("!cdiff_apply: fdopen() failed for descriptor %d\n", desc);
1080
984
            close(desc);
1081
985
            free(line);