loudmouth/lm-ssl-openssl.c
author Jayson Vantuyl <jvantuyl@engineyard.com>
Wed, 25 Mar 2009 11:28:18 -0700
changeset 596 9096d2549372
parent 565 87a57a62be8c
child 607 e2bd14357c9a
permissions -rw-r--r--
Removed Dangerous Instructions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
516
4dd3aa6b83e5 Change the tab width to 4 steps in the emacs headers
Mikael Hallendal <micke@imendio.com>
parents: 515
diff changeset
     1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     2
/*
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     3
 * Copyright (C) 2006 Imendio AB
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
     4
 * Copyright (C) 2006 Nokia Corporation. All rights reserved.
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     5
 *
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     7
 * modify it under the terms of the GNU Lesser General Public License as
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     8
 * published by the Free Software Foundation; either version 2 of the
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     9
 * License, or (at your option) any later version.
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    10
 *
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    14
 * Lesser General Public License for more details.
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    15
 *
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    16
 * You should have received a copy of the GNU Lesser General Public
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    17
 * License along with this program; if not, write to the
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    19
 * Boston, MA 02111-1307, USA.
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    20
 */
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    21
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    22
#include <config.h>
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    23
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    24
#include <stdio.h>
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    25
#include <string.h>
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    26
#include <unistd.h>
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    27
#include <glib.h>
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    28
213
27567fb5d37b Use g_warning and g_log for warnings and debug printouts in OpenSSL backend.
Mikael Hallendal <micke@imendio.com>
parents: 209
diff changeset
    29
