1 /* $NetBSD: oakley.c,v 1.9.6.2 2007/04/04 13:08:28 vanhu Exp $ */
3 /* Id: oakley.c,v 1.32 2006/05/26 12:19:46 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h> /* XXX for subjectaltname */
39 #include <netinet/in.h> /* XXX for subjectaltname */
42 #include <openssl/pkcs7.h>
43 #include <openssl/x509.h>
50 #include <arpa/inet.h>
51 #include <TargetConditionals.h>
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
58 # include <sys/time.h>
74 #include "isakmp_var.h"
77 #include "isakmp_xauth.h"
78 #include "isakmp_cfg.h"
81 #include "localconf.h"
84 #include "ipsec_doi.h"
85 #include "algorithm.h"
89 #include "crypto_openssl.h"
90 #include "crypto_cssm.h"
98 #include <CoreFoundation/CoreFoundation.h>
99 #include "remoteconf.h"
100 #include "vpn_control.h"
101 #if TARGET_OS_EMBEDDED
102 #include <Security/SecCertificate.h>
103 #include <Security/SecCertificatePriv.h>
105 #include "vpn_control_var.h"
108 #define OUTBOUND_SA 0
111 #define CERT_CHECKID_FROM_PEER 0
112 #define CERT_CHECKID_FROM_RMCONFIG 1
115 #define INITDHVAL(a, s, d, t) \
118 buf.v = str2val((s), 16, &buf.l); \
119 memset(&a, 0, sizeof(struct dhgroup)); \
121 a.prime = vdup(&buf); \
124 racoon_free(buf.v); \
126 #else /* HAVE_OPENSSL */
127 #define INITDHVAL(a, s, d, t) \
130 buf.v = str2val((s), 16, &buf.l); \
131 memset(&a, 0, sizeof(struct dhgroup)); \
134 a.prime = vdup(&buf); \
137 racoon_free(buf.v); \
139 #endif /* HAVE_OPENSSL */
141 struct dhgroup dh_modp768
;
142 struct dhgroup dh_modp1024
;
143 struct dhgroup dh_modp1536
;
144 struct dhgroup dh_modp2048
;
145 struct dhgroup dh_modp3072
;
146 struct dhgroup dh_modp4096
;
147 struct dhgroup dh_modp6144
;
148 struct dhgroup dh_modp8192
;
151 static int oakley_check_dh_pub (vchar_t
*, vchar_t
**);
152 static int oakley_compute_keymat_x (phase2_handle_t
*, int, int);
153 static int get_cert_fromlocal (phase1_handle_t
*, int);
154 static int oakley_check_certid (phase1_handle_t
*iph1
);
155 static int oakley_check_certid_1 (vchar_t
*, int, int, void*, cert_status_t
*certStatus
);
157 static int check_typeofcertname (int, int);
159 static cert_t
*save_certbuf (struct isakmp_gen
*);
160 static int oakley_padlen (int, int);
162 static int base64toCFData (vchar_t
*, CFDataRef
*);
163 static cert_t
*oakley_appendcert_to_certchain (cert_t
*, cert_t
*);
166 oakley_get_defaultlifetime()
168 return OAKLEY_ATTR_SA_LD_SEC_DEFAULT
;
175 INITDHVAL(dh_modp768
, OAKLEY_PRIME_MODP768
,
176 OAKLEY_ATTR_GRP_DESC_MODP768
, OAKLEY_ATTR_GRP_TYPE_MODP
);
177 INITDHVAL(dh_modp1024
, OAKLEY_PRIME_MODP1024
,
178 OAKLEY_ATTR_GRP_DESC_MODP1024
, OAKLEY_ATTR_GRP_TYPE_MODP
);
179 INITDHVAL(dh_modp1536
, OAKLEY_PRIME_MODP1536
,
180 OAKLEY_ATTR_GRP_DESC_MODP1536
, OAKLEY_ATTR_GRP_TYPE_MODP
);
181 INITDHVAL(dh_modp2048
, OAKLEY_PRIME_MODP2048
,
182 OAKLEY_ATTR_GRP_DESC_MODP2048
, OAKLEY_ATTR_GRP_TYPE_MODP
);
183 INITDHVAL(dh_modp3072
, OAKLEY_PRIME_MODP3072
,
184 OAKLEY_ATTR_GRP_DESC_MODP3072
, OAKLEY_ATTR_GRP_TYPE_MODP
);
185 INITDHVAL(dh_modp4096
, OAKLEY_PRIME_MODP4096
,
186 OAKLEY_ATTR_GRP_DESC_MODP4096
, OAKLEY_ATTR_GRP_TYPE_MODP
);
187 INITDHVAL(dh_modp6144
, OAKLEY_PRIME_MODP6144
,
188 OAKLEY_ATTR_GRP_DESC_MODP6144
, OAKLEY_ATTR_GRP_TYPE_MODP
);
189 INITDHVAL(dh_modp8192
, OAKLEY_PRIME_MODP8192
,
190 OAKLEY_ATTR_GRP_DESC_MODP8192
, OAKLEY_ATTR_GRP_TYPE_MODP
);
196 oakley_dhgrp_free(struct dhgroup
*dhgrp
)
198 VPTRINIT(dhgrp
->prime
);
199 VPTRINIT(dhgrp
->curve_a
);
200 VPTRINIT(dhgrp
->curve_b
);
201 VPTRINIT(dhgrp
->order
);
207 * The length of the Diffie-Hellman public value MUST be equal to the
208 * length of the prime modulus over which the exponentiation was
209 * performed, prepending zero bits to the value if necessary.
212 oakley_check_dh_pub(vchar_t
*prime
, vchar_t
**pub0
)
215 vchar_t
*pub
= *pub0
;
217 if (prime
->l
== pub
->l
)
220 if (prime
->l
< pub
->l
) {
221 /* what should i do ? */
223 "invalid public information was generated.\n");
227 /* prime->l > pub->l */
228 tmp
= vmalloc(prime
->l
);
231 "failed to get DH buffer.\n");
234 memcpy(tmp
->v
+ prime
->l
- pub
->l
, pub
->v
, pub
->l
);
243 * compute sharing secret of DH
244 * IN: *dh, *pub, *priv, *pub_p
249 oakley_dh_compute(const struct dhgroup
*dh
, vchar_t
*pub
, vchar_t
*priv
, vchar_t
*pub_p
, vchar_t
**gxy
)
252 struct timeval start
, end
;
254 if ((*gxy
= vmalloc(dh
->prime
->l
)) == NULL
) {
256 "failed to get DH buffer.\n");
261 gettimeofday(&start
, NULL
);
264 case OAKLEY_ATTR_GRP_TYPE_MODP
:
265 if (eay_dh_compute(dh
->prime
, dh
->gen1
, pub
, priv
, pub_p
, gxy
) < 0) {
267 "failed to compute dh value.\n");
271 case OAKLEY_ATTR_GRP_TYPE_ECP
:
272 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
274 "dh type %d isn't supported.\n", dh
->type
);
278 "invalid dh type %d.\n", dh
->type
);
283 gettimeofday(&end
, NULL
);
284 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
285 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
286 timedelta(&start
, &end
));
289 plog(ASL_LEVEL_DEBUG
, "compute DH's shared.\n");
295 oakley_dh_compute(const struct dhgroup
*dh
, vchar_t
*pub_p
, size_t publicKeySize
, vchar_t
**gxy
, SecDHContext
*dhC
)
298 vchar_t
*computed_key
= NULL
;
299 size_t computed_keylen
;
303 struct timeval start
, end
;
304 gettimeofday(&start
, NULL
);
307 plog(ASL_LEVEL_DEBUG
, "compute DH result.\n");
309 maxKeyLen
= SecDHGetMaxKeyLength(*dhC
);
310 computed_key
= vmalloc(maxKeyLen
);
311 if (computed_key
== NULL
) {
312 plog(ASL_LEVEL_ERR
, "memory error.\n");
315 computed_keylen
= computed_key
->l
;
316 if (SecDHComputeKey(*dhC
, (uint8_t*)pub_p
->v
+ (maxKeyLen
- publicKeySize
), publicKeySize
,
317 (uint8_t*)computed_key
->v
, &computed_keylen
)) {
318 plog(ASL_LEVEL_ERR
, "failed to compute dh value.\n");
323 gettimeofday(&end
, NULL
);
324 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
325 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
326 timedelta(&start
, &end
));
329 *gxy
= vmalloc(maxKeyLen
);
331 plog(ASL_LEVEL_ERR
, "memory error.\n");
334 memcpy((*gxy
)->v
+ (maxKeyLen
- computed_keylen
), computed_key
->v
, computed_keylen
);
335 plog(ASL_LEVEL_DEBUG
, "compute DH's shared.\n");
356 * generate values of DH
362 oakley_dh_generate(const struct dhgroup
*dh
, vchar_t
**pub
, vchar_t
**priv
)
365 struct timeval start
, end
;
366 gettimeofday(&start
, NULL
);
369 case OAKLEY_ATTR_GRP_TYPE_MODP
:
370 if (eay_dh_generate(dh
->prime
, dh
->gen1
, dh
->gen2
, pub
, priv
) < 0) {
372 "failed to compute dh value.\n");
377 case OAKLEY_ATTR_GRP_TYPE_ECP
:
378 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
380 "dh type %d isn't supported.\n", dh
->type
);
384 "invalid dh type %d.\n", dh
->type
);
389 gettimeofday(&end
, NULL
);
390 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
391 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
392 timedelta(&start
, &end
));
395 if (oakley_check_dh_pub(dh
->prime
, pub
) != 0)
398 plog(ASL_LEVEL_DEBUG
, "compute DH's private.\n");
399 plog(ASL_LEVEL_DEBUG
, "compute DH's public.\n");
405 oakley_dh_generate(const struct dhgroup
*dh
, vchar_t
**pub
, size_t *publicKeySize
, SecDHContext
*dhC
)
407 vchar_t
*public = NULL
;
411 struct timeval start
, end
;
412 gettimeofday(&start
, NULL
);
415 plog(ASL_LEVEL_DEBUG
, "generate DH key pair.\n");
418 case OAKLEY_ATTR_GRP_TYPE_MODP
:
419 #define SECDH_MODP_GENERATOR 2
420 if (SecDHCreate(SECDH_MODP_GENERATOR
, (uint8_t*)dh
->prime
->v
, dh
->prime
->l
, 0, NULL
, 0, dhC
)) {
421 plog(ASL_LEVEL_ERR
, "failed to create dh context.\n");
424 maxKeyLen
= SecDHGetMaxKeyLength(*dhC
);
425 public = vmalloc(maxKeyLen
);
426 *publicKeySize
= public->l
;
427 if (public == NULL
) {
428 plog(ASL_LEVEL_ERR
, "memory error.\n");
431 if (SecDHGenerateKeypair(*dhC
, (uint8_t*)public->v
, publicKeySize
)) {
432 plog(ASL_LEVEL_ERR
, "failed to generate dh key pair.\n");
435 plog(ASL_LEVEL_DEBUG
, "got DH key pair.\n");
437 *pub
= vmalloc(maxKeyLen
);
439 plog(ASL_LEVEL_ERR
, "memory error.\n");
442 /* copy and fill with leading zeros */
443 memcpy((*pub
)->v
+ (maxKeyLen
- *publicKeySize
), public->v
, *publicKeySize
);
446 case OAKLEY_ATTR_GRP_TYPE_ECP
:
447 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
449 "dh type %d isn't supported.\n", dh
->type
);
453 "invalid dh type %d.\n", dh
->type
);
458 gettimeofday(&end
, NULL
);
459 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
460 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
461 timedelta(&start
, &end
));
464 if (oakley_check_dh_pub(dh
->prime
, pub
) != 0) {
465 plog(ASL_LEVEL_DEBUG
, "failed DH public key size check.\n");
469 //plogdump(ASL_LEVEL_DEBUG, (*pub)->v, (*pub)->l, "compute DH's public.\n");
487 * copy pre-defined dhgroup values.
490 oakley_setdhgroup(int group
, struct dhgroup
**dhgrp
)
494 *dhgrp
= NULL
; /* just make sure, initialize */
496 g
= alg_oakley_dhdef_group(group
);
499 "invalid DH parameter grp=%d.\n", group
);
503 if (!g
->type
|| !g
->prime
|| !g
->gen1
) {
506 "unsupported DH parameters grp=%d.\n", group
);
510 *dhgrp
= racoon_calloc(1, sizeof(struct dhgroup
));
511 if (*dhgrp
== NULL
) {
513 "failed to get DH buffer.\n");
517 /* set defined dh vlaues */
518 memcpy(*dhgrp
, g
, sizeof(*g
));
519 (*dhgrp
)->prime
= vdup(g
->prime
);
527 * NOTE: we do not support prf with different input/output bitwidth,
528 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example) in
529 * oakley_compute_keymat(). If you add support for such prf function,
530 * modify oakley_compute_keymat() accordingly.
533 oakley_prf(vchar_t
*key
, vchar_t
*buf
, phase1_handle_t
*iph1
)
536 int type
= OAKLEY_ATTR_HASH_ALG_MD5
;
538 if (iph1
->approval
== NULL
) {
539 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
541 * it's before negotiating hash algorithm.
542 * We use md5 as default.
544 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
548 type
= iph1
->approval
->hashtype
;
550 res
= alg_oakley_hmacdef_one(type
, key
, buf
);
553 "invalid hmac algorithm %d.\n", type
);
564 oakley_hash(vchar_t
*buf
, phase1_handle_t
*iph1
)
567 int type
= OAKLEY_ATTR_HASH_ALG_MD5
;
569 if (iph1
->approval
== NULL
) {
570 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
572 * it's before negotiating hash algorithm.
573 * We use md5 as default.
575 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
578 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
579 type
= iph1
->approval
->hashtype
;
583 res
= alg_oakley_hashdef_one(type
, buf
);
586 "invalid hash algorithm %d.\n", type
);
595 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
598 oakley_compute_keymat(phase2_handle_t
*iph2
, int side
)
602 /* compute sharing secret of DH when PFS */
603 if (iph2
->approval
->pfs_group
&& iph2
->dhpub_p
) {
605 if (oakley_dh_compute(iph2
->pfsgrp
, iph2
->dhpub
,
606 iph2
->dhpriv
, iph2
->dhpub_p
, &iph2
->dhgxy
) < 0)
608 if (oakley_dh_compute(iph2
->pfsgrp
, iph2
->dhpub_p
, iph2
->publicKeySize
, &iph2
->dhgxy
, &iph2
->dhC
) < 0)
614 if (oakley_compute_keymat_x(iph2
, side
, INBOUND_SA
) < 0
615 || oakley_compute_keymat_x(iph2
, side
, OUTBOUND_SA
) < 0)
618 plog(ASL_LEVEL_DEBUG
, "KEYMAT computed.\n");
628 * KEYMAT = prf(SKEYID_d, protocol | SPI | Ni_b | Nr_b).
629 * If PFS is desired and KE payloads were exchanged,
630 * KEYMAT = prf(SKEYID_d, g(qm)^xy | protocol | SPI | Ni_b | Nr_b)
632 * NOTE: we do not support prf with different input/output bitwidth,
633 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example).
636 oakley_compute_keymat_x(phase2_handle_t
*iph2
, int side
, int sa_dir
)
638 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
643 int dupkeymat
; /* generate K[1-dupkeymat] */
646 int encklen
, authklen
, l
;
648 pfs
= ((iph2
->approval
->pfs_group
&& iph2
->dhgxy
) ? 1 : 0);
650 len
= pfs
? iph2
->dhgxy
->l
: 0;
652 + sizeof(u_int32_t
) /* XXX SPI size */
658 "failed to get keymat buffer.\n");
662 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
667 memcpy(p
, iph2
->dhgxy
->v
, iph2
->dhgxy
->l
);
674 memcpy(p
, (sa_dir
== INBOUND_SA
? &pr
->spi
: &pr
->spi_p
),
676 p
+= sizeof(pr
->spi
);
678 bp
= (side
== INITIATOR
? iph2
->nonce
: iph2
->nonce_p
);
679 memcpy(p
, bp
->v
, bp
->l
);
682 bp
= (side
== INITIATOR
? iph2
->nonce_p
: iph2
->nonce
);
683 memcpy(p
, bp
->v
, bp
->l
);
687 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "KEYMAT compute with\n");
690 res
= oakley_prf(iph2
->ph1
->skeyid_d
, buf
, iph2
->ph1
);
694 /* compute key length needed */
695 encklen
= authklen
= 0;
696 switch (pr
->proto_id
) {
697 case IPSECDOI_PROTO_IPSEC_ESP
:
698 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
699 l
= alg_ipsec_encdef_keylen(tr
->trns_id
,
704 l
= alg_ipsec_hmacdef_hashlen(tr
->authtype
);
709 case IPSECDOI_PROTO_IPSEC_AH
:
710 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
711 l
= alg_ipsec_hmacdef_hashlen(tr
->trns_id
);
719 plog(ASL_LEVEL_DEBUG
, "encklen=%d authklen=%d\n",
722 dupkeymat
= (encklen
+ authklen
) / 8 / res
->l
;
723 dupkeymat
+= 2; /* safety mergin */
726 //plog(ASL_LEVEL_DEBUG,
727 // "generating %zu bits of key (dupkeymat=%d)\n",
728 // dupkeymat * 8 * res->l, dupkeymat);
729 if (0 < --dupkeymat
) {
730 vchar_t
*prev
= res
; /* K(n-1) */
731 vchar_t
*seed
= NULL
; /* seed for Kn */
735 * generating long key (isakmp-oakley-08 5.5)
736 * KEYMAT = K1 | K2 | K3 | ...
738 * src = [ g(qm)^xy | ] protocol | SPI | Ni_b | Nr_b
739 * K1 = prf(SKEYID_d, src)
740 * K2 = prf(SKEYID_d, K1 | src)
741 * K3 = prf(SKEYID_d, K2 | src)
742 * Kn = prf(SKEYID_d, K(n-1) | src)
744 //plog(ASL_LEVEL_DEBUG,
745 // "generating K1...K%d for KEYMAT.\n",
748 seed
= vmalloc(prev
->l
+ buf
->l
);
751 "failed to get keymat buffer.\n");
752 if (prev
&& prev
!= res
)
757 while (dupkeymat
--) {
758 vchar_t
*this = NULL
; /* Kn */
761 memcpy(seed
->v
, prev
->v
, prev
->l
);
762 memcpy(seed
->v
+ prev
->l
, buf
->v
, buf
->l
);
763 this = oakley_prf(iph2
->ph1
->skeyid_d
, seed
,
767 "oakley_prf memory overflow\n");
768 if (prev
&& prev
!= res
)
775 update_prev
= (prev
&& prev
== res
) ? 1 : 0;
778 res
= vrealloc(res
, l
+ this->l
);
785 "failed to get keymat buffer.\n");
786 if (prev
&& prev
!= res
)
792 memcpy(res
->v
+ l
, this->v
, this->l
);
794 if (prev
&& prev
!= res
)
800 if (prev
&& prev
!= res
)
805 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "");
807 if (sa_dir
== INBOUND_SA
)
818 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
840 * compute HASH(3) prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b)
841 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
844 oakley_compute_hash3(phase1_handle_t
*iph1
, u_int32_t msgid
, vchar_t
*body
)
846 vchar_t
*buf
= 0, *res
= 0;
851 len
= 1 + sizeof(u_int32_t
) + body
->l
;
854 plog(ASL_LEVEL_DEBUG
,
855 "failed to get hash buffer\n");
861 memcpy(buf
->v
+ 1, (char *)&msgid
, sizeof(msgid
));
863 memcpy(buf
->v
+ 1 + sizeof(u_int32_t
), body
->v
, body
->l
);
866 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
872 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH computed:\n");
881 * compute HASH type of prf(SKEYID_a, M-ID | buffer)
883 * for quick mode HASH(1):
884 * prf(SKEYID_a, M-ID | SA | Ni [ | KE ] [ | IDci | IDcr ])
885 * for quick mode HASH(2):
886 * prf(SKEYID_a, M-ID | Ni_b | SA | Nr [ | KE ] [ | IDci | IDcr ])
887 * for Informational exchange:
888 * prf(SKEYID_a, M-ID | N/D)
891 oakley_compute_hash1(phase1_handle_t
*iph1
, u_int32_t msgid
, vchar_t
*body
)
893 vchar_t
*buf
= NULL
, *res
= NULL
;
899 len
= sizeof(u_int32_t
) + body
->l
;
902 plog(ASL_LEVEL_DEBUG
,
903 "failed to get hash buffer\n");
909 memcpy(buf
->v
, (char *)&msgid
, sizeof(msgid
));
910 p
+= sizeof(u_int32_t
);
912 memcpy(p
, body
->v
, body
->l
);
915 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
921 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH computed:\n");
930 * compute phase1 HASH
932 * I-digest = prf(SKEYID, g^i | g^r | CKY-I | CKY-R | SAi_b | ID_i1_b)
933 * R-digest = prf(SKEYID, g^r | g^i | CKY-R | CKY-I | SAi_b | ID_r1_b)
934 * for gssapi, also include all GSS tokens, and call gss_wrap on the result
937 oakley_ph1hash_common(phase1_handle_t
*iph1
, int sw
)
939 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
947 + sizeof(cookie_t
) * 2
949 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
954 "failed to get hash buffer\n");
960 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
961 memcpy(p
, bp
->v
, bp
->l
);
964 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
965 memcpy(p
, bp
->v
, bp
->l
);
968 if (iph1
->side
== INITIATOR
)
969 bp2
= (sw
== GENERATE
?
970 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
972 bp2
= (sw
== GENERATE
?
973 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
974 bl
= sizeof(cookie_t
);
978 if (iph1
->side
== INITIATOR
)
979 bp2
= (sw
== GENERATE
?
980 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
982 bp2
= (sw
== GENERATE
?
983 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
984 bl
= sizeof(cookie_t
);
989 memcpy(p
, bp
->v
, bp
->l
);
992 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
993 memcpy(p
, bp
->v
, bp
->l
);
997 res
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
1010 * compute HASH_I on base mode.
1012 * HASH_I = prf(SKEYID, g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
1014 * HASH_I = prf(hash(Ni_b | Nr_b), g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
1017 oakley_ph1hash_base_i(phase1_handle_t
*iph1
, int sw
)
1019 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
1020 vchar_t
*hashkey
= NULL
;
1021 vchar_t
*hash
= NULL
; /* for signature mode */
1027 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
1029 "invalid etype for this hash function\n");
1033 switch (AUTHMETHOD(iph1
)) {
1034 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1035 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1036 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1037 #ifdef ENABLE_HYBRID
1038 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
1039 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1040 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
1041 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1042 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
1043 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1045 if (iph1
->skeyid
== NULL
) {
1046 plog(ASL_LEVEL_ERR
, "no SKEYID found.\n");
1049 hashkey
= iph1
->skeyid
;
1052 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1053 #ifdef ENABLE_HYBRID
1054 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1055 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1056 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
1057 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1059 /* make hash for seed */
1060 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
1064 "failed to get hash buffer\n");
1069 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
1070 memcpy(p
, bp
->v
, bp
->l
);
1073 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
1074 memcpy(p
, bp
->v
, bp
->l
);
1077 hash
= oakley_hash(buf
, iph1
);
1088 "not supported authentication method %d\n",
1089 iph1
->approval
->authmethod
);
1094 len
= (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
1095 + sizeof(cookie_t
) * 2
1097 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
1101 "failed to get hash buffer\n");
1106 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
1107 memcpy(p
, bp
->v
, bp
->l
);
1110 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1111 p
+= sizeof(cookie_t
);
1112 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1113 p
+= sizeof(cookie_t
);
1115 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
1118 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
1119 memcpy(p
, bp
->v
, bp
->l
);
1122 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "HASH_I with:\n");
1125 res
= oakley_prf(hashkey
, buf
, iph1
);
1131 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH_I computed:\n");
1142 * compute HASH_R on base mode for signature method.
1144 * HASH_R = prf(hash(Ni_b | Nr_b), g^xi | g^xr | CKY-I | CKY-R | SAi_b | IDii_b)
1147 oakley_ph1hash_base_r(phase1_handle_t
*iph1
, int sw
)
1149 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
1150 vchar_t
*hash
= NULL
;
1156 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
1158 "invalid etype for this hash function\n");
1162 switch(AUTHMETHOD(iph1
)) {
1163 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1164 #ifdef ENABLE_HYBRID
1165 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1166 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1167 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
1168 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1169 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
1174 "not supported authentication method %d\n",
1175 iph1
->approval
->authmethod
);
1180 /* make hash for seed */
1181 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
1185 "failed to get hash buffer\n");
1190 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
1191 memcpy(p
, bp
->v
, bp
->l
);
1194 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
1195 memcpy(p
, bp
->v
, bp
->l
);
1198 hash
= oakley_hash(buf
, iph1
);
1204 /* make really hash */
1205 len
= (sw
== GENERATE
? iph1
->dhpub_p
->l
: iph1
->dhpub
->l
)
1206 + (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
1207 + sizeof(cookie_t
) * 2
1209 + (sw
== GENERATE
? iph1
->id_p
->l
: iph1
->id
->l
);
1213 "failed to get hash buffer\n");
1219 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
1220 memcpy(p
, bp
->v
, bp
->l
);
1223 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
1224 memcpy(p
, bp
->v
, bp
->l
);
1227 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1228 p
+= sizeof(cookie_t
);
1229 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1230 p
+= sizeof(cookie_t
);
1232 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
1235 bp
= (sw
== GENERATE
? iph1
->id_p
: iph1
->id
);
1236 memcpy(p
, bp
->v
, bp
->l
);
1239 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "HASH_R with:\n");
1242 res
= oakley_prf(hash
, buf
, iph1
);
1248 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH_R computed:\n");
1260 oakley_verify_userid(phase1_handle_t
*iph1
)
1264 int user_id_found
= 0;
1266 for (p
= iph1
->cert_p
; p
; p
= p
->chain
) {
1267 user_id
= eay_get_x509_common_name(&p
->cert
); //%%%%%%%% fix this
1270 // the following functions will check if user_id == 0
1271 if (open_dir_authorize_id(user_id
, iph1
->rmconf
->open_dir_auth_group
)) {
1278 if (user_id_found
) {
1280 "the peer is not authorized for access.\n");
1283 "the peer is not authorized for access - user ID not found.\n");
1285 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1287 #endif /* HAVE_OPENDIR */
1290 * compute each authentication method in phase 1.
1294 * other: error to be reply with notification.
1295 * the value is notification type.
1298 oakley_validate_auth(phase1_handle_t
*iph1
)
1300 vchar_t
*my_hash
= NULL
;
1303 struct timeval start
, end
;
1305 SecKeyRef publicKeyRef
= NULL
;
1308 gettimeofday(&start
, NULL
);
1311 switch (AUTHMETHOD(iph1
)) {
1312 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1313 #ifdef ENABLE_HYBRID
1314 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
1315 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1321 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1323 "few isakmp message received.\n");
1324 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1326 #ifdef ENABLE_HYBRID
1327 if (AUTHMETHOD(iph1
) == FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
&&
1328 ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0))
1330 plog(ASL_LEVEL_ERR
, "No SIG was passed, "
1331 "hybrid auth is enabled, "
1332 "but peer is no Xauth compliant\n");
1333 return ISAKMP_NTYPE_SITUATION_NOT_SUPPORTED
;
1337 r_hash
= (caddr_t
)(iph1
->pl_hash
+ 1);
1339 //plogdump(ASL_LEVEL_DEBUG, r_hash,
1340 // ntohs(iph1->pl_hash->h.len) - sizeof(*iph1->pl_hash), "HASH received:\n");
1342 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1343 switch (iph1
->etype
) {
1344 case ISAKMP_ETYPE_IDENT
:
1345 case ISAKMP_ETYPE_AGG
:
1346 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1348 case ISAKMP_ETYPE_BASE
:
1349 if (iph1
->side
== INITIATOR
)
1350 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1352 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1356 "invalid etype %d\n", iph1
->etype
);
1357 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1360 if (my_hash
== NULL
)
1361 return ISAKMP_INTERNAL_ERROR
;
1363 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1367 plog(ASL_LEVEL_ERR
, "HASH mismatched\n");
1368 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1371 plog(ASL_LEVEL_DEBUG
, "HASH for PSK validated.\n");
1374 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1375 #ifdef ENABLE_HYBRID
1376 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1377 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
1378 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1385 if (iph1
->id_p
== NULL
) {
1387 "no ID payload was passed.\n");
1388 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1390 if (iph1
->sig_p
== NULL
) {
1392 "no SIG payload was passed.\n");
1393 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1396 plog(ASL_LEVEL_DEBUG
, "*** SIGN passed\n");
1398 /* get peer's cert */
1399 switch (iph1
->rmconf
->getcert_method
) {
1400 case ISAKMP_GETCERT_PAYLOAD
:
1401 if (iph1
->cert_p
== NULL
) {
1403 "no peer's CERT payload found.\n");
1404 return ISAKMP_INTERNAL_ERROR
;
1409 "invalid getcert_mothod: %d\n",
1410 iph1
->rmconf
->getcert_method
);
1411 return ISAKMP_INTERNAL_ERROR
;
1414 /* compare ID payload and certificate name */
1415 if (iph1
->rmconf
->verify_cert
&&
1416 (error
= oakley_check_certid(iph1
)) != 0)
1420 /* check cert common name against Open Directory authentication group */
1421 if (iph1
->rmconf
->cert_verification_option
== VERIFICATION_OPTION_OPEN_DIR
) {
1422 if (oakley_verify_userid(iph1
)) {
1423 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1426 #endif /* HAVE_OPENDIR */
1428 /* verify certificate */
1429 if (iph1
->rmconf
->verify_cert
1430 && iph1
->rmconf
->getcert_method
== ISAKMP_GETCERT_PAYLOAD
) {
1431 certtype
= iph1
->rmconf
->certtype
;
1432 #ifdef ENABLE_HYBRID
1433 switch (AUTHMETHOD(iph1
)) {
1434 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1435 certtype
= iph1
->cert_p
->type
;
1442 case ISAKMP_CERT_X509SIGN
:
1444 /* use ID from remote configuration */
1445 /* check each ID in list */
1446 struct idspec
*id_spec
;
1447 CFStringRef hostname
= NULL
;
1449 struct genlist_entry
*gpb
= NULL
;
1451 if (iph1
->rmconf
->cert_verification_option
== VERIFICATION_OPTION_PEERS_IDENTIFIER
) {
1452 id_spec
= genlist_next(iph1
->rmconf
->idvl_p
, &gpb
); /* expect only one id */
1453 if (id_spec
->idtype
== IDTYPE_ADDRESS
) {
1454 switch ((ALIGNED_CAST(struct sockaddr_storage
*)(id_spec
->id
->v
))->ss_family
) {
1456 peers_id
= inet_ntoa((ALIGNED_CAST(struct sockaddr_in
*)(id_spec
->id
->v
))->sin_addr
);
1457 hostname
= CFStringCreateWithCString(NULL
, peers_id
, kCFStringEncodingUTF8
);
1461 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
; /* not currently supported for embedded */
1466 "unknown address type for peers identifier.\n");
1467 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1471 hostname
= CFStringCreateWithBytes(NULL
, (u_int8_t
*)id_spec
->id
->v
, id_spec
->id
->l
, kCFStringEncodingUTF8
, FALSE
);
1473 error
= crypto_cssm_check_x509cert(oakley_get_peer_cert_from_certchain(iph1
), iph1
->cert_p
, hostname
, &publicKeyRef
);
1475 CFRelease(hostname
);
1481 "no supported certtype %d\n", certtype
);
1482 return ISAKMP_INTERNAL_ERROR
;
1486 "the peer's certificate is not verified.\n");
1487 return ISAKMP_NTYPE_INVALID_CERT_AUTHORITY
;
1491 plog(ASL_LEVEL_DEBUG
, "CERT validated\n");
1493 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1495 switch (iph1
->etype
) {
1496 case ISAKMP_ETYPE_IDENT
:
1497 case ISAKMP_ETYPE_AGG
:
1498 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1500 case ISAKMP_ETYPE_BASE
:
1501 if (iph1
->side
== INITIATOR
)
1502 my_hash
= oakley_ph1hash_base_r(iph1
, VALIDATE
);
1504 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1508 "invalid etype %d\n", iph1
->etype
);
1509 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1512 if (my_hash
== NULL
)
1513 return ISAKMP_INTERNAL_ERROR
;
1516 certtype
= iph1
->rmconf
->certtype
;
1517 #ifdef ENABLE_HYBRID
1518 switch (AUTHMETHOD(iph1
)) {
1519 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1520 certtype
= iph1
->cert_p
->type
;
1526 /* check signature */
1528 case ISAKMP_CERT_X509SIGN
:
1529 if (publicKeyRef
== NULL
) {
1530 plog(ASL_LEVEL_ERR
, "@@@@@@ publicKeyRef is NULL\n");
1532 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1533 error
= crypto_cssm_verify_x509sign(publicKeyRef
, my_hash
, iph1
->sig_p
, FALSE
);
1536 plog(ASL_LEVEL_ERR
, "error verifying signature %s\n", GetSecurityErrorString(error
));
1539 CFRelease(publicKeyRef
);
1543 "no supported certtype %d\n",
1546 return ISAKMP_INTERNAL_ERROR
;
1553 return ISAKMP_NTYPE_INVALID_SIGNATURE
;
1555 plog(ASL_LEVEL_DEBUG
, "SIG authenticated\n");
1558 #ifdef ENABLE_HYBRID
1559 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1561 if ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0) {
1562 plog(ASL_LEVEL_ERR
, "No SIG was passed, "
1563 "hybrid auth is enabled, "
1564 "but peer is no Xauth compliant\n");
1565 return ISAKMP_NTYPE_SITUATION_NOT_SUPPORTED
;
1568 plog(ASL_LEVEL_INFO
, "No SIG was passed, "
1569 "but hybrid auth is enabled\n");
1575 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1576 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1577 #ifdef ENABLE_HYBRID
1578 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
1579 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1580 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
1581 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1583 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1585 "few isakmp message received.\n");
1586 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1589 "not supported authmethod type %s\n",
1590 s_oakley_attr_method(iph1
->approval
->authmethod
));
1591 return ISAKMP_INTERNAL_ERROR
;
1594 "invalid authmethod %d why ?\n",
1595 iph1
->approval
->authmethod
);
1596 return ISAKMP_INTERNAL_ERROR
;
1599 gettimeofday(&end
, NULL
);
1600 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f", __func__
,
1601 s_oakley_attr_method(iph1
->approval
->authmethod
),
1602 timedelta(&start
, &end
));
1609 oakley_find_status_in_certchain (cert_t
*certchain
, cert_status_t certStatus
)
1613 for (p
= certchain
; p
; p
= p
->chain
) {
1614 if (p
->status
== certStatus
) {
1623 oakley_vpncontrol_notify_ike_failed_if_mycert_invalid (phase1_handle_t
*iph1
, int notify_initiator
)
1625 #if TARGET_OS_EMBEDDED
1626 int premature
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_PREMATURE
);
1627 int expired
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_EXPIRED
);
1628 if (premature
|| expired
) {
1630 u_int32_t fail_reason
;
1632 if (iph1
->remote
->ss_family
== AF_INET
)
1633 address
= ((struct sockaddr_in
*)(iph1
->remote
))->sin_addr
.s_addr
;
1637 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_PREMATURE
;
1639 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_EXPIRED
;
1641 vpncontrol_notify_ike_failed(fail_reason
, notify_initiator
, address
, 0, NULL
);
1644 #endif /* TARGET_OS_EMBEDDED */
1648 /* get my certificate
1649 * NOTE: include certificate type.
1652 oakley_getmycert(phase1_handle_t
*iph1
)
1656 switch (iph1
->rmconf
->certtype
) {
1657 case ISAKMP_CERT_X509SIGN
:
1660 if ( !(err
= get_cert_fromlocal(iph1
, 1))){
1661 if (oakley_vpncontrol_notify_ike_failed_if_mycert_invalid(iph1
, FROM_LOCAL
)) {
1668 "Unknown certtype #%d\n",
1669 iph1
->rmconf
->certtype
);
1676 * get a CERT from local file.
1679 * my == 0 peer's cert.
1682 get_cert_fromlocal(phase1_handle_t
*iph1
, int my
)
1684 vchar_t
*cert
= NULL
;
1687 cert_status_t status
= CERT_STATUS_OK
;
1690 certpl
= &iph1
->cert
;
1692 certpl
= &iph1
->cert_p
;
1693 if (iph1
->rmconf
->identity_in_keychain
== 0) {
1694 plog(ASL_LEVEL_ERR
, "no CERT defined.\n");
1698 switch (iph1
->rmconf
->certtype
) {
1699 case ISAKMP_CERT_X509SIGN
:
1700 if (iph1
->rmconf
->identity_in_keychain
) {
1703 if (iph1
->rmconf
->keychainCertRef
== NULL
|| base64toCFData(iph1
->rmconf
->keychainCertRef
, &dataRef
))
1705 cert
= crypto_cssm_get_x509cert(dataRef
, &status
);
1706 plog(ASL_LEVEL_DEBUG
, "done with chking cert status %d\n",status
);
1712 "not supported certtype %d\n",
1713 iph1
->rmconf
->certtype
);
1719 "failed to get %s CERT.\n",
1720 my
? "my" : "peers");
1724 *certpl
= oakley_newcert();
1727 "failed to get cert buffer.\n");
1730 (*certpl
)->pl
= vmalloc(cert
->l
+ 1);
1731 if ((*certpl
)->pl
== NULL
) {
1733 "failed to get cert buffer\n");
1734 oakley_delcert(*certpl
);
1738 memcpy((*certpl
)->pl
->v
+ 1, cert
->v
, cert
->l
);
1739 (*certpl
)->pl
->v
[0] = iph1
->rmconf
->certtype
;
1740 (*certpl
)->type
= iph1
->rmconf
->certtype
;
1741 (*certpl
)->status
= status
;
1742 (*certpl
)->cert
.v
= (*certpl
)->pl
->v
+ 1;
1743 (*certpl
)->cert
.l
= (*certpl
)->pl
->l
- 1;
1745 plog(ASL_LEVEL_DEBUG
, "created CERT payload\n");
1759 oakley_getsign(phase1_handle_t
*iph1
)
1761 vchar_t
*privkey
= NULL
;
1764 switch (iph1
->rmconf
->certtype
) {
1765 case ISAKMP_CERT_X509SIGN
:
1766 // cert in keychain - use cssm to sign
1767 if (iph1
->rmconf
->identity_in_keychain
) {
1770 if (iph1
->rmconf
->keychainCertRef
== NULL
|| base64toCFData(iph1
->rmconf
->keychainCertRef
, &dataRef
))
1772 iph1
->sig
= crypto_cssm_getsign(dataRef
, iph1
->hash
);
1778 "Unknown certtype #%d\n",
1779 iph1
->rmconf
->certtype
);
1783 if (iph1
->sig
== NULL
) {
1784 plog(ASL_LEVEL_ERR
, "failed to sign.\n");
1788 //plogdump(ASL_LEVEL_DEBUG, iph1->sig->v, iph1->sig->l, "SIGN computed:\n");
1793 if (privkey
!= NULL
)
1800 oakley_verify_certid(phase1_handle_t
*iph1
)
1802 if (iph1
->rmconf
->verify_cert
&&
1803 oakley_check_certid(iph1
)){
1804 plog(ASL_LEVEL_DEBUG
,
1805 "Discarding CERT: does not match ID:\n");
1806 oakley_delcert(iph1
->cert_p
);
1807 iph1
->cert_p
= NULL
;
1812 oakley_check_certid_in_certchain(cert_t
*certchain
, int idtype
, int idlen
, void *id
)
1816 for (p
= certchain
; p
; p
= p
->chain
) {
1817 if (oakley_check_certid_1(&p
->cert
, idtype
, idlen
, id
, &p
->status
) == 0) {
1821 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1825 oakley_get_peer_cert_from_certchain(phase1_handle_t
* iph1
)
1828 struct ipsecdoi_id_b
*id_b
;
1832 if (!iph1
->id_p
|| !iph1
->cert_p
) {
1833 plog(ASL_LEVEL_ERR
, "no ID nor CERT found.\n");
1836 if (!iph1
->cert_p
->chain
) {
1837 // no chain: simply return the only cert
1838 return iph1
->cert_p
;
1841 id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
1842 peers_id
= id_b
+ 1;
1843 idlen
= iph1
->id_p
->l
- sizeof(*id_b
);
1844 for (p
= iph1
->cert_p
; p
; p
= p
->chain
) {
1845 if (oakley_check_certid_1(&p
->cert
, id_b
->type
, idlen
, peers_id
, &p
->status
) == 0) {
1853 * compare certificate name and ID value.
1856 oakley_check_certid(phase1_handle_t
*iph1
)
1858 struct ipsecdoi_id_b
*id_b
;
1860 u_int8_t doi_type
= 255;
1861 void *peers_id
= NULL
;
1863 /* use ID from peer */
1864 if (iph1
->id_p
== NULL
|| iph1
->cert_p
== NULL
) {
1865 plog(ASL_LEVEL_ERR
, "no ID nor CERT found.\n");
1866 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1868 id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
1869 doi_type
= id_b
->type
;
1870 peers_id
= id_b
+ 1;
1871 idlen
= iph1
->id_p
->l
- sizeof(*id_b
);
1873 return oakley_check_certid_in_certchain(iph1
->cert_p
, doi_type
, idlen
, peers_id
);
1878 oakley_check_certid_1(vchar_t
*cert
, int idtype
, int idlen
, void *id
, cert_status_t
*certStatus
)
1884 #if !TARGET_OS_EMBEDDED
1886 char *altname
= NULL
;
1890 case IPSECDOI_ID_DER_ASN1_DN
:
1893 SecCertificateRef certificate
;
1896 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
1897 if (certificate
== NULL
) {
1899 "failed to get SecCertificateRef\n");
1900 if (certStatus
&& !*certStatus
) {
1901 *certStatus
= CERT_STATUS_INVALID
;
1903 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1905 subject
= crypto_cssm_CopySubjectSequence(certificate
);
1906 if (subject
== NULL
) {
1907 plog(ASL_LEVEL_ERR
, "failed to get certificate subjectName\n");
1908 if (certStatus
&& !*certStatus
) {
1909 *certStatus
= CERT_STATUS_INVALID_SUBJNAME
;
1911 error
= ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1913 len
= CFDataGetLength(subject
);
1914 namePtr
= (UInt8
*)CFDataGetBytePtr(subject
);
1916 if (idlen
!= len
|| memcmp(id
, namePtr
, idlen
)) {
1917 plog(ASL_LEVEL_ERR
, "ID mismatched with certificate subjectName\n");
1918 error
=ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1921 plog(ASL_LEVEL_ERR
, "no certificate subjectName found\n");
1922 error
= ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1927 "ID mismatched with certificate subjectName\n");
1928 plogdump(ASL_LEVEL_ERR
, namePtr
, len
, "subjectName (type %s):\n",
1929 s_ipsecdoi_ident(idtype
));
1930 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
1931 if (certStatus
&& !*certStatus
) {
1932 *certStatus
= CERT_STATUS_INVALID_SUBJNAME
;
1935 CFRelease(certificate
);
1936 if (subject
!= NULL
) {
1943 case IPSECDOI_ID_IPV4_ADDR
:
1944 case IPSECDOI_ID_IPV6_ADDR
:
1946 #if TARGET_OS_EMBEDDED
1948 SecCertificateRef certificate
;
1949 CFArrayRef addresses
;
1950 #define ADDRESS_BUF_SIZE 64
1952 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
1953 if (certificate
== NULL
) {
1955 "failed to get SecCertificateRef\n");
1956 if (certStatus
&& !*certStatus
) {
1957 *certStatus
= CERT_STATUS_INVALID
;
1959 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1961 addresses
= SecCertificateCopyIPAddresses(certificate
);
1962 if (addresses
== NULL
) {
1963 plog(ASL_LEVEL_ERR
, "failed to get subjectName\n");
1964 if (certStatus
&& !*certStatus
) {
1965 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
1967 CFRelease(certificate
);
1968 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1970 count
= CFArrayGetCount(addresses
);
1971 for (pos
= 0; pos
< count
; pos
++) {
1973 CFStringRef address
;
1975 char *addressBuf
, numAddress
[128];
1978 address
= CFArrayGetValueAtIndex(addresses
, pos
);
1979 addressLen
= CFStringGetLength(address
);
1980 if (addressLen
== 0)
1982 addressBuf
= racoon_malloc(ADDRESS_BUF_SIZE
);
1983 if (addressBuf
== NULL
) {
1984 plog(ASL_LEVEL_ERR
, "out of memory\n");
1985 CFRelease(addresses
);
1986 CFRelease(certificate
);
1989 if (CFStringGetCString(address
, addressBuf
, ADDRESS_BUF_SIZE
, kCFStringEncodingUTF8
) == TRUE
) {
1990 result
= inet_pton(idtype
== IPSECDOI_ID_IPV4_ADDR
? AF_INET
: AF_INET6
, addressBuf
, numAddress
);
1991 racoon_free(addressBuf
);
1993 continue; // wrong type or invalid address
1994 if (!memcmp(id
, numAddress
, idtype
== IPSECDOI_ID_IPV4_ADDR
? 32 : 128) == 0) { // found a match ?
1995 CFRelease(addresses
);
1996 CFRelease(certificate
);
2000 racoon_free(addressBuf
);
2002 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2004 "subjectAltName (expected type %s):\n", s_ipsecdoi_ident(idtype
));
2005 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2006 CFRelease(addresses
);
2007 CFRelease(certificate
);
2008 if (certStatus
&& !*certStatus
) {
2009 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2011 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2014 * Openssl returns the IPAddress as an ASN1 octet string (binary format)
2015 * followed by a trailing NULL. 5 bytes for IPv4 and 17 bytes for IPv6
2017 #define SUBJ_ALT_NAME_IPV4_ADDRESS_LEN 5
2018 #define SUBJ_ALT_NAME_IPV6_ADDRESS_LEN 17
2022 if ((idtype
== IPSECDOI_ID_IPV4_ADDR
&& idlen
!= sizeof(struct in_addr
))
2023 || (idtype
== IPSECDOI_ID_IPV6_ADDR
&& idlen
!= sizeof(struct in6_addr
))) {
2025 "invalid address length passed.\n");
2026 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2029 for (pos
= 1; ; pos
++) {
2030 if (eay_get_x509subjectaltname(cert
, &altname
, &type
, pos
, &len
) !=0) {
2032 "failed to get subjectAltName\n");
2033 if (certStatus
&& !*certStatus
) {
2034 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2036 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2039 /* it's the end condition of the loop. */
2042 "invalid subjectAltName\n");
2043 if (certStatus
&& !*certStatus
) {
2044 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2046 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2049 if (check_typeofcertname(idtype
, type
) != 0) {
2050 /* wrong type - skip this one */
2051 racoon_free(altname
);
2056 if (len
== SUBJ_ALT_NAME_IPV4_ADDRESS_LEN
) { /* IPv4 */
2057 if (idtype
!= IPSECDOI_ID_IPV4_ADDR
) {
2058 /* wrong IP address type - skip this one */
2059 racoon_free(altname
);
2065 else if (len
== SUBJ_ALT_NAME_IPV6_ADDRESS_LEN
) { /* IPv6 */
2066 if (idtype
!= IPSECDOI_ID_IPV6_ADDR
) {
2067 /* wrong IP address type - skip this one */
2068 racoon_free(altname
);
2075 /* invalid IP address length in certificate - bad or bogus certificate */
2077 "invalid IP address in certificate.\n");
2078 plogdump(ASL_LEVEL_ERR
, altname
, len
, "subjectAltName (expected type %s, got type %s):\n",
2079 s_ipsecdoi_ident(idtype
),
2080 s_ipsecdoi_ident(type
));
2081 racoon_free(altname
);
2083 if (certStatus
&& !*certStatus
) {
2084 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2086 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2089 /* compare the addresses */
2090 error
= memcmp(id
, altname
, idlen
);
2093 racoon_free(altname
);
2096 /* failed to find a match */
2098 "ID mismatched with subjectAltName.\n");
2099 plogdump(ASL_LEVEL_ERR
, altname
, len
, "subjectAltName (expected type %s, got type %s):\n",
2100 s_ipsecdoi_ident(idtype
),
2101 s_ipsecdoi_ident(type
));
2102 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2103 racoon_free(altname
);
2104 if (certStatus
&& !*certStatus
)
2105 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2106 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2108 #endif /* TARGET_OS_EMBEDDED */
2111 #if TARGET_OS_EMBEDDED
2112 case IPSECDOI_ID_FQDN
:
2115 SecCertificateRef certificate
;
2117 CFStringRef name
, ID
;
2119 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
2120 if (certificate
== NULL
) {
2122 "failed to get SecCertificateRef\n");
2123 if (certStatus
&& !*certStatus
) {
2124 *certStatus
= CERT_STATUS_INVALID
;
2126 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2128 names
= SecCertificateCopyDNSNames(certificate
);
2129 if (names
== NULL
) {
2131 "failed to get subjectName\n");
2132 if (certStatus
&& !*certStatus
) {
2133 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2135 CFRelease(certificate
);
2136 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2138 count
= CFArrayGetCount(names
);
2139 ID
= CFStringCreateWithBytes(kCFAllocatorDefault
, id
, idlen
, kCFStringEncodingUTF8
, FALSE
);
2141 plog(ASL_LEVEL_ERR
, "memory error\n");
2143 CFRelease(certificate
);
2146 for (pos
= 0; pos
< count
; pos
++) {
2147 name
= CFArrayGetValueAtIndex(names
, pos
);
2148 if (CFStringCompare(name
, ID
, 0) == kCFCompareEqualTo
) {
2151 CFRelease(certificate
);
2155 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2157 "subjectAltName (expected type %s):\n", s_ipsecdoi_ident(idtype
));
2158 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2161 CFRelease(certificate
);
2162 if (certStatus
&& !*certStatus
) {
2163 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2165 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2168 case IPSECDOI_ID_USER_FQDN
:
2172 SecCertificateRef certificate
;
2174 CFStringRef name
, ID
;
2176 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
2177 if (certificate
== NULL
) {
2179 "failed to get SecCertificateRef\n");
2180 if (certStatus
&& !*certStatus
) {
2181 *certStatus
= CERT_STATUS_INVALID
;
2183 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2185 names
= SecCertificateCopyRFC822Names(certificate
);
2186 if (names
== NULL
) {
2188 "failed to get subjectName\n");
2189 if (certStatus
&& !*certStatus
) {
2190 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2192 CFRelease(certificate
);
2193 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2195 count
= CFArrayGetCount(names
);
2196 ID
= CFStringCreateWithBytes(kCFAllocatorDefault
, id
, idlen
, kCFStringEncodingUTF8
, FALSE
);
2200 if (certStatus
&& !*certStatus
) {
2201 *certStatus
= CERT_STATUS_INVALID
;
2204 CFRelease(certificate
);
2205 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2207 for (pos
= 0; pos
< count
; pos
++) {
2208 name
= CFArrayGetValueAtIndex(names
, pos
);
2209 if (CFStringCompare(name
, ID
, 0) == kCFCompareEqualTo
) {
2212 CFRelease(certificate
);
2216 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2218 "subjectAltName (expected type %s):\n", s_ipsecdoi_ident(idtype
));
2219 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2222 CFRelease(certificate
);
2223 if (certStatus
&& !*certStatus
) {
2224 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2226 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2229 case IPSECDOI_ID_FQDN
:
2230 case IPSECDOI_ID_USER_FQDN
:
2234 for (pos
= 1; ; pos
++) {
2235 if (eay_get_x509subjectaltname(cert
, &altname
, &type
, pos
, &len
) != 0) {
2237 "failed to get subjectAltName\n");
2238 if (certStatus
&& !*certStatus
) {
2239 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2241 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2244 /* it's the end condition of the loop. */
2247 "invalid subjectAltName\n");
2248 if (certStatus
&& !*certStatus
) {
2249 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2251 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2254 if (check_typeofcertname(idtype
, type
) != 0) {
2255 /* wrong general type - skip this one */
2256 racoon_free(altname
);
2261 if (idlen
!= strlen(altname
)) {
2262 /* wrong length - skip this one */
2263 racoon_free(altname
);
2267 error
= memcmp(id
, altname
, idlen
);
2270 racoon_free(altname
);
2273 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2275 "subjectAltName (expected type %s, got type %s):\n",
2276 s_ipsecdoi_ident(idtype
),
2277 s_ipsecdoi_ident(type
));
2278 plogdump(ASL_LEVEL_ERR
, altname
, len
, "subjectAltName (expected type %s, got type %s):\n",
2279 s_ipsecdoi_ident(idtype
),
2280 s_ipsecdoi_ident(type
));
2281 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2282 racoon_free(altname
);
2283 if (certStatus
&& !*certStatus
)
2284 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2285 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2290 "Impropper ID type passed: %s.\n",
2291 s_ipsecdoi_ident(idtype
));
2292 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2298 check_typeofcertname(int doi
, int genid
)
2301 case IPSECDOI_ID_IPV4_ADDR
:
2302 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
2303 case IPSECDOI_ID_IPV6_ADDR
:
2304 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
2305 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
2306 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
2307 if (genid
!= GENT_IPADD
)
2310 case IPSECDOI_ID_FQDN
:
2311 if (genid
!= GENT_DNS
)
2314 case IPSECDOI_ID_USER_FQDN
:
2315 if (genid
!= GENT_EMAIL
)
2318 case IPSECDOI_ID_DER_ASN1_DN
: /* should not be passed to this function*/
2319 case IPSECDOI_ID_DER_ASN1_GN
:
2320 case IPSECDOI_ID_KEY_ID
:
2329 * save certificate including certificate type.
2332 oakley_savecert(phase1_handle_t
*iph1
, struct isakmp_gen
*gen
)
2336 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
2339 case ISAKMP_CERT_X509SIGN
:
2344 "Invalid CERT type %d\n", type
);
2349 plog(ASL_LEVEL_WARNING
,
2350 "preexisting CERT payload... chaining.\n");
2354 new = save_certbuf(gen
);
2357 "Failed to get CERT buffer.\n");
2361 switch (new->type
) {
2362 case ISAKMP_CERT_X509SIGN
:
2363 /* Ignore cert if it doesn't match identity
2364 * XXX If verify cert is disabled, we still just take
2365 * the first certificate....
2367 *c
= oakley_appendcert_to_certchain(*c
, new);
2368 plog(ASL_LEVEL_DEBUG
, "CERT saved:\n");
2372 oakley_delcert(new);
2380 * save certificate including certificate type.
2383 oakley_savecr(phase1_handle_t
*iph1
, struct isakmp_gen
*gen
)
2389 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
2392 case ISAKMP_CERT_X509SIGN
:
2394 oakley_delcert(iph1
->cr_p
);
2401 "Invalid CR type %d\n", type
);
2405 new = save_certbuf(gen
);
2408 "Failed to get CR buffer.\n");
2411 *c
= oakley_appendcert_to_certchain(*c
, new);
2412 plog(ASL_LEVEL_DEBUG
, "CR saved\n");
2418 save_certbuf(struct isakmp_gen
*gen
)
2422 if(ntohs(gen
->len
) <= sizeof(*gen
)){
2424 "Len is too small !!.\n");
2428 new = oakley_newcert();
2431 "Failed to get CERT buffer.\n");
2435 new->pl
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2436 if (new->pl
== NULL
) {
2438 "Failed to copy CERT from packet.\n");
2439 oakley_delcert(new);
2443 memcpy(new->pl
->v
, gen
+ 1, new->pl
->l
);
2444 new->type
= new->pl
->v
[0] & 0xff;
2445 new->cert
.v
= new->pl
->v
+ 1;
2446 new->cert
.l
= new->pl
->l
- 1;
2453 * NOTE: No Certificate Authority field is included to CR payload at the
2454 * moment. Becuase any certificate authority are accepted without any check.
2455 * The section 3.10 in RFC2408 says that this field SHOULD not be included,
2456 * if there is no specific certificate authority requested.
2459 oakley_getcr(phase1_handle_t
*iph1
)
2466 "failed to get cr buffer\n");
2469 if(iph1
->rmconf
->certtype
== ISAKMP_CERT_NONE
) {
2470 buf
->v
[0] = iph1
->rmconf
->cacerttype
;
2471 plog(ASL_LEVEL_DEBUG
, "create my CR: NONE, using %s instead\n",
2472 s_isakmp_certtype(iph1
->rmconf
->cacerttype
));
2474 buf
->v
[0] = iph1
->rmconf
->certtype
;
2475 plog(ASL_LEVEL_DEBUG
, "create my CR: %s\n",
2476 s_isakmp_certtype(iph1
->rmconf
->certtype
));
2479 // plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "");
2488 oakley_checkcr(phase1_handle_t
*iph1
)
2490 if (iph1
->cr_p
== NULL
)
2493 plog(ASL_LEVEL_DEBUG
,
2494 "peer transmitted CR: %s\n",
2495 s_isakmp_certtype(iph1
->cr_p
->type
));
2497 if (iph1
->cr_p
->type
!= iph1
->rmconf
->certtype
) {
2499 "such a cert type isn't supported: %d\n",
2500 (char)iph1
->cr_p
->type
);
2508 * check to need CR payload.
2511 oakley_needcr(int type
)
2514 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2515 #ifdef ENABLE_HYBRID
2516 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2517 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2518 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2528 oakley_getpskall(phase1_handle_t
*iph1
)
2530 vchar_t
*secret
= NULL
;
2532 if (iph1
->rmconf
->shared_secret
) {
2534 switch (iph1
->rmconf
->secrettype
) {
2535 case SECRETTYPE_KEY
:
2536 /* in psk file - use KEY from remote configuration to locate it */
2537 secret
= getpsk(iph1
->rmconf
->shared_secret
->v
, iph1
->rmconf
->shared_secret
->l
-1);
2540 case SECRETTYPE_KEYCHAIN
:
2541 /* in the system keychain */
2542 secret
= getpskfromkeychain(iph1
->rmconf
->shared_secret
->v
, iph1
->etype
, iph1
->rmconf
->secrettype
, NULL
);
2544 case SECRETTYPE_KEYCHAIN_BY_ID
:
2545 /* in the system keychain - use peer id */
2546 secret
= getpskfromkeychain(iph1
->rmconf
->shared_secret
->v
, iph1
->etype
, iph1
->rmconf
->secrettype
, iph1
->id_p
);
2548 #endif // HAVE_KEYCHAIN
2549 case SECRETTYPE_USE
:
2550 /* in the remote configuration */
2552 /* rmconf->shared_secret is a string and contains a NULL character that must be removed */
2553 secret
= vmalloc(iph1
->rmconf
->shared_secret
->l
- 1);
2554 if (secret
== NULL
) {
2555 plog(ASL_LEVEL_ERR
, "memory error.\n");
2558 memcpy(secret
->v
, iph1
->rmconf
->shared_secret
->v
, secret
->l
);
2560 } else if (iph1
->etype
!= ISAKMP_ETYPE_IDENT
) {
2561 secret
= getpskbyname(iph1
->id_p
);
2563 if (iph1
->rmconf
->verify_identifier
) {
2564 plog(ASL_LEVEL_ERR
, "couldn't find pskey by peer's ID.\n");
2570 plog(ASL_LEVEL_NOTICE
, "try to get pskey by the peer's address.\n");
2571 secret
= getpskbyaddr(iph1
->remote
);
2574 "couldn't find the pskey by address %s.\n",
2575 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2585 * see seciton 5. Exchanges in RFC 2409
2586 * psk: SKEYID = prf(pre-shared-key, Ni_b | Nr_b)
2587 * sig: SKEYID = prf(Ni_b | Nr_b, g^ir)
2588 * enc: SKEYID = prf(H(Ni_b | Nr_b), CKY-I | CKY-R)
2591 oakley_skeyid(phase1_handle_t
*iph1
)
2593 vchar_t
*key
= NULL
;
2594 vchar_t
*buf
= NULL
;
2601 switch (AUTHMETHOD(iph1
)) {
2602 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2603 #ifdef ENABLE_HYBRID
2604 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
2605 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
2607 key
= oakley_getpskall(iph1
);
2610 "couldn't find the pskey for %s.\n",
2611 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2614 plog(ASL_LEVEL_DEBUG
, "the psk found.\n");
2615 /* should be secret PSK */
2616 plogdump(ASL_LEVEL_DEBUG
, key
->v
, key
->l
, "psk: ");
2618 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2622 "failed to get skeyid buffer\n");
2627 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2628 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce 1: ");
2629 memcpy(p
, bp
->v
, bp
->l
);
2632 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2633 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce 2: ");
2634 memcpy(p
, bp
->v
, bp
->l
);
2637 iph1
->skeyid
= oakley_prf(key
, buf
, iph1
);
2639 if (iph1
->skeyid
== NULL
)
2643 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2644 #ifdef ENABLE_HYBRID
2645 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2646 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
2647 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2648 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2650 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2654 "failed to get nonce buffer\n");
2659 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2660 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce1: ");
2661 memcpy(p
, bp
->v
, bp
->l
);
2664 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2665 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce2: ");
2666 memcpy(p
, bp
->v
, bp
->l
);
2669 iph1
->skeyid
= oakley_prf(buf
, iph1
->dhgxy
, iph1
);
2670 if (iph1
->skeyid
== NULL
)
2673 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2674 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2675 #ifdef ENABLE_HYBRID
2676 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
2677 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
2678 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
2679 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
2681 plog(ASL_LEVEL_WARNING
,
2682 "not supported authentication method %s\n",
2683 s_oakley_attr_method(iph1
->approval
->authmethod
));
2687 "invalid authentication method %d\n",
2688 iph1
->approval
->authmethod
);
2692 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid->v, iph1->skeyid->l, "IKEv1 SKEYID computed:\n");
2705 * compute SKEYID_[dae]
2708 oakley_skeyid_dae(phase1_handle_t
*iph1
)
2710 vchar_t
*buf
= NULL
;
2715 if (iph1
->skeyid
== NULL
) {
2716 plog(ASL_LEVEL_ERR
, "no SKEYID found.\n");
2721 * see seciton 5. Exchanges in RFC 2409
2722 * SKEYID_d = prf(SKEYID, g^ir | CKY-I | CKY-R | 0)
2723 * SKEYID_a = prf(SKEYID, SKEYID_d | g^ir | CKY-I | CKY-R | 1)
2724 * SKEYID_e = prf(SKEYID, SKEYID_a | g^ir | CKY-I | CKY-R | 2)
2727 /* SKEYID_d = prf(SKEYID, g^xy | CKY-I | CKY-R | 0) */
2728 len
= iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2732 "failed to get skeyid buffer\n");
2737 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2738 p
+= iph1
->dhgxy
->l
;
2739 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2740 p
+= sizeof(cookie_t
);
2741 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2742 p
+= sizeof(cookie_t
);
2744 iph1
->skeyid_d
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2745 if (iph1
->skeyid_d
== NULL
)
2751 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid_d->v, iph1->skeyid_d->l, "SKEYID_d computed:\n");
2754 /* SKEYID_a = prf(SKEYID, SKEYID_d | g^xy | CKY-I | CKY-R | 1) */
2755 len
= iph1
->skeyid_d
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2759 "failed to get skeyid buffer\n");
2763 memcpy(p
, iph1
->skeyid_d
->v
, iph1
->skeyid_d
->l
);
2764 p
+= iph1
->skeyid_d
->l
;
2765 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2766 p
+= iph1
->dhgxy
->l
;
2767 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2768 p
+= sizeof(cookie_t
);
2769 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2770 p
+= sizeof(cookie_t
);
2772 iph1
->skeyid_a
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2773 if (iph1
->skeyid_a
== NULL
)
2779 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid_a->v, iph1->skeyid_a->l, "SKEYID_a computed:\n");
2782 /* SKEYID_e = prf(SKEYID, SKEYID_a | g^xy | CKY-I | CKY-R | 2) */
2783 len
= iph1
->skeyid_a
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2787 "failed to get skeyid buffer\n");
2791 memcpy(p
, iph1
->skeyid_a
->v
, iph1
->skeyid_a
->l
);
2792 p
+= iph1
->skeyid_a
->l
;
2793 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2794 p
+= iph1
->dhgxy
->l
;
2795 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2796 p
+= sizeof(cookie_t
);
2797 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2798 p
+= sizeof(cookie_t
);
2800 iph1
->skeyid_e
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2801 if (iph1
->skeyid_e
== NULL
)
2807 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid_e->v, iph1->skeyid_e->l, "SKEYID_e computed:\n");
2818 * compute final encryption key.
2822 oakley_compute_enckey(phase1_handle_t
*iph1
)
2824 u_int keylen
, prflen
;
2828 keylen
= alg_oakley_encdef_keylen(iph1
->approval
->enctype
,
2829 iph1
->approval
->encklen
);
2832 "invalid encryption algoritym %d, "
2833 "or invalid key length %d.\n",
2834 iph1
->approval
->enctype
,
2835 iph1
->approval
->encklen
);
2838 iph1
->key
= vmalloc(keylen
>> 3);
2839 if (iph1
->key
== NULL
) {
2841 "failed to get key buffer\n");
2845 /* set prf length */
2846 prflen
= alg_oakley_hashdef_hashlen(iph1
->approval
->hashtype
);
2849 "invalid hash type %d.\n", iph1
->approval
->hashtype
);
2853 /* see isakmp-oakley-08 5.3. */
2854 if (iph1
->key
->l
<= iph1
->skeyid_e
->l
) {
2856 * if length(Ka) <= length(SKEYID_e)
2857 * Ka = first length(K) bit of SKEYID_e
2859 memcpy(iph1
->key
->v
, iph1
->skeyid_e
->v
, iph1
->key
->l
);
2861 vchar_t
*buf
= NULL
, *res
= NULL
;
2870 * K1 = prf(SKEYID_e, 0)
2871 * K2 = prf(SKEYID_e, K1)
2872 * K3 = prf(SKEYID_e, K2)
2874 plog(ASL_LEVEL_DEBUG
,
2875 "len(SKEYID_e) < len(Ka) (%zu < %zu), "
2876 "generating long key (Ka = K1 | K2 | ...)\n",
2877 iph1
->skeyid_e
->l
, iph1
->key
->l
);
2879 if ((buf
= vmalloc(prflen
>> 3)) == 0) {
2881 "failed to get key buffer\n");
2884 p
= (u_char
*)iph1
->key
->v
;
2885 ep
= p
+ iph1
->key
->l
;
2889 if (p
== (u_char
*)iph1
->key
->v
) {
2890 /* just for computing K1 */
2894 res
= oakley_prf(iph1
->skeyid_e
, buf
, iph1
);
2899 plog(ASL_LEVEL_DEBUG
,
2900 "compute intermediate encryption key K%d\n",
2902 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "");
2903 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "");
2905 cplen
= (res
->l
< ep
- p
) ? res
->l
: ep
- p
;
2906 memcpy(p
, res
->v
, cplen
);
2909 buf
->l
= prflen
>> 3; /* to cancel K1 speciality */
2910 if (res
->l
!= buf
->l
) {
2912 "internal error: res->l=%zu buf->l=%zu\n",
2918 memcpy(buf
->v
, res
->v
, res
->l
);
2927 * don't check any weak key or not.
2928 * draft-ietf-ipsec-ike-01.txt Appendix B.
2929 * draft-ietf-ipsec-ciph-aes-cbc-00.txt Section 2.3.
2932 //plogdump(ASL_LEVEL_DEBUG, iph1->key->v, iph1->key->l, "final encryption key computed:\n");
2940 /* allocated new buffer for CERT */
2942 oakley_newcert(void)
2946 new = racoon_calloc(1, sizeof(*new));
2949 "failed to get cert's buffer\n");
2959 /* delete buffer for CERT */
2961 oakley_delcert_1(cert_t
*cert
)
2970 /* delete buffer for CERT */
2972 oakley_delcert(cert_t
*cert
)
2974 cert_t
*p
, *to_delete
;
2979 for (p
= cert
; p
;) {
2982 oakley_delcert_1(to_delete
);
2986 /* delete buffer for CERT */
2988 oakley_appendcert_to_certchain(cert_t
*certchain
, cert_t
*new)
2995 for (p
= certchain
; p
; p
= p
->chain
) {
3005 * compute IV and set to ph1handle
3006 * IV = hash(g^xi | g^xr)
3007 * see 4.1 Phase 1 state in draft-ietf-ipsec-ike.
3010 oakley_newiv(phase1_handle_t
*iph1
)
3012 struct isakmp_ivm
*newivm
= NULL
;
3013 vchar_t
*buf
= NULL
, *bp
;
3018 len
= iph1
->dhpub
->l
+ iph1
->dhpub_p
->l
;
3022 "Failed to get IV buffer\n");
3028 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub
: iph1
->dhpub_p
);
3029 memcpy(p
, bp
->v
, bp
->l
);
3032 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub_p
: iph1
->dhpub
);
3033 memcpy(p
, bp
->v
, bp
->l
);
3037 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
3038 if (newivm
== NULL
) {
3040 "Failed to get IV buffer\n");
3046 newivm
->iv
= oakley_hash(buf
, iph1
);
3047 if (newivm
->iv
== NULL
) {
3049 oakley_delivm(newivm
);
3053 /* adjust length of iv */
3054 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3055 if (newivm
->iv
->l
== -1) {
3057 "Invalid encryption algorithm %d.\n",
3058 iph1
->approval
->enctype
);
3060 oakley_delivm(newivm
);
3064 /* create buffer to save iv */
3065 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
3067 "vdup (%s)\n", strerror(errno
));
3069 oakley_delivm(newivm
);
3075 //plogdump(ASL_LEVEL_DEBUG, newivm->iv->v, newivm->iv->l, "IV computed:\n");
3077 if (iph1
->ivm
!= NULL
)
3078 oakley_delivm(iph1
->ivm
);
3086 * compute IV for the payload after phase 1.
3087 * It's not limited for phase 2.
3088 * if pahse 1 was encrypted.
3089 * IV = hash(last CBC block of Phase 1 | M-ID)
3090 * if phase 1 was not encrypted.
3091 * IV = hash(phase 1 IV | M-ID)
3092 * see 4.2 Phase 2 state in draft-ietf-ipsec-ike.
3095 oakley_newiv2(phase1_handle_t
*iph1
, u_int32_t msgid
)
3097 struct isakmp_ivm
*newivm
= NULL
;
3098 vchar_t
*buf
= NULL
;
3104 len
= iph1
->ivm
->iv
->l
+ sizeof(msgid_t
);
3108 "Failed to get IV buffer\n");
3114 memcpy(p
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
3115 p
+= iph1
->ivm
->iv
->l
;
3117 memcpy(p
, &msgid
, sizeof(msgid
));
3119 plog(ASL_LEVEL_DEBUG
, "Compute IV for Phase 2\n");
3120 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "Phase 1 last IV:\n");
3123 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
3124 if (newivm
== NULL
) {
3126 "Failed to get IV buffer\n");
3131 if ((newivm
->iv
= oakley_hash(buf
, iph1
)) == NULL
)
3134 /* adjust length of iv */
3135 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3136 if (newivm
->iv
->l
== -1) {
3138 "Invalid encryption algorithm %d.\n",
3139 iph1
->approval
->enctype
);
3143 /* create buffer to save new iv */
3144 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
3145 plog(ASL_LEVEL_ERR
, "vdup (%s)\n", strerror(errno
));
3151 //plogdump(ASL_LEVEL_DEBUG, newivm->iv->v, newivm->iv->l, "Phase 2 IV computed:\n");
3154 if (error
&& newivm
!= NULL
){
3155 oakley_delivm(newivm
);
3164 oakley_delivm(struct isakmp_ivm
*ivm
)
3169 if (ivm
->iv
!= NULL
)
3171 if (ivm
->ive
!= NULL
)
3174 plog(ASL_LEVEL_DEBUG
, "IV freed\n");
3181 * save new iv and old iv.
3184 oakley_do_ikev1_decrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivdp
, vchar_t
*ivep
)
3186 vchar_t
*buf
= NULL
, *new = NULL
;
3193 plog(ASL_LEVEL_DEBUG
, "Begin decryption.\n");
3195 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3198 "Invalid encryption algorithm %d.\n",
3199 iph1
->approval
->enctype
);
3203 /* save IV for next, but not sync. */
3204 memset(ivep
->v
, 0, ivep
->l
);
3205 memcpy(ivep
->v
, (caddr_t
)&msg
->v
[msg
->l
- blen
], blen
);
3207 plogdump(ASL_LEVEL_DEBUG
, ivep
->v
, ivep
->l
, "IV was saved for next processing:\n");
3209 pl
= msg
->v
+ sizeof(struct isakmp
);
3211 len
= msg
->l
- sizeof(struct isakmp
);
3217 "Failed to get buffer to decrypt.\n");
3220 memcpy(buf
->v
, pl
, len
);
3223 new = alg_oakley_encdef_decrypt(iph1
->approval
->enctype
,
3224 buf
, iph1
->key
, ivdp
);
3225 if (new == NULL
|| new->v
== NULL
|| new->l
== 0) {
3227 "Decryption %d failed.\n", iph1
->approval
->enctype
);
3230 //plogdump(ASL_LEVEL_DEBUG, iph1->key->v, iph1->key->l, "with key:\n");
3237 plog(ASL_LEVEL_DEBUG
, "decrypted payload by IV:\n");
3239 /* get padding length */
3240 if (lcconf
->pad_excltail
)
3241 padlen
= new->v
[new->l
- 1] + 1;
3243 padlen
= new->v
[new->l
- 1];
3244 plog(ASL_LEVEL_DEBUG
, "padding len=%u\n", padlen
);
3247 if (lcconf
->pad_strict
) {
3248 if (padlen
> new->l
) {
3249 plog(ASL_LEVEL_ERR
, "invalid padding len=%u, buflen=%zu.\n",
3254 plog(ASL_LEVEL_DEBUG
, "trimmed padding\n");
3256 plog(ASL_LEVEL_DEBUG
, "skip to trim padding.\n");
3259 /* create new buffer */
3260 len
= sizeof(struct isakmp
) + new->l
;
3264 "failed to get buffer to decrypt.\n");
3267 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
3268 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
3269 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
3271 plog(ASL_LEVEL_DEBUG
, "decrypted.\n");
3273 #ifdef HAVE_PRINT_ISAKMP_C
3274 isakmp_printpacket(buf
, iph1
->remote
, iph1
->local
, 1);
3280 if (error
&& buf
!= NULL
) {
3294 oakley_do_decrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivdp
, vchar_t
*ivep
)
3296 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
3297 return(oakley_do_ikev1_decrypt(iph1
, msg
, ivdp
, ivep
));
3300 plog(ASL_LEVEL_ERR
, "Failed to decrypt invalid IKE version");
3308 oakley_do_ikev1_encrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivep
, vchar_t
*ivp
)
3310 vchar_t
*buf
= 0, *new = 0;
3317 plog(ASL_LEVEL_DEBUG
, "Begin encryption.\n");
3319 /* set cbc block length */
3320 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3323 "Invalid encryption algorithm %d.\n",
3324 iph1
->approval
->enctype
);
3328 pl
= msg
->v
+ sizeof(struct isakmp
);
3329 len
= msg
->l
- sizeof(struct isakmp
);
3332 padlen
= oakley_padlen(len
, blen
);
3333 plog(ASL_LEVEL_DEBUG
, "pad length = %u\n", padlen
);
3336 buf
= vmalloc(len
+ padlen
);
3339 "Failed to get buffer to encrypt.\n");
3344 char *p
= &buf
->v
[len
];
3345 if (lcconf
->pad_random
) {
3346 for (i
= 0; i
< padlen
; i
++)
3347 *p
++ = eay_random() & 0xff;
3350 memcpy(buf
->v
, pl
, len
);
3352 /* make pad into tail */
3353 if (lcconf
->pad_excltail
)
3354 buf
->v
[len
+ padlen
- 1] = padlen
- 1;
3356 buf
->v
[len
+ padlen
- 1] = padlen
;
3358 plogdump(ASL_LEVEL_DEBUG
, buf
->v
, buf
->l
, "About to encrypt %d bytes", buf
->l
);
3361 new = alg_oakley_encdef_encrypt(iph1
->approval
->enctype
,
3362 buf
, iph1
->key
, ivep
);
3365 "Encryption %d failed.\n", iph1
->approval
->enctype
);
3368 //plogdump(ASL_LEVEL_DEBUG, iph1->key->v, iph1->key->l, "with key:\n");
3375 //plogdump(ASL_LEVEL_DEBUG, ivep->v, ivep->l, "encrypted payload by IV:\n");
3377 /* save IV for next */
3378 memset(ivp
->v
, 0, ivp
->l
);
3379 memcpy(ivp
->v
, (caddr_t
)&new->v
[new->l
- blen
], blen
);
3381 //plogdump(ASL_LEVEL_DEBUG, ivp->v, ivp->l, "save IV for next:\n");
3383 /* create new buffer */
3384 len
= sizeof(struct isakmp
) + new->l
;
3388 "Failed to get buffer to encrypt.\n");
3391 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
3392 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
3393 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
3397 plog(ASL_LEVEL_DEBUG
, "Encrypted.\n");
3400 if (error
&& buf
!= NULL
) {
3414 oakley_do_encrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivep
, vchar_t
*ivp
)
3416 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
3417 return(oakley_do_ikev1_encrypt(iph1
, msg
, ivep
, ivp
));
3420 plog(ASL_LEVEL_ERR
, "Failed to encrypt invalid IKE version");
3424 /* culculate padding length */
3426 oakley_padlen(int len
, int base
)
3430 padlen
= base
- (len
% base
);
3432 if (lcconf
->pad_randomlen
)
3433 padlen
+= ((eay_random() % (lcconf
->pad_maxsize
+ 1) + 1) *
3439 /* -----------------------------------------------------------------------------
3440 The base-64 encoding packs three 8-bit bytes into four 7-bit ASCII
3441 characters. If the number of bytes in the original data isn't divisable
3442 by three, "=" characters are used to pad the encoded data. The complete
3443 set of characters used in base-64 are:
3451 ----------------------------------------------------------------------------- */
3452 static const signed char base64_DecodeTable
[128] = {
3453 /* 000 */ -1, -1, -1, -1, -1, -1, -1, -1,
3454 /* 010 */ -1, -1, -1, -1, -1, -1, -1, -1,
3455 /* 020 */ -1, -1, -1, -1, -1, -1, -1, -1,
3456 /* 030 */ -1, -1, -1, -1, -1, -1, -1, -1,
3457 /* ' ' */ -1, -1, -1, -1, -1, -1, -1, -1,
3458 /* '(' */ -1, -1, -1, 62, -1, -1, -1, 63,
3459 /* '0' */ 52, 53, 54, 55, 56, 57, 58, 59,
3460 /* '8' */ 60, 61, -1, -1, -1, 0, -1, -1,
3461 /* '@' */ -1, 0, 1, 2, 3, 4, 5, 6,
3462 /* 'H' */ 7, 8, 9, 10, 11, 12, 13, 14,
3463 /* 'P' */ 15, 16, 17, 18, 19, 20, 21, 22,
3464 /* 'X' */ 23, 24, 25, -1, -1, -1, -1, -1,
3465 /* '`' */ -1, 26, 27, 28, 29, 30, 31, 32,
3466 /* 'h' */ 33, 34, 35, 36, 37, 38, 39, 40,
3467 /* 'p' */ 41, 42, 43, 44, 45, 46, 47, 48,
3468 /* 'x' */ 49, 50, 51, -1, -1, -1, -1, -1
3471 static int base64toCFData(vchar_t
*textin
, CFDataRef
*dataRef
)
3479 uint8_t *textcur
= (__typeof__(textcur
))textin
->v
;
3480 int len
= textin
->l
;
3483 tmpbuf
= malloc(len
); // len of result will be less than encoded len
3484 if (tmpbuf
== NULL
) {
3485 yyerror("memory error - could not allocate buffer for certificate reference conversion from base-64.");
3489 for (i
= 0; i
< len
; i
++) {
3493 else if (!isspace(c
))
3495 if (base64_DecodeTable
[c
] < 0)
3499 acc
+= base64_DecodeTable
[c
];
3500 if (0 == (cntr
& 0x3)) {
3501 tmpbuf
[tmpbufpos
++] = (acc
>> 16) & 0xff;
3503 tmpbuf
[tmpbufpos
++] = (acc
>> 8) & 0xff;
3505 tmpbuf
[tmpbufpos
++] = acc
& 0xff;
3508 *dataRef
= CFDataCreate(NULL
, tmpbuf
, tmpbufpos
);