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