[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / tls-certificate.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2011 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
19  */
20
21 #include "config.h"
22
23 #include <gio/gio.h>
24
25 #include "gtesttlsbackend.h"
26
27 typedef struct
28 {
29   gchar *cert_pems[3];
30   gchar *key_pem;
31   gchar *key8_pem;
32 } Reference;
33
34 static void
35 pem_parser (const Reference *ref)
36 {
37   GTlsCertificate *cert;
38   gchar *pem;
39   gchar *parsed_cert_pem = NULL;
40   const gchar *parsed_key_pem = NULL;
41   GError *error = NULL;
42
43   /* Check PEM parsing in certificate, private key order. */
44   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert-key.pem", NULL), &pem, NULL, &error);
45   g_assert_no_error (error);
46   g_assert (pem);
47
48   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
49   g_assert_no_error (error);
50   g_assert (cert);
51
52   g_object_get (cert,
53       "certificate-pem", &parsed_cert_pem,
54       NULL);
55   parsed_key_pem = g_test_tls_connection_get_private_key_pem (cert);
56   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
57   g_free (parsed_cert_pem);
58   parsed_cert_pem = NULL;
59   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
60   parsed_key_pem = NULL;
61
62   g_object_unref (cert);
63
64   /* Make sure length is respected and parser detect invalid (truncated) PEM. */
65   cert = g_tls_certificate_new_from_pem (pem, 10, &error);
66   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
67   g_clear_error (&error);
68   g_free (pem);
69
70   /* Check PEM parsing in private key, certificate order */
71   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL), &pem, NULL, &error);
72   g_assert_no_error (error);
73   g_assert (pem);
74
75   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
76   g_assert_no_error (error);
77   g_assert (cert);
78
79   g_object_get (cert,
80       "certificate-pem", &parsed_cert_pem,
81       NULL);
82   parsed_key_pem = g_test_tls_connection_get_private_key_pem (cert);
83   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
84   g_free (parsed_cert_pem);
85   parsed_cert_pem = NULL;
86   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
87   parsed_key_pem = NULL;
88
89   g_free (pem);
90   g_object_unref (cert);
91
92   /* Check certificate only PEM */
93   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL), &pem, NULL, &error);
94   g_assert_no_error (error);
95   g_assert (pem);
96
97   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
98   g_assert_no_error (error);
99   g_assert (cert);
100
101   g_object_get (cert,
102       "certificate-pem", &parsed_cert_pem,
103       NULL);
104   parsed_key_pem = g_test_tls_connection_get_private_key_pem (cert);
105   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
106   g_free (parsed_cert_pem);
107   parsed_cert_pem = NULL;
108   g_assert (parsed_key_pem == NULL);
109
110   g_free (pem);
111   g_object_unref (cert);
112
113   /* Check error with private key only PEM */
114   g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL), &pem, NULL, &error);
115   g_assert_no_error (error);
116   g_assert (pem);
117
118   cert = g_tls_certificate_new_from_pem (pem, -1, &error);
119   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
120   g_clear_error (&error);
121   g_assert (cert == NULL);
122   g_free (pem);
123 }
124
125 static void
126 from_file (const Reference *ref)
127 {
128   GTlsCertificate *cert;
129   gchar *parsed_cert_pem = NULL;
130   const gchar *parsed_key_pem = NULL;
131   GError *error = NULL;
132
133   cert = g_tls_certificate_new_from_file (g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL),
134                                           &error);
135   g_assert_no_error (error);
136   g_assert (cert);
137
138   g_object_get (cert,
139       "certificate-pem", &parsed_cert_pem,
140       NULL);
141   parsed_key_pem = g_test_tls_connection_get_private_key_pem (cert);
142   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
143   g_free (parsed_cert_pem);
144   parsed_cert_pem = NULL;
145   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
146   parsed_key_pem = NULL;
147
148   g_object_unref (cert);
149 }
150
151 static void
152 from_files (const Reference *ref)
153 {
154   GTlsCertificate *cert;
155   gchar *parsed_cert_pem = NULL;
156   const gchar *parsed_key_pem = NULL;
157   GError *error = NULL;
158
159   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
160                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL),
161                                            &error);
162   g_assert_no_error (error);
163   g_assert (cert);
164
165   g_object_get (cert,
166       "certificate-pem", &parsed_cert_pem,
167       NULL);
168   parsed_key_pem = g_test_tls_connection_get_private_key_pem (cert);
169   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
170   g_free (parsed_cert_pem);
171   parsed_cert_pem = NULL;
172   g_assert_cmpstr (parsed_key_pem, ==, ref->key_pem);
173   parsed_key_pem = NULL;
174
175   g_object_unref (cert);
176
177   /* Missing private key */
178   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
179                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "cert2.pem", NULL),
180                                            &error);
181   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
182   g_clear_error (&error);
183   g_assert (cert == NULL);
184
185   /* Missing certificate */
186   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL),
187                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL),
188                                            &error);
189   g_assert_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE);
190   g_clear_error (&error);
191   g_assert (cert == NULL);
192
193   /* Using this method twice with a file containing both private key and
194    * certificate as a way to inforce private key presence is a fair use
195    */
196   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL),
197                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key-cert.pem", NULL),
198                                            &error);
199   g_assert_no_error (error);
200   g_assert (cert);
201   g_object_unref (cert);
202 }
203
204
205 static void
206 from_files_pkcs8 (const Reference *ref)
207 {
208   GTlsCertificate *cert;
209   gchar *parsed_cert_pem = NULL;
210   const gchar *parsed_key_pem = NULL;
211   GError *error = NULL;
212
213   cert = g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL),
214                                            g_test_get_filename (G_TEST_DIST, "cert-tests", "key8.pem", NULL),
215                                            &error);
216   g_assert_no_error (error);
217   g_assert (cert);
218
219   g_object_get (cert,
220       "certificate-pem", &parsed_cert_pem,
221       NULL);
222   parsed_key_pem = g_test_tls_connection_get_private_key_pem (cert);
223   g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[0]);
224   g_free (parsed_cert_pem);
225   parsed_cert_pem = NULL;
226   g_assert_cmpstr (parsed_key_pem, ==, ref->key8_pem);
227   parsed_key_pem = NULL;
228
229   g_object_unref (cert);
230 }
231
232 static void
233 list_from_file (const Reference *ref)
234 {
235   GList *list, *l;
236   GError *error = NULL;
237   int i;
238
239   list = g_tls_certificate_list_new_from_file (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert-list.pem", NULL),
240                                                &error);
241   g_assert_no_error (error);
242   g_assert_cmpint (g_list_length (list), ==, 3);
243
244   l = list;
245   for (i = 0; i < 3; i++)
246     {
247       GTlsCertificate *cert = l->data;
248       gchar *parsed_cert_pem = NULL;
249       g_object_get (cert,
250           "certificate-pem", &parsed_cert_pem,
251           NULL);
252       g_assert_cmpstr (parsed_cert_pem, ==, ref->cert_pems[i]);
253       g_free (parsed_cert_pem);
254       l = g_list_next (l);
255     }
256
257   g_list_free_full (list, g_object_unref);
258
259   /* Empty list is not an error */
260   list = g_tls_certificate_list_new_from_file (g_test_get_filename (G_TEST_DIST, "cert-tests", "nothing.pem", NULL),
261                                                &error);
262   g_assert_no_error (error);
263   g_assert_cmpint (g_list_length (list), ==, 0);
264 }
265
266 int
267 main (int   argc,
268       char *argv[])
269 {
270   int rtv;
271   Reference ref;
272   GError *error = NULL;
273   gchar *path;
274
275   g_test_init (&argc, &argv, NULL);
276
277   _g_test_tls_backend_get_type ();
278
279   /* Load reference PEM */
280   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL);
281   g_file_get_contents (path, &ref.cert_pems[0], NULL, &error);
282   g_assert_no_error (error);
283   g_assert (ref.cert_pems[0]);
284   g_free (path);
285   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert2.pem", NULL);
286   g_file_get_contents (path, &ref.cert_pems[1], NULL, &error);
287   g_assert_no_error (error);
288   g_assert (ref.cert_pems[1]);
289   g_free (path);
290   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert3.pem", NULL);
291   g_file_get_contents (path, &ref.cert_pems[2], NULL, &error);
292   g_assert_no_error (error);
293   g_assert (ref.cert_pems[2]);
294   g_free (path);
295   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "key.pem", NULL);
296   g_file_get_contents (path, &ref.key_pem, NULL, &error);
297   g_assert_no_error (error);
298   g_assert (ref.key_pem);
299   g_free (path);
300   path = g_test_build_filename (G_TEST_DIST, "cert-tests", "key8.pem", NULL);
301   g_file_get_contents (path, &ref.key8_pem, NULL, &error);
302   g_assert_no_error (error);
303   g_assert (ref.key8_pem);
304   g_free (path);
305
306   g_test_add_data_func ("/tls-certificate/pem-parser",
307                         &ref, (GTestDataFunc)pem_parser);
308   g_test_add_data_func ("/tls-certificate/from_file",
309                         &ref, (GTestDataFunc)from_file);
310   g_test_add_data_func ("/tls-certificate/from_files",
311                         &ref, (GTestDataFunc)from_files);
312   g_test_add_data_func ("/tls-certificate/from_files_pkcs8",
313                         &ref, (GTestDataFunc)from_files_pkcs8);
314   g_test_add_data_func ("/tls-certificate/list_from_file",
315                         &ref, (GTestDataFunc)list_from_file);
316
317   rtv = g_test_run();
318
319   g_free (ref.cert_pems[0]);
320   g_free (ref.cert_pems[1]);
321   g_free (ref.cert_pems[2]);
322   g_free (ref.key_pem);
323   g_free (ref.key8_pem);
324
325   return rtv;
326 }