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>
71 #include "isakmp_var.h"
74 #include "isakmp_xauth.h"
75 #include "isakmp_cfg.h"
78 #include "localconf.h"
81 #include "ipsec_doi.h"
82 #include "algorithm.h"
86 #include "crypto_openssl.h"
87 #include "crypto_cssm.h"
94 #include <CoreFoundation/CoreFoundation.h>
95 #include "remoteconf.h"
96 #include "vpn_control.h"
98 #include <Security/SecCertificate.h>
99 #include <Security/SecCertificatePriv.h>
101 #include "vpn_control_var.h"
104 #define OUTBOUND_SA 0
107 #define CERT_CHECKID_FROM_PEER 0
108 #define CERT_CHECKID_FROM_RMCONFIG 1
111 #define INITDHVAL(a, s, d, t) \
114 buf.v = str2val((s), 16, &buf.l); \
115 memset(&a, 0, sizeof(struct dhgroup)); \
117 a.prime = vdup(&buf); \
120 racoon_free(buf.v); \
122 #else /* HAVE_OPENSSL */
123 #define INITDHVAL(a, s, d, t) \
126 buf.v = str2val((s), 16, &buf.l); \
127 memset(&a, 0, sizeof(struct dhgroup)); \
130 a.prime = vdup(&buf); \
133 racoon_free(buf.v); \
135 #endif /* HAVE_OPENSSL */
137 struct dhgroup dh_modp768
;
138 struct dhgroup dh_modp1024
;
139 struct dhgroup dh_modp1536
;
140 struct dhgroup dh_modp2048
;
141 struct dhgroup dh_modp3072
;
142 struct dhgroup dh_modp4096
;
143 struct dhgroup dh_modp6144
;
144 struct dhgroup dh_modp8192
;
147 static int oakley_check_dh_pub (vchar_t
*, vchar_t
**);
148 static int oakley_compute_keymat_x (phase2_handle_t
*, int, int);
149 static int get_cert_fromlocal (phase1_handle_t
*, int);
150 static int oakley_check_certid (phase1_handle_t
*iph1
);
151 static int oakley_check_certid_1 (vchar_t
*, int, int, void*, cert_status_t
*certStatus
);
153 static int check_typeofcertname (int, int);
155 static cert_t
*save_certbuf (struct isakmp_gen
*);
156 static int oakley_padlen (int, int);
158 static int base64toCFData (vchar_t
*, CFDataRef
*);
159 static cert_t
*oakley_appendcert_to_certchain (cert_t
*, cert_t
*);
162 oakley_get_defaultlifetime()
164 return OAKLEY_ATTR_SA_LD_SEC_DEFAULT
;
171 INITDHVAL(dh_modp768
, OAKLEY_PRIME_MODP768
,
172 OAKLEY_ATTR_GRP_DESC_MODP768
, OAKLEY_ATTR_GRP_TYPE_MODP
);
173 INITDHVAL(dh_modp1024
, OAKLEY_PRIME_MODP1024
,
174 OAKLEY_ATTR_GRP_DESC_MODP1024
, OAKLEY_ATTR_GRP_TYPE_MODP
);
175 INITDHVAL(dh_modp1536
, OAKLEY_PRIME_MODP1536
,
176 OAKLEY_ATTR_GRP_DESC_MODP1536
, OAKLEY_ATTR_GRP_TYPE_MODP
);
177 INITDHVAL(dh_modp2048
, OAKLEY_PRIME_MODP2048
,
178 OAKLEY_ATTR_GRP_DESC_MODP2048
, OAKLEY_ATTR_GRP_TYPE_MODP
);
179 INITDHVAL(dh_modp3072
, OAKLEY_PRIME_MODP3072
,
180 OAKLEY_ATTR_GRP_DESC_MODP3072
, OAKLEY_ATTR_GRP_TYPE_MODP
);
181 INITDHVAL(dh_modp4096
, OAKLEY_PRIME_MODP4096
,
182 OAKLEY_ATTR_GRP_DESC_MODP4096
, OAKLEY_ATTR_GRP_TYPE_MODP
);
183 INITDHVAL(dh_modp6144
, OAKLEY_PRIME_MODP6144
,
184 OAKLEY_ATTR_GRP_DESC_MODP6144
, OAKLEY_ATTR_GRP_TYPE_MODP
);
185 INITDHVAL(dh_modp8192
, OAKLEY_PRIME_MODP8192
,
186 OAKLEY_ATTR_GRP_DESC_MODP8192
, OAKLEY_ATTR_GRP_TYPE_MODP
);
192 oakley_dhgrp_free(struct dhgroup
*dhgrp
)
194 VPTRINIT(dhgrp
->prime
);
195 VPTRINIT(dhgrp
->curve_a
);
196 VPTRINIT(dhgrp
->curve_b
);
197 VPTRINIT(dhgrp
->order
);
203 * The length of the Diffie-Hellman public value MUST be equal to the
204 * length of the prime modulus over which the exponentiation was
205 * performed, prepending zero bits to the value if necessary.
208 oakley_check_dh_pub(vchar_t
*prime
, vchar_t
**pub0
)
211 vchar_t
*pub
= *pub0
;
213 if (prime
->l
== pub
->l
)
216 if (prime
->l
< pub
->l
) {
217 /* what should i do ? */
219 "invalid public information was generated.\n");
223 /* prime->l > pub->l */
224 tmp
= vmalloc(prime
->l
);
227 "failed to get DH buffer.\n");
230 memcpy(tmp
->v
+ prime
->l
- pub
->l
, pub
->v
, pub
->l
);
239 * compute sharing secret of DH
240 * IN: *dh, *pub, *priv, *pub_p
245 oakley_dh_compute(const struct dhgroup
*dh
, vchar_t
*pub
, vchar_t
*priv
, vchar_t
*pub_p
, vchar_t
**gxy
)
248 struct timeval start
, end
;
250 if ((*gxy
= vmalloc(dh
->prime
->l
)) == NULL
) {
252 "failed to get DH buffer.\n");
257 gettimeofday(&start
, NULL
);
260 case OAKLEY_ATTR_GRP_TYPE_MODP
:
261 if (eay_dh_compute(dh
->prime
, dh
->gen1
, pub
, priv
, pub_p
, gxy
) < 0) {
263 "failed to compute dh value.\n");
267 case OAKLEY_ATTR_GRP_TYPE_ECP
:
268 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
270 "dh type %d isn't supported.\n", dh
->type
);
274 "invalid dh type %d.\n", dh
->type
);
279 gettimeofday(&end
, NULL
);
280 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
281 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
282 timedelta(&start
, &end
));
285 plog(ASL_LEVEL_DEBUG
, "compute DH's shared.\n");
291 oakley_dh_compute(const struct dhgroup
*dh
, vchar_t
*pub_p
, size_t publicKeySize
, vchar_t
**gxy
, SecDHContext
*dhC
)
294 vchar_t
*computed_key
= NULL
;
295 size_t computed_keylen
;
299 struct timeval start
, end
;
300 gettimeofday(&start
, NULL
);
303 plog(ASL_LEVEL_DEBUG
, "compute DH result.\n");
305 maxKeyLen
= SecDHGetMaxKeyLength(*dhC
);
306 computed_key
= vmalloc(maxKeyLen
);
307 if (computed_key
== NULL
) {
308 plog(ASL_LEVEL_ERR
, "memory error.\n");
311 computed_keylen
= computed_key
->l
;
312 if (SecDHComputeKey(*dhC
, (uint8_t*)pub_p
->v
+ (maxKeyLen
- publicKeySize
), publicKeySize
,
313 (uint8_t*)computed_key
->v
, &computed_keylen
)) {
314 plog(ASL_LEVEL_ERR
, "failed to compute dh value.\n");
319 gettimeofday(&end
, NULL
);
320 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
321 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
322 timedelta(&start
, &end
));
325 *gxy
= vmalloc(maxKeyLen
);
327 plog(ASL_LEVEL_ERR
, "memory error.\n");
330 memcpy((*gxy
)->v
+ (maxKeyLen
- computed_keylen
), computed_key
->v
, computed_keylen
);
331 plog(ASL_LEVEL_DEBUG
, "compute DH's shared.\n");
352 * generate values of DH
358 oakley_dh_generate(const struct dhgroup
*dh
, vchar_t
**pub
, vchar_t
**priv
)
361 struct timeval start
, end
;
362 gettimeofday(&start
, NULL
);
365 case OAKLEY_ATTR_GRP_TYPE_MODP
:
366 if (eay_dh_generate(dh
->prime
, dh
->gen1
, dh
->gen2
, pub
, priv
) < 0) {
368 "failed to compute dh value.\n");
373 case OAKLEY_ATTR_GRP_TYPE_ECP
:
374 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
376 "dh type %d isn't supported.\n", dh
->type
);
380 "invalid dh type %d.\n", dh
->type
);
385 gettimeofday(&end
, NULL
);
386 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
387 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
388 timedelta(&start
, &end
));
391 if (oakley_check_dh_pub(dh
->prime
, pub
) != 0)
394 plog(ASL_LEVEL_DEBUG
, "compute DH's private.\n");
395 plog(ASL_LEVEL_DEBUG
, "compute DH's public.\n");
401 oakley_dh_generate(const struct dhgroup
*dh
, vchar_t
**pub
, size_t *publicKeySize
, SecDHContext
*dhC
)
403 vchar_t
*public = NULL
;
407 struct timeval start
, end
;
408 gettimeofday(&start
, NULL
);
411 plog(ASL_LEVEL_DEBUG
, "generate DH key pair.\n");
414 case OAKLEY_ATTR_GRP_TYPE_MODP
:
415 #define SECDH_MODP_GENERATOR 2
416 if (SecDHCreate(SECDH_MODP_GENERATOR
, (uint8_t*)dh
->prime
->v
, dh
->prime
->l
, 0, NULL
, 0, dhC
)) {
417 plog(ASL_LEVEL_ERR
, "failed to create dh context.\n");
420 maxKeyLen
= SecDHGetMaxKeyLength(*dhC
);
421 public = vmalloc(maxKeyLen
);
422 *publicKeySize
= public->l
;
423 if (public == NULL
) {
424 plog(ASL_LEVEL_ERR
, "memory error.\n");
427 if (SecDHGenerateKeypair(*dhC
, (uint8_t*)public->v
, publicKeySize
)) {
428 plog(ASL_LEVEL_ERR
, "failed to generate dh key pair.\n");
431 plog(ASL_LEVEL_DEBUG
, "got DH key pair.\n");
433 *pub
= vmalloc(maxKeyLen
);
435 plog(ASL_LEVEL_ERR
, "memory error.\n");
438 /* copy and fill with leading zeros */
439 memcpy((*pub
)->v
+ (maxKeyLen
- *publicKeySize
), public->v
, *publicKeySize
);
442 case OAKLEY_ATTR_GRP_TYPE_ECP
:
443 case OAKLEY_ATTR_GRP_TYPE_EC2N
:
445 "dh type %d isn't supported.\n", dh
->type
);
449 "invalid dh type %d.\n", dh
->type
);
454 gettimeofday(&end
, NULL
);
455 plog(ASL_LEVEL_NOTICE
, "%s(%s%d): %8.6f", __func__
,
456 s_attr_isakmp_group(dh
->type
), dh
->prime
->l
<< 3,
457 timedelta(&start
, &end
));
460 if (oakley_check_dh_pub(dh
->prime
, pub
) != 0) {
461 plog(ASL_LEVEL_DEBUG
, "failed DH public key size check.\n");
465 //plogdump(ASL_LEVEL_DEBUG, (*pub)->v, (*pub)->l, "compute DH's public.\n");
483 * copy pre-defined dhgroup values.
486 oakley_setdhgroup(int group
, struct dhgroup
**dhgrp
)
490 *dhgrp
= NULL
; /* just make sure, initialize */
492 g
= alg_oakley_dhdef_group(group
);
495 "invalid DH parameter grp=%d.\n", group
);
499 if (!g
->type
|| !g
->prime
|| !g
->gen1
) {
502 "unsupported DH parameters grp=%d.\n", group
);
506 *dhgrp
= racoon_calloc(1, sizeof(struct dhgroup
));
507 if (*dhgrp
== NULL
) {
509 "failed to get DH buffer.\n");
513 /* set defined dh vlaues */
514 memcpy(*dhgrp
, g
, sizeof(*g
));
515 (*dhgrp
)->prime
= vdup(g
->prime
);
523 * NOTE: we do not support prf with different input/output bitwidth,
524 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example) in
525 * oakley_compute_keymat(). If you add support for such prf function,
526 * modify oakley_compute_keymat() accordingly.
529 oakley_prf(vchar_t
*key
, vchar_t
*buf
, phase1_handle_t
*iph1
)
532 int type
= OAKLEY_ATTR_HASH_ALG_MD5
;
534 if (iph1
->approval
== NULL
) {
535 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
537 * it's before negotiating hash algorithm.
538 * We use md5 as default.
540 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
544 type
= iph1
->approval
->hashtype
;
546 res
= alg_oakley_hmacdef_one(type
, key
, buf
);
549 "invalid hmac algorithm %d.\n", type
);
560 oakley_hash(vchar_t
*buf
, phase1_handle_t
*iph1
)
563 int type
= OAKLEY_ATTR_HASH_ALG_MD5
;
565 if (iph1
->approval
== NULL
) {
566 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
568 * it's before negotiating hash algorithm.
569 * We use md5 as default.
571 type
= OAKLEY_ATTR_HASH_ALG_MD5
;
574 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
575 type
= iph1
->approval
->hashtype
;
579 res
= alg_oakley_hashdef_one(type
, buf
);
582 "invalid hash algorithm %d.\n", type
);
591 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
594 oakley_compute_keymat(phase2_handle_t
*iph2
, int side
)
598 /* compute sharing secret of DH when PFS */
599 if (iph2
->approval
->pfs_group
&& iph2
->dhpub_p
) {
601 if (oakley_dh_compute(iph2
->pfsgrp
, iph2
->dhpub
,
602 iph2
->dhpriv
, iph2
->dhpub_p
, &iph2
->dhgxy
) < 0)
604 if (oakley_dh_compute(iph2
->pfsgrp
, iph2
->dhpub_p
, iph2
->publicKeySize
, &iph2
->dhgxy
, &iph2
->dhC
) < 0)
610 if (oakley_compute_keymat_x(iph2
, side
, INBOUND_SA
) < 0
611 || oakley_compute_keymat_x(iph2
, side
, OUTBOUND_SA
) < 0)
614 plog(ASL_LEVEL_DEBUG
, "KEYMAT computed.\n");
624 * KEYMAT = prf(SKEYID_d, protocol | SPI | Ni_b | Nr_b).
625 * If PFS is desired and KE payloads were exchanged,
626 * KEYMAT = prf(SKEYID_d, g(qm)^xy | protocol | SPI | Ni_b | Nr_b)
628 * NOTE: we do not support prf with different input/output bitwidth,
629 * so we do not implement RFC2409 Appendix B (DOORAK-MAC example).
632 oakley_compute_keymat_x(phase2_handle_t
*iph2
, int side
, int sa_dir
)
634 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
639 int dupkeymat
; /* generate K[1-dupkeymat] */
642 int encklen
, authklen
, l
;
644 pfs
= ((iph2
->approval
->pfs_group
&& iph2
->dhgxy
) ? 1 : 0);
646 len
= pfs
? iph2
->dhgxy
->l
: 0;
648 + sizeof(u_int32_t
) /* XXX SPI size */
654 "failed to get keymat buffer.\n");
658 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
663 memcpy(p
, iph2
->dhgxy
->v
, iph2
->dhgxy
->l
);
670 memcpy(p
, (sa_dir
== INBOUND_SA
? &pr
->spi
: &pr
->spi_p
),
672 p
+= sizeof(pr
->spi
);
674 bp
= (side
== INITIATOR
? iph2
->nonce
: iph2
->nonce_p
);
675 memcpy(p
, bp
->v
, bp
->l
);
678 bp
= (side
== INITIATOR
? iph2
->nonce_p
: iph2
->nonce
);
679 memcpy(p
, bp
->v
, bp
->l
);
683 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "KEYMAT compute with\n");
686 res
= oakley_prf(iph2
->ph1
->skeyid_d
, buf
, iph2
->ph1
);
690 /* compute key length needed */
691 encklen
= authklen
= 0;
692 switch (pr
->proto_id
) {
693 case IPSECDOI_PROTO_IPSEC_ESP
:
694 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
695 l
= alg_ipsec_encdef_keylen(tr
->trns_id
,
700 l
= alg_ipsec_hmacdef_hashlen(tr
->authtype
);
705 case IPSECDOI_PROTO_IPSEC_AH
:
706 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
707 l
= alg_ipsec_hmacdef_hashlen(tr
->trns_id
);
715 plog(ASL_LEVEL_DEBUG
, "encklen=%d authklen=%d\n",
718 dupkeymat
= (encklen
+ authklen
) / 8 / res
->l
;
719 dupkeymat
+= 2; /* safety mergin */
722 //plog(ASL_LEVEL_DEBUG,
723 // "generating %zu bits of key (dupkeymat=%d)\n",
724 // dupkeymat * 8 * res->l, dupkeymat);
725 if (0 < --dupkeymat
) {
726 vchar_t
*prev
= res
; /* K(n-1) */
727 vchar_t
*seed
= NULL
; /* seed for Kn */
731 * generating long key (isakmp-oakley-08 5.5)
732 * KEYMAT = K1 | K2 | K3 | ...
734 * src = [ g(qm)^xy | ] protocol | SPI | Ni_b | Nr_b
735 * K1 = prf(SKEYID_d, src)
736 * K2 = prf(SKEYID_d, K1 | src)
737 * K3 = prf(SKEYID_d, K2 | src)
738 * Kn = prf(SKEYID_d, K(n-1) | src)
740 //plog(ASL_LEVEL_DEBUG,
741 // "generating K1...K%d for KEYMAT.\n",
744 seed
= vmalloc(prev
->l
+ buf
->l
);
747 "failed to get keymat buffer.\n");
748 if (prev
&& prev
!= res
)
753 while (dupkeymat
--) {
754 vchar_t
*this = NULL
; /* Kn */
757 memcpy(seed
->v
, prev
->v
, prev
->l
);
758 memcpy(seed
->v
+ prev
->l
, buf
->v
, buf
->l
);
759 this = oakley_prf(iph2
->ph1
->skeyid_d
, seed
,
763 "oakley_prf memory overflow\n");
764 if (prev
&& prev
!= res
)
771 update_prev
= (prev
&& prev
== res
) ? 1 : 0;
774 res
= vrealloc(res
, l
+ this->l
);
781 "failed to get keymat buffer.\n");
782 if (prev
&& prev
!= res
)
788 memcpy(res
->v
+ l
, this->v
, this->l
);
790 if (prev
&& prev
!= res
)
796 if (prev
&& prev
!= res
)
801 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "");
803 if (sa_dir
== INBOUND_SA
)
814 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
836 * compute HASH(3) prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b)
837 * see seciton 5.5 Phase 2 - Quick Mode in isakmp-oakley-05.
840 oakley_compute_hash3(phase1_handle_t
*iph1
, u_int32_t msgid
, vchar_t
*body
)
842 vchar_t
*buf
= 0, *res
= 0;
847 len
= 1 + sizeof(u_int32_t
) + body
->l
;
850 plog(ASL_LEVEL_NOTICE
,
851 "failed to get hash buffer\n");
857 memcpy(buf
->v
+ 1, (char *)&msgid
, sizeof(msgid
));
859 memcpy(buf
->v
+ 1 + sizeof(u_int32_t
), body
->v
, body
->l
);
862 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
868 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH computed:\n");
877 * compute HASH type of prf(SKEYID_a, M-ID | buffer)
879 * for quick mode HASH(1):
880 * prf(SKEYID_a, M-ID | SA | Ni [ | KE ] [ | IDci | IDcr ])
881 * for quick mode HASH(2):
882 * prf(SKEYID_a, M-ID | Ni_b | SA | Nr [ | KE ] [ | IDci | IDcr ])
883 * for Informational exchange:
884 * prf(SKEYID_a, M-ID | N/D)
887 oakley_compute_hash1(phase1_handle_t
*iph1
, u_int32_t msgid
, vchar_t
*body
)
889 vchar_t
*buf
= NULL
, *res
= NULL
;
895 len
= sizeof(u_int32_t
) + body
->l
;
898 plog(ASL_LEVEL_NOTICE
,
899 "failed to get hash buffer\n");
905 memcpy(buf
->v
, (char *)&msgid
, sizeof(msgid
));
906 p
+= sizeof(u_int32_t
);
908 memcpy(p
, body
->v
, body
->l
);
911 res
= oakley_prf(iph1
->skeyid_a
, buf
, iph1
);
917 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH computed:\n");
926 * compute phase1 HASH
928 * I-digest = prf(SKEYID, g^i | g^r | CKY-I | CKY-R | SAi_b | ID_i1_b)
929 * R-digest = prf(SKEYID, g^r | g^i | CKY-R | CKY-I | SAi_b | ID_r1_b)
930 * for gssapi, also include all GSS tokens, and call gss_wrap on the result
933 oakley_ph1hash_common(phase1_handle_t
*iph1
, int sw
)
935 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
943 + sizeof(cookie_t
) * 2
945 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
950 "failed to get hash buffer\n");
956 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
957 memcpy(p
, bp
->v
, bp
->l
);
960 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
961 memcpy(p
, bp
->v
, bp
->l
);
964 if (iph1
->side
== INITIATOR
)
965 bp2
= (sw
== GENERATE
?
966 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
968 bp2
= (sw
== GENERATE
?
969 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
970 bl
= sizeof(cookie_t
);
974 if (iph1
->side
== INITIATOR
)
975 bp2
= (sw
== GENERATE
?
976 (char *)&iph1
->index
.r_ck
: (char *)&iph1
->index
.i_ck
);
978 bp2
= (sw
== GENERATE
?
979 (char *)&iph1
->index
.i_ck
: (char *)&iph1
->index
.r_ck
);
980 bl
= sizeof(cookie_t
);
985 memcpy(p
, bp
->v
, bp
->l
);
988 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
989 memcpy(p
, bp
->v
, bp
->l
);
993 res
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
1006 * compute HASH_I on base mode.
1008 * HASH_I = prf(SKEYID, g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
1010 * HASH_I = prf(hash(Ni_b | Nr_b), g^xi | CKY-I | CKY-R | SAi_b | IDii_b)
1013 oakley_ph1hash_base_i(phase1_handle_t
*iph1
, int sw
)
1015 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
1016 vchar_t
*hashkey
= NULL
;
1017 vchar_t
*hash
= NULL
; /* for signature mode */
1023 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
1025 "invalid etype for this hash function\n");
1029 switch (AUTHMETHOD(iph1
)) {
1030 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1031 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1032 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1033 #ifdef ENABLE_HYBRID
1034 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
1035 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1036 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
1037 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1038 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
1039 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1041 if (iph1
->skeyid
== NULL
) {
1042 plog(ASL_LEVEL_ERR
, "no SKEYID found.\n");
1045 hashkey
= iph1
->skeyid
;
1048 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1049 #ifdef ENABLE_HYBRID
1050 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1051 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1052 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
1053 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1055 /* make hash for seed */
1056 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
1060 "failed to get hash buffer\n");
1065 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
1066 memcpy(p
, bp
->v
, bp
->l
);
1069 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
1070 memcpy(p
, bp
->v
, bp
->l
);
1073 hash
= oakley_hash(buf
, iph1
);
1084 "not supported authentication method %d\n",
1085 iph1
->approval
->authmethod
);
1090 len
= (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
1091 + sizeof(cookie_t
) * 2
1093 + (sw
== GENERATE
? iph1
->id
->l
: iph1
->id_p
->l
);
1097 "failed to get hash buffer\n");
1102 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
1103 memcpy(p
, bp
->v
, bp
->l
);
1106 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1107 p
+= sizeof(cookie_t
);
1108 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1109 p
+= sizeof(cookie_t
);
1111 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
1114 bp
= (sw
== GENERATE
? iph1
->id
: iph1
->id_p
);
1115 memcpy(p
, bp
->v
, bp
->l
);
1118 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "HASH_I with:\n");
1121 res
= oakley_prf(hashkey
, buf
, iph1
);
1127 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH_I computed:\n");
1138 * compute HASH_R on base mode for signature method.
1140 * HASH_R = prf(hash(Ni_b | Nr_b), g^xi | g^xr | CKY-I | CKY-R | SAi_b | IDii_b)
1143 oakley_ph1hash_base_r(phase1_handle_t
*iph1
, int sw
)
1145 vchar_t
*buf
= NULL
, *res
= NULL
, *bp
;
1146 vchar_t
*hash
= NULL
;
1152 if (iph1
->etype
!= ISAKMP_ETYPE_BASE
) {
1154 "invalid etype for this hash function\n");
1158 switch(AUTHMETHOD(iph1
)) {
1159 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1160 #ifdef ENABLE_HYBRID
1161 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1162 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1163 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
1164 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1165 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
1170 "not supported authentication method %d\n",
1171 iph1
->approval
->authmethod
);
1176 /* make hash for seed */
1177 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
1181 "failed to get hash buffer\n");
1186 bp
= (sw
== GENERATE
? iph1
->nonce_p
: iph1
->nonce
);
1187 memcpy(p
, bp
->v
, bp
->l
);
1190 bp
= (sw
== GENERATE
? iph1
->nonce
: iph1
->nonce_p
);
1191 memcpy(p
, bp
->v
, bp
->l
);
1194 hash
= oakley_hash(buf
, iph1
);
1200 /* make really hash */
1201 len
= (sw
== GENERATE
? iph1
->dhpub_p
->l
: iph1
->dhpub
->l
)
1202 + (sw
== GENERATE
? iph1
->dhpub
->l
: iph1
->dhpub_p
->l
)
1203 + sizeof(cookie_t
) * 2
1205 + (sw
== GENERATE
? iph1
->id_p
->l
: iph1
->id
->l
);
1209 "failed to get hash buffer\n");
1215 bp
= (sw
== GENERATE
? iph1
->dhpub_p
: iph1
->dhpub
);
1216 memcpy(p
, bp
->v
, bp
->l
);
1219 bp
= (sw
== GENERATE
? iph1
->dhpub
: iph1
->dhpub_p
);
1220 memcpy(p
, bp
->v
, bp
->l
);
1223 memcpy(p
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1224 p
+= sizeof(cookie_t
);
1225 memcpy(p
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1226 p
+= sizeof(cookie_t
);
1228 memcpy(p
, iph1
->sa
->v
, iph1
->sa
->l
);
1231 bp
= (sw
== GENERATE
? iph1
->id_p
: iph1
->id
);
1232 memcpy(p
, bp
->v
, bp
->l
);
1235 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "HASH_R with:\n");
1238 res
= oakley_prf(hash
, buf
, iph1
);
1244 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "HASH_R computed:\n");
1256 oakley_verify_userid(phase1_handle_t
*iph1
)
1260 int user_id_found
= 0;
1261 #ifndef HAVE_OPENSSL
1262 SecCertificateRef certificate
;
1263 CFArrayRef commonNames
;
1266 #endif /* HAVE_OPENSSL */
1268 for (p
= iph1
->cert_p
; p
; p
= p
->chain
) {
1270 user_id
= eay_get_x509_common_name(&p
->cert
); //%%%%%%%% fix this
1273 // the following functions will check if user_id == 0
1274 if (open_dir_authorize_id(user_id
, iph1
->rmconf
->open_dir_auth_group
)) {
1280 #else /* HAVE_OPENSSL */
1281 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(&p
->cert
);
1282 if (certificate
== NULL
) {
1284 "ovuid failed to get SecCertificateRef\n");
1288 commonNames
= SecCertificateCopyCommonNames(certificate
);
1289 if (commonNames
== NULL
) {
1291 "ovuid failed to get commonNames\n");
1292 CFRelease(certificate
);
1296 l
= CFArrayGetCount(commonNames
);
1297 for (i
= 0; i
< l
; i
++) {
1298 name
= CFArrayGetValueAtIndex(commonNames
, i
);
1299 user_id
= vmalloc(CFStringGetMaximumSizeForEncoding(CFStringGetLength(name
),
1300 kCFStringEncodingUTF8
) + 1);
1302 if (CFStringGetCString(name
, user_id
->v
, user_id
->l
,
1303 kCFStringEncodingUTF8
)) {
1305 // the following functions will check if user_id == 0
1306 if (open_dir_authorize_id(user_id
, iph1
->rmconf
->open_dir_auth_group
)) {
1308 CFRelease(certificate
);
1309 CFRelease(commonNames
);
1316 CFRelease(certificate
);
1317 CFRelease(commonNames
);
1318 #endif /* HAVE_OPENSSL */
1320 if (user_id_found
) {
1322 "the peer is not authorized for access.\n");
1325 "the peer is not authorized for access - user ID not found.\n");
1327 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1329 #endif /* HAVE_OPENDIR */
1332 * compute each authentication method in phase 1.
1336 * other: error to be reply with notification.
1337 * the value is notification type.
1340 oakley_validate_auth(phase1_handle_t
*iph1
)
1342 vchar_t
*my_hash
= NULL
;
1345 struct timeval start
, end
;
1347 SecKeyRef publicKeyRef
= NULL
;
1350 gettimeofday(&start
, NULL
);
1353 switch (AUTHMETHOD(iph1
)) {
1354 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1355 #ifdef ENABLE_HYBRID
1356 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
1357 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1363 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1365 "few isakmp message received.\n");
1366 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1368 #ifdef ENABLE_HYBRID
1369 if (AUTHMETHOD(iph1
) == FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
&&
1370 ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0))
1372 plog(ASL_LEVEL_ERR
, "No SIG was passed, "
1373 "hybrid auth is enabled, "
1374 "but peer is no Xauth compliant\n");
1375 return ISAKMP_NTYPE_SITUATION_NOT_SUPPORTED
;
1379 r_hash
= (caddr_t
)(iph1
->pl_hash
+ 1);
1381 //plogdump(ASL_LEVEL_DEBUG, r_hash,
1382 // ntohs(iph1->pl_hash->h.len) - sizeof(*iph1->pl_hash), "HASH received:\n");
1384 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1385 switch (iph1
->etype
) {
1386 case ISAKMP_ETYPE_IDENT
:
1387 case ISAKMP_ETYPE_AGG
:
1388 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1390 case ISAKMP_ETYPE_BASE
:
1391 if (iph1
->side
== INITIATOR
)
1392 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1394 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1398 "invalid etype %d\n", iph1
->etype
);
1399 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1402 if (my_hash
== NULL
)
1403 return ISAKMP_INTERNAL_ERROR
;
1405 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1409 plog(ASL_LEVEL_ERR
, "HASH mismatched\n");
1410 return ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1413 plog(ASL_LEVEL_DEBUG
, "HASH for PSK validated.\n");
1416 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1417 #ifdef ENABLE_HYBRID
1418 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1419 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
1420 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1427 if (iph1
->id_p
== NULL
) {
1429 "no ID payload was passed.\n");
1430 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1432 if (iph1
->sig_p
== NULL
) {
1434 "no SIG payload was passed.\n");
1435 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1438 plog(ASL_LEVEL_DEBUG
, "SIGN passed\n");
1440 /* get peer's cert */
1441 switch (iph1
->rmconf
->getcert_method
) {
1442 case ISAKMP_GETCERT_PAYLOAD
:
1443 if (iph1
->cert_p
== NULL
) {
1445 "no peer's CERT payload found.\n");
1446 return ISAKMP_INTERNAL_ERROR
;
1451 "invalid getcert_mothod: %d\n",
1452 iph1
->rmconf
->getcert_method
);
1453 return ISAKMP_INTERNAL_ERROR
;
1456 /* compare ID payload and certificate name */
1457 if (iph1
->rmconf
->verify_cert
&&
1458 (error
= oakley_check_certid(iph1
)) != 0)
1462 /* check cert common name against Open Directory authentication group */
1463 if (iph1
->rmconf
->cert_verification_option
== VERIFICATION_OPTION_OPEN_DIR
) {
1464 if (oakley_verify_userid(iph1
)) {
1465 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1468 #endif /* HAVE_OPENDIR */
1470 /* verify certificate */
1471 if (iph1
->rmconf
->verify_cert
1472 && iph1
->rmconf
->getcert_method
== ISAKMP_GETCERT_PAYLOAD
) {
1473 certtype
= iph1
->rmconf
->certtype
;
1474 #ifdef ENABLE_HYBRID
1475 switch (AUTHMETHOD(iph1
)) {
1476 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1477 certtype
= iph1
->cert_p
->type
;
1484 case ISAKMP_CERT_X509SIGN
:
1486 /* use ID from remote configuration */
1487 /* check each ID in list */
1488 struct idspec
*id_spec
;
1489 CFStringRef hostname
= NULL
;
1491 struct genlist_entry
*gpb
= NULL
;
1493 if (iph1
->rmconf
->cert_verification_option
== VERIFICATION_OPTION_PEERS_IDENTIFIER
) {
1494 id_spec
= genlist_next(iph1
->rmconf
->idvl_p
, &gpb
); /* expect only one id */
1495 if (id_spec
->idtype
== IDTYPE_ADDRESS
) {
1496 switch ((ALIGNED_CAST(struct sockaddr_storage
*)(id_spec
->id
->v
))->ss_family
) {
1498 peers_id
= inet_ntoa((ALIGNED_CAST(struct sockaddr_in
*)(id_spec
->id
->v
))->sin_addr
);
1499 hostname
= CFStringCreateWithCString(NULL
, peers_id
, kCFStringEncodingUTF8
);
1503 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
; /* not currently supported for embedded */
1508 "unknown address type for peers identifier.\n");
1509 return ISAKMP_NTYPE_AUTHENTICATION_FAILED
;
1513 hostname
= CFStringCreateWithBytes(NULL
, (u_int8_t
*)id_spec
->id
->v
, id_spec
->id
->l
, kCFStringEncodingUTF8
, FALSE
);
1515 error
= crypto_cssm_check_x509cert(oakley_get_peer_cert_from_certchain(iph1
), iph1
->cert_p
, hostname
, &publicKeyRef
);
1517 CFRelease(hostname
);
1523 "no supported certtype %d\n", certtype
);
1524 return ISAKMP_INTERNAL_ERROR
;
1528 "the peer's certificate is not verified.\n");
1529 return ISAKMP_NTYPE_INVALID_CERT_AUTHORITY
;
1533 plog(ASL_LEVEL_DEBUG
, "CERT validated\n");
1535 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1537 switch (iph1
->etype
) {
1538 case ISAKMP_ETYPE_IDENT
:
1539 case ISAKMP_ETYPE_AGG
:
1540 my_hash
= oakley_ph1hash_common(iph1
, VALIDATE
);
1542 case ISAKMP_ETYPE_BASE
:
1543 if (iph1
->side
== INITIATOR
)
1544 my_hash
= oakley_ph1hash_base_r(iph1
, VALIDATE
);
1546 my_hash
= oakley_ph1hash_base_i(iph1
, VALIDATE
);
1550 "invalid etype %d\n", iph1
->etype
);
1551 return ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
;
1554 if (my_hash
== NULL
)
1555 return ISAKMP_INTERNAL_ERROR
;
1558 certtype
= iph1
->rmconf
->certtype
;
1559 #ifdef ENABLE_HYBRID
1560 switch (AUTHMETHOD(iph1
)) {
1561 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
1562 certtype
= iph1
->cert_p
->type
;
1568 /* check signature */
1570 case ISAKMP_CERT_X509SIGN
:
1571 if (publicKeyRef
== NULL
) {
1572 plog(ASL_LEVEL_ERR
, "@@@@@@ publicKeyRef is NULL\n");
1574 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1575 error
= crypto_cssm_verify_x509sign(publicKeyRef
, my_hash
, iph1
->sig_p
, FALSE
);
1578 plog(ASL_LEVEL_ERR
, "error verifying signature %s\n", GetSecurityErrorString(error
));
1581 CFRelease(publicKeyRef
);
1585 "no supported certtype %d\n",
1588 return ISAKMP_INTERNAL_ERROR
;
1595 return ISAKMP_NTYPE_INVALID_SIGNATURE
;
1597 plog(ASL_LEVEL_DEBUG
, "SIG authenticated\n");
1600 #ifdef ENABLE_HYBRID
1601 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1603 if ((iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) == 0) {
1604 plog(ASL_LEVEL_ERR
, "No SIG was passed, "
1605 "hybrid auth is enabled, "
1606 "but peer is no Xauth compliant\n");
1607 return ISAKMP_NTYPE_SITUATION_NOT_SUPPORTED
;
1610 plog(ASL_LEVEL_NOTICE
, "No SIG was passed, "
1611 "but hybrid auth is enabled\n");
1617 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1618 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1619 #ifdef ENABLE_HYBRID
1620 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
1621 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1622 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
1623 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1625 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
) {
1627 "few isakmp message received.\n");
1628 return ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1631 "not supported authmethod type %s\n",
1632 s_oakley_attr_method(iph1
->approval
->authmethod
));
1633 return ISAKMP_INTERNAL_ERROR
;
1636 "invalid authmethod %d why ?\n",
1637 iph1
->approval
->authmethod
);
1638 return ISAKMP_INTERNAL_ERROR
;
1641 gettimeofday(&end
, NULL
);
1642 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f", __func__
,
1643 s_oakley_attr_method(iph1
->approval
->authmethod
),
1644 timedelta(&start
, &end
));
1651 oakley_find_status_in_certchain (cert_t
*certchain
, cert_status_t certStatus
)
1655 for (p
= certchain
; p
; p
= p
->chain
) {
1656 if (p
->status
== certStatus
) {
1665 oakley_vpncontrol_notify_ike_failed_if_mycert_invalid (phase1_handle_t
*iph1
, int notify_initiator
)
1667 #ifndef HAVE_OPENSSL
1668 int premature
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_PREMATURE
);
1669 int expired
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_EXPIRED
);
1670 if (premature
|| expired
) {
1671 u_int32_t fail_reason
;
1674 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_PREMATURE
;
1676 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_EXPIRED
;
1678 vpncontrol_notify_ike_failed(fail_reason
, notify_initiator
, iph1_get_remote_v4_address(iph1
), 0, NULL
);
1681 #endif /* HAVE_OPENSSL */
1685 /* get my certificate
1686 * NOTE: include certificate type.
1689 oakley_getmycert(phase1_handle_t
*iph1
)
1693 switch (iph1
->rmconf
->certtype
) {
1694 case ISAKMP_CERT_X509SIGN
:
1697 if ( !(err
= get_cert_fromlocal(iph1
, 1))){
1698 if (oakley_vpncontrol_notify_ike_failed_if_mycert_invalid(iph1
, FROM_LOCAL
)) {
1705 "Unknown certtype #%d\n",
1706 iph1
->rmconf
->certtype
);
1713 * get a CERT from local file.
1716 * my == 0 peer's cert.
1719 get_cert_fromlocal(phase1_handle_t
*iph1
, int my
)
1721 vchar_t
*cert
= NULL
;
1724 cert_status_t status
= CERT_STATUS_OK
;
1727 certpl
= &iph1
->cert
;
1729 certpl
= &iph1
->cert_p
;
1730 if (iph1
->rmconf
->identity_in_keychain
== 0) {
1731 plog(ASL_LEVEL_ERR
, "no CERT defined.\n");
1735 switch (iph1
->rmconf
->certtype
) {
1736 case ISAKMP_CERT_X509SIGN
:
1737 if (iph1
->rmconf
->identity_in_keychain
) {
1740 if (iph1
->rmconf
->keychainCertRef
== NULL
|| base64toCFData(iph1
->rmconf
->keychainCertRef
, &dataRef
))
1742 cert
= crypto_cssm_get_x509cert(dataRef
, &status
);
1743 plog(ASL_LEVEL_DEBUG
, "done with chking cert status %d\n",status
);
1749 "not supported certtype %d\n",
1750 iph1
->rmconf
->certtype
);
1756 "failed to get %s CERT.\n",
1757 my
? "my" : "peers");
1761 *certpl
= oakley_newcert();
1764 "failed to get cert buffer.\n");
1767 (*certpl
)->pl
= vmalloc(cert
->l
+ 1);
1768 if ((*certpl
)->pl
== NULL
) {
1770 "failed to get cert buffer\n");
1771 oakley_delcert(*certpl
);
1775 memcpy((*certpl
)->pl
->v
+ 1, cert
->v
, cert
->l
);
1776 (*certpl
)->pl
->v
[0] = iph1
->rmconf
->certtype
;
1777 (*certpl
)->type
= iph1
->rmconf
->certtype
;
1778 (*certpl
)->status
= status
;
1779 (*certpl
)->cert
.v
= (*certpl
)->pl
->v
+ 1;
1780 (*certpl
)->cert
.l
= (*certpl
)->pl
->l
- 1;
1782 plog(ASL_LEVEL_DEBUG
, "created CERT payload\n");
1796 oakley_getsign(phase1_handle_t
*iph1
)
1798 vchar_t
*privkey
= NULL
;
1801 switch (iph1
->rmconf
->certtype
) {
1802 case ISAKMP_CERT_X509SIGN
:
1803 // cert in keychain - use cssm to sign
1804 if (iph1
->rmconf
->identity_in_keychain
) {
1807 if (iph1
->rmconf
->keychainCertRef
== NULL
|| base64toCFData(iph1
->rmconf
->keychainCertRef
, &dataRef
))
1809 iph1
->sig
= crypto_cssm_getsign(dataRef
, iph1
->hash
);
1815 "Unknown certtype #%d\n",
1816 iph1
->rmconf
->certtype
);
1820 if (iph1
->sig
== NULL
) {
1821 plog(ASL_LEVEL_ERR
, "failed to sign.\n");
1825 //plogdump(ASL_LEVEL_DEBUG, iph1->sig->v, iph1->sig->l, "SIGN computed:\n");
1830 if (privkey
!= NULL
)
1837 oakley_verify_certid(phase1_handle_t
*iph1
)
1839 if (iph1
->rmconf
->verify_cert
&&
1840 oakley_check_certid(iph1
)){
1841 plog(ASL_LEVEL_DEBUG
,
1842 "Discarding CERT: does not match ID:\n");
1843 oakley_delcert(iph1
->cert_p
);
1844 iph1
->cert_p
= NULL
;
1849 oakley_check_certid_in_certchain(cert_t
*certchain
, int idtype
, int idlen
, void *id
)
1853 for (p
= certchain
; p
; p
= p
->chain
) {
1854 if (oakley_check_certid_1(&p
->cert
, idtype
, idlen
, id
, &p
->status
) == 0) {
1858 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1862 oakley_get_peer_cert_from_certchain(phase1_handle_t
* iph1
)
1865 struct ipsecdoi_id_b
*id_b
;
1869 if (!iph1
->id_p
|| !iph1
->cert_p
) {
1870 plog(ASL_LEVEL_ERR
, "no ID nor CERT found.\n");
1873 if (!iph1
->cert_p
->chain
) {
1874 // no chain: simply return the only cert
1875 return iph1
->cert_p
;
1878 id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
1879 peers_id
= id_b
+ 1;
1880 idlen
= iph1
->id_p
->l
- sizeof(*id_b
);
1881 for (p
= iph1
->cert_p
; p
; p
= p
->chain
) {
1882 if (oakley_check_certid_1(&p
->cert
, id_b
->type
, idlen
, peers_id
, &p
->status
) == 0) {
1890 * compare certificate name and ID value.
1893 oakley_check_certid(phase1_handle_t
*iph1
)
1895 struct ipsecdoi_id_b
*id_b
;
1897 u_int8_t doi_type
= 255;
1898 void *peers_id
= NULL
;
1900 /* use ID from peer */
1901 if (iph1
->id_p
== NULL
|| iph1
->cert_p
== NULL
) {
1902 plog(ASL_LEVEL_ERR
, "no ID nor CERT found.\n");
1903 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1905 id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
1906 doi_type
= id_b
->type
;
1907 peers_id
= id_b
+ 1;
1908 idlen
= iph1
->id_p
->l
- sizeof(*id_b
);
1910 return oakley_check_certid_in_certchain(iph1
->cert_p
, doi_type
, idlen
, peers_id
);
1915 oakley_check_certid_1(vchar_t
*cert
, int idtype
, int idlen
, void *id
, cert_status_t
*certStatus
)
1923 char *altname
= NULL
;
1927 case IPSECDOI_ID_DER_ASN1_DN
:
1930 SecCertificateRef certificate
;
1931 UInt8
* namePtr
= NULL
;
1933 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
1934 if (certificate
== NULL
) {
1936 "failed to get SecCertificateRef\n");
1937 if (certStatus
&& !*certStatus
) {
1938 *certStatus
= CERT_STATUS_INVALID
;
1940 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1942 subject
= crypto_cssm_CopySubjectSequence(certificate
);
1943 if (subject
== NULL
) {
1944 plog(ASL_LEVEL_ERR
, "failed to get certificate subjectName\n");
1945 if (certStatus
&& !*certStatus
) {
1946 *certStatus
= CERT_STATUS_INVALID_SUBJNAME
;
1948 error
= ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1950 len
= CFDataGetLength(subject
);
1951 namePtr
= (UInt8
*)CFDataGetBytePtr(subject
);
1953 if (idlen
!= len
|| memcmp(id
, namePtr
, idlen
)) {
1954 plog(ASL_LEVEL_ERR
, "ID mismatched with certificate subjectName\n");
1955 error
=ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1958 plog(ASL_LEVEL_ERR
, "no certificate subjectName found\n");
1959 error
= ISAKMP_NTYPE_INVALID_CERTIFICATE
;
1964 "ID mismatched with certificate subjectName\n");
1965 if (namePtr
!= NULL
) {
1966 plogdump(ASL_LEVEL_ERR
, namePtr
, len
, "subjectName (type %s):\n",
1967 s_ipsecdoi_ident(idtype
));
1969 plog(ASL_LEVEL_ERR
, "subjectName (type %s):\n", s_ipsecdoi_ident(idtype
));
1971 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
1972 if (certStatus
&& !*certStatus
) {
1973 *certStatus
= CERT_STATUS_INVALID_SUBJNAME
;
1976 CFRelease(certificate
);
1977 if (subject
!= NULL
) {
1984 case IPSECDOI_ID_IPV4_ADDR
:
1985 case IPSECDOI_ID_IPV6_ADDR
:
1987 #ifndef HAVE_OPENSSL
1989 SecCertificateRef certificate
;
1990 CFArrayRef addresses
;
1991 #define ADDRESS_BUF_SIZE 64
1993 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
1994 if (certificate
== NULL
) {
1996 "failed to get SecCertificateRef\n");
1997 if (certStatus
&& !*certStatus
) {
1998 *certStatus
= CERT_STATUS_INVALID
;
2000 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2002 addresses
= SecCertificateCopyIPAddresses(certificate
);
2003 if (addresses
== NULL
) {
2004 plog(ASL_LEVEL_ERR
, "failed to get subjectName\n");
2005 if (certStatus
&& !*certStatus
) {
2006 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2008 CFRelease(certificate
);
2009 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2011 count
= CFArrayGetCount(addresses
);
2012 for (pos
= 0; pos
< count
; pos
++) {
2014 CFStringRef address
;
2016 char *addressBuf
, numAddress
[128];
2019 address
= CFArrayGetValueAtIndex(addresses
, pos
);
2020 addressLen
= CFStringGetLength(address
);
2021 if (addressLen
== 0)
2023 addressBuf
= racoon_malloc(ADDRESS_BUF_SIZE
);
2024 if (addressBuf
== NULL
) {
2025 plog(ASL_LEVEL_ERR
, "out of memory\n");
2026 CFRelease(addresses
);
2027 CFRelease(certificate
);
2030 if (CFStringGetCString(address
, addressBuf
, ADDRESS_BUF_SIZE
, kCFStringEncodingUTF8
) == TRUE
) {
2031 result
= inet_pton(idtype
== IPSECDOI_ID_IPV4_ADDR
? AF_INET
: AF_INET6
, addressBuf
, numAddress
);
2032 racoon_free(addressBuf
);
2034 continue; // wrong type or invalid address
2035 if (!memcmp(id
, numAddress
, idtype
== IPSECDOI_ID_IPV4_ADDR
? 32 : 128) == 0) { // found a match ?
2036 CFRelease(addresses
);
2037 CFRelease(certificate
);
2041 racoon_free(addressBuf
);
2043 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2045 "subjectAltName (expected type %s):\n", s_ipsecdoi_ident(idtype
));
2046 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2047 CFRelease(addresses
);
2048 CFRelease(certificate
);
2049 if (certStatus
&& !*certStatus
) {
2050 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2052 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2055 * Openssl returns the IPAddress as an ASN1 octet string (binary format)
2056 * followed by a trailing NULL. 5 bytes for IPv4 and 17 bytes for IPv6
2058 #define SUBJ_ALT_NAME_IPV4_ADDRESS_LEN 5
2059 #define SUBJ_ALT_NAME_IPV6_ADDRESS_LEN 17
2063 if ((idtype
== IPSECDOI_ID_IPV4_ADDR
&& idlen
!= sizeof(struct in_addr
))
2064 || (idtype
== IPSECDOI_ID_IPV6_ADDR
&& idlen
!= sizeof(struct in6_addr
))) {
2066 "invalid address length passed.\n");
2067 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2070 for (pos
= 1; ; pos
++) {
2071 if (eay_get_x509subjectaltname(cert
, &altname
, &type
, pos
, &len
) !=0) {
2073 "failed to get subjectAltName\n");
2074 if (certStatus
&& !*certStatus
) {
2075 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2077 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2080 /* it's the end condition of the loop. */
2083 "invalid subjectAltName\n");
2084 if (certStatus
&& !*certStatus
) {
2085 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2087 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2090 if (check_typeofcertname(idtype
, type
) != 0) {
2091 /* wrong type - skip this one */
2092 racoon_free(altname
);
2097 if (len
== SUBJ_ALT_NAME_IPV4_ADDRESS_LEN
) { /* IPv4 */
2098 if (idtype
!= IPSECDOI_ID_IPV4_ADDR
) {
2099 /* wrong IP address type - skip this one */
2100 racoon_free(altname
);
2106 else if (len
== SUBJ_ALT_NAME_IPV6_ADDRESS_LEN
) { /* IPv6 */
2107 if (idtype
!= IPSECDOI_ID_IPV6_ADDR
) {
2108 /* wrong IP address type - skip this one */
2109 racoon_free(altname
);
2116 /* invalid IP address length in certificate - bad or bogus certificate */
2118 "invalid IP address in certificate.\n");
2119 plogdump(ASL_LEVEL_ERR
, altname
, len
, "subjectAltName (expected type %s, got type %s):\n",
2120 s_ipsecdoi_ident(idtype
),
2121 s_ipsecdoi_ident(type
));
2122 racoon_free(altname
);
2124 if (certStatus
&& !*certStatus
) {
2125 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2127 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2130 /* compare the addresses */
2131 error
= memcmp(id
, altname
, idlen
);
2134 racoon_free(altname
);
2137 /* failed to find a match */
2139 "ID mismatched with subjectAltName.\n");
2140 plogdump(ASL_LEVEL_ERR
, altname
, len
, "subjectAltName (expected type %s, got type %s):\n",
2141 s_ipsecdoi_ident(idtype
),
2142 s_ipsecdoi_ident(type
));
2143 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2144 racoon_free(altname
);
2145 if (certStatus
&& !*certStatus
)
2146 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2147 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2149 #endif /* HAVE_OPENSSL */
2152 #ifndef HAVE_OPENSSL
2153 case IPSECDOI_ID_FQDN
:
2156 SecCertificateRef certificate
;
2158 CFStringRef name
, ID
;
2160 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
2161 if (certificate
== NULL
) {
2163 "failed to get SecCertificateRef\n");
2164 if (certStatus
&& !*certStatus
) {
2165 *certStatus
= CERT_STATUS_INVALID
;
2167 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2169 names
= SecCertificateCopyDNSNames(certificate
);
2170 if (names
== NULL
) {
2172 "failed to get subjectName\n");
2173 if (certStatus
&& !*certStatus
) {
2174 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2176 CFRelease(certificate
);
2177 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2179 count
= CFArrayGetCount(names
);
2180 ID
= CFStringCreateWithBytes(kCFAllocatorDefault
, id
, idlen
, kCFStringEncodingUTF8
, FALSE
);
2182 plog(ASL_LEVEL_ERR
, "memory error\n");
2184 CFRelease(certificate
);
2187 for (pos
= 0; pos
< count
; pos
++) {
2188 name
= CFArrayGetValueAtIndex(names
, pos
);
2189 if (CFStringCompare(name
, ID
, 0) == kCFCompareEqualTo
) {
2192 CFRelease(certificate
);
2196 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2198 "subjectAltName (expected type %s):\n", s_ipsecdoi_ident(idtype
));
2199 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2202 CFRelease(certificate
);
2203 if (certStatus
&& !*certStatus
) {
2204 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2206 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2209 case IPSECDOI_ID_USER_FQDN
:
2213 SecCertificateRef certificate
;
2215 CFStringRef name
, ID
;
2217 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(cert
);
2218 if (certificate
== NULL
) {
2220 "failed to get SecCertificateRef\n");
2221 if (certStatus
&& !*certStatus
) {
2222 *certStatus
= CERT_STATUS_INVALID
;
2224 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2226 names
= SecCertificateCopyRFC822Names(certificate
);
2227 if (names
== NULL
) {
2229 "failed to get subjectName\n");
2230 if (certStatus
&& !*certStatus
) {
2231 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2233 CFRelease(certificate
);
2234 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2236 count
= CFArrayGetCount(names
);
2237 ID
= CFStringCreateWithBytes(kCFAllocatorDefault
, id
, idlen
, kCFStringEncodingUTF8
, FALSE
);
2241 if (certStatus
&& !*certStatus
) {
2242 *certStatus
= CERT_STATUS_INVALID
;
2245 CFRelease(certificate
);
2246 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2248 for (pos
= 0; pos
< count
; pos
++) {
2249 name
= CFArrayGetValueAtIndex(names
, pos
);
2250 if (CFStringCompare(name
, ID
, 0) == kCFCompareEqualTo
) {
2253 CFRelease(certificate
);
2257 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2259 "subjectAltName (expected type %s):\n", s_ipsecdoi_ident(idtype
));
2260 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2263 CFRelease(certificate
);
2264 if (certStatus
&& !*certStatus
) {
2265 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2267 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2270 case IPSECDOI_ID_FQDN
:
2271 case IPSECDOI_ID_USER_FQDN
:
2275 for (pos
= 1; ; pos
++) {
2276 if (eay_get_x509subjectaltname(cert
, &altname
, &type
, pos
, &len
) != 0) {
2278 "failed to get subjectAltName\n");
2279 if (certStatus
&& !*certStatus
) {
2280 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2282 return ISAKMP_NTYPE_INVALID_CERTIFICATE
;
2285 /* it's the end condition of the loop. */
2288 "invalid subjectAltName\n");
2289 if (certStatus
&& !*certStatus
) {
2290 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2292 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2295 if (check_typeofcertname(idtype
, type
) != 0) {
2296 /* wrong general type - skip this one */
2297 racoon_free(altname
);
2302 if (idlen
!= strlen(altname
)) {
2303 /* wrong length - skip this one */
2304 racoon_free(altname
);
2308 error
= memcmp(id
, altname
, idlen
);
2311 racoon_free(altname
);
2314 plog(ASL_LEVEL_ERR
, "ID mismatched with subjectAltName.\n");
2316 "subjectAltName (expected type %s, got type %s):\n",
2317 s_ipsecdoi_ident(idtype
),
2318 s_ipsecdoi_ident(type
));
2319 plogdump(ASL_LEVEL_ERR
, altname
, len
, "subjectAltName (expected type %s, got type %s):\n",
2320 s_ipsecdoi_ident(idtype
),
2321 s_ipsecdoi_ident(type
));
2322 plogdump(ASL_LEVEL_ERR
, id
, idlen
, "ID:\n");
2323 racoon_free(altname
);
2324 if (certStatus
&& !*certStatus
)
2325 *certStatus
= CERT_STATUS_INVALID_SUBJALTNAME
;
2326 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2331 "Impropper ID type passed: %s.\n",
2332 s_ipsecdoi_ident(idtype
));
2333 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2339 check_typeofcertname(int doi
, int genid
)
2342 case IPSECDOI_ID_IPV4_ADDR
:
2343 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
2344 case IPSECDOI_ID_IPV6_ADDR
:
2345 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
2346 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
2347 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
2348 if (genid
!= GENT_IPADD
)
2351 case IPSECDOI_ID_FQDN
:
2352 if (genid
!= GENT_DNS
)
2355 case IPSECDOI_ID_USER_FQDN
:
2356 if (genid
!= GENT_EMAIL
)
2359 case IPSECDOI_ID_DER_ASN1_DN
: /* should not be passed to this function*/
2360 case IPSECDOI_ID_DER_ASN1_GN
:
2361 case IPSECDOI_ID_KEY_ID
:
2370 * save certificate including certificate type.
2373 oakley_savecert(phase1_handle_t
*iph1
, struct isakmp_gen
*gen
)
2377 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
2380 case ISAKMP_CERT_X509SIGN
:
2385 "Invalid CERT type %d\n", type
);
2390 plog(ASL_LEVEL_WARNING
,
2391 "preexisting CERT payload... chaining.\n");
2395 new = save_certbuf(gen
);
2398 "Failed to get CERT buffer.\n");
2402 switch (new->type
) {
2403 case ISAKMP_CERT_X509SIGN
:
2404 /* Ignore cert if it doesn't match identity
2405 * XXX If verify cert is disabled, we still just take
2406 * the first certificate....
2408 *c
= oakley_appendcert_to_certchain(*c
, new);
2409 plog(ASL_LEVEL_DEBUG
, "CERT saved:\n");
2413 oakley_delcert(new);
2421 * save certificate including certificate type.
2424 oakley_savecr(phase1_handle_t
*iph1
, struct isakmp_gen
*gen
)
2430 type
= *(u_int8_t
*)(gen
+ 1) & 0xff;
2433 case ISAKMP_CERT_X509SIGN
:
2435 oakley_delcert(iph1
->cr_p
);
2442 "Invalid CR type %d\n", type
);
2446 new = save_certbuf(gen
);
2449 "Failed to get CR buffer.\n");
2452 *c
= oakley_appendcert_to_certchain(*c
, new);
2453 plog(ASL_LEVEL_DEBUG
, "CR saved\n");
2459 save_certbuf(struct isakmp_gen
*gen
)
2463 if(ntohs(gen
->len
) <= sizeof(*gen
)){
2465 "Len is too small !!.\n");
2469 new = oakley_newcert();
2472 "Failed to get CERT buffer.\n");
2476 new->pl
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2477 if (new->pl
== NULL
) {
2479 "Failed to copy CERT from packet.\n");
2480 oakley_delcert(new);
2484 memcpy(new->pl
->v
, gen
+ 1, new->pl
->l
);
2485 new->type
= new->pl
->v
[0] & 0xff;
2486 new->cert
.v
= new->pl
->v
+ 1;
2487 new->cert
.l
= new->pl
->l
- 1;
2494 * NOTE: No Certificate Authority field is included to CR payload at the
2495 * moment. Becuase any certificate authority are accepted without any check.
2496 * The section 3.10 in RFC2408 says that this field SHOULD not be included,
2497 * if there is no specific certificate authority requested.
2500 oakley_getcr(phase1_handle_t
*iph1
)
2507 "failed to get cr buffer\n");
2510 if(iph1
->rmconf
->certtype
== ISAKMP_CERT_NONE
) {
2511 buf
->v
[0] = iph1
->rmconf
->cacerttype
;
2512 plog(ASL_LEVEL_DEBUG
, "create my CR: NONE, using %s instead\n",
2513 s_isakmp_certtype(iph1
->rmconf
->cacerttype
));
2515 buf
->v
[0] = iph1
->rmconf
->certtype
;
2516 plog(ASL_LEVEL_DEBUG
, "create my CR: %s\n",
2517 s_isakmp_certtype(iph1
->rmconf
->certtype
));
2520 // plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "");
2529 oakley_checkcr(phase1_handle_t
*iph1
)
2531 if (iph1
->cr_p
== NULL
)
2534 plog(ASL_LEVEL_DEBUG
,
2535 "peer transmitted CR: %s\n",
2536 s_isakmp_certtype(iph1
->cr_p
->type
));
2538 if (iph1
->cr_p
->type
!= iph1
->rmconf
->certtype
) {
2540 "such a cert type isn't supported: %d\n",
2541 (char)iph1
->cr_p
->type
);
2549 * check to need CR payload.
2552 oakley_needcr(int type
)
2555 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2556 #ifdef ENABLE_HYBRID
2557 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2558 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2559 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2569 oakley_getpskall(phase1_handle_t
*iph1
)
2571 vchar_t
*secret
= NULL
;
2573 if (iph1
->rmconf
->shared_secret
) {
2575 switch (iph1
->rmconf
->secrettype
) {
2576 case SECRETTYPE_KEY
:
2577 /* in psk file - use KEY from remote configuration to locate it */
2578 secret
= getpsk(iph1
->rmconf
->shared_secret
->v
, iph1
->rmconf
->shared_secret
->l
-1);
2581 case SECRETTYPE_KEYCHAIN
:
2582 /* in the system keychain */
2583 secret
= getpskfromkeychain(iph1
->rmconf
->shared_secret
->v
, iph1
->etype
, iph1
->rmconf
->secrettype
, NULL
);
2585 case SECRETTYPE_KEYCHAIN_BY_ID
:
2586 /* in the system keychain - use peer id */
2587 secret
= getpskfromkeychain(iph1
->rmconf
->shared_secret
->v
, iph1
->etype
, iph1
->rmconf
->secrettype
, iph1
->id_p
);
2589 #endif // HAVE_KEYCHAIN
2590 case SECRETTYPE_USE
:
2591 /* in the remote configuration */
2593 /* rmconf->shared_secret is a string and contains a NULL character that must be removed */
2594 secret
= vmalloc(iph1
->rmconf
->shared_secret
->l
- 1);
2595 if (secret
== NULL
) {
2596 plog(ASL_LEVEL_ERR
, "memory error.\n");
2599 memcpy(secret
->v
, iph1
->rmconf
->shared_secret
->v
, secret
->l
);
2601 } else if (iph1
->etype
!= ISAKMP_ETYPE_IDENT
) {
2602 secret
= getpskbyname(iph1
->id_p
);
2604 if (iph1
->rmconf
->verify_identifier
) {
2605 plog(ASL_LEVEL_ERR
, "couldn't find pskey by peer's ID.\n");
2611 plog(ASL_LEVEL_NOTICE
, "try to get pskey by the peer's address.\n");
2612 secret
= getpskbyaddr(iph1
->remote
);
2615 "couldn't find the pskey by address %s.\n",
2616 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2626 * see seciton 5. Exchanges in RFC 2409
2627 * psk: SKEYID = prf(pre-shared-key, Ni_b | Nr_b)
2628 * sig: SKEYID = prf(Ni_b | Nr_b, g^ir)
2629 * enc: SKEYID = prf(H(Ni_b | Nr_b), CKY-I | CKY-R)
2632 oakley_skeyid(phase1_handle_t
*iph1
)
2634 vchar_t
*key
= NULL
;
2635 vchar_t
*buf
= NULL
;
2642 switch (AUTHMETHOD(iph1
)) {
2643 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2644 #ifdef ENABLE_HYBRID
2645 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
2646 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
2648 key
= oakley_getpskall(iph1
);
2651 "couldn't find the pskey for %s.\n",
2652 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2655 plog(ASL_LEVEL_DEBUG
, "the psk found.\n");
2656 /* should be secret PSK */
2657 plogdump(ASL_LEVEL_DEBUG
, key
->v
, key
->l
, "psk: ");
2659 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2663 "failed to get skeyid buffer\n");
2668 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2669 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce 1: ");
2670 memcpy(p
, bp
->v
, bp
->l
);
2673 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2674 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce 2: ");
2675 memcpy(p
, bp
->v
, bp
->l
);
2678 iph1
->skeyid
= oakley_prf(key
, buf
, iph1
);
2680 if (iph1
->skeyid
== NULL
)
2684 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2685 #ifdef ENABLE_HYBRID
2686 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2687 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
2688 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2689 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2691 len
= iph1
->nonce
->l
+ iph1
->nonce_p
->l
;
2695 "failed to get nonce buffer\n");
2700 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce
: iph1
->nonce_p
);
2701 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce1: ");
2702 memcpy(p
, bp
->v
, bp
->l
);
2705 bp
= (iph1
->side
== INITIATOR
? iph1
->nonce_p
: iph1
->nonce
);
2706 //plogdump(ASL_LEVEL_DEBUG, bp->v, bp->l, "nonce2: ");
2707 memcpy(p
, bp
->v
, bp
->l
);
2710 iph1
->skeyid
= oakley_prf(buf
, iph1
->dhgxy
, iph1
);
2711 if (iph1
->skeyid
== NULL
)
2714 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2715 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2716 #ifdef ENABLE_HYBRID
2717 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
2718 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
2719 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
2720 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
2722 plog(ASL_LEVEL_WARNING
,
2723 "not supported authentication method %s\n",
2724 s_oakley_attr_method(iph1
->approval
->authmethod
));
2728 "invalid authentication method %d\n",
2729 iph1
->approval
->authmethod
);
2733 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid->v, iph1->skeyid->l, "IKEv1 SKEYID computed:\n");
2746 * compute SKEYID_[dae]
2749 oakley_skeyid_dae(phase1_handle_t
*iph1
)
2751 vchar_t
*buf
= NULL
;
2756 if (iph1
->skeyid
== NULL
) {
2757 plog(ASL_LEVEL_ERR
, "no SKEYID found.\n");
2762 * see seciton 5. Exchanges in RFC 2409
2763 * SKEYID_d = prf(SKEYID, g^ir | CKY-I | CKY-R | 0)
2764 * SKEYID_a = prf(SKEYID, SKEYID_d | g^ir | CKY-I | CKY-R | 1)
2765 * SKEYID_e = prf(SKEYID, SKEYID_a | g^ir | CKY-I | CKY-R | 2)
2768 /* SKEYID_d = prf(SKEYID, g^xy | CKY-I | CKY-R | 0) */
2769 len
= iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2773 "failed to get skeyid buffer\n");
2778 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2779 p
+= iph1
->dhgxy
->l
;
2780 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2781 p
+= sizeof(cookie_t
);
2782 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2783 p
+= sizeof(cookie_t
);
2785 iph1
->skeyid_d
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2786 if (iph1
->skeyid_d
== NULL
)
2792 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid_d->v, iph1->skeyid_d->l, "SKEYID_d computed:\n");
2795 /* SKEYID_a = prf(SKEYID, SKEYID_d | g^xy | CKY-I | CKY-R | 1) */
2796 len
= iph1
->skeyid_d
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2800 "failed to get skeyid buffer\n");
2804 memcpy(p
, iph1
->skeyid_d
->v
, iph1
->skeyid_d
->l
);
2805 p
+= iph1
->skeyid_d
->l
;
2806 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2807 p
+= iph1
->dhgxy
->l
;
2808 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2809 p
+= sizeof(cookie_t
);
2810 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2811 p
+= sizeof(cookie_t
);
2813 iph1
->skeyid_a
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2814 if (iph1
->skeyid_a
== NULL
)
2820 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid_a->v, iph1->skeyid_a->l, "SKEYID_a computed:\n");
2823 /* SKEYID_e = prf(SKEYID, SKEYID_a | g^xy | CKY-I | CKY-R | 2) */
2824 len
= iph1
->skeyid_a
->l
+ iph1
->dhgxy
->l
+ sizeof(cookie_t
) * 2 + 1;
2828 "failed to get skeyid buffer\n");
2832 memcpy(p
, iph1
->skeyid_a
->v
, iph1
->skeyid_a
->l
);
2833 p
+= iph1
->skeyid_a
->l
;
2834 memcpy(p
, iph1
->dhgxy
->v
, iph1
->dhgxy
->l
);
2835 p
+= iph1
->dhgxy
->l
;
2836 memcpy(p
, (caddr_t
)&iph1
->index
.i_ck
, sizeof(cookie_t
));
2837 p
+= sizeof(cookie_t
);
2838 memcpy(p
, (caddr_t
)&iph1
->index
.r_ck
, sizeof(cookie_t
));
2839 p
+= sizeof(cookie_t
);
2841 iph1
->skeyid_e
= oakley_prf(iph1
->skeyid
, buf
, iph1
);
2842 if (iph1
->skeyid_e
== NULL
)
2848 //plogdump(ASL_LEVEL_DEBUG, iph1->skeyid_e->v, iph1->skeyid_e->l, "SKEYID_e computed:\n");
2859 * compute final encryption key.
2863 oakley_compute_enckey(phase1_handle_t
*iph1
)
2865 u_int keylen
, prflen
;
2869 keylen
= alg_oakley_encdef_keylen(iph1
->approval
->enctype
,
2870 iph1
->approval
->encklen
);
2873 "invalid encryption algoritym %d, "
2874 "or invalid key length %d.\n",
2875 iph1
->approval
->enctype
,
2876 iph1
->approval
->encklen
);
2879 iph1
->key
= vmalloc(keylen
>> 3);
2880 if (iph1
->key
== NULL
) {
2882 "failed to get key buffer\n");
2886 /* set prf length */
2887 prflen
= alg_oakley_hashdef_hashlen(iph1
->approval
->hashtype
);
2890 "invalid hash type %d.\n", iph1
->approval
->hashtype
);
2894 /* see isakmp-oakley-08 5.3. */
2895 if (iph1
->key
->l
<= iph1
->skeyid_e
->l
) {
2897 * if length(Ka) <= length(SKEYID_e)
2898 * Ka = first length(K) bit of SKEYID_e
2900 memcpy(iph1
->key
->v
, iph1
->skeyid_e
->v
, iph1
->key
->l
);
2902 vchar_t
*buf
= NULL
, *res
= NULL
;
2911 * K1 = prf(SKEYID_e, 0)
2912 * K2 = prf(SKEYID_e, K1)
2913 * K3 = prf(SKEYID_e, K2)
2915 plog(ASL_LEVEL_DEBUG
,
2916 "len(SKEYID_e) < len(Ka) (%zu < %zu), "
2917 "generating long key (Ka = K1 | K2 | ...)\n",
2918 iph1
->skeyid_e
->l
, iph1
->key
->l
);
2920 if ((buf
= vmalloc(prflen
>> 3)) == 0) {
2922 "failed to get key buffer\n");
2925 p
= (u_char
*)iph1
->key
->v
;
2926 ep
= p
+ iph1
->key
->l
;
2930 if (p
== (u_char
*)iph1
->key
->v
) {
2931 /* just for computing K1 */
2935 res
= oakley_prf(iph1
->skeyid_e
, buf
, iph1
);
2940 plog(ASL_LEVEL_DEBUG
,
2941 "compute intermediate encryption key K%d\n",
2943 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "");
2944 //plogdump(ASL_LEVEL_DEBUG, res->v, res->l, "");
2946 cplen
= (res
->l
< ep
- p
) ? res
->l
: ep
- p
;
2947 memcpy(p
, res
->v
, cplen
);
2950 buf
->l
= prflen
>> 3; /* to cancel K1 speciality */
2951 if (res
->l
!= buf
->l
) {
2953 "internal error: res->l=%zu buf->l=%zu\n",
2959 memcpy(buf
->v
, res
->v
, res
->l
);
2968 * don't check any weak key or not.
2969 * draft-ietf-ipsec-ike-01.txt Appendix B.
2970 * draft-ietf-ipsec-ciph-aes-cbc-00.txt Section 2.3.
2973 //plogdump(ASL_LEVEL_DEBUG, iph1->key->v, iph1->key->l, "final encryption key computed:\n");
2981 /* allocated new buffer for CERT */
2983 oakley_newcert(void)
2987 new = racoon_calloc(1, sizeof(*new));
2990 "failed to get cert's buffer\n");
3000 /* delete buffer for CERT */
3002 oakley_delcert_1(cert_t
*cert
)
3011 /* delete buffer for CERT */
3013 oakley_delcert(cert_t
*cert
)
3015 cert_t
*p
, *to_delete
;
3020 for (p
= cert
; p
;) {
3023 oakley_delcert_1(to_delete
);
3027 /* delete buffer for CERT */
3029 oakley_appendcert_to_certchain(cert_t
*certchain
, cert_t
*new)
3036 for (p
= certchain
; p
; p
= p
->chain
) {
3046 * compute IV and set to ph1handle
3047 * IV = hash(g^xi | g^xr)
3048 * see 4.1 Phase 1 state in draft-ietf-ipsec-ike.
3051 oakley_newiv(phase1_handle_t
*iph1
)
3053 struct isakmp_ivm
*newivm
= NULL
;
3054 vchar_t
*buf
= NULL
, *bp
;
3059 len
= iph1
->dhpub
->l
+ iph1
->dhpub_p
->l
;
3063 "Failed to get IV buffer\n");
3069 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub
: iph1
->dhpub_p
);
3070 memcpy(p
, bp
->v
, bp
->l
);
3073 bp
= (iph1
->side
== INITIATOR
? iph1
->dhpub_p
: iph1
->dhpub
);
3074 memcpy(p
, bp
->v
, bp
->l
);
3078 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
3079 if (newivm
== NULL
) {
3081 "Failed to get IV buffer\n");
3087 newivm
->iv
= oakley_hash(buf
, iph1
);
3088 if (newivm
->iv
== NULL
) {
3090 oakley_delivm(newivm
);
3094 /* adjust length of iv */
3095 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3096 if (newivm
->iv
->l
== -1) {
3098 "Invalid encryption algorithm %d.\n",
3099 iph1
->approval
->enctype
);
3101 oakley_delivm(newivm
);
3105 /* create buffer to save iv */
3106 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
3108 "vdup (%s)\n", strerror(errno
));
3110 oakley_delivm(newivm
);
3116 //plogdump(ASL_LEVEL_DEBUG, newivm->iv->v, newivm->iv->l, "IV computed:\n");
3118 if (iph1
->ivm
!= NULL
)
3119 oakley_delivm(iph1
->ivm
);
3127 * compute IV for the payload after phase 1.
3128 * It's not limited for phase 2.
3129 * if pahse 1 was encrypted.
3130 * IV = hash(last CBC block of Phase 1 | M-ID)
3131 * if phase 1 was not encrypted.
3132 * IV = hash(phase 1 IV | M-ID)
3133 * see 4.2 Phase 2 state in draft-ietf-ipsec-ike.
3136 oakley_newiv2(phase1_handle_t
*iph1
, u_int32_t msgid
)
3138 struct isakmp_ivm
*newivm
= NULL
;
3139 vchar_t
*buf
= NULL
;
3145 len
= iph1
->ivm
->iv
->l
+ sizeof(msgid_t
);
3149 "Failed to get IV buffer\n");
3155 memcpy(p
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
3156 p
+= iph1
->ivm
->iv
->l
;
3158 memcpy(p
, &msgid
, sizeof(msgid
));
3160 plog(ASL_LEVEL_DEBUG
, "Compute IV for Phase 2\n");
3161 //plogdump(ASL_LEVEL_DEBUG, buf->v, buf->l, "Phase 1 last IV:\n");
3164 newivm
= racoon_calloc(1, sizeof(struct isakmp_ivm
));
3165 if (newivm
== NULL
) {
3167 "Failed to get IV buffer\n");
3172 if ((newivm
->iv
= oakley_hash(buf
, iph1
)) == NULL
)
3175 /* adjust length of iv */
3176 newivm
->iv
->l
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3177 if (newivm
->iv
->l
== -1) {
3179 "Invalid encryption algorithm %d.\n",
3180 iph1
->approval
->enctype
);
3184 /* create buffer to save new iv */
3185 if ((newivm
->ive
= vdup(newivm
->iv
)) == NULL
) {
3186 plog(ASL_LEVEL_ERR
, "vdup (%s)\n", strerror(errno
));
3192 //plogdump(ASL_LEVEL_DEBUG, newivm->iv->v, newivm->iv->l, "Phase 2 IV computed:\n");
3195 if (error
&& newivm
!= NULL
){
3196 oakley_delivm(newivm
);
3205 oakley_delivm(struct isakmp_ivm
*ivm
)
3210 if (ivm
->iv
!= NULL
)
3212 if (ivm
->ive
!= NULL
)
3215 plog(ASL_LEVEL_DEBUG
, "IV freed\n");
3222 * save new iv and old iv.
3225 oakley_do_ikev1_decrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivdp
, vchar_t
*ivep
)
3227 vchar_t
*buf
= NULL
, *new = NULL
;
3234 plog(ASL_LEVEL_DEBUG
, "Begin decryption.\n");
3236 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3239 "Invalid encryption algorithm %d.\n",
3240 iph1
->approval
->enctype
);
3244 /* save IV for next, but not sync. */
3245 memset(ivep
->v
, 0, ivep
->l
);
3246 memcpy(ivep
->v
, (caddr_t
)&msg
->v
[msg
->l
- blen
], blen
);
3248 plogdump(ASL_LEVEL_DEBUG
, ivep
->v
, ivep
->l
, "IV was saved for next processing:\n");
3250 pl
= msg
->v
+ sizeof(struct isakmp
);
3252 len
= msg
->l
- sizeof(struct isakmp
);
3258 "Failed to get buffer to decrypt.\n");
3261 memcpy(buf
->v
, pl
, len
);
3264 new = alg_oakley_encdef_decrypt(iph1
->approval
->enctype
,
3265 buf
, iph1
->key
, ivdp
);
3266 if (new == NULL
|| new->v
== NULL
|| new->l
== 0) {
3268 "Decryption %d failed.\n", iph1
->approval
->enctype
);
3271 //plogdump(ASL_LEVEL_DEBUG, iph1->key->v, iph1->key->l, "with key:\n");
3278 plog(ASL_LEVEL_DEBUG
, "decrypted payload by IV:\n");
3280 /* get padding length */
3281 if (lcconf
->pad_excltail
)
3282 padlen
= new->v
[new->l
- 1] + 1;
3284 padlen
= new->v
[new->l
- 1];
3285 plog(ASL_LEVEL_DEBUG
, "padding len=%u\n", padlen
);
3288 if (lcconf
->pad_strict
) {
3289 if (padlen
> new->l
) {
3290 plog(ASL_LEVEL_ERR
, "invalid padding len=%u, buflen=%zu.\n",
3295 plog(ASL_LEVEL_DEBUG
, "trimmed padding\n");
3297 plog(ASL_LEVEL_DEBUG
, "skip to trim padding.\n");
3300 /* create new buffer */
3301 len
= sizeof(struct isakmp
) + new->l
;
3305 "failed to get buffer to decrypt.\n");
3308 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
3309 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
3310 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
3312 plog(ASL_LEVEL_DEBUG
, "decrypted.\n");
3314 #ifdef HAVE_PRINT_ISAKMP_C
3315 isakmp_printpacket(buf
, iph1
->remote
, iph1
->local
, 1);
3321 if (error
&& buf
!= NULL
) {
3335 oakley_do_decrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivdp
, vchar_t
*ivep
)
3337 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
3338 return(oakley_do_ikev1_decrypt(iph1
, msg
, ivdp
, ivep
));
3341 plog(ASL_LEVEL_ERR
, "Failed to decrypt invalid IKE version");
3349 oakley_do_ikev1_encrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivep
, vchar_t
*ivp
)
3351 vchar_t
*buf
= 0, *new = 0;
3358 plog(ASL_LEVEL_DEBUG
, "Begin encryption.\n");
3360 /* set cbc block length */
3361 blen
= alg_oakley_encdef_blocklen(iph1
->approval
->enctype
);
3364 "Invalid encryption algorithm %d.\n",
3365 iph1
->approval
->enctype
);
3369 pl
= msg
->v
+ sizeof(struct isakmp
);
3370 len
= msg
->l
- sizeof(struct isakmp
);
3373 padlen
= oakley_padlen(len
, blen
);
3374 plog(ASL_LEVEL_DEBUG
, "pad length = %u\n", padlen
);
3377 buf
= vmalloc(len
+ padlen
);
3380 "Failed to get buffer to encrypt.\n");
3385 char *p
= &buf
->v
[len
];
3386 if (lcconf
->pad_random
) {
3387 for (i
= 0; i
< padlen
; i
++)
3388 *p
++ = eay_random() & 0xff;
3391 memcpy(buf
->v
, pl
, len
);
3393 /* make pad into tail */
3394 if (lcconf
->pad_excltail
)
3395 buf
->v
[len
+ padlen
- 1] = padlen
- 1;
3397 buf
->v
[len
+ padlen
- 1] = padlen
;
3399 plogdump(ASL_LEVEL_DEBUG
, buf
->v
, buf
->l
, "About to encrypt %d bytes", buf
->l
);
3402 new = alg_oakley_encdef_encrypt(iph1
->approval
->enctype
,
3403 buf
, iph1
->key
, ivep
);
3406 "Encryption %d failed.\n", iph1
->approval
->enctype
);
3409 //plogdump(ASL_LEVEL_DEBUG, iph1->key->v, iph1->key->l, "with key:\n");
3416 //plogdump(ASL_LEVEL_DEBUG, ivep->v, ivep->l, "encrypted payload by IV:\n");
3418 /* save IV for next */
3419 memset(ivp
->v
, 0, ivp
->l
);
3420 memcpy(ivp
->v
, (caddr_t
)&new->v
[new->l
- blen
], blen
);
3422 //plogdump(ASL_LEVEL_DEBUG, ivp->v, ivp->l, "save IV for next:\n");
3424 /* create new buffer */
3425 len
= sizeof(struct isakmp
) + new->l
;
3429 "Failed to get buffer to encrypt.\n");
3432 memcpy(buf
->v
, msg
->v
, sizeof(struct isakmp
));
3433 memcpy(buf
->v
+ sizeof(struct isakmp
), new->v
, new->l
);
3434 ((struct isakmp
*)buf
->v
)->len
= htonl(buf
->l
);
3438 plog(ASL_LEVEL_DEBUG
, "Encrypted.\n");
3441 if (error
&& buf
!= NULL
) {
3455 oakley_do_encrypt(phase1_handle_t
*iph1
, vchar_t
*msg
, vchar_t
*ivep
, vchar_t
*ivp
)
3457 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
3458 return(oakley_do_ikev1_encrypt(iph1
, msg
, ivep
, ivp
));
3461 plog(ASL_LEVEL_ERR
, "Failed to encrypt invalid IKE version");
3465 /* culculate padding length */
3467 oakley_padlen(int len
, int base
)
3471 padlen
= base
- (len
% base
);
3473 if (lcconf
->pad_randomlen
)
3474 padlen
+= ((eay_random() % (lcconf
->pad_maxsize
+ 1) + 1) *
3480 /* -----------------------------------------------------------------------------
3481 The base-64 encoding packs three 8-bit bytes into four 7-bit ASCII
3482 characters. If the number of bytes in the original data isn't divisable
3483 by three, "=" characters are used to pad the encoded data. The complete
3484 set of characters used in base-64 are:
3492 ----------------------------------------------------------------------------- */
3493 static const signed char base64_DecodeTable
[128] = {
3494 /* 000 */ -1, -1, -1, -1, -1, -1, -1, -1,
3495 /* 010 */ -1, -1, -1, -1, -1, -1, -1, -1,
3496 /* 020 */ -1, -1, -1, -1, -1, -1, -1, -1,
3497 /* 030 */ -1, -1, -1, -1, -1, -1, -1, -1,
3498 /* ' ' */ -1, -1, -1, -1, -1, -1, -1, -1,
3499 /* '(' */ -1, -1, -1, 62, -1, -1, -1, 63,
3500 /* '0' */ 52, 53, 54, 55, 56, 57, 58, 59,
3501 /* '8' */ 60, 61, -1, -1, -1, 0, -1, -1,
3502 /* '@' */ -1, 0, 1, 2, 3, 4, 5, 6,
3503 /* 'H' */ 7, 8, 9, 10, 11, 12, 13, 14,
3504 /* 'P' */ 15, 16, 17, 18, 19, 20, 21, 22,
3505 /* 'X' */ 23, 24, 25, -1, -1, -1, -1, -1,
3506 /* '`' */ -1, 26, 27, 28, 29, 30, 31, 32,
3507 /* 'h' */ 33, 34, 35, 36, 37, 38, 39, 40,
3508 /* 'p' */ 41, 42, 43, 44, 45, 46, 47, 48,
3509 /* 'x' */ 49, 50, 51, -1, -1, -1, -1, -1
3512 static int base64toCFData(vchar_t
*textin
, CFDataRef
*dataRef
)
3520 uint8_t *textcur
= (__typeof__(textcur
))textin
->v
;
3521 int len
= textin
->l
;
3524 tmpbuf
= malloc(len
); // len of result will be less than encoded len
3525 if (tmpbuf
== NULL
) {
3526 yyerror("memory error - could not allocate buffer for certificate reference conversion from base-64.");
3530 for (i
= 0; i
< len
; i
++) {
3534 else if (!isspace(c
))
3536 if (base64_DecodeTable
[c
] < 0)
3540 acc
+= base64_DecodeTable
[c
];
3541 if (0 == (cntr
& 0x3)) {
3542 tmpbuf
[tmpbufpos
++] = (acc
>> 16) & 0xff;
3544 tmpbuf
[tmpbufpos
++] = (acc
>> 8) & 0xff;
3546 tmpbuf
[tmpbufpos
++] = acc
& 0xff;
3549 *dataRef
= CFDataCreate(NULL
, tmpbuf
, tmpbufpos
);