Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/demos/bio/client-arg.c
34868 views
1
/*
2
* Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved.
3
*
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
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
int ret = EXIT_FAILURE;
26
27
ctx = SSL_CTX_new(TLS_client_method());
28
cctx = SSL_CONF_CTX_new();
29
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
30
SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
31
while (*args && **args == '-') {
32
int rv;
33
/* Parse standard arguments */
34
rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
35
if (rv == -3) {
36
fprintf(stderr, "Missing argument for %s\n", *args);
37
goto end;
38
}
39
if (rv < 0) {
40
fprintf(stderr, "Error in command %s\n", *args);
41
ERR_print_errors_fp(stderr);
42
goto end;
43
}
44
/* If rv > 0 we processed something so proceed to next arg */
45
if (rv > 0)
46
continue;
47
/* Otherwise application specific argument processing */
48
if (strcmp(*args, "-connect") == 0) {
49
connect_str = args[1];
50
if (connect_str == NULL) {
51
fprintf(stderr, "Missing -connect argument\n");
52
goto end;
53
}
54
args += 2;
55
nargs -= 2;
56
continue;
57
} else {
58
fprintf(stderr, "Unknown argument %s\n", *args);
59
goto end;
60
}
61
}
62
63
if (!SSL_CONF_CTX_finish(cctx)) {
64
fprintf(stderr, "Finish error\n");
65
ERR_print_errors_fp(stderr);
66
goto end;
67
}
68
69
/*
70
* We'd normally set some stuff like the verify paths and * mode here
71
* because as things stand this will connect to * any server whose
72
* certificate is signed by any CA.
73
*/
74
75
sbio = BIO_new_ssl_connect(ctx);
76
77
BIO_get_ssl(sbio, &ssl);
78
79
if (!ssl) {
80
fprintf(stderr, "Can't locate SSL pointer\n");
81
goto end;
82
}
83
84
/* We might want to do other things with ssl here */
85
86
BIO_set_conn_hostname(sbio, connect_str);
87
88
out = BIO_new_fp(stdout, BIO_NOCLOSE);
89
if (BIO_do_connect(sbio) <= 0) {
90
fprintf(stderr, "Error connecting to server\n");
91
ERR_print_errors_fp(stderr);
92
goto end;
93
}
94
95
/* Could examine ssl here to get connection info */
96
97
BIO_puts(sbio, "GET / HTTP/1.0\n\n");
98
for (;;) {
99
len = BIO_read(sbio, tmpbuf, 1024);
100
if (len <= 0)
101
break;
102
BIO_write(out, tmpbuf, len);
103
}
104
ret = EXIT_SUCCESS;
105
end:
106
SSL_CONF_CTX_free(cctx);
107
BIO_free_all(sbio);
108
BIO_free(out);
109
return ret;
110
}
111
112