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"
54 #include "isakmp_var.h"
56 #include "ipsec_doi.h"
57 #include "grabmyaddr.h"
66 #include <CoreFoundation/CoreFoundation.h>
67 #include <Security/Security.h>
70 struct localconf
*lcconf
;
72 static void setdefault
__P((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
;
93 for (i
= 0; i
< LC_PATHTYPE_MAX
; i
++) {
94 if (lcconf
->pathinfo
[i
]) {
95 racoon_free(lcconf
->pathinfo
[i
]);
96 lcconf
->pathinfo
[i
] = NULL
;
99 for (i
= 0; i
< LC_IDENTTYPE_MAX
; i
++) {
100 if (lcconf
->ident
[i
])
101 vfree(lcconf
->ident
[i
]);
102 lcconf
->ident
[i
] = NULL
;
111 lcconf
->chroot
= NULL
;
112 lcconf
->autograbaddr
= 1;
113 lcconf
->port_isakmp
= PORT_ISAKMP
;
114 lcconf
->port_isakmp_natt
= PORT_ISAKMP_NATT
;
115 lcconf
->default_af
= AF_INET
;
116 lcconf
->pad_random
= LC_DEFAULT_PAD_RANDOM
;
117 lcconf
->pad_randomlen
= LC_DEFAULT_PAD_RANDOMLEN
;
118 lcconf
->pad_maxsize
= LC_DEFAULT_PAD_MAXSIZE
;
119 lcconf
->pad_strict
= LC_DEFAULT_PAD_STRICT
;
120 lcconf
->pad_excltail
= LC_DEFAULT_PAD_EXCLTAIL
;
121 lcconf
->retry_counter
= LC_DEFAULT_RETRY_COUNTER
;
122 lcconf
->retry_interval
= LC_DEFAULT_RETRY_INTERVAL
;
123 lcconf
->count_persend
= LC_DEFAULT_COUNT_PERSEND
;
124 lcconf
->secret_size
= LC_DEFAULT_SECRETSIZE
;
125 lcconf
->retry_checkph1
= LC_DEFAULT_RETRY_CHECKPH1
;
126 lcconf
->wait_ph2complete
= LC_DEFAULT_WAIT_PH2COMPLETE
;
127 lcconf
->strict_address
= FALSE
;
128 lcconf
->complex_bundle
= TRUE
; /*XXX FALSE;*/
129 lcconf
->gss_id_enc
= LC_GSSENC_UTF16LE
; /* Windows compatibility */
130 lcconf
->natt_ka_interval
= LC_DEFAULT_NATT_KA_INTERVAL
;
131 lcconf
->auto_exit_delay
= 0;
132 lcconf
->auto_exit_state
&= ~LC_AUTOEXITSTATE_SET
;
145 id
= racoon_calloc(1, 1 + id0
->l
- sizeof(struct ipsecdoi_id_b
));
147 plog(LLV_ERROR
, LOCATION
, NULL
,
148 "failed to get psk buffer.\n");
151 memcpy(id
, id0
->v
+ sizeof(struct ipsecdoi_id_b
),
152 id0
->l
- sizeof(struct ipsecdoi_id_b
));
153 id
[id0
->l
- sizeof(struct ipsecdoi_id_b
)] = '\0';
155 key
= privsep_getpsk(id
, id0
->l
- sizeof(struct ipsecdoi_id_b
));
165 * get PSK from keyChain.
168 getpskfromkeychain(const char *name
, u_int8_t etype
, int secrettype
, vchar_t
*id_p
)
170 SecKeychainRef keychain
= NULL
;
172 void *cur_password
= NULL
;
173 UInt32 cur_password_len
= 0;
175 char serviceName
[] = "com.apple.net.racoon";
177 status
= SecKeychainSetPreferenceDomain(kSecPreferencesDomainSystem
);
178 if (status
!= noErr
) {
179 plog(LLV_ERROR
, LOCATION
, NULL
,
180 "failed to set system keychain domain.\n");
184 status
= SecKeychainCopyDomainDefault(kSecPreferencesDomainSystem
,
186 if (status
!= noErr
) {
187 plog(LLV_ERROR
, LOCATION
, NULL
,
188 "failed to get system keychain domain.\n");
192 if (secrettype
== SECRETTYPE_KEYCHAIN_BY_ID
&& etype
== ISAKMP_ETYPE_AGG
) {
193 /* try looking up based on peers id */
196 int idlen
= id_p
->l
- sizeof(struct ipsecdoi_id_b
);
197 u_int8_t id_type
= ((struct ipsecdoi_id_b
*)(id_p
->v
))->type
;
200 case IPSECDOI_ID_IPV4_ADDR
:
201 case IPSECDOI_ID_IPV6_ADDR
:
202 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
203 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
204 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
205 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
206 case IPSECDOI_ID_DER_ASN1_DN
:
207 case IPSECDOI_ID_DER_ASN1_GN
:
211 case IPSECDOI_ID_FQDN
:
212 case IPSECDOI_ID_USER_FQDN
:
213 case IPSECDOI_ID_KEY_ID
:
214 peer_id
= racoon_malloc(idlen
);
217 memcpy(peer_id
, id_p
->v
+ sizeof(struct ipsecdoi_id_b
), idlen
);
218 *(peer_id
+ idlen
) = '\0';
219 plog(LLV_ERROR
, LOCATION
, NULL
,
220 "getting shared secret from keychain using %s.\n", peer_id
);
228 status
= SecKeychainFindGenericPassword(keychain
,
239 /* otherwise fall through to use the default value */
242 /* use the value in remote config sharedsecret field
243 this is either the value specified for lookup or the
244 default when lookup by id fails.
246 status
= SecKeychainFindGenericPassword(keychain
,
255 /* try find it using using only name. */
256 if (status
== errSecItemNotFound
)
257 status
= SecKeychainFindGenericPassword(keychain
,
272 case errSecItemNotFound
:
276 plog(LLV_ERROR
, LOCATION
, NULL
,
277 "failed to get preshared key from system keychain (error %d).\n", status
);
283 key
= vmalloc(cur_password_len
+ 1);
285 plog(LLV_ERROR
, LOCATION
, NULL
,
286 "failed to allocate key buffer.\n");
288 memcpy(key
->v
, cur_password
, key
->l
);
289 key
->v
[cur_password_len
] = 0;
302 * get PSK by address.
306 struct sockaddr
*remote
;
309 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
311 GETNAMEINFO(remote
, addr
, port
);
313 key
= privsep_getpsk(addr
, strlen(addr
));
324 char buf
[1024]; /* XXX how is variable length ? */
330 if (safefile(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], 1) == 0)
331 fp
= fopen(lcconf
->pathinfo
[LC_PATHTYPE_PSK
], "r");
335 plog(LLV_ERROR
, LOCATION
, NULL
,
336 "failed to open pre_share_key file %s\n",
337 lcconf
->pathinfo
[LC_PATHTYPE_PSK
]);
341 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
346 /* search the end of 1st string. */
347 for (p
= buf
; *p
!= '\0' && !isspace((int)*p
); p
++)
350 continue; /* no 2nd parameter */
352 /* search the 1st of 2nd string. */
353 while (isspace((int)*++p
))
356 continue; /* no 2nd parameter */
358 if (strncmp(buf
, str
, len
) == 0 && buf
[len
] == '\0') {
361 for (q
= p
; *q
!= '\0' && *q
!= '\n'; q
++)
365 /* fix key if hex string */
366 if (strncmp(p
, "0x", 2) == 0) {
367 k
= str2val(p
+ 2, 16, &keylen
);
369 plog(LLV_ERROR
, LOCATION
, NULL
,
370 "failed to get psk buffer.\n");
376 key
= vmalloc(keylen
);
378 plog(LLV_ERROR
, LOCATION
, NULL
,
379 "failed to allocate key buffer.\n");
382 memcpy(key
->v
, p
, key
->l
);
395 * get a file name of a type specified.
398 getpathname(path
, len
, type
, name
)
403 snprintf(path
, len
, "%s%s%s",
404 name
[0] == '/' ? "" : lcconf
->pathinfo
[type
],
405 name
[0] == '/' ? "" : "/",
408 plog(LLV_DEBUG
, LOCATION
, NULL
, "filename: %s\n", path
);
412 static int lc_doi2idtype
[] = {
416 LC_IDENTTYPE_USERFQDN
,
422 LC_IDENTTYPE_CERTNAME
,
428 * convert DOI value to idtype
436 if (ARRAYLEN(lc_doi2idtype
) > idtype
)
437 return lc_doi2idtype
[idtype
];
442 static int lc_sittype2doi
[] = {
443 IPSECDOI_SIT_IDENTITY_ONLY
,
444 IPSECDOI_SIT_SECRECY
,
445 IPSECDOI_SIT_INTEGRITY
,
449 * convert sittype to DOI value.
457 if (ARRAYLEN(lc_sittype2doi
) > sittype
)
458 return lc_sittype2doi
[sittype
];
462 static int lc_doitype2doi
[] = {
467 * convert doitype to DOI value.
475 if (ARRAYLEN(lc_doitype2doi
) > doitype
)
476 return lc_doitype2doi
[doitype
];