Imported Upstream version 3.3.5
[platform/upstream/gnutls.git] / lib / gnutls_cert.c
1 /*
2  * Copyright (C) 2001-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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/>
20  *
21  */
22
23 /* Some of the stuff needed for Certificate authentication is contained
24  * in this file.
25  */
26
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"
42 #ifdef ENABLE_OPENPGP
43 #include "openpgp/gnutls_openpgp.h"
44 #endif
45 #include "gettext.h"
46 #define _(String) dgettext (PACKAGE, String)
47
48 /**
49  * gnutls_certificate_free_keys:
50  * @sc: is a #gnutls_certificate_credentials_t structure.
51  *
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.
55  *
56  **/
57 void gnutls_certificate_free_keys(gnutls_certificate_credentials_t sc)
58 {
59         unsigned i, j;
60
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]);
64                 }
65                 gnutls_free(sc->certs[i].cert_list);
66                 _gnutls_str_array_clear(&sc->certs[i].names);
67         }
68
69         gnutls_free(sc->certs);
70         sc->certs = NULL;
71
72         for (i = 0; i < sc->ncerts; i++) {
73                 gnutls_privkey_deinit(sc->pkey[i]);
74         }
75
76         gnutls_free(sc->pkey);
77         sc->pkey = NULL;
78
79         sc->ncerts = 0;
80 }
81
82 /**
83  * gnutls_certificate_free_cas:
84  * @sc: is a #gnutls_certificate_credentials_t structure.
85  *
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
89  * memory.
90  **/
91 void gnutls_certificate_free_cas(gnutls_certificate_credentials_t sc)
92 {
93         /* FIXME: do nothing for now */
94         return;
95 }
96
97 /**
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.
102  * @flags: Use zero.
103  *
104  * This function will return the issuer of a given certificate.
105  *
106  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
107  *   negative error value.
108  *
109  * Since: 3.0
110  **/
111 int
112 gnutls_certificate_get_issuer(gnutls_certificate_credentials_t sc,
113                               gnutls_x509_crt_t cert,
114                               gnutls_x509_crt_t * issuer,
115                               unsigned int flags)
116 {
117         return gnutls_x509_trust_list_get_issuer(sc->tlist, cert, issuer,
118                                                  flags);
119 }
120
121 /**
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.
127  *
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
131  * of @sc.
132  *
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
135  *   is returned.
136  *
137  * Since: 3.2.5
138  **/
139 int
140 gnutls_certificate_get_crt_raw(gnutls_certificate_credentials_t sc,
141                                unsigned idx1,
142                                unsigned idx2, gnutls_datum_t * cert)
143 {
144         if (idx1 >= sc->ncerts)
145                 return
146                     gnutls_assert_val
147                     (GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
148
149         if (idx2 >= sc->certs[idx1].cert_list_length)
150                 return
151                     gnutls_assert_val
152                     (GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
153
154         cert->data = sc->certs[idx1].cert_list[idx2].cert.data;
155         cert->size = sc->certs[idx1].cert_list[idx2].cert.size;
156
157         return 0;
158 }
159
160 /**
161  * gnutls_certificate_free_ca_names:
162  * @sc: is a #gnutls_certificate_credentials_t structure.
163  *
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.
170  *
171  * CA names are used by servers to advertise the CAs they support to
172  * clients.
173  **/
174 void gnutls_certificate_free_ca_names(gnutls_certificate_credentials_t sc)
175 {
176         _gnutls_free_datum(&sc->tlist->x509_rdn_sequence);
177 }
178
179
180 /**
181  * gnutls_certificate_free_credentials:
182  * @sc: is a #gnutls_certificate_credentials_t structure.
183  *
184  * This structure is complex enough to manipulate directly thus this
185  * helper function is provided in order to free (deallocate) it.
186  *
187  * This function does not free any temporary parameters associated
188  * with this structure (ie RSA and DH parameters are not freed by this
189  * function).
190  **/
191 void
192 gnutls_certificate_free_credentials(gnutls_certificate_credentials_t sc)
193 {
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);
200 #endif
201
202         gnutls_free(sc);
203 }
204
205
206 /**
207  * gnutls_certificate_allocate_credentials:
208  * @res: is a pointer to a #gnutls_certificate_credentials_t structure.
209  *
210  * This structure is complex enough to manipulate directly thus this
211  * helper function is provided in order to allocate it.
212  *
213  * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
214  **/
215 int
216 gnutls_certificate_allocate_credentials(gnutls_certificate_credentials_t *
217                                         res)
218 {
219         int ret;
220
221         *res = gnutls_calloc(1, sizeof(certificate_credentials_st));
222
223         if (*res == NULL)
224                 return GNUTLS_E_MEMORY_ERROR;
225
226         ret = gnutls_x509_trust_list_init(&(*res)->tlist, 0);
227         if (ret < 0) {
228                 gnutls_assert();
229                 gnutls_free(*res);
230                 return GNUTLS_E_MEMORY_ERROR;
231         }
232         (*res)->verify_bits = DEFAULT_MAX_VERIFY_BITS;
233         (*res)->verify_depth = DEFAULT_MAX_VERIFY_DEPTH;
234
235         return 0;
236 }
237
238
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.
244  */
245 int
246 _gnutls_selected_cert_supported_kx(gnutls_session_t session,
247                                    gnutls_kx_algorithm_t * alg,
248                                    int *alg_size)
249 {
250         unsigned kx;
251         gnutls_pk_algorithm_t pk, cert_pk;
252         gnutls_pcert_st *cert;
253         int i;
254
255         if (session->internals.selected_cert_list_length == 0) {
256                 *alg_size = 0;
257                 return 0;
258         }
259
260         cert = &session->internals.selected_cert_list[0];
261         cert_pk = gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
262         i = 0;
263
264         for (kx = 0; kx < MAX_ALGOS; kx++) {
265                 pk = _gnutls_map_pk_get_pk(kx);
266                 if (pk == cert_pk) {
267                         /* then check key usage */
268                         if (_gnutls_check_key_usage(cert, kx) == 0) {
269                                 alg[i] = kx;
270                                 i++;
271
272                                 if (i > *alg_size)
273                                         return
274                                             gnutls_assert_val
275                                             (GNUTLS_E_INTERNAL_ERROR);
276                         }
277                 }
278         }
279
280         if (i == 0) {
281                 gnutls_assert();
282                 return GNUTLS_E_INVALID_REQUEST;
283         }
284
285         *alg_size = i;
286
287         return 0;
288 }
289
290
291 /**
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
295  *
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.
301  **/
302 void
303 gnutls_certificate_server_set_request(gnutls_session_t session,
304                                       gnutls_certificate_request_t req)
305 {
306         session->internals.send_cert_req = req;
307 }
308
309 /**
310  * gnutls_certificate_client_set_retrieve_function:
311  * @cred: is a #gnutls_certificate_credentials_t structure.
312  * @func: is the callback function
313  *
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.
318  *
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);
322  *
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().
328  *
329  * @pk_algos contains a list with server's acceptable signature algorithms.
330  * The certificate returned should support the server's given algorithms.
331  *
332  * @st should contain the certificates and private keys.
333  *
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).
337  *
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.
342  **/
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;
347 }
348
349 /**
350  * gnutls_certificate_server_set_retrieve_function:
351  * @cred: is a #gnutls_certificate_credentials_t structure.
352  * @func: is the callback function
353  *
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.
358  *
359  * The callback's function prototype is:
360  * int (*callback)(gnutls_session_t, gnutls_retr_st* st);
361  *
362  * @st should contain the certificates and private keys.
363  *
364  * If the callback function is provided then gnutls will call it, in the
365  * handshake, after the certificate request message has been received.
366  *
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.
370  **/
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;
375 }
376
377 /**
378  * gnutls_certificate_set_retrieve_function:
379  * @cred: is a #gnutls_certificate_credentials_t structure.
380  * @func: is the callback function
381  *
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.
386  *
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);
390  *
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().
396  *
397  * @pk_algos contains a list with server's acceptable signature algorithms.
398  * The certificate returned should support the server's given algorithms.
399  *
400  * @st should contain the certificates and private keys.
401  *
402  * If the callback function is provided then gnutls will call it, in the
403  * handshake, after the certificate request message has been received.
404  *
405  * In server side pk_algos and req_ca_dn are NULL.
406  *
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.
411  *
412  * Since: 3.0
413  **/
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;
418 }
419
420 /**
421  * gnutls_certificate_set_retrieve_function2:
422  * @cred: is a #gnutls_certificate_credentials_t structure.
423  * @func: is the callback function
424  *
425  * This function sets a callback to be called in order to retrieve the
426  * certificate to be used in the handshake.
427  *
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);
432  *
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().
438  *
439  * @pk_algos contains a list with server's acceptable signature algorithms.
440  * The certificate returned should support the server's given algorithms.
441  *
442  * @pcert should contain a single certificate and public or a list of them.
443  *
444  * @pcert_length is the size of the previous list.
445  *
446  * @pkey is the private key.
447  *
448  * If the callback function is provided then gnutls will call it, in the
449  * handshake, after the certificate request message has been received.
450  *
451  * In server side pk_algos and req_ca_dn are NULL.
452  *
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.
457  *
458  * Since: 3.0
459  **/
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;
464 }
465
466 /**
467  * gnutls_certificate_set_verify_function:
468  * @cred: is a #gnutls_certificate_credentials_t structure.
469  * @func: is the callback function
470  *
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.
474  *
475  * The callback's function prototype is:
476  * int (*callback)(gnutls_session_t);
477  *
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
482  * can be used.
483  *
484  * The callback function should return 0 for the handshake to continue
485  * or non-zero to terminate.
486  *
487  * Since: 2.10.0
488  **/
489 void
490  gnutls_certificate_set_verify_function
491     (gnutls_certificate_credentials_t cred,
492      gnutls_certificate_verify_function * func) {
493         cred->verify_callback = func;
494 }
495
496 /*-
497  * _gnutls_x509_extract_certificate_activation_time - return the peer's certificate activation time
498  * @cert: should contain an X.509 DER encoded certificate
499  *
500  * This function will return the certificate's activation time in UNIX time
501  * (ie seconds since 00:00:00 UTC January 1, 1970).
502  *
503  * Returns a (time_t) -1 in case of an error.
504  *
505  -*/
506 static time_t
507 _gnutls_x509_get_raw_crt_activation_time(const gnutls_datum_t * cert)
508 {
509         gnutls_x509_crt_t xcert;
510         time_t result;
511
512         result = gnutls_x509_crt_init(&xcert);
513         if (result < 0)
514                 return (time_t) - 1;
515
516         result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
517         if (result < 0) {
518                 gnutls_x509_crt_deinit(xcert);
519                 return (time_t) - 1;
520         }
521
522         result = gnutls_x509_crt_get_activation_time(xcert);
523
524         gnutls_x509_crt_deinit(xcert);
525
526         return result;
527 }
528
529 /*-
530  * gnutls_x509_extract_certificate_expiration_time:
531  * @cert: should contain an X.509 DER encoded certificate
532  *
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
535  *
536  * (time_t) -1 in case of an error.
537  *
538  -*/
539 static time_t
540 _gnutls_x509_get_raw_crt_expiration_time(const gnutls_datum_t * cert)
541 {
542         gnutls_x509_crt_t xcert;
543         time_t result;
544
545         result = gnutls_x509_crt_init(&xcert);
546         if (result < 0)
547                 return (time_t) - 1;
548
549         result = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER);
550         if (result < 0) {
551                 gnutls_x509_crt_deinit(xcert);
552                 return (time_t) - 1;
553         }
554
555         result = gnutls_x509_crt_get_expiration_time(xcert);
556
557         gnutls_x509_crt_deinit(xcert);
558
559         return result;
560 }
561
562 #ifdef ENABLE_OPENPGP
563 /*-
564  * _gnutls_openpgp_crt_verify_peers - return the peer's certificate status
565  * @session: is a gnutls session
566  *
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.
569  -*/
570 static int
571 _gnutls_openpgp_crt_verify_peers(gnutls_session_t session,
572                                  const char *hostname,
573                                  unsigned int *status)
574 {
575         cert_auth_info_t info;
576         gnutls_certificate_credentials_t cred;
577         int peer_certificate_list_size, ret;
578         unsigned int verify_flags;
579
580         CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
581
582         info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
583         if (info == NULL)
584                 return GNUTLS_E_INVALID_REQUEST;
585
586         cred = (gnutls_certificate_credentials_t)
587             _gnutls_get_cred(session, GNUTLS_CRD_CERTIFICATE);
588         if (cred == NULL) {
589                 gnutls_assert();
590                 return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
591         }
592
593         if (info->raw_certificate_list == NULL || info->ncerts == 0) {
594                 gnutls_assert();
595                 return GNUTLS_E_NO_CERTIFICATE_FOUND;
596         }
597
598         verify_flags = cred->verify_flags | session->internals.priorities.additional_verify_flags;
599
600         /* generate a list of gnutls_certs based on the auth info
601          * raw certs.
602          */
603         peer_certificate_list_size = info->ncerts;
604
605         if (peer_certificate_list_size != 1) {
606                 gnutls_assert();
607                 return GNUTLS_E_INTERNAL_ERROR;
608         }
609
610         /* Verify certificate 
611          */
612         ret =
613             _gnutls_openpgp_verify_key(cred, hostname,
614                                        &info->raw_certificate_list[0],
615                                        peer_certificate_list_size,
616                                        verify_flags,
617                                        status);
618
619         if (ret < 0) {
620                 gnutls_assert();
621                 return ret;
622         }
623
624         return 0;
625 }
626 #endif
627
628 /**
629  * gnutls_certificate_verify_peers2:
630  * @session: is a gnutls session
631  * @status: is the output of the verification
632  *
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().
640  *
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).
644  * 
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().
648  *
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().
652  *
653  * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
654  **/
655 int
656 gnutls_certificate_verify_peers2(gnutls_session_t session,
657                                  unsigned int *status)
658 {
659         return gnutls_certificate_verify_peers(session, NULL, 0, status);
660 }
661
662 /**
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
667  *
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.
676  *
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.
681  *
682  * In order to verify the purpose of the end-certificate (by checking the extended
683  * key usage), use gnutls_certificate_verify_peers().
684  *
685  * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
686  *
687  * Since: 3.1.4
688  **/
689 int
690 gnutls_certificate_verify_peers3(gnutls_session_t session,
691                                  const char *hostname,
692                                  unsigned int *status)
693 {
694 gnutls_typed_vdata_st data;
695
696         data.type = GNUTLS_DT_DNS_HOSTNAME;
697         data.size = 0;
698         data.data = (void*)hostname;
699
700         return gnutls_certificate_verify_peers(session, &data, 1, status);
701 }
702
703 /**
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
709  *
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.
718  *
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.
724  *
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.
729  *
730  * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
731  *
732  * Since: 3.3.0
733  **/
734 int
735 gnutls_certificate_verify_peers(gnutls_session_t session,
736                                 gnutls_typed_vdata_st * data,
737                                 unsigned int elements,
738                                 unsigned int *status)
739 {
740         cert_auth_info_t info;
741         const char *hostname = NULL;
742         const char *purpose = NULL;
743         unsigned i;
744
745         CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
746
747         info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
748         if (info == NULL) {
749                 return GNUTLS_E_NO_CERTIFICATE_FOUND;
750         }
751
752         if (info->raw_certificate_list == NULL || info->ncerts == 0)
753                 return GNUTLS_E_NO_CERTIFICATE_FOUND;
754
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;
760                 } else {
761                         return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
762                 }
763         }
764
765         switch (gnutls_certificate_type_get(session)) {
766         case GNUTLS_CRT_X509:
767                 return _gnutls_x509_cert_verify_peers(session, hostname,
768                                                       purpose,
769                                                       status);
770 #ifdef ENABLE_OPENPGP
771         case GNUTLS_CRT_OPENPGP:
772                 return _gnutls_openpgp_crt_verify_peers(session, hostname,
773                                                         status);
774 #endif
775         default:
776                 return GNUTLS_E_INVALID_REQUEST;
777         }
778 }
779
780 /**
781  * gnutls_certificate_expiration_time_peers:
782  * @session: is a gnutls session
783  *
784  * This function will return the peer's certificate expiration time.
785  *
786  * Returns: (time_t)-1 on error.
787  *
788  * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
789  **/
790 time_t gnutls_certificate_expiration_time_peers(gnutls_session_t session)
791 {
792         cert_auth_info_t info;
793
794         CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
795
796         info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
797         if (info == NULL) {
798                 return (time_t) - 1;
799         }
800
801         if (info->raw_certificate_list == NULL || info->ncerts == 0) {
802                 gnutls_assert();
803                 return (time_t) - 1;
804         }
805
806         switch (gnutls_certificate_type_get(session)) {
807         case GNUTLS_CRT_X509:
808                 return
809                     _gnutls_x509_get_raw_crt_expiration_time(&info->
810                                                              raw_certificate_list
811                                                              [0]);
812 #ifdef ENABLE_OPENPGP
813         case GNUTLS_CRT_OPENPGP:
814                 return
815                     _gnutls_openpgp_get_raw_key_expiration_time
816                     (&info->raw_certificate_list[0]);
817 #endif
818         default:
819                 return (time_t) - 1;
820         }
821 }
822
823 /**
824  * gnutls_certificate_activation_time_peers:
825  * @session: is a gnutls session
826  *
827  * This function will return the peer's certificate activation time.
828  * This is the creation time for openpgp keys.
829  *
830  * Returns: (time_t)-1 on error.
831  *
832  * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
833  **/
834 time_t gnutls_certificate_activation_time_peers(gnutls_session_t session)
835 {
836         cert_auth_info_t info;
837
838         CHECK_AUTH(GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
839
840         info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
841         if (info == NULL) {
842                 return (time_t) - 1;
843         }
844
845         if (info->raw_certificate_list == NULL || info->ncerts == 0) {
846                 gnutls_assert();
847                 return (time_t) - 1;
848         }
849
850         switch (gnutls_certificate_type_get(session)) {
851         case GNUTLS_CRT_X509:
852                 return
853                     _gnutls_x509_get_raw_crt_activation_time(&info->
854                                                              raw_certificate_list
855                                                              [0]);
856 #ifdef ENABLE_OPENPGP
857         case GNUTLS_CRT_OPENPGP:
858                 return
859                     _gnutls_openpgp_get_raw_key_creation_time(&info->
860                                                               raw_certificate_list
861                                                               [0]);
862 #endif
863         default:
864                 return (time_t) - 1;
865         }
866 }
867
868 /**
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.
873  *
874  * Set the callback function.  The function must have this prototype:
875  *
876  * typedef int (*gnutls_sign_func) (gnutls_session_t session,
877  *                                  void *userdata,
878  *                                  gnutls_certificate_type_t cert_type,
879  *                                  const gnutls_datum_t * cert,
880  *                                  const gnutls_datum_t * hash,
881  *                                  gnutls_datum_t * signature);
882  *
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().
886  *
887  * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess like gnutls_privkey_import_ext() instead.
888  **/
889 void
890 gnutls_sign_callback_set(gnutls_session_t session,
891                          gnutls_sign_func sign_func, void *userdata)
892 {
893         session->internals.sign_func = sign_func;
894         session->internals.sign_func_userdata = userdata;
895 }
896
897 /**
898  * gnutls_sign_callback_get:
899  * @session: is a gnutls session
900  * @userdata: if non-%NULL, will be set to abstract callback pointer.
901  *
902  * Retrieve the callback function, and its userdata pointer.
903  *
904  * Returns: The function pointer set by gnutls_sign_callback_set(), or
905  *   if not set, %NULL.
906  *
907  * Deprecated: Use the PKCS 11 interfaces instead.
908  **/
909 gnutls_sign_func
910 gnutls_sign_callback_get(gnutls_session_t session, void **userdata)
911 {
912         if (userdata)
913                 *userdata = session->internals.sign_func_userdata;
914         return session->internals.sign_func;
915 }
916
917 #define TEST_TEXT "test text"
918 /* returns error if the certificate has different algorithm than
919  * the given key parameters.
920  */
921 int _gnutls_check_key_cert_match(gnutls_certificate_credentials_t res)
922 {
923         gnutls_datum_t test = {(void*)TEST_TEXT, sizeof(TEST_TEXT)-1};
924         gnutls_datum_t sig = {NULL, 0};
925         int pk, pk2, ret;
926
927         pk =
928             gnutls_pubkey_get_pk_algorithm(res->certs[res->ncerts - 1].
929                                            cert_list[0].pubkey, NULL);
930         pk2 =
931             gnutls_privkey_get_pk_algorithm(res->pkey[res->ncerts - 1],
932                                             NULL);
933
934         if (pk2 != pk) {
935                 gnutls_assert();
936                 return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
937         }
938
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);
944         if (ret < 0) {
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__);
949                 goto finish;
950         }
951
952         ret = gnutls_pubkey_verify_data2(res->certs[res->ncerts - 1].cert_list[0].pubkey,
953                 gnutls_pk_to_sign(pk, GNUTLS_DIG_SHA256),
954                 0, &test, &sig);
955
956         gnutls_free(sig.data);
957
958         if (ret < 0)
959                 return gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
960
961  finish:
962         return 0;
963 }
964
965 /**
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
971  *
972  * This function will pretty print the status of a verification
973  * process -- eg. the one obtained by gnutls_certificate_verify_peers3().
974  *
975  * The output @out needs to be deallocated using gnutls_free().
976  *
977  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
978  *   negative error value.
979  *
980  * Since: 3.1.4
981  **/
982 int
983 gnutls_certificate_verification_status_print(unsigned int status,
984                                              gnutls_certificate_type_t
985                                              type, gnutls_datum_t * out,
986                                              unsigned int flags)
987 {
988         gnutls_buffer_st str;
989         int ret;
990
991         _gnutls_buffer_init(&str);
992
993         if (status == 0)
994                 _gnutls_buffer_append_str(&str,
995                                           _
996                                           ("The certificate is trusted. "));
997         else
998                 _gnutls_buffer_append_str(&str,
999                                           _
1000                                           ("The certificate is NOT trusted. "));
1001
1002         if (type == GNUTLS_CRT_X509) {
1003                 if (status & GNUTLS_CERT_REVOKED)
1004                         _gnutls_buffer_append_str(&str,
1005                                                   _
1006                                                   ("The certificate chain is revoked. "));
1007
1008                 if (status & GNUTLS_CERT_MISMATCH)
1009                         _gnutls_buffer_append_str(&str,
1010                                                   _
1011                                                   ("The certificate doesn't match the local copy (TOFU). "));
1012
1013                 if (status & GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED)
1014                         _gnutls_buffer_append_str(&str,
1015                                                   _
1016                                                   ("The revocation data are old and have been superseded. "));
1017
1018                 if (status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)
1019                         _gnutls_buffer_append_str(&str,
1020                                                   _
1021                                                   ("The revocation data are issued with a future date. "));
1022
1023                 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
1024                         _gnutls_buffer_append_str(&str,
1025                                                   _
1026                                                   ("The certificate issuer is unknown. "));
1027
1028                 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
1029                         _gnutls_buffer_append_str(&str,
1030                                                   _
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,
1035                                                   _
1036                                                   ("Could not find a signer of the certificate. "));
1037
1038                 if (status & GNUTLS_CERT_REVOKED)
1039                         _gnutls_buffer_append_str(&str,
1040                                                   _
1041                                                   ("The certificate is revoked. "));
1042         }
1043
1044         if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
1045                 _gnutls_buffer_append_str(&str,
1046                                           _
1047                                           ("The certificate chain uses insecure algorithm. "));
1048
1049         if (status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE)
1050                 _gnutls_buffer_append_str(&str,
1051                                           _
1052                                           ("The certificate chain violates the signer's constraints. "));
1053
1054         if (status & GNUTLS_CERT_NOT_ACTIVATED)
1055                 _gnutls_buffer_append_str(&str,
1056                                           _
1057                                           ("The certificate chain uses not yet valid certificate. "));
1058
1059         if (status & GNUTLS_CERT_EXPIRED)
1060                 _gnutls_buffer_append_str(&str,
1061                                           _
1062                                           ("The certificate chain uses expired certificate. "));
1063
1064         if (status & GNUTLS_CERT_SIGNATURE_FAILURE)
1065                 _gnutls_buffer_append_str(&str,
1066                                           _
1067                                           ("The signature in the certificate is invalid. "));
1068
1069         if (status & GNUTLS_CERT_UNEXPECTED_OWNER)
1070                 _gnutls_buffer_append_str(&str,
1071                                           _
1072                                           ("The name in the certificate does not match the expected. "));
1073
1074         ret = _gnutls_buffer_to_datum(&str, out);
1075         if (out->size > 0)
1076                 out->size--;
1077
1078         return ret;
1079 }