~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to src/openvpn/ssl_verify_polarssl.c

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
6
 *             packet compression.
 
7
 *
 
8
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
 
9
 *  Copyright (C) 2010 Fox Crypto B.V. <openvpn@fox-it.com>
 
10
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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
 
24
 */
 
25
 
 
26
/**
 
27
 * @file Control Channel Verification Module PolarSSL backend
 
28
 */
 
29
 
 
30
#ifdef HAVE_CONFIG_H
 
31
#include "config.h"
 
32
#elif defined(_MSC_VER)
 
33
#include "config-msvc.h"
 
34
#endif
 
35
 
 
36
#include "syshead.h"
 
37
 
 
38
#if defined(ENABLE_SSL) && defined(ENABLE_CRYPTO_POLARSSL)
 
39
 
 
40
#include "ssl_verify.h"
 
41
#include <polarssl/sha1.h>
 
42
 
 
43
#define MAX_SUBJECT_LENGTH 256
 
44
 
 
45
int
 
46
verify_callback (void *session_obj, x509_cert *cert, int cert_depth,
 
47
    int *flags)
 
48
{
 
49
  struct tls_session *session = (struct tls_session *) session_obj;
 
50
  struct gc_arena gc = gc_new();
 
51
 
 
52
  ASSERT (cert);
 
53
  ASSERT (session);
 
54
 
 
55
  session->verified = false;
 
56
 
 
57
  /* Remember certificate hash */
 
58
  cert_hash_remember (session, cert_depth, x509_get_sha1_hash(cert, &gc));
 
59
 
 
60
  /* did peer present cert which was signed by our root cert? */
 
61
  if (*flags != 0)
 
62
    {
 
63
      char *subject = x509_get_subject(cert, &gc);
 
64
 
 
65
      if (subject)
 
66
        msg (D_TLS_ERRORS, "VERIFY ERROR: depth=%d, flags=%x, %s", cert_depth, *flags, subject);
 
67
      else
 
68
        msg (D_TLS_ERRORS, "VERIFY ERROR: depth=%d, flags=%x, could not extract X509 "
 
69
              "subject string from certificate", *flags, cert_depth);
 
70
 
 
71
      /* Leave flags set to non-zero to indicate that the cert is not ok */
 
72
    }
 
73
  else if (SUCCESS != verify_cert(session, cert, cert_depth))
 
74
    {
 
75
      *flags |= BADCERT_OTHER;
 
76
    }
 
77
 
 
78
  gc_free(&gc);
 
79
 
 
80
  /*
 
81
   * PolarSSL-1.2.0+ expects 0 on anything except fatal errors.
 
82
   */
 
83
  return 0;
 
84
}
 
85
 
 
86
#ifdef ENABLE_X509ALTUSERNAME
 
87
# warning "X509 alt user name not yet supported for PolarSSL"
 
88
#endif
 
89
 
 
90
result_t
 
91
x509_get_username (char *cn, int cn_len,
 
92
    char *x509_username_field, x509_cert *cert)
 
93
{
 
94
  x509_name *name;
 
95
 
 
96
  ASSERT( cn != NULL );
 
97
 
 
98
  name = &cert->subject;
 
99
 
 
100
  /* Find common name */
 
101
  while( name != NULL )
 
102
  {
 
103
      if( memcmp( name->oid.p, OID_CN, OID_SIZE(OID_CN) ) == 0)
 
104
        break;
 
105
 
 
106
      name = name->next;
 
107
  }
 
108
 
 
109
  /* Not found, return an error if this is the peer's certificate */
 
110
  if( name == NULL )
 
111
      return FAILURE;
 
112
 
 
113
  /* Found, extract CN */
 
114
  if (cn_len > name->val.len)
 
115
    memcpy( cn, name->val.p, name->val.len );
 
116
  else
 
117
    {
 
118
      memcpy( cn, name->val.p, cn_len);
 
119
      cn[cn_len-1] = '\0';
 
120
    }
 
121
 
 
122
  return SUCCESS;
 
123
}
 
124
 
 
125
char *
 
126
x509_get_serial (x509_cert *cert, struct gc_arena *gc)
 
127
{
 
128
  int ret = 0;
 
129
  int i = 0;
 
130
  char *buf = NULL;
 
131
  size_t len = cert->serial.len * 3 + 1;
 
132
 
 
133
  buf = gc_malloc(len, true, gc);
 
134
 
 
135
  if(x509parse_serial_gets(buf, len-1, &cert->serial) < 0)
 
136
    buf = NULL;
 
137
 
 
138
  return buf;
 
139
}
 
140
 
 
141
unsigned char *
 
