1 1.1 christos /* 2 1.1 christos * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos #include <openssl/ssl.h> 10 1.1 christos 11 1.1 christos #include "helpers/ssltestlib.h" 12 1.1 christos #include "internal/dane.h" 13 1.1 christos #include "testutil.h" 14 1.1 christos 15 1.1 christos #undef OSSL_NO_USABLE_TLS1_3 16 1.1 christos #if defined(OPENSSL_NO_TLS1_3) \ 17 1.1 christos || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)) 18 1.1 christos /* 19 1.1 christos * If we don't have ec or dh then there are no built-in groups that are usable 20 1.1 christos * with TLSv1.3 21 1.1 christos */ 22 1.1.1.2 christos #define OSSL_NO_USABLE_TLS1_3 23 1.1 christos #endif 24 1.1 christos 25 1.1 christos static char *certsdir = NULL; 26 1.1 christos static char *rootcert = NULL; 27 1.1 christos static char *cert = NULL; 28 1.1 christos static char *privkey = NULL; 29 1.1 christos static char *cert2 = NULL; 30 1.1 christos static char *privkey2 = NULL; 31 1.1 christos static char *cert448 = NULL; 32 1.1 christos static char *privkey448 = NULL; 33 1.1 christos static char *cert25519 = NULL; 34 1.1 christos static char *privkey25519 = NULL; 35 1.1 christos static OSSL_LIB_CTX *libctx = NULL; 36 1.1 christos static OSSL_PROVIDER *defctxnull = NULL; 37 1.1 christos 38 1.1 christos static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 }; 39 1.1 christos static const unsigned char SID_CTX[] = { 'r', 'p', 'k' }; 40 1.1 christos 41 1.1 christos static int rpk_verify_client_cb(int ok, X509_STORE_CTX *ctx) 42 1.1 christos { 43 1.1 christos int err = X509_STORE_CTX_get_error(ctx); 44 1.1 christos 45 1.1 christos if (X509_STORE_CTX_get0_rpk(ctx) != NULL) { 46 1.1 christos if (err != X509_V_OK) { 47 1.1 christos TEST_info("rpk_verify_client_cb: ok=%d err=%d", ok, err); 48 1.1 christos return 0; 49 1.1 christos } 50 1.1 christos } 51 1.1 christos return 1; 52 1.1 christos } 53 1.1 christos static int rpk_verify_server_cb(int ok, X509_STORE_CTX *ctx) 54 1.1 christos { 55 1.1 christos int err = X509_STORE_CTX_get_error(ctx); 56 1.1 christos 57 1.1 christos if (X509_STORE_CTX_get0_rpk(ctx) != NULL) { 58 1.1 christos if (err != X509_V_OK) { 59 1.1 christos TEST_info("rpk_verify_server_cb: ok=%d err=%d", ok, err); 60 1.1 christos return 0; 61 1.1 christos } 62 1.1 christos } 63 1.1 christos return 1; 64 1.1 christos } 65 1.1 christos 66 1.1 christos /* 67 1.1 christos * Test dimensions: 68 1.1 christos * (2) server_cert_type RPK off/on for server 69 1.1 christos * (2) client_cert_type RPK off/on for server 70 1.1 christos * (2) server_cert_type RPK off/on for client 71 1.1 christos * (2) client_cert_type RPK off/on for client 72 1.1 christos * (4) RSA vs ECDSA vs Ed25519 vs Ed448 certificates 73 1.1 christos * (2) TLSv1.2 vs TLSv1.3 74 1.1 christos * 75 1.1 christos * Tests: 76 1.1 christos * idx = 0 - is the normal success case, certificate, single peer key 77 1.1 christos * idx = 1 - only a private key 78 1.1 christos * idx = 2 - add client authentication 79 1.1 christos * idx = 3 - add second peer key (rootcert.pem) 80 1.1 christos * idx = 4 - add second peer key (different, RSA or ECDSA) 81 1.1 christos * idx = 5 - reverse peer keys (rootcert.pem, different order) 82 1.1 christos * idx = 6 - reverse peer keys (RSA or ECDSA, different order) 83 1.1 christos * idx = 7 - expects failure due to mismatched key (RSA or ECDSA) 84 1.1 christos * idx = 8 - expects failure due to no configured key on client 85 1.1 christos * idx = 9 - add client authentication (PHA) 86 1.1 christos * idx = 10 - add client authentication (privake key only) 87 1.1 christos * idx = 11 - simple resumption 88 1.1 christos * idx = 12 - simple resumption, no ticket 89 1.1 christos * idx = 13 - resumption with client authentication 90 1.1 christos * idx = 14 - resumption with client authentication, no ticket 91 1.1 christos * idx = 15 - like 0, but use non-default libctx 92 1.1 christos * idx = 16 - like 7, but with SSL_VERIFY_PEER connection should fail 93 1.1 christos * idx = 17 - like 8, but with SSL_VERIFY_PEER connection should fail 94 1.1 christos * 95 1.1 christos * 18 * 2 * 4 * 2 * 2 * 2 * 2 = 2304 tests 96 1.1 christos */ 97 1.1 christos static int test_rpk(int idx) 98 1.1 christos { 99 1.1.1.2 christos #define RPK_TESTS 18 100 1.1.1.2 christos #define RPK_DIMS (2 * 4 * 2 * 2 * 2 * 2) 101 1.1 christos SSL_CTX *cctx = NULL, *sctx = NULL; 102 1.1 christos SSL *clientssl = NULL, *serverssl = NULL; 103 1.1 christos EVP_PKEY *pkey = NULL, *other_pkey = NULL, *root_pkey = NULL; 104 1.1 christos X509 *x509 = NULL, *other_x509 = NULL, *root_x509 = NULL; 105 1.1 christos int testresult = 0, ret, expected = 1; 106 1.1 christos int client_expected = X509_V_OK; 107 1.1 christos int verify; 108 1.1 christos int tls_version; 109 1.1 christos char *cert_file = NULL; 110 1.1 christos char *privkey_file = NULL; 111 1.1 christos char *other_cert_file = NULL; 112 1.1 christos SSL_SESSION *client_sess = NULL; 113 1.1 christos SSL_SESSION *server_sess = NULL; 114 1.1 christos int idx_server_server_rpk, idx_server_client_rpk; 115 1.1 christos int idx_client_server_rpk, idx_client_client_rpk; 116 1.1 christos int idx_cert, idx_prot; 117 1.1 christos int client_auth = 0; 118 1.1 christos int resumption = 0; 119 1.1 christos int want_error = SSL_ERROR_NONE; 120 1.1 christos long server_verify_result = 0; 121 1.1 christos long client_verify_result = 0; 122 1.1 christos OSSL_LIB_CTX *test_libctx = NULL; 123 1.1 christos 124 1.1 christos if (!TEST_int_le(idx, RPK_TESTS * RPK_DIMS)) 125 1.1 christos return 0; 126 1.1 christos 127 1.1 christos idx_server_server_rpk = idx / (RPK_TESTS * 2 * 4 * 2 * 2 * 2); 128 1.1 christos idx %= RPK_TESTS * 2 * 4 * 2 * 2 * 2; 129 1.1 christos idx_server_client_rpk = idx / (RPK_TESTS * 2 * 4 * 2 * 2); 130 1.1 christos idx %= RPK_TESTS * 2 * 4 * 2 * 2; 131 1.1 christos idx_client_server_rpk = idx / (RPK_TESTS * 2 * 4 * 2); 132 1.1 christos idx %= RPK_TESTS * 2 * 4 * 2; 133 1.1 christos idx_client_client_rpk = idx / (RPK_TESTS * 2 * 4); 134 1.1 christos idx %= RPK_TESTS * 2 * 4; 135 1.1 christos idx_cert = idx / (RPK_TESTS * 2); 136 1.1 christos idx %= RPK_TESTS * 2; 137 1.1 christos idx_prot = idx / RPK_TESTS; 138 1.1 christos idx %= RPK_TESTS; 139 1.1 christos 140 1.1 christos /* Load "root" cert/pubkey */ 141 1.1 christos root_x509 = load_cert_pem(rootcert, NULL); 142 1.1 christos if (!TEST_ptr(root_x509)) 143 1.1 christos goto end; 144 1.1 christos root_pkey = X509_get0_pubkey(root_x509); 145 1.1 christos if (!TEST_ptr(root_pkey)) 146 1.1 christos goto end; 147 1.1 christos 148 1.1 christos switch (idx_cert) { 149 1.1.1.2 christos case 0: 150 1.1.1.2 christos /* use RSA */ 151 1.1.1.2 christos cert_file = cert; 152 1.1.1.2 christos privkey_file = privkey; 153 1.1.1.2 christos other_cert_file = cert2; 154 1.1.1.2 christos break; 155 1.1 christos #ifndef OPENSSL_NO_ECDSA 156 1.1.1.2 christos case 1: 157 1.1.1.2 christos /* use ECDSA */ 158 1.1.1.2 christos cert_file = cert2; 159 1.1.1.2 christos privkey_file = privkey2; 160 1.1.1.2 christos other_cert_file = cert; 161 1.1.1.2 christos break; 162 1.1.1.2 christos #ifndef OPENSSL_NO_ECX 163 1.1.1.2 christos case 2: 164 1.1.1.2 christos /* use Ed448 */ 165 1.1.1.2 christos cert_file = cert448; 166 1.1.1.2 christos privkey_file = privkey448; 167 1.1.1.2 christos other_cert_file = cert; 168 1.1.1.2 christos break; 169 1.1.1.2 christos case 3: 170 1.1.1.2 christos /* use Ed25519 */ 171 1.1.1.2 christos cert_file = cert25519; 172 1.1.1.2 christos privkey_file = privkey25519; 173 1.1.1.2 christos other_cert_file = cert; 174 1.1.1.2 christos break; 175 1.1 christos #endif 176 1.1.1.2 christos #endif 177 1.1.1.2 christos default: 178 1.1.1.2 christos testresult = TEST_skip("EDCSA disabled"); 179 1.1.1.2 christos goto end; 180 1.1 christos } 181 1.1 christos /* Load primary cert */ 182 1.1 christos x509 = load_cert_pem(cert_file, NULL); 183 1.1 christos if (!TEST_ptr(x509)) 184 1.1 christos goto end; 185 1.1 christos pkey = X509_get0_pubkey(x509); 186 1.1 christos /* load other cert */ 187 1.1 christos other_x509 = load_cert_pem(other_cert_file, NULL); 188 1.1 christos if (!TEST_ptr(other_x509)) 189 1.1 christos goto end; 190 1.1 christos other_pkey = X509_get0_pubkey(other_x509); 191 1.1 christos #ifdef OPENSSL_NO_ECDSA 192 1.1 christos /* Can't get other_key if it's ECDSA */ 193 1.1 christos if (other_pkey == NULL && idx_cert == 0 194 1.1 christos && (idx == 4 || idx == 6 || idx == 7 || idx == 16)) { 195 1.1 christos testresult = TEST_skip("EDCSA disabled"); 196 1.1 christos goto end; 197 1.1 christos } 198 1.1 christos #endif 199 1.1 christos 200 1.1 christos switch (idx_prot) { 201 1.1 christos case 0: 202 1.1 christos #ifdef OSSL_NO_USABLE_TLS1_3 203 1.1 christos testresult = TEST_skip("TLSv1.3 disabled"); 204 1.1 christos goto end; 205 1.1 christos #else 206 1.1 christos tls_version = TLS1_3_VERSION; 207 1.1 christos break; 208 1.1 christos #endif 209 1.1 christos case 1: 210 1.1 christos #ifdef OPENSSL_NO_TLS1_2 211 1.1 christos testresult = TEST_skip("TLSv1.2 disabled"); 212 1.1 christos goto end; 213 1.1 christos #else 214 1.1 christos tls_version = TLS1_2_VERSION; 215 1.1 christos break; 216 1.1 christos #endif 217 1.1 christos default: 218 1.1 christos goto end; 219 1.1 christos } 220 1.1 christos 221 1.1 christos if (idx == 15) { 222 1.1 christos test_libctx = libctx; 223 1.1 christos defctxnull = OSSL_PROVIDER_load(NULL, "null"); 224 1.1 christos if (!TEST_ptr(defctxnull)) 225 1.1 christos goto end; 226 1.1 christos } 227 1.1 christos if (!TEST_true(create_ssl_ctx_pair(test_libctx, 228 1.1.1.2 christos TLS_server_method(), TLS_client_method(), 229 1.1.1.2 christos tls_version, tls_version, 230 1.1.1.2 christos &sctx, &cctx, NULL, NULL))) 231 1.1 christos goto end; 232 1.1 christos 233 1.1 christos if (idx_server_server_rpk) 234 1.1 christos if (!TEST_true(SSL_CTX_set1_server_cert_type(sctx, cert_type_rpk, sizeof(cert_type_rpk)))) 235 1.1 christos goto end; 236 1.1 christos if (idx_server_client_rpk) 237 1.1 christos if (!TEST_true(SSL_CTX_set1_client_cert_type(sctx, cert_type_rpk, sizeof(cert_type_rpk)))) 238 1.1 christos goto end; 239 1.1 christos if (idx_client_server_rpk) 240 1.1 christos if (!TEST_true(SSL_CTX_set1_server_cert_type(cctx, cert_type_rpk, sizeof(cert_type_rpk)))) 241 1.1 christos goto end; 242 1.1 christos if (idx_client_client_rpk) 243 1.1 christos if (!TEST_true(SSL_CTX_set1_client_cert_type(cctx, cert_type_rpk, sizeof(cert_type_rpk)))) 244 1.1 christos goto end; 245 1.1 christos if (!TEST_true(SSL_CTX_set_session_id_context(sctx, SID_CTX, sizeof(SID_CTX)))) 246 1.1 christos goto end; 247 1.1 christos if (!TEST_true(SSL_CTX_set_session_id_context(cctx, SID_CTX, sizeof(SID_CTX)))) 248 1.1 christos goto end; 249 1.1 christos 250 1.1 christos if (!TEST_int_gt(SSL_CTX_dane_enable(sctx), 0)) 251 1.1 christos goto end; 252 1.1 christos if (!TEST_int_gt(SSL_CTX_dane_enable(cctx), 0)) 253 1.1 christos goto end; 254 1.1 christos 255 1.1 christos /* NEW */ 256 1.1 christos SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, rpk_verify_client_cb); 257 1.1 christos 258 1.1 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, 259 1.1.1.2 christos NULL, NULL))) 260 1.1 christos goto end; 261 1.1 christos 262 1.1 christos if (!TEST_int_gt(SSL_dane_enable(serverssl, NULL), 0)) 263 1.1 christos goto end; 264 1.1 christos if (!TEST_int_gt(SSL_dane_enable(clientssl, "example.com"), 0)) 265 1.1 christos goto end; 266 1.1 christos 267 1.1 christos /* Set private key and certificate */ 268 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(serverssl, privkey_file, SSL_FILETYPE_PEM), 1)) 269 1.1 christos goto end; 270 1.1 christos /* Only a private key */ 271 1.1 christos if (idx == 1) { 272 1.1 christos if (idx_server_server_rpk == 0 || idx_client_server_rpk == 0) { 273 1.1 christos expected = 0; 274 1.1 christos want_error = SSL_ERROR_SSL; 275 1.1 christos } 276 1.1 christos } else { 277 1.1 christos /* Add certificate */ 278 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(serverssl, cert_file, SSL_FILETYPE_PEM), 1)) 279 1.1 christos goto end; 280 1.1 christos if (!TEST_int_eq(SSL_check_private_key(serverssl), 1)) 281 1.1 christos goto end; 282 1.1 christos } 283 1.1 christos 284 1.1 christos switch (idx) { 285 1.1 christos default: 286 1.1 christos if (!TEST_true(idx < RPK_TESTS)) 287 1.1 christos goto end; 288 1.1 christos break; 289 1.1 christos case 0: 290 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 291 1.1 christos goto end; 292 1.1 christos break; 293 1.1 christos case 1: 294 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 295 1.1 christos goto end; 296 1.1 christos break; 297 1.1 christos case 2: 298 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 299 1.1 christos goto end; 300 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey))) 301 1.1 christos goto end; 302 1.1 christos /* Use the same key for client auth */ 303 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 304 1.1 christos goto end; 305 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1)) 306 1.1 christos goto end; 307 1.1 christos if (!TEST_int_eq(SSL_check_private_key(clientssl), 1)) 308 1.1 christos goto end; 309 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb); 310 1.1 christos client_auth = 1; 311 1.1 christos break; 312 1.1 christos case 3: 313 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 314 1.1 christos goto end; 315 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, root_pkey))) 316 1.1 christos goto end; 317 1.1 christos break; 318 1.1 christos case 4: 319 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 320 1.1 christos goto end; 321 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey))) 322 1.1 christos goto end; 323 1.1 christos break; 324 1.1 christos case 5: 325 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, root_pkey))) 326 1.1 christos goto end; 327 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 328 1.1 christos goto end; 329 1.1 christos break; 330 1.1 christos case 6: 331 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey))) 332 1.1 christos goto end; 333 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 334 1.1 christos goto end; 335 1.1 christos break; 336 1.1 christos case 7: 337 1.1 christos if (idx_server_server_rpk == 1 && idx_client_server_rpk == 1) 338 1.1 christos client_expected = -1; 339 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey))) 340 1.1 christos goto end; 341 1.1 christos SSL_set_verify(clientssl, SSL_VERIFY_NONE, rpk_verify_client_cb); 342 1.1 christos client_verify_result = X509_V_ERR_DANE_NO_MATCH; 343 1.1 christos break; 344 1.1 christos case 8: 345 1.1 christos if (idx_server_server_rpk == 1 && idx_client_server_rpk == 1) 346 1.1 christos client_expected = -1; 347 1.1 christos /* no peer keys */ 348 1.1 christos SSL_set_verify(clientssl, SSL_VERIFY_NONE, rpk_verify_client_cb); 349 1.1 christos client_verify_result = X509_V_ERR_RPK_UNTRUSTED; 350 1.1 christos break; 351 1.1 christos case 9: 352 1.1 christos if (tls_version != TLS1_3_VERSION) { 353 1.1 christos testresult = TEST_skip("PHA requires TLSv1.3"); 354 1.1 christos goto end; 355 1.1 christos } 356 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 357 1.1 christos goto end; 358 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey))) 359 1.1 christos goto end; 360 1.1 christos /* Use the same key for client auth */ 361 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 362 1.1 christos goto end; 363 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1)) 364 1.1 christos goto end; 365 1.1 christos if (!TEST_int_eq(SSL_check_private_key(clientssl), 1)) 366 1.1 christos goto end; 367 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_POST_HANDSHAKE, rpk_verify_server_cb); 368 1.1 christos SSL_set_post_handshake_auth(clientssl, 1); 369 1.1 christos client_auth = 1; 370 1.1 christos break; 371 1.1 christos case 10: 372 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 373 1.1 christos goto end; 374 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey))) 375 1.1 christos goto end; 376 1.1 christos /* Use the same key for client auth */ 377 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 378 1.1 christos goto end; 379 1.1 christos /* Since there's no cert, this is expected to fail without RPK support */ 380 1.1 christos if (!idx_server_client_rpk || !idx_client_client_rpk) { 381 1.1 christos expected = 0; 382 1.1 christos want_error = SSL_ERROR_SSL; 383 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); 384 1.1 christos } else { 385 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb); 386 1.1 christos } 387 1.1 christos client_auth = 1; 388 1.1 christos break; 389 1.1 christos case 11: 390 1.1 christos if (!idx_server_server_rpk || !idx_client_server_rpk) { 391 1.1 christos testresult = TEST_skip("Only testing resumption with server RPK"); 392 1.1 christos goto end; 393 1.1 christos } 394 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 395 1.1 christos goto end; 396 1.1 christos resumption = 1; 397 1.1 christos break; 398 1.1 christos case 12: 399 1.1 christos if (!idx_server_server_rpk || !idx_client_server_rpk) { 400 1.1 christos testresult = TEST_skip("Only testing resumption with server RPK"); 401 1.1 christos goto end; 402 1.1 christos } 403 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 404 1.1 christos goto end; 405 1.1 christos SSL_set_options(serverssl, SSL_OP_NO_TICKET); 406 1.1 christos SSL_set_options(clientssl, SSL_OP_NO_TICKET); 407 1.1 christos resumption = 1; 408 1.1 christos break; 409 1.1 christos case 13: 410 1.1 christos if (!idx_server_server_rpk || !idx_client_server_rpk) { 411 1.1 christos testresult = TEST_skip("Only testing resumption with server RPK"); 412 1.1 christos goto end; 413 1.1 christos } 414 1.1 christos if (!idx_server_client_rpk || !idx_client_client_rpk) { 415 1.1 christos testresult = TEST_skip("Only testing client authentication resumption with client RPK"); 416 1.1 christos goto end; 417 1.1 christos } 418 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 419 1.1 christos goto end; 420 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey))) 421 1.1 christos goto end; 422 1.1 christos /* Use the same key for client auth */ 423 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 424 1.1 christos goto end; 425 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1)) 426 1.1 christos goto end; 427 1.1 christos if (!TEST_int_eq(SSL_check_private_key(clientssl), 1)) 428 1.1 christos goto end; 429 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb); 430 1.1 christos client_auth = 1; 431 1.1 christos resumption = 1; 432 1.1 christos break; 433 1.1 christos case 14: 434 1.1 christos if (!idx_server_server_rpk || !idx_client_server_rpk) { 435 1.1 christos testresult = TEST_skip("Only testing resumption with server RPK"); 436 1.1 christos goto end; 437 1.1 christos } 438 1.1 christos if (!idx_server_client_rpk || !idx_client_client_rpk) { 439 1.1 christos testresult = TEST_skip("Only testing client authentication resumption with client RPK"); 440 1.1 christos goto end; 441 1.1 christos } 442 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 443 1.1 christos goto end; 444 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, pkey))) 445 1.1 christos goto end; 446 1.1 christos /* Use the same key for client auth */ 447 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 448 1.1 christos goto end; 449 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1)) 450 1.1 christos goto end; 451 1.1 christos if (!TEST_int_eq(SSL_check_private_key(clientssl), 1)) 452 1.1 christos goto end; 453 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb); 454 1.1 christos SSL_set_options(serverssl, SSL_OP_NO_TICKET); 455 1.1 christos SSL_set_options(clientssl, SSL_OP_NO_TICKET); 456 1.1 christos client_auth = 1; 457 1.1 christos resumption = 1; 458 1.1 christos break; 459 1.1 christos case 15: 460 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, pkey))) 461 1.1 christos goto end; 462 1.1 christos break; 463 1.1 christos case 16: 464 1.1 christos if (idx_server_server_rpk == 1 && idx_client_server_rpk == 1) { 465 1.1 christos /* wrong expected server key */ 466 1.1 christos expected = 0; 467 1.1 christos want_error = SSL_ERROR_SSL; 468 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL); 469 1.1 christos } 470 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, other_pkey))) 471 1.1 christos goto end; 472 1.1 christos break; 473 1.1 christos case 17: 474 1.1 christos if (idx_server_server_rpk == 1 && idx_client_server_rpk == 1) { 475 1.1 christos /* no expected server keys */ 476 1.1 christos expected = 0; 477 1.1 christos want_error = SSL_ERROR_SSL; 478 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL); 479 1.1 christos } 480 1.1 christos break; 481 1.1 christos } 482 1.1 christos 483 1.1 christos ret = create_ssl_connection(serverssl, clientssl, want_error); 484 1.1 christos if (!TEST_int_eq(expected, ret)) 485 1.1 christos goto end; 486 1.1 christos 487 1.1 christos if (expected <= 0) { 488 1.1 christos testresult = 1; 489 1.1 christos goto end; 490 1.1 christos } 491 1.1 christos 492 1.1 christos /* Make sure client gets RPK or certificate as configured */ 493 1.1 christos if (idx_server_server_rpk && idx_client_server_rpk) { 494 1.1 christos if (!TEST_long_eq(SSL_get_verify_result(clientssl), client_verify_result)) 495 1.1 christos goto end; 496 1.1 christos if (!TEST_ptr(SSL_get0_peer_rpk(clientssl))) 497 1.1 christos goto end; 498 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(serverssl), TLSEXT_cert_type_rpk)) 499 1.1 christos goto end; 500 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(clientssl), TLSEXT_cert_type_rpk)) 501 1.1 christos goto end; 502 1.1 christos } else { 503 1.1 christos if (!TEST_ptr(SSL_get0_peer_certificate(clientssl))) 504 1.1 christos goto end; 505 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(serverssl), TLSEXT_cert_type_x509)) 506 1.1 christos goto end; 507 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(clientssl), TLSEXT_cert_type_x509)) 508 1.1 christos goto end; 509 1.1 christos } 510 1.1 christos 511 1.1 christos if (idx == 9) { 512 1.1 christos /* Make PHA happen... */ 513 1.1 christos if (!TEST_true(SSL_verify_client_post_handshake(serverssl))) 514 1.1 christos goto end; 515 1.1 christos if (!TEST_true(SSL_do_handshake(serverssl))) 516 1.1 christos goto end; 517 1.1 christos if (!TEST_int_le(SSL_read(clientssl, NULL, 0), 0)) 518 1.1 christos goto end; 519 1.1 christos if (!TEST_int_le(SSL_read(serverssl, NULL, 0), 0)) 520 1.1 christos goto end; 521 1.1 christos } 522 1.1 christos 523 1.1 christos /* Make sure server gets an RPK or certificate as configured */ 524 1.1 christos if (client_auth) { 525 1.1 christos if (idx_server_client_rpk && idx_client_client_rpk) { 526 1.1 christos if (!TEST_long_eq(SSL_get_verify_result(serverssl), server_verify_result)) 527 1.1 christos goto end; 528 1.1 christos if (!TEST_ptr(SSL_get0_peer_rpk(serverssl))) 529 1.1 christos goto end; 530 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(serverssl), TLSEXT_cert_type_rpk)) 531 1.1 christos goto end; 532 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(clientssl), TLSEXT_cert_type_rpk)) 533 1.1 christos goto end; 534 1.1 christos } else { 535 1.1 christos if (!TEST_ptr(SSL_get0_peer_certificate(serverssl))) 536 1.1 christos goto end; 537 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(serverssl), TLSEXT_cert_type_x509)) 538 1.1 christos goto end; 539 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(clientssl), TLSEXT_cert_type_x509)) 540 1.1 christos goto end; 541 1.1 christos } 542 1.1 christos } 543 1.1 christos 544 1.1 christos if (resumption) { 545 1.1 christos EVP_PKEY *client_pkey = NULL; 546 1.1 christos EVP_PKEY *server_pkey = NULL; 547 1.1 christos 548 1.1 christos if (!TEST_ptr((client_sess = SSL_get1_session(clientssl))) 549 1.1.1.2 christos || !TEST_ptr((client_pkey = SSL_SESSION_get0_peer_rpk(client_sess)))) 550 1.1 christos goto end; 551 1.1 christos if (client_auth) { 552 1.1 christos if (!TEST_ptr((server_sess = SSL_get1_session(serverssl))) 553 1.1 christos || !TEST_ptr((server_pkey = SSL_SESSION_get0_peer_rpk(server_sess)))) 554 1.1.1.2 christos goto end; 555 1.1 christos } 556 1.1 christos SSL_shutdown(clientssl); 557 1.1 christos SSL_shutdown(serverssl); 558 1.1 christos SSL_free(clientssl); 559 1.1 christos SSL_free(serverssl); 560 1.1 christos serverssl = clientssl = NULL; 561 1.1 christos 562 1.1 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, 563 1.1.1.2 christos NULL, NULL)) 564 1.1.1.2 christos || !TEST_true(SSL_set_session(clientssl, client_sess))) 565 1.1 christos goto end; 566 1.1 christos 567 1.1 christos /* Set private key (and maybe certificate) */ 568 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(serverssl, privkey_file, SSL_FILETYPE_PEM), 1)) 569 1.1 christos goto end; 570 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(serverssl, cert_file, SSL_FILETYPE_PEM), 1)) 571 1.1 christos goto end; 572 1.1 christos if (!TEST_int_eq(SSL_check_private_key(serverssl), 1)) 573 1.1 christos goto end; 574 1.1 christos if (!TEST_int_gt(SSL_dane_enable(serverssl, "example.com"), 0)) 575 1.1 christos goto end; 576 1.1 christos if (!TEST_int_gt(SSL_dane_enable(clientssl, "example.com"), 0)) 577 1.1 christos goto end; 578 1.1 christos 579 1.1 christos switch (idx) { 580 1.1 christos default: 581 1.1 christos break; 582 1.1 christos case 11: 583 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey))) 584 1.1 christos goto end; 585 1.1 christos break; 586 1.1 christos case 12: 587 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey))) 588 1.1 christos goto end; 589 1.1 christos SSL_set_options(clientssl, SSL_OP_NO_TICKET); 590 1.1 christos SSL_set_options(serverssl, SSL_OP_NO_TICKET); 591 1.1 christos break; 592 1.1 christos case 13: 593 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey))) 594 1.1 christos goto end; 595 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, server_pkey))) 596 1.1 christos goto end; 597 1.1 christos /* Use the same key for client auth */ 598 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 599 1.1 christos goto end; 600 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1)) 601 1.1 christos goto end; 602 1.1 christos if (!TEST_int_eq(SSL_check_private_key(clientssl), 1)) 603 1.1 christos goto end; 604 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb); 605 1.1 christos break; 606 1.1 christos case 14: 607 1.1 christos if (!TEST_true(SSL_add_expected_rpk(clientssl, client_pkey))) 608 1.1 christos goto end; 609 1.1 christos if (!TEST_true(SSL_add_expected_rpk(serverssl, server_pkey))) 610 1.1 christos goto end; 611 1.1 christos /* Use the same key for client auth */ 612 1.1 christos if (!TEST_int_eq(SSL_use_PrivateKey_file(clientssl, privkey_file, SSL_FILETYPE_PEM), 1)) 613 1.1 christos goto end; 614 1.1 christos if (!TEST_int_eq(SSL_use_certificate_file(clientssl, cert_file, SSL_FILETYPE_PEM), 1)) 615 1.1 christos goto end; 616 1.1 christos if (!TEST_int_eq(SSL_check_private_key(clientssl), 1)) 617 1.1 christos goto end; 618 1.1 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, rpk_verify_server_cb); 619 1.1 christos SSL_set_options(serverssl, SSL_OP_NO_TICKET); 620 1.1 christos SSL_set_options(clientssl, SSL_OP_NO_TICKET); 621 1.1 christos break; 622 1.1 christos } 623 1.1 christos 624 1.1 christos ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE); 625 1.1 christos if (!TEST_true(ret)) 626 1.1 christos goto end; 627 1.1 christos verify = SSL_get_verify_result(clientssl); 628 1.1 christos if (!TEST_int_eq(client_expected, verify)) 629 1.1 christos goto end; 630 1.1 christos if (!TEST_true(SSL_session_reused(clientssl))) 631 1.1 christos goto end; 632 1.1 christos 633 1.1 christos if (!TEST_ptr(SSL_get0_peer_rpk(clientssl))) 634 1.1 christos goto end; 635 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(serverssl), TLSEXT_cert_type_rpk)) 636 1.1 christos goto end; 637 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_server_cert_type(clientssl), TLSEXT_cert_type_rpk)) 638 1.1 christos goto end; 639 1.1 christos 640 1.1 christos if (client_auth) { 641 1.1 christos if (!TEST_ptr(SSL_get0_peer_rpk(serverssl))) 642 1.1 christos goto end; 643 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(serverssl), TLSEXT_cert_type_rpk)) 644 1.1 christos goto end; 645 1.1 christos if (!TEST_int_eq(SSL_get_negotiated_client_cert_type(clientssl), TLSEXT_cert_type_rpk)) 646 1.1 christos goto end; 647 1.1 christos } 648 1.1 christos } 649 1.1 christos 650 1.1 christos testresult = 1; 651 1.1 christos 652 1.1.1.2 christos end: 653 1.1 christos OSSL_PROVIDER_unload(defctxnull); 654 1.1 christos defctxnull = NULL; 655 1.1 christos SSL_SESSION_free(client_sess); 656 1.1 christos SSL_SESSION_free(server_sess); 657 1.1 christos SSL_free(serverssl); 658 1.1 christos SSL_free(clientssl); 659 1.1 christos SSL_CTX_free(sctx); 660 1.1 christos SSL_CTX_free(cctx); 661 1.1 christos X509_free(x509); 662 1.1 christos X509_free(other_x509); 663 1.1 christos X509_free(root_x509); 664 1.1 christos 665 1.1 christos if (testresult == 0) { 666 1.1 christos TEST_info("idx_ss_rpk=%d, idx_sc_rpk=%d, idx_cs_rpk=%d, idx_cc_rpk=%d, idx_cert=%d, idx_prot=%d, idx=%d", 667 1.1.1.2 christos idx_server_server_rpk, idx_server_client_rpk, 668 1.1.1.2 christos idx_client_server_rpk, idx_client_client_rpk, 669 1.1.1.2 christos idx_cert, idx_prot, idx); 670 1.1 christos } 671 1.1 christos return testresult; 672 1.1 christos } 673 1.1 christos 674 1.1 christos static int test_rpk_api(void) 675 1.1 christos { 676 1.1 christos int ret = 0; 677 1.1 christos SSL_CTX *cctx = NULL, *sctx = NULL; 678 1.1 christos unsigned char cert_type_dups[] = { TLSEXT_cert_type_rpk, 679 1.1.1.2 christos TLSEXT_cert_type_x509, 680 1.1.1.2 christos TLSEXT_cert_type_x509 }; 681 1.1 christos unsigned char cert_type_bad[] = { 0xFF }; 682 1.1 christos unsigned char cert_type_extra[] = { TLSEXT_cert_type_rpk, 683 1.1.1.2 christos TLSEXT_cert_type_x509, 684 1.1.1.2 christos 0xFF }; 685 1.1 christos unsigned char cert_type_unsup[] = { TLSEXT_cert_type_pgp, 686 1.1.1.2 christos TLSEXT_cert_type_1609dot2 }; 687 1.1 christos unsigned char cert_type_just_x509[] = { TLSEXT_cert_type_x509 }; 688 1.1 christos unsigned char cert_type_just_rpk[] = { TLSEXT_cert_type_rpk }; 689 1.1 christos 690 1.1 christos if (!TEST_true(create_ssl_ctx_pair(NULL, 691 1.1.1.2 christos TLS_server_method(), TLS_client_method(), 692 1.1.1.2 christos TLS1_2_VERSION, TLS1_2_VERSION, 693 1.1.1.2 christos &sctx, &cctx, NULL, NULL))) 694 1.1 christos goto end; 695 1.1 christos 696 1.1 christos if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_dups, sizeof(cert_type_dups)))) 697 1.1 christos goto end; 698 1.1 christos 699 1.1 christos if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_bad, sizeof(cert_type_bad)))) 700 1.1 christos goto end; 701 1.1 christos 702 1.1 christos if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_extra, sizeof(cert_type_extra)))) 703 1.1 christos goto end; 704 1.1 christos 705 1.1 christos if (!TEST_false(SSL_CTX_set1_server_cert_type(sctx, cert_type_unsup, sizeof(cert_type_unsup)))) 706 1.1 christos goto end; 707 1.1 christos 708 1.1 christos if (!TEST_true(SSL_CTX_set1_server_cert_type(sctx, cert_type_just_x509, sizeof(cert_type_just_x509)))) 709 1.1 christos goto end; 710 1.1 christos 711 1.1 christos if (!TEST_true(SSL_CTX_set1_server_cert_type(sctx, cert_type_just_rpk, sizeof(cert_type_just_rpk)))) 712 1.1 christos goto end; 713 1.1 christos 714 1.1 christos ret = 1; 715 1.1.1.2 christos end: 716 1.1 christos SSL_CTX_free(sctx); 717 1.1 christos SSL_CTX_free(cctx); 718 1.1 christos return ret; 719 1.1 christos } 720 1.1 christos OPT_TEST_DECLARE_USAGE("certdir\n") 721 1.1 christos 722 1.1 christos int setup_tests(void) 723 1.1 christos { 724 1.1 christos if (!test_skip_common_options()) { 725 1.1 christos TEST_error("Error parsing test options\n"); 726 1.1 christos return 0; 727 1.1 christos } 728 1.1 christos 729 1.1 christos if (!TEST_ptr(certsdir = test_get_argument(0))) 730 1.1 christos return 0; 731 1.1 christos 732 1.1 christos rootcert = test_mk_file_path(certsdir, "rootcert.pem"); 733 1.1 christos if (rootcert == NULL) 734 1.1 christos goto err; 735 1.1 christos 736 1.1 christos cert = test_mk_file_path(certsdir, "servercert.pem"); 737 1.1 christos if (cert == NULL) 738 1.1 christos goto err; 739 1.1 christos 740 1.1 christos privkey = test_mk_file_path(certsdir, "serverkey.pem"); 741 1.1 christos if (privkey == NULL) 742 1.1 christos goto err; 743 1.1 christos 744 1.1 christos cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem"); 745 1.1 christos if (cert2 == NULL) 746 1.1 christos goto err; 747 1.1 christos 748 1.1 christos privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem"); 749 1.1 christos if (privkey2 == NULL) 750 1.1 christos goto err; 751 1.1 christos 752 1.1 christos cert448 = test_mk_file_path(certsdir, "server-ed448-cert.pem"); 753 1.1 christos if (cert2 == NULL) 754 1.1 christos goto err; 755 1.1 christos 756 1.1 christos privkey448 = test_mk_file_path(certsdir, "server-ed448-key.pem"); 757 1.1 christos if (privkey2 == NULL) 758 1.1 christos goto err; 759 1.1 christos 760 1.1 christos cert25519 = test_mk_file_path(certsdir, "server-ed25519-cert.pem"); 761 1.1 christos if (cert2 == NULL) 762 1.1 christos goto err; 763 1.1 christos 764 1.1 christos privkey25519 = test_mk_file_path(certsdir, "server-ed25519-key.pem"); 765 1.1 christos if (privkey2 == NULL) 766 1.1 christos goto err; 767 1.1 christos 768 1.1 christos libctx = OSSL_LIB_CTX_new(); 769 1.1 christos if (libctx == NULL) 770 1.1 christos goto err; 771 1.1 christos 772 1.1 christos ADD_TEST(test_rpk_api); 773 1.1 christos ADD_ALL_TESTS(test_rpk, RPK_TESTS * RPK_DIMS); 774 1.1 christos return 1; 775 1.1 christos 776 1.1.1.2 christos err: 777 1.1 christos return 0; 778 1.1 christos } 779 1.1 christos 780 1.1 christos void cleanup_tests(void) 781 1.1 christos { 782 1.1 christos OPENSSL_free(rootcert); 783 1.1 christos OPENSSL_free(cert); 784 1.1 christos OPENSSL_free(privkey); 785 1.1 christos OPENSSL_free(cert2); 786 1.1 christos OPENSSL_free(privkey2); 787 1.1 christos OPENSSL_free(cert448); 788 1.1 christos OPENSSL_free(privkey448); 789 1.1 christos OPENSSL_free(cert25519); 790 1.1 christos OPENSSL_free(privkey25519); 791 1.1 christos OSSL_LIB_CTX_free(libctx); 792 1.1.1.2 christos } 793