~ubuntu-branches/ubuntu/precise/openvpn/precise-updates

« back to all changes in this revision

Viewing changes to ssl.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-10-05 06:21:14 UTC
  • mfrom: (1.1.16 upstream) (10.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20101005062114-18lyqud9e3p4g735
Tags: 2.1.3-1ubuntu1
* 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 compatablitiy
  + debian/control: Add lsb-base >= 3.2-14 to allow status_of_proc()

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *             packet encryption, packet authentication, and
6
6
 *             packet compression.
7
7
 *
8
 
 *  Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
 
8
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9
9
 *
10
10
 *  Additions for eurephia plugin done by:
11
11
 *         David Sommerseth <dazo@users.sourceforge.net> Copyright (C) 2008-2009
1240
1240
}
1241
1241
 
1242
1242
#ifdef MANAGEMENT_DEF_AUTH
 
1243
/*
 
1244
 * For deferred auth, this is where the management interface calls (on server)
 
1245
 * to indicate auth failure/success.
 
1246
 */
1243
1247
bool
1244
1248
tls_authenticate_key (struct tls_multi *multi, const unsigned int mda_key_id, const bool auth, const char *client_reason)
1245
1249
{
1647
1651
                {
1648
1652
#ifdef ENABLE_MANAGEMENT
1649
1653
                  if (management && (ERR_GET_REASON (ERR_peek_error()) == EVP_R_BAD_DECRYPT))
1650
 
                    management_auth_failure (management, UP_TYPE_PRIVATE_KEY);
 
1654
                    management_auth_failure (management, UP_TYPE_PRIVATE_KEY, NULL);
1651
1655
#endif
1652
1656
                  msg (M_WARN|M_SSL, "Cannot load private key file %s", options->priv_key_file);
1653
1657
                  goto err;
2274
2278
  free_buf (&ks->plaintext_read_buf);
2275
2279
  free_buf (&ks->plaintext_write_buf);
2276
2280
  free_buf (&ks->ack_write_buf);
 
2281
  buffer_list_free(ks->paybuf);
2277
2282
 
2278
2283
  if (ks->send_reliable)
2279
2284
    {
2566
2571
 
2567
2572
#ifdef MANAGEMENT_DEF_AUTH
2568
2573
  man_def_auth_set_client_reason(multi, NULL);  
 
2574
 
 
2575
  free (multi->peer_info);
2569
2576
#endif
2570
2577
 
2571
2578
  if (multi->locked_cn)
3070
3077
  return 1;
3071
3078
}
3072
3079
 
 
3080
static void
 
3081
flush_payload_buffer (struct tls_multi *multi, struct key_state *ks)
 
3082
{
 
3083
  struct buffer *b;
 
3084
  while ((b = buffer_list_peek (ks->paybuf)))
 
3085
    {
 
3086
      key_state_write_plaintext_const (multi, ks, b->data, b->len);
 
3087
      buffer_list_pop (ks->paybuf);
 
3088
    }
 
3089
}
 
3090
 
3073
3091
/*
3074
3092
 * Macros for key_state_soft_reset & tls_process
3075
3093
 */
3114
3132
}
3115
3133
 
3116
3134
static bool
 
3135
write_empty_string (struct buffer *buf)
 
3136
{
 
3137
  if (!buf_write_u16 (buf, 0))
 
3138
    return false;
 
3139
  return true;
 
3140
}
 
3141
 
 
3142
static bool
3117
3143
read_string (struct buffer *buf, char *str, const unsigned int capacity)
3118
3144
{
3119
3145
  const int len = buf_read_u16 (buf);
3125
3151
  return true;
3126
3152
}
3127
3153
 
 
3154
static char *
 
3155
read_string_alloc (struct buffer *buf)
 
3156
{
 
3157
  const int len = buf_read_u16 (buf);
 
3158
  char *str;
 
3159
 
 
3160
  if (len < 1)
 
3161
    return NULL;
 
3162
  str = (char *) malloc(len);
 
3163
  check_malloc_return(str);
 
3164
  if (!buf_read (buf, str, len))
 
3165
    {
 
3166
      free (str);
 
3167
      return NULL;
 
3168
    }
 
3169
  str[len-1] = '\0';
 
3170
  return str;
 
3171
}
 
3172
 
 
3173
void
 
3174
read_string_discard (struct buffer *buf)
 
3175
{
 
3176
  char *data = read_string_alloc(buf);
 
3177
  if (data)
 
3178
    free (data);
 
3179
}
 
3180
 
3128
3181
/*
3129
3182
 * Authenticate a client using username/password.
3130
3183
 * Runs on server.
3336
3389
}
3337
3390
 
3338
3391
static bool
 
3392
push_peer_info(struct buffer *buf, struct tls_session *session)
 
3393
{
 
3394
  struct gc_arena gc = gc_new ();
 
3395
  bool ret = false;
 
3396
 
 
3397
#ifdef ENABLE_PUSH_PEER_INFO
 
3398
  if (session->opt->push_peer_info) /* write peer info */
 
3399
    {
 
3400
      struct env_set *es = session->opt->es;
 
3401
      struct env_item *e;
 
3402
      struct buffer out = alloc_buf_gc (512*3, &gc);
 
3403
 
 
3404
      /* push version */
 
3405
      buf_printf (&out, "IV_VER=%s\n", PACKAGE_VERSION);
 
3406
 
 
3407
      /* push platform */
 
3408
#if defined(TARGET_LINUX)
 
3409
      buf_printf (&out, "IV_PLAT=linux\n");
 
3410
#elif defined(TARGET_SOLARIS)
 
3411
      buf_printf (&out, "IV_PLAT=solaris\n");
 
3412
#elif defined(TARGET_OPENBSD)
 
3413
      buf_printf (&out, "IV_PLAT=openbsd\n");
 
3414
#elif defined(TARGET_DARWIN)
 
3415
      buf_printf (&out, "IV_PLAT=mac\n");
 
3416
#elif defined(TARGET_NETBSD)
 
3417
      buf_printf (&out, "IV_PLAT=netbsd\n");
 
3418
#elif defined(TARGET_FREEBSD)
 
3419
      buf_printf (&out, "IV_PLAT=freebsd\n");
 
3420
#elif defined(WIN32)
 
3421
      buf_printf (&out, "IV_PLAT=win\n");
 
3422
#endif
 
3423
 
 
3424
      /* push mac addr */
 
3425
      {
 
3426
        bool get_default_gateway_mac_addr (unsigned char *macaddr);
 
3427
        uint8_t macaddr[6];
 
3428
        get_default_gateway_mac_addr (macaddr);
 
3429
        buf_printf (&out, "IV_HWADDR=%s\n", format_hex_ex (macaddr, 6, 0, 1, ":", &gc));
 
3430
      }
 
3431
 
 
3432
      /* push env vars that begin with UV_ */
 
3433
      for (e=es->list; e != NULL; e=e->next)
 
3434
        {
 
3435
          if (e->string)
 
3436
            {
 
3437
              if (!strncmp(e->string, "UV_", 3) && buf_safe(&out, strlen(e->string)+1))
 
3438
                buf_printf (&out, "%s\n", e->string);
 
3439
            }
 
3440
        }
 
3441
 
 
3442
      if (!write_string(buf, BSTR(&out), -1))
 
3443
        goto error;
 
3444
    }
 
3445
  else
 
3446
#endif
 
3447
    {
 
3448
      if (!write_empty_string (buf)) /* no peer info */
 
3449
        goto error;
 
3450
    }
 
3451
  ret = true;
 
3452
 
 
3453
 error:
 
3454
  gc_free (&gc);
 
3455
  return ret;
 
3456
}
 