142
x509_get_sha1_hash (x509_cert *cert, struct gc_arena *gc)
 
143
{
 
144
  unsigned char *sha1_hash = gc_malloc(SHA_DIGEST_LENGTH, false, gc);
 
145
  sha1(cert->tbs.p, cert->tbs.len, sha1_hash);
 
146
  return sha1_hash;
 
147
}
 
148
 
 
149
char *
 
150
x509_get_subject(x509_cert *cert, struct gc_arena *gc)
 
151
{
 
152
  char tmp_subject[MAX_SUBJECT_LENGTH] = {0};
 
153
  char *subject = NULL;
 
154
 
 
155
  int ret = 0;
 
156
 
 
157
  ret = x509parse_dn_gets( tmp_subject, MAX_SUBJECT_LENGTH-1, &cert->subject );
 
158
  if (ret > 0)
 
159
    {
 
160
      /* Allocate the required space for the subject */
 
161
      subject = string_alloc(tmp_subject, gc);
 
162
    }
 
163
 
 
164
  return subject;
 
165
}
 
166
 
 
167
/*
 
168
 * Save X509 fields to environment, using the naming convention:
 
169
 *
 
170
 * X509_{cert_depth}_{name}={value}
 
171
 */
 
172
void
 
173
x509_setenv (struct env_set *es, int cert_depth, openvpn_x509_cert_t *cert)
 
174
{
 
175
  int i;
 
176
  unsigned char c;
 
177
  const x509_name *name;
 
178
  char s[128];
 
179
 
 
180
  name = &cert->subject;
 
181
 
 
182
  memset( s, 0, sizeof( s ) );
 
183
 
 
184
  while( name != NULL )
 
185
    {
 
186
      char name_expand[64+8];
 
187
 
 
188
      if( name->oid.len == 2 && memcmp( name->oid.p, OID_X520, 2 ) == 0 )
 
189
        {
 
190
          switch( name->oid.p[2] )
 
191
            {
 
192
            case X520_COMMON_NAME:
 
193
                openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_CN",
 
194
                    cert_depth); break;
 
195
 
 
196
            case X520_COUNTRY:
 
197
                openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_C",
 
198
                    cert_depth); break;
 
199
 
 
200
            case X520_LOCALITY:
 
201
                openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_L",
 
202
                    cert_depth); break;
 
203
 
 
204
            case X520_STATE:
 
205
                openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_ST",
 
206
                    cert_depth); break;
 
207
 
 
208
            case X520_ORGANIZATION:
 
209
                openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_O",
 
210
                    cert_depth); break;
 
211
 
 
212
            case X520_ORG_UNIT:
 
213
                openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_OU",
 
214
                    cert_depth); break;
 
215
 
 
216
            default:
 
217
                openvpn_snprintf (name_expand, sizeof(name_expand),
 
218
                    "X509_%d_0x%02X", cert_depth, name->oid.p[2]);
 
219
                break;
 
220
            }
 
221
        }
 
222
        else if( name->oid.len == 8 && memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
 
223
          {
 
224
            switch( name->oid.p[8] )
 
225
              {
 
226
                case PKCS9_EMAIL:
 
227
                  openvpn_snprintf (name_expand, sizeof(name_expand),
 
228
                      "X509_%d_emailAddress", cert_depth); break;
 
229
 
 
230
                default:
 
231
                  openvpn_snprintf (name_expand, sizeof(name_expand),
 
232
                      "X509_%d_0x%02X", cert_depth, name->oid.p[8]);
 
233
                  break;
 
234
              }
 
235
          }
 
236
        else
 
237
          {
 
238
            openvpn_snprintf (name_expand, sizeof(name_expand), "X509_%d_\?\?",
 
239
                cert_depth);
 
240
          }
 
241
 
 
242
        for( i = 0; i < name->val.len; i++ )
 
243
        {
 
244
            if( i >= (int) sizeof( s ) - 1 )
 
245
                break;
 
246
 
 
247
            c = name->val.p[i];
 
248
            if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
 
249
                 s[i] = '?';
 
250
            else s[i] = c;
 
251
        }
 
252
        s[i] = '\0';
 
253
 
 
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);
 
258
 
 
259
        name = name->next;
 
260
    }
 
261
}
 
262
 
 
263
result_t
 
264
x509_verify_ns_cert_type(const x509_cert *cert, const int usage)
 
265
{
 
266
  if (usage == NS_CERT_CHECK_NONE)
 
267
    return SUCCESS;
 
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;
 
274
 
 
275
  return FAILURE;
 
276
}
 
277
 
 
278
result_t
 
