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