GDBusMessage: fast-path encoding of fixed arrays
[platform/upstream/glib.git] / gio / gtlsinteraction.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 Collabora, Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Stef Walter <stefw@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include "gtlscertificate.h"
26 #include "gtlsconnection.h"
27 #include "gtlsinteraction.h"
28 #include "gtlspassword.h"
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
31 #include "gtask.h"
32 #include "gioenumtypes.h"
33 #include "glibintl.h"
34
35
36 /**
37  * SECTION:gtlsinteraction
38  * @short_description: Interaction with the user during TLS operations.
39  * @include: gio/gio.h
40  *
41  * #GTlsInteraction provides a mechanism for the TLS connection and database
42  * code to interact with the user. It can be used to ask the user for passwords.
43  *
44  * To use a #GTlsInteraction with a TLS connection use
45  * g_tls_connection_set_interaction().
46  *
47  * Callers should instantiate a derived class that implements the various
48  * interaction methods to show the required dialogs.
49  *
50  * Callers should use the 'invoke' functions like
51  * g_tls_interaction_invoke_ask_password() to run interaction methods. These
52  * functions make sure that the interaction is invoked in the main loop
53  * and not in the current thread, if the current thread is not running the
54  * main loop.
55  *
56  * Derived classes can choose to implement whichever interactions methods they'd
57  * like to support by overriding those virtual methods in their class
58  * initialization function. Any interactions not implemented will return
59  * %G_TLS_INTERACTION_UNHANDLED. If a derived class implements an async method,
60  * it must also implement the corresponding finish method.
61  */
62
63 /**
64  * GTlsInteraction:
65  *
66  * An object representing interaction that the TLS connection and database
67  * might have with the user.
68  *
69  * Since: 2.30
70  */
71
72 /**
73  * GTlsInteractionClass:
74  * @ask_password: ask for a password synchronously. If the implementation
75  *     returns %G_TLS_INTERACTION_HANDLED, then the password argument should
76  *     have been filled in by using g_tls_password_set_value() or a similar
77  *     function.
78  * @ask_password_async: ask for a password asynchronously.
79  * @ask_password_finish: complete operation to ask for a password asynchronously.
80  *     If the implementation returns %G_TLS_INTERACTION_HANDLED, then the
81  *     password argument of the async method should have been filled in by using
82  *     g_tls_password_set_value() or a similar function.
83  *
84  * The class for #GTlsInteraction. Derived classes implement the various
85  * virtual interaction methods to handle TLS interactions.
86  *
87  * Derived classes can choose to implement whichever interactions methods they'd
88  * like to support by overriding those virtual methods in their class
89  * initialization function. If a derived class implements an async method,
90  * it must also implement the corresponding finish method.
91  *
92  * The synchronous interaction methods should implement to display modal dialogs,
93  * and the asynchronous methods to display modeless dialogs.
94  *
95  * If the user cancels an interaction, then the result should be
96  * %G_TLS_INTERACTION_FAILED and the error should be set with a domain of
97  * %G_IO_ERROR and code of %G_IO_ERROR_CANCELLED.
98  *
99  * Since: 2.30
100  */
101
102 struct _GTlsInteractionPrivate {
103   GMainContext *context;
104 };
105
106 G_DEFINE_TYPE_WITH_PRIVATE (GTlsInteraction, g_tls_interaction, G_TYPE_OBJECT)
107
108 typedef struct {
109   GMutex mutex;
110
111   /* Input arguments */
112   GTlsInteraction *interaction;
113   GObject *argument;
114   GCancellable *cancellable;
115
116   /* Used when we're invoking async interactions */
117   GAsyncReadyCallback callback;
118   gpointer user_data;
119
120   /* Used when we expect results */
121   GTlsInteractionResult result;
122   GError *error;
123   gboolean complete;
124   GCond cond;
125 } InvokeClosure;
126
127 static void
128 invoke_closure_free (gpointer data)
129 {
130   InvokeClosure *closure = data;
131   g_assert (closure);
132   g_object_unref (closure->interaction);
133   g_clear_object (&closure->argument);
134   g_clear_object (&closure->cancellable);
135   g_cond_clear (&closure->cond);
136   g_mutex_clear (&closure->mutex);
137   g_clear_error (&closure->error);
138
139   /* Insurance that we've actually used these before freeing */
140   g_assert (closure->callback == NULL);
141   g_assert (closure->user_data == NULL);
142
143   g_free (closure);
144 }
145
146 static InvokeClosure *
147 invoke_closure_new (GTlsInteraction *interaction,
148                     GObject         *argument,
149                     GCancellable    *cancellable)
150 {
151   InvokeClosure *closure = g_new0 (InvokeClosure, 1);
152   closure->interaction = g_object_ref (interaction);
153   closure->argument = argument ? g_object_ref (argument) : NULL;
154   closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
155   g_mutex_init (&closure->mutex);
156   g_cond_init (&closure->cond);
157   closure->result = G_TLS_INTERACTION_UNHANDLED;
158   return closure;
159 }
160
161 static GTlsInteractionResult
162 invoke_closure_wait_and_free (InvokeClosure *closure,
163                               GError       **error)
164 {
165   GTlsInteractionResult result;
166
167   g_mutex_lock (&closure->mutex);
168
169   while (!closure->complete)
170     g_cond_wait (&closure->cond, &closure->mutex);
171
172   g_mutex_unlock (&closure->mutex);
173
174   if (closure->error)
175     {
176       g_propagate_error (error, closure->error);
177       closure->error = NULL;
178     }
179   result = closure->result;
180
181   invoke_closure_free (closure);
182   return result;
183 }
184
185 static GTlsInteractionResult
186 invoke_closure_complete_and_free (GTlsInteraction *interaction,
187                                   InvokeClosure *closure,
188                                   GError **error)
189 {
190   GTlsInteractionResult result;
191   gboolean complete;
192
193   /*
194    * Handle the case where we've been called from within the main context
195    * or in the case where the main context is not running. This approximates
196    * the behavior of a modal dialog.
197    */
198   if (g_main_context_acquire (interaction->priv->context))
199     {
200       for (;;)
201         {
202           g_mutex_lock (&closure->mutex);
203           complete = closure->complete;
204           g_mutex_unlock (&closure->mutex);
205           if (complete)
206             break;
207           g_main_context_iteration (interaction->priv->context, TRUE);
208         }
209
210       g_main_context_release (interaction->priv->context);
211
212       if (closure->error)
213         {
214           g_propagate_error (error, closure->error);
215           closure->error = NULL;
216         }
217
218       result = closure->result;
219       invoke_closure_free (closure);
220     }
221
222   /*
223    * Handle the case where we're in a different thread than the main
224    * context and a main loop is running.
225    */
226   else
227     {
228       result = invoke_closure_wait_and_free (closure, error);
229     }
230
231   return result;
232 }
233
234 static void
235 g_tls_interaction_init (GTlsInteraction *interaction)
236 {
237   interaction->priv = g_tls_interaction_get_instance_private (interaction);
238   interaction->priv->context = g_main_context_ref_thread_default ();
239 }
240
241 static void
242 g_tls_interaction_finalize (GObject *object)
243 {
244   GTlsInteraction *interaction = G_TLS_INTERACTION (object);
245
246   g_main_context_unref (interaction->priv->context);
247
248   G_OBJECT_CLASS (g_tls_interaction_parent_class)->finalize (object);
249 }
250
251 static void
252 g_tls_interaction_class_init (GTlsInteractionClass *klass)
253 {
254   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
255
256   gobject_class->finalize = g_tls_interaction_finalize;
257 }
258
259 static gboolean
260 on_invoke_ask_password_sync (gpointer user_data)
261 {
262   InvokeClosure *closure = user_data;
263   GTlsInteractionClass *klass;
264
265   g_mutex_lock (&closure->mutex);
266
267   klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
268   g_assert (klass->ask_password);
269
270   closure->result = klass->ask_password (closure->interaction,
271                                          G_TLS_PASSWORD (closure->argument),
272                                          closure->cancellable,
273                                          &closure->error);
274
275   closure->complete = TRUE;
276   g_cond_signal (&closure->cond);
277   g_mutex_unlock (&closure->mutex);
278
279   return FALSE; /* don't call again */
280 }
281
282 static void
283 on_ask_password_complete (GObject      *source,
284                           GAsyncResult *result,
285                           gpointer      user_data)
286 {
287   InvokeClosure *closure = user_data;
288   GTlsInteractionClass *klass;
289
290   g_mutex_lock (&closure->mutex);
291
292   klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
293   g_assert (klass->ask_password_finish);
294
295   closure->result = klass->ask_password_finish (closure->interaction,
296                                                 result,
297                                                 &closure->error);
298
299   closure->complete = TRUE;
300   g_cond_signal (&closure->cond);
301   g_mutex_unlock (&closure->mutex);
302 }
303
304 static gboolean
305 on_invoke_ask_password_async_as_sync (gpointer user_data)
306 {
307   InvokeClosure *closure = user_data;
308   GTlsInteractionClass *klass;
309
310   g_mutex_lock (&closure->mutex);
311
312   klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
313   g_assert (klass->ask_password_async);
314
315   klass->ask_password_async (closure->interaction,
316                              G_TLS_PASSWORD (closure->argument),
317                              closure->cancellable,
318                              on_ask_password_complete,
319                              closure);
320
321   /* Note that we've used these */
322   closure->callback = NULL;
323   closure->user_data = NULL;
324
325   g_mutex_unlock (&closure->mutex);
326
327   return FALSE; /* don't call again */
328 }
329
330 /**
331  * g_tls_interaction_invoke_ask_password:
332  * @interaction: a #GTlsInteraction object
333  * @password: a #GTlsPassword object
334  * @cancellable: an optional #GCancellable cancellation object
335  * @error: an optional location to place an error on failure
336  *
337  * Invoke the interaction to ask the user for a password. It invokes this
338  * interaction in the main loop, specifically the #GMainContext returned by
339  * g_main_context_get_thread_default() when the interaction is created. This
340  * is called by called by #GTlsConnection or #GTlsDatabase to ask the user
341  * for a password.
342  *
343  * Derived subclasses usually implement a password prompt, although they may
344  * also choose to provide a password from elsewhere. The @password value will
345  * be filled in and then @callback will be called. Alternatively the user may
346  * abort this password request, which will usually abort the TLS connection.
347  *
348  * The implementation can either be a synchronous (eg: modal dialog) or an
349  * asynchronous one (eg: modeless dialog). This function will take care of
350  * calling which ever one correctly.
351  *
352  * If the interaction is cancelled by the cancellation object, or by the
353  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
354  * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
355  * not support immediate cancellation.
356  *
357  * Returns: The status of the ask password interaction.
358  *
359  * Since: 2.30
360  */
361 GTlsInteractionResult
362 g_tls_interaction_invoke_ask_password (GTlsInteraction    *interaction,
363                                        GTlsPassword       *password,
364                                        GCancellable       *cancellable,
365                                        GError            **error)
366 {
367   GTlsInteractionResult result;
368   InvokeClosure *closure;
369   GTlsInteractionClass *klass;
370
371   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
372   g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
373   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
374
375   closure = invoke_closure_new (interaction, G_OBJECT (password), cancellable);
376
377   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
378   if (klass->ask_password)
379     {
380       g_main_context_invoke (interaction->priv->context,
381                              on_invoke_ask_password_sync, closure);
382       result = invoke_closure_wait_and_free (closure, error);
383     }
384   else if (klass->ask_password_async)
385     {
386       g_return_val_if_fail (klass->ask_password_finish, G_TLS_INTERACTION_UNHANDLED);
387       g_main_context_invoke (interaction->priv->context,
388                              on_invoke_ask_password_async_as_sync, closure);
389
390       result = invoke_closure_complete_and_free (interaction, closure, error);
391     }
392   else
393     {
394       result = G_TLS_INTERACTION_UNHANDLED;
395       invoke_closure_free (closure);
396     }
397
398   return result;
399 }
400
401 /**
402  * g_tls_interaction_ask_password:
403  * @interaction: a #GTlsInteraction object
404  * @password: a #GTlsPassword object
405  * @cancellable: an optional #GCancellable cancellation object
406  * @error: an optional location to place an error on failure
407  *
408  * Run synchronous interaction to ask the user for a password. In general,
409  * g_tls_interaction_invoke_ask_password() should be used instead of this
410  * function.
411  *
412  * Derived subclasses usually implement a password prompt, although they may
413  * also choose to provide a password from elsewhere. The @password value will
414  * be filled in and then @callback will be called. Alternatively the user may
415  * abort this password request, which will usually abort the TLS connection.
416  *
417  * If the interaction is cancelled by the cancellation object, or by the
418  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
419  * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
420  * not support immediate cancellation.
421  *
422  * Returns: The status of the ask password interaction.
423  *
424  * Since: 2.30
425  */
426 GTlsInteractionResult
427 g_tls_interaction_ask_password (GTlsInteraction    *interaction,
428                                 GTlsPassword       *password,
429                                 GCancellable       *cancellable,
430                                 GError            **error)
431 {
432   GTlsInteractionClass *klass;
433
434   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
435   g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED);
436   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
437
438   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
439   if (klass->ask_password)
440     return (klass->ask_password) (interaction, password, cancellable, error);
441   else
442     return G_TLS_INTERACTION_UNHANDLED;
443 }
444
445 /**
446  * g_tls_interaction_ask_password_async:
447  * @interaction: a #GTlsInteraction object
448  * @password: a #GTlsPassword object
449  * @cancellable: an optional #GCancellable cancellation object
450  * @callback: (allow-none): will be called when the interaction completes
451  * @user_data: (allow-none): data to pass to the @callback
452  *
453  * Run asynchronous interaction to ask the user for a password. In general,
454  * g_tls_interaction_invoke_ask_password() should be used instead of this
455  * function.
456  *
457  * Derived subclasses usually implement a password prompt, although they may
458  * also choose to provide a password from elsewhere. The @password value will
459  * be filled in and then @callback will be called. Alternatively the user may
460  * abort this password request, which will usually abort the TLS connection.
461  *
462  * If the interaction is cancelled by the cancellation object, or by the
463  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
464  * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
465  * not support immediate cancellation.
466  *
467  * Certain implementations may not support immediate cancellation.
468  *
469  * Since: 2.30
470  */
471 void
472 g_tls_interaction_ask_password_async (GTlsInteraction    *interaction,
473                                       GTlsPassword       *password,
474                                       GCancellable       *cancellable,
475                                       GAsyncReadyCallback callback,
476                                       gpointer            user_data)
477 {
478   GTlsInteractionClass *klass;
479   GTask *task;
480
481   g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
482   g_return_if_fail (G_IS_TLS_PASSWORD (password));
483   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
484
485   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
486   if (klass->ask_password_async)
487     {
488       g_return_if_fail (klass->ask_password_finish);
489       (klass->ask_password_async) (interaction, password, cancellable,
490                                    callback, user_data);
491     }
492   else
493     {
494       task = g_task_new (interaction, cancellable, callback, user_data);
495       g_task_set_source_tag (task, g_tls_interaction_ask_password_async);
496       g_task_return_int (task, G_TLS_INTERACTION_UNHANDLED);
497       g_object_unref (task);
498     }
499 }
500
501 /**
502  * g_tls_interaction_ask_password_finish:
503  * @interaction: a #GTlsInteraction object
504  * @result: the result passed to the callback
505  * @error: an optional location to place an error on failure
506  *
507  * Complete an ask password user interaction request. This should be once
508  * the g_tls_interaction_ask_password_async() completion callback is called.
509  *
510  * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsPassword passed
511  * to g_tls_interaction_ask_password() will have its password filled in.
512  *
513  * If the interaction is cancelled by the cancellation object, or by the
514  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
515  * contains a %G_IO_ERROR_CANCELLED error code.
516  *
517  * Returns: The status of the ask password interaction.
518  *
519  * Since: 2.30
520  */
521 GTlsInteractionResult
522 g_tls_interaction_ask_password_finish (GTlsInteraction    *interaction,
523                                        GAsyncResult       *result,
524                                        GError            **error)
525 {
526   GTlsInteractionClass *klass;
527
528   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
529   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
530
531   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
532   if (klass->ask_password_finish)
533     {
534       g_return_val_if_fail (klass->ask_password_async != NULL, G_TLS_INTERACTION_UNHANDLED);
535
536       return (klass->ask_password_finish) (interaction, result, error);
537     }
538   else
539     {
540       g_return_val_if_fail (g_async_result_is_tagged (result, g_tls_interaction_ask_password_async), G_TLS_INTERACTION_UNHANDLED);
541
542       return g_task_propagate_int (G_TASK (result), error);
543     }
544 }
545
546 static gboolean
547 on_invoke_request_certificate_sync (gpointer user_data)
548 {
549   InvokeClosure *closure = user_data;
550   GTlsInteractionClass *klass;
551
552   g_mutex_lock (&closure->mutex);
553
554   klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
555   g_assert (klass->request_certificate != NULL);
556
557   closure->result = klass->request_certificate (closure->interaction,
558                                                 G_TLS_CONNECTION (closure->argument),
559                                                 0,
560                                                 closure->cancellable,
561                                                 &closure->error);
562
563   closure->complete = TRUE;
564   g_cond_signal (&closure->cond);
565   g_mutex_unlock (&closure->mutex);
566
567   return FALSE; /* don't call again */
568 }
569
570 static void
571 on_request_certificate_complete (GObject      *source,
572                                  GAsyncResult *result,
573                                  gpointer      user_data)
574 {
575   InvokeClosure *closure = user_data;
576   GTlsInteractionClass *klass;
577
578   g_mutex_lock (&closure->mutex);
579
580   klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
581   g_assert (klass->request_certificate_finish != NULL);
582
583   closure->result = klass->request_certificate_finish (closure->interaction,
584                                                        result, &closure->error);
585
586   closure->complete = TRUE;
587   g_cond_signal (&closure->cond);
588   g_mutex_unlock (&closure->mutex);
589 }
590
591 static gboolean
592 on_invoke_request_certificate_async_as_sync (gpointer user_data)
593 {
594   InvokeClosure *closure = user_data;
595   GTlsInteractionClass *klass;
596
597   g_mutex_lock (&closure->mutex);
598
599   klass = G_TLS_INTERACTION_GET_CLASS (closure->interaction);
600   g_assert (klass->request_certificate_async);
601
602   klass->request_certificate_async (closure->interaction,
603                                     G_TLS_CONNECTION (closure->argument), 0,
604                                     closure->cancellable,
605                                     on_request_certificate_complete,
606                                     closure);
607
608   /* Note that we've used these */
609   closure->callback = NULL;
610   closure->user_data = NULL;
611
612   g_mutex_unlock (&closure->mutex);
613
614   return FALSE; /* don't call again */
615 }
616
617 /**
618  * g_tls_interaction_invoke_request_certificate:
619  * @interaction: a #GTlsInteraction object
620  * @connection: a #GTlsConnection object
621  * @flags: flags providing more information about the request
622  * @cancellable: an optional #GCancellable cancellation object
623  * @error: an optional location to place an error on failure
624  *
625  * Invoke the interaction to ask the user to choose a certificate to
626  * use with the connection. It invokes this interaction in the main
627  * loop, specifically the #GMainContext returned by
628  * g_main_context_get_thread_default() when the interaction is
629  * created. This is called by called by #GTlsConnection when the peer
630  * requests a certificate during the handshake.
631  *
632  * Derived subclasses usually implement a certificate selector,
633  * although they may also choose to provide a certificate from
634  * elsewhere. Alternatively the user may abort this certificate
635  * request, which may or may not abort the TLS connection.
636  *
637  * The implementation can either be a synchronous (eg: modal dialog) or an
638  * asynchronous one (eg: modeless dialog). This function will take care of
639  * calling which ever one correctly.
640  *
641  * If the interaction is cancelled by the cancellation object, or by the
642  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
643  * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
644  * not support immediate cancellation.
645  *
646  * Returns: The status of the certificate request interaction.
647  *
648  * Since: 2.40
649  */
650 GTlsInteractionResult
651 g_tls_interaction_invoke_request_certificate (GTlsInteraction    *interaction,
652                                               GTlsConnection               *connection,
653                                               GTlsCertificateRequestFlags   flags,
654                                               GCancellable       *cancellable,
655                                               GError            **error)
656 {
657   GTlsInteractionResult result;
658   InvokeClosure *closure;
659   GTlsInteractionClass *klass;
660
661   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
662   g_return_val_if_fail (G_IS_TLS_CONNECTION (connection), G_TLS_INTERACTION_UNHANDLED);
663   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
664
665   closure = invoke_closure_new (interaction, G_OBJECT (connection), cancellable);
666
667   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
668   if (klass->request_certificate)
669     {
670       g_main_context_invoke (interaction->priv->context,
671                              on_invoke_request_certificate_sync, closure);
672       result = invoke_closure_wait_and_free (closure, error);
673     }
674   else if (klass->request_certificate_async)
675     {
676       g_return_val_if_fail (klass->request_certificate_finish, G_TLS_INTERACTION_UNHANDLED);
677       g_main_context_invoke (interaction->priv->context,
678                              on_invoke_request_certificate_async_as_sync, closure);
679
680       result = invoke_closure_complete_and_free (interaction, closure, error);
681     }
682   else
683     {
684       result = G_TLS_INTERACTION_UNHANDLED;
685       invoke_closure_free (closure);
686     }
687
688   return result;
689 }
690
691 /**
692  * g_tls_interaction_request_certificate:
693  * @interaction: a #GTlsInteraction object
694  * @connection: a #GTlsConnection object
695  * @flags: flags providing more information about the request
696  * @cancellable: an optional #GCancellable cancellation object
697  * @error: an optional location to place an error on failure
698  *
699  * Run synchronous interaction to ask the user to choose a certificate to use
700  * with the connection. In general, g_tls_interaction_invoke_request_certificate()
701  * should be used instead of this function.
702  *
703  * Derived subclasses usually implement a certificate selector, although they may
704  * also choose to provide a certificate from elsewhere. Alternatively the user may
705  * abort this certificate request, which will usually abort the TLS connection.
706  *
707  * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsConnection
708  * passed to g_tls_interaction_request_certificate() will have had its
709  * #GTlsConnection:certificate filled in.
710  *
711  * If the interaction is cancelled by the cancellation object, or by the
712  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
713  * contains a %G_IO_ERROR_CANCELLED error code. Certain implementations may
714  * not support immediate cancellation.
715  *
716  * Returns: The status of the request certificate interaction.
717  *
718  * Since: 2.40
719  */
720 GTlsInteractionResult
721 g_tls_interaction_request_certificate (GTlsInteraction              *interaction,
722                                        GTlsConnection               *connection,
723                                        GTlsCertificateRequestFlags   flags,
724                                        GCancellable                 *cancellable,
725                                        GError                      **error)
726 {
727   GTlsInteractionClass *klass;
728
729   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
730   g_return_val_if_fail (G_IS_TLS_CONNECTION (connection), G_TLS_INTERACTION_UNHANDLED);
731   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED);
732
733   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
734   if (klass->request_certificate)
735     return (klass->request_certificate) (interaction, connection, flags, cancellable, error);
736   else
737     return G_TLS_INTERACTION_UNHANDLED;
738 }
739
740 /**
741  * g_tls_interaction_request_certificate_async:
742  * @interaction: a #GTlsInteraction object
743  * @connection: a #GTlsConnection object
744  * @flags: flags providing more information about the request
745  * @cancellable: an optional #GCancellable cancellation object
746  * @callback: (allow-none): will be called when the interaction completes
747  * @user_data: (allow-none): data to pass to the @callback
748  *
749  * Run asynchronous interaction to ask the user for a certificate to use with
750  * the connection. In general, g_tls_interaction_invoke_request_certificate() should
751  * be used instead of this function.
752  *
753  * Derived subclasses usually implement a certificate selector, although they may
754  * also choose to provide a certificate from elsewhere. @callback will be called
755  * when the operation completes. Alternatively the user may abort this certificate
756  * request, which will usually abort the TLS connection.
757  *
758  * Since: 2.40
759  */
760 void
761 g_tls_interaction_request_certificate_async (GTlsInteraction              *interaction,
762                                              GTlsConnection               *connection,
763                                              GTlsCertificateRequestFlags   flags,
764                                              GCancellable                 *cancellable,
765                                              GAsyncReadyCallback           callback,
766                                              gpointer                      user_data)
767 {
768   GTlsInteractionClass *klass;
769   GTask *task;
770
771   g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
772   g_return_if_fail (G_IS_TLS_CONNECTION (connection));
773   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
774
775   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
776   if (klass->request_certificate_async)
777     {
778       g_return_if_fail (klass->request_certificate_finish);
779       (klass->request_certificate_async) (interaction, connection, flags,
780                                           cancellable, callback, user_data);
781     }
782   else
783     {
784       task = g_task_new (interaction, cancellable, callback, user_data);
785       g_task_set_source_tag (task, g_tls_interaction_request_certificate_async);
786       g_task_return_int (task, G_TLS_INTERACTION_UNHANDLED);
787       g_object_unref (task);
788     }
789 }
790
791 /**
792  * g_tls_interaction_request_certificate_finish:
793  * @interaction: a #GTlsInteraction object
794  * @result: the result passed to the callback
795  * @error: an optional location to place an error on failure
796  *
797  * Complete an request certificate user interaction request. This should be once
798  * the g_tls_interaction_request_certificate_async() completion callback is called.
799  *
800  * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsConnection
801  * passed to g_tls_interaction_request_certificate_async() will have had its
802  * #GTlsConnection:certificate filled in.
803  *
804  * If the interaction is cancelled by the cancellation object, or by the
805  * user then %G_TLS_INTERACTION_FAILED will be returned with an error that
806  * contains a %G_IO_ERROR_CANCELLED error code.
807  *
808  * Returns: The status of the request certificate interaction.
809  *
810  * Since: 2.40
811  */
812 GTlsInteractionResult
813 g_tls_interaction_request_certificate_finish (GTlsInteraction    *interaction,
814                                               GAsyncResult       *result,
815                                               GError            **error)
816 {
817   GTlsInteractionClass *klass;
818
819   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
820   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
821
822   klass = G_TLS_INTERACTION_GET_CLASS (interaction);
823   if (klass->request_certificate_finish)
824     {
825       g_return_val_if_fail (klass->request_certificate_async != NULL, G_TLS_INTERACTION_UNHANDLED);
826
827       return (klass->request_certificate_finish) (interaction, result, error);
828     }
829   else
830     {
831       g_return_val_if_fail (g_async_result_is_tagged (result, g_tls_interaction_request_certificate_async), G_TLS_INTERACTION_UNHANDLED);
832
833       return g_task_propagate_int (G_TASK (result), error);
834     }
835 }