279
x509_verify_cert_ku (x509_cert *cert, const unsigned * const expected_ku,
 
280
    int expected_len)
 
281
{
 
282
  result_t fFound = FAILURE;
 
283
 
 
284
  if(!(cert->ext_types & EXT_KEY_USAGE))
 
285
    {
 
286
      msg (D_HANDSHAKE, "Certificate does not have key usage extension");
 
287
    }
 
288
  else
 
289
    {
 
290
      int i;
 
291
      unsigned nku = cert->key_usage;
 
292
 
 
293
      msg (D_HANDSHAKE, "Validating certificate key usage");
 
294
      for (i=0; SUCCESS != fFound && i<expected_len; i++)
 
295
        {
 
296
          if (expected_ku[i] != 0)
 
297
            {
 
298
              msg (D_HANDSHAKE, "++ Certificate has key usage  %04x, expects "
 
299
                  "%04x", nku, expected_ku[i]);
 
300
 
 
301
              if (nku == expected_ku[i])
 
302
                {
 
303
                  fFound = SUCCESS;
 
304
                }
 
305
            }
 
306
        }
 
307
    }
 
308
  return fFound;
 
309
}
 
310
 
 
311
result_t
 
312
x509_verify_cert_eku (x509_cert *cert, const char * const expected_oid)
 
313
{
 
314
  result_t fFound = FAILURE;
 
315
 
 
316
  if (!(cert->ext_types & EXT_EXTENDED_KEY_USAGE))
 
317
    {
 
318
      msg (D_HANDSHAKE, "Certificate does not have extended key usage extension");
 
319
    }
 
320
  else
 
321
    {
 
322
      x509_sequence *oid_seq = &(cert->ext_key_usage);
 
323
 
 
324
      msg (D_HANDSHAKE, "Validating certificate extended key usage");
 
325
      while (oid_seq != NULL)
 
326
        {
 
327
          x509_buf *oid = &oid_seq->buf;
 
328
          char oid_num_str[1024];
 
329
          const char *oid_str;
 
330
 
 
331
          oid_str = x509_oid_get_description(oid);
 
332
          if (oid_str != NULL)
 
333
            {
 
334
              msg (D_HANDSHAKE, "++ Certificate has EKU (str) %s, expects %s",
 
335
                  oid_str, expected_oid);
 
336
              if (!strcmp (expected_oid, oid_str))
 
337
                {
 
338
                  fFound = SUCCESS;
 
339
                  break;
 
340
                }
 
341
            }
 
342
 
 
343
          if (0 == x509_oid_get_numeric_string( oid_num_str,
 
344
              sizeof (oid_num_str), oid))
 
345
            {
 
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))
 
349
                {
 
350
                  fFound = SUCCESS;
 
351
                  break;
 
352
                }
 
353
            }
 
354
          oid_seq = oid_seq->next;
 
355
        }
 
356
    }
 
357
 
 
358
    return fFound;
 
359
}
 
360
 
 
361
result_t
 
362
x509_write_pem(FILE *peercert_file, x509_cert *peercert)
 
363
{
 
364
    msg (M_WARN, "PolarSSL does not support writing peer certificate in PEM format");
 
365
    return FAILURE;
 
366
}
 
367
 
 
368
/*
 
369
 * check peer cert against CRL
 
370
 */
 
371
result_t
 
372
x509_verify_crl(const char *crl_file, x509_cert *cert, const char *subject)
 
373
{
 
374
  result_t retval = FAILURE;
 
375
  x509_crl crl = {0};
 
376
 
 
377
  if (x509parse_crlfile(&crl, crl_file) != 0)
 
378
    {
 
379
      msg (M_ERR, "CRL: cannot read CRL from file %s", crl_file);
 
380
      goto end;
 
381
    }
 
382
 
 
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)
 
385
    {
 
386
      msg (M_WARN, "CRL: CRL %s is from a different issuer than the issuer of "
 
387
          "certificate %s", crl_file, subject);
 
388
      retval = SUCCESS;
 
389
      goto end;
 
390
    }
 
391
 
 
392
  if (0 != x509parse_revoked(cert, &crl))
 
393
    {
 
394
      msg (D_HANDSHAKE, "CRL CHECK FAILED: %s is REVOKED", subject);
 
395
      goto end;
 
396
    }
 
397
 
 
398
  retval = SUCCESS;
 
399
  msg (D_HANDSHAKE, "CRL CHECK OK: %s",subject);
 
400
 
 
401
end:
 
402
  x509_crl_free(&crl);
 
403
  return retval;
 
404
}
 
405
 
 
406
#endif /* #if defined(ENABLE_SSL) && defined(ENABLE_CRYPTO_POLARSSL) */