1 /* IPsec DOI and Oakley resolution routines
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2002 D. Hugh Redelmeier.
4 * Copyright (C) 2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
26 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
27 #include <sys/queue.h>
32 #include <asn1/asn1.h>
33 #include <crypto/hashers/hasher.h>
34 #include <crypto/prfs/prf.h>
35 #include <crypto/rngs/rng.h>
36 #include <credentials/keys/private_key.h>
37 #include <credentials/keys/public_key.h>
38 #include <utils/identification.h>
40 #include "constants.h"
49 #include "smartcard.h"
50 #include "connections.h"
53 #include "demux.h" /* needs packet.h */
54 #include "adns.h" /* needs <resolv.h> */
55 #include "dnskey.h" /* needs keys.h and adns.h */
62 #include "ipsec_doi.h" /* needs demux.h and state.h */
70 #include "kernel_alg.h"
71 #include "nat_traversal.h"
75 * are we sending Pluto's Vendor ID?
78 #define SEND_PLUTO_VID 1
80 #define SEND_PLUTO_VID 0
81 #endif /* !VENDORID */
84 * are we sending an XAUTH VID?
87 #define SEND_XAUTH_VID 1
88 #else /* !XAUTH_VID */
89 #define SEND_XAUTH_VID 0
90 #endif /* !XAUTH_VID */
93 * are we sending a Cisco Unity VID?
96 #define SEND_CISCO_UNITY_VID 1
97 #else /* !CISCO_QUIRKS */
98 #define SEND_CISCO_UNITY_VID 0
99 #endif /* !CISCO_QUIRKS */
101 /* MAGIC: perform f, a function that returns notification_t
102 * and return from the ENCLOSING stf_status returning function if it fails.
104 #define RETURN_STF_FAILURE(f) \
105 { int r = (f); if (r != ISAKMP_NOTHING_WRONG) return STF_FAIL + r; }
107 /* create output HDR as replica of input HDR */
108 void echo_hdr(struct msg_digest
*md
, bool enc
, u_int8_t np
)
110 struct isakmp_hdr r_hdr
= md
->hdr
; /* mostly same as incoming header */
112 r_hdr
.isa_flags
&= ~ISAKMP_FLAG_COMMIT
; /* we won't ever turn on this bit */
115 r_hdr
.isa_flags
|= ISAKMP_FLAG_ENCRYPTION
;
117 /* some day, we may have to set r_hdr.isa_version */
119 if (!out_struct(&r_hdr
, &isakmp_hdr_desc
, &md
->reply
, &md
->rbody
))
121 impossible(); /* surely must have room and be well-formed */
125 /* Compute DH shared secret from our local secret and the peer's public value.
126 * We make the leap that the length should be that of the group
127 * (see quoted passage at start of ACCEPT_KE).
129 static void compute_dh_shared(struct state
*st
, const chunk_t g
)
132 st
->st_dh
->set_other_public_value(st
->st_dh
, g
);
133 st
->st_dh
->get_shared_secret(st
->st_dh
, &st
->st_shared
);
134 DBG_cond_dump_chunk(DBG_CRYPT
, "DH shared secret:\n", st
->st_shared
);
137 /* if we haven't already done so, compute a local DH secret (st->st_sec) and
138 * the corresponding public value (g). This is emitted as a KE payload.
140 static bool build_and_ship_KE(struct state
*st
, chunk_t
*g
,
141 const struct dh_desc
*group
,
142 pb_stream
*outs
, u_int8_t np
)
144 if (st
->st_dh
== NULL
)
146 st
->st_dh
= lib
->crypto
->create_dh(lib
->crypto
, group
->algo_id
);
147 if (st
->st_dh
== NULL
)
149 plog("Diffie Hellman group %N is not available",
150 diffie_hellman_group_names
, group
->algo_id
);
154 st
->st_dh
->get_my_public_value(st
->st_dh
, g
);
156 DBG_dump_chunk("Public DH value sent:\n", *g
)
158 return out_generic_chunk(np
, &isakmp_keyex_desc
, outs
, *g
, "keyex value");
163 * Check and accept DH public value (Gi or Gr) from peer's message.
164 * According to RFC2409 "The Internet key exchange (IKE)" 5:
165 * The Diffie-Hellman public value passed in a KE payload, in either
166 * a phase 1 or phase 2 exchange, MUST be the length of the negotiated
167 * Diffie-Hellman group enforced, if necessary, by pre-pending the
170 static notification_t
accept_KE(chunk_t
*dest
, const char *val_name
,
171 const struct dh_desc
*gr
,
174 if (pbs_left(pbs
) != gr
->ke_size
)
176 loglog(RC_LOG_SERIOUS
, "KE has %u byte DH public value; %u required"
177 , (unsigned) pbs_left(pbs
), gr
->ke_size
);
178 /* XXX Could send notification back */
179 return ISAKMP_INVALID_KEY_INFORMATION
;
182 *dest
= chunk_create(pbs
->cur
, pbs_left(pbs
));
183 *dest
= chunk_clone(*dest
);
184 DBG_cond_dump_chunk(DBG_CRYPT
, "DH public value received:\n", *dest
);
185 return ISAKMP_NOTHING_WRONG
;
190 * Check and accept optional Quick Mode KE payload for PFS.
191 * Extends ACCEPT_PFS to check whether KE is allowed or required.
193 static notification_t
accept_PFS_KE(struct msg_digest
*md
, chunk_t
*dest
,
194 const char *val_name
, const char *msg_name
)
196 struct state
*st
= md
->st
;
197 struct payload_digest
*const ke_pd
= md
->chain
[ISAKMP_NEXT_KE
];
201 if (st
->st_pfs_group
!= NULL
)
203 loglog(RC_LOG_SERIOUS
, "missing KE payload in %s message", msg_name
);
204 return ISAKMP_INVALID_KEY_INFORMATION
;
209 if (st
->st_pfs_group
== NULL
)
211 loglog(RC_LOG_SERIOUS
, "%s message KE payload requires a GROUP_DESCRIPTION attribute in SA"
213 return ISAKMP_INVALID_KEY_INFORMATION
;
215 if (ke_pd
->next
!= NULL
)
217 loglog(RC_LOG_SERIOUS
, "%s message contains several KE payloads; we accept at most one", msg_name
);
218 return ISAKMP_INVALID_KEY_INFORMATION
; /* ??? */
220 return accept_KE(dest
, val_name
, st
->st_pfs_group
, &ke_pd
->pbs
);
222 return ISAKMP_NOTHING_WRONG
;
225 static bool build_and_ship_nonce(chunk_t
*n
, pb_stream
*outs
, u_int8_t np
,
231 *n
= chunk_create(malloc(DEFAULT_NONCE_SIZE
), DEFAULT_NONCE_SIZE
);
232 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
233 rng
->get_bytes(rng
, DEFAULT_NONCE_SIZE
, n
->ptr
);
235 return out_generic_chunk(np
, &isakmp_nonce_desc
, outs
, *n
, name
);
238 static linked_list_t
* collect_rw_ca_candidates(struct msg_digest
*md
)
240 linked_list_t
*list
= linked_list_create();
243 d
= find_host_connection(&md
->iface
->addr
, pluto_port
, (ip_address
*)NULL
,
244 md
->sender_port
, LEMPTY
);
246 for (; d
!= NULL
; d
= d
->hp_next
)
248 /* must be a road warrior connection */
249 if (d
->kind
== CK_TEMPLATE
&& !(d
->policy
& POLICY_OPPO
) &&
252 enumerator_t
*enumerator
;
253 identification_t
*ca
;
254 bool new_entry
= TRUE
;
256 enumerator
= list
->create_enumerator(list
);
257 while (enumerator
->enumerate(enumerator
, &ca
))
259 if (ca
->equals(ca
, d
->spd
.that
.ca
))
265 enumerator
->destroy(enumerator
);
269 list
->insert_last(list
, d
->spd
.that
.ca
->clone(d
->spd
.that
.ca
));
276 static bool build_and_ship_CR(u_int8_t type
, chunk_t ca
, pb_stream
*outs
,
280 struct isakmp_cr cr_hd
;
282 cr_hd
.isacr_type
= type
;
284 /* build CR header */
285 if (!out_struct(&cr_hd
, &isakmp_ipsec_cert_req_desc
, outs
, &cr_pbs
))
291 /* build CR body containing the distinguished name of the CA */
292 if (!out_chunk(ca
, &cr_pbs
, "CA"))
295 close_output_pbs(&cr_pbs
);
299 /* Send a notification to the peer. We could decide
300 * whether to send the notification, based on the type and the
301 * destination, if we care to.
303 static void send_notification(struct state
*sndst
, u_int16_t type
,
304 struct state
*encst
, msgid_t msgid
,
305 u_char
*icookie
, u_char
*rcookie
,
306 u_char
*spi
, size_t spisize
, u_char protoid
)
309 pb_stream pbs
, r_hdr_pbs
;
310 u_char
*r_hashval
= NULL
; /* where in reply to jam hash value */
311 u_char
*r_hash_start
= NULL
; /* start of what is to be hashed */
313 passert((sndst
) && (sndst
->st_connection
));
315 plog("sending %snotification %s to %s:%u"
316 , encst ?
"encrypted " : ""
317 , enum_name(¬ification_names
, type
)
318 , ip_str(&sndst
->st_connection
->spd
.that
.host_addr
)
319 , (unsigned)sndst
->st_connection
->spd
.that
.host_port
);
321 memset(buffer
, 0, sizeof(buffer
));
322 init_pbs(&pbs
, buffer
, sizeof(buffer
), "ISAKMP notify");
326 struct isakmp_hdr hdr
;
328 hdr
.isa_version
= ISAKMP_MAJOR_VERSION
<< ISA_MAJ_SHIFT
| ISAKMP_MINOR_VERSION
;
329 hdr
.isa_np
= encst ? ISAKMP_NEXT_HASH
: ISAKMP_NEXT_N
;
330 hdr
.isa_xchg
= ISAKMP_XCHG_INFO
;
331 hdr
.isa_msgid
= msgid
;
332 hdr
.isa_flags
= encst ? ISAKMP_FLAG_ENCRYPTION
: 0;
335 memcpy(hdr
.isa_icookie
, icookie
, COOKIE_SIZE
);
339 memcpy(hdr
.isa_rcookie
, rcookie
, COOKIE_SIZE
);
341 if (!out_struct(&hdr
, &isakmp_hdr_desc
, &pbs
, &r_hdr_pbs
))
347 /* HASH -- value to be filled later */
351 if (!out_generic(ISAKMP_NEXT_N
, &isakmp_hash_desc
, &r_hdr_pbs
, &hash_pbs
))
355 r_hashval
= hash_pbs
.cur
; /* remember where to plant value */
357 encst
->st_oakley
.hasher
->hash_digest_size
, &hash_pbs
, "HASH"))
361 close_output_pbs(&hash_pbs
);
362 r_hash_start
= r_hdr_pbs
.cur
; /* hash from after HASH */
365 /* Notification Payload */
368 struct isakmp_notification isan
;
370 isan
.isan_doi
= ISAKMP_DOI_IPSEC
;
371 isan
.isan_np
= ISAKMP_NEXT_NONE
;
372 isan
.isan_type
= type
;
373 isan
.isan_spisize
= spisize
;
374 isan
.isan_protoid
= protoid
;
376 if (!out_struct(&isan
, &isakmp_notification_desc
, &r_hdr_pbs
, ¬_pbs
)
377 || !out_raw(spi
, spisize
, ¬_pbs
, "spi"))
381 close_output_pbs(¬_pbs
);
384 /* calculate hash value and patch into Hash Payload */
387 chunk_t msgid_chunk
= chunk_from_thing(msgid
);
388 chunk_t msg_chunk
= { r_hash_start
, r_hdr_pbs
.cur
-r_hash_start
};
389 pseudo_random_function_t prf_alg
;
392 prf_alg
= oakley_to_prf(encst
->st_oakley
.hash
);
393 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
394 prf
->set_key(prf
, encst
->st_skeyid_a
);
395 prf
->get_bytes(prf
, msgid_chunk
, NULL
);
396 prf
->get_bytes(prf
, msg_chunk
, r_hashval
);
399 DBG_log("HASH computed:");
400 DBG_dump("", r_hashval
, prf
->get_block_size(prf
));
405 /* Encrypt message (preserve st_iv and st_new_iv) */
408 u_char old_iv
[MAX_DIGEST_LEN
];
409 u_char new_iv
[MAX_DIGEST_LEN
];
411 u_int old_iv_len
= encst
->st_iv_len
;
412 u_int new_iv_len
= encst
->st_new_iv_len
;
414 if (old_iv_len
> MAX_DIGEST_LEN
|| new_iv_len
> MAX_DIGEST_LEN
)
418 memcpy(old_iv
, encst
->st_iv
, old_iv_len
);
419 memcpy(new_iv
, encst
->st_new_iv
, new_iv_len
);
421 if (!IS_ISAKMP_SA_ESTABLISHED(encst
->st_state
))
423 memcpy(encst
->st_ph1_iv
, encst
->st_new_iv
, encst
->st_new_iv_len
);
424 encst
->st_ph1_iv_len
= encst
->st_new_iv_len
;
426 init_phase2_iv(encst
, &msgid
);
427 if (!encrypt_message(&r_hdr_pbs
, encst
))
432 /* restore preserved st_iv and st_new_iv */
433 memcpy(encst
->st_iv
, old_iv
, old_iv_len
);
434 memcpy(encst
->st_new_iv
, new_iv
, new_iv_len
);
435 encst
->st_iv_len
= old_iv_len
;
436 encst
->st_new_iv_len
= new_iv_len
;
440 close_output_pbs(&r_hdr_pbs
);
443 /* Send packet (preserve st_tpacket) */
445 chunk_t saved_tpacket
= sndst
->st_tpacket
;
447 sndst
->st_tpacket
= chunk_create(pbs
.start
, pbs_offset(&pbs
));
448 send_packet(sndst
, "ISAKMP notify");
449 sndst
->st_tpacket
= saved_tpacket
;
453 void send_notification_from_state(struct state
*st
, enum state_kind state
,
460 if (state
== STATE_UNDEFINED
)
461 state
= st
->st_state
;
465 p1st
= find_phase1_state(st
->st_connection
, ISAKMP_SA_ESTABLISHED_STATES
);
466 if ((p1st
== NULL
) || (!IS_ISAKMP_SA_ESTABLISHED(p1st
->st_state
)))
468 loglog(RC_LOG_SERIOUS
,
469 "no Phase1 state for Quick mode notification");
472 send_notification(st
, type
, p1st
, generate_msgid(p1st
),
473 st
->st_icookie
, st
->st_rcookie
, NULL
, 0, PROTO_ISAKMP
);
475 else if (IS_ISAKMP_ENCRYPTED(state
) && st
->st_enc_key
.ptr
!= NULL
)
477 send_notification(st
, type
, st
, generate_msgid(st
),
478 st
->st_icookie
, st
->st_rcookie
, NULL
, 0, PROTO_ISAKMP
);
482 /* no ISAKMP SA established - don't encrypt notification */
483 send_notification(st
, type
, NULL
, 0,
484 st
->st_icookie
, st
->st_rcookie
, NULL
, 0, PROTO_ISAKMP
);
488 void send_notification_from_md(struct msg_digest
*md
, u_int16_t type
)
491 * Create a dummy state to be able to use send_packet in
495 * st_connection->that.host_addr
496 * st_connection->that.host_port
497 * st_connection->interface
504 memset(&st
, 0, sizeof(st
));
505 memset(&cnx
, 0, sizeof(cnx
));
506 st
.st_connection
= &cnx
;
507 cnx
.spd
.that
.host_addr
= md
->sender
;
508 cnx
.spd
.that
.host_port
= md
->sender_port
;
509 cnx
.interface
= md
->iface
;
511 send_notification(&st
, type
, NULL
, 0,
512 md
->hdr
.isa_icookie
, md
->hdr
.isa_rcookie
, NULL
, 0, PROTO_ISAKMP
);
515 /* Send a Delete Notification to announce deletion of ISAKMP SA or
516 * inbound IPSEC SAs. Does nothing if no such SAs are being deleted.
517 * Delete Notifications cannot announce deletion of outbound IPSEC/ISAKMP SAs.
519 void send_delete(struct state
*st
)
526 ip_said said
[EM_MAXRELSPIS
];
529 *r_hashval
, /* where in reply to jam hash value */
530 *r_hash_start
; /* start of what is to be hashed */
531 bool isakmp_sa
= FALSE
;
533 if (IS_IPSEC_SA_ESTABLISHED(st
->st_state
))
535 p1st
= find_phase1_state(st
->st_connection
, ISAKMP_SA_ESTABLISHED_STATES
);
538 DBG(DBG_CONTROL
, DBG_log("no Phase 1 state for Delete"));
542 if (st
->st_ah
.present
)
544 ns
->spi
= st
->st_ah
.our_spi
;
545 ns
->dst
= st
->st_connection
->spd
.this.host_addr
;
546 ns
->proto
= PROTO_IPSEC_AH
;
549 if (st
->st_esp
.present
)
551 ns
->spi
= st
->st_esp
.our_spi
;
552 ns
->dst
= st
->st_connection
->spd
.this.host_addr
;
553 ns
->proto
= PROTO_IPSEC_ESP
;
557 passert(ns
!= said
); /* there must be some SAs to delete */
559 else if (IS_ISAKMP_SA_ESTABLISHED(st
->st_state
))
566 return; /* nothing to do */
569 msgid
= generate_msgid(p1st
);
572 init_pbs(&reply_pbs
, buffer
, sizeof(buffer
), "delete msg");
576 struct isakmp_hdr hdr
;
578 hdr
.isa_version
= ISAKMP_MAJOR_VERSION
<< ISA_MAJ_SHIFT
| ISAKMP_MINOR_VERSION
;
579 hdr
.isa_np
= ISAKMP_NEXT_HASH
;
580 hdr
.isa_xchg
= ISAKMP_XCHG_INFO
;
581 hdr
.isa_msgid
= msgid
;
582 hdr
.isa_flags
= ISAKMP_FLAG_ENCRYPTION
;
583 memcpy(hdr
.isa_icookie
, p1st
->st_icookie
, COOKIE_SIZE
);
584 memcpy(hdr
.isa_rcookie
, p1st
->st_rcookie
, COOKIE_SIZE
);
585 if (!out_struct(&hdr
, &isakmp_hdr_desc
, &reply_pbs
, &r_hdr_pbs
))
589 /* HASH -- value to be filled later */
593 if (!out_generic(ISAKMP_NEXT_D
, &isakmp_hash_desc
, &r_hdr_pbs
, &hash_pbs
))
597 r_hashval
= hash_pbs
.cur
; /* remember where to plant value */
598 if (!out_zero(p1st
->st_oakley
.hasher
->hash_digest_size
, &hash_pbs
, "HASH(1)"))
602 close_output_pbs(&hash_pbs
);
603 r_hash_start
= r_hdr_pbs
.cur
; /* hash from after HASH(1) */
606 /* Delete Payloads */
610 struct isakmp_delete isad
;
611 u_char isakmp_spi
[2*COOKIE_SIZE
];
613 isad
.isad_doi
= ISAKMP_DOI_IPSEC
;
614 isad
.isad_np
= ISAKMP_NEXT_NONE
;
615 isad
.isad_spisize
= (2 * COOKIE_SIZE
);
616 isad
.isad_protoid
= PROTO_ISAKMP
;
619 memcpy(isakmp_spi
, st
->st_icookie
, COOKIE_SIZE
);
620 memcpy(isakmp_spi
+COOKIE_SIZE
, st
->st_rcookie
, COOKIE_SIZE
);
622 if (!out_struct(&isad
, &isakmp_delete_desc
, &r_hdr_pbs
, &del_pbs
)
623 || !out_raw(&isakmp_spi
, (2*COOKIE_SIZE
), &del_pbs
, "delete payload"))
627 close_output_pbs(&del_pbs
);
635 struct isakmp_delete isad
;
638 isad
.isad_doi
= ISAKMP_DOI_IPSEC
;
639 isad
.isad_np
= ns
== said? ISAKMP_NEXT_NONE
: ISAKMP_NEXT_D
;
640 isad
.isad_spisize
= sizeof(ipsec_spi_t
);
641 isad
.isad_protoid
= ns
->proto
;
644 if (!out_struct(&isad
, &isakmp_delete_desc
, &r_hdr_pbs
, &del_pbs
)
645 || !out_raw(&ns
->spi
, sizeof(ipsec_spi_t
), &del_pbs
, "delete payload"))
649 close_output_pbs(&del_pbs
);
653 /* calculate hash value and patch into Hash Payload */
655 chunk_t msgid_chunk
= chunk_from_thing(msgid
);
656 chunk_t msg_chunk
= { r_hash_start
, r_hdr_pbs
.cur
-r_hash_start
};
657 pseudo_random_function_t prf_alg
;
660 prf_alg
= oakley_to_prf(p1st
->st_oakley
.hash
);
661 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
662 prf
->set_key(prf
, p1st
->st_skeyid_a
);
663 prf
->get_bytes(prf
, msgid_chunk
, NULL
);
664 prf
->get_bytes(prf
, msg_chunk
, r_hashval
);
667 DBG_log("HASH(1) computed:");
668 DBG_dump("", r_hashval
, prf
->get_block_size(prf
));
674 /* Do a dance to avoid needing a new state object.
675 * We use the Phase 1 State. This is the one with right
677 * The tricky bits are:
678 * - we need to preserve (save/restore) st_iv (but not st_iv_new)
679 * - we need to preserve (save/restore) st_tpacket.
682 u_char old_iv
[MAX_DIGEST_LEN
];
683 chunk_t saved_tpacket
= p1st
->st_tpacket
;
685 memcpy(old_iv
, p1st
->st_iv
, p1st
->st_iv_len
);
686 init_phase2_iv(p1st
, &msgid
);
688 if (!encrypt_message(&r_hdr_pbs
, p1st
))
692 p1st
->st_tpacket
= chunk_create(reply_pbs
.start
, pbs_offset(&reply_pbs
));
693 send_packet(p1st
, "delete notify");
694 p1st
->st_tpacket
= saved_tpacket
;
696 /* get back old IV for this state */
697 memcpy(p1st
->st_iv
, old_iv
, p1st
->st_iv_len
);
701 void accept_delete(struct state
*st
, struct msg_digest
*md
,
702 struct payload_digest
*p
)
704 struct isakmp_delete
*d
= &(p
->payload
.delete);
705 identification_t
*this_id
, *that_id
;
706 ip_address peer_addr
;
712 loglog(RC_LOG_SERIOUS
, "ignoring Delete SA payload: not encrypted");
716 if (!IS_ISAKMP_SA_ESTABLISHED(st
->st_state
))
718 /* can't happen (if msg is encrypt), but just to be sure */
719 loglog(RC_LOG_SERIOUS
, "ignoring Delete SA payload: "
720 "ISAKMP SA not established");
724 if (d
->isad_nospi
== 0)
726 loglog(RC_LOG_SERIOUS
, "ignoring Delete SA payload: no SPI");
730 switch (d
->isad_protoid
)
733 sizespi
= 2 * COOKIE_SIZE
;
736 case PROTO_IPSEC_ESP
:
737 sizespi
= sizeof(ipsec_spi_t
);
740 /* nothing interesting to delete */
743 loglog(RC_LOG_SERIOUS
744 , "ignoring Delete SA payload: unknown Protocol ID (%s)"
745 , enum_show(&protocol_names
, d
->isad_protoid
));
749 if (d
->isad_spisize
!= sizespi
)
751 loglog(RC_LOG_SERIOUS
752 , "ignoring Delete SA payload: bad SPI size (%d) for %s"
753 , d
->isad_spisize
, enum_show(&protocol_names
, d
->isad_protoid
));
757 if (pbs_left(&p
->pbs
) != d
->isad_nospi
* sizespi
)
759 loglog(RC_LOG_SERIOUS
760 , "ignoring Delete SA payload: invalid payload size");
764 if (d
->isad_protoid
== PROTO_ISAKMP
)
766 struct end
*this = &st
->st_connection
->spd
.this;
767 struct end
*that
= &st
->st_connection
->spd
.that
;
768 this_id
= this->id
->clone(this->id
);
769 that_id
= that
->id
->clone(that
->id
);
770 peer_addr
= st
->st_connection
->spd
.that
.host_addr
;
773 for (i
= 0; i
< d
->isad_nospi
; i
++)
775 u_char
*spi
= p
->pbs
.cur
+ (i
* sizespi
);
777 if (d
->isad_protoid
== PROTO_ISAKMP
)
782 struct state
*dst
= find_state(spi
/*iCookie*/
783 , spi
+COOKIE_SIZE
/*rCookie*/
789 loglog(RC_LOG_SERIOUS
, "ignoring Delete SA payload: "
790 "ISAKMP SA not found (maybe expired)");
792 else if (! this_id
->equals(this_id
, dst
->st_connection
->spd
.this.id
) ||
793 ! that_id
->equals(that_id
, dst
->st_connection
->spd
.that
.id
))
795 /* we've not authenticated the relevant identities */
796 loglog(RC_LOG_SERIOUS
, "ignoring Delete SA payload: "
797 "ISAKMP SA used to convey Delete has different IDs from ISAKMP SA it deletes");
803 oldc
= cur_connection
;
804 set_cur_connection(dst
->st_connection
);
806 if (nat_traversal_enabled
)
808 nat_traversal_change_port_lookup(md
, dst
);
810 loglog(RC_LOG_SERIOUS
, "received Delete SA payload: "
811 "deleting ISAKMP State #%lu", dst
->st_serialno
);
813 set_cur_connection(oldc
);
822 struct state
*dst
= find_phase2_state_to_delete(st
824 , *(ipsec_spi_t
*)spi
/* network order */
829 loglog(RC_LOG_SERIOUS
830 , "ignoring Delete SA payload: %s SA(0x%08lx) not found (%s)"
831 , enum_show(&protocol_names
, d
->isad_protoid
)
832 , (unsigned long)ntohl((unsigned long)*(ipsec_spi_t
*)spi
)
833 , bogus ?
"our SPI - bogus implementation" : "maybe expired");
837 connection_t
*rc
= dst
->st_connection
;
840 oldc
= cur_connection
;
841 set_cur_connection(rc
);
843 if (nat_traversal_enabled
)
845 nat_traversal_change_port_lookup(md
, dst
);
847 if (rc
->newest_ipsec_sa
== dst
->st_serialno
848 && (rc
->policy
& POLICY_UP
))
850 /* Last IPSec SA for a permanent connection that we
851 * have initiated. Replace it in a few seconds.
853 * Useful if the other peer is rebooting.
855 #define DELETE_SA_DELAY EVENT_RETRANSMIT_DELAY_0
856 if (dst
->st_event
!= NULL
857 && dst
->st_event
->ev_type
== EVENT_SA_REPLACE
858 && dst
->st_event
->ev_time
<= DELETE_SA_DELAY
+ now())
860 /* Patch from Angus Lees to ignore retransmited
863 loglog(RC_LOG_SERIOUS
, "received Delete SA payload: "
864 "already replacing IPSEC State #%lu in %d seconds"
865 , dst
->st_serialno
, (int)(dst
->st_event
->ev_time
- now()));
869 loglog(RC_LOG_SERIOUS
, "received Delete SA payload: "
870 "replace IPSEC State #%lu in %d seconds"
871 , dst
->st_serialno
, DELETE_SA_DELAY
);
872 dst
->st_margin
= DELETE_SA_DELAY
;
874 event_schedule(EVENT_SA_REPLACE
, DELETE_SA_DELAY
, dst
);
879 loglog(RC_LOG_SERIOUS
, "received Delete SA(0x%08lx) payload: "
880 "deleting IPSEC State #%lu"
881 , (unsigned long)ntohl((unsigned long)*(ipsec_spi_t
*)spi
)
886 /* reset connection */
887 set_cur_connection(oldc
);
892 if (d
->isad_protoid
== PROTO_ISAKMP
)
894 this_id
->destroy(this_id
);
895 that_id
->destroy(that_id
);
899 /* The whole message must be a multiple of 4 octets.
900 * I'm not sure where this is spelled out, but look at
901 * rfc2408 3.6 Transform Payload.
902 * Note: it talks about 4 BYTE boundaries!
904 void close_message(pb_stream
*pbs
)
906 size_t padding
= pad_up(pbs_offset(pbs
), 4);
910 (void) out_zero(padding
, pbs
, "message padding");
912 close_output_pbs(pbs
);
915 /* Initiate an Oakley Main Mode exchange.
917 * Note: this is not called from demux.c
920 main_outI1(int whack_sock
, connection_t
*c
, struct state
*predecessor
921 , lset_t policy
, unsigned long try)
923 struct state
*st
= new_state();
924 pb_stream reply
; /* not actually a reply, but you know what I mean */
926 int vids_to_send
= 0;
928 /* set up new state */
929 st
->st_connection
= c
;
930 set_cur_state(st
); /* we must reset before exit */
931 st
->st_policy
= policy
& ~POLICY_IPSEC_MASK
;
932 st
->st_whack_sock
= whack_sock
;
934 st
->st_state
= STATE_MAIN_I1
;
936 /* determine how many Vendor ID payloads we will be sending */
941 if (SEND_CISCO_UNITY_VID
)
945 if (c
->spd
.this.cert
&&
946 c
->spd
.this.cert
->cert
->get_type(c
->spd
.this.cert
->cert
) == CERT_GPG
)
955 /* always send DPD Vendor ID */
958 if (nat_traversal_enabled
)
963 get_cookie(TRUE
, st
->st_icookie
, COOKIE_SIZE
, &c
->spd
.that
.host_addr
);
965 insert_state(st
); /* needs cookies, connection, and msgid (0) */
967 if (HAS_IPSEC_POLICY(policy
))
969 add_pending(dup_any(whack_sock
), st
, c
, policy
, 1
970 , predecessor
== NULL? SOS_NOBODY
: predecessor
->st_serialno
);
972 if (predecessor
== NULL
)
974 plog("initiating Main Mode");
978 plog("initiating Main Mode to replace #%lu", predecessor
->st_serialno
);
982 init_pbs(&reply
, reply_buffer
, sizeof(reply_buffer
), "reply packet");
986 struct isakmp_hdr hdr
;
988 zero(&hdr
); /* default to 0 */
989 hdr
.isa_version
= ISAKMP_MAJOR_VERSION
<< ISA_MAJ_SHIFT
| ISAKMP_MINOR_VERSION
;
990 hdr
.isa_np
= ISAKMP_NEXT_SA
;
991 hdr
.isa_xchg
= ISAKMP_XCHG_IDPROT
;
992 memcpy(hdr
.isa_icookie
, st
->st_icookie
, COOKIE_SIZE
);
993 /* R-cookie, flags and MessageID are left zero */
995 if (!out_struct(&hdr
, &isakmp_hdr_desc
, &reply
, &rbody
))
998 return STF_INTERNAL_ERROR
;
1004 u_char
*sa_start
= rbody
.cur
;
1006 if (!out_sa(&rbody
, &oakley_sadb
, st
, TRUE
1007 , vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
))
1010 return STF_INTERNAL_ERROR
;
1013 /* save initiator SA for later HASH */
1014 passert(st
->st_p1isa
.ptr
== NULL
); /* no leak! (MUST be first time) */
1015 st
->st_p1isa
= chunk_create(sa_start
, rbody
.cur
- sa_start
);
1016 st
->st_p1isa
= chunk_clone(st
->st_p1isa
);
1019 /* if enabled send Pluto Vendor ID */
1022 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
1023 , &rbody
, VID_STRONGSWAN
))
1026 return STF_INTERNAL_ERROR
;
1030 /* if enabled send Cisco Unity Vendor ID */
1031 if (SEND_CISCO_UNITY_VID
)
1033 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
1034 , &rbody
, VID_CISCO_UNITY
))
1037 return STF_INTERNAL_ERROR
;
1040 /* if we have an OpenPGP certificate we assume an
1041 * OpenPGP peer and have to send the Vendor ID
1043 if (c
->spd
.this.cert
&&
1044 c
->spd
.this.cert
->cert
->get_type(c
->spd
.this.cert
->cert
) == CERT_GPG
)
1046 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
1047 , &rbody
, VID_OPENPGP
))
1050 return STF_INTERNAL_ERROR
;
1054 /* Announce our ability to do eXtended AUTHentication to the peer */
1057 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
1058 , &rbody
, VID_MISC_XAUTH
))
1061 return STF_INTERNAL_ERROR
;
1065 /* Announce our ability to do Dead Peer Detection to the peer */
1067 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
1068 , &rbody
, VID_MISC_DPD
))
1071 return STF_INTERNAL_ERROR
;
1075 if (nat_traversal_enabled
)
1077 /* Add supported NAT-Traversal VID */
1078 if (!nat_traversal_add_vid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
1082 return STF_INTERNAL_ERROR
;
1086 close_message(&rbody
);
1087 close_output_pbs(&reply
);
1088 st
->st_tpacket
= chunk_create(reply
.start
, pbs_offset(&reply
));
1089 st
->st_tpacket
= chunk_clone(st
->st_tpacket
);
1093 send_packet(st
, "main_outI1");
1095 /* Set up a retransmission event, half a minute henceforth */
1097 event_schedule(EVENT_RETRANSMIT
, EVENT_RETRANSMIT_DELAY_0
, st
);
1099 if (predecessor
!= NULL
)
1101 update_pending(predecessor
, st
);
1102 whack_log(RC_NEW_STATE
+ STATE_MAIN_I1
1103 , "%s: initiate, replacing #%lu"
1104 , enum_name(&state_names
, st
->st_state
)
1105 , predecessor
->st_serialno
);
1109 whack_log(RC_NEW_STATE
+ STATE_MAIN_I1
1110 , "%s: initiate", enum_name(&state_names
, st
->st_state
));
1116 void ipsecdoi_initiate(int whack_sock
, connection_t
*c
, lset_t policy
,
1117 unsigned long try, so_serial_t replacing
)
1119 /* If there's already an ISAKMP SA established, use that and
1120 * go directly to Quick Mode. We are even willing to use one
1121 * that is still being negotiated, but only if we are the Initiator
1122 * (thus we can be sure that the IDs are not going to change;
1123 * other issues around intent might matter).
1124 * Note: there is no way to initiate with a Road Warrior.
1126 struct state
*st
= find_phase1_state(c
1127 , ISAKMP_SA_ESTABLISHED_STATES
| PHASE1_INITIATOR_STATES
);
1131 (void) main_outI1(whack_sock
, c
, NULL
, policy
, try);
1133 else if (HAS_IPSEC_POLICY(policy
))
1135 if (!IS_ISAKMP_SA_ESTABLISHED(st
->st_state
))
1137 /* leave our Phase 2 negotiation pending */
1138 add_pending(whack_sock
, st
, c
, policy
, try, replacing
);
1142 /* ??? we assume that peer_nexthop_sin isn't important:
1143 * we already have it from when we negotiated the ISAKMP SA!
1144 * It isn't clear what to do with the error return.
1146 (void) quick_outI1(whack_sock
, st
, c
, policy
, try, replacing
);
1151 close_any(whack_sock
);
1155 /* Replace SA with a fresh one that is similar
1157 * Shares some logic with ipsecdoi_initiate, but not the same!
1158 * - we must not reuse the ISAKMP SA if we are trying to replace it!
1159 * - if trying to replace IPSEC SA, use ipsecdoi_initiate to build
1160 * ISAKMP SA if needed.
1161 * - duplicate whack fd, if live.
1162 * Does not delete the old state -- someone else will do that.
1164 void ipsecdoi_replace(struct state
*st
, unsigned long try)
1166 int whack_sock
= dup_any(st
->st_whack_sock
);
1167 lset_t policy
= st
->st_policy
;
1169 if (IS_PHASE1(st
->st_state
))
1171 passert(!HAS_IPSEC_POLICY(policy
));
1172 (void) main_outI1(whack_sock
, st
->st_connection
, st
, policy
, try);
1176 /* Add features of actual old state to policy. This ensures
1177 * that rekeying doesn't downgrade security. I admit that
1178 * this doesn't capture everything.
1180 if (st
->st_pfs_group
!= NULL
)
1181 policy
|= POLICY_PFS
;
1182 if (st
->st_ah
.present
)
1184 policy
|= POLICY_AUTHENTICATE
;
1185 if (st
->st_ah
.attrs
.encapsulation
== ENCAPSULATION_MODE_TUNNEL
)
1186 policy
|= POLICY_TUNNEL
;
1188 if (st
->st_esp
.present
&& st
->st_esp
.attrs
.transid
!= ESP_NULL
)
1190 policy
|= POLICY_ENCRYPT
;
1191 if (st
->st_esp
.attrs
.encapsulation
== ENCAPSULATION_MODE_TUNNEL
)
1192 policy
|= POLICY_TUNNEL
;
1194 if (st
->st_ipcomp
.present
)
1196 policy
|= POLICY_COMPRESS
;
1197 if (st
->st_ipcomp
.attrs
.encapsulation
== ENCAPSULATION_MODE_TUNNEL
)
1198 policy
|= POLICY_TUNNEL
;
1200 passert(HAS_IPSEC_POLICY(policy
));
1201 ipsecdoi_initiate(whack_sock
, st
->st_connection
, policy
, try
1206 /* SKEYID for preshared keys.
1207 * See draft-ietf-ipsec-ike-01.txt 4.1
1209 static bool skeyid_preshared(struct state
*st
)
1211 const chunk_t
*pss
= get_preshared_secret(st
->st_connection
);
1215 loglog(RC_LOG_SERIOUS
, "preshared secret disappeared!");
1220 pseudo_random_function_t prf_alg
;
1223 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1224 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1227 loglog(RC_LOG_SERIOUS
, "%N not available to compute skeyid",
1228 pseudo_random_function_names
, prf_alg
);
1231 free(st
->st_skeyid
.ptr
);
1232 prf
->set_key(prf
, *pss
);
1233 prf
->allocate_bytes(prf
, st
->st_ni
, NULL
);
1234 prf
->allocate_bytes(prf
, st
->st_nr
, &st
->st_skeyid
);
1240 static bool skeyid_digisig(struct state
*st
)
1243 pseudo_random_function_t prf_alg
;
1246 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1247 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1250 loglog(RC_LOG_SERIOUS
, "%N not available to compute skeyid",
1251 pseudo_random_function_names
, prf_alg
);
1254 free(st
->st_skeyid
.ptr
);
1255 nir
= chunk_cat("cc", st
->st_ni
, st
->st_nr
);
1256 prf
->set_key(prf
, nir
);
1257 prf
->allocate_bytes(prf
, st
->st_shared
, &st
->st_skeyid
);
1263 /* Generate the SKEYID_* and new IV
1264 * See draft-ietf-ipsec-ike-01.txt 4.1
1266 static bool generate_skeyids_iv(struct state
*st
)
1268 /* Generate the SKEYID */
1269 switch (st
->st_oakley
.auth
)
1271 case OAKLEY_PRESHARED_KEY
:
1272 case XAUTHInitPreShared
:
1273 case XAUTHRespPreShared
:
1274 if (!skeyid_preshared(st
))
1280 case OAKLEY_RSA_SIG
:
1281 case OAKLEY_ECDSA_256
:
1282 case OAKLEY_ECDSA_384
:
1283 case OAKLEY_ECDSA_521
:
1286 if (!skeyid_digisig(st
))
1292 case OAKLEY_DSS_SIG
:
1295 case OAKLEY_RSA_ENC
:
1296 case OAKLEY_RSA_ENC_REV
:
1297 case OAKLEY_ELGAMAL_ENC
:
1298 case OAKLEY_ELGAMAL_ENC_REV
:
1302 bad_case(st
->st_oakley
.auth
);
1305 /* generate SKEYID_* from SKEYID */
1307 chunk_t seed_skeyid_d
= chunk_from_chars(0x00);
1308 chunk_t seed_skeyid_a
= chunk_from_chars(0x01);
1309 chunk_t seed_skeyid_e
= chunk_from_chars(0x02);
1310 chunk_t icookie
= { st
->st_icookie
, COOKIE_SIZE
};
1311 chunk_t rcookie
= { st
->st_rcookie
, COOKIE_SIZE
};
1312 pseudo_random_function_t prf_alg
;
1315 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1316 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1317 prf
->set_key(prf
, st
->st_skeyid
);
1320 free(st
->st_skeyid_d
.ptr
);
1321 prf
->allocate_bytes(prf
, st
->st_shared
, NULL
);
1322 prf
->allocate_bytes(prf
, icookie
, NULL
);
1323 prf
->allocate_bytes(prf
, rcookie
, NULL
);
1324 prf
->allocate_bytes(prf
, seed_skeyid_d
, &st
->st_skeyid_d
);
1327 free(st
->st_skeyid_a
.ptr
);
1328 prf
->allocate_bytes(prf
, st
->st_skeyid_d
, NULL
);
1329 prf
->allocate_bytes(prf
, st
->st_shared
, NULL
);
1330 prf
->allocate_bytes(prf
, icookie
, NULL
);
1331 prf
->allocate_bytes(prf
, rcookie
, NULL
);
1332 prf
->allocate_bytes(prf
, seed_skeyid_a
, &st
->st_skeyid_a
);
1335 free(st
->st_skeyid_e
.ptr
);
1336 prf
->allocate_bytes(prf
, st
->st_skeyid_a
, NULL
);
1337 prf
->allocate_bytes(prf
, st
->st_shared
, NULL
);
1338 prf
->allocate_bytes(prf
, icookie
, NULL
);
1339 prf
->allocate_bytes(prf
, rcookie
, NULL
);
1340 prf
->allocate_bytes(prf
, seed_skeyid_e
, &st
->st_skeyid_e
);
1347 hash_algorithm_t hash_alg
;
1350 hash_alg
= oakley_to_hash_algorithm(st
->st_oakley
.hash
);
1351 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
1352 st
->st_new_iv_len
= hasher
->get_hash_size(hasher
);
1353 passert(st
->st_new_iv_len
<= sizeof(st
->st_new_iv
));
1356 DBG_dump_chunk("DH_i:", st
->st_gi
);
1357 DBG_dump_chunk("DH_r:", st
->st_gr
);
1360 hasher
->get_hash(hasher
, st
->st_gi
, NULL
);
1361 hasher
->get_hash(hasher
, st
->st_gr
, st
->st_new_iv
);
1362 hasher
->destroy(hasher
);
1365 /* Oakley Keying Material
1366 * Derived from Skeyid_e: if it is not big enough, generate more
1368 * See RFC 2409 "IKE" Appendix B
1371 size_t keysize
= st
->st_oakley
.enckeylen
/BITS_PER_BYTE
;
1373 /* free any existing key */
1374 free(st
->st_enc_key
.ptr
);
1376 if (keysize
> st
->st_skeyid_e
.len
)
1378 u_char keytemp
[MAX_OAKLEY_KEY_LEN
+ MAX_DIGEST_LEN
];
1379 chunk_t seed
= chunk_from_chars(0x00);
1380 size_t prf_block_size
, i
;
1381 pseudo_random_function_t prf_alg
;
1384 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1385 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1386 prf
->set_key(prf
, st
->st_skeyid_e
);
1387 prf_block_size
= prf
->get_block_size(prf
);
1391 prf
->get_bytes(prf
, seed
, &keytemp
[i
]);
1392 i
+= prf_block_size
;
1397 seed
= chunk_create(&keytemp
[i
-prf_block_size
], prf_block_size
);
1400 st
->st_enc_key
= chunk_create(keytemp
, keysize
);
1404 st
->st_enc_key
= chunk_create(st
->st_skeyid_e
.ptr
, keysize
);
1406 st
->st_enc_key
= chunk_clone(st
->st_enc_key
);
1410 DBG_dump_chunk("Skeyid: ", st
->st_skeyid
);
1411 DBG_dump_chunk("Skeyid_d:", st
->st_skeyid_d
);
1412 DBG_dump_chunk("Skeyid_a:", st
->st_skeyid_a
);
1413 DBG_dump_chunk("Skeyid_e:", st
->st_skeyid_e
);
1414 DBG_dump_chunk("enc key:", st
->st_enc_key
);
1415 DBG_dump("IV:", st
->st_new_iv
, st
->st_new_iv_len
));
1419 /* Generate HASH_I or HASH_R for ISAKMP Phase I.
1420 * This will *not* generate other hash payloads (eg. Phase II or Quick Mode,
1421 * New Group Mode, or ISAKMP Informational Exchanges).
1422 * If the hashi argument is TRUE, generate HASH_I; if FALSE generate HASH_R.
1423 * If hashus argument is TRUE, we're generating a hash for our end.
1424 * See RFC2409 IKE 5.
1426 static void main_mode_hash(struct state
*st
, chunk_t
*hash
, bool hashi
,
1427 const pb_stream
*idpl
)
1429 chunk_t icookie
= { st
->st_icookie
, COOKIE_SIZE
};
1430 chunk_t rcookie
= { st
->st_rcookie
, COOKIE_SIZE
};
1431 chunk_t sa_body
= { st
->st_p1isa
.ptr
+ sizeof(struct isakmp_generic
),
1432 st
->st_p1isa
.len
- sizeof(struct isakmp_generic
) };
1433 chunk_t id_body
= { idpl
->start
+ sizeof(struct isakmp_generic
),
1434 pbs_offset(idpl
) - sizeof(struct isakmp_generic
) };
1435 pseudo_random_function_t prf_alg
;
1438 switch (st
->st_oakley
.auth
)
1440 case OAKLEY_ECDSA_256
:
1441 prf_alg
= PRF_HMAC_SHA2_256
;
1443 case OAKLEY_ECDSA_384
:
1444 prf_alg
= PRF_HMAC_SHA2_384
;
1446 case OAKLEY_ECDSA_521
:
1447 prf_alg
= PRF_HMAC_SHA2_512
;
1450 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1452 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1453 prf
->set_key(prf
, st
->st_skeyid
);
1457 prf
->get_bytes(prf
, st
->st_gi
, NULL
);
1458 prf
->get_bytes(prf
, st
->st_gr
, NULL
);
1459 prf
->get_bytes(prf
, icookie
, NULL
);
1460 prf
->get_bytes(prf
, rcookie
, NULL
);
1464 prf
->get_bytes(prf
, st
->st_gr
, NULL
);
1465 prf
->get_bytes(prf
, st
->st_gi
, NULL
);
1466 prf
->get_bytes(prf
, rcookie
, NULL
);
1467 prf
->get_bytes(prf
, icookie
, NULL
);
1471 DBG_log("hashing %u bytes of SA", sa_body
.len
)
1473 prf
->get_bytes(prf
, sa_body
, NULL
);
1475 /* Hash identification payload, without generic payload header.
1476 * We used to reconstruct ID Payload for this purpose, but now
1477 * we use the bytes as they appear on the wire to avoid
1478 * "spelling problems".
1480 prf
->get_bytes(prf
, id_body
, hash
->ptr
);
1481 hash
->len
= prf
->get_block_size(prf
);
1485 /* Create a public key signature of a hash.
1486 * Poorly specified in draft-ietf-ipsec-ike-01.txt 6.1.1.2.
1487 * Use PKCS#1 version 1.5 encryption of hash (called
1488 * RSAES-PKCS1-V1_5) in PKCS#2.
1490 static size_t sign_hash(signature_scheme_t scheme
, connection_t
*c
,
1491 u_char sig_val
[RSA_MAX_OCTETS
], chunk_t hash
)
1494 smartcard_t
*sc
= c
->spd
.this.sc
;
1496 if (sc
== NULL
) /* no smartcard */
1499 private_key_t
*private = get_private_key(c
);
1501 if (private == NULL
)
1503 return 0; /* failure: no key to use */
1505 if (!private->sign(private, scheme
, hash
, &sig
))
1509 memcpy(sig_val
, sig
.ptr
, sig
.len
);
1513 else if (sc
->valid
) /* if valid pin then sign hash on the smartcard */
1515 lock_certs_and_keys("sign_hash");
1516 if (!scx_establish_context(sc
) || !scx_login(sc
))
1518 scx_release_context(sc
);
1519 unlock_certs_and_keys("sign_hash");
1523 sz
= scx_get_keylength(sc
);
1526 plog("failed to get keylength from smartcard");
1527 scx_release_context(sc
);
1528 unlock_certs_and_keys("sign_hash");
1532 DBG(DBG_CONTROL
| DBG_CRYPT
,
1533 DBG_log("signing hash with private key from smartcard (slot: %d, id: %s)"
1534 , (int)sc
->slot
, sc
->id
)
1536 sz
= scx_sign_hash(sc
, hash
.ptr
, hash
.len
, sig_val
, sz
) ? sz
: 0;
1537 if (!pkcs11_keep_state
)
1539 scx_release_context(sc
);
1541 unlock_certs_and_keys("sign_hash");
1546 /* Check signature against all public keys we can find.
1547 * If we need keys from DNS KEY records, and they haven't been fetched,
1548 * return STF_SUSPEND to ask for asynch DNS lookup.
1550 * Note: parameter keys_from_dns contains results of DNS lookup for key
1551 * or is NULL indicating lookup not yet tried.
1553 * take_a_crack is a helper function. Mostly forensic.
1554 * If only we had coroutines.
1560 int tried_cnt
; /* number of keys tried */
1563 static bool take_a_crack(struct tac_state
*s
, pubkey_t
*kr
)
1565 public_key_t
*pub_key
= kr
->public_key
;
1566 chunk_t keyid
= chunk_empty
;
1567 signature_scheme_t scheme
;
1570 scheme
= oakley_to_signature_scheme(s
->st
->st_oakley
.auth
);
1571 pub_key
->get_fingerprint(pub_key
, KEY_ID_PUBKEY_INFO_SHA1
, &keyid
);
1573 if (pub_key
->verify(pub_key
, scheme
, s
->hash
, s
->sig
))
1575 DBG(DBG_CRYPT
| DBG_CONTROL
,
1576 DBG_log("%s check passed with keyid %#B",
1577 enum_show(&oakley_auth_names
, s
->st
->st_oakley
.auth
), &keyid
)
1579 unreference_key(&s
->st
->st_peer_pubkey
);
1580 s
->st
->st_peer_pubkey
= reference_key(kr
);
1586 DBG_log("%s check failed with keyid %#B",
1587 enum_show(&oakley_auth_names
, s
->st
->st_oakley
.auth
), &keyid
)
1593 static stf_status
check_signature(key_type_t key_type
, identification_t
* peer
,
1594 struct state
*st
, chunk_t hash
,
1595 const pb_stream
*sig_pbs
,
1597 const pubkey_list_t
*keys_from_dns
,
1598 #endif /* USE_KEYRR */
1599 const struct gw_info
*gateways_from_dns
)
1601 const connection_t
*c
= st
->st_connection
;
1606 s
.sig
= chunk_create(sig_pbs
->cur
, pbs_left(sig_pbs
));
1609 /* try all gateway records hung off c */
1610 if (c
->policy
& POLICY_OPPO
)
1614 for (gw
= c
->gw_info
; gw
!= NULL
; gw
= gw
->next
)
1616 /* only consider entries that have a key and are for our peer */
1617 if (gw
->gw_key_present
&&
1618 gw
->gw_id
->equals(gw
->gw_id
, c
->spd
.that
.id
) &&
1619 take_a_crack(&s
, gw
->key
))
1626 /* try all appropriate Public keys */
1628 pubkey_list_t
*p
, **pp
;
1632 for (p
= pubkeys
; p
!= NULL
; p
= *pp
)
1634 pubkey_t
*key
= p
->key
;
1635 key_type_t type
= key
->public_key
->get_type(key
->public_key
);
1637 if (type
== key_type
&& peer
->equals(peer
, key
->id
))
1639 time_t now
= time(NULL
);
1641 /* check if found public key has expired */
1642 if (key
->until_time
!= UNDEFINED_TIME
&& key
->until_time
< now
)
1644 loglog(RC_LOG_SERIOUS
,
1645 "cached public key has expired and has been deleted");
1646 *pp
= free_public_keyentry(p
);
1647 continue; /* continue with next public key */
1649 if (take_a_crack(&s
, key
))
1658 /* if no key was found and that side of connection is
1659 * key_from_DNS_on_demand then go search DNS for keys for peer.
1661 if (s
.tried_cnt
== 0 && c
->spd
.that
.key_from_DNS_on_demand
)
1663 if (gateways_from_dns
!= NULL
)
1666 const struct gw_info
*gwp
;
1668 for (gwp
= gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
1670 if (gwp
->gw_key_present
&& take_a_crack(&s
, gwp
->key
))
1677 else if (keys_from_dns
!= NULL
)
1680 const pubkey_list_t
*kr
;
1682 for (kr
= keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
1684 if (kr
->key
->alg
== PUBKEY_ALG_RSA
&& take_a_crack(&s
, kr
->key
))
1690 #endif /* USE_KEYRR */
1693 /* nothing yet: ask for asynch DNS lookup */
1698 /* no acceptable key was found: diagnose */
1700 if (s
.tried_cnt
== 0)
1702 loglog(RC_LOG_SERIOUS
, "no public key known for '%Y'", peer
);
1704 else if (s
.tried_cnt
== 1)
1706 loglog(RC_LOG_SERIOUS
, "signature check for '%Y' failed: "
1707 " wrong key?; tried %d", peer
, s
.tried_cnt
);
1709 DBG_log("public key for '%Y' failed: "
1710 "decrypted SIG payload into a malformed ECB", peer
)
1715 loglog(RC_LOG_SERIOUS
, "signature check for '%Y' failed: "
1716 "tried %d keys but none worked.", peer
, s
.tried_cnt
);
1718 DBG_log("all %d public keys for '%Y' failed: "
1719 "best decrypted SIG payload into a malformed ECB",
1723 return STF_FAIL
+ ISAKMP_INVALID_KEY_INFORMATION
;
1727 static notification_t
accept_nonce(struct msg_digest
*md
, chunk_t
*dest
,
1730 pb_stream
*nonce_pbs
= &md
->chain
[ISAKMP_NEXT_NONCE
]->pbs
;
1731 size_t len
= pbs_left(nonce_pbs
);
1733 if (len
< MINIMUM_NONCE_SIZE
|| MAXIMUM_NONCE_SIZE
< len
)
1735 loglog(RC_LOG_SERIOUS
, "%s length not between %d and %d"
1736 , name
, MINIMUM_NONCE_SIZE
, MAXIMUM_NONCE_SIZE
);
1737 return ISAKMP_PAYLOAD_MALFORMED
; /* ??? */
1740 *dest
= chunk_create(nonce_pbs
->cur
, len
);
1741 *dest
= chunk_clone(*dest
);
1742 return ISAKMP_NOTHING_WRONG
;
1745 /* encrypt message, sans fixed part of header
1746 * IV is fetched from st->st_new_iv and stored into st->st_iv.
1747 * The theory is that there will be no "backing out", so we commit to IV.
1748 * We also close the pbs.
1750 bool encrypt_message(pb_stream
*pbs
, struct state
*st
)
1752 u_int8_t
*enc_start
= pbs
->start
+ sizeof(struct isakmp_hdr
);
1753 size_t enc_len
= pbs_offset(pbs
) - sizeof(struct isakmp_hdr
);
1756 size_t crypter_block_size
;
1757 encryption_algorithm_t enc_alg
;
1760 DBG_cond_dump(DBG_CRYPT
| DBG_RAW
, "encrypting:\n", enc_start
, enc_len
);
1761 enc_alg
= oakley_to_encryption_algorithm(st
->st_oakley
.encrypt
);
1762 crypter
= lib
->crypto
->create_crypter(lib
->crypto
, enc_alg
, st
->st_enc_key
.len
);
1763 crypter_block_size
= crypter
->get_block_size(crypter
);
1765 /* Pad up to multiple of encryption blocksize.
1766 * See the description associated with the definition of
1767 * struct isakmp_hdr in packet.h.
1770 size_t padding
= pad_up(enc_len
, crypter_block_size
);
1774 if (!out_zero(padding
, pbs
, "encryption padding"))
1780 DBG(DBG_CRYPT
, DBG_log("encrypting using %s", enum_show(&oakley_enc_names
, st
->st_oakley
.encrypt
)));
1781 data
= chunk_create(enc_start
, enc_len
);
1783 /* form iv by truncation */
1784 st
->st_new_iv_len
= crypter_block_size
;
1785 iv
= chunk_create(st
->st_new_iv
, st
->st_new_iv_len
);
1787 crypter
->set_key(crypter
, st
->st_enc_key
);
1788 crypter
->encrypt(crypter
, data
, iv
, NULL
);
1789 crypter
->destroy(crypter
);
1791 new_iv
= data
.ptr
+ data
.len
- crypter_block_size
;
1792 memcpy(st
->st_new_iv
, new_iv
, crypter_block_size
);
1794 DBG_cond_dump(DBG_CRYPT
, "next IV:", st
->st_iv
, st
->st_iv_len
);
1799 /* Compute HASH(1), HASH(2) of Quick Mode.
1800 * HASH(1) is part of Quick I1 message.
1801 * HASH(2) is part of Quick R1 message.
1802 * Used by: quick_outI1, quick_inI1_outR1 (twice), quick_inR1_outI2
1803 * (see RFC 2409 "IKE" 5.5, pg. 18 or draft-ietf-ipsec-ike-01.txt 6.2 pg 25)
1805 static size_t quick_mode_hash12(u_char
*dest
, u_char
*start
, u_char
*roof
,
1806 const struct state
*st
, const msgid_t
*msgid
,
1809 chunk_t msgid_chunk
= chunk_from_thing(*msgid
);
1810 chunk_t msg_chunk
= { start
, roof
- start
};
1811 pseudo_random_function_t prf_alg
;
1813 size_t prf_block_size
;
1815 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1816 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1817 prf
->set_key(prf
, st
->st_skeyid_a
);
1818 prf
->get_bytes(prf
, msgid_chunk
, NULL
);
1821 prf
->get_bytes(prf
, st
->st_ni
, NULL
); /* include Ni_b in the hash */
1823 prf
->get_bytes(prf
, msg_chunk
, dest
);
1824 prf_block_size
= prf
->get_block_size(prf
);
1828 DBG_log("HASH(%d) computed:", hash2
+ 1);
1829 DBG_dump("", dest
, prf_block_size
)
1831 return prf_block_size
;
1834 /* Compute HASH(3) in Quick Mode (part of Quick I2 message).
1835 * Used by: quick_inR1_outI2, quick_inI2
1836 * See RFC2409 "The Internet Key Exchange (IKE)" 5.5.
1837 * NOTE: this hash (unlike HASH(1) and HASH(2)) ONLY covers the
1838 * Message ID and Nonces. This is a mistake.
1840 static size_t quick_mode_hash3(u_char
*dest
, struct state
*st
)
1842 chunk_t seed_chunk
= chunk_from_chars(0x00);
1843 chunk_t msgid_chunk
= chunk_from_thing(st
->st_msgid
);
1844 pseudo_random_function_t prf_alg
;
1846 size_t prf_block_size
;
1848 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
1849 prf
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
1850 prf
->set_key(prf
, st
->st_skeyid_a
);
1851 prf
->get_bytes(prf
, seed_chunk
, NULL
);
1852 prf
->get_bytes(prf
, msgid_chunk
, NULL
);
1853 prf
->get_bytes(prf
, st
->st_ni
, NULL
);
1854 prf
->get_bytes(prf
, st
->st_nr
, dest
);
1855 prf_block_size
= prf
->get_block_size(prf
);
1858 DBG_cond_dump(DBG_CRYPT
, "HASH(3) computed:", dest
, prf_block_size
);
1859 return prf_block_size
;
1862 /* Compute Phase 2 IV.
1863 * Uses Phase 1 IV from st_iv; puts result in st_new_iv.
1865 void init_phase2_iv(struct state
*st
, const msgid_t
*msgid
)
1867 chunk_t iv_chunk
= { st
->st_ph1_iv
, st
->st_ph1_iv_len
};
1868 chunk_t msgid_chunk
= chunk_from_thing(*msgid
);
1869 hash_algorithm_t hash_alg
;
1872 hash_alg
= oakley_to_hash_algorithm(st
->st_oakley
.hash
);
1873 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
1875 DBG_cond_dump(DBG_CRYPT
, "last Phase 1 IV:",
1876 st
->st_ph1_iv
, st
->st_ph1_iv_len
);
1878 st
->st_new_iv_len
= hasher
->get_hash_size(hasher
);
1879 passert(st
->st_new_iv_len
<= sizeof(st
->st_new_iv
));
1881 hasher
->get_hash(hasher
, iv_chunk
, NULL
);
1882 hasher
->get_hash(hasher
, msgid_chunk
, st
->st_new_iv
);
1883 hasher
->destroy(hasher
);
1885 DBG_cond_dump(DBG_CRYPT
, "computed Phase 2 IV:",
1886 st
->st_new_iv
, st
->st_new_iv_len
);
1889 /* Initiate quick mode.
1890 * --> HDR*, HASH(1), SA, Nr [, KE ] [, IDci, IDcr ]
1891 * (see RFC 2409 "IKE" 5.5)
1892 * Note: this is not called from demux.c
1895 static bool emit_subnet_id(ip_subnet
*net
, u_int8_t np
, u_int8_t protoid
,
1896 u_int16_t port
, pb_stream
*outs
)
1898 struct isakmp_ipsec_id id
;
1901 const unsigned char *tbp
;
1905 id
.isaiid_idtype
= subnetishost(net
)
1906 ?
aftoinfo(subnettypeof(net
))->id_addr
1907 : aftoinfo(subnettypeof(net
))->id_subnet
;
1908 id
.isaiid_protoid
= protoid
;
1909 id
.isaiid_port
= port
;
1911 if (!out_struct(&id
, &isakmp_ipsec_identification_desc
, outs
, &id_pbs
))
1915 networkof(net
, &ta
);
1916 tal
= addrbytesptr(&ta
, &tbp
);
1917 if (!out_raw(tbp
, tal
, &id_pbs
, "client network"))
1921 if (!subnetishost(net
))
1924 tal
= addrbytesptr(&ta
, &tbp
);
1925 if (!out_raw(tbp
, tal
, &id_pbs
, "client mask"))
1930 close_output_pbs(&id_pbs
);
1934 stf_status
quick_outI1(int whack_sock
, struct state
*isakmp_sa
,
1935 connection_t
*c
, lset_t policy
, unsigned long try,
1936 so_serial_t replacing
)
1938 struct state
*st
= duplicate_state(isakmp_sa
);
1939 pb_stream reply
; /* not really a reply */
1941 u_char
/* set by START_HASH_PAYLOAD: */
1942 *r_hashval
, /* where in reply to jam hash value */
1943 *r_hash_start
; /* start of what is to be hashed */
1944 bool has_client
= c
->spd
.this.has_client
|| c
->spd
.that
.has_client
||
1945 c
->spd
.this.protocol
|| c
->spd
.that
.protocol
||
1946 c
->spd
.this.port
|| c
->spd
.that
.port
;
1948 bool send_natoa
= FALSE
;
1949 u_int8_t np
= ISAKMP_NEXT_NONE
;
1951 if (c
->spd
.this.modecfg
&& !c
->spd
.this.has_client
&&
1952 isanyaddr(&c
->spd
.this.host_srcip
))
1954 connection_t
*ph1_c
= isakmp_sa
->st_connection
;
1956 if (ph1_c
->spd
.this.modecfg
&& !isanyaddr(&ph1_c
->spd
.this.host_srcip
))
1958 char srcip
[ADDRTOT_BUF
];
1960 c
->spd
.this.host_srcip
= ph1_c
->spd
.this.host_srcip
;
1961 c
->spd
.this.client
= ph1_c
->spd
.this.client
;
1962 c
->spd
.this.has_client
= TRUE
;
1963 addrtot(&c
->spd
.this.host_srcip
, 0, srcip
, sizeof(srcip
));
1964 plog("inheriting virtual IP source address %s from ModeCfg", srcip
);
1968 st
->st_whack_sock
= whack_sock
;
1969 st
->st_connection
= c
;
1970 set_cur_state(st
); /* we must reset before exit */
1971 st
->st_policy
= policy
;
1974 st
->st_myuserprotoid
= c
->spd
.this.protocol
;
1975 st
->st_peeruserprotoid
= c
->spd
.that
.protocol
;
1976 st
->st_myuserport
= c
->spd
.this.port
;
1977 st
->st_peeruserport
= c
->spd
.that
.port
;
1979 st
->st_msgid
= generate_msgid(isakmp_sa
);
1980 st
->st_state
= STATE_QUICK_I1
;
1982 insert_state(st
); /* needs cookies, connection, and msgid */
1984 if (replacing
== SOS_NOBODY
)
1986 plog("initiating Quick Mode %s {using isakmp#%lu}",
1987 prettypolicy(policy
), isakmp_sa
->st_serialno
);
1991 plog("initiating Quick Mode %s to replace #%lu {using isakmp#%lu}",
1992 prettypolicy(policy
), replacing
, isakmp_sa
->st_serialno
);
1994 if (isakmp_sa
->nat_traversal
& NAT_T_DETECTED
)
1996 /* Duplicate nat_traversal status in new state */
1997 st
->nat_traversal
= isakmp_sa
->nat_traversal
;
1999 if (isakmp_sa
->nat_traversal
& LELEM(NAT_TRAVERSAL_NAT_BHND_ME
))
2003 nat_traversal_change_port_lookup(NULL
, st
);
2007 st
->nat_traversal
= 0;
2010 /* are we going to send a NAT-OA payload? */
2011 if ((st
->nat_traversal
& NAT_T_WITH_NATOA
)
2012 && !(st
->st_policy
& POLICY_TUNNEL
)
2013 && (st
->nat_traversal
& LELEM(NAT_TRAVERSAL_NAT_BHND_ME
)))
2016 np
= (st
->nat_traversal
& NAT_T_WITH_RFC_VALUES
) ?
2017 ISAKMP_NEXT_NATOA_RFC
: ISAKMP_NEXT_NATOA_DRAFTS
;
2021 init_pbs(&reply
, reply_buffer
, sizeof(reply_buffer
), "reply packet");
2025 struct isakmp_hdr hdr
;
2027 hdr
.isa_version
= ISAKMP_MAJOR_VERSION
<< ISA_MAJ_SHIFT
| ISAKMP_MINOR_VERSION
;
2028 hdr
.isa_np
= ISAKMP_NEXT_HASH
;
2029 hdr
.isa_xchg
= ISAKMP_XCHG_QUICK
;
2030 hdr
.isa_msgid
= st
->st_msgid
;
2031 hdr
.isa_flags
= ISAKMP_FLAG_ENCRYPTION
;
2032 memcpy(hdr
.isa_icookie
, st
->st_icookie
, COOKIE_SIZE
);
2033 memcpy(hdr
.isa_rcookie
, st
->st_rcookie
, COOKIE_SIZE
);
2034 if (!out_struct(&hdr
, &isakmp_hdr_desc
, &reply
, &rbody
))
2037 return STF_INTERNAL_ERROR
;
2041 /* HASH(1) -- create and note space to be filled later */
2042 START_HASH_PAYLOAD(rbody
, ISAKMP_NEXT_SA
);
2047 * See if pfs_group has been specified for this conn,
2048 * if not, fallback to old use-same-as-P1 behaviour
2051 if (st
->st_connection
)
2053 st
->st_pfs_group
= ike_alg_pfsgroup(st
->st_connection
, policy
);
2055 if (!st
->st_pfs_group
)
2057 /* If PFS specified, use the same group as during Phase 1:
2058 * since no negotiation is possible, we pick one that is
2059 * very likely supported.
2061 st
->st_pfs_group
= policy
& POLICY_PFS? isakmp_sa
->st_oakley
.group
: NULL
;
2063 /* Emit SA payload based on a subset of the policy bits.
2064 * POLICY_COMPRESS is considered iff we can do IPcomp.
2067 lset_t pm
= POLICY_ENCRYPT
| POLICY_AUTHENTICATE
;
2071 pm
|= POLICY_COMPRESS
;
2074 &ipsec_sadb
[(st
->st_policy
& pm
) >> POLICY_IPSEC_SHIFT
],
2075 st
, FALSE
, ISAKMP_NEXT_NONCE
))
2078 return STF_INTERNAL_ERROR
;
2083 if (!build_and_ship_nonce(&st
->st_ni
, &rbody
2084 , policy
& POLICY_PFS? ISAKMP_NEXT_KE
: has_client? ISAKMP_NEXT_ID
: np
2088 return STF_INTERNAL_ERROR
;
2091 /* [ KE ] out (for PFS) */
2093 if (st
->st_pfs_group
!= NULL
)
2095 if (!build_and_ship_KE(st
, &st
->st_gi
, st
->st_pfs_group
2096 , &rbody
, has_client? ISAKMP_NEXT_ID
: np
))
2099 return STF_INTERNAL_ERROR
;
2103 /* [ IDci, IDcr ] out */
2106 /* IDci (we are initiator), then IDcr (peer is responder) */
2107 if (!emit_subnet_id(&c
->spd
.this.client
2108 , ISAKMP_NEXT_ID
, st
->st_myuserprotoid
, st
->st_myuserport
, &rbody
)
2109 || !emit_subnet_id(&c
->spd
.that
.client
2110 , np
, st
->st_peeruserprotoid
, st
->st_peeruserport
, &rbody
))
2113 return STF_INTERNAL_ERROR
;
2117 /* Send NAT-OA if our address is NATed */
2120 if (!nat_traversal_add_natoa(ISAKMP_NEXT_NONE
, &rbody
, st
))
2123 return STF_INTERNAL_ERROR
;
2127 /* finish computing HASH(1), inserting it in output */
2128 (void) quick_mode_hash12(r_hashval
, r_hash_start
, rbody
.cur
2129 , st
, &st
->st_msgid
, FALSE
);
2131 /* encrypt message, except for fixed part of header */
2133 init_phase2_iv(isakmp_sa
, &st
->st_msgid
);
2134 st
->st_new_iv_len
= isakmp_sa
->st_new_iv_len
;
2135 memcpy(st
->st_new_iv
, isakmp_sa
->st_new_iv
, st
->st_new_iv_len
);
2137 if (!encrypt_message(&rbody
, st
))
2140 return STF_INTERNAL_ERROR
;
2143 /* save packet, now that we know its size */
2144 st
->st_tpacket
= chunk_create(reply
.start
, pbs_offset(&reply
));
2145 st
->st_tpacket
= chunk_clone(st
->st_tpacket
);
2147 /* send the packet */
2149 send_packet(st
, "quick_outI1");
2152 event_schedule(EVENT_RETRANSMIT
, EVENT_RETRANSMIT_DELAY_0
, st
);
2154 if (replacing
== SOS_NOBODY
)
2156 whack_log(RC_NEW_STATE
+ STATE_QUICK_I1
2158 , enum_name(&state_names
, st
->st_state
));
2162 whack_log(RC_NEW_STATE
+ STATE_QUICK_I1
2163 , "%s: initiate to replace #%lu"
2164 , enum_name(&state_names
, st
->st_state
)
2173 * Decode the CERT payload of Phase 1.
2175 static void decode_cert(struct msg_digest
*md
)
2177 struct payload_digest
*p
;
2179 for (p
= md
->chain
[ISAKMP_NEXT_CERT
]; p
!= NULL
; p
= p
->next
)
2181 struct isakmp_cert
*const cert
= &p
->payload
.cert
;
2184 blob
.ptr
= p
->pbs
.cur
;
2185 blob
.len
= pbs_left(&p
->pbs
);
2186 if (cert
->isacert_type
== CERT_X509_SIGNATURE
)
2188 cert_t x509cert
= cert_empty
;
2190 x509cert
.cert
= lib
->creds
->create(lib
->creds
,
2191 CRED_CERTIFICATE
, CERT_X509
,
2192 BUILD_BLOB_ASN1_DER
, blob
,
2196 if (verify_x509cert(&x509cert
, strict_crl_policy
, &valid_until
))
2199 DBG_log("Public key validated")
2201 add_public_key_from_cert(&x509cert
, valid_until
, DAL_SIGNED
);
2205 plog("X.509 certificate rejected");
2207 x509cert
.cert
->destroy(x509cert
.cert
);
2211 plog("Syntax error in X.509 certificate");
2214 else if (cert
->isacert_type
== CERT_PKCS7_WRAPPED_X509
)
2216 linked_list_t
*certs
= linked_list_create();
2218 if (pkcs7_parse_signedData(blob
, NULL
, certs
, NULL
, NULL
))
2220 store_x509certs(certs
, strict_crl_policy
);
2224 plog("Syntax error in PKCS#7 wrapped X.509 certificates");
2226 certs
->destroy_offset(certs
, offsetof(certificate_t
, destroy
));
2230 loglog(RC_LOG_SERIOUS
, "ignoring %s certificate payload",
2231 enum_show(&cert_type_names
, cert
->isacert_type
));
2232 DBG_cond_dump_chunk(DBG_PARSING
, "CERT:\n", blob
);
2238 * Decode the CR payload of Phase 1.
2240 static void decode_cr(struct msg_digest
*md
, connection_t
*c
)
2242 struct payload_digest
*p
;
2244 for (p
= md
->chain
[ISAKMP_NEXT_CR
]; p
!= NULL
; p
= p
->next
)
2246 struct isakmp_cr
*const cr
= &p
->payload
.cr
;
2249 ca_name
.len
= pbs_left(&p
->pbs
);
2250 ca_name
.ptr
= (ca_name
.len
> 0)? p
->pbs
.cur
: NULL
;
2252 DBG_cond_dump_chunk(DBG_PARSING
, "CR", ca_name
);
2254 if (cr
->isacr_type
== CERT_X509_SIGNATURE
)
2256 if (ca_name
.len
> 0)
2258 identification_t
*ca
;
2260 if (!is_asn1(ca_name
))
2264 if (c
->requested_ca
== NULL
)
2266 c
->requested_ca
= linked_list_create();
2268 ca
= identification_create_from_encoding(ID_DER_ASN1_DN
, ca_name
);
2269 c
->requested_ca
->insert_last(c
->requested_ca
, ca
);
2270 DBG(DBG_PARSING
| DBG_CONTROL
,
2271 DBG_log("requested CA: \"%Y\"", ca
)
2276 DBG(DBG_PARSING
| DBG_CONTROL
,
2277 DBG_log("requested CA: %%any")
2280 c
->got_certrequest
= TRUE
;
2284 loglog(RC_LOG_SERIOUS
, "ignoring %s certificate request payload",
2285 enum_show(&cert_type_names
, cr
->isacr_type
));
2290 /* Decode the ID payload of Phase 1 (main_inI3_outR3 and main_inR3)
2291 * Note: we may change connections as a result.
2292 * We must be called before SIG or HASH are decoded since we
2293 * may change the peer's public key or ID.
2295 static bool decode_peer_id(struct msg_digest
*md
, identification_t
**peer
)
2297 struct state
*const st
= md
->st
;
2298 struct payload_digest
*const id_pld
= md
->chain
[ISAKMP_NEXT_ID
];
2299 const pb_stream
*const id_pbs
= &id_pld
->pbs
;
2300 struct isakmp_id
*const id
= &id_pld
->payload
.id
;
2303 /* I think that RFC2407 (IPSEC DOI) 4.6.2 is confused.
2304 * It talks about the protocol ID and Port fields of the ID
2305 * Payload, but they don't exist as such in Phase 1.
2306 * We use more appropriate names.
2307 * isaid_doi_specific_a is in place of Protocol ID.
2308 * isaid_doi_specific_b is in place of Port.
2309 * Besides, there is no good reason for allowing these to be
2310 * other than 0 in Phase 1.
2312 if ((st
->nat_traversal
& NAT_T_WITH_PORT_FLOATING
)
2313 && id
->isaid_doi_specific_a
== IPPROTO_UDP
2314 && (id
->isaid_doi_specific_b
== 0 || id
->isaid_doi_specific_b
== NAT_T_IKE_FLOAT_PORT
))
2316 DBG_log("protocol/port in Phase 1 ID Payload is %d/%d. "
2317 "accepted with port_floating NAT-T",
2318 id
->isaid_doi_specific_a
, id
->isaid_doi_specific_b
);
2320 else if (!(id
->isaid_doi_specific_a
== 0 && id
->isaid_doi_specific_b
== 0)
2321 && !(id
->isaid_doi_specific_a
== IPPROTO_UDP
&& id
->isaid_doi_specific_b
== IKE_UDP_PORT
))
2323 loglog(RC_LOG_SERIOUS
, "protocol/port in Phase 1 ID Payload must be 0/0 or %d/%d"
2325 , IPPROTO_UDP
, IKE_UDP_PORT
2326 , id
->isaid_doi_specific_a
, id
->isaid_doi_specific_b
);
2330 id_payload
= chunk_create(id_pbs
->cur
, pbs_left(id_pbs
));
2332 switch (id
->isaid_idtype
)
2335 if (id_payload
.len
!= 4)
2337 loglog(RC_LOG_SERIOUS
, "improper %s Phase 1 ID payload",
2338 enum_show(&ident_names
, id
->isaid_idtype
));
2343 if (id_payload
.len
!= 16)
2345 loglog(RC_LOG_SERIOUS
, "improper %s Phase 1 ID payload",
2346 enum_show(&ident_names
, id
->isaid_idtype
));
2352 if (memchr(id_payload
.ptr
, '\0', id_payload
.len
) != NULL
)
2354 loglog(RC_LOG_SERIOUS
, "%s Phase 1 ID payload contains "
2356 enum_show(&ident_names
, id
->isaid_idtype
));
2361 case ID_DER_ASN1_DN
:
2364 /* XXX Could send notification back */
2365 loglog(RC_LOG_SERIOUS
, "unacceptable identity type (%s) "
2366 "in Phase 1 ID payload",
2367 enum_show(&ident_names
, id
->isaid_idtype
));
2370 *peer
= identification_create_from_encoding(id
->isaid_idtype
, id_payload
);
2372 plog("Peer ID is %s: '%Y'", enum_show(&ident_names
, id
->isaid_idtype
),
2375 /* check for certificates */
2380 /* Now that we've decoded the ID payload, let's see if we
2381 * need to switch connections.
2382 * We must not switch horses if we initiated:
2383 * - if the initiation was explicit, we'd be ignoring user's intent
2384 * - if opportunistic, we'll lose our HOLD info
2386 static bool switch_connection(struct msg_digest
*md
, identification_t
*peer
,
2389 struct state
*const st
= md
->st
;
2390 connection_t
*c
= st
->st_connection
;
2391 identification_t
*peer_ca
;
2393 peer_ca
= st
->st_peer_pubkey ? st
->st_peer_pubkey
->issuer
: NULL
;
2397 DBG_log("peer CA: \"%Y\"", peer_ca
)
2403 DBG_log("peer CA: %%none")
2411 if (!peer
->equals(peer
, c
->spd
.that
.id
))
2413 loglog(RC_LOG_SERIOUS
,
2414 "we require peer to have ID '%Y', but peer declares '%Y'",
2415 c
->spd
.that
.id
, peer
);
2422 DBG_log("required CA: \"%s\"", c
->spd
.that
.ca
);
2428 DBG_log("required CA: %%none");
2432 if (!trusted_ca(peer_ca
, c
->spd
.that
.ca
, &pathlen
))
2434 loglog(RC_LOG_SERIOUS
2435 , "we don't accept the peer's CA");
2443 /* check for certificate requests */
2446 r
= refine_host_connection(st
, peer
, peer_ca
);
2448 /* delete the collected certificate requests */
2449 if (c
->requested_ca
)
2451 c
->requested_ca
->destroy_offset(c
->requested_ca
,
2452 offsetof(identification_t
, destroy
));
2453 c
->requested_ca
= NULL
;
2458 loglog(RC_LOG_SERIOUS
, "no suitable connection for peer '%Y'", peer
);
2465 DBG_log("offered CA: \"%Y\"", r
->spd
.this.ca
)
2471 DBG_log("offered CA: %%none")
2477 /* apparently, r is an improvement on c -- replace */
2480 , DBG_log("switched from \"%s\" to \"%s\"", c
->name
, r
->name
));
2481 if (r
->kind
== CK_TEMPLATE
)
2483 /* instantiate it, filling in peer's ID */
2484 r
= rw_instantiate(r
, &c
->spd
.that
.host_addr
2485 , c
->spd
.that
.host_port
, NULL
, peer
);
2488 /* copy certificate request info */
2489 r
->got_certrequest
= c
->got_certrequest
;
2491 st
->st_connection
= r
; /* kill reference to c */
2492 set_cur_connection(r
);
2493 connection_discard(c
);
2495 else if (c
->spd
.that
.has_id_wildcards
)
2497 c
->spd
.that
.id
->destroy(c
->spd
.that
.id
);
2498 c
->spd
.that
.id
= peer
->clone(peer
);
2499 c
->spd
.that
.has_id_wildcards
= FALSE
;
2505 /* Decode the variable part of an ID packet (during Quick Mode).
2506 * This is designed for packets that identify clients, not peers.
2507 * Rejects 0.0.0.0/32 or IPv6 equivalent because
2508 * (1) it is wrong and (2) we use this value for inband signalling.
2510 static bool decode_net_id(struct isakmp_ipsec_id
*id
, pb_stream
*id_pbs
,
2511 ip_subnet
*net
, const char *which
)
2513 const struct af_info
*afi
= NULL
;
2515 /* Note: the following may be a pointer into static memory
2516 * that may be recycled, but only if the type is not known.
2517 * That case is disposed of very early -- in the first switch.
2519 const char *idtypename
= enum_show(&ident_names
, id
->isaiid_idtype
);
2521 switch (id
->isaiid_idtype
)
2524 case ID_IPV4_ADDR_SUBNET
:
2525 case ID_IPV4_ADDR_RANGE
:
2526 afi
= &af_inet4_info
;
2529 case ID_IPV6_ADDR_SUBNET
:
2530 case ID_IPV6_ADDR_RANGE
:
2531 afi
= &af_inet6_info
;
2536 /* XXX support more */
2537 loglog(RC_LOG_SERIOUS
, "unsupported ID type %s"
2539 /* XXX Could send notification back */
2543 switch (id
->isaiid_idtype
)
2548 ip_address temp_address
;
2551 ugh
= initaddr(id_pbs
->cur
, pbs_left(id_pbs
), afi
->af
, &temp_address
);
2555 loglog(RC_LOG_SERIOUS
, "%s ID payload %s has wrong length in Quick I1 (%s)"
2556 , which
, idtypename
, ugh
);
2557 /* XXX Could send notification back */
2560 if (isanyaddr(&temp_address
))
2562 loglog(RC_LOG_SERIOUS
, "%s ID payload %s is invalid (%s) in Quick I1"
2563 , which
, idtypename
, ip_str(&temp_address
));
2564 /* XXX Could send notification back */
2567 happy(addrtosubnet(&temp_address
, net
));
2568 DBG(DBG_PARSING
| DBG_CONTROL
2569 , DBG_log("%s is %s", which
, ip_str(&temp_address
)));
2573 case ID_IPV4_ADDR_SUBNET
:
2574 case ID_IPV6_ADDR_SUBNET
:
2576 ip_address temp_address
, temp_mask
;
2579 if (pbs_left(id_pbs
) != 2 * afi
->ia_sz
)
2581 loglog(RC_LOG_SERIOUS
, "%s ID payload %s wrong length in Quick I1"
2582 , which
, idtypename
);
2583 /* XXX Could send notification back */
2586 ugh
= initaddr(id_pbs
->cur
2587 , afi
->ia_sz
, afi
->af
, &temp_address
);
2590 ugh
= initaddr(id_pbs
->cur
+ afi
->ia_sz
2591 , afi
->ia_sz
, afi
->af
, &temp_mask
);
2595 ugh
= initsubnet(&temp_address
, masktocount(&temp_mask
)
2598 if (ugh
== NULL
&& subnetisnone(net
))
2600 ugh
= "contains only anyaddr";
2604 loglog(RC_LOG_SERIOUS
, "%s ID payload %s bad subnet in Quick I1 (%s)"
2605 , which
, idtypename
, ugh
);
2606 /* XXX Could send notification back */
2609 DBG(DBG_PARSING
| DBG_CONTROL
,
2611 char temp_buff
[SUBNETTOT_BUF
];
2613 subnettot(net
, 0, temp_buff
, sizeof(temp_buff
));
2614 DBG_log("%s is subnet %s", which
, temp_buff
);
2619 case ID_IPV4_ADDR_RANGE
:
2620 case ID_IPV6_ADDR_RANGE
:
2622 ip_address temp_address_from
, temp_address_to
;
2625 if (pbs_left(id_pbs
) != 2 * afi
->ia_sz
)
2627 loglog(RC_LOG_SERIOUS
, "%s ID payload %s wrong length in Quick I1"
2628 , which
, idtypename
);
2629 /* XXX Could send notification back */
2632 ugh
= initaddr(id_pbs
->cur
, afi
->ia_sz
, afi
->af
, &temp_address_from
);
2635 ugh
= initaddr(id_pbs
->cur
+ afi
->ia_sz
2636 , afi
->ia_sz
, afi
->af
, &temp_address_to
);
2640 loglog(RC_LOG_SERIOUS
, "%s ID payload %s malformed (%s) in Quick I1"
2641 , which
, idtypename
, ugh
);
2642 /* XXX Could send notification back */
2646 ugh
= rangetosubnet(&temp_address_from
, &temp_address_to
, net
);
2647 if (ugh
== NULL
&& subnetisnone(net
))
2649 ugh
= "contains only anyaddr";
2653 char temp_buff1
[ADDRTOT_BUF
], temp_buff2
[ADDRTOT_BUF
];
2655 addrtot(&temp_address_from
, 0, temp_buff1
, sizeof(temp_buff1
));
2656 addrtot(&temp_address_to
, 0, temp_buff2
, sizeof(temp_buff2
));
2657 loglog(RC_LOG_SERIOUS
, "%s ID payload in Quick I1, %s"
2658 " %s - %s unacceptable: %s"
2659 , which
, idtypename
, temp_buff1
, temp_buff2
, ugh
);
2662 DBG(DBG_PARSING
| DBG_CONTROL
,
2664 char temp_buff
[SUBNETTOT_BUF
];
2666 subnettot(net
, 0, temp_buff
, sizeof(temp_buff
));
2667 DBG_log("%s is subnet %s (received as range)"
2668 , which
, temp_buff
);
2674 /* set the port selector */
2675 setportof(htons(id
->isaiid_port
), &net
->addr
);
2677 DBG(DBG_PARSING
| DBG_CONTROL
,
2678 DBG_log("%s protocol/port is %d/%d", which
, id
->isaiid_protoid
, id
->isaiid_port
)
2684 /* like decode, but checks that what is received matches what was sent */
2685 static bool check_net_id(struct isakmp_ipsec_id
*id
, pb_stream
*id_pbs
,
2686 u_int8_t
*protoid
, u_int16_t
*port
, ip_subnet
*net
,
2691 if (!decode_net_id(id
, id_pbs
, &net_temp
, which
))
2695 if (!samesubnet(net
, &net_temp
)
2696 || *protoid
!= id
->isaiid_protoid
|| *port
!= id
->isaiid_port
)
2698 loglog(RC_LOG_SERIOUS
, "%s ID returned doesn't match my proposal", which
);
2705 * look for the existence of a non-expiring preloaded public key
2707 static bool has_preloaded_public_key(struct state
*st
)
2709 connection_t
*c
= st
->st_connection
;
2711 /* do not consider rw connections since
2712 * the peer's identity must be known
2714 if (c
->kind
== CK_PERMANENT
)
2718 /* look for a matching RSA public key */
2719 for (p
= pubkeys
; p
!= NULL
; p
= p
->next
)
2721 pubkey_t
*key
= p
->key
;
2722 key_type_t type
= key
->public_key
->get_type(key
->public_key
);
2724 if (type
== KEY_RSA
&&
2725 c
->spd
.that
.id
->equals(c
->spd
.that
.id
, key
->id
) &&
2726 key
->until_time
== UNDEFINED_TIME
)
2728 /* found a preloaded public key */
2737 * Produce the new key material of Quick Mode.
2738 * RFC 2409 "IKE" section 5.5
2739 * specifies how this is to be done.
2741 static void compute_proto_keymat(struct state
*st
, u_int8_t protoid
,
2742 struct ipsec_proto_info
*pi
)
2744 size_t needed_len
= 0; /* bytes of keying material needed */
2746 /* Add up the requirements for keying material
2747 * (It probably doesn't matter if we produce too much!)
2751 case PROTO_IPSEC_ESP
:
2753 needed_len
= kernel_alg_esp_enc_keylen(pi
->attrs
.transid
);
2755 if (needed_len
&& pi
->attrs
.key_len
)
2757 needed_len
= pi
->attrs
.key_len
/ BITS_PER_BYTE
;
2760 switch (pi
->attrs
.transid
)
2766 case ESP_AES_CCM_12
:
2767 case ESP_AES_CCM_16
:
2771 case ESP_AES_GCM_12
:
2772 case ESP_AES_GCM_16
:
2778 if (needed_len
== 0)
2780 bad_case(pi
->attrs
.transid
);
2784 if (kernel_alg_esp_auth_ok(pi
->attrs
.auth
, NULL
))
2786 needed_len
+= kernel_alg_esp_auth_keylen(pi
->attrs
.auth
);
2790 switch (pi
->attrs
.auth
)
2792 case AUTH_ALGORITHM_NONE
:
2794 case AUTH_ALGORITHM_HMAC_MD5
:
2795 needed_len
+= HMAC_MD5_KEY_LEN
;
2797 case AUTH_ALGORITHM_HMAC_SHA1
:
2798 needed_len
+= HMAC_SHA1_KEY_LEN
;
2800 case AUTH_ALGORITHM_DES_MAC
:
2802 bad_case(pi
->attrs
.auth
);
2807 case PROTO_IPSEC_AH
:
2809 switch (pi
->attrs
.transid
)
2812 needed_len
= HMAC_MD5_KEY_LEN
;
2815 needed_len
= HMAC_SHA1_KEY_LEN
;
2818 bad_case(pi
->attrs
.transid
);
2826 pi
->keymat_len
= needed_len
;
2828 /* Allocate space for the keying material. Although only needed_len bytes
2829 * are desired, we must round up to a multiple of hash_size
2830 * so that our buffer isn't overrun.
2833 size_t needed_space
; /* space needed for keying material (rounded up) */
2834 size_t i
, prf_block_size
;
2835 chunk_t protoid_chunk
= chunk_from_thing(protoid
);
2836 chunk_t spi_our
= chunk_from_thing(pi
->our_spi
);
2837 chunk_t spi_peer
= chunk_from_thing(pi
->attrs
.spi
);
2838 pseudo_random_function_t prf_alg
;
2839 prf_t
*prf_our
, *prf_peer
;
2841 prf_alg
= oakley_to_prf(st
->st_oakley
.hash
);
2842 prf_our
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
2843 prf_peer
= lib
->crypto
->create_prf(lib
->crypto
, prf_alg
);
2844 prf_our
->set_key(prf_our
, st
->st_skeyid_d
);
2845 prf_peer
->set_key(prf_peer
, st
->st_skeyid_d
);
2846 prf_block_size
= prf_our
->get_block_size(prf_our
);
2848 needed_space
= needed_len
+ pad_up(needed_len
, prf_block_size
);
2849 replace(pi
->our_keymat
, malloc(needed_space
));
2850 replace(pi
->peer_keymat
, malloc(needed_space
));
2854 char *keymat_i_our
= pi
->our_keymat
+ i
;
2855 char *keymat_i_peer
= pi
->peer_keymat
+ i
;
2856 chunk_t keymat_our
= { keymat_i_our
, prf_block_size
};
2857 chunk_t keymat_peer
= { keymat_i_peer
, prf_block_size
};
2859 if (st
->st_shared
.ptr
!= NULL
)
2861 /* PFS: include the g^xy */
2862 prf_our
->get_bytes(prf_our
, st
->st_shared
, NULL
);
2863 prf_peer
->get_bytes(prf_peer
, st
->st_shared
, NULL
);
2865 prf_our
->get_bytes(prf_our
, protoid_chunk
, NULL
);
2866 prf_peer
->get_bytes(prf_peer
, protoid_chunk
, NULL
);
2868 prf_our
->get_bytes(prf_our
, spi_our
, NULL
);
2869 prf_peer
->get_bytes(prf_peer
, spi_peer
, NULL
);
2871 prf_our
->get_bytes(prf_our
, st
->st_ni
, NULL
);
2872 prf_peer
->get_bytes(prf_peer
, st
->st_ni
, NULL
);
2874 prf_our
->get_bytes(prf_our
, st
->st_nr
, keymat_i_our
);
2875 prf_peer
->get_bytes(prf_peer
, st
->st_nr
, keymat_i_peer
);
2877 i
+= prf_block_size
;
2878 if (i
>= needed_space
)
2883 /* more keying material needed: prepare to go around again */
2884 prf_our
->get_bytes(prf_our
, keymat_our
, NULL
);
2885 prf_peer
->get_bytes(prf_peer
, keymat_peer
, NULL
);
2887 prf_our
->destroy(prf_our
);
2888 prf_peer
->destroy(prf_peer
);
2891 DBG_dump("KEYMAT computed:\n", pi
->our_keymat
, pi
->keymat_len
);
2892 DBG_dump("Peer KEYMAT computed:\n", pi
->peer_keymat
, pi
->keymat_len
));
2895 static void compute_keymats(struct state
*st
)
2897 if (st
->st_ah
.present
)
2899 compute_proto_keymat(st
, PROTO_IPSEC_AH
, &st
->st_ah
);
2901 if (st
->st_esp
.present
)
2903 compute_proto_keymat(st
, PROTO_IPSEC_ESP
, &st
->st_esp
);
2907 static bool uses_pubkey_auth(int auth
)
2911 case OAKLEY_RSA_SIG
:
2912 case OAKLEY_ECDSA_SIG
:
2913 case OAKLEY_ECDSA_256
:
2914 case OAKLEY_ECDSA_384
:
2915 case OAKLEY_ECDSA_521
:
2924 /* build an ID payload
2925 * Note: no memory is allocated for the body of the payload (tl->ptr).
2926 * We assume it will end up being a pointer into a sufficiently
2927 * stable datastructure. It only needs to last a short time.
2929 static void build_id_payload(struct isakmp_ipsec_id
*hd
, chunk_t
*tl
, struct end
*end
)
2931 identification_t
*id
= resolve_myid(end
->id
);
2934 hd
->isaiid_idtype
= id
->get_type(id
);
2936 switch (id
->get_type(id
))
2939 hd
->isaiid_idtype
= aftoinfo(addrtypeof(&end
->host_addr
))->id_addr
;
2940 tl
->len
= addrbytesptr(&end
->host_addr
,
2941 (const unsigned char **)&tl
->ptr
); /* sets tl->ptr too */
2947 case ID_DER_ASN1_DN
:
2949 *tl
= id
->get_encoding(id
);
2952 bad_case(id
->get_type(id
));
2956 /* State Transition Functions.
2958 * The definition of state_microcode_table in demux.c is a good
2959 * overview of these routines.
2961 * - Called from process_packet; result handled by complete_state_transition
2962 * - struct state_microcode member "processor" points to these
2963 * - these routine definitionss are in state order
2964 * - these routines must be restartable from any point of error return:
2965 * beware of memory allocated before any error.
2966 * - output HDR is usually emitted by process_packet (if state_microcode
2967 * member first_out_payload isn't ISAKMP_NEXT_NONE).
2969 * The transition functions' functions include:
2970 * - process and judge payloads
2971 * - update st_iv (result of decryption is in st_new_iv)
2972 * - build reply packet
2975 /* Handle a Main Mode Oakley first packet (responder side).
2978 stf_status
main_inI1_outR1(struct msg_digest
*md
)
2980 struct payload_digest
*const sa_pd
= md
->chain
[ISAKMP_NEXT_SA
];
2983 struct isakmp_proposal proposal
;
2984 pb_stream proposal_pbs
;
2986 u_int32_t ipsecdoisit
;
2987 lset_t policy
= LEMPTY
;
2988 int vids_to_send
= 0;
2990 /* We preparse the peer's proposal in order to determine
2991 * the requested authentication policy (RSA or PSK)
2993 RETURN_STF_FAILURE(preparse_isakmp_sa_body(&sa_pd
->payload
.sa
2994 , &sa_pd
->pbs
, &ipsecdoisit
, &proposal_pbs
, &proposal
));
2996 backup_pbs(&proposal_pbs
);
2997 RETURN_STF_FAILURE(parse_isakmp_policy(&proposal_pbs
2998 , proposal
.isap_notrans
, &policy
));
2999 restore_pbs(&proposal_pbs
);
3001 /* We are only considering candidate connections that match
3002 * the requested authentication policy (RSA or PSK)
3004 c
= find_host_connection(&md
->iface
->addr
, pluto_port
3005 , &md
->sender
, md
->sender_port
, policy
);
3007 if (c
== NULL
&& md
->iface
->ike_float
)
3009 c
= find_host_connection(&md
->iface
->addr
, NAT_T_IKE_FLOAT_PORT
3010 , &md
->sender
, md
->sender_port
, policy
);
3015 /* See if a wildcarded connection can be found.
3016 * We cannot pick the right connection, so we're making a guess.
3017 * All Road Warrior connections are fair game:
3018 * we pick the first we come across (if any).
3019 * If we don't find any, we pick the first opportunistic
3020 * with the smallest subnet that includes the peer.
3021 * There is, of course, no necessary relationship between
3022 * an Initiator's address and that of its client,
3023 * but Food Groups kind of assumes one.
3028 d
= find_host_connection(&md
->iface
->addr
3029 , pluto_port
, (ip_address
*)NULL
, md
->sender_port
, policy
);
3031 for (; d
!= NULL
; d
= d
->hp_next
)
3033 if (d
->kind
== CK_GROUP
)
3039 if (d
->kind
== CK_TEMPLATE
&& !(d
->policy
& POLICY_OPPO
))
3041 /* must be Road Warrior: we have a winner */
3046 /* Opportunistic or Shunt: pick tightest match */
3047 if (addrinsubnet(&md
->sender
, &d
->spd
.that
.client
)
3048 && (c
== NULL
|| !subnetinsubnet(&c
->spd
.that
.client
, &d
->spd
.that
.client
)))
3056 loglog(RC_LOG_SERIOUS
, "initial Main Mode message received on %s:%u"
3057 " but no connection has been authorized%s%s"
3058 , ip_str(&md
->iface
->addr
), ntohs(portof(&md
->iface
->addr
))
3059 , (policy
!= LEMPTY
) ?
" with policy=" : ""
3060 , (policy
!= LEMPTY
) ?
bitnamesof(sa_policy_bit_names
, policy
) : "");
3061 /* XXX notification is in order! */
3064 else if (c
->kind
!= CK_TEMPLATE
)
3066 loglog(RC_LOG_SERIOUS
, "initial Main Mode message received on %s:%u"
3067 " but \"%s\" forbids connection"
3068 , ip_str(&md
->iface
->addr
), pluto_port
, c
->name
);
3069 /* XXX notification is in order! */
3074 /* Create a temporary connection that is a copy of this one.
3075 * His ID isn't declared yet.
3077 c
= rw_instantiate(c
, &md
->sender
, md
->sender_port
, NULL
, NULL
);
3080 else if (c
->kind
== CK_TEMPLATE
)
3082 /* Create an instance
3083 * This is a rare case: wildcard peer ID but static peer IP address
3085 c
= rw_instantiate(c
, &md
->sender
, md
->sender_port
, NULL
, c
->spd
.that
.id
);
3089 md
->st
= st
= new_state();
3090 st
->st_connection
= c
;
3091 set_cur_state(st
); /* (caller will reset cur_state) */
3092 st
->st_try
= 0; /* not our job to try again from start */
3093 st
->st_policy
= c
->policy
& ~POLICY_IPSEC_MASK
; /* only as accurate as connection */
3095 memcpy(st
->st_icookie
, md
->hdr
.isa_icookie
, COOKIE_SIZE
);
3096 get_cookie(FALSE
, st
->st_rcookie
, COOKIE_SIZE
, &md
->sender
);
3098 insert_state(st
); /* needs cookies, connection, and msgid (0) */
3100 st
->st_doi
= ISAKMP_DOI_IPSEC
;
3101 st
->st_situation
= SIT_IDENTITY_ONLY
; /* We only support this */
3103 if ((c
->kind
== CK_INSTANCE
) && (c
->spd
.that
.host_port
!= pluto_port
))
3105 plog("responding to Main Mode from unknown peer %s:%u"
3106 , ip_str(&c
->spd
.that
.host_addr
), c
->spd
.that
.host_port
);
3108 else if (c
->kind
== CK_INSTANCE
)
3110 plog("responding to Main Mode from unknown peer %s"
3111 , ip_str(&c
->spd
.that
.host_addr
));
3115 plog("responding to Main Mode");
3118 /* parse_isakmp_sa also spits out a winning SA into our reply,
3119 * so we have to build our md->reply and emit HDR before calling it.
3122 /* determine how many Vendor ID payloads we will be sending */
3127 if (SEND_CISCO_UNITY_VID
)
3139 /* always send DPD Vendor ID */
3141 if (md
->nat_traversal_vid
&& nat_traversal_enabled
)
3147 * We can't leave this to comm_handle() because we must
3148 * fill in the cookie.
3151 struct isakmp_hdr r_hdr
= md
->hdr
;
3153 r_hdr
.isa_flags
&= ~ISAKMP_FLAG_COMMIT
; /* we won't ever turn on this bit */
3154 memcpy(r_hdr
.isa_rcookie
, st
->st_rcookie
, COOKIE_SIZE
);
3155 r_hdr
.isa_np
= ISAKMP_NEXT_SA
;
3156 if (!out_struct(&r_hdr
, &isakmp_hdr_desc
, &md
->reply
, &md
->rbody
))
3157 return STF_INTERNAL_ERROR
;
3160 /* start of SA out */
3162 struct isakmp_sa r_sa
= sa_pd
->payload
.sa
;
3164 r_sa
.isasa_np
= vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
;
3166 if (!out_struct(&r_sa
, &isakmp_sa_desc
, &md
->rbody
, &r_sa_pbs
))
3167 return STF_INTERNAL_ERROR
;
3170 /* SA body in and out */
3171 RETURN_STF_FAILURE(parse_isakmp_sa_body(ipsecdoisit
, &proposal_pbs
3172 ,&proposal
, &r_sa_pbs
, st
, FALSE
));
3174 /* if enabled send Pluto Vendor ID */
3177 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
3178 , &md
->rbody
, VID_STRONGSWAN
))
3180 return STF_INTERNAL_ERROR
;
3184 /* if enabled send Cisco Unity Vendor ID */
3185 if (SEND_CISCO_UNITY_VID
)
3187 if (!out_vendorid(vids_to_send
-- ? ISAKMP_NEXT_VID
: ISAKMP_NEXT_NONE
3188 , &md
->rbody
, VID_CISCO_UNITY
))
3190 return STF_INTERNAL_ERROR
;
3195 * if the peer sent an OpenPGP Vendor ID we offer the same capability