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 2.1 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
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/verify-high.h>
41 #include "x509/x509_int.h"
43 #include "openpgp/gnutls_openpgp.h"
46 #define _(String) dgettext (PACKAGE, String)
49 * gnutls_certificate_free_keys:
50 * @sc: is a #gnutls_certificate_credentials_t structure.
52 * This function will delete all the keys and the certificates associated
53 * with the given credentials. This function must not be called when a
54 * TLS negotiation that uses the credentials is in progress.
57 void gnutls_certificate_free_keys(gnutls_certificate_credentials_t sc)
61 for (i = 0; i < sc->ncerts; i++) {
62 for (j = 0; j < sc->certs[i].cert_list_length; j++) {
63 gnutls_pcert_deinit(&sc->certs[i].cert_list[j]);
65 gnutls_free(sc->certs[i].cert_list);
66 _gnutls_str_array_clear(&sc->certs[i].names);
69 gnutls_free(sc->certs);
72 for (i = 0; i < sc->ncerts; i++) {
73 gnutls_privkey_deinit(sc->pkey[i]);
76 gnutls_free(sc->pkey);
83 * gnutls_certificate_free_cas:
84 * @sc: is a #gnutls_certificate_credentials_t structure.
86 * This function will delete all the CAs associated with the given
87 * credentials. Servers that do not use
88 * gnutls_certificate_verify_peers2() may call this to save some
91 void gnutls_certificate_free_cas(gnutls_certificate_credentials_t sc)
93 /* FIXME: do nothing for now */
98 * gnutls_certificate_get_issuer:
99 * @sc: is a #gnutls_certificate_credentials_t structure.
100 * @cert: is the certificate to find issuer for
101 * @issuer: Will hold the issuer if any. Should be treated as constant.
104 * This function will return the issuer of a given certificate.
106 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
107 * negative error value.
112 gnutls_certificate_get_issuer(gnutls_certificate_credentials_t sc,
113 gnutls_x509_crt_t cert,
114 gnutls_x509_crt_t * issuer,
117 return gnutls_x509_trust_list_get_issuer(sc->tlist, cert, issuer,
122 * gnutls_certificate_get_crt_raw:
123 * @sc: is a #gnutls_certificate_credentials_t structure.
124 * @idx1: the index of the certificate if multiple are present
125 * @idx2: the index in the certificate list. Zero gives the server's certificate.
126 * @cert: Will hold the DER encoded certificate.
128 * This function will return the DER encoded certificate of the
129 * server or any other certificate on its certificate chain (based on @idx2).
130 * The returned data should be treated as constant and only accessible during the lifetime
133 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
134 * negative error value. In case the indexes are out of bounds %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
140 gnutls_certificate_get_crt_raw(gnutls_certificate_credentials_t sc,
142 unsigned idx2, gnutls_datum_t * cert)
144 if (idx1 >= sc->ncerts)
147 (GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
149 if (idx2 >= sc->certs[idx1].cert_list_length)
152 (GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
154 cert->data = sc->certs[idx1].cert_list[idx2].cert.data;
155 cert->size = sc->certs[idx1].cert_list[idx2].cert.size;
161 * gnutls_certificate_free_ca_names:
162 * @sc: is a #gnutls_certificate_credentials_t structure.
164 * This function will delete all the CA name in the given
165 * credentials. Clients may call this to save some memory since in
166 * client side the CA names are not used. Servers might want to use
167 * this function if a large list of trusted CAs is present and
168 * sending the names of it would just consume bandwidth without providing
169 * information to client.
171 * CA names are used by servers to advertise the CAs they support to
174 void gnutls_certificate_free_ca_names(gnutls_certificate_credentials_t sc)
176 _gnutls_free_datum(&sc->tlist->x509_rdn_sequence);
181 * gnutls_certificate_free_credentials:
182 * @sc: is a #gnutls_certificate_credentials_t structure.
184 * This structure is complex enough to manipulate directly thus this
185 * helper function is provided in order to free (deallocate) it.
187 * This function does not free any temporary parameters associated
188 * with this structure (ie RSA and DH parameters are not freed by this
192 gnutls_certificate_free_credentials(gnutls_certificate_credentials_t sc)
194 gnutls_x509_trust_list_deinit(sc->tlist, 1);
195 gnutls_certificate_free_keys(sc);
196 gnutls_free(sc->ocsp_response_file);
197 memset(sc->pin_tmp, 0, sizeof(sc->pin_tmp));
198 #ifdef ENABLE_OPENPGP
199 gnutls_openpgp_keyring_deinit(sc->keyring);
207 * gnutls_certificate_allocate_credentials:
208 * @res: is a pointer to a #gnutls_certificate_credentials_t structure.
210 * This structure is complex enough to manipulate directly thus this
211 * helper function is provided in order to allocate it.
213 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
216 gnutls_certificate_allocate_credentials(gnutls_certificate_credentials_t *
221 *res = gnutls_calloc(1, sizeof(certificate_credentials_st));
224 return GNUTLS_E_MEMORY_ERROR;
226 ret = gnutls_x509_trust_list_init(&(*res)->tlist, 0);
230 return GNUTLS_E_MEMORY_ERROR;
232 (*res)->verify_bits = DEFAULT_MAX_VERIFY_BITS;
233 (*res)->verify_depth = DEFAULT_MAX_VERIFY_DEPTH;
239 /* returns the KX algorithms that are supported by a
240 * certificate. (Eg a certificate with RSA params, supports
241 * GNUTLS_KX_RSA algorithm).
242 * This function also uses the KeyUsage field of the certificate
243 * extensions in order to disable unneded algorithms.
246 _gnutls_selected_cert_supported_kx(gnutls_session_t session,
247 gnutls_kx_algorithm_t * alg,
251 gnutls_pk_algorithm_t pk, cert_pk;
252 gnutls_pcert_st *cert;
255 if (session->internals.selected_cert_list_length == 0) {
260 cert = &session->internals.selected_cert_list[0];
261 cert_pk = gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
264 for (kx = 0; kx < MAX_ALGOS; kx++) {
265 pk = _gnutls_map_pk_get_pk(kx);
267 /* then check key usage */
268 if (_gnutls_check_key_usage(cert, kx) == 0) {
275 (GNUTLS_E_INTERNAL_ERROR);
282 return GNUTLS_E_INVALID_REQUEST;
292 * gnutls_certificate_server_set_request:
293 * @session: is a #gnutls_session_t structure.
294 * @req: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE
296 * This function specifies if we (in case of a server) are going to
297 * send a certificate request message to the client. If @req is
298 * GNUTLS_CERT_REQUIRE then the server will return an error if the
299 * peer does not provide a certificate. If you do not call this
300 * function then the client will not be asked to send a certificate.
303 gnutls_certificate_server_set_request(gnutls_session_t session,
304 gnutls_certificate_request_t req)
306 session->internals.send_cert_req = req;
310 * gnutls_certificate_client_set_retrieve_function:
311 * @cred: is a #gnutls_certificate_credentials_t structure.
312 * @func: is the callback function
314 * This function sets a callback to be called in order to retrieve the
315 * certificate to be used in the handshake.
316 * You are advised to use gnutls_certificate_set_retrieve_function2() because it
317 * is much more efficient in the processing it requires from gnutls.
319 * The callback's function prototype is:
320 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
321 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr_st* st);
323 * @req_ca_cert is only used in X.509 certificates.
324 * Contains a list with the CA names that the server considers trusted.
325 * Normally we should send a certificate that is signed
326 * by one of these CAs. These names are DER encoded. To get a more
327 * meaningful value use the function gnutls_x509_rdn_get().
329 * @pk_algos contains a list with server's acceptable signature algorithms.
330 * The certificate returned should support the server's given algorithms.
332 * @st should contain the certificates and private keys.
334 * If the callback function is provided then gnutls will call it, in the
335 * handshake, if a certificate is requested by the server (and after the
336 * certificate request message has been received).
338 * The callback function should set the certificate list to be sent,
339 * and return 0 on success. If no certificate was selected then the
340 * number of certificates should be set to zero. The value (-1)
341 * indicates error and the handshake will be terminated.
343 void gnutls_certificate_client_set_retrieve_function
344 (gnutls_certificate_credentials_t cred,
345 gnutls_certificate_client_retrieve_function * func) {
346 cred->client_get_cert_callback = func;
350 * gnutls_certificate_server_set_retrieve_function:
351 * @cred: is a #gnutls_certificate_credentials_t structure.
352 * @func: is the callback function
354 * This function sets a callback to be called in order to retrieve the
355 * certificate to be used in the handshake.
356 * You are advised to use gnutls_certificate_set_retrieve_function2() because it
357 * is much more efficient in the processing it requires from gnutls.
359 * The callback's function prototype is:
360 * int (*callback)(gnutls_session_t, gnutls_retr_st* st);
362 * @st should contain the certificates and private keys.
364 * If the callback function is provided then gnutls will call it, in the
365 * handshake, after the certificate request message has been received.
367 * The callback function should set the certificate list to be sent, and
368 * return 0 on success. The value (-1) indicates error and the handshake
369 * will be terminated.
371 void gnutls_certificate_server_set_retrieve_function
372 (gnutls_certificate_credentials_t cred,
373 gnutls_certificate_server_retrieve_function * func) {
374 cred->server_get_cert_callback = func;
378 * gnutls_certificate_set_retrieve_function:
379 * @cred: is a #gnutls_certificate_credentials_t structure.
380 * @func: is the callback function
382 * This function sets a callback to be called in order to retrieve the
383 * certificate to be used in the handshake. You are advised
384 * to use gnutls_certificate_set_retrieve_function2() because it
385 * is much more efficient in the processing it requires from gnutls.
387 * The callback's function prototype is:
388 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
389 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr2_st* st);
391 * @req_ca_cert is only used in X.509 certificates.
392 * Contains a list with the CA names that the server considers trusted.
393 * Normally we should send a certificate that is signed
394 * by one of these CAs. These names are DER encoded. To get a more
395 * meaningful value use the function gnutls_x509_rdn_get().
397 * @pk_algos contains a list with server's acceptable signature algorithms.
398 * The certificate returned should support the server's given algorithms.
400 * @st should contain the certificates and private keys.
402 * If the callback function is provided then gnutls will call it, in the
403 * handshake, after the certificate request message has been received.
405 * In server side pk_algos and req_ca_dn are NULL.
407 * The callback function should set the certificate list to be sent,
408 * and return 0 on success. If no certificate was selected then the
409 * number of certificates should be set to zero. The value (-1)
410 * indicates error and the handshake will be terminated.
414 void gnutls_certificate_set_retrieve_function
415 (gnutls_certificate_credentials_t cred,
416 gnutls_certificate_retrieve_function * func) {
417 cred->get_cert_callback = func;
421 * gnutls_certificate_set_retrieve_function2:
422 * @cred: is a #gnutls_certificate_credentials_t structure.
423 * @func: is the callback function
425 * This function sets a callback to be called in order to retrieve the
426 * certificate to be used in the handshake.
428 * The callback's function prototype is:
429 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
430 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_pcert_st** pcert,
431 * unsigned int *pcert_length, gnutls_privkey_t * pkey);
433 * @req_ca_cert is only used in X.509 certificates.
434 * Contains a list with the CA names that the server considers trusted.
435 * Normally we should send a certificate that is signed
436 * by one of these CAs. These names are DER encoded. To get a more
437 * meaningful value use the function gnutls_x509_rdn_get().
439 * @pk_algos contains a list with server's acceptable signature algorithms.
440 * The certificate returned should support the server's given algorithms.
442 * @pcert should contain a single certificate and public or a list of them.
444 * @pcert_length is the size of the previous list.
446 * @pkey is the private key.
448 * If the callback function is provided then gnutls will call it, in the
449 * handshake, after the certificate request message has been received.
451 * In server side pk_algos and req_ca_dn are NULL.
453 * The callback function should set the certificate list to be sent,
454 * and return 0 on success. If no certificate was selected then the
455 * number of certificates should be set to zero. The value (-1)
456 * indicates error and the handshake will be terminated.
460 void gnutls_certificate_set_retrieve_function2
461 (gnutls_certificate_credentials_t cred,
462 gnutls_certificate_retrieve_function2 * func) {
463 cred->get_cert_callback2 = func;
467 * gnutls_certificate_set_verify_function:
468 * @cred: is a #gnutls_certificate_credentials_t structure.
469 * @func: is the callback function
471 * This function sets a callback to be called when peer's certificate
472 * has been received in order to verify it on receipt rather than
473 * doing after the handshake is completed.
475 * The callback's function prototype is:
476 * int (*callback)(gnutls_session_t);
478 * If the callback function is provided then gnutls will call it, in the
479 * handshake, just after the certificate message has been received.
480 * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
481 * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
484 * The callback function should return 0 for the handshake to continue
485 * or non-zero to terminate.
490 gnutls_certificate_set_verify_function
491 (gnutls_certificate_credentials_t cred,
492 gnutls_certificate_verify_function * func) {
493 cred->verify_callback = func;
497 * _gnutls_x509_extract_certificate_activation_time - return the peer's certificate activation time
498 * @cert: should contain an X.509 DER encoded certificate
500 * This function will return the certificate's activation time in UNIX time
501 * (ie seconds since 00:00:00 UTC January 1, 1970).
503 * Returns a (time_t) -1 in case of an error.
507 _gnutls_x509_get_raw_crt_activation_time(const gnutls_datum_t * cert)
509 gnutls_x509_crt_t xcert;
512 result = gnutls_x509_crt_init(&xcert);
516 result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
518 gnutls_x509_crt_deinit(xcert);
522 result = gnutls_x509_crt_get_activation_time(xcert);
524 gnutls_x509_crt_deinit(xcert);
530 * gnutls_x509_extract_certificate_expiration_time:
531 * @cert: should contain an X.509 DER encoded certificate
533 * This function will return the certificate's expiration time in UNIX
534 * time (ie seconds since 00:00:00 UTC January 1, 1970). Returns a
536 * (time_t) -1 in case of an error.
540 _gnutls_x509_get_raw_crt_expiration_time(const gnutls_datum_t * cert)
542 gnutls_x509_crt_t xcert;
545 result = gnutls_x509_crt_init(&xcert);
549 result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
551 gnutls_x509_crt_deinit(xcert);
555 result = gnutls_x509_crt_get_expiration_time(xcert);
557 gnutls_x509_crt_deinit(xcert);
562 #ifdef ENABLE_OPENPGP
564 * _gnutls_openpgp_crt_verify_peers - return the peer's certificate status
565 * @session: is a gnutls session
567 * This function will try to verify the peer's certificate and return its status (TRUSTED, INVALID etc.).
568 * Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
571 _gnutls_openpgp_crt_verify_peers(gnutls_session_t session,
572 const char *hostname,
573 unsigned int *status)
575 cert_auth_info_t info;
576 gnutls_certificate_credentials_t cred;
577 int peer_certificate_list_size, ret;
578 unsigned int verify_flags;
580 CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
582 info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
584 return GNUTLS_E_INVALID_REQUEST;
586 cred = (gnutls_certificate_credentials_t)
587 _gnutls_get_cred(session, GNUTLS_CRD_CERTIFICATE);
590 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
593 if (info->raw_certificate_list == NULL || info->ncerts == 0) {
595 return GNUTLS_E_NO_CERTIFICATE_FOUND;
598 verify_flags = cred->verify_flags | session->internals.priorities.additional_verify_flags;
600 /* generate a list of gnutls_certs based on the auth info
603 peer_certificate_list_size = info->ncerts;
605 if (peer_certificate_list_size != 1) {
607 return GNUTLS_E_INTERNAL_ERROR;
610 /* Verify certificate
613 _gnutls_openpgp_verify_key(cred, hostname,
614 &info->raw_certificate_list[0],
615 peer_certificate_list_size,
629 * gnutls_certificate_verify_peers2:
630 * @session: is a gnutls session
631 * @status: is the output of the verification
633 * This function will verify the peer's certificate and store
634 * the status in the @status variable as a bitwise or'd gnutls_certificate_status_t
635 * values or zero if the certificate is trusted. Note that value in @status
636 * is set only when the return value of this function is success (i.e, failure
637 * to trust a certificate does not imply a negative return value).
638 * The default verification flags used by this function can be overridden
639 * using gnutls_certificate_set_verify_flags().
641 * This function will take into account the OCSP Certificate Status TLS extension,
642 * as well as the following X.509 certificate extensions: Name Constraints,
643 * Key Usage, and Basic Constraints (pathlen).
645 * To avoid denial of service attacks some
646 * default upper limits regarding the certificate key size and chain
647 * size are set. To override them use gnutls_certificate_set_verify_limits().
649 * Note that you must also check the peer's name in order to check if
650 * the verified certificate belongs to the actual peer, see gnutls_x509_crt_check_hostname(),
651 * or use gnutls_certificate_verify_peers3().
653 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
656 gnutls_certificate_verify_peers2(gnutls_session_t session,
657 unsigned int *status)
659 return gnutls_certificate_verify_peers(session, NULL, 0, status);
663 * gnutls_certificate_verify_peers3:
664 * @session: is a gnutls session
665 * @hostname: is the expected name of the peer; may be %NULL
666 * @status: is the output of the verification
668 * This function will verify the peer's certificate and store the
669 * status in the @status variable as a bitwise or'd gnutls_certificate_status_t
670 * values or zero if the certificate is trusted. Note that value in @status
671 * is set only when the return value of this function is success (i.e, failure
672 * to trust a certificate does not imply a negative return value).
673 * The default verification flags used by this function can be overridden
674 * using gnutls_certificate_set_verify_flags(). See the documentation
675 * of gnutls_certificate_verify_peers2() for details in the verification process.
677 * If the @hostname provided is non-NULL then this function will compare
678 * the hostname in the certificate against the given. The comparison will
679 * be accurate for ascii names; non-ascii names are compared byte-by-byte.
680 * If names do not match the %GNUTLS_CERT_UNEXPECTED_OWNER status flag will be set.
682 * In order to verify the purpose of the end-certificate (by checking the extended
683 * key usage), use gnutls_certificate_verify_peers().
685 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
690 gnutls_certificate_verify_peers3(gnutls_session_t session,
691 const char *hostname,
692 unsigned int *status)
694 gnutls_typed_vdata_st data;
696 data.type = GNUTLS_DT_DNS_HOSTNAME;
698 data.data = (void*)hostname;
700 return gnutls_certificate_verify_peers(session, &data, 1, status);
704 * gnutls_certificate_verify_peers:
705 * @session: is a gnutls session
706 * @data: an array of typed data
707 * @elements: the number of data elements
708 * @status: is the output of the verification
710 * This function will verify the peer's certificate and store the
711 * status in the @status variable as a bitwise or'd gnutls_certificate_status_t
712 * values or zero if the certificate is trusted. Note that value in @status
713 * is set only when the return value of this function is success (i.e, failure
714 * to trust a certificate does not imply a negative return value).
715 * The default verification flags used by this function can be overridden
716 * using gnutls_certificate_set_verify_flags(). See the documentation
717 * of gnutls_certificate_verify_peers2() for details in the verification process.
719 * The acceptable data types are %GNUTLS_DT_DNS_HOSTNAME and %GNUTLS_DT_KEY_PURPOSE_OID.
720 * If a DNS hostname is provided then this function will compare
721 * the hostname in the certificate against the given. The comparison will
722 * be accurate for ascii names; non-ascii names are compared byte-by-byte.
723 * If names do not match the %GNUTLS_CERT_UNEXPECTED_OWNER status flag will be set.
725 * If a key purpose OID is provided and the end-certificate contains the extended key
726 * usage PKIX extension, it will be required to be have the provided key purpose
727 * (e.g., %GNUTLS_KP_TLS_WWW_SERVER), or be marked for any purpose, otherwise
728 * verification will fail with %GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE status.
730 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
735 gnutls_certificate_verify_peers(gnutls_session_t session,
736 gnutls_typed_vdata_st * data,
737 unsigned int elements,
738 unsigned int *status)
740 cert_auth_info_t info;
741 const char *hostname = NULL;
742 const char *purpose = NULL;
745 CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
747 info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
749 return GNUTLS_E_NO_CERTIFICATE_FOUND;
752 if (info->raw_certificate_list == NULL || info->ncerts == 0)
753 return GNUTLS_E_NO_CERTIFICATE_FOUND;
755 for (i=0;i<elements;i++) {
756 if (data[i].type == GNUTLS_DT_DNS_HOSTNAME) {
757 hostname = (void*)data[i].data;
758 } else if (data[i].type == GNUTLS_DT_KEY_PURPOSE_OID) {
759 purpose = (void*)data[i].data;
761 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
765 switch (gnutls_certificate_type_get(session)) {
766 case GNUTLS_CRT_X509:
767 return _gnutls_x509_cert_verify_peers(session, hostname,
770 #ifdef ENABLE_OPENPGP
771 case GNUTLS_CRT_OPENPGP:
772 return _gnutls_openpgp_crt_verify_peers(session, hostname,
776 return GNUTLS_E_INVALID_REQUEST;
781 * gnutls_certificate_expiration_time_peers:
782 * @session: is a gnutls session
784 * This function will return the peer's certificate expiration time.
786 * Returns: (time_t)-1 on error.
788 * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
790 time_t gnutls_certificate_expiration_time_peers(gnutls_session_t session)
792 cert_auth_info_t info;
794 CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
796 info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
801 if (info->raw_certificate_list == NULL || info->ncerts == 0) {
806 switch (gnutls_certificate_type_get(session)) {
807 case GNUTLS_CRT_X509:
809 _gnutls_x509_get_raw_crt_expiration_time(&info->
812 #ifdef ENABLE_OPENPGP
813 case GNUTLS_CRT_OPENPGP:
815 _gnutls_openpgp_get_raw_key_expiration_time
816 (&info->raw_certificate_list[0]);
824 * gnutls_certificate_activation_time_peers:
825 * @session: is a gnutls session
827 * This function will return the peer's certificate activation time.
828 * This is the creation time for openpgp keys.
830 * Returns: (time_t)-1 on error.
832 * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
834 time_t gnutls_certificate_activation_time_peers(gnutls_session_t session)
836 cert_auth_info_t info;
838 CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
840 info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
845 if (info->raw_certificate_list == NULL || info->ncerts == 0) {
850 switch (gnutls_certificate_type_get(session)) {
851 case GNUTLS_CRT_X509:
853 _gnutls_x509_get_raw_crt_activation_time(&info->
856 #ifdef ENABLE_OPENPGP
857 case GNUTLS_CRT_OPENPGP:
859 _gnutls_openpgp_get_raw_key_creation_time(&info->
869 * gnutls_sign_callback_set:
870 * @session: is a gnutls session
871 * @sign_func: function pointer to application's sign callback.
872 * @userdata: void pointer that will be passed to sign callback.
874 * Set the callback function. The function must have this prototype:
876 * typedef int (*gnutls_sign_func) (gnutls_session_t session,
878 * gnutls_certificate_type_t cert_type,
879 * const gnutls_datum_t * cert,
880 * const gnutls_datum_t * hash,
881 * gnutls_datum_t * signature);
883 * The @userdata parameter is passed to the @sign_func verbatim, and
884 * can be used to store application-specific data needed in the
885 * callback function. See also gnutls_sign_callback_get().
887 * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess like gnutls_privkey_import_ext() instead.
890 gnutls_sign_callback_set(gnutls_session_t session,
891 gnutls_sign_func sign_func, void *userdata)
893 session->internals.sign_func = sign_func;
894 session->internals.sign_func_userdata = userdata;
898 * gnutls_sign_callback_get:
899 * @session: is a gnutls session
900 * @userdata: if non-%NULL, will be set to abstract callback pointer.
902 * Retrieve the callback function, and its userdata pointer.
904 * Returns: The function pointer set by gnutls_sign_callback_set(), or
907 * Deprecated: Use the PKCS 11 interfaces instead.
910 gnutls_sign_callback_get(gnutls_session_t session, void **userdata)
913 *userdata = session->internals.sign_func_userdata;
914 return session->internals.sign_func;
917 #define TEST_TEXT "test text"
918 /* returns error if the certificate has different algorithm than
919 * the given key parameters.
921 int _gnutls_check_key_cert_match(gnutls_certificate_credentials_t res)
923 gnutls_datum_t test = {(void*)TEST_TEXT, sizeof(TEST_TEXT)-1};
924 gnutls_datum_t sig = {NULL, 0};
928 gnutls_pubkey_get_pk_algorithm(res->certs[res->ncerts - 1].
929 cert_list[0].pubkey, NULL);
931 gnutls_privkey_get_pk_algorithm(res->pkey[res->ncerts - 1],
936 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
939 /* now check if keys really match. We use the sign/verify approach
940 * because we cannot always obtain the parameters from the abstract
941 * keys (e.g. PKCS #11). */
942 ret = gnutls_privkey_sign_data(res->pkey[res->ncerts - 1],
943 GNUTLS_DIG_SHA256, 0, &test, &sig);
945 /* for some reason we couldn't sign that. That shouldn't have
946 * happened, but since it did, report the issue and do not
947 * try the key matching test */
948 _gnutls_debug_log("%s: failed signing\n", __func__);
952 ret = gnutls_pubkey_verify_data2(res->certs[res->ncerts - 1].cert_list[0].pubkey,
953 gnutls_pk_to_sign(pk, GNUTLS_DIG_SHA256),
956 gnutls_free(sig.data);
959 return gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
966 * gnutls_certificate_verification_status_print:
967 * @status: The status flags to be printed
968 * @type: The certificate type
969 * @out: Newly allocated datum with (0) terminated string.
970 * @flags: should be zero
972 * This function will pretty print the status of a verification
973 * process -- eg. the one obtained by gnutls_certificate_verify_peers3().
975 * The output @out needs to be deallocated using gnutls_free().
977 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
978 * negative error value.
983 gnutls_certificate_verification_status_print(unsigned int status,
984 gnutls_certificate_type_t
985 type, gnutls_datum_t * out,
988 gnutls_buffer_st str;
991 _gnutls_buffer_init(&str);
994 _gnutls_buffer_append_str(&str,
996 ("The certificate is trusted. "));
998 _gnutls_buffer_append_str(&str,
1000 ("The certificate is NOT trusted. "));
1002 if (type == GNUTLS_CRT_X509) {
1003 if (status & GNUTLS_CERT_REVOKED)
1004 _gnutls_buffer_append_str(&str,
1006 ("The certificate chain is revoked. "));
1008 if (status & GNUTLS_CERT_MISMATCH)
1009 _gnutls_buffer_append_str(&str,
1011 ("The certificate doesn't match the local copy (TOFU). "));
1013 if (status & GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED)
1014 _gnutls_buffer_append_str(&str,
1016 ("The revocation data are old and have been superseded. "));
1018 if (status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)
1019 _gnutls_buffer_append_str(&str,
1021 ("The revocation data are issued with a future date. "));
1023 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
1024 _gnutls_buffer_append_str(&str,
1026 ("The certificate issuer is unknown. "));
1028 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
1029 _gnutls_buffer_append_str(&str,
1031 ("The certificate issuer is not a CA. "));
1032 } else if (type == GNUTLS_CRT_OPENPGP) {
1033 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
1034 _gnutls_buffer_append_str(&str,
1036 ("Could not find a signer of the certificate. "));
1038 if (status & GNUTLS_CERT_REVOKED)
1039 _gnutls_buffer_append_str(&str,
1041 ("The certificate is revoked. "));
1044 if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
1045 _gnutls_buffer_append_str(&str,
1047 ("The certificate chain uses insecure algorithm. "));
1049 if (status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE)
1050 _gnutls_buffer_append_str(&str,
1052 ("The certificate chain violates the signer's constraints. "));
1054 if (status & GNUTLS_CERT_NOT_ACTIVATED)
1055 _gnutls_buffer_append_str(&str,
1057 ("The certificate chain uses not yet valid certificate. "));
1059 if (status & GNUTLS_CERT_EXPIRED)
1060 _gnutls_buffer_append_str(&str,
1062 ("The certificate chain uses expired certificate. "));
1064 if (status & GNUTLS_CERT_SIGNATURE_FAILURE)
1065 _gnutls_buffer_append_str(&str,
1067 ("The signature in the certificate is invalid. "));
1069 if (status & GNUTLS_CERT_UNEXPECTED_OWNER)
1070 _gnutls_buffer_append_str(&str,
1072 ("The name in the certificate does not match the expected. "));
1074 ret = _gnutls_buffer_to_datum(&str, out);