699ab4effc88878f8967c1d2821323acdaaffa41
[strongswan.git] / src / pluto / spdb.c
1 /* Security Policy Data Base (such as it is)
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * RCSID $Id$
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <sys/queue.h>
24
25 #include <freeswan.h>
26 #include <ipsec_policy.h>
27
28 #include "constants.h"
29 #include "defs.h"
30 #include "id.h"
31 #include "connections.h"
32 #include "state.h"
33 #include "packet.h"
34 #include "keys.h"
35 #include "kernel.h"
36 #include "log.h"
37 #include "spdb.h"
38 #include "whack.h"
39 #include "sha1.h"
40 #include "md5.h"
41 #include "crypto.h" /* requires sha1.h and md5.h */
42 #include "alg_info.h"
43 #include "kernel_alg.h"
44 #include "ike_alg.h"
45 #include "db_ops.h"
46 #include "nat_traversal.h"
47
48 #define AD(x) x, elemsof(x) /* Array Description */
49 #define AD_NULL NULL, 0
50
51 /**************** Oakely (main mode) SA database ****************/
52
53 /* array of proposals to be conjoined (can only be one for Oakley) */
54
55 static struct db_prop oakley_pc[] =
56 { { PROTO_ISAKMP, AD_NULL } };
57
58 /* array of proposal conjuncts (can only be one) */
59
60 static struct db_prop_conj oakley_props[] = { { AD(oakley_pc) } };
61
62 /* the sadb entry */
63 struct db_sa oakley_sadb = { AD(oakley_props) };
64
65 /**************** IPsec (quick mode) SA database ****************/
66
67 /* arrays of attributes for transforms */
68
69 static struct db_attr espsha1_attr[] = {
70 { AUTH_ALGORITHM, AUTH_ALGORITHM_HMAC_SHA1 },
71 };
72
73 static struct db_attr ah_HMAC_SHA1_attr[] = {
74 { AUTH_ALGORITHM, AUTH_ALGORITHM_HMAC_SHA1 },
75 };
76
77 /* arrays of transforms, each in in preference order */
78
79 static struct db_trans espa_trans[] = {
80 { ESP_3DES, AD(espsha1_attr) },
81 };
82
83 static struct db_trans esp_trans[] = {
84 { ESP_3DES, AD_NULL },
85 };
86
87 #ifdef SUPPORT_ESP_NULL
88 static struct db_trans espnull_trans[] = {
89 { ESP_NULL, AD(espsha1_attr) },
90 };
91 #endif /* SUPPORT_ESP_NULL */
92
93 static struct db_trans ah_trans[] = {
94 { AH_SHA, AD(ah_HMAC_SHA1_attr) },
95 };
96
97 static struct db_trans ipcomp_trans[] = {
98 { IPCOMP_DEFLATE, AD_NULL },
99 };
100
101 /* arrays of proposals to be conjoined */
102
103 static struct db_prop ah_pc[] = {
104 { PROTO_IPSEC_AH, AD(ah_trans) },
105 };
106
107 #ifdef SUPPORT_ESP_NULL
108 static struct db_prop espnull_pc[] = {
109 { PROTO_IPSEC_ESP, AD(espnull_trans) },
110 };
111 #endif /* SUPPORT_ESP_NULL */
112
113 static struct db_prop esp_pc[] = {
114 { PROTO_IPSEC_ESP, AD(espa_trans) },
115 };
116
117 static struct db_prop ah_esp_pc[] = {
118 { PROTO_IPSEC_AH, AD(ah_trans) },
119 { PROTO_IPSEC_ESP, AD(esp_trans) },
120 };
121
122 static struct db_prop compress_pc[] = {
123 { PROTO_IPCOMP, AD(ipcomp_trans) },
124 };
125
126 static struct db_prop ah_compress_pc[] = {
127 { PROTO_IPSEC_AH, AD(ah_trans) },
128 { PROTO_IPCOMP, AD(ipcomp_trans) },
129 };
130
131 #ifdef SUPPORT_ESP_NULL
132 static struct db_prop espnull_compress_pc[] = {
133 { PROTO_IPSEC_ESP, AD(espnull_trans) },
134 { PROTO_IPCOMP, AD(ipcomp_trans) },
135 };
136 #endif /* SUPPORT_ESP_NULL */
137
138 static struct db_prop esp_compress_pc[] = {
139 { PROTO_IPSEC_ESP, AD(espa_trans) },
140 { PROTO_IPCOMP, AD(ipcomp_trans) },
141 };
142
143 static struct db_prop ah_esp_compress_pc[] = {
144 { PROTO_IPSEC_AH, AD(ah_trans) },
145 { PROTO_IPSEC_ESP, AD(esp_trans) },
146 { PROTO_IPCOMP, AD(ipcomp_trans) },
147 };
148
149 /* arrays of proposal alternatives (each element is a conjunction) */
150
151 static struct db_prop_conj ah_props[] = {
152 { AD(ah_pc) },
153 #ifdef SUPPORT_ESP_NULL
154 { AD(espnull_pc) }
155 #endif
156 };
157
158 static struct db_prop_conj esp_props[] =
159 { { AD(esp_pc) } };
160
161 static struct db_prop_conj ah_esp_props[] =
162 { { AD(ah_esp_pc) } };
163
164 static struct db_prop_conj compress_props[] = {
165 { AD(compress_pc) },
166 };
167
168 static struct db_prop_conj ah_compress_props[] = {
169 { AD(ah_compress_pc) },
170 #ifdef SUPPORT_ESP_NULL
171 { AD(espnull_compress_pc) }
172 #endif
173 };
174
175 static struct db_prop_conj esp_compress_props[] =
176 { { AD(esp_compress_pc) } };
177
178 static struct db_prop_conj ah_esp_compress_props[] =
179 { { AD(ah_esp_compress_pc) } };
180
181 /* The IPsec sadb is subscripted by a bitset (subset of policy)
182 * with members from { POLICY_ENCRYPT, POLICY_AUTHENTICATE, POLICY_COMPRESS }
183 * shifted right by POLICY_IPSEC_SHIFT.
184 */
185 struct db_sa ipsec_sadb[1 << 3] = {
186 { AD_NULL }, /* none */
187 { AD(esp_props) }, /* POLICY_ENCRYPT */
188 { AD(ah_props) }, /* POLICY_AUTHENTICATE */
189 { AD(ah_esp_props) }, /* POLICY_ENCRYPT+POLICY_AUTHENTICATE */
190 { AD(compress_props) }, /* POLICY_COMPRESS */
191 { AD(esp_compress_props) }, /* POLICY_ENCRYPT+POLICY_COMPRESS */
192 { AD(ah_compress_props) }, /* POLICY_AUTHENTICATE+POLICY_COMPRESS */
193 { AD(ah_esp_compress_props) }, /* POLICY_ENCRYPT+POLICY_AUTHENTICATE+POLICY_COMPRESS */
194 };
195
196 #undef AD
197 #undef AD_NULL
198
199 /* output an attribute (within an SA) */
200 static bool
201 out_attr(int type
202 , unsigned long val
203 , struct_desc *attr_desc
204 , enum_names **attr_val_descs USED_BY_DEBUG
205 , pb_stream *pbs)
206 {
207 struct isakmp_attribute attr;
208
209 if (val >> 16 == 0)
210 {
211 /* short value: use TV form */
212 attr.isaat_af_type = type | ISAKMP_ATTR_AF_TV;
213 attr.isaat_lv = val;
214 if (!out_struct(&attr, attr_desc, pbs, NULL))
215 return FALSE;
216 }
217 else
218 {
219 /* This is a real fudge! Since we rarely use long attributes
220 * and since this is the only place where we can cause an
221 * ISAKMP message length to be other than a multiple of 4 octets,
222 * we force the length of the value to be a multiple of 4 octets.
223 * Furthermore, we only handle values up to 4 octets in length.
224 * Voila: a fixed format!
225 */
226 pb_stream val_pbs;
227 u_int32_t nval = htonl(val);
228
229 attr.isaat_af_type = type | ISAKMP_ATTR_AF_TLV;
230 if (!out_struct(&attr, attr_desc, pbs, &val_pbs)
231 || !out_raw(&nval, sizeof(nval), &val_pbs, "long attribute value"))
232 return FALSE;
233 close_output_pbs(&val_pbs);
234 }
235 DBG(DBG_EMITTING,
236 enum_names *d = attr_val_descs[type];
237
238 if (d != NULL)
239 DBG_log(" [%lu is %s]"
240 , val, enum_show(d, val)));
241 return TRUE;
242 }
243 #define return_on(var, val) do { var=val;goto return_out; } while(0);
244 /* Output an SA, as described by a db_sa.
245 * This has the side-effect of allocating SPIs for us.
246 */
247 bool
248 out_sa(pb_stream *outs
249 , struct db_sa *sadb
250 , struct state *st
251 , bool oakley_mode
252 , u_int8_t np)
253 {
254 pb_stream sa_pbs;
255 int pcn;
256 bool ret = FALSE;
257 bool ah_spi_generated = FALSE
258 , esp_spi_generated = FALSE
259 , ipcomp_cpi_generated = FALSE;
260 #if !defined NO_KERNEL_ALG || !defined NO_IKE_ALG
261 struct db_context *db_ctx = NULL;
262 #endif
263
264 /* SA header out */
265 {
266 struct isakmp_sa sa;
267
268 sa.isasa_np = np;
269 st->st_doi = sa.isasa_doi = ISAKMP_DOI_IPSEC; /* all we know */
270 if (!out_struct(&sa, &isakmp_sa_desc, outs, &sa_pbs))
271 return_on(ret, FALSE);
272 }
273
274 /* within SA: situation out */
275 st->st_situation = SIT_IDENTITY_ONLY;
276 if (!out_struct(&st->st_situation, &ipsec_sit_desc, &sa_pbs, NULL))
277 return_on(ret, FALSE);
278
279 /* within SA: Proposal Payloads
280 *
281 * Multiple Proposals with the same number are simultaneous
282 * (conjuncts) and must deal with different protocols (AH or ESP).
283 * Proposals with different numbers are alternatives (disjuncts),
284 * in preference order.
285 * Proposal numbers must be monotonic.
286 * See RFC 2408 "ISAKMP" 4.2
287 */
288
289 for (pcn = 0; pcn != sadb->prop_conj_cnt; pcn++)
290 {
291 struct db_prop_conj *pc = &sadb->prop_conjs[pcn];
292 int pn;
293
294 for (pn = 0; pn != pc->prop_cnt; pn++)
295 {
296 struct db_prop *p = &pc->props[pn];
297 pb_stream proposal_pbs;
298 struct isakmp_proposal proposal;
299 struct_desc *trans_desc;
300 struct_desc *attr_desc;
301 enum_names **attr_val_descs;
302 int tn;
303 bool tunnel_mode;
304
305 tunnel_mode = (pn == pc->prop_cnt-1)
306 && (st->st_policy & POLICY_TUNNEL);
307
308 /* Proposal header */
309 proposal.isap_np = pcn == sadb->prop_conj_cnt-1 && pn == pc->prop_cnt-1
310 ? ISAKMP_NEXT_NONE : ISAKMP_NEXT_P;
311 proposal.isap_proposal = pcn;
312 proposal.isap_protoid = p->protoid;
313 proposal.isap_spisize = oakley_mode ? 0
314 : p->protoid == PROTO_IPCOMP ? IPCOMP_CPI_SIZE
315 : IPSEC_DOI_SPI_SIZE;
316
317 /* In quick mode ONLY, create proposal for runtime kernel algos.
318 * Replace ESP proposals with runtime created one
319 */
320 if (!oakley_mode && p->protoid == PROTO_IPSEC_ESP)
321 {
322 DBG(DBG_CONTROL | DBG_CRYPT,
323 if (st->st_connection->alg_info_esp)
324 {
325 static char buf[256]="";
326
327 alg_info_snprint(buf, sizeof (buf),
328 (struct alg_info *)st->st_connection->alg_info_esp);
329 DBG_log(buf);
330 }
331 )
332 db_ctx = kernel_alg_db_new(st->st_connection->alg_info_esp, st->st_policy);
333 p = db_prop_get(db_ctx);
334
335 if (!p || p->trans_cnt == 0)
336 {
337 loglog(RC_LOG_SERIOUS,
338 "empty IPSEC SA proposal to send "
339 "(no kernel algorithms for esp selection)");
340 return_on(ret, FALSE);
341 }
342 }
343
344 if (oakley_mode && p->protoid == PROTO_ISAKMP)
345 {
346 DBG(DBG_CONTROL | DBG_CRYPT,
347 if (st->st_connection->alg_info_ike)
348 {
349 static char buf[256]="";
350
351 alg_info_snprint(buf, sizeof (buf),
352 (struct alg_info *)st->st_connection->alg_info_ike);
353 DBG_log(buf);
354 }
355 )
356 db_ctx = ike_alg_db_new(st->st_connection->alg_info_ike, st->st_policy);
357 p = db_prop_get(db_ctx);
358
359 if (!p || p->trans_cnt == 0)
360 {
361 loglog(RC_LOG_SERIOUS,
362 "empty ISAKMP SA proposal to send "
363 "(no algorithms for ike selection?)");
364 return_on(ret, FALSE);
365 }
366 }
367
368 proposal.isap_notrans = p->trans_cnt;
369 if (!out_struct(&proposal, &isakmp_proposal_desc, &sa_pbs, &proposal_pbs))
370 return_on(ret, FALSE);
371
372 /* Per-protocols stuff:
373 * Set trans_desc.
374 * Set attr_desc.
375 * Set attr_val_descs.
376 * If not oakley_mode, emit SPI.
377 * We allocate SPIs on demand.
378 * All ESPs in an SA will share a single SPI.
379 * All AHs in an SAwill share a single SPI.
380 * AHs' SPI will be distinct from ESPs'.
381 * This latter is needed because KLIPS doesn't
382 * use the protocol when looking up a (dest, protocol, spi).
383 * ??? If multiple ESPs are composed, how should their SPIs
384 * be allocated?
385 */
386 {
387 ipsec_spi_t *spi_ptr = NULL;
388 int proto = 0;
389 bool *spi_generated = NULL;
390
391 switch (p->protoid)
392 {
393 case PROTO_ISAKMP:
394 passert(oakley_mode);
395 trans_desc = &isakmp_isakmp_transform_desc;
396 attr_desc = &isakmp_oakley_attribute_desc;
397 attr_val_descs = oakley_attr_val_descs;
398 /* no SPI needed */
399 break;
400 case PROTO_IPSEC_AH:
401 passert(!oakley_mode);
402 trans_desc = &isakmp_ah_transform_desc;
403 attr_desc = &isakmp_ipsec_attribute_desc;
404 attr_val_descs = ipsec_attr_val_descs;
405 spi_ptr = &st->st_ah.our_spi;
406 spi_generated = &ah_spi_generated;
407 proto = IPPROTO_AH;
408 break;
409 case PROTO_IPSEC_ESP:
410 passert(!oakley_mode);
411 trans_desc = &isakmp_esp_transform_desc;
412 attr_desc = &isakmp_ipsec_attribute_desc;
413 attr_val_descs = ipsec_attr_val_descs;
414 spi_ptr = &st->st_esp.our_spi;
415 spi_generated = &esp_spi_generated;
416 proto = IPPROTO_ESP;
417 break;
418 case PROTO_IPCOMP:
419 passert(!oakley_mode);
420 trans_desc = &isakmp_ipcomp_transform_desc;
421 attr_desc = &isakmp_ipsec_attribute_desc;
422 attr_val_descs = ipsec_attr_val_descs;
423
424 /* a CPI isn't quite the same as an SPI
425 * so we use specialized code to emit it.
426 */
427 if (!ipcomp_cpi_generated)
428 {
429 st->st_ipcomp.our_spi = get_my_cpi(
430 &st->st_connection->spd, tunnel_mode);
431 if (st->st_ipcomp.our_spi == 0)
432 return_on(ret, FALSE); /* problem generating CPI */
433
434 ipcomp_cpi_generated = TRUE;
435 }
436 /* CPI is stored in network low order end of an
437 * ipsec_spi_t. So we start a couple of bytes in.
438 */
439 if (!out_raw((u_char *)&st->st_ipcomp.our_spi
440 + IPSEC_DOI_SPI_SIZE - IPCOMP_CPI_SIZE
441 , IPCOMP_CPI_SIZE
442 , &proposal_pbs, "CPI"))
443 return_on(ret, FALSE);
444 break;
445 default:
446 bad_case(p->protoid);
447 }
448 if (spi_ptr != NULL)
449 {
450 if (!*spi_generated)
451 {
452 *spi_ptr = get_ipsec_spi(0
453 , proto
454 , &st->st_connection->spd
455 , tunnel_mode);
456 if (*spi_ptr == 0)
457 return FALSE;
458 *spi_generated = TRUE;
459 }
460 if (!out_raw((u_char *)spi_ptr, IPSEC_DOI_SPI_SIZE
461 , &proposal_pbs, "SPI"))
462 return_on(ret, FALSE);
463 }
464 }
465
466 /* within proposal: Transform Payloads */
467 for (tn = 0; tn != p->trans_cnt; tn++)
468 {
469 struct db_trans *t = &p->trans[tn];
470 pb_stream trans_pbs;
471 struct isakmp_transform trans;
472 int an;
473
474 trans.isat_np = (tn == p->trans_cnt - 1)
475 ? ISAKMP_NEXT_NONE : ISAKMP_NEXT_T;
476 trans.isat_transnum = tn;
477 trans.isat_transid = t->transid;
478 if (!out_struct(&trans, trans_desc, &proposal_pbs, &trans_pbs))
479 return_on(ret, FALSE);
480
481 /* Within tranform: Attributes. */
482
483 /* For Phase 2 / Quick Mode, GROUP_DESCRIPTION is
484 * automatically generated because it must be the same
485 * in every transform. Except IPCOMP.
486 */
487 if (p->protoid != PROTO_IPCOMP
488 && st->st_pfs_group != NULL)
489 {
490 passert(!oakley_mode);
491 passert(st->st_pfs_group != &unset_group);
492 out_attr(GROUP_DESCRIPTION, st->st_pfs_group->group
493 , attr_desc, attr_val_descs
494 , &trans_pbs);
495 }
496
497 /* automatically generate duration
498 * and, for Phase 2 / Quick Mode, encapsulation.
499 */
500 if (oakley_mode)
501 {
502 out_attr(OAKLEY_LIFE_TYPE, OAKLEY_LIFE_SECONDS
503 , attr_desc, attr_val_descs
504 , &trans_pbs);
505 out_attr(OAKLEY_LIFE_DURATION
506 , st->st_connection->sa_ike_life_seconds
507 , attr_desc, attr_val_descs
508 , &trans_pbs);
509 }
510 else
511 {
512 /* RFC 2407 (IPSEC DOI) 4.5 specifies that
513 * the default is "unspecified (host-dependent)".
514 * This makes little sense, so we always specify it.
515 *
516 * Unlike other IPSEC transforms, IPCOMP defaults
517 * to Transport Mode, so we can exploit the default
518 * (draft-shacham-ippcp-rfc2393bis-05.txt 4.1).
519 */
520 if (p->protoid != PROTO_IPCOMP
521 || st->st_policy & POLICY_TUNNEL)
522 {
523 #ifndef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
524 if ((st->nat_traversal & NAT_T_DETECTED)
525 && !(st->st_policy & POLICY_TUNNEL))
526 {
527 /* Inform user that we will not respect policy and only
528 * propose Tunnel Mode
529 */
530 loglog(RC_LOG_SERIOUS, "NAT-Traversal: "
531 "Transport Mode not allowed due to security concerns -- "
532 "using Tunnel mode");
533 }
534 #endif
535 out_attr(ENCAPSULATION_MODE
536 #ifdef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
537 , NAT_T_ENCAPSULATION_MODE(st, st->st_policy)
538 #else
539 /* If NAT-T is detected, use UDP_TUNNEL as long as Transport
540 * Mode has security concerns.
541 *
542 * User has been informed of that
543 */
544 , NAT_T_ENCAPSULATION_MODE(st, POLICY_TUNNEL)
545 #endif
546 , attr_desc, attr_val_descs
547 , &trans_pbs);
548 }
549 out_attr(SA_LIFE_TYPE, SA_LIFE_TYPE_SECONDS
550 , attr_desc, attr_val_descs
551 , &trans_pbs);
552 out_attr(SA_LIFE_DURATION
553 , st->st_connection->sa_ipsec_life_seconds
554 , attr_desc, attr_val_descs
555 , &trans_pbs);
556 }
557
558 /* spit out attributes from table */
559 for (an = 0; an != t->attr_cnt; an++)
560 {
561 struct db_attr *a = &t->attrs[an];
562
563 out_attr(a->type, a->val
564 , attr_desc, attr_val_descs
565 , &trans_pbs);
566 }
567
568 close_output_pbs(&trans_pbs);
569 }
570 close_output_pbs(&proposal_pbs);
571 }
572 /* end of a conjunction of proposals */
573 }
574 close_output_pbs(&sa_pbs);
575 ret = TRUE;
576
577 return_out:
578
579 #if !defined NO_KERNEL_ALG || !defined NO_IKE_ALG
580 if (db_ctx)
581 db_destroy(db_ctx);
582 #endif
583 return ret;
584 }
585
586 /* Handle long form of duration attribute.
587 * The code is can only handle values that can fit in unsigned long.
588 * "Clamping" is probably an acceptable way to impose this limitation.
589 */
590 static u_int32_t
591 decode_long_duration(pb_stream *pbs)
592 {
593 u_int32_t val = 0;
594
595 /* ignore leading zeros */
596 while (pbs_left(pbs) != 0 && *pbs->cur == '\0')
597 pbs->cur++;
598
599 if (pbs_left(pbs) > sizeof(val))
600 {
601 /* "clamp" too large value to max representable value */
602 val -= 1; /* portable way to get to maximum value */
603 DBG(DBG_PARSING, DBG_log(" too large duration clamped to: %lu"
604 , (unsigned long)val));
605 }
606 else
607 {
608 /* decode number */
609 while (pbs_left(pbs) != 0)
610 val = (val << BITS_PER_BYTE) | *pbs->cur++;
611 DBG(DBG_PARSING, DBG_log(" long duration: %lu", (unsigned long)val));
612 }
613 return val;
614 }
615
616 /* Preparse the body of an ISAKMP SA Payload and
617 * return body of ISAKMP Proposal Payload
618 *
619 * Only IPsec DOI is accepted (what is the ISAKMP DOI?).
620 * Error response is rudimentary.
621 */
622 notification_t
623 preparse_isakmp_sa_body(const struct isakmp_sa *sa
624 , pb_stream *sa_pbs
625 , u_int32_t *ipsecdoisit
626 , pb_stream *proposal_pbs
627 , struct isakmp_proposal *proposal)
628 {
629 /* DOI */
630 if (sa->isasa_doi != ISAKMP_DOI_IPSEC)
631 {
632 loglog(RC_LOG_SERIOUS, "Unknown/unsupported DOI %s", enum_show(&doi_names, sa->isasa_doi));
633 /* XXX Could send notification back */
634 return DOI_NOT_SUPPORTED;
635 }
636
637 /* Situation */
638 if (!in_struct(ipsecdoisit, &ipsec_sit_desc, sa_pbs, NULL))
639 return SITUATION_NOT_SUPPORTED;
640
641 if (*ipsecdoisit != SIT_IDENTITY_ONLY)
642 {
643 loglog(RC_LOG_SERIOUS, "unsupported IPsec DOI situation (%s)"
644 , bitnamesof(sit_bit_names, *ipsecdoisit));
645 /* XXX Could send notification back */
646 return SITUATION_NOT_SUPPORTED;
647 }
648
649 /* The rules for ISAKMP SAs are scattered.
650 * RFC 2409 "IKE" section 5 says that there
651 * can only be one SA, and it can have only one proposal in it.
652 * There may well be multiple transforms.
653 */
654 if (!in_struct(proposal, &isakmp_proposal_desc, sa_pbs, proposal_pbs))
655 return PAYLOAD_MALFORMED;
656
657 if (proposal->isap_np != ISAKMP_NEXT_NONE)
658 {
659 loglog(RC_LOG_SERIOUS, "Proposal Payload must be alone in Oakley SA; found %s following Proposal"
660 , enum_show(&payload_names, proposal->isap_np));
661 return PAYLOAD_MALFORMED;
662 }
663
664 if (proposal->isap_protoid != PROTO_ISAKMP)
665 {
666 loglog(RC_LOG_SERIOUS, "unexpected Protocol ID (%s) found in Oakley Proposal"
667 , enum_show(&protocol_names, proposal->isap_protoid));
668 return INVALID_PROTOCOL_ID;
669 }
670
671 /* Just what should we accept for the SPI field?
672 * The RFC is sort of contradictory. We will ignore the SPI
673 * as long as it is of the proper size.
674 *
675 * From RFC2408 2.4 Identifying Security Associations:
676 * During phase 1 negotiations, the initiator and responder cookies
677 * determine the ISAKMP SA. Therefore, the SPI field in the Proposal
678 * payload is redundant and MAY be set to 0 or it MAY contain the
679 * transmitting entity's cookie.
680 *
681 * From RFC2408 3.5 Proposal Payload:
682 * o SPI Size (1 octet) - Length in octets of the SPI as defined by
683 * the Protocol-Id. In the case of ISAKMP, the Initiator and
684 * Responder cookie pair from the ISAKMP Header is the ISAKMP SPI,
685 * therefore, the SPI Size is irrelevant and MAY be from zero (0) to
686 * sixteen (16). If the SPI Size is non-zero, the content of the
687 * SPI field MUST be ignored. If the SPI Size is not a multiple of
688 * 4 octets it will have some impact on the SPI field and the
689 * alignment of all payloads in the message. The Domain of
690 * Interpretation (DOI) will dictate the SPI Size for other
691 * protocols.
692 */
693 if (proposal->isap_spisize == 0)
694 {
695 /* empty (0) SPI -- fine */
696 }
697 else if (proposal->isap_spisize <= MAX_ISAKMP_SPI_SIZE)
698 {
699 u_char junk_spi[MAX_ISAKMP_SPI_SIZE];
700
701 if (!in_raw(junk_spi, proposal->isap_spisize, proposal_pbs, "Oakley SPI"))
702 return PAYLOAD_MALFORMED;
703 }
704 else
705 {
706 loglog(RC_LOG_SERIOUS, "invalid SPI size (%u) in Oakley Proposal"
707 , (unsigned)proposal->isap_spisize);
708 return INVALID_SPI;
709 }
710 return NOTHING_WRONG;
711 }
712
713 static struct {
714 u_int8_t *start;
715 u_int8_t *cur;
716 u_int8_t *roof;
717 } backup;
718
719 /*
720 * backup the pointer into a pb_stream
721 */
722 void
723 backup_pbs(pb_stream *pbs)
724 {
725 backup.start = pbs->start;
726 backup.cur = pbs->cur;
727 backup.roof = pbs->roof;
728 }
729
730 /*
731 * restore the pointer into a pb_stream
732 */
733 void
734 restore_pbs(pb_stream *pbs)
735 {
736 pbs->start = backup.start;
737 pbs->cur = backup.cur;
738 pbs->roof = backup.roof;
739 }
740
741 /*
742 * Parse an ISAKMP Proposal Payload for RSA and PSK authentication policies
743 */
744 notification_t
745 parse_isakmp_policy(pb_stream *proposal_pbs
746 , u_int notrans
747 , lset_t *policy)
748 {
749 int last_transnum = -1;
750
751 *policy = LEMPTY;
752
753 while (notrans--)
754 {
755 pb_stream trans_pbs;
756 u_char *attr_start;
757 size_t attr_len;
758 struct isakmp_transform trans;
759
760 if (!in_struct(&trans, &isakmp_isakmp_transform_desc, proposal_pbs, &trans_pbs))
761 return BAD_PROPOSAL_SYNTAX;
762
763 if (trans.isat_transnum <= last_transnum)
764 {
765 /* picky, picky, picky */
766 loglog(RC_LOG_SERIOUS, "Transform Numbers are not monotonically increasing"
767 " in Oakley Proposal");
768 return BAD_PROPOSAL_SYNTAX;
769 }
770 last_transnum = trans.isat_transnum;
771
772 if (trans.isat_transid != KEY_IKE)
773 {
774 loglog(RC_LOG_SERIOUS, "expected KEY_IKE but found %s in Oakley Transform"
775 , enum_show(&isakmp_transformid_names, trans.isat_transid));
776 return INVALID_TRANSFORM_ID;
777 }
778
779 attr_start = trans_pbs.cur;
780 attr_len = pbs_left(&trans_pbs);
781
782 /* preprocess authentication attributes only */
783 while (pbs_left(&trans_pbs) != 0)
784 {
785 struct isakmp_attribute a;
786 pb_stream attr_pbs;
787
788 if (!in_struct(&a, &isakmp_oakley_attribute_desc, &trans_pbs, &attr_pbs))
789 return BAD_PROPOSAL_SYNTAX;
790
791 passert((a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK) < 32);
792
793 switch (a.isaat_af_type)
794 {
795 case OAKLEY_AUTHENTICATION_METHOD | ISAKMP_ATTR_AF_TV:
796 switch (a.isaat_lv)
797 {
798 case OAKLEY_PRESHARED_KEY:
799 *policy |= POLICY_PSK;
800 break;
801 case OAKLEY_RSA_SIG:
802 *policy |= POLICY_RSASIG;
803 break;
804 case XAUTHInitPreShared:
805 *policy |= POLICY_XAUTH_SERVER;
806 /* fall through */
807 case XAUTHRespPreShared:
808 *policy |= POLICY_XAUTH_PSK;
809 break;
810 case XAUTHInitRSA:
811 *policy |= POLICY_XAUTH_SERVER;
812 /* fall through */
813 case XAUTHRespRSA:
814 *policy |= POLICY_XAUTH_RSASIG;
815 break;
816 default:
817 break;
818 }
819 break;
820 default:
821 break;
822 }
823 }
824 }
825 DBG(DBG_CONTROL|DBG_PARSING,
826 DBG_log("preparse_isakmp_policy: peer requests %s authentication"
827 , prettypolicy(*policy))
828 )
829 return NOTHING_WRONG;
830 }
831
832 /*
833 * check that we can find a preshared secret
834 */
835 static err_t
836 find_preshared_key(struct state* st)
837 {
838 err_t ugh = NULL;
839 struct connection *c = st->st_connection;
840
841 if (get_preshared_secret(c) == NULL)
842 {
843 char my_id[BUF_LEN], his_id[BUF_LEN];
844
845 idtoa(&c->spd.this.id, my_id, sizeof(my_id));
846 if (his_id_was_instantiated(c))
847 strcpy(his_id, "%any");
848 else
849 idtoa(&c->spd.that.id, his_id, sizeof(his_id));
850 ugh = builddiag("Can't authenticate: no preshared key found for `%s' and `%s'"
851 , my_id, his_id);
852 }
853 return ugh;
854 }
855
856 /* Parse the body of an ISAKMP SA Payload (i.e. Phase 1 / Main Mode).
857 * Various shortcuts are taken. In particular, the policy, such as
858 * it is, is hardwired.
859 *
860 * If r_sa is non-NULL, the body of an SA representing the selected
861 * proposal is emitted.
862 *
863 * This routine is used by main_inI1_outR1() and main_inR1_outI2().
864 */
865 notification_t
866 parse_isakmp_sa_body(u_int32_t ipsecdoisit
867 , pb_stream *proposal_pbs
868 , struct isakmp_proposal *proposal
869 , pb_stream *r_sa_pbs
870 , struct state *st
871 , bool initiator)
872 {
873 struct connection *c = st->st_connection;
874 unsigned no_trans_left;
875
876 /* for each transform payload... */
877 no_trans_left = proposal->isap_notrans;
878
879 for (;;)
880 {
881 pb_stream trans_pbs;
882 u_char *attr_start;
883 size_t attr_len;
884 struct isakmp_transform trans;
885 lset_t seen_attrs = 0;
886 lset_t seen_durations = 0;
887 u_int16_t life_type = 0;
888 struct oakley_trans_attrs ta;
889 err_t ugh = NULL; /* set to diagnostic when problem detected */
890
891 /* initialize only optional field in ta */
892 ta.life_seconds = OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT; /* When this SA expires (seconds) */
893
894 if (no_trans_left == 0)
895 {
896 loglog(RC_LOG_SERIOUS, "number of Transform Payloads disagrees with Oakley Proposal Payload");
897 return BAD_PROPOSAL_SYNTAX;
898 }
899
900 in_struct(&trans, &isakmp_isakmp_transform_desc, proposal_pbs, &trans_pbs);
901 attr_start = trans_pbs.cur;
902 attr_len = pbs_left(&trans_pbs);
903
904 /* process all the attributes that make up the transform */
905
906 while (pbs_left(&trans_pbs) != 0)
907 {
908 struct isakmp_attribute a;
909 pb_stream attr_pbs;
910 u_int32_t val; /* room for larger values */
911
912 if (!in_struct(&a, &isakmp_oakley_attribute_desc, &trans_pbs, &attr_pbs))
913 return BAD_PROPOSAL_SYNTAX;
914
915 passert((a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK) < 32);
916
917 if (LHAS(seen_attrs, a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK))
918 {
919 loglog(RC_LOG_SERIOUS, "repeated %s attribute in Oakley Transform %u"
920 , enum_show(&oakley_attr_names, a.isaat_af_type)
921 , trans.isat_transnum);
922 return BAD_PROPOSAL_SYNTAX;
923 }
924
925 seen_attrs |= LELEM(a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK);
926
927 val = a.isaat_lv;
928
929 DBG(DBG_PARSING,
930 {
931 enum_names *vdesc = oakley_attr_val_descs
932 [a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK];
933
934 if (vdesc != NULL)
935 {
936 const char *nm = enum_name(vdesc, val);
937
938 if (nm != NULL)
939 DBG_log(" [%u is %s]", (unsigned)val, nm);
940 }
941 });
942
943 switch (a.isaat_af_type)
944 {
945 case OAKLEY_ENCRYPTION_ALGORITHM | ISAKMP_ATTR_AF_TV:
946 if (ike_alg_enc_present(val))
947 {
948 ta.encrypt = val;
949 ta.encrypter = ike_alg_get_encrypter(val);
950 ta.enckeylen = ta.encrypter->keydeflen;
951 }
952 else
953 {
954 ugh = builddiag("%s is not supported"
955 , enum_show(&oakley_enc_names, val));
956 }
957 break;
958
959 case OAKLEY_HASH_ALGORITHM | ISAKMP_ATTR_AF_TV:
960 if (ike_alg_hash_present(val))
961 {
962 ta.hash = val;
963 ta.hasher = ike_alg_get_hasher(val);
964 }
965 else
966 {
967 ugh = builddiag("%s is not supported"
968 , enum_show(&oakley_hash_names, val));
969 }
970 break;
971
972 case OAKLEY_AUTHENTICATION_METHOD | ISAKMP_ATTR_AF_TV:
973 {
974 /* check that authentication method is acceptable */
975 lset_t iap = st->st_policy & POLICY_ID_AUTH_MASK;
976
977 /* is the initiator the XAUTH client? */
978 bool xauth_init = ( initiator && (st->st_policy & POLICY_XAUTH_SERVER) == LEMPTY)
979 || (!initiator && (st->st_policy & POLICY_XAUTH_SERVER) != LEMPTY);
980
981 switch (val)
982 {
983 case OAKLEY_PRESHARED_KEY:
984 if ((iap & POLICY_PSK) == LEMPTY)
985 {
986 ugh = "policy does not allow OAKLEY_PRESHARED_KEY authentication";
987 }
988 else
989 {
990 ugh = find_preshared_key(st);
991 ta.auth = OAKLEY_PRESHARED_KEY;
992 }
993 break;
994 case XAUTHInitPreShared:
995 if ((iap & POLICY_XAUTH_PSK) == LEMPTY || !xauth_init)
996 {
997 ugh = "policy does not allow XAUTHInitPreShared authentication";
998 }
999 else
1000 {
1001 ugh = find_preshared_key(st);
1002 ta.auth = XAUTHInitPreShared;
1003 }
1004 break;
1005 case XAUTHRespPreShared:
1006 if ((iap & POLICY_XAUTH_PSK) == LEMPTY || xauth_init)
1007 {
1008 ugh = "policy does not allow XAUTHRespPreShared authentication";
1009 }
1010 else
1011 {
1012 ugh = find_preshared_key(st);
1013 ta.auth = XAUTHRespPreShared;
1014 }
1015 break;
1016 case OAKLEY_RSA_SIG:
1017 /* Accept if policy specifies RSASIG or is default */
1018 if ((iap & POLICY_RSASIG) == LEMPTY)
1019 {
1020 ugh = "policy does not allow OAKLEY_RSA_SIG authentication";
1021 }
1022 else
1023 {
1024 ta.auth = OAKLEY_RSA_SIG;
1025 }
1026 break;
1027 case XAUTHInitRSA:
1028 if ((iap & POLICY_XAUTH_RSASIG) == LEMPTY || !xauth_init)
1029 {
1030 ugh = "policy does not allow XAUTHInitRSA authentication";
1031 }
1032 else
1033 {
1034 ta.auth = XAUTHInitRSA;
1035 }
1036 break;
1037 case XAUTHRespRSA:
1038 if ((iap & POLICY_XAUTH_RSASIG) == LEMPTY || xauth_init)
1039 {
1040 ugh = "policy does not allow XAUTHRespRSA authentication";
1041 }
1042 else
1043 {
1044 ta.auth = XAUTHRespRSA;
1045 }
1046 break;
1047 default:
1048 ugh = builddiag("Pluto does not support %s authentication"
1049 , enum_show(&oakley_auth_names, val));
1050 break;
1051 }
1052 }
1053 break;
1054
1055 case OAKLEY_GROUP_DESCRIPTION | ISAKMP_ATTR_AF_TV:
1056 ta.group = lookup_group(val);
1057 if (ta.group == NULL)
1058 {
1059 ugh = "only OAKLEY_GROUP_MODP1024 and OAKLEY_GROUP_MODP1536 supported";
1060 }
1061 break;
1062
1063 case OAKLEY_LIFE_TYPE | ISAKMP_ATTR_AF_TV:
1064 switch (val)
1065 {
1066 case OAKLEY_LIFE_SECONDS:
1067 case OAKLEY_LIFE_KILOBYTES:
1068 if (LHAS(seen_durations, val))
1069 {
1070 loglog(RC_LOG_SERIOUS
1071 , "attribute OAKLEY_LIFE_TYPE value %s repeated"
1072 , enum_show(&oakley_lifetime_names, val));
1073 return BAD_PROPOSAL_SYNTAX;
1074 }
1075 seen_durations |= LELEM(val);
1076 life_type = val;
1077 break;
1078 default:
1079 ugh = builddiag("unknown value %s"
1080 , enum_show(&oakley_lifetime_names, val));
1081 break;
1082 }
1083 break;
1084
1085 case OAKLEY_LIFE_DURATION | ISAKMP_ATTR_AF_TLV:
1086 val = decode_long_duration(&attr_pbs);
1087 /* fall through */
1088 case OAKLEY_LIFE_DURATION | ISAKMP_ATTR_AF_TV:
1089 if (!LHAS(seen_attrs, OAKLEY_LIFE_TYPE))
1090 {
1091 ugh = "OAKLEY_LIFE_DURATION attribute not preceded by OAKLEY_LIFE_TYPE attribute";
1092 break;
1093 }
1094 seen_attrs &= ~(LELEM(OAKLEY_LIFE_DURATION) | LELEM(OAKLEY_LIFE_TYPE));
1095
1096 switch (life_type)
1097 {
1098 case OAKLEY_LIFE_SECONDS:
1099 if (val > OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM)
1100 {
1101 #ifdef CISCO_QUIRKS
1102 plog("peer requested %lu seconds"
1103 " which exceeds our limit %d seconds"
1104 , (long) val
1105 , OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM);
1106 plog("lifetime reduced to %d seconds "
1107 "(todo: IPSEC_RESPONDER_LIFETIME notification)"
1108 , OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM);
1109 val = OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM;
1110 #else
1111 ugh = builddiag("peer requested %lu seconds"
1112 " which exceeds our limit %d seconds"
1113 , (long) val
1114 , OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM);
1115 #endif
1116 }
1117 ta.life_seconds = val;
1118 break;
1119 case OAKLEY_LIFE_KILOBYTES:
1120 ta.life_kilobytes = val;
1121 break;
1122 default:
1123 bad_case(life_type);
1124 }
1125 break;
1126
1127 case OAKLEY_KEY_LENGTH | ISAKMP_ATTR_AF_TV:
1128 if ((seen_attrs & LELEM(OAKLEY_ENCRYPTION_ALGORITHM)) == 0)
1129 {
1130 ugh = "OAKLEY_KEY_LENGTH attribute not preceded by "
1131 "OAKLEY_ENCRYPTION_ALGORITHM attribute";
1132 break;
1133 }
1134 if (ta.encrypter == NULL)
1135 {
1136 ugh = "NULL encrypter with seen OAKLEY_ENCRYPTION_ALGORITHM";
1137 break;
1138 }
1139 /*
1140 * check if this keylen is compatible with specified algorithm
1141 */
1142 if (val
1143 && (val < ta.encrypter->keyminlen || val > ta.encrypter->keymaxlen))
1144 {
1145 ugh = "peer proposed key length not valid for "
1146 "encryption algorithm specified";
1147 }
1148 ta.enckeylen = val;
1149 break;
1150 #if 0 /* not yet supported */
1151 case OAKLEY_GROUP_TYPE | ISAKMP_ATTR_AF_TV:
1152 case OAKLEY_PRF | ISAKMP_ATTR_AF_TV:
1153 case OAKLEY_FIELD_SIZE | ISAKMP_ATTR_AF_TV:
1154
1155 case OAKLEY_GROUP_PRIME | ISAKMP_ATTR_AF_TV:
1156 case OAKLEY_GROUP_PRIME | ISAKMP_ATTR_AF_TLV:
1157 case OAKLEY_GROUP_GENERATOR_ONE | ISAKMP_ATTR_AF_TV:
1158 case OAKLEY_GROUP_GENERATOR_ONE | ISAKMP_ATTR_AF_TLV:
1159 case OAKLEY_GROUP_GENERATOR_TWO | ISAKMP_ATTR_AF_TV:
1160 case OAKLEY_GROUP_GENERATOR_TWO | ISAKMP_ATTR_AF_TLV:
1161 case OAKLEY_GROUP_CURVE_A | ISAKMP_ATTR_AF_TV:
1162 case OAKLEY_GROUP_CURVE_A | ISAKMP_ATTR_AF_TLV:
1163 case OAKLEY_GROUP_CURVE_B | ISAKMP_ATTR_AF_TV:
1164 case OAKLEY_GROUP_CURVE_B | ISAKMP_ATTR_AF_TLV:
1165 case OAKLEY_GROUP_ORDER | ISAKMP_ATTR_AF_TV:
1166 case OAKLEY_GROUP_ORDER | ISAKMP_ATTR_AF_TLV:
1167 #endif
1168 default:
1169 ugh = "unsupported OAKLEY attribute";
1170 break;
1171 }
1172
1173 if (ugh != NULL)
1174 {
1175 loglog(RC_LOG_SERIOUS, "%s. Attribute %s"
1176 , ugh, enum_show(&oakley_attr_names, a.isaat_af_type));
1177 break;
1178 }
1179 }
1180
1181 /*
1182 * ML: at last check for allowed transforms in alg_info_ike
1183 * (ALG_INFO_F_STRICT flag)
1184 */
1185 if (ugh == NULL)
1186 {
1187 if (!ike_alg_ok_final(ta.encrypt, ta.enckeylen, ta.hash,
1188 ta.group ? ta.group->group : -1, c->alg_info_ike))
1189 {
1190 ugh = "OAKLEY proposal refused";
1191 }
1192 }
1193
1194 if (ugh == NULL)
1195 {
1196 /* a little more checking is in order */
1197 {
1198 lset_t missing
1199 = ~seen_attrs
1200 & (LELEM(OAKLEY_ENCRYPTION_ALGORITHM)
1201 | LELEM(OAKLEY_HASH_ALGORITHM)
1202 | LELEM(OAKLEY_AUTHENTICATION_METHOD)
1203 | LELEM(OAKLEY_GROUP_DESCRIPTION));
1204
1205 if (missing)
1206 {
1207 loglog(RC_LOG_SERIOUS, "missing mandatory attribute(s) %s in Oakley Transform %u"
1208 , bitnamesof(oakley_attr_bit_names, missing)
1209 , trans.isat_transnum);
1210 return BAD_PROPOSAL_SYNTAX;
1211 }
1212 }
1213 /* We must have liked this transform.
1214 * Lets finish early and leave.
1215 */
1216
1217 DBG(DBG_PARSING | DBG_CRYPT
1218 , DBG_log("Oakley Transform %u accepted", trans.isat_transnum));
1219
1220 if (r_sa_pbs != NULL)
1221 {
1222 struct isakmp_proposal r_proposal = *proposal;
1223 pb_stream r_proposal_pbs;
1224 struct isakmp_transform r_trans = trans;
1225 pb_stream r_trans_pbs;
1226
1227 /* Situation */
1228 if (!out_struct(&ipsecdoisit, &ipsec_sit_desc, r_sa_pbs, NULL))
1229 impossible();
1230
1231 /* Proposal */
1232 #ifdef EMIT_ISAKMP_SPI
1233 r_proposal.isap_spisize = COOKIE_SIZE;
1234 #else
1235 r_proposal.isap_spisize = 0;
1236 #endif
1237 r_proposal.isap_notrans = 1;
1238 if (!out_struct(&r_proposal, &isakmp_proposal_desc, r_sa_pbs, &r_proposal_pbs))
1239 impossible();
1240
1241 /* SPI */
1242 #ifdef EMIT_ISAKMP_SPI
1243 if (!out_raw(my_cookie, COOKIE_SIZE, &r_proposal_pbs, "SPI"))
1244 impossible();
1245 r_proposal.isap_spisize = COOKIE_SIZE;
1246 #else
1247 /* none (0) */
1248 #endif
1249
1250 /* Transform */
1251 r_trans.isat_np = ISAKMP_NEXT_NONE;
1252 if (!out_struct(&r_trans, &isakmp_isakmp_transform_desc, &r_proposal_pbs, &r_trans_pbs))
1253 impossible();
1254
1255 if (!out_raw(attr_start, attr_len, &r_trans_pbs, "attributes"))
1256 impossible();
1257 close_output_pbs(&r_trans_pbs);
1258 close_output_pbs(&r_proposal_pbs);
1259 close_output_pbs(r_sa_pbs);
1260 }
1261
1262 /* copy over the results */
1263 st->st_oakley = ta;
1264 return NOTHING_WRONG;
1265 }
1266
1267 /* on to next transform */
1268 no_trans_left--;
1269
1270 if (trans.isat_np == ISAKMP_NEXT_NONE)
1271 {
1272 if (no_trans_left != 0)
1273 {
1274 loglog(RC_LOG_SERIOUS, "number of Transform Payloads disagrees with Oakley Proposal Payload");
1275 return BAD_PROPOSAL_SYNTAX;
1276 }
1277 break;
1278 }
1279 if (trans.isat_np != ISAKMP_NEXT_T)
1280 {
1281 loglog(RC_LOG_SERIOUS, "unexpected %s payload in Oakley Proposal"
1282 , enum_show(&payload_names, proposal->isap_np));
1283 return BAD_PROPOSAL_SYNTAX;
1284 }
1285 }
1286 loglog(RC_LOG_SERIOUS, "no acceptable Oakley Transform");
1287 return NO_PROPOSAL_CHOSEN;
1288 }
1289
1290 /* Parse the body of an IPsec SA Payload (i.e. Phase 2 / Quick Mode).
1291 *
1292 * The main routine is parse_ipsec_sa_body; other functions defined
1293 * between here and there are just helpers.
1294 *
1295 * Various shortcuts are taken. In particular, the policy, such as
1296 * it is, is hardwired.
1297 *
1298 * If r_sa is non-NULL, the body of an SA representing the selected
1299 * proposal is emitted into it.
1300 *
1301 * If "selection" is true, the SA is supposed to represent the
1302 * single tranform that the peer has accepted.
1303 * ??? We only check that it is acceptable, not that it is one that we offered!
1304 *
1305 * Only IPsec DOI is accepted (what is the ISAKMP DOI?).
1306 * Error response is rudimentary.
1307 *
1308 * Since all ISAKMP groups in all SA Payloads must match, st->st_pfs_group
1309 * holds this across multiple payloads.
1310 * &unset_group signifies not yet "set"; NULL signifies NONE.
1311 *
1312 * This routine is used by quick_inI1_outR1() and quick_inR1_outI2().
1313 */
1314
1315 static const struct ipsec_trans_attrs null_ipsec_trans_attrs = {
1316 0, /* transid (NULL, for now) */
1317 0, /* spi */
1318 SA_LIFE_DURATION_DEFAULT, /* life_seconds */
1319 SA_LIFE_DURATION_K_DEFAULT, /* life_kilobytes */
1320 ENCAPSULATION_MODE_UNSPECIFIED, /* encapsulation */
1321 AUTH_ALGORITHM_NONE, /* auth */
1322 0, /* key_len */
1323 0, /* key_rounds */
1324 };
1325
1326 static bool
1327 parse_ipsec_transform(struct isakmp_transform *trans
1328 , struct ipsec_trans_attrs *attrs
1329 , pb_stream *prop_pbs
1330 , pb_stream *trans_pbs
1331 , struct_desc *trans_desc
1332 , int previous_transnum /* or -1 if none */
1333 , bool selection
1334 , bool is_last
1335 , bool is_ipcomp
1336 , struct state *st) /* current state object */
1337 {
1338 lset_t seen_attrs = 0;
1339 lset_t seen_durations = 0;
1340 u_int16_t life_type = 0;
1341 const struct oakley_group_desc *pfs_group = NULL;
1342
1343 if (!in_struct(trans, trans_desc, prop_pbs, trans_pbs))
1344 return FALSE;
1345
1346 if (trans->isat_transnum <= previous_transnum)
1347 {
1348 loglog(RC_LOG_SERIOUS, "Transform Numbers in Proposal are not monotonically increasing");
1349 return FALSE;
1350 }
1351
1352 switch (trans->isat_np)
1353 {
1354 case ISAKMP_NEXT_T:
1355 if (is_last)
1356 {
1357 loglog(RC_LOG_SERIOUS, "Proposal Payload has more Transforms than specified");
1358 return FALSE;
1359 }
1360 break;
1361 case ISAKMP_NEXT_NONE:
1362 if (!is_last)
1363 {
1364 loglog(RC_LOG_SERIOUS, "Proposal Payload has fewer Transforms than specified");
1365 return FALSE;
1366 }
1367 break;
1368 default:
1369 loglog(RC_LOG_SERIOUS, "expecting Transform Payload, but found %s in Proposal"
1370 , enum_show(&payload_names, trans->isat_np));
1371 return FALSE;
1372 }
1373
1374 *attrs = null_ipsec_trans_attrs;
1375 attrs->transid = trans->isat_transid;
1376
1377 while (pbs_left(trans_pbs) != 0)
1378 {
1379 struct isakmp_attribute a;
1380 pb_stream attr_pbs;
1381 enum_names *vdesc;
1382 u_int32_t val; /* room for larger value */
1383 bool ipcomp_inappropriate = is_ipcomp; /* will get reset if OK */
1384
1385 if (!in_struct(&a, &isakmp_ipsec_attribute_desc, trans_pbs, &attr_pbs))
1386 return FALSE;
1387
1388 passert((a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK) < 32);
1389
1390 if (LHAS(seen_attrs, a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK))
1391 {
1392 loglog(RC_LOG_SERIOUS, "repeated %s attribute in IPsec Transform %u"
1393 , enum_show(&ipsec_attr_names, a.isaat_af_type)
1394 , trans->isat_transnum);
1395 return FALSE;
1396 }
1397
1398 seen_attrs |= LELEM(a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK);
1399
1400 val = a.isaat_lv;
1401
1402 vdesc = ipsec_attr_val_descs[a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK];
1403 if (vdesc != NULL)
1404 {
1405 if (enum_name(vdesc, val) == NULL)
1406 {
1407 loglog(RC_LOG_SERIOUS, "invalid value %u for attribute %s in IPsec Transform"
1408 , (unsigned)val, enum_show(&ipsec_attr_names, a.isaat_af_type));
1409 return FALSE;
1410 }
1411 DBG(DBG_PARSING
1412 , if ((a.isaat_af_type & ISAKMP_ATTR_AF_MASK) == ISAKMP_ATTR_AF_TV)
1413 DBG_log(" [%u is %s]"
1414 , (unsigned)val, enum_show(vdesc, val)));
1415 }
1416
1417 switch (a.isaat_af_type)
1418 {
1419 case SA_LIFE_TYPE | ISAKMP_ATTR_AF_TV:
1420 ipcomp_inappropriate = FALSE;
1421 if (LHAS(seen_durations, val))
1422 {
1423 loglog(RC_LOG_SERIOUS, "attribute SA_LIFE_TYPE value %s repeated in message"
1424 , enum_show(&sa_lifetime_names, val));
1425 return FALSE;
1426 }
1427 seen_durations |= LELEM(val);
1428 life_type = val;
1429 break;
1430 case SA_LIFE_DURATION | ISAKMP_ATTR_AF_TLV:
1431 val = decode_long_duration(&attr_pbs);
1432 /* fall through */
1433 case SA_LIFE_DURATION | ISAKMP_ATTR_AF_TV:
1434 ipcomp_inappropriate = FALSE;
1435 if (!LHAS(seen_attrs, SA_LIFE_DURATION))
1436 {
1437 loglog(RC_LOG_SERIOUS, "SA_LIFE_DURATION IPsec attribute not preceded by SA_LIFE_TYPE attribute");
1438 return FALSE;
1439 }
1440 seen_attrs &= ~(LELEM(SA_LIFE_DURATION) | LELEM(SA_LIFE_TYPE));
1441
1442 switch (life_type)
1443 {
1444 case SA_LIFE_TYPE_SECONDS:
1445 /* silently limit duration to our maximum */
1446 attrs->life_seconds = val <= SA_LIFE_DURATION_MAXIMUM
1447 ? val : SA_LIFE_DURATION_MAXIMUM;
1448 break;
1449 case SA_LIFE_TYPE_KBYTES:
1450 attrs->life_kilobytes = val;
1451 break;
1452 default:
1453 bad_case(life_type);
1454 }
1455 break;
1456 case GROUP_DESCRIPTION | ISAKMP_ATTR_AF_TV:
1457 if (is_ipcomp)
1458 {
1459 /* Accept reluctantly. Should not happen, according to
1460 * draft-shacham-ippcp-rfc2393bis-05.txt 4.1.
1461 */
1462 ipcomp_inappropriate = FALSE;
1463 loglog(RC_COMMENT
1464 , "IPCA (IPcomp SA) contains GROUP_DESCRIPTION."
1465 " Ignoring inapproprate attribute.");
1466 }
1467 pfs_group = lookup_group(val);
1468 if (pfs_group == NULL)
1469 {
1470 loglog(RC_LOG_SERIOUS, "only OAKLEY_GROUP_MODP1024 and OAKLEY_GROUP_MODP1536 supported for PFS");
1471 return FALSE;
1472 }
1473 break;
1474 case ENCAPSULATION_MODE | ISAKMP_ATTR_AF_TV:
1475 ipcomp_inappropriate = FALSE;
1476 switch (val)
1477 {
1478 case ENCAPSULATION_MODE_TUNNEL:
1479 case ENCAPSULATION_MODE_TRANSPORT:
1480 if (st->nat_traversal & NAT_T_DETECTED)
1481 {
1482 loglog(RC_LOG_SERIOUS
1483 , "%s must only be used if NAT-Traversal is not detected"
1484 , enum_name(&enc_mode_names, val));
1485 /*
1486 * Accept it anyway because SSH-Sentinel does not
1487 * use UDP_TUNNEL or UDP_TRANSPORT for the diagnostic.
1488 *
1489 * remove when SSH-Sentinel is fixed
1490 */
1491 #ifdef I_DONT_CARE_OF_SSH_SENTINEL
1492 return FALSE;
1493 #endif
1494 }
1495 attrs->encapsulation = val;
1496 break;
1497 case ENCAPSULATION_MODE_UDP_TRANSPORT_DRAFTS:
1498 #ifndef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
1499 loglog(RC_LOG_SERIOUS
1500 , "NAT-Traversal: Transport mode disabled due to security concerns");
1501 return FALSE;
1502 #endif
1503 case ENCAPSULATION_MODE_UDP_TUNNEL_DRAFTS:
1504 if (st->nat_traversal & NAT_T_WITH_RFC_VALUES)
1505 {
1506 loglog(RC_LOG_SERIOUS
1507 , "%s must only be used with old IETF drafts"
1508 , enum_name(&enc_mode_names, val));
1509 return FALSE;
1510 }
1511 else if (st->nat_traversal & NAT_T_DETECTED)
1512 {
1513 attrs->encapsulation = val
1514 - ENCAPSULATION_MODE_UDP_TUNNEL_DRAFTS
1515 + ENCAPSULATION_MODE_TUNNEL;
1516 }
1517 else
1518 {
1519 loglog(RC_LOG_SERIOUS
1520 , "%s must only be used if NAT-Traversal is detected"
1521 , enum_name(&enc_mode_names, val));
1522 return FALSE;
1523 }
1524 break;
1525 case ENCAPSULATION_MODE_UDP_TRANSPORT_RFC:
1526 #ifndef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
1527 loglog(RC_LOG_SERIOUS
1528 , "NAT-Traversal: Transport mode disabled due "
1529 "to security concerns");
1530 return FALSE;
1531 #endif
1532 case ENCAPSULATION_MODE_UDP_TUNNEL_RFC:
1533 if ((st->nat_traversal & NAT_T_DETECTED)
1534 && (st->nat_traversal & NAT_T_WITH_RFC_VALUES))
1535 {
1536 attrs->encapsulation = val
1537 - ENCAPSULATION_MODE_UDP_TUNNEL_RFC
1538 + ENCAPSULATION_MODE_TUNNEL;
1539 }
1540 else if (st->nat_traversal & NAT_T_DETECTED)
1541 {
1542 loglog(RC_LOG_SERIOUS
1543 , "%s must only be used with NAT-T RFC"
1544 , enum_name(&enc_mode_names, val));
1545 return FALSE;
1546 }
1547 else
1548 {
1549 loglog(RC_LOG_SERIOUS
1550 , "%s must only be used if NAT-Traversal is detected"
1551 , enum_name(&enc_mode_names, val));
1552 return FALSE;
1553 }
1554 break;
1555 default:
1556 loglog(RC_LOG_SERIOUS
1557 , "unknown ENCAPSULATION_MODE %d in IPSec SA", val);
1558 return FALSE;
1559 }
1560 break;
1561 case AUTH_ALGORITHM | ISAKMP_ATTR_AF_TV:
1562 attrs->auth = val;
1563 break;
1564 case KEY_LENGTH | ISAKMP_ATTR_AF_TV:
1565 attrs->key_len = val;
1566 break;
1567 case KEY_ROUNDS | ISAKMP_ATTR_AF_TV:
1568 attrs->key_rounds = val;
1569 break;
1570 #if 0 /* not yet implemented */
1571 case COMPRESS_DICT_SIZE | ISAKMP_ATTR_AF_TV:
1572 break;
1573 case COMPRESS_PRIVATE_ALG | ISAKMP_ATTR_AF_TV:
1574 break;
1575
1576 case SA_LIFE_DURATION | ISAKMP_ATTR_AF_TLV:
1577 break;
1578 case COMPRESS_PRIVATE_ALG | ISAKMP_ATTR_AF_TLV:
1579 break;
1580 #endif
1581 default:
1582 loglog(RC_LOG_SERIOUS, "unsupported IPsec attribute %s"
1583 , enum_show(&ipsec_attr_names, a.isaat_af_type));
1584 return FALSE;
1585 }
1586 if (ipcomp_inappropriate)
1587 {
1588 loglog(RC_LOG_SERIOUS, "IPsec attribute %s inappropriate for IPCOMP"
1589 , enum_show(&ipsec_attr_names, a.isaat_af_type));
1590 return FALSE;
1591 }
1592 }
1593
1594 /* Although an IPCOMP SA (IPCA) ought not to have a pfs_group,
1595 * if it does, demand that it be consistent.
1596 * See draft-shacham-ippcp-rfc2393bis-05.txt 4.1.
1597 */
1598 if (!is_ipcomp || pfs_group != NULL)
1599 {
1600 if (st->st_pfs_group == &unset_group)
1601 st->st_pfs_group = pfs_group;
1602
1603 if (st->st_pfs_group != pfs_group)
1604 {
1605 loglog(RC_LOG_SERIOUS, "GROUP_DESCRIPTION inconsistent with that of %s in IPsec SA"
1606 , selection? "the Proposal" : "a previous Transform");
1607 return FALSE;
1608 }
1609 }
1610
1611 if (LHAS(seen_attrs, SA_LIFE_DURATION))
1612 {
1613 loglog(RC_LOG_SERIOUS, "SA_LIFE_TYPE IPsec attribute not followed by SA_LIFE_DURATION attribute in message");
1614 return FALSE;
1615 }
1616
1617 if (!LHAS(seen_attrs, ENCAPSULATION_MODE))
1618 {
1619 if (is_ipcomp)
1620 {
1621 /* draft-shacham-ippcp-rfc2393bis-05.txt 4.1:
1622 * "If the Encapsulation Mode is unspecified,
1623 * the default value of Transport Mode is assumed."
1624 * This contradicts/overrides the DOI (quuoted below).
1625 */
1626 attrs->encapsulation = ENCAPSULATION_MODE_TRANSPORT;
1627 }
1628 else
1629 {
1630 /* ??? Technically, RFC 2407 (IPSEC DOI) 4.5 specifies that
1631 * the default is "unspecified (host-dependent)".
1632 * This makes little sense, so we demand that it be specified.
1633 */
1634 loglog(RC_LOG_SERIOUS, "IPsec Transform must specify ENCAPSULATION_MODE");
1635 return FALSE;
1636 }
1637 }
1638
1639 /* ??? should check for key_len and/or key_rounds if required */
1640
1641 return TRUE;
1642 }
1643
1644 static void
1645 echo_proposal(
1646 struct isakmp_proposal r_proposal, /* proposal to emit */
1647 struct isakmp_transform r_trans, /* winning transformation within it */
1648 u_int8_t np, /* Next Payload for proposal */
1649 pb_stream *r_sa_pbs, /* SA PBS into which to emit */
1650 struct ipsec_proto_info *pi, /* info about this protocol instance */
1651 struct_desc *trans_desc, /* descriptor for this transformation */
1652 pb_stream *trans_pbs, /* PBS for incoming transform */
1653 struct spd_route *sr, /* host details for the association */
1654 bool tunnel_mode) /* true for inner most tunnel SA */
1655 {
1656 pb_stream r_proposal_pbs;
1657 pb_stream r_trans_pbs;
1658
1659 /* Proposal */
1660 r_proposal.isap_np = np;
1661 r_proposal.isap_notrans = 1;
1662 if (!out_struct(&r_proposal, &isakmp_proposal_desc, r_sa_pbs, &r_proposal_pbs))
1663 impossible();
1664
1665 /* allocate and emit our CPI/SPI */
1666 if (r_proposal.isap_protoid == PROTO_IPCOMP)
1667 {
1668 /* CPI is stored in network low order end of an
1669 * ipsec_spi_t. So we start a couple of bytes in.
1670 * Note: we may fail to generate a satisfactory CPI,
1671 * but we'll ignore that.
1672 */
1673 pi->our_spi = get_my_cpi(sr, tunnel_mode);
1674 out_raw((u_char *) &pi->our_spi
1675 + IPSEC_DOI_SPI_SIZE - IPCOMP_CPI_SIZE
1676 , IPCOMP_CPI_SIZE
1677 , &r_proposal_pbs, "CPI");
1678 }
1679 else
1680 {
1681 pi->our_spi = get_ipsec_spi(pi->attrs.spi
1682 , r_proposal.isap_protoid == PROTO_IPSEC_AH ?
1683 IPPROTO_AH : IPPROTO_ESP
1684 , sr
1685 , tunnel_mode);
1686 /* XXX should check for errors */
1687 out_raw((u_char *) &pi->our_spi, IPSEC_DOI_SPI_SIZE
1688 , &r_proposal_pbs, "SPI");
1689 }
1690
1691 /* Transform */
1692 r_trans.isat_np = ISAKMP_NEXT_NONE;
1693 if (!out_struct(&r_trans, trans_desc, &r_proposal_pbs, &r_trans_pbs))
1694 impossible();
1695
1696 /* Transform Attributes: pure echo */
1697 trans_pbs->cur = trans_pbs->start + sizeof(struct isakmp_transform);
1698 if (!out_raw(trans_pbs->cur, pbs_left(trans_pbs)
1699 , &r_trans_pbs, "attributes"))
1700 impossible();
1701
1702 close_output_pbs(&r_trans_pbs);
1703 close_output_pbs(&r_proposal_pbs);
1704 }
1705
1706 notification_t
1707 parse_ipsec_sa_body(
1708 pb_stream *sa_pbs, /* body of input SA Payload */
1709 const struct isakmp_sa *sa, /* header of input SA Payload */
1710 pb_stream *r_sa_pbs, /* if non-NULL, where to emit body of winning SA */
1711 bool selection, /* if this SA is a selection, only one transform may appear */
1712 struct state *st) /* current state object */
1713 {
1714 const struct connection *c = st->st_connection;
1715 u_int32_t ipsecdoisit;
1716 pb_stream next_proposal_pbs;
1717
1718 struct isakmp_proposal next_proposal;
1719 ipsec_spi_t next_spi;
1720
1721 bool next_full = TRUE;
1722
1723 /* DOI */
1724 if (sa->isasa_doi != ISAKMP_DOI_IPSEC)
1725 {
1726 loglog(RC_LOG_SERIOUS, "Unknown or unsupported DOI %s", enum_show(&doi_names, sa->isasa_doi));
1727 /* XXX Could send notification back */
1728 return DOI_NOT_SUPPORTED;
1729 }
1730
1731 /* Situation */
1732 if (!in_struct(&ipsecdoisit, &ipsec_sit_desc, sa_pbs, NULL))
1733 return SITUATION_NOT_SUPPORTED;
1734
1735 if (ipsecdoisit != SIT_IDENTITY_ONLY)
1736 {
1737 loglog(RC_LOG_SERIOUS, "unsupported IPsec DOI situation (%s)"
1738 , bitnamesof(sit_bit_names, ipsecdoisit));
1739 /* XXX Could send notification back */
1740 return SITUATION_NOT_SUPPORTED;
1741 }
1742
1743 /* The rules for IPsec SAs are scattered.
1744 * RFC 2408 "ISAKMP" section 4.2 gives some info.
1745 * There may be multiple proposals. Those with identical proposal
1746 * numbers must be considered as conjuncts. Those with different
1747 * numbers are disjuncts.
1748 * Each proposal may have several transforms, each considered
1749 * an alternative.
1750 * Each transform may have several attributes, all applying.
1751 *
1752 * To handle the way proposals are combined, we need to do a
1753 * look-ahead.
1754 */
1755
1756 if (!in_struct(&next_proposal, &isakmp_proposal_desc, sa_pbs, &next_proposal_pbs))
1757 return BAD_PROPOSAL_SYNTAX;
1758
1759 /* for each conjunction of proposals... */
1760 while (next_full)
1761 {
1762 int propno = next_proposal.isap_proposal;
1763 pb_stream ah_prop_pbs, esp_prop_pbs, ipcomp_prop_pbs;
1764 struct isakmp_proposal ah_proposal, esp_proposal, ipcomp_proposal;
1765 ipsec_spi_t ah_spi = 0;
1766 ipsec_spi_t esp_spi = 0;
1767 ipsec_spi_t ipcomp_cpi = 0;
1768 bool ah_seen = FALSE;
1769 bool esp_seen = FALSE;
1770 bool ipcomp_seen = FALSE;
1771 bool tunnel_mode = FALSE;
1772 int inner_proto = 0;
1773 u_int16_t well_known_cpi = 0;
1774
1775 pb_stream ah_trans_pbs, esp_trans_pbs, ipcomp_trans_pbs;
1776 struct isakmp_transform ah_trans, esp_trans, ipcomp_trans;
1777 struct ipsec_trans_attrs ah_attrs, esp_attrs, ipcomp_attrs;
1778
1779 /* for each proposal in the conjunction */
1780 do {
1781
1782 if (next_proposal.isap_protoid == PROTO_IPCOMP)
1783 {
1784 /* IPCOMP CPI */
1785 if (next_proposal.isap_spisize == IPSEC_DOI_SPI_SIZE)
1786 {
1787 /* This code is to accommodate those peculiar
1788 * implementations that send a CPI in the bottom of an
1789 * SPI-sized field.
1790 * See draft-shacham-ippcp-rfc2393bis-05.txt 4.1
1791 */
1792 u_int8_t filler[IPSEC_DOI_SPI_SIZE - IPCOMP_CPI_SIZE];
1793
1794 if (!in_raw(filler, sizeof(filler)
1795 , &next_proposal_pbs, "CPI filler")
1796 || !all_zero(filler, sizeof(filler)))
1797 return INVALID_SPI;
1798 }
1799 else if (next_proposal.isap_spisize != IPCOMP_CPI_SIZE)
1800 {
1801 loglog(RC_LOG_SERIOUS, "IPsec Proposal with improper CPI size (%u)"
1802 , next_proposal.isap_spisize);
1803 return INVALID_SPI;
1804 }
1805
1806 /* We store CPI in the low order of a network order
1807 * ipsec_spi_t. So we start a couple of bytes in.
1808 */
1809 zero(&next_spi);
1810 if (!in_raw((u_char *)&next_spi
1811 + IPSEC_DOI_SPI_SIZE - IPCOMP_CPI_SIZE
1812 , IPCOMP_CPI_SIZE, &next_proposal_pbs, "CPI"))
1813 return INVALID_SPI;
1814
1815 /* If sanity ruled, CPIs would have to be such that
1816 * the SAID (the triple (CPI, IPCOM, destination IP))
1817 * would be unique, just like for SPIs. But there is a
1818 * perversion where CPIs can be well-known and consequently
1819 * the triple is not unique. We hide this fact from
1820 * ourselves by fudging the top 16 bits to make
1821 * the property true internally!
1822 */
1823 switch (ntohl(next_spi))
1824 {
1825 case IPCOMP_DEFLATE:
1826 well_known_cpi = ntohl(next_spi);
1827 next_spi = uniquify_his_cpi(next_spi, st);
1828 if (next_spi == 0)
1829 {
1830 loglog(RC_LOG_SERIOUS
1831 , "IPsec Proposal contains well-known CPI that I cannot uniquify");
1832 return INVALID_SPI;
1833 }
1834 break;
1835 default:
1836 if (ntohl(next_spi) < IPCOMP_FIRST_NEGOTIATED
1837 || ntohl(next_spi) > IPCOMP_LAST_NEGOTIATED)
1838 {
1839 loglog(RC_LOG_SERIOUS, "IPsec Proposal contains CPI from non-negotiated range (0x%lx)"
1840 , (unsigned long) ntohl(next_spi));
1841 return INVALID_SPI;
1842 }
1843 break;
1844 }
1845 }
1846 else
1847 {
1848 /* AH or ESP SPI */
1849 if (next_proposal.isap_spisize != IPSEC_DOI_SPI_SIZE)
1850 {
1851 loglog(RC_LOG_SERIOUS, "IPsec Proposal with improper SPI size (%u)"
1852 , next_proposal.isap_spisize);
1853 return INVALID_SPI;
1854 }
1855
1856 if (!in_raw((u_char *)&next_spi, sizeof(next_spi), &next_proposal_pbs, "SPI"))
1857 return INVALID_SPI;
1858
1859 /* SPI value 0 is invalid and values 1-255 are reserved to IANA.
1860 * RFC 2402 (ESP) 2.4, RFC 2406 (AH) 2.1
1861 * IPCOMP???
1862 */
1863 if (ntohl(next_spi) < IPSEC_DOI_SPI_MIN)
1864 {
1865 loglog(RC_LOG_SERIOUS, "IPsec Proposal contains invalid SPI (0x%lx)"
1866 , (unsigned long) ntohl(next_spi));
1867 return INVALID_SPI;
1868 }
1869 }
1870
1871 if (next_proposal.isap_notrans == 0)
1872 {
1873 loglog(RC_LOG_SERIOUS, "IPsec Proposal contains no Transforms");
1874 return BAD_PROPOSAL_SYNTAX;
1875 }
1876
1877 switch (next_proposal.isap_protoid)
1878 {
1879 case PROTO_IPSEC_AH:
1880 if (ah_seen)
1881 {
1882 loglog(RC_LOG_SERIOUS, "IPsec SA contains two simultaneous AH Proposals");
1883 return BAD_PROPOSAL_SYNTAX;
1884 }
1885 ah_seen = TRUE;
1886 ah_prop_pbs = next_proposal_pbs;
1887 ah_proposal = next_proposal;
1888 ah_spi = next_spi;
1889 break;
1890
1891 case PROTO_IPSEC_ESP:
1892 if (esp_seen)
1893 {
1894 loglog(RC_LOG_SERIOUS, "IPsec SA contains two simultaneous ESP Proposals");
1895 return BAD_PROPOSAL_SYNTAX;
1896 }
1897 esp_seen = TRUE;
1898 esp_prop_pbs = next_proposal_pbs;
1899 esp_proposal = next_proposal;
1900 esp_spi = next_spi;
1901 break;
1902
1903 case PROTO_IPCOMP:
1904 if (ipcomp_seen)
1905 {
1906 loglog(RC_LOG_SERIOUS, "IPsec SA contains two simultaneous IPCOMP Proposals");
1907 return BAD_PROPOSAL_SYNTAX;
1908 }
1909 ipcomp_seen = TRUE;
1910 ipcomp_prop_pbs = next_proposal_pbs;
1911 ipcomp_proposal = next_proposal;
1912 ipcomp_cpi = next_spi;
1913 break;
1914
1915 default:
1916 loglog(RC_LOG_SERIOUS, "unexpected Protocol ID (%s) in IPsec Proposal"
1917 , enum_show(&protocol_names, next_proposal.isap_protoid));
1918 return INVALID_PROTOCOL_ID;
1919 }
1920
1921 /* refill next_proposal */
1922 if (next_proposal.isap_np == ISAKMP_NEXT_NONE)
1923 {
1924 next_full = FALSE;
1925 break;
1926 }
1927 else if (next_proposal.isap_np != ISAKMP_NEXT_P)
1928 {
1929 loglog(RC_LOG_SERIOUS, "unexpected in Proposal: %s"
1930 , enum_show(&payload_names, next_proposal.isap_np));
1931 return BAD_PROPOSAL_SYNTAX;
1932 }
1933
1934 if (!in_struct(&next_proposal, &isakmp_proposal_desc, sa_pbs, &next_proposal_pbs))
1935 return BAD_PROPOSAL_SYNTAX;
1936 } while (next_proposal.isap_proposal == propno);
1937
1938 /* Now that we have all conjuncts, we should try
1939 * the Cartesian product of eachs tranforms!
1940 * At the moment, we take short-cuts on account of
1941 * our rudimentary hard-wired policy.
1942 * For now, we find an acceptable AH (if any)
1943 * and then an acceptable ESP. The only interaction
1944 * is that the ESP acceptance can know whether there
1945 * was an acceptable AH and hence not require an AUTH.
1946 */
1947
1948 if (ah_seen)
1949 {
1950 int previous_transnum = -1;
1951 int tn;
1952
1953 for (tn = 0; tn != ah_proposal.isap_notrans; tn++)
1954 {
1955 int ok_transid = 0;
1956 bool ok_auth = FALSE;
1957
1958 if (!parse_ipsec_transform(&ah_trans
1959 , &ah_attrs
1960 , &ah_prop_pbs
1961 , &ah_trans_pbs
1962 , &isakmp_ah_transform_desc
1963 , previous_transnum
1964 , selection
1965 , tn == ah_proposal.isap_notrans - 1
1966 , FALSE
1967 , st))
1968 return BAD_PROPOSAL_SYNTAX;
1969
1970 previous_transnum = ah_trans.isat_transnum;
1971
1972 /* we must understand ah_attrs.transid
1973 * COMBINED with ah_attrs.auth.
1974 * See RFC 2407 "IPsec DOI" section 4.4.3
1975 * The following combinations are legal,
1976 * but we don't implement all of them:
1977 * It seems as if each auth algorithm
1978 * only applies to one ah transid.
1979 * AH_MD5, AUTH_ALGORITHM_HMAC_MD5
1980 * AH_MD5, AUTH_ALGORITHM_KPDK (unimplemented)
1981 * AH_SHA, AUTH_ALGORITHM_HMAC_SHA1
1982 * AH_DES, AUTH_ALGORITHM_DES_MAC (unimplemented)
1983 */
1984 switch (ah_attrs.auth)
1985 {
1986 case AUTH_ALGORITHM_NONE:
1987 loglog(RC_LOG_SERIOUS, "AUTH_ALGORITHM attribute missing in AH Transform");
1988 return BAD_PROPOSAL_SYNTAX;
1989
1990 case AUTH_ALGORITHM_HMAC_MD5:
1991 ok_auth = TRUE;
1992 /* fall through */
1993 case AUTH_ALGORITHM_KPDK:
1994 ok_transid = AH_MD5;
1995 break;
1996
1997 case AUTH_ALGORITHM_HMAC_SHA1:
1998 ok_auth = TRUE;
1999 ok_transid = AH_SHA;
2000 break;
2001
2002 case AUTH_ALGORITHM_DES_MAC:
2003 ok_transid = AH_DES;
2004 break;
2005 }
2006 if (ah_attrs.transid != ok_transid)
2007 {
2008 loglog(RC_LOG_SERIOUS, "%s attribute inappropriate in %s Transform"
2009 , enum_name(&auth_alg_names, ah_attrs.auth)
2010 , enum_show(&ah_transformid_names, ah_attrs.transid));
2011 return BAD_PROPOSAL_SYNTAX;
2012 }
2013 if (!ok_auth)
2014 {
2015 DBG(DBG_CONTROL | DBG_CRYPT
2016 , DBG_log("%s attribute unsupported"
2017 " in %s Transform from %s"
2018 , enum_name(&auth_alg_names, ah_attrs.auth)
2019 , enum_show(&ah_transformid_names, ah_attrs.transid)
2020 , ip_str(&c->spd.that.host_addr)));
2021 continue; /* try another */
2022 }
2023 break; /* we seem to be happy */
2024 }
2025 if (tn == ah_proposal.isap_notrans)
2026 continue; /* we didn't find a nice one */
2027 ah_attrs.spi = ah_spi;
2028 inner_proto = IPPROTO_AH;
2029 if (ah_attrs.encapsulation == ENCAPSULATION_MODE_TUNNEL)
2030 tunnel_mode = TRUE;
2031 }
2032
2033 if (esp_seen)
2034 {
2035 int previous_transnum = -1;
2036 int tn;
2037
2038 for (tn = 0; tn != esp_proposal.isap_notrans; tn++)
2039 {
2040 if (!parse_ipsec_transform(&esp_trans
2041 , &esp_attrs
2042 , &esp_prop_pbs
2043 , &esp_trans_pbs
2044 , &isakmp_esp_transform_desc
2045 , previous_transnum
2046 , selection
2047 , tn == esp_proposal.isap_notrans - 1
2048 , FALSE
2049 , st))
2050 return BAD_PROPOSAL_SYNTAX;
2051
2052 previous_transnum = esp_trans.isat_transnum;
2053
2054 /* set default key length for AES encryption */
2055 if (!esp_attrs.key_len && esp_attrs.transid == ESP_AES)
2056 {
2057 esp_attrs.key_len = 128 / BITS_PER_BYTE;
2058 }
2059
2060 if (!kernel_alg_esp_enc_ok(esp_attrs.transid, esp_attrs.key_len
2061 ,c->alg_info_esp))
2062 {
2063 switch (esp_attrs.transid)
2064 {
2065 case ESP_3DES:
2066 break;
2067 #ifdef SUPPORT_ESP_NULL /* should be about as secure as AH-only */
2068 case ESP_NULL:
2069 if (esp_attrs.auth == AUTH_ALGORITHM_NONE)
2070 {
2071 loglog(RC_LOG_SERIOUS, "ESP_NULL requires auth algorithm");
2072 return BAD_PROPOSAL_SYNTAX;
2073 }
2074 if (st->st_policy & POLICY_ENCRYPT)
2075 {
2076 DBG(DBG_CONTROL | DBG_CRYPT
2077 , DBG_log("ESP_NULL Transform Proposal from %s"
2078 " does not satisfy POLICY_ENCRYPT"
2079 , ip_str(&c->spd.that.host_addr)));
2080 continue; /* try another */
2081 }
2082 break;
2083 #endif
2084 default:
2085 DBG(DBG_CONTROL | DBG_CRYPT
2086 , DBG_log("unsupported ESP Transform %s from %s"
2087 , enum_show(&esp_transformid_names, esp_attrs.transid)
2088 , ip_str(&c->spd.that.host_addr)));
2089 continue; /* try another */
2090 }
2091 }
2092
2093 if (!kernel_alg_esp_auth_ok(esp_attrs.auth, c->alg_info_esp))
2094 {
2095 switch (esp_attrs.auth)
2096 {
2097 case AUTH_ALGORITHM_NONE:
2098 if (!ah_seen)
2099 {
2100 DBG(DBG_CONTROL | DBG_CRYPT
2101 , DBG_log("ESP from %s must either have AUTH or be combined with AH"
2102 , ip_str(&c->spd.that.host_addr)));
2103 continue; /* try another */
2104 }
2105 break;
2106 case AUTH_ALGORITHM_HMAC_MD5:
2107 case AUTH_ALGORITHM_HMAC_SHA1:
2108 break;
2109 default:
2110 DBG(DBG_CONTROL | DBG_CRYPT
2111 , DBG_log("unsupported ESP auth alg %s from %s"
2112 , enum_show(&auth_alg_names, esp_attrs.auth)
2113 , ip_str(&c->spd.that.host_addr)));
2114 continue; /* try another */
2115 }
2116 }
2117
2118 /* A last check for allowed transforms in alg_info_esp
2119 * (ALG_INFO_F_STRICT flag)
2120 */
2121 if (!kernel_alg_esp_ok_final(esp_attrs.transid, esp_attrs.key_len
2122 ,esp_attrs.auth, c->alg_info_esp))
2123 {
2124 continue;
2125 }
2126
2127 if (ah_seen && ah_attrs.encapsulation != esp_attrs.encapsulation)
2128 {
2129 /* ??? This should be an error, but is it? */
2130 DBG(DBG_CONTROL | DBG_CRYPT
2131 , DBG_log("AH and ESP transforms disagree about encapsulation; TUNNEL presumed"));
2132 }
2133
2134 break; /* we seem to be happy */
2135 }
2136 if (tn == esp_proposal.isap_notrans)
2137 continue; /* we didn't find a nice one */
2138
2139 esp_attrs.spi = esp_spi;
2140 inner_proto = IPPROTO_ESP;
2141 if (esp_attrs.encapsulation == ENCAPSULATION_MODE_TUNNEL)
2142 tunnel_mode = TRUE;
2143 }
2144 else if (st->st_policy & POLICY_ENCRYPT)
2145 {
2146 DBG(DBG_CONTROL | DBG_CRYPT
2147 , DBG_log("policy for \"%s\" requires encryption but ESP not in Proposal from %s"
2148 , c->name, ip_str(&c->spd.that.host_addr)));
2149 continue; /* we needed encryption, but didn't find ESP */
2150 }
2151 else if ((st->st_policy & POLICY_AUTHENTICATE) && !ah_seen)
2152 {
2153 DBG(DBG_CONTROL | DBG_CRYPT
2154 , DBG_log("policy for \"%s\" requires authentication"
2155 " but none in Proposal from %s"
2156 , c->name, ip_str(&c->spd.that.host_addr)));
2157 continue; /* we need authentication, but we found neither ESP nor AH */
2158 }
2159
2160 if (ipcomp_seen)
2161 {
2162 int previous_transnum = -1;
2163 int tn;
2164
2165 #ifdef NEVER /* we think IPcomp is working now */
2166 /**** FUDGE TO PREVENT UNREQUESTED IPCOMP:
2167 **** NEEDED BECAUSE OUR IPCOMP IS EXPERIMENTAL (UNSTABLE).
2168 ****/
2169 if (!(st->st_policy & POLICY_COMPRESS))
2170 {
2171 plog("compression proposed by %s, but policy for \"%s\" forbids it"
2172 , ip_str(&c->spd.that.host_addr), c->name);
2173 continue; /* unwanted compression proposal */
2174 }
2175 #endif
2176 if (!can_do_IPcomp)
2177 {
2178 plog("compression proposed by %s, but KLIPS is not configured with IPCOMP"
2179 , ip_str(&c->spd.that.host_addr));
2180 continue;
2181 }
2182
2183 if (well_known_cpi != 0 && !ah_seen && !esp_seen)
2184 {
2185 plog("illegal proposal: bare IPCOMP used with well-known CPI");
2186 return BAD_PROPOSAL_SYNTAX;
2187 }
2188
2189 for (tn = 0; tn != ipcomp_proposal.isap_notrans; tn++)
2190 {
2191 if (!parse_ipsec_transform(&ipcomp_trans
2192 , &ipcomp_attrs
2193 , &ipcomp_prop_pbs
2194 , &ipcomp_trans_pbs
2195 , &isakmp_ipcomp_transform_desc
2196 , previous_transnum
2197 , selection
2198 , tn == ipcomp_proposal.isap_notrans - 1
2199 , TRUE
2200 , st))
2201 return BAD_PROPOSAL_SYNTAX;
2202
2203 previous_transnum = ipcomp_trans.isat_transnum;
2204
2205 if (well_known_cpi != 0 && ipcomp_attrs.transid != well_known_cpi)
2206 {
2207 plog("illegal proposal: IPCOMP well-known CPI disagrees with transform");
2208 return BAD_PROPOSAL_SYNTAX;
2209 }
2210
2211 switch (ipcomp_attrs.transid)
2212 {
2213 case IPCOMP_DEFLATE: /* all we can handle! */
2214 break;
2215
2216 default:
2217 DBG(DBG_CONTROL | DBG_CRYPT
2218 , DBG_log("unsupported IPCOMP Transform %s from %s"
2219 , enum_show(&ipcomp_transformid_names, ipcomp_attrs.transid)
2220 , ip_str(&c->spd.that.host_addr)));
2221 continue; /* try another */
2222 }
2223
2224 if (ah_seen && ah_attrs.encapsulation != ipcomp_attrs.encapsulation)
2225 {
2226 /* ??? This should be an error, but is it? */
2227 DBG(DBG_CONTROL | DBG_CRYPT
2228 , DBG_log("AH and IPCOMP transforms disagree about encapsulation; TUNNEL presumed"));
2229 } else if (esp_seen && esp_attrs.encapsulation != ipcomp_attrs.encapsulation)
2230 {
2231 /* ??? This should be an error, but is it? */
2232 DBG(DBG_CONTROL | DBG_CRYPT
2233 , DBG_log("ESP and IPCOMP transforms disagree about encapsulation; TUNNEL presumed"));
2234 }
2235
2236 break; /* we seem to be happy */
2237 }
2238 if (tn == ipcomp_proposal.isap_notrans)
2239 continue; /* we didn't find a nice one */
2240 ipcomp_attrs.spi = ipcomp_cpi;
2241 inner_proto = IPPROTO_COMP;
2242 if (ipcomp_attrs.encapsulation == ENCAPSULATION_MODE_TUNNEL)
2243 tunnel_mode = TRUE;
2244 }
2245
2246 /* Eureka: we liked what we saw -- accept it. */
2247
2248 if (r_sa_pbs != NULL)
2249 {
2250 /* emit what we've accepted */
2251
2252 /* Situation */
2253 if (!out_struct(&ipsecdoisit, &ipsec_sit_desc, r_sa_pbs, NULL))
2254 impossible();
2255
2256 /* AH proposal */
2257 if (ah_seen)
2258 echo_proposal(ah_proposal
2259 , ah_trans
2260 , esp_seen || ipcomp_seen? ISAKMP_NEXT_P : ISAKMP_NEXT_NONE
2261 , r_sa_pbs
2262 , &st->st_ah
2263 , &isakmp_ah_transform_desc
2264 , &ah_trans_pbs
2265 , &st->st_connection->spd
2266 , tunnel_mode && inner_proto == IPPROTO_AH);
2267
2268 /* ESP proposal */
2269 if (esp_seen)
2270 echo_proposal(esp_proposal
2271 , esp_trans
2272 , ipcomp_seen? ISAKMP_NEXT_P : ISAKMP_NEXT_NONE
2273 , r_sa_pbs
2274 , &st->st_esp
2275 , &isakmp_esp_transform_desc
2276 , &esp_trans_pbs
2277 , &st->st_connection->spd
2278 , tunnel_mode && inner_proto == IPPROTO_ESP);
2279
2280 /* IPCOMP proposal */
2281 if (ipcomp_seen)
2282 echo_proposal(ipcomp_proposal
2283 , ipcomp_trans
2284 , ISAKMP_NEXT_NONE
2285 , r_sa_pbs
2286 , &st->st_ipcomp
2287 , &isakmp_ipcomp_transform_desc
2288 , &ipcomp_trans_pbs
2289 , &st->st_connection->spd
2290 , tunnel_mode && inner_proto == IPPROTO_COMP);
2291
2292 close_output_pbs(r_sa_pbs);
2293 }
2294
2295 /* save decoded version of winning SA in state */
2296
2297 st->st_ah.present = ah_seen;
2298 if (ah_seen)
2299 st->st_ah.attrs = ah_attrs;
2300
2301 st->st_esp.present = esp_seen;
2302 if (esp_seen)
2303 st->st_esp.attrs = esp_attrs;
2304
2305 st->st_ipcomp.present = ipcomp_seen;
2306 if (ipcomp_seen)
2307 st->st_ipcomp.attrs = ipcomp_attrs;
2308
2309 return NOTHING_WRONG;
2310 }
2311
2312 loglog(RC_LOG_SERIOUS, "no acceptable Proposal in IPsec SA");
2313 return NO_PROPOSAL_CHOSEN;
2314 }