| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
#include "CUnit/Basic.h" |
|---|
| 26 |
|
|---|
| 27 |
#include <openpgpsdk/defs.h> |
|---|
| 28 |
#include <openpgpsdk/types.h> |
|---|
| 29 |
#include "openpgpsdk/keyring.h" |
|---|
| 30 |
#include <openpgpsdk/armour.h> |
|---|
| 31 |
#include <openpgpsdk/create.h> |
|---|
| 32 |
#include "openpgpsdk/packet.h" |
|---|
| 33 |
#include "openpgpsdk/packet-parse.h" |
|---|
| 34 |
#include "openpgpsdk/packet-show.h" |
|---|
| 35 |
#include "openpgpsdk/util.h" |
|---|
| 36 |
#include "openpgpsdk/std_print.h" |
|---|
| 37 |
#include "openpgpsdk/readerwriter.h" |
|---|
| 38 |
#include "openpgpsdk/validate.h" |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
#include "../src/lib/parse_local.h" |
|---|
| 42 |
|
|---|
| 43 |
#include "tests.h" |
|---|
| 44 |
|
|---|
| 45 |
static int debug=0; |
|---|
| 46 |
|
|---|
| 47 |
static const char filename_rsa_large_noarmour_nopassphrase[] |
|---|
| 48 |
="ops_rsa_signed_large_noarmour_nopassphrase.txt"; |
|---|
| 49 |
static const char filename_rsa_large_armour_nopassphrase[] |
|---|
| 50 |
="ops_rsa_signed_large_armour_nopassphrase.txt"; |
|---|
| 51 |
static const char filename_rsa_noarmour_nopassphrase[] |
|---|
| 52 |
="ops_rsa_signed_noarmour_nopassphrase.txt"; |
|---|
| 53 |
static const char filename_rsa_noarmour_passphrase[] |
|---|
| 54 |
="ops_rsa_signed_noarmour_passphrase.txt"; |
|---|
| 55 |
static const char filename_rsa_armour_nopassphrase[] |
|---|
| 56 |
="ops_rsa_signed_armour_nopassphrase.txt"; |
|---|
| 57 |
static const char filename_rsa_armour_passphrase[] |
|---|
| 58 |
="ops_rsa_signed_armour_passphrase.txt"; |
|---|
| 59 |
static const char filename_rsa_clearsign_file_nopassphrase[] |
|---|
| 60 |
="ops_rsa_signed_clearsign_file_nopassphrase.txt"; |
|---|
| 61 |
static const char filename_rsa_clearsign_file_passphrase[] |
|---|
| 62 |
="ops_rsa_signed_clearsign_file_passphrase.txt"; |
|---|
| 63 |
static const char filename_rsa_clearsign_buf_nopassphrase[] |
|---|
| 64 |
="ops_rsa_signed_clearsign_buf_nopassphrase.txt"; |
|---|
| 65 |
static const char filename_rsa_clearsign_buf_passphrase[] |
|---|
| 66 |
="ops_rsa_signed_clearsign_buf_passphrase.txt"; |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
int init_suite_rsa_signature(void) |
|---|
| 74 |
{ |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
create_small_testfile(filename_rsa_noarmour_nopassphrase); |
|---|
| 78 |
create_small_testfile(filename_rsa_noarmour_passphrase); |
|---|
| 79 |
create_small_testfile(filename_rsa_armour_nopassphrase); |
|---|
| 80 |
create_small_testfile(filename_rsa_armour_passphrase); |
|---|
| 81 |
create_small_testfile(filename_rsa_clearsign_file_nopassphrase); |
|---|
| 82 |
create_small_testfile(filename_rsa_clearsign_file_passphrase); |
|---|
| 83 |
create_small_testfile(filename_rsa_clearsign_buf_nopassphrase); |
|---|
| 84 |
create_small_testfile(filename_rsa_clearsign_buf_passphrase); |
|---|
| 85 |
|
|---|
| 86 |
create_large_testfile(filename_rsa_large_noarmour_nopassphrase); |
|---|
| 87 |
create_large_testfile(filename_rsa_large_armour_nopassphrase); |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
return 0; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
int clean_suite_rsa_signature(void) |
|---|
| 94 |
{ |
|---|
| 95 |
ops_finish(); |
|---|
| 96 |
|
|---|
| 97 |
reset_vars(); |
|---|
| 98 |
|
|---|
| 99 |
return 0; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
static void test_rsa_signature_clearsign_file(const char *filename, |
|---|
| 103 |
const ops_secret_key_t *skey) |
|---|
| 104 |
{ |
|---|
| 105 |
char myfile[MAXBUF]; |
|---|
| 106 |
char signed_file[MAXBUF]; |
|---|
| 107 |
ops_boolean_t overwrite; |
|---|
| 108 |
|
|---|
| 109 |
set_up_file_names(myfile, signed_file, filename, "asc"); |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
overwrite=ops_true; |
|---|
| 113 |
ops_sign_file_as_cleartext(myfile, NULL, skey, overwrite); |
|---|
| 114 |
|
|---|
| 115 |
check_sig(signed_file); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
static void test_rsa_signature_clearsign_buf(const char *filename, |
|---|
| 119 |
const ops_secret_key_t *skey) |
|---|
| 120 |
{ |
|---|
| 121 |
char myfile[MAXBUF]; |
|---|
| 122 |
char signed_file[MAXBUF]; |
|---|
| 123 |
ops_memory_t *input=NULL; |
|---|
| 124 |
ops_memory_t *output=NULL; |
|---|
| 125 |
ops_boolean_t overwrite; |
|---|
| 126 |
int errnum=0; |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
set_up_file_names(myfile, signed_file, filename, "asc"); |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
input=ops_write_mem_from_file(myfile, &errnum); |
|---|
| 134 |
CU_ASSERT(errnum==0); |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
ops_sign_buf_as_cleartext(ops_memory_get_data(input), |
|---|
| 138 |
ops_memory_get_length(input), &output,skey); |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
overwrite=ops_true; |
|---|
| 142 |
ops_write_file_from_buf(signed_file, ops_memory_get_data(output), |
|---|
| 143 |
ops_memory_get_length(output), overwrite); |
|---|
| 144 |
|
|---|
| 145 |
check_sig(signed_file); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
static void test_rsa_signature_sign(const int use_armour, const char *filename, |
|---|
| 149 |
const ops_secret_key_t *skey) |
|---|
| 150 |
{ |
|---|
| 151 |
char myfile[MAXBUF]; |
|---|
| 152 |
char signed_file[MAXBUF]; |
|---|
| 153 |
char *suffix= use_armour ? "asc" : "gpg"; |
|---|
| 154 |
ops_boolean_t overwrite=ops_true; |
|---|
| 155 |
|
|---|
| 156 |
set_up_file_names(myfile, signed_file, filename, suffix); |
|---|
| 157 |
|
|---|
| 158 |
ops_sign_file(myfile, signed_file, skey, use_armour, overwrite); |
|---|
| 159 |
|
|---|
| 160 |
check_sig(signed_file); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
static void test_rsa_signature_sign_memory(const int use_armour, |
|---|
| 164 |
const void* input, |
|---|
| 165 |
const int input_len, |
|---|
| 166 |
const ops_secret_key_t *skey) |
|---|
| 167 |
{ |
|---|
| 168 |
ops_memory_t* mem=NULL; |
|---|
| 169 |
ops_parse_info_t *pinfo=NULL; |
|---|
| 170 |
validate_data_cb_arg_t validate_arg; |
|---|
| 171 |
|
|---|
| 172 |
mem=ops_sign_buf(input, input_len, OPS_SIG_TEXT, skey, use_armour); |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
if (debug) |
|---|
| 179 |
{ |
|---|
| 180 |
fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
ops_write_file_from_buf("/tmp/memory.asc", ops_memory_get_data(mem), |
|---|
| 184 |
ops_memory_get_length(mem), ops_true); |
|---|
| 185 |
|
|---|
| 186 |
ops_setup_memory_read(&pinfo, mem, &validate_arg, callback_verify, |
|---|
| 187 |
ops_true); |
|---|
| 188 |
|
|---|
| 189 |
check_sig_with_ops_core(pinfo, use_armour, &validate_arg); |
|---|
| 190 |
|
|---|
| 191 |
ops_memory_free(mem); |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
static void test_rsa_signature_large_noarmour_nopassphrase(void) |
|---|
| 195 |
{ |
|---|
| 196 |
assert(pub_keyring.nkeys); |
|---|
| 197 |
test_rsa_signature_sign(OPS_UNARMOURED, |
|---|
| 198 |
filename_rsa_large_noarmour_nopassphrase, |
|---|
| 199 |
alpha_skey); |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
static void test_rsa_signature_large_armour_nopassphrase(void) |
|---|
| 203 |
{ |
|---|
| 204 |
assert(pub_keyring.nkeys); |
|---|
| 205 |
test_rsa_signature_sign(OPS_ARMOURED, |
|---|
| 206 |
filename_rsa_large_armour_nopassphrase, alpha_skey); |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
static void test_rsa_signature_noarmour_nopassphrase(void) |
|---|
| 210 |
{ |
|---|
| 211 |
unsigned char testdata[MAXBUF]; |
|---|
| 212 |
assert(pub_keyring.nkeys); |
|---|
| 213 |
test_rsa_signature_sign(OPS_UNARMOURED, filename_rsa_noarmour_nopassphrase, |
|---|
| 214 |
alpha_skey); |
|---|
| 215 |
create_testdata("test_rsa_signature_noarmour_nopassphrase", testdata, |
|---|
| 216 |
MAXBUF); |
|---|
| 217 |
test_rsa_signature_sign_memory(OPS_UNARMOURED, testdata, MAXBUF, |
|---|
| 218 |
alpha_skey); |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
static void test_rsa_signature_noarmour_passphrase(void) |
|---|
| 222 |
{ |
|---|
| 223 |
unsigned char testdata[MAXBUF]; |
|---|
| 224 |
assert(pub_keyring.nkeys); |
|---|
| 225 |
test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_noarmour_passphrase, |
|---|
| 226 |
bravo_skey); |
|---|
| 227 |
|
|---|
| 228 |
create_testdata("test_rsa_signature_noarmour_passphrase", testdata, MAXBUF); |
|---|
| 229 |
test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, bravo_skey); |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
static void test_rsa_signature_armour_nopassphrase(void) |
|---|
| 233 |
{ |
|---|
| 234 |
unsigned char testdata[MAXBUF]; |
|---|
| 235 |
assert(pub_keyring.nkeys); |
|---|
| 236 |
test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_armour_nopassphrase, |
|---|
| 237 |
alpha_skey); |
|---|
| 238 |
|
|---|
| 239 |
create_testdata("test_rsa_signature_armour_nopassphrase", testdata, MAXBUF); |
|---|
| 240 |
test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, alpha_skey); |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
static void test_rsa_signature_armour_passphrase(void) |
|---|
| 244 |
{ |
|---|
| 245 |
unsigned char testdata[MAXBUF]; |
|---|
| 246 |
|
|---|
| 247 |
assert(pub_keyring.nkeys); |
|---|
| 248 |
test_rsa_signature_sign(OPS_ARMOURED, filename_rsa_armour_passphrase, |
|---|
| 249 |
bravo_skey); |
|---|
| 250 |
|
|---|
| 251 |
create_testdata("test_rsa_signature_armour_passphrase", testdata, MAXBUF); |
|---|
| 252 |
test_rsa_signature_sign_memory(OPS_ARMOURED, testdata, MAXBUF, bravo_skey); |
|---|
| 253 |
} |
|---|
| 254 |
|
|---|
| 255 |
static void test_rsa_signature_clearsign_file_nopassphrase(void) |
|---|
| 256 |
{ |
|---|
| 257 |
assert(pub_keyring.nkeys); |
|---|
| 258 |
test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_nopassphrase, |
|---|
| 259 |
alpha_skey); |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
static void test_rsa_signature_clearsign_file_passphrase(void) |
|---|
| 263 |
{ |
|---|
| 264 |
assert(pub_keyring.nkeys); |
|---|
| 265 |
test_rsa_signature_clearsign_file(filename_rsa_clearsign_file_passphrase, |
|---|
| 266 |
bravo_skey); |
|---|
| 267 |
} |
|---|
| 268 |
|
|---|
| 269 |
static void test_rsa_signature_clearsign_buf_nopassphrase(void) |
|---|
| 270 |
{ |
|---|
| 271 |
assert(pub_keyring.nkeys); |
|---|
| 272 |
test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_nopassphrase, |
|---|
| 273 |
alpha_skey); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
static void test_rsa_signature_clearsign_buf_passphrase(void) |
|---|
| 277 |
{ |
|---|
| 278 |
assert(pub_keyring.nkeys); |
|---|
| 279 |
test_rsa_signature_clearsign_buf(filename_rsa_clearsign_buf_passphrase, |
|---|
| 280 |
bravo_skey); |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
static int add_tests(CU_pSuite suite) |
|---|
| 295 |
{ |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
if (NULL == CU_add_test(suite, "Unarmoured, no passphrase", |
|---|
| 299 |
test_rsa_signature_noarmour_nopassphrase)) |
|---|
| 300 |
return 0; |
|---|
| 301 |
|
|---|
| 302 |
if (NULL == CU_add_test(suite, "Unarmoured, passphrase", |
|---|
| 303 |
test_rsa_signature_noarmour_passphrase)) |
|---|
| 304 |
return 0; |
|---|
| 305 |
|
|---|
| 306 |
if (NULL == CU_add_test(suite, "Clearsigned file, no passphrase", |
|---|
| 307 |
test_rsa_signature_clearsign_file_nopassphrase)) |
|---|
| 308 |
return 0; |
|---|
| 309 |
|
|---|
| 310 |
if (NULL == CU_add_test(suite, "Clearsigned file, passphrase", |
|---|
| 311 |
test_rsa_signature_clearsign_file_passphrase)) |
|---|
| 312 |
return 0; |
|---|
| 313 |
|
|---|
| 314 |
if (NULL == CU_add_test(suite, "Clearsigned buf, no passphrase", |
|---|
| 315 |
test_rsa_signature_clearsign_buf_nopassphrase)) |
|---|
| 316 |
return 0; |
|---|
| 317 |
|
|---|
| 318 |
if (NULL == CU_add_test(suite, "Clearsigned buf, passphrase", |
|---|
| 319 |
test_rsa_signature_clearsign_buf_passphrase)) |
|---|
| 320 |
return 0; |
|---|
| 321 |
|
|---|
| 322 |
if (NULL == CU_add_test(suite, "Armoured, no passphrase", |
|---|
| 323 |
test_rsa_signature_armour_nopassphrase)) |
|---|
| 324 |
return 0; |
|---|
| 325 |
|
|---|
| 326 |
if (NULL == CU_add_test(suite, "Armoured, passphrase", |
|---|
| 327 |
test_rsa_signature_armour_passphrase)) |
|---|
| 328 |
return 0; |
|---|
| 329 |
|
|---|
| 330 |
if (NULL == CU_add_test(suite, "Large, no armour, no passphrase", |
|---|
| 331 |
test_rsa_signature_large_noarmour_nopassphrase)) |
|---|
| 332 |
return 0; |
|---|
| 333 |
|
|---|
| 334 |
if (NULL == CU_add_test(suite, "Large, armour, no passphrase", |
|---|
| 335 |
test_rsa_signature_large_armour_nopassphrase)) |
|---|
| 336 |
return 0; |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
return 1; |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
CU_pSuite suite_rsa_signature() |
|---|
| 346 |
{ |
|---|
| 347 |
CU_pSuite suite = NULL; |
|---|
| 348 |
|
|---|
| 349 |
suite = CU_add_suite("RSA Signature Suite", init_suite_rsa_signature, |
|---|
| 350 |
clean_suite_rsa_signature); |
|---|
| 351 |
if (!suite) |
|---|
| 352 |
return NULL; |
|---|
| 353 |
|
|---|
| 354 |
if (!add_tests(suite)) |
|---|
| 355 |
return NULL; |
|---|
| 356 |
|
|---|
| 357 |
return suite; |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|