1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2009-2011 Red Hat, Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * In addition, when the library is used with OpenSSL, a special
13 * exception applies. Refer to the LICENSE_EXCEPTION file for details.
16 #ifndef __G_TLS_CONNECTION_BASE_H__
17 #define __G_TLS_CONNECTION_BASE_H__
23 #define G_TYPE_TLS_CONNECTION_BASE (g_tls_connection_base_get_type ())
24 #define G_TLS_CONNECTION_BASE(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), G_TYPE_TLS_CONNECTION_BASE, GTlsConnectionBase))
25 #define G_TLS_CONNECTION_BASE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_TLS_CONNECTION_BASE, GTlsConnectionBaseClass))
26 #define G_IS_TLS_CONNECTION_BASE(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), G_TYPE_TLS_CONNECTION_BASE))
27 #define G_IS_TLS_CONNECTION_BASE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_TLS_CONNECTION_BASE))
28 #define G_TLS_CONNECTION_BASE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), G_TYPE_TLS_CONNECTION_BASE, GTlsConnectionBaseClass))
30 typedef struct _GTlsConnectionBasePrivate GTlsConnectionBasePrivate;
31 typedef struct _GTlsConnectionBaseClass GTlsConnectionBaseClass;
32 typedef struct _GTlsConnectionBase GTlsConnectionBase;
35 G_TLS_CONNECTION_BASE_OK,
36 G_TLS_CONNECTION_BASE_WOULD_BLOCK,
37 G_TLS_CONNECTION_BASE_TIMED_OUT,
38 G_TLS_CONNECTION_BASE_REHANDSHAKE,
39 G_TLS_CONNECTION_BASE_TRY_AGAIN,
40 G_TLS_CONNECTION_BASE_ERROR,
41 } GTlsConnectionBaseStatus;
43 struct _GTlsConnectionBaseClass
45 GTlsConnectionClass parent_class;
47 GTlsConnectionBaseStatus (*request_rehandshake) (GTlsConnectionBase *tls,
48 GCancellable *cancellable,
50 GTlsConnectionBaseStatus (*handshake) (GTlsConnectionBase *tls,
51 GCancellable *cancellable,
53 GTlsConnectionBaseStatus (*complete_handshake) (GTlsConnectionBase *tls,
56 void (*push_io) (GTlsConnectionBase *tls,
57 GIOCondition direction,
59 GCancellable *cancellable);
60 GTlsConnectionBaseStatus (*pop_io) (GTlsConnectionBase *tls,
61 GIOCondition direction,
65 GTlsConnectionBaseStatus (*read_fn) (GTlsConnectionBase *tls,
70 GCancellable *cancellable,
72 GTlsConnectionBaseStatus (*write_fn) (GTlsConnectionBase *tls,
77 GCancellable *cancellable,
80 GTlsConnectionBaseStatus (*close_fn) (GTlsConnectionBase *tls,
81 GCancellable *cancellable,
85 struct _GTlsConnectionBase
87 GTlsConnection parent_instance;
89 GIOStream *base_io_stream;
90 GPollableInputStream *base_istream;
91 GPollableOutputStream *base_ostream;
93 GTlsDatabase *database;
94 GTlsInteraction *interaction;
96 GTlsCertificate *certificate;
97 gboolean certificate_requested;
98 GError *certificate_error;
99 GTlsCertificate *peer_certificate;
100 GTlsCertificateFlags peer_certificate_errors;
102 gboolean require_close_notify;
103 GTlsRehandshakeMode rehandshake_mode;
105 /* need_handshake means the next claim_op() will get diverted into
106 * an implicit handshake (unless it's an OP_HANDSHAKE or OP_CLOSE*).
107 * need_finish_handshake means the next claim_op() will get diverted
108 * into finish_handshake() (unless it's an OP_CLOSE*).
110 * handshaking is TRUE as soon as a handshake thread is queued. For
111 * a sync handshake it becomes FALSE after finish_handshake()
112 * completes in the calling thread, but for an async implicit
113 * handshake, it becomes FALSE (and need_finish_handshake becomes
114 * TRUE) at the end of the handshaking thread (and then the next
115 * non-close op will call finish_handshake()). We can't just wait
116 * for handshake_thread_completed() to run, because it's possible
117 * that its main loop is being blocked by a synchronous op which is
118 * waiting for handshaking to become FALSE...
120 * started_handshake indicates that the current handshake attempt
121 * got at least as far as sending the first handshake packet (and so
122 * any error should be copied to handshake_error and returned on all
123 * future operations). ever_handshaked indicates that TLS has been
124 * successfully negotiated at some point.
126 gboolean need_handshake;
127 gboolean need_finish_handshake;
128 gboolean started_handshake;
129 gboolean handshaking;
130 gboolean ever_handshaked;
131 GTask *implicit_handshake;
132 GError *handshake_error;
133 GByteArray *app_data_buf;
135 /* read_closed means the read direction has closed; write_closed similarly.
136 * If (and only if) both are set, the entire GTlsConnection is closed. */
137 gboolean read_closing, read_closed;
138 gboolean write_closing, write_closed;
141 gboolean read_blocking;
143 GCancellable *read_cancellable;
146 gboolean write_blocking;
148 GCancellable *write_cancellable;
151 gboolean is_system_certdb;
152 gboolean database_is_unset;
154 GInputStream *tls_istream;
155 GOutputStream *tls_ostream;
158 GCancellable *waiting_for_op;
161 GType g_tls_connection_base_get_type (void) G_GNUC_CONST;
163 gboolean g_tls_connection_base_accept_peer_certificate (GTlsConnectionBase *tls,
164 GTlsCertificate *peer_certificate,
165 GTlsCertificateFlags peer_certificate_errors);
167 void g_tls_connection_base_set_peer_certificate (GTlsConnectionBase *tls,
168 GTlsCertificate *peer_certificate,
169 GTlsCertificateFlags peer_certificate_errors);
171 void g_tls_connection_base_push_io (GTlsConnectionBase *tls,
172 GIOCondition direction,
174 GCancellable *cancellable);
175 GTlsConnectionBaseStatus
176 g_tls_connection_base_pop_io (GTlsConnectionBase *tls,
177 GIOCondition direction,
181 gssize g_tls_connection_base_read (GTlsConnectionBase *tls,
185 GCancellable *cancellable,
187 gssize g_tls_connection_base_write (GTlsConnectionBase *tls,
191 GCancellable *cancellable,
194 gboolean g_tls_connection_base_check (GTlsConnectionBase *tls,
195 GIOCondition condition);
196 GSource *g_tls_connection_base_create_source (GTlsConnectionBase *tls,
197 GIOCondition condition,
198 GCancellable *cancellable);
201 G_TLS_DIRECTION_NONE = 0,
202 G_TLS_DIRECTION_READ = 1 << 0,
203 G_TLS_DIRECTION_WRITE = 1 << 1,
206 #define G_TLS_DIRECTION_BOTH (G_TLS_DIRECTION_READ | G_TLS_DIRECTION_WRITE)
208 gboolean g_tls_connection_base_close_internal (GIOStream *stream,
209 GTlsDirection direction,
210 GCancellable *cancellable,
215 #endif /* __G_TLS_CONNECTION_BASE_H___ */