loudmouth/lm-ssl-gnutls.c
author Frank Zschockelt <lm@freakysoft.de>
Thu, 04 Feb 2016 19:18:03 +0100
changeset 690 7ccf2113ec5f
parent 654 54f17719b9d0
child 700 9d99e76c64fe
permissions -rw-r--r--
Update the postal address of the FSF
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 -*- */
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     2
/*
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     3
 * Copyright (C) 2003-2006 Imendio AB
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     4
 *
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public License as
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     7
 * published by the Free Software Foundation; either version 2 of the
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     8
 * License, or (at your option) any later version.
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     9
 *
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    13
 * Lesser General Public License for more details.
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    14
 *
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
690
7ccf2113ec5f Update the postal address of the FSF
Frank Zschockelt <lm@freakysoft.de>
parents: 654
diff changeset
    16
 * License along with this program; if not, see <https://www.gnu.org/licenses>
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    17
 */
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    18
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    19
#include <config.h>
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    20
651
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
    21
#include <errno.h>
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    22
#include <string.h>
651
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
    23
#include <sys/types.h>
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
    24
#include <sys/stat.h>
654
54f17719b9d0 Make opendir(3) prototypes available
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 651
diff changeset
    25
#include <dirent.h>
651
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
    26
#include <unistd.h>
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    27
#include <glib.h>
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    28
319
2a892980f78b Enable compression in GNUTLS. (Patch by Robert McQueen)
Senko Rasic <senko.rasic@collabora.co.uk>
parents: 301
diff changeset
    29
#include "lm-debug.h"
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    30
#include "lm-error.h"
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    31
#include "lm-ssl-base.h"
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    32
#include "lm-ssl-internals.h"
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    33
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    34
#ifdef HAVE_GNUTLS
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    35
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    36
#include <gnutls/x509.h>
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    37
273
6be9056c1fb3 Verify certificates in GnuTLS code. Fixes LM-107. Patch by Sjoerd Simons.
Dafydd Harries <daf@rhydd.org>
parents: 189
diff changeset
    38
#define CA_PEM_FILE "/etc/ssl/certs/ca-certificates.crt"
6be9056c1fb3 Verify certificates in GnuTLS code. Fixes LM-107. Patch by Sjoerd Simons.
Dafydd Harries <daf@rhydd.org>
parents: 189
diff changeset
    39
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    40
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
    41
    LmSSLBase base;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    42
633
08a8206c3c21 Gnutls fixes part1
Myhailo Danylenko <isbear@ukrpost.net>
parents: 607
diff changeset
    43
    gnutls_session_t                 gnutls_session;
08a8206c3c21 Gnutls fixes part1
Myhailo Danylenko <isbear@ukrpost.net>
parents: 607
diff changeset
    44
    gnutls_certificate_credentials_t gnutls_xcred;
08a8206c3c21 Gnutls fixes part1
Myhailo Danylenko <isbear@ukrpost.net>
parents: 607
diff changeset
    45
    gboolean                         started;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    46
};
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    47
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    48
static gboolean       ssl_verify_certificate    (LmSSL       *ssl,
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
    49
                                                 const gchar *server);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    50
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    51
static gboolean
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    52
ssl_verify_certificate (LmSSL *ssl, const gchar *server)
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    53
{
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
    54
    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
    55
    unsigned int        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
    56
    int rc;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    57
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
    58
    base = LM_SSL_BASE (ssl);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
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
    /* This verification function uses the trusted CAs in the credentials
cdd6a0c5b439 Went 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
     * structure. So you must have installed one or more CA 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
    62
     */
cdd6a0c5b439 Went 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
    rc = gnutls_certificate_verify_peers2 (ssl->gnutls_session, &status);
273
6be9056c1fb3 Verify certificates in GnuTLS code. Fixes LM-107. Patch by Sjoerd Simons.
Dafydd Harries <daf@rhydd.org>
parents: 189
diff changeset
    64
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
    65
    if (rc == GNUTLS_E_NO_CERTIFICATE_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
    66
        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
    67
                        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
    68
                        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
    69
            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
    70
        }
cdd6a0c5b439 Went 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
    }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    72
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
    73
    if (rc != 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
    74
        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
    75
                        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
    76
                        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
    77
            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
    78
        }
