| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2024 by Red Hat, Inc. |
| 5 |
* |
| 6 |
* Authors: Jakub Jelen <[email protected]> |
| 7 |
* Sahana Prasad <[email protected]> |
| 8 |
* |
| 9 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU Lesser General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 12 |
* option) any later version. |
| 13 |
* |
| 14 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 |
* License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public License |
| 20 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 21 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 22 |
* MA 02111-1307, USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#define LIBSSH_STATIC |
| 28 |
|
| 29 |
#include "pki.c" |
| 30 |
#include "torture.h" |
| 31 |
#include "torture_key.h" |
| 32 |
#include "torture_pki.h" |
| 33 |
|
| 34 |
#define LIBSSH_EDDSA_TESTKEY "libssh_testkey.id_ed25519" |
| 35 |
#define LIBSSH_EDDSA_TESTKEY_PASSPHRASE "libssh_testkey_passphrase.id_ed25519" |
| 36 |
#define PUB_URI_FMT "pkcs11:token=%s;object=%s;type=public" |
| 37 |
#define PRIV_URI_FMT "pkcs11:token=%s;object=%s;type=private?pin-value=%s" |
| 38 |
|
| 39 |
const char template[] = "/tmp/temp_dir_XXXXXX"; |
| 40 |
const unsigned char INPUT[] = "1234567890123456789012345678901234567890" |
| 41 |
"123456789012345678901234"; |
| 42 |
struct pki_st { |
| 43 |
char *orig_dir; |
| 44 |
char *temp_dir; |
| 45 |
char *pub_uri; |
| 46 |
char *priv_uri; |
| 47 |
char *priv_uri_invalid_object; |
| 48 |
char *priv_uri_invalid_token; |
| 49 |
char *pub_uri_invalid_object; |
| 50 |
char *pub_uri_invalid_token; |
| 51 |
}; |
| 52 |
|
| 53 |
static int setup_tokens(void **state) |
| 54 |
{ |
| 55 |
char keys_path[1024] = {0}; |
| 56 |
char keys_path_pub[1024] = {0}; |
| 57 |
char *cwd = NULL; |
| 58 |
struct pki_st *test_state = *state; |
| 59 |
char obj_tempname[] = "label_XXXXXX"; |
| 60 |
char pub_uri[1024] = {0}; |
| 61 |
char priv_uri[1024] = {0}; |
| 62 |
char pub_uri_invalid_object[1024] = {0}; |
| 63 |
char priv_uri_invalid_object[1024] = {0}; |
| 64 |
char pub_uri_invalid_token[1024] = {0}; |
| 65 |
char priv_uri_invalid_token[1024] = {0}; |
| 66 |
|
| 67 |
cwd = test_state->temp_dir; |
| 68 |
assert_non_null(cwd); |
| 69 |
|
| 70 |
ssh_tmpname(obj_tempname); |
| 71 |
|
| 72 |
snprintf(pub_uri, sizeof(pub_uri), PUB_URI_FMT, obj_tempname, obj_tempname); |
| 73 |
|
| 74 |
snprintf(priv_uri, |
| 75 |
sizeof(priv_uri), |
| 76 |
PRIV_URI_FMT, |
| 77 |
obj_tempname, |
| 78 |
obj_tempname, |
| 79 |
"1234"); |
| 80 |
|
| 81 |
snprintf(pub_uri_invalid_token, |
| 82 |
sizeof(pub_uri_invalid_token), |
| 83 |
PUB_URI_FMT, |
| 84 |
"invalid", |
| 85 |
obj_tempname); |
| 86 |
|
| 87 |
snprintf(priv_uri_invalid_token, |
| 88 |
sizeof(priv_uri_invalid_token), |
| 89 |
PRIV_URI_FMT, |
| 90 |
"invalid", |
| 91 |
obj_tempname, |
| 92 |
"1234"); |
| 93 |
|
| 94 |
snprintf(pub_uri_invalid_object, |
| 95 |
sizeof(pub_uri_invalid_object), |
| 96 |
PUB_URI_FMT, |
| 97 |
obj_tempname, |
| 98 |
"invalid"); |
| 99 |
|
| 100 |
snprintf(priv_uri_invalid_object, |
| 101 |
sizeof(priv_uri_invalid_object), |
| 102 |
PRIV_URI_FMT, |
| 103 |
obj_tempname, |
| 104 |
"invalid", |
| 105 |
"1234"); |
| 106 |
|
| 107 |
snprintf(keys_path, sizeof(keys_path), "%s/%s", cwd, LIBSSH_EDDSA_TESTKEY); |
| 108 |
|
| 109 |
snprintf(keys_path_pub, |
| 110 |
sizeof(keys_path_pub), |
| 111 |
"%s/%s.pub", |
| 112 |
cwd, |
| 113 |
LIBSSH_EDDSA_TESTKEY); |
| 114 |
|
| 115 |
test_state->pub_uri = strdup(pub_uri); |
| 116 |
test_state->priv_uri = strdup(priv_uri); |
| 117 |
test_state->pub_uri_invalid_token = strdup(pub_uri_invalid_token); |
| 118 |
test_state->pub_uri_invalid_object = strdup(pub_uri_invalid_object); |
| 119 |
test_state->priv_uri_invalid_token = strdup(priv_uri_invalid_token); |
| 120 |
test_state->priv_uri_invalid_object = strdup(priv_uri_invalid_object); |
| 121 |
|
| 122 |
torture_write_file(keys_path, torture_get_testkey(SSH_KEYTYPE_ED25519, 0)); |
| 123 |
torture_write_file(keys_path_pub, |
| 124 |
torture_get_testkey_pub_pem(SSH_KEYTYPE_ED25519)); |
| 125 |
|
| 126 |
torture_setup_tokens(cwd, keys_path, obj_tempname, "1"); |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static int setup_directory_structure(void **state) |
| 132 |
{ |
| 133 |
struct pki_st *test_state = NULL; |
| 134 |
char *temp_dir = NULL; |
| 135 |
int rc; |
| 136 |
|
| 137 |
test_state = (struct pki_st *)malloc(sizeof(struct pki_st)); |
| 138 |
assert_non_null(test_state); |
| 139 |
|
| 140 |
test_state->orig_dir = torture_get_current_working_dir(); |
| 141 |
assert_non_null(test_state->orig_dir); |
| 142 |
|
| 143 |
temp_dir = torture_make_temp_dir(template); |
| 144 |
assert_non_null(temp_dir); |
| 145 |
|
| 146 |
rc = torture_change_dir(temp_dir); |
| 147 |
assert_int_equal(rc, 0); |
| 148 |
free(temp_dir); |
| 149 |
|
| 150 |
test_state->temp_dir = torture_get_current_working_dir(); |
| 151 |
assert_non_null(test_state->temp_dir); |
| 152 |
|
| 153 |
*state = test_state; |
| 154 |
|
| 155 |
rc = setup_tokens(state); |
| 156 |
assert_int_equal(rc, 0); |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static int teardown_directory_structure(void **state) |
| 162 |
{ |
| 163 |
struct pki_st *test_state = *state; |
| 164 |
int rc; |
| 165 |
|
| 166 |
torture_cleanup_tokens(test_state->temp_dir); |
| 167 |
|
| 168 |
rc = torture_change_dir(test_state->orig_dir); |
| 169 |
assert_int_equal(rc, 0); |
| 170 |
|
| 171 |
rc = torture_rmdirs(test_state->temp_dir); |
| 172 |
assert_int_equal(rc, 0); |
| 173 |
|
| 174 |
SAFE_FREE(test_state->temp_dir); |
| 175 |
SAFE_FREE(test_state->orig_dir); |
| 176 |
SAFE_FREE(test_state->priv_uri); |
| 177 |
SAFE_FREE(test_state->pub_uri); |
| 178 |
SAFE_FREE(test_state->priv_uri_invalid_object); |
| 179 |
SAFE_FREE(test_state->pub_uri_invalid_object); |
| 180 |
SAFE_FREE(test_state->priv_uri_invalid_token); |
| 181 |
SAFE_FREE(test_state->pub_uri_invalid_token); |
| 182 |
SAFE_FREE(test_state); |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static void torture_pki_ed25519_import_pubkey_uri(void **state) |
| 188 |
{ |
| 189 |
ssh_key pubkey = NULL; |
| 190 |
int rc; |
| 191 |
struct pki_st *test_state = *state; |
| 192 |
|
| 193 |
rc = ssh_pki_import_pubkey_file(test_state->pub_uri, &pubkey); |
| 194 |
|
| 195 |
assert_return_code(rc, errno); |
| 196 |
assert_non_null(pubkey); |
| 197 |
|
| 198 |
rc = ssh_key_is_public(pubkey); |
| 199 |
assert_int_equal(rc, 1); |
| 200 |
|
| 201 |
SSH_KEY_FREE(pubkey); |
| 202 |
} |
| 203 |
|
| 204 |
static void torture_pki_ed25519_import_privkey_uri(void **state) |
| 205 |
{ |
| 206 |
int rc; |
| 207 |
ssh_key privkey = NULL; |
| 208 |
struct pki_st *test_state = *state; |
| 209 |
|
| 210 |
rc = ssh_pki_import_privkey_file(test_state->priv_uri, |
| 211 |
NULL, |
| 212 |
NULL, |
| 213 |
NULL, |
| 214 |
&privkey); |
| 215 |
assert_return_code(rc, errno); |
| 216 |
assert_non_null(privkey); |
| 217 |
|
| 218 |
rc = ssh_key_is_private(privkey); |
| 219 |
assert_int_equal(rc, 1); |
| 220 |
|
| 221 |
SSH_KEY_FREE(privkey); |
| 222 |
} |
| 223 |
|
| 224 |
static void torture_pki_sign_verify_uri(void **state) |
| 225 |
{ |
| 226 |
int rc; |
| 227 |
ssh_key privkey = NULL, pubkey = NULL; |
| 228 |
ssh_signature sign = NULL; |
| 229 |
ssh_session session = ssh_new(); |
| 230 |
struct pki_st *test_state = *state; |
| 231 |
|
| 232 |
rc = ssh_pki_import_privkey_file(test_state->priv_uri, |
| 233 |
NULL, |
| 234 |
NULL, |
| 235 |
NULL, |
| 236 |
&privkey); |
| 237 |
assert_return_code(rc, errno); |
| 238 |
assert_non_null(privkey); |
| 239 |
|
| 240 |
rc = ssh_pki_import_pubkey_file(test_state->pub_uri, &pubkey); |
| 241 |
assert_return_code(rc, errno); |
| 242 |
assert_non_null(pubkey); |
| 243 |
|
| 244 |
sign = pki_do_sign(privkey, INPUT, sizeof(INPUT), SSH_DIGEST_AUTO); |
| 245 |
assert_non_null(sign); |
| 246 |
|
| 247 |
rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT)); |
| 248 |
assert_return_code(rc, errno); |
| 249 |
|
| 250 |
ssh_signature_free(sign); |
| 251 |
SSH_KEY_FREE(privkey); |
| 252 |
SSH_KEY_FREE(pubkey); |
| 253 |
|
| 254 |
ssh_free(session); |
| 255 |
} |
| 256 |
|
| 257 |
static void torture_pki_ed25519_publickey_from_privatekey_uri(void **state) |
| 258 |
{ |
| 259 |
int rc; |
| 260 |
ssh_key privkey = NULL; |
| 261 |
ssh_key pubkey = NULL; |
| 262 |
struct pki_st *test_state = *state; |
| 263 |
|
| 264 |
rc = ssh_pki_import_privkey_file(test_state->priv_uri, |
| 265 |
NULL, |
| 266 |
NULL, |
| 267 |
NULL, |
| 268 |
&privkey); |
| 269 |
assert_return_code(rc, errno); |
| 270 |
assert_non_null(privkey); |
| 271 |
|
| 272 |
rc = ssh_key_is_private(privkey); |
| 273 |
assert_int_equal(rc, 1); |
| 274 |
|
| 275 |
rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey); |
| 276 |
assert_return_code(rc, errno); |
| 277 |
assert_non_null(pubkey); |
| 278 |
|
| 279 |
SSH_KEY_FREE(privkey); |
| 280 |
SSH_KEY_FREE(pubkey); |
| 281 |
} |
| 282 |
|
| 283 |
static void torture_pki_ed25519_uri_invalid_configurations(void **state) |
| 284 |
{ |
| 285 |
int rc; |
| 286 |
ssh_key pubkey = NULL; |
| 287 |
ssh_key privkey = NULL; |
| 288 |
|
| 289 |
struct pki_st *test_state = *state; |
| 290 |
|
| 291 |
rc = ssh_pki_import_pubkey_file(test_state->pub_uri_invalid_object, &pubkey); |
| 292 |
assert_int_not_equal(rc, 0); |
| 293 |
assert_null(pubkey); |
| 294 |
|
| 295 |
rc = ssh_pki_import_pubkey_file(test_state->pub_uri_invalid_token, &pubkey); |
| 296 |
assert_int_not_equal(rc, 0); |
| 297 |
assert_null(pubkey); |
| 298 |
|
| 299 |
rc = ssh_pki_import_privkey_file(test_state->priv_uri_invalid_object, |
| 300 |
NULL, |
| 301 |
NULL, |
| 302 |
NULL, |
| 303 |
&privkey); |
| 304 |
assert_int_not_equal(rc, 0); |
| 305 |
assert_null(privkey); |
| 306 |
|
| 307 |
rc = ssh_pki_import_privkey_file(test_state->priv_uri_invalid_token, |
| 308 |
NULL, |
| 309 |
NULL, |
| 310 |
NULL, |
| 311 |
&privkey); |
| 312 |
assert_int_not_equal(rc, 0); |
| 313 |
assert_null(privkey); |
| 314 |
} |
| 315 |
|
| 316 |
int torture_run_tests(void) |
| 317 |
{ |
| 318 |
int rc; |
| 319 |
struct CMUnitTest tests[] = { |
| 320 |
cmocka_unit_test(torture_pki_ed25519_import_pubkey_uri), |
| 321 |
cmocka_unit_test(torture_pki_ed25519_import_privkey_uri), |
| 322 |
cmocka_unit_test(torture_pki_sign_verify_uri), |
| 323 |
cmocka_unit_test(torture_pki_ed25519_publickey_from_privatekey_uri), |
| 324 |
cmocka_unit_test(torture_pki_ed25519_uri_invalid_configurations), |
| 325 |
}; |
| 326 |
|
| 327 |
ssh_session session = ssh_new(); |
| 328 |
int verbosity = torture_libssh_verbosity(); |
| 329 |
|
| 330 |
/* Skip test FIPS mode altogether. */ |
| 331 |
if (ssh_fips_mode()) { |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
/* Do not use system openssl.cnf for the pkcs11 uri tests. |
| 336 |
* It can load a pkcs11 provider too early before we will set up environment |
| 337 |
* variables that are needed for the pkcs11 provider to access correct |
| 338 |
* tokens, causing unexpected failures. |
| 339 |
* Make sure this comes before ssh_init(), which initializes OpenSSL! |
| 340 |
*/ |
| 341 |
setenv("OPENSSL_CONF", SOURCEDIR "/tests/etc/openssl.cnf", 1); |
| 342 |
|
| 343 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 344 |
|
| 345 |
ssh_init(); |
| 346 |
|
| 347 |
torture_filter_tests(tests); |
| 348 |
rc = cmocka_run_group_tests(tests, |
| 349 |
setup_directory_structure, |
| 350 |
teardown_directory_structure); |
| 351 |
|
| 352 |
ssh_free(session); |
| 353 |
|
| 354 |
ssh_finalize(); |
| 355 |
|
| 356 |
return rc; |
| 357 |
} |