~ubuntu-branches/ubuntu/hardy/openswan/hardy-updates

« back to all changes in this revision

Viewing changes to linux/net/ipsec/ipsec_esp.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2005-01-27 16:10:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050127161011-idgybmyz3vwhpfiq
Tags: 2.3.0-2
Urgency HIGH due to security issue and problems with build-deps in sarge.
* Fix the security issue. Please see
  http://www.idefense.com/application/poi/display?id=190&
      type=vulnerabilities&flashstatus=false
  for more details. Thanks to Martin Schulze for informing me about
  this issue.
  Closes: #292458: Openswan XAUTH/PAM Buffer Overflow Vulnerability
* Added a Build-Dependency to lynx.
  Closes: #291143: openswan: FTBFS: Missing build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 * for more details.
14
14
 */
15
15
 
16
 
char ipsec_esp_c_version[] = "RCSID $Id: ipsec_esp.c,v 1.1 2003/12/13 19:10:21 mcr Exp $";
 
16
char ipsec_esp_c_version[] = "RCSID $Id: ipsec_esp.c,v 1.8 2004/09/14 00:22:57 mcr Exp $";
17
17
#include <linux/config.h>
18
18
#include <linux/version.h>
19
19
 
21
21
#include <linux/module.h>
22
22
#include <linux/kernel.h> /* printk() */
23
23
 
24
 
#include "freeswan/ipsec_param.h"
 
24
#include "openswan/ipsec_param.h"
25
25
 
26
26
#ifdef MALLOC_SLAB
27
27
# include <linux/slab.h> /* kmalloc() */
36
36
#include <linux/etherdevice.h>  /* eth_type_trans */
37
37
#include <linux/ip.h>           /* struct iphdr */
38
38
#include <linux/skbuff.h>
39
 
#include <freeswan.h>
 
39
#include <openswan.h>
40
40
#ifdef SPINLOCK
41
41
# ifdef SPINLOCK_23
42
42
#  include <linux/spinlock.h> /* *lock* */
51
51
#endif /* NET21 */
52
52
#include <asm/checksum.h>
53
53
#include <net/ip.h>
54
 
 
55
 
#include "freeswan/radij.h"
56
 
#include "freeswan/ipsec_encap.h"
57
 
#include "freeswan/ipsec_sa.h"
58
 
 
59
 
#include "freeswan/ipsec_radij.h"
60
 
#include "freeswan/ipsec_xform.h"
61
 
#include "freeswan/ipsec_tunnel.h"
62
 
#include "freeswan/ipsec_rcv.h"
63
 
#include "freeswan/ipsec_xmit.h"
64
 
 
65
 
#include "freeswan/ipsec_auth.h"
66
 
 
67
 
#ifdef CONFIG_IPSEC_ESP
68
 
#include "freeswan/ipsec_esp.h"
69
 
#endif /* CONFIG_IPSEC_ESP */
70
 
 
71
 
#include "freeswan/ipsec_proto.h"
72
 
 
73
 
#ifdef CONFIG_IPSEC_DEBUG
74
 
int debug_esp = 0;
75
 
#endif /* CONFIG_IPSEC_DEBUG */
76
 
 
77
 
 
78
 
#ifdef CONFIG_IPSEC_ESP
 
54
#include <net/protocol.h>
 
55
 
 
56
#include "openswan/radij.h"
 
57
#include "openswan/ipsec_encap.h"
 
58
#include "openswan/ipsec_sa.h"
 
59
 
 
60
#include "openswan/ipsec_radij.h"
 
61
#include "openswan/ipsec_xform.h"
 
62
#include "openswan/ipsec_tunnel.h"
 
63
#include "openswan/ipsec_rcv.h"
 
64
#include "openswan/ipsec_xmit.h"
 
65
 
 
66
#include "openswan/ipsec_auth.h"
 
67
 
 
68
#ifdef CONFIG_KLIPS_ESP
 
