Frameworks/libpurple.framework/Versions/0.6.2/Headers/sslconn.h
author Zachary West <zacw@adium.im>
Fri Aug 21 13:25:11 2009 -0700 (2009-08-21)
changeset 2592 e8d15275025e
parent 2571 Frameworks/libpurple.framework/Versions/0.6.0/Headers/sslconn.h@75fb8ee8f2e6
permissions -rw-r--r--
im.pidgin.adium.1-4 at 267c6165e02e34318a1823960bd04c0639952f73
Evan@653
     1
/**
Evan@653
     2
 * @file sslconn.h SSL API
Evan@653
     3
 * @ingroup core
Evan@653
     4
 */
Evan@653
     5
Evan@653
     6
/* purple
Evan@653
     7
 *
Evan@653
     8
 * Purple is the legal property of its developers, whose names are too numerous
Evan@653
     9
 * to list here.  Please refer to the COPYRIGHT file distributed with this
Evan@653
    10
 * source distribution.
Evan@653
    11
 *
Evan@653
    12
 * This program is free software; you can redistribute it and/or modify
Evan@653
    13
 * it under the terms of the GNU General Public License as published by
Evan@653
    14
 * the Free Software Foundation; either version 2 of the License, or
Evan@653
    15
 * (at your option) any later version.
Evan@653
    16
 *
Evan@653
    17
 * This program is distributed in the hope that it will be useful,
Evan@653
    18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Evan@653
    19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Evan@653
    20
 * GNU General Public License for more details.
Evan@653
    21
 *
Evan@653
    22
 * You should have received a copy of the GNU General Public License
Evan@653
    23
 * along with this program; if not, write to the Free Software
Evan@653
    24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
Evan@653
    25
 */
Evan@653
    26
#ifndef _PURPLE_SSLCONN_H_
Evan@653
    27
#define _PURPLE_SSLCONN_H_
Evan@653
    28
Evan@653
    29
/** Possible SSL errors. */
Evan@653
    30
typedef enum
Evan@653
    31
{
Evan@653
    32
	PURPLE_SSL_HANDSHAKE_FAILED = 1,
Evan@653
    33
	PURPLE_SSL_CONNECT_FAILED = 2,
Evan@653
    34
	PURPLE_SSL_CERTIFICATE_INVALID = 3
Evan@653
    35
} PurpleSslErrorType;
Evan@653
    36
Evan@653
    37
#include "certificate.h"
Evan@653
    38
#include "proxy.h"
Evan@653
    39
Evan@653
    40
#define PURPLE_SSL_DEFAULT_PORT 443
Evan@653
    41
Evan@1427
    42
/** @copydoc _PurpleSslConnection */
Evan@653
    43
typedef struct _PurpleSslConnection PurpleSslConnection;
Evan@653
    44
Evan@653
    45
typedef void (*PurpleSslInputFunction)(gpointer, PurpleSslConnection *,
Evan@653
    46
									 PurpleInputCondition);
Evan@653
    47
typedef void (*PurpleSslErrorFunction)(PurpleSslConnection *, PurpleSslErrorType,
Evan@653
    48
									 gpointer);
Evan@653
    49
Evan@653
    50
