Changeset 607

Show
Ignore:
Timestamp:
08/28/08 17:07:03
Author:
rachel
Message:

Add tests to check that malformed armoured packets are detected.
Return error if malformed armoured packets detected.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/src/lib/reader_armoured.c

    r574 r607  
    2424 */ 
    2525 
     26#include <openpgpsdk/errors.h> 
    2627#include <openpgpsdk/callback.h> 
    2728#include <openpgpsdk/configure.h> 
     
    251252            { 
    252253            free(hash); 
    253             ERR(cbinfo,"Unknown hash algorithm",OPS_E_R_BAD_FORMAT); 
     254            OPS_ERROR_1(errors,OPS_E_R_BAD_FORMAT,"Unknown hash algorithm '%s'",hashstr); 
    254255            } 
    255256        ops_hash_any(hash,alg); 
     
    277278                /* then this had better be a trailer! */ 
    278279                if(c != '-') 
    279                     ERR(cbinfo,"Bad dash-escaping",OPS_E_R_BAD_FORMAT); 
     280                    OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad dash-escaping"); 
    280281                for(count=2 ; count < 5 ; ++count) 
    281282                    { 
     
    283284                        return -1; 
    284285                    if(c != '-') 
    285                        ERR(cbinfo,"Bad dash-escaping (2)",OPS_E_R_BAD_FORMAT); 
     286                OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad dash-escaping (2)"); 
    286287                    } 
    287288                arg->state=AT_TRAILER_NAME; 
     
    332333    } 
    333334 
     335/* \todo what does a return value of 0 indicate? 1 is good, -1 is bad */ 
    334336static int parse_headers(dearmour_arg_t *arg,ops_error_t **errors, 
    335337                         ops_reader_info_t *rinfo,ops_parse_cb_info_t *cbinfo) 
    336338    { 
     339    int rtn=1; 
    337340    char *buf; 
    338341    unsigned nbuf; 
    339342    unsigned size; 
    340343    ops_boolean_t first=ops_true; 
    341     ops_parser_content_t content; 
     344    //ops_parser_content_t content; 
    342345 
    343346    buf=NULL; 
     
    349352 
    350353        if((c=read_char(arg,errors,rinfo,cbinfo,ops_true)) < 0) 
    351             return -1; 
     354        { 
     355        OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Unexpected EOF"); 
     356        rtn=-1; 
     357        break; 
     358        } 
    352359 
    353360        if(c == '\n') 
     
    364371            if(!s) 
    365372                if(!first && !arg->allow_headers_without_gap) 
     373            { 
    366374                    // then we have seriously malformed armour 
    367                     ERR(cbinfo,"No colon in armour header",OPS_E_R_BAD_FORMAT); 
     375                    OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No colon in armour header"); 
     376            rtn=-1; 
     377            break; 
     378            } 
    368379                else 
    369380                    { 
    370381                    if(first && 
    371382                       !(arg->allow_headers_without_gap || arg->allow_no_gap)) 
    372                         ERR(cbinfo,"No colon in armour header (2)", 
    373                             OPS_E_R_BAD_FORMAT); 
    374                     // then we have a nasty armoured block with no 
    375                     // headers, not even a blank line. 
    376                     buf[nbuf]='\n'; 
    377                     push_back(arg,(unsigned char *)buf,nbuf+1); 
    378                     break; 
     383                { 
     384                OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No colon in armour header (2)"); 
     385                // then we have a nasty armoured block with no 
     386                // headers, not even a blank line. 
     387                buf[nbuf]='\n'; 
     388                push_back(arg,(unsigned char *)buf,nbuf+1); 
     389                rtn=-1; 
     390                break; 
     391                } 
    379392                    } 
    380393            else 
     
    382395                *s='\0'; 
    383396                if(s[1] != ' ') 
    384                     ERR(cbinfo,"No space in armour header",OPS_E_R_BAD_FORMAT); 
     397                    OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No space in armour header"); 
    385398                add_header(arg,buf,s+2); 
    386399                nbuf=0; 
     
    401414    free(buf); 
    402415 
    403     return 1
     416    return rtn
    404417    } 
    405418 
     
    468481    int n2; 
    469482    unsigned long l; 
    470     ops_parser_content_t content; 
     483    //ops_parser_content_t content; 
    471484    int c; 
    472485    int ret; 
     
    476489    ret=read4(arg,errors,rinfo,cbinfo,&c,&n,&l); 
    477490    if(ret < 0) 
    478         ERR(cbinfo,"Badly formed base64",OPS_E_R_BAD_FORMAT); 
     491        OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly formed base64"); 
    479492 
    480493    if(n == 3) 
    481494        { 
    482495        if(c != '=') 
    483             ERR(cbinfo,"Badly terminated base64 (2)",OPS_E_R_BAD_FORMAT); 
     496            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64 (2)"); 
    484497        arg->buffered=2; 
    485498        arg->eof64=ops_true; 
     
    489502        { 
    490503        if(c != '=') 
    491             ERR(cbinfo,"Badly terminated base64 (3)",OPS_E_R_BAD_FORMAT); 
     504            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64 (3)"); 
    492505        arg->buffered=1; 
    493506        arg->eof64=ops_true; 
     
    495508        c=read_char(arg,errors,rinfo,cbinfo,ops_false); 
    496509        if(c != '=') 
    497             ERR(cbinfo,"Badly terminated base64",OPS_E_R_BAD_FORMAT); 
     510            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64"); 
    498511        } 
    499512    else if(n == 0) 
    500513        { 
    501514        if(!arg->prev_nl || c != '=') 
    502             ERR(cbinfo,"Badly terminated base64 (4)",OPS_E_R_BAD_FORMAT); 
     515            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated base64 (4)"); 
    503516        arg->buffered=0; 
    504517        } 
     
    516529        c=read_and_eat_whitespace(arg,errors,rinfo,cbinfo,ops_true); 
    517530        if(c != '\n') 
    518             ERR(cbinfo,"No newline at base64 end",OPS_E_R_BAD_FORMAT); 
     531            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No newline at base64 end"); 
    519532        c=read_char(arg,errors,rinfo,cbinfo,ops_false); 
    520533        if(c != '=') 
    521             ERR(cbinfo,"No checksum at base64 end",OPS_E_R_BAD_FORMAT); 
     534            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"No checksum at base64 end"); 
    522535        } 
    523536 
     
    527540        ret=read4(arg,errors,rinfo,cbinfo,&c,&n,&arg->read_checksum); 
    528541        if(ret < 0 || n != 4) 
    529             ERR(cbinfo,"Error in checksum",OPS_E_R_BAD_FORMAT); 
     542            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Error in checksum"); 
    530543        c=read_char(arg,errors,rinfo,cbinfo,ops_true); 
    531544        if(arg->allow_trailing_whitespace) 
    532545            c=eat_whitespace(c,arg,errors,rinfo,cbinfo,ops_true); 
    533546        if(c != '\n') 
    534             ERR(cbinfo,"Badly terminated checksum",OPS_E_R_BAD_FORMAT); 
     547            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Badly terminated checksum"); 
    535548        c=read_char(arg,errors,rinfo,cbinfo,ops_false); 
    536549        if(c != '-') 
    537             ERR(cbinfo,"Bad base64 trailer (2)",OPS_E_R_BAD_FORMAT); 
     550            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad base64 trailer (2)"); 
    538551        } 
    539552 
     
    542555        for(n=0 ; n < 4 ; ++n) 
    543556            if(read_char(arg,errors,rinfo,cbinfo,ops_false) != '-') 
    544                 ERR(cbinfo,"Bad base64 trailer",OPS_E_R_BAD_FORMAT); 
     557                OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad base64 trailer"); 
    545558        arg->eof64=ops_true; 
    546559        } 
     
    558571 
    559572    if(arg->eof64 && arg->read_checksum != arg->checksum) 
    560        ERR(cbinfo,"Checksum mismatch",OPS_E_R_BAD_FORMAT); 
     573        OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Checksum mismatch"); 
    561574 
    562575    return 1; 
     
    726739                 } 
    727740             /* then I guess this wasn't a proper trailer */ 
    728              ERR(cbinfo,"Bad ASCII armour trailer",OPS_E_R_BAD_FORMAT); 
     741             OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad ASCII armour trailer"); 
    729742             break; 
    730743 
     
    739752                 if(c != '-') 
    740753                     /* wasn't a trailer after all */ 
    741                      ERR(cbinfo,"Bad ASCII armour trailer (2)", 
    742                          OPS_E_R_BAD_FORMAT); 
     754                     OPS_ERROR(errors, OPS_E_R_BAD_FORMAT,"Bad ASCII armour trailer (2)"); 
    743755                 } 
    744756 
     
    752764             if(c != '\n') 
    753765                 /* wasn't a trailer line after all */ 
    754                  ERR(cbinfo,"Bad ASCII armour trailer (3)",OPS_E_R_BAD_FORMAT); 
     766                 OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Bad ASCII armour trailer (3)"); 
    755767 
    756768             if(!strncmp(buf,"BEGIN ",6)) 
  • openpgpsdk/trunk/tests/test_rsa_decrypt.c

    r574 r607  
    199199    for (compress_alg=0; compress_alg<n_compress_algos; compress_alg++) 
    200200        { 
    201         for (compress_lvl=0; compress_lvl<MAX_COMPRESS_LEVEL; compress_lvl++) 
     201        for (compress_lvl=0; compress_lvl<=MAX_COMPRESS_LEVEL; compress_lvl++) 
    202202            { 
     203            /* only need to check every compression level if we're debugging */ 
     204            if (compress_lvl>0 && compress_lvl < MAX_COMPRESS_LEVEL) 
     205                continue; 
    203206            for (armour=0; armour<=1; armour++) 
    204207                { 
  • openpgpsdk/trunk/tests/test_rsa_verify.c

    r572 r607  
    4949static char *filename_rsa_clearsign_passphrase="gpg_rsa_clearsign_passphrase.txt"; 
    5050static char *filename_rsa_clearsign_fail_bad_sig="gpg_rsa_clearsign_fail_bad_sig.txt"; 
     51 
    5152static char *filename_rsa_noarmour_compress_base="gpg_rsa_sign_noarmour_compress"; 
    5253static char *filename_rsa_armour_compress_base="gpg_rsa_sign_armour_compress"; 
     
    5556 
    5657static char *filename_rsa_hash_md5="gpg_rsa_hash_md5.txt"; 
     58 
     59static int num_malformed=0; 
    5760 
    5861typedef ops_parse_cb_return_t (*ops_callback)(const ops_parser_content_t *, ops_parse_cb_info_t *); 
     
    6265 */ 
    6366 
     67static void make_filename_malformed(char* filename, int maxlen, const int i) 
     68    { 
     69    snprintf(filename,maxlen,"malformed_%d.txt",i); 
     70    } 
     71 
     72static void create_malformed_testfiles() 
     73    { 
     74    int i=0; 
     75    int fd=0; 
     76    char * malformed[]={ 
     77                        // no signature 
     78                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\n-----END PGP SIGNATURE-----\n", 
     79                        // no signature and early EOF 
     80                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\n-----END PGP SIGNATURE-----", 
     81                        // early EOF 
     82                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.6 (GNU/Linux)\n", 
     83                        // no signature 
     84                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: -----END PGP SIGNATURE-----GnuPG v1.4.6 (GNU/Linux)\n" 
     85    }; 
     86    num_malformed=sizeof (malformed)/sizeof(char *); 
     87    for (i=0; i<num_malformed; i++) 
     88        { 
     89        char fullname[MAXBUF]; 
     90        char filename[MAXBUF]; 
     91        make_filename_malformed(filename,MAXBUF,i); 
     92        snprintf(fullname,MAXBUF,"%s/%s.asc",dir,filename); 
     93        if ((fd=open(fullname,O_WRONLY | O_CREAT, 0600)) < 0) 
     94            { 
     95            fprintf(stderr,"create_malformed_testfiles: cannot open file %s for writing\n", fullname); 
     96            return; 
     97            } 
     98        write(fd,malformed[i],strlen(malformed[i])); 
     99        close(fd); 
     100        } 
     101    } 
     102 
    64103int init_suite_rsa_verify(void) 
    65104    { 
     
    77116    create_small_testfile(filename_rsa_noarmour_passphrase); 
    78117    create_small_testfile(filename_rsa_noarmour_fail_bad_sig); 
     118 
     119    create_malformed_testfiles(); 
    79120 
    80121    // Now sign the test files with GPG 
     
    441482    } 
    442483 
     484static void test_rsa_verify_clearsign_fail_malformed_msg(void) 
     485    { 
     486    int i=0; 
     487    int armour=1; 
     488    assert(pub_keyring.nkeys); 
     489 
     490    for (i=0; i<num_malformed; i++) 
     491        { 
     492        char filename[MAXBUF]; 
     493        make_filename_malformed(filename,MAXBUF,i); 
     494        test_rsa_verify_fail(armour,filename,NULL,OPS_E_R_BAD_FORMAT); 
     495        } 
     496    } 
     497 
    443498CU_pSuite suite_rsa_verify() 
    444499{ 
     
    480535            return NULL; 
    481536 
     537    if (NULL == CU_add_test(suite, "Clearsign: should fail on malformed message", test_rsa_verify_clearsign_fail_malformed_msg)) 
     538            return NULL; 
     539 
    482540    return suite; 
    483541}