cdd6a0c5b439 Went 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
    }
273
6be9056c1fb3 Verify certificates in GnuTLS code. Fixes LM-107. Patch by Sjoerd Simons.
Dafydd Harries <daf@rhydd.org>
parents: 189
diff changeset
    80
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
    81
    if (rc == GNUTLS_E_NO_CERTIFICATE_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
    82
        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
    83
                        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
    84
                        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
    85
            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
    86
        }
cdd6a0c5b439 Went 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
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
    88
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
    89
    if (status & GNUTLS_CERT_INVALID
cdd6a0c5b439 Went 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
        || status & GNUTLS_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
    91
        if (base->func (ssl, 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
    92
                        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
    93
            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
    94
        }
cdd6a0c5b439 Went 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
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
    96
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
    97
    if (gnutls_certificate_expiration_time_peers (ssl->gnutls_session) < time (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
    98
        if (base->func (ssl, 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
    99
                        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
   100
            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
   101
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   102
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   103
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
   104
    if (gnutls_certificate_activation_time_peers (ssl->gnutls_session) > time (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
   105
        if (base->func (ssl, 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
   106
                        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
   107
            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
   108
        }
cdd6a0c5b439 Went 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
    }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   110
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
   111
    if (gnutls_certificate_type_get (ssl->gnutls_session) == GNUTLS_CRT_X509) {
633
08a8206c3c21 Gnutls fixes part1
Myhailo Danylenko <isbear@ukrpost.net>
parents: 607
diff changeset
   112
        const gnutls_datum_t* cert_list;
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
        guint cert_list_size;
cdd6a0c5b439 Went 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
        size_t digest_size;
633
08a8206c3c21 Gnutls fixes part1
Myhailo Danylenko <isbear@ukrpost.net>
parents: 607
diff changeset
   115
        gnutls_x509_crt_t cert;
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   116
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
   117
        cert_list = gnutls_certificate_get_peers (ssl->gnutls_session, &cert_list_size);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   118
        if (cert_list == 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
   119
            if (base->func (ssl, 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
   120
                            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
   121
                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
   122
            }
cdd6a0c5b439 Went 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
        }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   124
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
   125
        gnutls_x509_crt_init (&cert);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   126
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
   127
        if (gnutls_x509_crt_import (cert, &cert_list[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
   128
                                    GNUTLS_X509_FMT_DER) != 0) {
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   129
            if (base->func (ssl, LM_SSL_STATUS_NO_CERT_FOUND,
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
   130
                            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
   131
                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
   132
            }
cdd6a0c5b439 Went 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
        }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   134
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
   135
        if (!gnutls_x509_crt_check_hostname (cert, 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
   136
            if (base->func (ssl, 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
   137
                            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
   138
                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
   139
            }
cdd6a0c5b439 Went 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
        }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   141
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
   142
        gnutls_x509_crt_deinit (cert);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   143
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
   144
        digest_size = sizeof (base->fingerprint);
146
5da5bf8186dd 2006-05-27 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 137
diff changeset
   145
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
   146
        if (gnutls_fingerprint (GNUTLS_DIG_MD5, &cert_list[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
   147
                                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
   148
                                &digest_size) >= 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
   149
            if (base->expected_fingerprint &&
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   150
                memcmp (base->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
   151
                        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
   152
                        digest_size) &&
cdd6a0c5b439 Went 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
                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
   154
                            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
   155
                            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
   156
                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
   157
            }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   158
        }
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
   159
        else 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
   160
                             base->func_data) != LM_SSL_RESPONSE_CONTINUE) {
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   161
            return FALSE;
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   162
        }
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
   163
    }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   164
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
   165
    return TRUE;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   166
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   167
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   168
/* From lm-ssl-protected.h */
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   169
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   170
LmSSL *
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   171
_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
   172
             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
   173
             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
   174
             GDestroyNotify  notify)
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   175
{
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
   176
    LmSSL *ssl;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   177
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
   178
    ssl = g_new0 (LmSSL, 1);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   179
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
   180
    _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
   181
                       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
   182
                       ssl_function, user_data, notify);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   183
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
   184
    return ssl;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   185
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   186
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   187
void
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   188
_lm_ssl_initialize (LmSSL *ssl)
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   189
{
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
   190
    gnutls_global_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
   191
    gnutls_certificate_allocate_credentials (&ssl->gnutls_xcred);
cdd6a0c5b439 Went 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
    gnutls_certificate_set_x509_trust_file(ssl->gnutls_xcred,
cdd6a0c5b439 Went 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
                                           CA_PEM_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
   194
                                           GNUTLS_X509_FMT_PEM);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   195
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   196
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   197
gboolean
651
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   198
_lm_ssl_set_ca (LmSSL       *ssl,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   199
                const gchar *ca_path)
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   200
{
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   201
    struct stat target;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   202
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   203
    if (stat (ca_path, &target) != 0) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   204
        g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   205
               "ca_path '%s': no such file or directory", ca_path);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   206
        return FALSE;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   207
    }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   208
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   209
    if (S_ISDIR (target.st_mode)) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   210
        int success = 0;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   211
        int worked_at_least_once = 0;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   212
        DIR *dir;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   213
        struct dirent *entry;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   214
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   215
        if ((dir = opendir (ca_path)) == NULL) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   216
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   217
                   "Couldn't open '%s': %s",
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   218
                   ca_path, strerror(errno));
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   219
            return FALSE;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   220
        }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   221
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   222
        for (entry = readdir (dir); entry != NULL; entry = readdir (dir)) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   223
            struct stat file;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   224
            gchar *path = g_build_path ("/", ca_path, entry->d_name, NULL);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   225
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   226
            if ((stat (path, &file) == 0) && S_ISREG (file.st_mode)) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   227
                success = gnutls_certificate_set_x509_trust_file (
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   228
                                ssl->gnutls_xcred, path, GNUTLS_X509_FMT_PEM);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   229
                if (success > 0)
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   230
                    worked_at_least_once = 1;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   231
                if (success < 0) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   232
                    g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   233
                           "Loading of certificate '%s' failed: %s",
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   234
                            path, gnutls_strerror(success));
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   235
                }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   236
            }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   237
            g_free (path);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   238
        }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   239
        closedir (dir);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   240
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   241
        if (!worked_at_least_once) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   242
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   243
                   "No certificates in ca_path '%s'. Are they in PEM format?",
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   244
                   ca_path);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   245
            return FALSE;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   246
        }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   247
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   248
    } else if (S_ISREG (target.st_mode)) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   249
        int success = 0;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   250
        success = gnutls_certificate_set_x509_trust_file (ssl->gnutls_xcred,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   251
                                                          ca_path,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   252
                                                          GNUTLS_X509_FMT_PEM);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   253
        if (success < 0) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   254
            g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_SSL,
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   255
                   "Loading of ca_path '%s' failed: %s",
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   256
                   ca_path, gnutls_strerror(success));
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   257
            return FALSE;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   258
        }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   259
    }
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   260
    return TRUE;
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   261
}
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   262
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   263
gboolean
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   264
_lm_ssl_begin (LmSSL *ssl, gint fd, const gchar *server, GError **error)
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   265
{
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
   266
    int ret;
650
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   267
    LmSSLBase *base;
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
   268
    gboolean auth_ok = TRUE;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   269
650
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   270
    base = LM_SSL_BASE(ssl);
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
   271
    gnutls_init (&ssl->gnutls_session, GNUTLS_CLIENT);
650
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   272
    if (base->cipher_list) {
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   273
      gnutls_priority_set_direct (ssl->gnutls_session, base->cipher_list, NULL);
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   274
    } else {
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   275
      gnutls_priority_set_direct (ssl->gnutls_session, "NORMAL", NULL);
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   276
    }
651
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   277
    if (base->ca_path) {
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   278
      _lm_ssl_set_ca(ssl, base->ca_path);
2990ee03cfa3 Implemented lm_ssl_set_ca
Frank Zschockelt <lm@freakysoft.de>
parents: 650
diff changeset
   279
    }
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
   280
    gnutls_credentials_set (ssl->gnutls_session,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   281
                            GNUTLS_CRD_CERTIFICATE,
cdd6a0c5b439 Went 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
                            ssl->gnutls_xcred);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   283
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
   284
    gnutls_transport_set_ptr (ssl->gnutls_session,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   285
                              (gnutls_transport_ptr_t)(glong) fd);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   286
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
   287
    ret = gnutls_handshake (ssl->gnutls_session);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   288
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
   289
    if (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
   290
        auth_ok = 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
   291
    }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   292
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
   293
    if (ret < 0 || !auth_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
   294
        char *errmsg;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   295
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
   296
        if (!auth_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
   297
            errmsg = "authentication 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
   298
        } 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
   299
            errmsg = "handshake 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
   300
        }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   301
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   302
        g_set_error (error,
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
   303
                     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
   304
                     "*** GNUTLS %s: %s",
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   305
                     errmsg, gnutls_strerror (ret));
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   306
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
   307
        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
   308
    }
