Changeset 610

Show
Ignore:
Timestamp:
09/01/08 13:31:54
Author:
rachel
Message:

Add more tests for malformed PGP armoured messages.
Implemented better checking for malformed PGP armoured messages.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/include/openpgpsdk/errors.h

    r573 r610  
    3030 
    3131/** error codes */ 
    32 // Remember to add names to map in adv_errors.c 
     32// Remember to add names to map in errors.c 
    3333typedef enum  
    3434    { 
     
    4343    OPS_E_R_EARLY_EOF           =OPS_E_R+2, 
    4444    OPS_E_R_BAD_FORMAT          =OPS_E_R+3, // For example, malformed armour 
    45     OPS_E_R_UNCONSUMED_DATA     =OPS_E_R+4, 
     45    OPS_E_R_UNSUPPORTED         =OPS_E_R+4, 
     46    OPS_E_R_UNCONSUMED_DATA     =OPS_E_R+5, 
    4647 
    4748    /* writer errors */ 
     
    6667    OPS_E_V=0x5000, /* general validation error */ 
    6768    OPS_E_V_BAD_SIGNATURE       =OPS_E_V+1, 
    68     OPS_E_V_UNKNOWN_SIGNER      =OPS_E_V+2, 
     69    OPS_E_V_NO_SIGNATURE        =OPS_E_V+2, 
     70    OPS_E_V_UNKNOWN_SIGNER      =OPS_E_V+3, 
    6971 
    7072    /* Algorithm support errors */ 
  • openpgpsdk/trunk/include/openpgpsdk/packet-parse.h

    r570 r610  
    7878typedef struct ops_reader_info ops_reader_info_t; 
    7979typedef struct ops_crypt_info ops_crypt_info_t; 
    80  
    81 /* 
    82 typedef ops_reader_ret_t ops_reader_t(unsigned char *dest, 
    83                                       unsigned *plength, 
    84                                       ops_reader_flags_t flags, 
    85                                       ops_error_t **errors, 
    86                                       ops_reader_info_t *rinfo, 
    87                                       ops_parse_cb_info_t *cbinfo); 
    88 */ 
    8980 
    9081/* 
  • openpgpsdk/trunk/src/lib/errors.c

    r589 r610  
    6767    ERRNAME(OPS_E_V), 
    6868    ERRNAME(OPS_E_V_BAD_SIGNATURE), 
     69    ERRNAME(OPS_E_V_NO_SIGNATURE), 
    6970    ERRNAME(OPS_E_V_UNKNOWN_SIGNER), 
    7071 
  • openpgpsdk/trunk/src/lib/reader_armoured.c

    r609 r610  
    3636#include <openpgpsdk/hash.h> 
    3737#include <openpgpsdk/packet-parse.h> 
     38#include "parse_local.h" 
    3839 
    3940#include <string.h> 
     
    4243#include <openpgpsdk/final.h> 
    4344 
    44 //static int debug=0; 
     45static int debug=0; 
    4546 
    4647#define CRC24_POLY 0x1864cfbL 
     
    5758        AT_TRAILER_NAME, 
    5859        } state; 
     60 
     61    enum 
     62        { 
     63        NONE=0, 
     64        BEGIN_PGP_MESSAGE, 
     65        BEGIN_PGP_PUBLIC_KEY_BLOCK, 
     66        BEGIN_PGP_PRIVATE_KEY_BLOCK, 
     67        BEGIN_PGP_MULTI, 
     68        BEGIN_PGP_SIGNATURE, 
     69 
     70        END_PGP_MESSAGE, 
     71        END_PGP_PUBLIC_KEY_BLOCK, 
     72        END_PGP_PRIVATE_KEY_BLOCK, 
     73        END_PGP_MULTI, 
     74        END_PGP_SIGNATURE, 
     75 
     76        BEGIN_PGP_SIGNED_MESSAGE 
     77        } lastseen; 
     78 
    5979    ops_parse_info_t *parse_info; 
    6080    ops_boolean_t seen_nl:1; 
     
    7393                                                 strictly expect it */ 
    7494 
     95    // it is an error to get a cleartext message without a sig 
     96    ops_boolean_t expect_sig:1; 
     97    ops_boolean_t got_sig:1; 
     98 
    7599    // base64 stuff 
    76100    unsigned buffered; 
     
    101125    } 
    102126     
     127static void set_lastseen_headerline(dearmour_arg_t* arg, char* buf, ops_error_t **errors) 
     128    { 
     129    char* begin_msg="BEGIN PGP MESSAGE"; 
     130    char* begin_public="BEGIN PGP PUBLIC KEY BLOCK"; 
     131    char* begin_private="BEGIN PGP PRIVATE KEY BLOCK"; 
     132    char* begin_multi="BEGIN PGP MESSAGE, PART "; 
     133    char* begin_sig="BEGIN PGP SIGNATURE"; 
     134 
     135    char* end_msg="END PGP MESSAGE"; 
     136    char* end_public="END PGP PUBLIC KEY BLOCK"; 
     137    char* end_private="END PGP PRIVATE KEY BLOCK"; 
     138    char* end_multi="END PGP MESSAGE, PART "; 
     139    char* end_sig="END PGP SIGNATURE"; 
     140 
     141    char* begin_signed_msg="BEGIN PGP SIGNED MESSAGE"; 
     142 
     143    int prev=arg->lastseen; 
     144 
     145    if (!strncmp(buf,begin_msg,strlen(begin_msg))) 
     146        arg->lastseen=BEGIN_PGP_MESSAGE; 
     147    if (!strncmp(buf,begin_public,strlen(begin_public))) 
     148        arg->lastseen=BEGIN_PGP_PUBLIC_KEY_BLOCK; 
     149    if (!strncmp(buf,begin_private,strlen(begin_private))) 
     150        arg->lastseen=BEGIN_PGP_PRIVATE_KEY_BLOCK; 
     151    if (!strncmp(buf,begin_multi,strlen(begin_multi))) 
     152        arg->lastseen=BEGIN_PGP_MULTI; 
     153    if (!strncmp(buf,begin_sig,strlen(begin_sig))) 
     154        arg->lastseen=BEGIN_PGP_SIGNATURE; 
     155 
     156    if (!strncmp(buf,end_msg,strlen(end_msg))) 
     157        arg->lastseen=END_PGP_MESSAGE; 
     158    if (!strncmp(buf,end_public,strlen(end_public))) 
     159        arg->lastseen=END_PGP_PUBLIC_KEY_BLOCK; 
     160    if (!strncmp(buf,end_private,strlen(end_private))) 
     161        arg->lastseen=END_PGP_PRIVATE_KEY_BLOCK; 
     162    if (!strncmp(buf,end_multi,strlen(end_multi))) 
     163        arg->lastseen=END_PGP_MULTI; 
     164    if (!strncmp(buf,end_sig,strlen(end_sig))) 
     165        arg->lastseen=END_PGP_SIGNATURE; 
     166 
     167    if (!strncmp(buf,begin_signed_msg,strlen(begin_signed_msg))) 
     168        arg->lastseen=BEGIN_PGP_SIGNED_MESSAGE; 
     169 
     170    if (debug) 
     171        printf("set header: buf=%s, arg->lastseen=%d, prev=%d\n", buf, arg->lastseen, prev); 
     172 
     173    switch (arg->lastseen)  
     174        { 
     175    case NONE: 
     176        OPS_ERROR_1(errors,OPS_E_R_BAD_FORMAT,"Unrecognised Header Line %s", buf); 
     177        break; 
     178 
     179    case END_PGP_MESSAGE: 
     180        if (prev!=BEGIN_PGP_MESSAGE) 
     181            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Got END PGP MESSAGE, but not after BEGIN"); 
     182        break; 
     183 
     184    case END_PGP_PUBLIC_KEY_BLOCK: 
     185        if (prev!=BEGIN_PGP_PUBLIC_KEY_BLOCK) 
     186            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Got END PGP PUBLIC KEY BLOCK, but not after BEGIN"); 
     187        break; 
     188 
     189    case END_PGP_PRIVATE_KEY_BLOCK: 
     190        if (prev!=BEGIN_PGP_PRIVATE_KEY_BLOCK) 
     191            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Got END PGP PRIVATE KEY BLOCK, but not after BEGIN"); 
     192        break; 
     193 
     194    case BEGIN_PGP_MULTI: 
     195    case END_PGP_MULTI: 
     196            OPS_ERROR(errors,OPS_E_R_UNSUPPORTED,"Multi-part messages are not yet supported"); 
     197        break; 
     198 
     199    case END_PGP_SIGNATURE: 
     200        if (prev!=BEGIN_PGP_SIGNATURE) 
     201            OPS_ERROR(errors,OPS_E_R_BAD_FORMAT,"Got END PGP SIGNATURE, but not after BEGIN"); 
     202        break; 
     203 
     204    case BEGIN_PGP_MESSAGE: 
     205    case BEGIN_PGP_PUBLIC_KEY_BLOCK: 
     206    case BEGIN_PGP_PRIVATE_KEY_BLOCK: 
     207    case BEGIN_PGP_SIGNATURE: 
     208    case BEGIN_PGP_SIGNED_MESSAGE: 
     209        break; 
     210        } 
     211    } 
     212 
    103213static int read_char(dearmour_arg_t *arg,ops_error_t **errors, 
    104214                     ops_reader_info_t *rinfo, 
     
    308418                hash->add(hash,(unsigned char *)"\r",1); 
    309419            hash->add(hash,body->data,body->length); 
     420            if (debug) 
     421                { fprintf(stderr,"Got body:\n%s\n",body->data); } 
    310422            CB(cbinfo,OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY,&content); 
    311423            body->length=0; 
     
    316428        if(body->length == sizeof body->data) 
    317429            { 
     430            if (debug) 
     431                { fprintf(stderr,"Got body (2):\n%s\n",body->data); } 
    318432            CB(cbinfo,OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY,&content); 
    319433            body->length=0; 
     
    685799                 return -1; 
    686800 
     801             set_lastseen_headerline(arg,buf,errors); 
     802 
    687803             if(!strcmp(buf,"BEGIN PGP SIGNED MESSAGE")) 
    688804                 { 
    689805                 ops_dup_headers(&content.content.signed_cleartext_header.headers,&arg->headers); 
    690806                 CB(cbinfo,OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER,&content); 
    691  
    692807                 ret=process_dash_escaped(arg,errors,rinfo,cbinfo); 
    693808                 if(ret <= 0) 
     
    754869             buf[n]='\0'; 
    755870 
     871             set_lastseen_headerline(arg,buf,errors); 
     872 
    756873             /* Consume trailing '-' */ 
    757874             for(count=1 ; count < 5 ; ++count) 
     
    777894             if(!strncmp(buf,"BEGIN ",6)) 
    778895                 { 
     896                 set_lastseen_headerline(arg,buf,errors); 
    779897                 if((ret=parse_headers(arg,errors,rinfo,cbinfo)) <= 0) 
    780898                     return ret; 
     
    823941    arg->allow_trailing_whitespace=trailing_whitespace; 
    824942 
     943    arg->expect_sig=ops_false; 
     944    arg->got_sig=ops_false; 
     945 
    825946    ops_reader_push(parse_info,armoured_data_reader,armoured_data_destroyer,arg); 
    826947    } 
     
    831952void ops_reader_pop_dearmour(ops_parse_info_t *pinfo) 
    832953    { 
    833     //    dearmour_arg_t *arg=ops_reader_get_arg(ops_parse_get_rinfo(parse_info)); 
    834     //    free(arg); 
     954    dearmour_arg_t *arg=ops_reader_get_arg(ops_parse_get_rinfo(pinfo)); 
     955    free(arg); 
    835956    ops_reader_pop(pinfo); 
    836957    } 
  • openpgpsdk/trunk/src/lib/validate.c

    r608 r610  
    351351    const ops_keydata_t *signer; 
    352352    ops_boolean_t valid=ops_false; 
    353     //    unsigned len=0; 
    354     //    unsigned char *data=NULL; 
    355353    ops_memory_t* mem=NULL; 
    356354 
  • openpgpsdk/trunk/tests/test_rsa_verify.c

    r608 r610  
    8383                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.6 (GNU/Linux)\n", 
    8484                        // no signature 
    85                         "-----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                        "-----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", 
     86                        // no gap after armour headers in message 
     87                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.6 (GNU/Linux)\n\niJwEAQECAAYFAkiup4kACgkQr5tWFB2nA4mpVwP8DeeMDFrp7ICHYleyW/UmBIQH\ndXuviEA9WK/BUyHVKxLOyciAw18vm1rKJE9Q30GUrFkPvaOV6XZXZMDBXY/CQixT\nHjKRoFapgbzA5hqDeLjjkJ59hjS5jmsOrdyIebOVrF7YaSRji15uAeeIzBQ0lClZ\nupkvjuuc6o0RoS/+otk=\n=itEi\n-----END PGP SIGNATURE-----\n", 
     88                        // no gap after armour headers in signature 
     89                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.6 (GNU/Linux)\niJwEAQECAAYFAkiup4kACgkQr5tWFB2nA4mpVwP8DeeMDFrp7ICHYleyW/UmBIQH\ndXuviEA9WK/BUyHVKxLOyciAw18vm1rKJE9Q30GUrFkPvaOV6XZXZMDBXY/CQixT\nHjKRoFapgbzA5hqDeLjjkJ59hjS5jmsOrdyIebOVrF7YaSRji15uAeeIzBQ0lClZ\nupkvjuuc6o0RoS/+otk=\n=itEi\n-----END PGP SIGNATURE-----\n", 
     90                        // unsupported hash 
     91                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\nmessage to encrypt\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.6 (GNU/Linux)\n\niJwEAQECAAYFAkiup4kACgkQr5tWFB2nA4mpVwP8DeeMDFrp7ICHYleyW/UmBIQH\ndXuviEA9WK/BUyHVKxLOyciAw18vm1rKJE9Q30GUrFkPvaOV6XZXZMDBXY/CQixT\nHjKRoFapgbzA5hqDeLjjkJ59hjS5jmsOrdyIebOVrF7YaSRji15uAeeIzBQ0lClZ\nupkvjuuc6o0RoS/+otk=\n=itEi\n-----END PGP SIGNATURE-----\n", 
     92                        // missing BEGIN SIG 
     93                        "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nmessage to encrypt\n-----END PGP SIGNATURE-----", 
     94 
    8695    }; 
    8796    num_malformed=sizeof (malformed)/sizeof(char *); 
     
    296305 
    297306    rtn=ops_parse(pinfo); 
     307     
    298308 
    299309    if (debug) 
     
    351361    // handle result - should fail 
    352362    errstack=ops_parse_info_get_errors(pinfo); 
     363 
    353364    // we are expecting one and only one error 
    354365    // print out errors if we have actually got a different error