static int ssl_do_config(SSL *s, SSL_CTX *ctx, const char *name) { SSL_CONF_CTX *cctx = NULL; size_t i; int rv = 0; unsigned int flags; const SSL_METHOD *meth; const struct ssl_conf_name *nm; struct ssl_conf_cmd *cmd; if (s == NULL && ctx == NULL) { SSLerr(SSL_F_SSL_DO_CONFIG, ERR_R_PASSED_NULL_PARAMETER); goto err; } nm = ssl_name_find(name); if (nm == NULL) { SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_INVALID_CONFIGURATION_NAME); ERR_add_error_data(2, "name=", name); goto err; } cctx = SSL_CONF_CTX_new(); if (cctx == NULL) goto err; flags = SSL_CONF_FLAG_FILE; flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE; if (s != NULL) { meth = s->method; SSL_CONF_CTX_set_ssl(cctx, s); } else { meth = ctx->method; SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); } if (meth->ssl_accept != ssl_undefined_function) flags |= SSL_CONF_FLAG_SERVER; if (meth->ssl_connect != ssl_undefined_function) flags |= SSL_CONF_FLAG_CLIENT; SSL_CONF_CTX_set_flags(cctx, flags); for (i = 0, cmd = nm->cmds; i < nm->cmd_count; i++, cmd++) { rv = SSL_CONF_cmd(cctx, cmd->cmd, cmd->arg); if (rv <= 0) { if (rv == -2) SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_UNKNOWN_COMMAND); else SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_BAD_VALUE); ERR_add_error_data(6, "section=", name, ", cmd=", cmd->cmd, ", arg=", cmd->arg); goto err; } } rv = SSL_CONF_CTX_finish(cctx); err: SSL_CONF_CTX_free(cctx); return rv <= 0 ? 0 : 1; }
int main(int argc, char *argv[]) { BIO *err; int testresult = 0; int currtest = 0; SSL_library_init(); SSL_load_error_strings(); err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); CRYPTO_malloc_debug_init(); CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); confctx = SSL_CONF_CTX_new(); ctx = SSL_CTX_new(SSLv23_method()); ssl = SSL_new(ctx); if (confctx == NULL || ctx == NULL) goto end; SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE | SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_SERVER); /* * For each test set up an SSL_CTX and SSL and see whether SSLv2 is enabled * as expected after various SSL_CONF_cmd("Protocol", ...) calls. */ for (currtest = 0; currtest < TOTAL_NUM_TESTS; currtest++) { BIO_printf(err, "SSLv2 CONF Test number %d\n", currtest); if (currtest == TEST_SSL_CTX) SSL_CONF_CTX_set_ssl_ctx(confctx, ctx); else SSL_CONF_CTX_set_ssl(confctx, ssl); /* SSLv2 should be off by default */ if (!checksslv2(currtest, SSLV2OFF)) { BIO_printf(err, "SSLv2 CONF Test: Off by default test FAIL\n"); goto end; } if (SSL_CONF_cmd(confctx, "Protocol", "ALL") != 2 || !SSL_CONF_CTX_finish(confctx)) { BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n"); goto end; } /* Should still be off even after ALL Protocols on */ if (!checksslv2(currtest, SSLV2OFF)) { BIO_printf(err, "SSLv2 CONF Test: Off after config #1 FAIL\n"); goto end; } if (SSL_CONF_cmd(confctx, "Protocol", "SSLv2") != 2 || !SSL_CONF_CTX_finish(confctx)) { BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n"); goto end; } /* Should still be off even if explicitly asked for */ if (!checksslv2(currtest, SSLV2OFF)) { BIO_printf(err, "SSLv2 CONF Test: Off after config #2 FAIL\n"); goto end; } if (SSL_CONF_cmd(confctx, "Protocol", "-SSLv2") != 2 || !SSL_CONF_CTX_finish(confctx)) { BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n");; goto end; } if (!checksslv2(currtest, SSLV2OFF)) { BIO_printf(err, "SSLv2 CONF Test: Off after config #3 FAIL\n"); goto end; } if (currtest == TEST_SSL_CTX) SSL_CTX_clear_options(ctx, SSL_OP_NO_SSLv2); else SSL_clear_options(ssl, SSL_OP_NO_SSLv2); if (!checksslv2(currtest, SSLV2ON)) { BIO_printf(err, "SSLv2 CONF Test: On after clear FAIL\n"); goto end; } if (SSL_CONF_cmd(confctx, "Protocol", "ALL") != 2 || !SSL_CONF_CTX_finish(confctx)) { BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n"); goto end; } /* Option has been cleared and config says have SSLv2 so should be on */ if (!checksslv2(currtest, SSLV2ON)) { BIO_printf(err, "SSLv2 CONF Test: On after config #1 FAIL\n"); goto end; } if (SSL_CONF_cmd(confctx, "Protocol", "SSLv2") != 2 || !SSL_CONF_CTX_finish(confctx)) { BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n"); goto end; } /* Option has been cleared and config says have SSLv2 so should be on */ if (!checksslv2(currtest, SSLV2ON)) { BIO_printf(err, "SSLv2 CONF Test: On after config #2 FAIL\n"); goto end; } if (SSL_CONF_cmd(confctx, "Protocol", "-SSLv2") != 2 || !SSL_CONF_CTX_finish(confctx)) { BIO_printf(err, "SSLv2 CONF Test: SSL_CONF command FAIL\n"); goto end; } /* Option has been cleared but config says no SSLv2 so should be off */ if (!checksslv2(currtest, SSLV2OFF)) { BIO_printf(err, "SSLv2 CONF Test: Off after config #4 FAIL\n"); goto end; } } testresult = 1; end: SSL_free(ssl); SSL_CTX_free(ctx); SSL_CONF_CTX_free(confctx); if (!testresult) { printf("SSLv2 CONF test: FAILED (Test %d)\n", currtest); ERR_print_errors(err); } else { printf("SSLv2 CONF test: PASSED\n"); } ERR_free_strings(); ERR_remove_thread_state(NULL); EVP_cleanup(); CRYPTO_cleanup_all_ex_data(); CRYPTO_mem_leaks(err); BIO_free(err); return testresult ? EXIT_SUCCESS : EXIT_FAILURE; }
NOEXPORT int conf_init(SERVICE_OPTIONS *section) { #if OPENSSL_VERSION_NUMBER>=0x10002000L SSL_CONF_CTX *cctx; NAME_LIST *curr; char *cmd, *param; if(!section->config) return 0; /* OK */ cctx=SSL_CONF_CTX_new(); if(!cctx) { sslerror("SSL_CONF_CTX_new"); return 1; /* FAILED */ } SSL_CONF_CTX_set_ssl_ctx(cctx, section->ctx); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE); SSL_CONF_CTX_set_flags(cctx, section->option.client ? SSL_CONF_FLAG_CLIENT : SSL_CONF_FLAG_SERVER); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE); for(curr=section->config; curr; curr=curr->next) { cmd=str_dup(curr->name); param=strchr(cmd, ':'); if(param) *param++='\0'; switch(SSL_CONF_cmd(cctx, cmd, param)) { case 2: s_log(LOG_DEBUG, "OpenSSL config \"%s\" set to \"%s\"", cmd, param); break; case 1: s_log(LOG_DEBUG, "OpenSSL config command \"%s\" executed", cmd); break; case -2: s_log(LOG_ERR, "OpenSSL config command \"%s\" was not recognised", cmd); str_free(cmd); SSL_CONF_CTX_free(cctx); return 1; /* FAILED */ case -3: s_log(LOG_ERR, "OpenSSL config command \"%s\" requires a parameter", cmd); str_free(cmd); SSL_CONF_CTX_free(cctx); return 1; /* FAILED */ default: sslerror("SSL_CONF_cmd"); str_free(cmd); SSL_CONF_CTX_free(cctx); return 1; /* FAILED */ } str_free(cmd); } if(!SSL_CONF_CTX_finish(cctx)) { sslerror("SSL_CONF_CTX_finish"); SSL_CONF_CTX_free(cctx); return 1; /* FAILED */ } SSL_CONF_CTX_free(cctx); #else /* OpenSSL earlier than 1.0.2 */ (void)section; /* squash the unused parameter warning */ #endif /* OpenSSL 1.0.2 or later */ return 0; /* OK */ }
int main(int argc, char **argv) { BIO *sbio = NULL, *out = NULL; int i, len, rv; char tmpbuf[1024]; SSL_CTX *ctx = NULL; SSL_CONF_CTX *cctx = NULL; SSL *ssl = NULL; CONF *conf = NULL; STACK_OF(CONF_VALUE) *sect = NULL; CONF_VALUE *cnf; const char *connect_str = "localhost:4433"; long errline = -1; ERR_load_crypto_strings(); ERR_load_SSL_strings(); SSL_library_init(); conf = NCONF_new(NULL); if (NCONF_load(conf, "connect.cnf", &errline) <= 0) { if (errline <= 0) fprintf(stderr, "Error processing config file\n"); else fprintf(stderr, "Error on line %ld\n", errline); goto end; } sect = NCONF_get_section(conf, "default"); if (sect == NULL) { fprintf(stderr, "Error retrieving default section\n"); goto end; } ctx = SSL_CTX_new(SSLv3_client_method()); cctx = SSL_CONF_CTX_new(); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); for (i = 0; i < sk_CONF_VALUE_num(sect); i++) { cnf = sk_CONF_VALUE_value(sect, i); rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value); if (rv > 0) continue; if (rv != -2) { fprintf(stderr, "Error processing %s = %s\n", cnf->name, cnf->value); ERR_print_errors_fp(stderr); goto end; } if (!strcmp(cnf->name, "Connect")) { connect_str = cnf->value; } else { fprintf(stderr, "Unknown configuration option %s\n", cnf->name); goto end; } } if (!SSL_CONF_CTX_finish(cctx)) { fprintf(stderr, "Finish error\n"); ERR_print_errors_fp(stderr); goto err; } /* * We'd normally set some stuff like the verify paths and * mode here * because as things stand this will connect to * any server whose * certificate is signed by any CA. */ sbio = BIO_new_ssl_connect(ctx); BIO_get_ssl(sbio, &ssl); if (!ssl) { fprintf(stderr, "Can't locate SSL pointer\n"); goto end; } /* Don't want any retries */ SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); /* We might want to do other things with ssl here */ BIO_set_conn_hostname(sbio, connect_str); out = BIO_new_fp(stdout, BIO_NOCLOSE); if (BIO_do_connect(sbio) <= 0) { fprintf(stderr, "Error connecting to server\n"); ERR_print_errors_fp(stderr); goto end; } if (BIO_do_handshake(sbio) <= 0) { fprintf(stderr, "Error establishing SSL connection\n"); ERR_print_errors_fp(stderr); goto end; } /* Could examine ssl here to get connection info */ BIO_puts(sbio, "GET / HTTP/1.0\n\n"); for (;;) { len = BIO_read(sbio, tmpbuf, 1024); if (len <= 0) break; BIO_write(out, tmpbuf, len); } end: SSL_CONF_CTX_free(cctx); BIO_free_all(sbio); BIO_free(out); NCONF_free(conf); return 0; }
int main(int argc, char **argv) { BIO *sbio = NULL, *out = NULL; int len; char tmpbuf[1024]; SSL_CTX *ctx; SSL_CONF_CTX *cctx; SSL *ssl; char **args = argv + 1; const char *connect_str = "localhost:4433"; int nargs = argc - 1; ERR_load_crypto_strings(); ERR_load_SSL_strings(); SSL_library_init(); ctx = SSL_CTX_new(TLS_client_method()); cctx = SSL_CONF_CTX_new(); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); while (*args && **args == '-') { int rv; /* Parse standard arguments */ rv = SSL_CONF_cmd_argv(cctx, &nargs, &args); if (rv == -3) { fprintf(stderr, "Missing argument for %s\n", *args); goto end; } if (rv < 0) { fprintf(stderr, "Error in command %s\n", *args); ERR_print_errors_fp(stderr); goto end; } /* If rv > 0 we processed something so proceed to next arg */ if (rv > 0) continue; /* Otherwise application specific argument processing */ if (strcmp(*args, "-connect") == 0) { connect_str = args[1]; if (connect_str == NULL) { fprintf(stderr, "Missing -connect argument\n"); goto end; } args += 2; nargs -= 2; continue; } else { fprintf(stderr, "Unknown argument %s\n", *args); goto end; } } if (!SSL_CONF_CTX_finish(cctx)) { fprintf(stderr, "Finish error\n"); ERR_print_errors_fp(stderr); goto end; } /* * We'd normally set some stuff like the verify paths and * mode here * because as things stand this will connect to * any server whose * certificate is signed by any CA. */ sbio = BIO_new_ssl_connect(ctx); BIO_get_ssl(sbio, &ssl); if (!ssl) { fprintf(stderr, "Can't locate SSL pointer\n"); goto end; } /* Don't want any retries */ SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); /* We might want to do other things with ssl here */ BIO_set_conn_hostname(sbio, connect_str); out = BIO_new_fp(stdout, BIO_NOCLOSE); if (BIO_do_connect(sbio) <= 0) { fprintf(stderr, "Error connecting to server\n"); ERR_print_errors_fp(stderr); goto end; } if (BIO_do_handshake(sbio) <= 0) { fprintf(stderr, "Error establishing SSL connection\n"); ERR_print_errors_fp(stderr); goto end; } /* Could examine ssl here to get connection info */ BIO_puts(sbio, "GET / HTTP/1.0\n\n"); for (;;) { len = BIO_read(sbio, tmpbuf, 1024); if (len <= 0) break; BIO_write(out, tmpbuf, len); } end: SSL_CONF_CTX_free(cctx); BIO_free_all(sbio); BIO_free(out); return 0; }