xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/demos/bio/client-arg.c (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos /*
2*4724848cSchristos  * Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos  *
4*4724848cSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*4724848cSchristos  * this file except in compliance with the License.  You can obtain a copy
6*4724848cSchristos  * in the file LICENSE in the source distribution or at
7*4724848cSchristos  * https://www.openssl.org/source/license.html
8*4724848cSchristos  */
9*4724848cSchristos 
10*4724848cSchristos #include <string.h>
11c9496f6bSchristos #include <openssl/err.h>
12c9496f6bSchristos #include <openssl/ssl.h>
13c9496f6bSchristos 
main(int argc,char ** argv)14c9496f6bSchristos int main(int argc, char **argv)
15c9496f6bSchristos {
16c9496f6bSchristos     BIO *sbio = NULL, *out = NULL;
17c9496f6bSchristos     int len;
18c9496f6bSchristos     char tmpbuf[1024];
19c9496f6bSchristos     SSL_CTX *ctx;
20c9496f6bSchristos     SSL_CONF_CTX *cctx;
21c9496f6bSchristos     SSL *ssl;
22c9496f6bSchristos     char **args = argv + 1;
23c9496f6bSchristos     const char *connect_str = "localhost:4433";
24c9496f6bSchristos     int nargs = argc - 1;
25c9496f6bSchristos 
26*4724848cSchristos     ctx = SSL_CTX_new(TLS_client_method());
27c9496f6bSchristos     cctx = SSL_CONF_CTX_new();
28c9496f6bSchristos     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
29c9496f6bSchristos     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
30c9496f6bSchristos     while (*args && **args == '-') {
31c9496f6bSchristos         int rv;
32c9496f6bSchristos         /* Parse standard arguments */
33c9496f6bSchristos         rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
34c9496f6bSchristos         if (rv == -3) {
35c9496f6bSchristos             fprintf(stderr, "Missing argument for %s\n", *args);
36c9496f6bSchristos             goto end;
37c9496f6bSchristos         }
38c9496f6bSchristos         if (rv < 0) {
39c9496f6bSchristos             fprintf(stderr, "Error in command %s\n", *args);
40c9496f6bSchristos             ERR_print_errors_fp(stderr);
41c9496f6bSchristos             goto end;
42c9496f6bSchristos         }
43c9496f6bSchristos         /* If rv > 0 we processed something so proceed to next arg */
44c9496f6bSchristos         if (rv > 0)
45c9496f6bSchristos             continue;
46c9496f6bSchristos         /* Otherwise application specific argument processing */
47*4724848cSchristos         if (strcmp(*args, "-connect") == 0) {
48c9496f6bSchristos             connect_str = args[1];
49c9496f6bSchristos             if (connect_str == NULL) {
50c9496f6bSchristos                 fprintf(stderr, "Missing -connect argument\n");
51c9496f6bSchristos                 goto end;
52c9496f6bSchristos             }
53c9496f6bSchristos             args += 2;
54c9496f6bSchristos             nargs -= 2;
55c9496f6bSchristos             continue;
56c9496f6bSchristos         } else {
57c9496f6bSchristos             fprintf(stderr, "Unknown argument %s\n", *args);
58c9496f6bSchristos             goto end;
59c9496f6bSchristos         }
60c9496f6bSchristos     }
61c9496f6bSchristos 
62c9496f6bSchristos     if (!SSL_CONF_CTX_finish(cctx)) {
63c9496f6bSchristos         fprintf(stderr, "Finish error\n");
64c9496f6bSchristos         ERR_print_errors_fp(stderr);
65*4724848cSchristos         goto end;
66c9496f6bSchristos     }
67c9496f6bSchristos 
68c9496f6bSchristos     /*
69c9496f6bSchristos      * We'd normally set some stuff like the verify paths and * mode here
70c9496f6bSchristos      * because as things stand this will connect to * any server whose
71c9496f6bSchristos      * certificate is signed by any CA.
72c9496f6bSchristos      */
73c9496f6bSchristos 
74c9496f6bSchristos     sbio = BIO_new_ssl_connect(ctx);
75c9496f6bSchristos 
76c9496f6bSchristos     BIO_get_ssl(sbio, &ssl);
77c9496f6bSchristos 
78c9496f6bSchristos     if (!ssl) {
79c9496f6bSchristos         fprintf(stderr, "Can't locate SSL pointer\n");
80c9496f6bSchristos         goto end;
81c9496f6bSchristos     }
82c9496f6bSchristos 
83c9496f6bSchristos     /* Don't want any retries */
84c9496f6bSchristos     SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
85c9496f6bSchristos 
86c9496f6bSchristos     /* We might want to do other things with ssl here */
87c9496f6bSchristos 
88c9496f6bSchristos     BIO_set_conn_hostname(sbio, connect_str);
89c9496f6bSchristos 
90c9496f6bSchristos     out = BIO_new_fp(stdout, BIO_NOCLOSE);
91c9496f6bSchristos     if (BIO_do_connect(sbio) <= 0) {
92c9496f6bSchristos         fprintf(stderr, "Error connecting to server\n");
93c9496f6bSchristos         ERR_print_errors_fp(stderr);
94c9496f6bSchristos         goto end;
95c9496f6bSchristos     }
96c9496f6bSchristos 
97c9496f6bSchristos     /* Could examine ssl here to get connection info */
98c9496f6bSchristos 
99c9496f6bSchristos     BIO_puts(sbio, "GET / HTTP/1.0\n\n");
100c9496f6bSchristos     for (;;) {
101c9496f6bSchristos         len = BIO_read(sbio, tmpbuf, 1024);
102c9496f6bSchristos         if (len <= 0)
103c9496f6bSchristos             break;
104c9496f6bSchristos         BIO_write(out, tmpbuf, len);
105c9496f6bSchristos     }
106c9496f6bSchristos  end:
107c9496f6bSchristos     SSL_CONF_CTX_free(cctx);
108c9496f6bSchristos     BIO_free_all(sbio);
109c9496f6bSchristos     BIO_free(out);
110c9496f6bSchristos     return 0;
111c9496f6bSchristos }
112