]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/sconnect.c
2 * Copyright 1998-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 * A minimal program to do SSL to a passed host and port.
12 * It is actually using non-blocking IO but in a very simple manner
13 * sconnect host:port - it does a 'GET / HTTP/1.0'
15 * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
22 #include <openssl/err.h>
23 #include <openssl/ssl.h>
25 #define HOSTPORT "localhost:4433"
26 #define CAFILE "root.pem"
28 int main(int argc
, char *argv
[])
30 const char *hostport
= HOSTPORT
;
31 const char *CAfile
= CAFILE
;
35 char buf
[1024 * 10], *p
;
36 SSL_CTX
*ssl_ctx
= NULL
;
39 int i
, len
, off
, ret
= EXIT_FAILURE
;
46 hostname
= OPENSSL_strdup(hostport
);
47 if ((cp
= strchr(hostname
, ':')) != NULL
)
55 ssl_ctx
= SSL_CTX_new(TLS_client_method());
57 /* Enable trust chain verification */
58 SSL_CTX_set_verify(ssl_ctx
, SSL_VERIFY_PEER
, NULL
);
59 SSL_CTX_load_verify_locations(ssl_ctx
, CAfile
, NULL
);
61 /* Lets make a SSL structure */
62 ssl
= SSL_new(ssl_ctx
);
63 SSL_set_connect_state(ssl
);
65 /* Enable peername verification */
66 if (SSL_set1_host(ssl
, hostname
) <= 0)
69 /* Use it inside an SSL BIO */
70 ssl_bio
= BIO_new(BIO_f_ssl());
71 BIO_set_ssl(ssl_bio
, ssl
, BIO_CLOSE
);
73 /* Lets use a connect BIO under the SSL BIO */
74 out
= BIO_new(BIO_s_connect());
75 BIO_set_conn_hostname(out
, hostport
);
77 out
= BIO_push(ssl_bio
, out
);
79 p
= "GET / HTTP/1.0\r\n\r\n";
84 i
= BIO_write(out
, &(p
[off
]), len
);
86 if (BIO_should_retry(out
)) {
87 fprintf(stderr
, "write DELAY\n");
101 i
= BIO_read(out
, buf
, sizeof(buf
));
105 if (BIO_should_retry(out
)) {
106 fprintf(stderr
, "read DELAY\n");
112 fwrite(buf
, 1, i
, stdout
);
119 if (ERR_peek_error() == 0) { /* system call error */
120 fprintf(stderr
, "errno=%d ", errno
);
123 ERR_print_errors_fp(stderr
);
127 SSL_CTX_free(ssl_ctx
);