Path: blob/main/crypto/openssl/demos/bio/client-arg.c
34868 views
/*1* Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <string.h>10#include <openssl/err.h>11#include <openssl/ssl.h>1213int main(int argc, char **argv)14{15BIO *sbio = NULL, *out = NULL;16int len;17char tmpbuf[1024];18SSL_CTX *ctx;19SSL_CONF_CTX *cctx;20SSL *ssl;21char **args = argv + 1;22const char *connect_str = "localhost:4433";23int nargs = argc - 1;24int ret = EXIT_FAILURE;2526ctx = SSL_CTX_new(TLS_client_method());27cctx = SSL_CONF_CTX_new();28SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);29SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);30while (*args && **args == '-') {31int rv;32/* Parse standard arguments */33rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);34if (rv == -3) {35fprintf(stderr, "Missing argument for %s\n", *args);36goto end;37}38if (rv < 0) {39fprintf(stderr, "Error in command %s\n", *args);40ERR_print_errors_fp(stderr);41goto end;42}43/* If rv > 0 we processed something so proceed to next arg */44if (rv > 0)45continue;46/* Otherwise application specific argument processing */47if (strcmp(*args, "-connect") == 0) {48connect_str = args[1];49if (connect_str == NULL) {50fprintf(stderr, "Missing -connect argument\n");51goto end;52}53args += 2;54nargs -= 2;55continue;56} else {57fprintf(stderr, "Unknown argument %s\n", *args);58goto end;59}60}6162if (!SSL_CONF_CTX_finish(cctx)) {63fprintf(stderr, "Finish error\n");64ERR_print_errors_fp(stderr);65goto end;66}6768/*69* We'd normally set some stuff like the verify paths and * mode here70* because as things stand this will connect to * any server whose71* certificate is signed by any CA.72*/7374sbio = BIO_new_ssl_connect(ctx);7576BIO_get_ssl(sbio, &ssl);7778if (!ssl) {79fprintf(stderr, "Can't locate SSL pointer\n");80goto end;81}8283/* We might want to do other things with ssl here */8485BIO_set_conn_hostname(sbio, connect_str);8687out = BIO_new_fp(stdout, BIO_NOCLOSE);88if (BIO_do_connect(sbio) <= 0) {89fprintf(stderr, "Error connecting to server\n");90ERR_print_errors_fp(stderr);91goto end;92}9394/* Could examine ssl here to get connection info */9596BIO_puts(sbio, "GET / HTTP/1.0\n\n");97for (;;) {98len = BIO_read(sbio, tmpbuf, 1024);99if (len <= 0)100break;101BIO_write(out, tmpbuf, len);102}103ret = EXIT_SUCCESS;104end:105SSL_CONF_CTX_free(cctx);106BIO_free_all(sbio);107BIO_free(out);108return ret;109}110111112