| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2018 by Anderson Toshiyuki Sasaki <[email protected]> |
| 5 |
* |
| 6 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 9 |
* option) any later version. |
| 10 |
* |
| 11 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 |
* License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 18 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 19 |
* MA 02111-1307, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#define LIBSSH_STATIC |
| 25 |
|
| 26 |
#include <sys/stat.h> |
| 27 |
#include <fcntl.h> |
| 28 |
|
| 29 |
#include "pki.c" |
| 30 |
#include "torture.h" |
| 31 |
#include "torture_pki.h" |
| 32 |
#include "torture_key.h" |
| 33 |
|
| 34 |
#include <pthread.h> |
| 35 |
|
| 36 |
#define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa" |
| 37 |
#define LIBSSH_RSA_TESTKEY_PASSPHRASE "libssh_testkey_passphrase.id_rsa" |
| 38 |
|
| 39 |
#define NUM_THREADS 10 |
| 40 |
|
| 41 |
const char template[] = "temp_dir_XXXXXX"; |
| 42 |
const unsigned char RSA_HASH[] = "12345678901234567890"; |
| 43 |
|
| 44 |
struct pki_st { |
| 45 |
char *cwd; |
| 46 |
char *temp_dir; |
| 47 |
}; |
| 48 |
|
| 49 |
static int run_on_threads(void *(*func)(void *)) |
| 50 |
{ |
| 51 |
pthread_t threads[NUM_THREADS]; |
| 52 |
int rc; |
| 53 |
int i; |
| 54 |
|
| 55 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 56 |
rc = pthread_create(&threads[i], NULL, func, NULL); |
| 57 |
assert_int_equal(rc, 0); |
| 58 |
} |
| 59 |
|
| 60 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 61 |
rc = pthread_join(threads[i], NULL); |
| 62 |
assert_int_equal(rc, 0); |
| 63 |
} |
| 64 |
|
| 65 |
return rc; |
| 66 |
} |
| 67 |
|
| 68 |
static int setup_rsa_key(void **state) |
| 69 |
{ |
| 70 |
struct pki_st *test_state = NULL; |
| 71 |
char *cwd = NULL; |
| 72 |
char *tmp_dir = NULL; |
| 73 |
int rc = 0; |
| 74 |
|
| 75 |
test_state = (struct pki_st *)malloc(sizeof(struct pki_st)); |
| 76 |
assert_non_null(test_state); |
| 77 |
|
| 78 |
cwd = torture_get_current_working_dir(); |
| 79 |
assert_non_null(cwd); |
| 80 |
|
| 81 |
tmp_dir = torture_make_temp_dir(template); |
| 82 |
assert_non_null(tmp_dir); |
| 83 |
|
| 84 |
test_state->cwd = cwd; |
| 85 |
test_state->temp_dir = tmp_dir; |
| 86 |
|
| 87 |
*state = test_state; |
| 88 |
|
| 89 |
rc = torture_change_dir(tmp_dir); |
| 90 |
assert_int_equal(rc, 0); |
| 91 |
|
| 92 |
printf("Changed directory to: %s\n", tmp_dir); |
| 93 |
|
| 94 |
torture_write_file(LIBSSH_RSA_TESTKEY, |
| 95 |
torture_get_testkey(SSH_KEYTYPE_RSA, 0)); |
| 96 |
torture_write_file(LIBSSH_RSA_TESTKEY_PASSPHRASE, |
| 97 |
torture_get_testkey(SSH_KEYTYPE_RSA, 1)); |
| 98 |
torture_write_file(LIBSSH_RSA_TESTKEY ".pub", |
| 99 |
torture_get_testkey_pub(SSH_KEYTYPE_RSA)); |
| 100 |
torture_write_file(LIBSSH_RSA_TESTKEY "-cert.pub", |
| 101 |
torture_get_testkey_pub(SSH_KEYTYPE_RSA_CERT01)); |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int teardown(void **state) { |
| 107 |
|
| 108 |
struct pki_st *test_state = NULL; |
| 109 |
int rc = 0; |
| 110 |
|
| 111 |
test_state = *((struct pki_st **)state); |
| 112 |
|
| 113 |
assert_non_null(test_state); |
| 114 |
assert_non_null(test_state->cwd); |
| 115 |
assert_non_null(test_state->temp_dir); |
| 116 |
|
| 117 |
rc = torture_change_dir(test_state->cwd); |
| 118 |
assert_int_equal(rc, 0); |
| 119 |
|
| 120 |
rc = torture_rmdirs(test_state->temp_dir); |
| 121 |
assert_int_equal(rc, 0); |
| 122 |
|
| 123 |
SAFE_FREE(test_state->temp_dir); |
| 124 |
SAFE_FREE(test_state->cwd); |
| 125 |
SAFE_FREE(test_state); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
disable_secmem(void) |
| 132 |
{ |
| 133 |
#if defined(HAVE_LIBGCRYPT) |
| 134 |
/* gcrypt currently is configured to use only 4kB of locked secmem |
| 135 |
* (see ssh_crypto_init() in src/libcrypt.c) |
| 136 |
* |
| 137 |
* This is insufficient to run the RSA key generation in many threads. |
| 138 |
* To avoid the expected warning, disable the secure memory. |
| 139 |
* */ |
| 140 |
|
| 141 |
gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN); |
| 142 |
gcry_control(GCRYCTL_DISABLE_SECMEM); |
| 143 |
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); |
| 144 |
#endif |
| 145 |
} |
| 146 |
|
| 147 |
static void *thread_pki_rsa_import_pubkey_file(void *threadid) |
| 148 |
{ |
| 149 |
ssh_key pubkey = NULL; |
| 150 |
int rc; |
| 151 |
|
| 152 |
(void) threadid; |
| 153 |
|
| 154 |
/* The key doesn't have the hostname as comment after the key */ |
| 155 |
rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey); |
| 156 |
assert_return_code(rc, errno); |
| 157 |
assert_non_null(pubkey); |
| 158 |
|
| 159 |
SSH_KEY_FREE(pubkey); |
| 160 |
|
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
static void torture_pki_rsa_import_pubkey_file(void **state) |
| 165 |
{ |
| 166 |
int rc; |
| 167 |
|
| 168 |
/* Unused */ |
| 169 |
(void) state; |
| 170 |
|
| 171 |
rc = run_on_threads(thread_pki_rsa_import_pubkey_file); |
| 172 |
assert_int_equal(rc, 0); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
static void *thread_pki_rsa_import_privkey_base64_NULL_key(void *threadid) |
| 177 |
{ |
| 178 |
int rc; |
| 179 |
const char *passphrase = torture_get_testkey_passphrase(); |
| 180 |
const char *testkey; |
| 181 |
|
| 182 |
(void) threadid; /* unused */ |
| 183 |
|
| 184 |
testkey = torture_get_testkey(SSH_KEYTYPE_RSA, 0); |
| 185 |
assert_non_null(testkey); |
| 186 |
|
| 187 |
/* test if it returns -1 if key is NULL */ |
| 188 |
rc = ssh_pki_import_privkey_base64(testkey, |
| 189 |
passphrase, |
| 190 |
NULL, |
| 191 |
NULL, |
| 192 |
NULL); |
| 193 |
assert_true(rc == -1); |
| 194 |
return NULL; |
| 195 |
} |
| 196 |
|
| 197 |
static void torture_pki_rsa_import_privkey_base64_NULL_key(void **state){ |
| 198 |
int rc; |
| 199 |
|
| 200 |
/* Unused */ |
| 201 |
(void) state; |
| 202 |
|
| 203 |
rc = run_on_threads(thread_pki_rsa_import_privkey_base64_NULL_key); |
| 204 |
assert_int_equal(rc, 0); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
static void *thread_pki_rsa_import_privkey_base64_NULL_str(void *threadid) |
| 209 |
{ |
| 210 |
int rc; |
| 211 |
ssh_key key = NULL; |
| 212 |
const char *passphrase = torture_get_testkey_passphrase(); |
| 213 |
|
| 214 |
(void) threadid; /* unused */ |
| 215 |
|
| 216 |
/* test if it returns -1 if key_str is NULL */ |
| 217 |
rc = ssh_pki_import_privkey_base64(NULL, passphrase, NULL, NULL, &key); |
| 218 |
assert_true(rc == -1); |
| 219 |
|
| 220 |
SSH_KEY_FREE(key); |
| 221 |
|
| 222 |
return NULL; |
| 223 |
} |
| 224 |
|
| 225 |
static void torture_pki_rsa_import_privkey_base64_NULL_str(void **state){ |
| 226 |
int rc; |
| 227 |
|
| 228 |
/* Unused */ |
| 229 |
(void) state; |
| 230 |
|
| 231 |
rc = run_on_threads(thread_pki_rsa_import_privkey_base64_NULL_str); |
| 232 |
assert_int_equal(rc, 0); |
| 233 |
} |
| 234 |
|
| 235 |
static void *thread_pki_rsa_import_privkey_base64(void *threadid) |
| 236 |
{ |
| 237 |
const char *passphrase = torture_get_testkey_passphrase(); |
| 238 |
char *key_str = NULL; |
| 239 |
ssh_key key = NULL; |
| 240 |
enum ssh_keytypes_e type; |
| 241 |
int ok; |
| 242 |
int rc; |
| 243 |
|
| 244 |
(void) threadid; /* unused */ |
| 245 |
|
| 246 |
key_str = torture_pki_read_file(LIBSSH_RSA_TESTKEY); |
| 247 |
assert_non_null(key_str); |
| 248 |
|
| 249 |
rc = ssh_pki_import_privkey_base64(key_str, passphrase, NULL, NULL, &key); |
| 250 |
assert_true(rc == 0); |
| 251 |
|
| 252 |
type = ssh_key_type(key); |
| 253 |
assert_true(type == SSH_KEYTYPE_RSA); |
| 254 |
|
| 255 |
ok = ssh_key_is_private(key); |
| 256 |
assert_true(ok); |
| 257 |
|
| 258 |
ok = ssh_key_is_public(key); |
| 259 |
assert_true(ok); |
| 260 |
|
| 261 |
free(key_str); |
| 262 |
SSH_KEY_FREE(key); |
| 263 |
|
| 264 |
return NULL; |
| 265 |
} |
| 266 |
|
| 267 |
static void torture_pki_rsa_import_privkey_base64(void **state) |
| 268 |
{ |
| 269 |
int rc; |
| 270 |
|
| 271 |
/* Unused */ |
| 272 |
(void) state; |
| 273 |
|
| 274 |
rc = run_on_threads(thread_pki_rsa_import_privkey_base64); |
| 275 |
assert_int_equal(rc, 0); |
| 276 |
} |
| 277 |
|
| 278 |
static void *thread_pki_rsa_publickey_from_privatekey(void *threadid) |
| 279 |
{ |
| 280 |
const char *passphrase = NULL; |
| 281 |
const char *testkey; |
| 282 |
ssh_key pubkey = NULL; |
| 283 |
ssh_key key = NULL; |
| 284 |
int rc; |
| 285 |
int ok; |
| 286 |
|
| 287 |
(void) threadid; /* unused */ |
| 288 |
|
| 289 |
testkey = torture_get_testkey(SSH_KEYTYPE_RSA, 0); |
| 290 |
rc = ssh_pki_import_privkey_base64(testkey, |
| 291 |
passphrase, |
| 292 |
NULL, |
| 293 |
NULL, |
| 294 |
&key); |
| 295 |
assert_true(rc == 0); |
| 296 |
assert_non_null(key); |
| 297 |
|
| 298 |
ok = ssh_key_is_private(key); |
| 299 |
assert_true(ok); |
| 300 |
|
| 301 |
rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey); |
| 302 |
assert_true(rc == SSH_OK); |
| 303 |
assert_non_null(pubkey); |
| 304 |
|
| 305 |
SSH_KEY_FREE(key); |
| 306 |
SSH_KEY_FREE(pubkey); |
| 307 |
|
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
static void torture_pki_rsa_publickey_from_privatekey(void **state) |
| 312 |
{ |
| 313 |
int rc; |
| 314 |
|
| 315 |
/* Unused */ |
| 316 |
(void) state; |
| 317 |
|
| 318 |
rc = run_on_threads(thread_pki_rsa_publickey_from_privatekey); |
| 319 |
assert_int_equal(rc, 0); |
| 320 |
} |
| 321 |
|
| 322 |
static void *thread_pki_rsa_copy_cert_to_privkey(void *threadid) |
| 323 |
{ |
| 324 |
/* |
| 325 |
* Tests copying a cert loaded into a public key to a private key. |
| 326 |
* The function is encryption type agnostic, no need to run this against |
| 327 |
* all supported key types. |
| 328 |
*/ |
| 329 |
const char *passphrase = torture_get_testkey_passphrase(); |
| 330 |
const char *testkey = NULL; |
| 331 |
ssh_key pubkey = NULL; |
| 332 |
ssh_key privkey = NULL; |
| 333 |
ssh_key cert = NULL; |
| 334 |
int rc; |
| 335 |
|
| 336 |
(void) threadid; /* unused */ |
| 337 |
|
| 338 |
rc = ssh_pki_import_cert_file(LIBSSH_RSA_TESTKEY "-cert.pub", &cert); |
| 339 |
assert_true(rc == SSH_OK); |
| 340 |
assert_non_null(cert); |
| 341 |
|
| 342 |
rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey); |
| 343 |
assert_true(rc == SSH_OK); |
| 344 |
assert_non_null(pubkey); |
| 345 |
|
| 346 |
testkey = torture_get_testkey(SSH_KEYTYPE_RSA, 0); |
| 347 |
assert_non_null(testkey); |
| 348 |
|
| 349 |
rc = ssh_pki_import_privkey_base64(testkey, |
| 350 |
passphrase, |
| 351 |
NULL, |
| 352 |
NULL, |
| 353 |
&privkey); |
| 354 |
assert_true(rc == SSH_OK); |
| 355 |
assert_non_null(privkey); |
| 356 |
|
| 357 |
/* Basic sanity. */ |
| 358 |
rc = ssh_pki_copy_cert_to_privkey(NULL, privkey); |
| 359 |
assert_true(rc == SSH_ERROR); |
| 360 |
|
| 361 |
rc = ssh_pki_copy_cert_to_privkey(pubkey, NULL); |
| 362 |
assert_true(rc == SSH_ERROR); |
| 363 |
|
| 364 |
/* A public key doesn't have a cert, copy should fail. */ |
| 365 |
rc = ssh_pki_copy_cert_to_privkey(pubkey, privkey); |
| 366 |
assert_true(rc == SSH_ERROR); |
| 367 |
|
| 368 |
/* Copying the cert to non-cert keys should work fine. */ |
| 369 |
rc = ssh_pki_copy_cert_to_privkey(cert, pubkey); |
| 370 |
assert_true(rc == SSH_OK); |
| 371 |
rc = ssh_pki_copy_cert_to_privkey(cert, privkey); |
| 372 |
assert_true(rc == SSH_OK); |
| 373 |
|
| 374 |
/* The private key's cert is already set, another copy should fail. */ |
| 375 |
rc = ssh_pki_copy_cert_to_privkey(cert, privkey); |
| 376 |
assert_true(rc == SSH_ERROR); |
| 377 |
|
| 378 |
SSH_KEY_FREE(cert); |
| 379 |
SSH_KEY_FREE(privkey); |
| 380 |
SSH_KEY_FREE(pubkey); |
| 381 |
|
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
static void torture_pki_rsa_copy_cert_to_privkey(void **state) |
| 386 |
{ |
| 387 |
int rc; |
| 388 |
|
| 389 |
/* Unused */ |
| 390 |
(void) state; |
| 391 |
|
| 392 |
rc = run_on_threads(thread_pki_rsa_copy_cert_to_privkey); |
| 393 |
assert_int_equal(rc, 0); |
| 394 |
} |
| 395 |
|
| 396 |
static void *thread_pki_rsa_import_cert_file(void *threadid) |
| 397 |
{ |
| 398 |
int rc; |
| 399 |
ssh_key cert = NULL; |
| 400 |
enum ssh_keytypes_e type; |
| 401 |
|
| 402 |
(void) threadid; /* unused */ |
| 403 |
|
| 404 |
rc = ssh_pki_import_cert_file(LIBSSH_RSA_TESTKEY "-cert.pub", &cert); |
| 405 |
assert_true(rc == 0); |
| 406 |
assert_non_null(cert); |
| 407 |
|
| 408 |
type = ssh_key_type(cert); |
| 409 |
assert_true(type == SSH_KEYTYPE_RSA_CERT01); |
| 410 |
|
| 411 |
rc = ssh_key_is_public(cert); |
| 412 |
assert_true(rc == 1); |
| 413 |
|
| 414 |
SSH_KEY_FREE(cert); |
| 415 |
|
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
static void torture_pki_rsa_import_cert_file(void **state) |
| 420 |
{ |
| 421 |
int rc; |
| 422 |
|
| 423 |
/* Unused */ |
| 424 |
(void) state; |
| 425 |
|
| 426 |
rc = run_on_threads(thread_pki_rsa_import_cert_file); |
| 427 |
assert_int_equal(rc, 0); |
| 428 |
} |
| 429 |
|
| 430 |
static void *thread_pki_rsa_publickey_base64(void *threadid) |
| 431 |
{ |
| 432 |
enum ssh_keytypes_e type; |
| 433 |
char *b64_key = NULL, *key_buf = NULL, *p = NULL; |
| 434 |
const char *q = NULL; |
| 435 |
ssh_key key; |
| 436 |
int rc; |
| 437 |
|
| 438 |
(void) threadid; /* unused */ |
| 439 |
|
| 440 |
key_buf = strdup(torture_get_testkey_pub(SSH_KEYTYPE_RSA)); |
| 441 |
assert_non_null(key_buf); |
| 442 |
|
| 443 |
q = p = key_buf; |
| 444 |
while (*p != ' ') p++; |
| 445 |
*p = '\0'; |
| 446 |
|
| 447 |
type = ssh_key_type_from_name(q); |
| 448 |
assert_true(type == SSH_KEYTYPE_RSA); |
| 449 |
|
| 450 |
q = ++p; |
| 451 |
while (*p != ' ') p++; |
| 452 |
*p = '\0'; |
| 453 |
|
| 454 |
rc = ssh_pki_import_pubkey_base64(q, type, &key); |
| 455 |
assert_true(rc == 0); |
| 456 |
assert_non_null(key); |
| 457 |
|
| 458 |
rc = ssh_pki_export_pubkey_base64(key, &b64_key); |
| 459 |
assert_true(rc == 0); |
| 460 |
assert_non_null(b64_key); |
| 461 |
|
| 462 |
assert_string_equal(q, b64_key); |
| 463 |
|
| 464 |
free(b64_key); |
| 465 |
free(key_buf); |
| 466 |
SSH_KEY_FREE(key); |
| 467 |
|
| 468 |
return NULL; |
| 469 |
} |
| 470 |
|
| 471 |
static void torture_pki_rsa_publickey_base64(void **state) |
| 472 |
{ |
| 473 |
int rc; |
| 474 |
|
| 475 |
/* Unused */ |
| 476 |
(void) state; |
| 477 |
|
| 478 |
rc = run_on_threads(thread_pki_rsa_publickey_base64); |
| 479 |
assert_int_equal(rc, 0); |
| 480 |
} |
| 481 |
|
| 482 |
static void *thread_pki_rsa_duplicate_key(void *threadid) |
| 483 |
{ |
| 484 |
char *b64_key = NULL; |
| 485 |
char *b64_key_gen = NULL; |
| 486 |
ssh_key pubkey = NULL; |
| 487 |
ssh_key privkey = NULL; |
| 488 |
ssh_key privkey_dup = NULL; |
| 489 |
int cmp; |
| 490 |
int rc; |
| 491 |
|
| 492 |
(void) threadid; |
| 493 |
|
| 494 |
rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey); |
| 495 |
assert_true(rc == 0); |
| 496 |
assert_non_null(pubkey); |
| 497 |
|
| 498 |
rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key); |
| 499 |
assert_true(rc == 0); |
| 500 |
SSH_KEY_FREE(pubkey); |
| 501 |
assert_non_null(b64_key); |
| 502 |
|
| 503 |
rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY, |
| 504 |
NULL, |
| 505 |
NULL, |
| 506 |
NULL, |
| 507 |
&privkey); |
| 508 |
assert_true(rc == 0); |
| 509 |
assert_non_null(privkey); |
| 510 |
|
| 511 |
privkey_dup = ssh_key_dup(privkey); |
| 512 |
assert_non_null(privkey_dup); |
| 513 |
|
| 514 |
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey); |
| 515 |
assert_true(rc == SSH_OK); |
| 516 |
assert_non_null(pubkey); |
| 517 |
|
| 518 |
rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key_gen); |
| 519 |
assert_true(rc == 0); |
| 520 |
assert_non_null(b64_key_gen); |
| 521 |
|
| 522 |
assert_string_equal(b64_key, b64_key_gen); |
| 523 |
|
| 524 |
cmp = ssh_key_cmp(privkey, privkey_dup, SSH_KEY_CMP_PRIVATE); |
| 525 |
assert_true(cmp == 0); |
| 526 |
|
| 527 |
SSH_KEY_FREE(pubkey); |
| 528 |
SSH_KEY_FREE(privkey); |
| 529 |
SSH_KEY_FREE(privkey_dup); |
| 530 |
SSH_STRING_FREE_CHAR(b64_key); |
| 531 |
SSH_STRING_FREE_CHAR(b64_key_gen); |
| 532 |
|
| 533 |
return NULL; |
| 534 |
} |
| 535 |
|
| 536 |
static void torture_pki_rsa_duplicate_key(void **state) |
| 537 |
{ |
| 538 |
int rc; |
| 539 |
|
| 540 |
/* Unused */ |
| 541 |
(void) state; |
| 542 |
|
| 543 |
rc = run_on_threads(thread_pki_rsa_duplicate_key); |
| 544 |
assert_int_equal(rc, 0); |
| 545 |
} |
| 546 |
|
| 547 |
static void *thread_pki_rsa_generate_key(void *threadid) |
| 548 |
{ |
| 549 |
int rc; |
| 550 |
ssh_key key = NULL, pubkey = NULL; |
| 551 |
ssh_signature sign = NULL; |
| 552 |
ssh_session session = NULL; |
| 553 |
|
| 554 |
(void) threadid; |
| 555 |
|
| 556 |
session = ssh_new(); |
| 557 |
assert_non_null(session); |
| 558 |
|
| 559 |
if (!ssh_fips_mode()) { |
| 560 |
rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 1024, &key); |
| 561 |
assert_ssh_return_code(session, rc); |
| 562 |
assert_non_null(key); |
| 563 |
|
| 564 |
rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey); |
| 565 |
assert_int_equal(rc, SSH_OK); |
| 566 |
assert_non_null(pubkey); |
| 567 |
|
| 568 |
sign = pki_do_sign(key, RSA_HASH, 20, SSH_DIGEST_SHA256); |
| 569 |
assert_non_null(sign); |
| 570 |
|
| 571 |
rc = ssh_pki_signature_verify(session, sign, pubkey, RSA_HASH, 20); |
| 572 |
assert_ssh_return_code(session, rc); |
| 573 |
|
| 574 |
ssh_signature_free(sign); |
| 575 |
SSH_KEY_FREE(key); |
| 576 |
SSH_KEY_FREE(pubkey); |
| 577 |
} |
| 578 |
|
| 579 |
rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &key); |
| 580 |
assert_ssh_return_code(session, rc); |
| 581 |
assert_non_null(key); |
| 582 |
|
| 583 |
rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey); |
| 584 |
assert_int_equal(rc, SSH_OK); |
| 585 |
assert_non_null(pubkey); |
| 586 |
|
| 587 |
sign = pki_do_sign(key, RSA_HASH, 20, SSH_DIGEST_SHA256); |
| 588 |
assert_non_null(sign); |
| 589 |
|
| 590 |
rc = ssh_pki_signature_verify(session, sign, pubkey, RSA_HASH, 20); |
| 591 |
assert_ssh_return_code(session, rc); |
| 592 |
|
| 593 |
ssh_signature_free(sign); |
| 594 |
SSH_KEY_FREE(key); |
| 595 |
SSH_KEY_FREE(pubkey); |
| 596 |
|
| 597 |
rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 4096, &key); |
| 598 |
assert_true(rc == SSH_OK); |
| 599 |
assert_non_null(key); |
| 600 |
|
| 601 |
rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey); |
| 602 |
assert_int_equal(rc, SSH_OK); |
| 603 |
assert_non_null(pubkey); |
| 604 |
|
| 605 |
sign = pki_do_sign(key, RSA_HASH, 20, SSH_DIGEST_SHA256); |
| 606 |
assert_non_null(sign); |
| 607 |
|
| 608 |
rc = ssh_pki_signature_verify(session, sign, pubkey, RSA_HASH, 20); |
| 609 |
assert_true(rc == SSH_OK); |
| 610 |
|
| 611 |
ssh_signature_free(sign); |
| 612 |
SSH_KEY_FREE(key); |
| 613 |
SSH_KEY_FREE(pubkey); |
| 614 |
|
| 615 |
ssh_free(session); |
| 616 |
|
| 617 |
return NULL; |
| 618 |
} |
| 619 |
|
| 620 |
static void torture_pki_rsa_generate_key(void **state) |
| 621 |
{ |
| 622 |
int rc; |
| 623 |
|
| 624 |
/* Unused */ |
| 625 |
(void) state; |
| 626 |
|
| 627 |
rc = run_on_threads(thread_pki_rsa_generate_key); |
| 628 |
assert_int_equal(rc, 0); |
| 629 |
} |
| 630 |
|
| 631 |
static void *thread_pki_rsa_import_privkey_base64_passphrase(void *threadid) |
| 632 |
{ |
| 633 |
int rc; |
| 634 |
ssh_key key = NULL; |
| 635 |
const char *passphrase = torture_get_testkey_passphrase(); |
| 636 |
const char *testkey; |
| 637 |
|
| 638 |
(void) threadid; /* unused */ |
| 639 |
|
| 640 |
testkey = torture_get_testkey(SSH_KEYTYPE_RSA, 1); |
| 641 |
assert_non_null(testkey); |
| 642 |
|
| 643 |
rc = ssh_pki_import_privkey_base64(testkey, |
| 644 |
passphrase, |
| 645 |
NULL, |
| 646 |
NULL, |
| 647 |
&key); |
| 648 |
assert_return_code(rc, errno); |
| 649 |
|
| 650 |
rc = ssh_key_is_private(key); |
| 651 |
assert_true(rc == 1); |
| 652 |
|
| 653 |
SSH_KEY_FREE(key); |
| 654 |
|
| 655 |
/* test if it returns -1 if passphrase is wrong */ |
| 656 |
rc = ssh_pki_import_privkey_base64(testkey, |
| 657 |
"wrong passphrase !!", |
| 658 |
NULL, |
| 659 |
NULL, |
| 660 |
&key); |
| 661 |
assert_true(rc == -1); |
| 662 |
SSH_KEY_FREE(key); |
| 663 |
|
| 664 |
#ifndef HAVE_LIBCRYPTO |
| 665 |
/* test if it returns -1 if passphrase is NULL */ |
| 666 |
/* libcrypto asks for a passphrase, so skip this test */ |
| 667 |
rc = ssh_pki_import_privkey_base64(testkey, |
| 668 |
NULL, |
| 669 |
NULL, |
| 670 |
NULL, |
| 671 |
&key); |
| 672 |
assert_true(rc == -1); |
| 673 |
SSH_KEY_FREE(key); |
| 674 |
#endif |
| 675 |
|
| 676 |
return NULL; |
| 677 |
} |
| 678 |
|
| 679 |
static void torture_pki_rsa_import_privkey_base64_passphrase(void **state) |
| 680 |
{ |
| 681 |
int rc; |
| 682 |
|
| 683 |
/* Unused */ |
| 684 |
(void) state; |
| 685 |
|
| 686 |
rc = run_on_threads(thread_pki_rsa_import_privkey_base64_passphrase); |
| 687 |
assert_int_equal(rc, 0); |
| 688 |
} |
| 689 |
|
| 690 |
#define NUM_TESTS 11 |
| 691 |
|
| 692 |
static void torture_mixed(void **state) |
| 693 |
{ |
| 694 |
pthread_t threads[NUM_TESTS][NUM_THREADS]; |
| 695 |
|
| 696 |
int i; |
| 697 |
int f; |
| 698 |
int rc; |
| 699 |
|
| 700 |
/* Array of functions to run on threads */ |
| 701 |
static void *(*funcs[NUM_TESTS])(void *) = { |
| 702 |
thread_pki_rsa_import_pubkey_file, |
| 703 |
thread_pki_rsa_import_privkey_base64_NULL_key, |
| 704 |
thread_pki_rsa_import_privkey_base64_NULL_str, |
| 705 |
thread_pki_rsa_import_privkey_base64, |
| 706 |
thread_pki_rsa_publickey_from_privatekey, |
| 707 |
thread_pki_rsa_import_privkey_base64_passphrase, |
| 708 |
thread_pki_rsa_copy_cert_to_privkey, |
| 709 |
thread_pki_rsa_import_cert_file, |
| 710 |
thread_pki_rsa_publickey_base64, |
| 711 |
thread_pki_rsa_duplicate_key, |
| 712 |
thread_pki_rsa_generate_key, |
| 713 |
}; |
| 714 |
|
| 715 |
(void) state; |
| 716 |
|
| 717 |
/* Call tests in a round-robin fashion */ |
| 718 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 719 |
for (f = 0; f < NUM_TESTS; f++) { |
| 720 |
rc = pthread_create(&threads[f][i], NULL, funcs[f], NULL); |
| 721 |
assert_int_equal(rc, 0); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
for (f = 0; f < NUM_TESTS; f++) { |
| 726 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 727 |
rc = pthread_join(threads[f][i], NULL); |
| 728 |
assert_int_equal(rc, 0); |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
int torture_run_tests(void) |
| 734 |
{ |
| 735 |
int rc; |
| 736 |
struct CMUnitTest tests[] = { |
| 737 |
cmocka_unit_test_setup_teardown(torture_pki_rsa_import_pubkey_file, |
| 738 |
setup_rsa_key, |
| 739 |
teardown), |
| 740 |
cmocka_unit_test_setup_teardown( |
| 741 |
torture_pki_rsa_import_privkey_base64_NULL_key, |
| 742 |
setup_rsa_key, |
| 743 |
teardown), |
| 744 |
cmocka_unit_test_setup_teardown( |
| 745 |
torture_pki_rsa_import_privkey_base64_NULL_str, |
| 746 |
setup_rsa_key, |
| 747 |
teardown), |
| 748 |
cmocka_unit_test_setup_teardown(torture_pki_rsa_import_privkey_base64, |
| 749 |
setup_rsa_key, |
| 750 |
teardown), |
| 751 |
cmocka_unit_test_setup_teardown( |
| 752 |
torture_pki_rsa_publickey_from_privatekey, |
| 753 |
setup_rsa_key, |
| 754 |
teardown), |
| 755 |
cmocka_unit_test(torture_pki_rsa_import_privkey_base64_passphrase), |
| 756 |
cmocka_unit_test_setup_teardown(torture_pki_rsa_copy_cert_to_privkey, |
| 757 |
setup_rsa_key, |
| 758 |
teardown), |
| 759 |
cmocka_unit_test_setup_teardown(torture_pki_rsa_import_cert_file, |
| 760 |
setup_rsa_key, |
| 761 |
teardown), |
| 762 |
cmocka_unit_test_setup_teardown(torture_pki_rsa_publickey_base64, |
| 763 |
setup_rsa_key, |
| 764 |
teardown), |
| 765 |
cmocka_unit_test_setup_teardown(torture_pki_rsa_duplicate_key, |
| 766 |
setup_rsa_key, |
| 767 |
teardown), |
| 768 |
cmocka_unit_test(torture_pki_rsa_generate_key), |
| 769 |
cmocka_unit_test_setup_teardown(torture_mixed, setup_rsa_key, teardown), |
| 770 |
}; |
| 771 |
|
| 772 |
/* |
| 773 |
* Not testing: |
| 774 |
* - pki_rsa_generate_pubkey_from_privkey |
| 775 |
* - pki_rsa_write_privkey |
| 776 |
* |
| 777 |
* The original tests in torture_pki_rsa.c require files to be erased |
| 778 |
*/ |
| 779 |
|
| 780 |
/* |
| 781 |
* If the library is statically linked, ssh_init() is not called |
| 782 |
* automatically |
| 783 |
*/ |
| 784 |
disable_secmem(); |
| 785 |
ssh_init(); |
| 786 |
torture_filter_tests(tests); |
| 787 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 788 |
ssh_finalize(); |
| 789 |
|
| 790 |
return rc; |
| 791 |
} |