Changeset 484
- Timestamp:
- 08/10/07 13:07:33
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/std_print.h (modified) (1 diff)
- openpgpsdk/trunk/src/advanced/adv_create.c (modified) (1 diff)
- openpgpsdk/trunk/src/standard/std_print.c (modified) (2 diffs)
- openpgpsdk/trunk/tests/test_packet_types.c (modified) (9 diffs)
- openpgpsdk/trunk/tests/tests.c (modified) (3 diffs)
- openpgpsdk/trunk/tests/tests.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/std_print.h
r447 r484 5 5 #define OPS_STD_PRINT_H 6 6 7 #include "openpgpsdk/packet.h" 8 #include "openpgpsdk/packet-parse.h" 9 #include "openpgpsdk/keyring.h" 10 11 void ops_print_pk_session_key(ops_content_tag_t tag, 12 const ops_pk_session_key_t *key); 7 13 void ops_print_public_key(const ops_key_data_t *key); 8 14 openpgpsdk/trunk/src/advanced/adv_create.c
r482 r484 815 815 && ops_write_scalar(pksk->algorithm, 1, info) 816 816 && ops_write_mpi(pksk->parameters.rsa.encrypted_m, info) 817 && ops_write_scalar(0, 2, info); 817 //?? && ops_write_scalar(0, 2, info); 818 ; 818 819 } 819 820 openpgpsdk/trunk/src/standard/std_print.c
r480 r484 488 488 } 489 489 490 static voidprint_pk_session_key(ops_content_tag_t tag,490 void ops_print_pk_session_key(ops_content_tag_t tag, 491 491 const ops_pk_session_key_t *key) 492 492 { … … 1116 1116 case OPS_PTAG_CT_PK_SESSION_KEY: 1117 1117 case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 1118 print_pk_session_key(content_->tag,&content->pk_session_key);1118 ops_print_pk_session_key(content_->tag,&content->pk_session_key); 1119 1119 break; 1120 1120 1121 1121 case OPS_PARSER_CMD_GET_SECRET_KEY: 1122 print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY,1122 ops_print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY, 1123 1123 content->get_secret_key.pk_session_key); 1124 1124 openpgpsdk/trunk/tests/test_packet_types.c
r480 r484 20 20 static unsigned char* mdc_data=NULL; 21 21 static size_t sz_mdc_data=0; 22 static unsigned char* encrypted_pk_sk=NULL; 23 static size_t sz_encrypted_pk_sk=0; 22 24 23 25 #define MAXBUF 128 24 26 25 27 static void cleanup(); 26 //static void print_hash(char* str, unsigned char* data);27 28 28 29 /* … … 32 33 int init_suite_packet_types(void) 33 34 { 35 char keydetails[MAXBUF+1]; 36 char keyring_name[MAXBUF+1]; 37 int fd=0; 38 char cmd[MAXBUF+1]; 39 34 40 // Initialise OPS 35 41 ops_init(); 42 43 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"; 44 // Create temp directory 45 if (!mktmpdir()) 46 return 1; 47 48 /* 49 * Create a RSA keypair with no passphrase 50 */ 51 52 snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 53 54 if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 55 { 56 fprintf(stderr,"Can't create key details\n"); 57 return 1; 58 } 59 60 write(fd,rsa_nopass,strlen(rsa_nopass)); 61 close(fd); 62 63 snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 64 system(cmd); 65 66 // read keyrings 67 snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 68 ops_keyring_read(&pub_keyring,keyring_name); 69 70 // read keyring 71 snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 72 ops_keyring_read(&sec_keyring,keyring_name); 36 73 37 74 // Return success … … 62 99 case OPS_PTAG_CT_LITERAL_DATA_BODY: 63 100 sz_literal_data=content->literal_data_body.length; 64 // literal_data=ops_mallocz(content->literal_data_body.length+1);65 // memcpy(literal_data,content->literal_data_body.data,content->literal_data_body.length);66 101 literal_data=ops_mallocz(sz_literal_data+1); 67 102 memcpy(literal_data,content->literal_data_body.data,sz_literal_data); 68 103 break; 69 104 70 case OPS_PARSER_PTAG:71 105 case OPS_PTAG_CT_LITERAL_DATA_HEADER: 72 106 // ignore 73 107 break; 74 108 75 case OPS_PARSER_ERROR:76 printf("parse error: %s\n",content->error.error);77 break;78 79 case OPS_PARSER_ERRCODE:80 printf("parse error: %s\n",81 ops_errcode(content->errcode.errcode));82 break;83 84 109 default: 85 fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 86 content_->tag); 87 assert(0); 110 return callback_general(content_,cbinfo); 88 111 } 89 112 … … 100 123 // ops_print_packet(content_); 101 124 102 // Read data from packet into static buffer103 125 switch(content_->tag) 104 126 { … … 110 132 break; 111 133 112 case OPS_PARSER_PTAG: 113 // ignore 134 default: 135 return callback_general(content_,cbinfo); 136 } 137 138 return OPS_RELEASE_MEMORY; 139 } 140 141 static ops_parse_cb_return_t 142 callback_encrypted_pk_session_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 143 { 144 ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 145 146 OPS_USED(cbinfo); 147 148 // ops_print_packet(content_); 149 150 switch(content_->tag) 151 { 152 case OPS_PTAG_CT_PK_SESSION_KEY: 114 153 break; 115 154 116 case OPS_PARSER_ERROR: 117 printf("parse error: %s\n",content->error.error); 118 break; 119 120 case OPS_PARSER_ERRCODE: 121 printf("parse error: %s\n", 122 ops_errcode(content->errcode.errcode)); 123 break; 155 case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 156 sz_encrypted_pk_sk=sizeof(*encrypted_pk_sk); 157 encrypted_pk_sk=ops_mallocz(sz_encrypted_pk_sk); 158 memcpy(encrypted_pk_sk,&content->pk_session_key,sz_encrypted_pk_sk); 159 break; 160 161 case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 162 return callback_cmd_get_secret_key_passphrase(content_,cbinfo); 163 164 case OPS_PARSER_CMD_GET_SECRET_KEY: 165 return callback_cmd_get_secret_key(content_,cbinfo); 124 166 125 167 default: 126 fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 127 content_->tag); 128 assert(0); 168 return callback_general(content_,cbinfo); 129 169 } 130 170 … … 143 183 switch(content_->tag) 144 184 { 145 case OPS_PARSER_PTAG:146 // ignore147 break;148 149 185 case OPS_PTAG_CT_LITERAL_DATA_HEADER: 150 186 case OPS_PTAG_CT_LITERAL_DATA_BODY: … … 153 189 154 190 default: 155 fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 156 content_->tag); 157 assert(0); 191 return callback_general(content_,cbinfo); 158 192 } 159 193 … … 438 472 } 439 473 474 static void test_ops_encrypted_pk_sk() 475 { 476 char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 477 ops_pk_session_key_t *encrypted_pk_session_key; 478 ops_create_info_t *cinfo; 479 ops_parse_info_t *pinfo; 480 ops_memory_t *mem; 481 int rtn=0; 482 483 // setup for write 484 ops_setup_memory_write(&cinfo,&mem,MAXBUF); 485 486 // write 487 const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 488 encrypted_pk_session_key=ops_create_pk_session_key(pub_key); 489 ops_write_pk_session_key(cinfo,encrypted_pk_session_key); 490 491 // setup for read 492 ops_setup_memory_read(&pinfo,mem,callback_encrypted_pk_session_key); 493 494 // read 495 rtn=ops_parse(pinfo); 496 497 // test 498 CU_ASSERT(memcmp(encrypted_pk_session_key, encrypted_pk_sk, sz_encrypted_pk_sk)==0); 499 500 // cleanup 501 cleanup(); 502 ops_teardown_memory_read(pinfo,mem); 503 } 504 440 505 CU_pSuite suite_packet_types() 441 506 { … … 461 526 462 527 if (NULL == CU_add_test(suite, "Tag 20: Sym. Encrypted Integrity Protected Data packet", test_ops_se_ip)) 528 return NULL; 529 530 if (NULL == CU_add_test(suite, "Tag 1: PK Encrypted Session Key packet", test_ops_encrypted_pk_sk)) 463 531 return NULL; 464 532 openpgpsdk/trunk/tests/tests.c
r480 r484 8 8 #include "CUnit/Basic.h" 9 9 #include "openpgpsdk/readerwriter.h" 10 // \todo remove the need for this 11 #include "../src/advanced/parse_local.h" 10 12 11 13 #include "tests.h" … … 17 19 18 20 char dir[MAXBUF+1]; 21 ops_keyring_t pub_keyring; 22 ops_keyring_t sec_keyring; 23 static char* no_passphrase=""; 19 24 20 25 int main() … … 94 99 } 95 100 96 97 101 ops_parse_cb_return_t 102 callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 103 { 104 ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 105 106 OPS_USED(cbinfo); 107 108 // ops_print_packet(content_); 109 110 switch(content_->tag) 111 { 112 case OPS_PARSER_PTAG: 113 // ignore 114 break; 115 116 case OPS_PARSER_ERROR: 117 printf("parse error: %s\n",content->error.error); 118 break; 119 120 case OPS_PARSER_ERRCODE: 121 printf("parse error: %s\n", 122 ops_errcode(content->errcode.errcode)); 123 break; 124 125 default: 126 fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 127 content_->tag); 128 assert(0); 129 } 130 131 return OPS_RELEASE_MEMORY; 132 } 133 134 ops_parse_cb_return_t 135 callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 136 { 137 ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 138 const ops_key_data_t *keydata=NULL; 139 const ops_secret_key_t *secret; 140 /* 141 static const ops_key_data_t *decrypt_key; 142 */ 143 144 OPS_USED(cbinfo); 145 146 // ops_print_packet(content_); 147 148 switch(content_->tag) 149 { 150 case OPS_PARSER_CMD_GET_SECRET_KEY: 151 keydata=ops_keyring_find_key_by_id(&sec_keyring,content->get_secret_key.pk_session_key->key_id); 152 if (!keydata || !ops_key_is_secret(keydata)) 153 return 0; 154 155 // Do we need the passphrase and not have it? If so, get it 156 ops_parser_content_t pc; 157 char *passphrase; 158 memset(&pc,'\0',sizeof pc); 159 passphrase=NULL; 160 pc.content.secret_key_passphrase.passphrase=&passphrase; 161 // pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey); 162 pc.content.secret_key_passphrase.secret_key=ops_get_secret_key_from_data(keydata); 163 164 /* Ugh. Need to duplicate this macro here to get the passphrase 165 Duplication to be removed when the callback gets moved to main code. 166 Can we make this inline code rather than a macro? 167 */ 168 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 169 CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 170 171 /* now get the key from the data */ 172 secret=ops_get_secret_key_from_data(keydata); 173 while(!secret) 174 { 175 /* then it must be encrypted */ 176 secret=ops_decrypt_secret_key_from_data(keydata,passphrase); 177 free(passphrase); 178 } 179 180 *content->get_secret_key.secret_key=secret; 181 break; 182 183 default: 184 return callback_general(content_,cbinfo); 185 } 186 187 return OPS_RELEASE_MEMORY; 188 } 189 190 ops_parse_cb_return_t 191 callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 192 { 193 ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 194 /* 195 static const ops_key_data_t *decrypt_key; 196 const ops_key_data_t *keydata=NULL; 197 const ops_secret_key_t *secret; 198 */ 199 200 OPS_USED(cbinfo); 201 202 // ops_print_packet(content_); 203 204 switch(content_->tag) 205 { 206 case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 207 /* 208 Doing this so the test can be automated. 209 Will move this into separate stacked callback later 210 */ 211 *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(no_passphrase); 212 return OPS_KEEP_MEMORY; 213 break; 214 215 default: 216 return callback_general(content_,cbinfo); 217 } 218 219 return OPS_RELEASE_MEMORY; 220 } 221 openpgpsdk/trunk/tests/tests.h
r480 r484 20 20 #define MAXBUF 128 21 21 22 ops_parse_cb_return_t 23 callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 24 ops_parse_cb_return_t 25 callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 26 ops_parse_cb_return_t 27 callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 28 29 ops_keyring_t pub_keyring; 30 ops_keyring_t sec_keyring; 22 31 #endif 23 32
