1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  *
4  * Author: 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/dtls.h>
33 #include <assert.h>
34 #include "utils.h"
35 
36 #define SERVER_PUSH_ADD if (len > 512 + 5+8+32) fail("max record set to 512, len: %d\n", (int)len);
37 #include "eagain-common.h"
38 
39 #include "cert-common.h"
40 
41 /* This tests whether the max-record extension is respected.
42  */
43 
44 const char *side;
45 
tls_log_func(int level,const char * str)46 static void tls_log_func(int level, const char *str)
47 {
48 	fprintf(stderr, "%s|<%d>| %s", side, level, str);
49 }
50 
51 static
run(const char * prio)52 void run(const char *prio)
53 {
54 	global_init();
55 
56 	int ret;
57 	char buf[1024];
58 	/* Server stuff. */
59 	gnutls_certificate_credentials_t serverx509cred;
60 	gnutls_session_t server;
61 	int sret = GNUTLS_E_AGAIN;
62 	/* Client stuff. */
63 	gnutls_certificate_credentials_t clientx509cred;
64 	gnutls_session_t client;
65 	int cret = GNUTLS_E_AGAIN;
66 
67 	/* General init. */
68 	gnutls_global_set_log_function(tls_log_func);
69 	if (debug)
70 		gnutls_global_set_log_level(6);
71 
72 	/* Init server */
73 	gnutls_certificate_allocate_credentials(&serverx509cred);
74 	gnutls_certificate_set_x509_key_mem(serverx509cred,
75 					    &server2_cert, &server2_key,
76 					    GNUTLS_X509_FMT_PEM);
77 
78 	gnutls_init(&server, GNUTLS_SERVER|GNUTLS_DATAGRAM | GNUTLS_NONBLOCK);
79 	gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE,
80 				serverx509cred);
81 
82 	assert(gnutls_priority_set_direct(server,
83 					  prio,
84 					  NULL)>=0);
85 	gnutls_transport_set_push_function(server, server_push);
86 	gnutls_transport_set_pull_function(server, server_pull);
87 	gnutls_transport_set_pull_timeout_function(server,
88 						   server_pull_timeout_func);
89 	gnutls_transport_set_ptr(server, server);
90 
91 	/* Init client */
92 
93 	ret = gnutls_certificate_allocate_credentials(&clientx509cred);
94 	if (ret < 0)
95 		exit(1);
96 
97 	ret = gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca2_cert, GNUTLS_X509_FMT_PEM);
98 	if (ret < 0)
99 		exit(1);
100 
101 	ret = gnutls_init(&client, GNUTLS_CLIENT|GNUTLS_DATAGRAM | GNUTLS_NONBLOCK);
102 	if (ret < 0)
103 		exit(1);
104 
105 	ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE,
106 				clientx509cred);
107 	if (ret < 0)
108 		exit(1);
109 
110 	ret = gnutls_priority_set_direct(client, prio, NULL);
111 	if (ret < 0)
112 		exit(1);
113 
114 	gnutls_record_set_max_size(client, 512);
115 	gnutls_transport_set_push_function(client, client_push);
116 	gnutls_transport_set_pull_function(client, client_pull);
117 	gnutls_transport_set_pull_timeout_function(client,
118 						   client_pull_timeout_func);
119 	gnutls_transport_set_ptr(client, client);
120 
121 	HANDSHAKE_DTLS(client, server);
122 
123 	memset(buf, 1, sizeof(buf));
124 	ret = gnutls_record_send(server, buf, 513);
125 	if (ret != GNUTLS_E_LARGE_PACKET && ret < 0) {
126 		gnutls_perror(ret);
127 		exit(1);
128 	}
129 	success("did not send a 513-byte packet\n");
130 
131 	ret = gnutls_record_send(server, buf, 512);
132 	if (ret < 0) {
133 		gnutls_perror(ret);
134 		exit(1);
135 	}
136 	success("did send a 512-byte packet\n");
137 
138 	gnutls_bye(client, GNUTLS_SHUT_RDWR);
139 	gnutls_bye(server, GNUTLS_SHUT_RDWR);
140 
141 	gnutls_deinit(client);
142 	gnutls_deinit(server);
143 
144 	gnutls_certificate_free_credentials(serverx509cred);
145 	gnutls_certificate_free_credentials(clientx509cred);
146 
147 	gnutls_global_deinit();
148 }
149 
doit(void)150 void doit(void)
151 {
152 	run("NORMAL:-VERS-ALL:+VERS-DTLS1.2");
153 }
154