struct _PurpleSslConnection
Evan@653
    51
{
Evan@653
    52
	/** Hostname to which the SSL connection will be made */
Evan@653
    53
	char *host;
Evan@653
    54
	/** Port to connect to */
Evan@653
    55
	int port;
Evan@653
    56
	/** Data to pass to PurpleSslConnection::connect_cb() */
Evan@653
    57
	void *connect_cb_data;
Evan@653
    58
	/** Callback triggered once the SSL handshake is complete */
Evan@653
    59
	PurpleSslInputFunction connect_cb;
Evan@653
    60
	/** Callback triggered if there is an error during connection */
Evan@653
    61
	PurpleSslErrorFunction error_cb;
Evan@653
    62
	/** Data passed to PurpleSslConnection::recv_cb() */
Evan@653
    63
	void *recv_cb_data;
Evan@653
    64
	/** User-defined callback executed when the SSL connection receives data */
Evan@653
    65
	PurpleSslInputFunction recv_cb;
Evan@653
    66
Evan@653
    67
	/** File descriptor used to refer to the socket */
Evan@653
    68
	int fd;
Evan@1427
    69
	/** Glib event source ID; used to refer to the received data callback
Evan@653
    70
	 * in the glib eventloop */
Evan@653
    71
	guint inpa;
Evan@653
    72
	/** Data related to the underlying TCP connection */
Evan@653
    73
	PurpleProxyConnectData *connect_data;
Evan@653
    74
Evan@653
    75
	/** Internal connection data managed by the SSL backend (GnuTLS/LibNSS/whatever) */
Evan@653
    76
	void *private_data;
Evan@653
    77
Evan@653
    78
	/** Verifier to use in authenticating the peer */
Evan@653
    79
	PurpleCertificateVerifier *verifier;
Evan@653
    80
};
Evan@653
    81
Evan@653
    82
/**
Evan@653
    83
 * SSL implementation operations structure.
Evan@653
    84
 *
Evan@653
    85
 * Every SSL implementation must provide all of these and register it via purple_ssl_set_ops()
Evan@653
    86
 * These should not be called directly! Instead, use the purple_ssl_* functions.
Evan@653
    87
 */
Evan@653
    88
typedef struct
Evan@653
    89
{
Evan@653
    90
	/** Initializes the SSL system provided.
Evan@653
    91
	 *  @return @a TRUE if initialization succeeded
Evan@653
    92
	 *  @see purple_ssl_init
Evan@653
    93
	 */
Evan@653
    94
	gboolean (*init)(void);
Evan@653
    95
	/** Unloads the SSL system. Inverse of PurpleSslOps::init.
Evan@653
    96
	 *  @see purple_ssl_uninit
Evan@653
    97
	 */
Evan@653
    98
	void (*uninit)(void);
Evan@653
    99
	/** Sets up the SSL connection for a #PurpleSslConnection once
Evan@653
   100
	 *  the TCP connection has been established
Evan@653
   101
	 *  @see purple_ssl_connect
Evan@653
   102
	 */
Evan@653
   103
	void (*connectfunc)(PurpleSslConnection *gsc);
Evan@653
   104
	/** Destroys the internal data of the SSL connection provided.
Evan@653
   105
	 *  Freeing gsc itself is left to purple_ssl_close()
Evan@653
   106
	 *  @see purple_ssl_close
Evan@653
   107
	 */
Evan@653
   108
	void (*close)(PurpleSslConnection *gsc);
Evan@653
   109
	/** Reads data from a connection (like POSIX read())
Evan@653
   110
	 * @param gsc   Connection context
Evan@653
   111
	 * @param data  Pointer to buffer to drop data into
Evan@653
   112
	 * @param len   Maximum number of bytes to read
Evan@653
   113
	 * @return      Number of bytes actually written into @a data (which may be
Evan@653
   114
	 *              less than @a len), or <0 on error
Evan@653
   115
	 * @see purple_ssl_read
Evan@653
   116
	*/
Evan@653
   117
	size_t (*read)(PurpleSslConnection *gsc, void *data, size_t len);
Evan@653
   118
	/** Writes data to a connection (like POSIX send())
Evan@653
   119
	* @param gsc    Connection context
Evan@653
   120
	* @param data   Data buffer to send data from
Evan@653
   121
	* @param len    Number of bytes to send from buffer
Evan@653
   122
	* @return       The number of bytes written to @a data (may be less than
Evan@653
   123
	*               @a len) or <0 on error
Evan@653
   124
	* @see purple_ssl_write
Evan@653
   125
	*/
Evan@653
   126
	size_t (*write)(PurpleSslConnection *gsc, const void *data, size_t len);
Evan@653
   127
	/** Obtains the certificate chain provided by the peer
Evan@653
   128
	 *
Evan@653
   129
	 * @param gsc   Connection context
Evan@653
   130
	 * @return      A newly allocated list containing the certificates
Evan@653
   131
	 *              the peer provided.
Evan@653
   132
	 * @see PurpleCertificate
Evan@653
   133
	 * @todo        Decide whether the ordering of certificates in this
Evan@653
   134
	 *              list can be guaranteed.
Evan@653
   135
	 */
Evan@653
   136
	GList * (* get_peer_certificates)(PurpleSslConnection * gsc);
Evan@1427
   137
Evan@653
   138
	void (*_purple_reserved2)(void);
Evan@653
   139
	void (*_purple_reserved3)(void);
Evan@653
   140
	void (*_purple_reserved4)(void);
Evan@653
   141
} PurpleSslOps;
Evan@653
   142
