2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 * 2010,2011 Free Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>
24 /* Some of the stuff needed for Certificate authentication is contained
28 #include <gnutls_int.h>
29 #include <gnutls_errors.h>
30 #include <auth/cert.h>
31 #include <gnutls_datum.h>
32 #include <gnutls_mpi.h>
33 #include <gnutls_global.h>
34 #include <algorithms.h>
35 #include <gnutls_dh.h>
36 #include <gnutls_str.h>
37 #include <gnutls_state.h>
38 #include <gnutls_auth.h>
39 #include <gnutls_x509.h>
40 #include <gnutls_str_array.h>
41 #include "x509/x509_int.h"
43 #include "openpgp/gnutls_openpgp.h"
47 * gnutls_certificate_free_keys:
48 * @sc: is a #gnutls_certificate_credentials_t structure.
50 * This function will delete all the keys and the certificates associated
51 * with the given credentials. This function must not be called when a
52 * TLS negotiation that uses the credentials is in progress.
56 gnutls_certificate_free_keys (gnutls_certificate_credentials_t sc
)
60 for (i
= 0; i
< sc
->ncerts
; i
++)
62 for (j
= 0; j
< sc
->certs
[i
].cert_list_length
; j
++)
64 gnutls_pcert_deinit (&sc
->certs
[i
].cert_list
[j
]);
66 gnutls_free (sc
->certs
[i
].cert_list
);
67 _gnutls_str_array_clear (&sc
->certs
[i
].names
);
70 gnutls_free (sc
->certs
);
73 for (i
= 0; i
< sc
->ncerts
; i
++)
75 gnutls_privkey_deinit (sc
->pkey
[i
]);
78 gnutls_free (sc
->pkey
);
85 * gnutls_certificate_free_cas:
86 * @sc: is a #gnutls_certificate_credentials_t structure.
88 * This function will delete all the CAs associated with the given
89 * credentials. Servers that do not use
90 * gnutls_certificate_verify_peers2() may call this to save some
94 gnutls_certificate_free_cas (gnutls_certificate_credentials_t sc
)
96 /* FIXME: do nothing for now */
101 * gnutls_certificate_get_issuer:
102 * @sc: is a #gnutls_certificate_credentials_t structure.
103 * @cert: is the certificate to find issuer for
104 * @issuer: Will hold the issuer if any. Should be treated as constant.
107 * This function will return the issuer of a given certificate.
109 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
110 * negative error value.
115 gnutls_certificate_get_issuer (gnutls_certificate_credentials_t sc
,
116 gnutls_x509_crt_t cert
, gnutls_x509_crt_t
* issuer
, unsigned int flags
)
118 return gnutls_x509_trust_list_get_issuer(sc
->tlist
, cert
, issuer
, flags
);
122 * gnutls_certificate_free_ca_names:
123 * @sc: is a #gnutls_certificate_credentials_t structure.
125 * This function will delete all the CA name in the given
126 * credentials. Clients may call this to save some memory since in
127 * client side the CA names are not used. Servers might want to use
128 * this function if a large list of trusted CAs is present and
129 * sending the names of it would just consume bandwidth without providing
130 * information to client.
132 * CA names are used by servers to advertize the CAs they support to
136 gnutls_certificate_free_ca_names (gnutls_certificate_credentials_t sc
)
138 _gnutls_free_datum (&sc
->x509_rdn_sequence
);
142 * _gnutls_certificate_get_rsa_params - Returns the RSA parameters pointer
143 * @rsa_params: holds the RSA parameters or NULL.
144 * @func: function to retrieve the parameters or NULL.
145 * @session: The session.
147 * This function will return the rsa parameters pointer.
150 _gnutls_certificate_get_rsa_params (gnutls_rsa_params_t rsa_params
,
151 gnutls_params_function
* func
,
152 gnutls_session_t session
)
154 gnutls_params_st params
;
157 if (session
->internals
.params
.rsa_params
)
159 return session
->internals
.params
.rsa_params
;
164 session
->internals
.params
.rsa_params
= rsa_params
;
168 ret
= func (session
, GNUTLS_PARAMS_RSA_EXPORT
, ¶ms
);
169 if (ret
== 0 && params
.type
== GNUTLS_PARAMS_RSA_EXPORT
)
171 session
->internals
.params
.rsa_params
= params
.params
.rsa_export
;
172 session
->internals
.params
.free_rsa_params
= params
.deinit
;
176 return session
->internals
.params
.rsa_params
;
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_certificate_free_ca_names (sc
);
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);
231 return GNUTLS_E_MEMORY_ERROR
;
233 (*res
)->verify_bits
= DEFAULT_VERIFY_BITS
;
234 (*res
)->verify_depth
= DEFAULT_VERIFY_DEPTH
;
240 /* returns the KX algorithms that are supported by a
241 * certificate. (Eg a certificate with RSA params, supports
242 * GNUTLS_KX_RSA algorithm).
243 * This function also uses the KeyUsage field of the certificate
244 * extensions in order to disable unneded algorithms.
247 _gnutls_selected_cert_supported_kx (gnutls_session_t session
,
248 gnutls_kx_algorithm_t
* alg
,
251 gnutls_kx_algorithm_t kx
;
252 gnutls_pk_algorithm_t pk
, cert_pk
;
253 gnutls_pcert_st
*cert
;
256 if (session
->internals
.selected_cert_list_length
== 0)
262 cert
= &session
->internals
.selected_cert_list
[0];
263 cert_pk
= gnutls_pubkey_get_pk_algorithm(cert
->pubkey
, NULL
);
266 for (kx
= 0; kx
< MAX_ALGOS
; kx
++)
268 pk
= _gnutls_map_pk_get_pk (kx
);
271 /* then check key usage */
272 if (_gnutls_check_key_usage (cert
, kx
) == 0)
278 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR
);
286 return GNUTLS_E_INVALID_REQUEST
;
296 * gnutls_certificate_server_set_request:
297 * @session: is a #gnutls_session_t structure.
298 * @req: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE
300 * This function specifies if we (in case of a server) are going to
301 * send a certificate request message to the client. If @req is
302 * GNUTLS_CERT_REQUIRE then the server will return an error if the
303 * peer does not provide a certificate. If you do not call this
304 * function then the client will not be asked to send a certificate.
307 gnutls_certificate_server_set_request (gnutls_session_t session
,
308 gnutls_certificate_request_t req
)
310 session
->internals
.send_cert_req
= req
;
314 * gnutls_certificate_client_set_retrieve_function:
315 * @cred: is a #gnutls_certificate_credentials_t structure.
316 * @func: is the callback function
318 * This function sets a callback to be called in order to retrieve the
319 * certificate to be used in the handshake.
320 * You are advised to use gnutls_certificate_set_retrieve_function2() because it
321 * is much more efficient in the processing it requires from gnutls.
323 * The callback's function prototype is:
324 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
325 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr_st* st);
327 * @req_ca_cert is only used in X.509 certificates.
328 * Contains a list with the CA names that the server considers trusted.
329 * Normally we should send a certificate that is signed
330 * by one of these CAs. These names are DER encoded. To get a more
331 * meaningful value use the function gnutls_x509_rdn_get().
333 * @pk_algos contains a list with server's acceptable signature algorithms.
334 * The certificate returned should support the server's given algorithms.
336 * @st should contain the certificates and private keys.
338 * If the callback function is provided then gnutls will call it, in the
339 * handshake, after the certificate request message has been received.
341 * The callback function should set the certificate list to be sent,
342 * and return 0 on success. If no certificate was selected then the
343 * number of certificates should be set to zero. The value (-1)
344 * indicates error and the handshake will be terminated.
346 void gnutls_certificate_client_set_retrieve_function
347 (gnutls_certificate_credentials_t cred
,
348 gnutls_certificate_client_retrieve_function
* func
)
350 cred
->client_get_cert_callback
= func
;
354 * gnutls_certificate_server_set_retrieve_function:
355 * @cred: is a #gnutls_certificate_credentials_t structure.
356 * @func: is the callback function
358 * This function sets a callback to be called in order to retrieve the
359 * certificate to be used in the handshake.
360 * You are advised to use gnutls_certificate_set_retrieve_function2() because it
361 * is much more efficient in the processing it requires from gnutls.
363 * The callback's function prototype is:
364 * int (*callback)(gnutls_session_t, gnutls_retr_st* st);
366 * @st should contain the certificates and private keys.
368 * If the callback function is provided then gnutls will call it, in the
369 * handshake, after the certificate request message has been received.
371 * The callback function should set the certificate list to be sent, and
372 * return 0 on success. The value (-1) indicates error and the handshake
373 * will be terminated.
375 void gnutls_certificate_server_set_retrieve_function
376 (gnutls_certificate_credentials_t cred
,
377 gnutls_certificate_server_retrieve_function
* func
)
379 cred
->server_get_cert_callback
= func
;
383 * gnutls_certificate_set_retrieve_function:
384 * @cred: is a #gnutls_certificate_credentials_t structure.
385 * @func: is the callback function
387 * This function sets a callback to be called in order to retrieve the
388 * certificate to be used in the handshake. You are advised
389 * to use gnutls_certificate_set_retrieve_function2() because it
390 * is much more efficient in the processing it requires from gnutls.
392 * The callback's function prototype is:
393 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
394 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr2_st* st);
396 * @req_ca_cert is only used in X.509 certificates.
397 * Contains a list with the CA names that the server considers trusted.
398 * Normally we should send a certificate that is signed
399 * by one of these CAs. These names are DER encoded. To get a more
400 * meaningful value use the function gnutls_x509_rdn_get().
402 * @pk_algos contains a list with server's acceptable signature algorithms.
403 * The certificate returned should support the server's given algorithms.
405 * @st should contain the certificates and private keys.
407 * If the callback function is provided then gnutls will call it, in the
408 * handshake, after the certificate request message has been received.
410 * In server side pk_algos and req_ca_dn are NULL.
412 * The callback function should set the certificate list to be sent,
413 * and return 0 on success. If no certificate was selected then the
414 * number of certificates should be set to zero. The value (-1)
415 * indicates error and the handshake will be terminated.
419 void gnutls_certificate_set_retrieve_function
420 (gnutls_certificate_credentials_t cred
,
421 gnutls_certificate_retrieve_function
* func
)
423 cred
->get_cert_callback
= func
;
427 * gnutls_certificate_set_retrieve_function2:
428 * @cred: is a #gnutls_certificate_credentials_t structure.
429 * @func: is the callback function
431 * This function sets a callback to be called in order to retrieve the
432 * certificate to be used in the handshake.
434 * The callback's function prototype is:
435 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
436 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_pcert_st* st);
438 * @req_ca_cert is only used in X.509 certificates.
439 * Contains a list with the CA names that the server considers trusted.
440 * Normally we should send a certificate that is signed
441 * by one of these CAs. These names are DER encoded. To get a more
442 * meaningful value use the function gnutls_x509_rdn_get().
444 * @pk_algos contains a list with server's acceptable signature algorithms.
445 * The certificate returned should support the server's given algorithms.
447 * @st should contain the certificates and private keys.
449 * If the callback function is provided then gnutls will call it, in the
450 * handshake, after the certificate request message has been received.
452 * In server side pk_algos and req_ca_dn are NULL.
454 * The callback function should set the certificate list to be sent,
455 * and return 0 on success. If no certificate was selected then the
456 * number of certificates should be set to zero. The value (-1)
457 * indicates error and the handshake will be terminated.
461 void gnutls_certificate_set_retrieve_function2
462 (gnutls_certificate_credentials_t cred
,
463 gnutls_certificate_retrieve_function2
* func
)
465 cred
->get_cert_callback2
= func
;
469 * gnutls_certificate_set_verify_function:
470 * @cred: is a #gnutls_certificate_credentials_t structure.
471 * @func: is the callback function
473 * This function sets a callback to be called when peer's certificate
474 * has been received in order to verify it on receipt rather than
475 * doing after the handshake is completed.
477 * The callback's function prototype is:
478 * int (*callback)(gnutls_session_t);
480 * If the callback function is provided then gnutls will call it, in the
481 * handshake, just after the certificate message has been received.
482 * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
483 * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
486 * The callback function should return 0 for the handshake to continue
487 * or non-zero to terminate.
492 gnutls_certificate_set_verify_function
493 (gnutls_certificate_credentials_t cred
,
494 gnutls_certificate_verify_function
* func
)
496 cred
->verify_callback
= func
;
500 * _gnutls_x509_extract_certificate_activation_time - return the peer's certificate activation time
501 * @cert: should contain an X.509 DER encoded certificate
503 * This function will return the certificate's activation time in UNIX time
504 * (ie seconds since 00:00:00 UTC January 1, 1970).
506 * Returns a (time_t) -1 in case of an error.
510 _gnutls_x509_get_raw_crt_activation_time (const gnutls_datum_t
* cert
)
512 gnutls_x509_crt_t xcert
;
515 result
= gnutls_x509_crt_init (&xcert
);
519 result
= gnutls_x509_crt_import (xcert
, cert
, GNUTLS_X509_FMT_DER
);
522 gnutls_x509_crt_deinit (xcert
);
526 result
= gnutls_x509_crt_get_activation_time (xcert
);
528 gnutls_x509_crt_deinit (xcert
);
534 * gnutls_x509_extract_certificate_expiration_time:
535 * @cert: should contain an X.509 DER encoded certificate
537 * This function will return the certificate's expiration time in UNIX
538 * time (ie seconds since 00:00:00 UTC January 1, 1970). Returns a
540 * (time_t) -1 in case of an error.
544 _gnutls_x509_get_raw_crt_expiration_time (const gnutls_datum_t
* cert
)
546 gnutls_x509_crt_t xcert
;
549 result
= gnutls_x509_crt_init (&xcert
);
553 result
= gnutls_x509_crt_import (xcert
, cert
, GNUTLS_X509_FMT_DER
);
556 gnutls_x509_crt_deinit (xcert
);
560 result
= gnutls_x509_crt_get_expiration_time (xcert
);
562 gnutls_x509_crt_deinit (xcert
);
567 #ifdef ENABLE_OPENPGP
569 * _gnutls_openpgp_crt_verify_peers - return the peer's certificate status
570 * @session: is a gnutls session
572 * This function will try to verify the peer's certificate and return its status (TRUSTED, INVALID etc.).
573 * Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
576 _gnutls_openpgp_crt_verify_peers (gnutls_session_t session
,
577 unsigned int *status
)
579 cert_auth_info_t info
;
580 gnutls_certificate_credentials_t cred
;
581 int peer_certificate_list_size
, ret
;
583 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
585 info
= _gnutls_get_auth_info (session
);
587 return GNUTLS_E_INVALID_REQUEST
;
589 cred
= (gnutls_certificate_credentials_t
)
590 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
594 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
597 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
600 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
603 /* generate a list of gnutls_certs based on the auth info
606 peer_certificate_list_size
= info
->ncerts
;
608 if (peer_certificate_list_size
!= 1)
611 return GNUTLS_E_INTERNAL_ERROR
;
614 /* Verify certificate
617 _gnutls_openpgp_verify_key (cred
, &info
->raw_certificate_list
[0],
618 peer_certificate_list_size
, status
);
631 * gnutls_certificate_verify_peers2:
632 * @session: is a gnutls session
633 * @status: is the output of the verification
635 * This function will try to verify the peer's certificate and return
636 * its status (trusted, invalid etc.). The value of @status should
637 * be one or more of the gnutls_certificate_status_t enumerated
638 * elements bitwise or'd. To avoid denial of service attacks some
639 * default upper limits regarding the certificate key size and chain
640 * size are set. To override them use
641 * gnutls_certificate_set_verify_limits().
643 * Note that you must also check the peer's name in order to check if
644 * the verified certificate belongs to the actual peer.
646 * This function uses gnutls_x509_crt_list_verify() with the CAs in
647 * the credentials as trusted CAs.
649 * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
652 gnutls_certificate_verify_peers2 (gnutls_session_t session
,
653 unsigned int *status
)
655 cert_auth_info_t info
;
657 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
659 info
= _gnutls_get_auth_info (session
);
662 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
665 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
666 return GNUTLS_E_NO_CERTIFICATE_FOUND
;
668 switch (gnutls_certificate_type_get (session
))
670 case GNUTLS_CRT_X509
:
671 return _gnutls_x509_cert_verify_peers (session
, status
);
672 #ifdef ENABLE_OPENPGP
673 case GNUTLS_CRT_OPENPGP
:
674 return _gnutls_openpgp_crt_verify_peers (session
, status
);
677 return GNUTLS_E_INVALID_REQUEST
;
682 * gnutls_certificate_expiration_time_peers:
683 * @session: is a gnutls session
685 * This function will return the peer's certificate expiration time.
687 * Returns: (time_t)-1 on error.
689 * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
692 gnutls_certificate_expiration_time_peers (gnutls_session_t session
)
694 cert_auth_info_t info
;
696 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
698 info
= _gnutls_get_auth_info (session
);
704 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
710 switch (gnutls_certificate_type_get (session
))
712 case GNUTLS_CRT_X509
:
714 _gnutls_x509_get_raw_crt_expiration_time (&info
->raw_certificate_list
716 #ifdef ENABLE_OPENPGP
717 case GNUTLS_CRT_OPENPGP
:
719 _gnutls_openpgp_get_raw_key_expiration_time
720 (&info
->raw_certificate_list
[0]);
728 * gnutls_certificate_activation_time_peers:
729 * @session: is a gnutls session
731 * This function will return the peer's certificate activation time.
732 * This is the creation time for openpgp keys.
734 * Returns: (time_t)-1 on error.
736 * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
739 gnutls_certificate_activation_time_peers (gnutls_session_t session
)
741 cert_auth_info_t info
;
743 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, GNUTLS_E_INVALID_REQUEST
);
745 info
= _gnutls_get_auth_info (session
);
751 if (info
->raw_certificate_list
== NULL
|| info
->ncerts
== 0)
757 switch (gnutls_certificate_type_get (session
))
759 case GNUTLS_CRT_X509
:
761 _gnutls_x509_get_raw_crt_activation_time (&info
->raw_certificate_list
763 #ifdef ENABLE_OPENPGP
764 case GNUTLS_CRT_OPENPGP
:
766 _gnutls_openpgp_get_raw_key_creation_time (&info
->raw_certificate_list
775 * gnutls_sign_callback_set:
776 * @session: is a gnutls session
777 * @sign_func: function pointer to application's sign callback.
778 * @userdata: void pointer that will be passed to sign callback.
780 * Set the callback function. The function must have this prototype:
782 * typedef int (*gnutls_sign_func) (gnutls_session_t session,
784 * gnutls_certificate_type_t cert_type,
785 * const gnutls_datum_t * cert,
786 * const gnutls_datum_t * hash,
787 * gnutls_datum_t * signature);
789 * The @userdata parameter is passed to the @sign_func verbatim, and
790 * can be used to store application-specific data needed in the
791 * callback function. See also gnutls_sign_callback_get().
793 * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess instead.
796 gnutls_sign_callback_set (gnutls_session_t session
,
797 gnutls_sign_func sign_func
, void *userdata
)
799 session
->internals
.sign_func
= sign_func
;
800 session
->internals
.sign_func_userdata
= userdata
;
804 * gnutls_sign_callback_get:
805 * @session: is a gnutls session
806 * @userdata: if non-%NULL, will be set to abstract callback pointer.
808 * Retrieve the callback function, and its userdata pointer.
810 * Returns: The function pointer set by gnutls_sign_callback_set(), or
813 * Deprecated: Use the PKCS 11 interfaces instead.
816 gnutls_sign_callback_get (gnutls_session_t session
, void **userdata
)
819 *userdata
= session
->internals
.sign_func_userdata
;
820 return session
->internals
.sign_func
;