[gcr] More implementation of selector widget.
[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-comparable.h"
26 #include "gcr-internal.h"
27 #include "gcr-simple-certificate.h"
28
29 #include <string.h>
30
31 /**
32  * SECTION:gcr-simple-certificate
33  * @title: GcrSimpleCertificate
34  * @short_description: A certificate loaded from a memory buffer
35  *
36  * An implementation of #GcrCertificate which loads a certificate from DER
37  * data already located in memory.
38  *
39  * To create a #GcrSimpleCertificate object use the
40  * gcr_simple_certificate_new() or gcr_simple_certificate_new_static()
41  * functions.
42  */
43
44 struct _GcrSimpleCertificatePrivate {
45         const guchar *data;
46         gsize n_data;
47         guchar *owned;
48 };
49
50 /* Forward declarations */
51 static void gcr_simple_certificate_iface_init (GcrCertificateIface *iface);
52
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);
56 );
57
58 /* -----------------------------------------------------------------------------
59  * OBJECT
60  */
61
62 static void
63 gcr_simple_certificate_init (GcrSimpleCertificate *self)
64 {
65         self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_SIMPLE_CERTIFICATE, GcrSimpleCertificatePrivate);
66 }
67
68 static void
69 gcr_simple_certificate_real_finalize (GObject *obj)
70 {
71         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (obj);
72
73         g_free (self->pv->owned);
74         self->pv->owned = NULL;
75         self->pv->data = NULL;
76         self->pv->n_data = 0;
77
78         G_OBJECT_CLASS (gcr_simple_certificate_parent_class)->finalize (obj);
79 }
80
81 static void
82 gcr_simple_certificate_class_init (GcrSimpleCertificateClass *klass)
83 {
84         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85
86         gobject_class->finalize = gcr_simple_certificate_real_finalize;
87         gobject_class->get_property = gcr_certificate_mixin_get_property;
88
89         g_type_class_add_private (gobject_class, sizeof (GcrSimpleCertificatePrivate));
90
91         gcr_certificate_mixin_class_init (gobject_class);
92         _gcr_initialize ();
93 }
94
95 static gconstpointer
96 gcr_simple_certificate_get_der_data (GcrCertificate *cert, gsize *n_data)
97 {
98         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (cert);
99
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);
103
104         /* This is called when we're not a base class */
105         *n_data = self->pv->n_data;
106         return self->pv->data;
107 }
108
109 static void
110 gcr_simple_certificate_iface_init (GcrCertificateIface *iface)
111 {
112         iface->get_der_data = gcr_simple_certificate_get_der_data;
113 }
114
115 /* -----------------------------------------------------------------------------
116  * PUBLIC
117  */
118
119 /**
120  * gcr_simple_certificate_new:
121  * @data: The raw DER certificate data
122  * @n_data: The length of @data
123  *
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.
126  *
127  * Returns: a new #GcrSimpleCertificate
128  */
129 GcrCertificate*
130 gcr_simple_certificate_new (gconstpointer data, gsize n_data)
131 {
132         GcrSimpleCertificate *cert;
133
134         g_return_val_if_fail (data, NULL);
135         g_return_val_if_fail (n_data, NULL);
136
137         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
138
139         cert->pv->data = cert->pv->owned = g_memdup (data, n_data);
140         cert->pv->n_data = n_data;
141         return GCR_CERTIFICATE (cert);
142 }
143
144 /**
145  * gcr_simple_certificate_new_static:
146  * @data: The raw DER certificate data
147  * @n_data: The length of @data
148  *
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
151  * destroyed.
152  *
153  * Returns: a new #GcrSimpleCertificate
154  */
155 GcrCertificate*
156 gcr_simple_certificate_new_static (gconstpointer data, gsize n_data)
157 {
158         GcrSimpleCertificate *cert;
159
160         g_return_val_if_fail (data, NULL);
161         g_return_val_if_fail (n_data, NULL);
162
163         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
164
165         cert->pv->owned = NULL;
166         cert->pv->data = data;
167         cert->pv->n_data = n_data;
168         return GCR_CERTIFICATE (cert);
169 }