Add.
[gnutls.git] / lib / gnutls_ui.c
blob277dfe441c068edc8613a85cc18ed67ffdc02dc7
1 /*
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,
21 * USA
25 /* This file contains certificate authentication functions to be exported in the
26 * API and did not fit elsewhere.
29 #include <gnutls_int.h>
30 #include <auth_srp.h>
31 #include <auth_anon.h>
32 #include <auth_cert.h>
33 #include <auth_psk.h>
34 #include <gnutls_errors.h>
35 #include <gnutls_auth_int.h>
36 #include <gnutls_state.h>
37 #include <gnutls_datum.h>
39 /* ANON & DHE */
41 /**
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.
54 **/
55 void
56 gnutls_dh_set_prime_bits (gnutls_session_t session, unsigned int bits)
58 session->internals.dh_prime_bits = bits;
62 /**
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.
76 **/
77 int
78 gnutls_dh_get_group (gnutls_session_t session,
79 gnutls_datum_t * raw_gen, gnutls_datum_t * raw_prime)
81 dh_info_st *dh;
82 int ret;
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))
89 case GNUTLS_CRD_ANON:
90 anon_info = _gnutls_get_auth_info (session);
91 if (anon_info == NULL)
92 return GNUTLS_E_INTERNAL_ERROR;
93 dh = &anon_info->dh;
94 break;
95 case GNUTLS_CRD_PSK:
96 psk_info = _gnutls_get_auth_info (session);
97 if (psk_info == NULL)
98 return GNUTLS_E_INTERNAL_ERROR;
99 dh = &psk_info->dh;
100 break;
101 case GNUTLS_CRD_CERTIFICATE:
102 cert_info = _gnutls_get_auth_info (session);
103 if (cert_info == NULL)
104 return GNUTLS_E_INTERNAL_ERROR;
105 dh = &cert_info->dh;
106 break;
107 default:
108 gnutls_assert ();
109 return GNUTLS_E_INVALID_REQUEST;
112 ret = _gnutls_set_datum (raw_prime, dh->prime.data, dh->prime.size);
113 if (ret < 0)
115 gnutls_assert ();
116 return ret;
119 ret = _gnutls_set_datum (raw_gen, dh->generator.data, dh->generator.size);
120 if (ret < 0)
122 gnutls_assert ();
123 _gnutls_free_datum (raw_prime);
124 return ret;
127 return 0;
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)
146 dh_info_st *dh;
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;
158 dh = &anon_info->dh;
159 break;
161 case GNUTLS_CRD_PSK:
163 psk_info = _gnutls_get_auth_info (session);
164 if (psk_info == NULL)
165 return GNUTLS_E_INTERNAL_ERROR;
166 dh = &psk_info->dh;
167 break;
169 case GNUTLS_CRD_CERTIFICATE:
172 cert_info = _gnutls_get_auth_info (session);
173 if (cert_info == NULL)
174 return GNUTLS_E_INTERNAL_ERROR;
175 dh = &cert_info->dh;
176 break;
178 default:
179 gnutls_assert ();
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;
206 int ret;
208 if (gnutls_auth_get_type (session) == GNUTLS_CRD_CERTIFICATE)
210 info = _gnutls_get_auth_info (session);
211 if (info == NULL)
212 return GNUTLS_E_INTERNAL_ERROR;
214 ret = _gnutls_set_datum (modulus, info->rsa_export.modulus.data,
215 info->rsa_export.modulus.size);
216 if (ret < 0)
218 gnutls_assert ();
219 return ret;
222 ret = _gnutls_set_datum (exponent, info->rsa_export.exponent.data,
223 info->rsa_export.exponent.size);
224 if (ret < 0)
226 gnutls_assert ();
227 _gnutls_free_datum (modulus);
228 return ret;
231 return 0;
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);
259 if (info == NULL)
260 return GNUTLS_E_INTERNAL_ERROR;
261 return info->dh.secret_bits;
263 case GNUTLS_CRD_PSK:
265 psk_auth_info_t info;
267 info = _gnutls_get_auth_info (session);
268 if (info == NULL)
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);
277 if (info == NULL)
278 return GNUTLS_E_INTERNAL_ERROR;
280 return info->dh.secret_bits;
282 default:
283 gnutls_assert ();
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)
303 dh_info_st *dh;
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);
312 if (info == NULL)
313 return GNUTLS_E_INTERNAL_ERROR;
314 dh = &info->dh;
315 break;
317 case GNUTLS_CRD_PSK:
319 psk_auth_info_t info;
321 info = _gnutls_get_auth_info (session);
322 if (info == NULL)
323 return GNUTLS_E_INTERNAL_ERROR;
324 dh = &info->dh;
325 break;
327 case GNUTLS_CRD_CERTIFICATE:
329 cert_auth_info_t info;
331 info = _gnutls_get_auth_info (session);
332 if (info == NULL)
333 return GNUTLS_E_INTERNAL_ERROR;
335 dh = &info->dh;
336 break;
338 default:
339 gnutls_assert ();
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);
362 if (info == NULL)
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
377 * error.
380 gnutls_dh_get_peers_public_bits (gnutls_session_t session)
382 dh_info_st *dh;
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);
391 if (info == NULL)
392 return GNUTLS_E_INTERNAL_ERROR;
394 dh = &info->dh;
395 break;
397 case GNUTLS_CRD_PSK:
399 psk_auth_info_t info;
401 info = _gnutls_get_auth_info (session);
402 if (info == NULL)
403 return GNUTLS_E_INTERNAL_ERROR;
405 dh = &info->dh;
406 break;
408 case GNUTLS_CRD_CERTIFICATE:
410 cert_auth_info_t info;
412 info = _gnutls_get_auth_info (session);
413 if (info == NULL)
414 return GNUTLS_E_INTERNAL_ERROR;
416 dh = &info->dh;
417 break;
419 default:
420 gnutls_assert ();
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
440 * was used.
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)
453 gnutls_assert ();
454 return NULL;
457 if (session->internals.selected_cert_list == NULL)
458 return 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
475 * format.
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
479 * was used.
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);
490 if (info == NULL)
491 return NULL;
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
506 * error.
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);
516 if (info == NULL)
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
524 * @data: is the data
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
536 * function.
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)
546 digest_hd_st td;
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;
556 if (result)
558 int ret = _gnutls_hash_init (&td, HASH2MAC (algo));
559 if (ret < 0) {
560 gnutls_assert();
561 return ret;
564 _gnutls_hash (&td, data->data, data->size);
566 _gnutls_hash_deinit (&td, result);
569 return 0;
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.
586 void
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.
603 void
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.
621 void
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
637 * limits.
639 void
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.
657 void
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.
673 void
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.
689 void
690 gnutls_anon_set_params_function (gnutls_anon_server_credentials_t res,
691 gnutls_params_function * func)
693 res->params_func = func;