| 1 |
#include "CUnit/Basic.h" |
|---|
| 2 |
|
|---|
| 3 |
#include <openpgpsdk/types.h> |
|---|
| 4 |
#include "openpgpsdk/keyring.h" |
|---|
| 5 |
#include <openpgpsdk/armour.h> |
|---|
| 6 |
#include "openpgpsdk/packet.h" |
|---|
| 7 |
#include "openpgpsdk/packet-parse.h" |
|---|
| 8 |
#include "openpgpsdk/util.h" |
|---|
| 9 |
#include "openpgpsdk/std_print.h" |
|---|
| 10 |
#include "openpgpsdk/readerwriter.h" |
|---|
| 11 |
#include "openpgpsdk/validate.h" |
|---|
| 12 |
|
|---|
| 13 |
#include "tests.h" |
|---|
| 14 |
|
|---|
| 15 |
#ifndef ATTRIBUTE_UNUSED |
|---|
| 16 |
|
|---|
| 17 |
#ifndef WIN32 |
|---|
| 18 |
#define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) |
|---|
| 19 |
#else |
|---|
| 20 |
#define ATTRIBUTE_UNUSED |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#endif |
|---|
| 24 |
|
|---|
| 25 |
static char *filename_rsa_noarmour_nopassphrase="gpg_signed_noarmour_nopassphrase.txt"; |
|---|
| 26 |
static char *filename_rsa_armour_nopassphrase="gpg_signed_armour_nopassphrase.txt"; |
|---|
| 27 |
static char *filename_rsa_noarmour_passphrase="gpg_signed_armour_nopassphrase.txt"; |
|---|
| 28 |
static char *filename_rsa_armour_passphrase="gpg_signed_armour_passphrase.txt"; |
|---|
| 29 |
|
|---|
| 30 |
static ops_parse_cb_return_t |
|---|
| 31 |
callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) |
|---|
| 32 |
{ |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
switch(content_->tag) |
|---|
| 38 |
{ |
|---|
| 39 |
case OPS_PTAG_CT_LITERAL_DATA_HEADER: |
|---|
| 40 |
break; |
|---|
| 41 |
|
|---|
| 42 |
case OPS_PTAG_CT_LITERAL_DATA_BODY: |
|---|
| 43 |
return callback_literal_data(content_,cbinfo); |
|---|
| 44 |
break; |
|---|
| 45 |
|
|---|
| 46 |
case OPS_PTAG_CT_ONE_PASS_SIGNATURE: |
|---|
| 47 |
case OPS_PTAG_CT_SIGNATURE: |
|---|
| 48 |
break; |
|---|
| 49 |
|
|---|
| 50 |
case OPS_PTAG_CT_SIGNATURE_HEADER: |
|---|
| 51 |
case OPS_PTAG_CT_SIGNATURE_FOOTER: |
|---|
| 52 |
return callback_signature(content_, cbinfo); |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
default: |
|---|
| 95 |
return callback_general(content_,cbinfo); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
return OPS_RELEASE_MEMORY; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
int init_suite_rsa_verify(void) |
|---|
| 106 |
{ |
|---|
| 107 |
char cmd[MAXBUF+1]; |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
create_testfile(filename_rsa_noarmour_nopassphrase); |
|---|
| 112 |
create_testfile(filename_rsa_armour_nopassphrase); |
|---|
| 113 |
create_testfile(filename_rsa_noarmour_passphrase); |
|---|
| 114 |
create_testfile(filename_rsa_armour_passphrase); |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --openpgp --compress-level 0 --sign --local-user %s %s/%s", |
|---|
| 119 |
dir, alpha_name, dir, filename_rsa_noarmour_nopassphrase); |
|---|
| 120 |
if (system(cmd)) |
|---|
| 121 |
{ return 1; } |
|---|
| 122 |
|
|---|
| 123 |
snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --armour --local-user %s %s/%s", |
|---|
| 124 |
dir, alpha_name, dir, filename_rsa_armour_nopassphrase); |
|---|
| 125 |
if (system(cmd)) |
|---|
| 126 |
{ return 1; } |
|---|
| 127 |
|
|---|
| 128 |
snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --local-user %s --passphrase %s %s/%s", |
|---|
| 129 |
dir, bravo_name, bravo_passphrase, dir, filename_rsa_noarmour_passphrase); |
|---|
| 130 |
if (system(cmd)) |
|---|
| 131 |
{ return 1; } |
|---|
| 132 |
|
|---|
| 133 |
snprintf(cmd,MAXBUF,"gpg --homedir=%s --quiet --compress-level 0 --sign --armour --local-user %s --passphrase %s %s/%s", |
|---|
| 134 |
dir, bravo_name, bravo_passphrase, dir, filename_rsa_armour_passphrase); |
|---|
| 135 |
if (system(cmd)) |
|---|
| 136 |
{ return 1; } |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
return 0; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
int clean_suite_rsa_verify(void) |
|---|
| 143 |
{ |
|---|
| 144 |
ops_finish(); |
|---|
| 145 |
|
|---|
| 146 |
reset_vars(); |
|---|
| 147 |
|
|---|
| 148 |
return 0; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
static void test_rsa_verify(const int has_armour, const int has_passphrase ATTRIBUTE_UNUSED, const char *filename, const char* protocol) |
|---|
| 152 |
{ |
|---|
| 153 |
char signedfile[MAXBUF+1]; |
|---|
| 154 |
|
|---|
| 155 |
char *suffix= has_armour ? "asc" : "gpg"; |
|---|
| 156 |
int fd=0; |
|---|
| 157 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 158 |
validate_cb_arg_t validate_arg; |
|---|
| 159 |
ops_validate_result_t result; |
|---|
| 160 |
int rtn=0; |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
snprintf(signedfile,MAXBUF,"%s/%s%s%s.%s",dir, |
|---|
| 164 |
protocol==NULL ? "" : protocol, |
|---|
| 165 |
protocol==NULL ? "" : "_", |
|---|
| 166 |
filename,suffix); |
|---|
| 167 |
#ifdef WIN32 |
|---|
| 168 |
fd=open(signedfile,O_RDONLY | O_BINARY); |
|---|
| 169 |
#else |
|---|
| 170 |
fd=open(signedfile,O_RDONLY); |
|---|
| 171 |
#endif |
|---|
| 172 |
if(fd < 0) |
|---|
| 173 |
{ |
|---|
| 174 |
perror(signedfile); |
|---|
| 175 |
exit(2); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
pinfo=ops_parse_info_new(); |
|---|
| 181 |
ops_parse_cb_set(pinfo,callback,&validate_arg); |
|---|
| 182 |
ops_reader_set_fd(pinfo,fd); |
|---|
| 183 |
|
|---|
| 184 |
memset(&validate_arg,'\0',sizeof validate_arg); |
|---|
| 185 |
validate_arg.result=&result; |
|---|
| 186 |
validate_arg.keyring=&pub_keyring; |
|---|
| 187 |
validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
if (has_armour) |
|---|
| 192 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
rtn=ops_parse(pinfo); |
|---|
| 198 |
ops_print_errors(ops_parse_info_get_errors(pinfo)); |
|---|
| 199 |
CU_ASSERT(rtn==1); |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
if (has_armour) |
|---|
| 203 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 204 |
|
|---|
| 205 |
ops_public_key_free(&validate_arg.pkey); |
|---|
| 206 |
if (validate_arg.subkey.version) |
|---|
| 207 |
ops_public_key_free(&validate_arg.subkey); |
|---|
| 208 |
ops_user_id_free(&validate_arg.user_id); |
|---|
| 209 |
ops_user_attribute_free(&validate_arg.user_attribute); |
|---|
| 210 |
ops_parse_info_delete(pinfo); |
|---|
| 211 |
|
|---|
| 212 |
close(fd); |
|---|
| 213 |
|
|---|
| 214 |
#ifdef NEEDED |
|---|
| 215 |
|
|---|
| 216 |
create_testtext(filename,&testtext[0],MAXBUF); |
|---|
| 217 |
CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); |
|---|
| 218 |
#endif |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
void test_rsa_verify_noarmour_nopassphrase(void) |
|---|
| 222 |
{ |
|---|
| 223 |
int armour=0; |
|---|
| 224 |
int passphrase=0; |
|---|
| 225 |
assert(pub_keyring.nkeys); |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
test_rsa_verify(armour,passphrase,filename_rsa_noarmour_nopassphrase,NULL); |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
#ifdef TBD |
|---|
| 232 |
void test_rsa_encrypt_armour_singlekey(void) |
|---|
| 233 |
{ |
|---|
| 234 |
int armour=1; |
|---|
| 235 |
char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; |
|---|
| 236 |
const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); |
|---|
| 237 |
assert(pub_key); |
|---|
| 238 |
test_rsa_encrypt(armour,pub_key,filename_rsa_armour_singlekey); |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
void test_rsa_encrypt_noarmour_passphrase(void) |
|---|
| 242 |
{ |
|---|
| 243 |
int armour=0; |
|---|
| 244 |
int passphrase=1; |
|---|
| 245 |
test_rsa_encrypt(armour,passphrase,filename_rsa_noarmour_passphrase); |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
void test_rsa_encrypt_armour_passphrase(void) |
|---|
| 249 |
{ |
|---|
| 250 |
int armour=1; |
|---|
| 251 |
int passphrase=1; |
|---|
| 252 |
test_rsa_encrypt(armour,passphrase,filename_rsa_armour_passphrase); |
|---|
| 253 |
} |
|---|
| 254 |
#endif |
|---|
| 255 |
|
|---|
| 256 |
CU_pSuite suite_rsa_verify() |
|---|
| 257 |
{ |
|---|
| 258 |
CU_pSuite suite = NULL; |
|---|
| 259 |
|
|---|
| 260 |
suite = CU_add_suite("RSA Verification Suite", init_suite_rsa_verify, clean_suite_rsa_verify); |
|---|
| 261 |
if (!suite) |
|---|
| 262 |
return NULL; |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", test_rsa_verify_noarmour_nopassphrase)) |
|---|
| 267 |
return NULL; |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
return suite; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|