loudmouth/src/lm-ssl-generic.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
       
     2 /*
       
     3  * Copyright (C) 2003-2006 Imendio AB
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public License as
       
     7  * published by the Free Software Foundation; either version 2 of the
       
     8  * License, or (at your option) any later version.
       
     9  *
       
    10  * This program 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 Public
       
    16  * License along with this program; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 #include <config.h>
       
    22 #include "lm-debug.h"
       
    23 #include "lm-ssl.h"
       
    24 #include "lm-ssl-base.h"
       
    25 #include "lm-ssl-internals.h"
       
    26 
       
    27 LmSSLResponse  
       
    28 _lm_ssl_func_always_continue (LmSSL       *ssl,
       
    29 			      LmSSLStatus  status,
       
    30 			      gpointer     user_data)
       
    31 {
       
    32      UNUSED_FORMAL_PARAM(ssl);
       
    33      UNUSED_FORMAL_PARAM(status);
       
    34      UNUSED_FORMAL_PARAM(user_data);
       
    35 	return LM_SSL_RESPONSE_CONTINUE;;
       
    36 }
       
    37 
       
    38 /* Define the SSL functions as noops if we compile without support */
       
    39 #ifndef HAVE_SSL
       
    40 
       
    41 LmSSL *
       
    42 _lm_ssl_new (const gchar    *expected_fingerprint,
       
    43 	     LmSSLFunction   ssl_function,
       
    44 	     gpointer        user_data,
       
    45 	     GDestroyNotify  notify)
       
    46 {
       
    47 	return NULL;
       
    48 }
       
    49 
       
    50 void
       
    51 _lm_ssl_initialize (LmSSL *ssl)
       
    52 {
       
    53 	/* NOOP */
       
    54 }
       
    55 
       
    56 gboolean
       
    57 _lm_ssl_begin (LmSSL        *ssl,
       
    58 	       gint          fd,
       
    59 	       const gchar  *server,
       
    60 	       GError      **error)
       
    61 {
       
    62 	return TRUE;
       
    63 }
       
    64 
       
    65 GIOStatus
       
    66 _lm_ssl_read (LmSSL *ssl,
       
    67 	      gchar *buf,
       
    68 	      gint   len,
       
    69 	      gsize  *bytes_read)
       
    70 {
       
    71 	/* NOOP */
       
    72 	*bytes_read = 0;
       
    73 
       
    74 	return G_IO_STATUS_EOF;
       
    75 }
       
    76 
       
    77 gboolean 
       
    78 _lm_ssl_send (LmSSL *ssl, const gchar *str, gint len)
       
    79 {
       
    80 	/* NOOP */
       
    81 	return TRUE;
       
    82 }
       
    83 void 
       
    84 _lm_ssl_close (LmSSL *ssl)
       
    85 {
       
    86 	/* NOOP */
       
    87 }
       
    88 
       
    89 void
       
    90 _lm_ssl_free (LmSSL *ssl)
       
    91 {
       
    92 	/* NOOP */
       
    93 }
       
    94 
       
    95 #endif /* HAVE_SSL */
       
    96 
       
    97 
       
    98 
       
    99 /**
       
   100  * lm_ssl_new:
       
   101  * @expected_fingerprint: The expected fingerprint. @ssl_function will be called if there is a mismatch. %NULL if you are not interested in this check.
       
   102  * @ssl_function: Callback called to inform the user of a problem during setting up the SSL connection and how to proceed. If %NULL is passed the default function that always continues will be used.
       
   103  * @user_data: Data sent with the callback.
       
   104  * @notify: Function to free @user_dataa when the connection is finished. %NULL if @user_data should not be freed.
       
   105  *
       
   106  * Creates a new SSL struct, call #lm_connection_set_ssl to use it. 
       
   107  *
       
   108  * Return value: A new #LmSSL struct.
       
   109  **/
       
   110 EXPORT_C LmSSL *
       
   111 lm_ssl_new (const gchar    *expected_fingerprint,
       
   112 	    LmSSLFunction   ssl_function,
       
   113 	    gpointer        user_data,
       
   114 	    GDestroyNotify  notify)
       
   115 {
       
   116 	/* The implementation of this function will be different depending
       
   117 	 * on which implementation is used 
       
   118 	 */
       
   119 	return _lm_ssl_new (expected_fingerprint,
       
   120 			    ssl_function, user_data, notify);
       
   121 }
       
   122 
       
   123 /**
       
   124  * lm_ssl_is_supported:
       
   125  *
       
   126  * Checks whether Loudmouth supports SSL or not.
       
   127  *
       
   128  * Return value: #TRUE if this installation of Loudmouth supports SSL, otherwise returns #FALSE.
       
   129  **/
       
   130 EXPORT_C gboolean
       
   131 lm_ssl_is_supported (void)
       
   132 {
       
   133 #ifdef HAVE_SSL
       
   134 	return TRUE;
       
   135 #else
       
   136 	return FALSE;
       
   137 #endif
       
   138 }
       
   139 
       
   140 
       
   141 /**
       
   142  * lm_ssl_get_fingerprint: 
       
   143  * @ssl: an #LmSSL
       
   144  *
       
   145  * Returns the MD5 fingerprint of the remote server's certificate.
       
   146  * 
       
   147  * Return value: A 16-byte array representing the fingerprint or %NULL if unknown.
       
   148  **/
       
   149 EXPORT_C const gchar *
       
   150 lm_ssl_get_fingerprint (LmSSL *ssl)
       
   151 {
       
   152 	g_return_val_if_fail (ssl != NULL, NULL);
       
   153 	
       
   154 	return LM_SSL_BASE(ssl)->fingerprint;
       
   155 }
       
   156 
       
   157 /**
       
   158  * lm_ssl_ref:
       
   159  * @ssl: an #LmSSL
       
   160  * 
       
   161  * Adds a reference to @ssl.
       
   162  * 
       
   163  * Return value: the ssl
       
   164  **/
       
   165 EXPORT_C LmSSL *
       
   166 lm_ssl_ref (LmSSL *ssl)
       
   167 {
       
   168 	g_return_val_if_fail (ssl != NULL, NULL);
       
   169 
       
   170 	LM_SSL_BASE(ssl)->ref_count++;
       
   171 
       
   172 	return ssl;
       
   173 }
       
   174 
       
   175 /**
       
   176  * lm_ssl_use_starttls:
       
   177  * @ssl: an #LmSSL
       
   178  *
       
   179  * Set whether STARTTLS should be used.
       
   180  **/
       
   181 EXPORT_C void
       
   182 lm_ssl_use_starttls (LmSSL *ssl,
       
   183 		     gboolean use_starttls,
       
   184 		     gboolean require_starttls)
       
   185 {
       
   186 	LmSSLBase *base;
       
   187 
       
   188 	base = LM_SSL_BASE (ssl);
       
   189 	base->use_starttls = use_starttls;
       
   190 	base->require_starttls = require_starttls;
       
   191 }
       
   192 
       
   193 /**
       
   194  * lm_ssl_get_use_starttls:
       
   195  *
       
   196  * Return value: TRUE is @ssl is configured to use STARTTLS.
       
   197  **/
       
   198 gboolean
       
   199 lm_ssl_get_use_starttls (LmSSL *ssl)
       
   200 {
       
   201 	LmSSLBase *base;
       
   202 
       
   203 	base = LM_SSL_BASE (ssl);
       
   204 	lm_verbose ("lm_ssl_get_use_starttls use_starttls[%d]\n", base->use_starttls);
       
   205 	return base->use_starttls;
       
   206 }
       
   207 
       
   208 /**
       
   209  * lm_ssl_get_require_starttls:
       
   210  *
       
   211  * Return value: TRUE if @ssl requires that STARTTLS succeed.
       
   212  **/
       
   213 gboolean
       
   214 lm_ssl_get_require_starttls (LmSSL *ssl)
       
   215 {
       
   216 	LmSSLBase *base;
       
   217 
       
   218 	base = LM_SSL_BASE (ssl);
       
   219 	return base->require_starttls;
       
   220 }
       
   221 
       
   222 /**
       
   223  * lm_ssl_unref
       
   224  * @ssl: an #LmSSL
       
   225  * 
       
   226  * Removes a reference from @ssl. When no more references are present
       
   227  * @ssl is freed.
       
   228  **/
       
   229 EXPORT_C void 
       
   230 lm_ssl_unref (LmSSL *ssl)
       
   231 {
       
   232 	LmSSLBase *base;
       
   233 	
       
   234 	g_return_if_fail (ssl != NULL);
       
   235         
       
   236 	base = LM_SSL_BASE (ssl);
       
   237 
       
   238 	base->ref_count --;
       
   239         
       
   240         if (base->ref_count == 0) {
       
   241 		if (base->data_notify) {
       
   242 			(* base->data_notify) (base->func_data);
       
   243 		}
       
   244              
       
   245 		_lm_ssl_free (ssl);
       
   246         }
       
   247 }
       
   248 
       
   249