Tizen 2.0 Release
[external/libgnutls26.git] / tests / safe-renegotiation / srn2.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 /* Code based on ../mini-x509-rehandshake.c.
24  *
25  * Check that new APIs are behaving properly.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <gnutls/gnutls.h>
37 #include "../eagain-common.h"
38 #include "../utils.h"
39
40 static void
41 tls_log_func (int level, const char *str)
42 {
43   fprintf (stderr, "|<%d>| %s", level, str);
44 }
45
46 static unsigned char server_cert_pem[] =
47   "-----BEGIN CERTIFICATE-----\n"
48   "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
49   "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
50   "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
51   "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
52   "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
53   "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
54   "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
55   "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
56   "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
57   "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
58   "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
59   "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
60   "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
61
62 const gnutls_datum_t server_cert = { server_cert_pem,
63   sizeof (server_cert_pem)
64 };
65
66 static unsigned char server_key_pem[] =
67   "-----BEGIN RSA PRIVATE KEY-----\n"
68   "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
69   "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
70   "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
71   "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
72   "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
73   "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
74   "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
75   "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
76   "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
77   "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
78   "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
79   "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
80   "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
81   "-----END RSA PRIVATE KEY-----\n";
82
83 const gnutls_datum_t server_key = { server_key_pem,
84   sizeof (server_key_pem)
85 };
86
87 void doit(void)
88 {
89   /* Server stuff. */
90   gnutls_certificate_credentials_t serverx509cred;
91   gnutls_session_t server;
92   int sret = GNUTLS_E_AGAIN;
93   /* Client stuff. */
94   gnutls_certificate_credentials_t clientx509cred;
95   gnutls_session_t client;
96   int cret = GNUTLS_E_AGAIN;
97
98   /* General init. */
99   gnutls_global_init ();
100   gnutls_global_set_log_function (tls_log_func);
101   if (debug) gnutls_global_set_log_level (2);
102
103   /* Init server */
104   gnutls_certificate_allocate_credentials (&serverx509cred);
105   gnutls_certificate_set_x509_key_mem (serverx509cred,
106                                        &server_cert, &server_key,
107                                        GNUTLS_X509_FMT_PEM);
108   gnutls_init (&server, GNUTLS_SERVER);
109   gnutls_credentials_set (server, GNUTLS_CRD_CERTIFICATE, serverx509cred);
110   gnutls_priority_set_direct (server, "NORMAL", NULL);
111   gnutls_transport_set_push_function (server, server_push);
112   gnutls_transport_set_pull_function (server, server_pull);
113   gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server);
114
115   /* Init client */
116   gnutls_certificate_allocate_credentials (&clientx509cred);
117   gnutls_init (&client, GNUTLS_CLIENT);
118   gnutls_credentials_set (client, GNUTLS_CRD_CERTIFICATE, clientx509cred);
119   gnutls_priority_set_direct (client, "NORMAL", NULL);
120   gnutls_transport_set_push_function (client, client_push);
121   gnutls_transport_set_pull_function (client, client_pull);
122   gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client);
123
124   /* Check that initially no session use the extension. */
125   if (gnutls_safe_renegotiation_status (server)
126       || gnutls_safe_renegotiation_status (client))
127     {
128       puts ("Client or server using extension before handshake?");
129       abort ();
130     }
131
132   HANDSHAKE(client, server);
133
134   /* Check that both sessions use the extension. */
135   if (!gnutls_safe_renegotiation_status (server)
136       || !gnutls_safe_renegotiation_status (client))
137     {
138       puts ("Client or server not using safe renegotiation extension?");
139       abort ();
140     }
141
142   sret = gnutls_rehandshake (server);
143   if (debug)
144     {
145       tls_log_func (0, "gnutls_rehandshake (server)...\n");
146       tls_log_func (0, gnutls_strerror (sret));
147       tls_log_func (0, "\n");
148     }
149
150   {
151     ssize_t n;
152     char b[1];
153     n = gnutls_record_recv (client, b, 1);
154     if (n != GNUTLS_E_REHANDSHAKE)
155       abort ();
156   }
157
158   HANDSHAKE(client, server);
159
160   /* Check that session still use the extension. */
161   if (!gnutls_safe_renegotiation_status (server)
162       || !gnutls_safe_renegotiation_status (client))
163     {
164       puts ("Client or server not using safe renegotiation extension?");
165       abort ();
166     }
167
168   /* Check that this API does not affect anything after first
169      handshake.
170      gnutls_safe_negotiation_set_initial (server, 0); */
171
172   sret = gnutls_rehandshake (server);
173   if (debug)
174     {
175       tls_log_func (0, "gnutls_rehandshake (server)...\n");
176       tls_log_func (0, gnutls_strerror (sret));
177       tls_log_func (0, "\n");
178     }
179
180   {
181     ssize_t n;
182     char b[1];
183     n = gnutls_record_recv (client, b, 1);
184     if (n != GNUTLS_E_REHANDSHAKE)
185       abort ();
186   }
187
188   HANDSHAKE(client, server);
189
190   /* Check that disabling the extension will break rehandshakes.
191      gnutls_safe_renegotiation_set (client, 0); */
192
193   sret = gnutls_rehandshake (server);
194   if (debug)
195     {
196       tls_log_func (0, "gnutls_rehandshake (server)...\n");
197       tls_log_func (0, gnutls_strerror (sret));
198       tls_log_func (0, "\n");
199     }
200
201   {
202     ssize_t n;
203     char b[1];
204     n = gnutls_record_recv (client, b, 1);
205     if (n != GNUTLS_E_REHANDSHAKE)
206       abort ();
207   }
208
209   HANDSHAKE(client, server);
210
211   gnutls_bye (client, GNUTLS_SHUT_RDWR);
212   gnutls_bye (server, GNUTLS_SHUT_RDWR);
213
214   gnutls_deinit (client);
215   gnutls_deinit (server);
216
217   gnutls_certificate_free_credentials (serverx509cred);
218   gnutls_certificate_free_credentials (clientx509cred);
219
220   gnutls_global_deinit ();
221
222   if (debug)
223     {
224       puts ("Self-test successful");
225     }
226
227   return;
228 }