Tizen 2.0 Release
[external/libgnutls26.git] / tests / mini-eagain.c
1 /*
2  * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson, Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <gnutls/gnutls.h>
32 #include <gnutls/crypto.h>
33
34 #include "utils.h"
35 #define RANDOMIZE
36 #include "eagain-common.h"
37
38 static void
39 tls_log_func (int level, const char *str)
40 {
41   fprintf (stderr, "|<%d>| %s", level, str);
42 }
43
44 static int handshake = 0;
45
46 #define MAX_BUF 1024
47 #define MSG "Hello TLS, and hi and how are you and more data here... and more... and even more and even more more data..."
48
49 void
50 doit (void)
51 {
52   /* Server stuff. */
53   gnutls_anon_server_credentials_t s_anoncred;
54   const gnutls_datum_t p3 = { (char *) pkcs3, strlen (pkcs3) };
55   static gnutls_dh_params_t dh_params;
56   gnutls_session_t server;
57   int sret, cret;
58   /* Client stuff. */
59   gnutls_anon_client_credentials_t c_anoncred;
60   gnutls_session_t client;
61   /* Need to enable anonymous KX specifically. */
62   char buffer[MAX_BUF + 1];
63   ssize_t ns;
64   int ret, transferred = 0, msglen;
65
66   /* General init. */
67   gnutls_global_init ();
68   gnutls_global_set_log_function (tls_log_func);
69   if (debug)
70     gnutls_global_set_log_level (2);
71
72   /* Init server */
73   gnutls_anon_allocate_server_credentials (&s_anoncred);
74   gnutls_dh_params_init (&dh_params);
75   gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
76   gnutls_anon_set_server_dh_params (s_anoncred, dh_params);
77   gnutls_init (&server, GNUTLS_SERVER);
78   ret = gnutls_priority_set_direct (server, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
79   if (ret < 0)
80     exit(1);
81   gnutls_credentials_set (server, GNUTLS_CRD_ANON, s_anoncred);
82   gnutls_dh_set_prime_bits (server, 1024);
83   gnutls_transport_set_push_function (server, server_push);
84   gnutls_transport_set_pull_function (server, server_pull);
85   gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server);
86
87   /* Init client */
88   gnutls_anon_allocate_client_credentials (&c_anoncred);
89   gnutls_init (&client, GNUTLS_CLIENT);
90   ret = gnutls_priority_set_direct (client, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
91   if (ret < 0)
92     exit(1);
93   gnutls_credentials_set (client, GNUTLS_CRD_ANON, c_anoncred);
94   gnutls_transport_set_push_function (client, client_push);
95   gnutls_transport_set_pull_function (client, client_pull);
96   gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client);
97
98   handshake = 1;
99   HANDSHAKE(client, server);
100
101   handshake = 0;
102   if (debug)
103     success ("Handshake established\n");
104
105   msglen = strlen(MSG);
106   TRANSFER(client, server, MSG, msglen, buffer, MAX_BUF);
107   if (debug)
108     fputs ("\n", stdout);
109
110   gnutls_bye (client, GNUTLS_SHUT_WR);
111   gnutls_bye (server, GNUTLS_SHUT_WR);
112
113   gnutls_deinit (client);
114   gnutls_deinit (server);
115
116   gnutls_anon_free_client_credentials (c_anoncred);
117   gnutls_anon_free_server_credentials (s_anoncred);
118
119   gnutls_dh_params_deinit (dh_params);
120
121   gnutls_global_deinit ();
122 }