1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
|
/* $OpenBSD: ike_auth.c,v 1.27 2000/08/03 07:23:32 niklas Exp $ */
/* $EOM: ike_auth.c,v 1.53 2000/07/25 17:15:40 provos Exp $ */
/*
* Copyright (c) 1998, 1999, 2000 Niklas Hallqvist. All rights reserved.
* Copyright (c) 1999 Niels Provos. All rights reserved.
* Copyright (c) 1999 Angelos D. Keromytis. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Ericsson Radio Systems.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <keynote.h>
#include <policy.h>
#include "sysdep.h"
#include "cert.h"
#include "conf.h"
#include "constants.h"
#include "exchange.h"
#include "gmp.h"
#include "gmp_util.h"
#include "hash.h"
#include "ike_auth.h"
#include "ipsec.h"
#include "ipsec_doi.h"
#include "libcrypto.h"
#include "log.h"
#include "message.h"
#include "prf.h"
#include "transport.h"
#include "util.h"
#ifdef notyet
static u_int8_t *enc_gen_skeyid (struct exchange *, size_t *);
#endif
static u_int8_t *pre_shared_gen_skeyid (struct exchange *, size_t *);
static int pre_shared_decode_hash (struct message *);
static int pre_shared_encode_hash (struct message *);
#if defined(USE_X509) || defined(USE_KEYNOTE)
static u_int8_t *sig_gen_skeyid (struct exchange *, size_t *);
static int rsa_sig_decode_hash (struct message *);
static int rsa_sig_encode_hash (struct message *);
#endif
static int ike_auth_hash (struct exchange *, u_int8_t *);
static struct ike_auth ike_auth[] = {
{
IKE_AUTH_PRE_SHARED, pre_shared_gen_skeyid, pre_shared_decode_hash,
pre_shared_encode_hash
},
#ifdef notdef
{
IKE_AUTH_DSS, sig_gen_skeyid, pre_shared_decode_hash,
pre_shared_encode_hash
},
#endif
#if defined(USE_X509) || defined(USE_KEYNOTE)
{
IKE_AUTH_RSA_SIG, sig_gen_skeyid, rsa_sig_decode_hash,
rsa_sig_encode_hash
},
#endif
#ifdef notdef
{
IKE_AUTH_RSA_ENC, enc_gen_skeyid, pre_shared_decode_hash,
pre_shared_encode_hash
},
{
IKE_AUTH_RSA_ENC_REV, enc_gen_skeyid, pre_shared_decode_hash,
pre_shared_encode_hash
},
#endif
};
struct ike_auth *
ike_auth_get (u_int16_t id)
{
int i;
for (i = 0; i < sizeof ike_auth / sizeof ike_auth[0]; i++)
if (id == ike_auth[i].id)
return &ike_auth[i];
return 0;
}
/*
* Find and decode the configured key (pre-shared or public) for the
* peer denoted by ID. Stash the len in KEYLEN.
*/
static void *
ike_auth_get_key (int type, char *id, char *local_id, size_t *keylen)
{
char *key, *buf;
#if defined(USE_X509) || defined(USE_KEYNOTE)
char *keyfile;
#if defined(USE_X509)
BIO *keyh;
RSA *rsakey;
#endif
#endif
switch (type)
{
case IKE_AUTH_PRE_SHARED:
/* Get the pre-shared key for our peer. */
key = conf_get_str (id, "Authentication");
if (!key && local_id)
key = conf_get_str (local_id, "Authentication");
if (!key)
{
log_print ("ike_auth_get_key: "
"no key found for peer \"%s\"or local ID \"%s\"",
id, local_id);
return 0;
}
/* If the key starts with 0x it is in hex format. */
if (strncasecmp (key, "0x", 2) == 0)
{
*keylen = (strlen (key) - 1) / 2;
buf = malloc (*keylen);
if (!buf)
{
log_print ("ike_auth_get_key: malloc (%d) failed", *keylen);
return 0;
}
if (hex2raw (key + 2, buf, *keylen))
{
free (buf);
log_print ("ike_auth_get_key: invalid hex key %s", key);
return 0;
}
key = buf;
}
else
*keylen = strlen (key);
break;
case IKE_AUTH_RSA_SIG:
#if defined(USE_X509) || defined(USE_KEYNOTE)
#ifdef HAVE_DLOPEN
if (!libcrypto)
return 0;
#endif
#if defined(USE_KEYNOTE)
if ((local_id) &&
((keyfile = conf_get_str ("KeyNote",
"Credential-directory")) != NULL))
{
struct stat sb;
struct keynote_deckey dc;
char *privkeyfile, *buf2;
int fd;
privkeyfile = calloc (strlen (keyfile) + strlen (local_id) +
strlen (PRIVATE_KEY_FILE) + 3, sizeof(char));
if (privkeyfile == NULL)
{
log_print ("ike_auth_get_key: failed to allocate %d bytes",
strlen (keyfile) + strlen (local_id) +
strlen (PRIVATE_KEY_FILE) + 3);
return 0;
}
sprintf (privkeyfile, "%s/%s/%s", keyfile, local_id,
PRIVATE_KEY_FILE);
keyfile = privkeyfile;
if (stat (keyfile, &sb) < 0)
goto ignorekeynote;
fd = open (keyfile, O_RDONLY, 0);
if (fd < 0)
{
log_print ("ike_auth_get_key: failed opening \"%s\"", keyfile);
free (keyfile);
return 0;
}
buf = calloc (sb.st_size + 1, sizeof(char));
if (buf == NULL)
{
log_print ("ike_auth_get_key: failed allocating %d bytes",
sb.st_size + 1);
free (keyfile);
return 0;
}
if (read (fd, buf, sb.st_size) != sb.st_size)
{
free (buf);
log_print ("ike_auth_get_key: "
"failed reading %d bytes from \"%s\"",
sb.st_size, keyfile);
free (keyfile);
return 0;
}
close (fd);
/* Parse private key string */
buf2 = kn_get_string(buf);
free(buf);
if (LK (kn_decode_key, (&dc, buf2, KEYNOTE_PRIVATE_KEY)) == -1)
{
free (buf2);
log_print ("ike_auth_get_key: failed decoding key in \"%s\"",
keyfile);
free (keyfile);
return 0;
}
free (buf2);
if (dc.dec_algorithm != KEYNOTE_ALGORITHM_RSA)
{
log_print ("ike_auth_get_key: wrong algorithm type %d in \"%s\"",
dc.dec_algorithm, keyfile);
free (keyfile);
LK (kn_free_key, (&dc));
return 0;
}
free (keyfile);
return dc.dec_key;
}
ignorekeynote:
#endif /* USE_KEYNOTE */
#ifdef USE_X509
/* Otherwise, try X.509 */
keyfile = conf_get_str ("X509-certificates", "Private-key");
if ((keyh = LC (BIO_new, (LC (BIO_s_file, ())))) == NULL)
{
log_print ("ike_auth_get_key: "
"BIO_new (BIO_s_file ()) failed");
return 0;
}
if (LC (BIO_read_filename, (keyh, keyfile)) == -1)
{
log_print ("ike_auth_get_key: "
"BIO_read_filename (keyh, \"%s\") failed",
keyfile);
LC (BIO_free, (keyh));
return 0;
}
#if SSLEAY_VERSION_NUMBER >= 0x00904100L
rsakey = LC (PEM_read_bio_RSAPrivateKey, (keyh, NULL, NULL, NULL));
#else
rsakey = LC (PEM_read_bio_RSAPrivateKey, (keyh, NULL, NULL));
#endif
if (!rsakey)
{
log_print ("ike_auth_get_key: PEM_read_bio_RSAPrivateKey failed");
LC (BIO_free, (keyh));
return 0;
}
LC (BIO_free, (keyh));
return rsakey;
#endif
#endif
default:
log_print ("ike_auth_get_key: unknown key type %d", type);
return 0;
}
return key;
}
static u_int8_t *
pre_shared_gen_skeyid (struct exchange *exchange, size_t *sz)
{
struct prf *prf;
struct ipsec_exch *ie = exchange->data;
u_int8_t *skeyid;
u_int8_t *key;
u_int8_t *buf = 0;
size_t keylen;
in_addr_t addr;
/*
* If we're the responder and have the initiator's ID (which is the
* case in Aggressive mode), try to find the preshared key in the
* section of the initiator's Phase 1 ID. This allows us to do
* mobile user support with preshared keys.
*/
if (!exchange->initiator && exchange->id_i)
{
switch (exchange->id_i[0])
{
case IPSEC_ID_IPV4_ADDR:
buf = malloc (16);
if (!buf)
{
log_error ("pre_shared_gen_skeyid: malloc (16) failed");
return 0;
}
addr = htonl (decode_32 (exchange->id_i + ISAKMP_ID_DATA_OFF -
ISAKMP_GEN_SZ));
inet_ntop (AF_INET, &addr, buf, 16);
break;
case IPSEC_ID_FQDN:
case IPSEC_ID_USER_FQDN:
buf = calloc (exchange->id_i_len - ISAKMP_ID_DATA_OFF +
ISAKMP_GEN_SZ + 1, sizeof (char));
if (!buf)
{
log_print ("pre_shared_gen_skeyid: malloc (%d) failed",
exchange->id_i_len - ISAKMP_ID_DATA_OFF
+ ISAKMP_GEN_SZ + 1);
return 0;
}
memcpy (buf, exchange->id_i + ISAKMP_ID_DATA_OFF - ISAKMP_GEN_SZ,
exchange->id_i_len - ISAKMP_ID_DATA_OFF + ISAKMP_GEN_SZ);
break;
/* XXX Support more ID types ? */
default:
break;
}
}
/* Get the pre-shared key for our peer. */
key = ike_auth_get_key (IKE_AUTH_PRE_SHARED, exchange->name, buf, &keylen);
if (buf)
free (buf);
/* Fail if no key could be found */
if (key == NULL)
return 0;
/* Store the secret key for later policy processing. */
exchange->recv_cert = malloc (keylen);
if (!exchange->recv_cert)
{
log_error ("pre_shared_gen_skeyid: malloc (%d) failed", keylen);
return 0;
}
memcpy (exchange->recv_cert, key, keylen);
exchange->recv_certlen = keylen;
exchange->recv_certtype = ISAKMP_CERTENC_NONE;
prf = prf_alloc (ie->prf_type, ie->hash->type, key, keylen);
if (!prf)
return 0;
*sz = prf->blocksize;
skeyid = malloc (*sz);
if (!skeyid)
{
log_error ("pre_shared_gen_skeyid: malloc (%d) failed", *sz);
prf_free (prf);
return 0;
}
prf->Init (prf->prfctx);
prf->Update (prf->prfctx, exchange->nonce_i, exchange->nonce_i_len);
prf->Update (prf->prfctx, exchange->nonce_r, exchange->nonce_r_len);
prf->Final (skeyid, prf->prfctx);
prf_free (prf);
return skeyid;
}
#if defined(USE_X509) || defined(USE_KEYNOTE)
/* Both DSS & RSA signature authentication use this algorithm. */
static u_int8_t *
sig_gen_skeyid (struct exchange *exchange, size_t *sz)
{
struct prf *prf;
struct ipsec_exch *ie = exchange->data;
u_int8_t *skeyid, *key;
key = malloc (exchange->nonce_i_len + exchange->nonce_r_len);
if (!key)
return 0;
memcpy (key, exchange->nonce_i, exchange->nonce_i_len);
memcpy (key + exchange->nonce_i_len, exchange->nonce_r,
exchange->nonce_r_len);
prf = prf_alloc (ie->prf_type, ie->hash->type, key,
exchange->nonce_i_len + exchange->nonce_r_len);
free (key);
if (!prf)
return 0;
*sz = prf->blocksize;
skeyid = malloc (*sz);
if (!skeyid)
{
log_error ("sig_gen_skeyid: malloc (%d) failed", *sz);
prf_free (prf);
return 0;
}
prf->Init (prf->prfctx);
prf->Update (prf->prfctx, ie->g_xy, ie->g_x_len);
prf->Final (skeyid, prf->prfctx);
prf_free (prf);
return skeyid;
}
#endif /* USE_X509 || USE_KEYNOTE */
#ifdef notdef
/*
* Both standard and revised RSA encryption authentication use this SKEYID
* computation.
*/
static u_int8_t *
enc_gen_skeyid (struct exchange *exchange, size_t *sz)
{
struct prf *prf;
struct ipsec_exch *ie = exchange->data;
struct hash *hash = ie->hash;
u_int8_t *skeyid;
hash->Init (hash->ctx);
hash->Update (hash->ctx, exchange->nonce_i, exchange->nonce_i_len);
hash->Update (hash->ctx, exchange->nonce_r, exchange->nonce_r_len);
hash->Final (hash->digest, hash->ctx);
prf = prf_alloc (ie->prf_type, hash->type, hash->digest, *sz);
if (!prf)
return 0;
*sz = prf->blocksize;
skeyid = malloc (*sz);
if (!skeyid)
{
log_error ("enc_gen_skeyid: malloc (%d) failed", *sz);
prf_free (prf);
return 0;
}
prf->Init (prf->prfctx);
prf->Update (prf->prfctx, exchange->cookies, ISAKMP_HDR_COOKIES_LEN);
prf->Final (skeyid, prf->prfctx);
prf_free (prf);
return skeyid;
}
#endif /* notdef */
static int
pre_shared_decode_hash (struct message *msg)
{
struct exchange *exchange = msg->exchange;
struct ipsec_exch *ie = exchange->data;
struct payload *payload;
size_t hashsize = ie->hash->hashsize;
char header[80];
int initiator = exchange->initiator;
u_int8_t **hash_p;
/* Choose the right fields to fill-in. */
hash_p = initiator ? &ie->hash_r : &ie->hash_i;
payload = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_HASH]);
if (!payload)
{
log_print ("pre_shared_decode_hash: no HASH payload found");
return -1;
}
/* Check that the hash is of the correct size. */
if (GET_ISAKMP_GEN_LENGTH (payload->p) - ISAKMP_GEN_SZ != hashsize)
return -1;
/* XXX Need this hash be in the SA? */
*hash_p = malloc (hashsize);
if (!*hash_p)
{
log_error ("pre_shared_decode_hash: malloc (%d) failed", hashsize);
return -1;
}
memcpy (*hash_p, payload->p + ISAKMP_HASH_DATA_OFF, hashsize);
snprintf (header, 80, "pre_shared_decode_hash: HASH_%c",
initiator ? 'R' : 'I');
LOG_DBG_BUF ((LOG_MISC, 80, header, *hash_p, hashsize));
payload->flags |= PL_MARK;
return 0;
}
#if defined(USE_X509) || defined(USE_KEYNOTE)
/* Decrypt the HASH in SIG, we already need a parsed ID payload. */
static int
rsa_sig_decode_hash (struct message *msg)
{
struct cert_handler *handler;
struct exchange *exchange = msg->exchange;
struct ipsec_exch *ie = exchange->data;
struct payload *p;
void *cert;
u_int8_t *rawcert;
u_int32_t rawlen;
RSA *key;
size_t hashsize = ie->hash->hashsize;
char header[80];
int len;
int initiator = exchange->initiator;
u_int8_t **hash_p, *id_cert, *id;
u_int32_t id_cert_len;
size_t id_len;
int found = 0;
/* Choose the right fields to fill-in. */
hash_p = initiator ? &ie->hash_r : &ie->hash_i;
id = initiator ? exchange->id_r : exchange->id_i;
id_len = initiator ? exchange->id_r_len : exchange->id_i_len;
if (!id || id_len == 0)
{
log_print ("rsa_sig_decode_hash: ID is missing");
return -1;
}
/* XXX Assume we should use the same kind of certification as the
XXX remote...moreover, just use the first CERT payload to
XXX decide what to use. */
p = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_CERT]);
if (!p)
handler = cert_get (ISAKMP_CERTENC_KEYNOTE);
else
handler = cert_get (GET_ISAKMP_CERT_ENCODING (p->p));
if (!handler)
{
log_print ("rsa_sig_decode_hash: "
"cert_get (%d) failed", GET_ISAKMP_CERT_ENCODING (p->p));
return -1;
}
#if defined(USE_POLICY) || defined(USE_KEYNOTE)
/*
* We need the policy session initialized now, so we can add
* credentials etc.
*/
exchange->policy_id = LK (kn_init, ());
if (exchange->policy_id == -1)
{
log_print ("rsa_sig_decode_hash: failed to initialize policy session");
return -1;
}
#endif /* USE_POLICY || USE_KEYNOTE */
/* Obtain a certificate from our certificate storage */
if (handler->cert_obtain (id, id_len, 0, &rawcert, &rawlen))
{
if (handler->id == ISAKMP_CERTENC_X509_SIG)
{
cert = handler->cert_get (rawcert, rawlen);
if (!cert)
LOG_DBG ((LOG_CRYPTO, 50,
"rsa_sig_decode_hash: certificate malformed"));
else
{
if (!handler->cert_get_key (cert, &key))
{
log_print ("rsa_sig_decode_hash: "
"decoding certificate failed");
handler->cert_free (cert);
}
else
{
found++;
LOG_DBG ((LOG_CRYPTO, 40,
"rsa_sig_decode_hash: using cert of type %d",
handler->id));
exchange->recv_cert = cert;
exchange->recv_certtype = handler->id;
}
}
}
else if (handler->id == ISAKMP_CERTENC_KEYNOTE)
handler->cert_insert (exchange->policy_id, rawcert);
free (rawcert);
}
/*
* Walk over potential CERT payloads in this message.
* XXX I believe this is the wrong spot for this. CERTs can appear
* anytime.
*/
for (p = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_CERT]); p;
p = TAILQ_NEXT (p, link))
{
p->flags |= PL_MARK;
/* When we have found a key, just walk over the rest, marking them. */
if (found)
continue;
handler = cert_get (GET_ISAKMP_CERT_ENCODING (p->p));
if (!handler)
{
LOG_DBG ((LOG_MISC, 30,
"rsa_sig_decode_hash: no handler for %s CERT encoding",
constant_lookup (isakmp_certenc_cst,
GET_ISAKMP_CERT_ENCODING (p->p))));
continue;
}
cert = handler->cert_get (p->p + ISAKMP_CERT_DATA_OFF,
GET_ISAKMP_GEN_LENGTH (p->p)
- ISAKMP_CERT_DATA_OFF);
if (!cert)
{
log_print ("rsa_sig_decode_hash: can not get data from CERT");
continue;
}
if (!handler->cert_validate (cert))
{
handler->cert_free (cert);
log_print ("rsa_sig_decode_hash: received CERT can't be validated");
continue;
}
if (GET_ISAKMP_CERT_ENCODING (p->p) == ISAKMP_CERTENC_X509_SIG)
{
if (!handler->cert_get_subject (cert, &id_cert, &id_cert_len))
{
handler->cert_free (cert);
log_print ("rsa_sig_decode_hash: can not get subject from CERT");
continue;
}
if (id_cert_len != id_len || memcmp (id, id_cert, id_len) != 0)
{
handler->cert_free (cert);
log_print ("rsa_sig_decode_hash: CERT subject does "
"not match ID");
free (id_cert);
continue;
}
free (id_cert);
}
if (!handler->cert_get_key (cert, &key))
{
handler->cert_free (cert);
log_print ("rsa_sig_decode_hash: decoding payload CERT failed");
continue;
}
/* We validated the cert, cache it for later use. */
handler->cert_insert (exchange->policy_id, cert);
exchange->recv_cert = cert;
exchange->recv_certtype = GET_ISAKMP_CERT_ENCODING (p->p);
#if defined(USE_POLICY) || defined(USE_KEYNOTE)
if (exchange->recv_certtype == ISAKMP_CERTENC_KEYNOTE)
{
struct keynote_deckey dc;
char *pp;
dc.dec_algorithm = KEYNOTE_ALGORITHM_RSA;
dc.dec_key = key;
pp = LK (kn_encode_key, (&dc, INTERNAL_ENC_PKCS1, ENCODING_HEX,
KEYNOTE_PUBLIC_KEY));
if (pp == NULL)
{
LK (kn_free_key, (&dc));
log_print ("rsa_sig_decode_hash: failed to ASCII-encode key");
return -1;
}
exchange->recv_key = calloc (strlen (pp) + strlen ("rsa-hex:") + 1,
sizeof(char));
if (exchange->recv_key == NULL)
{
free (pp);
LK (kn_free_key, (&dc));
log_print ("rsa_sig_decode_hash: failed to allocate %d bytes",
strlen (pp) + strlen ("rsa-hex:") + 1);
return -1;
}
sprintf (exchange->recv_key, "rsa-hex:%s", pp);
free (pp);
}
#endif
found++;
}
/* If no certificate provided a key, try the config file. */
if (!found)
{
#ifdef notyet
rawkey = ike_auth_get_key (IKE_AUTH_RSA_SIG, exchange->name, &keylen);
if (!rawkey)
{
log_print ("rsa_sig_decode_hash: no public key found");
return -1;
}
#else
log_print ("rsa_sig_decode_hash: no public key found");
return -1;
#endif
}
p = TAILQ_FIRST (&msg->payload[ISAKMP_PAYLOAD_SIG]);
if (!p)
{
log_print ("rsa_sig_decode_hash: missing signature payload");
LC (RSA_free, (key));
return -1;
}
/* Check that the sig is of the correct size. */
len = GET_ISAKMP_GEN_LENGTH (p->p) - ISAKMP_SIG_SZ;
if (len != LC (RSA_size, (key)))
{
LC (RSA_free, (key));
log_print ("rsa_sig_decode_hash: "
"SIG payload length does not match public key");
return -1;
}
*hash_p = malloc (len);
if (!*hash_p)
{
LC (RSA_free, (key));
log_error ("rsa_sig_decode_hash: malloc (%d) failed", len);
return -1;
}
len = LC (RSA_public_decrypt, (len, p->p + ISAKMP_SIG_DATA_OFF, *hash_p, key,
RSA_PKCS1_PADDING));
if (len == -1)
{
LC (RSA_free, (key));
log_print ("rsa_sig_decode_hash: RSA_public_decrypt () failed");
return -1;
}
LC (RSA_free, (key));
if (len != hashsize)
{
free (*hash_p);
*hash_p = 0;
log_print ("rsa_sig_decode_hash: len %d != hashsize %d", len, hashsize);
return -1;
}
snprintf (header, 80, "rsa_sig_decode_hash: HASH_%c", initiator ? 'R' : 'I');
LOG_DBG_BUF ((LOG_MISC, 80, header, *hash_p, hashsize));
p->flags |= PL_MARK;
return 0;
}
#endif /* USE_X509 || USE_KEYNOTE */
static int
pre_shared_encode_hash (struct message *msg)
{
struct exchange *exchange = msg->exchange;
struct ipsec_exch *ie = exchange->data;
size_t hashsize = ie->hash->hashsize;
char header[80];
int initiator = exchange->initiator;
u_int8_t *buf;
buf = ipsec_add_hash_payload (msg, hashsize);
if (!buf)
return -1;
if (ike_auth_hash (exchange, buf + ISAKMP_HASH_DATA_OFF) == -1)
return -1;
snprintf (header, 80, "pre_shared_encode_hash: HASH_%c",
initiator ? 'I' : 'R');
LOG_DBG_BUF ((LOG_MISC, 80, header, buf + ISAKMP_HASH_DATA_OFF, hashsize));
return 0;
}
#if defined(USE_X509) || defined(USE_KEYNOTE)
/* Encrypt the HASH into a SIG type. */
static int
rsa_sig_encode_hash (struct message *msg)
{
struct exchange *exchange = msg->exchange;
struct ipsec_exch *ie = exchange->data;
size_t hashsize = ie->hash->hashsize;
struct cert_handler *handler;
RSA *key;
char header[80];
int initiator = exchange->initiator;
u_int8_t *buf, *data, *buf2;
u_int32_t datalen;
u_int8_t *id;
size_t id_len;
int idtype;
in_addr_t addr;
id = initiator ? exchange->id_i : exchange->id_r;
id_len = initiator ? exchange->id_i_len : exchange->id_r_len;
/* XXX This needs to be configureable. */
idtype = ISAKMP_CERTENC_KEYNOTE;
doitagain:
handler = cert_get (idtype);
if (!handler)
{
log_print ("rsa_sig_encode_hash: "
"cert_get(ISAKMP_CERTENC_X509_SIG) failed");
return -1;
}
/* Find a certificate with subjectAltName = id. */
if (handler->cert_obtain (id, id_len, 0, &data, &datalen))
{
buf = realloc (data, ISAKMP_CERT_SZ + datalen);
if (!buf)
{
log_error ("rsa_sig_encode_hash: realloc (%p, %d) failed", data,
ISAKMP_CERT_SZ + datalen);
free (data);
return -1;
}
memmove (buf + ISAKMP_CERT_SZ, buf, datalen);
SET_ISAKMP_CERT_ENCODING (buf, idtype);
if (message_add_payload (msg, ISAKMP_PAYLOAD_CERT, buf,
ISAKMP_CERT_SZ + datalen, 1))
{
free (buf);
return -1;
}
}
else
{
if (handler->id == ISAKMP_CERTENC_KEYNOTE)
{
idtype = ISAKMP_CERTENC_X509_SIG;
goto doitagain;
}
else
LOG_DBG ((LOG_MISC, 10,
"rsa_sig_encode_hash: no certificate to send"));
}
switch (id[ISAKMP_ID_TYPE_OFF - ISAKMP_GEN_SZ])
{
case IPSEC_ID_IPV4_ADDR:
buf2 = malloc (16);
if (!buf2)
{
log_error ("rsa_sig_encode_hash: malloc (16) failed");
return 0;
}
addr = htonl (decode_32 (id + ISAKMP_ID_DATA_OFF - ISAKMP_GEN_SZ));
inet_ntop (AF_INET, &addr, buf2, 16);
break;
case IPSEC_ID_FQDN:
case IPSEC_ID_USER_FQDN:
buf2 = calloc (id_len - ISAKMP_ID_DATA_OFF +
ISAKMP_GEN_SZ + 1, sizeof (char));
if (!buf2)
{
log_print ("rsa_sig_encode_hash: malloc (%d) failed",
id_len - ISAKMP_ID_DATA_OFF + ISAKMP_GEN_SZ + 1);
return 0;
}
memcpy (buf2, id + ISAKMP_ID_DATA_OFF - ISAKMP_GEN_SZ,
id_len - ISAKMP_ID_DATA_OFF + ISAKMP_GEN_SZ);
break;
/* XXX Support more ID types ? */
default:
buf2 = NULL;
break;
}
key = ike_auth_get_key (IKE_AUTH_RSA_SIG, exchange->name, buf2, NULL);
free (buf2);
if (key == NULL)
{
log_print ("rsa_sig_encode_hash: could not get private key");
return -1;
}
/* XXX hashsize is not necessarily prf->blocksize. */
buf = malloc (hashsize);
if (!buf)
{
log_error ("rsa_sig_encode_hash: malloc (%d) failed", hashsize);
LC (RSA_free, (key));
return -1;
}
if (ike_auth_hash (exchange, buf) == -1)
{
free (buf);
LC (RSA_free, (key));
return -1;
}
snprintf (header, 80, "rsa_sig_encode_hash: HASH_%c", initiator ? 'I' : 'R');
LOG_DBG_BUF ((LOG_MISC, 80, header, buf, hashsize));
data = malloc (LC (RSA_size, (key)));
if (!data)
{
log_error ("rsa_sig_encode_hash: malloc (%d) failed",
LC (RSA_size, (key)));
LC (RSA_free, (key));
return -1;
}
datalen
= LC (RSA_private_encrypt, (hashsize, buf, data, key, RSA_PKCS1_PADDING));
if (datalen == -1)
{
log_error ("rsa_sig_encode_hash: RSA_private_encrypt () failed");
free (buf);
LC (RSA_free, (key));
return -1;
}
LC (RSA_free, (key));
free (buf);
buf = realloc (data, ISAKMP_SIG_SZ + datalen);
if (!buf)
{
log_error ("rsa_sig_encode_hash: realloc (%p, %d) failed", data,
ISAKMP_SIG_SZ + datalen);
free (data);
return -1;
}
memmove (buf + ISAKMP_SIG_SZ, buf, datalen);
snprintf (header, 80, "rsa_sig_encode_hash: SIG_%c", initiator ? 'I' : 'R');
LOG_DBG_BUF ((LOG_MISC, 80, header, buf + ISAKMP_SIG_DATA_OFF, datalen));
if (message_add_payload (msg, ISAKMP_PAYLOAD_SIG, buf,
ISAKMP_SIG_SZ + datalen, 1))
{
free (buf);
return -1;
}
return 0;
}
#endif /* USE_X509 || USE_KEYNOTE */
int
ike_auth_hash (struct exchange *exchange, u_int8_t *buf)
{
struct ipsec_exch *ie = exchange->data;
struct prf *prf;
struct hash *hash = ie->hash;
int initiator = exchange->initiator;
u_int8_t *id;
size_t id_len;
/* Choose the right fields to fill-in. */
id = initiator ? exchange->id_i : exchange->id_r;
id_len = initiator ? exchange->id_i_len : exchange->id_r_len;
/* Allocate the prf and start calculating our HASH. */
prf = prf_alloc (ie->prf_type, hash->type, ie->skeyid, ie->skeyid_len);
if (!prf)
return -1;
prf->Init (prf->prfctx);
prf->Update (prf->prfctx, initiator ? ie->g_xi : ie->g_xr, ie->g_x_len);
prf->Update (prf->prfctx, initiator ? ie->g_xr : ie->g_xi, ie->g_x_len);
prf->Update (prf->prfctx,
exchange->cookies
+ (initiator ? ISAKMP_HDR_ICOOKIE_OFF : ISAKMP_HDR_RCOOKIE_OFF),
ISAKMP_HDR_ICOOKIE_LEN);
prf->Update (prf->prfctx,
exchange->cookies
+ (initiator ? ISAKMP_HDR_RCOOKIE_OFF : ISAKMP_HDR_ICOOKIE_OFF),
ISAKMP_HDR_ICOOKIE_LEN);
prf->Update (prf->prfctx, ie->sa_i_b, ie->sa_i_b_len);
prf->Update (prf->prfctx, id, id_len);
prf->Final (buf, prf->prfctx);
prf_free (prf);
return 0;
}
|