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