ac2505199929c6c30258ae5fd5e3f58d70d71b40
[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 3 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/x509_int.h"
41 #ifdef ENABLE_OPENPGP
42 #include "openpgp/gnutls_openpgp.h"
43 #endif
44
45 /**
46  * gnutls_certificate_free_keys:
47  * @sc: is a #gnutls_certificate_credentials_t structure.
48  *
49  * This function will delete all the keys and the certificates associated
50  * with the given credentials. This function must not be called when a
51  * TLS negotiation that uses the credentials is in progress.
52  *
53  **/
54 void
55 gnutls_certificate_free_keys (gnutls_certificate_credentials_t sc)
56 {
57   unsigned i, j;
58
59   for (i = 0; i < sc->ncerts; i++)
60     {
61       for (j = 0; j < sc->certs[i].cert_list_length; j++)
62         {
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     {
74       gnutls_privkey_deinit (sc->pkey[i]);
75     }
76
77   gnutls_free (sc->pkey);
78   sc->pkey = NULL;
79
80   sc->ncerts = 0;
81 }
82
83 /**
84  * gnutls_certificate_free_cas:
85  * @sc: is a #gnutls_certificate_credentials_t structure.
86  *
87  * This function will delete all the CAs associated with the given
88  * credentials. Servers that do not use
89  * gnutls_certificate_verify_peers2() may call this to save some
90  * memory.
91  **/
92 void
93 gnutls_certificate_free_cas (gnutls_certificate_credentials_t sc)
94 {
95   /* FIXME: do nothing for now */
96   return;
97 }
98
99 /**
100  * gnutls_certificate_get_issuer:
101  * @sc: is a #gnutls_certificate_credentials_t structure.
102  * @cert: is the certificate to find issuer for
103  * @issuer: Will hold the issuer if any. Should be treated as constant.
104  * @flags: Use zero.
105  *
106  * This function will return the issuer of a given certificate.
107  *
108  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
109  *   negative error value.
110  *
111  * Since: 3.0
112  **/
113 int
114 gnutls_certificate_get_issuer (gnutls_certificate_credentials_t sc,
115   gnutls_x509_crt_t cert, gnutls_x509_crt_t* issuer, unsigned int flags)
116 {
117   return gnutls_x509_trust_list_get_issuer(sc->tlist, cert, issuer, flags);
118 }
119
120 /**
121  * gnutls_certificate_free_ca_names:
122  * @sc: is a #gnutls_certificate_credentials_t structure.
123  *
124  * This function will delete all the CA name in the given
125  * credentials. Clients may call this to save some memory since in
126  * client side the CA names are not used. Servers might want to use
127  * this function if a large list of trusted CAs is present and
128  * sending the names of it would just consume bandwidth without providing 
129  * information to client.
130  *
131  * CA names are used by servers to advertise the CAs they support to
132  * clients.
133  **/
134 void
135 gnutls_certificate_free_ca_names (gnutls_certificate_credentials_t sc)
136 {
137   _gnutls_free_datum (&sc->x509_rdn_sequence);
138 }
139
140 /*-
141  * _gnutls_certificate_get_rsa_params - Returns the RSA parameters pointer
142  * @rsa_params: holds the RSA parameters or NULL.
143  * @func: function to retrieve the parameters or NULL.
144  * @session: The session.
145  *
146  * This function will return the rsa parameters pointer.
147  -*/
148 gnutls_rsa_params_t
149 _gnutls_certificate_get_rsa_params (gnutls_rsa_params_t rsa_params,
150                                     gnutls_params_function * func,
151                                     gnutls_session_t session)
152 {
153   gnutls_params_st params;
154   int ret;
155
156   if (session->internals.params.rsa_params)
157     {
158       return session->internals.params.rsa_params;
159     }
160
161   if (rsa_params)
162     {
163       session->internals.params.rsa_params = rsa_params;
164     }
165   else if (func)
166     {
167       ret = func (session, GNUTLS_PARAMS_RSA_EXPORT, &params);
168       if (ret == 0 && params.type == GNUTLS_PARAMS_RSA_EXPORT)
169         {
170           session->internals.params.rsa_params = params.params.rsa_export;
171           session->internals.params.free_rsa_params = params.deinit;
172         }
173     }
174
175   return session->internals.params.rsa_params;
176 }
177
178
179 /**
180  * gnutls_certificate_free_credentials:
181  * @sc: is a #gnutls_certificate_credentials_t structure.
182  *
183  * This structure is complex enough to manipulate directly thus this
184  * helper function is provided in order to free (deallocate) it.
185  *
186  * This function does not free any temporary parameters associated
187  * with this structure (ie RSA and DH parameters are not freed by this
188  * function).
189  **/
190 void
191 gnutls_certificate_free_credentials (gnutls_certificate_credentials_t sc)
192 {
193   gnutls_x509_trust_list_deinit(sc->tlist, 1);
194   gnutls_certificate_free_keys (sc);
195   gnutls_certificate_free_ca_names (sc);
196
197 #ifdef ENABLE_OPENPGP
198   gnutls_openpgp_keyring_deinit (sc->keyring);
199 #endif
200
201   gnutls_free (sc);
202 }
203
204
205 /**
206  * gnutls_certificate_allocate_credentials:
207  * @res: is a pointer to a #gnutls_certificate_credentials_t structure.
208  *
209  * This structure is complex enough to manipulate directly thus this
210  * helper function is provided in order to allocate it.
211  *
212  * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
213  **/
214 int
215 gnutls_certificate_allocate_credentials (gnutls_certificate_credentials_t *
216                                          res)
217 {
218 int ret;
219
220   *res = gnutls_calloc (1, sizeof (certificate_credentials_st));
221
222   if (*res == NULL)
223     return GNUTLS_E_MEMORY_ERROR;
224
225   ret = gnutls_x509_trust_list_init( &(*res)->tlist, 0);
226   if (ret < 0)
227     {
228       gnutls_assert();
229       gnutls_free(*res);
230       return GNUTLS_E_MEMORY_ERROR;
231     }
232   (*res)->verify_bits = DEFAULT_VERIFY_BITS;
233   (*res)->verify_depth = DEFAULT_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   gnutls_kx_algorithm_t 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     {
257       *alg_size = 0;
258       return 0;
259     }
260
261   cert = &session->internals.selected_cert_list[0];
262   cert_pk = gnutls_pubkey_get_pk_algorithm(cert->pubkey, NULL);
263   i = 0;
264
265   for (kx = 0; kx < MAX_ALGOS; kx++)
266     {
267       pk = _gnutls_map_pk_get_pk (kx);
268       if (pk == cert_pk)
269         {
270           /* then check key usage */
271           if (_gnutls_check_key_usage (cert, kx) == 0)
272             {
273               alg[i] = kx;
274               i++;
275               
276               if (i > *alg_size)
277                 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
278             }
279         }
280     }
281
282   if (i == 0)
283     {
284       gnutls_assert ();
285       return GNUTLS_E_INVALID_REQUEST;
286     }
287
288   *alg_size = i;
289
290   return 0;
291 }
292
293
294 /**
295  * gnutls_certificate_server_set_request:
296  * @session: is a #gnutls_session_t structure.
297  * @req: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE
298  *
299  * This function specifies if we (in case of a server) are going to
300  * send a certificate request message to the client. If @req is
301  * GNUTLS_CERT_REQUIRE then the server will return an error if the
302  * peer does not provide a certificate. If you do not call this
303  * function then the client will not be asked to send a certificate.
304  **/
305 void
306 gnutls_certificate_server_set_request (gnutls_session_t session,
307                                        gnutls_certificate_request_t req)
308 {
309   session->internals.send_cert_req = req;
310 }
311
312 /**
313  * gnutls_certificate_client_set_retrieve_function:
314  * @cred: is a #gnutls_certificate_credentials_t structure.
315  * @func: is the callback function
316  *
317  * This function sets a callback to be called in order to retrieve the
318  * certificate to be used in the handshake.
319  * You are advised to use gnutls_certificate_set_retrieve_function2() because it
320  * is much more efficient in the processing it requires from gnutls.
321  *
322  * The callback's function prototype is:
323  * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
324  * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr_st* st);
325  *
326  * @req_ca_cert is only used in X.509 certificates.
327  * Contains a list with the CA names that the server considers trusted.
328  * Normally we should send a certificate that is signed
329  * by one of these CAs. These names are DER encoded. To get a more
330  * meaningful value use the function gnutls_x509_rdn_get().
331  *
332  * @pk_algos contains a list with server's acceptable signature algorithms.
333  * The certificate returned should support the server's given algorithms.
334  *
335  * @st should contain the certificates and private keys.
336  *
337  * If the callback function is provided then gnutls will call it, in the
338  * handshake, if a certificate is requested by the server (and after the 
339  * certificate request message has been received).
340  *
341  * The callback function should set the certificate list to be sent,
342  * and return 0 on success. If no certificate was selected then the
343  * number of certificates should be set to zero. The value (-1)
344  * indicates error and the handshake will be terminated.
345  **/
346 void gnutls_certificate_client_set_retrieve_function
347   (gnutls_certificate_credentials_t cred,
348    gnutls_certificate_client_retrieve_function * func)
349 {
350   cred->client_get_cert_callback = func;
351 }
352
353 /**
354  * gnutls_certificate_server_set_retrieve_function:
355  * @cred: is a #gnutls_certificate_credentials_t structure.
356  * @func: is the callback function
357  *
358  * This function sets a callback to be called in order to retrieve the
359  * certificate to be used in the handshake.
360  * You are advised to use gnutls_certificate_set_retrieve_function2() because it
361  * is much more efficient in the processing it requires from gnutls.
362  *
363  * The callback's function prototype is:
364  * int (*callback)(gnutls_session_t, gnutls_retr_st* st);
365  *
366  * @st should contain the certificates and private keys.
367  *
368  * If the callback function is provided then gnutls will call it, in the
369  * handshake, after the certificate request message has been received.
370  *
371  * The callback function should set the certificate list to be sent, and
372  * return 0 on success.  The value (-1) indicates error and the handshake
373  * will be terminated.
374  **/
375 void gnutls_certificate_server_set_retrieve_function
376   (gnutls_certificate_credentials_t cred,
377    gnutls_certificate_server_retrieve_function * func)
378 {
379   cred->server_get_cert_callback = func;
380 }
381
382 /**
383  * gnutls_certificate_set_retrieve_function:
384  * @cred: is a #gnutls_certificate_credentials_t structure.
385  * @func: is the callback function
386  *
387  * This function sets a callback to be called in order to retrieve the
388  * certificate to be used in the handshake. You are advised
389  * to use gnutls_certificate_set_retrieve_function2() because it
390  * is much more efficient in the processing it requires from gnutls.
391  *
392  * The callback's function prototype is:
393  * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
394  * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr2_st* st);
395  *
396  * @req_ca_cert is only used in X.509 certificates.
397  * Contains a list with the CA names that the server considers trusted.
398  * Normally we should send a certificate that is signed
399  * by one of these CAs. These names are DER encoded. To get a more
400  * meaningful value use the function gnutls_x509_rdn_get().
401  *
402  * @pk_algos contains a list with server's acceptable signature algorithms.
403  * The certificate returned should support the server's given algorithms.
404  *
405  * @st should contain the certificates and private keys.
406  *
407  * If the callback function is provided then gnutls will call it, in the
408  * handshake, after the certificate request message has been received.
409  *
410  * In server side pk_algos and req_ca_dn are NULL.
411  *
412  * The callback function should set the certificate list to be sent,
413  * and return 0 on success. If no certificate was selected then the
414  * number of certificates should be set to zero. The value (-1)
415  * indicates error and the handshake will be terminated.
416  *
417  * Since: 3.0
418  **/
419 void gnutls_certificate_set_retrieve_function
420   (gnutls_certificate_credentials_t cred,
421    gnutls_certificate_retrieve_function * func)
422 {
423   cred->get_cert_callback = func;
424 }
425
426 /**
427  * gnutls_certificate_set_retrieve_function2:
428  * @cred: is a #gnutls_certificate_credentials_t structure.
429  * @func: is the callback function
430  *
431  * This function sets a callback to be called in order to retrieve the
432  * certificate to be used in the handshake.
433  *
434  * The callback's function prototype is:
435  * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
436  * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_pcert_st** pcert,
437  * unsigned int *pcert_length, gnutls_privkey_t * pkey);
438  *
439  * @req_ca_cert is only used in X.509 certificates.
440  * Contains a list with the CA names that the server considers trusted.
441  * Normally we should send a certificate that is signed
442  * by one of these CAs. These names are DER encoded. To get a more
443  * meaningful value use the function gnutls_x509_rdn_get().
444  *
445  * @pk_algos contains a list with server's acceptable signature algorithms.
446  * The certificate returned should support the server's given algorithms.
447  *
448  * @pcert should contain a single certificate and public or a list of them.
449  *
450  * @pcert_length is the size of the previous list.
451  *
452  * @pkey is the private key.
453  *
454  * If the callback function is provided then gnutls will call it, in the
455  * handshake, after the certificate request message has been received.
456  *
457  * In server side pk_algos and req_ca_dn are NULL.
458  *
459  * The callback function should set the certificate list to be sent,
460  * and return 0 on success. If no certificate was selected then the
461  * number of certificates should be set to zero. The value (-1)
462  * indicates error and the handshake will be terminated.
463  *
464  * Since: 3.0
465  **/
466 void gnutls_certificate_set_retrieve_function2
467   (gnutls_certificate_credentials_t cred,
468    gnutls_certificate_retrieve_function2 * func)
469 {
470   cred->get_cert_callback2 = func;
471 }
472
473 /**
474  * gnutls_certificate_set_verify_function:
475  * @cred: is a #gnutls_certificate_credentials_t structure.
476  * @func: is the callback function
477  *
478  * This function sets a callback to be called when peer's certificate
479  * has been received in order to verify it on receipt rather than
480  * doing after the handshake is completed.
481  *
482  * The callback's function prototype is:
483  * int (*callback)(gnutls_session_t);
484  *
485  * If the callback function is provided then gnutls will call it, in the
486  * handshake, just after the certificate message has been received.
487  * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
488  * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
489  * can be used.
490  *
491  * The callback function should return 0 for the handshake to continue
492  * or non-zero to terminate.
493  *
494  * Since: 2.10.0
495  **/
496 void
497   gnutls_certificate_set_verify_function
498   (gnutls_certificate_credentials_t cred,
499    gnutls_certificate_verify_function * func)
500 {
501   cred->verify_callback = func;
502 }
503
504 /*-
505  * _gnutls_x509_extract_certificate_activation_time - return the peer's certificate activation time
506  * @cert: should contain an X.509 DER encoded certificate
507  *
508  * This function will return the certificate's activation time in UNIX time
509  * (ie seconds since 00:00:00 UTC January 1, 1970).
510  *
511  * Returns a (time_t) -1 in case of an error.
512  *
513  -*/
514 static time_t
515 _gnutls_x509_get_raw_crt_activation_time (const gnutls_datum_t * cert)
516 {
517   gnutls_x509_crt_t xcert;
518   time_t result;
519
520   result = gnutls_x509_crt_init (&xcert);
521   if (result < 0)
522     return (time_t) - 1;
523
524   result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
525   if (result < 0)
526     {
527       gnutls_x509_crt_deinit (xcert);
528       return (time_t) - 1;
529     }
530
531   result = gnutls_x509_crt_get_activation_time (xcert);
532
533   gnutls_x509_crt_deinit (xcert);
534
535   return result;
536 }
537
538 /*-
539  * gnutls_x509_extract_certificate_expiration_time:
540  * @cert: should contain an X.509 DER encoded certificate
541  *
542  * This function will return the certificate's expiration time in UNIX
543  * time (ie seconds since 00:00:00 UTC January 1, 1970).  Returns a
544  *
545  * (time_t) -1 in case of an error.
546  *
547  -*/
548 static time_t
549 _gnutls_x509_get_raw_crt_expiration_time (const gnutls_datum_t * cert)
550 {
551   gnutls_x509_crt_t xcert;
552   time_t result;
553
554   result = gnutls_x509_crt_init (&xcert);
555   if (result < 0)
556     return (time_t) - 1;
557
558   result = gnutls_x509_crt_import (xcert, cert, GNUTLS_X509_FMT_DER);
559   if (result < 0)
560     {
561       gnutls_x509_crt_deinit (xcert);
562       return (time_t) - 1;
563     }
564
565   result = gnutls_x509_crt_get_expiration_time (xcert);
566
567   gnutls_x509_crt_deinit (xcert);
568
569   return result;
570 }
571
572 #ifdef ENABLE_OPENPGP
573 /*-
574  * _gnutls_openpgp_crt_verify_peers - return the peer's certificate status
575  * @session: is a gnutls session
576  *
577  * This function will try to verify the peer's certificate and return its status (TRUSTED, INVALID etc.).
578  * Returns a negative error code in case of an error, or GNUTLS_E_NO_CERTIFICATE_FOUND if no certificate was sent.
579  -*/
580 static int
581 _gnutls_openpgp_crt_verify_peers (gnutls_session_t session,
582                                   unsigned int *status)
583 {
584   cert_auth_info_t info;
585   gnutls_certificate_credentials_t cred;
586   int peer_certificate_list_size, ret;
587
588   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
589
590   info = _gnutls_get_auth_info (session);
591   if (info == NULL)
592     return GNUTLS_E_INVALID_REQUEST;
593
594   cred = (gnutls_certificate_credentials_t)
595     _gnutls_get_cred (session->key, GNUTLS_CRD_CERTIFICATE, NULL);
596   if (cred == NULL)
597     {
598       gnutls_assert ();
599       return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
600     }
601
602   if (info->raw_certificate_list == NULL || info->ncerts == 0)
603     {
604       gnutls_assert ();
605       return GNUTLS_E_NO_CERTIFICATE_FOUND;
606     }
607
608   /* generate a list of gnutls_certs based on the auth info
609    * raw certs.
610    */
611   peer_certificate_list_size = info->ncerts;
612
613   if (peer_certificate_list_size != 1)
614     {
615       gnutls_assert ();
616       return GNUTLS_E_INTERNAL_ERROR;
617     }
618
619   /* Verify certificate 
620    */
621   ret =
622     _gnutls_openpgp_verify_key (cred, &info->raw_certificate_list[0],
623                                 peer_certificate_list_size, status);
624
625   if (ret < 0)
626     {
627       gnutls_assert ();
628       return ret;
629     }
630
631   return 0;
632 }
633 #endif
634
635 /**
636  * gnutls_certificate_verify_peers2:
637  * @session: is a gnutls session
638  * @status: is the output of the verification
639  *
640  * This function will try to verify the peer's certificate and return
641  * its status (trusted, invalid etc.).  The value of @status should
642  * be one or more of the gnutls_certificate_status_t enumerated
643  * elements bitwise or'd. To avoid denial of service attacks some
644  * default upper limits regarding the certificate key size and chain
645  * size are set. To override them use
646  * gnutls_certificate_set_verify_limits().
647  *
648  * Note that you must also check the peer's name in order to check if
649  * the verified certificate belongs to the actual peer.
650  *
651  * This function uses gnutls_x509_crt_list_verify() with the CAs in
652  * the credentials as trusted CAs.
653  *
654  * Returns: a negative error code on error and %GNUTLS_E_SUCCESS (0) on success.
655  **/
656 int
657 gnutls_certificate_verify_peers2 (gnutls_session_t session,
658                                   unsigned int *status)
659 {
660   cert_auth_info_t info;
661
662   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
663
664   info = _gnutls_get_auth_info (session);
665   if (info == NULL)
666     {
667       return GNUTLS_E_NO_CERTIFICATE_FOUND;
668     }
669
670   if (info->raw_certificate_list == NULL || info->ncerts == 0)
671     return GNUTLS_E_NO_CERTIFICATE_FOUND;
672
673   switch (gnutls_certificate_type_get (session))
674     {
675     case GNUTLS_CRT_X509:
676       return _gnutls_x509_cert_verify_peers (session, status);
677 #ifdef ENABLE_OPENPGP
678     case GNUTLS_CRT_OPENPGP:
679       return _gnutls_openpgp_crt_verify_peers (session, status);
680 #endif
681     default:
682       return GNUTLS_E_INVALID_REQUEST;
683     }
684 }
685
686 /**
687  * gnutls_certificate_expiration_time_peers:
688  * @session: is a gnutls session
689  *
690  * This function will return the peer's certificate expiration time.
691  *
692  * Returns: (time_t)-1 on error.
693  *
694  * Deprecated: gnutls_certificate_verify_peers2() now verifies expiration times.
695  **/
696 time_t
697 gnutls_certificate_expiration_time_peers (gnutls_session_t session)
698 {
699   cert_auth_info_t info;
700
701   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
702
703   info = _gnutls_get_auth_info (session);
704   if (info == NULL)
705     {
706       return (time_t) - 1;
707     }
708
709   if (info->raw_certificate_list == NULL || info->ncerts == 0)
710     {
711       gnutls_assert ();
712       return (time_t) - 1;
713     }
714
715   switch (gnutls_certificate_type_get (session))
716     {
717     case GNUTLS_CRT_X509:
718       return
719         _gnutls_x509_get_raw_crt_expiration_time (&info->raw_certificate_list
720                                                   [0]);
721 #ifdef ENABLE_OPENPGP
722     case GNUTLS_CRT_OPENPGP:
723       return
724         _gnutls_openpgp_get_raw_key_expiration_time
725         (&info->raw_certificate_list[0]);
726 #endif
727     default:
728       return (time_t) - 1;
729     }
730 }
731
732 /**
733  * gnutls_certificate_activation_time_peers:
734  * @session: is a gnutls session
735  *
736  * This function will return the peer's certificate activation time.
737  * This is the creation time for openpgp keys.
738  *
739  * Returns: (time_t)-1 on error.
740  *
741  * Deprecated: gnutls_certificate_verify_peers2() now verifies activation times.
742  **/
743 time_t
744 gnutls_certificate_activation_time_peers (gnutls_session_t session)
745 {
746   cert_auth_info_t info;
747
748   CHECK_AUTH (GNUTLS_CRD_CERTIFICATE, GNUTLS_E_INVALID_REQUEST);
749
750   info = _gnutls_get_auth_info (session);
751   if (info == NULL)
752     {
753       return (time_t) - 1;
754     }
755
756   if (info->raw_certificate_list == NULL || info->ncerts == 0)
757     {
758       gnutls_assert ();
759       return (time_t) - 1;
760     }
761
762   switch (gnutls_certificate_type_get (session))
763     {
764     case GNUTLS_CRT_X509:
765       return
766         _gnutls_x509_get_raw_crt_activation_time (&info->raw_certificate_list
767                                                   [0]);
768 #ifdef ENABLE_OPENPGP
769     case GNUTLS_CRT_OPENPGP:
770       return
771         _gnutls_openpgp_get_raw_key_creation_time (&info->raw_certificate_list
772                                                    [0]);
773 #endif
774     default:
775       return (time_t) - 1;
776     }
777 }
778
779 /**
780  * gnutls_sign_callback_set:
781  * @session: is a gnutls session
782  * @sign_func: function pointer to application's sign callback.
783  * @userdata: void pointer that will be passed to sign callback.
784  *
785  * Set the callback function.  The function must have this prototype:
786  *
787  * typedef int (*gnutls_sign_func) (gnutls_session_t session,
788  *                                  void *userdata,
789  *                                  gnutls_certificate_type_t cert_type,
790  *                                  const gnutls_datum_t * cert,
791  *                                  const gnutls_datum_t * hash,
792  *                                  gnutls_datum_t * signature);
793  *
794  * The @userdata parameter is passed to the @sign_func verbatim, and
795  * can be used to store application-specific data needed in the
796  * callback function.  See also gnutls_sign_callback_get().
797  *
798  * Deprecated: Use the PKCS 11 or #gnutls_privkey_t interfacess like gnutls_privkey_import_ext() instead.
799  */
800 void
801 gnutls_sign_callback_set (gnutls_session_t session,
802                           gnutls_sign_func sign_func, void *userdata)
803 {
804   session->internals.sign_func = sign_func;
805   session->internals.sign_func_userdata = userdata;
806 }
807
808 /**
809  * gnutls_sign_callback_get:
810  * @session: is a gnutls session
811  * @userdata: if non-%NULL, will be set to abstract callback pointer.
812  *
813  * Retrieve the callback function, and its userdata pointer.
814  *
815  * Returns: The function pointer set by gnutls_sign_callback_set(), or
816  *   if not set, %NULL.
817  *
818  * Deprecated: Use the PKCS 11 interfaces instead.
819  */
820 gnutls_sign_func
821 gnutls_sign_callback_get (gnutls_session_t session, void **userdata)
822 {
823   if (userdata)
824     *userdata = session->internals.sign_func_userdata;
825   return session->internals.sign_func;
826 }
827
828