Added new functions to convert types to strings.
[gnutls.git] / tests / mini-x509-callbacks.c
blob9c7a748becc1b202932134198952ce868e38ff38
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 "utils.h"
33 #include "eagain-common.h"
35 /* Tests whether the verify callbacks are operational.
38 const char* side;
39 static int client_ok = 0, server_ok = 0;
40 static int pch_ok = 0;
42 static int
43 client_callback (gnutls_session_t session)
45 client_ok = 1;
46 return 0;
49 static int post_client_hello_callback (gnutls_session_t session)
51 pch_ok = 1;
52 return 0;
55 static int
56 server_callback (gnutls_session_t session)
58 server_ok = 1;
59 return 0;
62 static void
63 tls_log_func (int level, const char *str)
65 fprintf (stderr, "%s|<%d>| %s", side, level, str);
68 static unsigned char server_cert_pem[] =
69 "-----BEGIN CERTIFICATE-----\n"
70 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
71 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
72 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
73 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
74 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
75 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
76 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
77 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
78 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
79 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
80 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
81 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
82 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
84 const gnutls_datum_t server_cert = { server_cert_pem,
85 sizeof (server_cert_pem)
88 static unsigned char server_key_pem[] =
89 "-----BEGIN RSA PRIVATE KEY-----\n"
90 "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
91 "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
92 "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
93 "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
94 "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
95 "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
96 "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
97 "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
98 "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
99 "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
100 "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
101 "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
102 "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
103 "-----END RSA PRIVATE KEY-----\n";
105 const gnutls_datum_t server_key = { server_key_pem,
106 sizeof (server_key_pem)
109 void doit(void)
111 /* Server stuff. */
112 gnutls_certificate_credentials_t serverx509cred;
113 gnutls_session_t server;
114 int sret = GNUTLS_E_AGAIN;
115 /* Client stuff. */
116 gnutls_certificate_credentials_t clientx509cred;
117 gnutls_session_t client;
118 int cret = GNUTLS_E_AGAIN;
120 /* General init. */
121 gnutls_global_init ();
122 gnutls_global_set_log_function (tls_log_func);
123 if (debug)
124 gnutls_global_set_log_level (2);
126 /* Init server */
127 gnutls_certificate_allocate_credentials (&serverx509cred);
128 gnutls_certificate_set_x509_key_mem (serverx509cred,
129 &server_cert, &server_key,
130 GNUTLS_X509_FMT_PEM);
131 gnutls_init (&server, GNUTLS_SERVER);
132 gnutls_credentials_set (server, GNUTLS_CRD_CERTIFICATE, serverx509cred);
133 gnutls_priority_set_direct (server, "NORMAL", NULL);
134 gnutls_transport_set_push_function (server, server_push);
135 gnutls_transport_set_pull_function (server, server_pull);
136 gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server);
137 gnutls_certificate_set_verify_function (serverx509cred, server_callback);
138 gnutls_handshake_set_post_client_hello_function (server, post_client_hello_callback);
140 /* Init client */
141 gnutls_certificate_allocate_credentials (&clientx509cred);
142 gnutls_init (&client, GNUTLS_CLIENT);
143 gnutls_credentials_set (client, GNUTLS_CRD_CERTIFICATE, clientx509cred);
144 gnutls_priority_set_direct (client, "NORMAL", NULL);
145 gnutls_transport_set_push_function (client, client_push);
146 gnutls_transport_set_pull_function (client, client_pull);
147 gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client);
148 gnutls_certificate_set_verify_function (clientx509cred, client_callback);
150 HANDSHAKE(client, server);
152 gnutls_bye (client, GNUTLS_SHUT_RDWR);
153 gnutls_bye (server, GNUTLS_SHUT_RDWR);
155 gnutls_deinit (client);
156 gnutls_deinit (server);
158 gnutls_certificate_free_credentials (serverx509cred);
159 gnutls_certificate_free_credentials (clientx509cred);
161 gnutls_global_deinit ();
163 if (pch_ok == 0)
164 fail("Post client hello callback wasn't called\n");
166 if (server_ok == 0)
167 fail("Server certificate verify callback wasn't called\n");
169 if (client_ok == 0)
170 fail("Client certificate verify callback wasn't called\n");