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
26 #include <gnutls/gnutls.h>
27 #include <gnutls/openpgp.h>
30 #include <read-file.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
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
;
51 log_message (int level
, const char *message
)
53 fprintf (stderr
, "[%5d|%2d] %s", getpid (), level
, message
);
63 char *pub_key_path
, *priv_key_path
;
66 gnutls_global_init ();
68 srcdir
= getenv ("srcdir") ? getenv ("srcdir") : ".";
70 for (i
= 0; i
< 4; i
++)
74 key_id
= NULL
; /* try using the master key */
76 key_id
= "auto"; /* test auto */
78 key_id
= "f30fd423c143e7ba";
82 gnutls_global_set_log_level (5);
83 gnutls_global_set_log_function (log_message
);
86 err
= socketpair (PF_UNIX
, SOCK_STREAM
, 0, sockets
);
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
);
102 fail ("fork %s\n", strerror (errno
));
106 /* Child process (client). */
107 gnutls_session_t session
;
108 gnutls_certificate_credentials_t cred
;
112 printf ("client process %i\n", getpid ());
114 err
= gnutls_init (&session
, GNUTLS_CLIENT
);
116 fail ("client session %d\n", err
);
118 if (i
==0) /* we use the primary key which is RSA. Test the RSA ciphersuite */
119 gnutls_priority_set_direct (session
,
120 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+RSA:+CTYPE-OPENPGP",
123 gnutls_priority_set_direct (session
,
124 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP",
126 gnutls_transport_set_ptr (session
,
127 (gnutls_transport_ptr_t
) (intptr_t)
130 err
= gnutls_certificate_allocate_credentials (&cred
);
132 fail ("client credentials %d\n", err
);
135 gnutls_certificate_set_openpgp_key_file2 (cred
,
137 priv_key_path
, key_id
,
138 GNUTLS_OPENPGP_FMT_BASE64
);
140 fail ("client openpgp keys %s\n", gnutls_strerror (err
));
143 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cred
);
145 fail ("client credential_set %d\n", err
);
147 gnutls_dh_set_prime_bits (session
, 1024);
149 err
= gnutls_handshake (session
);
151 fail ("client handshake %s (%d) \n", gnutls_strerror (err
), err
);
153 printf ("client handshake successful\n");
155 sent
= gnutls_record_send (session
, message
, sizeof (message
));
156 if (sent
!= sizeof (message
))
157 fail ("client sent %li vs. %li\n",
158 (long) sent
, (long) sizeof (message
));
160 err
= gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
162 fail ("client bye %d\n", err
);
165 printf ("client done\n");
167 gnutls_deinit(session
);
168 gnutls_certificate_free_credentials (cred
);
172 /* Parent process (server). */
173 gnutls_session_t session
;
174 gnutls_dh_params_t dh_params
;
175 gnutls_certificate_credentials_t cred
;
176 char greetings
[sizeof (message
) * 2];
180 const gnutls_datum_t p3
= { (void *) pkcs3
, strlen (pkcs3
) };
183 printf ("server process %i (child %i)\n", getpid (), child
);
185 err
= gnutls_init (&session
, GNUTLS_SERVER
);
187 fail ("server session %d\n", err
);
189 gnutls_priority_set_direct (session
,
190 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+RSA:+CTYPE-OPENPGP",
192 gnutls_transport_set_ptr (session
,
193 (gnutls_transport_ptr_t
) (intptr_t)
196 err
= gnutls_certificate_allocate_credentials (&cred
);
198 fail ("server credentials %d\n", err
);
201 gnutls_certificate_set_openpgp_key_file2 (cred
,
203 priv_key_path
, key_id
,
204 GNUTLS_OPENPGP_FMT_BASE64
);
206 fail ("server openpgp keys %s\n", gnutls_strerror (err
));
208 err
= gnutls_dh_params_init (&dh_params
);
210 fail ("server DH params init %d\n", err
);
213 gnutls_dh_params_import_pkcs3 (dh_params
, &p3
,
214 GNUTLS_X509_FMT_PEM
);
216 fail ("server DH params generate %d\n", err
);
218 gnutls_certificate_set_dh_params (cred
, dh_params
);
221 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cred
);
223 fail ("server credential_set %d\n", err
);
225 gnutls_certificate_server_set_request (session
,
226 GNUTLS_CERT_REQUIRE
);
228 err
= gnutls_handshake (session
);
230 fail ("server handshake %s (%d) \n", gnutls_strerror (err
), err
);
233 gnutls_record_recv (session
, greetings
, sizeof (greetings
));
234 if (received
!= sizeof (message
)
235 || memcmp (greetings
, message
, sizeof (message
)))
236 fail ("server received %li vs. %li\n", (long) received
,
237 (long) sizeof (message
));
239 err
= gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
241 fail ("server bye %s (%d) \n", gnutls_strerror (err
), err
);
244 printf ("server done\n");
246 gnutls_deinit(session
);
247 gnutls_certificate_free_credentials (cred
);
248 gnutls_dh_params_deinit (dh_params
);
250 done
= wait (&status
);
252 fail ("wait %s\n", strerror (errno
));
255 fail ("who's that?! %d\n", done
);
257 if (WIFEXITED (status
))
259 if (WEXITSTATUS (status
) != 0)
260 fail ("child exited with status %d\n", WEXITSTATUS (status
));
262 else if (WIFSIGNALED (status
))
263 fail ("child stopped by signal %d\n", WTERMSIG (status
));
265 fail ("child failed: %d\n", status
);
270 gnutls_global_deinit ();