Evan@653
   143
#ifdef __cplusplus
Evan@653
   144
extern "C" {
Evan@653
   145
#endif
Evan@653
   146
Evan@653
   147
/**************************************************************************/
Evan@653
   148
/** @name SSL API                                                         */
Evan@653
   149
/**************************************************************************/
Evan@653
   150
/*@{*/
Evan@653
   151
Evan@653
   152
/**
Evan@653
   153
 * Returns whether or not SSL is currently supported.
Evan@653
   154
 *
Evan@653
   155
 * @return @a TRUE if SSL is supported, or @a FALSE otherwise.
Evan@653
   156
 */
Evan@653
   157
gboolean purple_ssl_is_supported(void);
Evan@653
   158
Evan@653
   159
/**
Evan@653
   160
 * Returns a human-readable string for an SSL error.
Evan@653
   161
 *
Evan@653
   162
 * @param error      Error code
Evan@653
   163
 * @return Human-readable error explanation
Evan@653
   164
 */
Evan@653
   165
const gchar * purple_ssl_strerror(PurpleSslErrorType error);
Evan@653
   166
Evan@653
   167
/**
Evan@653
   168
 * Makes a SSL connection to the specified host and port.  The caller
Evan@653
   169
 * should keep track of the returned value and use it to cancel the
Evan@653
   170
 * connection, if needed.
Evan@653
   171
 *
Evan@653
   172
 * @param account    The account making the connection.
Evan@653
   173
 * @param host       The destination host.
Evan@653
   174
 * @param port       The destination port.
Evan@653
   175
 * @param func       The SSL input handler function.
Evan@653
   176
 * @param error_func The SSL error handler function.  This function
Evan@653
   177
 *                   should <strong>NOT</strong> call purple_ssl_close().  In
Evan@653
   178
 *                   the event of an error the #PurpleSslConnection will be
Evan@653
   179
 *                   destroyed for you.
Evan@653
   180
 * @param data       User-defined data.
Evan@653
   181
 *
Evan@653
   182
 * @return The SSL connection handle.
Evan@653
   183
 */
Evan@653
   184
PurpleSslConnection *purple_ssl_connect(PurpleAccount *account, const char *host,
Evan@653
   185
									int port, PurpleSslInputFunction func,
Evan@653
   186
									PurpleSslErrorFunction error_func,
Evan@653
   187
									void *data);
Evan@653
   188
Evan@2571
   189
/**
Evan@2571
   190
 * Makes a SSL connection to the specified host and port, using the separate
Evan@2571
   191
 * name to verify with the certificate.  The caller should keep track of the
Evan@2571
   192
 * returned value and use it to cancel the connection, if needed.
Evan@2571
   193
 *
Evan@2571
   194
 * @param account    The account making the connection.
Evan@2571
   195
 * @param host       The destination host.
Evan@2571
   196
 * @param port       The destination port.
Evan@2571
   197
 * @param func       The SSL input handler function.
Evan@2571
   198
 * @param error_func The SSL error handler function.  This function
Evan@2571
   199
 *                   should <strong>NOT</strong> call purple_ssl_close().  In
Evan@2571
   200
 *                   the event of an error the #PurpleSslConnection will be
Evan@2571
   201
 *                   destroyed for you.
Evan@2571
   202
 * @param ssl_host   The hostname of the other peer (to verify the CN)
Evan@2571
   203
 * @param data       User-defined data.
Evan@2571
   204
 *
Evan@2571
   205
 * @return The SSL connection handle.
Evan@2571
   206
 * @since 2.6.0
Evan@2571
   207
 */
Evan@2571
   208
PurpleSslConnection *purple_ssl_connect_with_ssl_cn(PurpleAccount *account, const char *host,
Evan@2571
   209
									int port, PurpleSslInputFunction func,
Evan@2571
   210
									PurpleSslErrorFunction error_func,
Evan@2571
   211
									const char *ssl_host,
Evan@2571
   212
									void *data);
Evan@2571
   213
Evan@653
   214
#if !(defined PURPLE_DISABLE_DEPRECATED) || (defined _PURPLE_SSLCONN_C_)
Evan@653
   215
/**
Evan@653
   216
 * Makes a SSL connection using an already open file descriptor.
Evan@653
   217
 *
Evan@653
   218
 * @deprecated Use purple_ssl_connect_with_host_fd() instead.
Evan@653
   219
 *
Evan@653
   220
 * @param account    The account making the connection.
Evan@653
   221
 * @param fd         The file descriptor.
Evan@653
   222
 * @param func       The SSL input handler function.
Evan@653
   223
 * @param error_func The SSL error handler function.
Evan@653
   224
 * @param data       User-defined data.
Evan@653
   225
 *
Evan@653
   226
 * @return The SSL connection handle.
Evan@653
   227
 */
Evan@653
   228
PurpleSslConnection *purple_ssl_connect_fd(PurpleAccount *account, int fd,
Evan@653
   229
									   PurpleSslInputFunction func,
Evan@653
   230
									   PurpleSslErrorFunction error_func,
Evan@653
   231
 									   void *data);
Evan@653
   232
#endif
Evan@653
   233
Evan@653
   234
/**
Evan@653
   235
 * Makes a SSL connection using an already open file descriptor.
Evan@653
   236
 *
Evan@653
   237
 * @param account    The account making the connection.
Evan@653
   238
 * @param fd         The file descriptor.
Evan@653
   239
 * @param func       The SSL input handler function.
Evan@653
   240
 * @param error_func The SSL error handler function.
Evan@653
   241
 * @param host       The hostname of the other peer (to verify the CN)
Evan@653
   242
 * @param data       User-defined data.
Evan@653
   243
 *
Evan@653
   244
 * @return The SSL connection handle.
Evan@653
   245
 *
Evan@653
   246
 * @since 2.2.0
Evan@653
   247
 */
Evan@653
   248
PurpleSslConnection *purple_ssl_connect_with_host_fd(PurpleAccount *account, int fd,
Evan@653
   249
                                           PurpleSslInputFunction func,
Evan@653
   250
                                           PurpleSslErrorFunction error_func,
Evan@653
   251
                                           const char *host,
Evan@653
   252
                                           void *data);
Evan@653
   253
Evan@653
   254
/**
Evan@653
   255
 * Adds an input watcher for the specified SSL connection.
Evan@653
   256
 * Once the SSL handshake is complete, use this to watch for actual data across it.
Evan@653
   257
 *
Evan@653
   258
 * @param gsc   The SSL connection handle.
Evan@653
   259
 * @param func  The callback function.
Evan@653
   260
 * @param data  User-defined data.
Evan@653
   261
 */
Evan@653
   262
void purple_ssl_input_add(PurpleSslConnection *gsc, PurpleSslInputFunction func,
Evan@653
   263
						void *data);
Evan@653
   264
Evan@653
   265
/**
Evan@653
   266
 * Closes a SSL connection.
Evan@653
   267
 *
Evan@653
   268
 * @param gsc The SSL connection to close.
Evan@653
   269
 */
Evan@653
   270
void purple_ssl_close(PurpleSslConnection *gsc);
Evan@653
   271
Evan@653
   272
/**
Evan@653
   273
 * Reads data from an SSL connection.
Evan@653
   274
 *
Evan@653
   275
 * @param gsc    The SSL connection handle.
Evan@653
   276
 * @param buffer The destination buffer.
Evan@653
   277
 * @param len    The maximum number of bytes to read.
Evan@653
   278
 *
Evan@653
   279
 * @return The number of bytes read.
Evan@653
   280
 */
Evan@653
   281
size_t purple_ssl_read(PurpleSslConnection *gsc, void *buffer, size_t len);
Evan@653
   282
Evan@653
   283
/**
Evan@653
   284
 * Writes data to an SSL connection.
Evan@653
   285
 *
Evan@653
   286
 * @param gsc    The SSL connection handle.
Evan@653
   287
 * @param buffer The buffer to write.
Evan@653
   288
 * @param len    The length of the data to write.
Evan@653
   289
 *
Evan@653
   290
 * @return The number of bytes written.
Evan@653
   291
 */
Evan@653
   292
size_t purple_ssl_write(PurpleSslConnection *gsc, const void *buffer, size_t len);
Evan@653
   293
Evan@653
   294
/**
Evan@653
   295
 * Obtains the peer's presented certificates
Evan@653
   296
 *
Evan@653
   297
 * @param gsc    The SSL connection handle
Evan@653
   298
 *
Evan@653
   299
 * @return The peer certificate chain, in the order of certificate, issuer,
Evan@653
   300
 *         issuer's issuer, etc. @a NULL if no certificates have been provided,
Evan@653
   301
 *
Evan@653
   302
 * @since 2.2.0
Evan@653
   303
 */
Evan@653
   304
GList * purple_ssl_get_peer_certificates(PurpleSslConnection *gsc);
Evan@653
   305
Evan@653
   306
/*@}*/
Evan@653
   307
Evan@653
   308
/**************************************************************************/
Evan@653
   309
/** @name Subsystem API                                                   */
Evan@653
   310
/**************************************************************************/
Evan@653
   311
/*@{*/
Evan@653
   312
Evan@653
   313
/**
Evan@653
   314
 * Sets the current SSL operations structure.
Evan@653
   315
 *
Evan@653
   316
 * @param ops The SSL operations structure to assign.
Evan@653
   317
 */
Evan@653
   318
void purple_ssl_set_ops(PurpleSslOps *ops);
Evan@653
   319
Evan@653
   320
/**
Evan@653
   321
 * Returns the current SSL operations structure.
Evan@653
   322
 *
Evan@653
   323
 * @return The SSL operations structure.
Evan@653
   324
 */
Evan@653
   325
PurpleSslOps *purple_ssl_get_ops(void);
Evan@653
   326
Evan@653
   327
/**
Evan@653
   328
 * Initializes the SSL subsystem.
Evan@653
   329
 */
Evan@653
   330
void purple_ssl_init(void);
Evan@653
   331
Evan@653
   332
/**
Evan@653
   333
 * Uninitializes the SSL subsystem.
Evan@653
   334
 */
Evan@653
   335
void purple_ssl_uninit(void);
Evan@653
   336
Evan@653
   337
/*@}*/
Evan@653
   338
Evan@653
   339
#ifdef __cplusplus
Evan@653
   340
}
Evan@653
   341
#endif
Evan@653
   342
Evan@653
   343
#endif /* _PURPLE_SSLCONN_H_ */