SKEYID derivation based on libstrongswan
[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
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <sys/queue.h>
22
23 #include <freeswan.h>
24 #include <ipsec_policy.h>
25
26 #include "constants.h"
27 #include "defs.h"
28 #include "id.h"
29 #include "connections.h"
30 #include "state.h"
31 #include "packet.h"
32 #include "keys.h"
33 #include "kernel.h"
34 #include "log.h"
35 #include "spdb.h"
36 #include "whack.h"
37 #include "crypto.h"
38 #include "alg_info.h"
39 #include "kernel_alg.h"
40 #include "ike_alg.h"
41 #include "db_ops.h"
42 #include "nat_traversal.h"
43
44 #define AD(x) x, countof(x) /* Array Description */
45 #define AD_NULL NULL, 0
46
47 /**************** Oakely (main mode) SA database ****************/
48
49 /* array of proposals to be conjoined (can only be one for Oakley) */
50
51 static struct db_prop oakley_pc[] =
52 { { PROTO_ISAKMP, AD_NULL } };
53
54 /* array of proposal conjuncts (can only be one) */
55
56 static struct db_prop_conj oakley_props[] = { { AD(oakley_pc) } };
57
58 /* the sadb entry */
59 struct db_sa oakley_sadb = { AD(oakley_props) };
60
61 /**************** IPsec (quick mode) SA database ****************/
62
63 /* arrays of attributes for transforms */
64
65 static struct db_attr espsha1_attr[] = {
66 { AUTH_ALGORITHM, AUTH_ALGORITHM_HMAC_SHA1 },
67 };
68
69 static struct db_attr ah_HMAC_SHA1_attr[] = {
70 { AUTH_ALGORITHM, AUTH_ALGORITHM_HMAC_SHA1 },
71 };
72
73 /* arrays of transforms, each in in preference order */
74
75 static struct db_trans espa_trans[] = {
76 { ESP_3DES, AD(espsha1_attr) },
77 };
78
79 static struct db_trans esp_trans[] = {
80 { ESP_3DES, AD_NULL },
81 };
82
83 #ifdef SUPPORT_ESP_NULL
84 static struct db_trans espnull_trans[] = {
85 { ESP_NULL, AD(espsha1_attr) },
86 };
87 #endif /* SUPPORT_ESP_NULL */
88
89 static struct db_trans ah_trans[] = {
90 { AH_SHA, AD(ah_HMAC_SHA1_attr) },
91 };
92
93 static struct db_trans ipcomp_trans[] = {
94 { IPCOMP_DEFLATE, AD_NULL },
95 };
96
97 /* arrays of proposals to be conjoined */
98
99 static struct db_prop ah_pc[] = {
100 { PROTO_IPSEC_AH, AD(ah_trans) },
101 };
102
103 #ifdef SUPPORT_ESP_NULL
104 static struct db_prop espnull_pc[] = {
105 { PROTO_IPSEC_ESP, AD(espnull_trans) },
106 };
107 #endif /* SUPPORT_ESP_NULL */
108
109 static struct db_prop esp_pc[] = {
110 { PROTO_IPSEC_ESP, AD(espa_trans) },
111 };
112
113 static struct db_prop ah_esp_pc[] = {
114 { PROTO_IPSEC_AH, AD(ah_trans) },
115 { PROTO_IPSEC_ESP, AD(esp_trans) },
116 };
117
118 static struct db_prop compress_pc[] = {
119 { PROTO_IPCOMP, AD(ipcomp_trans) },
120 };
121
122 static struct db_prop ah_compress_pc[] = {
123 { PROTO_IPSEC_AH, AD(ah_trans) },
124 { PROTO_IPCOMP, AD(ipcomp_trans) },
125 };
126
127 #ifdef SUPPORT_ESP_NULL
128 static struct db_prop espnull_compress_pc[] = {
129 { PROTO_IPSEC_ESP, AD(espnull_trans) },
130 { PROTO_IPCOMP, AD(ipcomp_trans) },
131 };
132 #endif /* SUPPORT_ESP_NULL */
133
134 static struct db_prop esp_compress_pc[] = {
135 { PROTO_IPSEC_ESP, AD(espa_trans) },
136 { PROTO_IPCOMP, AD(ipcomp_trans) },
137 };
138
139 static struct db_prop ah_esp_compress_pc[] = {
140 { PROTO_IPSEC_AH, AD(ah_trans) },
141 { PROTO_IPSEC_ESP, AD(esp_trans) },
142 { PROTO_IPCOMP, AD(ipcomp_trans) },
143 };
144
145 /* arrays of proposal alternatives (each element is a conjunction) */
146
147 static struct db_prop_conj ah_props[] = {
148 { AD(ah_pc) },
149 #ifdef SUPPORT_ESP_NULL
150 { AD(espnull_pc) }
151 #endif
152 };
153
154 static struct db_prop_conj esp_props[] =
155 { { AD(esp_pc) } };
156
157 static struct db_prop_conj ah_esp_props[] =
158 { { AD(ah_esp_pc) } };
159
160 static struct db_prop_conj compress_props[] = {
161 { AD(compress_pc) },
162 };
163
164 static struct db_prop_conj ah_compress_props[] = {
165 { AD(ah_compress_pc) },
166 #ifdef SUPPORT_ESP_NULL
167 { AD(espnull_compress_pc) }
168 #endif
169 };
170
171 static struct db_prop_conj esp_compress_props[] =
172 { { AD(esp_compress_pc) } };
173
174 static struct db_prop_conj ah_esp_compress_props[] =
175 { { AD(ah_esp_compress_pc) } };
176
177 /* The IPsec sadb is subscripted by a bitset (subset of policy)
178 * with members from { POLICY_ENCRYPT, POLICY_AUTHENTICATE, POLICY_COMPRESS }
179 * shifted right by POLICY_IPSEC_SHIFT.
180 */
181 struct db_sa ipsec_sadb[1 << 3] = {
182 { AD_NULL }, /* none */
183 { AD(esp_props) }, /* POLICY_ENCRYPT */
184 { AD(ah_props) }, /* POLICY_AUTHENTICATE */
185 { AD(ah_esp_props) }, /* POLICY_ENCRYPT+POLICY_AUTHENTICATE */
186 { AD(compress_props) }, /* POLICY_COMPRESS */
187 { AD(esp_compress_props) }, /* POLICY_ENCRYPT+POLICY_COMPRESS */
188 { AD(ah_compress_props) }, /* POLICY_AUTHENTICATE+POLICY_COMPRESS */
189 { AD(ah_esp_compress_props) }, /* POLICY_ENCRYPT+POLICY_AUTHENTICATE+POLICY_COMPRESS */
190 };
191
192 #undef AD
193 #undef AD_NULL
194
195 /* output an attribute (within an SA) */
196 static bool
197 out_attr(int type
198 , unsigned long val
199 , struct_desc *attr_desc
200 , enum_names **attr_val_descs USED_BY_DEBUG
201 , pb_stream *pbs)
202 {
203 struct isakmp_attribute attr;
204
205 if (val >> 16 == 0)
206 {
207 /* short value: use TV form */
208 attr.isaat_af_type = type | ISAKMP_ATTR_AF_TV;
209 attr.isaat_lv = val;
210 if (!out_struct(&attr, attr_desc, pbs, NULL))
211 return FALSE;
212 }
213 else
214 {
215 /* This is a real fudge! Since we rarely use long attributes
216 * and since this is the only place where we can cause an
217 * ISAKMP message length to be other than a multiple of 4 octets,
218 * we force the length of the value to be a multiple of 4 octets.
219 * Furthermore, we only handle values up to 4 octets in length.
220 * Voila: a fixed format!
221 */
222 pb_stream val_pbs;
223 u_int32_t nval = htonl(val);
224
225 attr.isaat_af_type = type | ISAKMP_ATTR_AF_TLV;
226 if (!out_struct(&attr, attr_desc, pbs, &val_pbs)
227 || !out_raw(&nval, sizeof(nval), &val_pbs, "long attribute value"))
228 return FALSE;
229 close_output_pbs(&val_pbs);
230 }
231 DBG(DBG_EMITTING,
232 enum_names *d = attr_val_descs[type];
233
234 if (d != NULL)
235 DBG_log(" [%lu is %s]"
236 , val, enum_show(d, val)));
237 return TRUE;
238 }
239 #define return_on(var, val) do { var=val;goto return_out; } while(0);
240 /* Output an SA, as described by a db_sa.
241 * This has the side-effect of allocating SPIs for us.
242 */
243 bool
244 out_sa(pb_stream *outs
245 , struct db_sa *sadb
246 , struct state *st
247 , bool oakley_mode
248 , u_int8_t np)
249 {
250 pb_stream sa_pbs;
251 int pcn;
252 bool ret = FALSE;
253 bool ah_spi_generated = FALSE
254 , esp_spi_generated = FALSE
255 , ipcomp_cpi_generated = FALSE;
256 #if !defined NO_KERNEL_ALG || !defined NO_IKE_ALG
257 struct db_context *db_ctx = NULL;
258 #endif
259
260 /* SA header out */
261 {
262 struct isakmp_sa sa;
263
264 sa.isasa_np = np;
265 st->st_doi = sa.isasa_doi = ISAKMP_DOI_IPSEC; /* all we know */
266 if (!out_struct(&sa, &isakmp_sa_desc, outs, &sa_pbs))
267 return_on(ret, FALSE);
268 }
269
270 /* within SA: situation out */
271 st->st_situation = SIT_IDENTITY_ONLY;
272 if (!out_struct(&st->st_situation, &ipsec_sit_desc, &sa_pbs, NULL))
273 return_on(ret, FALSE);
274
275 /* within SA: Proposal Payloads
276 *
277 * Multiple Proposals with the same number are simultaneous
278 * (conjuncts) and must deal with different protocols (AH or ESP).
279 * Proposals with different numbers are alternatives (disjuncts),
280 * in preference order.
281 * Proposal numbers must be monotonic.
282 * See RFC 2408 "ISAKMP" 4.2
283 */
284
285 for (pcn = 0; pcn != sadb->prop_conj_cnt; pcn++)
286 {
287 struct db_prop_conj *pc = &sadb->prop_conjs[pcn];
288 int pn;
289
290 for (pn = 0; pn != pc->prop_cnt; pn++)
291 {
292 struct db_prop *p = &pc->props[pn];
293 pb_stream proposal_pbs;
294 struct isakmp_proposal proposal;
295 struct_desc *trans_desc = NULL;
296 struct_desc *attr_desc = NULL;
297 enum_names **attr_val_descs = NULL;
298 int tn;
299 bool tunnel_mode;
300
301 tunnel_mode = (pn == pc->prop_cnt-1)
302 && (st->st_policy & POLICY_TUNNEL);
303
304 /* Proposal header */
305 proposal.isap_np = pcn == sadb->prop_conj_cnt-1 && pn == pc->prop_cnt-1
306 ? ISAKMP_NEXT_NONE : ISAKMP_NEXT_P;
307 proposal.isap_proposal = pcn;
308 proposal.isap_protoid = p->protoid;
309 proposal.isap_spisize = oakley_mode ? 0
310 : p->protoid == PROTO_IPCOMP ? IPCOMP_CPI_SIZE
311 : IPSEC_DOI_SPI_SIZE;
312
313 /* In quick mode ONLY, create proposal for runtime kernel algos.
314 * Replace ESP proposals with runtime created one
315 */
316 if (!oakley_mode && p->protoid == PROTO_IPSEC_ESP)
317 {
318 DBG(DBG_CONTROL | DBG_CRYPT,
319 if (st->st_connection->alg_info_esp)
320 {
321 static char buf[256]="";
322
323 alg_info_snprint(buf, sizeof (buf),
324 (struct alg_info *)st->st_connection->alg_info_esp);
325 DBG_log(buf);
326 }
327 )
328 db_ctx = kernel_alg_db_new(st->st_connection->alg_info_esp, st->st_policy);
329 p = db_prop_get(db_ctx);
330
331 if (!p || p->trans_cnt == 0)
332 {
333 loglog(RC_LOG_SERIOUS,
334 "empty IPSEC SA proposal to send "
335 "(no kernel algorithms for esp selection)");
336 return_on(ret, FALSE);
337 }
338 }
339
340 if (oakley_mode && p->protoid == PROTO_ISAKMP)
341 {
342 DBG(DBG_CONTROL | DBG_CRYPT,
343 if (st->st_connection->alg_info_ike)
344 {
345 static char buf[256]="";
346
347 alg_info_snprint(buf, sizeof (buf),
348 (struct alg_info *)st->st_connection->alg_info_ike);
349 DBG_log(buf);
350 }
351 )
352 db_ctx = ike_alg_db_new(st->st_connection->alg_info_ike, st->st_policy);
353 p = db_prop_get(db_ctx);
354
355 if (!p || p->trans_cnt == 0)
356 {
357 loglog(RC_LOG_SERIOUS,
358 "empty ISAKMP SA proposal to send "
359 "(no algorithms for ike selection?)");
360 return_on(ret, FALSE);
361 }
362 }
363
364 proposal.isap_notrans = p->trans_cnt;
365 if (!out_struct(&proposal, &isakmp_proposal_desc, &sa_pbs, &proposal_pbs))
366 return_on(ret, FALSE);
367
368 /* Per-protocols stuff:
369 * Set trans_desc.
370 * Set attr_desc.
371 * Set attr_val_descs.
372 * If not oakley_mode, emit SPI.
373 * We allocate SPIs on demand.
374 * All ESPs in an SA will share a single SPI.
375 * All AHs in an SAwill share a single SPI.
376 * AHs' SPI will be distinct from ESPs'.
377 * This latter is needed because KLIPS doesn't
378 * use the protocol when looking up a (dest, protocol, spi).
379 * ??? If multiple ESPs are composed, how should their SPIs
380 * be allocated?
381 */
382 {
383 ipsec_spi_t *spi_ptr = NULL;
384 int proto = 0;
385 bool *spi_generated = NULL;
386
387 switch (p->protoid)
388 {
389 case PROTO_ISAKMP:
390 passert(oakley_mode);
391 trans_desc = &isakmp_isakmp_transform_desc;
392 attr_desc = &isakmp_oakley_attribute_desc;
393 attr_val_descs = oakley_attr_val_descs;
394 /* no SPI needed */
395 break;
396 case PROTO_IPSEC_AH:
397 passert(!oakley_mode);
398 trans_desc = &isakmp_ah_transform_desc;
399 attr_desc = &isakmp_ipsec_attribute_desc;
400 attr_val_descs = ipsec_attr_val_descs;
401 spi_ptr = &st->st_ah.our_spi;
402 spi_generated = &ah_spi_generated;
403 proto = IPPROTO_AH;
404 break;
405 case PROTO_IPSEC_ESP:
406 passert(!oakley_mode);
407 trans_desc = &isakmp_esp_transform_desc;
408 attr_desc = &isakmp_ipsec_attribute_desc;
409 attr_val_descs = ipsec_attr_val_descs;
410 spi_ptr = &st->st_esp.our_spi;
411 spi_generated = &esp_spi_generated;
412 proto = IPPROTO_ESP;
413 break;
414 case PROTO_IPCOMP:
415 passert(!oakley_mode);
416 trans_desc = &isakmp_ipcomp_transform_desc;
417 attr_desc = &isakmp_ipsec_attribute_desc;
418 attr_val_descs = ipsec_attr_val_descs;
419
420 /* a CPI isn't quite the same as an SPI
421 * so we use specialized code to emit it.
422 */
423 if (!ipcomp_cpi_generated)
424 {
425 st->st_ipcomp.our_spi = get_my_cpi(
426 &st->st_connection->spd, tunnel_mode);
427 if (st->st_ipcomp.our_spi == 0)
428 return_on(ret, FALSE); /* problem generating CPI */
429
430 ipcomp_cpi_generated = TRUE;
431 }
432 /* CPI is stored in network low order end of an
433 * ipsec_spi_t. So we start a couple of bytes in.
434 */
435 if (!out_raw((u_char *)&st->st_ipcomp.our_spi
436 + IPSEC_DOI_SPI_SIZE - IPCOMP_CPI_SIZE
437 , IPCOMP_CPI_SIZE
438 , &proposal_pbs, "CPI"))
439 return_on(ret, FALSE);
440 break;
441 default:
442 bad_case(p->protoid);
443 }
444 if (spi_ptr != NULL)
445 {
446 if (!*spi_generated)
447 {
448 *spi_ptr = get_ipsec_spi(0
449 , proto
450 , &st->st_connection->spd
451 , tunnel_mode);
452 if (*spi_ptr == 0)
453 return FALSE;
454 *spi_generated = TRUE;
455 }
456 if (!out_raw((u_char *)spi_ptr, IPSEC_DOI_SPI_SIZE
457 , &proposal_pbs, "SPI"))
458 return_on(ret, FALSE);
459 }
460 }
461
462 /* within proposal: Transform Payloads */
463 for (tn = 0; tn != p->trans_cnt; tn++)
464 {
465 struct db_trans *t = &p->trans[tn];
466 pb_stream trans_pbs;
467 struct isakmp_transform trans;
468 int an;
469
470 trans.isat_np = (tn == p->trans_cnt - 1)
471 ? ISAKMP_NEXT_NONE : ISAKMP_NEXT_T;
472 trans.isat_transnum = tn;
473 trans.isat_transid = t->transid;
474 if (!out_struct(&trans, trans_desc, &proposal_pbs, &trans_pbs))
475 return_on(ret, FALSE);
476
477 /* Within tranform: Attributes. */
478
479 /* For Phase 2 / Quick Mode, GROUP_DESCRIPTION is
480 * automatically generated because it must be the same
481 * in every transform. Except IPCOMP.
482 */
483 if (p->protoid != PROTO_IPCOMP
484 && st->st_pfs_group != NULL)
485 {
486 passert(!oakley_mode);
487 passert(st->st_pfs_group != &unset_group);
488 out_attr(GROUP_DESCRIPTION, st->st_pfs_group->group
489 , attr_desc, attr_val_descs
490 , &trans_pbs);
491 }
492
493 /* automatically generate duration
494 * and, for Phase 2 / Quick Mode, encapsulation.
495 */
496 if (oakley_mode)
497 {
498 out_attr(OAKLEY_LIFE_TYPE, OAKLEY_LIFE_SECONDS
499 , attr_desc, attr_val_descs
500 , &trans_pbs);
501 out_attr(OAKLEY_LIFE_DURATION
502 , st->st_connection->sa_ike_life_seconds
503 , attr_desc, attr_val_descs
504 , &trans_pbs);
505 }
506 else
507 {
508 /* RFC 2407 (IPSEC DOI) 4.5 specifies that
509 * the default is "unspecified (host-dependent)".
510 * This makes little sense, so we always specify it.
511 *
512 * Unlike other IPSEC transforms, IPCOMP defaults
513 * to Transport Mode, so we can exploit the default
514 * (draft-shacham-ippcp-rfc2393bis-05.txt 4.1).
515 */
516 if (p->protoid != PROTO_IPCOMP
517 || st->st_policy & POLICY_TUNNEL)
518 {
519 #ifndef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
520 if ((st->nat_traversal & NAT_T_DETECTED)
521 && !(st->st_policy & POLICY_TUNNEL))
522 {
523 /* Inform user that we will not respect policy and only
524 * propose Tunnel Mode
525 */
526 loglog(RC_LOG_SERIOUS, "NAT-Traversal: "
527 "Transport Mode not allowed due to security concerns -- "
528 "using Tunnel mode");
529 }
530 #endif
531 out_attr(ENCAPSULATION_MODE
532 #ifdef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
533 , NAT_T_ENCAPSULATION_MODE(st, st->st_policy)
534 #else
535 /* If NAT-T is detected, use UDP_TUNNEL as long as Transport
536 * Mode has security concerns.
537 *
538 * User has been informed of that
539 */
540 , NAT_T_ENCAPSULATION_MODE(st, POLICY_TUNNEL)
541 #endif
542 , attr_desc, attr_val_descs
543 , &trans_pbs);
544 }
545 out_attr(SA_LIFE_TYPE, SA_LIFE_TYPE_SECONDS
546 , attr_desc, attr_val_descs
547 , &trans_pbs);
548 out_attr(SA_LIFE_DURATION
549 , st->st_connection->sa_ipsec_life_seconds
550 , attr_desc, attr_val_descs
551 , &trans_pbs);
552 }
553
554 /* spit out attributes from table */
555 for (an = 0; an != t->attr_cnt; an++)
556 {
557 struct db_attr *a = &t->attrs[an];
558
559 out_attr(a->type, a->val
560 , attr_desc, attr_val_descs
561 , &trans_pbs);
562 }
563
564 close_output_pbs(&trans_pbs);
565 }
566 close_output_pbs(&proposal_pbs);
567 }
568 /* end of a conjunction of proposals */
569 }
570 close_output_pbs(&sa_pbs);
571 ret = TRUE;
572
573 return_out:
574
575 #if !defined NO_KERNEL_ALG || !defined NO_IKE_ALG
576 if (db_ctx)
577 db_destroy(db_ctx);
578 #endif
579 return ret;
580 }
581
582 /* Handle long form of duration attribute.
583 * The code is can only handle values that can fit in unsigned long.
584 * "Clamping" is probably an acceptable way to impose this limitation.
585 */
586 static u_int32_t
587 decode_long_duration(pb_stream *pbs)
588 {
589 u_int32_t val = 0;
590
591 /* ignore leading zeros */
592 while (pbs_left(pbs) != 0 && *pbs->cur == '\0')
593 pbs->cur++;
594
595 if (pbs_left(pbs) > sizeof(val))
596 {
597 /* "clamp" too large value to max representable value */
598 val -= 1; /* portable way to get to maximum value */
599 DBG(DBG_PARSING, DBG_log(" too large duration clamped to: %lu"
600 , (unsigned long)val));
601 }
602 else
603 {
604 /* decode number */
605 while (pbs_left(pbs) != 0)
606 val = (val << BITS_PER_BYTE) | *pbs->cur++;
607 DBG(DBG_PARSING, DBG_log(" long duration: %lu", (unsigned long)val));
608 }
609 return val;
610 }
611
612 /* Preparse the body of an ISAKMP SA Payload and
613 * return body of ISAKMP Proposal Payload
614 *
615 * Only IPsec DOI is accepted (what is the ISAKMP DOI?).
616 * Error response is rudimentary.
617 */
618 notification_t
619 preparse_isakmp_sa_body(const struct isakmp_sa *sa
620 , pb_stream *sa_pbs
621 , u_int32_t *ipsecdoisit
622 , pb_stream *proposal_pbs
623 , struct isakmp_proposal *proposal)
624 {
625 /* DOI */
626 if (sa->isasa_doi != ISAKMP_DOI_IPSEC)
627 {
628 loglog(RC_LOG_SERIOUS, "Unknown/unsupported DOI %s", enum_show(&doi_names, sa->isasa_doi));
629 /* XXX Could send notification back */
630 return DOI_NOT_SUPPORTED;
631 }
632
633 /* Situation */
634 if (!in_struct(ipsecdoisit, &ipsec_sit_desc, sa_pbs, NULL))
635 return SITUATION_NOT_SUPPORTED;
636
637 if (*ipsecdoisit != SIT_IDENTITY_ONLY)
638 {
639 loglog(RC_LOG_SERIOUS, "unsupported IPsec DOI situation (%s)"
640 , bitnamesof(sit_bit_names, *ipsecdoisit));
641 /* XXX Could send notification back */
642 return SITUATION_NOT_SUPPORTED;
643 }
644
645 /* The rules for ISAKMP SAs are scattered.
646 * RFC 2409 "IKE" section 5 says that there
647 * can only be one SA, and it can have only one proposal in it.
648 * There may well be multiple transforms.
649 */
650 if (!in_struct(proposal, &isakmp_proposal_desc, sa_pbs, proposal_pbs))
651 return PAYLOAD_MALFORMED;
652
653 if (proposal->isap_np != ISAKMP_NEXT_NONE)
654 {
655 loglog(RC_LOG_SERIOUS, "Proposal Payload must be alone in Oakley SA; found %s following Proposal"
656 , enum_show(&payload_names, proposal->isap_np));
657 return PAYLOAD_MALFORMED;
658 }
659
660 if (proposal->isap_protoid != PROTO_ISAKMP)
661 {
662 loglog(RC_LOG_SERIOUS, "unexpected Protocol ID (%s) found in Oakley Proposal"
663 , enum_show(&protocol_names, proposal->isap_protoid));
664 return INVALID_PROTOCOL_ID;
665 }
666
667 /* Just what should we accept for the SPI field?
668 * The RFC is sort of contradictory. We will ignore the SPI
669 * as long as it is of the proper size.
670 *
671 * From RFC2408 2.4 Identifying Security Associations:
672 * During phase 1 negotiations, the initiator and responder cookies
673 * determine the ISAKMP SA. Therefore, the SPI field in the Proposal
674 * payload is redundant and MAY be set to 0 or it MAY contain the
675 * transmitting entity's cookie.
676 *
677 * From RFC2408 3.5 Proposal Payload:
678 * o SPI Size (1 octet) - Length in octets of the SPI as defined by
679 * the Protocol-Id. In the case of ISAKMP, the Initiator and
680 * Responder cookie pair from the ISAKMP Header is the ISAKMP SPI,
681 * therefore, the SPI Size is irrelevant and MAY be from zero (0) to
682 * sixteen (16). If the SPI Size is non-zero, the content of the
683 * SPI field MUST be ignored. If the SPI Size is not a multiple of
684 * 4 octets it will have some impact on the SPI field and the
685 * alignment of all payloads in the message. The Domain of
686 * Interpretation (DOI) will dictate the SPI Size for other
687 * protocols.
688 */
689 if (proposal->isap_spisize == 0)
690 {
691 /* empty (0) SPI -- fine */
692 }
693 else if (proposal->isap_spisize <= MAX_ISAKMP_SPI_SIZE)
694 {
695 u_char junk_spi[MAX_ISAKMP_SPI_SIZE];
696
697 if (!in_raw(junk_spi, proposal->isap_spisize, proposal_pbs, "Oakley SPI"))
698 return PAYLOAD_MALFORMED;
699 }
700 else
701 {
702 loglog(RC_LOG_SERIOUS, "invalid SPI size (%u) in Oakley Proposal"
703 , (unsigned)proposal->isap_spisize);
704 return INVALID_SPI;
705 }
706 return NOTHING_WRONG;
707 }
708
709 static struct {
710 u_int8_t *start;
711 u_int8_t *cur;
712 u_int8_t *roof;
713 } backup;
714
715 /*
716 * backup the pointer into a pb_stream
717 */
718 void
719 backup_pbs(pb_stream *pbs)
720 {
721 backup.start = pbs->start;
722 backup.cur = pbs->cur;
723 backup.roof = pbs->roof;
724 }
725
726 /*
727 * restore the pointer into a pb_stream
728 */
729 void
730 restore_pbs(pb_stream *pbs)
731 {
732 pbs->start = backup.start;
733 pbs->cur = backup.cur;
734 pbs->roof = backup.roof;
735 }
736
737 /*
738 * Parse an ISAKMP Proposal Payload for RSA and PSK authentication policies
739 */
740 notification_t
741 parse_isakmp_policy(pb_stream *proposal_pbs
742 , u_int notrans
743 , lset_t *policy)
744 {
745 int last_transnum = -1;
746
747 *policy = LEMPTY;
748
749 while (notrans--)
750 {
751 pb_stream trans_pbs;
752 u_char *attr_start;
753 size_t attr_len;
754 struct isakmp_transform trans;
755
756 if (!in_struct(&trans, &isakmp_isakmp_transform_desc, proposal_pbs, &trans_pbs))
757 return BAD_PROPOSAL_SYNTAX;
758
759 if (trans.isat_transnum <= last_transnum)
760 {
761 /* picky, picky, picky */
762 loglog(RC_LOG_SERIOUS, "Transform Numbers are not monotonically increasing"
763 " in Oakley Proposal");
764 return BAD_PROPOSAL_SYNTAX;
765 }
766 last_transnum = trans.isat_transnum;
767
768 if (trans.isat_transid != KEY_IKE)
769 {
770 loglog(RC_LOG_SERIOUS, "expected KEY_IKE but found %s in Oakley Transform"
771 , enum_show(&isakmp_transformid_names, trans.isat_transid));
772 return INVALID_TRANSFORM_ID;
773 }
774
775 attr_start = trans_pbs.cur;
776 attr_len = pbs_left(&trans_pbs);
777
778 /* preprocess authentication attributes only */
779 while (pbs_left(&trans_pbs) != 0)
780 {
781 struct isakmp_attribute a;
782 pb_stream attr_pbs;
783
784 if (!in_struct(&a, &isakmp_oakley_attribute_desc, &trans_pbs, &attr_pbs))
785 return BAD_PROPOSAL_SYNTAX;
786
787 passert((a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK) < 32);
788
789 switch (a.isaat_af_type)
790 {
791 case OAKLEY_AUTHENTICATION_METHOD | ISAKMP_ATTR_AF_TV:
792 switch (a.isaat_lv)
793 {
794 case OAKLEY_PRESHARED_KEY:
795 *policy |= POLICY_PSK;
796 break;
797 case OAKLEY_RSA_SIG:
798 *policy |= POLICY_RSASIG;
799 break;
800 case XAUTHInitPreShared:
801 *policy |= POLICY_XAUTH_SERVER;
802 /* fall through */
803 case XAUTHRespPreShared:
804 *policy |= POLICY_XAUTH_PSK;
805 break;
806 case XAUTHInitRSA:
807 *policy |= POLICY_XAUTH_SERVER;
808 /* fall through */
809 case XAUTHRespRSA:
810 *policy |= POLICY_XAUTH_RSASIG;
811 break;
812 default:
813 break;
814 }
815 break;
816 default:
817 break;
818 }
819 }
820 }
821 DBG(DBG_CONTROL|DBG_PARSING,
822 DBG_log("preparse_isakmp_policy: peer requests %s authentication"
823 , prettypolicy(*policy))
824 )
825 return NOTHING_WRONG;
826 }
827
828 /*
829 * check that we can find a preshared secret
830 */
831 static err_t
832 find_preshared_key(struct state* st)
833 {
834 err_t ugh = NULL;
835 struct connection *c = st->st_connection;
836
837 if (get_preshared_secret(c) == NULL)
838 {
839 char my_id[BUF_LEN], his_id[BUF_LEN];
840
841 idtoa(&c->spd.this.id, my_id, sizeof(my_id));
842 if (his_id_was_instantiated(c))
843 strcpy(his_id, "%any");
844 else
845 idtoa(&c->spd.that.id, his_id, sizeof(his_id));
846 ugh = builddiag("Can't authenticate: no preshared key found for `%s' and `%s'"
847 , my_id, his_id);
848 }
849 return ugh;
850 }
851
852 /* Parse the body of an ISAKMP SA Payload (i.e. Phase 1 / Main Mode).
853 * Various shortcuts are taken. In particular, the policy, such as
854 * it is, is hardwired.
855 *
856 * If r_sa is non-NULL, the body of an SA representing the selected
857 * proposal is emitted.
858 *
859 * This routine is used by main_inI1_outR1() and main_inR1_outI2().
860 */
861 notification_t
862 parse_isakmp_sa_body(u_int32_t ipsecdoisit
863 , pb_stream *proposal_pbs
864 , struct isakmp_proposal *proposal
865 , pb_stream *r_sa_pbs
866 , struct state *st
867 , bool initiator)
868 {
869 struct connection *c = st->st_connection;
870 unsigned no_trans_left;
871
872 /* for each transform payload... */
873 no_trans_left = proposal->isap_notrans;
874
875 for (;;)
876 {
877 pb_stream trans_pbs;
878 u_char *attr_start;
879 size_t attr_len;
880 struct isakmp_transform trans;
881 lset_t seen_attrs = 0;
882 lset_t seen_durations = 0;
883 u_int16_t life_type = 0;
884 struct oakley_trans_attrs ta;
885 err_t ugh = NULL; /* set to diagnostic when problem detected */
886
887 /* initialize only optional field in ta */
888 ta.life_seconds = OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT; /* When this SA expires (seconds) */
889
890 if (no_trans_left == 0)
891 {
892 loglog(RC_LOG_SERIOUS, "number of Transform Payloads disagrees with Oakley Proposal Payload");
893 return BAD_PROPOSAL_SYNTAX;
894 }
895
896 in_struct(&trans, &isakmp_isakmp_transform_desc, proposal_pbs, &trans_pbs);
897 attr_start = trans_pbs.cur;
898 attr_len = pbs_left(&trans_pbs);
899
900 /* process all the attributes that make up the transform */
901
902 while (pbs_left(&trans_pbs) != 0)
903 {
904 struct isakmp_attribute a;
905 pb_stream attr_pbs;
906 u_int32_t val; /* room for larger values */
907
908 if (!in_struct(&a, &isakmp_oakley_attribute_desc, &trans_pbs, &attr_pbs))
909 return BAD_PROPOSAL_SYNTAX;
910
911 passert((a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK) < 32);
912
913 if (LHAS(seen_attrs, a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK))
914 {
915 loglog(RC_LOG_SERIOUS, "repeated %s attribute in Oakley Transform %u"
916 , enum_show(&oakley_attr_names, a.isaat_af_type)
917 , trans.isat_transnum);
918 return BAD_PROPOSAL_SYNTAX;
919 }
920
921 seen_attrs |= LELEM(a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK);
922
923 val = a.isaat_lv;
924
925 DBG(DBG_PARSING,
926 {
927 enum_names *vdesc = oakley_attr_val_descs
928 [a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK];
929
930 if (vdesc != NULL)
931 {
932 const char *nm = enum_name(vdesc, val);
933
934 if (nm != NULL)
935 DBG_log(" [%u is %s]", (unsigned)val, nm);
936 }
937 });
938
939 switch (a.isaat_af_type)
940 {
941 case OAKLEY_ENCRYPTION_ALGORITHM | ISAKMP_ATTR_AF_TV:
942 if (ike_alg_enc_present(val))
943 {
944 ta.encrypt = val;
945 ta.encrypter = ike_alg_get_encrypter(val);
946 ta.enckeylen = ta.encrypter->keydeflen;
947 }
948 else
949 {
950 ugh = builddiag("%s is not supported"
951 , enum_show(&oakley_enc_names, val));
952 }
953 break;
954
955 case OAKLEY_HASH_ALGORITHM | ISAKMP_ATTR_AF_TV:
956 if (ike_alg_hash_present(val))
957 {
958 ta.hash = val;
959 ta.hasher = ike_alg_get_hasher(val);
960 }
961 else
962 {
963 ugh = builddiag("%s is not supported"
964 , enum_show(&oakley_hash_names, val));
965 }
966 break;
967
968 case OAKLEY_AUTHENTICATION_METHOD | ISAKMP_ATTR_AF_TV:
969 {
970 /* check that authentication method is acceptable */
971 lset_t iap = st->st_policy & POLICY_ID_AUTH_MASK;
972
973 /* is the initiator the XAUTH client? */
974 bool xauth_init = ( initiator && (st->st_policy & POLICY_XAUTH_SERVER) == LEMPTY)
975 || (!initiator && (st->st_policy & POLICY_XAUTH_SERVER) != LEMPTY);
976
977 switch (val)
978 {
979 case OAKLEY_PRESHARED_KEY:
980 if ((iap & POLICY_PSK) == LEMPTY)
981 {
982 ugh = "policy does not allow OAKLEY_PRESHARED_KEY authentication";
983 }
984 else
985 {
986 ugh = find_preshared_key(st);
987 ta.auth = OAKLEY_PRESHARED_KEY;
988 }
989 break;
990 case XAUTHInitPreShared:
991 if ((iap & POLICY_XAUTH_PSK) == LEMPTY || !xauth_init)
992 {
993 ugh = "policy does not allow XAUTHInitPreShared authentication";
994 }
995 else
996 {
997 ugh = find_preshared_key(st);
998 ta.auth = XAUTHInitPreShared;
999 }
1000 break;
1001 case XAUTHRespPreShared:
1002 if ((iap & POLICY_XAUTH_PSK) == LEMPTY || xauth_init)
1003 {
1004 ugh = "policy does not allow XAUTHRespPreShared authentication";
1005 }
1006 else
1007 {
1008 ugh = find_preshared_key(st);
1009 ta.auth = XAUTHRespPreShared;
1010 }
1011 break;
1012 case OAKLEY_RSA_SIG:
1013 /* Accept if policy specifies RSASIG or is default */
1014 if ((iap & POLICY_RSASIG) == LEMPTY)
1015 {
1016 ugh = "policy does not allow OAKLEY_RSA_SIG authentication";
1017 }
1018 else
1019 {
1020 ta.auth = OAKLEY_RSA_SIG;
1021 }
1022 break;
1023 case XAUTHInitRSA:
1024 if ((iap & POLICY_XAUTH_RSASIG) == LEMPTY || !xauth_init)
1025 {
1026 ugh = "policy does not allow XAUTHInitRSA authentication";
1027 }
1028 else
1029 {
1030 ta.auth = XAUTHInitRSA;
1031 }
1032 break;
1033 case XAUTHRespRSA:
1034 if ((iap & POLICY_XAUTH_RSASIG) == LEMPTY || xauth_init)
1035 {
1036 ugh = "policy does not allow XAUTHRespRSA authentication";
1037 }
1038 else
1039 {
1040 ta.auth = XAUTHRespRSA;
1041 }
1042 break;
1043 default:
1044 ugh = builddiag("Pluto does not support %s authentication"
1045 , enum_show(&oakley_auth_names, val));
1046 break;
1047 }
1048 }
1049 break;
1050
1051 case OAKLEY_GROUP_DESCRIPTION | ISAKMP_ATTR_AF_TV:
1052 ta.group = lookup_group(val);
1053 if (ta.group == NULL)
1054 {
1055 ugh = "only OAKLEY_GROUP_MODP1024 and OAKLEY_GROUP_MODP1536 supported";
1056 }
1057 break;
1058
1059 case OAKLEY_LIFE_TYPE | ISAKMP_ATTR_AF_TV:
1060 switch (val)
1061 {
1062 case OAKLEY_LIFE_SECONDS:
1063 case OAKLEY_LIFE_KILOBYTES:
1064 if (LHAS(seen_durations, val))
1065 {
1066 loglog(RC_LOG_SERIOUS
1067 , "attribute OAKLEY_LIFE_TYPE value %s repeated"
1068 , enum_show(&oakley_lifetime_names, val));
1069 return BAD_PROPOSAL_SYNTAX;
1070 }
1071 seen_durations |= LELEM(val);
1072 life_type = val;
1073 break;
1074 default:
1075 ugh = builddiag("unknown value %s"
1076 , enum_show(&oakley_lifetime_names, val));
1077 break;
1078 }
1079 break;
1080
1081 case OAKLEY_LIFE_DURATION | ISAKMP_ATTR_AF_TLV:
1082 val = decode_long_duration(&attr_pbs);
1083 /* fall through */
1084 case OAKLEY_LIFE_DURATION | ISAKMP_ATTR_AF_TV:
1085 if (!LHAS(seen_attrs, OAKLEY_LIFE_TYPE))
1086 {
1087 ugh = "OAKLEY_LIFE_DURATION attribute not preceded by OAKLEY_LIFE_TYPE attribute";
1088 break;
1089 }
1090 seen_attrs &= ~(LELEM(OAKLEY_LIFE_DURATION) | LELEM(OAKLEY_LIFE_TYPE));
1091
1092 switch (life_type)
1093 {
1094 case OAKLEY_LIFE_SECONDS:
1095 if (val > OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM)
1096 {
1097 #ifdef CISCO_QUIRKS
1098 plog("peer requested %lu seconds"
1099 " which exceeds our limit %d seconds"
1100 , (long) val
1101 , OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM);
1102 plog("lifetime reduced to %d seconds "
1103 "(todo: IPSEC_RESPONDER_LIFETIME notification)"
1104 , OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM);
1105 val = OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM;
1106 #else
1107 ugh = builddiag("peer requested %lu seconds"
1108 " which exceeds our limit %d seconds"
1109 , (long) val
1110 , OAKLEY_ISAKMP_SA_LIFETIME_MAXIMUM);
1111 #endif
1112 }
1113 ta.life_seconds = val;
1114 break;
1115 case OAKLEY_LIFE_KILOBYTES:
1116 ta.life_kilobytes = val;
1117 break;
1118 default:
1119 bad_case(life_type);
1120 }
1121 break;
1122
1123 case OAKLEY_KEY_LENGTH | ISAKMP_ATTR_AF_TV:
1124 if ((seen_attrs & LELEM(OAKLEY_ENCRYPTION_ALGORITHM)) == 0)
1125 {
1126 ugh = "OAKLEY_KEY_LENGTH attribute not preceded by "
1127 "OAKLEY_ENCRYPTION_ALGORITHM attribute";
1128 break;
1129 }
1130 if (ta.encrypter == NULL)
1131 {
1132 ugh = "NULL encrypter with seen OAKLEY_ENCRYPTION_ALGORITHM";
1133 break;
1134 }
1135 /*
1136 * check if this keylen is compatible with specified algorithm
1137 */
1138 if (val
1139 && (val < ta.encrypter->keyminlen || val > ta.encrypter->keymaxlen))
1140 {
1141 ugh = "peer proposed key length not valid for "
1142 "encryption algorithm specified";
1143 }
1144 ta.enckeylen = val;
1145 break;
1146 #if 0 /* not yet supported */
1147 case OAKLEY_GROUP_TYPE | ISAKMP_ATTR_AF_TV:
1148 case OAKLEY_PRF | ISAKMP_ATTR_AF_TV:
1149 case OAKLEY_FIELD_SIZE | ISAKMP_ATTR_AF_TV:
1150
1151 case OAKLEY_GROUP_PRIME | ISAKMP_ATTR_AF_TV:
1152 case OAKLEY_GROUP_PRIME | ISAKMP_ATTR_AF_TLV:
1153 case OAKLEY_GROUP_GENERATOR_ONE | ISAKMP_ATTR_AF_TV:
1154 case OAKLEY_GROUP_GENERATOR_ONE | ISAKMP_ATTR_AF_TLV:
1155 case OAKLEY_GROUP_GENERATOR_TWO | ISAKMP_ATTR_AF_TV:
1156 case OAKLEY_GROUP_GENERATOR_TWO | ISAKMP_ATTR_AF_TLV:
1157 case OAKLEY_GROUP_CURVE_A | ISAKMP_ATTR_AF_TV:
1158 case OAKLEY_GROUP_CURVE_A | ISAKMP_ATTR_AF_TLV:
1159 case OAKLEY_GROUP_CURVE_B | ISAKMP_ATTR_AF_TV:
1160 case OAKLEY_GROUP_CURVE_B | ISAKMP_ATTR_AF_TLV:
1161 case OAKLEY_GROUP_ORDER | ISAKMP_ATTR_AF_TV:
1162 case OAKLEY_GROUP_ORDER | ISAKMP_ATTR_AF_TLV:
1163 #endif
1164 default:
1165 /* fix compiler warning */
1166 memset(&ta, 0, sizeof(ta));
1167 ugh = "unsupported OAKLEY attribute";
1168 break;
1169 }
1170
1171 if (ugh != NULL)
1172 {
1173 loglog(RC_LOG_SERIOUS, "%s. Attribute %s"
1174 , ugh, enum_show(&oakley_attr_names, a.isaat_af_type));
1175 break;
1176 }
1177 }
1178
1179 /*
1180 * ML: at last check for allowed transforms in alg_info_ike
1181 * (ALG_INFO_F_STRICT flag)
1182 */
1183 if (ugh == NULL)
1184 {
1185 if (!ike_alg_ok_final(ta.encrypt, ta.enckeylen, ta.hash,
1186 ta.group ? ta.group->group : -1, c->alg_info_ike))
1187 {
1188 ugh = "OAKLEY proposal refused";
1189 }
1190 }
1191
1192 if (ugh == NULL)
1193 {
1194 /* a little more checking is in order */
1195 {
1196 lset_t missing
1197 = ~seen_attrs
1198 & (LELEM(OAKLEY_ENCRYPTION_ALGORITHM)
1199 | LELEM(OAKLEY_HASH_ALGORITHM)
1200 | LELEM(OAKLEY_AUTHENTICATION_METHOD)
1201 | LELEM(OAKLEY_GROUP_DESCRIPTION));
1202
1203 if (missing)
1204 {
1205 loglog(RC_LOG_SERIOUS, "missing mandatory attribute(s) %s in Oakley Transform %u"
1206 , bitnamesof(oakley_attr_bit_names, missing)
1207 , trans.isat_transnum);
1208 return BAD_PROPOSAL_SYNTAX;
1209 }
1210 }
1211 /* We must have liked this transform.
1212 * Lets finish early and leave.
1213 */
1214
1215 DBG(DBG_PARSING | DBG_CRYPT
1216 , DBG_log("Oakley Transform %u accepted", trans.isat_transnum));
1217
1218 if (r_sa_pbs != NULL)
1219 {
1220 struct isakmp_proposal r_proposal = *proposal;
1221 pb_stream r_proposal_pbs;
1222 struct isakmp_transform r_trans = trans;
1223 pb_stream r_trans_pbs;
1224
1225 /* Situation */
1226 if (!out_struct(&ipsecdoisit, &ipsec_sit_desc, r_sa_pbs, NULL))
1227 impossible();
1228
1229 /* Proposal */
1230 #ifdef EMIT_ISAKMP_SPI
1231 r_proposal.isap_spisize = COOKIE_SIZE;
1232 #else
1233 r_proposal.isap_spisize = 0;
1234 #endif
1235 r_proposal.isap_notrans = 1;
1236 if (!out_struct(&r_proposal, &isakmp_proposal_desc, r_sa_pbs, &r_proposal_pbs))
1237 impossible();
1238
1239 /* SPI */
1240 #ifdef EMIT_ISAKMP_SPI
1241 if (!out_raw(my_cookie, COOKIE_SIZE, &r_proposal_pbs, "SPI"))
1242 impossible();
1243 r_proposal.isap_spisize = COOKIE_SIZE;
1244 #else
1245 /* none (0) */
1246 #endif
1247
1248 /* Transform */
1249 r_trans.isat_np = ISAKMP_NEXT_NONE;
1250 if (!out_struct(&r_trans, &isakmp_isakmp_transform_desc, &r_proposal_pbs, &r_trans_pbs))
1251 impossible();
1252
1253 if (!out_raw(attr_start, attr_len, &r_trans_pbs, "attributes"))
1254 impossible();
1255 close_output_pbs(&r_trans_pbs);
1256 close_output_pbs(&r_proposal_pbs);
1257 close_output_pbs(r_sa_pbs);
1258 }
1259
1260 /* copy over the results */
1261 st->st_oakley = ta;
1262 return NOTHING_WRONG;
1263 }
1264
1265 /* on to next transform */
1266 no_trans_left--;
1267
1268 if (trans.isat_np == ISAKMP_NEXT_NONE)
1269 {
1270 if (no_trans_left != 0)
1271 {
1272 loglog(RC_LOG_SERIOUS, "number of Transform Payloads disagrees with Oakley Proposal Payload");
1273 return BAD_PROPOSAL_SYNTAX;
1274 }
1275 break;
1276 }
1277 if (trans.isat_np != ISAKMP_NEXT_T)
1278 {
1279 loglog(RC_LOG_SERIOUS, "unexpected %s payload in Oakley Proposal"
1280 , enum_show(&payload_names, proposal->isap_np));
1281 return BAD_PROPOSAL_SYNTAX;
1282 }
1283 }
1284 loglog(RC_LOG_SERIOUS, "no acceptable Oakley Transform");
1285 return NO_PROPOSAL_CHOSEN;
1286 }
1287
1288 /* Parse the body of an IPsec SA Payload (i.e. Phase 2 / Quick Mode).
1289 *
1290 * The main routine is parse_ipsec_sa_body; other functions defined
1291 * between here and there are just helpers.
1292 *
1293 * Various shortcuts are taken. In particular, the policy, such as
1294 * it is, is hardwired.
1295 *
1296 * If r_sa is non-NULL, the body of an SA representing the selected
1297 * proposal is emitted into it.
1298 *
1299 * If "selection" is true, the SA is supposed to represent the
1300 * single tranform that the peer has accepted.
1301 * ??? We only check that it is acceptable, not that it is one that we offered!
1302 *
1303 * Only IPsec DOI is accepted (what is the ISAKMP DOI?).
1304 * Error response is rudimentary.
1305 *
1306 * Since all ISAKMP groups in all SA Payloads must match, st->st_pfs_group
1307 * holds this across multiple payloads.
1308 * &unset_group signifies not yet "set"; NULL signifies NONE.
1309 *
1310 * This routine is used by quick_inI1_outR1() and quick_inR1_outI2().
1311 */
1312
1313 static const struct ipsec_trans_attrs null_ipsec_trans_attrs = {
1314 0, /* transid (NULL, for now) */
1315 0, /* spi */
1316 SA_LIFE_DURATION_DEFAULT, /* life_seconds */
1317 SA_LIFE_DURATION_K_DEFAULT, /* life_kilobytes */
1318 ENCAPSULATION_MODE_UNSPECIFIED, /* encapsulation */
1319 AUTH_ALGORITHM_NONE, /* auth */
1320 0, /* key_len */
1321 0, /* key_rounds */
1322 };
1323
1324 static bool
1325 parse_ipsec_transform(struct isakmp_transform *trans
1326 , struct ipsec_trans_attrs *attrs
1327 , pb_stream *prop_pbs
1328 , pb_stream *trans_pbs
1329 , struct_desc *trans_desc
1330 , int previous_transnum /* or -1 if none */
1331 , bool selection
1332 , bool is_last
1333 , bool is_ipcomp
1334 , struct state *st) /* current state object */
1335 {
1336 lset_t seen_attrs = 0;
1337 lset_t seen_durations = 0;
1338 u_int16_t life_type = 0;
1339 const struct oakley_group_desc *pfs_group = NULL;
1340
1341 if (!in_struct(trans, trans_desc, prop_pbs, trans_pbs))
1342 return FALSE;
1343
1344 if (trans->isat_transnum <= previous_transnum)
1345 {
1346 loglog(RC_LOG_SERIOUS, "Transform Numbers in Proposal are not monotonically increasing");
1347 return FALSE;
1348 }
1349
1350 switch (trans->isat_np)
1351 {
1352 case ISAKMP_NEXT_T:
1353 if (is_last)
1354 {
1355 loglog(RC_LOG_SERIOUS, "Proposal Payload has more Transforms than specified");
1356 return FALSE;
1357 }
1358 break;
1359 case ISAKMP_NEXT_NONE:
1360 if (!is_last)
1361 {
1362 loglog(RC_LOG_SERIOUS, "Proposal Payload has fewer Transforms than specified");
1363 return FALSE;
1364 }
1365 break;
1366 default:
1367 loglog(RC_LOG_SERIOUS, "expecting Transform Payload, but found %s in Proposal"
1368 , enum_show(&payload_names, trans->isat_np));
1369 return FALSE;
1370 }
1371
1372 *attrs = null_ipsec_trans_attrs;
1373 attrs->transid = trans->isat_transid;
1374
1375 while (pbs_left(trans_pbs) != 0)
1376 {
1377 struct isakmp_attribute a;
1378 pb_stream attr_pbs;
1379 enum_names *vdesc;
1380 u_int32_t val; /* room for larger value */
1381 bool ipcomp_inappropriate = is_ipcomp; /* will get reset if OK */
1382
1383 if (!in_struct(&a, &isakmp_ipsec_attribute_desc, trans_pbs, &attr_pbs))
1384 return FALSE;
1385
1386 passert((a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK) < 32);
1387
1388 if (LHAS(seen_attrs, a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK))
1389 {
1390 loglog(RC_LOG_SERIOUS, "repeated %s attribute in IPsec Transform %u"
1391 , enum_show(&ipsec_attr_names, a.isaat_af_type)
1392 , trans->isat_transnum);
1393 return FALSE;
1394 }
1395
1396 seen_attrs |= LELEM(a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK);
1397
1398 val = a.isaat_lv;
1399
1400 vdesc = ipsec_attr_val_descs[a.isaat_af_type & ISAKMP_ATTR_RTYPE_MASK];
1401 if (vdesc != NULL)
1402 {
1403 if (enum_name(vdesc, val) == NULL)
1404 {
1405 loglog(RC_LOG_SERIOUS, "invalid value %u for attribute %s in IPsec Transform"
1406 , (unsigned)val, enum_show(&ipsec_attr_names, a.isaat_af_type));
1407 return FALSE;
1408 }
1409 DBG(DBG_PARSING
1410 , if ((a.isaat_af_type & ISAKMP_ATTR_AF_MASK) == ISAKMP_ATTR_AF_TV)
1411 DBG_log(" [%u is %s]"
1412 , (unsigned)val, enum_show(vdesc, val)));
1413 }
1414
1415 switch (a.isaat_af_type)
1416 {
1417 case SA_LIFE_TYPE | ISAKMP_ATTR_AF_TV:
1418 ipcomp_inappropriate = FALSE;
1419 if (LHAS(seen_durations, val))
1420 {
1421 loglog(RC_LOG_SERIOUS, "attribute SA_LIFE_TYPE value %s repeated in message"
1422 , enum_show(&sa_lifetime_names, val));
1423 return FALSE;
1424 }
1425 seen_durations |= LELEM(val);
1426 life_type = val;
1427 break;
1428 case SA_LIFE_DURATION | ISAKMP_ATTR_AF_TLV:
1429 val = decode_long_duration(&attr_pbs);
1430 /* fall through */
1431 case SA_LIFE_DURATION | ISAKMP_ATTR_AF_TV:
1432 ipcomp_inappropriate = FALSE;
1433 if (!LHAS(seen_attrs, SA_LIFE_DURATION))
1434 {
1435 loglog(RC_LOG_SERIOUS, "SA_LIFE_DURATION IPsec attribute not preceded by SA_LIFE_TYPE attribute");
1436 return FALSE;
1437 }
1438 seen_attrs &= ~(LELEM(SA_LIFE_DURATION) | LELEM(SA_LIFE_TYPE));
1439
1440 switch (life_type)
1441 {
1442 case SA_LIFE_TYPE_SECONDS:
1443 /* silently limit duration to our maximum */
1444 attrs->life_seconds = val <= SA_LIFE_DURATION_MAXIMUM
1445 ? val : SA_LIFE_DURATION_MAXIMUM;
1446 break;
1447 case SA_LIFE_TYPE_KBYTES:
1448 attrs->life_kilobytes = val;
1449 break;
1450 default:
1451 bad_case(life_type);
1452 }
1453 break;
1454 case GROUP_DESCRIPTION | ISAKMP_ATTR_AF_TV:
1455 if (is_ipcomp)
1456 {
1457 /* Accept reluctantly. Should not happen, according to
1458 * draft-shacham-ippcp-rfc2393bis-05.txt 4.1.
1459 */
1460 ipcomp_inappropriate = FALSE;
1461 loglog(RC_COMMENT
1462 , "IPCA (IPcomp SA) contains GROUP_DESCRIPTION."
1463 " Ignoring inapproprate attribute.");
1464 }
1465 pfs_group = lookup_group(val);
1466 if (pfs_group == NULL)
1467 {
1468 loglog(RC_LOG_SERIOUS, "only OAKLEY_GROUP_MODP1024 and OAKLEY_GROUP_MODP1536 supported for PFS");
1469 return FALSE;
1470 }
1471 break;
1472 case ENCAPSULATION_MODE | ISAKMP_ATTR_AF_TV:
1473 ipcomp_inappropriate = FALSE;
1474 switch (val)
1475 {
1476 case ENCAPSULATION_MODE_TUNNEL:
1477 case ENCAPSULATION_MODE_TRANSPORT:
1478 if (st->nat_traversal & NAT_T_DETECTED)
1479 {
1480 loglog(RC_LOG_SERIOUS
1481 , "%s must only be used if NAT-Traversal is not detected"
1482 , enum_name(&enc_mode_names, val));
1483 /*
1484 * Accept it anyway because SSH-Sentinel does not
1485 * use UDP_TUNNEL or UDP_TRANSPORT for the diagnostic.
1486 *
1487 * remove when SSH-Sentinel is fixed
1488 */
1489 #ifdef I_DONT_CARE_OF_SSH_SENTINEL
1490 return FALSE;
1491 #endif
1492 }
1493 attrs->encapsulation = val;
1494 break;
1495 case ENCAPSULATION_MODE_UDP_TRANSPORT_DRAFTS:
1496 #ifndef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
1497 loglog(RC_LOG_SERIOUS
1498 , "NAT-Traversal: Transport mode disabled due to security concerns");
1499 return FALSE;
1500 #endif
1501 case ENCAPSULATION_MODE_UDP_TUNNEL_DRAFTS:
1502 if (st->nat_traversal & NAT_T_WITH_RFC_VALUES)
1503 {
1504 loglog(RC_LOG_SERIOUS
1505 , "%s must only be used with old IETF drafts"
1506 , enum_name(&enc_mode_names, val));
1507 return FALSE;
1508 }
1509 else if (st->nat_traversal & NAT_T_DETECTED)
1510 {
1511 attrs->encapsulation = val
1512 - ENCAPSULATION_MODE_UDP_TUNNEL_DRAFTS
1513 + ENCAPSULATION_MODE_TUNNEL;
1514 }
1515 else
1516 {
1517 loglog(RC_LOG_SERIOUS
1518 , "%s must only be used if NAT-Traversal is detected"
1519 , enum_name(&enc_mode_names, val));
1520 return FALSE;
1521 }
1522 break;
1523 case ENCAPSULATION_MODE_UDP_TRANSPORT_RFC:
1524 #ifndef I_KNOW_TRANSPORT_MODE_HAS_SECURITY_CONCERN_BUT_I_WANT_IT
1525 loglog(RC_LOG_SERIOUS
1526 , "NAT-Traversal: Transport mode disabled due "
1527 "to security concerns");
1528 return FALSE;
1529 #endif
1530 case ENCAPSULATION_MODE_UDP_TUNNEL_RFC:
1531 if ((st->nat_traversal & NAT_T_DETECTED)
1532 && (st->nat_traversal & NAT_T_WITH_RFC_VALUES))
1533 {
1534 attrs->encapsulation = val
1535 - ENCAPSULATION_MODE_UDP_TUNNEL_RFC
1536 + ENCAPSULATION_MODE_TUNNEL;
1537 }
1538 else if (st->nat_traversal & NAT_T_DETECTED)
1539 {
1540 loglog(RC_LOG_SERIOUS
1541 , "%s must only be used with NAT-T RFC"
1542 , enum_name(&enc_mode_names, val));
1543 return FALSE;
1544 }
1545 else
1546 {
1547 loglog(RC_LOG_SERIOUS
1548 , "%s must only be used if NAT-Traversal is detected"
1549 , enum_name(&enc_mode_names, val));
1550 return FALSE;
1551 }
1552 break;
1553 default:
1554 loglog(RC_LOG_SERIOUS
1555 , "unknown ENCAPSULATION_MODE %d in IPSec SA", val);
1556 return FALSE;
1557 }
1558 break;
1559 case AUTH_ALGORITHM | ISAKMP_ATTR_AF_TV:
1560 attrs->auth = val;
1561 break;
1562 case KEY_LENGTH | ISAKMP_ATTR_AF_TV:
1563 attrs->key_len = val;
1564 break;
1565 case KEY_ROUNDS | ISAKMP_ATTR_AF_TV:
1566 attrs->key_rounds = val;
1567 break;
1568 #if 0 /* not yet implemented */
1569 case COMPRESS_DICT_SIZE | ISAKMP_ATTR_AF_TV:
1570 break;
1571 case COMPRESS_PRIVATE_ALG | ISAKMP_ATTR_AF_TV:
1572 break;
1573
1574 case SA_LIFE_DURATION | ISAKMP_ATTR_AF_TLV:
1575 break;
1576 case COMPRESS_PRIVATE_ALG | ISAKMP_ATTR_AF_TLV:
1577 break;
1578 #endif
1579 default:
1580 loglog(RC_LOG_SERIOUS, "unsupported IPsec attribute %s"
1581 , enum_show(&ipsec_attr_names, a.isaat_af_type));
1582 return FALSE;
1583 }
1584 if (ipcomp_inappropriate)
1585 {
1586 loglog(RC_LOG_SERIOUS, "IPsec attribute %s inappropriate for IPCOMP"
1587 , enum_show(&ipsec_attr_names, a.isaat_af_type));
1588 return FALSE;
1589 }
1590 }
1591
1592 /* Although an IPCOMP SA (IPCA) ought not to have a pfs_group,
1593 * if it does, demand that it be consistent.
1594 * See draft-shacham-ippcp-rfc2393bis-05.txt 4.1.
1595 */
1596 if (!is_ipcomp || pfs_group != NULL)
1597 {
1598 if (st->st_pfs_group == &unset_group)
1599 st->st_pfs_group = pfs_group;
1600
1601 if (st->st_pfs_group != pfs_group)
1602 {
1603 loglog(RC_LOG_SERIOUS, "GROUP_DESCRIPTION inconsistent with that of %s in IPsec SA"
1604 , selection? "the Proposal" : "a previous Transform");
1605 return FALSE;
1606 }
1607 }
1608
1609 if (LHAS(seen_attrs, SA_LIFE_DURATION))
1610 {
1611 loglog(RC_LOG_SERIOUS, "SA_LIFE_TYPE IPsec attribute not followed by SA_LIFE_DURATION attribute in message");
1612 return FALSE;
1613 }
1614
1615 if (!LHAS(seen_attrs, ENCAPSULATION_MODE))
1616 {
1617 if (is_ipcomp)
1618 {
1619 /* draft-shacham-ippcp-rfc2393bis-05.txt 4.1:
1620 * "If the Encapsulation Mode is unspecified,
1621 * the default value of Transport Mode is assumed."
1622 * This contradicts/overrides the DOI (quuoted below).
1623 */
1624 attrs->encapsulation = ENCAPSULATION_MODE_TRANSPORT;
1625 }
1626 else
1627 {
1628 /* ??? Technically, RFC 2407 (IPSEC DOI) 4.5 specifies that
1629 * the default is "unspecified (host-dependent)".
1630 * This makes little sense, so we demand that it be specified.
1631 */
1632 loglog(RC_LOG_SERIOUS, "IPsec Transform must specify ENCAPSULATION_MODE");
1633 return FALSE;
1634 }
1635 }
1636
1637 /* ??? should check for key_len and/or key_rounds if required */
1638
1639 return TRUE;
1640 }
1641
1642 static void
1643 echo_proposal(
1644 struct isakmp_proposal r_proposal, /* proposal to emit */
1645 struct isakmp_transform r_trans, /* winning transformation within it */
1646 u_int8_t np, /* Next Payload for proposal */
1647 pb_stream *r_sa_pbs, /* SA PBS into which to emit */
1648 struct ipsec_proto_info *pi, /* info about this protocol instance */
1649 struct_desc *trans_desc, /* descriptor for this transformation */
1650 pb_stream *trans_pbs, /* PBS for incoming transform */
1651 struct spd_route *sr, /* host details for the association */
1652 bool tunnel_mode) /* true for inner most tunnel SA */
1653 {
1654 pb_stream r_proposal_pbs;
1655 pb_stream r_trans_pbs;
1656
1657 /* Proposal */
1658 r_proposal.isap_np = np;
1659 r_proposal.isap_notrans = 1;
1660 if (!out_struct(&r_proposal, &isakmp_proposal_desc, r_sa_pbs, &r_proposal_pbs))
1661 impossible();
1662
1663 /* allocate and emit our CPI/SPI */
1664 if (r_proposal.isap_protoid == PROTO_IPCOMP)
1665 {
1666 /* CPI is stored in network low order end of an
1667 * ipsec_spi_t. So we start a couple of bytes in.
1668 * Note: we may fail to generate a satisfactory CPI,
1669 * but we'll ignore that.
1670 */
1671 pi->our_spi = get_my_cpi(sr, tunnel_mode);
1672 out_raw((u_char *) &pi->our_spi
1673 + IPSEC_DOI_SPI_SIZE - IPCOMP_CPI_SIZE
1674 , IPCOMP_CPI_SIZE
1675 , &r_proposal_pbs, "CPI");
1676 }
1677 else
1678 {
1679 pi->our_spi = get_ipsec_spi(pi->attrs.spi
1680 , r_proposal.isap_protoid == PROTO_IPSEC_AH ?
1681 IPPROTO_AH : IPPROTO_ESP
1682 , sr
1683 , tunnel_mode);
1684 /* XXX should check for errors */
1685 out_raw((u_char *) &pi->our_spi, IPSEC_DOI_SPI_SIZE
1686 , &r_proposal_pbs, "SPI");
1687 }
1688
1689 /* Transform */
1690 r_trans.isat_np = ISAKMP_NEXT_NONE;
1691 if (!out_struct(&r_trans, trans_desc, &r_proposal_pbs, &r_trans_pbs))
1692 impossible();
1693
1694 /* Transform Attributes: pure echo */
1695 trans_pbs->cur = trans_pbs->start + sizeof(struct isakmp_transform);
1696 if (!out_raw(trans_pbs->cur, pbs_left(trans_pbs)
1697 , &r_trans_pbs, "attributes"))
1698 impossible();
1699
1700 close_output_pbs(&r_trans_pbs);
1701 close_output_pbs(&r_proposal_pbs);
1702 }
1703
1704 notification_t
1705 parse_ipsec_sa_body(
1706 pb_stream *sa_pbs, /* body of input SA Payload */
1707 const struct isakmp_sa *sa, /* header of input SA Payload */
1708 pb_stream *r_sa_pbs, /* if non-NULL, where to emit body of winning SA */
1709 bool selection, /* if this SA is a selection, only one transform may appear */
1710 struct state *st) /* current state object */
1711 {
1712 const struct connection *c = st->st_connection;
1713 u_int32_t ipsecdoisit;
1714 pb_stream next_proposal_pbs;
1715
1716 struct isakmp_proposal next_proposal;
1717 ipsec_spi_t next_spi;
1718
1719 bool next_full = TRUE;
1720
1721 /* DOI */
1722 if (sa->isasa_doi != ISAKMP_DOI_IPSEC)
1723 {
1724 loglog(RC_LOG_SERIOUS, "Unknown or unsupported DOI %s", enum_show(&doi_names, sa->isasa_doi));
1725 /* XXX Could send notification back */
1726 return DOI_NOT_SUPPORTED;
1727 }
1728
1729 /* Situation */
1730 if (!in_struct(&ipsecdoisit, &ipsec_sit_desc, sa_pbs, NULL))
1731 return SITUATION_NOT_SUPPORTED;
1732
1733 if (ipsecdoisit != SIT_IDENTITY_ONLY)
1734 {
1735 loglog(RC_LOG_SERIOUS, "unsupported IPsec DOI situation (%s)"
1736 , bitnamesof(sit_bit_names, ipsecdoisit));
1737 /* XXX Could send notification back */
1738 return SITUATION_NOT_SUPPORTED;
1739 }
1740
1741 /* The rules for IPsec SAs are scattered.
1742 * RFC 2408 "ISAKMP" section 4.2 gives some info.
1743 * There may be multiple proposals. Those with identical proposal
1744 * numbers must be considered as conjuncts. Those with different
1745 * numbers are disjuncts.
1746 * Each proposal may have several transforms, each considered
1747 * an alternative.
1748 * Each transform may have several attributes, all applying.
1749 *
1750 * To handle the way proposals are combined, we need to do a
1751 * look-ahead.
1752 */
1753
1754 if (!in_struct(&next_proposal, &isakmp_proposal_desc, sa_pbs, &next_proposal_pbs))
1755 return BAD_PROPOSAL_SYNTAX;
1756
1757 /* for each conjunction of proposals... */
1758 while (next_full)
1759 {
1760 int propno = next_proposal.isap_proposal;
1761 pb_stream ah_prop_pbs, esp_prop_pbs, ipcomp_prop_pbs;
1762 struct isakmp_proposal ah_proposal = {0, 0, 0, 0, 0, 0, 0};
1763 struct isakmp_proposal esp_proposal = {0, 0, 0, 0, 0, 0, 0};
1764 struct isakmp_proposal ipcomp_proposal = {0, 0, 0, 0, 0, 0, 0};
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 */
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 }