bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / programs / ssl / ssl_client1.c
blob3af7a59e07d4f97bb2e2dfff1120eae267934a34
1 /*
2 * SSL client demonstration program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
38 #endif
40 #include <string.h>
41 #include <stdio.h>
43 #include "tropicssl/net.h"
44 #include "tropicssl/ssl.h"
45 #include "tropicssl/havege.h"
47 #define SERVER_PORT 443
49 #define SERVER_NAME "localhost"
50 #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
52 #define SERVER_NAME "tropicssl.org"
53 #define GET_REQUEST \
54 "GET /hello/ HTTP/1.1\r\n" \
55 "Host: tropicssl.org\r\n\r\n"
57 #define DEBUG_LEVEL 0
59 static void my_debug(void *ctx, int level, const char *str)
61 if (level < DEBUG_LEVEL) {
62 fprintf((FILE *) ctx, "%s", str);
63 fflush((FILE *) ctx);
67 int main(void)
69 int ret, len, server_fd = -1;
70 unsigned char buf[1024];
71 havege_state hs;
72 ssl_context ssl;
73 ssl_session ssn;
76 * 0. Initialize the RNG and the session data
78 havege_init(&hs);
79 memset(&ssl, 0, sizeof(ssl));
80 memset(&ssn, 0, sizeof(ssl_session));
83 * 1. Start the connection
85 printf("\n . Connecting to tcp/%s/%4d...", SERVER_NAME, SERVER_PORT);
86 fflush(stdout);
88 if ((ret = net_connect(&server_fd, SERVER_NAME, SERVER_PORT)) != 0) {
89 printf(" failed\n ! net_connect returned %d\n\n", ret);
90 goto exit;
93 printf(" ok\n");
96 * 2. Setup stuff
98 printf(" . Setting up the SSL/TLS structure...");
99 fflush(stdout);
101 if ((ret = ssl_init(&ssl)) != 0) {
102 printf(" failed\n ! ssl_init returned %d\n\n", ret);
103 goto exit;
106 printf(" ok\n");
108 ssl_set_endpoint(&ssl, SSL_IS_CLIENT);
109 ssl_set_authmode(&ssl, SSL_VERIFY_NONE);
111 ssl_set_rng(&ssl, havege_rand, &hs);
112 ssl_set_dbg(&ssl, my_debug, stdout);
113 ssl_set_bio(&ssl, net_recv, &server_fd, net_send, &server_fd);
115 ssl_set_ciphers(&ssl, ssl_default_ciphers);
116 ssl_set_session(&ssl, 1, 600, &ssn);
119 * 3. Write the GET request
121 printf(" > Write to server:");
122 fflush(stdout);
124 len = sprintf((char *)buf, GET_REQUEST);
126 while ((ret = ssl_write(&ssl, buf, len)) <= 0) {
127 if (ret != TROPICSSL_ERR_NET_TRY_AGAIN) {
128 printf(" failed\n ! ssl_write returned %d\n\n", ret);
129 goto exit;
133 len = ret;
134 printf(" %d bytes written\n\n%s", len, (char *)buf);
137 * 7. Read the HTTP response
139 printf(" < Read from server:");
140 fflush(stdout);
142 do {
143 len = sizeof(buf) - 1;
144 memset(buf, 0, sizeof(buf));
145 ret = ssl_read(&ssl, buf, len);
147 if (ret == TROPICSSL_ERR_NET_TRY_AGAIN)
148 continue;
150 if (ret == TROPICSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
151 break;
153 if (ret <= 0) {
154 printf("failed\n ! ssl_read returned %d\n\n", ret);
155 break;
158 len = ret;
159 printf(" %d bytes read\n\n%s", len, (char *)buf);
161 while (0);
163 ssl_close_notify(&ssl);
165 exit:
166 net_close(server_fd);
167 ssl_free(&ssl);
169 memset(&ssl, 0, sizeof(ssl));
171 #ifdef WIN32
172 printf(" + Press Enter to exit this program.\n");
173 fflush(stdout);
174 getchar();
175 #endif
177 return (ret);