Changeset 568
- Timestamp:
- 07/21/08 11:24:55
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/create.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/readerwriter.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/signature.h (modified) (1 diff)
- openpgpsdk/trunk/src/lib/adv_create.c (modified) (1 diff)
- openpgpsdk/trunk/src/lib/adv_readerwriter.c (modified) (2 diffs)
- openpgpsdk/trunk/src/lib/std_keyring.c (modified) (1 diff)
- openpgpsdk/trunk/src/lib/std_signature.c (modified) (5 diffs)
- openpgpsdk/trunk/tests/test_packet_types.c (modified) (6 diffs)
- openpgpsdk/trunk/tests/test_rsa_signature.c (modified) (3 diffs)
- openpgpsdk/trunk/tests/tests.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/create.h
r560 r568 106 106 107 107 ops_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 );108 int ops_write_file_from_buf(const char* filename, const char* buf, const size_t len, const ops_boolean_t overwrite); 109 109 110 110 ops_boolean_t ops_write_symmetrically_encrypted_data(const unsigned char *data, openpgpsdk/trunk/include/openpgpsdk/readerwriter.h
r567 r568 51 51 // memory reading 52 52 void ops_setup_memory_read(ops_parse_info_t **pinfo, ops_memory_t *mem, 53 void* arg, 53 54 ops_parse_cb_return_t callback(const ops_parser_content_t *, ops_parse_cb_info_t *)); 54 55 void ops_teardown_memory_read(ops_parse_info_t *pinfo, ops_memory_t *mem); openpgpsdk/trunk/include/openpgpsdk/signature.h
r567 r568 70 70 void ops_sign_buf_as_cleartext(const char* input, const size_t len, ops_memory_t** output, const ops_secret_key_t *skey); 71 71 void 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); 72 ops_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); 72 73 73 74 #endif openpgpsdk/trunk/src/lib/adv_create.c
r567 r568 1384 1384 } 1385 1385 1386 int ops_write_file_from_buf(const char *filename, const char* buf, const size_t len )1386 int ops_write_file_from_buf(const char *filename, const char* buf, const size_t len, const ops_boolean_t overwrite) 1387 1387 { 1388 1388 int fd=0; 1389 1389 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; 1391 1397 #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; 1395 1399 #endif 1400 fd=open(filename,flags, 0600); 1396 1401 if (fd < 0) 1397 1402 { openpgpsdk/trunk/src/lib/adv_readerwriter.c
r567 r568 39 39 40 40 void 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 *)) 42 43 { 43 44 /* … … 46 47 47 48 *pinfo=ops_parse_info_new(); 48 ops_parse_cb_set(*pinfo,callback, NULL);49 ops_parse_cb_set(*pinfo,callback,arg); 49 50 ops_reader_set_memory(*pinfo, 50 51 ops_memory_get_data(mem), openpgpsdk/trunk/src/lib/std_keyring.c
r567 r568 181 181 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 182 182 183 ops_setup_memory_read(&pinfo, mem, cb_keyring_read);183 ops_setup_memory_read(&pinfo, mem, NULL, cb_keyring_read); 184 184 185 185 ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_true); openpgpsdk/trunk/src/lib/std_signature.c
r567 r568 33 33 ops_create_info_t *cinfo=NULL; 34 34 unsigned char buf[MAXBUF]; 35 int flags=0;35 //int flags=0; 36 36 37 37 // open file to sign … … 47 47 } 48 48 49 // set up output file 49 50 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 66 53 // set up signature 67 54 sig=ops_create_signature_new(); 68 55 ops_signature_start_cleartext_signature(sig,skey,OPS_HASH_SHA1,OPS_SIG_BINARY); 69 70 // set up output file71 cinfo=ops_create_info_new();72 ops_writer_set_fd(cinfo,fd_out);73 56 ops_writer_push_clearsigned(cinfo,sig); 74 57 … … 92 75 ops_writer_switch_to_armoured_signature(cinfo); 93 76 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); 102 86 } 103 87 … … 141 125 142 126 ops_write_signature(sig,&skey->public_key,skey,cinfo); 127 128 // the calling function must free signed_cleartext 143 129 ops_writer_close(cinfo); 130 ops_create_info_delete(cinfo); 144 131 } 145 132 … … 231 218 } 232 219 220 ops_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 233 295 // EOF openpgpsdk/trunk/tests/test_packet_types.c
r567 r568 153 153 154 154 // 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); 156 156 157 157 // setup for writing parsed data to mem_out … … 202 202 203 203 // 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); 205 205 206 206 // setup for writing parsed data to 2nd mem … … 289 289 290 290 // 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); 292 292 293 293 // setup for writing parsed data to mem_out … … 348 348 349 349 // Read back and verify contents 350 ops_setup_memory_read(&pinfo,mem, callback_mdc);350 ops_setup_memory_read(&pinfo,mem,NULL,callback_mdc); 351 351 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 352 352 rtn=ops_parse(pinfo); … … 402 402 403 403 // 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); 405 405 406 406 // setup for writing parsed data to 2nd mem … … 455 455 456 456 // 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); 458 458 459 459 // read openpgpsdk/trunk/tests/test_rsa_signature.c
r567 r568 156 156 ops_memory_t *input=NULL; 157 157 ops_memory_t *output=NULL; 158 ops_boolean_t overwrite; 158 159 159 160 // setup filenames … … 171 172 172 173 // 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); 174 176 175 177 /* … … 336 338 } 337 339 340 static 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 338 395 static void test_rsa_signature_noarmour_nopassphrase(void) 339 396 { 397 unsigned char testdata[MAXBUF]; 340 398 int armour=0; 341 399 assert(pub_keyring.nkeys); 342 400 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); 343 403 } 344 404 345 405 static void test_rsa_signature_noarmour_passphrase(void) 346 406 { 407 unsigned char testdata[MAXBUF]; 347 408 int armour=0; 348 409 assert(pub_keyring.nkeys); 349 410 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); 350 414 } 351 415 352 416 static void test_rsa_signature_armour_nopassphrase(void) 353 417 { 418 unsigned char testdata[MAXBUF]; 354 419 int armour=1; 355 420 assert(pub_keyring.nkeys); 356 421 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); 357 425 } 358 426 359 427 static void test_rsa_signature_armour_passphrase(void) 360 428 { 429 unsigned char testdata[MAXBUF]; 430 361 431 int armour=1; 362 432 assert(pub_keyring.nkeys); 363 433 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); 364 437 } 365 438 openpgpsdk/trunk/tests/tests.c
r567 r568 24 24 return CU_get_error(); 25 25 } 26 26 #ifdef XXX 27 27 if (NULL == suite_crypto()) 28 28 { … … 51 51 return CU_get_error(); 52 52 } 53 53 #endif 54 54 if (NULL == suite_rsa_signature()) 55 55 { … … 58 58 return CU_get_error(); 59 59 } 60 60 #ifdef XXX 61 61 if (NULL == suite_rsa_verify()) 62 62 { … … 79 79 return CU_get_error(); 80 80 } 81 #endif 81 82 82 83 // Run tests
