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>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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"
54 "GET /hello/ HTTP/1.1\r\n" \
55 "Host: tropicssl.org\r\n\r\n"
59 static void my_debug(void *ctx
, int level
, const char *str
)
61 if (level
< DEBUG_LEVEL
) {
62 fprintf((FILE *) ctx
, "%s", str
);
69 int ret
, len
, server_fd
= -1;
70 unsigned char buf
[1024];
76 * 0. Initialize the RNG and the session data
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
);
88 if ((ret
= net_connect(&server_fd
, SERVER_NAME
, SERVER_PORT
)) != 0) {
89 printf(" failed\n ! net_connect returned %d\n\n", ret
);
98 printf(" . Setting up the SSL/TLS structure...");
101 if ((ret
= ssl_init(&ssl
)) != 0) {
102 printf(" failed\n ! ssl_init returned %d\n\n", ret
);
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:");
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
);
134 printf(" %d bytes written\n\n%s", len
, (char *)buf
);
137 * 7. Read the HTTP response
139 printf(" < Read from server:");
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
)
150 if (ret
== TROPICSSL_ERR_SSL_PEER_CLOSE_NOTIFY
)
154 printf("failed\n ! ssl_read returned %d\n\n", ret
);
159 printf(" %d bytes read\n\n%s", len
, (char *)buf
);
163 ssl_close_notify(&ssl
);
166 net_close(server_fd
);
169 memset(&ssl
, 0, sizeof(ssl
));
172 printf(" + Press Enter to exit this program.\n");