/* * Main libfaim header. Must be included in client for prototypes/macros. * * "come on, i turned a chick lesbian; i think this is the hackish equivalent" * -- Josh Meyer * */ #ifndef __AIM_H__ #define __AIM_H__ #include #include #include #include #include #include #include #include #include #include "bitlbee.h" /* XXX adjust these based on autoconf-detected platform */ typedef guint32 aim_snacid_t; typedef guint16 flap_seqnum_t; /* Portability stuff (DMP) */ #if defined(mach) && defined(__APPLE__) #define gethostbyname(x) gethostbyname2(x, AF_INET) #endif /* * Current Maximum Length for Screen Names (not including NULL) * * Currently only names up to 16 characters can be registered * however it is aparently legal for them to be larger. */ #define MAXSNLEN 32 /* * Current Maximum Length for Instant Messages * * This was found basically by experiment, but not wholly * accurate experiment. It should not be regarded * as completely correct. But its a decent approximation. * * Note that although we can send this much, its impossible * for WinAIM clients (up through the latest (4.0.1957)) to * send any more than 1kb. Amaze all your windows friends * with utterly oversized instant messages! * * XXX: the real limit is the total SNAC size at 8192. Fix this. * */ #define MAXMSGLEN 7987 /* * Maximum size of a Buddy Icon. */ #define MAXICONLEN 7168 #define AIM_ICONIDENT "AVT1picture.id" /* * Current Maximum Length for Chat Room Messages * * This is actually defined by the protocol to be * dynamic, but I have yet to see due cause to * define it dynamically here. Maybe later. * */ #define MAXCHATMSGLEN 512 /* * Standard size of an AIM authorization cookie */ #define AIM_COOKIELEN 0x100 #define AIM_MD5_STRING "AOL Instant Messenger (SM)" /* * Default Authorizer server name and TCP port for the OSCAR farm. * * You shouldn't need to change this unless you're writing * your own server. * * Note that only one server is needed to start the whole * AIM process. The later server addresses come from * the authorizer service. * * This is only here for convenience. Its still up to * the client to connect to it. * */ #define AIM_DEFAULT_LOGIN_SERVER "login.oscar.aol.com" #define AIM_LOGIN_PORT 5190 /* * Size of the SNAC caching hash. * * Default: 16 * */ #define AIM_SNAC_HASH_SIZE 16 /* * Client info. Filled in by the client and passed in to * aim_send_login(). The information ends up getting passed to OSCAR * through the initial login command. * */ struct client_info_s { const char *clientstring; guint16 clientid; int major; int minor; int point; int build; const char *country; /* two-letter abbrev */ const char *lang; /* two-letter abbrev */ }; #define AIM_CLIENTINFO_KNOWNGOOD_3_5_1670 { \ "AOL Instant Messenger (SM), version 3.5.1670/WIN32", \ 0x0004, \ 0x0003, \ 0x0005, \ 0x0000, \ 0x0686, \ "us", \ "en", \ } #define AIM_CLIENTINFO_KNOWNGOOD_4_1_2010 { \ "AOL Instant Messenger (SM), version 4.1.2010/WIN32", \ 0x0004, \ 0x0004, \ 0x0001, \ 0x0000, \ 0x07da, \ "us", \ "en", \ } /* * I would make 4.1.2010 the default, but they seem to have found * an alternate way of breaking that one. * * 3.5.1670 should work fine, however, you will be subjected to the * memory test, which may require you to have a WinAIM binary laying * around. (see login.c::memrequest()) */ #define AIM_CLIENTINFO_KNOWNGOOD AIM_CLIENTINFO_KNOWNGOOD_3_5_1670 #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif /* * These could be arbitrary, but its easier to use the actual AIM values */ #define AIM_CONN_TYPE_AUTH 0x0007 #define AIM_CONN_TYPE_ADS 0x0005 #define AIM_CONN_TYPE_BOS 0x0002 #define AIM_CONN_TYPE_CHAT 0x000e #define AIM_CONN_TYPE_CHATNAV 0x000d /* * Status values returned from aim_conn_new(). ORed together. */ #define AIM_CONN_STATUS_READY 0x0001 #define AIM_CONN_STATUS_INTERNALERR 0x0002 #define AIM_CONN_STATUS_RESOLVERR 0x0040 #define AIM_CONN_STATUS_CONNERR 0x0080 #define AIM_CONN_STATUS_INPROGRESS 0x0100 #define AIM_FRAMETYPE_FLAP 0x0000 /* * message type flags */ #define AIM_MTYPE_PLAIN 0x01 #define AIM_MTYPE_CHAT 0x02 #define AIM_MTYPE_FILEREQ 0x03 #define AIM_MTYPE_URL 0x04 #define AIM_MTYPE_AUTHREQ 0x06 #define AIM_MTYPE_AUTHDENY 0x07 #define AIM_MTYPE_AUTHOK 0x08 #define AIM_MTYPE_SERVER 0x09 #define AIM_MTYPE_ADDED 0x0C #define AIM_MTYPE_WWP 0x0D #define AIM_MTYPE_EEXPRESS 0x0E #define AIM_MTYPE_CONTACTS 0x13 #define AIM_MTYPE_PLUGIN 0x1A #define AIM_MTYPE_AUTOAWAY 0xE8 #define AIM_MTYPE_AUTOBUSY 0xE9 #define AIM_MTYPE_AUTONA 0xEA #define AIM_MTYPE_AUTODND 0xEB #define AIM_MTYPE_AUTOFFC 0xEC typedef struct aim_conn_s { int fd; guint16 type; guint16 subtype; flap_seqnum_t seqnum; guint32 status; void *priv; /* misc data the client may want to store */ void *internal; /* internal conn-specific libfaim data */ time_t lastactivity; /* time of last transmit */ int forcedlatency; void *handlerlist; void *sessv; /* pointer to parent session */ void *inside; /* only accessible from inside libfaim */ struct aim_conn_s *next; } aim_conn_t; /* * Byte Stream type. Sort of. * * Use of this type serves a couple purposes: * - Buffer/buflen pairs are passed all around everywhere. This turns * that into one value, as well as abstracting it slightly. * - Through the abstraction, it is possible to enable bounds checking * for robustness at the cost of performance. But a clean failure on * weird packets is much better than a segfault. * - I like having variables named "bs". * * Don't touch the insides of this struct. Or I'll have to kill you. * */ typedef struct aim_bstream_s { guint8 *data; guint32 len; guint32 offset; } aim_bstream_t; typedef struct aim_frame_s { guint8 hdrtype; /* defines which piece of the union to use */ union { struct { guint8 type; flap_seqnum_t seqnum; } flap; } hdr; aim_bstream_t data; /* payload stream */ guint8 handled; /* 0 = new, !0 = been handled */ guint8 nofree; /* 0 = free data on purge, 1 = only unlink */ aim_conn_t *conn; /* the connection it came in on... */ struct aim_frame_s *next; } aim_frame_t; typedef struct aim_msgcookie_s { unsigned char cookie[8]; int type; void *data; time_t addtime; struct aim_msgcookie_s *next; } aim_msgcookie_t; /* * AIM Session: The main client-data interface. * */ typedef struct aim_session_s { /* ---- Client Accessible ------------------------ */ /* Our screen name. */ char sn[MAXSNLEN+1]; /* * Pointer to anything the client wants to * explicitly associate with this session. * * This is for use in the callbacks mainly. In any * callback, you can access this with sess->aux_data. * */ void *aux_data; /* ---- Internal Use Only ------------------------ */ /* Server-stored information (ssi) */ struct { int received_data; guint16 revision; struct aim_ssi_item *items; time_t timestamp; int waiting_for_ack; aim_frame_t *holding_queue; } ssi; /* Connection information */ aim_conn_t *connlist; /* * Transmit/receive queues. * * These are only used when you don't use your own lowlevel * I/O. I don't suggest that you use libfaim's internal I/O. * Its really bad and the API/event model is quirky at best. * */ aim_frame_t *queue_outgoing; aim_frame_t *queue_incoming; /* * Tx Enqueuing function. * * This is how you override the transmit direction of libfaim's * internal I/O. This function will be called whenever it needs * to send something. * */ int (*tx_enqueue)(struct aim_session_s *, aim_frame_t *); /* * Outstanding snac handling * * XXX: Should these be per-connection? -mid */ void *snac_hash[AIM_SNAC_HASH_SIZE]; aim_snacid_t snacid_next; struct aim_icq_info *icq_info; struct aim_oft_info *oft_info; struct aim_authresp_info *authinfo; struct aim_emailinfo *emailinfo; struct { struct aim_userinfo_s *userinfo; struct userinfo_node *torequest; struct userinfo_node *requested; int waiting_for_response; } locate; guint32 flags; /* AIM_SESS_FLAGS_ */ aim_msgcookie_t *msgcookies; void *modlistv; guint8 aim_icq_state; /* ICQ representation of away state */ } aim_session_t; /* Values for sess->flags */ #define AIM_SESS_FLAGS_SNACLOGIN 0x00000001 #define A
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2004 Wilmer van der Gaast and others                *
  \********************************************************************/

