-
Notifications
You must be signed in to change notification settings - Fork 59
/
isakmp.c
2780 lines (2569 loc) · 83.2 KB
/
isakmp.c
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
/*
* The IKE Scanner (ike-scan) is Copyright (C) 2003-2013 Roy Hills,
* NTA Monitor Ltd.
*
* This file is part of ike-scan.
*
* ike-scan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ike-scan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ike-scan. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library, and distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version.
*
* If this license is unacceptable to you, I may be willing to negotiate
* alternative licenses (contact ike-scan@nta-monitor.com).
*
* You are encouraged to submit comments, improvements or suggestions
* at the github repository https://github.com/royhills/ike-scan
*
* Author: Roy Hills
* Date: 7 November 2003
*
* Functions to construct ISAKMP headers and payloads.
*
*/
#include "ike-scan.h"
const id_name_map notification_map[] = { /* From RFC 2408 3.14.1 */
{0, "UNSPECIFIED"},
{1, "INVALID-PAYLOAD-TYPE"},
{2, "DOI-NOT-SUPPORTED"},
{3, "SITUATION-NOT-SUPPORTED"},
{4, "INVALID-COOKIE"},
{5, "INVALID-MAJOR-VERSION"},
{6, "INVALID-MINOR-VERSION"},
{7, "INVALID-EXCHANGE-TYPE"},
{8, "INVALID-FLAGS"},
{9, "INVALID-MESSAGE-ID"},
{10, "INVALID-PROTOCOL-ID"},
{11, "INVALID-SPI"},
{12, "INVALID-TRANSFORM-ID"},
{13, "ATTRIBUTES-NOT-SUPPORTED"},
{14, "NO-PROPOSAL-CHOSEN"},
{15, "BAD-PROPOSAL-SYNTAX"},
{16, "PAYLOAD-MALFORMED"},
{17, "INVALID-KEY-INFORMATION"},
{18, "INVALID-ID-INFORMATION"},
{19, "INVALID-CERT-ENCODING"},
{20, "INVALID-CERTIFICATE"},
{21, "CERT-TYPE-UNSUPPORTED"},
{22, "INVALID-CERT-AUTHORITY"},
{23, "INVALID-HASH-INFORMATION"},
{24, "AUTHENTICATION-FAILED"},
{25, "INVALID-SIGNATURE"},
{26, "ADDRESS-NOTIFICATION"},
{27, "NOTIFY-SA-LIFETIME"},
{28, "CERTIFICATE-UNAVAILABLE"},
{29, "UNSUPPORTED-EXCHANGE-TYPE"},
{30, "UNEQUAL-PAYLOAD-LENGTHS"},
{9101, "Checkpoint-Firewall-1"},
{9110, "Checkpoint-Firewall-1"},
{24576, "RESPONDER-LIFETIME"}, /* Next 3 are from RFC 2407 4.6.3 */
{24577, "REPLAY-STATUS"},
{24578, "INITIAL-CONTACT"},
{-1, NULL}
};
const id_name_map notification_map2[] = { /* From RFC 5996 3.10.1 */
{0, "RESERVED"},
{1, "UNSUPPORTED_CRITICAL_PAYLOAD"},
{4, "INVALID_IKE_SPI"},
{5, "INVALID_MAJOR_VERSION"},
{7, "INVALID_SYNTAX"},
{9, "INVALID_MESSAGE_ID"},
{11, "INVALID_SPI"},
{14, "NO_PROPOSAL_CHOSEN"},
{17, "INVALID_KE_PAYLOAD"},
{24, "AUTHENTICATION_FAILED"},
{34, "SINGLE_PAIR_REQUIRED"},
{35, "NO_ADDITIONAL_SAS"},
{36, "INTERNAL_ADDRESS_FAILURE"},
{37, "FAILED_CP_REQUIRED"},
{38, "TS_UNACCEPTABLE"},
{39, "INVALID_SELECTORS"},
{43, "TEMPORARY_FAILURE"},
{44, "CHILD_SA_NOT_FOUND"},
{9101, "Checkpoint-Firewall-1"},
{9110, "Checkpoint-Firewall-1"},
{16384, "INITIAL_CONTACT"},
{16385, "SET_WINDOW_SIZE"},
{16386, "ADDITIONAL_TS_POSSIBLE"},
{16387, "IPCOMP_SUPPORTED"},
{16388, "NAT_DETECTION_SOURCE_IP"},
{16389, "NAT_DETECTION_DESTINATION_IP"},
{16390, "COOKIE"},
{16391, "USE_TRANSPORT_MODE"},
{16392, "HTTP_CERT_LOOKUP_SUPPORTED"},
{16393, "REKEY_SA"},
{16394, "ESP_TFC_PADDING_NOT_SUPPORTED"},
{16395, "NON_FIRST_FRAGMENTS_ALSO"},
{-1, NULL}
};
const id_name_map attr_map[] = { /* From RFC 2409 App. A and */
{1, "Enc"}, /* draft-ietf-ipsec-isakmp-gss-auth */
{2, "Hash"},
{3, "Auth"},
{4, "Group"},
{5, "GroupType"},
{6, "GroupPrime/IrreduciblePolynomial"},
{7, "GroupGeneratorOne"},
{8, "GroupGeneratorTwo"},
{9, "GroupCurve A"},
{10, "GroupCurve B"},
{11, "LifeType"},
{12, "LifeDuration"},
{13, "PRF"},
{14, "KeyLength"},
{15, "FieldSize"},
{16, "GroupOrder"},
{16384, "GSSIdentityName"},
{-1, NULL}
};
const id_name_map trans_type_map[] = { /* From RFC 5996 3.3.2 */
{1, "Encr"},
{2, "Prf"},
{3, "Integ"},
{4, "DH_Group"},
{5, "ESN"},
{-1, NULL}
};
const id_name_map enc_map[] = { /* From RFC 2409 App. A */
{1, "DES"},
{2, "IDEA"},
{3, "Blowfish"},
{4, "RC5"},
{5, "3DES"},
{6, "CAST"},
{7, "AES"}, /* RFC 3602 */
{8, "Camellia"}, /* RFC 4312 */
{65001, "Mars"}, /* Defined in strongSwan constants.h */
{65002, "RC6"}, /* Defined in strongSwan constants.h */
{65003, "ID_65003"}, /* Defined in strongSwan constants.h */
{65004, "Serpent"}, /* Defined in strongSwan constants.h */
{65005, "Twofish"}, /* Defined in strongSwan constants.h */
{-1, NULL}
};
const id_name_map encr_map[] = { /* From RFC 5996 (IKEv2) 3.3.2 */
{1, "DES_IV64"},
{2, "DES"},
{3, "3DES"},
{4, "RC5"},
{5, "IDEA"},
{6, "CAST"},
{7, "Blowfish"},
{8, "3IDEA"},
{9, "DES_IV32"},
{11, "NULL"},
{12, "AES_CBC"},
{13, "AES_CTR"},
{14, "AES_CCM_ICV8"}, /* RFC 5282 */
{15, "AES_CCM_ICV12"}, /* RFC 5282 */
{16, "AES_CCM_ICV16"}, /* RFC 5282 */
{18, "AES_GCM_ICV8"}, /* RFC 5282 */
{19, "AES_GCM_ICV12"}, /* RFC 5282 */
{20, "AES_GCM_ICV16"}, /* RFC 5282 */
{23, "CAMELLIA_CBC"}, /* RFC 5996 */
{-1, NULL}
};
const id_name_map hash_map[] = { /* From RFC 2409 App. A */
{1, "MD5"},
{2, "SHA1"},
{3, "Tiger"},
{4, "SHA2-256"},
{5, "SHA2-384"},
{6, "SHA2-512"},
{-1, NULL}
};
const id_name_map prf_map[] = { /* From RFC 5996 3.3.2 */
{1, "HMAC_MD5"},
{2, "HMAC_SHA1"},
{3, "HMAC_TIGER"},
{4, "AES128_XCBC"}, /* RFC 4434 */
{5, "HMAC_SHA2_256"}, /* RFC 4868 */
{6, "HMAC_SHA2_384"}, /* RFC 4868 */
{7, "HMAC_SHA2_512"}, /* RFC 4868 */
{8, "HMAC_AES128_CMAC"}, /* RFC 4615 */
{-1, NULL}
};
const id_name_map auth_map[] = { /* From RFC 2409 App. A */
{1, "PSK"},
{2, "DSS"},
{3, "RSA_Sig"},
{4, "RSA_Enc"},
{5, "RSA_RevEnc"},
{6, "ElGamel_Enc"},
{7, "ElGamel_RevEnc"},
{8, "ECDSA_Sig"},
{9, "ECDSA_SHA256"}, /* RFC 4754 */
{10, "ECDSA_SHA384"}, /* RFC 4754 */
{11, "ECDSA_SHA512"}, /* RFC 4754 */
{128, "CRACK"}, /* draft-harkins-ipsra-crack-00 */
{64221, "Hybrid_RSA"}, /* draft-ietf-ipsec-isakmp-hybrid-auth-05 */
{64223, "Hybrid_DSS"}, /* draft-ietf-ipsec-isakmp-hybrid-auth-05 */
{65001, "XAUTH_PSK"}, /* draft-ietf-ipsec-isakmp-xauth-06 */
{65003, "XAUTH_DSS"}, /* draft-ietf-ipsec-isakmp-xauth-06 */
{65005, "XAUTH_RSA"}, /* draft-ietf-ipsec-isakmp-xauth-06 */
{65007, "XAUTH_RSA_Enc"}, /* draft-ietf-ipsec-isakmp-xauth-06 */
{65009, "XAUTH_RSA_RevEnc"}, /* draft-ietf-ipsec-isakmp-xauth-06 */
{-1, NULL}
};
const id_name_map integ_map[] = { /* From RFC 5996 3.3.2 */
{1, "HMAC_MD5_96"},
{2, "HMAC_SHA1_96"},
{3, "DES_MAC"},
{4, "KPDK_MD5"},
{5, "AES_XCBC_96"},
{6, "HMAC_MD5_128"}, /* RFC 4595 */
{7, "HMAC_SHA1_160"}, /* RFC 4595 */
{8, "AES_CMAC_96"}, /* RFC 4494 */
{9, "AES_128_GMAC"}, /* RFC 4543 */
{10, "AES_192_GMAC"}, /* RFC 4543 */
{11, "AES_256_GMAC"}, /* RFC 4543 */
{12, "HMAC_SHA2_256_128"}, /* RFC 4868 */
{13, "HMAC_SHA2_384_192"}, /* RFC 4868 */
{14, "HMAC_SHA2_512_256"}, /* RFC 4868 */
{-1, NULL}
};
const id_name_map dh_map[] = { /* From RFC 2409 App. A */
{1, "1:modp768"},
{2, "2:modp1024"},
{3, "3:ec2n155"},
{4, "4:ec2n185"},
{5, "5:modp1536"}, /* RFC 3526 */
{6, "6:ec2n163"},
{7, "7:ec2n163"},
{8, "8:ec2n283"},
{9, "9:ec2n283"},
{10, "10:ec2n409"},
{11, "11:ec2n409"},
{12, "12:ec2n571"},
{13, "13:ec2n571"},
{14, "14:modp2048"}, /* RFC 3526 */
{15, "15:modp3072"}, /* RFC 3526 */
{16, "16:modp4096"}, /* RFC 3526 */
{17, "17:modp6144"}, /* RFC 3526 */
{18, "18:modp8192"}, /* RFC 3526 */
{19, "19:ecp256"}, /* RFC 5903 */
{20, "20:ecp384"}, /* RFC 5903 */
{21, "21:ecp521"}, /* RFC 5903 */
{22, "22:modp1024s160"}, /* RFC 5114 */
{23, "23:modp2048s224"}, /* RFC 5114 */
{24, "24:modp2048s256"}, /* RFC 5114 */
{25, "25:ecp192"}, /* RFC 5114 */
{26, "26:ecp224"}, /* RFC 5114 */
{27, "27:brainpoolP224r1"}, /* RFC 6954 */
{28, "28:brainpoolP256r1"}, /* RFC 6954 */
{29, "29:brainpoolP384r1"}, /* RFC 6954 */
{30, "30:brainpoolP512r1"}, /* RFC 6954 */
{-1, NULL}
};
const id_name_map life_map[] = { /* From RFC 2409 App. A */
{1, "Seconds"},
{2, "Kilobytes"},
{-1, NULL}
};
const id_name_map payload_map[] = { /* Payload types from RFC 2408 3.1 */
{1, "SecurityAssociation"}, /* and RFC 4306 3.2 */
{2, "Proposal"},
{3, "Transform"},
{4, "KeyExchange"},
{5, "Identification"},
{6, "Certificate"},
{7, "CertificateRequest"},
{8, "Hash"},
{9, "Signature"},
{10, "Nonce"},
{11, "Notification"},
{12, "Delete"},
{13, "VendorID"},
{20, "NAT-D"}, /* RFC 3947 NAT Discovery */
{33, "SecurityAssociation"}, /* Values 33-48 are from RFC 5996 IKEv2 */
{34, "KeyExchange"},
{35, "IDI"},
{36, "IDR"},
{37, "Certificate"},
{38, "CertificateRequest"},
{39, "AUTH"},
{40, "Nonce"},
{41, "Notification"},
{42, "Delete"},
{43, "VendorID"},
{44, "TSI"},
{45, "TSR"},
{46, "Encrypted"},
{47, "Configuration"},
{48, "EAP"},
{49, "GSPM"}, /* RFC 6467 */
{-1, NULL}
};
const id_name_map doi_map[] = {
{0, "ISAKMP"},
{1, "IPsec"},
{2, "GDOI"}, /* RFC 6407 */
{-1, NULL}
};
const id_name_map protocol_map[] = {
{1, "PROTO_ISAKMP"},
{2, "PROTO_IPSEC_AH"},
{3, "PROTO_IPSEC_ESP"},
{4, "PROTO_IPSEC_COMP"},
{-1, NULL}
};
const id_name_map id_map[] = { /* From RFC 2407 4.6.2.1 */
{1, "ID_IPV4_ADDR"},
{2, "ID_FQDN"},
{3, "ID_USER_FQDN"},
{4, "ID_IPV4_ADDR_SUBNET"},
{5, "ID_IPV6_ADDR"},
{6, "ID_IPV6_ADDR_SUBNET"},
{7, "ID_IPV4_ADDR_RANGE"},
{8, "ID_IPV6_ADDR_RANGE"},
{9, "ID_DER_ASN1_DN"},
{10, "ID_DER_ASN1_GN"},
{11, "ID_KEY_ID"},
};
const id_name_map cert_map[] = { /* From RFC 2408 Sec. 3.9 */
{1, "PKCS #7 wrapped X.509 certificate"},
{2, "PGP Certificate"},
{3, "DNS Signed Key"},
{4, "X.509 Certificate - Signature"},
{5, "X.509 Certificate - Key Exchange"},
{6, "Kerberos Tokens"},
{7, "Certificate Revocation List (CRL)"},
{8, "Authority Revocation List (ARL)"},
{9, "SPKI Certificate"},
{10, "X.509 Certificate - Attribute"},
{-1, NULL}
};
extern psk_crack psk_values;
extern int mbz_value;
/*
* make_isakmp_hdr -- Construct an ISAKMP Header
*
* Inputs:
*
* xchg Exchange Type (e.g. ISAKMP_XCHG_IDPROT for main mode)
* next Next Payload Type
* length ISAKMP Message total length
* header_version Version number to put in the header
* hdr_flags Flags to put in the header
* hdr_msgid Message ID to put in the header
* rcookie_data Responder cookie data, or NULL for default
* rcookie_data_len Length of responder cookie data (<=8)
*
* Returns:
*
* Pointer to created ISAKMP Header.
*
* This constructs an ISAKMP header. It fills in the static values.
* The initiator cookie should be changed to a unique per-host value
* before the packet is sent.
*/
unsigned char*
make_isakmp_hdr(unsigned xchg, unsigned next, unsigned length,
int header_version, int hdr_flags, unsigned hdr_msgid,
unsigned char *rcookie_data, size_t rcookie_data_len) {
unsigned char *payload;
struct isakmp_hdr* hdr;
payload = Malloc(sizeof(struct isakmp_hdr));
hdr = (struct isakmp_hdr*) payload; /* Overlay header struct on payload */
memset(hdr, mbz_value, sizeof(struct isakmp_hdr));
hdr->isa_icookie[0] = 0xdeadbeef; /* Initiator cookie */
hdr->isa_icookie[1] = 0xdeadbeef;
hdr->isa_rcookie[0] = 0; /* Set responder cookie to 0 */
hdr->isa_rcookie[1] = 0;
if (rcookie_data) {
memcpy(hdr->isa_rcookie, rcookie_data, rcookie_data_len);
}
hdr->isa_np = next; /* Next Payload Type */
hdr->isa_version = header_version; /* v1.0 by default */
hdr->isa_xchg = xchg; /* Exchange type */
hdr->isa_flags = hdr_flags; /* Flags */
hdr->isa_msgid = htonl(hdr_msgid); /* Message ID */
hdr->isa_length = htonl(length); /* Total ISAKMP message length */
return payload;
}
/*
* make_sa -- Construct an SA payload
*
* Inputs:
*
* outlen (output) length of SA payload
* next Next Payload Type
* doi Domain of interpretation
* situation Situation
* proposals Pointer to list of proposals
* proposal_len length of proposal list
*
* Returns:
*
* Pointer to the SA payload.
*
* This constructs an SA payload.
*/
unsigned char*
make_sa(size_t *outlen, unsigned next, unsigned doi, unsigned situation,
unsigned char *proposals, size_t proposal_len) {
unsigned char *payload;
struct isakmp_sa* hdr;
unsigned char *cp;
size_t len;
hdr = Malloc(sizeof(struct isakmp_sa));
memset(hdr, mbz_value, sizeof(struct isakmp_sa));
hdr->isasa_np = next; /* Next Payload Type */
hdr->isasa_doi = htonl(doi); /* Default is IPsec DOI */
hdr->isasa_situation = htonl(situation); /* Default SIT_IDENTITY_ONLY */
len = sizeof(struct isakmp_sa) + proposal_len;
hdr->isasa_length = htons(len); /* SA Payload length */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_sa));
cp += sizeof(struct isakmp_sa);
memcpy(cp, proposals, proposal_len);
*outlen = len;
return payload;
}
/*
* make_sa2 -- Construct an IKEv2 SA payload
*
* Inputs:
*
* outlen (output) length of SA payload
* next Next Payload Type
* proposals Pointer to list of proposals
* proposal_len length of proposal list
*
* Returns:
*
* Pointer to the SA payload.
*
* This constructs an IKEv2 SA payload.
*/
unsigned char*
make_sa2(size_t *outlen, unsigned next,
unsigned char *proposals, size_t proposal_len) {
unsigned char *payload;
struct isakmp_sa2* hdr;
unsigned char *cp;
size_t len;
hdr = Malloc(sizeof(struct isakmp_sa2));
memset(hdr, mbz_value, sizeof(struct isakmp_sa2));
hdr->isasa2_np = next; /* Next Payload Type */
len = sizeof(struct isakmp_sa2) + proposal_len;
hdr->isasa2_length = htons(len); /* SA Payload length */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_sa2));
cp += sizeof(struct isakmp_sa2);
memcpy(cp, proposals, proposal_len);
*outlen = len;
return payload;
}
/*
* add_prop -- Add a proposal payload to the list of proposals
*
* Inputs:
*
* outlen (output) Proposal payload length
* notrans Number of transforms in this proposal
* protocol Protocol
* spi_size SPI Size
* transforms Pointer to transform list
* transform_len Length of transform list
*
* Returns:
*
* Pointer to proposal payload.
*
* This function can either be called with finished = 0, in which case
* notrans, protocol, spi_size, transforms and transform_len must be
* specified, and the function will return NULL, OR it can be called with
* finished = 1 in which case notrans, protocol, spi_size, transforms and
* transform_len are ignored and the function will return a pointer to the
* finished payload and will set *length to the length of this payload.
*
* ISAKMP SAs are only allowed to contain one proposal, RFC 2409 section 5
* states:
*
* "To put it another way, for phase 1 exchanges there MUST NOT be
* multiple Proposal Payloads for a single SA payload and there MUST NOT
* be multiple SA payloads."
*
* However, this function does not enforce this restriction.
*/
unsigned char*
add_prop(int finished, size_t *outlen,
unsigned notrans, unsigned protocol, unsigned spi_size,
unsigned char *transforms, size_t transform_len) {
static int first_proposal = 1;
static unsigned char *prop_start=NULL; /* Start of set of proposals */
static size_t cur_offset; /* Start of current proposal */
static size_t end_offset; /* End of proposals */
static unsigned prop_no=1;
unsigned char *prop; /* Proposal payload */
size_t len; /* Proposal length */
/*
* Construct a proposal if we are not finalising.
* Set next to ISAKMP_NEXT_P (more proposals), and increment prop_no for next
* time round.
*/
if (!finished) {
prop = make_prop(&len, ISAKMP_NEXT_P, prop_no, notrans, protocol,
spi_size, transforms, transform_len);
prop_no++;
if (first_proposal) {
cur_offset = 0;
end_offset = len;
prop_start = Malloc(end_offset);
memcpy(prop_start, prop, len);
first_proposal = 0;
} else {
cur_offset = end_offset;
end_offset += len;
prop_start = Realloc(prop_start, end_offset);
memcpy(prop_start+cur_offset, prop, len);
}
free(prop);
return NULL;
} else {
struct isakmp_proposal* hdr =
(struct isakmp_proposal*) (prop_start+cur_offset); /* Overlay */
first_proposal = 1;
hdr->isap_np = ISAKMP_NEXT_NONE; /* No more proposals */
*outlen = end_offset;
return prop_start;
}
}
/*
* make_prop -- Construct a proposal payload
*
* Inputs:
*
* outlen (output) Proposal payload length
* next next payload (2=more props, 0=no more props)
* number proposal number
* notrans Number of transforms in this proposal
* protocol Protocol
* spi_size SPI Size
* transforms Pointer to transform list
* transform_len Length of transform list
*
* Returns:
*
* Pointer to proposal payload.
*
* This constructs a single proposal payload.
*/
unsigned char*
make_prop(size_t *outlen, unsigned next, unsigned number, unsigned notrans,
unsigned protocol, unsigned spi_size, unsigned char *transforms,
size_t transform_len) {
unsigned char *payload;
struct isakmp_proposal* hdr;
unsigned char *cp;
size_t len;
/* Allocate and initialise the proposal header */
hdr = Malloc(sizeof(struct isakmp_proposal));
memset(hdr, mbz_value, sizeof(struct isakmp_proposal));
hdr->isap_np = next;
hdr->isap_proposal = number;
hdr->isap_protoid = protocol;
hdr->isap_spisize = spi_size; /* SPI Size */
hdr->isap_notrans = notrans; /* Number of transforms */
/* Determine total SA length and allocate payload memory */
len = sizeof(struct isakmp_proposal) + spi_size + transform_len;
hdr->isap_length = htons(len); /* Proposal payload length */
payload = Malloc(len);
cp = payload;
/* Copy the proposal header to the payload */
memcpy(cp, hdr, sizeof(struct isakmp_proposal));
cp += sizeof(struct isakmp_proposal);
free(hdr);
/* If the SPI size is non-zero, add a random SPI of the specified length */
if (spi_size > 0) {
unsigned i;
for (i=0; i<spi_size; i++)
*(cp++) = (unsigned char) random_byte();
}
/* Add the transforms */
memcpy(cp, transforms, transform_len);
*outlen = len;
return payload;
}
/*
* make_trans_simple -- Construct a single simple transform payload
*
* Inputs:
*
* length (output) length of entire transform payload.
* next Next Payload Type (3 = More transforms; 0=No more transforms)
* number Transform number
* cipher The encryption algorithm
* keylen Key length for variable length keys (0=fixed key length)
* hash Hash algorithm
* auth Authentication method
* group DH Group number
* lifetime Lifetime in seconds (0=no lifetime)
* lifesize Life in kilobytes (0=no life)
*
* Returns:
*
* Pointer to transform payload.
*
* This constructs a single simple transform payload.
* Most of the values are defined in RFC 2409 Appendix A.
*
* This function can only create a transform with a restricted set of
* attributes in a defined order. To create a transform with an arbitrary
* set of attributes in any order, use the make_transform function
* instead.
*/
unsigned char*
make_trans_simple(size_t *length, unsigned next, unsigned number,
unsigned cipher, unsigned keylen, unsigned hash, unsigned auth,
unsigned group, unsigned char *lifetime_data,
size_t lifetime_data_len, unsigned char *lifesize_data,
size_t lifesize_data_len, int gss_id_flag, unsigned char *gss_data,
size_t gss_data_len, unsigned trans_id) {
unsigned char *payload;
unsigned char *attr;
size_t attr_len; /* Attribute Length */
/* Allocate and initialise the mandatory attributes */
add_attr(0, NULL, 'B', OAKLEY_ENCRYPTION_ALGORITHM, 0, cipher, NULL);
add_attr(0, NULL, 'B', OAKLEY_HASH_ALGORITHM, 0, hash, NULL);
add_attr(0, NULL, 'B', OAKLEY_AUTHENTICATION_METHOD, 0, auth, NULL);
add_attr(0, NULL, 'B', OAKLEY_GROUP_DESCRIPTION, 0, group, NULL);
/* Allocate and initialise the optional attributes */
if (keylen)
add_attr(0, NULL, 'B', OAKLEY_KEY_LENGTH, 0, keylen, NULL);
if (lifetime_data_len) {
add_attr(0, NULL, 'B', OAKLEY_LIFE_TYPE, 0, SA_LIFE_TYPE_SECONDS, NULL);
add_attr(0, NULL, 'V', OAKLEY_LIFE_DURATION, lifetime_data_len, 0,
lifetime_data);
}
if (lifesize_data_len) {
add_attr(0, NULL, 'B', OAKLEY_LIFE_TYPE, 0, SA_LIFE_TYPE_KBYTES, NULL);
add_attr(0, NULL, 'V', OAKLEY_LIFE_DURATION, lifesize_data_len, 0,
lifesize_data);
}
if (gss_id_flag)
add_attr(0, NULL, 'V', OAKLEY_GSS_ID, gss_data_len, 0, gss_data);
/* Finalise attributes and fill in length value */
attr = add_attr(1, &attr_len, '\0', 0, 0, 0, NULL);
/* Create transform */
payload = make_transform(length, next, number, trans_id, attr, attr_len);
free(attr);
return payload;
}
/*
* add_trans_simple -- Add a simple transform payload to set of transforms.
*
* Inputs:
*
* finished 0 if adding a new transform; 1 if finalising.
* length (output) length of entire transform payload.
* cipher The encryption algorithm
* keylen Key length for variable length keys (0=fixed key length)
* hash Hash algorithm
* auth Authentication method
* group DH Group number
* lifetime Lifetime in seconds (0=no lifetime)
*
* Returns:
*
* Pointer to new set of transform payloads.
*
* This function can either be called with finished = 0, in which case
* cipher, keylen, hash, auth, group and lifetime must be specified, and
* the function will return NULL, OR it can be called with finished = 1
* in which case cipher, keylen, hash, auth, group and lifetime are
* ignored and the function will return a pointer to the finished
* payload and will set *length to the length of this payload.
*
* This function can only create transforms with a restricted set of
* attributes in a defined order. To create transforms with an arbitrary
* set of attributes in any order, use the add_transform function
* instead.
*/
unsigned char*
add_trans_simple(int finished, size_t *length, unsigned cipher,
unsigned keylen, unsigned hash, unsigned auth,
unsigned group, unsigned char *lifetime_data,
size_t lifetime_data_len, unsigned char *lifesize_data,
size_t lifesize_data_len, int gss_id_flag,
unsigned char *gss_data, size_t gss_data_len,
unsigned trans_id) {
static int first_transform = 1;
static unsigned char *trans_start=NULL; /* Start of set of transforms */
static size_t cur_offset; /* Start of current transform */
static size_t end_offset; /* End of transforms */
static unsigned trans_no=1;
unsigned char *trans; /* Transform payload */
size_t len; /* Transform length */
/*
* Construct a transform if we are not finalising.
* Set next to ISAKMP_NEXT_T (more transforms), and increment trans_no for
* next time round.
*/
if (!finished) {
trans = make_trans_simple(&len, ISAKMP_NEXT_T, trans_no, cipher, keylen,
hash, auth, group, lifetime_data,
lifetime_data_len, lifesize_data,
lifesize_data_len, gss_id_flag, gss_data,
gss_data_len, trans_id);
trans_no++;
if (first_transform) {
cur_offset = 0;
end_offset = len;
trans_start = Malloc(end_offset);
memcpy(trans_start, trans, len);
first_transform = 0;
} else {
cur_offset = end_offset;
end_offset += len;
trans_start = Realloc(trans_start, end_offset);
memcpy(trans_start+cur_offset, trans, len);
}
free(trans);
return NULL;
} else {
struct isakmp_transform* hdr =
(struct isakmp_transform*) (trans_start+cur_offset); /* Overlay */
first_transform = 1;
hdr->isat_np = 0; /* No more transforms */
*length = end_offset;
return trans_start;
}
}
/*
* make_transform -- Construct a single transform payload
*
* Inputs:
*
* length (output) length of entire transform payload.
* next Next Payload Type (3 = More transforms; 0=No more transforms)
* number Transform number
* trans_id Transform ID (generally KEY_IKE)
* attr Pointer to list of attributes
* attr_len Attribute length in bytes
*
* Returns:
*
* Pointer to transform payload.
*
* This constructs a single transform payload.
* Most of the values are defined in RFC 2409 Appendix A.
*/
unsigned char*
make_transform(size_t *length, unsigned next, unsigned number,
unsigned trans_id, unsigned char *attr, size_t attr_len) {
struct isakmp_transform* hdr; /* Transform header */
unsigned char *payload;
unsigned char *cp;
size_t len; /* Payload Length */
/* Allocate and initialise the transform header */
hdr = Malloc(sizeof(struct isakmp_transform));
memset(hdr, mbz_value, sizeof(struct isakmp_transform));
hdr->isat_np = next; /* Next payload type */
hdr->isat_transnum = number; /* Transform Number */
hdr->isat_transid = trans_id;
len = attr_len + sizeof(struct isakmp_transform);
hdr->isat_length = htons(len); /* Transform length */
*length = len;
/* Allocate memory for payload and copy structures to payload */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_transform));
free(hdr);
cp += sizeof(struct isakmp_transform);
memcpy(cp, attr, attr_len);
return payload;
}
/*
* add_transform -- Add a transform payload to set of transforms.
*
* Inputs:
*
* finished 0 if adding a new transform; 1 if finalising.
* length (output) length of entire transform payload.
* trans_id Transform ID
* attr Pointer to list of attributes
* attr_len Length of attribute list
*
* Returns:
*
* Pointer to new set of transform payloads.
*
* This function can either be called with finished = 0, in which case
* attr and attr_len must be specified, and the function will return NULL,
* OR it can be called with finished = 1 in which case attr and attr_len
* are ignored and the function will return a pointer to the finished
* payload and will set *length to the length of this payload.
*/
unsigned char*
add_transform(int finished, size_t *length, unsigned trans_id,
unsigned char *attr, size_t attr_len) {
static int first_transform = 1;
static unsigned char *trans_start=NULL; /* Start of set of transforms */
static size_t cur_offset; /* Start of current transform */
static size_t end_offset; /* End of transforms */
static unsigned trans_no=1;
unsigned char *trans; /* Transform payload */
size_t len; /* Transform length */
/*
* Construct a transform if we are not finalising.
* Set next to ISAKMP_NEXT_T (more transforms), and increment trans_no for
* next time round.
*/
if (!finished) {
trans = make_transform(&len, ISAKMP_NEXT_T, trans_no, trans_id, attr,
attr_len);
trans_no++;
if (first_transform) {
cur_offset = 0;
end_offset = len;
trans_start = Malloc(end_offset);
memcpy(trans_start, trans, len);
first_transform = 0;
} else {
cur_offset = end_offset;
end_offset += len;
trans_start = Realloc(trans_start, end_offset);
memcpy(trans_start+cur_offset, trans, len);
}
free(trans);
return NULL;
} else {
struct isakmp_transform* hdr =
(struct isakmp_transform*) (trans_start+cur_offset); /* Overlay */
first_transform = 1;
hdr->isat_np = ISAKMP_NEXT_NONE; /* No more transforms */
*length = end_offset;
return trans_start;
}
}
/*
* make_transform2 -- Construct a single IKEv2 transform payload
*
* Inputs:
*
* length (output) length of entire transform payload.
* next Next Payload Type (3 = More transforms; 0=No more transforms)
* trans_type Transform type
* trans_id Transform ID
* attr Pointer to list of attributes, or NULL for no attributes
* attr_len Attribute length in bytes. Zero if no attributes.
*
* Returns:
*
* Pointer to transform payload.
*
* This constructs a single IKEv2 transform payload.
* Most of the values are defined in RFC 5996 Section 3.3.
*/
unsigned char*
make_transform2(size_t *length, unsigned next, unsigned trans_type,
unsigned trans_id, unsigned char *attr, size_t attr_len) {
struct isakmp_transform2* hdr; /* Transform header */
unsigned char *payload;
unsigned char *cp;
size_t len; /* Payload Length */
/* Allocate and initialise the transform header */
hdr = Malloc(sizeof(struct isakmp_transform2));
memset(hdr, mbz_value, sizeof(struct isakmp_transform2));
hdr->isat2_np = next; /* Next payload type */
hdr->isat2_transtype = trans_type; /* Transform Type */
hdr->isat2_transid = htons(trans_id); /* Transform ID */
len = attr_len + sizeof(struct isakmp_transform2);
hdr->isat2_length = htons(len); /* Transform length */
*length = len;
/* Allocate memory for payload and copy structures to payload */
payload = Malloc(len);
cp = payload;
memcpy(cp, hdr, sizeof(struct isakmp_transform2));
free(hdr);
cp += sizeof(struct isakmp_transform2);
memcpy(cp, attr, attr_len);
return payload;
}
/*
* add_transform2 -- Add a transform payload to set of transforms.
*
* Inputs:
*
* finished 0 if adding a new transform; 1 if finalising.
* length (output) length of entire transform payload.
* trans_type Transform type
* trans_id Transform ID
* attr Pointer to list of attributes
* attr_len Length of attribute list
*
* Returns:
*
* Pointer to new set of transform payloads.
*
* This function can either be called with finished = 0, in which case
* attr and attr_len must be specified, and the function will return NULL,
* OR it can be called with finished = 1 in which case attr and attr_len