added new functions
[gnutls.git] / lib / gnutls_cert.c
blobf784cc5f812377a1e3910e801d079bb9484eab34
1 /*
2 * Copyright (C) 2001-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 /* Some of the stuff needed for Certificate authentication is contained
24 * in this file.
27 #include <gnutls_int.h>
28 #include <gnutls_errors.h>
29 #include <auth/cert.h>
30 #include <gnutls_datum.h>
31 #include <gnutls_mpi.h>
32 #include <gnutls_global.h>
33 #include <algorithms.h>
34 #include <gnutls_dh.h>
35 #include <gnutls_str.h>
36 #include <gnutls_state.h>
37 #include <gnutls_auth.h>
38 #include <gnutls_x509.h>
39 #include <gnutls_str_array.h>
40 #include "x509/x509_int.h"
41 #ifdef ENABLE_OPENPGP
42 #include "openpgp/gnutls_openpgp.h"
43 #endif
44 #include "gettext.h"
45 #define _(String) dgettext (PACKAGE, String)
47 /**
48 * gnutls_certificate_free_keys:
49 * @sc: is a #gnutls_certificate_credentials_t structure.
51 * This function will delete all the keys and the certificates associated
52 * with the given credentials. This function must not be called when a
53 * TLS negotiation that uses the credentials is in progress.
55 **/
56 void
57 gnutls_certificate_free_keys (gnutls_certificate_credentials_t sc)
59 unsigned i, j;
61 for (i = 0; i < sc->ncerts; i++)
63 for (j = 0; j < sc->certs[i].cert_list_length; j++)
65 gnutls_pcert_deinit (&sc->certs[i].cert_list[j]);
67 gnutls_free (sc->certs[i].cert_list);
68 _gnutls_str_array_clear (&sc->certs[i].names);
71 gnutls_free (sc->certs);
72 sc->certs = NULL;
74 for (i = 0; i < sc->ncerts; i++)
76 gnutls_privkey_deinit (sc->pkey[i]);
79 gnutls_free (sc->pkey);
80 sc->pkey = NULL;
82 sc->ncerts = 0;
85 /**
86 * gnutls_certificate_free_cas:
87 * @sc: is a #gnutls_certificate_credentials_t structure.
89 * This function will delete all the CAs associated with the given
90 * credentials. Servers that do not use
91 * gnutls_certificate_verify_peers2() may call this to save some
92 * memory.
93 **/
94 void
95 gnutls_certificate_free_cas (gnutls_certificate_credentials_t sc)
97 /* FIXME: do nothing for now */
98 return;
102 * gnutls_certificate_get_issuer:
103 * @sc: is a #gnutls_certificate_credentials_t structure.
104 * @cert: is the certificate to find issuer for
105 * @issuer: Will hold the issuer if any. Should be treated as constant.
106 * @flags: Use zero.
108 * This function will return the issuer of a given certificate.
110 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
111 * negative error value.
113 * Since: 3.0
116 gnutls_certificate_get_issuer (gnutls_certificate_credentials_t sc,
117 gnutls_x509_crt_t cert, gnutls_x509_crt_t* issuer, unsigned int flags)
119 return gnutls_x509_trust_list_get_issuer(sc->tlist, cert, issuer, flags);
123 * gnutls_certificate_free_ca_names:
124 * @sc: is a #gnutls_certificate_credentials_t structure.
126 * This function will delete all the CA name in the given
127 * credentials. Clients may call this to save some memory since in
128 * client side the CA names are not used. Servers might want to use
129 * this function if a large list of trusted CAs is present and
130 * sending the names of it would just consume bandwidth without providing
131 * information to client.
133 * CA names are used by servers to advertise the CAs they support to
134 * clients.
136 void
137 gnutls_certificate_free_ca_names (gnutls_certificate_credentials_t sc)
139 _gnutls_free_datum (&sc->x509_rdn_sequence);
143 * _gnutls_certificate_get_rsa_params - Returns the RSA parameters pointer
144 * @rsa_params: holds the RSA parameters or NULL.
145 * @func: function to retrieve the parameters or NULL.
146 * @session: The session.
148 * This function will return the rsa parameters pointer.
150 gnutls_rsa_params_t
151 _gnutls_certificate_get_rsa_params (gnutls_rsa_params_t rsa_params,
152 gnutls_params_function * func,
153 gnutls_session_t session)
155 gnutls_params_st params;
156 int ret;
158 if (session->internals.params.rsa_params)
160 return session->internals.params.rsa_params;
163 if (rsa_params)
165 session->internals.params.rsa_params = rsa_params;
167 else if (func)
169 ret = func (session, GNUTLS_PARAMS_RSA_EXPORT, &params);
170 if (ret == 0 && params.type == GNUTLS_PARAMS_RSA_EXPORT)
172 session->internals.params.rsa_params = params.params.rsa_export;
173 session->internals.params.free_rsa_params = params.deinit;
177 return session->internals.params.rsa_params;
182 * gnutls_certificate_free_credentials:
183 * @sc: is a #gnutls_certificate_credentials_t structure.
185 * This structure is complex enough to manipulate directly thus this
186 * helper function is provided in order to free (deallocate) it.
188 * This function does not free any temporary parameters associated
189 * with this structure (ie RSA and DH parameters are not freed by this
190 * function).
192 void
193 gnutls_certificate_free_credentials (gnutls_certificate_credentials_t sc)
195 gnutls_x509_trust_list_deinit(sc->tlist, 1);
196 gnutls_certificate_free_keys (sc);
197 gnutls_certificate_free_ca_names (sc);
198 gnutls_free(sc->ocsp_response_file);
200 #ifdef ENABLE_OPENPGP
201 gnutls_openpgp_keyring_deinit (sc->keyring);
202 #endif
204 gnutls_free (sc);
209 * gnutls_certificate_allocate_credentials:
210 * @res: is a pointer to a #gnutls_certificate_credentials_t structure.
212 * This structure is complex enough to manipulate directly thus this
213 * helper function is provided in order to allocate it.
215 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
218 gnutls_certificate_allocate_credentials (gnutls_certificate_credentials_t *
219 res)
221 int ret;
223 *res = gnutls_calloc (1, sizeof (certificate_credentials_st));
225 if (*res == NULL)
226 return GNUTLS_E_MEMORY_ERROR;
228 ret = gnutls_x509_trust_list_init( &(*res)->tlist, 0);
229 if (ret < 0)
231 gnutls_assert();
232 gnutls_free(*res);
233 return GNUTLS_E_MEMORY_ERROR;
235 (*res)->verify_bits = DEFAULT_MAX_VERIFY_BITS;
236 (*res)->verify_depth = DEFAULT_MAX_VERIFY_DEPTH;
238 return 0;
242 /* returns the KX algorithms that are supported by a
243 * certificate. (Eg a certificate with RSA params, supports
244 * GNUTLS_KX_RSA algorithm).
245 * This function also uses the KeyUsage field of the certificate
246 * extensions in order to disable unneded algorithms.
249 _gnutls_selected_cert_supported_kx (gnutls_session_t session,
250 gnutls_kx_algorithm_t * alg,
251 int *alg_size)
253 gnutls_kx_algorithm_t kx;
254 gnutls_pk_algorithm_t pk, cert_pk;
255 gnutls_pcert_st *cert;
256 int i;
258 if (session->internals.selected_cert_list_length == 0)
260 *alg_size = 0;
261 return 0;
264 cert = &session->internals.selected_cert_list[0];
265 cert_pk = gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
266 i = 0;
268 for (kx = 0; kx < MAX_ALGOS; kx++)
270 pk = _gnutls_map_pk_get_pk (kx);
271 if (pk == cert_pk)
273 /* then check key usage */
274 if (_gnutls_check_key_usage (cert, kx) == 0)
276 alg[i] = kx;
277 i++;
279 if (i > *alg_size)
280 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
285 if (i == 0)
287 gnutls_assert ();
288 return GNUTLS_E_INVALID_REQUEST;
291 *alg_size = i;
293 return 0;
298 * gnutls_certificate_server_set_request:
299 * @session: is a #gnutls_session_t structure.
300 * @req: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE
302 * This function specifies if we (in case of a server) are going to
303 * send a certificate request message to the client. If @req is
304 * GNUTLS_CERT_REQUIRE then the server will return an error if the
305 * peer does not provide a certificate. If you do not call this
306 * function then the client will not be asked to send a certificate.
308 void
309 gnutls_certificate_server_set_request (gnutls_session_t session,
310 gnutls_certificate_request_t req)
312 session->internals.send_cert_req = req;
316 * gnutls_certificate_client_set_retrieve_function:
317 * @cred: is a #gnutls_certificate_credentials_t structure.
318 * @func: is the callback function
320 * This function sets a callback to be called in order to retrieve the
321 * certificate to be used in the handshake.
322 * You are advised to use gnutls_certificate_set_retrieve_function2() because it
323 * is much more efficient in the processing it requires from gnutls.
325 * The callback's function prototype is:
326 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
327 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr_st* st);
329 * @req_ca_cert is only used in X.509 certificates.
330 * Contains a list with the CA names that the server considers trusted.
331 * Normally we should send a certificate that is signed
332 * by one of these CAs. These names are DER encoded. To get a more
333 * meaningful value use the function gnutls_x509_rdn_get().
335 * @pk_algos contains a list with server's acceptable signature algorithms.
336 * The certificate returned should support the server's given algorithms.
338 * @st should contain the certificates and private keys.
340 * If the callback function is provided then gnutls will call it, in the
341 * handshake, if a certificate is requested by the server (and after the
342 * certificate request message has been received).
344 * The callback function should set the certificate list to be sent,
345 * and return 0 on success. If no certificate was selected then the
346 * number of certificates should be set to zero. The value (-1)
347 * indicates error and the handshake will be terminated.
349 void gnutls_certificate_client_set_retrieve_function
350 (gnutls_certificate_credentials_t cred,
351 gnutls_certificate_client_retrieve_function * func)
353 cred->client_get_cert_callback = func;
357 * gnutls_certificate_server_set_retrieve_function:
358 * @cred: is a #gnutls_certificate_credentials_t structure.
359 * @func: is the callback function
361 * This function sets a callback to be called in order to retrieve the
362 * certificate to be used in the handshake.
363 * You are advised to use gnutls_certificate_set_retrieve_function2() because it
364 * is much more efficient in the processing it requires from gnutls.
366 * The callback's function prototype is:
367 * int (*callback)(gnutls_session_t, gnutls_retr_st* st);
369 * @st should contain the certificates and private keys.
371 * If the callback function is provided then gnutls will call it, in the
372 * handshake, after the certificate request message has been received.
374 * The callback function should set the certificate list to be sent, and
375 * return 0 on success. The value (-1) indicates error and the handshake
376 * will be terminated.
378 void gnutls_certificate_server_set_retrieve_function
379 (gnutls_certificate_credentials_t cred,
380 gnutls_certificate_server_retrieve_function * func)
382 cred->server_get_cert_callback = func;
386 * gnutls_certificate_set_retrieve_function:
387 * @cred: is a #gnutls_certificate_credentials_t structure.
388 * @func: is the callback function
390 * This function sets a callback to be called in order to retrieve the
391 * certificate to be used in the handshake. You are advised
392 * to use gnutls_certificate_set_retrieve_function2() because it
393 * is much more efficient in the processing it requires from gnutls.
395 * The callback's function prototype is:
396 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
397 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr2_st* st);
399 * @req_ca_cert is only used in X.509 certificates.
400 * Contains a list with the CA names that the server considers trusted.
401 * Normally we should send a certificate that is signed
402 * by one of these CAs. These names are DER encoded. To get a more
403 * meaningful value use the function gnutls_x509_rdn_get().
405 * @pk_algos contains a list with server's acceptable signature algorithms.
406 * The certificate returned should support the server's given algorithms.
408 * @st should contain the certificates and private keys.
410 * If the callback function is provided then gnutls will call it, in the
411 * handshake, after the certificate request message has been received.
413 * In server side pk_algos and req_ca_dn are NULL.
415 * The callback function should set the certificate list to be sent,
416 * and return 0 on success. If no certificate was selected then the
417 * number of certificates should be set to zero. The value (-1)
418 * indicates error and the handshake will be terminated.
420 * Since: 3.0
422 void gnutls_certificate_set_retrieve_function
423 (gnutls_certificate_credentials_t cred,
424 gnutls_certificate_retrieve_function * func)
426 cred->get_cert_callback = func;
430 * gnutls_certificate_set_retrieve_function2:
431 * @cred: is a #gnutls_certificate_credentials_t structure.
432 * @func: is the callback function
434 * This function sets a callback to be called in order to retrieve the
435 * certificate to be used in the handshake.
437 * The callback's function prototype is:
438 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
439 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_pcert_st** pcert,
440 * unsigned int *pcert_length, gnutls_privkey_t * pkey);
442 * @req_ca_cert is only used in X.509 certificates.
443 * Contains a list with the CA names that the server considers trusted.
444 * Normally we should send a certificate that is signed
445 * by one of these CAs. These names are DER encoded. To get a more
446 * meaningful value use the function gnutls_x509_rdn_get().
448 * @pk_algos contains a list with server's acceptable signature algorithms.
449 * The certificate returned should support the server's given algorithms.
451 * @pcert should contain a single certificate and public or a list of them.
453 * @pcert_length is the size of the previous list.
455 * @pkey is the private key.
457 * If the callback function is provided then gnutls will call it, in the
458 * handshake, after the certificate request message has been received.
460 * In server side pk_algos and req_ca_dn are NULL.
462 * The callback function should set the certificate list to be sent,
463 * and return 0 on success. If no certificate was selected then the
464 * number of certificates should be set to zero. The value (-1)
465 * indicates error and the handshake will be terminated.
467 * Since: 3.0
469 void gnutls_certificate_set_retrieve_function2
470 (gnutls_certificate_credentials_t cred,
471 gnutls_certificate_retrieve_function2 * func)
473 cred->get_cert_callback2 = func;
477 * gnutls_certificate_set_verify_function:
478 * @cred: is a #gnutls_certificate_credentials_t structure.
479 * @func: is the callback function
481 * This function sets a callback to be called when peer's certificate
482 * has been received in order to verify it on receipt rather than
483 * doing after the handshake is completed.
485 * The callback's function prototype is:
486 * int (*callback)(gnutls_session_t);
488 * If the callback function is provided then gnutls will call it, in the
489 * handshake, just after the certificate message has been received.
490 * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
491 * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
492 * can be used.
494 * The callback function should return 0 for the handshake to continue
495 * or non-zero to terminate.
497 * Since: 2.10.0
499 void
500 gnutls_certificate_set_verify_function
501 (gnutls_certificate_credentials_t cred,
502 gnutls_certificate_verify_function * func)
504 cred->verify_callback = func;
508 * _gnutls_x509_extract_certificate_activation_time - return the peer's certificate activation time
509 * @cert: should contain an X.509 DER encoded certificate
511 * This function will return the certificate's activation time in UNIX time
512 * (ie seconds since 00:00:00 UTC January 1, 1970).
514 * Returns a (time_t) -1 in case of an error.
517 static time_t
518 _gnutls_x509_get_raw_crt_activation_time (const gnutls_datum_t * cert)
520 gnutls_x509_crt_t xcert;
521 time_t result;
523 result = gnutls_x509_crt_init (&xcert);
524 if (result < 0)
525 return (time_t) - 1;
527 result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
528 if (result < 0)
530 gnutls_x509_crt_deinit (xcert);
531 return (time_t) - 1;
534 result = gnutls_x509_crt_get_activation_time (xcert);
536 gnutls_x509_crt_deinit (xcert);
538 return result;
542 * gnutls_x509_extract_certificate_expiration_time:
543 * @cert: should contain an X.509 DER encoded certificate
545 * This function will return the certificate's expiration time in UNIX
546 * time (ie seconds since 00:00:00 UTC January 1, 1970). Returns a
548 * (time_t) -1 in case of an error.
551 static time_t
552 _gnutls_x509_get_raw_crt_expiration_time (const gnutls_datum_t * cert)
554 gnutls_x509_crt_t xcert;
555 time_t result;
557 result = gnutls_x509_crt_init (&xcert);
558 if (result < 0)
559 return (time_t) - 1;
561 result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
562 if (result < 0)
564 gnutls_x509_crt_deinit (xcert);
565 return (time_t) - 1;
568 result = gnutls_x509_crt_get_expiration_time (xcert);
570 gnutls_x509_crt_deinit (xcert);
572 return result;
575 #ifdef ENABLE_OPENPGP
577 * _gnutls_openpgp_crt_verify_peers - return the peer's certificate status
578 * @session: is a gnutls session
580 * This function will try to verify the peer's certificate and return its status (TRUSTED, INVALID etc.).
581 * Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
583 static int
584 _gnutls_openpgp_crt_verify_peers (gnutls_session_t session,
585 const char* hostname,
586 unsigned int *status)
588 cert_auth_info_t info;
589 gnutls_certificate_credentials_t cred;
590 int peer_certificate_list_size, ret;
592 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
594 info = _gnutls_get_auth_info (session);
595 if (info == NULL)
596 return GNUTLS_E_INVALID_REQUEST;
598 cred = (gnutls_certificate_credentials_t)
599 _gnutls_get_cred (session, GNUTLS_CRD_CERTIFICATE, NULL);
600 if (cred == NULL)
602 gnutls_assert ();
603 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
606 if (info->raw_certificate_list == NULL || info->ncerts == 0)
608 gnutls_assert ();
609 return GNUTLS_E_NO_CERTIFICATE_FOUND;
612 /* generate a list of gnutls_certs based on the auth info
613 * raw certs.
615 peer_certificate_list_size = info->ncerts;
617 if (peer_certificate_list_size != 1)
619 gnutls_assert ();
620 return GNUTLS_E_INTERNAL_ERROR;
623 /* Verify certificate
625 ret =
626 _gnutls_openpgp_verify_key (cred, hostname, &info->raw_certificate_list[0],
627 peer_certificate_list_size, status);
629 if (ret < 0)
631 gnutls_assert ();
632 return ret;
635 return 0;
637 #endif
640 * gnutls_certificate_verify_peers2:
641 * @session: is a gnutls session
642 * @status: is the output of the verification
644 * This function will verify the peer's certificate and return
645 * its status (trusted, invalid etc.). The value of @status will
646 * be one or more of the gnutls_certificate_status_t flags
647 * bitwise or'd or zero if the certificate is trusted. Note that verification
648 * does not imply a negative return value. Only the @status is updated.
650 * If available the OCSP Certificate Status extension will be
651 * utilized by this function.
653 * To avoid denial of service attacks some
654 * default upper limits regarding the certificate key size and chain
655 * size are set. To override them use gnutls_certificate_set_verify_limits().
657 * Note that you must also check the peer's name in order to check if
658 * the verified certificate belongs to the actual peer, see gnutls_x509_crt_check_hostname().
660 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
663 gnutls_certificate_verify_peers2 (gnutls_session_t session,
664 unsigned int *status)
666 cert_auth_info_t info;
668 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
670 info = _gnutls_get_auth_info (session);
671 if (info == NULL)
673 return GNUTLS_E_NO_CERTIFICATE_FOUND;
676 if (info->raw_certificate_list == NULL || info->ncerts == 0)
677 return GNUTLS_E_NO_CERTIFICATE_FOUND;
679 switch (gnutls_certificate_type_get (session))
681 case GNUTLS_CRT_X509:
682 return _gnutls_x509_cert_verify_peers (session, NULL, status);
683 #ifdef ENABLE_OPENPGP
684 case GNUTLS_CRT_OPENPGP:
685 return _gnutls_openpgp_crt_verify_peers (session, NULL, status);
686 #endif
687 default:
688 return GNUTLS_E_INVALID_REQUEST;
693 * gnutls_certificate_verify_peers3:
694 * @session: is a gnutls session
695 * @hostname: is the expected name of the peer
696 * @status: is the output of the verification
698 * This function will verify the peer's certificate and its name and
699 * return its status (trusted, invalid etc.). The value of @status will
700 * be one or more of the gnutls_certificate_status_t flags
701 * bitwise or'd or zero if the certificate is trusted. Note that verification
702 * failure does not imply a negative return value. Only the @status is updated.
704 * In case the @hostname does not match the %GNUTLS_CERT_UNEXPECTED_OWNER
705 * status flag will be set.
707 * If available the OCSP Certificate Status extension will be
708 * utilized by this function.
710 * To avoid denial of service attacks some
711 * default upper limits regarding the certificate key size and chain
712 * size are set. To override them use gnutls_certificate_set_verify_limits().
714 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
716 * Since: 3.1.4
719 gnutls_certificate_verify_peers3 (gnutls_session_t session,
720 const char* hostname,
721 unsigned int *status)
723 cert_auth_info_t info;
725 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
727 info = _gnutls_get_auth_info (session);
728 if (info == NULL)
730 return GNUTLS_E_NO_CERTIFICATE_FOUND;
733 if (info->raw_certificate_list == NULL || info->ncerts == 0)
734 return GNUTLS_E_NO_CERTIFICATE_FOUND;
736 switch (gnutls_certificate_type_get (session))
738 case GNUTLS_CRT_X509:
739 return _gnutls_x509_cert_verify_peers (session, hostname, status);
740 #ifdef ENABLE_OPENPGP
741 case GNUTLS_CRT_OPENPGP:
742 return _gnutls_openpgp_crt_verify_peers (session, hostname, status);
743 #endif
744 default:
745 return GNUTLS_E_INVALID_REQUEST;
750 * gnutls_certificate_expiration_time_peers:
751 * @session: is a gnutls session
753 * This function will return the peer's certificate expiration time.
755 * Returns: (time_t)-1 on error.
757 * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
759 time_t
760 gnutls_certificate_expiration_time_peers (gnutls_session_t session)
762 cert_auth_info_t info;
764 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
766 info = _gnutls_get_auth_info (session);
767 if (info == NULL)
769 return (time_t) - 1;
772 if (info->raw_certificate_list == NULL || info->ncerts == 0)
774 gnutls_assert ();
775 return (time_t) - 1;
778 switch (gnutls_certificate_type_get (session))
780 case GNUTLS_CRT_X509:
781 return
782 _gnutls_x509_get_raw_crt_expiration_time (&info->raw_certificate_list
783 [0]);
784 #ifdef ENABLE_OPENPGP
785 case GNUTLS_CRT_OPENPGP:
786 return
787 _gnutls_openpgp_get_raw_key_expiration_time
788 (&info->raw_certificate_list[0]);
789 #endif
790 default:
791 return (time_t) - 1;
796 * gnutls_certificate_activation_time_peers:
797 * @session: is a gnutls session
799 * This function will return the peer's certificate activation time.
800 * This is the creation time for openpgp keys.
802 * Returns: (time_t)-1 on error.
804 * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
806 time_t
807 gnutls_certificate_activation_time_peers (gnutls_session_t session)
809 cert_auth_info_t info;
811 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
813 info = _gnutls_get_auth_info (session);
814 if (info == NULL)
816 return (time_t) - 1;
819 if (info->raw_certificate_list == NULL || info->ncerts == 0)
821 gnutls_assert ();
822 return (time_t) - 1;
825 switch (gnutls_certificate_type_get (session))
827 case GNUTLS_CRT_X509:
828 return
829 _gnutls_x509_get_raw_crt_activation_time (&info->raw_certificate_list
830 [0]);
831 #ifdef ENABLE_OPENPGP
832 case GNUTLS_CRT_OPENPGP:
833 return
834 _gnutls_openpgp_get_raw_key_creation_time (&info->raw_certificate_list
835 [0]);
836 #endif
837 default:
838 return (time_t) - 1;
843 * gnutls_sign_callback_set:
844 * @session: is a gnutls session
845 * @sign_func: function pointer to application's sign callback.
846 * @userdata: void pointer that will be passed to sign callback.
848 * Set the callback function. The function must have this prototype:
850 * typedef int (*gnutls_sign_func) (gnutls_session_t session,
851 * void *userdata,
852 * gnutls_certificate_type_t cert_type,
853 * const gnutls_datum_t * cert,
854 * const gnutls_datum_t * hash,
855 * gnutls_datum_t * signature);
857 * The @userdata parameter is passed to the @sign_func verbatim, and
858 * can be used to store application-specific data needed in the
859 * callback function. See also gnutls_sign_callback_get().
861 * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess like gnutls_privkey_import_ext() instead.
863 void
864 gnutls_sign_callback_set (gnutls_session_t session,
865 gnutls_sign_func sign_func, void *userdata)
867 session->internals.sign_func = sign_func;
868 session->internals.sign_func_userdata = userdata;
872 * gnutls_sign_callback_get:
873 * @session: is a gnutls session
874 * @userdata: if non-%NULL, will be set to abstract callback pointer.
876 * Retrieve the callback function, and its userdata pointer.
878 * Returns: The function pointer set by gnutls_sign_callback_set(), or
879 * if not set, %NULL.
881 * Deprecated: Use the PKCS 11 interfaces instead.
883 gnutls_sign_func
884 gnutls_sign_callback_get (gnutls_session_t session, void **userdata)
886 if (userdata)
887 *userdata = session->internals.sign_func_userdata;
888 return session->internals.sign_func;
891 /* returns error if the certificate has different algorithm than
892 * the given key parameters.
895 _gnutls_check_key_cert_match (gnutls_certificate_credentials_t res)
897 int pk = gnutls_pubkey_get_pk_algorithm(res->certs[res->ncerts-1].cert_list[0].pubkey, NULL);
898 int pk2 = gnutls_privkey_get_pk_algorithm (res->pkey[res->ncerts - 1], NULL);
900 if (pk2 != pk)
902 gnutls_assert ();
903 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
906 return 0;
910 * gnutls_certificate_verification_status_print:
911 * @status: The status flags to be printed
912 * @type: The certificate type
913 * @out: Newly allocated datum with (0) terminated string.
914 * @flags: should be zero
916 * This function will pretty print the status of a verification
917 * process -- eg. the one obtained by gnutls_certificate_verify_peers3().
919 * The output @out needs to be deallocated using gnutls_free().
921 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
922 * negative error value.
924 * Since: 3.1.4
927 gnutls_certificate_verification_status_print (unsigned int status,
928 gnutls_certificate_type_t type,
929 gnutls_datum_t * out, unsigned int flags)
931 gnutls_buffer_st str;
932 int ret;
934 _gnutls_buffer_init (&str);
936 if (status == 0)
937 _gnutls_buffer_append_str (&str, _("The certificate is trusted. "));
938 else
939 _gnutls_buffer_append_str (&str, _("The certificate is NOT trusted. "));
941 if (type == GNUTLS_CRT_X509)
943 if (status & GNUTLS_CERT_REVOKED)
944 _gnutls_buffer_append_str (&str, _("The certificate chain revoked. "));
946 if (status & GNUTLS_CERT_REVOCATION_DATA_TOO_OLD)
947 _gnutls_buffer_append_str (&str, _("The revocation data are too old. "));
949 if (status & GNUTLS_CERT_REVOCATION_DATA_INVALID)
950 _gnutls_buffer_append_str (&str, _("The revocation data are invalid. "));
952 if (status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)
953 _gnutls_buffer_append_str (&str, _("The revocation data are issued with a future date. "));
955 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
956 _gnutls_buffer_append_str (&str, _("The certificate issuer is unknown. "));
958 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
959 _gnutls_buffer_append_str (&str, _("The certificate issuer is not a CA. "));
961 else if (type == GNUTLS_CRT_OPENPGP)
963 _gnutls_buffer_append_str (&str, _("The certificate is not trusted. "));
965 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
966 _gnutls_buffer_append_str (&str, _("Could not find a signer of the certificate. "));
968 if (status & GNUTLS_CERT_REVOKED)
969 _gnutls_buffer_append_str (&str, _("The certificate is revoked. "));
972 if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
973 _gnutls_buffer_append_str (&str, _("The certificate chain uses insecure algorithm. "));
975 if (status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE)
976 _gnutls_buffer_append_str (&str, _("The certificate chain violates the signer's constraints. "));
978 if (status & GNUTLS_CERT_NOT_ACTIVATED)
979 _gnutls_buffer_append_str (&str, _("The certificate chain uses not yet valid certificate. "));
981 if (status & GNUTLS_CERT_EXPIRED)
982 _gnutls_buffer_append_str (&str, _("The certificate chain uses expired certificate. "));
984 if (status & GNUTLS_CERT_SIGNATURE_FAILURE)
985 _gnutls_buffer_append_str (&str, _("The signature in the certificate is invalid. "));
987 if (status & GNUTLS_CERT_UNEXPECTED_OWNER)
988 _gnutls_buffer_append_str (&str, _("The name in the certificate does not match the expected. "));
990 ret = _gnutls_buffer_to_datum( &str, out);
991 if (out->size > 0) out->size--;
993 return ret;