remove p11-kit in gnutls.spec file
[platform/upstream/gnutls.git] / tests / openpgp-auth2.c
1 /*
2  * Copyright (C) 2010-2012 Free Software Foundation, Inc.
3  * Author: Ludovic Courtès
4  *
5  * This file is part of GNUTLS.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #if !defined(_WIN32)
27
28 #include <gnutls/gnutls.h>
29 #include <gnutls/openpgp.h>
30
31 #include "utils.h"
32 #include <read-file.h>
33
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/wait.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <stdio.h>
42
43
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.
47  */
48
49 static const char message[] = "Hello, brave GNU world!";
50
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.  */
56     /* "auto" */
57     /* "bd572cdcccc07c35" */ ;
58
59 static void log_message(int level, const char *message)
60 {
61         fprintf(stderr, "[%5d|%2d] %s", getpid(), level, message);
62 }
63 \f
64
65 void doit()
66 {
67         int err;
68         int sockets[2];
69         const char *srcdir;
70         char pub_key_path[512], priv_key_path[512];
71         pid_t child;
72
73         global_init();
74
75         srcdir = getenv("srcdir") ? getenv("srcdir") : ".";
76
77         if (debug) {
78                 gnutls_global_set_log_level(10);
79                 gnutls_global_set_log_function(log_message);
80         }
81
82         err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
83         if (err != 0)
84                 fail("socketpair %s\n", strerror(errno));
85
86         if (sizeof(pub_key_path) <
87             strlen(srcdir) + strlen(pub_key_file) + 2)
88                 abort();
89
90         strcpy(pub_key_path, srcdir);
91         strcat(pub_key_path, "/");
92         strcat(pub_key_path, pub_key_file);
93
94         if (sizeof(priv_key_path) <
95             strlen(srcdir) + strlen(priv_key_file) + 2)
96                 abort();
97
98         strcpy(priv_key_path, srcdir);
99         strcat(priv_key_path, "/");
100         strcat(priv_key_path, priv_key_file);
101
102         child = fork();
103         if (child == -1)
104                 fail("fork %s\n", strerror(errno));
105
106         if (child == 0) {
107                 /* Child process (client).  */
108                 gnutls_session_t session;
109                 gnutls_certificate_credentials_t cred;
110                 ssize_t sent;
111
112                 if (debug)
113                         printf("client process %i\n", getpid());
114
115                 err = gnutls_init(&session, GNUTLS_CLIENT);
116                 if (err != 0)
117                         fail("client session %d\n", err);
118
119                 gnutls_priority_set_direct(session,
120                                            "NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP",
121                                            NULL);
122                 gnutls_transport_set_int(session, sockets[0]);
123
124                 err = gnutls_certificate_allocate_credentials(&cred);
125                 if (err != 0)
126                         fail("client credentials %d\n", err);
127
128                 err =
129                     gnutls_certificate_set_openpgp_key_file2(cred,
130                                                              pub_key_path,
131                                                              priv_key_path,
132                                                              key_id,
133                                                              GNUTLS_OPENPGP_FMT_BASE64);
134                 if (err != 0)
135                         fail("client openpgp keys %d\n", err);
136
137                 err =
138                     gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
139                                            cred);
140                 if (err != 0)
141                         fail("client credential_set %d\n", err);
142
143                 gnutls_dh_set_prime_bits(session, 1024);
144
145                 err = gnutls_handshake(session);
146                 if (err != 0)
147                         fail("client handshake %s (%d) \n",
148                              gnutls_strerror(err), err);
149                 else if (debug)
150                         printf("client handshake successful\n");
151
152                 sent =
153                     gnutls_record_send(session, message, sizeof(message));
154                 if (sent != sizeof(message))
155                         fail("client sent %li vs. %li\n",
156                              (long) sent, (long) sizeof(message));
157
158                 err = gnutls_bye(session, GNUTLS_SHUT_RDWR);
159                 if (err != 0)
160                         fail("client bye %d\n", err);
161
162                 if (debug)
163                         printf("client done\n");
164
165                 gnutls_deinit(session);
166                 gnutls_certificate_free_credentials(cred);
167         } else {
168                 /* Parent process (server).  */
169                 gnutls_session_t session;
170                 gnutls_dh_params_t dh_params;
171                 gnutls_certificate_credentials_t cred;
172                 char greetings[sizeof(message) * 2];
173                 ssize_t received;
174                 pid_t done;
175                 int status;
176                 const gnutls_datum_t p3 =
177                     { (void *) pkcs3, strlen(pkcs3) };
178
179                 if (debug)
180                         printf("server process %i (child %i)\n", getpid(),
181                                child);
182
183                 err = gnutls_init(&session, GNUTLS_SERVER);
184                 if (err != 0)
185                         fail("server session %d\n", err);
186
187                 gnutls_priority_set_direct(session,
188                                            "NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+DHE-DSS:+DHE-RSA:+CTYPE-OPENPGP",
189                                            NULL);
190                 gnutls_transport_set_int(session, sockets[1]);
191
192                 err = gnutls_certificate_allocate_credentials(&cred);
193                 if (err != 0)
194                         fail("server credentials %d\n", err);
195
196                 err =
197                     gnutls_certificate_set_openpgp_key_file2(cred,
198                                                              pub_key_path,
199                                                              priv_key_path,
200                                                              key_id,
201                                                              GNUTLS_OPENPGP_FMT_BASE64);
202                 if (err != 0)
203                         fail("server openpgp keys %d\n", err);
204
205                 err = gnutls_dh_params_init(&dh_params);
206                 if (err)
207                         fail("server DH params init %d\n", err);
208
209                 err =
210                     gnutls_dh_params_import_pkcs3(dh_params, &p3,
211                                                   GNUTLS_X509_FMT_PEM);
212                 if (err)
213                         fail("server DH params generate %d\n", err);
214
215                 gnutls_certificate_set_dh_params(cred, dh_params);
216
217                 err =
218                     gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
219                                            cred);
220                 if (err != 0)
221                         fail("server credential_set %d\n", err);
222
223                 gnutls_certificate_server_set_request(session,
224                                                       GNUTLS_CERT_REQUIRE);
225
226                 err = gnutls_handshake(session);
227                 if (err != 0)
228                         fail("server handshake %s (%d) \n",
229                              gnutls_strerror(err), err);
230
231                 received =
232                     gnutls_record_recv(session, greetings,
233                                        sizeof(greetings));
234                 if (received != sizeof(message)
235                     || memcmp(greetings, message, sizeof(message)))
236                         fail("server received %li vs. %li\n",
237                              (long) received, (long) sizeof(message));
238
239                 err = gnutls_bye(session, GNUTLS_SHUT_RDWR);
240                 if (err != 0)
241                         fail("server bye %s (%d) \n", gnutls_strerror(err),
242                              err);
243
244                 if (debug)
245                         printf("server done\n");
246
247                 gnutls_deinit(session);
248                 gnutls_certificate_free_credentials(cred);
249                 gnutls_dh_params_deinit(dh_params);
250
251                 done = wait(&status);
252                 if (done < 0)
253                         fail("wait %s\n", strerror(errno));
254
255                 if (done != child)
256                         fail("who's that?! %d\n", done);
257
258                 if (WIFEXITED(status)) {
259                         if (WEXITSTATUS(status) != 0)
260                                 fail("child exited with status %d\n",
261                                      WEXITSTATUS(status));
262                 } else if (WIFSIGNALED(status))
263                         fail("child stopped by signal %d\n",
264                              WTERMSIG(status));
265                 else
266                         fail("child failed: %d\n", status);
267         }
268
269         gnutls_global_deinit();
270 }
271 #else
272 #include <stdlib.h>
273
274 void doit()
275 {
276         exit(77);
277 }
278 #endif