1 /* $KAME: localconf.c,v 1.33 2001/08/09 07:32:19 sakane Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
50 #include "localconf.h"
51 #include "algorithm.h"
52 #include "isakmp_var.h"
54 #include "ipsec_doi.h"
55 #include "grabmyaddr.h"
62 #include <CoreFoundation/CoreFoundation.h>
63 #if HAVE_SECURITY_FRAMEWORK
64 #include <Security/Security.h>
67 struct localconf
*lcconf
;
68 struct localconf
*saved_lcconf
;
70 static void setdefault (void);
75 lcconf
= racoon_calloc(1, sizeof(*lcconf
));
77 errx(1, "failed to allocate local conf.");
80 lcconf
->sock_vpncontrol
= -1; /* not to be done during flush */
81 lcconf
->racoon_conf
= LC_DEFAULT_CF
;
82 TAILQ_INIT(&lcconf
->saved_msg_queue
);
92 for (i
= 0; i
< LC_PATHTYPE_MAX
; i
++) {
93 if (lcconf
->pathinfo
[i
]) {
94 racoon_free(lcconf
->pathinfo
[i
]);
95 lcconf
->pathinfo
[i
] = NULL
;
98 for (i
= 0; i
< IDTYPE_MAX
; i
++) {
100 vfree(lcconf
->ident
[i
]);
101 lcconf
->ident
[i
] = NULL
;
103 if (lcconf
->ext_nat_id
) {
104 vfree(lcconf
->ext_nat_id
);
105 lcconf
->ext_nat_id
= NULL
;
114 lcconf
->autograbaddr
= 1;
115 lcconf
->port_isakmp
= PORT_ISAKMP
;
116 lcconf
->port_isakmp_natt
= PORT_ISAKMP_NATT
;
117 lcconf
->default_af
= AF_INET
;
118 lcconf
->pad_random
= LC_DEFAULT_PAD_RANDOM
;
119 lcconf
->pad_randomlen
= LC_DEFAULT_PAD_RANDOMLEN
;
120 lcconf
->pad_maxsize
= LC_DEFAULT_PAD_MAXSIZE
;
121 lcconf
->pad_strict
= LC_DEFAULT_PAD_STRICT
;
122 lcconf
->pad_excltail
= LC_DEFAULT_PAD_EXCLTAIL
;
123 lcconf
->retry_counter
= LC_DEFAULT_RETRY_COUNTER
;
124 lcconf
->retry_interval
= LC_DEFAULT_RETRY_INTERVAL
;
125 lcconf
->count_persend
= LC_DEFAULT_COUNT_PERSEND
;
126 lcconf
->secret_size
= LC_DEFAULT_SECRETSIZE
;
127 lcconf
->retry_checkph1
= LC_DEFAULT_RETRY_CHECKPH1
;
128 lcconf
->wait_ph2complete
= LC_DEFAULT_WAIT_PH2COMPLETE
;
129 lcconf
->strict_address
= FALSE
;
130 lcconf
->complex_bundle
= TRUE
; /*XXX FALSE;*/
131 lcconf
->natt_ka_interval
= LC_DEFAULT_NATT_KA_INTERVAL
;
132 lcconf
->auto_exit_delay
= 0;
133 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_SET
;
134 lcconf
->auto_exit_state
|= LC_AUTOEXITSTATE_CLIENT
; /* always auto exit as default */
141 saved_lcconf
= lcconf
;
151 lcconf
= saved_lcconf
;
166 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key by name.\n");
168 id
= racoon_calloc(1, 1 + id0
->l
- sizeof(struct ipsecdoi_id_b
));
171 "failed to get psk buffer.\n");
174 memcpy(id
, id0
->v
+ sizeof(struct ipsecdoi_id_b
),
175 id0
->l
- sizeof(struct ipsecdoi_id_b
));
176 id
[id0
->l
- sizeof(struct ipsecdoi_id_b
)] = '\0';
178 key
= getpsk(id
, id0
->l
- sizeof(struct ipsecdoi_id_b
));
188 * get PSK from keyChain.
191 getpskfromkeychain(const char *name
, u_int8_t etype
, int secrettype
, vchar_t
*id_p
)
193 SecKeychainRef keychain
= NULL
;
195 void *cur_password
= NULL
;
196 UInt32 cur_password_len
= 0;
198 char serviceName
[] = "com.apple.net.racoon";
200 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key from keychain.\n");
202 status
= SecKeychainSetPreferenceDomain(kSecPreferencesDomainSystem
);
203 if (status
!= noErr
) {
205 "failed to set system keychain domain.\n");
209 status
= SecKeychainCopyDomainDefault(kSecPreferencesDomainSystem
,
211 if (status
!= noErr
) {
213 "failed to get system keychain domain.\n");
217 if (secrettype
== SECRETTYPE_KEYCHAIN_BY_ID
&& etype
== ISAKMP_ETYPE_AGG
) {
218 /* try looking up based on peers id */
220 char* peer_id
= NULL
;
221 int idlen
= id_p
->l
- sizeof(struct ipsecdoi_id_b
);
222 u_int8_t id_type
= (ALIGNED_CAST(struct ipsecdoi_id_b
*)(id_p
->v
))->type
;
225 case IPSECDOI_ID_IPV4_ADDR
:
226 case IPSECDOI_ID_IPV6_ADDR
:
227 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
228 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
229 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
230 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
231 case IPSECDOI_ID_DER_ASN1_DN
:
232 case IPSECDOI_ID_DER_ASN1_GN
:
236 case IPSECDOI_ID_FQDN
:
237 case IPSECDOI_ID_USER_FQDN
:
238 case IPSECDOI_ID_KEY_ID
:
239 peer_id
= racoon_malloc(1 + idlen
);
242 memcpy(peer_id
, id_p
->v
+ sizeof(struct ipsecdoi_id_b
), idlen
);
243 *(peer_id
+ idlen
) = '\0';
245 "getting shared secret from keychain using %s.\n", peer_id
);
253 status
= SecKeychainFindGenericPassword(keychain
,
262 /* try find it using using only the peer id. */
263 if (status
== errSecItemNotFound
)
264 status
= SecKeychainFindGenericPassword(keychain
,
273 racoon_free(peer_id
);
276 /* otherwise fall through to use the default value */
280 /* use the value in remote config sharedsecret field
281 this is either the value specified for lookup or the
282 default when lookup by id fails.
284 status
= SecKeychainFindGenericPassword(keychain
,
293 /* try find it using using only name. */
294 if (status
== errSecItemNotFound
)
295 status
= SecKeychainFindGenericPassword(keychain
,
310 case errSecItemNotFound
:
315 "failed to get preshared key from system keychain (error %ld).\n", (long)status
);
321 key
= vmalloc(cur_password_len
);
324 "failed to allocate key buffer.\n");
326 memcpy(key
->v
, cur_password
, cur_password_len
);
338 * get PSK by address.
342 struct sockaddr_storage
*remote
;
345 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
347 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key by addr.\n");
349 GETNAMEINFO((struct sockaddr
*)remote
, addr
, port
);
351 key
= getpsk(addr
, strlen(addr
));
362 char buf
[1024]; /* XXX how is variable length ? */
368 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key from file.\n");
370 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], 1) == 0)
371 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], "r");
376 "failed to open pre_share_key file %s\n",
377 lcconf
->pathinfo
[LC_PATHTYPE_PSK
]);
381 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
386 /* search the end of 1st string. */
387 for (p
= buf
; *p
!= '\0' && !isspace((int)*p
); p
++)
390 continue; /* no 2nd parameter */
392 /* search the 1st of 2nd string. */
393 while (isspace((int)*++p
))
396 continue; /* no 2nd parameter */
398 if (strncmp(buf
, str
, len
) == 0 && buf
[len
] == '\0') {
401 for (q
= p
; *q
!= '\0' && *q
!= '\n'; q
++)
405 /* fix key if hex string */
406 if (strncmp(p
, "0x", 2) == 0) {
407 k
= str2val(p
+ 2, 16, &keylen
);
410 "failed to get psk buffer.\n");
416 key
= vmalloc(keylen
);
419 "failed to allocate key buffer.\n");
422 memcpy(key
->v
, p
, key
->l
);
435 * get a file name of a type specified.
438 getpathname(path
, len
, type
, name
)
443 snprintf(path
, len
, "%s%s%s",
444 name
[0] == '/' ? "" : lcconf
->pathinfo
[type
],
445 name
[0] == '/' ? "" : "/",
448 plog(ASL_LEVEL_DEBUG
, "filename: %s\n", path
);
452 static int lc_doi2idtype
[] = {
456 LC_IDENTTYPE_USERFQDN
,
462 LC_IDENTTYPE_CERTNAME
,
468 * convert DOI value to idtype
476 if (ARRAYLEN(lc_doi2idtype
) > idtype
)
477 return lc_doi2idtype
[idtype
];
482 static int lc_sittype2doi
[] = {
483 IPSECDOI_SIT_IDENTITY_ONLY
,
484 IPSECDOI_SIT_SECRECY
,
485 IPSECDOI_SIT_INTEGRITY
,
489 * convert sittype to DOI value.
497 if (ARRAYLEN(lc_sittype2doi
) > sittype
)
498 return lc_sittype2doi
[sittype
];
502 static int lc_doitype2doi
[] = {
507 * convert doitype to DOI value.
515 if (ARRAYLEN(lc_doitype2doi
) > doitype
)
516 return lc_doitype2doi
[doitype
];