Changeset 466
- Timestamp:
- 04/11/07 17:30:49
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/keyring.h (modified) (1 diff)
- openpgpsdk/trunk/src/advanced/adv_keyring.c (modified) (3 diffs)
- openpgpsdk/trunk/tests/tests.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/keyring.h
r447 r466 39 39 void ops_keyring_read(ops_keyring_t *keyring,const char *file); 40 40 41 char *ops_malloc_passphrase(char *passphrase); 41 42 char *ops_get_passphrase(void); 42 43 openpgpsdk/trunk/src/advanced/adv_keyring.c
r447 r466 88 88 } 89 89 90 char *ops_get_passphrase(void) 91 { 92 char buffer[1024]; 90 char *ops_malloc_passphrase(char *pp) 91 { 93 92 char *passphrase; 94 93 size_t n; 95 94 95 n=strlen(pp); 96 passphrase=malloc(n+1); 97 strcpy(passphrase,pp); 98 99 return passphrase; 100 } 101 102 char *ops_get_passphrase(void) 103 { 104 char buffer[1024]; 105 size_t n; 106 96 107 printf("Passphrase: "); 97 108 98 109 echo_off(); 99 110 fgets(buffer,sizeof buffer,stdin); … … 105 116 if(n && buffer[n-1] == '\n') 106 117 buffer[--n]='\0'; 107 passphrase=malloc(n+1); 108 strcpy(passphrase,buffer); 109 110 return passphrase; 118 return ops_malloc_passphrase(buffer); 111 119 } 112 120 … … 131 139 case OPS_PTAG_CT_USER_ID: 132 140 case OPS_PTAG_CT_SIGNATURE: 141 case OPS_PTAG_CT_SIGNATURE_HEADER: 142 case OPS_PTAG_CT_SIGNATURE_FOOTER: 133 143 case OPS_PTAG_CT_TRUST: 134 144 break; openpgpsdk/trunk/tests/tests.c
r464 r466 17 17 #include "openpgpsdk/std_print.h" 18 18 19 /* 20 These include files are needed by callback. 21 To be removed when callback gets added to main body of code 22 */ 23 #include "../src/advanced/parse_local.h" 24 #include "../src/advanced/keyring_local.h" 25 19 26 #define MAXBUF 128 20 27 static char secring[MAXBUF+1]; 21 28 static char dir[MAXBUF+1]; 22 static char file[MAXBUF+1];23 29 static char keydetails[MAXBUF+1]; 24 30 static ops_keyring_t keyring; 25 static char* testtxt="Hello World\n"; 31 static char *filename_rsa_noarmour_nopassphrase="rsa_noarmour_nopassphrase.txt"; 32 static char *filename_rsa_armour_nopassphrase="rsa_armour_nopassphrase.txt"; 33 static char *filename_rsa_noarmour_passphrase="rsa_noarmour_passphrase.txt"; 34 static char *filename_rsa_armour_passphrase="rsa_armour_passphrase.txt"; 35 static char *nopassphrase=""; 36 static char *passphrase="hello"; 37 static char *current_passphrase=NULL; 38 26 39 static char* text; 40 41 static char *create_testtext(const char *filename) 42 { 43 static char buffer[MAXBUF+1]; 44 snprintf(buffer,MAXBUF,"Hello world : %s/%s\n", dir, filename); 45 return &buffer[0]; 46 } 47 48 static int create_testfile(const char *name) 49 { 50 char filename[MAXBUF+1]; 51 char buffer[MAXBUF+1]; 52 53 int fd=0; 54 snprintf(filename,MAXBUF,"%s/%s",dir,name); 55 if ((fd=open(filename,O_WRONLY| O_CREAT | O_EXCL, 0600))<0) 56 return 0; 57 58 snprintf(buffer,MAXBUF,create_testtext(name)); 59 write(fd,buffer,strlen(buffer)); 60 close(fd); 61 return 1; 62 } 27 63 28 64 static ops_parse_cb_return_t … … 32 68 static ops_boolean_t skipping; 33 69 static const ops_key_data_t *decrypter; 34 const ops_key_data_t *key=NULL; 70 const ops_key_data_t *keydata=NULL; 71 const ops_secret_key_t *secret; 35 72 36 73 OPS_USED(cbinfo); … … 69 106 70 107 case OPS_PARSER_CMD_GET_SECRET_KEY: 71 key =ops_keyring_find_key_by_id(&keyring,content->get_secret_key.pk_session_key->key_id);72 if (!key || !ops_key_is_secret(key))108 keydata=ops_keyring_find_key_by_id(&keyring,content->get_secret_key.pk_session_key->key_id); 109 if (!keydata || !ops_key_is_secret(keydata)) 73 110 return 0; 74 111 75 ops_set_secret_key(content,key); 76 112 // ops_set_secret_key(content,keydata); 113 114 // Do we need the passphrase and not have it? If so, get it 115 ops_parser_content_t pc; 116 char *passphrase; 117 memset(&pc,'\0',sizeof pc); 118 passphrase=NULL; 119 pc.content.secret_key_passphrase.passphrase=&passphrase; 120 pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey); 121 122 /* Ugh. Need to duplicate this macro here to get the passphrase 123 Duplication to be removed when the callback gets moved to main code. 124 Can we make this inline code rather than a macro? 125 */ 126 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 127 CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 128 129 /* now get the key from the data */ 130 secret=ops_get_secret_key_from_data(keydata); 131 while(!secret) 132 { 133 /* then it must be encrypted */ 134 secret=ops_decrypt_secret_key_from_data(keydata,passphrase); 135 free(passphrase); 136 } 137 138 *content->get_secret_key.secret_key=secret; 139 140 break; 141 142 case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 143 /* 144 Doing this so the test can be automated. 145 Will move this into separate stacked callback later 146 */ 147 *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(current_passphrase); 148 return OPS_KEEP_MEMORY; 77 149 break; 78 150 … … 135 207 int init_suite_rsa_decrypt(void) 136 208 { 137 char *textfile="testfile.txt";138 209 int fd=0; 210 char cmd[MAXBUF+1]; 211 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"; 212 char *rsa_pass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Bravo\nName-Comment: RSA, passphrase\nName-Email: bravo@test.com\nPassphrase: hello\nKey-Length: 1024\n"; 139 213 140 214 // Create temp directory … … 142 216 return 1; 143 217 144 // printf("creating new file\n"); 145 // Create a new unencrypted test file 146 snprintf(file,MAXBUF,"%s/%s",dir,textfile);147 148 if ((fd=open(file,O_WRONLY| O_CREAT | O_EXCL, 0600))<0)149 return 1;150 write(fd,testtxt,strlen(testtxt)); 151 close(fd);152 153 // create new keyrings in that directory154 // and a new RSA keypair with no passphrase 155 156 snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails"); 218 // Create RSA test files 219 220 create_testfile(filename_rsa_noarmour_nopassphrase); 221 create_testfile(filename_rsa_armour_nopassphrase); 222 create_testfile(filename_rsa_noarmour_passphrase); 223 create_testfile(filename_rsa_armour_passphrase); 224 225 /* 226 * Create a RSA keypair with no passphrase 227 */ 228 229 snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 230 157 231 if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 158 232 { … … 161 235 } 162 236 163 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";164 237 write(fd,rsa_nopass,strlen(rsa_nopass)); 165 238 close(fd); 166 239 167 char cmd[MAXBUF+1]; 168 snprintf(cmd,MAXBUF,"gpg --gen-key --expert --homedir=%s --batch %s 2>&1 > /dev/null",dir,keydetails); 169 //printf("cmd: %s\n", cmd); 240 snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 170 241 system(cmd); 171 242 172 243 // Now encrypt the test file with GPG 173 snprintf(cmd,MAXBUF,"gpg -- encrypt --homedir=%s --recipient Alpha %s 2>&1 > /dev/null", dir, file);244 snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase); 174 245 if (system(cmd)) 175 246 { … … 178 249 179 250 // Now encrypt and ascii-armour the test file with GPG 180 snprintf(cmd,MAXBUF,"gpg -- encrypt --armor --homedir=%s --recipient Alpha %s 2>&1 > /dev/null", dir, file);251 snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_armour_nopassphrase); 181 252 if (system(cmd)) 182 253 { 183 254 return 1; 184 255 } 185 256 257 /* 258 * Create a RSA keypair with passphrase 259 */ 260 261 snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.bravo"); 262 if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 263 { 264 fprintf(stderr,"Can't create key details\n"); 265 return 1; 266 } 267 268 write(fd,rsa_pass,strlen(rsa_pass)); 269 close(fd); 270 271 snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 272 system(cmd); 273 274 // Now encrypt the test file with GPG 275 snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_noarmour_passphrase); 276 if (system(cmd)) 277 { 278 return 1; 279 } 280 281 // Now encrypt and ascii-armour the test file with GPG 282 snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_armour_passphrase); 283 if (system(cmd)) 284 { 285 return 1; 286 } 287 288 // Initialise OPS 289 ops_init(); 290 291 // read keyring 186 292 snprintf(secring,MAXBUF,"%s/secring.gpg", dir); 187 188 // Initialise OPS and read keyring189 ops_init();190 293 ops_keyring_read(&keyring,secring); 191 294 … … 198 301 char cmd[MAXBUF+1]; 199 302 200 /* Close OPS */201 303 /* Close OPS */ 304 202 305 ops_keyring_free(&keyring); 203 306 ops_finish(); 204 307 205 /* Remove test dir and files */308 /* Remove test dir and files */ 206 309 snprintf(cmd,MAXBUF,"rm -rf %s", dir); 207 310 if (system(cmd)) … … 210 313 return 1; 211 314 } 212 315 213 316 return 0; 214 317 } 215 318 216 void test_rsa_decryption_unarmoured_nopassphrase(void) 217 { 218 /* 219 * TEST: RSA encrypted file, unarmoured, no passphrase 220 */ 221 319 static void test_rsa_decrypt(const int has_armour, const int has_passphrase, const char *filename) 320 { 222 321 char encfile[MAXBUF+1]; 322 char *suffix= has_armour ? "asc" : "gpg"; 223 323 int fd=0; 224 324 ops_parse_info_t *pinfo; 225 226 // readencrypted file227 snprintf(encfile,MAXBUF,"%s .gpg", file);325 326 // open encrypted file 327 snprintf(encfile,MAXBUF,"%s/%s.%s",dir,filename,suffix); 228 328 fd=open(encfile,O_RDONLY); 229 329 if(fd < 0) … … 232 332 exit(2); 233 333 } 234 235 // Now do file 334 335 // Set decryption reader and handling options 336 236 337 pinfo=ops_parse_info_new(); 237 338 ops_reader_set_fd(pinfo,fd); 238 339 ops_parse_cb_set(pinfo,callback,NULL); 239 340 341 // Set up armour/passphrase options 342 343 if (has_armour) 344 ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); 345 current_passphrase=has_passphrase ? passphrase : nopassphrase; 346 347 // Do the decryption 348 240 349 ops_parse(pinfo); 241 350 242 close(fd); 243 351 // Tidy up 352 if (has_armour) 353 ops_reader_pop_dearmour(pinfo); 354 355 close(fd); 356 244 357 // File contents should match 245 CU_ASSERT(strcmp(text,testtxt)==0); 246 247 } 248 249 void test_rsa_decryption_armoured_nopassphrase(void) 250 { 251 /* 252 * TEST: RSA encrypted file, armoured, no passphrase 253 */ 254 255 char encfile[MAXBUF+1]; 256 int fd=0; 257 ops_parse_info_t *pinfo; 258 259 // read encrypted file 260 snprintf(encfile,MAXBUF,"%s.asc", file); 261 fd=open(encfile,O_RDONLY); 262 if(fd < 0) 263 { 264 perror(encfile); 265 exit(2); 266 } 267 268 // Now do file 269 pinfo=ops_parse_info_new(); 270 ops_reader_set_fd(pinfo,fd); 271 ops_parse_cb_set(pinfo,callback,NULL); 272 273 ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); 274 ops_parse(pinfo); 275 ops_reader_pop_dearmour(pinfo); 276 277 close(fd); 278 279 // File contents should match 280 CU_ASSERT(strcmp(text,testtxt)==0); 281 358 CU_ASSERT(strcmp(text,create_testtext(filename))==0); 359 } 360 361 void test_rsa_decrypt_noarmour_nopassphrase(void) 362 { 363 int armour=0; 364 int passphrase=0; 365 test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_nopassphrase); 366 } 367 368 void test_rsa_decrypt_armour_nopassphrase(void) 369 { 370 int armour=1; 371 int passphrase=0; 372 test_rsa_decrypt(armour,passphrase,filename_rsa_armour_nopassphrase); 373 } 374 375 void test_rsa_decrypt_noarmour_passphrase(void) 376 { 377 int armour=0; 378 int passphrase=1; 379 test_rsa_decrypt(armour,passphrase,filename_rsa_noarmour_passphrase); 380 } 381 382 void test_rsa_decrypt_armour_passphrase(void) 383 { 384 int armour=1; 385 int passphrase=1; 386 test_rsa_decrypt(armour,passphrase,filename_rsa_armour_passphrase); 282 387 } 283 388 … … 295 400 return CU_get_error(); 296 401 } 297 402 298 403 // add tests to suite 299 300 if (NULL == CU_add_test(pSuite, "Unarmoured, no passphrase", test_rsa_decrypt ion_unarmoured_nopassphrase))404 405 if (NULL == CU_add_test(pSuite, "Unarmoured, no passphrase", test_rsa_decrypt_noarmour_nopassphrase)) 301 406 { 302 407 CU_cleanup_registry(); 303 408 return CU_get_error(); 304 409 } 305 306 if (NULL == CU_add_test(pSuite, "Armoured, no passphrase", test_rsa_decrypt ion_armoured_nopassphrase))410 411 if (NULL == CU_add_test(pSuite, "Armoured, no passphrase", test_rsa_decrypt_armour_nopassphrase)) 307 412 { 308 413 CU_cleanup_registry(); 309 414 return CU_get_error(); 310 415 } 311 416 417 if (NULL == CU_add_test(pSuite, "Unarmoured, passphrase", test_rsa_decrypt_noarmour_passphrase)) 418 { 419 CU_cleanup_registry(); 420 return CU_get_error(); 421 } 422 423 if (NULL == CU_add_test(pSuite, "Armoured, passphrase", test_rsa_decrypt_armour_passphrase)) 424 { 425 CU_cleanup_registry(); 426 return CU_get_error(); 427 } 428 312 429 // Run tests 313 430 CU_basic_set_mode(CU_BRM_VERBOSE);
