]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/client-arg.c
2 * Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/err.h>
12 #include <openssl/ssl.h>
14 int main(int argc
, char **argv
)
16 BIO
*sbio
= NULL
, *out
= NULL
;
22 char **args
= argv
+ 1;
23 const char *connect_str
= "localhost:4433";
26 ctx
= SSL_CTX_new(TLS_client_method());
27 cctx
= SSL_CONF_CTX_new();
28 SSL_CONF_CTX_set_flags(cctx
, SSL_CONF_FLAG_CLIENT
);
29 SSL_CONF_CTX_set_ssl_ctx(cctx
, ctx
);
30 while (*args
&& **args
== '-') {
32 /* Parse standard arguments */
33 rv
= SSL_CONF_cmd_argv(cctx
, &nargs
, &args
);
35 fprintf(stderr
, "Missing argument for %s\n", *args
);
39 fprintf(stderr
, "Error in command %s\n", *args
);
40 ERR_print_errors_fp(stderr
);
43 /* If rv > 0 we processed something so proceed to next arg */
46 /* Otherwise application specific argument processing */
47 if (strcmp(*args
, "-connect") == 0) {
48 connect_str
= args
[1];
49 if (connect_str
== NULL
) {
50 fprintf(stderr
, "Missing -connect argument\n");
57 fprintf(stderr
, "Unknown argument %s\n", *args
);
62 if (!SSL_CONF_CTX_finish(cctx
)) {
63 fprintf(stderr
, "Finish error\n");
64 ERR_print_errors_fp(stderr
);
69 * We'd normally set some stuff like the verify paths and * mode here
70 * because as things stand this will connect to * any server whose
71 * certificate is signed by any CA.
74 sbio
= BIO_new_ssl_connect(ctx
);
76 BIO_get_ssl(sbio
, &ssl
);
79 fprintf(stderr
, "Can't locate SSL pointer\n");
83 /* We might want to do other things with ssl here */
85 BIO_set_conn_hostname(sbio
, connect_str
);
87 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
88 if (BIO_do_connect(sbio
) <= 0) {
89 fprintf(stderr
, "Error connecting to server\n");
90 ERR_print_errors_fp(stderr
);
94 /* Could examine ssl here to get connection info */
96 BIO_puts(sbio
, "GET / HTTP/1.0\n\n");
98 len
= BIO_read(sbio
, tmpbuf
, 1024);
101 BIO_write(out
, tmpbuf
, len
);
104 SSL_CONF_CTX_free(cctx
);