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>
66 typedef void * SecKeychainRef
;
69 struct localconf
*lcconf
;
70 struct localconf
*saved_lcconf
;
72 static void setdefault (void);
77 lcconf
= racoon_calloc(1, sizeof(*lcconf
));
79 errx(1, "failed to allocate local conf.");
82 lcconf
->sock_vpncontrol
= -1; /* not to be done during flush */
83 lcconf
->racoon_conf
= LC_DEFAULT_CF
;
84 TAILQ_INIT(&lcconf
->saved_msg_queue
);
94 for (i
= 0; i
< LC_PATHTYPE_MAX
; i
++) {
95 if (lcconf
->pathinfo
[i
]) {
96 racoon_free(lcconf
->pathinfo
[i
]);
97 lcconf
->pathinfo
[i
] = NULL
;
100 for (i
= 0; i
< IDTYPE_MAX
; i
++) {
101 if (lcconf
->ident
[i
])
102 vfree(lcconf
->ident
[i
]);
103 lcconf
->ident
[i
] = NULL
;
105 if (lcconf
->ext_nat_id
) {
106 vfree(lcconf
->ext_nat_id
);
107 lcconf
->ext_nat_id
= NULL
;
116 lcconf
->autograbaddr
= 1;
117 lcconf
->port_isakmp
= PORT_ISAKMP
;
118 lcconf
->port_isakmp_natt
= PORT_ISAKMP_NATT
;
119 lcconf
->default_af
= AF_INET
;
120 lcconf
->pad_random
= LC_DEFAULT_PAD_RANDOM
;
121 lcconf
->pad_randomlen
= LC_DEFAULT_PAD_RANDOMLEN
;
122 lcconf
->pad_maxsize
= LC_DEFAULT_PAD_MAXSIZE
;
123 lcconf
->pad_strict
= LC_DEFAULT_PAD_STRICT
;
124 lcconf
->pad_excltail
= LC_DEFAULT_PAD_EXCLTAIL
;
125 lcconf
->retry_counter
= LC_DEFAULT_RETRY_COUNTER
;
126 lcconf
->retry_interval
= LC_DEFAULT_RETRY_INTERVAL
;
127 lcconf
->count_persend
= LC_DEFAULT_COUNT_PERSEND
;
128 lcconf
->secret_size
= LC_DEFAULT_SECRETSIZE
;
129 lcconf
->retry_checkph1
= LC_DEFAULT_RETRY_CHECKPH1
;
130 lcconf
->wait_ph2complete
= LC_DEFAULT_WAIT_PH2COMPLETE
;
131 lcconf
->strict_address
= FALSE
;
132 lcconf
->complex_bundle
= TRUE
; /*XXX FALSE;*/
133 lcconf
->natt_ka_interval
= LC_DEFAULT_NATT_KA_INTERVAL
;
134 lcconf
->auto_exit_delay
= 0;
135 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_SET
;
136 lcconf
->auto_exit_state
|= LC_AUTOEXITSTATE_CLIENT
; /* always auto exit as default */
143 saved_lcconf
= lcconf
;
153 lcconf
= saved_lcconf
;
168 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key by name.\n");
170 id
= racoon_calloc(1, 1 + id0
->l
- sizeof(struct ipsecdoi_id_b
));
173 "failed to get psk buffer.\n");
176 memcpy(id
, id0
->v
+ sizeof(struct ipsecdoi_id_b
),
177 id0
->l
- sizeof(struct ipsecdoi_id_b
));
178 id
[id0
->l
- sizeof(struct ipsecdoi_id_b
)] = '\0';
180 key
= getpsk(id
, id0
->l
- sizeof(struct ipsecdoi_id_b
));
190 * get PSK from keyChain.
193 getpskfromkeychain(const char *name
, u_int8_t etype
, int secrettype
, vchar_t
*id_p
)
195 SecKeychainRef keychain
= NULL
;
197 void *cur_password
= NULL
;
198 UInt32 cur_password_len
= 0;
200 char serviceName
[] = "com.apple.net.racoon";
202 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key from keychain.\n");
204 status
= SecKeychainSetPreferenceDomain(kSecPreferencesDomainSystem
);
205 if (status
!= noErr
) {
207 "failed to set system keychain domain.\n");
211 status
= SecKeychainCopyDomainDefault(kSecPreferencesDomainSystem
,
213 if (status
!= noErr
) {
215 "failed to get system keychain domain.\n");
219 if (secrettype
== SECRETTYPE_KEYCHAIN_BY_ID
&& etype
== ISAKMP_ETYPE_AGG
) {
220 /* try looking up based on peers id */
222 char* peer_id
= NULL
;
223 int idlen
= id_p
->l
- sizeof(struct ipsecdoi_id_b
);
224 u_int8_t id_type
= (ALIGNED_CAST(struct ipsecdoi_id_b
*)(id_p
->v
))->type
;
227 case IPSECDOI_ID_IPV4_ADDR
:
228 case IPSECDOI_ID_IPV6_ADDR
:
229 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
230 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
231 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
232 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
233 case IPSECDOI_ID_DER_ASN1_DN
:
234 case IPSECDOI_ID_DER_ASN1_GN
:
238 case IPSECDOI_ID_FQDN
:
239 case IPSECDOI_ID_USER_FQDN
:
240 case IPSECDOI_ID_KEY_ID
:
241 peer_id
= racoon_malloc(1 + idlen
);
244 memcpy(peer_id
, id_p
->v
+ sizeof(struct ipsecdoi_id_b
), idlen
);
245 *(peer_id
+ idlen
) = '\0';
247 "getting shared secret from keychain using %s.\n", peer_id
);
255 status
= SecKeychainFindGenericPassword(keychain
,
264 /* try find it using using only the peer id. */
265 if (status
== errSecItemNotFound
)
266 status
= SecKeychainFindGenericPassword(keychain
,
275 racoon_free(peer_id
);
278 /* otherwise fall through to use the default value */
282 /* use the value in remote config sharedsecret field
283 this is either the value specified for lookup or the
284 default when lookup by id fails.
286 status
= SecKeychainFindGenericPassword(keychain
,
295 /* try find it using using only name. */
296 if (status
== errSecItemNotFound
)
297 status
= SecKeychainFindGenericPassword(keychain
,
312 case errSecItemNotFound
:
317 "failed to get preshared key from system keychain (error %ld).\n", (long)status
);
323 key
= vmalloc(cur_password_len
);
326 "failed to allocate key buffer.\n");
328 memcpy(key
->v
, cur_password
, cur_password_len
);
340 * get PSK by address.
344 struct sockaddr_storage
*remote
;
347 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
349 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key by addr.\n");
351 GETNAMEINFO((struct sockaddr
*)remote
, addr
, port
);
353 key
= getpsk(addr
, strlen(addr
));
364 char buf
[1024]; /* XXX how is variable length ? */
370 plog(ASL_LEVEL_DEBUG
, "Getting pre-shared key from file.\n");
372 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], 1) == 0)
373 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], "r");
378 "failed to open pre_share_key file %s\n",
379 lcconf
->pathinfo
[LC_PATHTYPE_PSK
]);
383 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
388 /* search the end of 1st string. */
389 for (p
= buf
; *p
!= '\0' && !isspace((int)*p
); p
++)
392 continue; /* no 2nd parameter */
394 /* search the 1st of 2nd string. */
395 while (isspace((int)*++p
))
398 continue; /* no 2nd parameter */
400 if (strncmp(buf
, str
, len
) == 0 && buf
[len
] == '\0') {
403 for (q
= p
; *q
!= '\0' && *q
!= '\n'; q
++)
407 /* fix key if hex string */
408 if (strncmp(p
, "0x", 2) == 0) {
409 k
= str2val(p
+ 2, 16, &keylen
);
412 "failed to get psk buffer.\n");
418 key
= vmalloc(keylen
);
421 "failed to allocate key buffer.\n");
424 memcpy(key
->v
, p
, key
->l
);
437 * get a file name of a type specified.
440 getpathname(path
, len
, type
, name
)
445 snprintf(path
, len
, "%s%s%s",
446 name
[0] == '/' ? "" : lcconf
->pathinfo
[type
],
447 name
[0] == '/' ? "" : "/",
450 plog(ASL_LEVEL_DEBUG
, "filename: %s\n", path
);
454 static int lc_doi2idtype
[] = {
458 LC_IDENTTYPE_USERFQDN
,
464 LC_IDENTTYPE_CERTNAME
,
470 * convert DOI value to idtype
478 if (ARRAYLEN(lc_doi2idtype
) > idtype
)
479 return lc_doi2idtype
[idtype
];
484 static int lc_sittype2doi
[] = {
485 IPSECDOI_SIT_IDENTITY_ONLY
,
486 IPSECDOI_SIT_SECRECY
,
487 IPSECDOI_SIT_INTEGRITY
,
491 * convert sittype to DOI value.
499 if (ARRAYLEN(lc_sittype2doi
) > sittype
)
500 return lc_sittype2doi
[sittype
];
504 static int lc_doitype2doi
[] = {
509 * convert doitype to DOI value.
517 if (ARRAYLEN(lc_doitype2doi
) > doitype
)
518 return lc_doitype2doi
[doitype
];