Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / gtlscertificate.c
1 /* GIO - GLib Input, Output and Certificateing Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
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.1 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
19 #include "config.h"
20
21 #include "gtlscertificate.h"
22
23 #include <string.h>
24 #include "ginitable.h"
25 #include "gtlsbackend.h"
26 #include "gtlsconnection.h"
27 #include "glibintl.h"
28
29 /**
30  * SECTION:gtlscertificate
31  * @title: GTlsCertificate
32  * @short_description: TLS certificate
33  * @include: gio/gio.h
34  * @see_also: #GTlsConnection
35  *
36  * A certificate used for TLS authentication and encryption.
37  * This can represent either a certificate only (eg, the certificate
38  * received by a client from a server), or the combination of
39  * a certificate and a private key (which is needed when acting as a
40  * #GTlsServerConnection).
41  *
42  * Since: 2.28
43  */
44
45 /**
46  * GTlsCertificate:
47  *
48  * Abstract base class for TLS certificate types.
49  *
50  * Since: 2.28
51  */
52
53 G_DEFINE_ABSTRACT_TYPE (GTlsCertificate, g_tls_certificate, G_TYPE_OBJECT)
54
55 enum
56 {
57   PROP_0,
58
59   PROP_CERTIFICATE,
60   PROP_CERTIFICATE_PEM,
61   PROP_PRIVATE_KEY,
62   PROP_PRIVATE_KEY_PEM,
63   PROP_ISSUER
64 };
65
66 static void
67 g_tls_certificate_init (GTlsCertificate *cert)
68 {
69 }
70
71 static void
72 g_tls_certificate_get_property (GObject    *object,
73                                 guint       prop_id,
74                                 GValue     *value,
75                                 GParamSpec *pspec)
76 {
77   G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
78 }
79
80 static void
81 g_tls_certificate_set_property (GObject      *object,
82                                 guint         prop_id,
83                                 const GValue *value,
84                                 GParamSpec   *pspec)
85 {
86   G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 }
88
89 static void
90 g_tls_certificate_class_init (GTlsCertificateClass *class)
91 {
92   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
93
94   gobject_class->set_property = g_tls_certificate_set_property;
95   gobject_class->get_property = g_tls_certificate_get_property;
96
97   /**
98    * GTlsCertificate:certificate:
99    *
100    * The DER (binary) encoded representation of the certificate.
101    * This property and the #GTlsCertificate:certificate-pem property
102    * represent the same data, just in different forms.
103    *
104    * Since: 2.28
105    */
106   g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
107                                    g_param_spec_boxed ("certificate",
108                                                        P_("Certificate"),
109                                                        P_("The DER representation of the certificate"),
110                                                        G_TYPE_BYTE_ARRAY,
111                                                        G_PARAM_READWRITE |
112                                                        G_PARAM_CONSTRUCT_ONLY |
113                                                        G_PARAM_STATIC_STRINGS));
114   /**
115    * GTlsCertificate:certificate-pem:
116    *
117    * The PEM (ASCII) encoded representation of the certificate.
118    * This property and the #GTlsCertificate:certificate
119    * property represent the same data, just in different forms.
120    *
121    * Since: 2.28
122    */
123   g_object_class_install_property (gobject_class, PROP_CERTIFICATE_PEM,
124                                    g_param_spec_string ("certificate-pem",
125                                                         P_("Certificate (PEM)"),
126                                                         P_("The PEM representation of the certificate"),
127                                                         NULL,
128                                                         G_PARAM_READWRITE |
129                                                         G_PARAM_CONSTRUCT_ONLY |
130                                                         G_PARAM_STATIC_STRINGS));
131   /**
132    * GTlsCertificate:private-key:
133    *
134    * The DER (binary) encoded representation of the certificate's
135    * private key, in either PKCS#1 format or unencrypted PKCS#8
136    * format. This property (or the #GTlsCertificate:private-key-pem
137    * property) can be set when constructing a key (eg, from a file),
138    * but cannot be read.
139    *
140    * PKCS#8 format is supported since 2.32; earlier releases only
141    * support PKCS#1. You can use the `openssl rsa`
142    * tool to convert PKCS#8 keys to PKCS#1.
143    *
144    * Since: 2.28
145    */
146   g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY,
147                                    g_param_spec_boxed ("private-key",
148                                                        P_("Private key"),
149                                                        P_("The DER representation of the certificate’s private key"),
150                                                        G_TYPE_BYTE_ARRAY,
151                                                        G_PARAM_WRITABLE |
152                                                        G_PARAM_CONSTRUCT_ONLY |
153                                                        G_PARAM_STATIC_STRINGS));
154   /**
155    * GTlsCertificate:private-key-pem:
156    *
157    * The PEM (ASCII) encoded representation of the certificate's
158    * private key in either PKCS#1 format ("`BEGIN RSA PRIVATE
159    * KEY`") or unencrypted PKCS#8 format ("`BEGIN
160    * PRIVATE KEY`"). This property (or the
161    * #GTlsCertificate:private-key property) can be set when
162    * constructing a key (eg, from a file), but cannot be read.
163    *
164    * PKCS#8 format is supported since 2.32; earlier releases only
165    * support PKCS#1. You can use the `openssl rsa`
166    * tool to convert PKCS#8 keys to PKCS#1.
167    *
168    * Since: 2.28
169    */
170   g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY_PEM,
171                                    g_param_spec_string ("private-key-pem",
172                                                         P_("Private key (PEM)"),
173                                                         P_("The PEM representation of the certificate’s private key"),
174                                                         NULL,
175                                                         G_PARAM_WRITABLE |
176                                                         G_PARAM_CONSTRUCT_ONLY |
177                                                         G_PARAM_STATIC_STRINGS));
178   /**
179    * GTlsCertificate:issuer:
180    *
181    * A #GTlsCertificate representing the entity that issued this
182    * certificate. If %NULL, this means that the certificate is either
183    * self-signed, or else the certificate of the issuer is not
184    * available.
185    *
186    * Since: 2.28
187    */
188   g_object_class_install_property (gobject_class, PROP_ISSUER,
189                                    g_param_spec_object ("issuer",
190                                                         P_("Issuer"),
191                                                         P_("The certificate for the issuing entity"),
192                                                         G_TYPE_TLS_CERTIFICATE,
193                                                         G_PARAM_READWRITE |
194                                                         G_PARAM_CONSTRUCT_ONLY |
195                                                         G_PARAM_STATIC_STRINGS));
196 }
197
198 static GTlsCertificate *
199 g_tls_certificate_new_internal (const gchar      *certificate_pem,
200                                 const gchar      *private_key_pem,
201                                 GTlsCertificate  *issuer,
202                                 GError          **error)
203 {
204   GObject *cert;
205   GTlsBackend *backend;
206
207   backend = g_tls_backend_get_default ();
208
209   cert = g_initable_new (g_tls_backend_get_certificate_type (backend),
210                          NULL, error,
211                          "certificate-pem", certificate_pem,
212                          "private-key-pem", private_key_pem,
213                          "issuer", issuer,
214                          NULL);
215
216   return G_TLS_CERTIFICATE (cert);
217 }
218
219 #define PEM_CERTIFICATE_HEADER     "-----BEGIN CERTIFICATE-----"
220 #define PEM_CERTIFICATE_FOOTER     "-----END CERTIFICATE-----"
221 #define PEM_PRIVKEY_HEADER_BEGIN   "-----BEGIN "
222 #define PEM_PRIVKEY_HEADER_END     "PRIVATE KEY-----"
223 #define PEM_PRIVKEY_FOOTER_BEGIN   "-----END "
224 #define PEM_PRIVKEY_FOOTER_END     "PRIVATE KEY-----"
225 #define PEM_PKCS8_ENCRYPTED_HEADER "-----BEGIN ENCRYPTED PRIVATE KEY-----"
226
227 static gchar *
228 parse_private_key (const gchar *data,
229                    gsize data_len,
230                    gboolean required,
231                    GError **error)
232 {
233   const gchar *header_start = NULL, *header_end, *footer_start = NULL, *footer_end;
234
235   header_end = g_strstr_len (data, data_len, PEM_PRIVKEY_HEADER_END);
236   if (header_end)
237     header_start = g_strrstr_len (data, header_end - data, PEM_PRIVKEY_HEADER_BEGIN);
238
239   if (!header_start)
240     {
241       if (required)
242         g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
243                              _("No PEM-encoded private key found"));
244
245       return NULL;
246     }
247
248   header_end += strlen (PEM_PRIVKEY_HEADER_END);
249
250   if (strncmp (header_start, PEM_PKCS8_ENCRYPTED_HEADER, header_end - header_start) == 0)
251     {
252       g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
253                            _("Cannot decrypt PEM-encoded private key"));
254       return NULL;
255     }
256
257   footer_end = g_strstr_len (header_end, data_len - (header_end - data), PEM_PRIVKEY_FOOTER_END);
258   if (footer_end)
259     footer_start = g_strrstr_len (header_end, footer_end - header_end, PEM_PRIVKEY_FOOTER_BEGIN);
260
261   if (!footer_start)
262     {
263       g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
264                            _("Could not parse PEM-encoded private key"));
265       return NULL;
266     }
267
268   footer_end += strlen (PEM_PRIVKEY_FOOTER_END);
269
270   while (*footer_end == '\r' || *footer_end == '\n')
271     footer_end++;
272
273   return g_strndup (header_start, footer_end - header_start);
274 }
275
276
277 static gchar *
278 parse_next_pem_certificate (const gchar **data,
279                             const gchar  *data_end,
280                             gboolean      required,
281                             GError      **error)
282 {
283   const gchar *start, *end;
284
285   start = g_strstr_len (*data, data_end - *data, PEM_CERTIFICATE_HEADER);
286   if (!start)
287     {
288       if (required)
289         {
290           g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
291                                _("No PEM-encoded certificate found"));
292         }
293       return NULL;
294     }
295
296   end = g_strstr_len (start, data_end - start, PEM_CERTIFICATE_FOOTER);
297   if (!end)
298     {
299       g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
300                            _("Could not parse PEM-encoded certificate"));
301       return NULL;
302     }
303   end += strlen (PEM_CERTIFICATE_FOOTER);
304   while (*end == '\r' || *end == '\n')
305     end++;
306
307   *data = end;
308
309   return g_strndup (start, end - start);
310 }
311
312 static GSList *
313 parse_and_create_certificate_list (const gchar  *data,
314                                    gsize         data_len,
315                                    GError      **error)
316 {
317   GSList *first_pem_list = NULL, *pem_list = NULL;
318   gchar *first_pem;
319   const gchar *p, *end;
320
321   p = data;
322   end = p + data_len;
323
324   /* Make sure we can load, at least, one certificate. */
325   first_pem = parse_next_pem_certificate (&p, end, TRUE, error);
326   if (!first_pem)
327     return NULL;
328
329   /* Create a list with a single element. If we load more certificates
330    * below, we will concatenate the two lists at the end. */
331   first_pem_list = g_slist_prepend (first_pem_list, first_pem);
332
333   /* If we read one certificate successfully, let's see if we can read
334    * some more. If not, we will simply return a list with the first one.
335    */
336   while (p && *p)
337     {
338       gchar *cert_pem;
339       GError *error = NULL;
340
341       cert_pem = parse_next_pem_certificate (&p, end, FALSE, &error);
342       if (error)
343         {
344           g_slist_free_full (pem_list, g_free);
345           g_error_free (error);
346           return first_pem_list;
347         }
348       else if (!cert_pem)
349         {
350           break;
351         }
352
353       pem_list = g_slist_prepend (pem_list, cert_pem);
354     }
355
356   pem_list = g_slist_concat (pem_list, first_pem_list);
357
358   return pem_list;
359 }
360
361 static GTlsCertificate *
362 create_certificate_chain_from_list (GSList       *pem_list,
363                                     const gchar  *key_pem)
364 {
365   GTlsCertificate *cert = NULL, *issuer = NULL, *root = NULL;
366   GTlsCertificateFlags flags;
367   GSList *pem;
368
369   pem = pem_list;
370   while (pem)
371     {
372       const gchar *key = NULL;
373
374       /* Private key belongs only to the first certificate. */
375       if (!pem->next)
376         key = key_pem;
377
378       /* We assume that the whole file is a certificate chain, so we use
379        * each certificate as the issuer of the next one (list is in
380        * reverse order).
381        */
382       issuer = cert;
383       cert = g_tls_certificate_new_internal (pem->data, key, issuer, NULL);
384       if (issuer)
385         g_object_unref (issuer);
386
387       if (!cert)
388         return NULL;
389
390       /* root will point to the last certificate in the file. */
391       if (!root)
392         root = cert;
393
394       pem = g_slist_next (pem);
395     }
396
397   /* Verify that the certificates form a chain. (We don't care at this
398    * point if there are other problems with it.)
399    */
400   flags = g_tls_certificate_verify (cert, NULL, root);
401   if (flags & G_TLS_CERTIFICATE_UNKNOWN_CA)
402     {
403       /* It wasn't a chain, it's just a bunch of unrelated certs. */
404       g_clear_object (&cert);
405     }
406
407   return cert;
408 }
409
410 static GTlsCertificate *
411 parse_and_create_certificate (const gchar  *data,
412                               gsize         data_len,
413                               const gchar  *key_pem,
414                               GError      **error)
415
416 {
417   GSList *pem_list;
418   GTlsCertificate *cert;
419
420   pem_list = parse_and_create_certificate_list (data, data_len, error);
421   if (!pem_list)
422     return NULL;
423
424   /* We don't pass the error here because, if it fails, we still want to
425    * load and return the first certificate.
426    */
427   cert = create_certificate_chain_from_list (pem_list, key_pem);
428   if (!cert)
429     {
430       GSList *last = NULL;
431
432       /* Get the first certificate (which is the last one as the list is
433        * in reverse order).
434        */
435       last = g_slist_last (pem_list);
436
437       cert = g_tls_certificate_new_internal (last->data, key_pem, NULL, error);
438     }
439
440   g_slist_free_full (pem_list, g_free);
441
442   return cert;
443 }
444
445 /**
446  * g_tls_certificate_new_from_pem:
447  * @data: PEM-encoded certificate data
448  * @length: the length of @data, or -1 if it's 0-terminated.
449  * @error: #GError for error reporting, or %NULL to ignore.
450  *
451  * Creates a #GTlsCertificate from the PEM-encoded data in @data. If
452  * @data includes both a certificate and a private key, then the
453  * returned certificate will include the private key data as well. (See
454  * the #GTlsCertificate:private-key-pem property for information about
455  * supported formats.)
456  *
457  * The returned certificate will be the first certificate found in
458  * @data. As of GLib 2.44, if @data contains more certificates it will
459  * try to load a certificate chain. All certificates will be verified in
460  * the order found (top-level certificate should be the last one in the
461  * file) and the #GTlsCertificate:issuer property of each certificate
462  * will be set accordingly if the verification succeeds. If any
463  * certificate in the chain cannot be verified, the first certificate in
464  * the file will still be returned.
465  *
466  * Returns: the new certificate, or %NULL if @data is invalid
467  *
468  * Since: 2.28
469  */
470 GTlsCertificate *
471 g_tls_certificate_new_from_pem  (const gchar  *data,
472                                  gssize        length,
473                                  GError      **error)
474 {
475   GError *child_error = NULL;
476   gchar *key_pem;
477   GTlsCertificate *cert;
478
479   g_return_val_if_fail (data != NULL, NULL);
480   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
481
482   if (length == -1)
483     length = strlen (data);
484
485   key_pem = parse_private_key (data, length, FALSE, &child_error);
486   if (child_error != NULL)
487     {
488       g_propagate_error (error, child_error);
489       return NULL;
490     }
491
492   cert = parse_and_create_certificate (data, length, key_pem, error);
493   g_free (key_pem);
494
495   return cert;
496 }
497
498 /**
499  * g_tls_certificate_new_from_file:
500  * @file: (type filename): file containing a PEM-encoded certificate to import
501  * @error: #GError for error reporting, or %NULL to ignore.
502  *
503  * Creates a #GTlsCertificate from the PEM-encoded data in @file. The
504  * returned certificate will be the first certificate found in @file. As
505  * of GLib 2.44, if @file contains more certificates it will try to load
506  * a certificate chain. All certificates will be verified in the order
507  * found (top-level certificate should be the last one in the file) and
508  * the #GTlsCertificate:issuer property of each certificate will be set
509  * accordingly if the verification succeeds. If any certificate in the
510  * chain cannot be verified, the first certificate in the file will
511  * still be returned.
512  *
513  * If @file cannot be read or parsed, the function will return %NULL and
514  * set @error. Otherwise, this behaves like
515  * g_tls_certificate_new_from_pem().
516  *
517  * Returns: the new certificate, or %NULL on error
518  *
519  * Since: 2.28
520  */
521 GTlsCertificate *
522 g_tls_certificate_new_from_file (const gchar  *file,
523                                  GError      **error)
524 {
525   GTlsCertificate *cert;
526   gchar *contents;
527   gsize length;
528
529   if (!g_file_get_contents (file, &contents, &length, error))
530     return NULL;
531
532   cert = g_tls_certificate_new_from_pem (contents, length, error);
533   g_free (contents);
534   return cert;
535 }
536
537 /**
538  * g_tls_certificate_new_from_files:
539  * @cert_file: (type filename): file containing one or more PEM-encoded
540  *     certificates to import
541  * @key_file: (type filename): file containing a PEM-encoded private key
542  *     to import
543  * @error: #GError for error reporting, or %NULL to ignore.
544  *
545  * Creates a #GTlsCertificate from the PEM-encoded data in @cert_file
546  * and @key_file. The returned certificate will be the first certificate
547  * found in @cert_file. As of GLib 2.44, if @cert_file contains more
548  * certificates it will try to load a certificate chain. All
549  * certificates will be verified in the order found (top-level
550  * certificate should be the last one in the file) and the
551  * #GTlsCertificate:issuer property of each certificate will be set
552  * accordingly if the verification succeeds. If any certificate in the
553  * chain cannot be verified, the first certificate in the file will
554  * still be returned.
555  *
556  * If either file cannot be read or parsed, the function will return
557  * %NULL and set @error. Otherwise, this behaves like
558  * g_tls_certificate_new_from_pem().
559  *
560  * Returns: the new certificate, or %NULL on error
561  *
562  * Since: 2.28
563  */
564 GTlsCertificate *
565 g_tls_certificate_new_from_files (const gchar  *cert_file,
566                                   const gchar  *key_file,
567                                   GError      **error)
568 {
569   GTlsCertificate *cert;
570   gchar *cert_data, *key_data;
571   gsize cert_len, key_len;
572   gchar *key_pem;
573
574   if (!g_file_get_contents (key_file, &key_data, &key_len, error))
575     return NULL;
576
577   key_pem = parse_private_key (key_data, key_len, TRUE, error);
578   g_free (key_data);
579   if (!key_pem)
580     return NULL;
581
582   if (!g_file_get_contents (cert_file, &cert_data, &cert_len, error))
583     {
584       g_free (key_pem);
585       return NULL;
586     }
587
588   cert = parse_and_create_certificate (cert_data, cert_len, key_pem, error);
589   g_free (cert_data);
590   g_free (key_pem);
591   return cert;
592 }
593
594 /**
595  * g_tls_certificate_list_new_from_file:
596  * @file: (type filename): file containing PEM-encoded certificates to import
597  * @error: #GError for error reporting, or %NULL to ignore.
598  *
599  * Creates one or more #GTlsCertificates from the PEM-encoded
600  * data in @file. If @file cannot be read or parsed, the function will
601  * return %NULL and set @error. If @file does not contain any
602  * PEM-encoded certificates, this will return an empty list and not
603  * set @error.
604  *
605  * Returns: (element-type Gio.TlsCertificate) (transfer full): a
606  * #GList containing #GTlsCertificate objects. You must free the list
607  * and its contents when you are done with it.
608  *
609  * Since: 2.28
610  */
611 GList *
612 g_tls_certificate_list_new_from_file (const gchar  *file,
613                                       GError      **error)
614 {
615   GQueue queue = G_QUEUE_INIT;
616   gchar *contents, *end;
617   const gchar *p;
618   gsize length;
619
620   if (!g_file_get_contents (file, &contents, &length, error))
621     return NULL;
622
623   end = contents + length;
624   p = contents;
625   while (p && *p)
626     {
627       gchar *cert_pem;
628       GTlsCertificate *cert = NULL;
629       GError *parse_error = NULL;
630
631       cert_pem = parse_next_pem_certificate (&p, end, FALSE, &parse_error);
632       if (cert_pem)
633         {
634           cert = g_tls_certificate_new_internal (cert_pem, NULL, NULL, &parse_error);
635           g_free (cert_pem);
636         }
637       if (!cert)
638         {
639           if (parse_error)
640             {
641               g_propagate_error (error, parse_error);
642               g_list_free_full (queue.head, g_object_unref);
643               queue.head = NULL;
644             }
645           break;
646         }
647       g_queue_push_tail (&queue, cert);
648     }
649
650   g_free (contents);
651   return queue.head;
652 }
653
654
655 /**
656  * g_tls_certificate_get_issuer:
657  * @cert: a #GTlsCertificate
658  *
659  * Gets the #GTlsCertificate representing @cert's issuer, if known
660  *
661  * Returns: (transfer none): The certificate of @cert's issuer,
662  * or %NULL if @cert is self-signed or signed with an unknown
663  * certificate.
664  *
665  * Since: 2.28
666  */
667 GTlsCertificate *
668 g_tls_certificate_get_issuer (GTlsCertificate  *cert)
669 {
670   GTlsCertificate *issuer;
671
672   g_object_get (G_OBJECT (cert), "issuer", &issuer, NULL);
673   if (issuer)
674     g_object_unref (issuer);
675
676   return issuer;
677 }
678
679 /**
680  * g_tls_certificate_verify:
681  * @cert: a #GTlsCertificate
682  * @identity: (nullable): the expected peer identity
683  * @trusted_ca: (nullable): the certificate of a trusted authority
684  *
685  * This verifies @cert and returns a set of #GTlsCertificateFlags
686  * indicating any problems found with it. This can be used to verify a
687  * certificate outside the context of making a connection, or to
688  * check a certificate against a CA that is not part of the system
689  * CA database.
690  *
691  * If @identity is not %NULL, @cert's name(s) will be compared against
692  * it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return
693  * value if it does not match. If @identity is %NULL, that bit will
694  * never be set in the return value.
695  *
696  * If @trusted_ca is not %NULL, then @cert (or one of the certificates
697  * in its chain) must be signed by it, or else
698  * %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If
699  * @trusted_ca is %NULL, that bit will never be set in the return
700  * value.
701  *
702  * (All other #GTlsCertificateFlags values will always be set or unset
703  * as appropriate.)
704  *
705  * Returns: the appropriate #GTlsCertificateFlags
706  *
707  * Since: 2.28
708  */
709 GTlsCertificateFlags
710 g_tls_certificate_verify (GTlsCertificate     *cert,
711                           GSocketConnectable  *identity,
712                           GTlsCertificate     *trusted_ca)
713 {
714   return G_TLS_CERTIFICATE_GET_CLASS (cert)->verify (cert, identity, trusted_ca);
715 }
716
717 /**
718  * g_tls_certificate_is_same:
719  * @cert_one: first certificate to compare
720  * @cert_two: second certificate to compare
721  *
722  * Check if two #GTlsCertificate objects represent the same certificate.
723  * The raw DER byte data of the two certificates are checked for equality.
724  * This has the effect that two certificates may compare equal even if
725  * their #GTlsCertificate:issuer, #GTlsCertificate:private-key, or
726  * #GTlsCertificate:private-key-pem properties differ.
727  *
728  * Returns: whether the same or not
729  *
730  * Since: 2.34
731  */
732 gboolean
733 g_tls_certificate_is_same (GTlsCertificate     *cert_one,
734                            GTlsCertificate     *cert_two)
735 {
736   GByteArray *b1, *b2;
737   gboolean equal;
738
739   g_return_val_if_fail (G_IS_TLS_CERTIFICATE (cert_one), FALSE);
740   g_return_val_if_fail (G_IS_TLS_CERTIFICATE (cert_two), FALSE);
741
742   g_object_get (cert_one, "certificate", &b1, NULL);
743   g_object_get (cert_two, "certificate", &b2, NULL);
744
745   equal = (b1->len == b2->len &&
746            memcmp (b1->data, b2->data, b1->len) == 0);
747
748   g_byte_array_unref (b1);
749   g_byte_array_unref (b2);
750
751   return equal;
752 }