Changeset 568

Show
Ignore:
Timestamp:
07/21/08 11:24:55
Author:
rachel
Message:

Implemented high-level function ops_sign_mem and corresponding test.

Files:

Legend:

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

    r560 r568  
    106106 
    107107ops_memory_t* ops_write_buf_from_file(const char *filename); 
    108 int ops_write_file_from_buf(const char* filename, const char* buf, const size_t len); 
     108int ops_write_file_from_buf(const char* filename, const char* buf, const size_t len, const ops_boolean_t overwrite); 
    109109 
    110110ops_boolean_t ops_write_symmetrically_encrypted_data(const unsigned char *data,  
  • openpgpsdk/trunk/include/openpgpsdk/readerwriter.h

    r567 r568  
    5151// memory reading 
    5252void ops_setup_memory_read(ops_parse_info_t **pinfo, ops_memory_t *mem, 
     53                           void* arg, 
    5354                              ops_parse_cb_return_t callback(const ops_parser_content_t *, ops_parse_cb_info_t *)); 
    5455void ops_teardown_memory_read(ops_parse_info_t *pinfo, ops_memory_t *mem); 
  • openpgpsdk/trunk/include/openpgpsdk/signature.h

    r567 r568  
    7070void ops_sign_buf_as_cleartext(const char* input, const size_t len, ops_memory_t** output, const ops_secret_key_t *skey); 
    7171void ops_sign_file(const char* input_filename, const char* output_filename, const ops_secret_key_t *skey, const ops_boolean_t use_armour, const ops_boolean_t overwrite); 
     72ops_memory_t * ops_sign_mem(const void* input, const int input_len, const ops_sig_type_t sig_type,  const ops_secret_key_t *skey, const ops_boolean_t use_armour); 
    7273 
    7374#endif 
  • openpgpsdk/trunk/src/lib/adv_create.c

    r567 r568  
    13841384    } 
    13851385 
    1386 int ops_write_file_from_buf(const char *filename, const char* buf, const size_t len
     1386int ops_write_file_from_buf(const char *filename, const char* buf, const size_t len, const ops_boolean_t overwrite
    13871387    { 
    13881388    int fd=0; 
    13891389    size_t n=0; 
    1390  
     1390    int flags=0; 
     1391 
     1392    flags=O_WRONLY | O_CREAT; 
     1393    if (overwrite==ops_true) 
     1394        flags |= O_TRUNC; 
     1395    else 
     1396        flags |= O_EXCL; 
    13911397#ifdef WIN32 
    1392     fd=open(filename,O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600); 
    1393 #else 
    1394     fd=open(filename,O_WRONLY | O_CREAT | O_EXCL, 0600); 
     1398    flags |= O_BINARY; 
    13951399#endif 
     1400    fd=open(filename,flags, 0600); 
    13961401    if (fd < 0) 
    13971402        { 
  • openpgpsdk/trunk/src/lib/adv_readerwriter.c

    r567 r568  
    3939 
    4040void ops_setup_memory_read(ops_parse_info_t **pinfo, ops_memory_t *mem, 
    41                               ops_parse_cb_return_t callback(const ops_parser_content_t *, ops_parse_cb_info_t *)) 
     41                           void* arg, 
     42                           ops_parse_cb_return_t callback(const ops_parser_content_t *, ops_parse_cb_info_t *)) 
    4243    { 
    4344    /* 
     
    4647 
    4748    *pinfo=ops_parse_info_new(); 
    48     ops_parse_cb_set(*pinfo,callback,NULL); 
     49    ops_parse_cb_set(*pinfo,callback,arg); 
    4950    ops_reader_set_memory(*pinfo, 
    5051                          ops_memory_get_data(mem), 
  • openpgpsdk/trunk/src/lib/std_keyring.c

    r567 r568  
    181181    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    182182 
    183     ops_setup_memory_read(&pinfo, mem, cb_keyring_read); 
     183    ops_setup_memory_read(&pinfo, mem, NULL, cb_keyring_read); 
    184184 
    185185    ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_true); 
  • openpgpsdk/trunk/src/lib/std_signature.c

    r567 r568  
    3333    ops_create_info_t *cinfo=NULL; 
    3434    unsigned char buf[MAXBUF]; 
    35     int flags=0; 
     35    //int flags=0; 
    3636 
    3737    // open file to sign 
     
    4747        } 
    4848     
     49    // set up output file 
    4950    snprintf(signed_file,sizeof signed_file,"%s.%s",filename,suffix); 
    50     flags=O_WRONLY | O_CREAT; 
    51     if (overwrite==ops_true) 
    52         flags |= O_TRUNC; 
    53     else 
    54         flags |= O_EXCL; 
    55 #ifdef WIN32 
    56     flags |= O_BINARY; 
    57 #endif 
    58  
    59     fd_out=open(signed_file,flags, 0600); 
    60     if(fd_out < 0) 
    61         { 
    62         perror(signed_file); 
    63         exit(2); 
    64         } 
    65      
     51    fd_out=ops_setup_file_write(&cinfo, signed_file, overwrite); 
     52 
    6653    // set up signature 
    6754    sig=ops_create_signature_new(); 
    6855    ops_signature_start_cleartext_signature(sig,skey,OPS_HASH_SHA1,OPS_SIG_BINARY); 
    69  
    70     // set up output file 
    71     cinfo=ops_create_info_new(); 
    72     ops_writer_set_fd(cinfo,fd_out);  
    7356    ops_writer_push_clearsigned(cinfo,sig); 
    7457 
     
    9275    ops_writer_switch_to_armoured_signature(cinfo); 
    9376 
    94     ops_signature_add_creation_time(sig,time(NULL)); 
    95     ops_keyid(keyid,&skey->public_key); 
    96     ops_signature_add_issuer_key_id(sig,keyid); 
    97     ops_signature_hashed_subpackets_end(sig); 
    98  
    99     ops_write_signature(sig,&skey->public_key,skey,cinfo); 
    100     ops_writer_close(cinfo); 
    101     close(fd_out); 
     77    // \todo creation time 
     78    ops_signature_add_creation_time(sig,time(NULL)); 
     79    ops_keyid(keyid,&skey->public_key); 
     80    ops_signature_add_issuer_key_id(sig,keyid); 
     81    ops_signature_hashed_subpackets_end(sig); 
     82 
     83    ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     84 
     85    ops_teardown_file_write(cinfo,fd_out); 
    10286    } 
    10387 
     
    141125 
    142126    ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     127 
     128    // the calling function must free signed_cleartext 
    143129    ops_writer_close(cinfo); 
     130    ops_create_info_delete(cinfo); 
    144131    } 
    145132 
     
    231218    } 
    232219 
     220ops_memory_t* ops_sign_mem(const void* input, const int input_len, const ops_sig_type_t sig_type, const ops_secret_key_t *skey, const ops_boolean_t use_armour) 
     221    { 
     222    // \todo allow choice of hash algorithams 
     223    // enforce use of SHA1 for now 
     224 
     225    unsigned char keyid[OPS_KEY_ID_SIZE]; 
     226    ops_create_signature_t *sig=NULL; 
     227 
     228    ops_create_info_t *cinfo=NULL; 
     229    ops_memory_t *mem=ops_memory_new(); 
     230 
     231    ops_hash_algorithm_t hash_alg=OPS_HASH_SHA1; 
     232    //    ops_sig_type_t sig_type=OPS_SIG_BINARY; 
     233    ops_literal_data_type_t ld_type; 
     234    ops_hash_t* hash=NULL; 
     235 
     236    // setup literal data packet type 
     237    if (sig_type==OPS_SIG_BINARY) 
     238        ld_type=OPS_LDT_BINARY; 
     239    else 
     240        ld_type=OPS_LDT_TEXT; 
     241 
     242    // set up signature 
     243    sig=ops_create_signature_new(); 
     244    ops_signature_start_message_signature(sig, skey, hash_alg, sig_type); 
     245 
     246    // setup writer 
     247    ops_setup_memory_write(&cinfo, &mem, input_len); 
     248 
     249    //  set armoured/not armoured here 
     250    if (use_armour) 
     251        ops_writer_push_armoured_message(cinfo); 
     252 
     253    if (debug) 
     254        { fprintf(stderr, "** Writing out one pass sig\n"); }  
     255 
     256    // write one_pass_sig 
     257    ops_write_one_pass_sig(skey, hash_alg, sig_type, cinfo); 
     258 
     259    // hash file contents 
     260    hash=ops_signature_get_hash(sig); 
     261    hash->add(hash, input, input_len); 
     262     
     263    // output file contents as Literal Data packet 
     264 
     265    if (debug) 
     266        { fprintf(stderr,"** Writing out data now\n"); } 
     267 
     268    ops_write_literal_data_from_buf(input, input_len, ld_type, cinfo); 
     269 
     270    if (debug) 
     271        { fprintf(stderr,"** After Writing out data now\n");} 
     272 
     273    // add subpackets to signature 
     274    // - creation time 
     275    // - key id 
     276 
     277    // \todo add creation time 
     278    ops_signature_add_creation_time(sig,time(NULL)); 
     279 
     280    ops_keyid(keyid,&skey->public_key); 
     281    ops_signature_add_issuer_key_id(sig,keyid); 
     282 
     283    ops_signature_hashed_subpackets_end(sig); 
     284 
     285    // write out sig 
     286    ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     287 
     288    // tidy up 
     289    ops_writer_close(cinfo); 
     290    ops_create_signature_delete(sig); 
     291 
     292    return mem; 
     293    } 
     294 
    233295// EOF 
  • openpgpsdk/trunk/tests/test_packet_types.c

    r567 r568  
    153153 
    154154    // setup for reading from this mem 
    155     ops_setup_memory_read(&pinfo,mem,callback_literal_data); 
     155    ops_setup_memory_read(&pinfo,mem,NULL,callback_literal_data); 
    156156 
    157157    // setup for writing parsed data to mem_out 
     
    202202 
    203203    // setup for reading from this mem 
    204     ops_setup_memory_read(&pinfo,mem,callback_literal_data); 
     204    ops_setup_memory_read(&pinfo,mem,NULL,callback_literal_data); 
    205205 
    206206    // setup for writing parsed data to 2nd mem 
     
    289289 
    290290    // setup for reading from this compressed packet 
    291     ops_setup_memory_read(&pinfo,mem_compress,callback_literal_data); 
     291    ops_setup_memory_read(&pinfo,mem_compress,NULL,callback_literal_data); 
    292292 
    293293    // setup for writing parsed data to mem_out 
     
    348348 
    349349        // Read back and verify contents 
    350         ops_setup_memory_read(&pinfo,mem,callback_mdc); 
     350        ops_setup_memory_read(&pinfo,mem,NULL,callback_mdc); 
    351351        ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    352352        rtn=ops_parse(pinfo); 
     
    402402 
    403403    // setup for reading from this mem 
    404     ops_setup_memory_read(&pinfo,mem,callback_se_ip_data); 
     404    ops_setup_memory_read(&pinfo,mem,NULL,callback_se_ip_data); 
    405405 
    406406    // setup for writing parsed data to 2nd mem 
     
    455455 
    456456    // setup for read 
    457     ops_setup_memory_read(&pinfo,mem,callback_encrypted_pk_session_key); 
     457    ops_setup_memory_read(&pinfo,mem,NULL,callback_encrypted_pk_session_key); 
    458458 
    459459    // read 
  • openpgpsdk/trunk/tests/test_rsa_signature.c

    r567 r568  
    156156    ops_memory_t *input=NULL; 
    157157    ops_memory_t *output=NULL; 
     158    ops_boolean_t overwrite; 
    158159 
    159160    // setup filenames  
     
    171172 
    172173    // write to file 
    173     ops_write_file_from_buf(signed_file, (const char*)ops_memory_get_data(output),ops_memory_get_length(output)); 
     174    overwrite=ops_true; 
     175    ops_write_file_from_buf(signed_file, (const char*)ops_memory_get_data(output),ops_memory_get_length(output),overwrite); 
    174176 
    175177    /* 
     
    336338    } 
    337339 
     340static void test_rsa_signature_sign_memory(const int use_armour, const void* input, const int input_len, const ops_secret_key_t *skey) 
     341    { 
     342    int rtn=0; 
     343    ops_memory_t* mem=NULL; 
     344    ops_parse_info_t *pinfo=NULL; 
     345    validate_data_cb_arg_t validate_arg; 
     346    ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); 
     347     
     348 
     349    // filenames 
     350 
     351    mem=ops_sign_mem(input, input_len, OPS_SIG_TEXT, skey, use_armour); 
     352 
     353    /* 
     354     * Validate output 
     355     */ 
     356 
     357    if (debug) 
     358        { 
     359        fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); 
     360        } 
     361     
     362    ops_write_file_from_buf("/tmp/memory.asc", ops_memory_get_data(mem), ops_memory_get_length(mem),ops_true); 
     363 
     364    // Set verification reader and handling options 
     365     
     366    ops_setup_memory_read(&pinfo, mem, &validate_arg, callback_verify); 
     367     
     368    memset(&validate_arg,'\0',sizeof validate_arg); 
     369    validate_arg.result=result; 
     370    validate_arg.keyring=&pub_keyring; 
     371    validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); 
     372     
     373    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
     374    pinfo->rinfo.accumulate=ops_true; 
     375     
     376    // Set up armour/passphrase options 
     377     
     378    if (use_armour) 
     379        ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); 
     380     
     381    // Do the verification 
     382     
     383    rtn=ops_parse_and_print_errors(pinfo); 
     384    CU_ASSERT(rtn==1); 
     385     
     386    // Tidy up 
     387    if (use_armour) 
     388        ops_reader_pop_dearmour(pinfo); 
     389     
     390    ops_parse_info_delete(pinfo); 
     391    ops_memory_free(mem); 
     392    ops_validate_result_free(result); 
     393    } 
     394 
    338395static void test_rsa_signature_noarmour_nopassphrase(void) 
    339396    { 
     397    unsigned char testdata[MAXBUF]; 
    340398    int armour=0; 
    341399    assert(pub_keyring.nkeys); 
    342400    test_rsa_signature_sign(armour,filename_rsa_noarmour_nopassphrase, alpha_skey); 
     401    create_testdata("test_rsa_signature_noarmour_nopassphrase",testdata, MAXBUF); 
     402    test_rsa_signature_sign_memory(armour,testdata,MAXBUF, alpha_skey); 
    343403    } 
    344404 
    345405static void test_rsa_signature_noarmour_passphrase(void) 
    346406    { 
     407    unsigned char testdata[MAXBUF]; 
    347408    int armour=0; 
    348409    assert(pub_keyring.nkeys); 
    349410    test_rsa_signature_sign(armour,filename_rsa_noarmour_passphrase, bravo_skey); 
     411 
     412    create_testdata("test_rsa_signature_noarmour_passphrase",testdata, MAXBUF); 
     413    test_rsa_signature_sign_memory(armour,testdata,MAXBUF, bravo_skey); 
    350414    } 
    351415 
    352416static void test_rsa_signature_armour_nopassphrase(void) 
    353417    { 
     418    unsigned char testdata[MAXBUF]; 
    354419    int armour=1; 
    355420    assert(pub_keyring.nkeys); 
    356421    test_rsa_signature_sign(armour,filename_rsa_armour_nopassphrase, alpha_skey); 
     422 
     423    create_testdata("test_rsa_signature_armour_nopassphrase",testdata, MAXBUF); 
     424    test_rsa_signature_sign_memory(armour,testdata,MAXBUF, alpha_skey); 
    357425    } 
    358426 
    359427static void test_rsa_signature_armour_passphrase(void) 
    360428    { 
     429    unsigned char testdata[MAXBUF]; 
     430 
    361431    int armour=1; 
    362432    assert(pub_keyring.nkeys); 
    363433    test_rsa_signature_sign(armour,filename_rsa_armour_passphrase, bravo_skey); 
     434 
     435    create_testdata("test_rsa_signature_armour_passphrase",testdata, MAXBUF); 
     436    test_rsa_signature_sign_memory(armour,testdata,MAXBUF, bravo_skey); 
    364437    } 
    365438 
  • openpgpsdk/trunk/tests/tests.c

    r567 r568  
    2424        return CU_get_error(); 
    2525        } 
    26  
     26#ifdef XXX 
    2727    if (NULL == suite_crypto()) 
    2828        { 
     
    5151        return CU_get_error(); 
    5252        } 
    53  
     53#endif 
    5454    if (NULL == suite_rsa_signature())  
    5555        { 
     
    5858        return CU_get_error(); 
    5959        } 
    60  
     60#ifdef XXX 
    6161    if (NULL == suite_rsa_verify())  
    6262        { 
     
    7979        return CU_get_error(); 
    8080        } 
     81#endif 
    8182 
    8283    // Run tests