Merge branch 'trust-store'
[platform/upstream/gcr.git] / gcr / gcr-simple-certificate.c
1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2008 Stefan Walter
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "gcr-certificate.h"
25 #include "gcr-internal.h"
26 #include "gcr-simple-certificate.h"
27
28 #include <string.h>
29
30 /**
31  * SECTION:gcr-simple-certificate
32  * @title: GcrSimpleCertificate
33  * @short_description: A certificate loaded from a memory buffer
34  *
35  * An implementation of #GcrCertificate which loads a certificate from DER
36  * data already located in memory.
37  *
38  * To create a #GcrSimpleCertificate object use the
39  * gcr_simple_certificate_new() or gcr_simple_certificate_new_static()
40  * functions.
41  */
42
43 struct _GcrSimpleCertificatePrivate {
44         const guchar *data;
45         gsize n_data;
46         guchar *owned;
47 };
48
49 static void gcr_certificate_iface (GcrCertificateIface *iface);
50 G_DEFINE_TYPE_WITH_CODE (GcrSimpleCertificate, gcr_simple_certificate, G_TYPE_OBJECT,
51                          G_IMPLEMENT_INTERFACE (GCR_TYPE_CERTIFICATE, gcr_certificate_iface));
52
53 /* -----------------------------------------------------------------------------
54  * OBJECT
55  */
56
57 static void
58 gcr_simple_certificate_init (GcrSimpleCertificate *self)
59 {
60         self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_SIMPLE_CERTIFICATE, GcrSimpleCertificatePrivate);
61 }
62
63 static void
64 gcr_simple_certificate_finalize (GObject *obj)
65 {
66         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (obj);
67
68         g_free (self->pv->owned);
69         self->pv->owned = NULL;
70         self->pv->data = NULL;
71         self->pv->n_data = 0;
72
73         G_OBJECT_CLASS (gcr_simple_certificate_parent_class)->finalize (obj);
74 }
75
76 static void
77 gcr_simple_certificate_class_init (GcrSimpleCertificateClass *klass)
78 {
79         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
80         gobject_class->finalize = gcr_simple_certificate_finalize;
81         g_type_class_add_private (gobject_class, sizeof (GcrSimpleCertificatePrivate));
82         _gcr_initialize ();
83 }
84
85 static gconstpointer
86 gcr_simple_certificate_real_get_der_data (GcrCertificate *base, gsize *n_data)
87 {
88         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (base);
89
90         g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
91         g_return_val_if_fail (n_data, NULL);
92         g_return_val_if_fail (self->pv->data, NULL);
93
94         /* This is called when we're not a base class */
95         *n_data = self->pv->n_data;
96         return self->pv->data;
97 }
98
99 static void
100 gcr_certificate_iface (GcrCertificateIface *iface)
101 {
102         iface->get_der_data = (gpointer)gcr_simple_certificate_real_get_der_data;
103 }
104
105 /* -----------------------------------------------------------------------------
106  * PUBLIC
107  */
108
109 /**
110  * gcr_simple_certificate_new:
111  * @data: The raw DER certificate data
112  * @n_data: The length of @data
113  *
114  * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
115  * copied so you can dispose of it after this function returns.
116  *
117  * Returns: a new #GcrSimpleCertificate
118  */
119 GcrCertificate*
120 gcr_simple_certificate_new (gconstpointer data, gsize n_data)
121 {
122         GcrSimpleCertificate *cert;
123
124         g_return_val_if_fail (data, NULL);
125         g_return_val_if_fail (n_data, NULL);
126
127         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
128
129         cert->pv->data = cert->pv->owned = g_memdup (data, n_data);
130         cert->pv->n_data = n_data;
131         return GCR_CERTIFICATE (cert);
132 }
133
134 /**
135  * gcr_simple_certificate_new_static:
136  * @data: The raw DER certificate data
137  * @n_data: The length of @data
138  *
139  * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
140  * not copied and must persist until the #GcrSimpleCertificate object is
141  * destroyed.
142  *
143  * Returns: a new #GcrSimpleCertificate
144  */
145 GcrCertificate*
146 gcr_simple_certificate_new_static (gconstpointer data, gsize n_data)
147 {
148         GcrSimpleCertificate *cert;
149
150         g_return_val_if_fail (data, NULL);
151         g_return_val_if_fail (n_data, NULL);
152
153         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
154
155         cert->pv->owned = NULL;
156         cert->pv->data = data;
157         cert->pv->n_data = n_data;
158         return GCR_CERTIFICATE (cert);
159 }