File: tls-util.c

package info (click to toggle)
libprelude 1.0.0-11.9
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 24,456 kB
  • ctags: 17,851
  • sloc: ansic: 190,841; cpp: 38,769; xml: 30,141; sh: 11,668; makefile: 692; python: 481; awk: 341; yacc: 238; lex: 186; perl: 5
file content (235 lines) | stat: -rw-r--r-- 7,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*****
*
* Copyright (C) 2004, 2005 PreludeIDS Technologies. All Rights Reserved.
* Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
*
* This file is part of the Prelude library.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING.  If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <gnutls/gnutls.h>
#include <gnutls/x509.h>

#define PRELUDE_ERROR_SOURCE_DEFAULT PRELUDE_ERROR_SOURCE_CLIENT
#include "prelude-error.h"

#include "common.h"
#include "prelude-log.h"
#include "prelude-client.h"
#include "tls-util.h"


#ifndef HAVE_GNUTLS_X509_CRT_LIST_IMPORT

#define X509_BEGIN_STR1 "-----BEGIN X509 CERTIFICATE"
#define X509_BEGIN_STR2 "-----BEGIN CERTIFICATE"

int _prelude_tls_crt_list_import(gnutls_x509_crt_t *certs, unsigned int *cmax,
                                 const gnutls_datum_t *indata, gnutls_x509_crt_fmt_t format)
{
        int ret;
        size_t skiplen;
        gnutls_datum_t data;
        unsigned int i = 0;
        unsigned char *ptr;

        data.size = indata->size;
        data.data = indata->data;

        while ( i < *cmax || ! certs ) {
                skiplen = sizeof(X509_BEGIN_STR1) - 1;
                ptr = memmem(data.data, data.size, X509_BEGIN_STR1, skiplen);
                if ( ! ptr ) {
                        skiplen = sizeof(X509_BEGIN_STR2) - 1;
                        ptr = memmem(data.data, data.size, X509_BEGIN_STR2, skiplen);
                }

                if ( ! ptr )
                        break;

                data.data = ptr;
                data.size = data.size - (ptr - data.data);

                if ( ! certs )
                        i++;
                else {
                        ret = gnutls_x509_crt_init(&certs[i]);
                        if ( ret < 0 )
                                goto err;

                        ret = gnutls_x509_crt_import(certs[i++], &data, format);
                        if ( ret < 0 )
                                goto err;
                }

                data.data += skiplen;
                data.size -= skiplen;
        }

        *cmax = i;
        return i;

   err:
        *cmax = 0;
        while ( i-- >= 0 )
                gnutls_x509_crt_deinit(certs[i]);

        return ret;
}

#else

int _prelude_tls_crt_list_import(gnutls_x509_crt_t *certs, unsigned int *cmax,
                                 const gnutls_datum_t *indata, gnutls_x509_crt_fmt_t format)
{
        return gnutls_x509_crt_list_import(certs, cmax, indata, format, GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
}

#endif


int tls_certificates_load(gnutls_x509_privkey_t key, const char *certfname, gnutls_certificate_credentials_t cred)
{
        int ret;
        size_t size;
        gnutls_datum_t certfile;
        unsigned int cert_max, i;
        gnutls_x509_crt_t certs[1024];

        ret = _prelude_load_file(certfname, &certfile.data, &size);
        if ( ret < 0 )
                return ret;
        certfile.size = (unsigned int) size;

        cert_max = sizeof(certs) / sizeof(*certs);
        ret = _prelude_tls_crt_list_import(certs, &cert_max, &certfile, GNUTLS_X509_FMT_PEM);
        if ( ret < 0 ) {
                ret = prelude_error_verbose(PRELUDE_ERROR_PROFILE, "error importing certificate listing: %s", gnutls_strerror(ret));
                goto err;
        }

        for ( i = 0; i < cert_max; i++) {
                ret = gnutls_certificate_set_x509_key(cred, &certs[i], 1, key);
                gnutls_x509_crt_deinit(certs[i]);

                if ( ret < 0 ) {
                        ret = prelude_error_verbose(PRELUDE_ERROR_PROFILE, "error importing certificate: %s", gnutls_strerror(ret));
                        break;
                }
        }

err:
        _prelude_unload_file(certfile.data, certfile.size);

        return ret;
}



int tls_certificate_get_peer_analyzerid(gnutls_session_t session, uint64_t *analyzerid)
{
        int ret;
        char buf[1024];
        gnutls_x509_crt_t cert;
        size_t size = sizeof(buf);
        unsigned int cert_list_size;
        const gnutls_datum_t *cert_list;

        cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
        if ( ! cert_list || cert_list_size != 1 )
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "invalid number of peer certificate: %d", cert_list_size);

        ret = gnutls_x509_crt_init(&cert);
        if ( ret < 0 )
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "%s", gnutls_strerror(ret));

        ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
        if ( ret < 0) {
                gnutls_x509_crt_deinit(cert);
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "error importing certificate: %s", gnutls_strerror(ret));
        }

        size = sizeof(buf);
        ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_DN_QUALIFIER, 0, 0, buf, &size);
        if ( ret < 0 ) {
                gnutls_x509_crt_deinit(cert);
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "certificate miss DN qualifier");
        }

        ret = sscanf(buf, "%" PRELUDE_PRIu64, analyzerid);
        if ( ret != 1 ) {
                gnutls_x509_crt_deinit(cert);
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "certificate analyzerid '%s' is invalid", buf);
        }

        gnutls_x509_crt_deinit(cert);

        return 0;
}



int tls_certificate_get_permission(gnutls_session_t session,
                                   prelude_connection_permission_t *permission)
{
        int ret, tmp;
        char buf[1024];
        gnutls_x509_crt_t cert;
        size_t size = sizeof(buf);
        const gnutls_datum_t *data;

        data = gnutls_certificate_get_ours(session);
        if ( ! data )
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "could not get own certificate");

        ret = gnutls_x509_crt_init(&cert);
        if ( ret < 0 )
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "error initializing certificate: %s", gnutls_strerror(ret));

        ret = gnutls_x509_crt_import(cert, data, GNUTLS_X509_FMT_DER);
        if ( ret < 0 ) {
                gnutls_x509_crt_deinit(cert);
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "error importing certificate: %s", gnutls_strerror(ret));
        }

        ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0, buf, &size);
        if ( ret < 0 ) {
                gnutls_x509_crt_deinit(cert);
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "could not get certificate CN field: %s", gnutls_strerror(ret));
        }

        ret = sscanf(buf, "%d", &tmp);
        if ( ret != 1 ) {
                gnutls_x509_crt_deinit(cert);
                return prelude_error_verbose(PRELUDE_ERROR_TLS, "certificate analyzerid value '%s' is invalid", buf);
        }

        *permission = (prelude_connection_permission_t) tmp;
        gnutls_x509_crt_deinit(cert);

        return 0;
}