File: certificate.c

package info (click to toggle)
openvas-libraries 2.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,720 kB
  • ctags: 1,334
  • sloc: ansic: 12,441; sh: 8,238; makefile: 325
file content (255 lines) | stat: -rw-r--r-- 6,410 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* openvas-libraries/libopenvascommon
 * $Id$
 * Description: Facilities for certificates and certificate collections.
 *
 * Authors:
 * Matthew Mundell <matt@mundell.ukfsn.org>
 *
 * Copyright:
 * Copyright (C) 2009 Greenbone Networks GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * or, at your option, any later version as published by the Free
 * Software Foundation
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * @file certificate.c
 * @brief Facilities for certificates and certificate collections.
 *
 * This file provides facilities for data about certificates, and
 * collections of such data.  This includes two types, certificate_t
 * and certificates_t, and functions for manipulating structures of
 * these types.
 */

#include "certificate.h"

/**
 * @brief Create a new, empty certificate structure.
 *
 * @return NULL in case the memory could not be allocated.
 *         Else an empty certificate structure which needs to be
 *         released using @ref certificate_free .
 */
certificate_t *
certificate_create ()
{
  return (certificate_t *) g_malloc0 (sizeof (certificate_t));
}

/**
 * @brief Free memory of a certificate structure.
 *
 * @param  n  The structure to be freed.
 */
void
certificate_free (certificate_t * certificate)
{
  if (certificate == NULL) return;
  if (certificate->fingerprint) g_free (certificate->fingerprint);
  if (certificate->owner) g_free (certificate->owner);
  if (certificate->public_key) g_free (certificate->public_key);
  g_free (certificate);
}

/**
 * @brief Get the fingerprint of a certificate.
 *
 * @param  certificate  The certificate.
 *
 * @return The fingerprint, which may be NULL.
 */
const gchar *
certificate_fingerprint (const certificate_t * certificate)
{
  return certificate->fingerprint;
}

/**
 * @brief Get the owner of a certificate.
 *
 * @param  certificate  The certificate.
 *
 * @return The owner, which may be NULL.
 */
const gchar *
certificate_owner (const certificate_t * certificate)
{
  return certificate->owner;
}

/**
 * @brief Get the public key of a certificate.
 *
 * @param  certificate  The certificate.
 *
 * @return The public key, which may be NULL.
 */
const gchar *
certificate_public_key (const certificate_t * certificate)
{
  return certificate->public_key;
}

/**
 * @brief Get the trustedness of a certificate.
 *
 * @param  certificate  The certificate.
 *
 * @return TRUE if the key is trusted, else FALSE.
 */
gboolean
certificate_trusted (const certificate_t * certificate)
{
  return certificate->trusted;
}

/**
 * @brief Set the fingerprint of a certificate.
 *
 * @param  certificate  The certificate.
 * @param  fingerprint  The fingerprint.
 *
 * @return 0 on success, -1 on error.
 */
int
certificate_set_fingerprint (certificate_t * certificate, const gchar * fingerprint)
{
  if (certificate->fingerprint)
    g_free (certificate->fingerprint);
  // FIX this aborts on out of mem, while certificate_create returns NULL
  certificate->fingerprint = g_strdup (fingerprint);
  return 0;
}

/**
 * @brief Set the owner of a certificate.
 *
 * @param  certificate  The certificate.
 * @param  owner        The owner.
 *
 * @return 0 on success, -1 on error.
 */
int
certificate_set_owner (certificate_t * certificate, const gchar * owner)
{
  if (certificate->owner)
    g_free (certificate->owner);
  certificate->owner = g_strdup (owner);
  return 0;
}

/**
 * @brief Set the public key of a certificate.
 *
 * @param  certificate  The certificate.
 * @param  public key   The public key.
 *
 * @return 0 on success, -1 on error.
 */
int
certificate_set_public_key (certificate_t * certificate, const gchar * public_key)
{
  if (certificate->public_key)
    g_free (certificate->public_key);
  certificate->public_key = g_strdup (public_key);
  return 0;
}

/**
 * @brief Set the trustedness of a certificate.
 *
 * @param  certificate  The certificate.
 * @param  trusted      TRUE if trusted, else FALSE.
 */
void
certificate_set_trusted (certificate_t * certificate, gboolean trusted)
{
  certificate->trusted = trusted;
}


/* Collections of certificates. */

/**
 * @brief Make a collection of certificates.
 *
 * @return A new collection of certificates or NULL on error.
 */
certificates_t*
certificates_create ()
{
  certificates_t *certs;
  certs = (certificates_t *) g_malloc0 (sizeof (certificates_t));
  return certs;
}

/**
 * @brief Free a collection of certificates.
 *
 * @param  certificates  The collection of certificates.
 */
void
certificates_free (certificates_t* certificates)
{
  GSList *list;
  if (certificates == NULL) return;
  for (list = certificates->list; list; list = g_slist_next (list))
    certificate_free (list->data);
  g_slist_free (certificates->list);
  g_free (certificates);
}

/**
 * @brief Get the size of a collection of certificates.
 *
 * @return The number of entries in the collection.
 */
guint
certificates_size (certificates_t* certificates)
{
  return g_slist_length (certificates->list);
}

/**
 * @brief Add a certificate to a collection of certificate
 *
 * @param  certificates  The collection of certificates.
 */
void
certificates_add (certificates_t* certificates, certificate_t* certificate)
{
  if (certificate)
    certificates->list = g_slist_prepend (certificates->list,
                                          (gpointer) certificate);
}

/**
 * @brief Search the certificates with a function.
 *
 * @param  certificates  Certificates to search.
 * @param  data          First argument to function.
 * @param  function      Compare function.
 *
 * @return The first element for which the function returns 0.
 */
certificate_t*
certificates_find (certificates_t* certificates,
                   gconstpointer data,
                   GCompareFunc function)
{
  GSList* element = g_slist_find_custom (certificates->list, data, function);
  if (element) return element->data;
  return NULL;
}