1 /* IPSec VPN client compatible with Cisco equipment.
2 Copyright (C) 2002 Geoffrey Keating
3 Copyright (C) 2003-2004 Maurice Massar
4 Copyright (C) 2004 Tomas Mraz
5 Copyright (C) 2004 Martin von Gagern
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/fcntl.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <arpa/inet.h>
37 #include <sys/ioctl.h>
38 #include <sys/utsname.h>
44 #include "isakmp-pkt.h"
45 #include "math_group.h"
49 #define ISAKMP_PORT (500)
51 struct sa_block oursa
[1];
53 static int sockfd
= -1;
54 static struct sockaddr
*dest_addr
;
55 static uint16_t local_port
; /* in network byte order */
56 static uint16_t encap_mode
= IPSEC_ENCAP_TUNNEL
;
57 static int timeout
= 5000; /* 5 seconds */
58 static uint8_t *resend_hash
= NULL
;
60 static uint8_t r_packet
[2048];
61 static ssize_t r_length
;
63 extern void vpnc_doit(unsigned long tous_spi
,
64 const unsigned char *tous_key
,
65 struct sockaddr_in
*tous_dest
,
66 unsigned long tothem_spi
,
67 const unsigned char *tothem_key
,
68 struct sockaddr_in
*tothem_dest
,
69 int tun_fd
, int md_algo
, int cry_algo
,
70 uint8_t * kill_packet_p
, size_t kill_packet_size_p
,
71 struct sockaddr
*kill_dest_p
,
72 uint16_t encap_mode
, int isakmp_fd
,
75 extern int find_local_addr(struct sockaddr_in
*dest
,
76 struct sockaddr_in
*source
);
92 supported_algo_t supp_dh_group
[] = {
93 {"nopfs", 0, 0, 0, 0},
94 {"dh1", OAKLEY_GRP_1
, IKE_GROUP_MODP_768
, IKE_GROUP_MODP_768
, 0},
95 {"dh2", OAKLEY_GRP_2
, IKE_GROUP_MODP_1024
, IKE_GROUP_MODP_1024
, 0},
96 {"dh5", OAKLEY_GRP_5
, IKE_GROUP_MODP_1536
, IKE_GROUP_MODP_1536
, 0},
97 /*{ "dh7", OAKLEY_GRP_7, IKE_GROUP_EC2N_163K, IKE_GROUP_EC2N_163K, 0 } note: code missing */
101 supported_algo_t supp_hash
[] = {
102 {"md5", GCRY_MD_MD5
, IKE_HASH_MD5
, IPSEC_AUTH_HMAC_MD5
, 0},
103 {"sha1", GCRY_MD_SHA1
, IKE_HASH_SHA
, IPSEC_AUTH_HMAC_SHA
, 0},
107 supported_algo_t supp_crypt
[] = {
108 /*note: working, but not recommended */
109 {"des", GCRY_CIPHER_DES
, IKE_ENC_DES_CBC
, ISAKMP_IPSEC_ESP_DES
, 0},
110 {"3des", GCRY_CIPHER_3DES
, IKE_ENC_3DES_CBC
, ISAKMP_IPSEC_ESP_3DES
, 0},
111 {"aes128", GCRY_CIPHER_AES128
, IKE_ENC_AES_CBC
, ISAKMP_IPSEC_ESP_AES
, 128},
112 {"aes192", GCRY_CIPHER_AES192
, IKE_ENC_AES_CBC
, ISAKMP_IPSEC_ESP_AES
, 192},
113 {"aes256", GCRY_CIPHER_AES256
, IKE_ENC_AES_CBC
, ISAKMP_IPSEC_ESP_AES
, 256},
117 supported_algo_t supp_auth
[] = {
118 {"psk", 0, IKE_AUTH_PRESHARED
, 0, 0},
119 {"psk+xauth", 0, IKE_AUTH_XAUTHInitPreShared
, 0, 0},
123 const supported_algo_t
*get_algo(enum algo_group what
, enum supp_algo_key key
, int id
,
124 const char *name
, int keylen
)
126 supported_algo_t
*sa
= NULL
;
128 const char *valname
= NULL
;
131 case SUPP_ALGO_DH_GROUP
:
137 case SUPP_ALGO_CRYPT
:
147 for (i
= 0; sa
[i
].name
!= NULL
; i
++) {
150 valname
= sa
[i
].name
;
152 case SUPP_ALGO_MY_ID
:
155 case SUPP_ALGO_IKE_SA
:
156 val
= sa
[i
].ike_sa_id
;
158 case SUPP_ALGO_IPSEC_SA
:
159 val
= sa
[i
].ipsec_sa_id
;
164 if ((key
== SUPP_ALGO_NAME
) ? !strcasecmp(name
, valname
) : (val
== id
))
165 if (keylen
== sa
[i
].keylen
)
172 const supported_algo_t
*get_dh_group_ike(void)
174 return get_algo(SUPP_ALGO_DH_GROUP
, SUPP_ALGO_NAME
, 0, config
[CONFIG_IKE_DH
], 0);
176 const supported_algo_t
*get_dh_group_ipsec(int server_setting
)
178 const char *pfs_setting
= config
[CONFIG_IPSEC_PFS
];
180 if (!strcmp(config
[CONFIG_IPSEC_PFS
], "server")) {
181 /* treat server_setting == -1 (unknown) as 0 */
182 pfs_setting
= (server_setting
== 1) ? "dh2" : "nopfs";
185 return get_algo(SUPP_ALGO_DH_GROUP
, SUPP_ALGO_NAME
, 0, pfs_setting
, 0);
188 static __inline__
int min(int a
, int b
)
190 return (a
< b
) ? a
: b
;
193 static void addenv(const void *name
, const char *value
)
195 char *strbuf
= NULL
, *oldval
;
197 oldval
= getenv(name
);
198 if (oldval
!= NULL
) {
199 strbuf
= xallocc(strlen(oldval
) + 1 + strlen(value
) + 1);
200 strcat(strbuf
, oldval
);
202 strcat(strbuf
, value
);
205 setenv(name
, strbuf
? strbuf
: value
, 1);
211 static void addenv_ipv4(const void *name
, uint8_t * data
)
213 addenv(name
, inet_ntoa(*((struct in_addr
*)data
)));
216 static int make_socket(uint16_t port
)
219 struct sockaddr_in name
;
221 /* Create the socket. */
222 sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
224 error(1, errno
, "making socket");
226 /* Give the socket a name. */
227 name
.sin_family
= AF_INET
;
228 name
.sin_port
= port
;
229 name
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
230 if (bind(sock
, (struct sockaddr
*)&name
, sizeof(name
)) < 0)
231 error(1, errno
, "binding to port %d", ntohs(port
));
236 static struct sockaddr
*init_sockaddr(const char *hostname
, uint16_t port
)
238 struct hostent
*hostinfo
;
239 struct sockaddr_in
*result
;
241 result
= malloc(sizeof(struct sockaddr_in
));
243 error(1, errno
, "out of memory");
245 result
->sin_family
= AF_INET
;
246 result
->sin_port
= htons(port
);
247 if (inet_aton(hostname
, &result
->sin_addr
) == 0) {
248 hostinfo
= gethostbyname(hostname
);
249 if (hostinfo
== NULL
)
250 error(1, 0, "unknown host `%s'\n", hostname
);
251 result
->sin_addr
= *(struct in_addr
*)hostinfo
->h_addr
;
253 return (struct sockaddr
*)result
;
256 static void setup_tunnel(struct sa_block
*s
)
258 if (config
[CONFIG_IF_NAME
])
259 memcpy(s
->tun_name
, config
[CONFIG_IF_NAME
], strlen(config
[CONFIG_IF_NAME
]));
261 s
->tun_fd
= tun_open(s
->tun_name
);
262 DEBUG(2, printf("using interface %s\n", s
->tun_name
));
265 error(1, errno
, "can't initialise tunnel interface");
268 void config_tunnel(struct sa_block
*s
)
270 setenv("TUNDEV", s
->tun_name
, 1);
271 setenv("VPNGATEWAY", inet_ntoa(((struct sockaddr_in
*)dest_addr
)->sin_addr
), 1);
273 system(config
[CONFIG_CONFIG_SCRIPT
]);
276 static int recv_ignore_dup(void *recvbuf
, size_t recvbufsize
)
278 uint8_t *resend_check_hash
;
279 int recvsize
, hash_len
;
280 struct sockaddr_in recvaddr
;
281 socklen_t recvaddr_size
= sizeof(recvaddr
);
284 recvsize
= recvfrom(sockfd
, recvbuf
, recvbufsize
, 0,
285 (struct sockaddr
*)&recvaddr
, &recvaddr_size
);
287 error(1, errno
, "receiving packet");
289 if (recvaddr_size
!= sizeof(recvaddr
)
290 || recvaddr
.sin_family
!= dest_addr
->sa_family
291 || recvaddr
.sin_port
!= ((struct sockaddr_in
*)dest_addr
)->sin_port
292 || memcmp(&recvaddr
.sin_addr
,
293 &((struct sockaddr_in
*)dest_addr
)->sin_addr
,
294 sizeof(struct in_addr
)) != 0) {
295 error(0, 0, "got response from unknown host %s:%d",
296 inet_ntop(recvaddr
.sin_family
, &recvaddr
.sin_addr
,
297 ntop_buf
, sizeof(ntop_buf
)), ntohs(recvaddr
.sin_port
));
301 hash_len
= gcry_md_get_algo_dlen(GCRY_MD_SHA1
);
302 resend_check_hash
= malloc(hash_len
);
303 gcry_md_hash_buffer(GCRY_MD_SHA1
, resend_check_hash
, recvbuf
, recvsize
);
304 if (resend_hash
&& memcmp(resend_hash
, resend_check_hash
, hash_len
) == 0) {
305 free(resend_check_hash
);
309 resend_hash
= resend_check_hash
;
311 memcpy(resend_hash
, resend_check_hash
, hash_len
);
312 free(resend_check_hash
);
318 /* Send TOSEND of size SENDSIZE to the socket. Then wait for a new packet,
319 resending TOSEND on timeout, and ignoring duplicate packets; the
320 new packet is put in RECVBUF of size RECVBUFSIZE and the actual size
321 of the new packet is returned. */
324 sendrecv(void *recvbuf
, size_t recvbufsize
, void *tosend
, size_t sendsize
, int sendonly
)
329 time_t start
= time(NULL
);
337 if ((tosend
!= NULL
)&&(encap_mode
!= IPSEC_ENCAP_TUNNEL
)) {
338 DEBUG(2, printf("NAT-T mode, adding non-esp marker\n"));
339 realtosend
= xallocc(sendsize
+4);
340 memcpy(realtosend
+4, tosend
, sendsize
);
349 if (realtosend
!= NULL
)
350 if (sendto(sockfd
, realtosend
, sendsize
, 0,
351 dest_addr
, sizeof(struct sockaddr_in
)) != (int)sendsize
)
352 error(1, errno
, "can't send packet");
357 pollresult
= poll(&pfd
, 1, timeout
<< tries
);
358 } while (pollresult
== -1 && errno
== EINTR
);
360 if (pollresult
== -1)
361 error(1, errno
, "can't poll socket");
362 if (pollresult
!= 0) {
363 recvsize
= recv_ignore_dup(recvbuf
, recvbufsize
);
371 error(1, 0, "no response from target");
375 if ((tosend
!= NULL
)&&(encap_mode
!= IPSEC_ENCAP_TUNNEL
))
381 if (encap_mode
!= IPSEC_ENCAP_TUNNEL
) {
382 recvsize
-= 4; /* 4 bytes non-esp marker */
383 memcpy(recvbuf
, recvbuf
+4, recvsize
);
386 /* Wait at least 2s for a response or 4 times the time it took
391 timeout
= 4000 * (end
- start
);
396 int isakmp_crypt(struct sa_block
*s
, uint8_t * block
, size_t blocklen
, int enc
)
398 unsigned char *new_iv
, *iv
= NULL
;
400 gcry_cipher_hd_t cry_ctx
;
402 if (blocklen
< ISAKMP_PAYLOAD_O
|| ((blocklen
- ISAKMP_PAYLOAD_O
) % s
->ivlen
!= 0))
405 if (!enc
&& (memcmp(block
+ ISAKMP_I_COOKIE_O
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
) != 0
406 || memcmp(block
+ ISAKMP_R_COOKIE_O
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
) != 0)) {
407 DEBUG(2, printf("got paket with wrong cookies\n"));
408 return ISAKMP_N_INVALID_COOKIE
;
411 info_ex
= block
[ISAKMP_EXCHANGE_TYPE_O
] == ISAKMP_EXCHANGE_INFORMATIONAL
;
413 if (memcmp(block
+ ISAKMP_MESSAGE_ID_O
, s
->current_iv_msgid
, 4) != 0) {
416 gcry_md_open(&md_ctx
, s
->md_algo
, 0);
417 gcry_md_write(md_ctx
, s
->initial_iv
, s
->ivlen
);
418 gcry_md_write(md_ctx
, block
+ ISAKMP_MESSAGE_ID_O
, 4);
419 gcry_md_final(md_ctx
);
421 iv
= xallocc(s
->ivlen
);
422 memcpy(iv
, gcry_md_read(md_ctx
, 0), s
->ivlen
);
424 memcpy(s
->current_iv
, gcry_md_read(md_ctx
, 0), s
->ivlen
);
425 memcpy(s
->current_iv_msgid
, block
+ ISAKMP_MESSAGE_ID_O
, 4);
427 gcry_md_close(md_ctx
);
428 } else if (info_ex
) {
436 new_iv
= xallocc(s
->ivlen
);
437 gcry_cipher_open(&cry_ctx
, s
->cry_algo
, GCRY_CIPHER_MODE_CBC
, 0);
438 gcry_cipher_setkey(cry_ctx
, s
->key
, s
->keylen
);
439 gcry_cipher_setiv(cry_ctx
, iv
, s
->ivlen
);
441 memcpy(new_iv
, block
+ blocklen
- s
->ivlen
, s
->ivlen
);
442 gcry_cipher_decrypt(cry_ctx
, block
+ ISAKMP_PAYLOAD_O
, blocklen
- ISAKMP_PAYLOAD_O
,
445 memcpy(s
->current_iv
, new_iv
, s
->ivlen
);
447 gcry_cipher_encrypt(cry_ctx
, block
+ ISAKMP_PAYLOAD_O
, blocklen
- ISAKMP_PAYLOAD_O
,
450 memcpy(s
->current_iv
, block
+ blocklen
- s
->ivlen
, s
->ivlen
);
452 gcry_cipher_close(cry_ctx
);
461 static uint16_t unpack_verify_phase2(struct sa_block
*s
,
463 size_t r_length
, struct isakmp_packet
**r_p
, const uint8_t * nonce
, size_t nonce_size
)
465 struct isakmp_packet
*r
;
470 if (r_length
< ISAKMP_PAYLOAD_O
|| ((r_length
- ISAKMP_PAYLOAD_O
) % s
->ivlen
!= 0)) {
471 DEBUG(2, printf("payload to short or not padded: len=%d, min=%d (ivlen=%d)\n",
472 r_length
, ISAKMP_PAYLOAD_O
, s
->ivlen
));
473 return ISAKMP_N_UNEQUAL_PAYLOAD_LENGTHS
;
476 reject
= isakmp_crypt(s
, r_packet
, r_length
, 0);
481 r
= parse_isakmp_packet(r_packet
, r_length
, &reject
);
486 /* Verify the basic stuff. */
487 if (r
->flags
!= ISAKMP_FLAG_E
)
488 return ISAKMP_N_INVALID_FLAGS
;
493 unsigned char *expected_hash
;
494 struct isakmp_payload
*h
= r
->payload
;
496 if (h
== NULL
|| h
->type
!= ISAKMP_PAYLOAD_HASH
|| h
->u
.hash
.length
!= s
->md_len
)
497 return ISAKMP_N_INVALID_HASH_INFORMATION
;
499 spos
= (ISAKMP_PAYLOAD_O
+ (r_packet
[ISAKMP_PAYLOAD_O
+ 2] << 8)
500 + r_packet
[ISAKMP_PAYLOAD_O
+ 3]);
502 /* Compute the real length based on the payload lengths. */
503 for (sz
= spos
; r_packet
[sz
] != 0; sz
+= r_packet
[sz
+ 2] << 8 | r_packet
[sz
+ 3]) ;
504 sz
+= r_packet
[sz
+ 2] << 8 | r_packet
[sz
+ 3];
506 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
507 gcry_md_setkey(hm
, s
->skeyid_a
, s
->md_len
);
508 gcry_md_write(hm
, r_packet
+ ISAKMP_MESSAGE_ID_O
, 4);
510 gcry_md_write(hm
, nonce
, nonce_size
);
511 gcry_md_write(hm
, r_packet
+ spos
, sz
- spos
);
513 expected_hash
= gcry_md_read(hm
, 0);
515 if (opt_debug
>= 3) {
516 printf("hashlen: %lu\n", (unsigned long)s
->md_len
);
517 printf("u.hash.length: %d\n", h
->u
.hash
.length
);
518 hex_dump("expected_hash", expected_hash
, s
->md_len
);
519 hex_dump("h->u.hash.data", h
->u
.hash
.data
, s
->md_len
);
523 if (memcmp(h
->u
.hash
.data
, expected_hash
, s
->md_len
) != 0)
524 reject
= ISAKMP_N_AUTHENTICATION_FAILED
;
536 phase2_authpacket(struct sa_block
*s
, struct isakmp_payload
*pl
,
537 uint8_t exchange_type
, uint32_t msgid
,
538 uint8_t ** p_flat
, size_t * p_size
,
539 uint8_t * nonce_i
, int ni_len
, uint8_t * nonce_r
, int nr_len
)
541 struct isakmp_packet
*p
;
545 uint8_t msgid_sent
[4];
547 /* Build up the packet. */
548 p
= new_isakmp_packet();
549 memcpy(p
->i_cookie
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
550 memcpy(p
->r_cookie
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
551 p
->flags
= ISAKMP_FLAG_E
;
552 p
->isakmp_version
= ISAKMP_VERSION
;
553 p
->exchange_type
= exchange_type
;
554 p
->message_id
= msgid
;
555 p
->payload
= new_isakmp_payload(ISAKMP_PAYLOAD_HASH
);
556 p
->payload
->next
= pl
;
557 p
->payload
->u
.hash
.length
= s
->md_len
;
558 p
->payload
->u
.hash
.data
= xallocc(s
->md_len
);
561 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
562 gcry_md_setkey(hm
, s
->skeyid_a
, s
->md_len
);
565 DEBUG(3, printf("authing NULL package!\n"));
566 gcry_md_write(hm
, "" /* \0 */ , 1);
569 msgid_sent
[0] = msgid
>> 24;
570 msgid_sent
[1] = msgid
>> 16;
571 msgid_sent
[2] = msgid
>> 8;
572 msgid_sent
[3] = msgid
;
573 gcry_md_write(hm
, msgid_sent
, sizeof(msgid_sent
));
576 gcry_md_write(hm
, nonce_i
, ni_len
);
579 gcry_md_write(hm
, nonce_r
, nr_len
);
582 flatten_isakmp_payload(pl
, &pl_flat
, &pl_size
);
583 gcry_md_write(hm
, pl_flat
, pl_size
);
584 memset(pl_flat
, 0, pl_size
);
589 memcpy(p
->payload
->u
.hash
.data
, gcry_md_read(hm
, 0), s
->md_len
);
592 flatten_isakmp_packet(p
, p_flat
, p_size
, s
->ivlen
);
593 free_isakmp_packet(p
);
596 static void sendrecv_phase2(struct sa_block
*s
, struct isakmp_payload
*pl
,
597 uint8_t exchange_type
, uint32_t msgid
, int sendonly
,
598 uint8_t ** save_p_flat
, size_t * save_p_size
,
599 uint8_t * nonce_i
, int ni_len
, uint8_t * nonce_r
, int nr_len
)
605 if ((save_p_flat
== NULL
) || (*save_p_flat
== NULL
)) {
606 phase2_authpacket(s
, pl
, exchange_type
, msgid
, &p_flat
, &p_size
,
607 nonce_i
, ni_len
, nonce_r
, nr_len
);
608 isakmp_crypt(s
, p_flat
, p_size
, 1);
610 p_flat
= *save_p_flat
;
611 p_size
= *save_p_size
;
614 recvlen
= sendrecv(r_packet
, sizeof(r_packet
), p_flat
, p_size
, sendonly
);
618 if (save_p_flat
== NULL
) {
621 *save_p_flat
= p_flat
;
622 *save_p_size
= p_size
;
626 static void phase2_fatal(struct sa_block
*s
, const char *msg
, int id
)
628 struct isakmp_payload
*pl
;
631 DEBUG(1, printf("\n\n---!!!!!!!!! entering phase2_fatal !!!!!!!!!---\n\n\n"));
632 gcry_randomize((uint8_t *) & msgid
, sizeof(msgid
), GCRY_WEAK_RANDOM
);
633 pl
= new_isakmp_payload(ISAKMP_PAYLOAD_N
);
634 pl
->u
.n
.doi
= ISAKMP_DOI_IPSEC
;
635 pl
->u
.n
.protocol
= ISAKMP_IPSEC_PROTO_ISAKMP
;
637 sendrecv_phase2(s
, pl
, ISAKMP_EXCHANGE_INFORMATIONAL
, msgid
, 1, 0, 0, 0, 0, 0, 0);
639 gcry_randomize((uint8_t *) & msgid
, sizeof(msgid
), GCRY_WEAK_RANDOM
);
640 pl
= new_isakmp_payload(ISAKMP_PAYLOAD_D
);
641 pl
->u
.d
.doi
= ISAKMP_DOI_IPSEC
;
642 pl
->u
.d
.protocol
= ISAKMP_IPSEC_PROTO_ISAKMP
;
643 pl
->u
.d
.spi_length
= 2 * ISAKMP_COOKIE_LENGTH
;
645 pl
->u
.d
.spi
= xallocc(1 * sizeof(uint8_t *));
646 pl
->u
.d
.spi
[0] = xallocc(2 * ISAKMP_COOKIE_LENGTH
);
647 memcpy(pl
->u
.d
.spi
[0] + ISAKMP_COOKIE_LENGTH
* 0, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
648 memcpy(pl
->u
.d
.spi
[0] + ISAKMP_COOKIE_LENGTH
* 1, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
649 sendrecv_phase2(s
, pl
, ISAKMP_EXCHANGE_INFORMATIONAL
, msgid
, 1, 0, 0, 0, 0, 0, 0);
651 error(1, 0, msg
, isakmp_notify_to_error(id
));
654 static uint8_t *gen_keymat(struct sa_block
*s
,
655 uint8_t protocol
, uint32_t spi
,
656 int md_algo
, int crypt_algo
,
657 const uint8_t * dh_shared
, size_t dh_size
,
658 const uint8_t * ni_data
, size_t ni_size
, const uint8_t * nr_data
, size_t nr_size
)
666 int md_len
= gcry_md_get_algo_dlen(md_algo
);
669 gcry_cipher_algo_info(crypt_algo
, GCRYCTL_GET_KEYLEN
, NULL
, &cry_len
);
670 blksz
= md_len
+ cry_len
;
671 cnt
= (blksz
+ s
->md_len
- 1) / s
->md_len
;
672 block
= xallocc(cnt
* s
->md_len
);
673 DEBUG(3, printf("generating %d bytes keymat (cnt=%d)\n", blksz
, cnt
));
677 for (i
= 0; i
< cnt
; i
++) {
678 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
679 gcry_md_setkey(hm
, s
->skeyid_d
, s
->md_len
);
681 gcry_md_write(hm
, block
+ (i
- 1) * s
->md_len
, s
->md_len
);
682 if (dh_shared
!= NULL
)
683 gcry_md_write(hm
, dh_shared
, dh_size
);
684 gcry_md_write(hm
, &protocol
, 1);
685 gcry_md_write(hm
, (uint8_t *) & spi
, sizeof(spi
));
686 gcry_md_write(hm
, ni_data
, ni_size
);
687 gcry_md_write(hm
, nr_data
, nr_size
);
689 memcpy(block
+ i
* s
->md_len
, gcry_md_read(hm
, 0), s
->md_len
);
697 struct isakmp_attribute
*make_transform_ike(int dh_group
, int crypt
, int hash
, int keylen
, int auth
)
699 struct isakmp_attribute
*a
= NULL
;
701 a
= new_isakmp_attribute(IKE_ATTRIB_LIFE_DURATION
, a
);
702 a
->af
= isakmp_attr_lots
;
703 a
->u
.lots
.length
= 4;
704 a
->u
.lots
.data
= xallocc(a
->u
.lots
.length
);
705 *((uint32_t *) a
->u
.lots
.data
) = htonl(2147483);
706 a
= new_isakmp_attribute_16(IKE_ATTRIB_LIFE_TYPE
, IKE_LIFE_TYPE_SECONDS
, a
);
707 a
= new_isakmp_attribute_16(IKE_ATTRIB_GROUP_DESC
, dh_group
, a
);
708 a
= new_isakmp_attribute_16(IKE_ATTRIB_AUTH_METHOD
, auth
, a
);
709 a
= new_isakmp_attribute_16(IKE_ATTRIB_HASH
, hash
, a
);
710 a
= new_isakmp_attribute_16(IKE_ATTRIB_ENC
, crypt
, a
);
712 a
= new_isakmp_attribute_16(IKE_ATTRIB_KEY_LENGTH
, keylen
, a
);
716 struct isakmp_payload
*make_our_sa_ike(void)
718 struct isakmp_payload
*r
= new_isakmp_payload(ISAKMP_PAYLOAD_SA
);
719 struct isakmp_payload
*t
= NULL
, *tn
;
720 struct isakmp_attribute
*a
;
721 int dh_grp
= get_dh_group_ike()->ike_sa_id
;
722 unsigned int auth
, crypt
, hash
, keylen
;
725 r
->u
.sa
.doi
= ISAKMP_DOI_IPSEC
;
726 r
->u
.sa
.situation
= ISAKMP_IPSEC_SIT_IDENTITY_ONLY
;
727 r
->u
.sa
.proposals
= new_isakmp_payload(ISAKMP_PAYLOAD_P
);
728 r
->u
.sa
.proposals
->u
.p
.prot_id
= ISAKMP_IPSEC_PROTO_ISAKMP
;
729 for (auth
= 0; supp_auth
[auth
].name
!= NULL
; auth
++) {
730 for (crypt
= 0; supp_crypt
[crypt
].name
!= NULL
; crypt
++) {
731 if ((supp_crypt
[crypt
].my_id
== GCRY_CIPHER_DES
) && (opt_1des
== 0))
733 keylen
= supp_crypt
[crypt
].keylen
;
734 for (hash
= 0; supp_hash
[hash
].name
!= NULL
; hash
++) {
736 t
= new_isakmp_payload(ISAKMP_PAYLOAD_T
);
737 t
->u
.t
.id
= ISAKMP_IPSEC_KEY_IKE
;
738 a
= make_transform_ike(dh_grp
, supp_crypt
[crypt
].ike_sa_id
,
739 supp_hash
[hash
].ike_sa_id
, keylen
, supp_auth
[auth
].ike_sa_id
);
740 t
->u
.t
.attributes
= a
;
745 for (i
= 0, tn
= t
; tn
; tn
= tn
->next
)
746 tn
->u
.t
.number
= i
++;
747 r
->u
.sa
.proposals
->u
.p
.transforms
= t
;
751 void do_phase_1(const char *key_id
, const char *shared_key
, struct sa_block
*s
)
753 unsigned char i_nonce
[20];
754 struct group
*dh_grp
;
755 unsigned char *dh_public
;
756 unsigned char *returned_hash
;
757 static const uint8_t xauth_vid
[] = XAUTH_VENDOR_ID
;
758 static const uint8_t unity_vid
[] = UNITY_VENDOR_ID
;
759 static const uint8_t unknown_vid
[] = UNKNOWN_VENDOR_ID
;
760 static const uint8_t natt_vid
[] = NATT_VENDOR_ID
; /* NAT traversal */
762 static const uint8_t dpd_vid
[] = DPD_VENDOR_ID
; /* dead peer detection */
763 static const uint8_t my_vid
[] = {
764 0x35, 0x53, 0x07, 0x6c, 0x4f, 0x65, 0x12, 0x68, 0x02, 0x82, 0xf2, 0x15,
765 0x8a, 0xa8, 0xa0, 0x9e
769 struct isakmp_packet
*p1
;
770 int seen_natt_vid
= 0, seen_natd
= 0, seen_natd_them
= 0, seen_natd_us
= 0, natd_type
= 0;
771 unsigned char *natd_us
= NULL
, *natd_them
= NULL
;
773 DEBUG(2, printf("S4.1\n"));
774 gcry_randomize(s
->i_cookie
, ISAKMP_COOKIE_LENGTH
, GCRY_STRONG_RANDOM
);
776 if (s
->i_cookie
[0] == 0)
778 hex_dump("i_cookie", s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
779 gcry_randomize(i_nonce
, sizeof(i_nonce
), GCRY_STRONG_RANDOM
);
780 hex_dump("i_nonce", i_nonce
, sizeof(i_nonce
));
781 DEBUG(2, printf("S4.2\n"));
782 /* Set up the Diffie-Hellman stuff. */
784 dh_grp
= group_get(get_dh_group_ike()->my_id
);
785 dh_public
= xallocc(dh_getlen(dh_grp
));
786 dh_create_exchange(dh_grp
, dh_public
);
787 hex_dump("dh_public", dh_public
, dh_getlen(dh_grp
));
790 DEBUG(2, printf("S4.3\n"));
791 /* Create the first packet. */
793 struct isakmp_payload
*l
;
797 p1
= new_isakmp_packet();
798 memcpy(p1
->i_cookie
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
799 p1
->isakmp_version
= ISAKMP_VERSION
;
800 p1
->exchange_type
= ISAKMP_EXCHANGE_AGGRESSIVE
;
801 p1
->payload
= l
= make_our_sa_ike();
802 l
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_KE
, dh_public
, dh_getlen(dh_grp
));
803 l
->next
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_NONCE
,
804 i_nonce
, sizeof(i_nonce
));
806 l
->next
= new_isakmp_payload(ISAKMP_PAYLOAD_ID
);
808 l
->u
.id
.type
= ISAKMP_IPSEC_ID_KEY_ID
;
809 l
->u
.id
.protocol
= IPPROTO_UDP
;
810 l
->u
.id
.port
= ntohs(local_port
);
811 l
->u
.id
.length
= strlen(key_id
);
812 l
->u
.id
.data
= xallocc(l
->u
.id
.length
);
813 memcpy(l
->u
.id
.data
, key_id
, strlen(key_id
));
814 l
= l
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_VID
,
815 xauth_vid
, sizeof(xauth_vid
));
816 l
= l
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_VID
,
817 unity_vid
, sizeof(unity_vid
));
818 if (!config
[CONFIG_DISABLE_NATT
])
819 l
= l
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_VID
,
820 natt_vid
, sizeof(natt_vid
));
822 l
= l
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_VID
,
823 dpd_vid
, sizeof(dpd_vid
));
825 flatten_isakmp_packet(p1
, &pkt
, &pkt_len
, 0);
827 /* Now, send that packet and receive a new one. */
828 r_length
= sendrecv(r_packet
, sizeof(r_packet
), pkt
, pkt_len
, 0);
831 DEBUG(2, printf("S4.4\n"));
832 /* Decode the recieved packet. */
834 struct isakmp_packet
*r
;
836 struct isakmp_payload
*rp
;
837 struct isakmp_payload
*nonce
= NULL
;
838 struct isakmp_payload
*ke
= NULL
;
839 struct isakmp_payload
*hash
= NULL
;
840 struct isakmp_payload
*idp
= NULL
;
841 int seen_sa
= 0, seen_xauth_vid
= 0;
842 unsigned char *skeyid
;
843 gcry_md_hd_t skeyid_ctx
;
846 r
= parse_isakmp_packet(r_packet
, r_length
, &reject
);
848 /* Verify the correctness of the recieved packet. */
849 if (reject
== 0 && memcmp(r
->i_cookie
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
) != 0)
850 reject
= ISAKMP_N_INVALID_COOKIE
;
852 memcpy(s
->r_cookie
, r
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
853 if (reject
== 0 && r
->exchange_type
!= ISAKMP_EXCHANGE_AGGRESSIVE
)
854 reject
= ISAKMP_N_INVALID_EXCHANGE_TYPE
;
855 if (reject
== 0 && r
->flags
!= 0)
856 reject
= ISAKMP_N_INVALID_FLAGS
;
857 if (reject
== 0 && r
->message_id
!= 0)
858 reject
= ISAKMP_N_INVALID_MESSAGE_ID
;
860 error(1, 0, "response was invalid [1]: %s", isakmp_notify_to_error(reject
));
861 for (rp
= r
->payload
; rp
&& reject
== 0; rp
= rp
->next
)
863 case ISAKMP_PAYLOAD_SA
:
864 if (reject
== 0 && rp
->u
.sa
.doi
!= ISAKMP_DOI_IPSEC
)
865 reject
= ISAKMP_N_DOI_NOT_SUPPORTED
;
867 rp
->u
.sa
.situation
!= ISAKMP_IPSEC_SIT_IDENTITY_ONLY
)
868 reject
= ISAKMP_N_SITUATION_NOT_SUPPORTED
;
870 (rp
->u
.sa
.proposals
== NULL
871 || rp
->u
.sa
.proposals
->next
!= NULL
))
872 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
874 rp
->u
.sa
.proposals
->u
.p
.prot_id
!=
875 ISAKMP_IPSEC_PROTO_ISAKMP
)
876 reject
= ISAKMP_N_INVALID_PROTOCOL_ID
;
877 if (reject
== 0 && rp
->u
.sa
.proposals
->u
.p
.spi_size
!= 0)
878 reject
= ISAKMP_N_INVALID_SPI
;
880 (rp
->u
.sa
.proposals
->u
.p
.transforms
== NULL
881 || rp
->u
.sa
.proposals
->u
.p
.transforms
->next
!=
883 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
885 (rp
->u
.sa
.proposals
->u
.p
.transforms
->u
.t
.id
886 != ISAKMP_IPSEC_KEY_IKE
))
887 reject
= ISAKMP_N_INVALID_TRANSFORM_ID
;
889 struct isakmp_attribute
*a
891 rp
->u
.sa
.proposals
->u
.p
.transforms
->u
.t
.attributes
;
892 int seen_enc
= 0, seen_hash
= 0, seen_auth
= 0;
893 int seen_group
= 0, seen_keylen
= 0;
894 for (; a
&& reject
== 0; a
= a
->next
)
896 case IKE_ATTRIB_GROUP_DESC
:
897 if (a
->af
== isakmp_attr_16
&&
899 get_dh_group_ike()->ike_sa_id
)
902 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
904 case IKE_ATTRIB_AUTH_METHOD
:
905 if (a
->af
== isakmp_attr_16
)
906 seen_auth
= a
->u
.attr_16
;
908 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
910 case IKE_ATTRIB_HASH
:
911 if (a
->af
== isakmp_attr_16
)
912 seen_hash
= a
->u
.attr_16
;
914 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
917 if (a
->af
== isakmp_attr_16
)
918 seen_enc
= a
->u
.attr_16
;
920 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
922 case IKE_ATTRIB_KEY_LENGTH
:
923 if (a
->af
== isakmp_attr_16
)
924 seen_keylen
= a
->u
.attr_16
;
926 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
928 case IKE_ATTRIB_LIFE_TYPE
:
929 case IKE_ATTRIB_LIFE_DURATION
:
933 ("unknown attribute %d, arborting..\n",
935 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
938 if (!seen_group
|| !seen_auth
|| !seen_hash
|| !seen_enc
)
939 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
941 if (get_algo(SUPP_ALGO_AUTH
, SUPP_ALGO_IKE_SA
, seen_auth
,
943 reject
= ISAKMP_N_NO_PROPOSAL_CHOSEN
;
944 if (get_algo(SUPP_ALGO_HASH
, SUPP_ALGO_IKE_SA
, seen_hash
,
946 reject
= ISAKMP_N_NO_PROPOSAL_CHOSEN
;
947 if (get_algo(SUPP_ALGO_CRYPT
, SUPP_ALGO_IKE_SA
, seen_enc
,
948 NULL
, seen_keylen
) == NULL
)
949 reject
= ISAKMP_N_NO_PROPOSAL_CHOSEN
;
953 s
->auth_algo
= seen_auth
;
955 get_algo(SUPP_ALGO_CRYPT
, SUPP_ALGO_IKE_SA
,
956 seen_enc
, NULL
, seen_keylen
)->my_id
;
958 get_algo(SUPP_ALGO_HASH
, SUPP_ALGO_IKE_SA
,
959 seen_hash
, NULL
, 0)->my_id
;
960 s
->md_len
= gcry_md_get_algo_dlen(s
->md_algo
);
961 DEBUG(1, printf("IKE SA selected %s-%s-%s\n",
962 get_algo(SUPP_ALGO_AUTH
,
963 SUPP_ALGO_IKE_SA
, seen_auth
,
965 get_algo(SUPP_ALGO_CRYPT
,
966 SUPP_ALGO_IKE_SA
, seen_enc
,
967 NULL
, seen_keylen
)->name
,
968 get_algo(SUPP_ALGO_HASH
,
969 SUPP_ALGO_IKE_SA
, seen_hash
,
975 case ISAKMP_PAYLOAD_ID
:
978 case ISAKMP_PAYLOAD_KE
:
981 case ISAKMP_PAYLOAD_NONCE
:
984 case ISAKMP_PAYLOAD_HASH
:
987 case ISAKMP_PAYLOAD_VID
:
988 if (rp
->u
.vid
.length
== sizeof(xauth_vid
)
989 && memcmp(rp
->u
.vid
.data
, xauth_vid
,
990 sizeof(xauth_vid
)) == 0)
992 else if (rp
->u
.vid
.length
== sizeof(natt_vid
)
993 && memcmp(rp
->u
.vid
.data
, natt_vid
,
994 sizeof(natt_vid
)) == 0)
997 case ISAKMP_PAYLOAD_NAT_D_OLD
:
998 case ISAKMP_PAYLOAD_NAT_D
:
999 natd_type
= rp
->type
;
1000 DEBUG(2, printf("peer is using type %d for NAT-Discovery payloads\n", natd_type
));
1001 if (!seen_sa
|| !seen_natt_vid
) {
1002 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1003 } else if (config
[CONFIG_DISABLE_NATT
]) {
1005 } else if (rp
->u
.natd
.length
!= s
->md_len
) {
1006 reject
= ISAKMP_N_PAYLOAD_MALFORMED
;
1007 } else if (seen_natd
== 0) {
1009 natd_us
= xallocc(s
->md_len
);
1010 natd_them
= xallocc(s
->md_len
);
1011 memcpy(natd_us
, rp
->u
.natd
.data
, s
->md_len
);
1012 gcry_md_open(&hm
, s
->md_algo
, 0);
1013 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1014 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1015 gcry_md_write(hm
, &((struct sockaddr_in
*)dest_addr
)->sin_addr
,
1016 sizeof(struct in_addr
));
1017 gcry_md_write(hm
, &((struct sockaddr_in
*)dest_addr
)->sin_port
,
1020 memcpy(natd_them
, gcry_md_read(hm
, 0), s
->md_len
);
1024 if (memcmp(natd_them
, rp
->u
.natd
.data
, s
->md_len
) == 0)
1029 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1034 gcry_cipher_algo_info(s
->cry_algo
, GCRYCTL_GET_BLKLEN
, NULL
, &(s
->ivlen
));
1035 gcry_cipher_algo_info(s
->cry_algo
, GCRYCTL_GET_KEYLEN
, NULL
, &(s
->keylen
));
1038 if (reject
== 0 && (ke
== NULL
|| ke
->u
.ke
.length
!= dh_getlen(dh_grp
)))
1039 reject
= ISAKMP_N_INVALID_KEY_INFORMATION
;
1040 if (reject
== 0 && nonce
== NULL
)
1041 reject
= ISAKMP_N_INVALID_HASH_INFORMATION
;
1043 error(1, 0, "response was invalid [2]: %s", isakmp_notify_to_error(reject
));
1044 if (reject
== 0 && idp
== NULL
)
1045 reject
= ISAKMP_N_INVALID_ID_INFORMATION
;
1046 if (reject
== 0 && (hash
== NULL
|| hash
->u
.hash
.length
!= s
->md_len
))
1047 reject
= ISAKMP_N_INVALID_HASH_INFORMATION
;
1049 error(1, 0, "response was invalid [3]: %s", isakmp_notify_to_error(reject
));
1051 /* Generate SKEYID. */
1053 gcry_md_open(&skeyid_ctx
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1054 gcry_md_setkey(skeyid_ctx
, shared_key
, strlen(shared_key
));
1055 gcry_md_write(skeyid_ctx
, i_nonce
, sizeof(i_nonce
));
1056 gcry_md_write(skeyid_ctx
, nonce
->u
.nonce
.data
, nonce
->u
.nonce
.length
);
1057 gcry_md_final(skeyid_ctx
);
1058 skeyid
= gcry_md_read(skeyid_ctx
, 0);
1059 hex_dump("skeyid", skeyid
, s
->md_len
);
1062 /* Verify the hash. */
1065 unsigned char *expected_hash
;
1066 uint8_t *sa_f
, *idi_f
, *idp_f
;
1067 size_t sa_size
, idi_size
, idp_size
;
1068 struct isakmp_payload
*sa
, *idi
;
1071 for (idi
= sa
; idi
->type
!= ISAKMP_PAYLOAD_ID
; idi
= idi
->next
) ;
1075 flatten_isakmp_payload(sa
, &sa_f
, &sa_size
);
1076 flatten_isakmp_payload(idi
, &idi_f
, &idi_size
);
1077 flatten_isakmp_payload(idp
, &idp_f
, &idp_size
);
1079 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1080 gcry_md_setkey(hm
, skeyid
, s
->md_len
);
1081 gcry_md_write(hm
, ke
->u
.ke
.data
, ke
->u
.ke
.length
);
1082 gcry_md_write(hm
, dh_public
, dh_getlen(dh_grp
));
1083 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1084 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1085 gcry_md_write(hm
, sa_f
+ 4, sa_size
- 4);
1086 gcry_md_write(hm
, idp_f
+ 4, idp_size
- 4);
1088 expected_hash
= gcry_md_read(hm
, 0);
1090 if (memcmp(expected_hash
, hash
->u
.hash
.data
, s
->md_len
) != 0) {
1091 error(1, 0, "hash comparison failed: %s\ncheck group password!",
1092 isakmp_notify_to_error(ISAKMP_N_AUTHENTICATION_FAILED
));
1096 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1097 gcry_md_setkey(hm
, skeyid
, s
->md_len
);
1098 gcry_md_write(hm
, dh_public
, dh_getlen(dh_grp
));
1099 gcry_md_write(hm
, ke
->u
.ke
.data
, ke
->u
.ke
.length
);
1100 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1101 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1102 gcry_md_write(hm
, sa_f
+ 4, sa_size
- 4);
1103 gcry_md_write(hm
, idi_f
+ 4, idi_size
- 4);
1105 returned_hash
= xallocc(s
->md_len
);
1106 memcpy(returned_hash
, gcry_md_read(hm
, 0), s
->md_len
);
1108 hex_dump("returned_hash", returned_hash
, s
->md_len
);
1115 /* Determine all the SKEYID_x keys. */
1119 static const unsigned char c012
[3] = { 0, 1, 2 };
1120 unsigned char *skeyid_e
;
1121 unsigned char *dh_shared_secret
;
1123 /* Determine the shared secret. */
1124 dh_shared_secret
= xallocc(dh_getlen(dh_grp
));
1125 dh_create_shared(dh_grp
, dh_shared_secret
, ke
->u
.ke
.data
);
1126 hex_dump("dh_shared_secret", dh_shared_secret
, dh_getlen(dh_grp
));
1128 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1129 gcry_md_setkey(hm
, skeyid
, s
->md_len
);
1130 gcry_md_write(hm
, dh_shared_secret
, dh_getlen(dh_grp
));
1131 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1132 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1133 gcry_md_write(hm
, c012
+ 0, 1);
1135 s
->skeyid_d
= xallocc(s
->md_len
);
1136 memcpy(s
->skeyid_d
, gcry_md_read(hm
, 0), s
->md_len
);
1138 hex_dump("skeyid_d", s
->skeyid_d
, s
->md_len
);
1140 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1141 gcry_md_setkey(hm
, skeyid
, s
->md_len
);
1142 gcry_md_write(hm
, s
->skeyid_d
, s
->md_len
);
1143 gcry_md_write(hm
, dh_shared_secret
, dh_getlen(dh_grp
));
1144 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1145 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1146 gcry_md_write(hm
, c012
+ 1, 1);
1148 s
->skeyid_a
= xallocc(s
->md_len
);
1149 memcpy(s
->skeyid_a
, gcry_md_read(hm
, 0), s
->md_len
);
1151 hex_dump("skeyid_a", s
->skeyid_a
, s
->md_len
);
1153 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1154 gcry_md_setkey(hm
, skeyid
, s
->md_len
);
1155 gcry_md_write(hm
, s
->skeyid_a
, s
->md_len
);
1156 gcry_md_write(hm
, dh_shared_secret
, dh_getlen(dh_grp
));
1157 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1158 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1159 gcry_md_write(hm
, c012
+ 2, 1);
1161 skeyid_e
= xallocc(s
->md_len
);
1162 memcpy(skeyid_e
, gcry_md_read(hm
, 0), s
->md_len
);
1164 hex_dump("skeyid_e", skeyid_e
, s
->md_len
);
1166 memset(dh_shared_secret
, 0, sizeof(dh_shared_secret
));
1168 /* Determine the IKE encryption key. */
1169 s
->key
= xallocc(s
->keylen
);
1171 if (s
->keylen
> s
->md_len
) {
1172 for (i
= 0; i
* s
->md_len
< s
->keylen
; i
++) {
1173 gcry_md_open(&hm
, s
->md_algo
, GCRY_MD_FLAG_HMAC
);
1174 gcry_md_setkey(hm
, skeyid_e
, s
->md_len
);
1176 gcry_md_write(hm
, "" /* &'\0' */ , 1);
1178 gcry_md_write(hm
, s
->key
+ (i
- 1) * s
->md_len
,
1181 memcpy(s
->key
+ i
* s
->md_len
, gcry_md_read(hm
, 0),
1182 min(s
->md_len
, s
->keylen
- i
* s
->md_len
));
1185 } else { /* keylen <= md_len */
1186 memcpy(s
->key
, skeyid_e
, s
->keylen
);
1188 hex_dump("enc-key", s
->key
, s
->keylen
);
1190 memset(skeyid_e
, 0, s
->md_len
);
1193 /* Determine the initial IV. */
1197 assert(s
->ivlen
<= s
->md_len
);
1198 gcry_md_open(&hm
, s
->md_algo
, 0);
1199 gcry_md_write(hm
, dh_public
, dh_getlen(dh_grp
));
1200 gcry_md_write(hm
, ke
->u
.ke
.data
, ke
->u
.ke
.length
);
1202 s
->current_iv
= xallocc(s
->ivlen
);
1203 memcpy(s
->current_iv
, gcry_md_read(hm
, 0), s
->ivlen
);
1205 hex_dump("current_iv", s
->current_iv
, s
->ivlen
);
1206 memset(s
->current_iv_msgid
, 0, 4);
1209 gcry_md_close(skeyid_ctx
);
1212 DEBUG(2, printf("S4.5\n"));
1213 /* Send final phase 1 packet. */
1215 struct isakmp_packet
*p2
;
1218 struct isakmp_payload
*pl
;
1220 p2
= new_isakmp_packet();
1221 memcpy(p2
->i_cookie
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1222 memcpy(p2
->r_cookie
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1223 p2
->flags
= ISAKMP_FLAG_E
;
1224 p2
->isakmp_version
= ISAKMP_VERSION
;
1225 p2
->exchange_type
= ISAKMP_EXCHANGE_AGGRESSIVE
;
1226 p2
->payload
= new_isakmp_data_payload(ISAKMP_PAYLOAD_HASH
,
1227 returned_hash
, s
->md_len
);
1228 p2
->payload
->next
= pl
= new_isakmp_payload(ISAKMP_PAYLOAD_N
);
1229 pl
->u
.n
.doi
= ISAKMP_DOI_IPSEC
;
1230 pl
->u
.n
.protocol
= ISAKMP_IPSEC_PROTO_ISAKMP
;
1231 pl
->u
.n
.type
= ISAKMP_N_IPSEC_INITIAL_CONTACT
;
1232 pl
->u
.n
.spi_length
= 2 * ISAKMP_COOKIE_LENGTH
;
1233 pl
->u
.n
.spi
= xallocc(2 * ISAKMP_COOKIE_LENGTH
);
1234 memcpy(pl
->u
.n
.spi
+ ISAKMP_COOKIE_LENGTH
* 0, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1235 memcpy(pl
->u
.n
.spi
+ ISAKMP_COOKIE_LENGTH
* 1, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1236 pl
= pl
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_VID
,
1237 unknown_vid
, sizeof(unknown_vid
));
1238 pl
= pl
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_VID
,
1239 unity_vid
, sizeof(unity_vid
));
1241 /* include NAT traversal discovery payloads */
1242 if (seen_natt_vid
) {
1243 assert(natd_type
!= 0);
1244 pl
= pl
->next
= new_isakmp_data_payload(natd_type
,
1245 natd_them
, s
->md_len
);
1246 /* this could be repeated fo any known outbound interfaces */
1249 struct sockaddr_in src_addr
;
1250 src_addr
.sin_port
=local_port
;
1251 find_local_addr((struct sockaddr_in
*)dest_addr
, &src_addr
);
1252 gcry_md_open(&hm
, s
->md_algo
, 0);
1253 gcry_md_write(hm
, s
->i_cookie
, ISAKMP_COOKIE_LENGTH
);
1254 gcry_md_write(hm
, s
->r_cookie
, ISAKMP_COOKIE_LENGTH
);
1255 gcry_md_write(hm
, &src_addr
.sin_addr
, sizeof(struct in_addr
));
1256 gcry_md_write(hm
, &local_port
, sizeof(uint16_t));
1258 pl
= pl
->next
= new_isakmp_data_payload(natd_type
,
1259 gcry_md_read(hm
, 0), s
->md_len
);
1260 if (seen_natd
&& memcmp(natd_us
, pl
->u
.ke
.data
, s
->md_len
) == 0)
1268 /* if there is a NAT, change to port 4500 and select UDP encap */
1269 if (!seen_natd_us
|| !seen_natd_them
) {
1270 DEBUG(1, printf("NAT status: this end behind NAT? %s -- remote end behind NAT? %s\n",
1271 seen_natd_us
? "no" : "YES", seen_natd_them
? "no" : "YES"));
1272 switch (natd_type
) {
1273 case ISAKMP_PAYLOAD_NAT_D
:
1274 encap_mode
= IPSEC_ENCAP_UDP_TUNNEL
;
1276 case ISAKMP_PAYLOAD_NAT_D_OLD
:
1277 encap_mode
= IPSEC_ENCAP_UDP_TUNNEL_OLD
;
1282 ((struct sockaddr_in
*)dest_addr
)->sin_port
= htons(4500);
1283 if (local_port
== htons(500)) {
1285 sockfd
= make_socket(local_port
= htons(4500));
1288 DEBUG(1, printf("NAT status: NAT-T VID seen, no NAT device detected\n"));
1291 DEBUG(1, printf("NAT status: no NAT-T VID seen\n"));
1295 flatten_isakmp_packet(p2
, &p2kt
, &p2kt_len
, s
->ivlen
);
1296 free_isakmp_packet(p2
);
1297 isakmp_crypt(s
, p2kt
, p2kt_len
, 1);
1299 s
->initial_iv
= xallocc(s
->ivlen
);
1300 memcpy(s
->initial_iv
, s
->current_iv
, s
->ivlen
);
1301 hex_dump("initial_iv", s
->initial_iv
, s
->ivlen
);
1303 /* Now, send that packet and receive a new one. */
1304 r_length
= sendrecv(r_packet
, sizeof(r_packet
), p2kt
, p2kt_len
, 0);
1307 DEBUG(2, printf("S4.6\n"));
1309 free(returned_hash
);
1312 static int do_phase2_notice_check(struct sa_block
*s
, struct isakmp_packet
**r_p
)
1314 int reject
= 0, old_connection
= 0;
1315 struct isakmp_packet
*r
;
1318 reject
= unpack_verify_phase2(s
, r_packet
, r_length
, r_p
, NULL
, 0);
1319 if (reject
== ISAKMP_N_INVALID_COOKIE
) {
1320 r_length
= sendrecv(r_packet
, sizeof(r_packet
), NULL
, 0, 0);
1326 /* check for notices */
1327 if (r
->exchange_type
== ISAKMP_EXCHANGE_INFORMATIONAL
&&
1328 r
->payload
->next
!= NULL
) {
1329 if (r
->payload
->next
->type
== ISAKMP_PAYLOAD_N
) {
1330 if (r
->payload
->next
->u
.n
.type
== ISAKMP_N_CISCO_LOAD_BALANCE
) {
1331 /* load balancing notice ==> restart with new gw */
1332 if (r
->payload
->next
->u
.n
.data_length
!= 4)
1333 error(1, 0, "malformed loadbalance target");
1334 memcpy(&((struct sockaddr_in
*)dest_addr
)->sin_addr
,
1335 r
->payload
->next
->u
.n
.data
, 4);
1336 ((struct sockaddr_in
*)dest_addr
)->sin_port
= htons(ISAKMP_PORT
);
1337 encap_mode
= IPSEC_ENCAP_TUNNEL
;
1338 if (local_port
== htons(4500)) {
1340 sockfd
= make_socket(local_port
= htons(500));
1342 DEBUG(2, printf("got cisco loadbalancing notice, diverting to %s\n",
1343 inet_ntoa(((struct sockaddr_in
*)dest_addr
)->
1347 if (r
->payload
->next
->u
.n
.type
== ISAKMP_N_IPSEC_RESPONDER_LIFETIME
) {
1348 /* responder liftime notice ==> ignore */
1349 DEBUG(2, printf("got responder liftime notice, ignoring..\n"));
1350 r_length
= sendrecv(r_packet
, sizeof(r_packet
), NULL
, 0, 0);
1354 if (r
->payload
->next
->type
== ISAKMP_PAYLOAD_D
&& old_connection
) {
1355 /* responder liftime notice ==> ignore */
1356 DEBUG(2, printf("got delete for old connection, ignoring..\n"));
1357 r_length
= sendrecv(r_packet
, sizeof(r_packet
), NULL
, 0, 0);
1367 static int do_phase_2_xauth(struct sa_block
*s
)
1369 struct isakmp_packet
*r
;
1372 DEBUG(2, printf("S5.1\n"));
1373 /* This can go around for a while. */
1374 for (loopcount
= 0;; loopcount
++) {
1376 struct isakmp_payload
*rp
;
1377 struct isakmp_attribute
*a
, *ap
, *reply_attr
;
1379 int seen_answer
= 0;
1381 DEBUG(2, printf("S5.2\n"));
1383 /* recv and check for notices */
1384 reject
= do_phase2_notice_check(s
, &r
);
1388 DEBUG(2, printf("S5.3\n"));
1389 /* Check the transaction type is OK. */
1390 if (reject
== 0 && r
->exchange_type
!= ISAKMP_EXCHANGE_MODECFG_TRANSACTION
)
1391 reject
= ISAKMP_N_INVALID_EXCHANGE_TYPE
;
1393 /* After the hash, expect an attribute block. */
1395 && (r
->payload
->next
== NULL
1396 || r
->payload
->next
->next
!= NULL
1397 || r
->payload
->next
->type
!= ISAKMP_PAYLOAD_MODECFG_ATTR
))
1398 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1400 if (reject
== 0 && r
->payload
->next
->u
.modecfg
.type
== ISAKMP_MODECFG_CFG_SET
)
1402 if (reject
== 0 && r
->payload
->next
->u
.modecfg
.type
!= ISAKMP_MODECFG_CFG_REQUEST
)
1403 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1406 phase2_fatal(s
, "expected xauth packet; rejected: %s", reject
);
1408 DEBUG(2, printf("S5.4\n"));
1409 a
= r
->payload
->next
->u
.modecfg
.attributes
;
1410 /* First, print any messages, and verify that we understand the
1412 for (ap
= a
; ap
&& seen_answer
== 0; ap
= ap
->next
)
1413 if (ap
->type
== ISAKMP_XAUTH_ATTRIB_ANSWER
)
1416 for (ap
= a
; ap
&& reject
== 0; ap
= ap
->next
)
1418 case ISAKMP_XAUTH_ATTRIB_TYPE
:
1419 if (ap
->af
!= isakmp_attr_16
|| ap
->u
.attr_16
!= 0)
1420 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1422 case ISAKMP_XAUTH_ATTRIB_USER_NAME
:
1423 case ISAKMP_XAUTH_ATTRIB_USER_PASSWORD
:
1424 case ISAKMP_XAUTH_ATTRIB_PASSCODE
:
1425 case ISAKMP_XAUTH_ATTRIB_DOMAIN
:
1426 case ISAKMP_XAUTH_ATTRIB_ANSWER
:
1427 case ISAKMP_XAUTH_ATTRIB_CISCOEXT_VENDOR
:
1429 case ISAKMP_XAUTH_ATTRIB_MESSAGE
:
1430 if (opt_debug
|| seen_answer
|| config
[CONFIG_XAUTH_INTERACTIVE
]) {
1431 if (ap
->af
== isakmp_attr_16
)
1432 printf("%c%c\n", ap
->u
.attr_16
>> 8, ap
->u
.attr_16
);
1434 printf("%.*s%s", ap
->u
.lots
.length
, ap
->u
.lots
.data
,
1436 && ap
->u
.lots
.data
[ap
->u
.
1443 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1445 DEBUG(2, printf("S5.5\n"));
1447 phase2_fatal(s
, "xauth packet unsupported: %s", reject
);
1449 inet_ntop(dest_addr
->sa_family
,
1450 &((struct sockaddr_in
*)dest_addr
)->sin_addr
, ntop_buf
, sizeof(ntop_buf
));
1452 /* Collect data from the user. */
1454 for (ap
= a
; ap
&& reject
== 0; ap
= ap
->next
)
1456 case ISAKMP_XAUTH_ATTRIB_DOMAIN
:
1458 struct isakmp_attribute
*na
;
1459 na
= new_isakmp_attribute(ap
->type
, reply_attr
);
1461 na
->u
.lots
.length
= strlen(config
[CONFIG_DOMAIN
]);
1462 if (na
->u
.lots
.length
== 0)
1464 "server requested domain, but none set (use \"Domain ...\" in config or --domain");
1465 na
->u
.lots
.data
= xallocc(na
->u
.lots
.length
);
1466 memcpy(na
->u
.lots
.data
, config
[CONFIG_DOMAIN
],
1470 case ISAKMP_XAUTH_ATTRIB_USER_NAME
:
1472 struct isakmp_attribute
*na
;
1473 na
= new_isakmp_attribute(ap
->type
, reply_attr
);
1475 na
->u
.lots
.length
= strlen(config
[CONFIG_XAUTH_USERNAME
]);
1476 na
->u
.lots
.data
= xallocc(na
->u
.lots
.length
);
1477 memcpy(na
->u
.lots
.data
, config
[CONFIG_XAUTH_USERNAME
],
1481 case ISAKMP_XAUTH_ATTRIB_ANSWER
:
1482 case ISAKMP_XAUTH_ATTRIB_USER_PASSWORD
:
1483 case ISAKMP_XAUTH_ATTRIB_PASSCODE
:
1484 if (seen_answer
|| config
[CONFIG_XAUTH_INTERACTIVE
]) {
1485 char *pass
, *prompt
= NULL
;
1486 struct isakmp_attribute
*na
;
1488 asprintf(&prompt
, "%s for VPN %s@%s: ",
1489 (ap
->type
== ISAKMP_XAUTH_ATTRIB_ANSWER
) ?
1491 (ap
->type
== ISAKMP_XAUTH_ATTRIB_USER_PASSWORD
) ?
1492 "Password" : "Passcode",
1493 config
[CONFIG_XAUTH_USERNAME
], ntop_buf
);
1494 pass
= getpass(prompt
);
1497 na
= new_isakmp_attribute(ap
->type
, reply_attr
);
1499 na
->u
.lots
.length
= strlen(pass
);
1500 na
->u
.lots
.data
= xallocc(na
->u
.lots
.length
);
1501 memcpy(na
->u
.lots
.data
, pass
, na
->u
.lots
.length
);
1502 memset(pass
, 0, na
->u
.lots
.length
);
1504 struct isakmp_attribute
*na
;
1505 na
= new_isakmp_attribute(ap
->type
, reply_attr
);
1507 na
->u
.lots
.length
= strlen(config
[CONFIG_XAUTH_PASSWORD
]);
1508 na
->u
.lots
.data
= xallocc(na
->u
.lots
.length
);
1509 memcpy(na
->u
.lots
.data
, config
[CONFIG_XAUTH_PASSWORD
],
1517 /* Send the response. */
1518 rp
= new_isakmp_payload(ISAKMP_PAYLOAD_MODECFG_ATTR
);
1519 rp
->u
.modecfg
.type
= ISAKMP_MODECFG_CFG_REPLY
;
1520 rp
->u
.modecfg
.id
= r
->payload
->next
->u
.modecfg
.id
;
1521 rp
->u
.modecfg
.attributes
= reply_attr
;
1522 sendrecv_phase2(s
, rp
, ISAKMP_EXCHANGE_MODECFG_TRANSACTION
,
1523 r
->message_id
, 0, 0, 0, 0, 0, 0, 0);
1525 free_isakmp_packet(r
);
1528 DEBUG(2, printf("S5.6\n"));
1530 /* The final SET should have just one attribute. */
1532 struct isakmp_attribute
*a
= r
->payload
->next
->u
.modecfg
.attributes
;
1533 uint16_t set_result
;
1536 || a
->type
!= ISAKMP_XAUTH_ATTRIB_STATUS
1537 || a
->af
!= isakmp_attr_16
|| a
->next
!= NULL
) {
1538 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1539 phase2_fatal(s
, "xauth SET response rejected: %s", reject
);
1541 set_result
= a
->u
.attr_16
;
1544 r
->payload
->next
->u
.modecfg
.type
= ISAKMP_MODECFG_CFG_ACK
;
1545 sendrecv_phase2(s
, r
->payload
->next
, ISAKMP_EXCHANGE_MODECFG_TRANSACTION
,
1546 r
->message_id
, 1, 0, 0, 0, 0, 0, 0);
1547 r
->payload
->next
= NULL
;
1548 free_isakmp_packet(r
);
1550 if (set_result
== 0)
1551 error(2, 0, "authentication unsuccessful");
1553 DEBUG(2, printf("S5.7\n"));
1557 static int do_phase_2_config(struct sa_block
*s
)
1559 struct isakmp_payload
*rp
;
1560 struct isakmp_attribute
*a
;
1561 struct isakmp_packet
*r
;
1565 int seen_address
= 0;
1570 gcry_randomize((uint8_t *) & msgid
, sizeof(msgid
), GCRY_WEAK_RANDOM
);
1574 rp
= new_isakmp_payload(ISAKMP_PAYLOAD_MODECFG_ATTR
);
1575 rp
->u
.modecfg
.type
= ISAKMP_MODECFG_CFG_REQUEST
;
1576 rp
->u
.modecfg
.id
= 20;
1579 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_APPLICATION_VERSION
, a
);
1580 a
->u
.lots
.length
= strlen(config
[CONFIG_VERSION
]);
1581 a
->u
.lots
.data
= xallocc(a
->u
.lots
.length
);
1582 memcpy(a
->u
.lots
.data
, config
[CONFIG_VERSION
], a
->u
.lots
.length
);
1584 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_CISCO_DDNS_HOSTNAME
, a
);
1585 a
->u
.lots
.length
= strlen(uts
.nodename
);
1586 a
->u
.lots
.data
= xallocc(a
->u
.lots
.length
);
1587 memcpy(a
->u
.lots
.data
, uts
.nodename
, a
->u
.lots
.length
);
1589 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_CISCO_BANNER
, a
);
1590 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_CISCO_DO_PFS
, a
);
1592 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_CISCO_UDP_ENCAP_PORT
, a
);
1593 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_CISCO_DEF_DOMAIN
, a
);
1594 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_NBNS
, a
);
1595 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_DNS
, a
);
1596 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_NETMASK
, a
);
1597 a
= new_isakmp_attribute(ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_ADDRESS
, a
);
1599 rp
->u
.modecfg
.attributes
= a
;
1600 sendrecv_phase2(s
, rp
, ISAKMP_EXCHANGE_MODECFG_TRANSACTION
, msgid
, 0, 0, 0, 0, 0, 0, 0);
1602 /* recv and check for notices */
1603 reject
= do_phase2_notice_check(s
, &r
);
1607 /* Check the transaction type & message ID are OK. */
1608 if (reject
== 0 && r
->message_id
!= msgid
)
1609 reject
= ISAKMP_N_INVALID_MESSAGE_ID
;
1610 if (reject
== 0 && r
->exchange_type
!= ISAKMP_EXCHANGE_MODECFG_TRANSACTION
)
1611 reject
= ISAKMP_N_INVALID_EXCHANGE_TYPE
;
1613 /* After the hash, expect an attribute block. */
1615 && (r
->payload
->next
== NULL
1616 || r
->payload
->next
->next
!= NULL
1617 || r
->payload
->next
->type
!= ISAKMP_PAYLOAD_MODECFG_ATTR
1619 || r
->payload
->next
->u
.modecfg
.id
!= 20
1621 || r
->payload
->next
->u
.modecfg
.type
!= ISAKMP_MODECFG_CFG_REPLY
))
1622 reject
= ISAKMP_N_PAYLOAD_MALFORMED
;
1625 phase2_fatal(s
, "configuration response rejected: %s", reject
);
1627 unsetenv("CISCO_BANNER");
1628 unsetenv("CISCO_DEF_DOMAIN");
1629 unsetenv("INTERNAL_IP4_NBNS");
1630 unsetenv("INTERNAL_IP4_DNS");
1631 unsetenv("INTERNAL_IP4_NETMASK");
1632 unsetenv("INTERNAL_IP4_ADDRESS");
1634 for (a
= r
->payload
->next
->u
.modecfg
.attributes
; a
&& reject
== 0; a
= a
->next
)
1636 case ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_ADDRESS
:
1637 if (a
->af
!= isakmp_attr_lots
|| a
->u
.lots
.length
!= 4)
1638 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1640 addenv_ipv4("INTERNAL_IP4_ADDRESS", a
->u
.lots
.data
);
1641 memcpy(s
->our_address
, a
->u
.lots
.data
, 4);
1646 case ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_NETMASK
:
1647 if (a
->af
== isakmp_attr_lots
&& a
->u
.lots
.length
== 0) {
1648 DEBUG(2, printf("ignoring zero length netmask\n"));
1651 if (a
->af
!= isakmp_attr_lots
|| a
->u
.lots
.length
!= 4)
1652 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1654 addenv_ipv4("INTERNAL_IP4_NETMASK", a
->u
.lots
.data
);
1657 case ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_DNS
:
1658 if (a
->af
!= isakmp_attr_lots
|| a
->u
.lots
.length
!= 4)
1659 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1661 addenv_ipv4("INTERNAL_IP4_DNS", a
->u
.lots
.data
);
1664 case ISAKMP_MODECFG_ATTRIB_INTERNAL_IP4_NBNS
:
1665 if (a
->af
!= isakmp_attr_lots
|| a
->u
.lots
.length
!= 4)
1666 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1668 addenv_ipv4("INTERNAL_IP4_NBNS", a
->u
.lots
.data
);
1671 case ISAKMP_MODECFG_ATTRIB_CISCO_DEF_DOMAIN
:
1672 if (a
->af
!= isakmp_attr_lots
) {
1673 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1676 strbuf
= xallocc(a
->u
.lots
.length
+ 1);
1677 memcpy(strbuf
, a
->u
.lots
.data
, a
->u
.lots
.length
);
1678 addenv("CISCO_DEF_DOMAIN", strbuf
);
1682 case ISAKMP_MODECFG_ATTRIB_CISCO_BANNER
:
1683 if (a
->af
!= isakmp_attr_lots
) {
1684 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1687 strbuf
= xallocc(a
->u
.lots
.length
+ 1);
1688 memcpy(strbuf
, a
->u
.lots
.data
, a
->u
.lots
.length
);
1689 addenv("CISCO_BANNER", strbuf
);
1691 DEBUG(1, printf("Banner: "));
1692 DEBUG(1, fwrite(a
->u
.lots
.data
, a
->u
.lots
.length
, 1, stdout
));
1693 DEBUG(1, printf("\n"));
1696 case ISAKMP_MODECFG_ATTRIB_APPLICATION_VERSION
:
1697 DEBUG(2, printf("Remote Application Version: "));
1698 DEBUG(2, fwrite(a
->u
.lots
.data
, a
->u
.lots
.length
, 1, stdout
));
1699 DEBUG(2, printf("\n"));
1702 case ISAKMP_MODECFG_ATTRIB_CISCO_DO_PFS
:
1703 if (a
->af
!= isakmp_attr_16
)
1704 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1706 s
->do_pfs
= a
->u
.attr_16
;
1707 DEBUG(2, printf("got pfs setting: %d\n", s
->do_pfs
));
1711 case ISAKMP_MODECFG_ATTRIB_CISCO_UDP_ENCAP_PORT
:
1712 if (a
->af
!= isakmp_attr_16
)
1713 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1715 s
->peer_udpencap_port
= a
->u
.attr_16
;
1716 DEBUG(2, printf("got peer udp encapsulation port: %hu\n", s
->peer_udpencap_port
));
1721 DEBUG(2, printf("unknown attriube %d / 0x%X\n", a
->type
, a
->type
));
1725 if (reject
== 0 && !seen_address
)
1726 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1729 phase2_fatal(s
, "configuration response rejected: %s", reject
);
1731 DEBUG(1, printf("got address %s\n", getenv("INTERNAL_IP4_ADDRESS")));
1735 struct isakmp_attribute
*make_transform_ipsec(int dh_group
, int hash
, int keylen
)
1737 struct isakmp_attribute
*a
= NULL
;
1739 a
= new_isakmp_attribute(ISAKMP_IPSEC_ATTRIB_SA_LIFE_DURATION
, a
);
1740 a
->af
= isakmp_attr_lots
;
1741 a
->u
.lots
.length
= 4;
1742 a
->u
.lots
.data
= xallocc(a
->u
.lots
.length
);
1743 *((uint32_t *) a
->u
.lots
.data
) = htonl(2147483);
1744 a
= new_isakmp_attribute_16(ISAKMP_IPSEC_ATTRIB_SA_LIFE_TYPE
, IPSEC_LIFE_SECONDS
, a
);
1747 a
= new_isakmp_attribute_16(ISAKMP_IPSEC_ATTRIB_GROUP_DESC
, dh_group
, a
);
1748 a
= new_isakmp_attribute_16(ISAKMP_IPSEC_ATTRIB_AUTH_ALG
, hash
, a
);
1749 a
= new_isakmp_attribute_16(ISAKMP_IPSEC_ATTRIB_ENCAP_MODE
, encap_mode
, a
);
1751 a
= new_isakmp_attribute_16(ISAKMP_IPSEC_ATTRIB_KEY_LENGTH
, keylen
, a
);
1756 struct isakmp_payload
*make_our_sa_ipsec(struct sa_block
*s
)
1758 struct isakmp_payload
*r
= new_isakmp_payload(ISAKMP_PAYLOAD_SA
);
1759 struct isakmp_payload
*p
= NULL
, *pn
;
1760 struct isakmp_attribute
*a
;
1761 int dh_grp
= get_dh_group_ipsec(s
->do_pfs
)->ipsec_sa_id
;
1762 unsigned int crypt
, hash
, keylen
;
1765 r
= new_isakmp_payload(ISAKMP_PAYLOAD_SA
);
1766 r
->u
.sa
.doi
= ISAKMP_DOI_IPSEC
;
1767 r
->u
.sa
.situation
= ISAKMP_IPSEC_SIT_IDENTITY_ONLY
;
1768 r
->u
.sa
.proposals
= new_isakmp_payload(ISAKMP_PAYLOAD_P
);
1769 r
->u
.sa
.proposals
->u
.p
.spi_size
= 4;
1770 r
->u
.sa
.proposals
->u
.p
.spi
= xallocc(4);
1771 /* The sadb_sa_spi field is already in network order. */
1772 memcpy(r
->u
.sa
.proposals
->u
.p
.spi
, &s
->tous_esp_spi
, 4);
1773 r
->u
.sa
.proposals
->u
.p
.prot_id
= ISAKMP_IPSEC_PROTO_IPSEC_ESP
;
1774 for (crypt
= 0; supp_crypt
[crypt
].name
!= NULL
; crypt
++) {
1775 if ((supp_crypt
[crypt
].my_id
== GCRY_CIPHER_DES
) && (opt_1des
== 0))
1777 keylen
= supp_crypt
[crypt
].keylen
;
1778 for (hash
= 0; supp_hash
[hash
].name
!= NULL
; hash
++) {
1780 p
= new_isakmp_payload(ISAKMP_PAYLOAD_P
);
1781 p
->u
.p
.spi_size
= 4;
1782 p
->u
.p
.spi
= xallocc(4);
1783 /* The sadb_sa_spi field is already in network order. */
1784 memcpy(p
->u
.p
.spi
, &s
->tous_esp_spi
, 4);
1785 p
->u
.p
.prot_id
= ISAKMP_IPSEC_PROTO_IPSEC_ESP
;
1786 p
->u
.p
.transforms
= new_isakmp_payload(ISAKMP_PAYLOAD_T
);
1787 p
->u
.p
.transforms
->u
.t
.id
= supp_crypt
[crypt
].ipsec_sa_id
;
1788 a
= make_transform_ipsec(dh_grp
, supp_hash
[hash
].ipsec_sa_id
, keylen
);
1789 p
->u
.p
.transforms
->u
.t
.attributes
= a
;
1793 for (i
= 0, pn
= p
; pn
; pn
= pn
->next
)
1794 pn
->u
.p
.number
= i
++;
1795 r
->u
.sa
.proposals
= p
;
1799 static void setup_link(struct sa_block
*s
)
1801 struct isakmp_payload
*rp
, *us
, *ke
= NULL
, *them
, *nonce_r
= NULL
;
1802 struct isakmp_packet
*r
;
1803 struct group
*dh_grp
= NULL
;
1806 uint8_t *p_flat
= NULL
, *realiv
= NULL
, realiv_msgid
[4];
1808 uint8_t nonce
[20], *dh_public
= NULL
;
1809 int ipsec_cry_algo
= 0, ipsec_hash_algo
= 0, i
;
1811 DEBUG(2, printf("S7.1\n"));
1812 /* Set up the Diffie-Hellman stuff. */
1813 if (get_dh_group_ipsec(s
->do_pfs
)->my_id
) {
1814 dh_grp
= group_get(get_dh_group_ipsec(s
->do_pfs
)->my_id
);
1815 DEBUG(3, printf("len = %d\n", dh_getlen(dh_grp
)));
1816 dh_public
= xallocc(dh_getlen(dh_grp
));
1817 dh_create_exchange(dh_grp
, dh_public
);
1818 hex_dump("dh_public", dh_public
, dh_getlen(dh_grp
));
1821 gcry_randomize((uint8_t *) & s
->tous_esp_spi
, sizeof(s
->tous_esp_spi
), GCRY_WEAK_RANDOM
);
1822 rp
= make_our_sa_ipsec(s
);
1823 gcry_randomize((uint8_t *) nonce
, sizeof(nonce
), GCRY_WEAK_RANDOM
);
1824 rp
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_NONCE
, nonce
, sizeof(nonce
));
1826 us
= new_isakmp_payload(ISAKMP_PAYLOAD_ID
);
1827 us
->u
.id
.type
= ISAKMP_IPSEC_ID_IPV4_ADDR
;
1828 us
->u
.id
.length
= 4;
1829 us
->u
.id
.data
= xallocc(4);
1830 memcpy(us
->u
.id
.data
, s
->our_address
, sizeof(struct in_addr
));
1831 them
= new_isakmp_payload(ISAKMP_PAYLOAD_ID
);
1832 them
->u
.id
.type
= ISAKMP_IPSEC_ID_IPV4_ADDR_SUBNET
;
1833 them
->u
.id
.length
= 8;
1834 them
->u
.id
.data
= xallocc(8);
1835 memset(them
->u
.id
.data
, 0, 8);
1839 rp
->next
->next
= us
;
1841 rp
->next
->next
= new_isakmp_data_payload(ISAKMP_PAYLOAD_KE
,
1842 dh_public
, dh_getlen(dh_grp
));
1843 rp
->next
->next
->next
= us
;
1846 gcry_randomize((uint8_t *) & msgid
, sizeof(&msgid
), GCRY_WEAK_RANDOM
);
1850 DEBUG(2, printf("S7.2\n"));
1851 for (i
= 0; i
< 4; i
++) {
1852 sendrecv_phase2(s
, rp
, ISAKMP_EXCHANGE_IKE_QUICK
,
1853 msgid
, 0, &p_flat
, &p_size
, 0, 0, 0, 0);
1855 if (realiv
== NULL
) {
1856 realiv
= xallocc(s
->ivlen
);
1857 memcpy(realiv
, s
->current_iv
, s
->ivlen
);
1858 memcpy(realiv_msgid
, s
->current_iv_msgid
, 4);
1861 DEBUG(2, printf("S7.3\n"));
1862 reject
= unpack_verify_phase2(s
, r_packet
, r_length
, &r
, nonce
, sizeof(nonce
));
1864 DEBUG(2, printf("S7.4\n"));
1865 if (((reject
== 0) || (reject
== ISAKMP_N_AUTHENTICATION_FAILED
))
1866 && r
->exchange_type
== ISAKMP_EXCHANGE_INFORMATIONAL
) {
1867 /* handle notifie responder-lifetime (ignore) */
1868 /* (broken hash => ignore AUTHENTICATION_FAILED) */
1869 if (reject
== 0 && r
->payload
->next
->type
!= ISAKMP_PAYLOAD_N
)
1870 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1873 && r
->payload
->next
->u
.n
.type
==
1874 ISAKMP_N_IPSEC_RESPONDER_LIFETIME
) {
1875 DEBUG(2, printf("ignoring responder-lifetime notify\n"));
1876 memcpy(s
->current_iv
, realiv
, s
->ivlen
);
1877 memcpy(s
->current_iv_msgid
, realiv_msgid
, 4);
1882 /* Check the transaction type & message ID are OK. */
1883 if (reject
== 0 && r
->message_id
!= msgid
)
1884 reject
= ISAKMP_N_INVALID_MESSAGE_ID
;
1886 if (reject
== 0 && r
->exchange_type
!= ISAKMP_EXCHANGE_IKE_QUICK
)
1887 reject
= ISAKMP_N_INVALID_EXCHANGE_TYPE
;
1889 /* The SA payload must be second. */
1890 if (reject
== 0 && r
->payload
->next
->type
!= ISAKMP_PAYLOAD_SA
)
1891 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
1900 DEBUG(2, printf("S7.5\n"));
1902 phase2_fatal(s
, "quick mode response rejected: %s\ncheck pfs setting", reject
);
1904 DEBUG(2, printf("S7.6\n"));
1905 for (rp
= r
->payload
->next
; rp
&& reject
== 0; rp
= rp
->next
)
1907 case ISAKMP_PAYLOAD_SA
:
1908 if (reject
== 0 && rp
->u
.sa
.doi
!= ISAKMP_DOI_IPSEC
)
1909 reject
= ISAKMP_N_DOI_NOT_SUPPORTED
;
1910 if (reject
== 0 && rp
->u
.sa
.situation
!= ISAKMP_IPSEC_SIT_IDENTITY_ONLY
)
1911 reject
= ISAKMP_N_SITUATION_NOT_SUPPORTED
;
1913 (rp
->u
.sa
.proposals
== NULL
|| rp
->u
.sa
.proposals
->next
!= NULL
))
1914 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1916 rp
->u
.sa
.proposals
->u
.p
.prot_id
!= ISAKMP_IPSEC_PROTO_IPSEC_ESP
)
1917 reject
= ISAKMP_N_INVALID_PROTOCOL_ID
;
1918 if (reject
== 0 && rp
->u
.sa
.proposals
->u
.p
.spi_size
!= 4)
1919 reject
= ISAKMP_N_INVALID_SPI
;
1921 (rp
->u
.sa
.proposals
->u
.p
.transforms
== NULL
1922 || rp
->u
.sa
.proposals
->u
.p
.transforms
->next
!= NULL
))
1923 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1925 struct isakmp_attribute
*a
1926 = rp
->u
.sa
.proposals
->u
.p
.transforms
->u
.t
.attributes
;
1927 int seen_enc
= rp
->u
.sa
.proposals
->u
.p
.transforms
->u
.t
.id
;
1928 int seen_auth
= 0, seen_encap
= 0, seen_group
= 0, seen_keylen
= 0;
1930 memcpy(&s
->tothem_esp_spi
, rp
->u
.sa
.proposals
->u
.p
.spi
, 4);
1932 for (; a
&& reject
== 0; a
= a
->next
)
1934 case ISAKMP_IPSEC_ATTRIB_AUTH_ALG
:
1935 if (a
->af
== isakmp_attr_16
)
1936 seen_auth
= a
->u
.attr_16
;
1938 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1940 case ISAKMP_IPSEC_ATTRIB_ENCAP_MODE
:
1941 if (a
->af
== isakmp_attr_16
&&
1942 a
->u
.attr_16
== encap_mode
)
1945 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1947 case ISAKMP_IPSEC_ATTRIB_GROUP_DESC
:
1949 a
->af
== isakmp_attr_16
&&
1951 get_dh_group_ipsec(s
->do_pfs
)->ipsec_sa_id
)
1954 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1956 case ISAKMP_IPSEC_ATTRIB_KEY_LENGTH
:
1957 if (a
->af
== isakmp_attr_16
)
1958 seen_keylen
= a
->u
.attr_16
;
1960 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1962 case ISAKMP_IPSEC_ATTRIB_SA_LIFE_TYPE
:
1963 case ISAKMP_IPSEC_ATTRIB_SA_LIFE_DURATION
:
1966 reject
= ISAKMP_N_ATTRIBUTES_NOT_SUPPORTED
;
1969 if (reject
== 0 && (!seen_auth
|| !seen_encap
||
1970 (dh_grp
&& !seen_group
)))
1971 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1974 && get_algo(SUPP_ALGO_HASH
, SUPP_ALGO_IPSEC_SA
, seen_auth
,
1976 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1978 && get_algo(SUPP_ALGO_CRYPT
, SUPP_ALGO_IPSEC_SA
, seen_enc
,
1979 NULL
, seen_keylen
) == NULL
)
1980 reject
= ISAKMP_N_BAD_PROPOSAL_SYNTAX
;
1984 get_algo(SUPP_ALGO_CRYPT
, SUPP_ALGO_IPSEC_SA
,
1985 seen_enc
, NULL
, seen_keylen
)->my_id
;
1987 get_algo(SUPP_ALGO_HASH
, SUPP_ALGO_IPSEC_SA
,
1988 seen_auth
, NULL
, 0)->my_id
;
1989 DEBUG(1, printf("IPSEC SA selected %s-%s\n",
1990 get_algo(SUPP_ALGO_CRYPT
,
1991 SUPP_ALGO_IPSEC_SA
, seen_enc
, NULL
,
1993 get_algo(SUPP_ALGO_HASH
, SUPP_ALGO_IPSEC_SA
,
1994 seen_auth
, NULL
, 0)->name
));
1999 case ISAKMP_PAYLOAD_N
:
2001 case ISAKMP_PAYLOAD_ID
:
2003 case ISAKMP_PAYLOAD_KE
:
2006 case ISAKMP_PAYLOAD_NONCE
:
2011 reject
= ISAKMP_N_INVALID_PAYLOAD_TYPE
;
2015 if (reject
== 0 && nonce_r
== NULL
)
2016 reject
= ISAKMP_N_INVALID_HASH_INFORMATION
;
2017 if (reject
== 0 && dh_grp
&& (ke
== NULL
|| ke
->u
.ke
.length
!= dh_getlen(dh_grp
)))
2018 reject
= ISAKMP_N_INVALID_KEY_INFORMATION
;
2020 phase2_fatal(s
, "quick mode response rejected [2]: %s", reject
);
2022 /* send final packet */
2023 sendrecv_phase2(s
, NULL
, ISAKMP_EXCHANGE_IKE_QUICK
,
2024 msgid
, 1, 0, 0, nonce
, sizeof(nonce
),
2025 nonce_r
->u
.nonce
.data
, nonce_r
->u
.nonce
.length
);
2027 DEBUG(2, printf("S7.7\n"));
2028 /* Create the delete payload, now that we have all the information. */
2030 struct isakmp_payload
*d_isakmp
, *d_ipsec
;
2033 gcry_randomize((uint8_t *) & del_msgid
, sizeof(del_msgid
), GCRY_WEAK_RANDOM
);
2034 d_isakmp
= new_isakmp_payload(ISAKMP_PAYLOAD_D
);
2035 d_isakmp
->u
.d
.doi
= ISAKMP_DOI_IPSEC
;
2036 d_isakmp
->u
.d
.protocol
= ISAKMP_IPSEC_PROTO_ISAKMP
;
2037 d_isakmp
->u
.d
.spi_length
= 2 * ISAKMP_COOKIE_LENGTH
;
2038 d_isakmp
->u
.d
.num_spi
= 1;
2039 d_isakmp
->u
.d
.spi
= xallocc(1 * sizeof(uint8_t *));
2040 d_isakmp
->u
.d
.spi
[0] = xallocc(2 * ISAKMP_COOKIE_LENGTH
);
2041 memcpy(d_isakmp
->u
.d
.spi
[0] + ISAKMP_COOKIE_LENGTH
* 0, s
->i_cookie
,
2042 ISAKMP_COOKIE_LENGTH
);
2043 memcpy(d_isakmp
->u
.d
.spi
[0] + ISAKMP_COOKIE_LENGTH
* 1, s
->r_cookie
,
2044 ISAKMP_COOKIE_LENGTH
);
2045 d_ipsec
= new_isakmp_payload(ISAKMP_PAYLOAD_D
);
2046 d_ipsec
->next
= d_isakmp
;
2047 d_ipsec
->u
.d
.doi
= ISAKMP_DOI_IPSEC
;
2048 d_ipsec
->u
.d
.protocol
= ISAKMP_IPSEC_PROTO_IPSEC_ESP
;
2049 d_ipsec
->u
.d
.spi_length
= 4;
2050 d_ipsec
->u
.d
.num_spi
= 2;
2051 d_ipsec
->u
.d
.spi
= xallocc(2 * sizeof(uint8_t *));
2052 d_ipsec
->u
.d
.spi
[0] = xallocc(d_ipsec
->u
.d
.spi_length
);
2053 memcpy(d_ipsec
->u
.d
.spi
[0], &s
->tous_esp_spi
, 4);
2054 d_ipsec
->u
.d
.spi
[1] = xallocc(d_ipsec
->u
.d
.spi_length
);
2055 memcpy(d_ipsec
->u
.d
.spi
[1], &s
->tothem_esp_spi
, 4);
2056 phase2_authpacket(s
, d_ipsec
, ISAKMP_EXCHANGE_INFORMATIONAL
,
2057 del_msgid
, &s
->kill_packet
, &s
->kill_packet_size
,
2058 nonce
, sizeof(nonce
), nonce_r
->u
.nonce
.data
, nonce_r
->u
.nonce
.length
);
2059 isakmp_crypt(s
, s
->kill_packet
, s
->kill_packet_size
, 1);
2061 DEBUG(2, printf("S7.8\n"));
2063 /* Set up the interface here so it's ready when our acknowledgement
2066 DEBUG(2, printf("S7.9\n"));
2068 uint8_t *tous_keys
, *tothem_keys
;
2069 struct sockaddr_in tothem_dest
, tous_dest
;
2070 unsigned char *dh_shared_secret
= NULL
;
2071 int tunnelfd
= sockfd
;
2074 /* Determine the shared secret. */
2075 dh_shared_secret
= xallocc(dh_getlen(dh_grp
));
2076 dh_create_shared(dh_grp
, dh_shared_secret
, ke
->u
.ke
.data
);
2077 hex_dump("dh_shared_secret", dh_shared_secret
, dh_getlen(dh_grp
));
2079 tous_keys
= gen_keymat(s
, ISAKMP_IPSEC_PROTO_IPSEC_ESP
, s
->tous_esp_spi
,
2080 ipsec_hash_algo
, ipsec_cry_algo
,
2081 dh_shared_secret
, dh_grp
? dh_getlen(dh_grp
) : 0,
2082 nonce
, sizeof(nonce
), nonce_r
->u
.nonce
.data
, nonce_r
->u
.nonce
.length
);
2083 memset(&tothem_dest
, 0, sizeof(tothem_dest
));
2084 tothem_dest
.sin_family
= AF_INET
;
2085 memcpy(&tothem_dest
.sin_addr
, s
->our_address
, 4);
2086 tothem_keys
= gen_keymat(s
, ISAKMP_IPSEC_PROTO_IPSEC_ESP
, s
->tothem_esp_spi
,
2087 ipsec_hash_algo
, ipsec_cry_algo
,
2088 dh_shared_secret
, dh_grp
? dh_getlen(dh_grp
) : 0,
2089 nonce
, sizeof(nonce
), nonce_r
->u
.nonce
.data
, nonce_r
->u
.nonce
.length
);
2090 memcpy(&tous_dest
, dest_addr
, sizeof(tous_dest
));
2091 if (opt_udpencap
&& s
->peer_udpencap_port
) {
2093 tunnelfd
= make_socket(htons(opt_udpencapport
));
2094 tous_dest
.sin_port
= htons(s
->peer_udpencap_port
);
2095 encap_mode
= IPSEC_ENCAP_UDP_TUNNEL
;
2097 DEBUG(2, printf("S7.10\n"));
2098 vpnc_doit(s
->tous_esp_spi
, tous_keys
, &tothem_dest
,
2099 s
->tothem_esp_spi
, tothem_keys
, (struct sockaddr_in
*)&tous_dest
,
2100 s
->tun_fd
, ipsec_hash_algo
, ipsec_cry_algo
,
2101 s
->kill_packet
, s
->kill_packet_size
, dest_addr
,
2102 encap_mode
, tunnelfd
,
2103 config
[CONFIG_PID_FILE
]);
2107 int main(int argc
, char **argv
)
2109 int do_load_balance
;
2110 const uint8_t hex_test
[] = { 0, 1, 2, 3 };
2113 gcry_check_version("1.1.90");
2114 gcry_control(GCRYCTL_INIT_SECMEM
, 16384, 0);
2116 memset(oursa
, 0, sizeof(oursa
));
2118 do_config(argc
, argv
);
2120 hex_dump("hex_test", hex_test
, sizeof(hex_test
));
2122 DEBUG(1, printf("vpnc version " VERSION
"\n"));
2123 DEBUG(2, printf("S1\n"));
2124 dest_addr
= init_sockaddr(config
[CONFIG_IPSEC_GATEWAY
], ISAKMP_PORT
);
2125 DEBUG(2, printf("S2\n"));
2126 local_port
= htons(atoi(config
[CONFIG_LOCAL_PORT
]));
2127 sockfd
= make_socket(local_port
);
2128 DEBUG(2, printf("S3\n"));
2129 setup_tunnel(oursa
);
2131 do_load_balance
= 0;
2133 DEBUG(2, printf("S4\n"));
2134 do_phase_1(config
[CONFIG_IPSEC_ID
], config
[CONFIG_IPSEC_SECRET
], oursa
);
2135 DEBUG(2, printf("S5\n"));
2136 if (oursa
->auth_algo
== IKE_AUTH_XAUTHInitPreShared
)
2137 do_load_balance
= do_phase_2_xauth(oursa
);
2138 DEBUG(2, printf("S6\n"));
2139 if (do_load_balance
== 0)
2140 do_load_balance
= do_phase_2_config(oursa
);
2141 } while (do_load_balance
);
2142 DEBUG(2, printf("S7\n"));