Changeset 507

Show
Ignore:
Timestamp:
09/20/07 11:03:06
Author:
rachel
Message:

Test RSA encryption with passphrase

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/tests/test_rsa_encrypt.c

    r506 r507  
    1212#include "tests.h" 
    1313 
    14 static char *filename_rsa_noarmour_singlekey="enc_rsa_noarmour_singlekey.txt"; 
     14static char *filename_rsa_noarmour_nopassphrase_singlekey="enc_rsa_noarmour_np_singlekey.txt"; 
     15static char *filename_rsa_noarmour_passphrase_singlekey="enc_rsa_noarmour_pp_singlekey.txt"; 
    1516static char *filename_rsa_armour_singlekey="enc_rsa_armour_singlekey.txt"; 
    1617 
     
    9394    // Create RSA test files 
    9495 
    95     create_testfile(filename_rsa_noarmour_singlekey); 
     96    create_testfile(filename_rsa_noarmour_nopassphrase_singlekey); 
     97    create_testfile(filename_rsa_noarmour_passphrase_singlekey); 
    9698    create_testfile(filename_rsa_armour_singlekey); 
    9799    /* 
     
    149151    } 
    150152 
    151 static void test_rsa_encrypt(const int has_armour, const ops_key_data_t *key, const char *filename) 
     153static void test_rsa_encrypt(const int has_armour, const ops_key_data_t *pub_key, const char *filename) 
    152154    { 
    153155    ops_memory_t *mem_ldt; 
     
    234236    // Create and write encrypted PK session key 
    235237 
    236     char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
    237     const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 
     238    //    char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
     239    //    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 
    238240    //    ops_print_public_key_verbose(pub_key); 
    239241 
     
    247249    iv=ops_mallocz(encrypt.blocksize); 
    248250    encrypt.set_iv(&encrypt, iv); 
    249     key=ops_mallocz(encrypt.keysize);  
     251    //key=ops_mallocz(encrypt.keysize);  
    250252    encrypt.set_key(&encrypt, &encrypted_pk_session_key->key[0]); 
    251253    ops_encrypt_init(&encrypt); 
     
    272274    // File contents should match - check with GPG 
    273275 
     276    char pp[MAXBUF]; 
     277    if (pub_key==alpha_pub_keydata) 
     278        pp[0]='\0'; 
     279    else if (pub_key==bravo_pub_keydata) 
     280        snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 
    274281    snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 
    275     snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s",decrypted_file, dir, encrypted_file); 
     282    snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 
    276283    //    printf("cmd: %s\n", cmd); 
    277284    rtn=system(cmd); 
     
    280287    } 
    281288 
    282 void test_rsa_encrypt_noarmour_singlekey(void) 
     289void test_rsa_encrypt_noarmour_nopassphrase_singlekey(void) 
    283290    { 
    284291    int armour=0; 
    285     char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
    286     assert(pub_keyring.nkeys); 
    287     const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 
    288     assert(pub_key); 
    289     test_rsa_encrypt(armour,pub_key,filename_rsa_noarmour_singlekey); 
     292    test_rsa_encrypt(armour,alpha_pub_keydata,filename_rsa_noarmour_nopassphrase_singlekey); 
     293    } 
     294 
     295void test_rsa_encrypt_noarmour_passphrase_singlekey(void) 
     296    { 
     297    int armour=0; 
     298    test_rsa_encrypt(armour,bravo_pub_keydata,filename_rsa_noarmour_passphrase_singlekey);   
    290299    } 
    291300 
     
    300309    } 
    301310 
    302 void test_rsa_encrypt_noarmour_passphrase(void) 
    303     { 
    304     int armour=0; 
    305     int passphrase=1; 
    306     test_rsa_encrypt(armour,passphrase,filename_rsa_noarmour_passphrase); 
    307     } 
    308  
    309311void test_rsa_encrypt_armour_passphrase(void) 
    310312    { 
     
    325327    // add tests to suite 
    326328     
    327     if (NULL == CU_add_test(suite, "Unarmoured, single key", test_rsa_encrypt_noarmour_singlekey)) 
     329    if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase", test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 
     330            return NULL; 
     331     
     332    if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase", test_rsa_encrypt_noarmour_passphrase_singlekey)) 
    328333            return NULL; 
    329334     
     
    332337            return NULL; 
    333338     
    334     if (NULL == CU_add_test(suite, "Unarmoured, passphrase", test_rsa_encrypt_noarmour_passphrase)) 
    335             return NULL; 
    336      
    337339    if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 
    338340            return NULL; 
  • openpgpsdk/trunk/tests/test_rsa_verify.c

    r504 r507  
    139139        { return 1; } 
    140140 
    141     snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --local-user %s --passphrase hello %s/%s", 
    142              dir, bravo_name, dir, filename_rsa_noarmour_passphrase); 
    143     if (system(cmd)) 
    144         { return 1; } 
    145  
    146     snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --armour --local-user %s --passphrase hello %s/%s", 
    147              dir, bravo_name, dir, filename_rsa_armour_passphrase); 
     141    snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --local-user %s --passphrase %s %s/%s", 
     142             dir, bravo_name, bravo_passphrase, dir, filename_rsa_noarmour_passphrase); 
     143    if (system(cmd)) 
     144        { return 1; } 
     145 
     146    snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --armour --local-user %s --passphrase %s %s/%s", 
     147             dir, bravo_name, bravo_passphrase, dir, filename_rsa_armour_passphrase); 
    148148    if (system(cmd)) 
    149149        { return 1; } 
  • openpgpsdk/trunk/tests/tests.c

    r506 r507  
    1919unsigned char* literal_data=NULL; 
    2020size_t sz_literal_data=0; 
     21 
    2122char *alpha_user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
    22 char *bravo_user_id="Bravo (RSA, passphrase) <bravo@test.com>"; 
    2323char *alpha_name="Alpha"; 
    24 char *bravo_name="Bravo"; 
    2524const ops_public_key_t *alpha_pkey; 
    2625const ops_secret_key_t *alpha_skey; 
     26const ops_key_data_t *alpha_pub_keydata; 
     27const ops_key_data_t *alpha_sec_keydata; 
     28char* alpha_passphrase=""; 
     29 
     30char *bravo_user_id="Bravo (RSA, passphrase) <bravo@test.com>"; 
     31char *bravo_name="Bravo"; 
    2732const ops_public_key_t *bravo_pkey; 
    2833const ops_secret_key_t *bravo_skey; 
     34const ops_key_data_t *bravo_pub_keydata; 
     35const ops_key_data_t *bravo_sec_keydata; 
    2936char* bravo_passphrase="hello"; 
     37 
    3038const ops_key_data_t *decrypter=NULL; 
    3139 
    3240void setup_test_keys() 
    3341    { 
    34     const ops_key_data_t* alpha_keydata; 
    35     const ops_key_data_t* bravo_keydata; 
    3642    char keydetails[MAXBUF+1]; 
    3743    char keyring_name[MAXBUF+1]; 
     
    98104    assert(pub_keyring.nkeys); 
    99105 
    100     alpha_keydata=ops_keyring_find_key_by_userid(&sec_keyring, alpha_user_id); 
    101     bravo_keydata=ops_keyring_find_key_by_userid(&sec_keyring, bravo_user_id); 
    102     assert(alpha_keydata); 
    103     assert(bravo_keydata); 
    104  
    105     alpha_pkey=ops_get_public_key_from_data(alpha_keydata); 
    106     alpha_skey=ops_get_secret_key_from_data(alpha_keydata); 
    107     bravo_pkey=ops_get_public_key_from_data(bravo_keydata); 
    108     bravo_skey=ops_decrypt_secret_key_from_data(bravo_keydata,bravo_passphrase); 
     106    alpha_pub_keydata=ops_keyring_find_key_by_userid(&pub_keyring, alpha_user_id); 
     107    bravo_pub_keydata=ops_keyring_find_key_by_userid(&pub_keyring, bravo_user_id); 
     108    assert(alpha_pub_keydata); 
     109    assert(bravo_pub_keydata); 
     110 
     111    alpha_sec_keydata=ops_keyring_find_key_by_userid(&sec_keyring, alpha_user_id); 
     112    bravo_sec_keydata=ops_keyring_find_key_by_userid(&sec_keyring, bravo_user_id); 
     113    assert(alpha_sec_keydata); 
     114    assert(bravo_sec_keydata); 
     115 
     116    alpha_pkey=ops_get_public_key_from_data(alpha_pub_keydata); 
     117    alpha_skey=ops_get_secret_key_from_data(alpha_sec_keydata); 
     118    bravo_pkey=ops_get_public_key_from_data(bravo_pub_keydata); 
     119    bravo_skey=ops_decrypt_secret_key_from_data(bravo_sec_keydata,bravo_passphrase); 
    109120 
    110121    assert(alpha_pkey); 
     
    295306 
    296307        // Do we need the passphrase and not have it? If so, get it 
    297         ops_parser_content_t pc; 
    298         char *passphrase; 
    299         memset(&pc,'\0',sizeof pc); 
     308        char *passphrase=NULL; 
    300309        passphrase=NULL; 
    301         pc.content.secret_key_passphrase.passphrase=&passphrase; 
    302         pc.content.secret_key_passphrase.secret_key=ops_get_secret_key_from_data(keydata); 
    303  
    304         /* Ugh. Need to duplicate this macro here to get the passphrase  
    305            Duplication to be removed when the callback gets moved to main code. 
    306            Can we make this inline code rather than a macro? 
    307         */ 
    308 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 
    309         CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 
    310          
     310 
     311        /* 
     312         * Hard-coded to allow automated test 
     313         */ 
     314        if (keydata==alpha_sec_keydata) 
     315            passphrase=alpha_passphrase; 
     316        else if (keydata==bravo_sec_keydata) 
     317            passphrase=bravo_passphrase; 
     318        else 
     319            assert(0); 
     320 
    311321        /* now get the key from the data */ 
    312322        secret=ops_get_secret_key_from_data(keydata); 
     
    315325            /* then it must be encrypted */ 
    316326            secret=ops_decrypt_secret_key_from_data(keydata,passphrase); 
    317             free(passphrase); 
    318327            } 
    319328         
     
    332341    { 
    333342    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
    334     /* 
    335     static const ops_key_data_t *decrypt_key; 
    336     const ops_key_data_t *keydata=NULL; 
    337     const ops_secret_key_t *secret; 
    338     */ 
    339343 
    340344    OPS_USED(cbinfo); 
  • openpgpsdk/trunk/tests/tests.h

    r505 r507  
    5454unsigned char* literal_data; 
    5555size_t sz_literal_data; 
     56 
     57// "Alpha" is the user who has NO passphrase on his key 
    5658char* alpha_user_id; 
    57 char* bravo_user_id; 
    5859char* alpha_name; 
     60const ops_key_data_t *alpha_pub_keydata; 
     61const ops_key_data_t *alpha_sec_keydata; 
     62const ops_public_key_t *alpha_pkey; 
     63const ops_secret_key_t *alpha_skey; 
     64char* alpha_passphrase; 
     65 
     66// "Bravo" is the user who has a passphrase on his key 
    5967char* bravo_name; 
    6068char* bravo_passphrase; 
    61 const ops_public_key_t *alpha_pkey; 
    62 const ops_secret_key_t *alpha_skey; 
     69char* bravo_user_id; 
     70const ops_key_data_t *bravo_pub_keydata; 
     71const ops_key_data_t *bravo_sec_keydata; 
    6372const ops_public_key_t *bravo_pkey; 
    6473const ops_secret_key_t *bravo_skey; 
    65 const ops_key_data_t *decrypter; 
     74//const ops_key_data_t *decrypter; 
    6675#endif 
    6776