| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#include "tests.h" |
|---|
| 23 |
|
|---|
| 24 |
#include "CUnit/Basic.h" |
|---|
| 25 |
|
|---|
| 26 |
#include <openpgpsdk/types.h> |
|---|
| 27 |
#include "openpgpsdk/keyring.h" |
|---|
| 28 |
#include <openpgpsdk/armour.h> |
|---|
| 29 |
#include "openpgpsdk/packet.h" |
|---|
| 30 |
#include "openpgpsdk/packet-parse.h" |
|---|
| 31 |
#include "openpgpsdk/readerwriter.h" |
|---|
| 32 |
#include "openpgpsdk/util.h" |
|---|
| 33 |
#include "openpgpsdk/std_print.h" |
|---|
| 34 |
#include "../src/lib/parse_local.h" |
|---|
| 35 |
|
|---|
| 36 |
static char *compress_algos[]={ "zip", "zlib", "bzip2" }; |
|---|
| 37 |
static int n_compress_algos=3; |
|---|
| 38 |
|
|---|
| 39 |
static ops_parse_cb_return_t |
|---|
| 40 |
callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) |
|---|
| 41 |
{ |
|---|
| 42 |
ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; |
|---|
| 43 |
static ops_boolean_t skipping; |
|---|
| 44 |
|
|---|
| 45 |
OPS_USED(cbinfo); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
if(content_->tag != OPS_PTAG_CT_UNARMOURED_TEXT && skipping) |
|---|
| 50 |
{ |
|---|
| 51 |
puts("...end of skip"); |
|---|
| 52 |
skipping=ops_false; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
switch(content_->tag) |
|---|
| 56 |
{ |
|---|
| 57 |
case OPS_PTAG_CT_UNARMOURED_TEXT: |
|---|
| 58 |
printf("OPS_PTAG_CT_UNARMOURED_TEXT\n"); |
|---|
| 59 |
if(!skipping) |
|---|
| 60 |
{ |
|---|
| 61 |
puts("Skipping..."); |
|---|
| 62 |
skipping=ops_true; |
|---|
| 63 |
} |
|---|
| 64 |
fwrite(content->unarmoured_text.data,1, |
|---|
| 65 |
content->unarmoured_text.length,stdout); |
|---|
| 66 |
break; |
|---|
| 67 |
|
|---|
| 68 |
case OPS_PTAG_CT_PK_SESSION_KEY: |
|---|
| 69 |
return callback_pk_session_key(content_,cbinfo); |
|---|
| 70 |
break; |
|---|
| 71 |
|
|---|
| 72 |
case OPS_PARSER_CMD_GET_SECRET_KEY: |
|---|
| 73 |
return callback_cmd_get_secret_key(content_,cbinfo); |
|---|
| 74 |
break; |
|---|
| 75 |
|
|---|
| 76 |
case OPS_PARSER_CMD_GET_SK_PASSPHRASE: |
|---|
| 77 |
return test_cb_get_passphrase(content_,cbinfo); |
|---|
| 78 |
break; |
|---|
| 79 |
|
|---|
| 80 |
case OPS_PTAG_CT_LITERAL_DATA_BODY: |
|---|
| 81 |
return callback_literal_data(content_,cbinfo); |
|---|
| 82 |
break; |
|---|
| 83 |
|
|---|
| 84 |
case OPS_PTAG_CT_ARMOUR_HEADER: |
|---|
| 85 |
case OPS_PTAG_CT_ARMOUR_TRAILER: |
|---|
| 86 |
case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: |
|---|
| 87 |
case OPS_PTAG_CT_COMPRESSED: |
|---|
| 88 |
case OPS_PTAG_CT_LITERAL_DATA_HEADER: |
|---|
| 89 |
case OPS_PTAG_CT_SE_IP_DATA_BODY: |
|---|
| 90 |
case OPS_PTAG_CT_SE_IP_DATA_HEADER: |
|---|
| 91 |
case OPS_PTAG_CT_SE_DATA_BODY: |
|---|
| 92 |
case OPS_PTAG_CT_SE_DATA_HEADER: |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
break; |
|---|
| 98 |
|
|---|
| 99 |
default: |
|---|
| 100 |
return callback_general(content_,cbinfo); |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
return OPS_RELEASE_MEMORY; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
int init_suite_rsa_decrypt(void) |
|---|
| 115 |
{ |
|---|
| 116 |
|
|---|
| 117 |
return 0; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
int clean_suite_rsa_decrypt(void) |
|---|
| 121 |
{ |
|---|
| 122 |
|
|---|
| 123 |
reset_vars(); |
|---|
| 124 |
|
|---|
| 125 |
return 0; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
static void test_rsa_decrypt(const int has_armour, const char *filename) |
|---|
| 129 |
{ |
|---|
| 130 |
char encfile[MAXBUF+1]; |
|---|
| 131 |
char* testtext=NULL; |
|---|
| 132 |
char *suffix= has_armour ? "asc" : "gpg"; |
|---|
| 133 |
int fd=0; |
|---|
| 134 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 135 |
ops_memory_t* mem_out=NULL; |
|---|
| 136 |
int rtn=0; |
|---|
| 137 |
int repeats=10; |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
snprintf(encfile,sizeof encfile,"%s/%s.%s",dir, |
|---|
| 141 |
filename,suffix); |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
ops_setup_file_read(&pinfo, encfile, |
|---|
| 145 |
NULL, |
|---|
| 146 |
callback, |
|---|
| 147 |
ops_false |
|---|
| 148 |
); |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
pinfo->cbinfo.cryptinfo.keyring=&sec_keyring; |
|---|
| 152 |
pinfo->cbinfo.cryptinfo.cb_get_passphrase=test_cb_get_passphrase; |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
if (has_armour) |
|---|
| 157 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
ops_setup_memory_write(&pinfo->cbinfo.cinfo, &mem_out, 128); |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
rtn=ops_parse_and_print_errors(pinfo); |
|---|
| 164 |
CU_ASSERT(rtn==1); |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
if (has_armour) |
|---|
| 168 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 169 |
|
|---|
| 170 |
close(fd); |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
testtext=create_testtext(filename,repeats); |
|---|
| 174 |
CU_ASSERT(strlen(testtext)==ops_memory_get_length(mem_out)); |
|---|
| 175 |
CU_ASSERT(memcmp(ops_memory_get_data(mem_out), |
|---|
| 176 |
testtext, |
|---|
| 177 |
ops_memory_get_length(mem_out))==0); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
static void create_filename(char* buf, int maxbuf, int armour, int passphrase, char * sym_alg, char * compress_alg, int compress_level) |
|---|
| 181 |
{ |
|---|
| 182 |
snprintf(buf,maxbuf,"gpg_enc_rsa_%s_%s_%s_%s_%d.txt", |
|---|
| 183 |
armour ? "arm" : "noarm", |
|---|
| 184 |
passphrase ? "pp" : "nopp", |
|---|
| 185 |
sym_alg, |
|---|
| 186 |
compress_alg, compress_level); |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
static void test_rsa_decrypt_generic(char* sym_alg) |
|---|
| 190 |
{ |
|---|
| 191 |
char filename[MAXBUF+1]; |
|---|
| 192 |
char cmd[MAXBUF+1]; |
|---|
| 193 |
int armour=0; |
|---|
| 194 |
int passphrase=0; |
|---|
| 195 |
int compress_alg=0; |
|---|
| 196 |
int compress_lvl=0; |
|---|
| 197 |
|
|---|
| 198 |
for (compress_alg=0; compress_alg<n_compress_algos; compress_alg++) |
|---|
| 199 |
{ |
|---|
| 200 |
for (compress_lvl=0; compress_lvl<=MAX_COMPRESS_LEVEL; compress_lvl++) |
|---|
| 201 |
{ |
|---|
| 202 |
|
|---|
| 203 |
if (compress_lvl>0 && compress_lvl < MAX_COMPRESS_LEVEL) |
|---|
| 204 |
continue; |
|---|
| 205 |
for (armour=0; armour<=1; armour++) |
|---|
| 206 |
{ |
|---|
| 207 |
char *armour_cmd= armour ? "--armor " : ""; |
|---|
| 208 |
char *suffix= armour ? "asc" : "gpg"; |
|---|
| 209 |
|
|---|
| 210 |
for (passphrase=0; passphrase<=1; passphrase++) |
|---|
| 211 |
{ |
|---|
| 212 |
char *rcpt= passphrase ? "Bravo" : "Alpha"; |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
create_filename(&filename[0],sizeof filename, |
|---|
| 216 |
armour, passphrase, |
|---|
| 217 |
sym_alg, |
|---|
| 218 |
compress_algos[compress_alg], compress_lvl); |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
create_small_testfile(filename); |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
snprintf(cmd,sizeof cmd,"gpg --quiet --no-tty --homedir=%s --cipher-algo \"%s\" --compress-algo \"%s\" --compress-level %d --output=%s/%s.%s --force-mdc --encrypt --recipient %s %s %s/%s", |
|---|
| 225 |
dir, |
|---|
| 226 |
sym_alg, |
|---|
| 227 |
compress_algos[compress_alg], |
|---|
| 228 |
compress_lvl, |
|---|
| 229 |
dir, filename, suffix, |
|---|
| 230 |
rcpt, |
|---|
| 231 |
armour_cmd, |
|---|
| 232 |
dir, filename); |
|---|
| 233 |
if (system(cmd)) |
|---|
| 234 |
{ |
|---|
| 235 |
fprintf(stderr,"Err: cmd is %s\n", cmd); |
|---|
| 236 |
|
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
test_rsa_decrypt(armour,filename); |
|---|
| 241 |
} |
|---|
| 242 |
} |
|---|
| 243 |
} |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
static void test_rsa_decrypt_cast5(void) |
|---|
| 248 |
{ |
|---|
| 249 |
return test_rsa_decrypt_generic("cast5"); |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
static void test_rsa_decrypt_aes128(void) |
|---|
| 253 |
{ |
|---|
| 254 |
return test_rsa_decrypt_generic("aes"); |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
static void test_rsa_decrypt_aes256(void) |
|---|
| 258 |
{ |
|---|
| 259 |
return test_rsa_decrypt_generic("aes256"); |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
static void test_rsa_decrypt_3des(void) |
|---|
| 263 |
{ |
|---|
| 264 |
return test_rsa_decrypt_generic("3des"); |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
static int add_tests(CU_pSuite suite) |
|---|
| 279 |
{ |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
if (NULL == CU_add_test(suite, "CAST5", test_rsa_decrypt_cast5)) |
|---|
| 283 |
return 0; |
|---|
| 284 |
|
|---|
| 285 |
if (NULL == CU_add_test(suite, "AES128", test_rsa_decrypt_aes128)) |
|---|
| 286 |
return 0; |
|---|
| 287 |
|
|---|
| 288 |
if (NULL == CU_add_test(suite, "AES256", test_rsa_decrypt_aes256)) |
|---|
| 289 |
return 0; |
|---|
| 290 |
|
|---|
| 291 |
if (NULL == CU_add_test(suite, "3DES", test_rsa_decrypt_3des)) |
|---|
| 292 |
return 0; |
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
return 1; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
CU_pSuite suite_rsa_decrypt() |
|---|
| 302 |
{ |
|---|
| 303 |
CU_pSuite suite = NULL; |
|---|
| 304 |
|
|---|
| 305 |
suite = CU_add_suite("RSA Decryption Suite", init_suite_rsa_decrypt, clean_suite_rsa_decrypt); |
|---|
| 306 |
if (!suite) |
|---|
| 307 |
return NULL; |
|---|
| 308 |
|
|---|
| 309 |
if (!add_tests(suite)) |
|---|
| 310 |
return NULL; |
|---|
| 311 |
|
|---|
| 312 |
return suite; |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|