1	#include "common.h"
2	 
3	#define CERTFILE "client.pem"
4	SSL_CTX *setup_client_ctx(void)
5	{
6	    SSL_CTX *ctx;
7
8	    ctx = SSL_CTX_new(SSLv23_method());
9	    if (SSL_CTX_use_certificate_chain_file(ctx, CERTFILE) != 1)
10	        int_error("Error loading certificate from file");
11	    if (SSL_CTX_use_PrivateKey_file(ctx, CERTFILE, SSL_FILETYPE_PEM) != 1)
12	        int_error("Error loading private key from file");  
13	    return ctx;
14	}
15
16	int do_client_loop(SSL *ssl)
17	{
18	    int  err, nwritten;
19	    char buf[80];
20	 
21	    for (;;)
22	    {
23	        if (!fgets(buf, sizeof(buf), stdin))
24	            break;
25	        for (nwritten = 0;  nwritten < sizeof(buf);  nwritten += err)
26	        {
27	            err = SSL_write(ssl, buf + nwritten, strlen(buf) - nwritten);
28	            if (err <= 0)
29	                return 0;
30	        }
31	    }
32	    return 1;
33	}
34	 
35	int main(int argc, char *argv[])
36	{
37	    BIO     *conn;
38	    SSL     *ssl;
39	    SSL_CTX *ctx;
40
41	    init_OpenSSL();
42	    seed_prng();
43
44	    ctx = setup_client_ctx();
45
46	    conn = BIO_new_connect(SERVER ":" PORT);
47	    if (!conn)
48	        int_error("Error creating connection BIO");
49	 
50	    if (BIO_do_connect(conn) <= 0)
51	        int_error("Error connecting to remote machine");
52	 
53	    if (!(ssl = SSL_new(ctx)))
54	        int_error("Error creating an SSL context");
55	    SSL_set_bio(ssl, conn, conn);
56	    if (SSL_connect(ssl) <= 0)
57	        int_error("Error connecting SSL object");
58
59	    fprintf(stderr, "SSL Connection opened\n");
60	    if (do_client_loop(ssl))
61	        SSL_shutdown(ssl);
62	    else
63	        SSL_clear(ssl);
64	    fprintf(stderr, "SSL Connection closed\n");
65
66	    SSL_free(ssl);
67	    SSL_CTX_free(ctx);
68	    return 0;
69	}
