changed generation of manpages.
[gnutls.git] / lib / gnutls_pcert.c
blob1211e2d6fb9f23dced3458605e1c8e8ec13e9014
1 /*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
24 #include <gnutls_errors.h>
25 #include <auth/cert.h>
26 #include <gnutls_x509.h>
27 #include "x509/x509_int.h"
28 #ifdef ENABLE_OPENPGP
29 #include "openpgp/gnutls_openpgp.h"
30 #endif
32 /**
33 * gnutls_pcert_import_x509:
34 * @pcert: The pcert structure
35 * @crt: The raw certificate to be imported
36 * @flags: zero for now
38 * This convenience function will import the given certificate to a
39 * #gnutls_pcert_st structure. The structure must be deinitialized
40 * afterwards using gnutls_pcert_deinit();
42 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
43 * negative error value.
45 * Since: 3.0
46 **/
47 int gnutls_pcert_import_x509 (gnutls_pcert_st* pcert,
48 gnutls_x509_crt_t crt, unsigned int flags)
50 int ret;
51 size_t sz;
53 memset(pcert, 0, sizeof(*pcert));
55 pcert->type = GNUTLS_CRT_X509;
56 pcert->cert.data = NULL;
58 sz = 0;
59 ret = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_DER, NULL, &sz);
60 if (ret < 0 && ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
62 ret = gnutls_assert_val(ret);
63 goto cleanup;
66 pcert->cert.data = gnutls_malloc(sz);
67 if (pcert->cert.data == NULL)
69 ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
70 goto cleanup;
73 ret = gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_DER, pcert->cert.data, &sz);
74 if (ret < 0)
76 ret = gnutls_assert_val(ret);
77 goto cleanup;
79 pcert->cert.size = sz;
81 ret = gnutls_pubkey_init(&pcert->pubkey);
82 if (ret < 0)
84 ret = gnutls_assert_val(ret);
85 goto cleanup;
88 ret = gnutls_pubkey_import_x509(pcert->pubkey, crt, 0);
89 if (ret < 0)
91 gnutls_pubkey_deinit(pcert->pubkey);
92 pcert->pubkey = NULL;
93 ret = gnutls_assert_val(ret);
94 goto cleanup;
97 return 0;
99 cleanup:
100 _gnutls_free_datum(&pcert->cert);
102 return ret;
106 * gnutls_pcert_list_import_x509_raw:
107 * @pcerts: The structures to store the parsed certificate. Must not be initialized.
108 * @pcert_max: Initially must hold the maximum number of certs. It will be updated with the number of certs available.
109 * @data: The certificates.
110 * @format: One of DER or PEM.
111 * @flags: must be (0) or an OR'd sequence of gnutls_certificate_import_flags.
113 * This function will convert the given PEM encoded certificate list
114 * to the native gnutls_x509_crt_t format. The output will be stored
115 * in @certs. They will be automatically initialized.
117 * If the Certificate is PEM encoded it should have a header of "X509
118 * CERTIFICATE", or "CERTIFICATE".
120 * Returns: the number of certificates read or a negative error value.
122 * Since: 3.0
125 gnutls_pcert_list_import_x509_raw (gnutls_pcert_st * pcerts,
126 unsigned int *pcert_max,
127 const gnutls_datum_t * data,
128 gnutls_x509_crt_fmt_t format, unsigned int flags)
130 int ret;
131 unsigned int i = 0, j;
132 gnutls_x509_crt_t *crt;
134 crt = gnutls_malloc((*pcert_max) * sizeof(gnutls_x509_crt_t));
136 if (crt == NULL)
137 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
139 ret = gnutls_x509_crt_list_import( crt, pcert_max, data, format, flags);
140 if (ret < 0)
142 ret = gnutls_assert_val(ret);
143 goto cleanup;
146 for (i=0;i<*pcert_max;i++)
148 ret = gnutls_pcert_import_x509(&pcerts[i], crt[i], flags);
149 if (ret < 0)
151 ret = gnutls_assert_val(ret);
152 goto cleanup_pcert;
156 ret = 0;
157 goto cleanup;
159 cleanup_pcert:
160 for (j=0;j<i;j++)
161 gnutls_pcert_deinit(&pcerts[j]);
163 cleanup:
164 for (i=0;i<*pcert_max;i++)
165 gnutls_x509_crt_deinit(crt[i]);
167 gnutls_free(crt);
168 return ret;
173 * gnutls_pcert_import_x509_raw:
174 * @pcert: The pcert structure
175 * @cert: The raw certificate to be imported
176 * @format: The format of the certificate
177 * @flags: zero for now
179 * This convenience function will import the given certificate to a
180 * #gnutls_pcert_st structure. The structure must be deinitialized
181 * afterwards using gnutls_pcert_deinit();
183 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
184 * negative error value.
186 * Since: 3.0
188 int gnutls_pcert_import_x509_raw (gnutls_pcert_st *pcert,
189 const gnutls_datum_t* cert,
190 gnutls_x509_crt_fmt_t format, unsigned int flags)
192 int ret;
193 gnutls_x509_crt_t crt;
195 memset(pcert, 0, sizeof(*pcert));
197 ret = gnutls_x509_crt_init(&crt);
198 if (ret < 0)
199 return gnutls_assert_val(ret);
201 ret = gnutls_x509_crt_import(crt, cert, format);
202 if (ret < 0)
204 ret = gnutls_assert_val(ret);
205 goto cleanup;
208 ret = gnutls_pcert_import_x509(pcert, crt, flags);
209 if (ret < 0)
211 ret = gnutls_assert_val(ret);
212 goto cleanup;
215 ret = 0;
217 cleanup:
218 gnutls_x509_crt_deinit(crt);
220 return ret;
223 #ifdef ENABLE_OPENPGP
226 * gnutls_pcert_import_openpgp:
227 * @pcert: The pcert structure
228 * @crt: The raw certificate to be imported
229 * @flags: zero for now
231 * This convenience function will import the given certificate to a
232 * #gnutls_pcert_st structure. The structure must be deinitialized
233 * afterwards using gnutls_pcert_deinit();
235 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
236 * negative error value.
238 * Since: 3.0
240 int gnutls_pcert_import_openpgp (gnutls_pcert_st* pcert,
241 gnutls_openpgp_crt_t crt, unsigned int flags)
243 int ret;
244 size_t sz;
246 memset(pcert, 0, sizeof(*pcert));
248 pcert->type = GNUTLS_CRT_OPENPGP;
249 pcert->cert.data = NULL;
251 sz = 0;
252 ret = gnutls_openpgp_crt_export(crt, GNUTLS_OPENPGP_FMT_RAW, NULL, &sz);
253 if (ret < 0 && ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
255 ret = gnutls_assert_val(ret);
256 goto cleanup;
259 pcert->cert.data = gnutls_malloc(sz);
260 if (pcert->cert.data == NULL)
262 ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
263 goto cleanup;
266 ret = gnutls_openpgp_crt_export(crt, GNUTLS_X509_FMT_DER, pcert->cert.data, &sz);
267 if (ret < 0)
269 ret = gnutls_assert_val(ret);
270 goto cleanup;
272 pcert->cert.size = sz;
274 ret = gnutls_pubkey_init(&pcert->pubkey);
275 if (ret < 0)
277 ret = gnutls_assert_val(ret);
278 goto cleanup;
281 ret = gnutls_pubkey_import_openpgp(pcert->pubkey, crt, 0);
282 if (ret < 0)
284 gnutls_pubkey_deinit(pcert->pubkey);
285 pcert->pubkey = NULL;
286 ret = gnutls_assert_val(ret);
287 goto cleanup;
290 return 0;
292 cleanup:
293 _gnutls_free_datum(&pcert->cert);
295 return ret;
299 * gnutls_pcert_import_openpgp_raw:
300 * @pcert: The pcert structure
301 * @cert: The raw certificate to be imported
302 * @format: The format of the certificate
303 * @keyid: The key ID to use (NULL for the master key)
304 * @flags: zero for now
306 * This convenience function will import the given certificate to a
307 * #gnutls_pcert_st structure. The structure must be deinitialized
308 * afterwards using gnutls_pcert_deinit();
310 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
311 * negative error value.
313 * Since: 3.0
315 int gnutls_pcert_import_openpgp_raw (gnutls_pcert_st *pcert,
316 const gnutls_datum_t* cert,
317 gnutls_openpgp_crt_fmt_t format,
318 gnutls_openpgp_keyid_t keyid,
319 unsigned int flags)
321 int ret;
322 gnutls_openpgp_crt_t crt;
324 memset(pcert, 0, sizeof(*pcert));
326 pcert->cert.data = NULL;
328 ret = gnutls_openpgp_crt_init(&crt);
329 if (ret < 0)
330 return gnutls_assert_val(ret);
332 ret = gnutls_openpgp_crt_import(crt, cert, format);
333 if (ret < 0)
335 ret = gnutls_assert_val(ret);
336 goto cleanup;
339 if (keyid != NULL)
341 ret = gnutls_openpgp_crt_set_preferred_key_id(crt, keyid);
342 if (ret < 0)
344 ret = gnutls_assert_val(ret);
345 goto cleanup;
349 ret = gnutls_pcert_import_openpgp(pcert, crt, flags);
350 if (ret < 0)
352 ret = gnutls_assert_val(ret);
353 goto cleanup;
355 ret = 0;
357 cleanup:
358 gnutls_openpgp_crt_deinit(crt);
360 return ret;
363 #endif
366 * gnutls_pcert_deinit:
367 * @pcert: The structure to be deinitialized
369 * This function will deinitialize a pcert structure.
371 * Since: 3.0
373 void
374 gnutls_pcert_deinit (gnutls_pcert_st *pcert)
376 gnutls_pubkey_deinit(pcert->pubkey);
377 pcert->pubkey = NULL;
378 _gnutls_free_datum(&pcert->cert);
381 /* Converts the first certificate for the cert_auth_info structure
382 * to a pcert.
385 _gnutls_get_auth_info_pcert (gnutls_pcert_st* pcert,
386 gnutls_certificate_type_t type,
387 cert_auth_info_t info)
389 switch (type)
391 case GNUTLS_CRT_X509:
392 return gnutls_pcert_import_x509_raw(pcert, &info->raw_certificate_list[0],
393 GNUTLS_X509_FMT_DER, GNUTLS_PCERT_NO_CERT);
394 #ifdef ENABLE_OPENPGP
395 case GNUTLS_CRT_OPENPGP:
396 return gnutls_pcert_import_openpgp_raw(pcert,
397 &info->raw_certificate_list[0],
398 GNUTLS_OPENPGP_FMT_RAW,
399 info->use_subkey ? info->
400 subkey_id : NULL, GNUTLS_PCERT_NO_CERT);
401 #endif
402 default:
403 gnutls_assert ();
404 return GNUTLS_E_INTERNAL_ERROR;