Properly restore compression method on session resumption. Reported by Dash Shendy.
[gnutls.git] / tests / openpgp-auth.c
blob9da10b6642bab226035b255902ff16ee07ddaa42
1 /*
2 * Copyright (C) 2010 Free Software Foundation
3 * Author: Ludovic Courtès
5 * This file is part of GNUTLS.
7 * GNUTLS is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GNUTLS is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNUTLS; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <gnutls/gnutls.h>
27 #include <gnutls/openpgp.h>
29 #include "utils.h"
30 #include <read-file.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #if !defined(_WIN32)
37 #include <sys/wait.h>
38 #endif
39 #include <string.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #if !defined(_WIN32)
43 static const char message[] = "Hello, brave GNU world!";
45 /* The OpenPGP key pair for use and the key ID in those keys. */
46 static const char pub_key_file[] = "../guile/tests/openpgp-pub.asc";
47 static const char priv_key_file[] = "../guile/tests/openpgp-sec.asc";
48 static const char *key_id = NULL;
50 static void
51 log_message (int level, const char *message)
53 fprintf (stderr, "[%5d|%2d] %s", getpid (), level, message);
57 void
58 doit ()
60 int err, i;
61 int sockets[2];
62 const char *srcdir;
63 char *pub_key_path, *priv_key_path;
64 pid_t child;
66 gnutls_global_init ();
68 srcdir = getenv ("srcdir") ? getenv ("srcdir") : ".";
70 for (i = 0; i < 3; i++)
73 if (i == 0)
74 key_id = NULL; /* try using the master key */
75 else if (i == 1)
76 key_id = "auto"; /* test auto */
77 else if (i == 2)
78 key_id = "f30fd423c143e7ba";
80 if (debug)
82 gnutls_global_set_log_level (10);
83 gnutls_global_set_log_function (log_message);
86 err = socketpair (PF_UNIX, SOCK_STREAM, 0, sockets);
87 if (err != 0)
88 fail ("socketpair %s\n", strerror (errno));
90 pub_key_path = alloca (strlen (srcdir) + strlen (pub_key_file) + 2);
91 strcpy (pub_key_path, srcdir);
92 strcat (pub_key_path, "/");
93 strcat (pub_key_path, pub_key_file);
95 priv_key_path = alloca (strlen (srcdir) + strlen (priv_key_file) + 2);
96 strcpy (priv_key_path, srcdir);
97 strcat (priv_key_path, "/");
98 strcat (priv_key_path, priv_key_file);
100 child = fork ();
101 if (child == -1)
102 fail ("fork %s\n", strerror (errno));
104 if (child == 0)
106 /* Child process (client). */
107 gnutls_session_t session;
108 gnutls_certificate_credentials_t cred;
109 ssize_t sent;
111 if (debug)
112 printf ("client process %i\n", getpid ());
114 err = gnutls_init (&session, GNUTLS_CLIENT);
115 if (err != 0)
116 fail ("client session %d\n", err);
118 gnutls_priority_set_direct (session,
119 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP",
120 NULL);
121 gnutls_transport_set_ptr (session,
122 (gnutls_transport_ptr_t) (intptr_t)
123 sockets[0]);
125 err = gnutls_certificate_allocate_credentials (&cred);
126 if (err != 0)
127 fail ("client credentials %d\n", err);
129 err =
130 gnutls_certificate_set_openpgp_key_file2 (cred,
131 pub_key_path,
132 priv_key_path, key_id,
133 GNUTLS_OPENPGP_FMT_BASE64);
134 if (err != 0)
135 fail ("client openpgp keys %s\n", gnutls_strerror (err));
137 err =
138 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cred);
139 if (err != 0)
140 fail ("client credential_set %d\n", err);
142 gnutls_dh_set_prime_bits (session, 1024);
144 err = gnutls_handshake (session);
145 if (err != 0)
146 fail ("client handshake %s (%d) \n", gnutls_strerror (err), err);
147 else if (debug)
148 printf ("client handshake successful\n");
150 sent = gnutls_record_send (session, message, sizeof (message));
151 if (sent != sizeof (message))
152 fail ("client sent %li vs. %li\n",
153 (long) sent, (long) sizeof (message));
155 err = gnutls_bye (session, GNUTLS_SHUT_RDWR);
156 if (err != 0)
157 fail ("client bye %d\n", err);
159 if (debug)
160 printf ("client done\n");
162 else
164 /* Parent process (server). */
165 gnutls_session_t session;
166 gnutls_dh_params_t dh_params;
167 gnutls_certificate_credentials_t cred;
168 char greetings[sizeof (message) * 2];
169 ssize_t received;
170 pid_t done;
171 int status;
172 const gnutls_datum_t p3 = { (char *) pkcs3, strlen (pkcs3) };
174 if (debug)
175 printf ("server process %i (child %i)\n", getpid (), child);
177 err = gnutls_init (&session, GNUTLS_SERVER);
178 if (err != 0)
179 fail ("server session %d\n", err);
181 gnutls_priority_set_direct (session,
182 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP",
183 NULL);
184 gnutls_transport_set_ptr (session,
185 (gnutls_transport_ptr_t) (intptr_t)
186 sockets[1]);
188 err = gnutls_certificate_allocate_credentials (&cred);
189 if (err != 0)
190 fail ("server credentials %d\n", err);
192 err =
193 gnutls_certificate_set_openpgp_key_file2 (cred,
194 pub_key_path,
195 priv_key_path, key_id,
196 GNUTLS_OPENPGP_FMT_BASE64);
197 if (err != 0)
198 fail ("server openpgp keys %s\n", gnutls_strerror (err));
200 err = gnutls_dh_params_init (&dh_params);
201 if (err)
202 fail ("server DH params init %d\n", err);
204 err =
205 gnutls_dh_params_import_pkcs3 (dh_params, &p3,
206 GNUTLS_X509_FMT_PEM);
207 if (err)
208 fail ("server DH params generate %d\n", err);
210 gnutls_certificate_set_dh_params (cred, dh_params);
212 err =
213 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cred);
214 if (err != 0)
215 fail ("server credential_set %d\n", err);
217 gnutls_certificate_server_set_request (session,
218 GNUTLS_CERT_REQUIRE);
220 err = gnutls_handshake (session);
221 if (err != 0)
222 fail ("server handshake %s (%d) \n", gnutls_strerror (err), err);
224 received =
225 gnutls_record_recv (session, greetings, sizeof (greetings));
226 if (received != sizeof (message)
227 || memcmp (greetings, message, sizeof (message)))
228 fail ("server received %li vs. %li\n", (long) received,
229 (long) sizeof (message));
231 err = gnutls_bye (session, GNUTLS_SHUT_RDWR);
232 if (err != 0)
233 fail ("server bye %s (%d) \n", gnutls_strerror (err), err);
235 if (debug)
236 printf ("server done\n");
238 done = wait (&status);
239 if (done < 0)
240 fail ("wait %s\n", strerror (errno));
242 if (done != child)
243 fail ("who's that?! %d\n", done);
245 if (WIFEXITED (status))
247 if (WEXITSTATUS (status) != 0)
248 fail ("child exited with status %d\n", WEXITSTATUS (status));
250 else if (WIFSIGNALED (status))
251 fail ("child stopped by signal %d\n", WTERMSIG (status));
252 else
253 fail ("child failed: %d\n", status);
258 #else
259 void
260 doit ()
262 exit (77);
264 #endif