Blame gio/gtlscertificate.c

Packit ae235b
/* GIO - GLib Input, Output and Certificateing Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gtlscertificate.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include "ginitable.h"
Packit ae235b
#include "gtlsbackend.h"
Packit ae235b
#include "gtlsconnection.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtlscertificate
Packit ae235b
 * @title: GTlsCertificate
Packit ae235b
 * @short_description: TLS certificate
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GTlsConnection
Packit ae235b
 *
Packit ae235b
 * A certificate used for TLS authentication and encryption.
Packit ae235b
 * This can represent either a certificate only (eg, the certificate
Packit ae235b
 * received by a client from a server), or the combination of
Packit ae235b
 * a certificate and a private key (which is needed when acting as a
Packit ae235b
 * #GTlsServerConnection).
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTlsCertificate:
Packit ae235b
 *
Packit ae235b
 * Abstract base class for TLS certificate types.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE (GTlsCertificate, g_tls_certificate, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
Packit ae235b
  PROP_CERTIFICATE,
Packit ae235b
  PROP_CERTIFICATE_PEM,
Packit ae235b
  PROP_PRIVATE_KEY,
Packit ae235b
  PROP_PRIVATE_KEY_PEM,
Packit ae235b
  PROP_ISSUER
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_certificate_init (GTlsCertificate *cert)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_certificate_get_property (GObject    *object,
Packit ae235b
				guint       prop_id,
Packit ae235b
				GValue     *value,
Packit ae235b
				GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_certificate_set_property (GObject      *object,
Packit ae235b
				guint         prop_id,
Packit ae235b
				const GValue *value,
Packit ae235b
				GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_tls_certificate_class_init (GTlsCertificateClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  gobject_class->set_property = g_tls_certificate_set_property;
Packit ae235b
  gobject_class->get_property = g_tls_certificate_get_property;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTlsCertificate:certificate:
Packit ae235b
   *
Packit ae235b
   * The DER (binary) encoded representation of the certificate.
Packit ae235b
   * This property and the #GTlsCertificate:certificate-pem property
Packit ae235b
   * represent the same data, just in different forms.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
Packit ae235b
				   g_param_spec_boxed ("certificate",
Packit ae235b
						       P_("Certificate"),
Packit ae235b
						       P_("The DER representation of the certificate"),
Packit ae235b
						       G_TYPE_BYTE_ARRAY,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsCertificate:certificate-pem:
Packit ae235b
   *
Packit ae235b
   * The PEM (ASCII) encoded representation of the certificate.
Packit ae235b
   * This property and the #GTlsCertificate:certificate
Packit ae235b
   * property represent the same data, just in different forms.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_CERTIFICATE_PEM,
Packit ae235b
				   g_param_spec_string ("certificate-pem",
Packit ae235b
							P_("Certificate (PEM)"),
Packit ae235b
							P_("The PEM representation of the certificate"),
Packit ae235b
							NULL,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsCertificate:private-key:
Packit ae235b
   *
Packit ae235b
   * The DER (binary) encoded representation of the certificate's
Packit ae235b
   * private key, in either PKCS#1 format or unencrypted PKCS#8
Packit ae235b
   * format. This property (or the #GTlsCertificate:private-key-pem
Packit ae235b
   * property) can be set when constructing a key (eg, from a file),
Packit ae235b
   * but cannot be read.
Packit ae235b
   *
Packit ae235b
   * PKCS#8 format is supported since 2.32; earlier releases only
Packit ae235b
   * support PKCS#1. You can use the `openssl rsa`
Packit ae235b
   * tool to convert PKCS#8 keys to PKCS#1.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY,
Packit ae235b
				   g_param_spec_boxed ("private-key",
Packit ae235b
						       P_("Private key"),
Packit ae235b
						       P_("The DER representation of the certificate’s private key"),
Packit ae235b
						       G_TYPE_BYTE_ARRAY,
Packit ae235b
						       G_PARAM_WRITABLE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsCertificate:private-key-pem:
Packit ae235b
   *
Packit ae235b
   * The PEM (ASCII) encoded representation of the certificate's
Packit ae235b
   * private key in either PKCS#1 format ("`BEGIN RSA PRIVATE
Packit ae235b
   * KEY`") or unencrypted PKCS#8 format ("`BEGIN
Packit ae235b
   * PRIVATE KEY`"). This property (or the
Packit ae235b
   * #GTlsCertificate:private-key property) can be set when
Packit ae235b
   * constructing a key (eg, from a file), but cannot be read.
Packit ae235b
   *
Packit ae235b
   * PKCS#8 format is supported since 2.32; earlier releases only
Packit ae235b
   * support PKCS#1. You can use the `openssl rsa`
Packit ae235b
   * tool to convert PKCS#8 keys to PKCS#1.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY_PEM,
Packit ae235b
				   g_param_spec_string ("private-key-pem",
Packit ae235b
							P_("Private key (PEM)"),
Packit ae235b
							P_("The PEM representation of the certificate’s private key"),
Packit ae235b
							NULL,
Packit ae235b
							G_PARAM_WRITABLE |
Packit ae235b
							G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
  /**
Packit ae235b
   * GTlsCertificate:issuer:
Packit ae235b
   *
Packit ae235b
   * A #GTlsCertificate representing the entity that issued this
Packit ae235b
   * certificate. If %NULL, this means that the certificate is either
Packit ae235b
   * self-signed, or else the certificate of the issuer is not
Packit ae235b
   * available.
Packit ae235b
   *
Packit ae235b
   * Since: 2.28
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_ISSUER,
Packit ae235b
				   g_param_spec_object ("issuer",
Packit ae235b
							P_("Issuer"),
Packit ae235b
							P_("The certificate for the issuing entity"),
Packit ae235b
							G_TYPE_TLS_CERTIFICATE,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsCertificate *
Packit ae235b
g_tls_certificate_new_internal (const gchar      *certificate_pem,
Packit ae235b
				const gchar      *private_key_pem,
Packit ae235b
				GTlsCertificate  *issuer,
Packit ae235b
				GError          **error)
Packit ae235b
{
Packit ae235b
  GObject *cert;
Packit ae235b
  GTlsBackend *backend;
Packit ae235b
Packit ae235b
  backend = g_tls_backend_get_default ();
Packit ae235b
Packit ae235b
  cert = g_initable_new (g_tls_backend_get_certificate_type (backend),
Packit ae235b
			 NULL, error,
Packit ae235b
			 "certificate-pem", certificate_pem,
Packit ae235b
			 "private-key-pem", private_key_pem,
Packit ae235b
			 "issuer", issuer,
Packit ae235b
			 NULL);
Packit ae235b
Packit ae235b
  return G_TLS_CERTIFICATE (cert);
Packit ae235b
}
Packit ae235b
Packit ae235b
#define PEM_CERTIFICATE_HEADER     "-----BEGIN CERTIFICATE-----"
Packit ae235b
#define PEM_CERTIFICATE_FOOTER     "-----END CERTIFICATE-----"
Packit ae235b
#define PEM_PKCS1_PRIVKEY_HEADER   "-----BEGIN RSA PRIVATE KEY-----"
Packit ae235b
#define PEM_PKCS1_PRIVKEY_FOOTER   "-----END RSA PRIVATE KEY-----"
Packit ae235b
#define PEM_PKCS8_PRIVKEY_HEADER   "-----BEGIN PRIVATE KEY-----"
Packit ae235b
#define PEM_PKCS8_PRIVKEY_FOOTER   "-----END PRIVATE KEY-----"
Packit ae235b
#define PEM_PKCS8_ENCRYPTED_HEADER "-----BEGIN ENCRYPTED PRIVATE KEY-----"
Packit ae235b
#define PEM_PKCS8_ENCRYPTED_FOOTER "-----END ENCRYPTED PRIVATE KEY-----"
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
parse_private_key (const gchar *data,
Packit ae235b
		   gsize data_len,
Packit ae235b
		   gboolean required,
Packit ae235b
		   GError **error)
Packit ae235b
{
Packit ae235b
  const gchar *start, *end, *footer;
Packit ae235b
Packit ae235b
  start = g_strstr_len (data, data_len, PEM_PKCS1_PRIVKEY_HEADER);
Packit ae235b
  if (start)
Packit ae235b
    footer = PEM_PKCS1_PRIVKEY_FOOTER;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      start = g_strstr_len (data, data_len, PEM_PKCS8_PRIVKEY_HEADER);
Packit ae235b
      if (start)
Packit ae235b
	footer = PEM_PKCS8_PRIVKEY_FOOTER;
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  start = g_strstr_len (data, data_len, PEM_PKCS8_ENCRYPTED_HEADER);
Packit ae235b
	  if (start)
Packit ae235b
	    {
Packit ae235b
	      g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
Packit ae235b
				   _("Cannot decrypt PEM-encoded private key"));
Packit ae235b
	    }
Packit ae235b
	  else if (required)
Packit ae235b
	    {
Packit ae235b
	      g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
Packit ae235b
				   _("No PEM-encoded private key found"));
Packit ae235b
	    }
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  end = g_strstr_len (start, data_len - (data - start), footer);
Packit ae235b
  if (!end)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
Packit ae235b
			   _("Could not parse PEM-encoded private key"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  end += strlen (footer);
Packit ae235b
  while (*end == '\r' || *end == '\n')
Packit ae235b
    end++;
Packit ae235b
Packit ae235b
  return g_strndup (start, end - start);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
parse_next_pem_certificate (const gchar **data,
Packit ae235b
			    const gchar  *data_end,
Packit ae235b
			    gboolean      required,
Packit ae235b
			    GError      **error)
Packit ae235b
{
Packit ae235b
  const gchar *start, *end;
Packit ae235b
Packit ae235b
  start = g_strstr_len (*data, data_end - *data, PEM_CERTIFICATE_HEADER);
Packit ae235b
  if (!start)
Packit ae235b
    {
Packit ae235b
      if (required)
Packit ae235b
	{
Packit ae235b
	  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
Packit ae235b
			       _("No PEM-encoded certificate found"));
Packit ae235b
	}
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  end = g_strstr_len (start, data_end - start, PEM_CERTIFICATE_FOOTER);
Packit ae235b
  if (!end)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
Packit ae235b
			   _("Could not parse PEM-encoded certificate"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  end += strlen (PEM_CERTIFICATE_FOOTER);
Packit ae235b
  while (*end == '\r' || *end == '\n')
Packit ae235b
    end++;
Packit ae235b
Packit ae235b
  *data = end;
Packit ae235b
Packit ae235b
  return g_strndup (start, end - start);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSList *
Packit ae235b
parse_and_create_certificate_list (const gchar  *data,
Packit ae235b
                                   gsize         data_len,
Packit ae235b
                                   GError      **error)
Packit ae235b
{
Packit ae235b
  GSList *first_pem_list = NULL, *pem_list = NULL;
Packit ae235b
  gchar *first_pem;
Packit ae235b
  const gchar *p, *end;
Packit ae235b
Packit ae235b
  p = data;
Packit ae235b
  end = p + data_len;
Packit ae235b
Packit ae235b
  /* Make sure we can load, at least, one certificate. */
