~ubuntu-branches/ubuntu/trusty/gcr/trusty-proposed

« back to all changes in this revision

Viewing changes to gcr/gcr-simple-certificate.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-03 10:18:39 UTC
  • Revision ID: package-import@ubuntu.com-20120503101839-wuvloldm7gmdsnij
Tags: upstream-3.4.1
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/**
 
45
 * GcrSimpleCertificate:
 
46
 *
 
47
 * A #GcrCertificate which represents a certificate already in memory.
 
48
 */
 
49
 
 
50
/**
 
51
 * GcrSimpleCertificateClass:
 
52
 * @parent_class: The parent class
 
53
 *
 
54
 * The class for #GcrSimpleCertificate.
 
55
 */
 
56
 
 
57
struct _GcrSimpleCertificatePrivate {
 
58
        const guchar *data;
 
59
        gsize n_data;
 
60
        guchar *owned;
 
61
};
 
62
 
 
63
/* Forward declarations */
 
64
static void gcr_simple_certificate_iface_init (GcrCertificateIface *iface);
 
65
 
 
66
G_DEFINE_TYPE_WITH_CODE (GcrSimpleCertificate, gcr_simple_certificate, G_TYPE_OBJECT,
 
67
        GCR_CERTIFICATE_MIXIN_IMPLEMENT_COMPARABLE ();
 
68
        G_IMPLEMENT_INTERFACE (GCR_TYPE_CERTIFICATE, gcr_simple_certificate_iface_init);
 
69
);
 
70
 
 
71
/* -----------------------------------------------------------------------------
 
72
 * OBJECT
 
73
 */
 
74
 
 
75
static void
 
76
gcr_simple_certificate_init (GcrSimpleCertificate *self)
 
77
{
 
78
        self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_SIMPLE_CERTIFICATE, GcrSimpleCertificatePrivate);
 
79
}
 
80
 
 
81
static void
 
82
gcr_simple_certificate_real_finalize (GObject *obj)
 
83
{
 
84
        GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (obj);
 
85
 
 
86
        g_free (self->pv->owned);
 
87
        self->pv->owned = NULL;
 
88
        self->pv->data = NULL;
 
89
        self->pv->n_data = 0;
 
90
 
 
91
        G_OBJECT_CLASS (gcr_simple_certificate_parent_class)->finalize (obj);
 
92
}
 
93
 
 
94
static void
 
95
gcr_simple_certificate_class_init (GcrSimpleCertificateClass *klass)
 
96
{
 
97
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
98
 
 
99
        gobject_class->finalize = gcr_simple_certificate_real_finalize;
 
100
        gobject_class->get_property = gcr_certificate_mixin_get_property;
 
101
 
 
102
        g_type_class_add_private (gobject_class, sizeof (GcrSimpleCertificatePrivate));
 
103
 
 
104
        gcr_certificate_mixin_class_init (gobject_class);
 
105
        _gcr_initialize_library ();
 
106
}
 
107
 
 
108
static const guchar *
 
109
gcr_simple_certificate_get_der_data (GcrCertificate *cert,
 
110
                                     gsize *n_data)
 
111
{
 
112
        GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (cert);
 
113
 
 
114
        g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
 
115
        g_return_val_if_fail (n_data, NULL);
 
116
        g_return_val_if_fail (self->pv->data, NULL);
 
117
 
 
118
        /* This is called when we're not a base class */
 
119
        *n_data = self->pv->n_data;
 
120
        return self->pv->data;
 
121
}
 
122
 
 
123
static void
 
124
gcr_simple_certificate_iface_init (GcrCertificateIface *iface)
 
125
{
 
126
        iface->get_der_data = gcr_simple_certificate_get_der_data;
 
127
}
 
128
 
 
129
/* -----------------------------------------------------------------------------
 
130
 * PUBLIC
 
131
 */
 
132
 
 
133
/**
 
134
 * gcr_simple_certificate_new:
 
135
 * @data: (array length=n_data): the raw DER certificate data
 
136
 * @n_data: The length of @data
 
137
 *
 
138
 * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
 
139
 * copied so you can dispose of it after this function returns.
 
140
 *
 
141
 * Returns: (transfer full) (type Gcr.SimpleCertificate): a new #GcrSimpleCertificate
 
142
 */
 
143
GcrCertificate *
 
144
gcr_simple_certificate_new (const guchar *data,
 
145
                            gsize n_data)
 
146
{
 
147
        GcrSimpleCertificate *cert;
 
148
 
 
149
        g_return_val_if_fail (data, NULL);
 
150
        g_return_val_if_fail (n_data, NULL);
 
151
 
 
152
        cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
 
153
 
 
154
        cert->pv->data = cert->pv->owned = g_memdup (data, n_data);
 
155
        cert->pv->n_data = n_data;
 
156
        return GCR_CERTIFICATE (cert);
 
157
}
 
158
 
 
159
/**
 
160
 * gcr_simple_certificate_new_static: (skip)
 
161
 * @data: (array length=n_data): The raw DER certificate data
 
162
 * @n_data: The length of @data
 
163
 *
 
164
 * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
 
165
 * not copied and must persist until the #GcrSimpleCertificate object is
 
166
 * destroyed.
 
167
 *
 
168
 * Returns: (transfer full) (type Gcr.SimpleCertificate): a new #GcrSimpleCertificate
 
169
 */
 
170
GcrCertificate *
 
171
gcr_simple_certificate_new_static (const guchar *data,
 
172
                                   gsize n_data)
 
173
{
 
174
        GcrSimpleCertificate *cert;
 
175
 
 
176
        g_return_val_if_fail (data, NULL);
 
177
        g_return_val_if_fail (n_data, NULL);
 
178
 
 
179
        cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
 
180
 
 
181
        cert->pv->owned = NULL;
 
182
        cert->pv->data = data;
 
183
        cert->pv->n_data = n_data;
 
184
        return GCR_CERTIFICATE (cert);
 
185
}