added missing file
[gnutls.git] / tests / openpgpself.c
blob1691190dd1e3d09f0814dc32ca55715d0cb90b9b
1 /*
2 * Copyright (C) 2004-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 /* Parts copied from GnuTLS example programs. */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #if !defined(_WIN32)
35 #include <netinet/in.h>
36 #include <sys/wait.h>
37 #include <arpa/inet.h>
38 #endif
39 #include <unistd.h>
40 #include <gnutls/gnutls.h>
41 #include <gnutls/openpgp.h>
43 #include "utils.h"
45 #include "ex-session-info.c"
46 #include "ex-x509-info.c"
47 #include "tcp.c"
49 pid_t child;
51 static void
52 tls_log_func (int level, const char *str)
54 fprintf (stderr, "%s |<%d>| %s", child ? "server" : "client", level, str);
57 /* A very basic TLS client, with anonymous authentication.
60 #define MAX_BUF 1024
61 #define MSG "Hello TLS"
63 static unsigned char cert_txt[] =
64 "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
65 "Version: GnuPG v1.4.10 (GNU/Linux)\n"
66 "Comment: Test key for GnuTLS\n"
67 "\n"
68 "mI0ETYD2OQEEAMHmDBtJii82NbWuYcvEWCYnwa7GTcz2PYikYCcq/t5nkyb5Bfmx\n"
69 "mh2hpto7Lr5d1L/shvab1gXCcrWEA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gBBkRAgAGBQJNgPY5AAoJEPMP1CPBQ+e6\n"
85 "3fQAnR7HWLnQTbxCIhlBTZiuJv2HC6cbAJwJ6VsSU6ADCkMuGT3LLNo+UnckK+4i\n"
86 "BACcivWsW40ddtEQ0wno1uP65TmKq3aJrdODXTAnqkmNQKL7X7Fz+nmEWiS+LBH8\n"
87 "lRvAaeRPX2LV+DCJDbAPrYd7LkOHyuM0I+ZApto5cjem/EnO7op2QwkCCa6oUp0l\n"
88 "YA6i6aGF2KGx7WQwi2URIMPhihpOvAbkjfszYpFL4VP5wQ==\n"
89 "=ydIq\n"
90 "-----END PGP PUBLIC KEY BLOCK-----\n";
92 const gnutls_datum_t cert = { cert_txt, sizeof (cert_txt) };
94 static unsigned char key_txt[] =
95 "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"
96 "Version: GnuPG v1.4.10 (GNU/Linux)\n"
97 "Comment: Test key for GnuTLS\n"
98 "\n"
99 "lQHYBE2A9jkBBADB5gwbSYovNjW1rmHLxFgmJ8Guxk3M9j2IpGAnKv7eZ5Mm+QX5\n"
100 "sZodoabaOy6+XdS/7Ib2m9YFwnK1hAERIDTZPS4qMLS7kxwXXjhZSdbtT9a73RVS\n"
101 "qtmun7e+0OVZbVAyj4E6s+DVh9ilJS3078CQ56tg8WLG8FaQQ1EblKHF4QARAQAB\n"
102 "AAP9HJePsXZmqg+UW/Ya9bE+TmIObXdQgajN6hhTFXOBocokKNsPxoIp97Sepg+U\n"
103 "FP5BIQv/2t2f8bl6sMmGXsAhCqVzRxGuA+9USx8OfTHSdgIKT5T2VFSGJaU4df3Q\n"
104 "rstUY3dcvl6VKpDDZic1T7u2ANzaWM2u+pwooKC4cc/k9AECAMNDvrKF3FC7R9sd\n"
105 "TagVrrfde0RZuwhbGW9ghslkY893EelXQL/lbBI20crPdrsdDpMe370KO2bQLqwO\n"
106 "HGAxIYUCAP41iC7KReYvysLZ34tM55ZFE7BPsMcXUeu6hkYOMDZYvE+x4KV6Umo+\n"
107 "Civ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QYAQIACQUCTYD2OQIbIgBSCRAMTrFUBnAKMEcgBBkR\n"
123 "AgAGBQJNgPY5AAoJEPMP1CPBQ+e63fQAniK5kU+dwIbkD+OHJHkC73V6v4D8AJ0Z\n"
124 "+GBYj4nhKEX21QXfj55F3Zpg1e4iBACcivWsW40ddtEQ0wno1uP65TmKq3aJrdOD\n"
125 "XTAnqkmNQKL7X7Fz+nmEWiS+LBH8lRvAaeRPX2LV+DCJDbAPrYd7LkOHyuM0I+ZA\n"
126 "pto5cjem/EnO7op2QwkCCa6oUp0lYA6i6aGF2KGx7WQwi2URIMPhihpOvAbkjfsz\n"
127 "YpFL4VP5wQ==\n"
128 "=zzoN\n"
129 "-----END PGP PRIVATE KEY BLOCK-----\n";
131 const gnutls_datum_t key = { key_txt, sizeof (key_txt) };
134 static void
135 client (void)
137 int ret, sd, ii, j;
138 gnutls_session_t session;
139 char buffer[MAX_BUF + 1];
140 gnutls_certificate_credentials_t xcred;
142 gnutls_global_init ();
144 gnutls_global_set_log_function (tls_log_func);
145 if (debug)
146 gnutls_global_set_log_level (9);
148 gnutls_certificate_allocate_credentials (&xcred);
150 /* sets the trusted cas file
152 if (debug)
153 success ("Setting key files...\n");
155 ret = gnutls_certificate_set_openpgp_key_mem (xcred, &cert, &key,
156 GNUTLS_OPENPGP_FMT_BASE64);
157 if (ret < 0)
159 fail ("Could not set key files...\n");
160 return;
163 for (j = 0; j < 2; j++)
167 /* Initialize TLS session
169 gnutls_init (&session, GNUTLS_CLIENT);
171 /* Use default priorities */
172 gnutls_priority_set_direct (session, "NORMAL:+CTYPE-OPENPGP", NULL);
174 /* put the x509 credentials to the current session
176 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
178 /* connect to the peer
180 if (debug)
181 success ("Connecting...\n");
182 sd = tcp_connect ();
184 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
186 /* Perform the TLS handshake
188 ret = gnutls_handshake (session);
190 if (ret < 0)
192 fail ("client: Handshake %d failed\n", j);
193 gnutls_perror (ret);
194 goto end;
196 else if (debug)
198 success ("client: Handshake %d was completed\n", j);
201 if (debug)
202 success ("client: TLS version is: %s\n",
203 gnutls_protocol_get_name (gnutls_protocol_get_version
204 (session)));
206 /* see the Getting peer's information example */
207 if (debug)
208 print_info (session);
210 gnutls_record_send (session, MSG, strlen (MSG));
212 ret = gnutls_record_recv (session, buffer, MAX_BUF);
213 if (ret == 0)
215 if (debug)
216 success ("client: Peer has closed the TLS connection\n");
217 goto end;
219 else if (ret < 0)
221 fail ("client: Error: %s\n", gnutls_strerror (ret));
222 goto end;
225 if (debug)
227 printf ("- Received %d bytes: ", ret);
228 for (ii = 0; ii < ret; ii++)
230 fputc (buffer[ii], stdout);
232 fputs ("\n", stdout);
235 gnutls_bye (session, GNUTLS_SHUT_RDWR);
238 tcp_close (sd);
240 gnutls_deinit (session);
244 end:
246 gnutls_certificate_free_credentials (xcred);
248 gnutls_global_deinit ();
251 /* This is a sample TLS 1.0 echo server, using X.509 authentication.
254 #define SA struct sockaddr
255 #define MAX_BUF 1024
256 #define PORT 5556 /* listen to 5556 port */
257 #define DH_BITS 1024
259 /* These are global */
260 gnutls_certificate_credentials_t pgp_cred;
262 static gnutls_session_t
263 initialize_tls_session (void)
265 gnutls_session_t session;
267 gnutls_init (&session, GNUTLS_SERVER);
269 /* avoid calling all the priority functions, since the defaults
270 * are adequate.
272 gnutls_priority_set_direct (session, "NORMAL:+CTYPE-OPENPGP", NULL);
274 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, pgp_cred);
276 /* request client certificate if any.
278 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
280 gnutls_dh_set_prime_bits (session, DH_BITS);
282 return session;
285 static gnutls_dh_params_t dh_params;
287 static int
288 generate_dh_params (void)
290 const gnutls_datum_t p3 = { (void *) pkcs3, strlen (pkcs3) };
291 /* Generate Diffie-Hellman parameters - for use with DHE
292 * kx algorithms. These should be discarded and regenerated
293 * once a day, once a week or once a month. Depending on the
294 * security requirements.
296 gnutls_dh_params_init (&dh_params);
297 return gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
300 int err, listen_sd, i;
301 int sd, ret;
302 struct sockaddr_in sa_serv;
303 struct sockaddr_in sa_cli;
304 socklen_t client_len;
305 char topbuf[512];
306 gnutls_session_t session;
307 char buffer[MAX_BUF + 1];
308 int optval = 1;
310 static unsigned char server_crt_txt[] =
311 "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
312 "Version: GnuPG v1.4.6 (GNU/Linux)\n"
313 "\n"
314 "mNEER2PogwEGINdIR4u5PR4SwADWwj/ztgtoi7XVbmlfbQTHpBYFxTSC88pISSNy\n"
315 "V/rgnlqunYP77F7aHL4KUReN3v9sKw01xSGEfox/JmlqUUg6CVvTjdeLfkuVIBnH\n"
316 "j+2KMlaxezp7IxtPaTXpXcSf8iOuVq7UX7p6tKbppKXO5GgmfA88VUVvGBs1/PQp\n"
317 "WKQdGrj+6I3RRmDN/hna1jGU/N23230Hbx+bu7g9cviiSh10ri7rdDhVJ67tRkRG\n"
318 "Usy3XO6dWC7EmzZlEO8AEQEAAbQQdGVzdDMuZ251dGxzLm9yZ4kBAAQTAQIAJgUC\n"
319 "R2PogwIbAwUJCWYBgAYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJEKAh4/gImZBR\n"
320 "96QGH3E3zynETuQS3++hGMvMXq2mDJeT2e8964y/ifIOBpr2K2isuLYnrtGKyxi+\n"
321 "ZptyHv6ymR3bDvio50cjnoT/WK1onosOJvtijGBS+U/ooq3im7ExpeQYXc/zpYsX\n"
322 "OmB5m6BvdomUp2PMqdxsmOPoaRkSYx5R2Rlo/z3csodl6sp3k465Y/jg7L4gkxDz\n"
323 "XJM+CS1xMhcOF0gBhppqLnG67x0ow847Pydstzkw0sOqedkLPuScaHNnlAWQ7QH6\n"
324 "mbbpqHJwekS4jQRHiKV8AQQA0iZ81WXypLI4ZE2+hYfBCnfMVfQF/vPgvASxhwri\n"
325 "GDa9Zc2f/VfakfNiwZgHH6iCeppHBiP2jljnbuOsL6f1R+0FsnyTVwHbuEU7IU2y\n"
326 "+J0/s0z3wcx9sx8T7brP5z5F2hdagBsD9YFGCifHDAEew4mmAisY0i2QHVIuXJFj\n"
327 "4RMAEQEAAYkBhwQYAQIADwUCR4ilfAIbAgUJEOrPgACoCRCgIeP4CJmQUZ0gBBkB\n"
328 "AgAGBQJHiKV8AAoJEIN7b7QuD+F2AEcEAKAjhO9kSOE8UuwEOKlwsWL9LUUSkHJj\n"
329 "c/ca0asLAerzrHsldRAcwCbWkVxBBHySw2CLFjzpgdXhwRtsytMgHaapfAPbinAW\n"
330 "jCPIEJx2gDZeZnTgi4DVbZn5E3UzHGyL69MEoXr5t+vpiemQFd/nGD+h/Q2A76od\n"
331 "gvAryRvS1Soj8bcGHjUflayXGOSvaD8P2V5Vz0hS82QZcqWxD8qUBqbcB8atokmO\n"
332 "IYxhKyRmO58T5Ma+iaxBTUIwee+pBYDgdH6E2dh9xLlwwzZKaCcIRCQcObkLsMVo\n"
333 "fZJo+m0Xf8zI57NeQF+hXJhW7lIrWgQVr8IVp/lgo76acLHfL/t1n0Nhg4r2srz2\n"
334 "fpP2w5laQ0qImYLnZhGFHU+rJUyFaHfhD8/svN2LuZkO570pjV/K68EaHnEfk5b8\n"
335 "jWu/euohwcCwf20M1kTo3Bg=\n"
336 "=Xjon\n" "-----END PGP PUBLIC KEY BLOCK-----\n";
337 const gnutls_datum_t server_crt = { server_crt_txt, sizeof (server_crt_txt) };
339 static unsigned char server_key_txt[] =
340 "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"
341 "Version: GnuPG v1.4.6 (GNU/Linux)\n"
342 "\n"
343 "lQLGBEdj6IMBBiDXSEeLuT0eEsAA1sI/87YLaIu11W5pX20Ex6QWBcU0gvPKSEkj\n"
344 "clf64J5arp2D++xe2hy+ClEXjd7/bCsNNcUhhH6MfyZpalFIOglb043Xi35LlSAZ\n"
345 "x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RKQcmNz9xrRqwsB6vOseyV1EBzAJtaRXEEEfJLDYIsWPOmB\n"
376 "1eHBG2zK0yAdpql8A9uKcBaMI8gQnHaANl5mdOCLgNVtmfkTdTMcbIvr0wShevm3\n"
377 "6+mJ6ZAV3+cYP6H9DYDvqh2C8CvJG9LVKiPxtwYeNR+VrJcY5K9oPw/ZXlXPSFLz\n"
378 "ZBlypbEPypQGptwHxq2iSY4hjGErJGY7nxPkxr6JrEFNQjB576kFgOB0foTZ2H3E\n"
379 "uXDDNkpoJwhEJBw5uQuwxWh9kmj6bRd/zMjns15AX6FcmFbuUitaBBWvwhWn+WCj\n"
380 "vppwsd8v+3WfQ2GDivayvPZ+k/bDmVpDSoiZgudmEYUdT6slTIVod+EPz+y83Yu5\n"
381 "mQ7nvSmNX8rrwRoecR+TlvyNa7966iHBwLB/bQzWROjcGA==\n"
382 "=mZnW\n" "-----END PGP PRIVATE KEY BLOCK-----\n";
383 const gnutls_datum_t server_key = { server_key_txt, sizeof (server_key_txt) };
385 static unsigned char cert2048_txt[] =
386 "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
387 "Version: GnuPG v1.4.10 (GNU/Linux)\n"
388 "Comment: Test key for GnuTLS\n"
389 "\n"
390 "mQMuBE1/6bQRCAD8TQlwbkkX3bLJvemSA/BqT/z0OrJsuXKFQqK5Pp0BRTwC4iCg\n"
391 "wnUFrr012up66YTzaA0aQpkf48gqxZ1XTGZtZ13+aAArChqKiffR7OS+BnROd+D3\n"
392 "NkPF0tWDAqRFsybIej1GcdSyPw+neExSfoeYzNpUW9oX2iLh5QZC/xt++kE8tOr8\n"
393 "BXiDW/+rudjf8Rc0ZI10vi12rb64eYd7szE49crS2YsjqarnncN+J7RX3jSifKrZ\n"
394 "XqP/F5s/0a1Nfd4xQU2fsnbQwiIuKTQjU6BHD/2ILnhZImEUn4KqZvbEt6yIJiLy\n"
395 "u+KerhTiuAhl+sx2DQf3EVxD8EpCwzFqXtF3AQD9Nf9OFJ2Cchwuz8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/7qxCSHhT3OQpiOW65\n"
426 "dyBX/44XAVQaWtf6DJc84nWDYsCgscEZzGAUyBY8Fw9S7We5OFLNcYWIwQQYEQgA\n"
427 "CQUCTX/ptAIbIgBqCRB7/ynKCzvfuF8gBBkRCAAGBQJNf+m0AAoJEEPv0WrPxcc9\n"
428 "aJwA/0zWQ0RfRhlC1nbf7ISEOF36WQjslGKXjf6z6rSNgphoAP4119FDX9jaW0B8\n"
429 "HL9p+XRZTOTSo5GMLUTH5zo+zpTbB2cxAP9moc/i1z2D8AXTnUk7YfSm+o7rFThu\n"
430 "2Cx0oO7h1g0MjQD6A/6e68DhK9altb/xqtHeG0jbLmvFRtkC0zu7WZjvSbc=\n"
431 "=v3gg\n"
432 "-----END PGP PUBLIC KEY BLOCK-----\n";
434 const gnutls_datum_t cert2048 = { cert2048_txt, sizeof (cert2048_txt) };
436 static unsigned char key2048_txt[] =
437 "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"
438 "Version: GnuPG v1.4.10 (GNU/Linux)\n"
439 "Comment: Test key for GnuTLS\n"
440 "\n"
441 "lQNTBE1/6bQRCAD8TQlwbkkX3bLJvemSA/BqT/z0OrJsuXKFQqK5Pp0BRTwC4iCg\n"
442 "wnUFrr012up66YTzaA0aQpkf48gqxZ1XTGZtZ13+aAArChqKiffR7OS+BnROd+D3\n"
443 "NkPF0tWDAqRFsybIej1GcdSyPw+neExSfoeYzNpUW9oX2iLh5QZC/xt++kE8tOr8\n"
444 "BXiDW/+rudjf8Rc0ZI10vi12rb64eYd7szE49crS2YsjqarnncN+J7RX3jSifKrZ\n"
445 "XqP/F5s/0a1Nfd4xQU2fsnbQwiIuKTQjU6BHD/2ILnhZImEUn4KqZvbEt6yIJiLy\n"
446 "u+KerhTiuAhl+sx2DQf3EVxD8EpCwzFqXtF3AQD9Nf9OFJ2Cchwuz8Q5VDBoRFhP\n"
447 "4p/hGWqAsmRSZlxdQQf/Q5R15CMDtCrZnuSeptfgdZUfB0gi0aYeKE2TWto5J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FwFUGlrX\n"
478 "+gyXPOJ1g2LAoLHBGcxgFMgWPBcPUu1nuThSzXGFAAEAgj6e0tgxENBORrJkBCl6\n"
479 "xfV6iTNXa3HDArTNTyURRzEN0YjBBBgRCAAJBQJNf+m0AhsiAGoJEHv/KcoLO9+4\n"
480 "XyAEGREIAAYFAk1/6bQACgkQQ+/Ras/Fxz1onAD/W3lWDopZrH9R66tiyjYOX4sV\n"
481 "b1SoPlKRJngsHouxc4oA/RYoFGrhoY+nL22eza/Ku/SUnVrufZ/jIvQakhpmrLD/\n"
482 "ZzEBAJ1w0ez3wUJbsfGlWBkb16pYpIh68/qvTTj84v5N0picAQC1p8JjouN88BJw\n"
483 "9UquUquXdK1TY965biHIQ70uaOU4Hw==\n"
484 "=Rrkw\n"
485 "-----END PGP PRIVATE KEY BLOCK-----\n";
487 const gnutls_datum_t key2048 = { key2048_txt, sizeof (key2048_txt) };
490 static void
491 server_start (void)
493 /* Socket operations
495 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
496 if (err == -1)
498 perror ("socket");
499 fail ("server: socket failed\n");
500 return;
503 memset (&sa_serv, '\0', sizeof (sa_serv));
504 sa_serv.sin_family = AF_INET;
505 sa_serv.sin_addr.s_addr = INADDR_ANY;
506 sa_serv.sin_port = htons (PORT); /* Server Port number */
508 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
509 sizeof (int));
511 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
512 if (err == -1)
514 perror ("bind");
515 fail ("server: bind failed\n");
516 return;
519 err = listen (listen_sd, 1024);
520 if (err == -1)
522 perror ("listen");
523 fail ("server: listen failed\n");
524 return;
527 if (debug)
528 success ("server: ready. Listening to port '%d'.\n", PORT);
531 static void
532 server (void)
534 int j;
535 /* this must be called once in the program
537 gnutls_global_init ();
539 gnutls_global_set_log_function (tls_log_func);
540 if (debug)
541 gnutls_global_set_log_level (5);
543 if (debug)
544 success ("Launched, setting DH parameters...\n");
546 generate_dh_params ();
548 client_len = sizeof (sa_cli);
550 for (j = 0; j < 2; j++)
552 if (j==0)
554 gnutls_certificate_allocate_credentials (&pgp_cred);
555 ret = gnutls_certificate_set_openpgp_key_mem2 (pgp_cred, &server_crt,
556 &server_key, "auto",
557 GNUTLS_OPENPGP_FMT_BASE64);
559 else
561 gnutls_certificate_free_credentials (pgp_cred);
562 gnutls_certificate_allocate_credentials (&pgp_cred);
563 ret =
564 gnutls_certificate_set_openpgp_key_mem2 (pgp_cred, &cert2048, &key2048,
565 "auto", GNUTLS_OPENPGP_FMT_BASE64);
568 if (ret < 0)
570 fail ("Could not set server key files...\n");
571 goto end;
574 gnutls_certificate_set_dh_params (pgp_cred, dh_params);
576 session = initialize_tls_session ();
578 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
580 if (debug)
581 success ("server: connection from %s, port %d\n",
582 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
583 sizeof (topbuf)), ntohs (sa_cli.sin_port));
585 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
586 ret = gnutls_handshake (session);
587 if (ret < 0)
589 close (sd);
590 gnutls_deinit (session);
591 fail ("server: Handshake %d has failed (%s)\n\n",
592 j, gnutls_strerror (ret));
593 goto end;
595 if (debug)
596 success ("server: Handshake %d was completed\n", j);
598 if (debug)
599 success ("server: TLS version is: %s\n",
600 gnutls_protocol_get_name (gnutls_protocol_get_version
601 (session)));
603 /* see the Getting peer's information example */
604 if (debug)
605 print_info (session);
607 i = 0;
608 for (;;)
610 memset (buffer, 0, MAX_BUF + 1);
611 ret = gnutls_record_recv (session, buffer, MAX_BUF);
613 if (ret == 0)
615 if (debug)
616 success ("server: Peer has closed the GnuTLS connection\n");
617 break;
619 else if (ret < 0)
621 fail ("server: Received corrupted data(%d). Closing...\n", ret);
622 goto end;
624 else if (ret > 0)
626 /* echo data back to the client
628 gnutls_record_send (session, buffer, strlen (buffer));
631 /* do not wait for the peer to close the connection.
633 gnutls_bye (session, GNUTLS_SHUT_WR);
635 close (sd);
636 gnutls_deinit (session);
639 end:
640 close (listen_sd);
642 gnutls_certificate_free_credentials (pgp_cred);
644 gnutls_dh_params_deinit (dh_params);
646 gnutls_global_deinit ();
648 if (debug)
649 success ("server: finished\n");
653 void
654 doit (void)
656 server_start ();
657 if (error_count)
658 return;
660 child = fork ();
661 if (child < 0)
663 perror ("fork");
664 fail ("fork");
665 return;
668 if (child)
670 int status;
671 /* parent */
672 server ();
673 wait (&status);
675 else
676 client ();