xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/demos/bio/client-conf.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 #include <openssl/conf.h>
14c9496f6bSchristos 
main(int argc,char ** argv)15c9496f6bSchristos int main(int argc, char **argv)
16c9496f6bSchristos {
17c9496f6bSchristos     BIO *sbio = NULL, *out = NULL;
18c9496f6bSchristos     int i, len, rv;
19c9496f6bSchristos     char tmpbuf[1024];
20c9496f6bSchristos     SSL_CTX *ctx = NULL;
21c9496f6bSchristos     SSL_CONF_CTX *cctx = NULL;
22c9496f6bSchristos     SSL *ssl = NULL;
23c9496f6bSchristos     CONF *conf = NULL;
24c9496f6bSchristos     STACK_OF(CONF_VALUE) *sect = NULL;
25c9496f6bSchristos     CONF_VALUE *cnf;
26c9496f6bSchristos     const char *connect_str = "localhost:4433";
27c9496f6bSchristos     long errline = -1;
28c9496f6bSchristos 
29c9496f6bSchristos     conf = NCONF_new(NULL);
30c9496f6bSchristos 
31c9496f6bSchristos     if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {
32c9496f6bSchristos         if (errline <= 0)
33c9496f6bSchristos             fprintf(stderr, "Error processing config file\n");
34c9496f6bSchristos         else
35c9496f6bSchristos             fprintf(stderr, "Error on line %ld\n", errline);
36c9496f6bSchristos         goto end;
37c9496f6bSchristos     }
38c9496f6bSchristos 
39c9496f6bSchristos     sect = NCONF_get_section(conf, "default");
40c9496f6bSchristos 
41c9496f6bSchristos     if (sect == NULL) {
42c9496f6bSchristos         fprintf(stderr, "Error retrieving default section\n");
43c9496f6bSchristos         goto end;
44c9496f6bSchristos     }
45c9496f6bSchristos 
46*4724848cSchristos     ctx = SSL_CTX_new(TLS_client_method());
47c9496f6bSchristos     cctx = SSL_CONF_CTX_new();
48c9496f6bSchristos     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
49c9496f6bSchristos     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
50c9496f6bSchristos     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
51c9496f6bSchristos     for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
52c9496f6bSchristos         cnf = sk_CONF_VALUE_value(sect, i);
53c9496f6bSchristos         rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
54c9496f6bSchristos         if (rv > 0)
55c9496f6bSchristos             continue;
56c9496f6bSchristos         if (rv != -2) {
57c9496f6bSchristos             fprintf(stderr, "Error processing %s = %s\n",
58c9496f6bSchristos                     cnf->name, cnf->value);
59c9496f6bSchristos             ERR_print_errors_fp(stderr);
60c9496f6bSchristos             goto end;
61c9496f6bSchristos         }
62*4724848cSchristos         if (strcmp(cnf->name, "Connect") == 0) {
63c9496f6bSchristos             connect_str = cnf->value;
64c9496f6bSchristos         } else {
65c9496f6bSchristos             fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
66c9496f6bSchristos             goto end;
67c9496f6bSchristos         }
68c9496f6bSchristos     }
69c9496f6bSchristos 
70c9496f6bSchristos     if (!SSL_CONF_CTX_finish(cctx)) {
71c9496f6bSchristos         fprintf(stderr, "Finish error\n");
72c9496f6bSchristos         ERR_print_errors_fp(stderr);
73*4724848cSchristos         goto end;
74c9496f6bSchristos     }
75c9496f6bSchristos 
76c9496f6bSchristos     /*
77c9496f6bSchristos      * We'd normally set some stuff like the verify paths and * mode here
78c9496f6bSchristos      * because as things stand this will connect to * any server whose
79c9496f6bSchristos      * certificate is signed by any CA.
80c9496f6bSchristos      */
81c9496f6bSchristos 
82c9496f6bSchristos     sbio = BIO_new_ssl_connect(ctx);
83c9496f6bSchristos 
84c9496f6bSchristos     BIO_get_ssl(sbio, &ssl);
85c9496f6bSchristos 
86c9496f6bSchristos     if (!ssl) {
87c9496f6bSchristos         fprintf(stderr, "Can't locate SSL pointer\n");
88c9496f6bSchristos         goto end;
89c9496f6bSchristos     }
90c9496f6bSchristos 
91c9496f6bSchristos     /* Don't want any retries */
92c9496f6bSchristos     SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
93c9496f6bSchristos 
94c9496f6bSchristos     /* We might want to do other things with ssl here */
95c9496f6bSchristos 
96c9496f6bSchristos     BIO_set_conn_hostname(sbio, connect_str);
97c9496f6bSchristos 
98c9496f6bSchristos     out = BIO_new_fp(stdout, BIO_NOCLOSE);
99c9496f6bSchristos     if (BIO_do_connect(sbio) <= 0) {
100c9496f6bSchristos         fprintf(stderr, "Error connecting to server\n");
101c9496f6bSchristos         ERR_print_errors_fp(stderr);
102c9496f6bSchristos         goto end;
103c9496f6bSchristos     }
104c9496f6bSchristos 
105c9496f6bSchristos     /* Could examine ssl here to get connection info */
106c9496f6bSchristos 
107c9496f6bSchristos     BIO_puts(sbio, "GET / HTTP/1.0\n\n");
108c9496f6bSchristos     for (;;) {
109c9496f6bSchristos         len = BIO_read(sbio, tmpbuf, 1024);
110c9496f6bSchristos         if (len <= 0)
111c9496f6bSchristos             break;
112c9496f6bSchristos         BIO_write(out, tmpbuf, len);
113c9496f6bSchristos     }
114c9496f6bSchristos  end:
115c9496f6bSchristos     SSL_CONF_CTX_free(cctx);
116c9496f6bSchristos     BIO_free_all(sbio);
117c9496f6bSchristos     BIO_free(out);
118c9496f6bSchristos     NCONF_free(conf);
119c9496f6bSchristos     return 0;
120c9496f6bSchristos }
121