| 1 |
|
| 2 |
#include "config.h" |
| 3 |
|
| 4 |
#define LIBSSH_STATIC |
| 5 |
|
| 6 |
#include <sys/stat.h> |
| 7 |
#include <fcntl.h> |
| 8 |
|
| 9 |
#include "pki.c" |
| 10 |
#include "torture.h" |
| 11 |
#include "torture_pki.h" |
| 12 |
#include "torture_key.h" |
| 13 |
|
| 14 |
#define LIBSSH_ECDSA_TESTKEY "libssh_testkey.id_" |
| 15 |
#define LIBSSH_ECDSA_TESTKEY_PEM "libssh_testkey_pem.id_" |
| 16 |
#define LABEL_256 "ecdsa256" |
| 17 |
#define LABEL_384 "ecdsa384" |
| 18 |
#define LABEL_521 "ecdsa521" |
| 19 |
#define PUB_URI_FMT "pkcs11:token=%s;object=%s;type=public" |
| 20 |
#define PRIV_URI_FMT "pkcs11:token=%s;object=%s;type=private?pin-value=1234" |
| 21 |
#define PRIV_URI_NO_PUB_FMT "pkcs11:token=%s_no_pub_uri;object=%s_no_pub_uri;type=private?pin-value=1234" |
| 22 |
|
| 23 |
/** PKCS#11 URIs with invalid fields**/ |
| 24 |
|
| 25 |
#define PRIV_URI_FMT_384_INVALID_TOKEN "pkcs11:token=ecdsa521;object=ecdsa384;type=private?pin-value=1234" |
| 26 |
#define PRIV_URI_FMT_521_INVALID_OBJECT "pkcs11:token=ecdsa521;object=ecdsa384;type=private?pin-value=1234" |
| 27 |
#define PUB_URI_FMT_384_INVALID_TOKEN "pkcs11:token=ecdsa521;object=ecdsa384;type=public" |
| 28 |
#define PUB_URI_FMT_521_INVALID_OBJECT "pkcs11:token=ecdsa521;object=ecdsa384;type=public" |
| 29 |
|
| 30 |
const char template[] = "/tmp/temp_dir_XXXXXX"; |
| 31 |
const unsigned char INPUT[] = "1234567890123456789012345678901234567890" |
| 32 |
"123456789012345678901234"; |
| 33 |
struct pki_st { |
| 34 |
char *orig_dir; |
| 35 |
char *temp_dir; |
| 36 |
enum ssh_keytypes_e type; |
| 37 |
}; |
| 38 |
|
| 39 |
static int setup_tokens_ecdsa(void **state, int ecdsa_bits, const char *obj_tempname, const char *load_public) |
| 40 |
{ |
| 41 |
|
| 42 |
struct pki_st *test_state = *state; |
| 43 |
char priv_filename[1024]; |
| 44 |
char pub_filename[1024]; |
| 45 |
char *cwd = NULL; |
| 46 |
|
| 47 |
cwd = test_state->temp_dir; |
| 48 |
assert_non_null(cwd); |
| 49 |
|
| 50 |
snprintf(priv_filename, sizeof(priv_filename), "%s%s%s%s", cwd, "/", LIBSSH_ECDSA_TESTKEY, obj_tempname); |
| 51 |
snprintf(pub_filename, sizeof(pub_filename), "%s%s%s%s%s", cwd, "/", LIBSSH_ECDSA_TESTKEY, obj_tempname, ".pub"); |
| 52 |
|
| 53 |
switch (ecdsa_bits) { |
| 54 |
case 521: |
| 55 |
test_state->type = SSH_KEYTYPE_ECDSA_P521; |
| 56 |
break; |
| 57 |
case 384: |
| 58 |
test_state->type = SSH_KEYTYPE_ECDSA_P384; |
| 59 |
break; |
| 60 |
default: |
| 61 |
test_state->type = SSH_KEYTYPE_ECDSA_P256; |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
torture_write_file(priv_filename, |
| 66 |
torture_get_testkey(test_state->type, 0)); |
| 67 |
torture_write_file(pub_filename, |
| 68 |
torture_get_testkey_pub_pem(test_state->type)); |
| 69 |
torture_setup_tokens(cwd, priv_filename, obj_tempname, load_public); |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static int setup_directory_structure(void **state) |
| 75 |
{ |
| 76 |
struct pki_st *test_state = NULL; |
| 77 |
char *temp_dir; |
| 78 |
int rc; |
| 79 |
|
| 80 |
test_state = (struct pki_st *)malloc(sizeof(struct pki_st)); |
| 81 |
assert_non_null(test_state); |
| 82 |
|
| 83 |
test_state->orig_dir = torture_get_current_working_dir(); |
| 84 |
assert_non_null(test_state->orig_dir); |
| 85 |
|
| 86 |
temp_dir = torture_make_temp_dir(template); |
| 87 |
assert_non_null(temp_dir); |
| 88 |
|
| 89 |
rc = torture_change_dir(temp_dir); |
| 90 |
assert_int_equal(rc, 0); |
| 91 |
SAFE_FREE(temp_dir); |
| 92 |
|
| 93 |
test_state->temp_dir = torture_get_current_working_dir(); |
| 94 |
assert_non_null(test_state->temp_dir); |
| 95 |
|
| 96 |
*state = test_state; |
| 97 |
|
| 98 |
setup_tokens_ecdsa(state, 256, "ecdsa256", "1"); |
| 99 |
setup_tokens_ecdsa(state, 384, "ecdsa384", "1"); |
| 100 |
setup_tokens_ecdsa(state, 521, "ecdsa521", "1"); |
| 101 |
setup_tokens_ecdsa(state, 256, "ecdsa256_no_pub_uri", "0"); |
| 102 |
setup_tokens_ecdsa(state, 384, "ecdsa384_no_pub_uri", "0"); |
| 103 |
setup_tokens_ecdsa(state, 521, "ecdsa521_no_pub_uri", "0"); |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static int teardown_directory_structure(void **state) |
| 109 |
{ |
| 110 |
struct pki_st *test_state = *state; |
| 111 |
int rc; |
| 112 |
|
| 113 |
torture_cleanup_tokens(test_state->temp_dir); |
| 114 |
|
| 115 |
rc = torture_change_dir(test_state->orig_dir); |
| 116 |
assert_int_equal(rc, 0); |
| 117 |
|
| 118 |
rc = torture_rmdirs(test_state->temp_dir); |
| 119 |
assert_int_equal(rc, 0); |
| 120 |
|
| 121 |
SAFE_FREE(test_state->temp_dir); |
| 122 |
SAFE_FREE(test_state->orig_dir); |
| 123 |
SAFE_FREE(test_state); |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static void torture_pki_ecdsa_import_pubkey_uri(void **state, const char *label) |
| 129 |
{ |
| 130 |
char uri[128] = {0}; |
| 131 |
ssh_key pubkey = NULL; |
| 132 |
int rc; |
| 133 |
|
| 134 |
rc = snprintf(uri, sizeof(uri), PUB_URI_FMT, label, label); |
| 135 |
assert_in_range(rc, 0, sizeof(uri) - 1); |
| 136 |
|
| 137 |
rc = ssh_pki_import_pubkey_file(uri, &pubkey); |
| 138 |
assert_return_code(rc, errno); |
| 139 |
assert_non_null(pubkey); |
| 140 |
|
| 141 |
rc = ssh_key_is_public(pubkey); |
| 142 |
assert_int_equal(rc, 1); |
| 143 |
|
| 144 |
SSH_KEY_FREE(pubkey); |
| 145 |
} |
| 146 |
|
| 147 |
static void torture_pki_ecdsa_import_pubkey_uri_256(void **state) |
| 148 |
{ |
| 149 |
torture_pki_ecdsa_import_pubkey_uri(state, LABEL_256); |
| 150 |
} |
| 151 |
|
| 152 |
static void torture_pki_ecdsa_import_pubkey_uri_384(void **state) |
| 153 |
{ |
| 154 |
torture_pki_ecdsa_import_pubkey_uri(state, LABEL_384); |
| 155 |
} |
| 156 |
|
| 157 |
static void torture_pki_ecdsa_import_pubkey_uri_521(void **state) |
| 158 |
{ |
| 159 |
torture_pki_ecdsa_import_pubkey_uri(state, LABEL_521); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
torture_pki_ecdsa_publickey_from_privatekey_uri(void **state, |
| 164 |
const char *label, |
| 165 |
const char *type) |
| 166 |
{ |
| 167 |
int rc; |
| 168 |
char uri[128] = {0}; |
| 169 |
ssh_key privkey = NULL; |
| 170 |
ssh_key pubkey = NULL; |
| 171 |
ssh_string pblob = NULL; |
| 172 |
char pubkey_original[4096] = {0}; |
| 173 |
char pubkey_generated[4096] = {0}; |
| 174 |
char convert_key_to_pem[4096]; |
| 175 |
char pub_filename[1024]; |
| 176 |
char pub_filename_generated[1024]; |
| 177 |
char pub_filename_pem[1024]; |
| 178 |
|
| 179 |
rc = snprintf(uri, sizeof(uri), PRIV_URI_FMT, label, label); |
| 180 |
assert_in_range(rc, 0, sizeof(uri) - 1); |
| 181 |
|
| 182 |
rc = ssh_pki_import_privkey_file(uri, |
| 183 |
NULL, |
| 184 |
NULL, |
| 185 |
NULL, |
| 186 |
&privkey); |
| 187 |
assert_return_code(rc, errno); |
| 188 |
assert_non_null(privkey); |
| 189 |
|
| 190 |
rc = ssh_pki_export_pubkey_blob(privkey, &pblob); |
| 191 |
assert_return_code(rc, errno); |
| 192 |
assert_non_null(pblob); |
| 193 |
ssh_string_free(pblob); |
| 194 |
|
| 195 |
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey); |
| 196 |
assert_return_code(rc, errno); |
| 197 |
assert_non_null(pubkey); |
| 198 |
|
| 199 |
snprintf(pub_filename, sizeof(pub_filename), "%s%s%s", LIBSSH_ECDSA_TESTKEY, type, ".pub"); |
| 200 |
snprintf(pub_filename_generated, sizeof(pub_filename_generated), "%s%s%s", |
| 201 |
LIBSSH_ECDSA_TESTKEY_PEM, type, "generated.pub"); |
| 202 |
snprintf(pub_filename_pem, sizeof(pub_filename_pem), "%s%s%s", LIBSSH_ECDSA_TESTKEY_PEM, type, ".pub"); |
| 203 |
|
| 204 |
rc = torture_read_one_line(pub_filename, |
| 205 |
pubkey_original, |
| 206 |
sizeof(pubkey_original)); |
| 207 |
assert_return_code(rc, errno); |
| 208 |
|
| 209 |
rc = ssh_pki_export_pubkey_file(pubkey, pub_filename_generated); |
| 210 |
assert_return_code(rc, errno); |
| 211 |
|
| 212 |
/* remove the public key, generate it from the private key and write it. */ |
| 213 |
unlink(pub_filename); |
| 214 |
|
| 215 |
snprintf(convert_key_to_pem, sizeof(convert_key_to_pem), "ssh-keygen -e -f %s -m PKCS8 > %s ", |
| 216 |
pub_filename_generated, pub_filename_pem); |
| 217 |
|
| 218 |
system(convert_key_to_pem); |
| 219 |
|
| 220 |
rc = torture_read_one_line(pub_filename_pem, |
| 221 |
pubkey_generated, |
| 222 |
sizeof(pubkey_generated)); |
| 223 |
assert_return_code(rc, errno); |
| 224 |
|
| 225 |
assert_memory_equal(pubkey_original, pubkey_generated, strlen(pubkey_original)); |
| 226 |
|
| 227 |
SSH_KEY_FREE(privkey); |
| 228 |
SSH_KEY_FREE(pubkey); |
| 229 |
} |
| 230 |
|
| 231 |
static void torture_pki_ecdsa_publickey_from_privatekey_uri_256(void **state) |
| 232 |
{ |
| 233 |
torture_pki_ecdsa_publickey_from_privatekey_uri(state, LABEL_256, "ecdsa256"); |
| 234 |
} |
| 235 |
|
| 236 |
static void torture_pki_ecdsa_publickey_from_privatekey_uri_384(void **state) |
| 237 |
{ |
| 238 |
torture_pki_ecdsa_publickey_from_privatekey_uri(state, LABEL_384, "ecdsa384"); |
| 239 |
} |
| 240 |
|
| 241 |
static void torture_pki_ecdsa_publickey_from_privatekey_uri_521(void **state) |
| 242 |
{ |
| 243 |
torture_pki_ecdsa_publickey_from_privatekey_uri(state, LABEL_521, "ecdsa521"); |
| 244 |
} |
| 245 |
|
| 246 |
static void |
| 247 |
import_pubkey_without_loading_public_uri(void **state, const char *label) |
| 248 |
{ |
| 249 |
int rc; |
| 250 |
char uri[128] = {0}; |
| 251 |
ssh_key privkey = NULL; |
| 252 |
ssh_key pubkey = NULL; |
| 253 |
ssh_string pblob = NULL; |
| 254 |
|
| 255 |
rc = snprintf(uri, sizeof(uri), PRIV_URI_NO_PUB_FMT, label, label); |
| 256 |
assert_in_range(rc, 0, sizeof(uri) - 1); |
| 257 |
|
| 258 |
rc = ssh_pki_import_privkey_file(uri, |
| 259 |
NULL, |
| 260 |
NULL, |
| 261 |
NULL, |
| 262 |
&privkey); |
| 263 |
assert_return_code(rc, errno); |
| 264 |
assert_non_null(privkey); |
| 265 |
|
| 266 |
rc = ssh_pki_export_pubkey_blob(privkey, &pblob); |
| 267 |
assert_int_not_equal(rc, 0); |
| 268 |
assert_null(pblob); |
| 269 |
|
| 270 |
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey); |
| 271 |
assert_int_not_equal(rc, 0); |
| 272 |
assert_null(pubkey); |
| 273 |
|
| 274 |
SSH_KEY_FREE(privkey); |
| 275 |
} |
| 276 |
|
| 277 |
static void torture_pki_ecdsa_import_pubkey_without_loading_public_uri_256(void **state) |
| 278 |
{ |
| 279 |
import_pubkey_without_loading_public_uri(state, LABEL_256); |
| 280 |
} |
| 281 |
|
| 282 |
static void torture_pki_ecdsa_import_pubkey_without_loading_public_uri_384(void **state) |
| 283 |
{ |
| 284 |
import_pubkey_without_loading_public_uri(state, LABEL_384); |
| 285 |
} |
| 286 |
|
| 287 |
static void torture_pki_ecdsa_import_pubkey_without_loading_public_uri_521(void **state) |
| 288 |
{ |
| 289 |
import_pubkey_without_loading_public_uri(state, LABEL_521); |
| 290 |
} |
| 291 |
|
| 292 |
static void |
| 293 |
torture_ecdsa_sign_verify_uri(void **state, |
| 294 |
const char *label, |
| 295 |
enum ssh_digest_e dig_type) |
| 296 |
{ |
| 297 |
int rc; |
| 298 |
char uri[128] = {0}; |
| 299 |
ssh_key privkey = NULL, pubkey = NULL; |
| 300 |
ssh_signature sign = NULL; |
| 301 |
enum ssh_keytypes_e type = SSH_KEYTYPE_UNKNOWN; |
| 302 |
const char *type_char = NULL; |
| 303 |
const char *etype_char = NULL; |
| 304 |
ssh_session session = ssh_new(); |
| 305 |
|
| 306 |
assert_non_null(session); |
| 307 |
|
| 308 |
rc = snprintf(uri, sizeof(uri), PRIV_URI_FMT, label, label); |
| 309 |
assert_in_range(rc, 0, sizeof(uri) - 1); |
| 310 |
|
| 311 |
rc = ssh_pki_import_privkey_file(uri, |
| 312 |
NULL, |
| 313 |
NULL, |
| 314 |
NULL, |
| 315 |
&privkey); |
| 316 |
assert_return_code(rc, errno); |
| 317 |
assert_non_null(privkey); |
| 318 |
|
| 319 |
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey); |
| 320 |
assert_return_code(rc, errno); |
| 321 |
assert_non_null(pubkey); |
| 322 |
|
| 323 |
sign = pki_do_sign(privkey, INPUT, sizeof(INPUT), dig_type); |
| 324 |
assert_non_null(sign); |
| 325 |
|
| 326 |
rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT)); |
| 327 |
assert_return_code(rc, errno); |
| 328 |
|
| 329 |
type = ssh_key_type(privkey); |
| 330 |
type_char = ssh_key_type_to_char(type); |
| 331 |
etype_char = ssh_pki_key_ecdsa_name(privkey); |
| 332 |
|
| 333 |
switch (dig_type) { |
| 334 |
case SSH_DIGEST_SHA256: |
| 335 |
assert_true(type == SSH_KEYTYPE_ECDSA_P256); |
| 336 |
assert_string_equal(type_char, "ecdsa-sha2-nistp256"); |
| 337 |
assert_string_equal(etype_char, "ecdsa-sha2-nistp256"); |
| 338 |
break; |
| 339 |
case SSH_DIGEST_SHA384: |
| 340 |
assert_true(type == SSH_KEYTYPE_ECDSA_P384); |
| 341 |
assert_string_equal(type_char, "ecdsa-sha2-nistp384"); |
| 342 |
assert_string_equal(etype_char, "ecdsa-sha2-nistp384"); |
| 343 |
break; |
| 344 |
case SSH_DIGEST_SHA512: |
| 345 |
assert_true(type == SSH_KEYTYPE_ECDSA_P521); |
| 346 |
assert_string_equal(type_char, "ecdsa-sha2-nistp521"); |
| 347 |
assert_string_equal(etype_char, "ecdsa-sha2-nistp521"); |
| 348 |
break; |
| 349 |
default: |
| 350 |
printf("Invalid hash type: %d\n", dig_type); |
| 351 |
} |
| 352 |
|
| 353 |
ssh_free(session); |
| 354 |
ssh_signature_free(sign); |
| 355 |
SSH_KEY_FREE(privkey); |
| 356 |
SSH_KEY_FREE(pubkey); |
| 357 |
} |
| 358 |
|
| 359 |
static void torture_ecdsa_sign_verify_uri_256(void **state) |
| 360 |
{ |
| 361 |
torture_ecdsa_sign_verify_uri(state, LABEL_256, SSH_DIGEST_SHA256); |
| 362 |
} |
| 363 |
|
| 364 |
static void torture_ecdsa_sign_verify_uri_384(void **state) |
| 365 |
{ |
| 366 |
torture_ecdsa_sign_verify_uri(state, LABEL_384, SSH_DIGEST_SHA384); |
| 367 |
} |
| 368 |
|
| 369 |
static void torture_ecdsa_sign_verify_uri_521(void **state) |
| 370 |
{ |
| 371 |
torture_ecdsa_sign_verify_uri(state, LABEL_521, SSH_DIGEST_SHA512); |
| 372 |
} |
| 373 |
|
| 374 |
static void torture_pki_ecdsa_duplicate_key_uri(void **state, const char *label) |
| 375 |
{ |
| 376 |
int rc; |
| 377 |
char pub_uri[128] = {0}; |
| 378 |
char priv_uri[128] = {0}; |
| 379 |
char *b64_key = NULL; |
| 380 |
char *b64_key_gen = NULL; |
| 381 |
ssh_key pubkey = NULL; |
| 382 |
ssh_key pubkey_dup = NULL; |
| 383 |
ssh_key privkey = NULL; |
| 384 |
ssh_key privkey_dup = NULL; |
| 385 |
|
| 386 |
(void) state; |
| 387 |
|
| 388 |
rc = snprintf(pub_uri, sizeof(pub_uri), PUB_URI_FMT, label, label); |
| 389 |
assert_in_range(rc, 0, sizeof(pub_uri) - 1); |
| 390 |
rc = snprintf(priv_uri, sizeof(priv_uri), PRIV_URI_FMT, label, label); |
| 391 |
assert_in_range(rc, 0, sizeof(priv_uri) - 1); |
| 392 |
|
| 393 |
rc = ssh_pki_import_pubkey_file(pub_uri, &pubkey); |
| 394 |
assert_return_code(rc, errno); |
| 395 |
assert_non_null(pubkey); |
| 396 |
|
| 397 |
rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key); |
| 398 |
assert_return_code(rc, errno); |
| 399 |
assert_non_null(b64_key); |
| 400 |
|
| 401 |
rc = ssh_pki_import_privkey_file(priv_uri, |
| 402 |
NULL, |
| 403 |
NULL, |
| 404 |
NULL, |
| 405 |
&privkey); |
| 406 |
assert_return_code(rc, errno); |
| 407 |
assert_non_null(privkey); |
| 408 |
|
| 409 |
privkey_dup = ssh_key_dup(privkey); |
| 410 |
assert_non_null(privkey_dup); |
| 411 |
|
| 412 |
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey_dup); |
| 413 |
assert_return_code(rc, errno); |
| 414 |
assert_non_null(pubkey_dup); |
| 415 |
|
| 416 |
rc = ssh_pki_export_pubkey_base64(pubkey_dup, &b64_key_gen); |
| 417 |
assert_return_code(rc, errno); |
| 418 |
assert_non_null(b64_key_gen); |
| 419 |
|
| 420 |
assert_string_equal(b64_key, b64_key_gen); |
| 421 |
|
| 422 |
rc = ssh_key_cmp(privkey, privkey_dup, SSH_KEY_CMP_PRIVATE); |
| 423 |
assert_return_code(rc, errno); |
| 424 |
|
| 425 |
rc = ssh_key_cmp(pubkey, pubkey_dup, SSH_KEY_CMP_PUBLIC); |
| 426 |
assert_return_code(rc, errno); |
| 427 |
|
| 428 |
SSH_KEY_FREE(pubkey); |
| 429 |
SSH_KEY_FREE(pubkey_dup); |
| 430 |
SSH_KEY_FREE(privkey); |
| 431 |
SSH_KEY_FREE(privkey_dup); |
| 432 |
SSH_STRING_FREE_CHAR(b64_key); |
| 433 |
SSH_STRING_FREE_CHAR(b64_key_gen); |
| 434 |
} |
| 435 |
|
| 436 |
static void torture_pki_ecdsa_duplicate_key_uri_256(void **state) |
| 437 |
{ |
| 438 |
torture_pki_ecdsa_duplicate_key_uri(state, LABEL_256); |
| 439 |
} |
| 440 |
|
| 441 |
static void torture_pki_ecdsa_duplicate_key_uri_384(void **state) |
| 442 |
{ |
| 443 |
torture_pki_ecdsa_duplicate_key_uri(state, LABEL_384); |
| 444 |
} |
| 445 |
|
| 446 |
static void torture_pki_ecdsa_duplicate_key_uri_521(void **state) |
| 447 |
{ |
| 448 |
torture_pki_ecdsa_duplicate_key_uri(state, LABEL_521); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
torture_pki_ecdsa_duplicate_then_demote_uri(void **state, const char *label) |
| 453 |
{ |
| 454 |
char priv_uri[128] = {0}; |
| 455 |
ssh_key pubkey = NULL; |
| 456 |
ssh_key privkey = NULL; |
| 457 |
ssh_key privkey_dup = NULL; |
| 458 |
int rc; |
| 459 |
|
| 460 |
(void) state; |
| 461 |
|
| 462 |
rc = snprintf(priv_uri, sizeof(priv_uri), PRIV_URI_FMT, label, label); |
| 463 |
assert_in_range(rc, 0, sizeof(priv_uri) - 1); |
| 464 |
|
| 465 |
rc = ssh_pki_import_privkey_file(priv_uri, |
| 466 |
NULL, |
| 467 |
NULL, |
| 468 |
NULL, |
| 469 |
&privkey); |
| 470 |
assert_return_code(rc, errno); |
| 471 |
assert_non_null(privkey); |
| 472 |
|
| 473 |
privkey_dup = ssh_key_dup(privkey); |
| 474 |
assert_non_null(privkey_dup); |
| 475 |
assert_int_equal(privkey->ecdsa_nid, privkey_dup->ecdsa_nid); |
| 476 |
|
| 477 |
rc = ssh_pki_export_privkey_to_pubkey(privkey_dup, &pubkey); |
| 478 |
assert_return_code(rc, errno); |
| 479 |
assert_non_null(pubkey); |
| 480 |
assert_int_equal(pubkey->ecdsa_nid, privkey->ecdsa_nid); |
| 481 |
|
| 482 |
SSH_KEY_FREE(pubkey); |
| 483 |
SSH_KEY_FREE(privkey); |
| 484 |
SSH_KEY_FREE(privkey_dup); |
| 485 |
} |
| 486 |
|
| 487 |
static void torture_pki_ecdsa_duplicate_then_demote_uri_256(void **state) |
| 488 |
{ |
| 489 |
torture_pki_ecdsa_duplicate_then_demote_uri(state, LABEL_256); |
| 490 |
} |
| 491 |
|
| 492 |
static void torture_pki_ecdsa_duplicate_then_demote_uri_384(void **state) |
| 493 |
{ |
| 494 |
torture_pki_ecdsa_duplicate_then_demote_uri(state, LABEL_384); |
| 495 |
} |
| 496 |
|
| 497 |
static void torture_pki_ecdsa_duplicate_then_demote_uri_521(void **state) |
| 498 |
{ |
| 499 |
torture_pki_ecdsa_duplicate_then_demote_uri(state, LABEL_521); |
| 500 |
} |
| 501 |
|
| 502 |
static void torture_pki_ecdsa_import_pubkey_uri_invalid_configurations(void **state) |
| 503 |
{ |
| 504 |
ssh_key privkey = NULL; |
| 505 |
ssh_key pubkey = NULL; |
| 506 |
int rc; |
| 507 |
|
| 508 |
/** invalid token for already setup Private PKCS #11 URI */ |
| 509 |
rc = ssh_pki_import_privkey_file(PRIV_URI_FMT_384_INVALID_TOKEN, |
| 510 |
NULL, |
| 511 |
NULL, |
| 512 |
NULL, |
| 513 |
&privkey); |
| 514 |
assert_int_not_equal(rc, 0); |
| 515 |
assert_null(privkey); |
| 516 |
|
| 517 |
/** invalid object for already setup Private PKCS #11 URI */ |
| 518 |
rc = ssh_pki_import_privkey_file(PRIV_URI_FMT_521_INVALID_OBJECT, |
| 519 |
NULL, |
| 520 |
NULL, |
| 521 |
NULL, |
| 522 |
&privkey); |
| 523 |
assert_int_not_equal(rc, 0); |
| 524 |
assert_null(privkey); |
| 525 |
/** invalid token for already setup Public PKCS #11 URI */ |
| 526 |
rc = ssh_pki_import_pubkey_file(PUB_URI_FMT_384_INVALID_TOKEN, |
| 527 |
&pubkey); |
| 528 |
assert_int_not_equal(rc, 0); |
| 529 |
assert_null(pubkey); |
| 530 |
|
| 531 |
/** invalid object for already setup Public PKCS #11 URI */ |
| 532 |
rc = ssh_pki_import_pubkey_file(PUB_URI_FMT_521_INVALID_OBJECT, |
| 533 |
&pubkey); |
| 534 |
assert_int_not_equal(rc, 0); |
| 535 |
assert_null(pubkey); |
| 536 |
} |
| 537 |
|
| 538 |
int torture_run_tests(void) { |
| 539 |
int rc; |
| 540 |
struct CMUnitTest tests[] = { |
| 541 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_uri_256), |
| 542 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_uri_384), |
| 543 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_uri_521), |
| 544 |
cmocka_unit_test(torture_pki_ecdsa_publickey_from_privatekey_uri_256), |
| 545 |
cmocka_unit_test(torture_pki_ecdsa_publickey_from_privatekey_uri_384), |
| 546 |
cmocka_unit_test(torture_pki_ecdsa_publickey_from_privatekey_uri_521), |
| 547 |
cmocka_unit_test(torture_ecdsa_sign_verify_uri_256), |
| 548 |
cmocka_unit_test(torture_ecdsa_sign_verify_uri_384), |
| 549 |
cmocka_unit_test(torture_ecdsa_sign_verify_uri_521), |
| 550 |
cmocka_unit_test(torture_pki_ecdsa_duplicate_key_uri_256), |
| 551 |
cmocka_unit_test(torture_pki_ecdsa_duplicate_key_uri_384), |
| 552 |
cmocka_unit_test(torture_pki_ecdsa_duplicate_key_uri_521), |
| 553 |
cmocka_unit_test(torture_pki_ecdsa_duplicate_then_demote_uri_256), |
| 554 |
cmocka_unit_test(torture_pki_ecdsa_duplicate_then_demote_uri_384), |
| 555 |
cmocka_unit_test(torture_pki_ecdsa_duplicate_then_demote_uri_521), |
| 556 |
|
| 557 |
/** Expect fail on these negative test cases **/ |
| 558 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_uri_invalid_configurations), |
| 559 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_without_loading_public_uri_256), |
| 560 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_without_loading_public_uri_384), |
| 561 |
cmocka_unit_test(torture_pki_ecdsa_import_pubkey_without_loading_public_uri_521), |
| 562 |
}; |
| 563 |
ssh_session session = ssh_new(); |
| 564 |
int verbosity = torture_libssh_verbosity(); |
| 565 |
|
| 566 |
/* Do not use system openssl.cnf for the pkcs11 uri tests. |
| 567 |
* It can load a pkcs11 provider too early before we will set up environment |
| 568 |
* variables that are needed for the pkcs11 provider to access correct |
| 569 |
* tokens, causing unexpected failures. |
| 570 |
* Make sure this comes before ssh_init(), which initializes OpenSSL! |
| 571 |
*/ |
| 572 |
setenv("OPENSSL_CONF", SOURCEDIR "/tests/etc/openssl.cnf", 1); |
| 573 |
|
| 574 |
ssh_init(); |
| 575 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 576 |
|
| 577 |
torture_filter_tests(tests); |
| 578 |
rc = cmocka_run_group_tests(tests, setup_directory_structure, teardown_directory_structure); |
| 579 |
|
| 580 |
ssh_free(session); |
| 581 |
ssh_finalize(); |
| 582 |
|
| 583 |
return rc; |
| 584 |
} |