1 /* $KAME: oakley.c,v 1.115 2003/01/10 08:38:23 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
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h> /* XXX for subjectaltname */
35 #include <netinet/in.h> /* XXX for subjectaltname */
36 #include <arpa/inet.h>
43 #if TIME_WITH_SYS_TIME
44 # include <sys/time.h>
48 # include <sys/time.h>
61 #include "isakmp_var.h"
64 #include "localconf.h"
65 #include "remoteconf.h"
68 #include "ipsec_doi.h"
69 #include "algorithm.h"
73 #include "crypto_openssl.h"
74 #include "crypto_cssm.h"
80 #ifndef HAVE_ARC4RANDOM
81 #include "arc4random.h"
91 #define INITDHVAL(a, s, d, t) \
94 buf.v = str2val((s), 16, &buf.l); \
95 memset(&a, 0, sizeof(struct dhgroup)); \
97 a.prime = vdup(&buf); \
103 struct dhgroup dh_modp768
;
104 struct dhgroup dh_modp1024
;
105 struct dhgroup dh_modp1536
;
106 struct dhgroup dh_modp2048
;
107 struct dhgroup dh_modp3072
;
108 struct dhgroup dh_modp4096
;
109 struct dhgroup dh_modp6144
;
110 struct dhgroup dh_modp8192
;
112 static int oakley_compute_keymat_x
__P((struct ph2handle
*, int, int));
113 #ifdef HAVE_SIGNING_C
114 static int get_cert_fromlocal
__P((struct ph1handle
*, int));
115 static int oakley_check_certid(u_int8_t idtype
, int idlen
, void* id
, cert_t
* cert_p
);
116 static int check_typeofcertname
__P((int, int));
117 static cert_t
*save_certbuf
__P((struct isakmp_gen
*));
119 static int oakley_padlen
__P((int, int));
122 oakley_get_defaultlifetime()
124 return OAKLEY_ATTR_SA_LD_SEC_DEFAULT
;
131 INITDHVAL(dh_modp768
, OAKLEY_PRIME_MODP768
,
132 OAKLEY_ATTR_GRP_DESC_MODP768
, OAKLEY_ATTR_GRP_TYPE_MODP
);
133 INITDHVAL(dh_modp1024
, OAKLEY_PRIME_MODP1024
,
134 OAKLEY_ATTR_GRP_DESC_MODP1024
, OAKLEY_ATTR_GRP_TYPE_MODP
);
135 INITDHVAL(dh_modp1536
, OAKLEY_PRIME_MODP1536
,
136 OAKLEY_ATTR_GRP_DESC_MODP1536
, OAKLEY_ATTR_GRP_TYPE_MODP
);
137 INITDHVAL(dh_modp2048
, OAKLEY_PRIME_MODP2048
,
138 OAKLEY_ATTR_GRP_DESC_MODP2048
, OAKLEY_ATTR_GRP_TYPE_MODP
);
139 INITDHVAL(dh_modp3072
, OAKLEY_PRIME_MODP3072
,
140 OAKLEY_ATTR_GRP_DESC_MODP3072
, OAKLEY_ATTR_GRP_TYPE_MODP
);
141 INITDHVAL(dh_modp4096
, OAKLEY_PRIME_MODP4096
,
142 OAKLEY_ATTR_GRP_DESC_MODP4096
, OAKLEY_ATTR_GRP_TYPE_MODP
);
143 INITDHVAL(dh_modp6144
, OAKLEY_PRIME_MODP6144
,
144 OAKLEY_ATTR_GRP_DESC_MODP6144
, OAKLEY_ATTR_GRP_TYPE_MODP
);
145 INITDHVAL(dh_modp8192
, OAKLEY_PRIME_MODP8192
,
146 OAKLEY_ATTR_GRP_DESC_MODP8192
, OAKLEY_ATTR_GRP_TYPE_MODP
);
152 oakley_dhgrp_free(dhgrp
)
153 struct dhgroup
*dhgrp
;
158 vfree(dhgrp
->curve_a
);
160 vfree(dhgrp
->curve_b
);
167 * compute sharing secret of DH
168 * IN: *dh, *pub, *priv, *pub_p
172 oakley_dh_compute(dh
, pub
, priv
, pub_p
, gxy
)
173 const struct dhgroup
*dh
;
174 vchar_t
*pub
, *priv
, *pub_p
, **gxy
;
177 struct timeval start
, end
;
179 if ((*gxy
= vmalloc(dh
->prime
->l
)) == NULL
) {
180 plog(LLV_ERROR
, LOCATION
, NULL
,
181 "failed to get DH buffer.\n");
186 gettimeofday(&start
, NULL
);
189 case OAKLEY_ATTR_GRP_TYPE_MODP
:
190 if (eay_dh_compute(dh
->prime
, dh
->gen1
, pub
, priv
, pub_p
, gxy
) < 0) {
191 plog(LLV_ERROR
, LOCATION
, NULL
,
192 "failed to compute dh value.\n");
196 case OAKLEY_ATTR_GRP_TYPE_ECP
:
197 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
198 plog(LLV_ERROR
, LOCATION
, NULL
,
199 "dh type %d isn't supported.\n", dh
->type
);
202 plog(LLV_ERROR
, LOCATION
, NULL
,
203 "invalid dh type %d.\n", dh
->type
);
208 gettimeofday(&end
, NULL
);
209 syslog(LOG_NOTICE
, "%s(%s%d): %8.6f", __func__
,
210 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
211 timedelta(&start
, &end
));
214 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute DH's shared.\n");
215 plogdump(LLV_DEBUG
, (*gxy
)->v
, (*gxy
)->l
);
221 * generate values of DH
226 oakley_dh_generate(dh
, pub
, priv
)
227 const struct dhgroup
*dh
;
228 vchar_t
**pub
, **priv
;
231 struct timeval start
, end
;
232 gettimeofday(&start
, NULL
);
235 case OAKLEY_ATTR_GRP_TYPE_MODP
:
236 if (eay_dh_generate(dh
->prime
, dh
->gen1
, dh
->gen2
, pub
, priv
) < 0) {
237 plog(LLV_ERROR
, LOCATION
, NULL
,
238 "failed to compute dh value.\n");
243 case OAKLEY_ATTR_GRP_TYPE_ECP
:
244 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
245 plog(LLV_ERROR
, LOCATION
, NULL
,
246 "dh type %d isn't supported.\n", dh
->type
);
249 plog(LLV_ERROR
, LOCATION
, NULL
,
250 "invalid dh type %d.\n", dh
->type
);
255 gettimeofday(&end
, NULL
);
256 syslog(LOG_NOTICE
, "%s(%s%d): %8.6f", __func__
,
257 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
258 timedelta(&start
, &end
));
260 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute DH's private.\n");
261 plogdump(LLV_DEBUG
, (*priv
)->v
, (*priv
)->l
);
262 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute DH's public.\n");
263 plogdump(LLV_DEBUG
, (*pub
)->v
, (*pub
)->l
);
269 * copy pre-defined dhgroup values.
272 oakley_setdhgroup(group
, dhgrp
)
274 struct dhgroup
**dhgrp
;
278 *dhgrp
= NULL
; /* just make sure, initialize */
280 g
= alg_oakley_dhdef_group(group
);
282 plog(LLV_ERROR
, LOCATION
, NULL
,
283 "invalid DH parameter grp=%d.\n", group
);
287 if (!g
->type
|| !g
->prime
|| !g
->gen1
) {
289 plog(LLV_ERROR
, LOCATION
, NULL
,
290 "unsupported DH parameters grp=%d.\n", group
);
294 *dhgrp
= racoon_calloc(1, sizeof(struct dhgroup
));
295 if (*dhgrp
== NULL
) {
296 plog(LLV_ERROR
, LOCATION
, NULL
,
297 "failed to get DH buffer.\n");
301 /* set defined dh vlaues */
302 memcpy(*dhgrp
, g
, sizeof(*g
));
303 (*dhgrp
)->prime
= vdup(g
->prime
);
311 * NOTE: we do not support prf with different input/output bitwidth,
312 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example) in
313 * oakley_compute_keymat(). If you add support for such prf function,
314 * modify oakley_compute_keymat() accordingly.
317 oakley_prf(key
, buf
, iph1
)
319 struct ph1handle
*iph1
;
324 if (iph1
->approval
== NULL
) {
326 * it's before negotiating hash algorithm.
327 * We use md5 as default.
329 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
331 type
= iph1
->approval
->hashtype
;
333 res
= alg_oakley_hmacdef_one(type
, key
, buf
);
335 plog(LLV_ERROR
, LOCATION
, NULL
,
336 "invalid hmac algorithm %d.\n", type
);
347 oakley_hash(buf
, iph1
)
349 struct ph1handle
*iph1
;
354 if (iph1
->approval
== NULL
) {
356 * it's before negotiating hash algorithm.
357 * We use md5 as default.
359 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
361 type
= iph1
->approval
->hashtype
;
363 res
= alg_oakley_hashdef_one(type
, buf
);
365 plog(LLV_ERROR
, LOCATION
, NULL
,
366 "invalid hash algorithm %d.\n", type
);
375 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
378 oakley_compute_keymat(iph2
, side
)
379 struct ph2handle
*iph2
;
384 /* compute sharing secret of DH when PFS */
385 if (iph2
->approval
->pfs_group
&& iph2
->dhpub_p
) {
386 if (oakley_dh_compute(iph2
->pfsgrp
, iph2
->dhpub
,
387 iph2
->dhpriv
, iph2
->dhpub_p
, &iph2
->dhgxy
) < 0)
392 if (oakley_compute_keymat_x(iph2
, side
, INBOUND_SA
) < 0
393 || oakley_compute_keymat_x(iph2
, side
, OUTBOUND_SA
) < 0)
396 plog(LLV_DEBUG
, LOCATION
, NULL
, "KEYMAT computed.\n");
406 * KEYMAT = prf(SKEYID_d, protocol | SPI | Ni_b | Nr_b).
407 * If PFS is desired and KE payloads were exchanged,
408 * KEYMAT = prf(SKEYID_d, g(qm)^xy | protocol | SPI | Ni_b | Nr_b)
410 * NOTE: we do not support prf with different input/output bitwidth,
411 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example).
414 oakley_compute_keymat_x(iph2
, side
, sa_dir
)
415 struct ph2handle
*iph2
;
419 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
424 int dupkeymat
; /* generate K[1-dupkeymat] */
427 int encklen
, authklen
, l
;
429 pfs
= ((iph2
->approval
->pfs_group
&& iph2
->dhgxy
) ? 1 : 0);
431 len
= pfs
? iph2
->dhgxy
->l
: 0;
433 + sizeof(u_int32_t
) /* XXX SPI size */
438 plog(LLV_ERROR
, LOCATION
, NULL
,
439 "failed to get keymat buffer.\n");
443 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
448 memcpy(p
, iph2
->dhgxy
->v
, iph2
->dhgxy
->l
);
455 memcpy(p
, (sa_dir
== INBOUND_SA
? &pr
->spi
: &pr
->spi_p
),
457 p
+= sizeof(pr
->spi
);
459 bp
= (side
== INITIATOR
? iph2
->nonce
: iph2
->nonce_p
);
460 memcpy(p
, bp
->v
, bp
->l
);
463 bp
= (side
== INITIATOR
? iph2
->nonce_p
: iph2
->nonce
);
464 memcpy(p
, bp
->v
, bp
->l
);
468 plog(LLV_DEBUG
, LOCATION
, NULL
, "KEYMAT compute with\n");
469 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
472 res
= oakley_prf(iph2
->ph1
->skeyid_d
, buf
, iph2
->ph1
);
476 /* compute key length needed */
477 encklen
= authklen
= 0;
478 switch (pr
->proto_id
) {
479 case IPSECDOI_PROTO_IPSEC_ESP
:
480 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
481 l
= alg_ipsec_encdef_keylen(tr
->trns_id
,
486 l
= alg_ipsec_hmacdef_hashlen(tr
->authtype
);
491 case IPSECDOI_PROTO_IPSEC_AH
:
492 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
493 l
= alg_ipsec_hmacdef_hashlen(tr
->trns_id
);
501 plog(LLV_DEBUG
, LOCATION
, NULL
, "encklen=%d authklen=%d\n",
504 dupkeymat
= (encklen
+ authklen
) / 8 / res
->l
;
505 dupkeymat
+= 2; /* safety mergin */
508 plog(LLV_DEBUG
, LOCATION
, NULL
,
509 "generating %d bits of key (dupkeymat=%d)\n",
510 dupkeymat
* 8 * res
->l
, dupkeymat
);
511 if (0 < --dupkeymat
) {
512 vchar_t
*prev
= res
; /* K(n-1) */
513 vchar_t
*seed
= NULL
; /* seed for Kn */
517 * generating long key (isakmp-oakley-08 5.5)
518 * KEYMAT = K1 | K2 | K3 | ...
520 * src = [ g(qm)^xy | ] protocol | SPI | Ni_b | Nr_b
521 * K1 = prf(SKEYID_d, src)
522 * K2 = prf(SKEYID_d, K1 | src)
523 * K3 = prf(SKEYID_d, K2 | src)
524 * Kn = prf(SKEYID_d, K(n-1) | src)
526 plog(LLV_DEBUG
, LOCATION
, NULL
,
527 "generating K1...K%d for KEYMAT.\n",
530 seed
= vmalloc(prev
->l
+ buf
->l
);
532 plog(LLV_ERROR
, LOCATION
, NULL
,
533 "failed to get keymat buffer.\n");
534 if (prev
&& prev
!= res
)
539 while (dupkeymat
--) {
540 vchar_t
*this = NULL
; /* Kn */
542 memcpy(seed
->v
, prev
->v
, prev
->l
);
543 memcpy(seed
->v
+ prev
->l
, buf
->v
, buf
->l
);
544 this = oakley_prf(iph2
->ph1
->skeyid_d
, seed
,
547 plog(LLV_ERROR
, LOCATION
, NULL
,
548 "oakley_prf memory overflow\n");
549 if (prev
&& prev
!= res
)
557 res
= vrealloc(res
, l
+ this->l
);
559 plog(LLV_ERROR
, LOCATION
, NULL
,
560 "failed to get keymat buffer.\n");
561 if (prev
&& prev
!= res
)
567 memcpy(res
->v
+ l
, this->v
, this->l
);
569 if (prev
&& prev
!= res
)
575 if (prev
&& prev
!= res
)
580 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
582 if (sa_dir
== INBOUND_SA
)
593 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
615 * NOTE: Must terminate by NULL.
618 oakley_compute_hashx(struct ph1handle
*iph1
, ...)
627 /* get buffer length */
630 while ((s
= va_arg(ap
, vchar_t
*)) != NULL
) {
637 plog(LLV_ERROR
, LOCATION
, NULL
,
638 "failed to get hash buffer\n");
645 while ((s
= va_arg(ap
, char *)) != NULL
) {
646 memcpy(p
, s
->v
, s
->l
);
651 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with: \n");
652 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
655 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
660 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
661 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
668 * compute HASH(3) prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b)
669 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
672 oakley_compute_hash3(iph1
, msgid
, body
)
673 struct ph1handle
*iph1
;
677 vchar_t
*buf
= 0, *res
= 0;
682 len
= 1 + sizeof(u_int32_t
) + body
->l
;
685 plog(LLV_DEBUG
, LOCATION
, NULL
,
686 "failed to get hash buffer\n");
692 memcpy(buf
->v
+ 1, (char *)&msgid
, sizeof(msgid
));
694 memcpy(buf
->v
+ 1 + sizeof(u_int32_t
), body
->v
, body
->l
);
696 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with: \n");
697 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
700 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
706 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
707 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
716 * compute HASH type of prf(SKEYID_a, M-ID | buffer)
718 * for quick mode HASH(1):
719 * prf(SKEYID_a, M-ID | SA | Ni [ | KE ] [ | IDci | IDcr ])
720 * for quick mode HASH(2):
721 * prf(SKEYID_a, M-ID | Ni_b | SA | Nr [ | KE ] [ | IDci | IDcr ])
722 * for Informational exchange:
723 * prf(SKEYID_a, M-ID | N/D)
726 oakley_compute_hash1(iph1
, msgid
, body
)
727 struct ph1handle
*iph1
;
731 vchar_t
*buf
= NULL
, *res
= NULL
;
737 len
= sizeof(u_int32_t
) + body
->l
;
740 plog(LLV_DEBUG
, LOCATION
, NULL
,
741 "failed to get hash buffer\n");
747 memcpy(buf
->v
, (char *)&msgid
, sizeof(msgid
));
748 p
+= sizeof(u_int32_t
);
750 memcpy(p
, body
->v
, body
->l
);
752 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with:\n");
753 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
756 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
762 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
763 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
772 * compute phase1 HASH
774 * I-digest = prf(SKEYID, g^i | g^r | CKY-I | CKY-R | SAi_b | ID_i1_b)
775 * R-digest = prf(SKEYID, g^r | g^i | CKY-R | CKY-I | SAi_b | ID_r1_b)
776 * for gssapi, also include all GSS tokens, and call gss_wrap on the result
779 oakley_ph1hash_common(iph1
, sw
)
780 struct ph1handle
*iph1
;
783 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
788 vchar_t
*gsstokens
= NULL
;
794 + sizeof(cookie_t
) * 2
796 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
799 if (iph1
->approval
->authmethod
== OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
800 if (iph1
->gi_i
!= NULL
&& iph1
->gi_r
!= NULL
) {
801 bp
= (sw
== GENERATE
? iph1
->gi_i
: iph1
->gi_r
);
805 gssapi_get_itokens(iph1
, &gsstokens
);
807 gssapi_get_rtokens(iph1
, &gsstokens
);
808 if (gsstokens
== NULL
)
816 plog(LLV_ERROR
, LOCATION
, NULL
,
817 "failed to get hash buffer\n");
823 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
824 memcpy(p
, bp
->v
, bp
->l
);
827 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
828 memcpy(p
, bp
->v
, bp
->l
);
831 if (iph1
->side
== INITIATOR
)
832 bp2
= (sw
== GENERATE
?
833 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
835 bp2
= (sw
== GENERATE
?
836 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
837 bl
= sizeof(cookie_t
);
841 if (iph1
->side
== INITIATOR
)
842 bp2
= (sw
== GENERATE
?
843 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
845 bp2
= (sw
== GENERATE
?
846 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
847 bl
= sizeof(cookie_t
);
852 memcpy(p
, bp
->v
, bp
->l
);
855 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
856 memcpy(p
, bp
->v
, bp
->l
);
860 if (iph1
->approval
->authmethod
== OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
861 if (iph1
->gi_i
!= NULL
&& iph1
->gi_r
!= NULL
) {
862 bp
= (sw
== GENERATE
? iph1
->gi_i
: iph1
->gi_r
);
863 memcpy(p
, bp
->v
, bp
->l
);
866 memcpy(p
, gsstokens
->v
, gsstokens
->l
);
871 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with:\n");
872 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
875 res
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
881 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
882 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
888 if (gsstokens
!= NULL
)
895 * compute HASH_I on base mode.
897 * HASH_I = prf(SKEYID, g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
899 * HASH_I = prf(hash(Ni_b | Nr_b), g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
902 oakley_ph1hash_base_i(iph1
, sw
)
903 struct ph1handle
*iph1
;
906 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
907 vchar_t
*hashkey
= NULL
;
908 vchar_t
*hash
= NULL
; /* for signature mode */
914 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
915 plog(LLV_ERROR
, LOCATION
, NULL
,
916 "invalid etype for this hash function\n");
920 switch (iph1
->approval
->authmethod
) {
921 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
922 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
923 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
924 if (iph1
->skeyid
== NULL
) {
925 plog(LLV_ERROR
, LOCATION
, NULL
, "no SKEYID found.\n");
928 hashkey
= iph1
->skeyid
;
931 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
932 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
933 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
934 /* make hash for seed */
935 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
938 plog(LLV_ERROR
, LOCATION
, NULL
,
939 "failed to get hash buffer\n");
944 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
945 memcpy(p
, bp
->v
, bp
->l
);
948 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
949 memcpy(p
, bp
->v
, bp
->l
);
952 hash
= oakley_hash(buf
, iph1
);
962 plog(LLV_ERROR
, LOCATION
, NULL
,
963 "not supported authentication method %d\n",
964 iph1
->approval
->authmethod
);
969 len
= (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
970 + sizeof(cookie_t
) * 2
972 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
975 plog(LLV_ERROR
, LOCATION
, NULL
,
976 "failed to get hash buffer\n");
981 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
982 memcpy(p
, bp
->v
, bp
->l
);
985 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
986 p
+= sizeof(cookie_t
);
987 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
988 p
+= sizeof(cookie_t
);
990 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
993 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
994 memcpy(p
, bp
->v
, bp
->l
);
997 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH_I with:\n");
998 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
1001 res
= oakley_prf(hashkey
, buf
, iph1
);
1007 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH_I computed:\n");
1008 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
1019 * compute HASH_R on base mode for signature method.
1021 * HASH_R = prf(hash(Ni_b | Nr_b), g^xi | g^xr | CKY-I | CKY-R | SAi_b | IDii_b)
1024 oakley_ph1hash_base_r(iph1
, sw
)
1025 struct ph1handle
*iph1
;
1028 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
1029 vchar_t
*hash
= NULL
;
1035 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
1036 plog(LLV_ERROR
, LOCATION
, NULL
,
1037 "invalid etype for this hash function\n");
1040 if (iph1
->approval
->authmethod
!= OAKLEY_ATTR_AUTH_METHOD_DSSSIG
1041 && iph1
->approval
->authmethod
!= OAKLEY_ATTR_AUTH_METHOD_RSASIG
) {
1042 plog(LLV_ERROR
, LOCATION
, NULL
,
1043 "not supported authentication method %d\n",
1044 iph1
->approval
->authmethod
);
1048 /* make hash for seed */
1049 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
1052 plog(LLV_ERROR
, LOCATION
, NULL
,
1053 "failed to get hash buffer\n");
1058 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
1059 memcpy(p
, bp
->v
, bp
->l
);
1062 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
1063 memcpy(p
, bp
->v
, bp
->l
);
1066 hash
= oakley_hash(buf
, iph1
);
1072 /* make really hash */
1073 len
= (sw
== GENERATE
? iph1
->dhpub_p
->l
: iph1
->dhpub
->l
)
1074 + (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
1075 + sizeof(cookie_t
) * 2
1077 + (sw
== GENERATE
? iph1
->id_p
->l
: iph1
->id
->l
);
1080 plog(LLV_ERROR
, LOCATION
, NULL
,
1081 "failed to get hash buffer\n");
1087 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
1088 memcpy(p
, bp
->v
, bp
->l
);
1091 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
1092 memcpy(p
, bp
->v
, bp
->l
);
1095 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1096 p
+= sizeof(cookie_t
);
1097 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1098 p
+= sizeof(cookie_t
);
1100 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
1103 bp
= (sw
== GENERATE
? iph1
->id_p
: iph1
->id
);
1104 memcpy(p
, bp
->v
, bp
->l
);
1107 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with:\n");
1108 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
1111 res
= oakley_prf(hash
, buf
, iph1
);
1117 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
1118 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
1129 * compute each authentication method in phase 1.
1133 * other: error to be reply with notification.
1134 * the value is notification type.
1137 oakley_validate_auth(iph1
)
1138 struct ph1handle
*iph1
;
1140 vchar_t
*my_hash
= NULL
;
1143 vchar_t
*gsshash
= NULL
;
1146 struct timeval start
, end
;
1150 gettimeofday(&start
, NULL
);
1152 switch (iph1
->approval
->authmethod
) {
1153 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1158 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1159 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1160 "few isakmp message received.\n");
1161 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1164 r_hash
= (caddr_t
)(iph1
->pl_hash
+ 1);
1166 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH received:");
1167 plogdump(LLV_DEBUG
, r_hash
,
1168 ntohs(iph1
->pl_hash
->h
.len
) - sizeof(*iph1
->pl_hash
));
1170 switch (iph1
->etype
) {
1171 case ISAKMP_ETYPE_IDENT
:
1172 case ISAKMP_ETYPE_AGG
:
1173 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1175 case ISAKMP_ETYPE_BASE
:
1176 if (iph1
->side
== INITIATOR
)
1177 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1179 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1182 plog(LLV_ERROR
, LOCATION
, NULL
,
1183 "invalid etype %d\n", iph1
->etype
);
1184 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1186 if (my_hash
== NULL
)
1187 return ISAKMP_INTERNAL_ERROR
;
1189 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1193 plog(LLV_ERROR
, LOCATION
, NULL
, "HASH mismatched\n");
1194 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1197 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH for PSK validated.\n");
1200 #ifdef HAVE_SIGNING_C
1201 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1202 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1207 if (iph1
->id_p
== NULL
) {
1208 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1209 "no ID payload was passed.\n");
1210 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1212 if (iph1
->sig_p
== NULL
) {
1213 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1214 "no SIG payload was passed.\n");
1215 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1218 plog(LLV_DEBUG
, LOCATION
, NULL
, "SIGN passed:\n");
1219 plogdump(LLV_DEBUG
, iph1
->sig_p
->v
, iph1
->sig_p
->l
);
1221 /* get peer's cert */
1222 switch (iph1
->rmconf
->getcert_method
) {
1223 case ISAKMP_GETCERT_PAYLOAD
:
1224 if (iph1
->cert_p
== NULL
) {
1225 plog(LLV_ERROR
, LOCATION
, NULL
,
1226 "no peer's CERT payload found.\n");
1227 return ISAKMP_INTERNAL_ERROR
;
1230 case ISAKMP_GETCERT_LOCALFILE
:
1231 if (iph1
->rmconf
->peerscertfile
== NULL
) {
1232 plog(LLV_ERROR
, LOCATION
, NULL
,
1233 "no peer's CERT file found.\n");
1234 return ISAKMP_INTERNAL_ERROR
;
1237 /* don't use cached cert */
1238 if (iph1
->cert_p
!= NULL
) {
1239 oakley_delcert(iph1
->cert_p
);
1240 iph1
->cert_p
= NULL
;
1243 error
= get_cert_fromlocal(iph1
, 0);
1245 return ISAKMP_INTERNAL_ERROR
;
1247 case ISAKMP_GETCERT_DNS
:
1248 if (iph1
->rmconf
->peerscertfile
!= NULL
) {
1249 plog(LLV_ERROR
, LOCATION
, NULL
,
1250 "why peer's CERT file is defined "
1251 "though getcert method is dns ?\n");
1252 return ISAKMP_INTERNAL_ERROR
;
1255 /* don't use cached cert */
1256 if (iph1
->cert_p
!= NULL
) {
1257 oakley_delcert(iph1
->cert_p
);
1258 iph1
->cert_p
= NULL
;
1261 iph1
->cert_p
= dnssec_getcert(iph1
->id_p
);
1262 if (iph1
->cert_p
== NULL
) {
1263 plog(LLV_ERROR
, LOCATION
, NULL
,
1264 "no CERT RR found.\n");
1265 return ISAKMP_INTERNAL_ERROR
;
1269 plog(LLV_ERROR
, LOCATION
, NULL
,
1270 "invalid getcert_mothod: %d\n",
1271 iph1
->rmconf
->getcert_method
);
1272 return ISAKMP_INTERNAL_ERROR
;
1276 if (iph1
->rmconf
->verify_cert
) {
1278 struct ipsecdoi_id_b
*id_b
;
1281 if (iph1
->id_p
== NULL
|| iph1
->cert_p
== NULL
) {
1282 plog(LLV_ERROR
, LOCATION
, NULL
, "no ID or CERT found.\n");
1283 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1286 id_b
= (struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
1287 idlen
= iph1
->id_p
->l
- sizeof(*id_b
);
1289 if ((error
= oakley_check_certid(id_b
->type
, idlen
, id_b
+ 1, iph1
->cert_p
)) != 0)
1293 /* verify certificate */
1294 if (iph1
->rmconf
->verify_cert
1295 && iph1
->rmconf
->getcert_method
== ISAKMP_GETCERT_PAYLOAD
) {
1296 switch (iph1
->rmconf
->certtype
) {
1297 case ISAKMP_CERT_X509SIGN
:
1298 if (iph1
->rmconf
->cert_verification
== VERIFICATION_MODULE_SEC_FRAMEWORK
)
1299 error
= crypto_cssm_check_x509cert(&iph1
->cert_p
->cert
);
1301 error
= eay_check_x509cert(&iph1
->cert_p
->cert
,
1302 lcconf
->pathinfo
[LC_PATHTYPE_CERT
], 0);
1305 plog(LLV_ERROR
, LOCATION
, NULL
,
1306 "no supported certtype %d\n",
1307 iph1
->rmconf
->certtype
);
1308 return ISAKMP_INTERNAL_ERROR
;
1311 plog(LLV_ERROR
, LOCATION
, NULL
,
1312 "the peer's certificate is not verified.\n");
1313 return ISAKMP_NTYPE_INVALID_CERT_AUTHORITY
;
1318 /* check configured peers identifier against cert IDs */
1319 /* allows checking of specified ID against multiple ids in the cert */
1320 /* such as multiple domain names */
1321 if (iph1
->rmconf
->cert_verification_option
== VERIFICATION_OPTION_PEERS_IDENTIFIER
) {
1322 u_int8_t doi_type
= 255;
1323 void *peers_id
= NULL
;
1324 int peers_id_len
= 0;
1326 if (iph1
->rmconf
->idvtype_p
== IDTYPE_ADDRESS
) {
1327 switch (((struct sockaddr
*)(iph1
->rmconf
->idv_p
->v
))->sa_family
) {
1329 doi_type
= IPSECDOI_ID_IPV4_ADDR
;
1330 peers_id_len
= sizeof(struct in_addr
);
1331 peers_id
= &(((struct sockaddr_in
*)(iph1
->rmconf
->idv_p
->v
))->sin_addr
.s_addr
);
1335 doi_type
= IPSECDOI_ID_IPV6_ADDR
;
1336 peers_id_len
= sizeof(struct in6_addr
);
1337 peers_id
= &(((struct sockaddr_in6
*)(iph1
->rmconf
->idv_p
->v
))->sin6_addr
.s6_addr
);
1341 plog(LLV_ERROR
, LOCATION
, NULL
,
1342 "unknown address type for peers identifier.\n");
1343 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1348 doi_type
= idtype2doi(iph1
->rmconf
->idvtype_p
);
1349 peers_id
= iph1
->rmconf
->idv_p
->v
;
1350 peers_id_len
= iph1
->rmconf
->idv_p
->l
;
1353 if ((error
= oakley_check_certid(doi_type
, peers_id_len
,
1354 peers_id
, iph1
->cert_p
)) != 0)
1358 if (iph1
->rmconf
->cert_verification_option
== VERIFICATION_OPTION_OPEN_DIR
) {
1360 vchar_t
*user_id
= NULL
;
1362 user_id
= eay_get_x509_common_name(&iph1
->cert_p
->cert
);
1364 // the following functions will check if user_id == 0
1365 if (open_dir_authorize_id(user_id
, iph1
->rmconf
->open_dir_auth_group
) == 0) {
1366 plog(LLV_ERROR
, LOCATION
, NULL
,
1367 "the peer is not authorized for access.\n");
1368 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1373 plog(LLV_DEBUG
, LOCATION
, NULL
, "CERT validated\n");
1377 switch (iph1
->etype
) {
1378 case ISAKMP_ETYPE_IDENT
:
1379 case ISAKMP_ETYPE_AGG
:
1380 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1382 case ISAKMP_ETYPE_BASE
:
1383 if (iph1
->side
== INITIATOR
)
1384 my_hash
= oakley_ph1hash_base_r(iph1
, VALIDATE
);
1386 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1389 plog(LLV_ERROR
, LOCATION
, NULL
,
1390 "invalid etype %d\n", iph1
->etype
);
1391 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1393 if (my_hash
== NULL
)
1394 return ISAKMP_INTERNAL_ERROR
;
1396 /* check signature */
1397 switch (iph1
->rmconf
->certtype
) {
1398 case ISAKMP_CERT_X509SIGN
:
1399 case ISAKMP_CERT_DNS
:
1400 error
= eay_check_x509sign(my_hash
,
1402 &iph1
->cert_p
->cert
);
1405 plog(LLV_ERROR
, LOCATION
, NULL
,
1406 "no supported certtype %d\n",
1407 iph1
->rmconf
->certtype
);
1409 return ISAKMP_INTERNAL_ERROR
;
1414 plog(LLV_ERROR
, LOCATION
, NULL
,
1416 return ISAKMP_NTYPE_INVALID_SIGNATURE
;
1418 plog(LLV_DEBUG
, LOCATION
, NULL
, "SIG authenticated\n");
1423 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1424 switch (iph1
->etype
) {
1425 case ISAKMP_ETYPE_IDENT
:
1426 case ISAKMP_ETYPE_AGG
:
1427 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1430 plog(LLV_ERROR
, LOCATION
, NULL
,
1431 "invalid etype %d\n", iph1
->etype
);
1432 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1435 if (my_hash
== NULL
) {
1436 if (gssapi_more_tokens(iph1
))
1437 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1439 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1442 gsshash
= gssapi_unwraphash(iph1
);
1443 if (gsshash
== NULL
) {
1445 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1448 result
= memcmp(my_hash
->v
, gsshash
->v
, my_hash
->l
);
1453 plog(LLV_ERROR
, LOCATION
, NULL
, "HASH mismatched\n");
1454 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1456 plog(LLV_DEBUG
, LOCATION
, NULL
, "hash compared OK\n");
1459 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1460 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1461 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1462 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1463 "few isakmp message received.\n");
1464 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1466 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1467 "not supported authmethod type %s\n",
1468 s_oakley_attr_method(iph1
->approval
->authmethod
));
1469 return ISAKMP_INTERNAL_ERROR
;
1471 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1472 "invalid authmethod %d why ?\n",
1473 iph1
->approval
->authmethod
);
1474 return ISAKMP_INTERNAL_ERROR
;
1477 gettimeofday(&end
, NULL
);
1478 syslog(LOG_NOTICE
, "%s(%s): %8.6f", __func__
,
1479 s_oakley_attr_method(iph1
->approval
->authmethod
),
1480 timedelta(&start
, &end
));
1486 #ifdef HAVE_SIGNING_C
1487 /* get my certificate
1488 * NOTE: include certificate type.
1491 oakley_getmycert(iph1
)
1492 struct ph1handle
*iph1
;
1495 return 0; /* There is CERT. */
1497 return get_cert_fromlocal(iph1
, 1);
1501 * get a CERT from local file.
1504 * my == 0 peer's cert.
1507 get_cert_fromlocal(iph1
, my
)
1508 struct ph1handle
*iph1
;
1511 char path
[MAXPATHLEN
];
1512 vchar_t
*cert
= NULL
;
1518 certfile
= iph1
->rmconf
->mycertfile
;
1519 certpl
= &iph1
->cert
;
1521 certfile
= iph1
->rmconf
->peerscertfile
;
1522 certpl
= &iph1
->cert_p
;
1524 if (!certfile
&& iph1
->rmconf
->identity_in_keychain
== 0) {
1525 plog(LLV_ERROR
, LOCATION
, NULL
, "no CERT defined.\n");
1529 switch (iph1
->rmconf
->certtype
) {
1530 case ISAKMP_CERT_X509SIGN
:
1531 if (iph1
->rmconf
->identity_in_keychain
) {
1532 cert
= crypto_cssm_get_x509cert(iph1
->rmconf
->keychainCertRef
);
1535 case ISAKMP_CERT_DNS
:
1536 /* make public file name */
1537 getpathname(path
, sizeof(path
), LC_PATHTYPE_CERT
, certfile
);
1538 cert
= eay_get_x509cert(path
);
1542 plog(LLV_ERROR
, LOCATION
, NULL
,
1543 "not supported certtype %d\n",
1544 iph1
->rmconf
->certtype
);
1550 p
= eay_get_x509text(cert
);
1551 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s", p
? p
: "\n");
1554 plog(LLV_ERROR
, LOCATION
, NULL
,
1555 "failed to get %s CERT.\n",
1556 my
? "my" : "peers");
1560 *certpl
= oakley_newcert();
1562 plog(LLV_ERROR
, LOCATION
, NULL
,
1563 "failed to get cert buffer.\n");
1566 (*certpl
)->pl
= vmalloc(cert
->l
+ 1);
1567 if ((*certpl
)->pl
== NULL
) {
1568 plog(LLV_ERROR
, LOCATION
, NULL
,
1569 "failed to get cert buffer\n");
1570 oakley_delcert(*certpl
);
1574 memcpy((*certpl
)->pl
->v
+ 1, cert
->v
, cert
->l
);
1575 (*certpl
)->pl
->v
[0] = iph1
->rmconf
->certtype
;
1576 (*certpl
)->type
= iph1
->rmconf
->certtype
;
1577 (*certpl
)->cert
.v
= (*certpl
)->pl
->v
+ 1;
1578 (*certpl
)->cert
.l
= (*certpl
)->pl
->l
- 1;
1580 plog(LLV_DEBUG
, LOCATION
, NULL
, "created CERT payload:\n");
1581 plogdump(LLV_DEBUG
, (*certpl
)->pl
->v
, (*certpl
)->pl
->l
);
1594 oakley_getsign(iph1
)
1595 struct ph1handle
*iph1
;
1597 char path
[MAXPATHLEN
];
1598 vchar_t
*privkey
= NULL
;
1601 switch (iph1
->rmconf
->certtype
) {
1602 case ISAKMP_CERT_X509SIGN
:
1603 // cert in keychain - use cssm to sign
1604 if (iph1
->rmconf
->identity_in_keychain
) {
1605 iph1
->sig
= crypto_cssm_getsign(iph1
->rmconf
->keychainCertRef
, iph1
->hash
);
1608 case ISAKMP_CERT_DNS
:
1609 if (iph1
->rmconf
->myprivfile
== NULL
) {
1610 plog(LLV_ERROR
, LOCATION
, NULL
, "no cert defined.\n");
1614 /* make private file name */
1615 getpathname(path
, sizeof(path
),
1617 iph1
->rmconf
->myprivfile
);
1618 privkey
= eay_get_pkcs1privkey(path
);
1619 if (privkey
== NULL
) {
1620 plog(LLV_ERROR
, LOCATION
, NULL
,
1621 "failed to get private key.\n");
1624 plog(LLV_DEBUG2
, LOCATION
, NULL
, "private key:\n");
1625 plogdump(LLV_DEBUG2
, privkey
->v
, privkey
->l
);
1627 iph1
->sig
= eay_get_x509sign(iph1
->hash
,
1628 privkey
, &iph1
->cert
->cert
);
1635 if (iph1
->sig
== NULL
) {
1636 plog(LLV_ERROR
, LOCATION
, NULL
, "failed to sign.\n");
1640 plog(LLV_DEBUG
, LOCATION
, NULL
, "SIGN computed:\n");
1641 plogdump(LLV_DEBUG
, iph1
->sig
->v
, iph1
->sig
->l
);
1646 if (privkey
!= NULL
)
1653 * compare certificate name and ID value.
1656 oakley_check_certid(u_int8_t idtype
, int idlen
, void* id
, cert_t
* cert_p
)
1658 vchar_t
*name
= NULL
;
1659 char *altname
= NULL
;
1664 case IPSECDOI_ID_DER_ASN1_DN
:
1665 name
= eay_get_x509asn1subjectname(&cert_p
->cert
);
1667 plog(LLV_ERROR
, LOCATION
, NULL
,
1668 "failed to get subjectName\n");
1669 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1671 if (idlen
!= name
->l
) {
1672 plog(LLV_ERROR
, LOCATION
, NULL
,
1673 "Invalid ID length in phase 1.\n");
1675 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1677 error
= memcmp(id
, name
->v
, idlen
);
1680 plog(LLV_ERROR
, LOCATION
, NULL
,
1681 "ID mismatched with subjectAltName.\n");
1682 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1685 case IPSECDOI_ID_IPV4_ADDR
:
1686 case IPSECDOI_ID_IPV6_ADDR
:
1690 * Openssl returns the IPAddress as an ASN1 octet string (binary format)
1691 * followed by a trailing NULL. 5 bytes for IPv4 and 17 bytes for IPv6
1693 #define SUBJ_ALT_NAME_IPV4_ADDRESS_LEN 5
1694 #define SUBJ_ALT_NAME_IPV6_ADDRESS_LEN 17
1698 if (idtype
== IPSECDOI_ID_IPV4_ADDR
&& idlen
!= sizeof(struct in_addr
)
1699 || idtype
== IPSECDOI_ID_IPV6_ADDR
&& idlen
!= sizeof(struct in6_addr
)) {
1700 plog(LLV_ERROR
, LOCATION
, NULL
,
1701 "invalid address length passed.\n");
1702 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1705 for (pos
= 1; ; pos
++) {
1706 if (eay_get_x509subjectaltname(&cert_p
->cert
, &altname
, &type
, pos
, &len
) !=0) {
1707 plog(LLV_ERROR
, LOCATION
, NULL
,
1708 "failed to get subjectAltName\n");
1709 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1712 /* it's the end condition of the loop. */
1714 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1717 if (check_typeofcertname(idtype
, type
) != 0) {
1718 /* wrong type - skip this one */
1719 racoon_free(altname
);
1724 if (len
== SUBJ_ALT_NAME_IPV4_ADDRESS_LEN
) { /* IPv4 */
1725 if (idtype
!= IPSECDOI_ID_IPV4_ADDR
) {
1726 /* wrong IP address type - skip this one */
1727 racoon_free(altname
);
1733 else if (len
== SUBJ_ALT_NAME_IPV6_ADDRESS_LEN
) { /* IPv6 */
1734 if (idtype
!= IPSECDOI_ID_IPV6_ADDR
) {
1735 /* wrong IP address type - skip this one */
1736 racoon_free(altname
);
1743 /* invalid IP address length in certificate - bad or bogus certificate */
1744 plog(LLV_ERROR
, LOCATION
, NULL
,
1745 "invalid IP address in certificate.\n");
1746 racoon_free(altname
);
1748 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1751 /* compare the addresses */
1752 error
= memcmp(id
, altname
, idlen
);
1753 racoon_free(altname
);
1755 plog(LLV_ERROR
, LOCATION
, NULL
,
1756 "ID mismatched with subjectAltName.\n");
1757 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1762 case IPSECDOI_ID_FQDN
:
1763 case IPSECDOI_ID_USER_FQDN
:
1767 for (pos
= 1; ; pos
++) {
1768 if (eay_get_x509subjectaltname(&cert_p
->cert
, &altname
, &type
, pos
, &len
) != 0) {
1769 plog(LLV_ERROR
, LOCATION
, NULL
,
1770 "failed to get subjectAltName\n");
1771 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1774 /* it's the end condition of the loop. */
1776 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1779 if (check_typeofcertname(idtype
, type
) != 0) {
1780 /* wrong general type - skip this one */
1781 racoon_free(altname
);
1786 if (idlen
!= strlen(altname
)) {
1787 /* wrong length - skip this one */
1788 racoon_free(altname
);
1792 error
= memcmp(id
, altname
, idlen
);
1793 racoon_free(altname
);
1795 plog(LLV_ERROR
, LOCATION
, NULL
, "ID mismatched.\n");
1796 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1802 plog(LLV_ERROR
, LOCATION
, NULL
,
1803 "Inpropper ID type passed: %s.\n",
1804 s_ipsecdoi_ident(idtype
));
1805 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1811 check_typeofcertname(doi
, genid
)
1815 case IPSECDOI_ID_IPV4_ADDR
:
1816 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
1817 case IPSECDOI_ID_IPV6_ADDR
:
1818 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
1819 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
1820 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
1821 if (genid
!= GENT_IPADD
)
1824 case IPSECDOI_ID_FQDN
:
1825 if (genid
!= GENT_DNS
)
1828 case IPSECDOI_ID_USER_FQDN
:
1829 if (genid
!= GENT_EMAIL
)
1832 case IPSECDOI_ID_DER_ASN1_DN
: /* should not be passed to this function*/
1833 case IPSECDOI_ID_DER_ASN1_GN
:
1834 case IPSECDOI_ID_KEY_ID
:
1842 * save certificate including certificate type.
1845 oakley_savecert(iph1
, gen
)
1846 struct ph1handle
*iph1
;
1847 struct isakmp_gen
*gen
;
1852 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
1855 case ISAKMP_CERT_DNS
:
1856 plog(LLV_WARNING
, LOCATION
, NULL
,
1857 "CERT payload is unnecessary in DNSSEC. "
1858 "ignore this CERT payload.\n");
1860 case ISAKMP_CERT_PKCS7
:
1861 case ISAKMP_CERT_PGP
:
1862 case ISAKMP_CERT_X509SIGN
:
1863 case ISAKMP_CERT_KERBEROS
:
1864 case ISAKMP_CERT_SPKI
:
1867 case ISAKMP_CERT_CRL
:
1870 case ISAKMP_CERT_X509KE
:
1871 case ISAKMP_CERT_X509ATTR
:
1872 case ISAKMP_CERT_ARL
:
1873 plog(LLV_ERROR
, LOCATION
, NULL
,
1874 "No supported such CERT type %d\n", type
);
1877 plog(LLV_ERROR
, LOCATION
, NULL
,
1878 "Invalid CERT type %d\n", type
);
1882 /* XXX choice the 1th cert, ignore after the cert. */
1883 /* XXX should be processed. */
1885 plog(LLV_WARNING
, LOCATION
, NULL
,
1886 "ignore 2nd CERT payload.\n");
1890 *c
= save_certbuf(gen
);
1892 plog(LLV_ERROR
, LOCATION
, NULL
,
1893 "Failed to get CERT buffer.\n");
1897 switch ((*c
)->type
) {
1898 case ISAKMP_CERT_DNS
:
1899 plog(LLV_WARNING
, LOCATION
, NULL
,
1900 "CERT payload is unnecessary in DNSSEC. "
1903 case ISAKMP_CERT_PKCS7
:
1904 case ISAKMP_CERT_PGP
:
1905 case ISAKMP_CERT_X509SIGN
:
1906 case ISAKMP_CERT_KERBEROS
:
1907 case ISAKMP_CERT_SPKI
:
1908 plog(LLV_DEBUG
, LOCATION
, NULL
, "CERT saved:\n");
1909 plogdump(LLV_DEBUG
, (*c
)->cert
.v
, (*c
)->cert
.l
);
1911 char *p
= eay_get_x509text(&(*c
)->cert
);
1912 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s", p
? p
: "\n");
1916 case ISAKMP_CERT_CRL
:
1917 plog(LLV_DEBUG
, LOCATION
, NULL
, "CRL saved:\n");
1918 plogdump(LLV_DEBUG
, (*c
)->cert
.v
, (*c
)->cert
.l
);
1920 case ISAKMP_CERT_X509KE
:
1921 case ISAKMP_CERT_X509ATTR
:
1922 case ISAKMP_CERT_ARL
:
1925 oakley_delcert((*c
));
1934 * save certificate including certificate type.
1937 oakley_savecr(iph1
, gen
)
1938 struct ph1handle
*iph1
;
1939 struct isakmp_gen
*gen
;
1944 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
1947 case ISAKMP_CERT_DNS
:
1948 plog(LLV_WARNING
, LOCATION
, NULL
,
1949 "CERT payload is unnecessary in DNSSEC\n");
1951 case ISAKMP_CERT_PKCS7
:
1952 case ISAKMP_CERT_PGP
:
1953 case ISAKMP_CERT_X509SIGN
:
1954 case ISAKMP_CERT_KERBEROS
:
1955 case ISAKMP_CERT_SPKI
:
1958 case ISAKMP_CERT_X509KE
:
1959 case ISAKMP_CERT_X509ATTR
:
1960 case ISAKMP_CERT_ARL
:
1961 plog(LLV_ERROR
, LOCATION
, NULL
,
1962 "No supported such CR type %d\n", type
);
1964 case ISAKMP_CERT_CRL
:
1966 plog(LLV_ERROR
, LOCATION
, NULL
,
1967 "Invalid CR type %d\n", type
);
1971 *c
= save_certbuf(gen
);
1973 plog(LLV_ERROR
, LOCATION
, NULL
,
1974 "Failed to get CR buffer.\n");
1978 plog(LLV_DEBUG
, LOCATION
, NULL
, "CR saved:\n");
1979 plogdump(LLV_DEBUG
, (*c
)->cert
.v
, (*c
)->cert
.l
);
1986 struct isakmp_gen
*gen
;
1990 new = oakley_newcert();
1992 plog(LLV_ERROR
, LOCATION
, NULL
,
1993 "Failed to get CERT buffer.\n");
1997 new->pl
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
1998 if (new->pl
== NULL
) {
1999 plog(LLV_ERROR
, LOCATION
, NULL
,
2000 "Failed to copy CERT from packet.\n");
2001 oakley_delcert(new);
2005 memcpy(new->pl
->v
, gen
+ 1, new->pl
->l
);
2006 new->type
= new->pl
->v
[0] & 0xff;
2007 new->cert
.v
= new->pl
->v
+ 1;
2008 new->cert
.l
= new->pl
->l
- 1;
2015 * NOTE: No Certificate Authority field is included to CR payload at the
2016 * moment. Becuase any certificate authority are accepted without any check.
2017 * The section 3.10 in RFC2408 says that this field SHOULD not be included,
2018 * if there is no specific certificate authority requested.
2022 struct ph1handle
*iph1
;
2028 plog(LLV_ERROR
, LOCATION
, NULL
,
2029 "failed to get cr buffer\n");
2032 buf
->v
[0] = iph1
->rmconf
->certtype
;
2034 plog(LLV_DEBUG
, LOCATION
, NULL
, "create my CR: %s\n",
2035 s_isakmp_certtype(iph1
->rmconf
->certtype
));
2037 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2046 oakley_checkcr(iph1
)
2047 struct ph1handle
*iph1
;
2049 if (iph1
->cr_p
== NULL
)
2052 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
2053 "peer transmitted CR: %s\n",
2054 s_isakmp_certtype(iph1
->cr_p
->type
));
2056 if (iph1
->cr_p
->type
!= iph1
->rmconf
->certtype
) {
2057 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
2058 "such a cert type isn't supported: %d\n",
2059 (char)iph1
->cr_p
->type
);
2067 * check to need CR payload.
2074 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
2075 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2082 #endif /*HAVE_SIGNING_C*/
2086 * see seciton 5. Exchanges in RFC 2409
2087 * psk: SKEYID = prf(pre-shared-key, Ni_b | Nr_b)
2088 * sig: SKEYID = prf(Ni_b | Nr_b, g^ir)
2089 * enc: SKEYID = prf(H(Ni_b | Nr_b), CKY-I | CKY-R)
2093 struct ph1handle
*iph1
;
2095 vchar_t
*buf
= NULL
, *bp
;
2101 switch(iph1
->approval
->authmethod
) {
2102 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2103 if (iph1
->nonce_p
== NULL
) {
2104 plog(LLV_ERROR
, LOCATION
, NULL
,
2105 "no nonce payload received from peer.\n");
2108 /* if we have a preshared key defined, just use it */
2109 if (iph1
->rmconf
->shared_secret
) {
2111 switch (iph1
->rmconf
->secrettype
) {
2112 case SECRETTYPE_KEY
:
2113 iph1
->authstr
= getpsk(iph1
->rmconf
->shared_secret
->v
, iph1
->rmconf
->shared_secret
->l
-1);
2115 case SECRETTYPE_KEYCHAIN
:
2116 iph1
->authstr
= getpskfromkeychain(iph1
->rmconf
->shared_secret
->v
);
2118 case SECRETTYPE_USE
:
2120 iph1
->authstr
= vdup(iph1
->rmconf
->shared_secret
);
2124 else if (iph1
->etype
!= ISAKMP_ETYPE_IDENT
) {
2125 iph1
->authstr
= getpskbyname(iph1
->id_p
);
2126 if (iph1
->authstr
== NULL
) {
2127 if (iph1
->rmconf
->verify_identifier
) {
2128 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
2129 "couldn't find the pskey.\n");
2132 plog(LLV_NOTIFY
, LOCATION
, iph1
->remote
,
2133 "couldn't find the proper pskey, "
2134 "try to get one by the peer's address.\n");
2137 if (iph1
->authstr
== NULL
) {
2139 * If the exchange type is the main mode or if it's
2140 * failed to get the psk by ID, racoon try to get
2141 * the psk by remote IP address.
2142 * It may be nonsense.
2144 iph1
->authstr
= getpskbyaddr(iph1
->remote
);
2145 if (iph1
->authstr
== NULL
) {
2146 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
2147 "couldn't find the pskey for %s.\n",
2148 saddrwop2str(iph1
->remote
));
2152 plog(LLV_DEBUG
, LOCATION
, NULL
, "the psk found.\n");
2153 /* should be secret PSK */
2154 plog(LLV_DEBUG2
, LOCATION
, NULL
, "psk: ");
2155 plogdump(LLV_DEBUG2
, iph1
->authstr
->v
, iph1
->authstr
->l
);
2157 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2160 plog(LLV_ERROR
, LOCATION
, NULL
,
2161 "failed to get skeyid buffer\n");
2166 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2167 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce 1: ");
2168 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2169 memcpy(p
, bp
->v
, bp
->l
);
2172 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2173 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce 2: ");
2174 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2175 memcpy(p
, bp
->v
, bp
->l
);
2178 iph1
->skeyid
= oakley_prf(iph1
->authstr
, buf
, iph1
);
2179 if (iph1
->skeyid
== NULL
)
2183 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
2184 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2186 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
2188 if (iph1
->nonce_p
== NULL
) {
2189 plog(LLV_ERROR
, LOCATION
, NULL
,
2190 "no nonce payload received from peer.\n");
2193 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2196 plog(LLV_ERROR
, LOCATION
, NULL
,
2197 "failed to get nonce buffer\n");
2202 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2203 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce1: ");
2204 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2205 memcpy(p
, bp
->v
, bp
->l
);
2208 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2209 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce2: ");
2210 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2211 memcpy(p
, bp
->v
, bp
->l
);
2214 iph1
->skeyid
= oakley_prf(buf
, iph1
->dhgxy
, iph1
);
2215 if (iph1
->skeyid
== NULL
)
2218 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2219 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2220 plog(LLV_WARNING
, LOCATION
, NULL
,
2221 "not supported authentication method %s\n",
2222 s_oakley_attr_method(iph1
->approval
->authmethod
));
2225 plog(LLV_ERROR
, LOCATION
, NULL
,
2226 "invalid authentication method %d\n",
2227 iph1
->approval
->authmethod
);
2231 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID computed:\n");
2232 plogdump(LLV_DEBUG
, iph1
->skeyid
->v
, iph1
->skeyid
->l
);
2243 * compute SKEYID_[dae]
2244 * see seciton 5. Exchanges in RFC 2409
2245 * SKEYID_d = prf(SKEYID, g^ir | CKY-I | CKY-R | 0)
2246 * SKEYID_a = prf(SKEYID, SKEYID_d | g^ir | CKY-I | CKY-R | 1)
2247 * SKEYID_e = prf(SKEYID, SKEYID_a | g^ir | CKY-I | CKY-R | 2)
2250 oakley_skeyid_dae(iph1
)
2251 struct ph1handle
*iph1
;
2253 vchar_t
*buf
= NULL
;
2258 if (iph1
->skeyid
== NULL
) {
2259 plog(LLV_ERROR
, LOCATION
, NULL
, "no SKEYID found.\n");
2264 /* SKEYID_d = prf(SKEYID, g^xy | CKY-I | CKY-R | 0) */
2265 len
= iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2268 plog(LLV_ERROR
, LOCATION
, NULL
,
2269 "failed to get skeyid buffer\n");
2274 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2275 p
+= iph1
->dhgxy
->l
;
2276 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2277 p
+= sizeof(cookie_t
);
2278 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2279 p
+= sizeof(cookie_t
);
2281 iph1
->skeyid_d
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2282 if (iph1
->skeyid_d
== NULL
)
2288 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID_d computed:\n");
2289 plogdump(LLV_DEBUG
, iph1
->skeyid_d
->v
, iph1
->skeyid
->l
);
2292 /* SKEYID_a = prf(SKEYID, SKEYID_d | g^xy | CKY-I | CKY-R | 1) */
2293 len
= iph1
->skeyid_d
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2296 plog(LLV_ERROR
, LOCATION
, NULL
,
2297 "failed to get skeyid buffer\n");
2301 memcpy(p
, iph1
->skeyid_d
->v
, iph1
->skeyid_d
->l
);
2302 p
+= iph1
->skeyid_d
->l
;
2303 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2304 p
+= iph1
->dhgxy
->l
;
2305 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2306 p
+= sizeof(cookie_t
);
2307 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2308 p
+= sizeof(cookie_t
);
2310 iph1
->skeyid_a
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2311 if (iph1
->skeyid_a
== NULL
)
2317 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID_a computed:\n");
2318 plogdump(LLV_DEBUG
, iph1
->skeyid_a
->v
, iph1
->skeyid_a
->l
);
2321 /* SKEYID_e = prf(SKEYID, SKEYID_a | g^xy | CKY-I | CKY-R | 2) */
2322 len
= iph1
->skeyid_a
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2325 plog(LLV_ERROR
, LOCATION
, NULL
,
2326 "failed to get skeyid buffer\n");
2330 memcpy(p
, iph1
->skeyid_a
->v
, iph1
->skeyid_a
->l
);
2331 p
+= iph1
->skeyid_a
->l
;
2332 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2333 p
+= iph1
->dhgxy
->l
;
2334 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2335 p
+= sizeof(cookie_t
);
2336 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2337 p
+= sizeof(cookie_t
);
2339 iph1
->skeyid_e
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2340 if (iph1
->skeyid_e
== NULL
)
2346 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID_e computed:\n");
2347 plogdump(LLV_DEBUG
, iph1
->skeyid_e
->v
, iph1
->skeyid_e
->l
);
2358 * compute final encryption key.
2362 oakley_compute_enckey(iph1
)
2363 struct ph1handle
*iph1
;
2365 u_int keylen
, prflen
;
2369 keylen
= alg_oakley_encdef_keylen(iph1
->approval
->enctype
,
2370 iph1
->approval
->encklen
);
2372 plog(LLV_ERROR
, LOCATION
, NULL
,
2373 "invalid encryption algoritym %d, "
2374 "or invalid key length %d.\n",
2375 iph1
->approval
->enctype
,
2376 iph1
->approval
->encklen
);
2379 iph1
->key
= vmalloc(keylen
>> 3);
2380 if (iph1
->key
== NULL
) {
2381 plog(LLV_ERROR
, LOCATION
, NULL
,
2382 "failed to get key buffer\n");
2386 /* set prf length */
2387 prflen
= alg_oakley_hashdef_hashlen(iph1
->approval
->hashtype
);
2389 plog(LLV_ERROR
, LOCATION
, NULL
,
2390 "invalid hash type %d.\n", iph1
->approval
->hashtype
);
2394 /* see isakmp-oakley-08 5.3. */
2395 if (iph1
->key
->l
<= iph1
->skeyid_e
->l
) {
2397 * if length(Ka) <= length(SKEYID_e)
2398 * Ka = first length(K) bit of SKEYID_e
2400 memcpy(iph1
->key
->v
, iph1
->skeyid_e
->v
, iph1
->key
->l
);
2402 vchar_t
*buf
= NULL
, *res
= NULL
;
2411 * K1 = prf(SKEYID_e, 0)
2412 * K2 = prf(SKEYID_e, K1)
2413 * K3 = prf(SKEYID_e, K2)
2415 plog(LLV_DEBUG
, LOCATION
, NULL
,
2416 "len(SKEYID_e) < len(Ka) (%d < %d), "
2417 "generating long key (Ka = K1 | K2 | ...)\n",
2418 iph1
->skeyid_e
->l
, iph1
->key
->l
);
2420 if ((buf
= vmalloc(prflen
>> 3)) == 0) {
2421 plog(LLV_ERROR
, LOCATION
, NULL
,
2422 "failed to get key buffer\n");
2425 p
= (u_char
*)iph1
->key
->v
;
2426 ep
= p
+ iph1
->key
->l
;
2430 if (p
== (u_char
*)iph1
->key
->v
) {
2431 /* just for computing K1 */
2435 res
= oakley_prf(iph1
->skeyid_e
, buf
, iph1
);
2440 plog(LLV_DEBUG
, LOCATION
, NULL
,
2441 "compute intermediate encryption key K%d\n",
2443 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2444 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
2446 cplen
= (res
->l
< ep
- p
) ? res
->l
: ep
- p
;
2447 memcpy(p
, res
->v
, cplen
);
2450 buf
->l
= prflen
>> 3; /* to cancel K1 speciality */
2451 if (res
->l
!= buf
->l
) {
2452 plog(LLV_ERROR
, LOCATION
, NULL
,
2453 "internal error: res->l=%d buf->l=%d\n",
2459 memcpy(buf
->v
, res
->v
, res
->l
);
2468 * don't check any weak key or not.
2469 * draft-ietf-ipsec-ike-01.txt Appendix B.
2470 * draft-ietf-ipsec-ciph-aes-cbc-00.txt Section 2.3.
2474 if (iph1
->approval
->enctype
> ARRAYLEN(oakley_encdef
)
2475 || oakley_encdef
[iph1
->approval
->enctype
].weakkey
== NULL
) {
2476 plog(LLV_ERROR
, LOCATION
, NULL
,
2477 "encryption algoritym %d isn't supported.\n",
2478 iph1
->approval
->enctype
);
2481 if ((oakley_encdef
[iph1
->approval
->enctype
].weakkey
)(iph1
->key
)) {
2482 plog(LLV_ERROR
, LOCATION
, NULL
,
2483 "weakkey was generated.\n");
2488 plog(LLV_DEBUG
, LOCATION
, NULL
, "final encryption key computed:\n");
2489 plogdump(LLV_DEBUG
, iph1
->key
->v
, iph1
->key
->l
);
2497 /* allocated new buffer for CERT */
2503 new = racoon_calloc(1, sizeof(*new));
2505 plog(LLV_ERROR
, LOCATION
, NULL
,
2506 "failed to get cert's buffer\n");
2515 /* delete buffer for CERT */
2517 oakley_delcert(cert
)
2528 * compute IV and set to ph1handle
2529 * IV = hash(g^xi | g^xr)
2530 * see 4.1 Phase 1 state in draft-ietf-ipsec-ike.
2534 struct ph1handle
*iph1
;
2536 struct isakmp_ivm
*newivm
= NULL
;
2537 vchar_t
*buf
= NULL
, *bp
;
2542 len
= iph1
->dhpub
->l
+ iph1
->dhpub_p
->l
;
2545 plog(LLV_ERROR
, LOCATION
, NULL
,
2546 "failed to get iv buffer\n");
2552 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub
: iph1
->dhpub_p
);
2553 memcpy(p
, bp
->v
, bp
->l
);
2556 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub_p
: iph1
->dhpub
);
2557 memcpy(p
, bp
->v
, bp
->l
);
2561 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
2562 if (newivm
== NULL
) {
2563 plog(LLV_ERROR
, LOCATION
, NULL
,
2564 "failed to get iv buffer\n");
2570 newivm
->iv
= oakley_hash(buf
, iph1
);
2571 if (newivm
->iv
== NULL
) {
2573 oakley_delivm(newivm
);
2577 /* adjust length of iv */
2578 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2579 if (newivm
->iv
->l
== -1) {
2580 plog(LLV_ERROR
, LOCATION
, NULL
,
2581 "invalid encryption algoriym %d.\n",
2582 iph1
->approval
->enctype
);
2584 oakley_delivm(newivm
);
2588 /* create buffer to save iv */
2589 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
2590 plog(LLV_ERROR
, LOCATION
, NULL
,
2591 "vdup (%s)\n", strerror(errno
));
2593 oakley_delivm(newivm
);
2599 plog(LLV_DEBUG
, LOCATION
, NULL
, "IV computed:\n");
2600 plogdump(LLV_DEBUG
, newivm
->iv
->v
, newivm
->iv
->l
);
2608 * compute IV for the payload after phase 1.
2609 * It's not limited for phase 2.
2610 * if pahse 1 was encrypted.
2611 * IV = hash(last CBC block of Phase 1 | M-ID)
2612 * if phase 1 was not encrypted.
2613 * IV = hash(phase 1 IV | M-ID)
2614 * see 4.2 Phase 2 state in draft-ietf-ipsec-ike.
2617 oakley_newiv2(iph1
, msgid
)
2618 struct ph1handle
*iph1
;
2621 struct isakmp_ivm
*newivm
= NULL
;
2622 vchar_t
*buf
= NULL
;
2628 len
= iph1
->ivm
->iv
->l
+ sizeof(msgid_t
);
2631 plog(LLV_ERROR
, LOCATION
, NULL
,
2632 "failed to get iv buffer\n");
2638 memcpy(p
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
2639 p
+= iph1
->ivm
->iv
->l
;
2641 memcpy(p
, &msgid
, sizeof(msgid
));
2643 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute IV for phase2\n");
2644 plog(LLV_DEBUG
, LOCATION
, NULL
, "phase1 last IV:\n");
2645 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2648 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
2649 if (newivm
== NULL
) {
2650 plog(LLV_ERROR
, LOCATION
, NULL
,
2651 "failed to get iv buffer\n");
2656 if ((newivm
->iv
= oakley_hash(buf
, iph1
)) == NULL
)
2659 /* adjust length of iv */
2660 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2661 if (newivm
->iv
->l
== -1) {
2662 plog(LLV_ERROR
, LOCATION
, NULL
,
2663 "invalid encryption algoriym %d.\n",
2664 iph1
->approval
->enctype
);
2668 /* create buffer to save new iv */
2669 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
2670 plog(LLV_ERROR
, LOCATION
, NULL
, "vdup (%s)\n", strerror(errno
));
2676 plog(LLV_DEBUG
, LOCATION
, NULL
, "phase2 IV computed:\n");
2677 plogdump(LLV_DEBUG
, newivm
->iv
->v
, newivm
->iv
->l
);
2680 if (error
&& newivm
!= NULL
)
2681 oakley_delivm(newivm
);
2689 struct isakmp_ivm
*ivm
;
2694 if (ivm
->iv
!= NULL
)
2696 if (ivm
->ive
!= NULL
)
2705 * save new iv and old iv.
2708 oakley_do_decrypt(iph1
, msg
, ivdp
, ivep
)
2709 struct ph1handle
*iph1
;
2710 vchar_t
*msg
, *ivdp
, *ivep
;
2712 vchar_t
*buf
= NULL
, *new = NULL
;
2719 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin decryption.\n");
2721 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2723 plog(LLV_ERROR
, LOCATION
, NULL
,
2724 "invalid encryption algoriym %d.\n",
2725 iph1
->approval
->enctype
);
2729 /* save IV for next, but not sync. */
2730 memset(ivep
->v
, 0, ivep
->l
);
2731 memcpy(ivep
->v
, (caddr_t
)&msg
->v
[msg
->l
- blen
], blen
);
2733 plog(LLV_DEBUG
, LOCATION
, NULL
,
2734 "IV was saved for next processing:\n");
2735 plogdump(LLV_DEBUG
, ivep
->v
, ivep
->l
);
2737 pl
= msg
->v
+ sizeof(struct isakmp
);
2739 len
= msg
->l
- sizeof(struct isakmp
);
2744 plog(LLV_ERROR
, LOCATION
, NULL
,
2745 "failed to get buffer to decrypt.\n");
2748 memcpy(buf
->v
, pl
, len
);
2751 new = alg_oakley_encdef_decrypt(iph1
->approval
->enctype
,
2752 buf
, iph1
->key
, ivdp
);
2754 plog(LLV_ERROR
, LOCATION
, NULL
,
2755 "decryption %d failed.\n", iph1
->approval
->enctype
);
2758 plog(LLV_DEBUG
, LOCATION
, NULL
, "with key:\n");
2759 plogdump(LLV_DEBUG
, iph1
->key
->v
, iph1
->key
->l
);
2766 plog(LLV_DEBUG
, LOCATION
, NULL
, "decrypted payload by IV:\n");
2767 plogdump(LLV_DEBUG
, ivdp
->v
, ivdp
->l
);
2769 plog(LLV_DEBUG
, LOCATION
, NULL
,
2770 "decrypted payload, but not trimed.\n");
2771 plogdump(LLV_DEBUG
, new->v
, new->l
);
2773 /* get padding length */
2774 if (lcconf
->pad_excltail
)
2775 padlen
= new->v
[new->l
- 1] + 1;
2777 padlen
= new->v
[new->l
- 1];
2778 plog(LLV_DEBUG
, LOCATION
, NULL
, "padding len=%u\n", padlen
);
2781 if (lcconf
->pad_strict
) {
2782 if (padlen
> new->l
) {
2783 plog(LLV_ERROR
, LOCATION
, NULL
,
2784 "invalied padding len=%u, buflen=%u.\n",
2786 plogdump(LLV_ERROR
, new->v
, new->l
);
2790 plog(LLV_DEBUG
, LOCATION
, NULL
, "trimmed padding\n");
2792 plog(LLV_DEBUG
, LOCATION
, NULL
, "skip to trim padding.\n");
2795 /* create new buffer */
2796 len
= sizeof(struct isakmp
) + new->l
;
2799 plog(LLV_ERROR
, LOCATION
, NULL
,
2800 "failed to get buffer to decrypt.\n");
2803 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
2804 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
2805 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
2807 plog(LLV_DEBUG
, LOCATION
, NULL
, "decrypted.\n");
2808 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2810 #ifdef HAVE_PRINT_ISAKMP_C
2811 isakmp_printpacket(buf
, iph1
->remote
, iph1
->local
, 1);
2817 if (error
&& buf
!= NULL
) {
2831 oakley_do_encrypt(iph1
, msg
, ivep
, ivp
)
2832 struct ph1handle
*iph1
;
2833 vchar_t
*msg
, *ivep
, *ivp
;
2835 vchar_t
*buf
= 0, *new = 0;
2842 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin encryption.\n");
2844 /* set cbc block length */
2845 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2847 plog(LLV_ERROR
, LOCATION
, NULL
,
2848 "invalid encryption algoriym %d.\n",
2849 iph1
->approval
->enctype
);
2853 pl
= msg
->v
+ sizeof(struct isakmp
);
2854 len
= msg
->l
- sizeof(struct isakmp
);
2857 padlen
= oakley_padlen(len
, blen
);
2858 plog(LLV_DEBUG
, LOCATION
, NULL
, "pad length = %u\n", padlen
);
2861 buf
= vmalloc(len
+ padlen
);
2863 plog(LLV_ERROR
, LOCATION
, NULL
,
2864 "failed to get buffer to encrypt.\n");
2869 char *p
= &buf
->v
[len
];
2870 if (lcconf
->pad_random
) {
2871 for (i
= 0; i
< padlen
; i
++)
2872 *p
++ = arc4random() & 0xff;
2875 memcpy(buf
->v
, pl
, len
);
2877 /* make pad into tail */
2878 if (lcconf
->pad_excltail
)
2879 buf
->v
[len
+ padlen
- 1] = padlen
- 1;
2881 buf
->v
[len
+ padlen
- 1] = padlen
;
2883 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2886 new = alg_oakley_encdef_encrypt(iph1
->approval
->enctype
,
2887 buf
, iph1
->key
, ivep
);
2889 plog(LLV_ERROR
, LOCATION
, NULL
,
2890 "encryption %d failed.\n", iph1
->approval
->enctype
);
2893 plog(LLV_DEBUG
, LOCATION
, NULL
, "with key:\n");
2894 plogdump(LLV_DEBUG
, iph1
->key
->v
, iph1
->key
->l
);
2901 plog(LLV_DEBUG
, LOCATION
, NULL
, "encrypted payload by IV:\n");
2902 plogdump(LLV_DEBUG
, ivep
->v
, ivep
->l
);
2904 /* save IV for next */
2905 memset(ivp
->v
, 0, ivp
->l
);
2906 memcpy(ivp
->v
, (caddr_t
)&new->v
[new->l
- blen
], blen
);
2908 plog(LLV_DEBUG
, LOCATION
, NULL
, "save IV for next:\n");
2909 plogdump(LLV_DEBUG
, ivp
->v
, ivp
->l
);
2911 /* create new buffer */
2912 len
= sizeof(struct isakmp
) + new->l
;
2915 plog(LLV_ERROR
, LOCATION
, NULL
,
2916 "failed to get buffer to encrypt.\n");
2919 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
2920 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
2921 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
2925 plog(LLV_DEBUG
, LOCATION
, NULL
, "encrypted.\n");
2928 if (error
&& buf
!= NULL
) {
2938 /* culculate padding length */
2940 oakley_padlen(len
, base
)
2945 padlen
= base
- len
% base
;
2947 if (lcconf
->pad_randomlen
)
2948 padlen
+= ((arc4random() % (lcconf
->pad_maxsize
+ 1) + 1) *