#include "lm-debug.h"
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    30
#include "lm-error.h"
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    31
#include "lm-ssl-base.h"
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    32
#include "lm-ssl-internals.h"
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    33
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    34
#ifdef HAVE_OPENSSL
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    35
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    36
#include <openssl/ssl.h>
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    37
#include <openssl/err.h>
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    38
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    39
#define LM_SSL_CN_MAX       63
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    40
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    41
struct _LmSSL {
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    42
    LmSSLBase base;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    43
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    44
    SSL_METHOD *ssl_method;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    45
    SSL_CTX *ssl_ctx;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    46
    SSL *ssl;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    47
    /*BIO *bio;*/
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    48
};
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    49
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    50
int ssl_verify_cb (int preverify_ok, X509_STORE_CTX *x509_ctx);
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    51
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    52
static gboolean ssl_verify_certificate (LmSSL *ssl, const gchar *server);
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    53
static GIOStatus ssl_io_status_from_return (LmSSL *ssl, gint error);
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    54
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    55
/*static char _ssl_error_code[11];*/
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    56
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    57
static void
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    58
ssl_print_state (LmSSL *ssl, const char *func, int val)
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    59
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    60
    unsigned long errid;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    61
    const char *errmsg;
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
    62
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    63
    switch (SSL_get_error(ssl->ssl, val)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    64
    case SSL_ERROR_NONE:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    65
        g_warning ("%s(): %i / SSL_ERROR_NONE",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    66
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    67
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    68
    case SSL_ERROR_ZERO_RETURN:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    69
        g_warning ("%s(): %i / SSL_ERROR_ZERO_RETURN",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    70
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    71
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    72
    case SSL_ERROR_WANT_READ:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    73
        g_warning ("%s(): %i / SSL_ERROR_WANT_READ",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    74
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    75
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    76
    case SSL_ERROR_WANT_WRITE:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    77
        g_warning ("%s(): %i / SSL_ERROR_WANT_WRITE",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    78
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    79
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    80
    case SSL_ERROR_WANT_X509_LOOKUP:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    81
        g_warning ("%s(): %i / SSL_ERROR_WANT_X509_LOOKUP",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    82
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    83
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    84
    case SSL_ERROR_SYSCALL:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    85
        g_warning ("%s(): %i / SSL_ERROR_SYSCALL",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    86
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    87
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    88
    case SSL_ERROR_SSL:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    89
        g_warning ("%s(): %i / SSL_ERROR_SSL",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    90
                   func, val);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    91
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    92
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    93
    do {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    94
        errid = ERR_get_error();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    95
        if (errid) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    96
            errmsg = ERR_error_string(errid, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    97
            g_warning ("\t%s", errmsg);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    98
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    99
    } while (errid != 0);
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   100
}
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   101
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   102
/*static const char *
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   103
  ssl_get_x509_err (long verify_res)
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   104
  {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   105
  sprintf(_ssl_error_code, "%ld", verify_res);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   106
  return _ssl_error_code;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   107
  }*/
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   108
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   109
    
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   110
int
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   111
ssl_verify_cb (int preverify_ok, X509_STORE_CTX *x509_ctx)
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   112
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   113
    /* As this callback doesn't get auxiliary pointer parameter we
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   114
     * cannot really use this. However, we can retrieve results later. */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   115
    return 1;
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   116
}
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   117
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   118
static gboolean
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   119
ssl_verify_certificate (LmSSL *ssl, const gchar *server)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   120
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   121
    gboolean retval = TRUE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   122
    LmSSLBase *base;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   123
    long verify_res;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   124
    unsigned int digest_len;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   125
    X509 *srv_crt;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   126
    gchar *cn;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   127
    X509_NAME *crt_subj;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   128
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   129
    base = LM_SSL_BASE(ssl);
169
10bcf8e0e443 2006-09-06 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 140
diff changeset
   130
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   131
    g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   132
           "%s: Cipher: %s/%s/%i\n",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   133
           __FILE__,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   134
           SSL_get_cipher_version(ssl->ssl),
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   135
           SSL_get_cipher_name(ssl->ssl),
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   136
           SSL_get_cipher_bits(ssl->ssl, NULL));
213
27567fb5d37b Use g_warning and g_log for warnings and debug printouts in OpenSSL backend.
Mikael Hallendal <micke@imendio.com>
parents: 209
diff changeset
   137
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   138
    verify_res = SSL_get_verify_result(ssl->ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   139
    srv_crt = SSL_get_peer_certificate(ssl->ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   140
    if (base->expected_fingerprint != NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   141
        X509_digest(srv_crt, EVP_md5(), (guchar *) base->fingerprint,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   142
                    &digest_len);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   143
        if (memcmp(base->expected_fingerprint, base->fingerprint,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   144
                   digest_len) != 0) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   145
            if (base->func(ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   146
                           LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   147
                           base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   148
                return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   149
            }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   150
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   151
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   152
    g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   153
           "%s: SSL_get_verify_result() = %ld\n",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   154
           __FILE__,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   155
           verify_res);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   156
    switch (verify_res) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   157
    case X509_V_OK:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   158
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   159
    case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   160
        /* special case for self signed certificates? */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   161
    case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   162
    case X509_V_ERR_UNABLE_TO_GET_CRL:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   163
    case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   164
        if (base->func(ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   165
                       LM_SSL_STATUS_NO_CERT_FOUND,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   166
                       base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   167
            retval = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   168
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   169
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   170
    case X509_V_ERR_INVALID_CA:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   171
    case X509_V_ERR_CERT_UNTRUSTED:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   172
    case X509_V_ERR_CERT_REVOKED:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   173
        if (base->func(ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   174
                       LM_SSL_STATUS_UNTRUSTED_CERT,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   175
                       base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   176
            retval = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   177
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   178
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   179
    case X509_V_ERR_CERT_NOT_YET_VALID:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   180
    case X509_V_ERR_CRL_NOT_YET_VALID:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   181
        if (base->func(ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   182
                       LM_SSL_STATUS_CERT_NOT_ACTIVATED,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   183
                       base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   184
            retval = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   185
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   186
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   187
    case X509_V_ERR_CERT_HAS_EXPIRED:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   188
    case X509_V_ERR_CRL_HAS_EXPIRED:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   189
        if (base->func(ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   190
                       LM_SSL_STATUS_CERT_EXPIRED,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   191
                       base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   192
            retval = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   193
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   194
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   195
    default:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   196
        if (base->func(ssl, LM_SSL_STATUS_GENERIC_ERROR,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   197
                       base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   198
            retval = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   199
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   200
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   201
    /*if (retval == FALSE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   202
      g_set_error (error, LM_ERROR, LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   203
      ssl_get_x509_err(verify_res), NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   204
      }*/
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   205
    crt_subj = X509_get_subject_name(srv_crt);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   206
    cn = (gchar *) g_malloc0(LM_SSL_CN_MAX + 1);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   207
    
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   208
    if (X509_NAME_get_text_by_NID(crt_subj, NID_commonName, cn, LM_SSL_CN_MAX) > 0) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   209
        gchar *domain = cn;
274
c32a7011e435 Adding StartTLS support.
Senko Rasic <senko@phyrexia.(none)>
parents: 269
diff changeset
   210
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   211
        g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   212
               "%s: server = '%s', cn = '%s'\n",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   213
               __FILE__, server, cn);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   214
        
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   215
        if ((cn[0] == '*') && (cn[1] == '.')) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   216
            domain = strstr (cn, server);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   217
        }
274
c32a7011e435 Adding StartTLS support.
Senko Rasic <senko@phyrexia.(none)>
parents: 269
diff changeset
   218
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   219
        if ((domain == NULL) || (strncmp (server, domain, LM_SSL_CN_MAX) != 0)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   220
            if (base->func (ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   221
                            LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   222
                            base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   223
                retval = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   224
            }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   225
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   226
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   227
        g_warning ("X509_NAME_get_text_by_NID() failed");
274
c32a7011e435 Adding StartTLS support.
Senko Rasic <senko@phyrexia.(none)>
parents: 269
diff changeset
   228
    }
213
27567fb5d37b Use g_warning and g_log for warnings and debug printouts in OpenSSL backend.
Mikael Hallendal <micke@imendio.com>
parents: 209
diff changeset
   229
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   230
    g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL, 
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   231
           "%s:\n\tIssuer: %s\n\tSubject: %s\n\tFor: %s\n",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   232
           __FILE__,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   233
           X509_NAME_oneline(X509_get_issuer_name(srv_crt), NULL, 0),
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   234
           X509_NAME_oneline(X509_get_subject_name(srv_crt), NULL, 0),
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   235
           cn);
213
27567fb5d37b Use g_warning and g_log for warnings and debug printouts in OpenSSL backend.
Mikael Hallendal <micke@imendio.com>
parents: 209
diff changeset
   236
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   237
    g_free(cn);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   238
    
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   239
    return retval;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   240
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   241
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   242
static GIOStatus
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   243
ssl_io_status_from_return (LmSSL *ssl, gint ret)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   244
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   245
    gint      error;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   246
    GIOStatus status;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   247
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   248
    if (ret > 0) return G_IO_STATUS_NORMAL;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   249
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   250
    error = SSL_get_error(ssl->ssl, ret);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   251
    switch (error) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   252
    case SSL_ERROR_WANT_READ:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   253
    case SSL_ERROR_WANT_WRITE:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   254
        status = G_IO_STATUS_AGAIN;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   255
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   256
    case SSL_ERROR_ZERO_RETURN:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   257
        status = G_IO_STATUS_EOF;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   258
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   259
    default:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   260
        status = G_IO_STATUS_ERROR;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   261
    }
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   262
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   263
    return status;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   264
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   265
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   266
/* From lm-ssl-protected.h */
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   267
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   268
LmSSL *
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   269
_lm_ssl_new (const gchar    *expected_fingerprint,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   270
             LmSSLFunction   ssl_function,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   271
             gpointer        user_data,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   272
             GDestroyNotify  notify)
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   273
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   274
    LmSSL *ssl;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   275
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   276
    ssl = g_new0 (LmSSL, 1);
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   277
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   278
    _lm_ssl_base_init ((LmSSLBase *) ssl,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   279
                       expected_fingerprint,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   280
                       ssl_function, user_data, notify);
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   281
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   282
    return ssl;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   283
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   284
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   285
void
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   286
_lm_ssl_initialize (LmSSL *ssl) 
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   287
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   288
    static gboolean initialized = FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   289
    /*const char *cert_file = NULL;*/
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   290
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   291
    if (!initialized) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   292
        SSL_library_init();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   293
        /* FIXME: Is this needed when we are not in debug? */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   294
        SSL_load_error_strings();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   295
        initialized = TRUE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   296
    }
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   297
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   298
    ssl->ssl_method = TLSv1_client_method();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   299
    if (ssl->ssl_method == NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   300
        g_warning ("TLSv1_client_method() == NULL");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   301
        abort();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   302
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   303
    ssl->ssl_ctx = SSL_CTX_new(ssl->ssl_method);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   304
    if (ssl->ssl_ctx == NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   305
        g_warning ("SSL_CTX_new() == NULL");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   306
        abort();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   307
    }
565
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   308
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   309
    /* Set the NO_TICKET option on the context to allow for talk to Google Talk 
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   310
     * which apparently seems to be having a problem handling empty session 
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   311
     * tickets due to a bug in Java.
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   312
     *
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   313
     * See http://twistedmatrix.com/trac/ticket/3463 and
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   314
     * Loudmouth [#28].
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   315
     */
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   316
    SSL_CTX_set_options (ssl->ssl_ctx, SSL_OP_NO_TICKET);
87a57a62be8c Use option NO_TICKET for OpenSSL [#28]
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   317
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   318
    /*if (access("/etc/ssl/cert.pem", R_OK) == 0)
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   319
      cert_file = "/etc/ssl/cert.pem";
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   320
      if (!SSL_CTX_load_verify_locations(ssl->ssl_ctx,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   321
      cert_file, "/etc/ssl/certs")) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   322
      g_warning("SSL_CTX_load_verify_locations() failed");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   323
      }*/
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   324
    SSL_CTX_set_default_verify_paths (ssl->ssl_ctx);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   325
    SSL_CTX_set_verify (ssl->ssl_ctx, SSL_VERIFY_PEER, ssl_verify_cb);
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   326
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   327
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   328
gboolean
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   329
_lm_ssl_begin (LmSSL *ssl, gint fd, const gchar *server, GError **error)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   330
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   331
    gint ssl_ret;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   332
    GIOStatus status;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   333
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   334
    if (!ssl->ssl_ctx) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   335
        g_set_error (error,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   336
                     LM_ERROR, LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   337
                     "No SSL Context for OpenSSL");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   338
        return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   339
    }
309
329900413057 Check to see that the OpenSSL session is setup correctly. Fixes LM-88.
Senko Rasic <senko.rasic@collabora.co.uk>
parents: 301
diff changeset
   340
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   341
    ssl->ssl = SSL_new(ssl->ssl_ctx);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   342
    if (ssl->ssl == NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   343
        g_warning ("SSL_new() == NULL");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   344
        g_set_error(error, LM_ERROR, LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   345
                    "SSL_new()");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   346
        return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   347
    }
213
27567fb5d37b Use g_warning and g_log for warnings and debug printouts in OpenSSL backend.
Mikael Hallendal <micke@imendio.com>
parents: 209
diff changeset
   348
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   349
    if (!SSL_set_fd (ssl->ssl, fd)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   350
        g_warning ("SSL_set_fd() failed");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   351
        g_set_error(error, LM_ERROR, LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   352
                    "SSL_set_fd()");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   353
        return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   354
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   355
    /*ssl->bio = BIO_new_socket (fd, BIO_NOCLOSE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   356
      if (ssl->bio == NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   357
      g_warning("BIO_new_socket() failed");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   358
      g_set_error(error, LM_ERROR, LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   359
      "BIO_new_socket()");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   360
      return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   361
      }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   362
      SSL_set_bio(ssl->ssl, ssl->bio, ssl->bio);*/
209
1fd5e3004816 Added SSL changes from Senko on SSL branch
Mikael Hallendal <micke@imendio.com>
parents: 169
diff changeset
   363
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   364
    do {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   365
        ssl_ret = SSL_connect(ssl->ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   366
        if (ssl_ret <= 0) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   367
            status = ssl_io_status_from_return(ssl, ssl_ret);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   368
            if (status != G_IO_STATUS_AGAIN) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   369
                ssl_print_state(ssl, "SSL_connect",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   370
                                ssl_ret);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   371
                g_set_error(error, LM_ERROR,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   372
                            LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   373
                            "SSL_connect()");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   374
                return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   375
            }
139
2d0dcbb757b3 2006-04-18 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 138
diff changeset
   376
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   377
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   378
    } while (ssl_ret <= 0);
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   379
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   380
    if (!ssl_verify_certificate (ssl, server)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   381
        g_set_error (error, LM_ERROR, LM_ERROR_CONNECTION_OPEN,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   382
                     "*** SSL certificate verification failed");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   383
        return FALSE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   384
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   385
    
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   386
    return TRUE; 
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   387
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   388
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   389
GIOStatus
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   390
_lm_ssl_read (LmSSL *ssl, gchar *buf, gint len, gsize *bytes_read)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   391
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   392
    GIOStatus status;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   393
    gint ssl_ret;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   394
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   395
    *bytes_read = 0;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   396
    ssl_ret = SSL_read(ssl->ssl, buf, len);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   397
    status = ssl_io_status_from_return(ssl, ssl_ret);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   398
    if (status == G_IO_STATUS_NORMAL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   399
        *bytes_read = ssl_ret;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   400
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   401
    
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   402
    return status;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   403
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   404
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   405
gint
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   406
_lm_ssl_send (LmSSL *ssl, const gchar *str, gint len)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   407
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   408
    GIOStatus status;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   409
    gint ssl_ret;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   410
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   411
    do {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   412
        ssl_ret = SSL_write(ssl->ssl, str, len);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   413
        if (ssl_ret <= 0) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   414
            status = ssl_io_status_from_return(ssl, ssl_ret);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   415
            if (status != G_IO_STATUS_AGAIN)
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   416
                return -1;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   417
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   418
    } while (ssl_ret <= 0);
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   419
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   420
    return ssl_ret;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   421
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   422
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   423
void 
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   424
_lm_ssl_close (LmSSL *ssl)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   425
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   426
    if (ssl->ssl != NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   427
        SSL_shutdown(ssl->ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   428
        SSL_free(ssl->ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   429
        ssl->ssl = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   430
    }
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   431
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   432
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   433
void
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   434
_lm_ssl_free (LmSSL *ssl)
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   435
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   436
    SSL_CTX_free(ssl->ssl_ctx);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   437
    ssl->ssl_ctx = NULL;
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   438
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   439
    _lm_ssl_base_free_fields (LM_SSL_BASE(ssl));
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   440
    g_free (ssl);
138
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   441
}
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   442
210cf8993393 2006-04-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   443
#endif /* HAVE_GNUTLS */