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 */
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
47 # include <sys/time.h>
60 #include "isakmp_var.h"
63 #include "localconf.h"
64 #include "remoteconf.h"
67 #include "ipsec_doi.h"
68 #include "algorithm.h"
72 #include "crypto_openssl.h"
77 #ifndef HAVE_ARC4RANDOM
78 #include "arc4random.h"
88 #define INITDHVAL(a, s, d, t) \
91 buf.v = str2val((s), 16, &buf.l); \
92 memset(&a, 0, sizeof(struct dhgroup)); \
94 a.prime = vdup(&buf); \
100 struct dhgroup dh_modp768
;
101 struct dhgroup dh_modp1024
;
102 struct dhgroup dh_modp1536
;
103 struct dhgroup dh_modp2048
;
104 struct dhgroup dh_modp3072
;
105 struct dhgroup dh_modp4096
;
106 struct dhgroup dh_modp6144
;
107 struct dhgroup dh_modp8192
;
109 static int oakley_compute_keymat_x
__P((struct ph2handle
*, int, int));
110 #ifdef HAVE_SIGNING_C
111 static int get_cert_fromlocal
__P((struct ph1handle
*, int));
112 static int oakley_check_certid
__P((struct ph1handle
*iph1
));
113 static int check_typeofcertname
__P((int, int));
114 static cert_t
*save_certbuf
__P((struct isakmp_gen
*));
116 static int oakley_padlen
__P((int, int));
119 oakley_get_defaultlifetime()
121 return OAKLEY_ATTR_SA_LD_SEC_DEFAULT
;
128 INITDHVAL(dh_modp768
, OAKLEY_PRIME_MODP768
,
129 OAKLEY_ATTR_GRP_DESC_MODP768
, OAKLEY_ATTR_GRP_TYPE_MODP
);
130 INITDHVAL(dh_modp1024
, OAKLEY_PRIME_MODP1024
,
131 OAKLEY_ATTR_GRP_DESC_MODP1024
, OAKLEY_ATTR_GRP_TYPE_MODP
);
132 INITDHVAL(dh_modp1536
, OAKLEY_PRIME_MODP1536
,
133 OAKLEY_ATTR_GRP_DESC_MODP1536
, OAKLEY_ATTR_GRP_TYPE_MODP
);
134 INITDHVAL(dh_modp2048
, OAKLEY_PRIME_MODP2048
,
135 OAKLEY_ATTR_GRP_DESC_MODP2048
, OAKLEY_ATTR_GRP_TYPE_MODP
);
136 INITDHVAL(dh_modp3072
, OAKLEY_PRIME_MODP3072
,
137 OAKLEY_ATTR_GRP_DESC_MODP3072
, OAKLEY_ATTR_GRP_TYPE_MODP
);
138 INITDHVAL(dh_modp4096
, OAKLEY_PRIME_MODP4096
,
139 OAKLEY_ATTR_GRP_DESC_MODP4096
, OAKLEY_ATTR_GRP_TYPE_MODP
);
140 INITDHVAL(dh_modp6144
, OAKLEY_PRIME_MODP6144
,
141 OAKLEY_ATTR_GRP_DESC_MODP6144
, OAKLEY_ATTR_GRP_TYPE_MODP
);
142 INITDHVAL(dh_modp8192
, OAKLEY_PRIME_MODP8192
,
143 OAKLEY_ATTR_GRP_DESC_MODP8192
, OAKLEY_ATTR_GRP_TYPE_MODP
);
149 oakley_dhgrp_free(dhgrp
)
150 struct dhgroup
*dhgrp
;
155 vfree(dhgrp
->curve_a
);
157 vfree(dhgrp
->curve_b
);
164 * compute sharing secret of DH
165 * IN: *dh, *pub, *priv, *pub_p
169 oakley_dh_compute(dh
, pub
, priv
, pub_p
, gxy
)
170 const struct dhgroup
*dh
;
171 vchar_t
*pub
, *priv
, *pub_p
, **gxy
;
174 struct timeval start
, end
;
176 if ((*gxy
= vmalloc(dh
->prime
->l
)) == NULL
) {
177 plog(LLV_ERROR
, LOCATION
, NULL
,
178 "failed to get DH buffer.\n");
183 gettimeofday(&start
, NULL
);
186 case OAKLEY_ATTR_GRP_TYPE_MODP
:
187 if (eay_dh_compute(dh
->prime
, dh
->gen1
, pub
, priv
, pub_p
, gxy
) < 0) {
188 plog(LLV_ERROR
, LOCATION
, NULL
,
189 "failed to compute dh value.\n");
193 case OAKLEY_ATTR_GRP_TYPE_ECP
:
194 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
195 plog(LLV_ERROR
, LOCATION
, NULL
,
196 "dh type %d isn't supported.\n", dh
->type
);
199 plog(LLV_ERROR
, LOCATION
, NULL
,
200 "invalid dh type %d.\n", dh
->type
);
205 gettimeofday(&end
, NULL
);
206 syslog(LOG_NOTICE
, "%s(%s%d): %8.6f", __func__
,
207 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
208 timedelta(&start
, &end
));
211 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute DH's shared.\n");
212 plogdump(LLV_DEBUG
, (*gxy
)->v
, (*gxy
)->l
);
218 * generate values of DH
223 oakley_dh_generate(dh
, pub
, priv
)
224 const struct dhgroup
*dh
;
225 vchar_t
**pub
, **priv
;
228 struct timeval start
, end
;
229 gettimeofday(&start
, NULL
);
232 case OAKLEY_ATTR_GRP_TYPE_MODP
:
233 if (eay_dh_generate(dh
->prime
, dh
->gen1
, dh
->gen2
, pub
, priv
) < 0) {
234 plog(LLV_ERROR
, LOCATION
, NULL
,
235 "failed to compute dh value.\n");
240 case OAKLEY_ATTR_GRP_TYPE_ECP
:
241 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
242 plog(LLV_ERROR
, LOCATION
, NULL
,
243 "dh type %d isn't supported.\n", dh
->type
);
246 plog(LLV_ERROR
, LOCATION
, NULL
,
247 "invalid dh type %d.\n", dh
->type
);
252 gettimeofday(&end
, NULL
);
253 syslog(LOG_NOTICE
, "%s(%s%d): %8.6f", __func__
,
254 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
255 timedelta(&start
, &end
));
257 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute DH's private.\n");
258 plogdump(LLV_DEBUG
, (*priv
)->v
, (*priv
)->l
);
259 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute DH's public.\n");
260 plogdump(LLV_DEBUG
, (*pub
)->v
, (*pub
)->l
);
266 * copy pre-defined dhgroup values.
269 oakley_setdhgroup(group
, dhgrp
)
271 struct dhgroup
**dhgrp
;
275 *dhgrp
= NULL
; /* just make sure, initialize */
277 g
= alg_oakley_dhdef_group(group
);
279 plog(LLV_ERROR
, LOCATION
, NULL
,
280 "invalid DH parameter grp=%d.\n", group
);
284 if (!g
->type
|| !g
->prime
|| !g
->gen1
) {
286 plog(LLV_ERROR
, LOCATION
, NULL
,
287 "unsupported DH parameters grp=%d.\n", group
);
291 *dhgrp
= racoon_calloc(1, sizeof(struct dhgroup
));
292 if (*dhgrp
== NULL
) {
293 plog(LLV_ERROR
, LOCATION
, NULL
,
294 "failed to get DH buffer.\n");
298 /* set defined dh vlaues */
299 memcpy(*dhgrp
, g
, sizeof(*g
));
300 (*dhgrp
)->prime
= vdup(g
->prime
);
308 * NOTE: we do not support prf with different input/output bitwidth,
309 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example) in
310 * oakley_compute_keymat(). If you add support for such prf function,
311 * modify oakley_compute_keymat() accordingly.
314 oakley_prf(key
, buf
, iph1
)
316 struct ph1handle
*iph1
;
321 if (iph1
->approval
== NULL
) {
323 * it's before negotiating hash algorithm.
324 * We use md5 as default.
326 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
328 type
= iph1
->approval
->hashtype
;
330 res
= alg_oakley_hmacdef_one(type
, key
, buf
);
332 plog(LLV_ERROR
, LOCATION
, NULL
,
333 "invalid hmac algorithm %d.\n", type
);
344 oakley_hash(buf
, iph1
)
346 struct ph1handle
*iph1
;
351 if (iph1
->approval
== NULL
) {
353 * it's before negotiating hash algorithm.
354 * We use md5 as default.
356 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
358 type
= iph1
->approval
->hashtype
;
360 res
= alg_oakley_hashdef_one(type
, buf
);
362 plog(LLV_ERROR
, LOCATION
, NULL
,
363 "invalid hash algorithm %d.\n", type
);
372 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
375 oakley_compute_keymat(iph2
, side
)
376 struct ph2handle
*iph2
;
381 /* compute sharing secret of DH when PFS */
382 if (iph2
->approval
->pfs_group
&& iph2
->dhpub_p
) {
383 if (oakley_dh_compute(iph2
->pfsgrp
, iph2
->dhpub
,
384 iph2
->dhpriv
, iph2
->dhpub_p
, &iph2
->dhgxy
) < 0)
389 if (oakley_compute_keymat_x(iph2
, side
, INBOUND_SA
) < 0
390 || oakley_compute_keymat_x(iph2
, side
, OUTBOUND_SA
) < 0)
393 plog(LLV_DEBUG
, LOCATION
, NULL
, "KEYMAT computed.\n");
403 * KEYMAT = prf(SKEYID_d, protocol | SPI | Ni_b | Nr_b).
404 * If PFS is desired and KE payloads were exchanged,
405 * KEYMAT = prf(SKEYID_d, g(qm)^xy | protocol | SPI | Ni_b | Nr_b)
407 * NOTE: we do not support prf with different input/output bitwidth,
408 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example).
411 oakley_compute_keymat_x(iph2
, side
, sa_dir
)
412 struct ph2handle
*iph2
;
416 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
421 int dupkeymat
; /* generate K[1-dupkeymat] */
424 int encklen
, authklen
, l
;
426 pfs
= ((iph2
->approval
->pfs_group
&& iph2
->dhgxy
) ? 1 : 0);
428 len
= pfs
? iph2
->dhgxy
->l
: 0;
430 + sizeof(u_int32_t
) /* XXX SPI size */
435 plog(LLV_ERROR
, LOCATION
, NULL
,
436 "failed to get keymat buffer.\n");
440 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
445 memcpy(p
, iph2
->dhgxy
->v
, iph2
->dhgxy
->l
);
452 memcpy(p
, (sa_dir
== INBOUND_SA
? &pr
->spi
: &pr
->spi_p
),
454 p
+= sizeof(pr
->spi
);
456 bp
= (side
== INITIATOR
? iph2
->nonce
: iph2
->nonce_p
);
457 memcpy(p
, bp
->v
, bp
->l
);
460 bp
= (side
== INITIATOR
? iph2
->nonce_p
: iph2
->nonce
);
461 memcpy(p
, bp
->v
, bp
->l
);
465 plog(LLV_DEBUG
, LOCATION
, NULL
, "KEYMAT compute with\n");
466 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
469 res
= oakley_prf(iph2
->ph1
->skeyid_d
, buf
, iph2
->ph1
);
473 /* compute key length needed */
474 encklen
= authklen
= 0;
475 switch (pr
->proto_id
) {
476 case IPSECDOI_PROTO_IPSEC_ESP
:
477 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
478 l
= alg_ipsec_encdef_keylen(tr
->trns_id
,
483 l
= alg_ipsec_hmacdef_hashlen(tr
->authtype
);
488 case IPSECDOI_PROTO_IPSEC_AH
:
489 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
490 l
= alg_ipsec_hmacdef_hashlen(tr
->trns_id
);
498 plog(LLV_DEBUG
, LOCATION
, NULL
, "encklen=%d authklen=%d\n",
501 dupkeymat
= (encklen
+ authklen
) / 8 / res
->l
;
502 dupkeymat
+= 2; /* safety mergin */
505 plog(LLV_DEBUG
, LOCATION
, NULL
,
506 "generating %d bits of key (dupkeymat=%d)\n",
507 dupkeymat
* 8 * res
->l
, dupkeymat
);
508 if (0 < --dupkeymat
) {
509 vchar_t
*prev
= res
; /* K(n-1) */
510 vchar_t
*seed
= NULL
; /* seed for Kn */
514 * generating long key (isakmp-oakley-08 5.5)
515 * KEYMAT = K1 | K2 | K3 | ...
517 * src = [ g(qm)^xy | ] protocol | SPI | Ni_b | Nr_b
518 * K1 = prf(SKEYID_d, src)
519 * K2 = prf(SKEYID_d, K1 | src)
520 * K3 = prf(SKEYID_d, K2 | src)
521 * Kn = prf(SKEYID_d, K(n-1) | src)
523 plog(LLV_DEBUG
, LOCATION
, NULL
,
524 "generating K1...K%d for KEYMAT.\n",
527 seed
= vmalloc(prev
->l
+ buf
->l
);
529 plog(LLV_ERROR
, LOCATION
, NULL
,
530 "failed to get keymat buffer.\n");
531 if (prev
&& prev
!= res
)
536 while (dupkeymat
--) {
537 vchar_t
*this = NULL
; /* Kn */
539 memcpy(seed
->v
, prev
->v
, prev
->l
);
540 memcpy(seed
->v
+ prev
->l
, buf
->v
, buf
->l
);
541 this = oakley_prf(iph2
->ph1
->skeyid_d
, seed
,
544 plog(LLV_ERROR
, LOCATION
, NULL
,
545 "oakley_prf memory overflow\n");
546 if (prev
&& prev
!= res
)
554 res
= vrealloc(res
, l
+ this->l
);
556 plog(LLV_ERROR
, LOCATION
, NULL
,
557 "failed to get keymat buffer.\n");
558 if (prev
&& prev
!= res
)
564 memcpy(res
->v
+ l
, this->v
, this->l
);
566 if (prev
&& prev
!= res
)
572 if (prev
&& prev
!= res
)
577 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
579 if (sa_dir
== INBOUND_SA
)
590 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
612 * NOTE: Must terminate by NULL.
615 oakley_compute_hashx(struct ph1handle
*iph1
, ...)
624 /* get buffer length */
627 while ((s
= va_arg(ap
, vchar_t
*)) != NULL
) {
634 plog(LLV_ERROR
, LOCATION
, NULL
,
635 "failed to get hash buffer\n");
642 while ((s
= va_arg(ap
, char *)) != NULL
) {
643 memcpy(p
, s
->v
, s
->l
);
648 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with: \n");
649 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
652 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
657 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
658 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
665 * compute HASH(3) prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b)
666 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
669 oakley_compute_hash3(iph1
, msgid
, body
)
670 struct ph1handle
*iph1
;
674 vchar_t
*buf
= 0, *res
= 0;
679 len
= 1 + sizeof(u_int32_t
) + body
->l
;
682 plog(LLV_DEBUG
, LOCATION
, NULL
,
683 "failed to get hash buffer\n");
689 memcpy(buf
->v
+ 1, (char *)&msgid
, sizeof(msgid
));
691 memcpy(buf
->v
+ 1 + sizeof(u_int32_t
), body
->v
, body
->l
);
693 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with: \n");
694 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
697 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
703 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
704 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
713 * compute HASH type of prf(SKEYID_a, M-ID | buffer)
715 * for quick mode HASH(1):
716 * prf(SKEYID_a, M-ID | SA | Ni [ | KE ] [ | IDci | IDcr ])
717 * for quick mode HASH(2):
718 * prf(SKEYID_a, M-ID | Ni_b | SA | Nr [ | KE ] [ | IDci | IDcr ])
719 * for Informational exchange:
720 * prf(SKEYID_a, M-ID | N/D)
723 oakley_compute_hash1(iph1
, msgid
, body
)
724 struct ph1handle
*iph1
;
728 vchar_t
*buf
= NULL
, *res
= NULL
;
734 len
= sizeof(u_int32_t
) + body
->l
;
737 plog(LLV_DEBUG
, LOCATION
, NULL
,
738 "failed to get hash buffer\n");
744 memcpy(buf
->v
, (char *)&msgid
, sizeof(msgid
));
745 p
+= sizeof(u_int32_t
);
747 memcpy(p
, body
->v
, body
->l
);
749 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with:\n");
750 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
753 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
759 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
760 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
769 * compute phase1 HASH
771 * I-digest = prf(SKEYID, g^i | g^r | CKY-I | CKY-R | SAi_b | ID_i1_b)
772 * R-digest = prf(SKEYID, g^r | g^i | CKY-R | CKY-I | SAi_b | ID_r1_b)
773 * for gssapi, also include all GSS tokens, and call gss_wrap on the result
776 oakley_ph1hash_common(iph1
, sw
)
777 struct ph1handle
*iph1
;
780 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
785 vchar_t
*gsstokens
= NULL
;
791 + sizeof(cookie_t
) * 2
793 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
796 if (iph1
->approval
->authmethod
== OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
797 if (iph1
->gi_i
!= NULL
&& iph1
->gi_r
!= NULL
) {
798 bp
= (sw
== GENERATE
? iph1
->gi_i
: iph1
->gi_r
);
802 gssapi_get_itokens(iph1
, &gsstokens
);
804 gssapi_get_rtokens(iph1
, &gsstokens
);
805 if (gsstokens
== NULL
)
813 plog(LLV_ERROR
, LOCATION
, NULL
,
814 "failed to get hash buffer\n");
820 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
821 memcpy(p
, bp
->v
, bp
->l
);
824 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
825 memcpy(p
, bp
->v
, bp
->l
);
828 if (iph1
->side
== INITIATOR
)
829 bp2
= (sw
== GENERATE
?
830 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
832 bp2
= (sw
== GENERATE
?
833 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
834 bl
= sizeof(cookie_t
);
838 if (iph1
->side
== INITIATOR
)
839 bp2
= (sw
== GENERATE
?
840 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
842 bp2
= (sw
== GENERATE
?
843 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
844 bl
= sizeof(cookie_t
);
849 memcpy(p
, bp
->v
, bp
->l
);
852 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
853 memcpy(p
, bp
->v
, bp
->l
);
857 if (iph1
->approval
->authmethod
== OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
858 if (iph1
->gi_i
!= NULL
&& iph1
->gi_r
!= NULL
) {
859 bp
= (sw
== GENERATE
? iph1
->gi_i
: iph1
->gi_r
);
860 memcpy(p
, bp
->v
, bp
->l
);
863 memcpy(p
, gsstokens
->v
, gsstokens
->l
);
868 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with:\n");
869 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
872 res
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
878 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
879 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
885 if (gsstokens
!= NULL
)
892 * compute HASH_I on base mode.
894 * HASH_I = prf(SKEYID, g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
896 * HASH_I = prf(hash(Ni_b | Nr_b), g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
899 oakley_ph1hash_base_i(iph1
, sw
)
900 struct ph1handle
*iph1
;
903 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
904 vchar_t
*hashkey
= NULL
;
905 vchar_t
*hash
= NULL
; /* for signature mode */
911 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
912 plog(LLV_ERROR
, LOCATION
, NULL
,
913 "invalid etype for this hash function\n");
917 switch (iph1
->approval
->authmethod
) {
918 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
919 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
920 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
921 if (iph1
->skeyid
== NULL
) {
922 plog(LLV_ERROR
, LOCATION
, NULL
, "no SKEYID found.\n");
925 hashkey
= iph1
->skeyid
;
928 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
929 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
930 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
931 /* make hash for seed */
932 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
935 plog(LLV_ERROR
, LOCATION
, NULL
,
936 "failed to get hash buffer\n");
941 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
942 memcpy(p
, bp
->v
, bp
->l
);
945 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
946 memcpy(p
, bp
->v
, bp
->l
);
949 hash
= oakley_hash(buf
, iph1
);
959 plog(LLV_ERROR
, LOCATION
, NULL
,
960 "not supported authentication method %d\n",
961 iph1
->approval
->authmethod
);
966 len
= (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
967 + sizeof(cookie_t
) * 2
969 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
972 plog(LLV_ERROR
, LOCATION
, NULL
,
973 "failed to get hash buffer\n");
978 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
979 memcpy(p
, bp
->v
, bp
->l
);
982 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
983 p
+= sizeof(cookie_t
);
984 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
985 p
+= sizeof(cookie_t
);
987 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
990 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
991 memcpy(p
, bp
->v
, bp
->l
);
994 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH_I with:\n");
995 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
998 res
= oakley_prf(hashkey
, buf
, iph1
);
1004 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH_I computed:\n");
1005 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
1016 * compute HASH_R on base mode for signature method.
1018 * HASH_R = prf(hash(Ni_b | Nr_b), g^xi | g^xr | CKY-I | CKY-R | SAi_b | IDii_b)
1021 oakley_ph1hash_base_r(iph1
, sw
)
1022 struct ph1handle
*iph1
;
1025 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
1026 vchar_t
*hash
= NULL
;
1032 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
1033 plog(LLV_ERROR
, LOCATION
, NULL
,
1034 "invalid etype for this hash function\n");
1037 if (iph1
->approval
->authmethod
!= OAKLEY_ATTR_AUTH_METHOD_DSSSIG
1038 && iph1
->approval
->authmethod
!= OAKLEY_ATTR_AUTH_METHOD_RSASIG
) {
1039 plog(LLV_ERROR
, LOCATION
, NULL
,
1040 "not supported authentication method %d\n",
1041 iph1
->approval
->authmethod
);
1045 /* make hash for seed */
1046 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
1049 plog(LLV_ERROR
, LOCATION
, NULL
,
1050 "failed to get hash buffer\n");
1055 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
1056 memcpy(p
, bp
->v
, bp
->l
);
1059 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
1060 memcpy(p
, bp
->v
, bp
->l
);
1063 hash
= oakley_hash(buf
, iph1
);
1069 /* make really hash */
1070 len
= (sw
== GENERATE
? iph1
->dhpub_p
->l
: iph1
->dhpub
->l
)
1071 + (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
1072 + sizeof(cookie_t
) * 2
1074 + (sw
== GENERATE
? iph1
->id_p
->l
: iph1
->id
->l
);
1077 plog(LLV_ERROR
, LOCATION
, NULL
,
1078 "failed to get hash buffer\n");
1084 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
1085 memcpy(p
, bp
->v
, bp
->l
);
1088 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
1089 memcpy(p
, bp
->v
, bp
->l
);
1092 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1093 p
+= sizeof(cookie_t
);
1094 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1095 p
+= sizeof(cookie_t
);
1097 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
1100 bp
= (sw
== GENERATE
? iph1
->id_p
: iph1
->id
);
1101 memcpy(p
, bp
->v
, bp
->l
);
1104 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH with:\n");
1105 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
1108 res
= oakley_prf(hash
, buf
, iph1
);
1114 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH computed:\n");
1115 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
1126 * compute each authentication method in phase 1.
1130 * other: error to be reply with notification.
1131 * the value is notification type.
1134 oakley_validate_auth(iph1
)
1135 struct ph1handle
*iph1
;
1137 vchar_t
*my_hash
= NULL
;
1140 vchar_t
*gsshash
= NULL
;
1143 struct timeval start
, end
;
1147 gettimeofday(&start
, NULL
);
1149 switch (iph1
->approval
->authmethod
) {
1150 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1155 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1156 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1157 "few isakmp message received.\n");
1158 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1161 r_hash
= (caddr_t
)(iph1
->pl_hash
+ 1);
1163 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH received:");
1164 plogdump(LLV_DEBUG
, r_hash
,
1165 ntohs(iph1
->pl_hash
->h
.len
) - sizeof(*iph1
->pl_hash
));
1167 switch (iph1
->etype
) {
1168 case ISAKMP_ETYPE_IDENT
:
1169 case ISAKMP_ETYPE_AGG
:
1170 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1172 case ISAKMP_ETYPE_BASE
:
1173 if (iph1
->side
== INITIATOR
)
1174 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1176 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1179 plog(LLV_ERROR
, LOCATION
, NULL
,
1180 "invalid etype %d\n", iph1
->etype
);
1181 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1183 if (my_hash
== NULL
)
1184 return ISAKMP_INTERNAL_ERROR
;
1186 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1190 plog(LLV_ERROR
, LOCATION
, NULL
, "HASH mismatched\n");
1191 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1194 plog(LLV_DEBUG
, LOCATION
, NULL
, "HASH for PSK validated.\n");
1197 #ifdef HAVE_SIGNING_C
1198 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1199 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1204 if (iph1
->id_p
== NULL
) {
1205 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1206 "no ID payload was passed.\n");
1207 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1209 if (iph1
->sig_p
== NULL
) {
1210 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1211 "no SIG payload was passed.\n");
1212 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1215 plog(LLV_DEBUG
, LOCATION
, NULL
, "SIGN passed:\n");
1216 plogdump(LLV_DEBUG
, iph1
->sig_p
->v
, iph1
->sig_p
->l
);
1218 /* get peer's cert */
1219 switch (iph1
->rmconf
->getcert_method
) {
1220 case ISAKMP_GETCERT_PAYLOAD
:
1221 if (iph1
->cert_p
== NULL
) {
1222 plog(LLV_ERROR
, LOCATION
, NULL
,
1223 "no peer's CERT payload found.\n");
1224 return ISAKMP_INTERNAL_ERROR
;
1227 case ISAKMP_GETCERT_LOCALFILE
:
1228 if (iph1
->rmconf
->peerscertfile
== NULL
) {
1229 plog(LLV_ERROR
, LOCATION
, NULL
,
1230 "no peer's CERT file found.\n");
1231 return ISAKMP_INTERNAL_ERROR
;
1234 /* don't use cached cert */
1235 if (iph1
->cert_p
!= NULL
) {
1236 oakley_delcert(iph1
->cert_p
);
1237 iph1
->cert_p
= NULL
;
1240 error
= get_cert_fromlocal(iph1
, 0);
1242 return ISAKMP_INTERNAL_ERROR
;
1244 case ISAKMP_GETCERT_DNS
:
1245 if (iph1
->rmconf
->peerscertfile
!= NULL
) {
1246 plog(LLV_ERROR
, LOCATION
, NULL
,
1247 "why peer's CERT file is defined "
1248 "though getcert method is dns ?\n");
1249 return ISAKMP_INTERNAL_ERROR
;
1252 /* don't use cached cert */
1253 if (iph1
->cert_p
!= NULL
) {
1254 oakley_delcert(iph1
->cert_p
);
1255 iph1
->cert_p
= NULL
;
1258 iph1
->cert_p
= dnssec_getcert(iph1
->id_p
);
1259 if (iph1
->cert_p
== NULL
) {
1260 plog(LLV_ERROR
, LOCATION
, NULL
,
1261 "no CERT RR found.\n");
1262 return ISAKMP_INTERNAL_ERROR
;
1266 plog(LLV_ERROR
, LOCATION
, NULL
,
1267 "invalid getcert_mothod: %d\n",
1268 iph1
->rmconf
->getcert_method
);
1269 return ISAKMP_INTERNAL_ERROR
;
1272 /* compare ID payload and certificate name */
1273 if (iph1
->rmconf
->verify_cert
&&
1274 (error
= oakley_check_certid(iph1
)) != 0)
1277 /* verify certificate */
1278 if (iph1
->rmconf
->verify_cert
1279 && iph1
->rmconf
->getcert_method
== ISAKMP_GETCERT_PAYLOAD
) {
1280 switch (iph1
->rmconf
->certtype
) {
1281 case ISAKMP_CERT_X509SIGN
:
1282 error
= eay_check_x509cert(&iph1
->cert_p
->cert
,
1283 lcconf
->pathinfo
[LC_PATHTYPE_CERT
]);
1286 plog(LLV_ERROR
, LOCATION
, NULL
,
1287 "no supported certtype %d\n",
1288 iph1
->rmconf
->certtype
);
1289 return ISAKMP_INTERNAL_ERROR
;
1292 plog(LLV_ERROR
, LOCATION
, NULL
,
1293 "the peer's certificate is not verified.\n");
1294 return ISAKMP_NTYPE_INVALID_CERT_AUTHORITY
;
1298 plog(LLV_DEBUG
, LOCATION
, NULL
, "CERT validated\n");
1301 switch (iph1
->etype
) {
1302 case ISAKMP_ETYPE_IDENT
:
1303 case ISAKMP_ETYPE_AGG
:
1304 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1306 case ISAKMP_ETYPE_BASE
:
1307 if (iph1
->side
== INITIATOR
)
1308 my_hash
= oakley_ph1hash_base_r(iph1
, VALIDATE
);
1310 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1313 plog(LLV_ERROR
, LOCATION
, NULL
,
1314 "invalid etype %d\n", iph1
->etype
);
1315 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1317 if (my_hash
== NULL
)
1318 return ISAKMP_INTERNAL_ERROR
;
1320 /* check signature */
1321 switch (iph1
->rmconf
->certtype
) {
1322 case ISAKMP_CERT_X509SIGN
:
1323 case ISAKMP_CERT_DNS
:
1324 error
= eay_check_x509sign(my_hash
,
1326 &iph1
->cert_p
->cert
);
1329 plog(LLV_ERROR
, LOCATION
, NULL
,
1330 "no supported certtype %d\n",
1331 iph1
->rmconf
->certtype
);
1333 return ISAKMP_INTERNAL_ERROR
;
1338 plog(LLV_ERROR
, LOCATION
, NULL
,
1340 return ISAKMP_NTYPE_INVALID_SIGNATURE
;
1342 plog(LLV_DEBUG
, LOCATION
, NULL
, "SIG authenticated\n");
1347 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1348 switch (iph1
->etype
) {
1349 case ISAKMP_ETYPE_IDENT
:
1350 case ISAKMP_ETYPE_AGG
:
1351 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1354 plog(LLV_ERROR
, LOCATION
, NULL
,
1355 "invalid etype %d\n", iph1
->etype
);
1356 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1359 if (my_hash
== NULL
) {
1360 if (gssapi_more_tokens(iph1
))
1361 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1363 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1366 gsshash
= gssapi_unwraphash(iph1
);
1367 if (gsshash
== NULL
) {
1369 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1372 result
= memcmp(my_hash
->v
, gsshash
->v
, my_hash
->l
);
1377 plog(LLV_ERROR
, LOCATION
, NULL
, "HASH mismatched\n");
1378 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1380 plog(LLV_DEBUG
, LOCATION
, NULL
, "hash compared OK\n");
1383 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1384 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1385 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1386 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1387 "few isakmp message received.\n");
1388 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1390 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1391 "not supported authmethod type %s\n",
1392 s_oakley_attr_method(iph1
->approval
->authmethod
));
1393 return ISAKMP_INTERNAL_ERROR
;
1395 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1396 "invalid authmethod %d why ?\n",
1397 iph1
->approval
->authmethod
);
1398 return ISAKMP_INTERNAL_ERROR
;
1401 gettimeofday(&end
, NULL
);
1402 syslog(LOG_NOTICE
, "%s(%s): %8.6f", __func__
,
1403 s_oakley_attr_method(iph1
->approval
->authmethod
),
1404 timedelta(&start
, &end
));
1410 #ifdef HAVE_SIGNING_C
1411 /* get my certificate
1412 * NOTE: include certificate type.
1415 oakley_getmycert(iph1
)
1416 struct ph1handle
*iph1
;
1419 return 0; /* There is CERT. */
1421 return get_cert_fromlocal(iph1
, 1);
1425 * get a CERT from local file.
1428 * my == 0 peer's cert.
1431 get_cert_fromlocal(iph1
, my
)
1432 struct ph1handle
*iph1
;
1435 char path
[MAXPATHLEN
];
1436 vchar_t
*cert
= NULL
;
1442 certfile
= iph1
->rmconf
->mycertfile
;
1443 certpl
= &iph1
->cert
;
1445 certfile
= iph1
->rmconf
->peerscertfile
;
1446 certpl
= &iph1
->cert_p
;
1449 plog(LLV_ERROR
, LOCATION
, NULL
, "no CERT defined.\n");
1453 switch (iph1
->rmconf
->certtype
) {
1454 case ISAKMP_CERT_X509SIGN
:
1455 case ISAKMP_CERT_DNS
:
1456 /* make public file name */
1457 getpathname(path
, sizeof(path
), LC_PATHTYPE_CERT
, certfile
);
1458 cert
= eay_get_x509cert(path
);
1461 p
= eay_get_x509text(cert
);
1462 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s", p
? p
: "\n");
1468 plog(LLV_ERROR
, LOCATION
, NULL
,
1469 "not supported certtype %d\n",
1470 iph1
->rmconf
->certtype
);
1475 plog(LLV_ERROR
, LOCATION
, NULL
,
1476 "failed to get %s CERT.\n",
1477 my
? "my" : "peers");
1481 *certpl
= oakley_newcert();
1483 plog(LLV_ERROR
, LOCATION
, NULL
,
1484 "failed to get cert buffer.\n");
1487 (*certpl
)->pl
= vmalloc(cert
->l
+ 1);
1488 if ((*certpl
)->pl
== NULL
) {
1489 plog(LLV_ERROR
, LOCATION
, NULL
,
1490 "failed to get cert buffer\n");
1491 oakley_delcert(*certpl
);
1495 memcpy((*certpl
)->pl
->v
+ 1, cert
->v
, cert
->l
);
1496 (*certpl
)->pl
->v
[0] = iph1
->rmconf
->certtype
;
1497 (*certpl
)->type
= iph1
->rmconf
->certtype
;
1498 (*certpl
)->cert
.v
= (*certpl
)->pl
->v
+ 1;
1499 (*certpl
)->cert
.l
= (*certpl
)->pl
->l
- 1;
1501 plog(LLV_DEBUG
, LOCATION
, NULL
, "created CERT payload:\n");
1502 plogdump(LLV_DEBUG
, (*certpl
)->pl
->v
, (*certpl
)->pl
->l
);
1515 oakley_getsign(iph1
)
1516 struct ph1handle
*iph1
;
1518 char path
[MAXPATHLEN
];
1519 vchar_t
*privkey
= NULL
;
1522 switch (iph1
->rmconf
->certtype
) {
1523 case ISAKMP_CERT_X509SIGN
:
1524 case ISAKMP_CERT_DNS
:
1525 if (iph1
->rmconf
->myprivfile
== NULL
) {
1526 plog(LLV_ERROR
, LOCATION
, NULL
, "no cert defined.\n");
1530 /* make private file name */
1531 getpathname(path
, sizeof(path
),
1533 iph1
->rmconf
->myprivfile
);
1534 privkey
= eay_get_pkcs1privkey(path
);
1535 if (privkey
== NULL
) {
1536 plog(LLV_ERROR
, LOCATION
, NULL
,
1537 "failed to get private key.\n");
1540 plog(LLV_DEBUG2
, LOCATION
, NULL
, "private key:\n");
1541 plogdump(LLV_DEBUG2
, privkey
->v
, privkey
->l
);
1543 iph1
->sig
= eay_get_x509sign(iph1
->hash
,
1544 privkey
, &iph1
->cert
->cert
);
1550 if (iph1
->sig
== NULL
) {
1551 plog(LLV_ERROR
, LOCATION
, NULL
, "failed to sign.\n");
1555 plog(LLV_DEBUG
, LOCATION
, NULL
, "SIGN computed:\n");
1556 plogdump(LLV_DEBUG
, iph1
->sig
->v
, iph1
->sig
->l
);
1561 if (privkey
!= NULL
)
1568 * compare certificate name and ID value.
1571 oakley_check_certid(iph1
)
1572 struct ph1handle
*iph1
;
1574 struct ipsecdoi_id_b
*id_b
;
1575 vchar_t
*name
= NULL
;
1576 char *altname
= NULL
;
1580 if (iph1
->id_p
== NULL
|| iph1
->cert_p
== NULL
) {
1581 plog(LLV_ERROR
, LOCATION
, NULL
, "no ID nor CERT found.\n");
1582 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1585 id_b
= (struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
1586 idlen
= iph1
->id_p
->l
- sizeof(*id_b
);
1588 switch (id_b
->type
) {
1589 case IPSECDOI_ID_DER_ASN1_DN
:
1590 name
= eay_get_x509asn1subjectname(&iph1
->cert_p
->cert
);
1592 plog(LLV_ERROR
, LOCATION
, NULL
,
1593 "failed to get subjectName\n");
1594 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1596 if (idlen
!= name
->l
) {
1597 plog(LLV_ERROR
, LOCATION
, NULL
,
1598 "Invalid ID length in phase 1.\n");
1600 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1602 error
= memcmp(id_b
+ 1, name
->v
, idlen
);
1605 plog(LLV_ERROR
, LOCATION
, NULL
,
1606 "ID mismatched with subjectAltName.\n");
1607 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1610 case IPSECDOI_ID_IPV4_ADDR
:
1611 case IPSECDOI_ID_IPV6_ADDR
:
1614 * converting to binary from string because openssl return
1615 * a string even if object is a binary.
1616 * XXX fix it ! access by ASN.1 directly without.
1618 struct addrinfo hints
, *res
;
1622 for (pos
= 1; ; pos
++) {
1623 if (eay_get_x509subjectaltname(&iph1
->cert_p
->cert
,
1624 &altname
, &type
, pos
) !=0) {
1625 plog(LLV_ERROR
, LOCATION
, NULL
,
1626 "failed to get subjectAltName\n");
1627 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1630 /* it's the end condition of the loop. */
1632 plog(LLV_ERROR
, LOCATION
, NULL
,
1633 "no proper subjectAltName.\n");
1634 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1637 if (check_typeofcertname(id_b
->type
, type
) == 0)
1641 racoon_free(altname
);
1644 memset(&hints
, 0, sizeof(hints
));
1645 hints
.ai_family
= PF_UNSPEC
;
1646 hints
.ai_socktype
= SOCK_RAW
;
1647 hints
.ai_flags
= AI_NUMERICHOST
;
1648 error
= getaddrinfo(altname
, NULL
, &hints
, &res
);
1650 plog(LLV_ERROR
, LOCATION
, NULL
,
1651 "no proper subjectAltName.\n");
1652 racoon_free(altname
);
1653 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1655 switch (res
->ai_family
) {
1657 a
= (caddr_t
)&((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
;
1661 a
= (caddr_t
)&((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
.s6_addr
;
1665 plog(LLV_ERROR
, LOCATION
, NULL
,
1666 "family not supported: %d.\n", res
->ai_family
);
1667 racoon_free(altname
);
1669 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1671 error
= memcmp(id_b
+ 1, a
, idlen
);
1675 plog(LLV_ERROR
, LOCATION
, NULL
,
1676 "ID mismatched with subjectAltName.\n");
1677 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1681 case IPSECDOI_ID_FQDN
:
1682 case IPSECDOI_ID_USER_FQDN
:
1686 for (pos
= 1; ; pos
++) {
1687 if (eay_get_x509subjectaltname(&iph1
->cert_p
->cert
,
1688 &altname
, &type
, pos
) != 0){
1689 plog(LLV_ERROR
, LOCATION
, NULL
,
1690 "failed to get subjectAltName\n");
1691 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1694 /* it's the end condition of the loop. */
1696 plog(LLV_ERROR
, LOCATION
, NULL
,
1697 "no proper subjectAltName.\n");
1698 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1701 if (check_typeofcertname(id_b
->type
, type
) == 0)
1705 racoon_free(altname
);
1708 if (idlen
!= strlen(altname
)) {
1709 plog(LLV_ERROR
, LOCATION
, NULL
,
1710 "Invalid ID length in phase 1.\n");
1711 racoon_free(altname
);
1712 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1714 if (check_typeofcertname(id_b
->type
, type
) != 0) {
1715 plog(LLV_ERROR
, LOCATION
, NULL
,
1716 "ID type mismatched. ID: %s CERT: %s.\n",
1717 s_ipsecdoi_ident(id_b
->type
),
1718 s_ipsecdoi_ident(type
));
1719 racoon_free(altname
);
1720 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1722 error
= memcmp(id_b
+ 1, altname
, idlen
);
1724 plog(LLV_ERROR
, LOCATION
, NULL
, "ID mismatched.\n");
1725 racoon_free(altname
);
1726 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1728 racoon_free(altname
);
1732 plog(LLV_ERROR
, LOCATION
, NULL
,
1733 "Inpropper ID type passed: %s.\n",
1734 s_ipsecdoi_ident(id_b
->type
));
1735 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1741 check_typeofcertname(doi
, genid
)
1745 case IPSECDOI_ID_IPV4_ADDR
:
1746 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
1747 case IPSECDOI_ID_IPV6_ADDR
:
1748 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
1749 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
1750 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
1751 if (genid
!= GENT_IPADD
)
1754 case IPSECDOI_ID_FQDN
:
1755 if (genid
!= GENT_DNS
)
1758 case IPSECDOI_ID_USER_FQDN
:
1759 if (genid
!= GENT_EMAIL
)
1762 case IPSECDOI_ID_DER_ASN1_DN
: /* should not be passed to this function*/
1763 case IPSECDOI_ID_DER_ASN1_GN
:
1764 case IPSECDOI_ID_KEY_ID
:
1772 * save certificate including certificate type.
1775 oakley_savecert(iph1
, gen
)
1776 struct ph1handle
*iph1
;
1777 struct isakmp_gen
*gen
;
1782 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
1785 case ISAKMP_CERT_DNS
:
1786 plog(LLV_WARNING
, LOCATION
, NULL
,
1787 "CERT payload is unnecessary in DNSSEC. "
1788 "ignore this CERT payload.\n");
1790 case ISAKMP_CERT_PKCS7
:
1791 case ISAKMP_CERT_PGP
:
1792 case ISAKMP_CERT_X509SIGN
:
1793 case ISAKMP_CERT_KERBEROS
:
1794 case ISAKMP_CERT_SPKI
:
1797 case ISAKMP_CERT_CRL
:
1800 case ISAKMP_CERT_X509KE
:
1801 case ISAKMP_CERT_X509ATTR
:
1802 case ISAKMP_CERT_ARL
:
1803 plog(LLV_ERROR
, LOCATION
, NULL
,
1804 "No supported such CERT type %d\n", type
);
1807 plog(LLV_ERROR
, LOCATION
, NULL
,
1808 "Invalid CERT type %d\n", type
);
1812 /* XXX choice the 1th cert, ignore after the cert. */
1813 /* XXX should be processed. */
1815 plog(LLV_WARNING
, LOCATION
, NULL
,
1816 "ignore 2nd CERT payload.\n");
1820 *c
= save_certbuf(gen
);
1822 plog(LLV_ERROR
, LOCATION
, NULL
,
1823 "Failed to get CERT buffer.\n");
1827 switch ((*c
)->type
) {
1828 case ISAKMP_CERT_DNS
:
1829 plog(LLV_WARNING
, LOCATION
, NULL
,
1830 "CERT payload is unnecessary in DNSSEC. "
1833 case ISAKMP_CERT_PKCS7
:
1834 case ISAKMP_CERT_PGP
:
1835 case ISAKMP_CERT_X509SIGN
:
1836 case ISAKMP_CERT_KERBEROS
:
1837 case ISAKMP_CERT_SPKI
:
1838 plog(LLV_DEBUG
, LOCATION
, NULL
, "CERT saved:\n");
1839 plogdump(LLV_DEBUG
, (*c
)->cert
.v
, (*c
)->cert
.l
);
1841 char *p
= eay_get_x509text(&(*c
)->cert
);
1842 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s", p
? p
: "\n");
1846 case ISAKMP_CERT_CRL
:
1847 plog(LLV_DEBUG
, LOCATION
, NULL
, "CRL saved:\n");
1848 plogdump(LLV_DEBUG
, (*c
)->cert
.v
, (*c
)->cert
.l
);
1850 case ISAKMP_CERT_X509KE
:
1851 case ISAKMP_CERT_X509ATTR
:
1852 case ISAKMP_CERT_ARL
:
1855 oakley_delcert((*c
));
1864 * save certificate including certificate type.
1867 oakley_savecr(iph1
, gen
)
1868 struct ph1handle
*iph1
;
1869 struct isakmp_gen
*gen
;
1874 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
1877 case ISAKMP_CERT_DNS
:
1878 plog(LLV_WARNING
, LOCATION
, NULL
,
1879 "CERT payload is unnecessary in DNSSEC\n");
1881 case ISAKMP_CERT_PKCS7
:
1882 case ISAKMP_CERT_PGP
:
1883 case ISAKMP_CERT_X509SIGN
:
1884 case ISAKMP_CERT_KERBEROS
:
1885 case ISAKMP_CERT_SPKI
:
1888 case ISAKMP_CERT_X509KE
:
1889 case ISAKMP_CERT_X509ATTR
:
1890 case ISAKMP_CERT_ARL
:
1891 plog(LLV_ERROR
, LOCATION
, NULL
,
1892 "No supported such CR type %d\n", type
);
1894 case ISAKMP_CERT_CRL
:
1896 plog(LLV_ERROR
, LOCATION
, NULL
,
1897 "Invalid CR type %d\n", type
);
1901 *c
= save_certbuf(gen
);
1903 plog(LLV_ERROR
, LOCATION
, NULL
,
1904 "Failed to get CR buffer.\n");
1908 plog(LLV_DEBUG
, LOCATION
, NULL
, "CR saved:\n");
1909 plogdump(LLV_DEBUG
, (*c
)->cert
.v
, (*c
)->cert
.l
);
1916 struct isakmp_gen
*gen
;
1920 new = oakley_newcert();
1922 plog(LLV_ERROR
, LOCATION
, NULL
,
1923 "Failed to get CERT buffer.\n");
1927 new->pl
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
1928 if (new->pl
== NULL
) {
1929 plog(LLV_ERROR
, LOCATION
, NULL
,
1930 "Failed to copy CERT from packet.\n");
1931 oakley_delcert(new);
1935 memcpy(new->pl
->v
, gen
+ 1, new->pl
->l
);
1936 new->type
= new->pl
->v
[0] & 0xff;
1937 new->cert
.v
= new->pl
->v
+ 1;
1938 new->cert
.l
= new->pl
->l
- 1;
1945 * NOTE: No Certificate Authority field is included to CR payload at the
1946 * moment. Becuase any certificate authority are accepted without any check.
1947 * The section 3.10 in RFC2408 says that this field SHOULD not be included,
1948 * if there is no specific certificate authority requested.
1952 struct ph1handle
*iph1
;
1958 plog(LLV_ERROR
, LOCATION
, NULL
,
1959 "failed to get cr buffer\n");
1962 buf
->v
[0] = iph1
->rmconf
->certtype
;
1964 plog(LLV_DEBUG
, LOCATION
, NULL
, "create my CR: %s\n",
1965 s_isakmp_certtype(iph1
->rmconf
->certtype
));
1967 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
1976 oakley_checkcr(iph1
)
1977 struct ph1handle
*iph1
;
1979 if (iph1
->cr_p
== NULL
)
1982 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
1983 "peer transmitted CR: %s\n",
1984 s_isakmp_certtype(iph1
->cr_p
->type
));
1986 if (iph1
->cr_p
->type
!= iph1
->rmconf
->certtype
) {
1987 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1988 "such a cert type isn't supported: %d\n",
1989 (char)iph1
->cr_p
->type
);
1997 * check to need CR payload.
2004 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
2005 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2012 #endif /*HAVE_SIGNING_C*/
2016 * see seciton 5. Exchanges in RFC 2409
2017 * psk: SKEYID = prf(pre-shared-key, Ni_b | Nr_b)
2018 * sig: SKEYID = prf(Ni_b | Nr_b, g^ir)
2019 * enc: SKEYID = prf(H(Ni_b | Nr_b), CKY-I | CKY-R)
2023 struct ph1handle
*iph1
;
2025 vchar_t
*buf
= NULL
, *bp
;
2031 switch(iph1
->approval
->authmethod
) {
2032 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2033 /* if we have a preshared key defined, just use it */
2034 if (iph1
->rmconf
->shared_secret
) {
2036 switch (iph1
->rmconf
->secrettype
) {
2037 case SECRETTYPE_KEY
:
2038 iph1
->authstr
= getpsk(iph1
->rmconf
->shared_secret
->v
, iph1
->rmconf
->shared_secret
->l
-1);
2040 case SECRETTYPE_KEYCHAIN
:
2041 iph1
->authstr
= getpskfromkeychain(iph1
->rmconf
->shared_secret
->v
);
2043 case SECRETTYPE_USE
:
2045 iph1
->authstr
= vdup(iph1
->rmconf
->shared_secret
);
2049 else if (iph1
->etype
!= ISAKMP_ETYPE_IDENT
) {
2050 iph1
->authstr
= getpskbyname(iph1
->id_p
);
2051 if (iph1
->authstr
== NULL
) {
2052 if (iph1
->rmconf
->verify_identifier
) {
2053 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
2054 "couldn't find the pskey.\n");
2057 plog(LLV_NOTIFY
, LOCATION
, iph1
->remote
,
2058 "couldn't find the proper pskey, "
2059 "try to get one by the peer's address.\n");
2062 if (iph1
->authstr
== NULL
) {
2064 * If the exchange type is the main mode or if it's
2065 * failed to get the psk by ID, racoon try to get
2066 * the psk by remote IP address.
2067 * It may be nonsense.
2069 iph1
->authstr
= getpskbyaddr(iph1
->remote
);
2070 if (iph1
->authstr
== NULL
) {
2071 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
2072 "couldn't find the pskey for %s.\n",
2073 saddrwop2str(iph1
->remote
));
2077 plog(LLV_DEBUG
, LOCATION
, NULL
, "the psk found.\n");
2078 /* should be secret PSK */
2079 plog(LLV_DEBUG2
, LOCATION
, NULL
, "psk: ");
2080 plogdump(LLV_DEBUG2
, iph1
->authstr
->v
, iph1
->authstr
->l
);
2082 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2085 plog(LLV_ERROR
, LOCATION
, NULL
,
2086 "failed to get skeyid buffer\n");
2091 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2092 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce 1: ");
2093 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2094 memcpy(p
, bp
->v
, bp
->l
);
2097 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2098 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce 2: ");
2099 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2100 memcpy(p
, bp
->v
, bp
->l
);
2103 iph1
->skeyid
= oakley_prf(iph1
->authstr
, buf
, iph1
);
2104 if (iph1
->skeyid
== NULL
)
2108 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
2109 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2111 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
2113 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2116 plog(LLV_ERROR
, LOCATION
, NULL
,
2117 "failed to get nonce buffer\n");
2122 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2123 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce1: ");
2124 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2125 memcpy(p
, bp
->v
, bp
->l
);
2128 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2129 plog(LLV_DEBUG
, LOCATION
, NULL
, "nonce2: ");
2130 plogdump(LLV_DEBUG
, bp
->v
, bp
->l
);
2131 memcpy(p
, bp
->v
, bp
->l
);
2134 iph1
->skeyid
= oakley_prf(buf
, iph1
->dhgxy
, iph1
);
2135 if (iph1
->skeyid
== NULL
)
2138 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2139 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2140 plog(LLV_WARNING
, LOCATION
, NULL
,
2141 "not supported authentication method %s\n",
2142 s_oakley_attr_method(iph1
->approval
->authmethod
));
2145 plog(LLV_ERROR
, LOCATION
, NULL
,
2146 "invalid authentication method %d\n",
2147 iph1
->approval
->authmethod
);
2151 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID computed:\n");
2152 plogdump(LLV_DEBUG
, iph1
->skeyid
->v
, iph1
->skeyid
->l
);
2163 * compute SKEYID_[dae]
2164 * see seciton 5. Exchanges in RFC 2409
2165 * SKEYID_d = prf(SKEYID, g^ir | CKY-I | CKY-R | 0)
2166 * SKEYID_a = prf(SKEYID, SKEYID_d | g^ir | CKY-I | CKY-R | 1)
2167 * SKEYID_e = prf(SKEYID, SKEYID_a | g^ir | CKY-I | CKY-R | 2)
2170 oakley_skeyid_dae(iph1
)
2171 struct ph1handle
*iph1
;
2173 vchar_t
*buf
= NULL
;
2178 if (iph1
->skeyid
== NULL
) {
2179 plog(LLV_ERROR
, LOCATION
, NULL
, "no SKEYID found.\n");
2184 /* SKEYID_d = prf(SKEYID, g^xy | CKY-I | CKY-R | 0) */
2185 len
= iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2188 plog(LLV_ERROR
, LOCATION
, NULL
,
2189 "failed to get skeyid buffer\n");
2194 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2195 p
+= iph1
->dhgxy
->l
;
2196 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2197 p
+= sizeof(cookie_t
);
2198 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2199 p
+= sizeof(cookie_t
);
2201 iph1
->skeyid_d
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2202 if (iph1
->skeyid_d
== NULL
)
2208 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID_d computed:\n");
2209 plogdump(LLV_DEBUG
, iph1
->skeyid_d
->v
, iph1
->skeyid
->l
);
2212 /* SKEYID_a = prf(SKEYID, SKEYID_d | g^xy | CKY-I | CKY-R | 1) */
2213 len
= iph1
->skeyid_d
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2216 plog(LLV_ERROR
, LOCATION
, NULL
,
2217 "failed to get skeyid buffer\n");
2221 memcpy(p
, iph1
->skeyid_d
->v
, iph1
->skeyid_d
->l
);
2222 p
+= iph1
->skeyid_d
->l
;
2223 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2224 p
+= iph1
->dhgxy
->l
;
2225 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2226 p
+= sizeof(cookie_t
);
2227 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2228 p
+= sizeof(cookie_t
);
2230 iph1
->skeyid_a
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2231 if (iph1
->skeyid_a
== NULL
)
2237 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID_a computed:\n");
2238 plogdump(LLV_DEBUG
, iph1
->skeyid_a
->v
, iph1
->skeyid_a
->l
);
2241 /* SKEYID_e = prf(SKEYID, SKEYID_a | g^xy | CKY-I | CKY-R | 2) */
2242 len
= iph1
->skeyid_a
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2245 plog(LLV_ERROR
, LOCATION
, NULL
,
2246 "failed to get skeyid buffer\n");
2250 memcpy(p
, iph1
->skeyid_a
->v
, iph1
->skeyid_a
->l
);
2251 p
+= iph1
->skeyid_a
->l
;
2252 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2253 p
+= iph1
->dhgxy
->l
;
2254 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2255 p
+= sizeof(cookie_t
);
2256 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2257 p
+= sizeof(cookie_t
);
2259 iph1
->skeyid_e
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2260 if (iph1
->skeyid_e
== NULL
)
2266 plog(LLV_DEBUG
, LOCATION
, NULL
, "SKEYID_e computed:\n");
2267 plogdump(LLV_DEBUG
, iph1
->skeyid_e
->v
, iph1
->skeyid_e
->l
);
2278 * compute final encryption key.
2282 oakley_compute_enckey(iph1
)
2283 struct ph1handle
*iph1
;
2285 u_int keylen
, prflen
;
2289 keylen
= alg_oakley_encdef_keylen(iph1
->approval
->enctype
,
2290 iph1
->approval
->encklen
);
2292 plog(LLV_ERROR
, LOCATION
, NULL
,
2293 "invalid encryption algoritym %d, "
2294 "or invalid key length %d.\n",
2295 iph1
->approval
->enctype
,
2296 iph1
->approval
->encklen
);
2299 iph1
->key
= vmalloc(keylen
>> 3);
2300 if (iph1
->key
== NULL
) {
2301 plog(LLV_ERROR
, LOCATION
, NULL
,
2302 "failed to get key buffer\n");
2306 /* set prf length */
2307 prflen
= alg_oakley_hashdef_hashlen(iph1
->approval
->hashtype
);
2309 plog(LLV_ERROR
, LOCATION
, NULL
,
2310 "invalid hash type %d.\n", iph1
->approval
->hashtype
);
2314 /* see isakmp-oakley-08 5.3. */
2315 if (iph1
->key
->l
<= iph1
->skeyid_e
->l
) {
2317 * if length(Ka) <= length(SKEYID_e)
2318 * Ka = first length(K) bit of SKEYID_e
2320 memcpy(iph1
->key
->v
, iph1
->skeyid_e
->v
, iph1
->key
->l
);
2322 vchar_t
*buf
= NULL
, *res
= NULL
;
2331 * K1 = prf(SKEYID_e, 0)
2332 * K2 = prf(SKEYID_e, K1)
2333 * K3 = prf(SKEYID_e, K2)
2335 plog(LLV_DEBUG
, LOCATION
, NULL
,
2336 "len(SKEYID_e) < len(Ka) (%d < %d), "
2337 "generating long key (Ka = K1 | K2 | ...)\n",
2338 iph1
->skeyid_e
->l
, iph1
->key
->l
);
2340 if ((buf
= vmalloc(prflen
>> 3)) == 0) {
2341 plog(LLV_ERROR
, LOCATION
, NULL
,
2342 "failed to get key buffer\n");
2345 p
= (u_char
*)iph1
->key
->v
;
2346 ep
= p
+ iph1
->key
->l
;
2350 if (p
== (u_char
*)iph1
->key
->v
) {
2351 /* just for computing K1 */
2355 res
= oakley_prf(iph1
->skeyid_e
, buf
, iph1
);
2360 plog(LLV_DEBUG
, LOCATION
, NULL
,
2361 "compute intermediate encryption key K%d\n",
2363 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2364 plogdump(LLV_DEBUG
, res
->v
, res
->l
);
2366 cplen
= (res
->l
< ep
- p
) ? res
->l
: ep
- p
;
2367 memcpy(p
, res
->v
, cplen
);
2370 buf
->l
= prflen
>> 3; /* to cancel K1 speciality */
2371 if (res
->l
!= buf
->l
) {
2372 plog(LLV_ERROR
, LOCATION
, NULL
,
2373 "internal error: res->l=%d buf->l=%d\n",
2379 memcpy(buf
->v
, res
->v
, res
->l
);
2388 * don't check any weak key or not.
2389 * draft-ietf-ipsec-ike-01.txt Appendix B.
2390 * draft-ietf-ipsec-ciph-aes-cbc-00.txt Section 2.3.
2394 if (iph1
->approval
->enctype
> ARRAYLEN(oakley_encdef
)
2395 || oakley_encdef
[iph1
->approval
->enctype
].weakkey
== NULL
) {
2396 plog(LLV_ERROR
, LOCATION
, NULL
,
2397 "encryption algoritym %d isn't supported.\n",
2398 iph1
->approval
->enctype
);
2401 if ((oakley_encdef
[iph1
->approval
->enctype
].weakkey
)(iph1
->key
)) {
2402 plog(LLV_ERROR
, LOCATION
, NULL
,
2403 "weakkey was generated.\n");
2408 plog(LLV_DEBUG
, LOCATION
, NULL
, "final encryption key computed:\n");
2409 plogdump(LLV_DEBUG
, iph1
->key
->v
, iph1
->key
->l
);
2417 /* allocated new buffer for CERT */
2423 new = racoon_calloc(1, sizeof(*new));
2425 plog(LLV_ERROR
, LOCATION
, NULL
,
2426 "failed to get cert's buffer\n");
2435 /* delete buffer for CERT */
2437 oakley_delcert(cert
)
2448 * compute IV and set to ph1handle
2449 * IV = hash(g^xi | g^xr)
2450 * see 4.1 Phase 1 state in draft-ietf-ipsec-ike.
2454 struct ph1handle
*iph1
;
2456 struct isakmp_ivm
*newivm
= NULL
;
2457 vchar_t
*buf
= NULL
, *bp
;
2462 len
= iph1
->dhpub
->l
+ iph1
->dhpub_p
->l
;
2465 plog(LLV_ERROR
, LOCATION
, NULL
,
2466 "failed to get iv buffer\n");
2472 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub
: iph1
->dhpub_p
);
2473 memcpy(p
, bp
->v
, bp
->l
);
2476 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub_p
: iph1
->dhpub
);
2477 memcpy(p
, bp
->v
, bp
->l
);
2481 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
2482 if (newivm
== NULL
) {
2483 plog(LLV_ERROR
, LOCATION
, NULL
,
2484 "failed to get iv buffer\n");
2490 newivm
->iv
= oakley_hash(buf
, iph1
);
2491 if (newivm
->iv
== NULL
) {
2493 oakley_delivm(newivm
);
2497 /* adjust length of iv */
2498 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2499 if (newivm
->iv
->l
== -1) {
2500 plog(LLV_ERROR
, LOCATION
, NULL
,
2501 "invalid encryption algoriym %d.\n",
2502 iph1
->approval
->enctype
);
2504 oakley_delivm(newivm
);
2508 /* create buffer to save iv */
2509 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
2510 plog(LLV_ERROR
, LOCATION
, NULL
,
2511 "vdup (%s)\n", strerror(errno
));
2513 oakley_delivm(newivm
);
2519 plog(LLV_DEBUG
, LOCATION
, NULL
, "IV computed:\n");
2520 plogdump(LLV_DEBUG
, newivm
->iv
->v
, newivm
->iv
->l
);
2528 * compute IV for the payload after phase 1.
2529 * It's not limited for phase 2.
2530 * if pahse 1 was encrypted.
2531 * IV = hash(last CBC block of Phase 1 | M-ID)
2532 * if phase 1 was not encrypted.
2533 * IV = hash(phase 1 IV | M-ID)
2534 * see 4.2 Phase 2 state in draft-ietf-ipsec-ike.
2537 oakley_newiv2(iph1
, msgid
)
2538 struct ph1handle
*iph1
;
2541 struct isakmp_ivm
*newivm
= NULL
;
2542 vchar_t
*buf
= NULL
;
2548 len
= iph1
->ivm
->iv
->l
+ sizeof(msgid_t
);
2551 plog(LLV_ERROR
, LOCATION
, NULL
,
2552 "failed to get iv buffer\n");
2558 memcpy(p
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
2559 p
+= iph1
->ivm
->iv
->l
;
2561 memcpy(p
, &msgid
, sizeof(msgid
));
2563 plog(LLV_DEBUG
, LOCATION
, NULL
, "compute IV for phase2\n");
2564 plog(LLV_DEBUG
, LOCATION
, NULL
, "phase1 last IV:\n");
2565 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2568 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
2569 if (newivm
== NULL
) {
2570 plog(LLV_ERROR
, LOCATION
, NULL
,
2571 "failed to get iv buffer\n");
2576 if ((newivm
->iv
= oakley_hash(buf
, iph1
)) == NULL
)
2579 /* adjust length of iv */
2580 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2581 if (newivm
->iv
->l
== -1) {
2582 plog(LLV_ERROR
, LOCATION
, NULL
,
2583 "invalid encryption algoriym %d.\n",
2584 iph1
->approval
->enctype
);
2588 /* create buffer to save new iv */
2589 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
2590 plog(LLV_ERROR
, LOCATION
, NULL
, "vdup (%s)\n", strerror(errno
));
2596 plog(LLV_DEBUG
, LOCATION
, NULL
, "phase2 IV computed:\n");
2597 plogdump(LLV_DEBUG
, newivm
->iv
->v
, newivm
->iv
->l
);
2600 if (error
&& newivm
!= NULL
)
2601 oakley_delivm(newivm
);
2609 struct isakmp_ivm
*ivm
;
2614 if (ivm
->iv
!= NULL
)
2616 if (ivm
->ive
!= NULL
)
2625 * save new iv and old iv.
2628 oakley_do_decrypt(iph1
, msg
, ivdp
, ivep
)
2629 struct ph1handle
*iph1
;
2630 vchar_t
*msg
, *ivdp
, *ivep
;
2632 vchar_t
*buf
= NULL
, *new = NULL
;
2639 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin decryption.\n");
2641 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2643 plog(LLV_ERROR
, LOCATION
, NULL
,
2644 "invalid encryption algoriym %d.\n",
2645 iph1
->approval
->enctype
);
2649 /* save IV for next, but not sync. */
2650 memset(ivep
->v
, 0, ivep
->l
);
2651 memcpy(ivep
->v
, (caddr_t
)&msg
->v
[msg
->l
- blen
], blen
);
2653 plog(LLV_DEBUG
, LOCATION
, NULL
,
2654 "IV was saved for next processing:\n");
2655 plogdump(LLV_DEBUG
, ivep
->v
, ivep
->l
);
2657 pl
= msg
->v
+ sizeof(struct isakmp
);
2659 len
= msg
->l
- sizeof(struct isakmp
);
2664 plog(LLV_ERROR
, LOCATION
, NULL
,
2665 "failed to get buffer to decrypt.\n");
2668 memcpy(buf
->v
, pl
, len
);
2671 new = alg_oakley_encdef_decrypt(iph1
->approval
->enctype
,
2672 buf
, iph1
->key
, ivdp
);
2674 plog(LLV_ERROR
, LOCATION
, NULL
,
2675 "decryption %d failed.\n", iph1
->approval
->enctype
);
2678 plog(LLV_DEBUG
, LOCATION
, NULL
, "with key:\n");
2679 plogdump(LLV_DEBUG
, iph1
->key
->v
, iph1
->key
->l
);
2686 plog(LLV_DEBUG
, LOCATION
, NULL
, "decrypted payload by IV:\n");
2687 plogdump(LLV_DEBUG
, ivdp
->v
, ivdp
->l
);
2689 plog(LLV_DEBUG
, LOCATION
, NULL
,
2690 "decrypted payload, but not trimed.\n");
2691 plogdump(LLV_DEBUG
, new->v
, new->l
);
2693 /* get padding length */
2694 if (lcconf
->pad_excltail
)
2695 padlen
= new->v
[new->l
- 1] + 1;
2697 padlen
= new->v
[new->l
- 1];
2698 plog(LLV_DEBUG
, LOCATION
, NULL
, "padding len=%u\n", padlen
);
2701 if (lcconf
->pad_strict
) {
2702 if (padlen
> new->l
) {
2703 plog(LLV_ERROR
, LOCATION
, NULL
,
2704 "invalied padding len=%u, buflen=%u.\n",
2706 plogdump(LLV_ERROR
, new->v
, new->l
);
2710 plog(LLV_DEBUG
, LOCATION
, NULL
, "trimmed padding\n");
2712 plog(LLV_DEBUG
, LOCATION
, NULL
, "skip to trim padding.\n");
2715 /* create new buffer */
2716 len
= sizeof(struct isakmp
) + new->l
;
2719 plog(LLV_ERROR
, LOCATION
, NULL
,
2720 "failed to get buffer to decrypt.\n");
2723 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
2724 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
2725 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
2727 plog(LLV_DEBUG
, LOCATION
, NULL
, "decrypted.\n");
2728 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2730 #ifdef HAVE_PRINT_ISAKMP_C
2731 isakmp_printpacket(buf
, iph1
->remote
, iph1
->local
, 1);
2737 if (error
&& buf
!= NULL
) {
2751 oakley_do_encrypt(iph1
, msg
, ivep
, ivp
)
2752 struct ph1handle
*iph1
;
2753 vchar_t
*msg
, *ivep
, *ivp
;
2755 vchar_t
*buf
= 0, *new = 0;
2762 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin encryption.\n");
2764 /* set cbc block length */
2765 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
2767 plog(LLV_ERROR
, LOCATION
, NULL
,
2768 "invalid encryption algoriym %d.\n",
2769 iph1
->approval
->enctype
);
2773 pl
= msg
->v
+ sizeof(struct isakmp
);
2774 len
= msg
->l
- sizeof(struct isakmp
);
2777 padlen
= oakley_padlen(len
, blen
);
2778 plog(LLV_DEBUG
, LOCATION
, NULL
, "pad length = %u\n", padlen
);
2781 buf
= vmalloc(len
+ padlen
);
2783 plog(LLV_ERROR
, LOCATION
, NULL
,
2784 "failed to get buffer to encrypt.\n");
2789 char *p
= &buf
->v
[len
];
2790 if (lcconf
->pad_random
) {
2791 for (i
= 0; i
< padlen
; i
++)
2792 *p
++ = arc4random() & 0xff;
2795 memcpy(buf
->v
, pl
, len
);
2797 /* make pad into tail */
2798 if (lcconf
->pad_excltail
)
2799 buf
->v
[len
+ padlen
- 1] = padlen
- 1;
2801 buf
->v
[len
+ padlen
- 1] = padlen
;
2803 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
2806 new = alg_oakley_encdef_encrypt(iph1
->approval
->enctype
,
2807 buf
, iph1
->key
, ivep
);
2809 plog(LLV_ERROR
, LOCATION
, NULL
,
2810 "encryption %d failed.\n", iph1
->approval
->enctype
);
2813 plog(LLV_DEBUG
, LOCATION
, NULL
, "with key:\n");
2814 plogdump(LLV_DEBUG
, iph1
->key
->v
, iph1
->key
->l
);
2821 plog(LLV_DEBUG
, LOCATION
, NULL
, "encrypted payload by IV:\n");
2822 plogdump(LLV_DEBUG
, ivep
->v
, ivep
->l
);
2824 /* save IV for next */
2825 memset(ivp
->v
, 0, ivp
->l
);
2826 memcpy(ivp
->v
, (caddr_t
)&new->v
[new->l
- blen
], blen
);
2828 plog(LLV_DEBUG
, LOCATION
, NULL
, "save IV for next:\n");
2829 plogdump(LLV_DEBUG
, ivp
->v
, ivp
->l
);
2831 /* create new buffer */
2832 len
= sizeof(struct isakmp
) + new->l
;
2835 plog(LLV_ERROR
, LOCATION
, NULL
,
2836 "failed to get buffer to encrypt.\n");
2839 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
2840 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
2841 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
2845 plog(LLV_DEBUG
, LOCATION
, NULL
, "encrypted.\n");
2848 if (error
&& buf
!= NULL
) {
2858 /* culculate padding length */
2860 oakley_padlen(len
, base
)
2865 padlen
= base
- len
% base
;
2867 if (lcconf
->pad_randomlen
)
2868 padlen
+= ((arc4random() % (lcconf
->pad_maxsize
+ 1) + 1) *