summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-tls-cert-interaction.c
blob: 7614244eff808d280788ced8d89262ecde165298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (C) 2014 Colin Walters <walters@verbum.org>
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "ostree-tls-cert-interaction-private.h"

struct _OstreeTlsCertInteraction
{
  GTlsInteraction parent_instance;

  char *cert_path;
  char *key_path;
  GTlsCertificate *cert;
};

struct _OstreeTlsCertInteractionClass
{
  GTlsInteractionClass parent_class;
};

#include <string.h>

G_DEFINE_TYPE (OstreeTlsCertInteraction, _ostree_tls_cert_interaction, G_TYPE_TLS_INTERACTION);

static GTlsInteractionResult
request_certificate (GTlsInteraction              *interaction,
                     GTlsConnection               *connection,
                     GTlsCertificateRequestFlags   flags,
                     GCancellable                 *cancellable,
                     GError                      **error)
{
  OstreeTlsCertInteraction *self = (OstreeTlsCertInteraction*)interaction;

  if (!self->cert)
    {
      self->cert = g_tls_certificate_new_from_files (self->cert_path, self->key_path, error);
      if (!self->cert)
        return G_TLS_INTERACTION_FAILED;
    }

  g_tls_connection_set_certificate (connection, self->cert);
  return G_TLS_INTERACTION_HANDLED;
}

static void
_ostree_tls_cert_interaction_init (OstreeTlsCertInteraction *interaction)
{
}

static void
_ostree_tls_cert_interaction_class_init (OstreeTlsCertInteractionClass *klass)
{
  GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
  interaction_class->request_certificate = request_certificate;
}

OstreeTlsCertInteraction *
_ostree_tls_cert_interaction_new (const char *cert_path,
                                  const char *key_path)
{
  OstreeTlsCertInteraction *self = g_object_new (OSTREE_TYPE_TLS_CERT_INTERACTION, NULL);
  self->cert_path = g_strdup (cert_path);
  self->key_path = g_strdup (key_path);
  return self;
}