69
#include "openswan/ipsec_esp.h"
 
70
#endif /* CONFIG_KLIPS_ESP */
 
71
 
 
72
#include "openswan/ipsec_proto.h"
 
73
#include "openswan/ipsec_alg.h"
 
74
 
 
75
#ifdef CONFIG_KLIPS_ESP
79
76
enum ipsec_rcv_value
80
77
ipsec_rcv_esp_checks(struct ipsec_rcv_state *irs,
81
78
                     struct sk_buff *skb)
152
149
                SHA1_CTX        sha1;
153
150
        } tctx;
154
151
 
 
152
#ifdef CONFIG_KLIPS_ALG
 
153
        if (irs->ipsp->ips_alg_auth) {
 
154
                KLIPS_PRINT(debug_rcv,
 
155
                                "klips_debug:ipsec_rcv: "
 
156
                                "ipsec_alg hashing proto=%d... ",
 
157
                                irs->said.proto);
 
158
                if(irs->said.proto == IPPROTO_ESP) {
 
159
                        ipsec_alg_sa_esp_hash(irs->ipsp,
 
160
                                        (caddr_t)espp, irs->ilen,
 
161
                                        irs->hash, AHHMAC_HASHLEN);
 
162
                        return IPSEC_RCV_OK;
 
163
                }
 
164
                return IPSEC_RCV_BADPROTO;
 
165
        }
 
166
#endif
155
167
        aa = irs->authfuncs;
156
168
 
157
169
        /* copy the initialized keying material */
182
194
        int badpad = 0;
183
195
        int i;
184
196
        struct sk_buff *skb;
 
197
#ifdef CONFIG_KLIPS_ALG
 
198
        struct ipsec_alg_enc *ixt_e=NULL;
 
199
#endif /* CONFIG_KLIPS_ALG */
185
200
 
186
201
        skb=irs->skb;
187
202
 
188
203
        idat = skb->data + irs->iphlen;
189
204
 
 
205
#ifdef CONFIG_KLIPS_ALG
 
206
        if ((ixt_e=ipsp->ips_alg_enc)) {
 
207
                esphlen = ESP_HEADER_LEN + ixt_e->ixt_ivlen/8;
 
208
                KLIPS_PRINT(debug_rcv,
 
209
                                "klips_debug:ipsec_rcv: "
 
210
                                "encalg=%d esphlen=%d\n",
 
211
                                ipsp->ips_encalg, esphlen);
 
212
        } else
 
