]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/sconnect.c
2 * A minimal program to do SSL to a passed host and port.
3 * It is actually using non-blocking IO but in a very simple manner
4 * sconnect host:port - it does a 'GET / HTTP/1.0'
6 * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
12 #include <openssl/err.h>
13 #include <openssl/ssl.h>
15 #define HOSTPORT "localhost:4433"
16 #define CAFILE "root.pem"
24 const char *hostport
= HOSTPORT
;
25 const char *CAfile
= CAFILE
;
29 char buf
[1024 * 10], *p
;
30 SSL_CTX
*ssl_ctx
= NULL
;
33 int i
, len
, off
, ret
= 1;
40 hostname
= OPENSSL_strdup(hostport
);
41 if ((cp
= strchr(hostname
, ':')) != NULL
)
49 /* Lets get nice error messages */
50 SSL_load_error_strings();
52 /* Setup all the global SSL stuff */
53 OpenSSL_add_ssl_algorithms();
54 ssl_ctx
= SSL_CTX_new(TLS_client_method());
56 /* Enable trust chain verification */
57 SSL_CTX_set_verify(ssl_ctx
, SSL_VERIFY_PEER
, NULL
);
58 SSL_CTX_load_verify_locations(ssl_ctx
, CAfile
, NULL
);
60 /* Lets make a SSL structure */
61 ssl
= SSL_new(ssl_ctx
);
62 SSL_set_connect_state(ssl
);
64 /* Enable peername verification */
65 if (SSL_set1_host(ssl
, hostname
) <= 0)
68 /* Use it inside an SSL BIO */
69 ssl_bio
= BIO_new(BIO_f_ssl());
70 BIO_set_ssl(ssl_bio
, ssl
, BIO_CLOSE
);
72 /* Lets use a connect BIO under the SSL BIO */
73 out
= BIO_new(BIO_s_connect());
74 BIO_set_conn_hostname(out
, hostport
);
76 out
= BIO_push(ssl_bio
, out
);
78 p
= "GET / HTTP/1.0\r\n\r\n";
83 i
= BIO_write(out
, &(p
[off
]), len
);
85 if (BIO_should_retry(out
)) {
86 fprintf(stderr
, "write DELAY\n");
100 i
= BIO_read(out
, buf
, sizeof(buf
));
104 if (BIO_should_retry(out
)) {
105 fprintf(stderr
, "read DELAY\n");
111 fwrite(buf
, 1, i
, stdout
);
118 if (ERR_peek_error() == 0) { /* system call error */
119 fprintf(stderr
, "errno=%d ", errno
);
122 ERR_print_errors_fp(stderr
);
125 SSL_CTX_free(ssl_ctx
);