Tizen 2.0 Release
[external/libgnutls26.git] / tests / mini.c
1 /*
2  * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
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 "eagain-common.h"
33
34 #include "utils.h"
35
36 static void
37 tls_log_func (int level, const char *str)
38 {
39   fprintf (stderr, "|<%d>| %s", level, str);
40 }
41
42 #define MAX_BUF 1024
43 #define MSG "Hello TLS"
44
45 void
46 doit (void)
47 {
48   /* Server stuff. */
49   gnutls_anon_server_credentials_t s_anoncred;
50   const gnutls_datum_t p3 = { (char *) pkcs3, strlen (pkcs3) };
51   static gnutls_dh_params_t dh_params;
52   gnutls_session_t server;
53   int sret = GNUTLS_E_AGAIN;
54   /* Client stuff. */
55   gnutls_anon_client_credentials_t c_anoncred;
56   gnutls_session_t client;
57   int cret = GNUTLS_E_AGAIN;
58   /* Need to enable anonymous KX specifically. */
59   char buffer[MAX_BUF + 1];
60   ssize_t ns;
61   int ret, transferred = 0, msglen;
62
63   /* General init. */
64   gnutls_global_init ();
65   gnutls_global_set_log_function (tls_log_func);
66   if (debug)
67     gnutls_global_set_log_level (4711);
68
69   /* Init server */
70   gnutls_anon_allocate_server_credentials (&s_anoncred);
71   gnutls_dh_params_init (&dh_params);
72   gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
73   gnutls_anon_set_server_dh_params (s_anoncred, dh_params);
74   gnutls_init (&server, GNUTLS_SERVER);
75   gnutls_priority_set_direct (server, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
76   gnutls_credentials_set (server, GNUTLS_CRD_ANON, s_anoncred);
77   gnutls_dh_set_prime_bits (server, 1024);
78   gnutls_transport_set_push_function (server, server_push);
79   gnutls_transport_set_pull_function (server, server_pull);
80   gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server);
81
82   /* Init client */
83   gnutls_anon_allocate_client_credentials (&c_anoncred);
84   gnutls_init (&client, GNUTLS_CLIENT);
85   gnutls_priority_set_direct (client, "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
86   gnutls_credentials_set (client, GNUTLS_CRD_ANON, c_anoncred);
87   gnutls_transport_set_push_function (client, client_push);
88   gnutls_transport_set_pull_function (client, client_pull);
89   gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client);
90
91   HANDSHAKE(client, server);
92
93   if (debug)
94     success ("Handshake established\n");
95
96   msglen = strlen(MSG);
97   TRANSFER(client, server, MSG, msglen, buffer, MAX_BUF);
98   if (debug)
99     fputs ("\n", stdout);
100
101   gnutls_bye (client, GNUTLS_SHUT_RDWR);
102   gnutls_bye (server, GNUTLS_SHUT_RDWR);
103
104   gnutls_deinit (client);
105   gnutls_deinit (server);
106
107   gnutls_anon_free_client_credentials (c_anoncred);
108   gnutls_anon_free_server_credentials (s_anoncred);
109
110   gnutls_dh_params_deinit (dh_params);
111
112   gnutls_global_deinit ();
113 }