Beispiel #1
0
static gboolean
load_cert (GTlsCertificate **out_cert,
           GError **error)
{
  GTlsCertificate *cert = NULL;
  gboolean ret = FALSE;
  gchar *cert_path = NULL;
  const gchar *cert_dir = PACKAGE_SYSCONF_DIR "/cockpit/ws-certs.d";
  GError *local_error;

  local_error = NULL;
  cert_path = load_cert_from_dir (cert_dir, &local_error);
  if (local_error != NULL)
    {
      g_propagate_prefixed_error (error, local_error,
                                  "Error loading certificates from %s: ",
                                  cert_dir);
      goto out;
    }

  /* Could be there's no certicate at all, so cert_path can indeed be
   * NULL. If so, use (and possibly generate) a temporary self-signed
   * certificate
   */
  if (cert_path == NULL)
    {
      cert_path = generate_temp_cert (error);
      if (cert_path == NULL)
        goto out;
    }

  cert = g_tls_certificate_new_from_file (cert_path, error);
  if (cert == NULL)
    {
      g_prefix_error (error, "Error loading certificate at path `%s': ", cert_path);
      goto out;
    }

  g_info ("Using certificate %s", cert_path);

  if (out_cert != NULL)
    {
      *out_cert = cert;
      cert = NULL;
    }

  ret = TRUE;

out:
  g_clear_object (&cert);
  g_free (cert_path);
  return ret;
}
Beispiel #2
0
gchar *
cockpit_certificate_locate (gboolean create_if_necessary,
                            GError **error)
{
  const gchar * const* dirs = cockpit_conf_get_dirs ();
  GError *local_error = NULL;
  gchar *cert_dir;
  gchar *cert_path;
  gint i;

  for (i = 0; dirs[i]; i++)
    {
      cert_dir = g_build_filename (dirs[i], "cockpit", "ws-certs.d", NULL);
      cert_path = load_cert_from_dir (cert_dir, &local_error);

      if (local_error != NULL)
        {
          g_propagate_prefixed_error (error, local_error,
                                      "Error loading certificates from %s: ",
                                      cert_dir);
          g_free (cert_dir);
          return NULL;
        }

      g_free (cert_dir);

      if (cert_path)
        return cert_path;
    }

  cert_dir = g_build_filename (dirs[0], "cockpit", "ws-certs.d", NULL);
  if (create_if_necessary)
    {
      cert_path = generate_temp_cert (cert_dir, error);
    }
  else
    {
      cert_path = NULL;
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
                   "No certificate found in dir: %s", cert_dir);
    }
  g_free (cert_dir);

  return cert_path;
}
gchar *
cockpit_certificate_locate (gboolean create_if_necessary,
                            GError **error)
{
  gchar *cert_path = NULL;
  const gchar *cert_dir = PACKAGE_SYSCONF_DIR "/cockpit/ws-certs.d";
  GError *local_error;

  local_error = NULL;
  cert_path = load_cert_from_dir (cert_dir, &local_error);
  if (local_error != NULL)
    {
      g_propagate_prefixed_error (error, local_error,
                                  "Error loading certificates from %s: ",
                                  cert_dir);
      return NULL;
    }

  /* Could be there's no certicate at all, so cert_path can indeed be
   * NULL. If so, use (and possibly generate) a temporary self-signed
   * certificate
   */
  if (cert_path == NULL)
    {
      if (create_if_necessary)
        {
          cert_path = generate_temp_cert (error);
        }
      else
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
                       "No certificate found in dir: %s", cert_dir);
        }
    }

  return cert_path;
}