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
[512], priv_key_path
[512];
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 (AF_UNIX
, SOCK_STREAM
, 0, sockets
);
88 fail ("socketpair %s\n", strerror (errno
));
90 if (sizeof(pub_key_path
) < strlen (srcdir
) + strlen (pub_key_file
) + 2)
93 strcpy (pub_key_path
, srcdir
);
94 strcat (pub_key_path
, "/");
95 strcat (pub_key_path
, pub_key_file
);
97 if (sizeof(priv_key_path
) < strlen (srcdir
) + strlen (priv_key_file
) + 2)
100 strcpy (priv_key_path
, srcdir
);
101 strcat (priv_key_path
, "/");
102 strcat (priv_key_path
, priv_key_file
);
106 fail ("fork %s\n", strerror (errno
));
110 /* Child process (client). */
111 gnutls_session_t session
;
112 gnutls_certificate_credentials_t cred
;
116 printf ("client process %i\n", getpid ());
118 err
= gnutls_init (&session
, GNUTLS_CLIENT
);
120 fail ("client session %d\n", err
);
122 if (i
==0) /* we use the primary key which is RSA. Test the RSA ciphersuite */
123 gnutls_priority_set_direct (session
,
124 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+RSA:+CTYPE-OPENPGP",
127 gnutls_priority_set_direct (session
,
128 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP",
130 gnutls_transport_set_ptr (session
,
131 (gnutls_transport_ptr_t
) (intptr_t)
134 err
= gnutls_certificate_allocate_credentials (&cred
);
136 fail ("client credentials %d\n", err
);
139 gnutls_certificate_set_openpgp_key_file2 (cred
,
141 priv_key_path
, key_id
,
142 GNUTLS_OPENPGP_FMT_BASE64
);
144 fail ("client openpgp keys %s\n", gnutls_strerror (err
));
147 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cred
);
149 fail ("client credential_set %d\n", err
);
151 gnutls_dh_set_prime_bits (session
, 1024);
153 err
= gnutls_handshake (session
);
155 fail ("client handshake %s (%d) \n", gnutls_strerror (err
), err
);
157 printf ("client handshake successful\n");
159 sent
= gnutls_record_send (session
, message
, sizeof (message
));
160 if (sent
!= sizeof (message
))
161 fail ("client sent %li vs. %li\n",
162 (long) sent
, (long) sizeof (message
));
164 err
= gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
166 fail ("client bye %d\n", err
);
169 printf ("client done\n");
171 gnutls_deinit(session
);
172 gnutls_certificate_free_credentials (cred
);
176 /* Parent process (server). */
177 gnutls_session_t session
;
178 gnutls_dh_params_t dh_params
;
179 gnutls_certificate_credentials_t cred
;
180 char greetings
[sizeof (message
) * 2];
184 const gnutls_datum_t p3
= { (void *) pkcs3
, strlen (pkcs3
) };
187 printf ("server process %i (child %i)\n", getpid (), child
);
189 err
= gnutls_init (&session
, GNUTLS_SERVER
);
191 fail ("server session %d\n", err
);
193 gnutls_priority_set_direct (session
,
194 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+RSA:+CTYPE-OPENPGP",
196 gnutls_transport_set_ptr (session
,
197 (gnutls_transport_ptr_t
) (intptr_t)
200 err
= gnutls_certificate_allocate_credentials (&cred
);
202 fail ("server credentials %d\n", err
);
205 gnutls_certificate_set_openpgp_key_file2 (cred
,
207 priv_key_path
, key_id
,
208 GNUTLS_OPENPGP_FMT_BASE64
);
210 fail ("server openpgp keys %s\n", gnutls_strerror (err
));
212 err
= gnutls_dh_params_init (&dh_params
);
214 fail ("server DH params init %d\n", err
);
217 gnutls_dh_params_import_pkcs3 (dh_params
, &p3
,
218 GNUTLS_X509_FMT_PEM
);
220 fail ("server DH params generate %d\n", err
);
222 gnutls_certificate_set_dh_params (cred
, dh_params
);
225 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cred
);
227 fail ("server credential_set %d\n", err
);
229 gnutls_certificate_server_set_request (session
,
230 GNUTLS_CERT_REQUIRE
);
232 err
= gnutls_handshake (session
);
234 fail ("server handshake %s (%d) \n", gnutls_strerror (err
), err
);
237 gnutls_record_recv (session
, greetings
, sizeof (greetings
));
238 if (received
!= sizeof (message
)
239 || memcmp (greetings
, message
, sizeof (message
)))
240 fail ("server received %li vs. %li\n", (long) received
,
241 (long) sizeof (message
));
243 err
= gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
245 fail ("server bye %s (%d) \n", gnutls_strerror (err
), err
);
248 printf ("server done\n");
250 gnutls_deinit(session
);
251 gnutls_certificate_free_credentials (cred
);
252 gnutls_dh_params_deinit (dh_params
);
254 done
= wait (&status
);
256 fail ("wait %s\n", strerror (errno
));
259 fail ("who's that?! %d\n", done
);
261 if (WIFEXITED (status
))
263 if (WEXITSTATUS (status
) != 0)
264 fail ("child exited with status %d\n", WEXITSTATUS (status
));
266 else if (WIFSIGNALED (status
))
267 fail ("child stopped by signal %d\n", WTERMSIG (status
));
269 fail ("child failed: %d\n", status
);
274 gnutls_global_deinit ();