certtool is able to set certificate policies via a template
[gnutls.git] / lib / gnutls_cert.c
blobc451ef5c59ce25e237b5b6663c0627a0113618cb
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 store
645 * the status in the @status variable as a bitwise or'd gnutls_certificate_status_t
646 * values or zero if the certificate is trusted. Note that value in @status
647 * is set only when the return value of this function is success (i.e, failure
648 * to trust a certificate does not imply a negative return value).
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(),
659 * or use gnutls_certificate_verify_peers3().
661 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
664 gnutls_certificate_verify_peers2 (gnutls_session_t session,
665 unsigned int *status)
667 cert_auth_info_t info;
669 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
671 info = _gnutls_get_auth_info (session);
672 if (info == NULL)
674 return GNUTLS_E_NO_CERTIFICATE_FOUND;
677 if (info->raw_certificate_list == NULL || info->ncerts == 0)
678 return GNUTLS_E_NO_CERTIFICATE_FOUND;
680 switch (gnutls_certificate_type_get (session))
682 case GNUTLS_CRT_X509:
683 return _gnutls_x509_cert_verify_peers (session, NULL, status);
684 #ifdef ENABLE_OPENPGP
685 case GNUTLS_CRT_OPENPGP:
686 return _gnutls_openpgp_crt_verify_peers (session, NULL, status);
687 #endif
688 default:
689 return GNUTLS_E_INVALID_REQUEST;
694 * gnutls_certificate_verify_peers3:
695 * @session: is a gnutls session
696 * @hostname: is the expected name of the peer; may be %NULL
697 * @status: is the output of the verification
699 * This function will verify the peer's certificate and store the
700 * status in the @status variable as a bitwise or'd gnutls_certificate_status_t
701 * values or zero if the certificate is trusted. Note that value in @status
702 * is set only when the return value of this function is success (i.e, failure
703 * to trust a certificate does not imply a negative return value).
705 * If the @hostname provided is non-NULL then this function will compare
706 * the hostname in the certificate against the given. If they do not match
707 * the %GNUTLS_CERT_UNEXPECTED_OWNER status flag will be set.
709 * If available the OCSP Certificate Status extension will be
710 * utilized by this function.
712 * To avoid denial of service attacks some
713 * default upper limits regarding the certificate key size and chain
714 * size are set. To override them use gnutls_certificate_set_verify_limits().
716 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
718 * Since: 3.1.4
721 gnutls_certificate_verify_peers3 (gnutls_session_t session,
722 const char* hostname,
723 unsigned int *status)
725 cert_auth_info_t info;
727 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
729 info = _gnutls_get_auth_info (session);
730 if (info == NULL)
732 return GNUTLS_E_NO_CERTIFICATE_FOUND;
735 if (info->raw_certificate_list == NULL || info->ncerts == 0)
736 return GNUTLS_E_NO_CERTIFICATE_FOUND;
738 switch (gnutls_certificate_type_get (session))
740 case GNUTLS_CRT_X509:
741 return _gnutls_x509_cert_verify_peers (session, hostname, status);
742 #ifdef ENABLE_OPENPGP
743 case GNUTLS_CRT_OPENPGP:
744 return _gnutls_openpgp_crt_verify_peers (session, hostname, status);
745 #endif
746 default:
747 return GNUTLS_E_INVALID_REQUEST;
752 * gnutls_certificate_expiration_time_peers:
753 * @session: is a gnutls session
755 * This function will return the peer's certificate expiration time.
757 * Returns: (time_t)-1 on error.
759 * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
761 time_t
762 gnutls_certificate_expiration_time_peers (gnutls_session_t session)
764 cert_auth_info_t info;
766 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
768 info = _gnutls_get_auth_info (session);
769 if (info == NULL)
771 return (time_t) - 1;
774 if (info->raw_certificate_list == NULL || info->ncerts == 0)
776 gnutls_assert ();
777 return (time_t) - 1;
780 switch (gnutls_certificate_type_get (session))
782 case GNUTLS_CRT_X509:
783 return
784 _gnutls_x509_get_raw_crt_expiration_time (&info->raw_certificate_list
785 [0]);
786 #ifdef ENABLE_OPENPGP
787 case GNUTLS_CRT_OPENPGP:
788 return
789 _gnutls_openpgp_get_raw_key_expiration_time
790 (&info->raw_certificate_list[0]);
791 #endif
792 default:
793 return (time_t) - 1;
798 * gnutls_certificate_activation_time_peers:
799 * @session: is a gnutls session
801 * This function will return the peer's certificate activation time.
802 * This is the creation time for openpgp keys.
804 * Returns: (time_t)-1 on error.
806 * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
808 time_t
809 gnutls_certificate_activation_time_peers (gnutls_session_t session)
811 cert_auth_info_t info;
813 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
815 info = _gnutls_get_auth_info (session);
816 if (info == NULL)
818 return (time_t) - 1;
821 if (info->raw_certificate_list == NULL || info->ncerts == 0)
823 gnutls_assert ();
824 return (time_t) - 1;
827 switch (gnutls_certificate_type_get (session))
829 case GNUTLS_CRT_X509:
830 return
831 _gnutls_x509_get_raw_crt_activation_time (&info->raw_certificate_list
832 [0]);
833 #ifdef ENABLE_OPENPGP
834 case GNUTLS_CRT_OPENPGP:
835 return
836 _gnutls_openpgp_get_raw_key_creation_time (&info->raw_certificate_list
837 [0]);
838 #endif
839 default:
840 return (time_t) - 1;
845 * gnutls_sign_callback_set:
846 * @session: is a gnutls session
847 * @sign_func: function pointer to application's sign callback.
848 * @userdata: void pointer that will be passed to sign callback.
850 * Set the callback function. The function must have this prototype:
852 * typedef int (*gnutls_sign_func) (gnutls_session_t session,
853 * void *userdata,
854 * gnutls_certificate_type_t cert_type,
855 * const gnutls_datum_t * cert,
856 * const gnutls_datum_t * hash,
857 * gnutls_datum_t * signature);
859 * The @userdata parameter is passed to the @sign_func verbatim, and
860 * can be used to store application-specific data needed in the
861 * callback function. See also gnutls_sign_callback_get().
863 * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess like gnutls_privkey_import_ext() instead.
865 void
866 gnutls_sign_callback_set (gnutls_session_t session,
867 gnutls_sign_func sign_func, void *userdata)
869 session->internals.sign_func = sign_func;
870 session->internals.sign_func_userdata = userdata;
874 * gnutls_sign_callback_get:
875 * @session: is a gnutls session
876 * @userdata: if non-%NULL, will be set to abstract callback pointer.
878 * Retrieve the callback function, and its userdata pointer.
880 * Returns: The function pointer set by gnutls_sign_callback_set(), or
881 * if not set, %NULL.
883 * Deprecated: Use the PKCS 11 interfaces instead.
885 gnutls_sign_func
886 gnutls_sign_callback_get (gnutls_session_t session, void **userdata)
888 if (userdata)
889 *userdata = session->internals.sign_func_userdata;
890 return session->internals.sign_func;
893 /* returns error if the certificate has different algorithm than
894 * the given key parameters.
897 _gnutls_check_key_cert_match (gnutls_certificate_credentials_t res)
899 int pk = gnutls_pubkey_get_pk_algorithm(res->certs[res->ncerts-1].cert_list[0].pubkey, NULL);
900 int pk2 = gnutls_privkey_get_pk_algorithm (res->pkey[res->ncerts - 1], NULL);
902 if (pk2 != pk)
904 gnutls_assert ();
905 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
908 return 0;
912 * gnutls_certificate_verification_status_print:
913 * @status: The status flags to be printed
914 * @type: The certificate type
915 * @out: Newly allocated datum with (0) terminated string.
916 * @flags: should be zero
918 * This function will pretty print the status of a verification
919 * process -- eg. the one obtained by gnutls_certificate_verify_peers3().
921 * The output @out needs to be deallocated using gnutls_free().
923 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
924 * negative error value.
926 * Since: 3.1.4
929 gnutls_certificate_verification_status_print (unsigned int status,
930 gnutls_certificate_type_t type,
931 gnutls_datum_t * out, unsigned int flags)
933 gnutls_buffer_st str;
934 int ret;
936 _gnutls_buffer_init (&str);
938 if (status == 0)
939 _gnutls_buffer_append_str (&str, _("The certificate is trusted. "));
940 else
941 _gnutls_buffer_append_str (&str, _("The certificate is NOT trusted. "));
943 if (type == GNUTLS_CRT_X509)
945 if (status & GNUTLS_CERT_REVOKED)
946 _gnutls_buffer_append_str (&str, _("The certificate chain revoked. "));
948 if (status & GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED)
949 _gnutls_buffer_append_str (&str, _("The revocation data are old and have been superseded. "));
951 if (status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)
952 _gnutls_buffer_append_str (&str, _("The revocation data are issued with a future date. "));
954 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
955 _gnutls_buffer_append_str (&str, _("The certificate issuer is unknown. "));
957 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
958 _gnutls_buffer_append_str (&str, _("The certificate issuer is not a CA. "));
960 else if (type == GNUTLS_CRT_OPENPGP)
962 _gnutls_buffer_append_str (&str, _("The certificate is not trusted. "));
964 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
965 _gnutls_buffer_append_str (&str, _("Could not find a signer of the certificate. "));
967 if (status & GNUTLS_CERT_REVOKED)
968 _gnutls_buffer_append_str (&str, _("The certificate is revoked. "));
971 if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
972 _gnutls_buffer_append_str (&str, _("The certificate chain uses insecure algorithm. "));
974 if (status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE)
975 _gnutls_buffer_append_str (&str, _("The certificate chain violates the signer's constraints. "));
977 if (status & GNUTLS_CERT_NOT_ACTIVATED)
978 _gnutls_buffer_append_str (&str, _("The certificate chain uses not yet valid certificate. "));
980 if (status & GNUTLS_CERT_EXPIRED)
981 _gnutls_buffer_append_str (&str, _("The certificate chain uses expired certificate. "));
983 if (status & GNUTLS_CERT_SIGNATURE_FAILURE)
984 _gnutls_buffer_append_str (&str, _("The signature in the certificate is invalid. "));
986 if (status & GNUTLS_CERT_UNEXPECTED_OWNER)
987 _gnutls_buffer_append_str (&str, _("The name in the certificate does not match the expected. "));
989 ret = _gnutls_buffer_to_datum( &str, out);
990 if (out->size > 0) out->size--;
992 return ret;