4 * Copyright (C) 2008 Stefan Walter
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.
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.
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
24 #include "gcr-certificate.h"
25 #include "gcr-comparable.h"
26 #include "gcr-internal.h"
27 #include "gcr-simple-certificate.h"
32 * SECTION:gcr-simple-certificate
33 * @title: GcrSimpleCertificate
34 * @short_description: A certificate loaded from a memory buffer
36 * An implementation of #GcrCertificate which loads a certificate from DER
37 * data already located in memory.
39 * To create a #GcrSimpleCertificate object use the
40 * gcr_simple_certificate_new() or gcr_simple_certificate_new_static()
44 struct _GcrSimpleCertificatePrivate {
50 /* Forward declarations */
51 static void gcr_simple_certificate_iface_init (GcrCertificateIface *iface);
53 G_DEFINE_TYPE_WITH_CODE (GcrSimpleCertificate, gcr_simple_certificate, G_TYPE_OBJECT,
54 GCR_CERTIFICATE_MIXIN_IMPLEMENT_COMPARABLE ();
55 G_IMPLEMENT_INTERFACE (GCR_TYPE_CERTIFICATE, gcr_simple_certificate_iface_init);
58 /* -----------------------------------------------------------------------------
63 gcr_simple_certificate_init (GcrSimpleCertificate *self)
65 self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_SIMPLE_CERTIFICATE, GcrSimpleCertificatePrivate);
69 gcr_simple_certificate_real_finalize (GObject *obj)
71 GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (obj);
73 g_free (self->pv->owned);
74 self->pv->owned = NULL;
75 self->pv->data = NULL;
78 G_OBJECT_CLASS (gcr_simple_certificate_parent_class)->finalize (obj);
82 gcr_simple_certificate_class_init (GcrSimpleCertificateClass *klass)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86 gobject_class->finalize = gcr_simple_certificate_real_finalize;
87 gobject_class->get_property = gcr_certificate_mixin_get_property;
89 g_type_class_add_private (gobject_class, sizeof (GcrSimpleCertificatePrivate));
91 gcr_certificate_mixin_class_init (gobject_class);
96 gcr_simple_certificate_get_der_data (GcrCertificate *cert, gsize *n_data)
98 GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (cert);
100 g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
101 g_return_val_if_fail (n_data, NULL);
102 g_return_val_if_fail (self->pv->data, NULL);
104 /* This is called when we're not a base class */
105 *n_data = self->pv->n_data;
106 return self->pv->data;
110 gcr_simple_certificate_iface_init (GcrCertificateIface *iface)
112 iface->get_der_data = gcr_simple_certificate_get_der_data;
115 /* -----------------------------------------------------------------------------
120 * gcr_simple_certificate_new:
121 * @data: The raw DER certificate data
122 * @n_data: The length of @data
124 * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
125 * copied so you can dispose of it after this function returns.
127 * Returns: a new #GcrSimpleCertificate
130 gcr_simple_certificate_new (gconstpointer data, gsize n_data)
132 GcrSimpleCertificate *cert;
134 g_return_val_if_fail (data, NULL);
135 g_return_val_if_fail (n_data, NULL);
137 cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
139 cert->pv->data = cert->pv->owned = g_memdup (data, n_data);
140 cert->pv->n_data = n_data;
141 return GCR_CERTIFICATE (cert);
145 * gcr_simple_certificate_new_static:
146 * @data: The raw DER certificate data
147 * @n_data: The length of @data
149 * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
150 * not copied and must persist until the #GcrSimpleCertificate object is
153 * Returns: a new #GcrSimpleCertificate
156 gcr_simple_certificate_new_static (gconstpointer data, gsize n_data)
158 GcrSimpleCertificate *cert;
160 g_return_val_if_fail (data, NULL);
161 g_return_val_if_fail (n_data, NULL);
163 cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
165 cert->pv->owned = NULL;
166 cert->pv->data = data;
167 cert->pv->n_data = n_data;
168 return GCR_CERTIFICATE (cert);