Packit ae235b
  first_pem = parse_next_pem_certificate (&p, end, TRUE, error);
Packit ae235b
  if (!first_pem)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  /* Create a list with a single element. If we load more certificates
Packit ae235b
   * below, we will concatenate the two lists at the end. */
Packit ae235b
  first_pem_list = g_slist_prepend (first_pem_list, first_pem);
Packit ae235b
Packit ae235b
  /* If we read one certificate successfully, let's see if we can read
Packit ae235b
   * some more. If not, we will simply return a list with the first one.
Packit ae235b
   */
Packit ae235b
  while (p && *p)
Packit ae235b
    {
Packit ae235b
      gchar *cert_pem;
Packit ae235b
      GError *error = NULL;
Packit ae235b
Packit ae235b
      cert_pem = parse_next_pem_certificate (&p, end, FALSE, &error);
Packit ae235b
      if (error)
Packit ae235b
        {
Packit ae235b
          g_slist_free_full (pem_list, g_free);
Packit ae235b
          g_error_free (error);
Packit ae235b
          return first_pem_list;
Packit ae235b
        }
Packit ae235b
      else if (!cert_pem)
Packit ae235b
        {
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      pem_list = g_slist_prepend (pem_list, cert_pem);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  pem_list = g_slist_concat (pem_list, first_pem_list);
Packit ae235b
Packit ae235b
  return pem_list;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsCertificate *
Packit ae235b
create_certificate_chain_from_list (GSList       *pem_list,
Packit ae235b
                                    const gchar  *key_pem)
Packit ae235b
{
Packit ae235b
  GTlsCertificate *cert = NULL, *issuer = NULL, *root = NULL;
Packit ae235b
  GTlsCertificateFlags flags;
Packit ae235b
  GSList *pem;
Packit ae235b
Packit ae235b
  pem = pem_list;
Packit ae235b
  while (pem)
Packit ae235b
    {
Packit ae235b
      const gchar *key = NULL;
Packit ae235b
Packit ae235b
      /* Private key belongs only to the first certificate. */
Packit ae235b
      if (!pem->next)
Packit ae235b
        key = key_pem;
Packit ae235b
Packit ae235b
      /* We assume that the whole file is a certificate chain, so we use
Packit ae235b
       * each certificate as the issuer of the next one (list is in
Packit ae235b
       * reverse order).
Packit ae235b
       */
Packit ae235b
      issuer = cert;
Packit ae235b
      cert = g_tls_certificate_new_internal (pem->data, key, issuer, NULL);
Packit ae235b
      if (issuer)
Packit ae235b
        g_object_unref (issuer);
Packit ae235b
Packit ae235b
      if (!cert)
Packit ae235b
        return NULL;
Packit ae235b
Packit ae235b
      /* root will point to the last certificate in the file. */
Packit ae235b
      if (!root)
Packit ae235b
        root = cert;
Packit ae235b
Packit ae235b
      pem = g_slist_next (pem);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Verify that the certificates form a chain. (We don't care at this
Packit ae235b
   * point if there are other problems with it.)
Packit ae235b
   */
Packit ae235b
  flags = g_tls_certificate_verify (cert, NULL, root);
Packit ae235b
  if (flags & G_TLS_CERTIFICATE_UNKNOWN_CA)
Packit ae235b
    {
Packit ae235b
      /* It wasn't a chain, it's just a bunch of unrelated certs. */
Packit ae235b
      g_clear_object (&cert);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return cert;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GTlsCertificate *
Packit ae235b
parse_and_create_certificate (const gchar  *data,
Packit ae235b
                              gsize         data_len,
Packit ae235b
                              const gchar  *key_pem,
Packit ae235b
                              GError      **error)
Packit ae235b
Packit ae235b
{
Packit ae235b
  GSList *pem_list;
Packit ae235b
  GTlsCertificate *cert;
Packit ae235b
Packit ae235b
  pem_list = parse_and_create_certificate_list (data, data_len, error);
Packit ae235b
  if (!pem_list)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  /* We don't pass the error here because, if it fails, we still want to
Packit ae235b
   * load and return the first certificate.
Packit ae235b
   */
Packit ae235b
  cert = create_certificate_chain_from_list (pem_list, key_pem);
Packit ae235b
  if (!cert)
Packit ae235b
    {
Packit ae235b
      GSList *last = NULL;
Packit ae235b
Packit ae235b
      /* Get the first certificate (which is the last one as the list is
Packit ae235b
       * in reverse order).
Packit ae235b
       */
Packit ae235b
      last = g_slist_last (pem_list);
Packit ae235b
Packit ae235b
      cert = g_tls_certificate_new_internal (last->data, key_pem, NULL, error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_slist_free_full (pem_list, g_free);
Packit ae235b
Packit ae235b
  return cert;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_new_from_pem:
Packit ae235b
 * @data: PEM-encoded certificate data
Packit ae235b
 * @length: the length of @data, or -1 if it's 0-terminated.
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Creates a #GTlsCertificate from the PEM-encoded data in @data. If
Packit ae235b
 * @data includes both a certificate and a private key, then the
Packit ae235b
 * returned certificate will include the private key data as well. (See
Packit ae235b
 * the #GTlsCertificate:private-key-pem property for information about
Packit ae235b
 * supported formats.)
Packit ae235b
 *
Packit ae235b
 * The returned certificate will be the first certificate found in
Packit ae235b
 * @data. As of GLib 2.44, if @data contains more certificates it will
Packit ae235b
 * try to load a certificate chain. All certificates will be verified in
Packit ae235b
 * the order found (top-level certificate should be the last one in the
Packit ae235b
 * file) and the #GTlsCertificate:issuer property of each certificate
Packit ae235b
 * will be set accordingly if the verification succeeds. If any
Packit ae235b
 * certificate in the chain cannot be verified, the first certificate in
Packit ae235b
 * the file will still be returned.
Packit ae235b
 *
Packit ae235b
 * Returns: the new certificate, or %NULL if @data is invalid
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificate *
Packit ae235b
g_tls_certificate_new_from_pem  (const gchar  *data,
Packit ae235b
				 gssize        length,
Packit ae235b
				 GError      **error)
Packit ae235b
{
Packit ae235b
  GError *child_error = NULL;
Packit ae235b
  gchar *key_pem;
Packit ae235b
  GTlsCertificate *cert;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (data != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  if (length == -1)
Packit ae235b
    length = strlen (data);
Packit ae235b
Packit ae235b
  key_pem = parse_private_key (data, length, FALSE, &child_error);
Packit ae235b
  if (child_error != NULL)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, child_error);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  cert = parse_and_create_certificate (data, length, key_pem, error);
Packit ae235b
  g_free (key_pem);
Packit ae235b
Packit ae235b
  return cert;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_new_from_file:
Packit ae235b
 * @file: (type filename): file containing a PEM-encoded certificate to import
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Creates a #GTlsCertificate from the PEM-encoded data in @file. The
Packit ae235b
 * returned certificate will be the first certificate found in @file. As
Packit ae235b
 * of GLib 2.44, if @file contains more certificates it will try to load
Packit ae235b
 * a certificate chain. All certificates will be verified in the order
Packit ae235b
 * found (top-level certificate should be the last one in the file) and
Packit ae235b
 * the #GTlsCertificate:issuer property of each certificate will be set
Packit ae235b
 * accordingly if the verification succeeds. If any certificate in the
Packit ae235b
 * chain cannot be verified, the first certificate in the file will
Packit ae235b
 * still be returned.
Packit ae235b
 *
Packit ae235b
 * If @file cannot be read or parsed, the function will return %NULL and
Packit ae235b
 * set @error. Otherwise, this behaves like
Packit ae235b
 * g_tls_certificate_new_from_pem().
Packit ae235b
 *
Packit ae235b
 * Returns: the new certificate, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificate *
Packit ae235b
g_tls_certificate_new_from_file (const gchar  *file,
Packit ae235b
				 GError      **error)
Packit ae235b
{
Packit ae235b
  GTlsCertificate *cert;
Packit ae235b
  gchar *contents;
Packit ae235b
  gsize length;
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (file, &contents, &length, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  cert = g_tls_certificate_new_from_pem (contents, length, error);
Packit ae235b
  g_free (contents);
Packit ae235b
  return cert;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_new_from_files:
Packit ae235b
 * @cert_file: (type filename): file containing one or more PEM-encoded
Packit ae235b
 *     certificates to import
Packit ae235b
 * @key_file: (type filename): file containing a PEM-encoded private key
Packit ae235b
 *     to import
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Creates a #GTlsCertificate from the PEM-encoded data in @cert_file
Packit ae235b
 * and @key_file. The returned certificate will be the first certificate
Packit ae235b
 * found in @cert_file. As of GLib 2.44, if @cert_file contains more
Packit ae235b
 * certificates it will try to load a certificate chain. All
Packit ae235b
 * certificates will be verified in the order found (top-level
Packit ae235b
 * certificate should be the last one in the file) and the
Packit ae235b
 * #GTlsCertificate:issuer property of each certificate will be set
Packit ae235b
 * accordingly if the verification succeeds. If any certificate in the
Packit ae235b
 * chain cannot be verified, the first certificate in the file will
Packit ae235b
 * still be returned.
Packit ae235b
 *
Packit ae235b
 * If either file cannot be read or parsed, the function will return
Packit ae235b
 * %NULL and set @error. Otherwise, this behaves like
Packit ae235b
 * g_tls_certificate_new_from_pem().
Packit ae235b
 *
Packit ae235b
 * Returns: the new certificate, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificate *
Packit ae235b
g_tls_certificate_new_from_files (const gchar  *cert_file,
Packit ae235b
                                  const gchar  *key_file,
Packit ae235b
                                  GError      **error)
Packit ae235b
{
Packit ae235b
  GTlsCertificate *cert;
Packit ae235b
  gchar *cert_data, *key_data;
Packit ae235b
  gsize cert_len, key_len;
Packit ae235b
  gchar *key_pem;
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (key_file, &key_data, &key_len, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  key_pem = parse_private_key (key_data, key_len, TRUE, error);
Packit ae235b
  g_free (key_data);
Packit ae235b
  if (!key_pem)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (cert_file, &cert_data, &cert_len, error))
Packit ae235b
    {
Packit ae235b
      g_free (key_pem);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  cert = parse_and_create_certificate (cert_data, cert_len, key_pem, error);
Packit ae235b
  g_free (cert_data);
Packit ae235b
  g_free (key_pem);
Packit ae235b
  return cert;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_list_new_from_file:
Packit ae235b
 * @file: (type filename): file containing PEM-encoded certificates to import
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Creates one or more #GTlsCertificates from the PEM-encoded
Packit ae235b
 * data in @file. If @file cannot be read or parsed, the function will
Packit ae235b
 * return %NULL and set @error. If @file does not contain any
Packit ae235b
 * PEM-encoded certificates, this will return an empty list and not
Packit ae235b
 * set @error.
Packit ae235b
 *
Packit ae235b
 * Returns: (element-type Gio.TlsCertificate) (transfer full): a
Packit ae235b
 * #GList containing #GTlsCertificate objects. You must free the list
Packit ae235b
 * and its contents when you are done with it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GList *
Packit ae235b
g_tls_certificate_list_new_from_file (const gchar  *file,
Packit ae235b
				      GError      **error)
Packit ae235b
{
Packit ae235b
  GQueue queue = G_QUEUE_INIT;
Packit ae235b
  gchar *contents, *end;
Packit ae235b
  const gchar *p;
Packit ae235b
  gsize length;
Packit ae235b
Packit ae235b
  if (!g_file_get_contents (file, &contents, &length, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  end = contents + length;
Packit ae235b
  p = contents;
Packit ae235b
  while (p && *p)
Packit ae235b
    {
Packit ae235b
      gchar *cert_pem;
Packit ae235b
      GTlsCertificate *cert = NULL;
Packit ae235b
      GError *parse_error = NULL;
Packit ae235b
Packit ae235b
      cert_pem = parse_next_pem_certificate (&p, end, FALSE, &parse_error);
Packit ae235b
      if (cert_pem)
Packit ae235b
        {
Packit ae235b
          cert = g_tls_certificate_new_internal (cert_pem, NULL, NULL, &parse_error);
Packit ae235b
          g_free (cert_pem);
Packit ae235b
        }
Packit ae235b
      if (!cert)
Packit ae235b
        {
Packit ae235b
          if (parse_error)
Packit ae235b
            {
Packit ae235b
              g_propagate_error (error, parse_error);
Packit ae235b
              g_list_free_full (queue.head, g_object_unref);
Packit ae235b
              queue.head = NULL;
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
      g_queue_push_tail (&queue, cert);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (contents);
Packit ae235b
  return queue.head;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_get_issuer:
Packit ae235b
 * @cert: a #GTlsCertificate
Packit ae235b
 *
Packit ae235b
 * Gets the #GTlsCertificate representing @cert's issuer, if known
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): The certificate of @cert's issuer,
Packit ae235b
 * or %NULL if @cert is self-signed or signed with an unknown
Packit ae235b
 * certificate.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificate *
Packit ae235b
g_tls_certificate_get_issuer (GTlsCertificate  *cert)
Packit ae235b
{
Packit ae235b
  GTlsCertificate *issuer;
Packit ae235b
Packit ae235b
  g_object_get (G_OBJECT (cert), "issuer", &issuer, NULL);
Packit ae235b
  if (issuer)
Packit ae235b
    g_object_unref (issuer);
Packit ae235b
Packit ae235b
  return issuer;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_verify:
Packit ae235b
 * @cert: a #GTlsCertificate
Packit ae235b
 * @identity: (nullable): the expected peer identity
Packit ae235b
 * @trusted_ca: (nullable): the certificate of a trusted authority
Packit ae235b
 *
Packit ae235b
 * This verifies @cert and returns a set of #GTlsCertificateFlags
Packit ae235b
 * indicating any problems found with it. This can be used to verify a
Packit ae235b
 * certificate outside the context of making a connection, or to
Packit ae235b
 * check a certificate against a CA that is not part of the system
Packit ae235b
 * CA database.
Packit ae235b
 *
Packit ae235b
 * If @identity is not %NULL, @cert's name(s) will be compared against
Packit ae235b
 * it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return
Packit ae235b
 * value if it does not match. If @identity is %NULL, that bit will
Packit ae235b
 * never be set in the return value.
Packit ae235b
 *
Packit ae235b
 * If @trusted_ca is not %NULL, then @cert (or one of the certificates
Packit ae235b
 * in its chain) must be signed by it, or else
Packit ae235b
 * %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If
Packit ae235b
 * @trusted_ca is %NULL, that bit will never be set in the return
Packit ae235b
 * value.
Packit ae235b
 *
Packit ae235b
 * (All other #GTlsCertificateFlags values will always be set or unset
Packit ae235b
 * as appropriate.)
Packit ae235b
 *
Packit ae235b
 * Returns: the appropriate #GTlsCertificateFlags
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GTlsCertificateFlags
Packit ae235b
g_tls_certificate_verify (GTlsCertificate     *cert,
Packit ae235b
			  GSocketConnectable  *identity,
Packit ae235b
			  GTlsCertificate     *trusted_ca)
Packit ae235b
{
Packit ae235b
  return G_TLS_CERTIFICATE_GET_CLASS (cert)->verify (cert, identity, trusted_ca);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_tls_certificate_is_same:
Packit ae235b
 * @cert_one: first certificate to compare
Packit ae235b
 * @cert_two: second certificate to compare
Packit ae235b
 *
Packit ae235b
 * Check if two #GTlsCertificate objects represent the same certificate.
Packit ae235b
 * The raw DER byte data of the two certificates are checked for equality.
Packit ae235b
 * This has the effect that two certificates may compare equal even if
Packit ae235b
 * their #GTlsCertificate:issuer, #GTlsCertificate:private-key, or
Packit ae235b
 * #GTlsCertificate:private-key-pem properties differ.
Packit ae235b
 *
Packit ae235b
 * Returns: whether the same or not
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_tls_certificate_is_same (GTlsCertificate     *cert_one,
Packit ae235b
                           GTlsCertificate     *cert_two)
Packit ae235b
{
Packit ae235b
  GByteArray *b1, *b2;
Packit ae235b
  gboolean equal;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CERTIFICATE (cert_one), FALSE);
Packit ae235b
  g_return_val_if_fail (G_IS_TLS_CERTIFICATE (cert_two), FALSE);
Packit ae235b
Packit ae235b
  g_object_get (cert_one, "certificate", &b1, NULL);
Packit ae235b
  g_object_get (cert_two, "certificate", &b2, NULL);
Packit ae235b
Packit ae235b
  equal = (b1->len == b2->len &&
Packit ae235b
           memcmp (b1->data, b2->data, b1->len) == 0);
Packit ae235b
Packit ae235b
  g_byte_array_unref (b1);
Packit ae235b
  g_byte_array_unref (b2);
Packit ae235b
Packit ae235b
  return equal;
Packit ae235b
}