3457
 
 
3458
static bool
3339
3459
key_method_2_write (struct buffer *buf, struct tls_session *session)
3340
3460
{
3341
3461
  ASSERT (session->opt->key_method == 2);
3369
3489
        goto error;
3370
3490
      purge_user_pass (&auth_user_pass, false);
3371
3491
    }
 
3492
  else
 
3493
    {
 
3494
      if (!write_empty_string (buf)) /* no username */
 
3495
        goto error;
 
3496
      if (!write_empty_string (buf)) /* no password */
 
3497
        goto error;
 
3498
    }
 
3499
 
 
3500
  if (!push_peer_info (buf, session))
 
3501
    goto error;
3372
3502
 
3373
3503
  /*
3374
3504
   * generate tunnel keys if server
3515
3645
      int s1 = OPENVPN_PLUGIN_FUNC_SUCCESS;
3516
3646
      bool s2 = true;
3517
3647
      char *raw_username;
 
3648
      bool username_status, password_status;
3518
3649
 
3519
3650
      /* get username/password from plaintext buffer */
3520
3651
      ALLOC_OBJ_CLEAR_GC (up, struct user_pass, &gc);
3521
 
      if (!read_string (buf, up->username, USER_PASS_LEN)
3522
 
          || !read_string (buf, up->password, USER_PASS_LEN))
 
3652
      username_status = read_string (buf, up->username, USER_PASS_LEN);
 
3653
      password_status = read_string (buf, up->password, USER_PASS_LEN);
 
3654
      if (!username_status || !password_status)
3523
3655
        {
3524
3656
          CLEAR (*up);
3525
3657
          if (!(session->opt->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL))
3540
3672
 
3541
3673
      /* call plugin(s) and/or script */
3542
3674
#ifdef MANAGEMENT_DEF_AUTH
 
3675
      /* get peer info from control channel */
 
3676
      free (multi->peer_info);
 
3677
      multi->peer_info = read_string_alloc (buf);
 
3678
 
3543
3679
      if (man_def_auth == KMDA_DEF)
3544
3680
        man_def_auth = verify_user_pass_management (session, up, raw_username);
3545
3681
#endif
3710
3846
static int
3711
3847
auth_deferred_expire_window (const struct tls_options *o)
3712
3848
{
3713
 
  const int hw = o->handshake_window;
 
3849
  int ret = o->handshake_window;
3714
3850
  const int r2 = o->renegotiate_seconds / 2;
3715
 
  return min_int (hw, r2);
 
3851
 
 
3852
  if (o->renegotiate_seconds && r2 < ret)
 
3853
    ret = r2;
 
3854
  return ret;
3716
3855
}
3717
3856
 
3718
3857
/*
3864
4003
                  /* Set outgoing address for data channel packets */
3865
4004
                  link_socket_set_outgoing_addr (NULL, to_link_socket_info, &ks->remote_addr, session->common_name, session->opt->es);
3866
4005
 
 
4006
                  /* Flush any payload packets that were buffered before our state transitioned to S_ACTIVE */
 
4007
                  flush_payload_buffer (multi, ks);
 
4008
 
3867
4009
#ifdef MEASURE_TLS_HANDSHAKE_STATS
3868
4010
                  show_tls_performance_stats();
3869
4011
#endif
4963
5105
      if (key_state_write_plaintext_const (multi, ks, data, size) == 1)
4964
5106
        ret = true;
4965
5107
    }
 
5108
  else
 
5109
    {
 
5110
      if (!ks->paybuf)
 
5111
        ks->paybuf = buffer_list_new (0);
 
5112
      buffer_list_push_data (ks->paybuf, data, (size_t)size);
 
5113
      ret = true;
 
5114
    }
4966
5115
 
4967
5116
  ERR_clear_error ();
4968
5117