2
* OpenVPN -- An application to securely tunnel IP networks
3
* over a single TCP/UDP port, with support for SSL/TLS-based
4
* session authentication and key exchange,
5
* packet encryption, packet authentication, and
8
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9
* Copyright (C) 2010 Fox Crypto B.V. <openvpn@fox-it.com>
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License version 2
13
* as published by the Free Software Foundation.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program (see the file COPYING included with this
22
* distribution); if not, write to the Free Software Foundation, Inc.,
23
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
* @file Control Channel Verification Module PolarSSL backend
32
#elif defined(_MSC_VER)
33
#include "config-msvc.h"
38
#if defined(ENABLE_SSL) && defined(ENABLE_CRYPTO_POLARSSL)
40
#include "ssl_verify.h"
41
#include <polarssl/sha1.h>
43
#define MAX_SUBJECT_LENGTH 256
46
verify_callback (void *session_obj, x509_cert *cert, int cert_depth,
49
struct tls_session *session = (struct tls_session *) session_obj;
50
struct gc_arena gc = gc_new();
55
session->verified = false;
57
/* Remember certificate hash */
58
cert_hash_remember (session, cert_depth, x509_get_sha1_hash(cert, &gc));
60
/* did peer present cert which was signed by our root cert? */
63
char *subject = x509_get_subject(cert, &gc);
66
msg (D_TLS_ERRORS, "VERIFY ERROR: depth=%d, flags=%x, %s", cert_depth, *flags, subject);
68
msg (D_TLS_ERRORS, "VERIFY ERROR: depth=%d, flags=%x, could not extract X509 "
69
"subject string from certificate", *flags, cert_depth);
71
/* Leave flags set to non-zero to indicate that the cert is not ok */
73
else if (SUCCESS != verify_cert(session, cert, cert_depth))
75
*flags |= BADCERT_OTHER;
81
* PolarSSL-1.2.0+ expects 0 on anything except fatal errors.
86
#ifdef ENABLE_X509ALTUSERNAME
87
# warning "X509 alt user name not yet supported for PolarSSL"
91
x509_get_username (char *cn, int cn_len,
92
char *x509_username_field, x509_cert *cert)
98
name = &cert->subject;
100
/* Find common name */
101
while( name != NULL )
103
if( memcmp( name->oid.p, OID_CN, OID_SIZE(OID_CN) ) == 0)
109
/* Not found, return an error if this is the peer's certificate */
113
/* Found, extract CN */
114
if (cn_len > name->val.len)
115
memcpy( cn, name->val.p, name->val.len );
118
memcpy( cn, name->val.p, cn_len);
126
x509_get_serial (x509_cert *cert, struct gc_arena *gc)
131
size_t len = cert->serial.len * 3 + 1;
133
buf = gc_malloc(len, true, gc);
135
if(x509parse_serial_gets(buf, len-1, &cert->serial) < 0)
142
x509_get_sha1_hash (x509_cert *cert, struct gc_arena *gc)
144
unsigned char *sha1_hash = gc_malloc(SHA_DIGEST_LENGTH, false, gc);
145
sha1(cert->tbs.p, cert->tbs.len, sha1_hash);
150
x509_get_subject(x509_cert *cert, struct gc_arena *gc)
152
char tmp_subject[MAX_SUBJECT_LENGTH] = {0};
153
char *subject = NULL;
157
ret = x509parse_dn_gets( tmp_subject, MAX_SUBJECT_LENGTH-1, &cert->subject );
160
/* Allocate the required space for the subject */
161
subject = string_alloc(tmp_subject, gc);
168
* Save X509 fields to environment, using the naming convention:
170
* X509_{cert_depth}_{name}={value}
173
x509_setenv (struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert)
177
const x509_name *name;
180
name = &cert->subject;
182
memset( s, 0, sizeof( s ) );
184
while( name != NULL )
186
char name_expand[64+8];
188
if( name->oid.len == 2 && memcmp( name->oid.p, OID_X520, 2 ) == 0 )
190
switch( name->oid.p[2] )
192
case X520_COMMON_NAME:
193
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_CN",
197
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_C",
201
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_L",
205
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_ST",
208
case X520_ORGANIZATION:
209
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_O",
213
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_OU",
217
openvpn_snprintf (name_expand, sizeof(name_expand),
218
"X509_%d_0x%02X", cert_depth, name->oid.p[2]);
222
else if( name->oid.len == 8 && memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
224
switch( name->oid.p[8] )
227
openvpn_snprintf (name_expand, sizeof(name_expand),
228
"X509_%d_emailAddress", cert_depth); break;
231
openvpn_snprintf (name_expand, sizeof(name_expand),
232
"X509_%d_0x%02X", cert_depth, name->oid.p[8]);
238
openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_\?\?",
242
for( i = 0; i < name->val.len; i++ )
244
if( i >= (int) sizeof( s ) - 1 )
248
if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
254
/* Check both strings, set environment variable */
255
string_mod (name_expand, CC_PRINT, CC_CRLF, '_');
256
string_mod ((char*)s, CC_PRINT, CC_CRLF, '_');
257
setenv_str (es, name_expand, (char*)s);
264
x509_verify_ns_cert_type(const x509_cert *cert, const int usage)
266
if (usage == NS_CERT_CHECK_NONE)
268
if (usage == NS_CERT_CHECK_CLIENT)
269
return ((cert->ext_types & EXT_NS_CERT_TYPE)
270
&& (cert->ns_cert_type & NS_CERT_TYPE_SSL_CLIENT)) ? SUCCESS : FAILURE;
271
if (usage == NS_CERT_CHECK_SERVER)
272
return ((cert->ext_types & EXT_NS_CERT_TYPE)
273
&& (cert->ns_cert_type & NS_CERT_TYPE_SSL_SERVER)) ? SUCCESS : FAILURE;
279
x509_verify_cert_ku (x509_cert *cert, const unsigned * const expected_ku,
282
result_t fFound = FAILURE;
284
if(!(cert->ext_types & EXT_KEY_USAGE))
286
msg (D_HANDSHAKE, "Certificate does not have key usage extension");
291
unsigned nku = cert->key_usage;
293
msg (D_HANDSHAKE, "Validating certificate key usage");
294
for (i=0; SUCCESS != fFound && i<expected_len; i++)
296
if (expected_ku[i] != 0)
298
msg (D_HANDSHAKE, "++ Certificate has key usage %04x, expects "
299
"%04x", nku, expected_ku[i]);
301
if (nku == expected_ku[i])
312
x509_verify_cert_eku (x509_cert *cert, const char * const expected_oid)
314
result_t fFound = FAILURE;
316
if (!(cert->ext_types & EXT_EXTENDED_KEY_USAGE))
318
msg (D_HANDSHAKE, "Certificate does not have extended key usage extension");
322
x509_sequence *oid_seq = &(cert->ext_key_usage);
324
msg (D_HANDSHAKE, "Validating certificate extended key usage");
325
while (oid_seq != NULL)
327
x509_buf *oid = &oid_seq->buf;
328
char oid_num_str[1024];
331
oid_str = x509_oid_get_description(oid);
334
msg (D_HANDSHAKE, "++ Certificate has EKU (str) %s, expects %s",
335
oid_str, expected_oid);
336
if (!strcmp (expected_oid, oid_str))
343
if (0 == x509_oid_get_numeric_string( oid_num_str,
344
sizeof (oid_num_str), oid))
346
msg (D_HANDSHAKE, "++ Certificate has EKU (oid) %s, expects %s",
347
oid_num_str, expected_oid);
348
if (!strcmp (expected_oid, oid_num_str))
354
oid_seq = oid_seq->next;
362
x509_write_pem(FILE *peercert_file, x509_cert *peercert)
364
msg (M_WARN, "PolarSSL does not support writing peer certificate in PEM format");
369
* check peer cert against CRL
372
x509_verify_crl(const char *crl_file, x509_cert *cert, const char *subject)
374
result_t retval = FAILURE;
377
if (x509parse_crlfile(&crl, crl_file) != 0)
379
msg (M_ERR, "CRL: cannot read CRL from file %s", crl_file);
383
if(cert->issuer_raw.len != crl.issuer_raw.len ||
384
memcmp(crl.issuer_raw.p, cert->issuer_raw.p, crl.issuer_raw.len) != 0)
386
msg (M_WARN, "CRL: CRL %s is from a different issuer than the issuer of "
387
"certificate %s", crl_file, subject);
392
if (0 != x509parse_revoked(cert, &crl))
394
msg (D_HANDSHAKE, "CRL CHECK FAILED: %s is REVOKED", subject);
399
msg (D_HANDSHAKE, "CRL CHECK OK: %s",subject);
406
#endif /* #if defined(ENABLE_SSL) && defined(ENABLE_CRYPTO_POLARSSL) */