Changeset 307
- Timestamp:
- 11/25/05 14:44:20
- Files:
-
- openpgpsdk/trunk/examples/Makefile.template (modified) (2 diffs)
- openpgpsdk/trunk/examples/create-key.c (modified) (2 diffs)
- openpgpsdk/trunk/examples/create-signed-key.c (modified) (3 diffs)
- openpgpsdk/trunk/examples/packet-dump.c (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/create.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/errors.h (modified) (3 diffs)
- openpgpsdk/trunk/include/openpgpsdk/memory.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/packet.h (modified) (4 diffs)
- openpgpsdk/trunk/include/openpgpsdk/signature.h (modified) (2 diffs)
- openpgpsdk/trunk/include/openpgpsdk/types.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/util.h (modified) (2 diffs)
- openpgpsdk/trunk/src/armour.c (modified) (1 diff)
- openpgpsdk/trunk/src/create.c (modified) (4 diffs)
- openpgpsdk/trunk/src/errors.c (modified) (3 diffs)
- openpgpsdk/trunk/src/memory.c (modified) (1 diff)
- openpgpsdk/trunk/src/packet-parse.c (modified) (2 diffs)
- openpgpsdk/trunk/src/signature.c (modified) (12 diffs)
- openpgpsdk/trunk/src/util.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/examples/Makefile.template
r305 r307 7 7 LIBDEPS=common.o ../src/libops.a 8 8 LIBS=$(LIBDEPS) %CRYPTO_LIBS% %ZLIB% $(DM_LIB) %LIBS% 9 EXES=packet-dump verify create-key create-signed-key verify2 sign-detached 9 10 10 all: Makefile .depend packet-dump verify create-key create-signed-key \ 11 verify2 sign-detached 11 all: Makefile .depend $(EXES) 12 12 13 13 test: test-dump test-verify test-verify-armoured test-create-key … … 39 39 40 40 clean: 41 rm -f packet-dump verify create-key*.o41 rm -f $(EXES) *.o 42 42 rm -f TAGS 43 43 openpgpsdk/trunk/examples/create-key.c
r301 r307 5 5 int main(int argc,char **argv) 6 6 { 7 ops_writer_fd_arg_t arg; 8 ops_create_info_t info; 7 ops_create_info_t *info; 9 8 const unsigned char *id; 10 9 const char *nstr; … … 26 25 BN_hex2bn(&e,estr); 27 26 28 arg.fd=1; 29 info.writer=ops_writer_fd; 30 info.arg=&arg; 27 info=ops_create_info_new(); 28 ops_create_info_set_writer_fd(info,1); 31 29 32 ops_write_rsa_public_key(time(NULL),n,e,&info); 33 ops_write_user_id(id,&info); 30 ops_write_rsa_public_key(time(NULL),n,e,info); 31 ops_write_user_id(id,info); 32 33 ops_create_info_delete(info); 34 34 35 35 return 0; openpgpsdk/trunk/examples/create-signed-key.c
r304 r307 20 20 int main(int argc,char **argv) 21 21 { 22 ops_writer_fd_arg_t arg; 23 ops_create_info_t info; 24 ops_create_signature_t sig; 22 ops_create_info_t *info; 23 ops_create_signature_t *sig; 25 24 ops_user_id_t id; 26 25 unsigned char keyid[OPS_KEY_ID_SIZE]; … … 31 30 const char *pubfile; 32 31 BIGNUM *f4; 32 int fd; 33 33 34 34 if(argc != 4) … … 51 51 ops_init(); 52 52 53 info=ops_create_info_new(); 54 53 55 // OpenSSL has p and q reversed relative to OpenPGP 54 56 ops_fast_create_rsa_secret_key(&skey,time(NULL),rsa->q,rsa->p,rsa->d, 55 57 rsa->iqmp,rsa->n,rsa->e); 56 58 57 skey.key.rsa.p=rsa->q; 58 skey.key.rsa.q=rsa->p; 59 skey.key.rsa.d=rsa->d; 60 skey.key.rsa.u=rsa->iqmp; 61 62 arg.fd=open(secfile,O_CREAT|O_TRUNC|O_WRONLY,0666); 63 if(arg.fd < 0) 59 fd=open(secfile,O_CREAT|O_TRUNC|O_WRONLY,0666); 60 if(fd < 0) 64 61 { 65 62 perror(secfile); 66 63 exit(2); 67 64 } 65 ops_create_info_set_writer_fd(info,fd); 68 66 69 info.writer=ops_writer_fd; 70 info.arg=&arg; 67 ops_write_struct_secret_key(&skey,info); 71 68 72 ops_ write_struct_secret_key(&skey,&info);69 ops_create_info_close_writer(info); 73 70 74 close( arg.fd);71 close(fd); 75 72 76 arg.fd=open(pubfile,O_CREAT|O_TRUNC|O_WRONLY,0666);77 if( arg.fd < 0)73 fd=open(pubfile,O_CREAT|O_TRUNC|O_WRONLY,0666); 74 if(fd < 0) 78 75 { 79 76 perror(pubfile); 80 77 exit(2); 81 78 } 79 ops_create_info_set_writer_fd(info,fd); 82 80 83 ops_write_struct_public_key(&skey.public_key, &info);81 ops_write_struct_public_key(&skey.public_key,info); 84 82 85 83 ops_fast_create_user_id(&id,user_id); 86 ops_write_struct_user_id(&id, &info);84 ops_write_struct_user_id(&id,info); 87 85 88 ops_signature_start_key_signature(&sig,&skey.public_key,&id, 86 sig=ops_create_signature_new(); 87 88 ops_signature_start_key_signature(sig,&skey.public_key,&id, 89 89 OPS_CERT_POSITIVE); 90 ops_signature_add_creation_time( &sig,time(NULL));90 ops_signature_add_creation_time(sig,time(NULL)); 91 91 92 92 ops_keyid(keyid,&skey.public_key); 93 ops_signature_add_issuer_key_id( &sig,keyid);93 ops_signature_add_issuer_key_id(sig,keyid); 94 94 95 ops_signature_add_primary_user_id( &sig,ops_true);95 ops_signature_add_primary_user_id(sig,ops_true); 96 96 97 ops_signature_hashed_subpackets_end( &sig);97 ops_signature_hashed_subpackets_end(sig); 98 98 99 ops_write_signature(&sig,&skey.public_key,&skey,&info); 99 ops_write_signature(sig,&skey.public_key,&skey,info); 100 101 ops_create_signature_delete(sig); 102 ops_create_info_delete(info); 103 104 close(fd); 105 106 RSA_free(rsa); 107 BN_free(f4); 100 108 101 109 ops_finish(); 102 110 103 close(arg.fd);104 105 111 return 0; 106 112 } openpgpsdk/trunk/examples/packet-dump.c
r306 r307 231 231 static void print_string_and_value( char *name, char *str, unsigned char value) 232 232 { 233 234 233 print_name(name); 235 234 openpgpsdk/trunk/include/openpgpsdk/create.h
r304 r307 20 20 }; 21 21 22 struct ops_create_info;23 22 /** 24 23 * \ingroup Create 25 24 * the writer function prototype 26 25 */ 27 typedef ops_writer_ret_t ops_packet_writer_t(const unsigned char *src, 28 unsigned length, 29 ops_writer_flags_t flags, 30 struct ops_create_info *create_info); 26 typedef ops_writer_ret_t ops_writer_t(const unsigned char *src, 27 unsigned length, 28 ops_writer_flags_t flags, 29 ops_error_t **errors, 30 void *arg); 31 31 32 /** 33 * \ingroup Create 34 * This struct contains the required information about how to write 35 */ 36 struct ops_create_info 37 { 38 ops_packet_writer_t *writer; /*!< the writer function */ 39 void *arg; /*!< arguments for the writer function */ 40 ops_error_t * errors; /*!< an error stack */ 41 }; 42 /** 43 * \ingroup Create 44 * Contains the required information about how to write 45 */ 46 typedef struct ops_create_info ops_create_info_t; 32 typedef void ops_writer_destroyer_t(void *arg); 33 34 void ops_create_info_set_writer(ops_create_info_t *info, 35 ops_writer_t *writer, 36 ops_writer_destroyer_t *destroyer, 37 void *arg); 38 39 ops_create_info_t *ops_create_info_new(void); 40 void ops_create_info_delete(ops_create_info_t *info); 41 void ops_create_info_set_writer_fd(ops_create_info_t *info,int fd); 42 void ops_create_info_close_writer(ops_create_info_t *info); 47 43 48 44 ops_boolean_t ops_write(const void *src,unsigned length, openpgpsdk/trunk/include/openpgpsdk/errors.h
r301 r307 6 6 7 7 #include "openpgpsdk/types.h" 8 #include <errno.h> 8 9 9 10 /** error codes */ … … 39 40 { 40 41 ops_errcode_t errcode; 41 int errno;/*!< irrelevent unless errcode == OPS_E_SYSTEM_ERROR */42 int sys_errno; /*!< irrelevent unless errcode == OPS_E_SYSTEM_ERROR */ 42 43 char *comment; 43 44 const char *file; … … 48 49 char *ops_errcode(const ops_errcode_t errcode); 49 50 50 void push_error(ops_error_t **errstack,ops_errcode_t errcode,int errno,51 void push_error(ops_error_t **errstack,ops_errcode_t errcode,int sys_errno, 51 52 const char *file,int line,const char *comment,...); 52 53 void print_error(ops_error_t *err); openpgpsdk/trunk/include/openpgpsdk/memory.h
r301 r307 26 26 void ops_memory_release(ops_memory_t *mem); 27 27 28 struct ops_create_info; 29 ops_writer_ret_t ops_writer_memory(const unsigned char *src,unsigned length, 30 ops_writer_flags_t flags, 31 struct ops_create_info *create_info); 32 28 void ops_create_info_set_writer_memory(ops_create_info_t *info, 29 ops_memory_t *mem); 33 30 #endif openpgpsdk/trunk/include/openpgpsdk/packet.h
r287 r307 299 299 } ops_public_key_union_t; 300 300 301 /** Version. 302 * OpenPGP has two different protocol versions: version 3 and version 4. 303 * 304 * \see RFC2440bis-12 5.2 305 */ 306 typedef enum 307 { 308 OPS_V3=3, /*<! Version 3 */ 309 OPS_V4=4, /*<! Version 4 */ 310 } ops_version_t; 311 301 312 /** Structure to hold one pgp public key */ 302 313 typedef struct 303 314 { 304 unsignedversion; /*!< version of the key (v3, v4...) */315 ops_version_t version; /*!< version of the key (v3, v4...) */ 305 316 time_t creation_time; /*!< when the key was created. Note that interpretation varies with key 306 317 version. */ … … 386 397 } ops_user_attribute_t; 387 398 388 /** Signature Version.389 * OpenPGP has two different signature versions: version 3 and version 4.390 *391 * \see RFC2440bis-12 5.2392 */393 typedef enum394 {395 OPS_SIG_V3=3, /*<! Version 3 Signature */396 OPS_SIG_V4=4, /*<! Version 4 Signature */397 } ops_sig_version_t;398 399 399 /** Signature Type. 400 400 * OpenPGP defines different signature types that allow giving different meanings to signatures. Signature types … … 485 485 } ops_signature_union_t; 486 486 487 #define OPS_KEY_ID_SIZE 8 488 487 489 /** Struct to hold a signature packet. 488 490 * … … 490 492 * \see RFC2440bis-12 5.2.3 491 493 */ 492 #define OPS_KEY_ID_SIZE 8 493 /** signature */ 494 typedef struct 495 { 496 ops_sig_version_t version; /*!< signature version number */ 494 typedef struct 495 { 496 ops_version_t version; /*!< signature version number */ 497 497 ops_sig_type_t type; /*!< signature type value */ 498 498 time_t creation_time; /*!< creation time of the signature */ openpgpsdk/trunk/include/openpgpsdk/signature.h
r304 r307 6 6 #include "create.h" 7 7 8 /** \ingroup Create 9 * needed for signature creation 10 */ 11 typedef struct 12 { 13 ops_packet_writer_t *writer; /*!< The writer function */ 14 void *arg; /*!< Arguments for the writer function */ 15 ops_hash_t hash; 16 ops_signature_t sig; 17 ops_memory_t mem; 18 ops_create_info_t info; /*!< how to do the writing */ 19 unsigned hashed_count_offset; 20 unsigned hashed_data_length; 21 unsigned unhashed_count_offset; 22 } ops_create_signature_t; 8 typedef struct ops_create_signature ops_create_signature_t; 9 10 ops_create_signature_t *ops_create_signature_new(void); 11 void ops_create_signature_delete(ops_create_signature_t *sig); 23 12 24 13 ops_boolean_t … … 43 32 ops_sig_type_t type); 44 33 void ops_signature_start_plaintext_signature(ops_create_signature_t *sig, 34 ops_secret_key_t *key, 45 35 ops_hash_algorithm_t hash, 46 36 ops_sig_type_t type); openpgpsdk/trunk/include/openpgpsdk/types.h
r304 r307 64 64 typedef enum ops_writer_ret_t ops_writer_ret_t; 65 65 66 /** 67 * \ingroup Create 68 * Contains the required information about how to write 69 */ 70 typedef struct ops_create_info ops_create_info_t; 66 71 67 72 #endif openpgpsdk/trunk/include/openpgpsdk/util.h
r301 r307 20 20 } ops_reader_fd_arg_t; 21 21 22 /** Arguments for writer_fd23 */24 typedef struct25 {26 int fd; /*!< file descriptor */27 } ops_writer_fd_arg_t;28 29 22 void hexdump(const unsigned char *src,size_t length); 30 23 ops_reader_ret_t ops_reader_fd(unsigned char *dest,unsigned *plength, 31 24 ops_reader_flags_t flags,ops_parse_info_t *parse_info); 32 struct ops_create_info;33 ops_writer_ret_t ops_writer_fd(const unsigned char *src,unsigned length,34 ops_writer_flags_t flags,struct ops_create_info *create_info);35 25 36 26 /* typesafe deconstification */ … … 44 34 #define OPS_ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) 45 35 36 /** Allocate zeroed memory */ 37 void *ops_mallocz(size_t n); 38 46 39 #endif openpgpsdk/trunk/src/armour.c
r296 r307 687 687 dearmour_arg_t *arg; 688 688 689 arg=malloc(sizeof *arg); 690 memset(arg,'\0',sizeof *arg); 689 arg=ops_mallocz(sizeof *arg); 691 690 692 691 arg->reader_arg=parse_info->reader_arg; openpgpsdk/trunk/src/create.c
r304 r307 6 6 #include <string.h> 7 7 #include <assert.h> 8 #include <unistd.h> 9 10 /** 11 * \ingroup Create 12 * This struct contains the required information about how to write 13 */ 14 struct ops_create_info 15 { 16 ops_writer_t *writer; /*!< the writer function */ 17 void *arg; /*!< arguments for the writer function */ 18 ops_writer_destroyer_t *destroyer; 19 ops_error_t *errors; /*!< an error stack */ 20 }; 8 21 9 22 /* … … 13 26 ops_create_info_t *info) 14 27 { 15 return info->writer(src,length,0, info) == OPS_W_OK;28 return info->writer(src,length,0,&info->errors,info->arg) == OPS_W_OK; 16 29 } 17 30 … … 401 414 ops_boolean_t make_packet) 402 415 { 403 ops_create_info_t info; 416 ops_create_info_t *info; 417 418 info=ops_create_info_new(); 404 419 405 420 ops_memory_init(out,128); 406 info.writer=ops_writer_memory; 407 info.arg=out; 408 409 write_public_key_body(key,&info); 421 ops_create_info_set_writer_memory(info,out); 422 423 write_public_key_body(key,info); 410 424 411 425 if(make_packet) 412 426 ops_memory_make_packet(out,OPS_PTAG_CT_PUBLIC_KEY); 427 428 ops_create_info_delete(info); 413 429 } 414 430 … … 466 482 && write_secret_key_body(key,info); 467 483 } 484 485 /** 486 * \ingroup Create 487 * 488 * Create a new ops_create_info_t structure. 489 * 490 * \return the new structure. 491 */ 492 ops_create_info_t *ops_create_info_new(void) 493 { return ops_mallocz(sizeof(ops_create_info_t)); } 494 495 /** 496 * \ingroup Create 497 * 498 * Delete an ops_create_info_t structure. If a writer is active, then 499 * that is also deleted. 500 * 501 * \param info the structure to be deleted. 502 */ 503 void ops_create_info_delete(ops_create_info_t *info) 504 { 505 if(info->destroyer) 506 { 507 info->destroyer(info->arg); 508 info->destroyer=NULL; 509 } 510 511 free(info); 512 } 513 514 typedef struct 515 { 516 int fd; 517 } writer_fd_arg_t; 518 519 static ops_writer_ret_t fd_writer(const unsigned char *src,unsigned length, 520 ops_writer_flags_t flags, 521 ops_error_t **errors,void *arg_) 522 { 523 writer_fd_arg_t *arg=arg_; 524 int n=write(arg->fd,src,length); 525 526 OPS_USED(flags); 527 528 if(n == -1) 529 { 530 ops_system_error_1(errors,OPS_E_W_WRITE_FAILED,"write", 531 "file descriptor %d",arg->fd); 532 return OPS_W_ERROR; 533 } 534 535 if((unsigned)n != length) 536 { 537 ops_error_1(errors,OPS_E_W_WRITE_TOO_SHORT, 538 "file descriptor %d",arg->fd); 539 return OPS_W_ERROR; 540 } 541 542 return OPS_W_OK; 543 } 544 545 static void fd_destroyer(void *arg) 546 { 547 free(arg); 548 } 549 550 /** 551 * \ingroup Create 552 * 553 * Set the writer in info to be a stock writer that writes to a file 554 * descriptor. If another writer has already been set, then that is 555 * first destroyed. 556 * 557 * \param info The info structure 558 * \param fd The file descriptor 559 * 560 */ 561 562 void ops_create_info_set_writer_fd(ops_create_info_t *info,int fd) 563 { 564 writer_fd_arg_t *arg=malloc(sizeof *arg); 565 566 arg->fd=fd; 567 ops_create_info_set_writer(info,fd_writer,fd_destroyer,arg); 568 } 569 570 /** 571 * \ingroup Create 572 * 573 * Set a writer in info. If another writer has already been set, then 574 * that is first destroyed. 575 * 576 * \param info The info structure 577 * \param writer The writer 578 * \param destroyer The destroyer 579 * \param arg The argument for the writer and destroyer 580 */ 581 void ops_create_info_set_writer(ops_create_info_t *info, 582 ops_writer_t *writer, 583 ops_writer_destroyer_t *destroyer, 584 void *arg) 585 { 586 ops_create_info_close_writer(info); 587 info->writer=writer; 588 info->destroyer=destroyer; 589 info->arg=arg; 590 } 591 592 /** 593 * \ingroup Create 594 * 595 * Close the writer currently set in info. 596 * 597 * \param info The info structure 598 */ 599 void ops_create_info_close_writer(ops_create_info_t *info) 600 { 601 if(info->destroyer) 602 info->destroyer(info->arg); 603 604 info->writer=NULL; 605 info->destroyer=NULL; 606 info->arg=NULL; 607 } openpgpsdk/trunk/src/errors.c
r301 r307 58 58 */ 59 59 60 void push_error(ops_error_t **errstack,ops_errcode_t errcode,int errno,60 void push_error(ops_error_t **errstack,ops_errcode_t errcode,int sys_errno, 61 61 const char *file,int line,const char *fmt,...) 62 62 { … … 84 84 // fill in the details 85 85 err->errcode=errcode; 86 err-> errno=errno;86 err->sys_errno=sys_errno; 87 87 err->file=file; 88 88 err->line=line; … … 95 95 printf("%s:%d: ",err->file,err->line); 96 96 if (err->errcode==OPS_E_SYSTEM_ERROR) 97 printf("system error %d returned from %s()\n",err->errno, err->comment); 97 printf("system error %d returned from %s()\n",err->sys_errno, 98 err->comment); 98 99 else 99 printf("%s, %s\n", 100 ops_errcode(err->errcode), 101 err->comment); 100 printf("%s, %s\n",ops_errcode(err->errcode),err->comment); 102 101 } 103 102 openpgpsdk/trunk/src/memory.c
r301 r307 58 58 } 59 59 60 ops_writer_ret_t ops_writer_memory(const unsigned char *src,unsigned length, 61 ops_writer_flags_t flags, 62 ops_create_info_t *create_info) 60 static ops_writer_ret_t memory_writer(const unsigned char *src,unsigned length, 61 ops_writer_flags_t flags, 62 ops_error_t **errors, 63 void *arg) 63 64 { 64 ops_memory_t *mem= create_info->arg;65 ops_memory_t *mem=arg; 65 66 66 67 OPS_USED(flags); 68 OPS_USED(errors); 67 69 ops_memory_add(mem,src,length); 68 70 return OPS_W_OK; 71 } 72 73 /** 74 * \ingroup Create 75 * 76 * Set a memory writer. Note that it is the caller's resposibility to 77 * release mem. 78 * 79 * \param info The info structure 80 * \param mem The memory structure 81 */ 82 83 void ops_create_info_set_writer_memory(ops_create_info_t *info, 84 ops_memory_t *mem) 85 { 86 ops_create_info_set_writer(info,memory_writer,NULL,mem); 69 87 } 70 88 openpgpsdk/trunk/src/packet-parse.c
r304 r307 990 990 ops_parser_content_t content; 991 991 992 C.signature.version=OPS_ SIG_V3;992 C.signature.version=OPS_V3; 993 993 994 994 /* hash info length */ … … 1396 1396 1397 1397 memset(&C.signature,'\0',sizeof C.signature); 1398 C.signature.version=OPS_ SIG_V4;1398 C.signature.version=OPS_V4; 1399 1399 C.signature.v4_hashed_data_start=v4_hashed_data_start; 1400 1400 openpgpsdk/trunk/src/signature.c
r304 r307 7 7 #include <assert.h> 8 8 #include <string.h> 9 10 /** \ingroup Create 11 * needed for signature creation 12 */ 13 struct ops_create_signature 14 { 15 ops_hash_t hash; 16 ops_signature_t sig; 17 ops_memory_t mem; 18 ops_create_info_t *info; /*!< how to do the writing */ 19 unsigned hashed_count_offset; 20 unsigned hashed_data_length; 21 unsigned unhashed_count_offset; 22 }; 23 24 ops_create_signature_t *ops_create_signature_new() 25 { return ops_mallocz(sizeof(ops_create_signature_t)); } 26 27 void ops_create_signature_delete(ops_create_signature_t *sig) 28 { 29 ops_create_info_delete(sig->info); 30 sig->info=NULL; 31 free(sig); 32 } 9 33 10 34 static unsigned char prefix_md5[]={ 0x30,0x20,0x30,0x0C,0x06,0x08,0x2A,0x86, … … 143 167 const unsigned char *raw_packet) 144 168 { 145 if(sig->version == OPS_ SIG_V4)169 if(sig->version == OPS_V4) 146 170 { 147 171 if(raw_packet) … … 231 255 init_signature(&hash,sig,key); 232 256 233 if(sig->version == OPS_ SIG_V4)257 if(sig->version == OPS_V4) 234 258 { 235 259 ops_hash_add_int(&hash,0xb4,1); … … 308 332 ops_sig_type_t type) 309 333 { 310 memset(sig,'\0',sizeof *sig); 334 sig->info=ops_create_info_new(); 335 311 336 // XXX: refactor with check (in several ways - check should probably 312 337 // use the buffered writer to construct packets (done), and also should 313 338 // share code for hash calculation) 314 sig->sig.version=OPS_ SIG_V4;339 sig->sig.version=OPS_V4; 315 340 sig->sig.hash_algorithm=OPS_HASH_SHA1; 316 341 sig->sig.key_algorithm=key->algorithm; … … 328 353 // thing to get counts before writing. 329 354 ops_memory_init(&sig->mem,100); 330 sig->info.writer=ops_writer_memory; 331 sig->info.arg=&sig->mem; 355 ops_create_info_set_writer_memory(sig->info,&sig->mem); 332 356 333 357 // write nearly up to the first subpacket 334 ops_write_scalar(sig->sig.version,1, &sig->info);335 ops_write_scalar(sig->sig.type,1, &sig->info);336 ops_write_scalar(sig->sig.key_algorithm,1, &sig->info);337 ops_write_scalar(sig->sig.hash_algorithm,1, &sig->info);358 ops_write_scalar(sig->sig.version,1,sig->info); 359 ops_write_scalar(sig->sig.type,1,sig->info); 360 ops_write_scalar(sig->sig.key_algorithm,1,sig->info); 361 ops_write_scalar(sig->sig.hash_algorithm,1,sig->info); 338 362 339 363 // dummy hashed subpacket count 340 364 sig->hashed_count_offset=sig->mem.length; 341 ops_write_scalar(0,2, &sig->info);365 ops_write_scalar(0,2,sig->info); 342 366 } 343 367 … … 353 377 */ 354 378 void ops_signature_start_plaintext_signature(ops_create_signature_t *sig, 379 ops_secret_key_t *key, 355 380 ops_hash_algorithm_t hash, 356 381 ops_sig_type_t type) 357 382 { 358 memset(sig,'\0',sizeof *sig); 383 sig->info=ops_create_info_new(); 384 359 385 // XXX: refactor with check (in several ways - check should probably 360 386 // use the buffered writer to construct packets (done), and also should 361 387 // share code for hash calculation) 362 sig->sig.version=OPS_SIG_V4; 388 sig->sig.version=OPS_V4; 389 sig->sig.key_algorithm=key->public_key.algorithm; 363 390 sig->sig.hash_algorithm=hash; 364 391 sig->sig.type=type; … … 371 398 // thing to get counts before writing. 372 399 ops_memory_init(&sig->mem,100); 373 sig->info.writer=ops_writer_memory; 374 sig->info.arg=&sig->mem; 400 ops_create_info_set_writer_memory(sig->info,&sig->mem); 375 401 376 402 // write nearly up to the first subpacket 377 ops_write_scalar(sig->sig.version,1, &sig->info);378 ops_write_scalar(sig->sig.type,1, &sig->info);379 ops_write_scalar(sig->sig.key_algorithm,1, &sig->info);380 ops_write_scalar(sig->sig.hash_algorithm,1, &sig->info);403 ops_write_scalar(sig->sig.version,1,sig->info); 404 ops_write_scalar(sig->sig.type,1,sig->info); 405 ops_write_scalar(sig->sig.key_algorithm,1,sig->info); 406 ops_write_scalar(sig->sig.hash_algorithm,1,sig->info); 381 407 382 408 // dummy hashed subpacket count 383 409 sig->hashed_count_offset=sig->mem.length; 384 ops_write_scalar(0,2, &sig->info);410 ops_write_scalar(0,2,sig->info); 385 411 } 386 412 … … 415 441 // dummy unhashed subpacket count 416 442 sig->unhashed_count_offset=sig->mem.length; 417 ops_write_scalar(0,2, &sig->info);443 ops_write_scalar(0,2,sig->info); 418 444 } 419 445 … … 448 474 // and write it directly to the output instead of via memory. 449 475 assert(key->algorithm == OPS_PKA_RSA); 450 rsa_sign(&sig->hash,&key->key.rsa,&skey->key.rsa, &sig->info);476 rsa_sign(&sig->hash,&key->key.rsa,&skey->key.rsa,sig->info); 451 477 452 478 ops_write_ptag(OPS_PTAG_CT_SIGNATURE,info); … … 467 493 void ops_signature_add_creation_time(ops_create_signature_t *sig,time_t when) 468 494 { 469 ops_write_ss_header(5,OPS_PTAG_SS_CREATION_TIME, &sig->info);470 ops_write_scalar(when,4, &sig->info);495 ops_write_ss_header(5,OPS_PTAG_SS_CREATION_TIME,sig->info); 496 ops_write_scalar(when,4,sig->info); 471 497 } 472 498 … … 483 509 const unsigned char keyid[OPS_KEY_ID_SIZE]) 484 510 { 485 ops_write_ss_header(OPS_KEY_ID_SIZE+1,OPS_PTAG_SS_ISSUER_KEY_ID, &sig->info);486 ops_write(keyid,OPS_KEY_ID_SIZE, &sig->info);511 ops_write_ss_header(OPS_KEY_ID_SIZE+1,OPS_PTAG_SS_ISSUER_KEY_ID,sig->info); 512 ops_write(keyid,OPS_KEY_ID_SIZE,sig->info); 487 513 } 488 514 … … 498 524 ops_boolean_t primary) 499 525 { 500 ops_write_ss_header(2,OPS_PTAG_SS_PRIMARY_USER_ID, &sig->info);501 ops_write_scalar(primary,1, &sig->info);502 } 526 ops_write_ss_header(2,OPS_PTAG_SS_PRIMARY_USER_ID,sig->info); 527 ops_write_scalar(primary,1,sig->info); 528 } openpgpsdk/trunk/src/util.c
r301 r307 10 10 #include <assert.h> 11 11 #include <unistd.h> 12 #include < errno.h>12 #include <string.h> 13 13 14 14 /** … … 141 141 } 142 142 143 /** 144 * \ingroup Create 145 * 146 * ops_writer_fd() attempts to write up to #length bytes 147 * to the file descriptor in #arg_ from the buffer #src 148 * using the rules contained in #flags 149 * 150 * \param src 151 * \param length Number of bytes to try to write 152 * \param flags Rules to use 153 * \param arg_ Gets cast to #ops_writer_fd_arg_t 154 * 155 * \return OPS_W_ERROR if not enough bytes written 156 * \return OPS_W_OK if all bytes written 157 * \todo change arg_ to typesafe? 158 */ 159 ops_writer_ret_t ops_writer_fd(const unsigned char *src,unsigned length, 160 ops_writer_flags_t flags, 161 ops_create_info_t *create_info) 143 void *ops_mallocz(size_t n) 162 144 { 163 ops_writer_fd_arg_t *arg=create_info->arg; 164 int n=write(arg->fd,src,length); 145 void *m=malloc(n); 165 146 166 OPS_USED(flags);147 memset(m,'\0',n); 167 148 168 if(n == -1) 169 { 170 ops_system_error_1(&create_info->errors,OPS_E_W_WRITE_FAILED,"write", 171 "file descriptor %d",arg->fd); 172 return OPS_W_ERROR; 173 } 174 175 if((unsigned)n != length) 176 { 177 ops_error_1(&create_info->errors,OPS_E_W_WRITE_TOO_SHORT, 178 "file descriptor %d",arg->fd); 179 return OPS_W_ERROR; 180 } 181 182 return OPS_W_OK; 149 return m; 183 150 }
