Changeset 466

Show
Ignore:
Timestamp:
04/11/07 17:30:49
Author:
rachel
Message:

More RSA decryption tests.
Decryption with supplied passphrase now working in tests.

Files:

Legend:

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

    r447 r466  
    3939void ops_keyring_read(ops_keyring_t *keyring,const char *file); 
    4040 
     41char *ops_malloc_passphrase(char *passphrase); 
    4142char *ops_get_passphrase(void); 
    4243 
  • openpgpsdk/trunk/src/advanced/adv_keyring.c

    r447 r466  
    8888    } 
    8989 
    90 char *ops_get_passphrase(void) 
    91     { 
    92     char buffer[1024]; 
     90char *ops_malloc_passphrase(char *pp) 
     91    { 
    9392    char *passphrase; 
    9493    size_t n; 
    9594 
     95    n=strlen(pp); 
     96    passphrase=malloc(n+1); 
     97    strcpy(passphrase,pp); 
     98 
     99    return passphrase; 
     100    } 
     101 
     102char *ops_get_passphrase(void) 
     103    { 
     104    char buffer[1024]; 
     105    size_t n; 
     106 
    96107    printf("Passphrase: "); 
    97  
     108     
    98109    echo_off(); 
    99110    fgets(buffer,sizeof buffer,stdin); 
     
    105116    if(n && buffer[n-1] == '\n') 
    106117        buffer[--n]='\0'; 
    107     passphrase=malloc(n+1); 
    108     strcpy(passphrase,buffer); 
    109  
    110     return passphrase; 
     118    return ops_malloc_passphrase(buffer); 
    111119    } 
    112120 
     
    131139    case OPS_PTAG_CT_USER_ID: 
    132140    case OPS_PTAG_CT_SIGNATURE: 
     141    case OPS_PTAG_CT_SIGNATURE_HEADER: 
     142    case OPS_PTAG_CT_SIGNATURE_FOOTER: 
    133143    case OPS_PTAG_CT_TRUST: 
    134144        break; 
  • openpgpsdk/trunk/tests/tests.c

    r464 r466  
    1717#include "openpgpsdk/std_print.h" 
    1818 
     19/*  
     20These include files are needed by callback. 
     21To be removed when callback gets added to main body of code 
     22*/ 
     23#include "../src/advanced/parse_local.h" 
     24#include "../src/advanced/keyring_local.h" 
     25 
    1926#define MAXBUF 128 
    2027static char secring[MAXBUF+1]; 
    2128static char dir[MAXBUF+1]; 
    22 static char file[MAXBUF+1]; 
    2329static char keydetails[MAXBUF+1]; 
    2430static ops_keyring_t keyring; 
    25 static char* testtxt="Hello World\n"; 
     31static char *filename_rsa_noarmour_nopassphrase="rsa_noarmour_nopassphrase.txt"; 
     32static char *filename_rsa_armour_nopassphrase="rsa_armour_nopassphrase.txt"; 
     33static char *filename_rsa_noarmour_passphrase="rsa_noarmour_passphrase.txt"; 
     34static char *filename_rsa_armour_passphrase="rsa_armour_passphrase.txt"; 
     35static char *nopassphrase=""; 
     36static char *passphrase="hello"; 
     37static char *current_passphrase=NULL; 
     38 
    2639static char* text; 
     40 
     41static char *create_testtext(const char *filename) 
     42    { 
     43    static char buffer[MAXBUF+1]; 
     44    snprintf(buffer,MAXBUF,"Hello world : %s/%s\n", dir, filename); 
     45    return &buffer[0]; 
     46    } 
     47 
     48static int create_testfile(const char *name) 
     49    { 
     50    char filename[MAXBUF+1]; 
     51    char buffer[MAXBUF+1]; 
     52 
     53    int fd=0; 
     54    snprintf(filename,MAXBUF,"%s/%s",dir,name); 
     55    if ((fd=open(filename,O_WRONLY| O_CREAT | O_EXCL, 0600))<0) 
     56        return 0; 
     57 
     58    snprintf(buffer,MAXBUF,create_testtext(name)); 
     59    write(fd,buffer,strlen(buffer)); 
     60    close(fd); 
     61    return 1; 
     62    } 
    2763 
    2864static ops_parse_cb_return_t 
     
    3268    static ops_boolean_t skipping; 
    3369    static const ops_key_data_t *decrypter; 
    34     const ops_key_data_t *key=NULL; 
     70    const ops_key_data_t *keydata=NULL; 
     71    const ops_secret_key_t *secret; 
    3572 
    3673    OPS_USED(cbinfo); 
     
    69106 
    70107    case OPS_PARSER_CMD_GET_SECRET_KEY: 
    71         key=ops_keyring_find_key_by_id(&keyring,content->get_secret_key.pk_session_key->key_id); 
    72         if (!key || !ops_key_is_secret(key)) 
     108        keydata=ops_keyring_find_key_by_id(&keyring,content->get_secret_key.pk_session_key->key_id); 
     109        if (!keydata || !ops_key_is_secret(keydata)) 
    73110            return 0; 
    74111 
    75         ops_set_secret_key(content,key); 
    76  
     112        //      ops_set_secret_key(content,keydata); 
     113 
     114        // Do we need the passphrase and not have it? If so, get it 
     115        ops_parser_content_t pc; 
     116        char *passphrase; 
     117        memset(&pc,'\0',sizeof pc); 
     118        passphrase=NULL; 
     119        pc.content.secret_key_passphrase.passphrase=&passphrase; 
     120        pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey); 
     121 
     122        /* Ugh. Need to duplicate this macro here to get the passphrase  
     123           Duplication to be removed when the callback gets moved to main code. 
     124           Can we make this inline code rather than a macro? 
     125        */ 
     126#define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 
     127        CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 
     128         
     129        /* now get the key from the data */ 
     130        secret=ops_get_secret_key_from_data(keydata); 
     131        while(!secret) 
     132            { 
     133            /* then it must be encrypted */ 
     134            secret=ops_decrypt_secret_key_from_data(keydata,passphrase); 
     135            free(passphrase); 
     136            } 
     137 
     138        *content->get_secret_key.secret_key=secret; 
     139         
     140        break; 
     141 
     142    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
     143        /* 
     144          Doing this so the test can be automated. 
     145          Will move this into separate stacked callback later 
     146        */ 
     147        *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(current_passphrase); 
     148        return OPS_KEEP_MEMORY; 
    77149        break; 
    78150 
     
    135207int init_suite_rsa_decrypt(void) 
    136208    { 
    137     char *textfile="testfile.txt"; 
    138209    int fd=0; 
     210    char cmd[MAXBUF+1]; 
     211    char *rsa_nopass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Alpha\nName-Comment: RSA, no passphrase\nName-Email: alpha@test.com\nKey-Length: 1024\n"; 
     212    char *rsa_pass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Bravo\nName-Comment: RSA, passphrase\nName-Email: bravo@test.com\nPassphrase: hello\nKey-Length: 1024\n"; 
    139213     
    140214    // Create temp directory 
     
    142216        return 1; 
    143217 
    144         //    printf("creating new file\n"); 
    145     // Create a new unencrypted test file 
    146     snprintf(file,MAXBUF,"%s/%s",dir,textfile); 
    147  
    148     if ((fd=open(file,O_WRONLY| O_CREAT | O_EXCL, 0600))<0) 
    149        return 1
    150     write(fd,testtxt,strlen(testtxt)); 
    151     close(fd); 
    152  
    153     // create new keyrings in that directory 
    154     // and a new RSA keypair with no passphrase 
    155  
    156     snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails"); 
     218    // Create RSA test files 
     219 
     220    create_testfile(filename_rsa_noarmour_nopassphrase); 
     221    create_testfile(filename_rsa_armour_nopassphrase); 
     222    create_testfile(filename_rsa_noarmour_passphrase); 
     223    create_testfile(filename_rsa_armour_passphrase)
     224 
     225    /* 
     226     * Create a RSA keypair with no passphrase 
     227    */ 
     228 
     229    snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 
     230 
    157231    if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
    158232        { 
     
    161235        } 
    162236 
    163     char *rsa_nopass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Alpha\nName-Comment: RSA, no passphrase\nName-Email: alpha@test.com\nKey-Length: 1024\n"; 
    164237    write(fd,rsa_nopass,strlen(rsa_nopass)); 
    165238    close(fd); 
    166239 
    167     char cmd[MAXBUF+1]; 
    168     snprintf(cmd,MAXBUF,"gpg --gen-key --expert --homedir=%s --batch %s 2>&1 > /dev/null",dir,keydetails); 
    169     //printf("cmd: %s\n", cmd); 
     240    snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
    170241    system(cmd); 
    171242 
    172243    // Now encrypt the test file with GPG 
    173     snprintf(cmd,MAXBUF,"gpg --encrypt --homedir=%s --recipient Alpha %s 2>&1 > /dev/null", dir, file); 
     244    snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase); 
    174245    if (system(cmd)) 
    175246        { 
     
    178249 
    179250    // Now encrypt and ascii-armour the test file with GPG 
    180     snprintf(cmd,MAXBUF,"gpg --encrypt --armor --homedir=%s --recipient Alpha %s 2>&1 > /dev/null", dir, file); 
     251    snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_armour_nopassphrase); 
    181252    if (system(cmd)) 
    182253        { 
    183254        return 1; 
    184255        } 
    185  
     256     
     257    /* 
     258     * Create a RSA keypair with passphrase 
     259     */ 
     260 
     261    snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.bravo"); 
     262    if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
     263        { 
     264        fprintf(stderr,"Can't create key details\n"); 
     265        return 1; 
     266        } 
     267 
     268    write(fd,rsa_pass,strlen(rsa_pass)); 
     269    close(fd); 
     270 
     271    snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
     272    system(cmd); 
     273 
     274    // Now encrypt the test file with GPG 
     275    snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_noarmour_passphrase); 
     276    if (system(cmd)) 
     277        { 
     278        return 1; 
     279        } 
     280 
     281    // Now encrypt and ascii-armour the test file with GPG 
     282    snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_armour_passphrase); 
     283    if (system(cmd)) 
     284        { 
     285        return 1; 
     286        } 
     287 
     288    // Initialise OPS  
     289    ops_init(); 
     290 
     291    // read keyring 
    186292    snprintf(secring,MAXBUF,"%s/secring.gpg", dir); 
    187  
    188     // Initialise OPS and read keyring 
    189     ops_init(); 
    190293    ops_keyring_read(&keyring,secring); 
    191294 
     
    198301    char cmd[MAXBUF+1]; 
    199302         
    200        /* Close OPS */ 
    201  
     303    /* Close OPS */ 
     304     
    202305    ops_keyring_free(&keyring); 
    203306    ops_finish(); 
    204307 
    205        /* Remove test dir and files */ 
     308    /* Remove test dir and files */ 
    206309    snprintf(cmd,MAXBUF,"rm -rf %s", dir); 
    207310    if (system(cmd)) 
     
    210313        return 1; 
    211314        } 
    212  
     315     
    213316    return 0; 
    214317    } 
    215318 
    216 void test_rsa_decryption_unarmoured_nopassphrase(void) 
    217     { 
    218         /* 
    219          * TEST: RSA encrypted file, unarmoured, no passphrase 
    220          */ 
    221  
     319static void test_rsa_decrypt(const int has_armour, const int has_passphrase, const char *filename) 
     320    { 
    222321    char encfile[MAXBUF+1]; 
     322    char *suffix= has_armour ? "asc" : "gpg"; 
    223323    int fd=0; 
    224324    ops_parse_info_t *pinfo; 
    225  
    226     // read encrypted file 
    227     snprintf(encfile,MAXBUF,"%s.gpg", file); 
     325     
     326    // open encrypted file 
     327    snprintf(encfile,MAXBUF,"%s/%s.%s",dir,filename,suffix); 
    228328    fd=open(encfile,O_RDONLY); 
    229329    if(fd < 0) 
     
    232332        exit(2); 
    233333        } 
    234  
    235     // Now do file 
     334     
     335    // Set decryption reader and handling options 
     336 
    236337    pinfo=ops_parse_info_new(); 
    237338    ops_reader_set_fd(pinfo,fd); 
    238339    ops_parse_cb_set(pinfo,callback,NULL); 
    239340 
     341    // Set up armour/passphrase options 
     342 
     343    if (has_armour) 
     344        ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); 
     345    current_passphrase=has_passphrase ? passphrase : nopassphrase; 
     346     
     347    // Do the decryption 
     348 
    240349    ops_parse(pinfo); 
    241350 
    242         close(fd); 
    243  
     351    // Tidy up 
     352    if (has_armour) 
     353        ops_reader_pop_dearmour(pinfo); 
     354 
     355    close(fd); 
     356     
    244357    // File contents should match 
    245     CU_ASSERT(strcmp(text,testtxt)==0); 
    246  
    247     } 
    248  
    249 void test_rsa_decryption_armoured_nopassphrase(void) 
    250     { 
    251         /* 
    252          * TEST: RSA encrypted file, armoured, no passphrase 
    253          */ 
    254  
    255     char encfile[MAXBUF+1]; 
    256     int fd=0; 
    257     ops_parse_info_t *pinfo; 
    258  
    259     // read encrypted file 
    260     snprintf(encfile,MAXBUF,"%s.asc", file); 
    261     fd=open(encfile,O_RDONLY); 
    262     if(fd < 0) 
    263         { 
    264         perror(encfile); 
    265         exit(2); 
    266         } 
    267  
    268     // Now do file 
    269     pinfo=ops_parse_info_new(); 
    270     ops_reader_set_fd(pinfo,fd); 
    271     ops_parse_cb_set(pinfo,callback,NULL); 
    272  
    273         ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); 
    274     ops_parse(pinfo); 
    275         ops_reader_pop_dearmour(pinfo); 
    276  
    277         close(fd); 
    278  
    279     // File contents should match 
    280     CU_ASSERT(strcmp(text,testtxt)==0); 
    281  
     358    CU_ASSERT(strcmp(text,create_testtext(filename))==0); 
     359    } 
     360 
     361void test_rsa_decrypt_noarmour_nopassphrase(void) 
     362    { 
     363    int armour=0; 
     364    int passphrase=0; 
     365    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase); 
     366    } 
     367 
     368void test_rsa_decrypt_armour_nopassphrase(void) 
     369    { 
     370    int armour=1; 
     371    int passphrase=0; 
     372    test_rsa_decrypt(armour,passphrase,filename_rsa_armour_nopassphrase); 
     373    } 
     374 
     375void test_rsa_decrypt_noarmour_passphrase(void) 
     376    { 
     377    int armour=0; 
     378    int passphrase=1; 
     379    test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_passphrase); 
     380    } 
     381 
     382void test_rsa_decrypt_armour_passphrase(void) 
     383    { 
     384    int armour=1; 
     385    int passphrase=1; 
     386    test_rsa_decrypt(armour,passphrase,filename_rsa_armour_passphrase); 
    282387    } 
    283388 
     
    295400        return CU_get_error(); 
    296401        } 
    297  
     402     
    298403    // add tests to suite 
    299  
    300     if (NULL == CU_add_test(pSuite, "Unarmoured, no passphrase", test_rsa_decryption_unarmoured_nopassphrase)) 
     404     
     405    if (NULL == CU_add_test(pSuite, "Unarmoured, no passphrase", test_rsa_decrypt_noarmour_nopassphrase)) 
    301406        { 
    302407        CU_cleanup_registry(); 
    303408        return CU_get_error(); 
    304409        } 
    305  
    306     if (NULL == CU_add_test(pSuite, "Armoured, no passphrase", test_rsa_decryption_armoured_nopassphrase)) 
     410     
     411    if (NULL == CU_add_test(pSuite, "Armoured, no passphrase", test_rsa_decrypt_armour_nopassphrase)) 
    307412        { 
    308413        CU_cleanup_registry(); 
    309414        return CU_get_error(); 
    310415        } 
    311  
     416     
     417    if (NULL == CU_add_test(pSuite, "Unarmoured, passphrase", test_rsa_decrypt_noarmour_passphrase)) 
     418        { 
     419        CU_cleanup_registry(); 
     420        return CU_get_error(); 
     421        } 
     422     
     423    if (NULL == CU_add_test(pSuite, "Armoured, passphrase", test_rsa_decrypt_armour_passphrase)) 
     424        { 
     425        CU_cleanup_registry(); 
     426        return CU_get_error(); 
     427        } 
     428     
    312429    // Run tests 
    313430    CU_basic_set_mode(CU_BRM_VERBOSE);