-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksrcommon.c
1739 lines (1577 loc) · 49.3 KB
/
ksrcommon.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
/*
* $Id: ksrcommon.c 583 2011-09-14 22:15:49Z lamb $
*
* Copyright (c) 2007 Internet Corporation for Assigned Names ("ICANN")
*
* Author: Richard H. Lamb ("RHL") [email protected]
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "util.h"
#include "logger.h"
#include "ksrcommon.h"
#include "ksrpolicy.h"
#include "pkcs11_dnssec.h"
#include "compat.h"
static signature *sig;
static signer *sgr;
static krecord *key;
static reqresp *rq;
static responsepolicy *rppolicy;
#if 0
static void dump_requestresponse(reqresp *rq);
#endif /* 0 */
static void free_signature(signature *s);
static void free_signer(signer *s);
static void free_responsepolicy(responsepolicy *r);
static char *gattr(char *tp,char *str);
static time_t ztime2sec(char *s);
static int fillinpinfo(krecord *kr);
static uint16_t updatekeytag(krecord *kr); // Called earlier to check RequestBundle keyTags with Key material
#define DBUFSIZE 2048
/*! Local zeroed memory allocation wrapper.
No real recovery from lack of memory. Better to just exit.
\param n elements
\param j of size j bytes each
\return char ptr to cleared allocated buffer
*/
static char *ksr_calloc(int n,int j)
{
char *p;
if((p=calloc(n,j)) == NULL) {
logger_fatal("fatal: Can not calloc(%d,%d) memory in %s",n,j,__func__);
}
return p;
}
/*! Standalone lightweight basic recursive XML parser for KSR and SKR
\param tp tag string to operate on. Initially empty str "".
\param xs I/O structure that also contains open imput file ptr "fin"
\return 0 on success. -1 for error
*/
int xmlparse(char *tp,xmlstate *xs)
{
int i,k,sttg,otag;
char lbuf[DBUFSIZE];
char data[DBUFSIZE];
char *q2p;
data[0] = '\0';
xs->depth++;
sttg = 0;
k = 0;
otag = 0;
if(xs->shorttag) {
char *p,*q;
q = &lbuf[2];
p = tp;
while(1) {
if(*p == '\0' || *p == ' ') break;
*q++ = *p++;
}
*q = '\0';
xs->shorttag = 0;
goto xmlprocess;
}
while((i=fgetc(xs->fin)) != EOF) {
if(i == '\n' || i == '\r') i = ' '; /* continue; */
if(k < (DBUFSIZE-3)) lbuf[k++] = i;
else {
lbuf[k] = i;
logger_error("%s depth=%d buffer length exceeded - truncating",
__func__,xs->depth);
return -1;
}
if(i == '>' && sttg) {
lbuf[k-1] = '\0';
/*myx_syslog(LOG_INFO,"===============|%s|\n",lbuf);*/
if(k > 1 && lbuf[k-2] == '/') {
lbuf[k-2] = '\0';
xs->shorttag = 1;
}
k = 0;
sttg = 0;
if(lbuf[1] != '/' && otag == 0) { /* started tag - resurse */
char *p;
/*
* parse tag attributes at start
*/
p = &lbuf[1];
#define KSR_STR "KSR "
if(strncasecmp(p,KSR_STR,sizeof(KSR_STR)-1) == 0) {
if((xs->ksrserial=gattr(p,"serial=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",p,"serial=");
ksrinvalid++;
break;
}
if((xs->ksrid=gattr(p,"id=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",p,"id=");
ksrinvalid++;
break;
}
if((xs->ksrdomain=gattr(p,"domain=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",p,"domain=");
ksrinvalid++;
break;
}
if(debug) logger_debug("=== KSR start serial=\"%s\" id=\"%s\" domain=\"%s\"",
xs->ksrserial,xs->ksrid,xs->ksrdomain);
#define RQSTBNDLE_STR "RequestBundle "
} else if(strncasecmp(p,RQSTBNDLE_STR,sizeof(RQSTBNDLE_STR)-1) == 0) {
if(rq) {
logger_warning("Unfreed RequestBundle");
free_requestresponse(rq);
rq = NULL;
}
rq = (reqresp *)ksr_calloc(1,sizeof(reqresp));
if((rq->id=gattr(p,"id=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",p,"id=");
ksrinvalid++;
break;
}
#define SGNR_STR "Signer "
} else if(strncasecmp(p,SGNR_STR,sizeof(SGNR_STR)-1) == 0) {
if(sgr) {
logger_warning("Unfreed Signer");
free_signer(sgr);
sgr = NULL;
}
sgr = (signer *)ksr_calloc(1,sizeof(signer));
if((sgr->keyIdentifier=gattr(p,"keyIdentifier=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",
p,"keyIdentifier=");
ksrinvalid++;
break;
}
#define KEY_STR "Key "
} else if(strncasecmp(p,KEY_STR,sizeof(KEY_STR)-1) == 0) {
char *q2;
if(key) {
logger_warning("Unfreed Key");
free_keyrecord(key);
key = NULL;
}
key = (krecord *)ksr_calloc(1,sizeof(krecord));
if((key->keyIdentifier=gattr(p,"keyIdentifier=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",
p,"keyIdentifier=");
ksrinvalid++;
break;
}
if((q2=gattr(p,"keyTag=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",
p,"keyTag=");
ksrinvalid++;
break;
}
key->keyTag = strtol(q2,&q2p,10);
if(*q2p) {
logger_error("keyTag non numeric \"%s\"",q2);
ksrinvalid++;
free(q2);
break;
}
free(q2);
#define SIGNATURE_STR "Signature "
} else if(strncasecmp(p,SIGNATURE_STR,sizeof(SIGNATURE_STR)-1) == 0) {
if(sig) {
logger_warning("Unfreed Signature");
free_signature(sig);
sig = NULL;
}
sig = (signature *)ksr_calloc(1,sizeof(signature));
if((sig->keyIdentifier=gattr(p,"keyIdentifier=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",
p,"keyIdentifier=");
ksrinvalid++;
break;
}
#define RSPNSBNDLE_STR "ResponseBundle "
} else if(strncasecmp(p,RSPNSBNDLE_STR,sizeof(RSPNSBNDLE_STR)-1) == 0) {
if(rq) {
logger_warning("Unfreed ResponseBundle");
free_requestresponse(rq);
rq = NULL;
}
rq = (reqresp *)ksr_calloc(1,sizeof(reqresp));
rq->response = 1;
if((rq->id=gattr(p,"id=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",p,"id=");
ksrinvalid++;
break;
}
#define RQSTPLCY_STR "RequestPolicy"
} else if(strncasecmp(p,RQSTPLCY_STR,sizeof(RQSTPLCY_STR)-1) == 0) {
if(rppolicy) {
logger_warning("Unfreed ResponsePolicy");
free_responsepolicy(rppolicy);
rppolicy = NULL;
}
rppolicy = (responsepolicy *)ksr_calloc(1,sizeof(responsepolicy));
#define SIGALG_STR "SignatureAlgorithm "
} else if(strncasecmp(p,SIGALG_STR,sizeof(SIGALG_STR)-1) == 0) {
if(rppolicy) {
char *q2;
sigalg *xpsigalg;
// Add multiple SignatureAlgorithm support
xpsigalg = (sigalg *)ksr_calloc(1,sizeof(sigalg));
if(rppolicy->sigalg) {
sigalg *xp;
for(xp=rppolicy->sigalg;xp->next;xp=xp->next) ;
xp->next = xpsigalg;
} else {
rppolicy->sigalg = xpsigalg;
}
if((q2=gattr(p,"algorithm=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",p,"algorithm=");
ksrinvalid++;
break;
}
xpsigalg->algorithm = strtol(q2,&q2p,10); // Add multiple SignatureAlgorithm support
if(*q2p) {
logger_error("algorithm non numeric \"%s\"",q2);
ksrinvalid++;
free(q2);
break;
}
free(q2);
}
#define RSACMP_STR "RSA "
} else if(strncasecmp(p,RSACMP_STR,sizeof(RSACMP_STR)-1) == 0) {
if(rppolicy && rppolicy->sigalg) {
char *q2;
sigalg *xp;
// Add multiple SignatureAlgorithm support
for(xp=rppolicy->sigalg;xp->next;xp=xp->next) ;
if((q2=gattr(p,"size=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",
p,"size=");
ksrinvalid++;
break;
}
xp->rsa_size = strtol(q2,&q2p,10); // Add multiple SignatureAlgorithm support
if(*q2p) {
logger_error("rsa size non numeric \"%s\"",q2);
ksrinvalid++;
free(q2);
break;
}
free(q2);
if((q2=gattr(p,"exponent=")) == NULL) {
logger_error("XML tag <%s> is missing attribute value for %s",
p,"exponent=");
ksrinvalid++;
break;
}
xp->rsa_exp = strtol(q2,&q2p,10); // Add multiple SignatureAlgorithm support
if(*q2p) {
logger_error("Exponent non numeric \"%s\"",q2);
ksrinvalid++;
free(q2);
break;
}
free(q2);
}
}
if(xmlparse(&lbuf[1],xs) < 0) return -1;
} else { /* done with this tag - process data and pop-stack */
char *p;
xmlprocess:
p = &lbuf[2];
if(strncasecmp(p,tp,strlen(p)) && otag == 0) {
logger_error("Keytag mismatch </%s> != <%s>",p,tp);
ksrinvalid++;
break; /* if not equal - problem */
}
/*myx_syslog(LOG_INFO,"tag=|%s| data=%s\n",tp,data);*/
if(strcasecmp(p,"RequestBundle") == 0) {
rq->incmin = rq->Inception;
rq->expmax = rq->Expiration;
if(xs->rqrscnt < MAX_BUNDLES) {
xs->rqrs[xs->rqrscnt++] = rq;
} else {
ksrinvalid++;
logger_error("Exceeded maximum request bundles");
}
rq = NULL;
} else if(strcasecmp(p,"ResponseBundle") == 0) {
rq->incmin = rq->Inception;
rq->expmax = rq->Expiration;
if(xs->rqrscnt < MAX_BUNDLES) {
xs->rqrs[xs->rqrscnt++] = rq;
} else {
ksrinvalid++;
logger_error("Exceeded maximum response bundles");
}
rq = NULL;
} else if(strcasecmp(p,"Request") == 0) {
} else if(strcasecmp(p,"PublishSafety") == 0) {
if(rppolicy) rppolicy->PublishSafety = strdup(data);
} else if(strcasecmp(p,"RetireSafety") == 0) {
if(rppolicy) rppolicy->RetireSafety = strdup(data);
} else if(strcasecmp(p,"MaxSignatureValidity") == 0) {
if(rppolicy) rppolicy->MaxSignatureValidity = strdup(data);
} else if(strcasecmp(p,"MinSignatureValidity") == 0) {
if(rppolicy) rppolicy->MinSignatureValidity = strdup(data);
} else if(strcasecmp(p,"MaxValidityOverlap") == 0) {
if(rppolicy) rppolicy->MaxValidityOverlap = strdup(data);
} else if(strcasecmp(p,"MinValidityOverlap") == 0) {
if(rppolicy) rppolicy->MinValidityOverlap = strdup(data);
} else if(strcasecmp(p,"Signer") == 0) {
sgr->next = rq->x_sgr;
rq->x_sgr = sgr;
sgr = NULL;
} else if(strcasecmp(p,"SignatureData") == 0) {
sig->SignatureData = strdup(data);
} else if(strcasecmp(p,"Signature") == 0) {
sig->next = rq->x_sig;
rq->x_sig = sig;
sig = NULL;
} else if(strcasecmp(p,"Key") == 0) {
key->next = rq->x_key;
rq->x_key = key;
key = NULL;
} else if(strcasecmp(p,"keyTag") == 0) {
sig->KeyTag = strtol(data,&q2p,10);
if(*q2p) {
logger_error("KeyTag non numeric \"%s\"",data);
ksrinvalid++;
}
} else if(strcasecmp(p,"Inception") == 0) {
rq->Inception = ztime2sec(data);
} else if(strcasecmp(p,"Expiration") == 0) {
rq->Expiration = ztime2sec(data);
} else if(strcasecmp(p,"SignersName") == 0) {
sig->SignersName = strdup(data);
} else if(strcasecmp(p,"Algorithm") == 0) {
if(sig) {
sig->Algorithm = strtol(data,&q2p,10);
if(*q2p) {
logger_error("KeyTag non numeric \"%s\"",data);
ksrinvalid++;
}
} else if(key) {
key->Algorithm = strtol(data,&q2p,10);
if(*q2p) {
logger_error("Algorithm non numeric \"%s\"",data);
ksrinvalid++;
}
} else { }
} else if(strcasecmp(p,"TTL") == 0) {
if(sig) sig->TTL = atoul(data);
else if(key) key->TTL = atoul(data);
else { }
} else if(strcasecmp(p,"Flags") == 0) {
key->Flags = strtol(data,&q2p,10);
if(*q2p) {
logger_error("Flags non numeric \"%s\"",data);
ksrinvalid++;
}
} else if(strcasecmp(p,"Protocol") == 0) {
key->Protocol = strtol(data,&q2p,10);
if(*q2p) {
logger_error("Protocol non numeric \"%s\"",data);
ksrinvalid++;
}
} else if(strcasecmp(p,"Labels") == 0) {
sig->Labels = strtol(data,&q2p,10);
if(*q2p) {
logger_error("Labels non numeric \"%s\"",data);
ksrinvalid++;
}
} else if(strcasecmp(p,"OriginalTTL") == 0) {
sig->OriginalTTL = strtol(data,&q2p,10);
if(*q2p) {
logger_error("TTL non numeric \"%s\"",data);
ksrinvalid++;
}
} else if(strcasecmp(p,"SignatureInception") == 0) {
sig->SignatureInception = ztime2sec(data);
} else if(strcasecmp(p,"SignatureExpiration") == 0) {
sig->SignatureExpiration = ztime2sec(data);
} else if(strcasecmp(p,"TypeCovered") == 0) {
sig->TypeCovered = strdup(data);
} else if(strcasecmp(p,"PublicKey") == 0) {
key->PublicKey = strdup(data);
} else if(strcasecmp(p,"KSR") == 0) {
if(debug) logger_debug("KSR end serial=\"%s\" id=\"%s\" domain=\"%s\"",xs->ksrserial,xs->ksrid,xs->ksrdomain);
} else {
}
break;
}
} else if(i == '<' && sttg == 0) {
sttg = 1;
if(k > 1) {
int j;
j = k; /*min(k,DBUFSIZE);*/
lbuf[j-1] = '\0';
memcpy(data,lbuf,j);
k = 0;
lbuf[k++] = i;
if(lbuf[1] == '?') otag = 1; else otag = 0;
}
continue;
}
}
xs->depth--;
return 0;
}
/*! parse, copy into new buffer, and return attributes in an XML tag
\param tp the full XML tag
\param str attribute label to search for inside tag
\return NULL on error or ptr to new buffer with attribute if found
*/
static char *gattr(char *tp,char *str)
{
char *q,*q2;
int quote;
if((q=strstr(tp,str))) {
q += strlen(str);
if(*q == '\0') return "";
if(*q == '"') quote = 1; else quote = 0;
if(quote) q++;
q = q2 = strdup(q);
while(1) {
if( (quote == 1 && *q == '"' && *(q-1) != '\\')
|| (quote == 0 && *q == ' ')
|| *q == '>'
|| *q == '\0' ) {
*q = '\0';
return q2;
}
q++;
}
}
logger_error("parsing \"%s\" attributes",tp);
return NULL;
}
/*! display series of internal request/response structure
\param rqrs array of pointers to internal request/response structures
\param cnt number of structures to display
*/
void display_reqresp(reqresp *rqrs[],int cnt)
{
char lbuf[MAXPATHLEN];
int i,jj;
reqresp *rq;
krecord *y;
logger_info("# Inception Expiration ZSK Tags KSK Tag(CKA_LABEL)");
for(i=0;i<cnt;i++) {
int cX;
rq = rqrs[i];
sec2ztime(rq->incmin,lbuf);
lbuf[19] = '\0';
myx_syslog(LOG_INFO,"%-2d %-15s",i+1,lbuf);
sec2ztime(rq->expmax,lbuf);
lbuf[19] = '\0';
myx_syslog(LOG_INFO," %-15s",lbuf);
lbuf[0] = '\0';
cX = sizeof(lbuf);
for(y=rq->x_key;y;y=y->next) {
if((y->Flags & DNSKEY_SEP_FLAG)) continue;
jj = strlen(lbuf);
if(jj) lbuf[jj++] = ',';
cX -= snprintf(&lbuf[jj],cX,"%05u",y->keyTag);
}
myx_syslog(LOG_INFO," %-12s",lbuf);
lbuf[0] = '\0';
cX = sizeof(lbuf);
#ifdef HSK_KEYS
for(j=0;j<nksk;j++) {
if((ksks[j]->Flags & DNSKEY_SEP_FLAG) == 0) continue;
jj = strlen(lbuf);
if(jj) lbuf[jj++] = ',';
cX -= snprintf(&lbuf[jj],cX,"%05u(%s)",ksks[j]->keyTag,ksks[j]->label->p0);
}
#else
for(y=rq->x_key;y;y=y->next) {
if((y->Flags & DNSKEY_SEP_FLAG) == 0) continue;
jj = strlen(lbuf);
if(jj) lbuf[jj++] = ',';
char kuse[5];
signature *s;
kuse[0] = '\0';
if( (y->Flags & DNSKEY_REVOKE_FLAG) ) strcat(kuse,"R");
for(s=rq->x_sig;s;s=s->next) if(strcmp(s->keyIdentifier,y->keyIdentifier) == 0) break;
if(s) strcat(kuse,"S"); else strcat(kuse,"P"); // signed or pub only
cX -= snprintf(&lbuf[jj],cX,"%05u(%s)/%s",y->keyTag,y->keyIdentifier,kuse); // note: Revoke flag changes tag value
}
#endif
myx_syslog(LOG_INFO," %-12s",lbuf);
myx_syslog(LOG_INFO,"\n");
}
}
#if 0
/*! dump detailed contents of request/response structure
\param rq pointer to internal request/response structure
*/
static void dump_requestresponse(reqresp *rq)
{
{
signer *s;
for(s=rq->x_sgr;s;s=s->next)
myx_syslog(LOG_INFO," Request KSK |%s|\n",s->keyIdentifier);
}
{
krecord *y;
for(y=rq->x_key;y;y=y->next)
myx_syslog(LOG_INFO," Key |%s| %05u\n",y->keyIdentifier,y->keyTag);
}
{
signature *s;
for(s=rq->x_sig;s;s=s->next)
myx_syslog(LOG_INFO," Sig |%s| %05u\n",s->keyIdentifier,s->KeyTag);
}
}
#endif /* 0 */
/*! Free internal request/response structure and contents
\param rq pointer to parsed internal structure
*/
void free_requestresponse(reqresp *rq)
{
{
signer *s,*sn;
for(s=rq->x_sgr;s;) {
sn = s->next;
free_signer(s);
s = sn;
}
}
{
krecord *s,*sn;
for(s=rq->x_key;s;) {
sn = s->next;
free_keyrecord(s);
s = sn;
}
}
{
signature *s,*sn;
for(s=rq->x_sig;s;) {
sn = s->next;
free_signature(s);
s = sn;
}
}
if(rq->id) free(rq->id);
free(rq);
}
/*! Free internal key record structure and contents
\param s internal key record structure
*/
void free_keyrecord(krecord *s)
{
if(s == NULL) return;
if(s->keyIdentifier) free(s->keyIdentifier);
if(s->PublicKey) free(s->PublicKey);
if(s->user) free(s->user);
if(s->modulus) mbuf_free(s->modulus);
if(s->pubexp) mbuf_free(s->pubexp);
if(s->label) mbuf_free(s->label);
if(s->pkcb) pkcs11_free_pkkeycb(s->pkcb);
free(s);
}
/*! Free internal signature structure and contents
\param s internal signature structure
*/
static void free_signature(signature *s)
{
if(s->keyIdentifier) free(s->keyIdentifier);
if(s->TypeCovered) free(s->TypeCovered);
if(s->SignersName) free(s->SignersName);
if(s->SignatureData) free(s->SignatureData);
free(s);
}
/*! Free internal signer structure and contents
\param s internal signer structure
*/
static void free_signer(signer *s)
{
if(s->keyIdentifier) free(s->keyIdentifier);
free(s);
}
/*! Free internal response policy structure and contents
\param s internal response policy structure
*/
static void free_responsepolicy(responsepolicy *r)
{
if(r->PublishSafety) free(r->PublishSafety);
if(r->RetireSafety) free(r->RetireSafety);
if(r->MaxSignatureValidity) free(r->MaxSignatureValidity);
if(r->MinSignatureValidity) free(r->MinSignatureValidity);
if(r->MaxValidityOverlap) free(r->MaxValidityOverlap);
if(r->MinValidityOverlap) free(r->MinValidityOverlap);
if(r->sigalg) {
sigalg *xp,*xpn;
for(xp=r->sigalg;xp;) { // Add multiple SignatureAlgorithm processing
xpn = xp->next;
free(xp);
xp = xpn;
}
}
free(r);
}
/*! Return (XML-format-days) - (days). Support for testing RequestPolicy
\param xst pointer to XML formated days. e.g. PxD x days
\param days
\return (XML-format-days) - (days)
*/
static int xmltimecmp(char *xst,int days)
{
int dout;
if(xst == NULL) return 0; // accept if not specified since this does not effect DNSKEY RRset
dout = 0;
sscanf(xst,"P%dD",&dout);
if(debug) logger_info("%s: %d - %d",__func__,dout,days);
return dout - days;
}
/*! Check RequestPolicy and Other sections of KSR. Note:Has no effect on resultant DNSKEY RRSet
\param rq pointer to parsed request
\return 0 but global variable ksrinvalid is incremented for each error
*/
static int check_requestpolicy(reqresp *rq)
{
static reqresp *last_rq=NULL;
if(last_rq) { // Test RRSIG overlap of RequestBundles (Other)
// On subsequent passes check T_MIN/MAX_VALIDITY_OVERLAP - MUST BE SEQUENTIAL RequestBundles
time_t li;
if(rq->Inception > last_rq->Expiration) {
logger_error("Signatures do not overlap");
ksrinvalid++;
}
li = (1 + last_rq->Expiration - rq->Inception)/T_ONEDAY;
if(debug) logger_info("Overlap = %d",li);
if(li < T_MIN_VALIDITY_OVERLAP || li > T_MAX_VALIDITY_OVERLAP) {
logger_error("Overlap period out of policy %d days",li);
ksrinvalid++;
}
}
last_rq = rq; // first pass so set last_rq for subsequent sequential overlap comparison
// Check stated VRSN ZSK RequestPolicy to see if they match specs.
// Not used in results (DNSKEY RRsets) except to check VRSN's own work.
// We enforce actual param limits on keys+sigs in later section.
if(xmltimecmp(rppolicy->PublishSafety,T_PUBLISH_SAFETY) < 0) {
logger_error("ZSK POLICY: Bad PublishSafety (%s)",rppolicy->PublishSafety);
ksrinvalid++;
}
if(xmltimecmp(rppolicy->RetireSafety,T_RETIRE_SAFETY) > 0) {
logger_error("ZSK POLICY: Bad RetireSafety (%s)",rppolicy->RetireSafety);
ksrinvalid++;
}
// ICANN controls this since we are the signer - see original code later
// if(li > (T_MAX_SIG_VAL*T_ONEDAY) || (li+1) < (T_MIN_SIG_VAL*T_ONEDAY)) 15-20 days
if(xmltimecmp(rppolicy->MaxSignatureValidity,T_MAX_SIG_VAL)) { // match
logger_error("ZSK POLICY: Bad MaxSignatureValidity (%s)",rppolicy->MaxSignatureValidity);
ksrinvalid++;
}
if(xmltimecmp(rppolicy->MinSignatureValidity,T_MIN_SIG_VAL)) { // match
logger_error("ZSK POLICY: Bad MinSignatureValidity (%s)",rppolicy->MinSignatureValidity);
ksrinvalid++;
}
if(xmltimecmp(rppolicy->MaxValidityOverlap,T_MAX_VALIDITY_OVERLAP) > 0) {
logger_error("ZSK POLICY: Bad MaxValidityOverlap (%s)",rppolicy->MaxValidityOverlap);
ksrinvalid++;
}
if(xmltimecmp(rppolicy->MinValidityOverlap,T_MIN_VALIDITY_OVERLAP) < 0) {
logger_error("ZSK POLICY: Bad MinValidityOverlap (%s)",rppolicy->MinValidityOverlap);
ksrinvalid++;
}
// Test ZSK Key parameters (Other) against VRSN stated policies (RequestPolicy)
{
sigalg *xp;
uint32_t rsa_exp;
int n;
uint8_t *p;
krecord *y;
for(y=rq->x_key;y;y=y->next) {
p = y->pubexp->p0;
n = (int)(y->pubexp->pc - p);
rsa_exp = 0;
if(n > 4) {
logger_error("Unsupported exponent length (%d)",n);
ksrinvalid++;
} else {
for(;n>0;n--) rsa_exp = (rsa_exp<<8)|*p++;
}
if(debug) logger_info("alg:%d size:%d exp:%d",y->Algorithm,y->bits,rsa_exp);
for(xp=rppolicy->sigalg;xp;xp=xp->next) { // walk RequestPolicy SignatureAlgorithm and compare
if(debug) logger_info(" alg=%d size=%d exp=%d",xp->algorithm,xp->rsa_size,xp->rsa_exp);
if(y->Algorithm == xp->algorithm && y->bits == xp->rsa_size
&& rsa_exp == (uint32_t)xp->rsa_exp) break;
}
if(xp == NULL) { // no matches
logger_error("ZSK does not match any xml ZSK SignatureAlgorithm");
ksrinvalid++;
}
// no effect on DNSKEY RRset result but check VRSN xml "keytag" match while at it
if(y->keyTag != updatekeytag(y)) {
logger_error("ZSK xml keytag and actual keytag do not match (%d != %d)",
y->keyTag,updatekeytag(y));
ksrinvalid++;
}
}
}
return 0;
}
/*! Check if request is valid (e.g., algorithms, protocols, validity period,
proof of private key ownership, etc..)
\param rq pointer to parsed request
\param ksrdomain domain name associated with this request
\return 0 but global variable ksrinvalid is incremented for each error
*/
int check_requestbundle(reqresp *rq,char *ksrdomain)
{
int keycnt;
if(debug) {
signer *s;
logger_info("RequestBundle id=%s",rq->id);
for(s=rq->x_sgr;s;s=s->next)
logger_info(" Request KSK |%s|",s->keyIdentifier);
}
{
krecord *y;
keycnt = 0;
for(y=rq->x_key;y;y=y->next) {
if(algtohash(y->Algorithm) < 0) {
logger_error("Unsupported key algorithm (%d)",y->Algorithm);
ksrinvalid++;
}
if((y->Flags & DNSKEY_SEP_FLAG)) {
logger_error("SEP bit set on ZSK flag=%d",y->Flags);
ksrinvalid++;
}
if(y->Protocol != DNSKEY_PROTOCOL_DNSSEC) {
logger_error("Unsupported protocol (%d) for ZSK",y->Protocol);
ksrinvalid++;
}
if(debug) logger_debug("Key |%s| %05u",y->keyIdentifier,y->keyTag);
fillinpinfo(y);
keycnt++;
}
if(keycnt <= 0) {
logger_error("No keys found in KSR");
ksrinvalid++;
}
if(keycnt >= MAX_KEYS) {
logger_error("Number of keys exceeded %d >= %d",keycnt,MAX_KEYS);
ksrinvalid++;
}
}
{
signature *s;
keycnt = 0;
for(s=rq->x_sig;s;s=s->next) {
if(algtohash(s->Algorithm) < 0) {
logger_error("Unsupported signature algorithm (%d)",s->Algorithm);
ksrinvalid++;
}
if(strcmp(s->TypeCovered,"DNSKEY")) {
logger_error("Unsupported Type Covered %s by signature",s->TypeCovered);
ksrinvalid++;
}
if(strcmp(s->SignersName,ksrdomain)) {
logger_error("SignersName (%s) != domain",s->SignersName);
ksrinvalid++;
}
if(debug) logger_debug("Sig |%s| %05u",s->keyIdentifier,s->KeyTag);
keycnt++;
}
if(keycnt <= 0) {
logger_error("No signatures found in KSR");
ksrinvalid++;
}
}
if(rq->Expiration > maxexpiration) {
/* Changed from error to warning in DVD r600 to provide flexibility to personnel scheduling.
Previously limited rq->Expiration to maxexpiration and incremented ksrinvalid. */
logger_warning("*** Requests signature expiration exceeds limit of %d days! ***",(T_VLIMIT+1));
}
if(rq->Inception >= rq->Expiration) {
char t0buf[30],t1buf[30];
sec2ztime(rq->Inception,t0buf);
sec2ztime(rq->Expiration,t1buf);
logger_error("Inception and Expiration times (%s - %s) do not allow for "
"key bundle generation.", t0buf, t1buf);
ksrinvalid++;
}
#ifdef WEDOESLOTS /* we calculate periods for key bundle slots */
/* we will automatically enforce */
#else /* The KSR tells us the slots periods - check here */
{
time_t li;
li = rq->Expiration - rq->Inception;
if(li > (T_MAX_SIG_VAL*T_ONEDAY) || (li+1) < (T_MIN_SIG_VAL*T_ONEDAY)) {
logger_error("Requested validity period (%u sec) out of policy",li);
ksrinvalid++;
}
}
#endif
check_requestpolicy(rq); // Add check of RequestPolicy. Note: This has no effect on resultant DNSKEY RRset
if(ksrinvalid) goto enderror;
/*
* Verify KSR[n] ZSK RRSIGs - Proof of Possesion
*/
keycnt = 0;
{
signature *s;
for(s=rq->x_sig;s;s=s->next) {
if(validatekeybundle(s,rq->x_key)) {
logger_error("Could not verify key with identifier %s",
s->keyIdentifier);
ksrinvalid++;
goto enderror; /* if any error - exit */
}
if(debug) logger_info("Verified private key ownership for %05u",s->KeyTag);
keycnt++;
}
}
if(keycnt == 0) {
logger_error("No valid keys in key bundle");
ksrinvalid++;
goto enderror;
}
/*
* Check other parameters and policy and signature lifetimes
* ===== most done prior to here ====
*/
enderror:
if(debug) logger_info("=====");
return 0;
}
/*! For REVOKE bit only, clearly should just backout and reinsert
the REVOKE bit but recalculating was easier.
\param kr key record to recalculate keytag over
\return 16-bit keytag
*/
static uint16_t updatekeytag(krecord *kr)
{
uint8_t *q,lbuf[4098];
int n;
q = lbuf;
*(uint16_t *)q = htons(kr->Flags);
q += 2;
*(uint8_t *)q = (uint8_t)kr->Protocol;
q++;
*(uint8_t *)q = (uint8_t)kr->Algorithm;
q++;
n = (int)(kr->pubexp->pc - kr->pubexp->p0);
*q++ = (uint8_t)n;
memcpy(q,kr->pubexp->p0,n);
q += n;
n = (int)(kr->modulus->pc - kr->modulus->p0);
memcpy(q,kr->modulus->p0,n);
q += n;
return dnssec_keytag(lbuf,(int)(q-lbuf));
}
/*! Sign the request
\param xs struct containing parsed validated request and signing key data
\param ftmp file pointer to write signed response in XML format to.
\return -1 if error; 0 if ok
*/
int signem(FILE *ftmp,xmlstate *xs)
{
krecord *keys[MAX_KEYS];
int keycnt,i,ir,ret;
reqresp *rq,**reqs;
int reqscnt;
ret = -1;
if(debug) logger_debug("Signem");
reqs = xs->rqrs;
reqscnt = xs->rqrscnt;
fprintf(ftmp,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(ftmp,"<KSR id=\"%s\" domain=\"%s\" serial=\"%s\">\n",xs->ksrid,xs->ksrdomain,xs->ksrserial);
fprintf(ftmp,"<Response>\n");
fprintf(ftmp,"<ResponsePolicy>\n");
fprintf(ftmp,"<KSK>\n");
fprintf(ftmp,"<PublishSafety>PT%dS</PublishSafety>\n", T_PUBLISH_SAFETY);
fprintf(ftmp,"<RetireSafety>P%uD</RetireSafety>\n",T_RETIRE_SAFETY);
fprintf(ftmp,"<MaxSignatureValidity>P%uD</MaxSignatureValidity>\n",T_MAX_SIG_VAL);
fprintf(ftmp,"<MinSignatureValidity>P%uD</MinSignatureValidity>\n",T_MIN_SIG_VAL);
fprintf(ftmp,"<MaxValidityOverlap>P%uD</MaxValidityOverlap>\n",T_MAX_VALIDITY_OVERLAP);
fprintf(ftmp,"<MinValidityOverlap>P%uD</MinValidityOverlap>\n",T_MIN_VALIDITY_OVERLAP);
fprintf(ftmp,"<SignatureAlgorithm algorithm=\"%d\">\n",KSK_RRSIG_ALG);
fprintf(ftmp,"<RSA size=\"%d\" exponent=\"%d\"/>\n", KSK_RRSIG_RSA_KEYSIZE, KSK_RRSIG_RSA_EXPONENT);
fprintf(ftmp,"</SignatureAlgorithm>\n");
fprintf(ftmp,"</KSK>\n");
if(rppolicy) {
fprintf(ftmp,"<ZSK>\n");
if(rppolicy->PublishSafety) fprintf(ftmp,"<PublishSafety>%s</PublishSafety>\n",rppolicy->PublishSafety);
if(rppolicy->RetireSafety) fprintf(ftmp,"<RetireSafety>%s</RetireSafety>\n",rppolicy->RetireSafety);
if(rppolicy->MaxSignatureValidity) fprintf(ftmp,"<MaxSignatureValidity>%s</MaxSignatureValidity>\n",rppolicy->MaxSignatureValidity);
if(rppolicy->MinSignatureValidity) fprintf(ftmp,"<MinSignatureValidity>%s</MinSignatureValidity>\n",rppolicy->MinSignatureValidity);
if(rppolicy->MaxValidityOverlap) fprintf(ftmp,"<MaxValidityOverlap>%s</MaxValidityOverlap>\n",rppolicy->MaxValidityOverlap);
if(rppolicy->MinValidityOverlap) fprintf(ftmp,"<MinValidityOverlap>%s</MinValidityOverlap>\n",rppolicy->MinValidityOverlap);