213
#endif /* CONFIG_KLIPS_ALG */
190
214
        switch(ipsp->ips_encalg) {
191
215
        case ESP_3DES:
192
216
                iv[0] = *((__u32 *)(espp->esp_iv)    );
204
228
        idat += esphlen;
205
229
        irs->ilen -= esphlen;
206
230
 
 
231
#ifdef CONFIG_KLIPS_ALG
 
232
        if (ixt_e)
 
233
        {
 
234
                if (ipsec_alg_esp_encrypt(ipsp, 
 
235
                                        idat, irs->ilen, espp->esp_iv, 
 
236
                                        IPSEC_ALG_DECRYPT) <= 0)
 
237
                {
 
238
                        printk("klips_error:ipsec_rcv: "
 
239
                                        "got packet with esplen = %d "
 
240
                                        "from %s -- should be on "
 
241
                                        "ENC(%d) octet boundary, "
 
242
                                        "packet dropped\n",
 
243
                                        irs->ilen,
 
244
                                        irs->ipsaddr_txt,
 
245
                                        ipsp->ips_encalg);
 
246
                        if(irs->stats) {
 
247
                                irs->stats->rx_errors++;
 
248
                        }
 
249
                        return IPSEC_RCV_BAD_DECRYPT;
 
250
                }
 
251
        } else
 
252
#endif /* CONFIG_KLIPS_ALG */
207
253
        switch(ipsp->ips_encalg) {
208
254
        case ESP_3DES:
209
255
                if ((irs->ilen) % 8) {
332
378
  unsigned char *idat, *pad;
333
379
  __u8 hash[AH_AMAX];
334
380
  union {
335
 
#ifdef CONFIG_IPSEC_AUTH_HMAC_MD5
 
381
#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
336
382
    MD5_CTX md5;
337
 
#endif /* CONFIG_IPSEC_AUTH_HMAC_MD5 */
338
 
#ifdef CONFIG_IPSEC_AUTH_HMAC_SHA1
 
383
#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
 
384
#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
339
385
    SHA1_CTX sha1;
340
 
#endif /* CONFIG_IPSEC_AUTH_HMAC_SHA1 */
 
386
#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
341
387
  } tctx;
342
388
 
343
389
  dat = (unsigned char *)ixs->iph;
347
393
  espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
348
394
  
349
395
  switch(ixs->ipsp->ips_encalg) {
350
 
#if defined(CONFIG_IPSEC_ENC_3DES)
351
 
#ifdef CONFIG_IPSEC_ENC_3DES
 
396
#if defined(CONFIG_KLIPS_ENC_3DES)
 
397
#ifdef CONFIG_KLIPS_ENC_3DES
352
398
  case ESP_3DES:
353
 
#endif /* CONFIG_IPSEC_ENC_3DES */
 
399
#endif /* CONFIG_KLIPS_ENC_3DES */
354
400
    iv[0] = *((__u32*)&(espp->esp_iv)    ) =
355
401
      ((__u32*)(ixs->ipsp->ips_iv))[0];
356
402
    iv[1] = *((__u32*)&(espp->esp_iv) + 1) =
357
403
      ((__u32*)(ixs->ipsp->ips_iv))[1];
358
404
    break;
359
 
#endif /* defined(CONFIG_IPSEC_ENC_3DES) */
 
405
#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
360
406
  default:
361
407
    ixs->stats->tx_errors++;
362
408
    return IPSEC_XMIT_ESP_BADALG;
377
423
  ixs->iph->protocol = IPPROTO_ESP;
378
424
  
379
425
  switch(ixs->ipsp->ips_encalg) {
380
 
#ifdef CONFIG_IPSEC_ENC_3DES
 
426
#ifdef CONFIG_KLIPS_ENC_3DES
381
427
  case ESP_3DES:
382
428
    des_ede3_cbc_encrypt((des_cblock *)idat,
383
429
                         (des_cblock *)idat,
387
433
                         ((struct des_eks *)(ixs->ipsp->ips_key_e))[2].ks,
388
434
                         (des_cblock *)iv, 1);
389
435
    break;
390
 
#endif /* CONFIG_IPSEC_ENC_3DES */
 
436
#endif /* CONFIG_KLIPS_ENC_3DES */
391
437
  default:
392
438
    ixs->stats->tx_errors++;
393
439
    return IPSEC_XMIT_ESP_BADALG;
394
440
  }
395
441
  
396
442
  switch(ixs->ipsp->ips_encalg) {
397
 
#if defined(CONFIG_IPSEC_ENC_3DES)
398
 
#ifdef CONFIG_IPSEC_ENC_3DES
 
443
#if defined(CONFIG_KLIPS_ENC_3DES)
 
444
#ifdef CONFIG_KLIPS_ENC_3DES
399
445
  case ESP_3DES:
400
 
#endif /* CONFIG_IPSEC_ENC_3DES */
 
446
#endif /* CONFIG_KLIPS_ENC_3DES */
401
447
    /* XXX update IV with the last 8 octets of the encryption */
402
448
#if KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK
403
449
    ((__u32*)(ixs->ipsp->ips_iv))[0] =
408
454
    prng_bytes(&ipsec_prng, (char *)ixs->ipsp->ips_iv, EMT_ESPDES_IV_SZ); 
409
455
#endif /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
410
456
    break;
411
 
#endif /* defined(CONFIG_IPSEC_ENC_3DES) */
 
457
#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
412
458
  default:
413
459
    ixs->stats->tx_errors++;
414
460
    return IPSEC_XMIT_ESP_BADALG;
415
461
  }
416
462
  
417
463
  switch(ixs->ipsp->ips_authalg) {
418
 
#ifdef CONFIG_IPSEC_AUTH_HMAC_MD5
 
464
#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
419
465
  case AH_MD5:
420
466
    ipsec_xmit_dmp("espp", (char*)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
421
467
    tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
422
468
    ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
423
 
    MD5Update(&tctx.md5, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
 
469
    osMD5Update(&tctx.md5, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
424
470
    ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
425
 
    MD5Final(hash, &tctx.md5);
 
471
    osMD5Final(hash, &tctx.md5);
426
472
    ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
427
473
    tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
428
474
    ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
429
 
    MD5Update(&tctx.md5, hash, AHMD596_ALEN);
 
475
    osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
430
476
    ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
431
 
    MD5Final(hash, &tctx.md5);
 
477
    osMD5Final(hash, &tctx.md5);
432
478
    ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
433
479
    memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
434
480
    
436
482
    memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
437
483
    memset((caddr_t)hash, 0, sizeof(*hash));
438
484
    break;
439
 
#endif /* CONFIG_IPSEC_AUTH_HMAC_MD5 */
440
 
#ifdef CONFIG_IPSEC_AUTH_HMAC_SHA1
 
485
#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
 
486
#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
441
487
  case AH_SHA:
442
488
    tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
443
489
    SHA1Update(&tctx.sha1, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
451
497
    memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
452
498
    memset((caddr_t)hash, 0, sizeof(*hash));
453
499
    break;
454
 
#endif /* CONFIG_IPSEC_AUTH_HMAC_SHA1 */
 
500
#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
455
501
  case AH_NONE:
456
502
    break;
457
503
  default:
478
524
        },
479
525
};
480
526
 
 
527
#ifdef NET_26
 
528
struct inet_protocol esp_protocol = {
 
529
  .handler = ipsec_rcv,
 
530
  .no_policy = 1,
 
531
};
 
532
#else
481
533
struct inet_protocol esp_protocol =
482
534
{
483
535
        ipsec_rcv,                      /* ESP handler          */
492
544
        "ESP"                           /* name */
493
545
#endif
494
546
};
495
 
 
496
 
 
497
 
 
498
 
#endif /* !CONFIG_IPSEC_ESP */
499
 
 
500
 
 
 
547
#endif /* NET_26 */
 
548
 
 
549
#endif /* !CONFIG_KLIPS_ESP */
 
550
 
 
551
 
 
552
/*
 
553
 * $Log: ipsec_esp.c,v $
 
554
 * Revision 1.8  2004/09/14 00:22:57  mcr
 
555
 *      adjustment of MD5* functions.
 
556
 *
 
557
 * Revision 1.7  2004/09/13 02:23:01  mcr
 
558
 *      #define inet_protocol if necessary.
 
559
 *
 
560
 * Revision 1.6  2004/09/06 18:35:49  mcr
 
561
 *      2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
 
562
 *      so adjust for that.
 
563
 *
 
564
 * Revision 1.5  2004/08/17 03:27:23  mcr
 
565
 *      klips 2.6 edits.
 
566
 *
 
567
 * Revision 1.4  2004/08/04 15:57:07  mcr
 
568
 *      moved des .h files to include/des/ *
 
569
 *      included 2.6 protocol specific things
 
570
 *      started at NAT-T support, but it will require a kernel patch.
 
571
 *
 
572
 * Revision 1.3  2004/07/10 19:11:18  mcr
 
573
 *      CONFIG_IPSEC -> CONFIG_KLIPS.
 
574
 *
 
575
 * Revision 1.2  2004/04/06 02:49:25  mcr
 
576
 *      pullup of algo code from alg-branch.
 
577
 *
 
578
 *
 
579
 *
 
580
 */