Tizen 2.0 Release
[external/libgnutls26.git] / tests / safe-renegotiation / srn3.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.c.
24  *
25  * This tests that a %SAFE_RENEGOTIATION client will reject handshakes
26  * against servers that do not support the extension.  This is sort of
27  * the inverse of what srn1.c is testing.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <gnutls/gnutls.h>
39 #include "../eagain-common.h"
40 #include "../utils.h"
41
42 static void
43 tls_log_func (int level, const char *str)
44 {
45   fprintf (stderr, "|<%d>| %s", level, str);
46 }
47
48 static unsigned char server_cert_pem[] =
49   "-----BEGIN CERTIFICATE-----\n"
50   "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
51   "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
52   "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
53   "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
54   "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
55   "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
56   "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
57   "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
58   "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
59   "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
60   "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
61   "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
62   "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
63
64 const gnutls_datum_t server_cert = { server_cert_pem,
65   sizeof (server_cert_pem)
66 };
67
68 static unsigned char server_key_pem[] =
69   "-----BEGIN RSA PRIVATE KEY-----\n"
70   "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
71   "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
72   "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
73   "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
74   "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
75   "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
76   "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
77   "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
78   "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
79   "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
80   "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
81   "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
82   "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
83   "-----END RSA PRIVATE KEY-----\n";
84
85 const gnutls_datum_t server_key = { server_key_pem,
86   sizeof (server_key_pem)
87 };
88
89 void doit(void)
90 {
91   /* Server stuff. */
92   gnutls_certificate_credentials_t serverx509cred;
93   gnutls_session_t server;
94   int sret = GNUTLS_E_AGAIN;
95   /* Client stuff. */
96   gnutls_certificate_credentials_t clientx509cred;
97   gnutls_session_t client;
98   int cret = GNUTLS_E_AGAIN;
99
100   /* General init. */
101   gnutls_global_init ();
102   gnutls_global_set_log_function (tls_log_func);
103   if (debug) gnutls_global_set_log_level (2);
104
105   /* Init server */
106   gnutls_certificate_allocate_credentials (&serverx509cred);
107   gnutls_certificate_set_x509_key_mem (serverx509cred,
108                                        &server_cert, &server_key,
109                                        GNUTLS_X509_FMT_PEM);
110   gnutls_init (&server, GNUTLS_SERVER);
111   gnutls_credentials_set (server, GNUTLS_CRD_CERTIFICATE, serverx509cred);
112   gnutls_priority_set_direct (server, "NORMAL:%DISABLE_SAFE_RENEGOTIATION",
113                               NULL);
114   gnutls_transport_set_push_function (server, server_push);
115   gnutls_transport_set_pull_function (server, server_pull);
116   gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server);
117
118   /* Init client */
119   gnutls_certificate_allocate_credentials (&clientx509cred);
120   gnutls_init (&client, GNUTLS_CLIENT);
121   gnutls_credentials_set (client, GNUTLS_CRD_CERTIFICATE, clientx509cred);
122   gnutls_priority_set_direct (client, "NORMAL:%SAFE_RENEGOTIATION", NULL);
123   gnutls_transport_set_push_function (client, client_push);
124   gnutls_transport_set_pull_function (client, client_pull);
125   gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client);
126
127   /* Check that initially no session use the extension. */
128   if (gnutls_safe_renegotiation_status (server)
129       || gnutls_safe_renegotiation_status (client))
130     {
131       puts ("Client or server using extension before handshake?");
132       abort ();
133     }
134
135   HANDSHAKE_EXPECT(client, server, GNUTLS_E_SAFE_RENEGOTIATION_FAILED, GNUTLS_E_AGAIN);
136
137   gnutls_bye (client, GNUTLS_SHUT_RDWR);
138   gnutls_bye (server, GNUTLS_SHUT_RDWR);
139
140   gnutls_deinit (client);
141   gnutls_deinit (server);
142
143   gnutls_certificate_free_credentials (serverx509cred);
144   gnutls_certificate_free_credentials (clientx509cred);
145
146   gnutls_global_deinit ();
147
148   if (debug)
149     {
150       puts ("Self-test successful");
151     }
152
153   return;
154 }