Tizen 2.0 Release
[external/libgnutls26.git] / lib / gnutls_cert.c
1 /*
2  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3  * 2010 Free Software Foundation, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
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 2.1 of
12  * the License, or (at your option) any later version.
13  *
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.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA
23  *
24  */
25
26 /* Some of the stuff needed for Certificate authentication is contained
27  * in this file.
28  */
29
30 #include <gnutls_int.h>
31 #include <gnutls_errors.h>
32 #include <auth_cert.h>
33 #include <gnutls_cert.h>
34 #include <gnutls_datum.h>
35 #include <gnutls_mpi.h>
36 #include <gnutls_global.h>
37 #include <gnutls_algorithms.h>
38 #include <gnutls_dh.h>
39 #include <gnutls_str.h>
40 #include <gnutls_state.h>
41 #include <gnutls_auth.h>
42 #include <gnutls_x509.h>
43 #include "x509/x509_int.h"
44 #ifdef ENABLE_OPENPGP
45 #include "openpgp/gnutls_openpgp.h"
46 #endif
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
58 gnutls_certificate_free_keys (gnutls_certificate_credentials_t sc)
59 {
60   unsigned i, j;
61
62   for (i = 0; i < sc->ncerts; i++)
63     {
64       for (j = 0; j < sc->cert_list_length[i]; j++)
65         {
66           _gnutls_gcert_deinit (&sc->cert_list[i][j]);
67         }
68       gnutls_free (sc->cert_list[i]);
69     }
70
71   gnutls_free (sc->cert_list_length);
72   sc->cert_list_length = NULL;
73
74   gnutls_free (sc->cert_list);
75   sc->cert_list = NULL;
76
77   for (i = 0; i < sc->ncerts; i++)
78     {
79       gnutls_privkey_deinit (sc->pkey[i]);
80     }
81
82   gnutls_free (sc->pkey);
83   sc->pkey = NULL;
84
85   sc->ncerts = 0;
86
87 }
88
89 /**
90  * gnutls_certificate_get_issuer:
91  * @sc: is a #gnutls_certificate_credentials_t structure.
92  * @cert: is the certificate to find issuer for
93  * @issuer: Will hold the issuer if any. Should be treated as constant.
94  * @flags: Use zero.
95  *
96  * This function will return the issuer of a given certificate.
97  *
98  * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
99  *   negative error value.
100  **/
101 int
102 gnutls_certificate_get_issuer (gnutls_certificate_credentials_t sc,
103   gnutls_x509_crt_t cert, gnutls_x509_crt_t* issuer, unsigned int flags)
104 {
105 int i, ret;
106
107   for (i=0;i<sc->x509_ncas;i++)
108     {
109       ret = gnutls_x509_crt_check_issuer(cert, sc->x509_ca_list[i]);
110       if (ret > 0)
111         {
112           *issuer = sc->x509_ca_list[i];
113           return 0;
114         }
115     }
116   
117   return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
118 }
119
120 /**
121  * gnutls_certificate_free_cas:
122  * @sc: is a #gnutls_certificate_credentials_t structure.
123  *
124  * This function will delete all the CAs associated with the given
125  * credentials. Servers that do not use
126  * gnutls_certificate_verify_peers2() may call this to save some
127  * memory.
128  **/
129 void
130 gnutls_certificate_free_cas (gnutls_certificate_credentials_t sc)
131 {
132   unsigned j;
133
134   for (j = 0; j < sc->x509_ncas; j++)
135     {
136       gnutls_x509_crt_deinit (sc->x509_ca_list[j]);
137     }
138
139   sc->x509_ncas = 0;
140
141   gnutls_free (sc->x509_ca_list);
142   sc->x509_ca_list = NULL;
143
144 }
145
146 /**
147  * gnutls_certificate_get_x509_cas:
148  * @sc: is a #gnutls_certificate_credentials_t structure.
149  * @x509_ca_list: will point to the CA list. Should be treated as constant
150  * @ncas: the number of CAs
151  *
152  * This function will export all the CAs associated with the given
153  * credentials.
154  *
155  * Since: 2.4.0
156  **/
157 void
158 gnutls_certificate_get_x509_cas (gnutls_certificate_credentials_t sc,
159                                  gnutls_x509_crt_t ** x509_ca_list,
160                                  unsigned int *ncas)
161 {
162   *x509_ca_list = sc->x509_ca_list;
163   *ncas = sc->x509_ncas;
164 }
165
166 /**
167  * gnutls_certificate_get_x509_crls:
168  * @sc: is a #gnutls_certificate_credentials_t structure.
169  * @x509_crl_list: the exported CRL list. Should be treated as constant
170  * @ncrls: the number of exported CRLs
171  *
172  * This function will export all the CRLs associated with the given
173  * credentials.
174  *
175  * Since: 2.4.0
176  **/
177 void
178 gnutls_certificate_get_x509_crls (gnutls_certificate_credentials_t sc,
179                                   gnutls_x509_crl_t ** x509_crl_list,
180                                   unsigned int *ncrls)
181 {
182   *x509_crl_list = sc->x509_crl_list;
183   *ncrls = sc->x509_ncrls;
184 }
185
186 #ifdef ENABLE_OPENPGP
187
188 /**
189  * gnutls_certificate_get_openpgp_keyring:
190  * @sc: is a #gnutls_certificate_credentials_t structure.
191  * @keyring: the exported keyring. Should be treated as constant
192  *
193  * This function will export the OpenPGP keyring associated with the
194  * given credentials.
195  *
196  * Since: 2.4.0
197  **/
198 void
199 gnutls_certificate_get_openpgp_keyring (gnutls_certificate_credentials_t sc,
200                                         gnutls_openpgp_keyring_t * keyring)
201 {
202   *keyring = sc->keyring;
203 }
204
205 #endif
206
207 /**
208  * gnutls_certificate_free_ca_names:
209  * @sc: is a #gnutls_certificate_credentials_t structure.
210  *
211  * This function will delete all the CA name in the given
212  * credentials. Clients may call this to save some memory since in
213  * client side the CA names are not used. Servers might want to use
214  * this function if a large list of trusted CAs is present and
215  * sending the names of it would just consume bandwidth without providing 
216  * information to client.
217  *
218  * CA names are used by servers to advertize the CAs they support to
219  * clients.
220  **/
221 void
222 gnutls_certificate_free_ca_names (gnutls_certificate_credentials_t sc)
223 {
224   _gnutls_free_datum (&sc->x509_rdn_sequence);
225 }
226
227 /*-
228  * _gnutls_certificate_get_rsa_params - Returns the RSA parameters pointer
229  * @rsa_params: holds the RSA parameters or NULL.
230  * @func: function to retrieve the parameters or NULL.
231  * @session: The session.
232  *
233  * This function will return the rsa parameters pointer.
234  -*/
235 gnutls_rsa_params_t
236 _gnutls_certificate_get_rsa_params (gnutls_rsa_params_t rsa_params,
237                                     gnutls_params_function * func,
238                                     gnutls_session_t session)
239 {
240   gnutls_params_st params;
241   int ret;
242
243   if (session->internals.params.rsa_params)
244     {
245       return session->internals.params.rsa_params;
246     }
247
248   if (rsa_params)
249     {
250       session->internals.params.rsa_params = rsa_params;
251     }
252   else if (func)
253     {
254       ret = func (session, GNUTLS_PARAMS_RSA_EXPORT, &params);
255       if (ret == 0 && params.type == GNUTLS_PARAMS_RSA_EXPORT)
256         {
257           session->internals.params.rsa_params = params.params.rsa_export;
258           session->internals.params.free_rsa_params = params.deinit;
259         }
260     }
261
262   return session->internals.params.rsa_params;
263 }
264
265
266 /**
267  * gnutls_certificate_free_credentials:
268  * @sc: is a #gnutls_certificate_credentials_t structure.
269  *
270  * This structure is complex enough to manipulate directly thus this
271  * helper function is provided in order to free (deallocate) it.
272  *
273  * This function does not free any temporary parameters associated
274  * with this structure (ie RSA and DH parameters are not freed by this
275  * function).
276  **/
277 void
278 gnutls_certificate_free_credentials (gnutls_certificate_credentials_t sc)
279 {
280   gnutls_certificate_free_keys (sc);
281   gnutls_certificate_free_cas (sc);
282   gnutls_certificate_free_ca_names (sc);
283 #ifdef ENABLE_PKI
284   gnutls_certificate_free_crls (sc);
285 #endif
286
287 #ifdef ENABLE_OPENPGP
288   gnutls_openpgp_keyring_deinit (sc->keyring);
289 #endif
290
291   gnutls_free (sc);
292 }
293
294
295 /**
296  * gnutls_certificate_allocate_credentials:
297  * @res: is a pointer to a #gnutls_certificate_credentials_t structure.
298  *
299  * This structure is complex enough to manipulate directly thus this
300  * helper function is provided in order to allocate it.
301  *
302  * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
303  **/
304 int
305 gnutls_certificate_allocate_credentials (gnutls_certificate_credentials_t *
306                                          res)
307 {
308   *res = gnutls_calloc (1, sizeof (certificate_credentials_st));
309
310   if (*res == NULL)
311     return GNUTLS_E_MEMORY_ERROR;
312
313   (*res)->verify_bits = DEFAULT_VERIFY_BITS;
314   (*res)->verify_depth = DEFAULT_VERIFY_DEPTH;
315
316   return 0;
317 }
318
319
320 /* returns the KX algorithms that are supported by a
321  * certificate. (Eg a certificate with RSA params, supports
322  * GNUTLS_KX_RSA algorithm).
323  * This function also uses the KeyUsage field of the certificate
324  * extensions in order to disable unneded algorithms.
325  */
326 int
327 _gnutls_selected_cert_supported_kx (gnutls_session_t session,
328                                     gnutls_kx_algorithm_t ** alg,
329                                     int *alg_size)
330 {
331   gnutls_kx_algorithm_t kx;
332   gnutls_pk_algorithm_t pk;
333   gnutls_kx_algorithm_t kxlist[MAX_ALGOS];
334   gnutls_cert *cert;
335   int i;
336
337   if (session->internals.selected_cert_list_length == 0)
338     {
339       *alg_size = 0;
340       *alg = NULL;
341       return 0;
342     }
343
344   cert = &session->internals.selected_cert_list[0];
345   i = 0;
346
347   for (kx = 0; kx < MAX_ALGOS; kx++)
348     {
349       pk = _gnutls_map_pk_get_pk (kx);
350       if (pk == cert->subject_pk_algorithm)
351         {
352           /* then check key usage */
353           if (_gnutls_check_key_usage (cert, kx) == 0)
354             {
355               kxlist[i] = kx;
356               i++;
357             }
358         }
359     }
360
361   if (i == 0)
362     {
363       gnutls_assert ();
364       return GNUTLS_E_INVALID_REQUEST;
365     }
366
367   *alg = gnutls_calloc (i, sizeof (gnutls_kx_algorithm_t));
368   if (*alg == NULL)
369     return GNUTLS_E_MEMORY_ERROR;
370
371   *alg_size = i;
372
373   memcpy (*alg, kxlist, i * sizeof (gnutls_kx_algorithm_t));
374
375   return 0;
376 }
377
378
379 /**
380  * gnutls_certificate_server_set_request:
381  * @session: is a #gnutls_session_t structure.
382  * @req: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE
383  *
384  * This function specifies if we (in case of a server) are going to
385  * send a certificate request message to the client. If @req is
386  * GNUTLS_CERT_REQUIRE then the server will return an error if the
387  * peer does not provide a certificate. If you do not call this
388  * function then the client will not be asked to send a certificate.
389  **/
390 void
391 gnutls_certificate_server_set_request (gnutls_session_t session,
392                                        gnutls_certificate_request_t req)
393 {
394   session->internals.send_cert_req = req;
395 }
396
397 /**
398  * gnutls_certificate_client_set_retrieve_function:
399  * @cred: is a #gnutls_certificate_credentials_t structure.
400  * @func: is the callback function
401  *
402  * This function sets a callback to be called in order to retrieve the
403  * certificate to be used in the handshake.
404  *
405  * The callback's function prototype is:
406  * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
407  * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr_st* st);
408  *
409  * @req_ca_cert is only used in X.509 certificates.
410  * Contains a list with the CA names that the server considers trusted.
411  * Normally we should send a certificate that is signed
412  * by one of these CAs. These names are DER encoded. To get a more
413  * meaningful value use the function gnutls_x509_rdn_get().
414  *
415  * @pk_algos contains a list with server's acceptable signature algorithms.
416  * The certificate returned should support the server's given algorithms.
417  *
418  * @st should contain the certificates and private keys.
419  *
420  * If the callback function is provided then gnutls will call it, in the
421  * handshake, after the certificate request message has been received.
422  *
423  * The callback function should set the certificate list to be sent,
424  * and return 0 on success. If no certificate was selected then the
425  * number of certificates should be set to zero. The value (-1)
426  * indicates error and the handshake will be terminated.
427  **/
428 void gnutls_certificate_client_set_retrieve_function
429   (gnutls_certificate_credentials_t cred,
430    gnutls_certificate_client_retrieve_function * func)
431 {
432   cred->client_get_cert_callback = func;
433 }
434
435 /**
436  * gnutls_certificate_server_set_retrieve_function:
437  * @cred: is a #gnutls_certificate_credentials_t structure.
438  * @func: is the callback function
439  *
440  * This function sets a callback to be called in order to retrieve the
441  * certificate to be used in the handshake.
442  *
443  * The callback's function prototype is:
444  * int (*callback)(gnutls_session_t, gnutls_retr_st* st);
445  *
446  * @st should contain the certificates and private keys.
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  * The callback function should set the certificate list to be sent, and
452  * return 0 on success.  The value (-1) indicates error and the handshake
453  * will be terminated.
454  **/
455 void gnutls_certificate_server_set_retrieve_function
456   (gnutls_certificate_credentials_t cred,
457    gnutls_certificate_server_retrieve_function * func)
458 {
459   cred->server_get_cert_callback = func;
460 }
461
462 /**
463  * gnutls_certificate_set_retrieve_function:
464  * @cred: is a #gnutls_certificate_credentials_t structure.
465  * @func: is the callback function
466  *
467  * This function sets a callback to be called in order to retrieve the
468  * certificate to be used in the handshake.
469  *
470  * The callback's function prototype is:
471  * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
472  * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr2_st* st);
473  *
474  * @req_ca_cert is only used in X.509 certificates.
475  * Contains a list with the CA names that the server considers trusted.
476  * Normally we should send a certificate that is signed
477  * by one of these CAs. These names are DER encoded. To get a more
478  * meaningful value use the function gnutls_x509_rdn_get().
479  *
480  * @pk_algos contains a list with server's acceptable signature algorithms.
481  * The certificate returned should support the server's given algorithms.
482  *
483  * @st should contain the certificates and private keys.
484  *
485  * If the callback function is provided then gnutls will call it, in the
486  * handshake, after the certificate request message has been received.
487  *
488  * In server side pk_algos and req_ca_dn are NULL.
489  *
490  * The callback function should set the certificate list to be sent,
491  * and return 0 on success. If no certificate was selected then the
492  * number of certificates should be set to zero. The value (-1)
493  * indicates error and the handshake will be terminated.
494  **/
495 void gnutls_certificate_set_retrieve_function
496   (gnutls_certificate_credentials_t cred,
497    gnutls_certificate_retrieve_function * func)
498 {
499   cred->get_cert_callback = func;
500 }
501
502 /**
503  * gnutls_certificate_set_verify_function:
504  * @cred: is a #gnutls_certificate_credentials_t structure.
505  * @func: is the callback function
506  *
507  * This function sets a callback to be called when peer's certificate
508  * has been received in order to verify it on receipt rather than
509  * doing after the handshake is completed.
510  *
511  * The callback's function prototype is:
512  * int (*callback)(gnutls_session_t);
513  *
514  * If the callback function is provided then gnutls will call it, in the
515  * handshake, just after the certificate message has been received.
516  * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
517  * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
518  * can be used.
519  *
520  * The callback function should return 0 for the handshake to continue
521  * or non-zero to terminate.
522  *
523  * Since: 2.10.0
524  **/
525 void
526   gnutls_certificate_set_verify_function
527   (gnutls_certificate_credentials_t cred,
528    gnutls_certificate_verify_function * func)
529 {
530   cred->verify_callback = func;
531 }
532
533 /*-
534  * _gnutls_x509_extract_certificate_activation_time - return the peer's certificate activation time
535  * @cert: should contain an X.509 DER encoded certificate
536  *
537  * This function will return the certificate's activation time in UNIX time
538  * (ie seconds since 00:00:00 UTC January 1, 1970).
539  *
540  * Returns a (time_t) -1 in case of an error.
541  *
542  -*/
543 static time_t
544 _gnutls_x509_get_raw_crt_activation_time (const gnutls_datum_t * cert)
545 {
546   gnutls_x509_crt_t xcert;
547   time_t result;
548
549   result = gnutls_x509_crt_init (&xcert);
550   if (result < 0)
551     return (time_t) - 1;
552
553   result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
554   if (result < 0)
555     {
556       gnutls_x509_crt_deinit (xcert);
557       return (time_t) - 1;
558     }
559
560   result = gnutls_x509_crt_get_activation_time (xcert);
561
562   gnutls_x509_crt_deinit (xcert);
563
564   return result;
565 }
566
567 /*-
568  * gnutls_x509_extract_certificate_expiration_time:
569  * @cert: should contain an X.509 DER encoded certificate
570  *
571  * This function will return the certificate's expiration time in UNIX
572  * time (ie seconds since 00:00:00 UTC January 1, 1970).  Returns a
573  *
574  * (time_t) -1 in case of an error.
575  *
576  -*/
577 static time_t
578 _gnutls_x509_get_raw_crt_expiration_time (const gnutls_datum_t * cert)
579 {
580   gnutls_x509_crt_t xcert;
581   time_t result;
582
583   result = gnutls_x509_crt_init (&xcert);
584   if (result < 0)
585     return (time_t) - 1;
586
587   result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
588   if (result < 0)
589     {
590       gnutls_x509_crt_deinit (xcert);
591       return (time_t) - 1;
592     }
593
594   result = gnutls_x509_crt_get_expiration_time (xcert);
595
596   gnutls_x509_crt_deinit (xcert);
597
598   return result;
599 }
600
601 #ifdef ENABLE_OPENPGP
602 /*-
603  * _gnutls_openpgp_crt_verify_peers - return the peer's certificate status
604  * @session: is a gnutls session
605  *
606  * This function will try to verify the peer's certificate and return its status (TRUSTED, INVALID etc.).
607  * Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
608  -*/
609 static int
610 _gnutls_openpgp_crt_verify_peers (gnutls_session_t session,
611                                   unsigned int *status)
612 {
613   cert_auth_info_t info;
614   gnutls_certificate_credentials_t cred;
615   int peer_certificate_list_size, ret;
616
617   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
618
619   info = _gnutls_get_auth_info (session);
620   if (info == NULL)
621     return GNUTLS_E_INVALID_REQUEST;
622
623   cred = (gnutls_certificate_credentials_t)
624     _gnutls_get_cred (session->key, GNUTLS_CRD_CERTIFICATE, NULL);
625   if (cred == NULL)
626     {
627       gnutls_assert ();
628       return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
629     }
630
631   if (info->raw_certificate_list == NULL || info->ncerts == 0)
632     {
633       gnutls_assert ();
634       return GNUTLS_E_NO_CERTIFICATE_FOUND;
635     }
636
637   /* generate a list of gnutls_certs based on the auth info
638    * raw certs.
639    */
640   peer_certificate_list_size = info->ncerts;
641
642   if (peer_certificate_list_size != 1)
643     {
644       gnutls_assert ();
645       return GNUTLS_E_INTERNAL_ERROR;
646     }
647
648   /* Verify certificate 
649    */
650   ret =
651     _gnutls_openpgp_verify_key (cred, &info->raw_certificate_list[0],
652                                 peer_certificate_list_size, status);
653
654   if (ret < 0)
655     {
656       gnutls_assert ();
657       return ret;
658     }
659
660   return 0;
661 }
662 #endif
663
664 /**
665  * gnutls_certificate_verify_peers2:
666  * @session: is a gnutls session
667  * @status: is the output of the verification
668  *
669  * This function will try to verify the peer's certificate and return
670  * its status (trusted, invalid etc.).  The value of @status should
671  * be one or more of the gnutls_certificate_status_t enumerated
672  * elements bitwise or'd. To avoid denial of service attacks some
673  * default upper limits regarding the certificate key size and chain
674  * size are set. To override them use
675  * gnutls_certificate_set_verify_limits().
676  *
677  * Note that you must also check the peer's name in order to check if
678  * the verified certificate belongs to the actual peer.
679  *
680  * This function uses gnutls_x509_crt_list_verify() with the CAs in
681  * the credentials as trusted CAs.
682  *
683  * Returns: a negative error code on error and zero on success.
684  **/
685 int
686 gnutls_certificate_verify_peers2 (gnutls_session_t session,
687                                   unsigned int *status)
688 {
689   cert_auth_info_t info;
690
691   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
692
693   info = _gnutls_get_auth_info (session);
694   if (info == NULL)
695     {
696       return GNUTLS_E_NO_CERTIFICATE_FOUND;
697     }
698
699   if (info->raw_certificate_list == NULL || info->ncerts == 0)
700     return GNUTLS_E_NO_CERTIFICATE_FOUND;
701
702   switch (gnutls_certificate_type_get (session))
703     {
704     case GNUTLS_CRT_X509:
705       return _gnutls_x509_cert_verify_peers (session, status);
706 #ifdef ENABLE_OPENPGP
707     case GNUTLS_CRT_OPENPGP:
708       return _gnutls_openpgp_crt_verify_peers (session, status);
709 #endif
710     default:
711       return GNUTLS_E_INVALID_REQUEST;
712     }
713 }
714
715 /**
716  * gnutls_certificate_verify_peers:
717  * @session: is a gnutls session
718  *
719  * This function will try to verify the peer's certificate and return
720  * its status (trusted, invalid etc.).  However you must also check
721  * the peer's name in order to check if the verified certificate
722  * belongs to the actual peer.
723  *
724  * This function uses gnutls_x509_crt_list_verify().
725  *
726  * Returns: one or more of the #gnutls_certificate_status_t
727  * enumerated elements bitwise or'd, or a negative value on error.
728  *
729  * Deprecated: Use gnutls_certificate_verify_peers2() instead.
730  **/
731 int
732 gnutls_certificate_verify_peers (gnutls_session_t session)
733 {
734   unsigned int status;
735   int ret;
736
737   ret = gnutls_certificate_verify_peers2 (session, &status);
738
739   if (ret < 0)
740     {
741       gnutls_assert ();
742       return ret;
743     }
744
745   return status;
746 }
747
748 /**
749  * gnutls_certificate_expiration_time_peers:
750  * @session: is a gnutls session
751  *
752  * This function will return the peer's certificate expiration time.
753  *
754  * Returns: (time_t)-1 on error.
755  *
756  * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
757  **/
758 time_t
759 gnutls_certificate_expiration_time_peers (gnutls_session_t session)
760 {
761   cert_auth_info_t info;
762
763   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
764
765   info = _gnutls_get_auth_info (session);
766   if (info == NULL)
767     {
768       return (time_t) - 1;
769     }
770
771   if (info->raw_certificate_list == NULL || info->ncerts == 0)
772     {
773       gnutls_assert ();
774       return (time_t) - 1;
775     }
776
777   switch (gnutls_certificate_type_get (session))
778     {
779     case GNUTLS_CRT_X509:
780       return
781         _gnutls_x509_get_raw_crt_expiration_time (&info->raw_certificate_list
782                                                   [0]);
783 #ifdef ENABLE_OPENPGP
784     case GNUTLS_CRT_OPENPGP:
785       return
786         _gnutls_openpgp_get_raw_key_expiration_time
787         (&info->raw_certificate_list[0]);
788 #endif
789     default:
790       return (time_t) - 1;
791     }
792 }
793
794 /**
795  * gnutls_certificate_activation_time_peers:
796  * @session: is a gnutls session
797  *
798  * This function will return the peer's certificate activation time.
799  * This is the creation time for openpgp keys.
800  *
801  * Returns: (time_t)-1 on error.
802  *
803  * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
804  **/
805 time_t
806 gnutls_certificate_activation_time_peers (gnutls_session_t session)
807 {
808   cert_auth_info_t info;
809
810   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
811
812   info = _gnutls_get_auth_info (session);
813   if (info == NULL)
814     {
815       return (time_t) - 1;
816     }
817
818   if (info->raw_certificate_list == NULL || info->ncerts == 0)
819     {
820       gnutls_assert ();
821       return (time_t) - 1;
822     }
823
824   switch (gnutls_certificate_type_get (session))
825     {
826     case GNUTLS_CRT_X509:
827       return
828         _gnutls_x509_get_raw_crt_activation_time (&info->raw_certificate_list
829                                                   [0]);
830 #ifdef ENABLE_OPENPGP
831     case GNUTLS_CRT_OPENPGP:
832       return
833         _gnutls_openpgp_get_raw_key_creation_time (&info->raw_certificate_list
834                                                    [0]);
835 #endif
836     default:
837       return (time_t) - 1;
838     }
839 }
840
841 /* Converts the first certificate for the cert_auth_info structure
842  * to a gcert.
843  */
844 int
845 _gnutls_get_auth_info_gcert (gnutls_cert * gcert,
846                              gnutls_certificate_type_t type,
847                              cert_auth_info_t info,
848                              int flags /* OR of ConvFlags */ )
849 {
850   switch (type)
851     {
852     case GNUTLS_CRT_X509:
853       return _gnutls_x509_raw_cert_to_gcert (gcert,
854                                              &info->raw_certificate_list[0],
855                                              flags);
856 #ifdef ENABLE_OPENPGP
857     case GNUTLS_CRT_OPENPGP:
858       return _gnutls_openpgp_raw_crt_to_gcert (gcert,
859                                                &info->raw_certificate_list[0],
860                                                info->use_subkey ? info->
861                                                subkey_id : NULL);
862 #endif
863     default:
864       gnutls_assert ();
865       return GNUTLS_E_INTERNAL_ERROR;
866     }
867 }
868
869 /* This function will convert a der certificate to a format
870  * (structure) that gnutls can understand and use. Actually the
871  * important thing on this function is that it extracts the 
872  * certificate's (public key) parameters.
873  *
874  * The noext flag is used to complete the handshake even if the
875  * extensions found in the certificate are unsupported and critical. 
876  * The critical extensions will be catched by the verification functions.
877  */
878 int
879 _gnutls_x509_raw_cert_to_gcert (gnutls_cert * gcert,
880                                 const gnutls_datum_t * derCert,
881                                 int flags /* OR of ConvFlags */ )
882 {
883   int ret;
884   gnutls_x509_crt_t cert;
885
886   ret = gnutls_x509_crt_init (&cert);
887   if (ret < 0)
888     {
889       gnutls_assert ();
890       return ret;
891     }
892
893   ret = gnutls_x509_crt_import (cert, derCert, GNUTLS_X509_FMT_DER);
894   if (ret < 0)
895     {
896       gnutls_assert ();
897       gnutls_x509_crt_deinit (cert);
898       return ret;
899     }
900
901   ret = _gnutls_x509_crt_to_gcert (gcert, cert, flags);
902   gnutls_x509_crt_deinit (cert);
903
904   return ret;
905 }
906
907 /* Like above but it accepts a parsed certificate instead.
908  */
909 int
910 _gnutls_x509_crt_to_gcert (gnutls_cert * gcert,
911                            gnutls_x509_crt_t cert, unsigned int flags)
912 {
913   int ret = 0;
914
915   memset (gcert, 0, sizeof (gnutls_cert));
916   gcert->cert_type = GNUTLS_CRT_X509;
917   gcert->sign_algo = gnutls_x509_crt_get_signature_algorithm (cert);
918
919   if (!(flags & CERT_NO_COPY))
920     {
921 #define SMALL_DER 1536
922       opaque *der;
923       size_t der_size = SMALL_DER;
924
925       /* initially allocate a bogus size, just in case the certificate
926        * fits in it. That way we minimize the DER encodings performed.
927        */
928       der = gnutls_malloc (SMALL_DER);
929       if (der == NULL)
930         {
931           gnutls_assert ();
932           return GNUTLS_E_MEMORY_ERROR;
933         }
934
935       ret =
936         gnutls_x509_crt_export (cert, GNUTLS_X509_FMT_DER, der, &der_size);
937       if (ret < 0 && ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
938         {
939           gnutls_assert ();
940           gnutls_free (der);
941           return ret;
942         }
943
944       if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
945         {
946           der = gnutls_realloc (der, der_size);
947           if (der == NULL)
948             {
949               gnutls_assert ();
950               return GNUTLS_E_MEMORY_ERROR;
951             }
952
953           ret =
954             gnutls_x509_crt_export (cert, GNUTLS_X509_FMT_DER, der,
955                                     &der_size);
956           if (ret < 0)
957             {
958               gnutls_assert ();
959               gnutls_free (der);
960               return ret;
961             }
962         }
963
964       gcert->raw.data = der;
965       gcert->raw.size = der_size;
966     }
967   else
968     /* now we have 0 or a bitwise or of things to decode */
969     flags ^= CERT_NO_COPY;
970
971
972   if (flags & CERT_ONLY_EXTENSIONS || flags == 0)
973     {
974       ret = gnutls_x509_crt_get_key_usage (cert, &gcert->key_usage, NULL);
975       if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
976         gcert->key_usage = 0;
977       else if (ret < 0)
978         {
979           gnutls_assert ();
980           return ret;
981         }
982       gcert->version = gnutls_x509_crt_get_version (cert);
983     }
984   gcert->subject_pk_algorithm = gnutls_x509_crt_get_pk_algorithm (cert, NULL);
985
986   if (flags & CERT_ONLY_PUBKEY || flags == 0)
987     {
988       gcert->params_size = MAX_PUBLIC_PARAMS_SIZE;
989       ret =
990         _gnutls_x509_crt_get_mpis (cert, gcert->params, &gcert->params_size);
991       if (ret < 0)
992         {
993           gnutls_assert ();
994           return ret;
995         }
996     }
997
998   return 0;
999
1000 }
1001
1002 void
1003 _gnutls_gcert_deinit (gnutls_cert * cert)
1004 {
1005   int i;
1006
1007   if (cert == NULL)
1008     return;
1009
1010   for (i = 0; i < cert->params_size; i++)
1011     {
1012       _gnutls_mpi_release (&cert->params[i]);
1013     }
1014
1015   _gnutls_free_datum (&cert->raw);
1016 }
1017
1018 /**
1019  * gnutls_sign_callback_set:
1020  * @session: is a gnutls session
1021  * @sign_func: function pointer to application's sign callback.
1022  * @userdata: void pointer that will be passed to sign callback.
1023  *
1024  * Set the callback function.  The function must have this prototype:
1025  *
1026  * typedef int (*gnutls_sign_func) (gnutls_session_t session,
1027  *                                  void *userdata,
1028  *                                  gnutls_certificate_type_t cert_type,
1029  *                                  const gnutls_datum_t * cert,
1030  *                                  const gnutls_datum_t * hash,
1031  *                                  gnutls_datum_t * signature);
1032  *
1033  * The @userdata parameter is passed to the @sign_func verbatim, and
1034  * can be used to store application-specific data needed in the
1035  * callback function.  See also gnutls_sign_callback_get().
1036  *
1037  * Deprecated: Use the PKCS 11 interfaces instead.
1038  */
1039 void
1040 gnutls_sign_callback_set (gnutls_session_t session,
1041                           gnutls_sign_func sign_func, void *userdata)
1042 {
1043   session->internals.sign_func = sign_func;
1044   session->internals.sign_func_userdata = userdata;
1045 }
1046
1047 /**
1048  * gnutls_sign_callback_get:
1049  * @session: is a gnutls session
1050  * @userdata: if non-%NULL, will be set to abstract callback pointer.
1051  *
1052  * Retrieve the callback function, and its userdata pointer.
1053  *
1054  * Returns: The function pointer set by gnutls_sign_callback_set(), or
1055  *   if not set, %NULL.
1056  *
1057  * Deprecated: Use the PKCS 11 interfaces instead.
1058  */
1059 gnutls_sign_func
1060 gnutls_sign_callback_get (gnutls_session_t session, void **userdata)
1061 {
1062   if (userdata)
1063     *userdata = session->internals.sign_func_userdata;
1064   return session->internals.sign_func;
1065 }