301
e83880152e77 Don't crash on connection close if SSL wasn't started.
Senko Rasic <senko.rasic@collabora.co.uk>
parents: 273
diff changeset
   309
650
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   310
    lm_verbose ("GNUTLS negotiated cipher suite: %s",
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   311
                gnutls_cipher_suite_get_name(gnutls_kx_get(ssl->gnutls_session),
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   312
                                             gnutls_cipher_get(ssl->gnutls_session),
a51209f570b6 Implemented lm_ssl_set_cipher_list
Frank Zschockelt <lm@freakysoft.de>
parents: 649
diff changeset
   313
                                             gnutls_mac_get(ssl->gnutls_session)));
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
   314
    lm_verbose ("GNUTLS negotiated compression: %s",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   315
                gnutls_compression_get_name (gnutls_compression_get
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   316
                                             (ssl->gnutls_session)));
319
2a892980f78b Enable compression in GNUTLS. (Patch by Robert McQueen)
Senko Rasic <senko.rasic@collabora.co.uk>
parents: 301
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
    ssl->started = TRUE;
301
e83880152e77 Don't crash on connection close if SSL wasn't started.
Senko Rasic <senko.rasic@collabora.co.uk>
parents: 273
diff changeset
   319
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
   320
    return TRUE;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   321
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   322
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   323
GIOStatus
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   324
_lm_ssl_read (LmSSL *ssl, gchar *buf, gint len, gsize *bytes_read)
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   325
{
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
   326
    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
   327
    gint      b_read;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   328
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
   329
    *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
   330
    b_read = gnutls_record_recv (ssl->gnutls_session, buf, len);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   331
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
   332
    if (b_read == GNUTLS_E_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
   333
        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
   334
    }
cdd6a0c5b439 Went 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
    else if (b_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
   336
        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
   337
    }
cdd6a0c5b439 Went 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
    else if (b_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
   339
        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
   340
    } 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
   341
        *bytes_read = (guint) b_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
   342
        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
   343
    }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   344
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
   345
    return status;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   346
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   347
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   348
gint
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   349
_lm_ssl_send (LmSSL *ssl, const gchar *str, gint len)
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   350
{
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
   351
    gint bytes_written;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   352
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
   353
    bytes_written = gnutls_record_send (ssl->gnutls_session, str, len);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   354
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
   355
    while (bytes_written < 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
   356
        if (bytes_written != GNUTLS_E_INTERRUPTED &&
cdd6a0c5b439 Went 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
            bytes_written != GNUTLS_E_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
   358
            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
   359
        }
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   360
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   361
        bytes_written = gnutls_record_send (ssl->gnutls_session,
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
   362
                                            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
   363
    }
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   364
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
   365
    return bytes_written;
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   366
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   367
607
e2bd14357c9a Some style fixes
Frank Zschockelt <lm@freakysoft.de>
parents: 518
diff changeset
   368
void
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   369
_lm_ssl_close (LmSSL *ssl)
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   370
{
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
   371
    if (!ssl->started)
cdd6a0c5b439 Went 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
        return;
301
e83880152e77 Don't crash on connection close if SSL wasn't started.
Senko Rasic <senko.rasic@collabora.co.uk>
parents: 273
diff changeset
   373
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
   374
    gnutls_deinit (ssl->gnutls_session);
cdd6a0c5b439 Went 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
    gnutls_certificate_free_credentials (ssl->gnutls_xcred);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   376
    gnutls_global_deinit ();
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   377
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   378
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   379
void
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   380
_lm_ssl_free (LmSSL *ssl)
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   381
{
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
   382
    _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
   383
    g_free (ssl);
137
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   384
}
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   385
18785575aa7a 2006-04-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   386
#endif /* HAVE_GNUTLS */