2 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
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
28 #include <gnutls/gnutls.h>
29 #include <gnutls/openpgp.h>
32 #include <read-file.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
44 /* This is the same test as openpgp-auth but tests
45 * openpgp under the latest TLS protocol (TLSv1.2). In
46 * addition it tests DSS signatures under that.
49 static const char message
[] = "Hello, brave GNU world!";
51 /* The OpenPGP key pair for use and the key ID in those keys. */
52 static const char pub_key_file
[] = "../guile/tests/openpgp-pub.asc";
53 static const char priv_key_file
[] = "../guile/tests/openpgp-sec.asc";
54 static const char *key_id
= NULL
55 /* FIXME: The values below don't work as expected. */
57 /* "bd572cdcccc07c35" */ ;
60 log_message (int level
, const char *message
)
62 fprintf (stderr
, "[%5d|%2d] %s", getpid (), level
, message
);
72 char *pub_key_path
, *priv_key_path
;
75 gnutls_global_init ();
77 srcdir
= getenv ("srcdir") ? getenv ("srcdir") : ".";
81 gnutls_global_set_log_level (10);
82 gnutls_global_set_log_function (log_message
);
85 err
= socketpair (PF_UNIX
, SOCK_STREAM
, 0, sockets
);
87 fail ("socketpair %s\n", strerror (errno
));
89 pub_key_path
= alloca (strlen (srcdir
) + strlen (pub_key_file
) + 2);
90 strcpy (pub_key_path
, srcdir
);
91 strcat (pub_key_path
, "/");
92 strcat (pub_key_path
, pub_key_file
);
94 priv_key_path
= alloca (strlen (srcdir
) + strlen (priv_key_file
) + 2);
95 strcpy (priv_key_path
, srcdir
);
96 strcat (priv_key_path
, "/");
97 strcat (priv_key_path
, priv_key_file
);
101 fail ("fork %s\n", strerror (errno
));
105 /* Child process (client). */
106 gnutls_session_t session
;
107 gnutls_certificate_credentials_t cred
;
111 printf ("client process %i\n", getpid ());
113 err
= gnutls_init (&session
, GNUTLS_CLIENT
);
115 fail ("client session %d\n", err
);
117 gnutls_priority_set_direct (session
, "NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP", NULL
);
118 gnutls_transport_set_ptr (session
,
119 (gnutls_transport_ptr_t
) (intptr_t)
122 err
= gnutls_certificate_allocate_credentials (&cred
);
124 fail ("client credentials %d\n", err
);
127 gnutls_certificate_set_openpgp_key_file2 (cred
,
128 pub_key_path
, priv_key_path
,
130 GNUTLS_OPENPGP_FMT_BASE64
);
132 fail ("client openpgp keys %d\n", err
);
134 err
= gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cred
);
136 fail ("client credential_set %d\n", err
);
138 gnutls_dh_set_prime_bits (session
, 1024);
140 err
= gnutls_handshake (session
);
142 fail ("client handshake %s (%d) \n", gnutls_strerror(err
), err
);
144 printf ("client handshake successful\n");
146 sent
= gnutls_record_send (session
, message
, sizeof (message
));
147 if (sent
!= sizeof (message
))
148 fail ("client sent %li vs. %li\n",
149 (long) sent
, (long) sizeof (message
));
151 err
= gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
153 fail ("client bye %d\n", err
);
156 printf ("client done\n");
158 gnutls_deinit(session
);
159 gnutls_certificate_free_credentials (cred
);
163 /* Parent process (server). */
164 gnutls_session_t session
;
165 gnutls_dh_params_t dh_params
;
166 gnutls_certificate_credentials_t cred
;
167 char greetings
[sizeof (message
) * 2];
171 const gnutls_datum_t p3
= { (void*) pkcs3
, strlen (pkcs3
) };
174 printf ("server process %i (child %i)\n", getpid (), child
);
176 err
= gnutls_init (&session
, GNUTLS_SERVER
);
178 fail ("server session %d\n", err
);
180 gnutls_priority_set_direct (session
, "NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP", NULL
);
181 gnutls_transport_set_ptr (session
,
182 (gnutls_transport_ptr_t
) (intptr_t)
185 err
= gnutls_certificate_allocate_credentials (&cred
);
187 fail ("server credentials %d\n", err
);
190 gnutls_certificate_set_openpgp_key_file2 (cred
,
191 pub_key_path
, priv_key_path
,
193 GNUTLS_OPENPGP_FMT_BASE64
);
195 fail ("server openpgp keys %d\n", err
);
197 err
= gnutls_dh_params_init (&dh_params
);
199 fail ("server DH params init %d\n", err
);
202 gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
204 fail ("server DH params generate %d\n", err
);
206 gnutls_certificate_set_dh_params (cred
, dh_params
);
208 err
= gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cred
);
210 fail ("server credential_set %d\n", err
);
212 gnutls_certificate_server_set_request (session
, GNUTLS_CERT_REQUIRE
);
214 err
= gnutls_handshake (session
);
216 fail ("server handshake %s (%d) \n", gnutls_strerror(err
), err
);
218 received
= gnutls_record_recv (session
, greetings
, sizeof (greetings
));
219 if (received
!= sizeof (message
)
220 || memcmp (greetings
, message
, sizeof (message
)))
221 fail ("server received %li vs. %li\n",
222 (long) received
, (long) sizeof (message
));
224 err
= gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
226 fail ("server bye %s (%d) \n", gnutls_strerror(err
), err
);
229 printf ("server done\n");
231 gnutls_deinit(session
);
232 gnutls_certificate_free_credentials (cred
);
233 gnutls_dh_params_deinit (dh_params
);
235 done
= wait (&status
);
237 fail ("wait %s\n", strerror (errno
));
240 fail ("who's that?! %d\n", done
);
242 if (WIFEXITED (status
))
244 if (WEXITSTATUS (status
) != 0)
245 fail ("child exited with status %d\n", WEXITSTATUS (status
));
247 else if (WIFSIGNALED (status
))
248 fail ("child stopped by signal %d\n", WTERMSIG (status
));
250 fail ("child failed: %d\n", status
);
253 gnutls_global_deinit ();