2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 /* This file contains certificate authentication functions to be exported in the
26 * API and did not fit elsewhere.
29 #include <gnutls_int.h>
31 #include <auth_anon.h>
32 #include <auth_cert.h>
34 #include <gnutls_errors.h>
35 #include <gnutls_auth_int.h>
36 #include <gnutls_state.h>
37 #include <gnutls_datum.h>
42 * gnutls_dh_set_prime_bits - Used to set the bits for a DH ciphersuite
43 * @session: is a #gnutls_session_t structure.
44 * @bits: is the number of bits
46 * This function sets the number of bits, for use in an Diffie Hellman
47 * key exchange. This is used both in DH ephemeral and DH anonymous
48 * cipher suites. This will set the minimum size of the prime that
49 * will be used for the handshake.
51 * In the client side it sets the minimum accepted number of bits. If
52 * a server sends a prime with less bits than that
53 * %GNUTLS_E_DH_PRIME_UNACCEPTABLE will be returned by the handshake.
56 gnutls_dh_set_prime_bits (gnutls_session_t session
, unsigned int bits
)
58 session
->internals
.dh_prime_bits
= bits
;
63 * gnutls_dh_get_group - return the group of the DH authentication
64 * @session: is a gnutls session
65 * @raw_gen: will hold the generator.
66 * @raw_prime: will hold the prime.
68 * This function will return the group parameters used in the last
69 * Diffie Hellman authentication with the peer. These are the prime
70 * and the generator used. This function should be used for both
71 * anonymous and ephemeral diffie Hellman. The output parameters must
72 * be freed with gnutls_free().
74 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
75 * an error code is returned.
78 gnutls_dh_get_group (gnutls_session_t session
,
79 gnutls_datum_t
* raw_gen
, gnutls_datum_t
* raw_prime
)
83 anon_auth_info_t anon_info
;
84 cert_auth_info_t cert_info
;
85 psk_auth_info_t psk_info
;
87 switch (gnutls_auth_get_type (session
))
90 anon_info
= _gnutls_get_auth_info (session
);
91 if (anon_info
== NULL
)
92 return GNUTLS_E_INTERNAL_ERROR
;
96 psk_info
= _gnutls_get_auth_info (session
);
98 return GNUTLS_E_INTERNAL_ERROR
;
101 case GNUTLS_CRD_CERTIFICATE
:
102 cert_info
= _gnutls_get_auth_info (session
);
103 if (cert_info
== NULL
)
104 return GNUTLS_E_INTERNAL_ERROR
;
109 return GNUTLS_E_INVALID_REQUEST
;
112 ret
= _gnutls_set_datum (raw_prime
, dh
->prime
.data
, dh
->prime
.size
);
119 ret
= _gnutls_set_datum (raw_gen
, dh
->generator
.data
, dh
->generator
.size
);
123 _gnutls_free_datum (raw_prime
);
131 * gnutls_dh_get_pubkey - return the peer's public key used in DH authentication
132 * @session: is a gnutls session
133 * @raw_key: will hold the public key.
135 * This function will return the peer's public key used in the last
136 * Diffie Hellman authentication. This function should be used for
137 * both anonymous and ephemeral diffie Hellman. The output
138 * parameters must be freed with gnutls_free().
140 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
141 * an error code is returned.
144 gnutls_dh_get_pubkey (gnutls_session_t session
, gnutls_datum_t
* raw_key
)
147 anon_auth_info_t anon_info
;
148 cert_auth_info_t cert_info
;
149 cert_auth_info_t psk_info
;
151 switch (gnutls_auth_get_type (session
))
153 case GNUTLS_CRD_ANON
:
155 anon_info
= _gnutls_get_auth_info (session
);
156 if (anon_info
== NULL
)
157 return GNUTLS_E_INTERNAL_ERROR
;
163 psk_info
= _gnutls_get_auth_info (session
);
164 if (psk_info
== NULL
)
165 return GNUTLS_E_INTERNAL_ERROR
;
169 case GNUTLS_CRD_CERTIFICATE
:
172 cert_info
= _gnutls_get_auth_info (session
);
173 if (cert_info
== NULL
)
174 return GNUTLS_E_INTERNAL_ERROR
;
180 return GNUTLS_E_INVALID_REQUEST
;
183 return _gnutls_set_datum (raw_key
, dh
->public_key
.data
,
184 dh
->public_key
.size
);
188 * gnutls_rsa_export_get_pubkey - return the peer's public key used in RSA-EXPORT authentication
189 * @session: is a gnutls session
190 * @exponent: will hold the exponent.
191 * @modulus: will hold the modulus.
193 * This function will return the peer's public key exponent and
194 * modulus used in the last RSA-EXPORT authentication. The output
195 * parameters must be freed with gnutls_free().
197 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
198 * an error code is returned.
201 gnutls_rsa_export_get_pubkey (gnutls_session_t session
,
202 gnutls_datum_t
* exponent
,
203 gnutls_datum_t
* modulus
)
205 cert_auth_info_t info
;
208 if (gnutls_auth_get_type (session
) == GNUTLS_CRD_CERTIFICATE
)
210 info
= _gnutls_get_auth_info (session
);
212 return GNUTLS_E_INTERNAL_ERROR
;
214 ret
= _gnutls_set_datum (modulus
, info
->rsa_export
.modulus
.data
,
215 info
->rsa_export
.modulus
.size
);
222 ret
= _gnutls_set_datum (exponent
, info
->rsa_export
.exponent
.data
,
223 info
->rsa_export
.exponent
.size
);
227 _gnutls_free_datum (modulus
);
234 return GNUTLS_E_INVALID_REQUEST
;
239 * gnutls_dh_get_secret_bits - return the bits used in DH authentication
240 * @session: is a gnutls session
242 * This function will return the bits used in the last Diffie Hellman
243 * authentication with the peer. Should be used for both anonymous
244 * and ephemeral diffie Hellman.
246 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
247 * an error code is returned.
250 gnutls_dh_get_secret_bits (gnutls_session_t session
)
252 switch (gnutls_auth_get_type (session
))
254 case GNUTLS_CRD_ANON
:
256 anon_auth_info_t info
;
258 info
= _gnutls_get_auth_info (session
);
260 return GNUTLS_E_INTERNAL_ERROR
;
261 return info
->dh
.secret_bits
;
265 psk_auth_info_t info
;
267 info
= _gnutls_get_auth_info (session
);
269 return GNUTLS_E_INTERNAL_ERROR
;
270 return info
->dh
.secret_bits
;
272 case GNUTLS_CRD_CERTIFICATE
:
274 cert_auth_info_t info
;
276 info
= _gnutls_get_auth_info (session
);
278 return GNUTLS_E_INTERNAL_ERROR
;
280 return info
->dh
.secret_bits
;
284 return GNUTLS_E_INVALID_REQUEST
;
290 * gnutls_dh_get_prime_bits - return the bits used in DH authentication
291 * @session: is a gnutls session
293 * This function will return the bits of the prime used in the last
294 * Diffie Hellman authentication with the peer. Should be used for
295 * both anonymous and ephemeral diffie Hellman.
297 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
298 * an error code is returned.
301 gnutls_dh_get_prime_bits (gnutls_session_t session
)
305 switch (gnutls_auth_get_type (session
))
307 case GNUTLS_CRD_ANON
:
309 anon_auth_info_t info
;
311 info
= _gnutls_get_auth_info (session
);
313 return GNUTLS_E_INTERNAL_ERROR
;
319 psk_auth_info_t info
;
321 info
= _gnutls_get_auth_info (session
);
323 return GNUTLS_E_INTERNAL_ERROR
;
327 case GNUTLS_CRD_CERTIFICATE
:
329 cert_auth_info_t info
;
331 info
= _gnutls_get_auth_info (session
);
333 return GNUTLS_E_INTERNAL_ERROR
;
340 return GNUTLS_E_INVALID_REQUEST
;
343 return (dh
->prime
.size
) * 8;
348 * gnutls_rsa_export_get_modulus_bits - return the bits used in RSA-export key exchange
349 * @session: is a gnutls session
351 * Get the export RSA parameter's modulus size.
353 * Returns: the bits used in the last RSA-EXPORT key exchange with the
354 * peer, or a negative value in case of error.
357 gnutls_rsa_export_get_modulus_bits (gnutls_session_t session
)
359 cert_auth_info_t info
;
361 info
= _gnutls_get_auth_info (session
);
363 return GNUTLS_E_INTERNAL_ERROR
;
365 return info
->rsa_export
.modulus
.size
* 8;
369 * gnutls_dh_get_peers_public_bits - return the bits used in DH authentication
370 * @session: is a gnutls session
372 * Get the Diffie-Hellman public key bit size. Can be used for both
373 * anonymous and ephemeral diffie Hellman.
375 * Returns: the public key bit size used in the last Diffie Hellman
376 * authentication with the peer, or a negative value in case of
380 gnutls_dh_get_peers_public_bits (gnutls_session_t session
)
384 switch (gnutls_auth_get_type (session
))
386 case GNUTLS_CRD_ANON
:
388 anon_auth_info_t info
;
390 info
= _gnutls_get_auth_info (session
);
392 return GNUTLS_E_INTERNAL_ERROR
;
399 psk_auth_info_t info
;
401 info
= _gnutls_get_auth_info (session
);
403 return GNUTLS_E_INTERNAL_ERROR
;
408 case GNUTLS_CRD_CERTIFICATE
:
410 cert_auth_info_t info
;
412 info
= _gnutls_get_auth_info (session
);
414 return GNUTLS_E_INTERNAL_ERROR
;
421 return GNUTLS_E_INVALID_REQUEST
;
424 return dh
->public_key
.size
* 8;
428 /* CERTIFICATE STUFF */
431 * gnutls_certificate_get_ours - return the raw certificate sent in the last handshake
432 * @session: is a gnutls session
434 * Get the certificate as sent to the peer, in the last handshake.
435 * These certificates are in raw format. In X.509 this is a
436 * certificate list. In OpenPGP this is a single certificate.
438 * Returns: return a pointer to a #gnutls_datum_t containing our
439 * certificates, or %NULL in case of an error or if no certificate
442 const gnutls_datum_t
*
443 gnutls_certificate_get_ours (gnutls_session_t session
)
445 gnutls_certificate_credentials_t cred
;
447 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, NULL
);
449 cred
= (gnutls_certificate_credentials_t
)
450 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
451 if (cred
== NULL
|| cred
->cert_list
== NULL
)
457 if (session
->internals
.selected_cert_list
== NULL
)
460 return &session
->internals
.selected_cert_list
[0].raw
;
464 * gnutls_certificate_get_peers - return the peer's raw certificate
465 * @session: is a gnutls session
466 * @list_size: is the length of the certificate list
468 * Get the peer's raw certificate (chain) as sent by the peer. These
469 * certificates are in raw format (DER encoded for X.509). In case of
470 * a X.509 then a certificate list may be present. The first
471 * certificate in the list is the peer's certificate, following the
472 * issuer's certificate, then the issuer's issuer etc.
474 * In case of OpenPGP keys a single key will be returned in raw
477 * Returns: return a pointer to a #gnutls_datum_t containing our
478 * certificates, or %NULL in case of an error or if no certificate
481 const gnutls_datum_t
*
482 gnutls_certificate_get_peers (gnutls_session_t
483 session
, unsigned int *list_size
)
485 cert_auth_info_t info
;
487 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, NULL
);
489 info
= _gnutls_get_auth_info (session
);
493 *list_size
= info
->ncerts
;
494 return info
->raw_certificate_list
;
499 * gnutls_certificate_client_get_request_status - return the certificate request status
500 * @session: is a gnutls session
502 * Get whether client certificate is requested or not.
504 * Returns: 0 if the peer (server) did not request client
505 * authentication or 1 otherwise, or a negative value in case of
509 gnutls_certificate_client_get_request_status (gnutls_session_t session
)
511 cert_auth_info_t info
;
513 CHECK_AUTH (GNUTLS_CRD_CERTIFICATE
, 0);
515 info
= _gnutls_get_auth_info (session
);
517 return GNUTLS_E_INTERNAL_ERROR
;
518 return info
->certificate_requested
;
522 * gnutls_fingerprint - calculate the fingerprint of the given data
523 * @algo: is a digest algorithm
525 * @result: is the place where the result will be copied (may be null).
526 * @result_size: should hold the size of the result. The actual size
527 * of the returned result will also be copied there.
529 * This function will calculate a fingerprint (actually a hash), of
530 * the given data. The result is not printable data. You should
531 * convert it to hex, or to something else printable.
533 * This is the usual way to calculate a fingerprint of an X.509 DER
534 * encoded certificate. Note however that the fingerprint of an
535 * OpenPGP is not just a hash and cannot be calculated with this
538 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
539 * an error code is returned.
542 gnutls_fingerprint (gnutls_digest_algorithm_t algo
,
543 const gnutls_datum_t
* data
, void *result
,
544 size_t * result_size
)
547 int hash_len
= _gnutls_hash_get_algo_len (HASH2MAC (algo
));
549 if (hash_len
< 0 || (unsigned) hash_len
> *result_size
|| result
== NULL
)
551 *result_size
= hash_len
;
552 return GNUTLS_E_SHORT_MEMORY_BUFFER
;
554 *result_size
= hash_len
;
558 int ret
= _gnutls_hash_init (&td
, HASH2MAC (algo
));
564 _gnutls_hash (&td
, data
->data
, data
->size
);
566 _gnutls_hash_deinit (&td
, result
);
574 * gnutls_certificate_set_dh_params - set the DH parameters for a server to use
575 * @res: is a gnutls_certificate_credentials_t structure
576 * @dh_params: is a structure that holds diffie hellman parameters.
578 * This function will set the diffie hellman parameters for a
579 * certificate server to use. These parameters will be used in
580 * Ephemeral Diffie Hellman cipher suites. Note that only a pointer
581 * to the parameters are stored in the certificate handle, so if you
582 * deallocate the parameters before the certificate is deallocated,
583 * you must change the parameters stored in the certificate first.
587 gnutls_certificate_set_dh_params (gnutls_certificate_credentials_t res
,
588 gnutls_dh_params_t dh_params
)
590 res
->dh_params
= dh_params
;
594 * gnutls_certificate_set_params_function - set the DH or RSA parameters callback
595 * @res: is a gnutls_certificate_credentials_t structure
596 * @func: is the function to be called
598 * This function will set a callback in order for the server to get
599 * the diffie hellman or RSA parameters for certificate
600 * authentication. The callback should return zero on success.
604 gnutls_certificate_set_params_function (gnutls_certificate_credentials_t res
,
605 gnutls_params_function
* func
)
607 res
->params_func
= func
;
612 * gnutls_certificate_set_verify_flags - set the flags to be used at certificate verification
613 * @res: is a gnutls_certificate_credentials_t structure
614 * @flags: are the flags
616 * This function will set the flags to be used at verification of the
617 * certificates. Flags must be OR of the
618 * #gnutls_certificate_verify_flags enumerations.
622 gnutls_certificate_set_verify_flags (gnutls_certificate_credentials_t
623 res
, unsigned int flags
)
625 res
->verify_flags
= flags
;
629 * gnutls_certificate_set_verify_limits - set the upper limits to be used at certificate verification
630 * @res: is a gnutls_certificate_credentials structure
631 * @max_bits: is the number of bits of an acceptable certificate (default 8200)
632 * @max_depth: is maximum depth of the verification of a certificate chain (default 5)
634 * This function will set some upper limits for the default
635 * verification function, gnutls_certificate_verify_peers2(), to avoid
636 * denial of service attacks. You can set them to zero to disable
640 gnutls_certificate_set_verify_limits (gnutls_certificate_credentials_t
641 res
, unsigned int max_bits
,
642 unsigned int max_depth
)
644 res
->verify_depth
= max_depth
;
645 res
->verify_bits
= max_bits
;
649 * gnutls_certificate_set_rsa_export_params - set the RSA parameters for a server to use
650 * @res: is a gnutls_certificate_credentials_t structure
651 * @rsa_params: is a structure that holds temporary RSA parameters.
653 * This function will set the temporary RSA parameters for a
654 * certificate server to use. These parameters will be used in
655 * RSA-EXPORT cipher suites.
658 gnutls_certificate_set_rsa_export_params (gnutls_certificate_credentials_t
659 res
, gnutls_rsa_params_t rsa_params
)
661 res
->rsa_params
= rsa_params
;
665 * gnutls_psk_set_params_function - set the DH or RSA parameters callback
666 * @res: is a gnutls_psk_server_credentials_t structure
667 * @func: is the function to be called
669 * This function will set a callback in order for the server to get
670 * the diffie hellman or RSA parameters for psk authentication. The
671 * callback should return zero on success.
674 gnutls_psk_set_params_function (gnutls_psk_server_credentials_t res
,
675 gnutls_params_function
* func
)
677 res
->params_func
= func
;
681 * gnutls_anon_set_params_function - set the DH or RSA parameters callback
682 * @res: is a gnutls_anon_server_credentials_t structure
683 * @func: is the function to be called
685 * This function will set a callback in order for the server to get
686 * the diffie hellman or RSA parameters for anonymous authentication.
687 * The callback should return zero on success.
690 gnutls_anon_set_params_function (gnutls_anon_server_credentials_t res
,
691 gnutls_params_function
* func
)
693 res
->params_func
= func
;