Changeset 511
- Timestamp:
- 09/21/07 18:35:41
- Files:
-
- openpgpsdk/trunk/examples/packet-dump.c (modified) (1 diff)
- openpgpsdk/trunk/src/advanced/adv_create.c (modified) (4 diffs)
- openpgpsdk/trunk/src/advanced/adv_packet-parse.c (modified) (7 diffs)
- openpgpsdk/trunk/src/advanced/adv_writer_encrypt.c (modified) (2 diffs)
- openpgpsdk/trunk/src/advanced/adv_writer_encrypt_se_ip.c (modified) (7 diffs)
- openpgpsdk/trunk/tests/test_packet_types.c (modified) (7 diffs)
- openpgpsdk/trunk/tests/test_rsa_decrypt.c (modified) (3 diffs)
- openpgpsdk/trunk/tests/test_rsa_encrypt.c (modified) (16 diffs)
- openpgpsdk/trunk/tests/tests.c (modified) (6 diffs)
- openpgpsdk/trunk/tests/tests.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/examples/packet-dump.c
r447 r511 462 462 content->ptag.length,content->ptag.length, 463 463 content->ptag.position,content->ptag.position); 464 /* 465 print_tagname(ops_str_from_single_packet_tag(content->ptag.content_tag)); 466 */ 464 print_tagname(ops_show_packet_tag(content->ptag.content_tag)); 467 465 break; 468 466 openpgpsdk/trunk/src/advanced/adv_create.c
r509 r511 784 784 ) 785 785 { 786 int debug=0; 787 786 788 //unsigned char encmpibuf[8192]; 787 789 // unsigned char EM[8192]; … … 818 820 819 821 820 /* 821 // int i=0; 822 fprintf(stderr,"Encoded Message: \n"); 823 for (i=0; i<mLen; i++) 824 fprintf(stderr,"%2x ", EM[i]); 825 fprintf(stderr,"\n"); 826 */ 822 if (debug) 823 { 824 unsigned int i=0; 825 fprintf(stderr,"Encoded Message: \n"); 826 for (i=0; i<mLen; i++) 827 fprintf(stderr,"%2x ", EM[i]); 828 fprintf(stderr,"\n"); 829 } 827 830 828 831 return ops_true; … … 841 844 */ 842 845 846 int debug=0; 847 843 848 const ops_public_key_t* pub_key=ops_get_public_key_from_data(key); 844 849 const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; … … 854 859 memcpy(session_key->key_id, key->key_id, sizeof session_key->key_id); 855 860 856 /* 857 fprintf(stderr,"Encrypting for RSA key id : "); 858 unsigned int i=0; 859 for (i=0; i<sizeof session_key->key_id; i++) 860 fprintf(stderr,"%2x ", key->key_id[i]); 861 fprintf(stderr,"\n"); 862 */ 861 if (debug) 862 { 863 fprintf(stderr,"Encrypting for RSA key id : "); 864 unsigned int i=0; 865 for (i=0; i<sizeof session_key->key_id; i++) 866 fprintf(stderr,"%2x ", key->key_id[i]); 867 fprintf(stderr,"\n"); 868 } 863 869 864 870 assert(key->key.pkey.algorithm == OPS_PKA_RSA); 865 871 session_key->algorithm=key->key.pkey.algorithm; 866 /* 867 session_key->symmetric_algorithm=OPS_SA_AES_256; 868 ops_random(session_key->key, 256/8); 869 */ 872 870 873 // \todo allow user to specify other algorithm 871 874 session_key->symmetric_algorithm=OPS_SA_CAST5; 872 873 875 ops_random(session_key->key, CAST_KEY_LENGTH); 874 /* 875 fprintf(stderr,"CAST5 session key created (len=%d):\n ", CAST_KEY_LENGTH); 876 for (i=0; i<CAST_KEY_LENGTH; i++) 877 fprintf(stderr,"%2x ", session_key->key[i]); 878 fprintf(stderr,"\n"); 879 */ 876 877 if (debug) 878 { 879 unsigned int i=0; 880 fprintf(stderr,"CAST5 session key created (len=%d):\n ", CAST_KEY_LENGTH); 881 for (i=0; i<CAST_KEY_LENGTH; i++) 882 fprintf(stderr,"%2x ", session_key->key[i]); 883 fprintf(stderr,"\n"); 884 } 880 885 881 886 if (create_unencoded_m_buf(session_key, &unencoded_m_buf[0])==ops_false) 882 887 return NULL; 883 888 884 /* 885 printf("unencoded m buf:\n"); 886 for (i=0; i<sz_unencoded_m_buf; i++) 887 printf("%2x ", unencoded_m_buf[i]); 888 printf("\n"); 889 */ 889 if (debug) 890 { 891 unsigned int i=0; 892 printf("unencoded m buf:\n"); 893 for (i=0; i<sz_unencoded_m_buf; i++) 894 printf("%2x ", unencoded_m_buf[i]); 895 printf("\n"); 896 } 890 897 encode_m_buf(&unencoded_m_buf[0], sz_unencoded_m_buf, pub_key, &encoded_m_buf[0]); 891 898 892 899 // and encrypt it 893 900 if(!ops_encrypt_mpi(encoded_m_buf, sz_encoded_m_buf, pub_key, &session_key->parameters)) openpgpsdk/trunk/src/advanced/adv_packet-parse.c
r501 r511 2210 2210 ops_parse_info_t *pinfo) 2211 2211 { 2212 int debug=0; 2212 2213 unsigned char c[1]; 2213 2214 ops_parser_content_t content; … … 2220 2221 2221 2222 // Can't rely on it being CAST5 2223 // \todo FIXME RW 2222 2224 // const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 2223 2225 const size_t sz_unencoded_m_buf=1024; … … 2239 2241 return 0; 2240 2242 2241 /* 2242 int i; 2243 int x=sizeof C.pk_session_key.key_id; 2244 printf("session key: public key id: x=%d\n",x); 2245 for (i=0; i<x; i++) 2246 printf("%2x ", C.pk_session_key.key_id[i]); 2247 printf("\n"); 2248 */ 2243 if (debug) 2244 { 2245 int i; 2246 int x=sizeof C.pk_session_key.key_id; 2247 printf("session key: public key id: x=%d\n",x); 2248 for (i=0; i<x; i++) 2249 printf("%2x ", C.pk_session_key.key_id[i]); 2250 printf("\n"); 2251 } 2249 2252 2250 2253 if(!limited_read(c,1,region,pinfo)) … … 2325 2328 memcpy(C.pk_session_key.key,unencoded_m_buf+1,k); 2326 2329 2327 /* 2328 printf("session key recovered (len=%d):\n",k); 2329 unsigned int j; 2330 for(j=0; j<k; j++) 2331 printf("%2x ", C.pk_session_key.key[j]); 2332 printf("\n"); 2333 */ 2330 if (debug) 2331 { 2332 printf("session key recovered (len=%d):\n",k); 2333 unsigned int j; 2334 for(j=0; j<k; j++) 2335 printf("%2x ", C.pk_session_key.key[j]); 2336 printf("\n"); 2337 } 2334 2338 2335 2339 C.pk_session_key.checksum=unencoded_m_buf[k+1]+(unencoded_m_buf[k+2] << 8); 2336 // printf("session key checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 2340 if (debug) 2341 { 2342 printf("session key checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 2343 } 2337 2344 2338 2345 // Check checksum … … 2363 2370 ops_parse_cb_info_t *cbinfo) 2364 2371 { 2372 int debug=0; 2373 2365 2374 /* 2366 2375 Gets entire SE_IP data packet. … … 2393 2402 return -1; 2394 2403 2404 if (debug) 2405 { 2406 fprintf(stderr,"\n\nentire SE IP packet (len=%d):\n",decrypted_region.length); 2407 unsigned int i=0; 2408 for (i=0; i<decrypted_region.length; i++) 2409 { 2410 fprintf(stderr,"0x%02x ", buf[i]); 2411 if (!((i+1)%8)) 2412 fprintf(stderr,"\n"); 2413 } 2414 fprintf(stderr,"\n"); 2415 fprintf(stderr,"\n"); 2416 } 2417 2395 2418 // verify leading preamble 2396 2419 2397 /* debug 2398 fprintf(stderr,"\npreamble: "); 2399 unsigned int i=0; 2400 for (i=0; i<arg->decrypt->blocksize+2;i++) 2401 fprintf(stderr," 0x%02x", buf[i]); 2402 fprintf(stderr,"\n"); 2403 */ 2420 if (debug) 2421 { 2422 fprintf(stderr,"\npreamble: "); 2423 unsigned int i=0; 2424 for (i=0; i<arg->decrypt->blocksize+2;i++) 2425 fprintf(stderr," 0x%02x", buf[i]); 2426 fprintf(stderr,"\n"); 2427 } 2404 2428 2405 2429 size_t b=arg->decrypt->blocksize; … … 2424 2448 unsigned char* mdc_hash=mdc+2; 2425 2449 2450 if (debug) 2451 { 2452 unsigned int i=0; 2453 2454 fprintf(stderr,"\nplaintext (len=%ld): ",sz_plaintext); 2455 for (i=0; i<sz_plaintext;i++) 2456 fprintf(stderr," 0x%02x", plaintext[i]); 2457 fprintf(stderr,"\n"); 2458 2459 fprintf(stderr,"\nmdc (len=%ld): ",sz_mdc); 2460 for (i=0; i<sz_mdc;i++) 2461 fprintf(stderr," 0x%02x", mdc[i]); 2462 fprintf(stderr,"\n"); 2463 } 2464 2426 2465 ops_calc_mdc_hash(preamble,sz_preamble,plaintext,sz_plaintext,&hashed[0]); 2427 2466 /* openpgpsdk/trunk/src/advanced/adv_writer_encrypt.c
r509 r511 22 22 ops_writer_info_t *winfo) 23 23 { 24 int debug=0; 24 25 crypt_arg_t *arg=(crypt_arg_t *)ops_writer_get_arg(winfo); 25 26 … … 28 29 29 30 #define BUFSZ 1024 // arbitrary number 30 unsigned char buf[BUFSZ];31 // unsigned char buf[BUFSZ]; 31 32 unsigned char encbuf[BUFSZ]; 32 33 unsigned remaining=length; 34 unsigned done=0; 33 35 while (remaining) 34 36 { 35 37 unsigned len = remaining < BUFSZ ? remaining : BUFSZ; 36 memcpy(buf,src,len); // \todo copy needed here?38 // memcpy(buf,src,len); // \todo copy needed here? 37 39 38 arg->crypt->cfb_encrypt(arg->crypt, encbuf, buf, len);40 arg->crypt->cfb_encrypt(arg->crypt, encbuf, src+done, len); 39 41 40 /* 41 fprintf(stderr,"WRITING:\nunencrypted: "); 42 int i=0; 43 for (i=0; i<16; i++) 44 fprintf(stderr,"%2x ", buf[i]); 45 fprintf(stderr,"\n"); 46 fprintf(stderr,"encrypted: "); 47 for (i=0; i<16; i++) 48 fprintf(stderr,"%2x ", encbuf[i]); 49 fprintf(stderr,"\n"); 50 */ 42 if (debug) 43 { 44 fprintf(stderr,"WRITING:\nunencrypted: "); 45 int i=0; 46 for (i=0; i<16; i++) 47 fprintf(stderr,"%2x ", src[done+i]); 48 fprintf(stderr,"\n"); 49 fprintf(stderr,"encrypted: "); 50 for (i=0; i<16; i++) 51 fprintf(stderr,"%2x ", encbuf[i]); 52 fprintf(stderr,"\n"); 53 } 51 54 52 55 if (!ops_stacked_write(encbuf,len,errors,winfo)) 53 56 return ops_false; 54 57 remaining-=len; 58 done+=len; 55 59 } 56 60 openpgpsdk/trunk/src/advanced/adv_writer_encrypt_se_ip.c
r509 r511 72 72 ops_create_info_t *my_cinfo; 73 73 74 const unsigned int bufsz=128; // \todo good value?74 const unsigned int bufsz=128; // initial value; gets expanded as necessary 75 75 ops_setup_memory_write(&cinfo_literal,&mem_literal,bufsz); 76 76 ops_setup_memory_write(&my_cinfo,&my_mem,bufsz); 77 77 78 // \todo handle larger packets e.g. do loop 79 80 unsigned remaining=length; 81 unsigned done=0; 82 while (remaining && rtn==ops_true) 83 { 84 unsigned len = remaining < bufsz ? remaining : bufsz; 85 86 // create literal data packet from source data 87 ops_write_literal_data(src+done, len, OPS_LDT_BINARY, cinfo_literal); 78 // create literal data packet from source data 79 ops_write_literal_data(src, length, OPS_LDT_BINARY, cinfo_literal); 80 assert(ops_memory_get_length(mem_literal)>length); 81 82 // create SE IP packet set from this literal data 83 ops_write_se_ip_pktset(ops_memory_get_data(mem_literal), 84 ops_memory_get_length(mem_literal), 85 arg->crypt, my_cinfo); 86 assert(ops_memory_get_length(my_mem)>ops_memory_get_length(mem_literal)); 87 88 // now write memory to next writer 89 rtn=ops_stacked_write(ops_memory_get_data(my_mem), 90 ops_memory_get_length(my_mem), 91 errors, winfo); 88 92 89 // create SE IP packet set from this literal data90 ops_write_se_ip_pktset(ops_memory_get_data(mem_literal),91 ops_memory_get_length(mem_literal),92 arg->crypt, my_cinfo);93 94 // now write memory to next writer95 rtn=ops_stacked_write(ops_memory_get_data(my_mem),96 ops_memory_get_length(my_mem),97 errors, winfo);98 99 remaining-=len;100 done+=len;101 }102 93 ops_memory_free(my_mem); 103 94 ops_memory_free(mem_literal); … … 115 106 } 116 107 117 void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* data, const unsigned int len, unsigned char *hashed) 118 { 108 void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* plaintext, const unsigned int sz_plaintext, unsigned char *hashed) 109 { 110 int debug=0; 119 111 ops_hash_t hash; 120 // unsigned char hashed[SHA_DIGEST_LENGTH];121 112 unsigned char c[0]; 113 114 if (debug) 115 { 116 fprintf(stderr,"ops_calc_mdc_hash():\n"); 117 118 fprintf(stderr,"\npreamble: "); 119 unsigned int i=0; 120 for (i=0; i<sz_preamble;i++) 121 fprintf(stderr," 0x%02x", preamble[i]); 122 fprintf(stderr,"\n"); 123 124 fprintf(stderr,"\nplaintext (len=%d): ",sz_plaintext); 125 for (i=0; i<sz_plaintext;i++) 126 fprintf(stderr," 0x%02x", plaintext[i]); 127 fprintf(stderr,"\n"); 128 } 122 129 123 130 // init … … 128 135 hash.add(&hash,preamble,sz_preamble); 129 136 // plaintext 130 hash.add(&hash, data,len);137 hash.add(&hash,plaintext,sz_plaintext); 131 138 // MDC packet tag 132 139 c[0]=0xD3; … … 138 145 //finish 139 146 hash.finish(&hash,hashed); 147 148 if (debug) 149 { 150 unsigned int i=0; 151 fprintf(stderr,"\nhashed (len=%d): ",SHA_DIGEST_LENGTH); 152 for (i=0; i<SHA_DIGEST_LENGTH;i++) 153 fprintf(stderr," 0x%02x", hashed[i]); 154 fprintf(stderr,"\n"); 155 } 140 156 } 141 157 … … 145 161 ops_create_info_t *cinfo) 146 162 { 163 int debug=0; 147 164 unsigned char hashed[SHA_DIGEST_LENGTH]; 148 165 const size_t sz_mdc=1+1+SHA_DIGEST_LENGTH; … … 165 182 preamble[crypt->blocksize+1]=preamble[crypt->blocksize-1]; 166 183 167 /* debug 168 fprintf(stderr,"\npreamble: "); 169 unsigned int i=0; 170 for (i=0; i<sz_preamble;i++) 171 fprintf(stderr," 0x%02x", preamble[i]); 172 fprintf(stderr,"\n"); 173 */ 184 if (debug) 185 { 186 fprintf(stderr,"\npreamble: "); 187 unsigned int i=0; 188 for (i=0; i<sz_preamble;i++) 189 fprintf(stderr," 0x%02x", preamble[i]); 190 fprintf(stderr,"\n"); 191 } 174 192 175 193 // now construct MDC packet and add to the end of the buffer … … 184 202 ops_write_mdc(hashed, cinfo_mdc); 185 203 204 if (debug) 205 { 206 unsigned int i=0; 207 208 fprintf(stderr,"\nplaintext: "); 209 size_t sz_plaintext=len; 210 for (i=0; i<sz_plaintext;i++) 211 fprintf(stderr," 0x%02x", data[i]); 212 fprintf(stderr,"\n"); 213 214 fprintf(stderr,"\nmdc: "); 215 size_t sz_mdc=1+1+OPS_SHA1_HASH_SIZE; 216 unsigned char* mdc=ops_memory_get_data(mem_mdc); 217 for (i=0; i<sz_mdc;i++) 218 fprintf(stderr," 0x%02x", mdc[i]); 219 fprintf(stderr,"\n"); 220 } 221 186 222 // and write it out 187 223 188 224 ops_writer_push_encrypt_crypt(cinfo, crypt); 189 225 190 /* 191 fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); 192 */ 226 if (debug) 227 { 228 fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); 229 } 193 230 194 231 if (!ops_write(preamble, sz_preamble,cinfo) openpgpsdk/trunk/tests/test_packet_types.c
r510 r511 162 162 // and parse it 163 163 164 ops_memory_init(mem_literal_data,128); 164 165 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 165 166 rtn=ops_parse(pinfo); … … 170 171 */ 171 172 172 CU_ASSERT(strncmp((char *) literal_data,testtext,MAXBUF)==0);173 CU_ASSERT(strncmp((char *)ops_memory_get_data(mem_literal_data),testtext,MAXBUF)==0); 173 174 174 175 // cleanup … … 209 210 // and parse it 210 211 212 ops_memory_init(mem_literal_data,128); 211 213 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 212 214 rtn=ops_parse(pinfo); … … 217 219 */ 218 220 219 CU_ASSERT(memcmp( literal_data,in,MAXBUF)==0);221 CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data),in,MAXBUF)==0); 220 222 221 223 // cleanup … … 339 341 ops_encrypt_init(&pinfo->decrypt); 340 342 343 ops_memory_init(mem_literal_data,0); 341 344 rtn=ops_parse(pinfo); 342 345 CU_ASSERT(rtn==1); … … 346 349 */ 347 350 348 CU_ASSERT(memcmp( literal_data,ldt_text, strlen(ldt_text))==0);351 CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data),ldt_text, strlen(ldt_text))==0); 349 352 350 353 // cleanup … … 453 456 static void local_cleanup() 454 457 { 455 if (literal_data) 456 { 457 free(literal_data); 458 literal_data=NULL; 459 } 458 // ops_memory_init(mem_literal_data); 460 459 461 460 if (mdc_data) openpgpsdk/trunk/tests/test_rsa_decrypt.c
r510 r511 71 71 72 72 case OPS_PTAG_CT_LITERAL_DATA_BODY: 73 /*74 text=ops_mallocz(content->literal_data_body.length+1);75 memcpy(text,content->literal_data_body.data,content->literal_data_body.length);76 */77 73 return callback_literal_data(content_,cbinfo); 78 74 break; … … 239 235 // Do the decryption 240 236 237 ops_memory_init(mem_literal_data,0); 241 238 rtn=ops_parse(pinfo); 242 239 CU_ASSERT(rtn==1); … … 250 247 // File contents should match 251 248 testtext=create_testtext(filename); 252 CU_ASSERT(strlen(testtext)==sz_literal_data); 253 CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); 249 CU_ASSERT(strlen(testtext)==ops_memory_get_length(mem_literal_data)); 250 CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data), 251 testtext, 252 ops_memory_get_length(mem_literal_data))==0); 254 253 } 255 254 openpgpsdk/trunk/tests/test_rsa_encrypt.c
r510 r511 1 1 #include "CUnit/Basic.h" 2 2 3 3 #include <openpgpsdk/types.h> 4 4 #include "openpgpsdk/keyring.h" … … 11 11 12 12 #include "tests.h" 13 14 static int do_gpgtest=0; 13 15 14 16 static char *filename_rsa_noarmour_nopassphrase_singlekey_old="enc_rsa_noarmour_np_singlekey_old.txt"; … … 87 89 int init_suite_rsa_encrypt(void) 88 90 { 91 do_gpgtest=0; 92 89 93 // Create RSA test files 90 94 … … 103 107 } 104 108 109 int init_suite_rsa_encrypt_gpgtest(void) 110 { 111 init_suite_rsa_encrypt(); 112 113 do_gpgtest=1; 114 115 return 0; 116 } 117 105 118 int clean_suite_rsa_encrypt(void) 106 119 { … … 113 126 } 114 127 115 static voidtest_rsa_decrypt(const char *encfile, const char*testtext)128 static int test_rsa_decrypt(const char *encfile, const char*testtext) 116 129 { 117 130 int fd=0; … … 135 148 // Do the decryption 136 149 150 ops_memory_init(mem_literal_data,0); 137 151 rtn=ops_parse(pinfo); 138 152 CU_ASSERT(rtn==1); … … 143 157 144 158 // File contents should match 145 CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); 159 160 CU_ASSERT(memcmp(ops_memory_get_data(mem_literal_data),testtext,ops_memory_get_length(mem_literal_data))==0); 161 162 return rtn; 146 163 } 147 164 … … 240 257 */ 241 258 242 // File contents should match - checking with OPS 243 244 testtext=create_testtext(filename); 245 test_rsa_decrypt(encrypted_file,testtext); 246 247 // File contents should match - check with GPG 248 249 char pp[MAXBUF]; 250 if (pub_key==alpha_pub_keydata) 251 pp[0]='\0'; 252 else if (pub_key==bravo_pub_keydata) 253 snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 254 snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 255 snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 256 // printf("cmd: %s\n", cmd); 257 rtn=system(cmd); 258 CU_ASSERT(rtn==0); 259 CU_ASSERT(file_compare(myfile,decrypted_file)==0); 259 if (!do_gpgtest) 260 { 261 // File contents should match - checking with OPS 262 263 testtext=create_testtext(filename); 264 CU_ASSERT(test_rsa_decrypt(encrypted_file,testtext)==1); 265 } 266 267 else 268 { 269 // File contents should match - check with GPG 270 271 char pp[MAXBUF]; 272 if (pub_key==alpha_pub_keydata) 273 pp[0]='\0'; 274 else if (pub_key==bravo_pub_keydata) 275 snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 276 snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 277 snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 278 // printf("cmd: %s\n", cmd); 279 rtn=system(cmd); 280 CU_ASSERT(rtn==0); 281 CU_ASSERT(file_compare(myfile,decrypted_file)==0); 282 } 260 283 } 261 284 … … 267 290 char decrypted_file[MAXBUF+1]; 268 291 char *suffix= has_armour ? "asc" : "gpg"; 292 char *gpgtest = do_gpgtest ? "gpgtest_" : ""; 269 293 int fd_in=0; 270 294 int fd_out=0; … … 274 298 * Read from test file and write plaintext to memory 275 299 * in set of Literal Data packets 276 * \todo optimise using stacked writers277 300 */ 278 301 … … 286 309 } 287 310 288 snprintf(encrypted_file,MAXBUF,"%s/%s .%s",dir,filename,suffix);311 snprintf(encrypted_file,MAXBUF,"%s/%s%s.%s",dir,gpgtest,filename,suffix); 289 312 fd_out=open(encrypted_file,O_WRONLY | O_CREAT | O_EXCL, 0600); 290 313 if(fd_out < 0) … … 295 318 296 319 /* 297 * This is how to doencryption320 * This shows how to use encryption 298 321 */ 299 322 … … 310 333 // Do the writing 311 334 335 const unsigned bufsz=16; 312 336 for (;;) 313 337 { 314 unsigned char buf[ MAXBUF];338 unsigned char buf[bufsz]; 315 339 int n=0; 316 340 … … 339 363 */ 340 364 341 // File contents should match - checking with OPS 342 343 char* testtext=NULL; 344 testtext=create_testtext(filename); 345 test_rsa_decrypt(encrypted_file,testtext); 346 347 // File contents should match - check with GPG 348 349 char pp[MAXBUF]; 350 if (pub_key==alpha_pub_keydata) 351 pp[0]='\0'; 352 else if (pub_key==bravo_pub_keydata) 353 snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 354 snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 355 snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 356 // printf("cmd: %s\n", cmd); 357 rtn=system(cmd); 358 CU_ASSERT(rtn==0); 359 CU_ASSERT(file_compare(myfile,decrypted_file)==0); 365 if (do_gpgtest) 366 { 367 // File contents should match - check with GPG 368 369 char pp[MAXBUF]; 370 if (pub_key==alpha_pub_keydata) 371 pp[0]='\0'; 372 else if (pub_key==bravo_pub_keydata) 373 snprintf(pp,MAXBUF," --passphrase %s ", bravo_passphrase); 374 snprintf(decrypted_file,MAXBUF,"%s/decrypted_%s",dir,filename); 375 snprintf(cmd,MAXBUF,"gpg --decrypt --output=%s --quiet --homedir %s %s %s",decrypted_file, dir, pp, encrypted_file); 376 // printf("cmd: %s\n", cmd); 377 rtn=system(cmd); 378 CU_ASSERT(rtn==0); 379 CU_ASSERT(file_compare(myfile,decrypted_file)==0); 380 } 381 else 382 { 383 // File contents should match - checking with OPS 384 385 char* testtext=NULL; 386 testtext=create_testtext(filename); 387 test_rsa_decrypt(encrypted_file,testtext); 388 } 360 389 } 361 390 … … 402 431 #endif /*TBD*/ 403 432 433 int add_tests(CU_pSuite suite) 434 { 435 // add tests to suite 436 437 if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase", test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 438 return 0; 439 440 if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase", test_rsa_encrypt_noarmour_passphrase_singlekey)) 441 return 0; 442 443 #ifdef OLD 444 if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase (OLD)", old_test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 445 return 0; 446 447 if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase (OLD)", old_test_rsa_encrypt_noarmour_passphrase_singlekey)) 448 return 0; 449 #endif 450 451 #ifdef TBD 452 if (NULL == CU_add_test(suite, "Armoured, single key", test_rsa_encrypt_armour_singlekey)) 453 return 0; 454 455 if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 456 return 0; 457 #endif /*TBD*/ 458 459 return 1; 460 } 461 404 462 CU_pSuite suite_rsa_encrypt() 405 {463 { 406 464 CU_pSuite suite = NULL; 407 465 … … 410 468 return NULL; 411 469 412 // add tests to suite 413 414 if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase", test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 470 if (!add_tests(suite)) 471 return NULL; 472 473 return suite; 474 } 475 476 CU_pSuite suite_rsa_encrypt_GPGtest() 477 { 478 CU_pSuite suite = NULL; 479 480 suite = CU_add_suite("RSA Encryption Suite (GPG interoperability)", init_suite_rsa_encrypt_gpgtest, clean_suite_rsa_encrypt); 481 482 if (!suite) 415 483 return NULL; 416 417 if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase", test_rsa_encrypt_noarmour_passphrase_singlekey)) 418 return NULL; 419 420 if (NULL == CU_add_test(suite, "Unarmoured, single key, no passphrase (OLD)", old_test_rsa_encrypt_noarmour_nopassphrase_singlekey)) 421 return NULL; 422 423 if (NULL == CU_add_test(suite, "Unarmoured, single key, passphrase (OLD)", old_test_rsa_encrypt_noarmour_passphrase_singlekey)) 424 return NULL; 425 426 #ifdef TBD 427 if (NULL == CU_add_test(suite, "Armoured, single key", test_rsa_encrypt_armour_singlekey)) 428 return NULL; 429 430 if (NULL == CU_add_test(suite, "Armoured, passphrase", test_rsa_encrypt_armour_passphrase)) 431 return NULL; 432 #endif /*TBD*/ 433 484 485 if (!add_tests(suite)) 486 return NULL; 487 434 488 return suite; 435 } 436 489 } 490 491 // EOF openpgpsdk/trunk/tests/tests.c
r510 r511 17 17 ops_keyring_t sec_keyring; 18 18 static char* no_passphrase=""; 19 unsigned char* literal_data=NULL; 20 size_t sz_literal_data=0; 19 ops_memory_t* mem_literal_data=NULL; 21 20 22 21 char *alpha_user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; … … 143 142 { 144 143 144 mem_literal_data=ops_memory_new(); 145 145 setup_test_keys(); 146 146 … … 185 185 } 186 186 #endif 187 188 if (NULL == suite_rsa_encrypt_GPGtest()) 189 { 190 CU_cleanup_registry(); 191 return CU_get_error(); 192 } 187 193 188 194 // Run tests … … 201 207 long int rnd=0; 202 208 while (limit--) 203 { 204 rnd=random(); 205 snprintf(dir,MAXBUF,"./testdir.%ld",rnd); 206 207 // Try to create directory 208 if (!mkdir(dir,0700)) 209 { 210 // success 211 return 1; 212 } 213 else 214 { 215 fprintf (stderr,"Couldn't open dir: errno=%d\n", errno); 216 perror(NULL); 217 } 218 } 219 return 0; 209 { 210 rnd=random(); 211 snprintf(dir,MAXBUF,"./testdir.%ld",rnd); 212 213 // Try to create directory 214 if (!mkdir(dir,0700)) 215 { 216 // success 217 return 1; 218 } 219 else 220 { 221 fprintf (stderr,"Couldn't open dir: errno=%d\n", errno); 222 perror(NULL); 223 } 224 } 225 fprintf(stderr,"Too many temp dirs: please delete them\n"); 226 exit(1); 220 227 } 221 228 222 229 char* create_testtext(const char *text) 223 230 { 224 const unsigned int repeats= 2;231 const unsigned int repeats=100; 225 232 unsigned int i=0; 226 233 … … 394 401 { 395 402 case OPS_PTAG_CT_LITERAL_DATA_BODY: 396 sz_literal_data=content->literal_data_body.length;397 literal_data=ops_mallocz(sz_literal_data+1);398 memcpy(literal_data,content->literal_data_body.data,sz_literal_data);403 ops_memory_add(mem_literal_data, 404 content->literal_data_body.data, 405 content->literal_data_body.length); 399 406 break; 400 407 … … 470 477 void reset_vars() 471 478 { 472 if (literal_data) 473 { 474 free (literal_data); 475 literal_data=NULL; 476 sz_literal_data=0; 477 } 479 ops_memory_init(mem_literal_data,0); 480 478 481 if (decrypter) 479 482 { openpgpsdk/trunk/tests/tests.h
r510 r511 22 22 extern CU_pSuite suite_rsa_decrypt(); 23 23 extern CU_pSuite suite_rsa_encrypt(); 24 extern CU_pSuite suite_rsa_encrypt_GPGtest(); 24 25 extern CU_pSuite suite_rsa_signature(); 25 26 extern CU_pSuite suite_rsa_verify(); … … 52 53 ops_keyring_t pub_keyring; 53 54 ops_keyring_t sec_keyring; 54 unsigned char* literal_data; 55 size_t sz_literal_data; 55 ops_memory_t* mem_literal_data; 56 56 57 57 // "Alpha" is the user who has NO passphrase on his key