/* SSL module - GnuTLS version                                          */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#include "proxy.h"
#include "ssl_client.h"
#include "sock.h"

static gboolean initialized = FALSE;

struct scd
{
	SslInputFunction func;
	gpointer data;
	int fd;
	gboolean established;
	
	SSL *ssl;
	SSL_CTX *ssl_ctx;
};

static void ssl_connected( gpointer data, gint source, GaimInputCondition cond );



void *ssl_connect( char *host, int port, SslInputFunction func, gpointer data )
{
	struct scd *conn = g_new0( struct scd, 1 );
	SSL_METHOD *meth;
	
	conn->fd = proxy_connect( host, port, ssl_connected, conn );
	conn->func = func;
	conn->data = data;
	
	if( conn->fd < 0 )
	{
		g_free( conn );
		return( NULL );
	}
	
	if( !initialized )
	{
		initialized = TRUE;
		SSLeay_add_ssl_algorithms();
	}
	
	meth = TLSv1_client_method();
	conn->ssl_ctx = SSL_CTX_new( meth );
	if( conn->ssl_ctx == NULL )
	{
		conn->fd = -1;
		return( NULL );
	}
	
	conn->ssl = SSL_new( conn->ssl_ctx );
	if( conn->ssl == NULL )
	{
		conn->fd = -1;
		return( NULL );
	}
	
	return( conn );
}

static void ssl_connected( gpointer data, gint source, GaimInputCondition cond )
{
	struct scd *conn = data;
	
	if( source == -1 )
		goto ssl_connected_failure;
	
	SSL_set_fd( conn->ssl, conn->fd );
	
	if( SSL_connect( conn->ssl ) < 0 )
		goto ssl_connected_failure;
	
	conn->established = TRUE;
	conn->func( conn->data, conn, cond );
	return;
	
ssl_connected_failure:
	conn->func( conn->data, NULL, cond );
	
	if( conn->ssl