-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathx509_helpers.c
More file actions
1416 lines (1240 loc) · 48.4 KB
/
x509_helpers.c
File metadata and controls
1416 lines (1240 loc) · 48.4 KB
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
/*
* XML Security Library (http://www.aleksey.com/xmlsec).
*
* X509 helper functions.
*
* This is free software; see the Copyright file in the source
* distribution for precise wording.
*
* Copyright (C) 2002-2024 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
*/
#include "globals.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <ctype.h>
#include <xmlsec/xmlsec.h>
#include <xmlsec/base64.h>
#include <xmlsec/errors.h>
#include <xmlsec/private.h>
#include <xmlsec/xmltree.h>
#include "x509_helpers.h"
#ifndef XMLSEC_NO_X509
#define XMLSEC_X509_NAME_SIZE 256
#define XMLSEC_X509_VALUE_SIZE 1024
#define XMLSEC_X509_NAME_READ_STATE_NORMAL 0
#define XMLSEC_X509_NAME_READ_STATE_AFTER_SLASH1 1
#define XMLSEC_X509_NAME_READ_STATE_AFTER_SLASH2 2
#define XMLSEC_X509_NAME_READ_STATE_DELIMETER 3
/**************************************************************************
*
* Helper functions to read/write <dsig:X509Data/>
*
*
* The X509Data Element (http://www.w3.org/TR/xmldsig-core/#sec-X509Data)
*
* An X509Data element within KeyInfo contains one or more identifiers of keys
* or X509 certificates (or certificates' identifiers or a revocation list).
* The content of X509Data is:
*
* 1. At least one element, from the following set of element types; any of these may appear together or more than once iff (if and only if) each instance describes or is related to the same certificate:
* 2.
* * The X509IssuerSerial element, which contains an X.509 issuer
* distinguished name/serial number pair that SHOULD be compliant
* with RFC2253 [LDAP-DN],
* * The X509SubjectName element, which contains an X.509 subject
* distinguished name that SHOULD be compliant with RFC2253 [LDAP-DN],
* * The X509SKI element, which contains the base64 encoded plain (i.e.
* non-DER-encoded) value of a X509 V.3 SubjectKeyIdentifier extension.
* * The X509Certificate element, which contains a base64-encoded [X509v3]
* certificate, and
* * Elements from an external namespace which accompanies/complements any
* of the elements above.
* * The X509CRL element, which contains a base64-encoded certificate
* revocation list (CRL) [X509v3].
*
* Any X509IssuerSerial, X509SKI, and X509SubjectName elements that appear
* MUST refer to the certificate or certificates containing the validation key.
* All such elements that refer to a particular individual certificate MUST be
* grouped inside a single X509Data element and if the certificate to which
* they refer appears, it MUST also be in that X509Data element.
*
* Any X509IssuerSerial, X509SKI, and X509SubjectName elements that relate to
* the same key but different certificates MUST be grouped within a single
* KeyInfo but MAY occur in multiple X509Data elements.
*
* All certificates appearing in an X509Data element MUST relate to the
* validation key by either containing it or being part of a certification
* chain that terminates in a certificate containing the validation key.
*
* No ordering is implied by the above constraints.
*
* Note, there is no direct provision for a PKCS#7 encoded "bag" of
* certificates or CRLs. However, a set of certificates and CRLs can occur
* within an X509Data element and multiple X509Data elements can occur in a
* KeyInfo. Whenever multiple certificates occur in an X509Data element, at
* least one such certificate must contain the public key which verifies the
* signature.
*
* <programlisting><![CDATA[
* Schema Definition:
*
* <element name="X509Data" type="ds:X509DataType"/>
* <complexType name="X509DataType">
* <sequence maxOccurs="unbounded">
* <choice>
* <element name="X509IssuerSerial" type="ds:X509IssuerSerialType"/>
* <element name="X509SKI" type="base64Binary"/>
* <element name="X509SubjectName" type="string"/>
* <element name="X509Certificate" type="base64Binary"/>
* <element name="X509CRL" type="base64Binary"/>
* <any namespace="##other" processContents="lax"/>
* </choice>
* </sequence>
* </complexType>
* <complexType name="X509IssuerSerialType">
* <sequence>
* <element name="X509IssuerName" type="string"/>
* <element name="X509SerialNumber" type="integer"/>
* </sequence>
* </complexType>
*
* DTD:
*
* <!ELEMENT X509Data ((X509IssuerSerial | X509SKI | X509SubjectName |
* X509Certificate | X509CRL)+ %X509.ANY;)>
* <!ELEMENT X509IssuerSerial (X509IssuerName, X509SerialNumber) >
* <!ELEMENT X509IssuerName (#PCDATA) >
* <!ELEMENT X509SubjectName (#PCDATA) >
* <!ELEMENT X509SerialNumber (#PCDATA) >
* <!ELEMENT X509SKI (#PCDATA) >
* <!ELEMENT X509Certificate (#PCDATA) >
* <!ELEMENT X509CRL (#PCDATA) >
* ]]></programlisting>
*
*************************************************************************/
#define XMLSEC_KEY_DATA_X509_INIT_BUF_SIZE 512
static int xmlSecKeyX509DataValueInitialize (xmlSecKeyX509DataValuePtr x509Value);
static void xmlSecKeyX509DataValueFinalize (xmlSecKeyX509DataValuePtr x509Value);
static void xmlSecKeyX509DataValueReset (xmlSecKeyX509DataValuePtr x509Value,
int writeMode);
static int xmlSecKeyX509DataValueXmlRead (xmlSecKeyX509DataValuePtr x509Value,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecKeyX509DataValueXmlWrite (xmlSecKeyX509DataValuePtr x509Value,
xmlNodePtr node,
int base64LineSize,
int addLineBreaks);
/**
* xmlSecKeyDataX509XmlRead:
* @key: the resulting key
* @data: the X509 key data.
* @node: the pointer to data's value XML node.
* @keyInfoCtx: the <dsig:KeyInfo/> node processing context.
* @readFunc: the pointer to the function that converts
* @xmlSecKeyX509DataValue to @xmlSecKeyData.
*
* X509 Key data method for reading XML node.
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecKeyDataX509XmlRead(xmlSecKeyPtr key, xmlSecKeyDataPtr data, xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx, xmlSecKeyDataX509Read readFunc
) {
xmlSecKeyX509DataValue x509Value;
int x509ValueInitialized = 0;
xmlNodePtr cur;
int keyFound = 0;
int res = -1;
int ret;
xmlSecAssert2(data != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
xmlSecAssert2(keyInfoCtx->keysMngr != NULL, -1);
ret = xmlSecKeyX509DataValueInitialize(&x509Value);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueInitialize", NULL);
goto done;
}
x509ValueInitialized = 1;
for(cur = xmlSecGetNextElementNode(node->children); cur != NULL; cur = xmlSecGetNextElementNode(cur->next)) {
ret = xmlSecKeyX509DataValueXmlRead(&x509Value, cur, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlRead", NULL);
goto done;
}
/* first try to lookup key in keys manager using x509 data */
if(keyFound == 0) {
xmlSecKeyPtr tmpKey;
tmpKey = xmlSecKeysMngrFindKeyFromX509Data(keyInfoCtx->keysMngr, &x509Value, keyInfoCtx);
if(tmpKey != NULL) {
ret = xmlSecKeySwap(key, tmpKey);
if(ret < 0) {
xmlSecInternalError("xmlSecKeysMngrFindKeyFromX509Data", NULL);
xmlSecKeyDestroy(tmpKey);
goto done;
}
xmlSecKeyDestroy(tmpKey);
/* key was found but we want to keep reading X509Data node to ensure it is valid */
keyFound = 1;
}
}
/* otherwise, see if we can get it from certs, etc */
if((keyFound == 0) && (readFunc != NULL)) {
/* xmlSecKeyDataX509Read: 0 on success and a negative value otherwise */
ret = readFunc(data, &x509Value, keyInfoCtx->keysMngr, keyInfoCtx->flags);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyDataX509Read", NULL);
goto done;
}
}
/* cleanup for the next node */
xmlSecKeyX509DataValueReset(&x509Value, 0);
}
/* success */
res = 0;
done:
/* cleanup */
if(x509ValueInitialized != 0) {
xmlSecKeyX509DataValueFinalize(&x509Value);
}
return(res);
}
static int
xmlSecX509DataGetNodeContent(xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx, xmlChar** digestAlgorithm) {
xmlNodePtr cur;
int content = 0;
xmlSecAssert2(node != NULL, 0);
xmlSecAssert2(keyInfoCtx != NULL, -1);
xmlSecAssert2(digestAlgorithm != NULL, -1);
xmlSecAssert2((*digestAlgorithm) == NULL, -1);
/* determine the current node content */
cur = xmlSecGetNextElementNode(node->children);
while(cur != NULL) {
if(xmlSecCheckNodeName(cur, xmlSecNodeX509Certificate, xmlSecDSigNs)) {
if(xmlSecIsEmptyNode(cur) == 1) {
content |= XMLSEC_X509DATA_CERTIFICATE_NODE;
} else {
/* ensure return value isn't 0 if there are non-empty elements */
content |= (XMLSEC_X509DATA_CERTIFICATE_NODE << XMLSEC_X509DATA_SHIFT_IF_NOT_EMPTY);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509SubjectName, xmlSecDSigNs)) {
if(xmlSecIsEmptyNode(cur) == 1) {
content |= XMLSEC_X509DATA_SUBJECTNAME_NODE;
} else {
content |= (XMLSEC_X509DATA_SUBJECTNAME_NODE << XMLSEC_X509DATA_SHIFT_IF_NOT_EMPTY);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509IssuerSerial, xmlSecDSigNs)) {
if(xmlSecIsEmptyNode(cur) == 1) {
content |= XMLSEC_X509DATA_ISSUERSERIAL_NODE;
} else {
content |= (XMLSEC_X509DATA_ISSUERSERIAL_NODE << XMLSEC_X509DATA_SHIFT_IF_NOT_EMPTY);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509SKI, xmlSecDSigNs)) {
if(xmlSecIsEmptyNode(cur) == 1) {
content |= XMLSEC_X509DATA_SKI_NODE;
} else {
content |= (XMLSEC_X509DATA_SKI_NODE << XMLSEC_X509DATA_SHIFT_IF_NOT_EMPTY);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509Digest, xmlSecDSig11Ns)) {
if(xmlSecIsEmptyNode(cur) == 1) {
content |= XMLSEC_X509DATA_DIGEST_NODE;
} else {
content |= (XMLSEC_X509DATA_DIGEST_NODE << XMLSEC_X509DATA_SHIFT_IF_NOT_EMPTY);
}
/* only read the first digestAlgorithm */
if((*digestAlgorithm) == NULL) {
(*digestAlgorithm) = xmlGetProp(cur, xmlSecAttrAlgorithm);
if((*digestAlgorithm) == NULL) {
xmlSecInvalidNodeAttributeError(cur, xmlSecAttrAlgorithm, NULL, "empty");
return(-1);
}
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509CRL, xmlSecDSigNs)) {
if(xmlSecIsEmptyNode(cur) == 1) {
content |= XMLSEC_X509DATA_CRL_NODE;
} else {
content |= (XMLSEC_X509DATA_CRL_NODE << 16);
}
} else {
/* todo: fail on unknown child node? */
}
cur = xmlSecGetNextElementNode(cur->next);
}
return (content);
}
/**
* xmlSecKeyDataDsaXmlWrite:
* @data: the x509 key data.
* @x509ObjNum: the number of X509 objects in @data.
* @node: the pointer to data's value XML node.
* @keyInfoCtx: the <dsig:KeyInfo/> node processing context.
* @base64LineSize: the base64 max line size.
* @addLineBreaks: the flag indicating if we need to add line breaks around base64 output.
* @writeFunc: the pointer to the function that converts
* @xmlSecKeyData to @xmlSecKeyValueDsa.
*
* DSA Key data method for writing XML node.
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecKeyDataX509XmlWrite(xmlSecKeyDataPtr data, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx,
int base64LineSize, int addLineBreaks,
xmlSecKeyDataX509Write writeFunc, void* writeFuncContext) {
xmlSecKeyX509DataValue x509Value;
int x509ValueInitialized = 0;
int content;
int ret;
int res = -1;
xmlSecAssert2(data != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
xmlSecAssert2(base64LineSize > 0, -1);
xmlSecAssert2(writeFunc != NULL, -1);
if(((xmlSecKeyDataTypePublic) & keyInfoCtx->keyReq.keyType) == 0) {
/* we can only write public key */
return(0);
}
ret = xmlSecKeyX509DataValueInitialize(&x509Value);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueInitialize",
xmlSecKeyDataGetName(data));
goto done;
}
x509ValueInitialized = 1;
content = xmlSecX509DataGetNodeContent(node, keyInfoCtx, &(x509Value.digestAlgorithm));
if (content < 0) {
xmlSecInternalError2("xmlSecX509DataGetNodeContent",
xmlSecKeyDataGetName(data), "content=%d", content);
goto done;
} else if(content == 0) {
/* by default we are writing certificates and crls */
content = XMLSEC_X509DATA_DEFAULT;
}
while(1) {
/* xmlSecKeyDataX509Write: returns 1 on success, 0 if no more certs/crls are available,
* or a negative value if an error occurs.
*/
ret = writeFunc(data, &x509Value, content, writeFuncContext);
if(ret < 0) {
xmlSecInternalError("writeFunc", xmlSecKeyDataGetName(data));
goto done;
} else if (ret == 0) {
break;
}
ret = xmlSecKeyX509DataValueXmlWrite(&x509Value, node, base64LineSize, addLineBreaks);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlWrite", xmlSecKeyDataGetName(data));
goto done;
}
/* cleanup for the next obj */
xmlSecKeyX509DataValueReset(&x509Value, 1);
}
/* success */
res = 0;
done:
/* cleanup */
if(x509ValueInitialized != 0) {
xmlSecKeyX509DataValueFinalize(&x509Value);
}
return(res);
}
static int
xmlSecKeyX509DataValueInitialize(xmlSecKeyX509DataValuePtr x509Value) {
int ret;
xmlSecAssert2(x509Value != NULL, -1);
memset(x509Value, 0, sizeof(xmlSecKeyX509DataValue));
ret = xmlSecBufferInitialize(&(x509Value->cert), XMLSEC_KEY_DATA_X509_INIT_BUF_SIZE);
if(ret < 0) {
xmlSecInternalError("xmlSecBufferInitialize(cert)", NULL);
xmlSecKeyX509DataValueFinalize(x509Value);
return(-1);
}
ret = xmlSecBufferInitialize(&(x509Value->crl), XMLSEC_KEY_DATA_X509_INIT_BUF_SIZE);
if(ret < 0) {
xmlSecInternalError("xmlSecBufferInitialize(crl)", NULL);
xmlSecKeyX509DataValueFinalize(x509Value);
return(-1);
}
ret = xmlSecBufferInitialize(&(x509Value->ski), XMLSEC_KEY_DATA_X509_INIT_BUF_SIZE);
if(ret < 0) {
xmlSecInternalError("xmlSecBufferInitialize(ski)", NULL);
xmlSecKeyX509DataValueFinalize(x509Value);
return(-1);
}
ret = xmlSecBufferInitialize(&(x509Value->digest), XMLSEC_KEY_DATA_X509_INIT_BUF_SIZE);
if(ret < 0) {
xmlSecInternalError("xmlSecBufferInitialize(digest)", NULL);
xmlSecKeyX509DataValueFinalize(x509Value);
return(-1);
}
return(0);
}
static void
xmlSecKeyX509DataValueFinalize(xmlSecKeyX509DataValuePtr x509Value) {
xmlSecAssert(x509Value != NULL);
xmlSecBufferFinalize(&(x509Value->cert));
xmlSecBufferFinalize(&(x509Value->crl));
xmlSecBufferFinalize(&(x509Value->ski));
if(x509Value->subject != NULL) {
xmlFree(x509Value->subject);
}
if(x509Value->issuerName != NULL) {
xmlFree(x509Value->issuerName);
}
if(x509Value->issuerSerial != NULL) {
xmlFree(x509Value->issuerSerial);
}
if(x509Value->digestAlgorithm != NULL) {
xmlFree(x509Value->digestAlgorithm);
}
xmlSecBufferFinalize(&(x509Value->digest));
memset(x509Value, 0, sizeof(xmlSecKeyX509DataValue));
}
static void
xmlSecKeyX509DataValueReset(xmlSecKeyX509DataValuePtr x509Value, int writeMode) {
xmlSecAssert(x509Value != NULL);
xmlSecBufferEmpty(&(x509Value->cert));
xmlSecBufferEmpty(&(x509Value->crl));
xmlSecBufferEmpty(&(x509Value->ski));
if(x509Value->subject != NULL) {
xmlFree(x509Value->subject);
x509Value->subject = NULL;
}
if(x509Value->issuerName != NULL) {
xmlFree(x509Value->issuerName);
x509Value->issuerName = NULL;
}
if(x509Value->issuerSerial != NULL) {
xmlFree(x509Value->issuerSerial);
x509Value->issuerSerial = NULL;
}
/* we keep digest algorithm as-is for the next certificate if we are writing it out */
if((writeMode == 0) && (x509Value->digestAlgorithm != NULL)) {
xmlFree(x509Value->digestAlgorithm);
x509Value->digestAlgorithm = NULL;
}
xmlSecBufferEmpty(&(x509Value->digest));
}
static int
xmlSecKeyX509DataValueXmlReadBase64Blob(xmlSecBufferPtr buf, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlChar *content;
xmlSecSize decodedSize;
int ret;
int res = -1;
xmlSecAssert2(buf != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
content = xmlSecGetNodeContentAndTrim(node);
if((content == NULL) || (xmlSecIsEmptyString(content) == 1)) {
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_STOP_ON_EMPTY_NODE) != 0) {
xmlSecInvalidNodeContentError(node, NULL, "empty");
goto done;
}
/* success */
res = 0;
goto done;
}
/* usual trick with base64 decoding "in-place" */
decodedSize = 0;
ret = xmlSecBase64DecodeInPlace(content, &decodedSize);
if(ret < 0) {
xmlSecInternalError2("xmlSecBase64DecodeInPlace", NULL,
"node=%s", xmlSecErrorsSafeString(xmlSecNodeGetName(node)));
goto done;
}
ret = xmlSecBufferSetData(buf, (xmlSecByte*)content, decodedSize);
if(ret < 0) {
xmlSecInternalError3("xmlSecBufferSetData", NULL,
"node=%s; size=" XMLSEC_SIZE_FMT,
xmlSecErrorsSafeString(xmlSecNodeGetName(node)),
decodedSize);
goto done;
}
/* success */
res = 0;
done:
/* cleanup */
if(content != NULL) {
xmlFree(content);
}
return(res);
}
static int
xmlSecKeyX509DataValueXmlReadString(xmlChar **str, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlChar *content;
int res = -1;
xmlSecAssert2(str != NULL, -1);
xmlSecAssert2((*str) == NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
content = xmlSecGetNodeContentAndTrim(node);
if((content == NULL) || (xmlStrlen(content) <= 0)) {
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_STOP_ON_EMPTY_NODE) != 0) {
xmlSecInvalidNodeContentError(node, NULL, "empty");
goto done;
}
/* success */
res = 0;
goto done;
}
/* success */
(*str) = content;
content = NULL;
res = 0;
done:
/* cleanup */
if(content != NULL) {
xmlFree(content);
}
return(res);
}
static int
xmlSecKeyX509DataValueXmlReadIssuerSerial(xmlSecKeyX509DataValuePtr x509Value, xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx
) {
xmlNodePtr cur;
xmlSecAssert2(x509Value != NULL, -1);
xmlSecAssert2(x509Value->issuerName == NULL, -1);
xmlSecAssert2(x509Value->issuerSerial == NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
cur = xmlSecGetNextElementNode(node->children);
if(cur == NULL) {
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_STOP_ON_EMPTY_NODE) != 0) {
xmlSecNodeNotFoundError("xmlSecGetNextElementNode", node, NULL, NULL);
return(-1);
}
return(0);
}
/* the first is required node X509IssuerName */
if(!xmlSecCheckNodeName(cur, xmlSecNodeX509IssuerName, xmlSecDSigNs)) {
xmlSecInvalidNodeError(cur, xmlSecNodeX509IssuerName, NULL);
return(-1);
}
x509Value->issuerName = xmlSecGetNodeContentAndTrim(cur);
if((x509Value->issuerName == NULL) || (xmlSecIsEmptyString(x509Value->issuerName) == 1)) {
xmlSecInvalidNodeContentError(cur, NULL, "empty");
return(-1);
}
cur = xmlSecGetNextElementNode(cur->next);
/* next is required node X509SerialNumber */
if((cur == NULL) || !xmlSecCheckNodeName(cur, xmlSecNodeX509SerialNumber, xmlSecDSigNs)) {
xmlSecInvalidNodeError(cur, xmlSecNodeX509SerialNumber, NULL);
return(-1);
}
x509Value->issuerSerial = xmlSecGetNodeContentAndTrim(cur);
if((x509Value->issuerSerial == NULL) || (xmlSecIsEmptyString(x509Value->issuerSerial) == 1)) {
xmlSecInvalidNodeContentError(cur, NULL, "empty");
return(-1);
}
cur = xmlSecGetNextElementNode(cur->next);
/* nothing else is expected */
if(cur != NULL) {
xmlSecUnexpectedNodeError(cur, NULL);
return(-1);
}
/* success */
return(0);
}
static int
xmlSecKeyX509DataValueXmlRead(xmlSecKeyX509DataValuePtr x509Value, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
int ret;
xmlSecAssert2(x509Value != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
if(xmlSecCheckNodeName(node, xmlSecNodeX509Certificate, xmlSecDSigNs)) {
ret = xmlSecKeyX509DataValueXmlReadBase64Blob(&(x509Value->cert), node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlReadBase64Blob(cert)", NULL);
return(-1);
}
} else if(xmlSecCheckNodeName(node, xmlSecNodeX509CRL, xmlSecDSigNs)) {
ret = xmlSecKeyX509DataValueXmlReadBase64Blob(&(x509Value->crl), node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlReadBase64Blob(crl)", NULL);
return(-1);
}
} else if(xmlSecCheckNodeName(node, xmlSecNodeX509SKI, xmlSecDSigNs)) {
ret = xmlSecKeyX509DataValueXmlReadBase64Blob(&(x509Value->ski), node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlReadBase64Blob(ski)", NULL);
return(-1);
}
} else if(xmlSecCheckNodeName(node, xmlSecNodeX509SubjectName, xmlSecDSigNs)) {
ret = xmlSecKeyX509DataValueXmlReadString(&(x509Value->subject), node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlReadString(subject)", NULL);
return(-1);
}
} else if(xmlSecCheckNodeName(node, xmlSecNodeX509IssuerSerial, xmlSecDSigNs)) {
ret = xmlSecKeyX509DataValueXmlReadIssuerSerial(x509Value, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlReadIssuerSerial", NULL);
return(-1);
}
} else if(xmlSecCheckNodeName(node, xmlSecNodeX509Digest, xmlSecDSig11Ns)) {
xmlSecAssert2(x509Value->digestAlgorithm == NULL, -1);
/* The digest algorithm URI is identified with a required Algorithm attribute */
x509Value->digestAlgorithm = xmlGetProp(node, xmlSecAttrAlgorithm);
if(x509Value->digestAlgorithm == NULL) {
xmlSecInvalidNodeAttributeError(node, xmlSecAttrAlgorithm, NULL, "empty");
return(-1);
}
/* The<dsig11:X509Digest/> element contains a base64-encoded digest of a certificate. */
ret = xmlSecKeyX509DataValueXmlReadBase64Blob(&(x509Value->digest), node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlReadBase64Blob(digest)", NULL);
return(-1);
}
} else if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_STOP_ON_UNKNOWN_CHILD) != 0) {
/* laxi schema validation: ignore unknown nodes */
xmlSecUnexpectedNodeError(node, NULL);
return(-1);
}
/* done */
return(0);
}
static xmlNodePtr
xmlSecKeyX509DataValueXmlWriteBase64Blob(xmlSecBufferPtr buf, xmlNodePtr node,
const xmlChar* nodeName, const xmlChar* nodeNs,
int base64LineSize, int addLineBreaks) {
xmlNodePtr child = NULL;
xmlChar *content;
xmlSecAssert2(buf != NULL, NULL);
xmlSecAssert2(node != NULL, NULL);
xmlSecAssert2(nodeName != NULL, NULL);
content = xmlSecBase64Encode(xmlSecBufferGetData(buf), xmlSecBufferGetSize(buf),
base64LineSize);
if(content == NULL) {
xmlSecInternalError("xmlSecBase64Encode", NULL);
goto done;
}
child = xmlSecEnsureEmptyChild(node, nodeName, nodeNs);
if(child == NULL) {
xmlSecInternalError2("xmlSecEnsureEmptyChild()", NULL,
"nodeName=%s", xmlSecErrorsSafeString(nodeName));
goto done;
}
if(addLineBreaks) {
xmlNodeAddContent(child, xmlSecGetDefaultLineFeed());
}
xmlNodeSetContent(child, content);
if(addLineBreaks) {
xmlNodeAddContent(child, xmlSecGetDefaultLineFeed());
}
/* success */
done:
/* cleanup */
if(content != NULL) {
xmlFree(content);
}
return(child);
}
static int
xmlSecKeyX509DataValueXmlWriteString(const xmlChar* content, xmlNodePtr node,
const xmlChar* nodeName, const xmlChar* nodeNs) {
xmlNodePtr cur;
xmlSecAssert2(content != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(nodeName != NULL, -1);
cur = xmlSecEnsureEmptyChild(node, nodeName, nodeNs);
if(cur == NULL) {
xmlSecInternalError2("xmlSecEnsureEmptyChild()", NULL,
"nodeName=%s", xmlSecErrorsSafeString(nodeName));
return(-1);
}
xmlNodeSetContent(cur, content);
/* success */
return(0);
}
static int
xmlSecKeyX509DataValueXmlWrite(xmlSecKeyX509DataValuePtr x509Value, xmlNodePtr node,
int base64LineSize, int addLineBreaks) {
xmlSecAssert2(x509Value != NULL, -1);
xmlSecAssert2(node != NULL, -1);
if(!xmlSecBufferIsEmpty(&(x509Value->cert))) {
xmlNodePtr child;
child = xmlSecKeyX509DataValueXmlWriteBase64Blob(&(x509Value->cert), node,
xmlSecNodeX509Certificate, xmlSecDSigNs,
base64LineSize, addLineBreaks);
if(child == NULL) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlWriteBase64Blob(cert)", NULL);
return(-1);
}
}
if(!xmlSecBufferIsEmpty(&(x509Value->crl))) {
xmlNodePtr child;
child = xmlSecKeyX509DataValueXmlWriteBase64Blob(&(x509Value->crl), node,
xmlSecNodeX509CRL, xmlSecDSigNs,
base64LineSize, addLineBreaks);
if(child == NULL) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlWriteBase64Blob(cert)", NULL);
return(-1);
}
}
if(!xmlSecBufferIsEmpty(&(x509Value->ski))) {
xmlNodePtr child;
child = xmlSecKeyX509DataValueXmlWriteBase64Blob(&(x509Value->ski), node,
xmlSecNodeX509SKI, xmlSecDSigNs,
base64LineSize, addLineBreaks);
if(child == NULL) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlWriteBase64Blob(ski)", NULL);
return(-1);
}
}
if(x509Value->subject != NULL) {
int ret;
ret = xmlSecKeyX509DataValueXmlWriteString(x509Value->subject, node,
xmlSecNodeX509SubjectName, xmlSecDSigNs);
if(ret < 0) {
xmlSecInternalError2("xmlSecKeyX509DataValueXmlWriteString", NULL,
"subject=%s", xmlSecErrorsSafeString(x509Value->subject));
return(-1);
}
}
if((x509Value->issuerName != NULL) && (x509Value->issuerSerial != NULL)) {
xmlNodePtr issuerSerial;
int ret;
issuerSerial = xmlSecEnsureEmptyChild(node, xmlSecNodeX509IssuerSerial, xmlSecDSigNs);
if(issuerSerial == NULL) {
xmlSecInternalError("xmlSecEnsureEmptyChild(xmlSecNodeX509IssuerSerial)", NULL);
return(-1);
}
ret = xmlSecKeyX509DataValueXmlWriteString(x509Value->issuerName, issuerSerial,
xmlSecNodeX509IssuerName, xmlSecDSigNs);
if(ret < 0) {
xmlSecInternalError2("xmlSecKeyX509DataValueXmlWriteString", NULL,
"issuerName=%s", xmlSecErrorsSafeString(x509Value->issuerName));
return(-1);
}
ret = xmlSecKeyX509DataValueXmlWriteString(x509Value->issuerSerial, issuerSerial,
xmlSecNodeX509SerialNumber, xmlSecDSigNs);
if(ret < 0) {
xmlSecInternalError2("xmlSecKeyX509DataValueXmlWriteString", NULL,
"issuerSerial=%s", xmlSecErrorsSafeString(x509Value->issuerSerial));
return(-1);
}
}
if((!xmlSecBufferIsEmpty(&(x509Value->digest))) && (x509Value->digestAlgorithm != NULL)) {
xmlNodePtr child;
child = xmlSecKeyX509DataValueXmlWriteBase64Blob(&(x509Value->digest), node,
xmlSecNodeX509Digest, xmlSecDSig11Ns,
base64LineSize, addLineBreaks);
if(child == NULL) {
xmlSecInternalError("xmlSecKeyX509DataValueXmlWriteBase64Blob(digest)", NULL);
return(-1);
}
if(xmlSetProp(child, xmlSecAttrAlgorithm, x509Value->digestAlgorithm) == NULL) {
xmlSecXmlError2("xmlSetProp", NULL, "name=%s", xmlSecErrorsSafeString(xmlSecAttrAlgorithm));
return(-1);
}
}
return(0);
}
/**
* xmlSecX509EscapedStringRead:
* @in: the in/out pointer to the parsed string.
* @inSize: the in/out size of the parsed string.
* @out: the pointer to output string.
* @outSize: the size of the output string.
* @outWritten: the number of characters written to the output string.
* @delim: the delimiter (stop char).
* @ingoreTrailingSpaces: the flag indicating if trailing spaces should not be copied to output.
*
* Reads X509 escaped string (see https://datatracker.ietf.org/doc/html/rfc4514#section-3).
* The function parses the string in the @in paramter until end of string or @delim is encountered.
* The @in and @inSize parameters are moved to the next character (e.g. delimeter if it was encountered
* during parsing).
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecX509EscapedStringRead(const xmlChar **in, xmlSecSize *inSize,
xmlSecByte *out, xmlSecSize outSize, xmlSecSize *outWritten,
xmlSecByte delim, int ingoreTrailingSpaces
) {
xmlSecByte inCh, inFirstHex = 0;
xmlSecSize ii, jj, nonSpaceJJ;
int state = XMLSEC_X509_NAME_READ_STATE_NORMAL;
xmlSecAssert2(in != NULL, -1);
xmlSecAssert2((*in) != NULL, -1);
xmlSecAssert2(inSize != NULL, -1);
xmlSecAssert2(out != NULL, -1);
xmlSecAssert2(outWritten != NULL, -1);
ii = jj = nonSpaceJJ = 0;
while ((ii < (*inSize)) && (state != XMLSEC_X509_NAME_READ_STATE_DELIMETER)) {
inCh = (*in)[ii];
if (jj >= outSize) {
xmlSecInvalidSizeOtherError("output buffer is too small", NULL);
return(-1);
}
switch(state) {
case XMLSEC_X509_NAME_READ_STATE_NORMAL:
if (inCh == delim) {
/* stop */
state = XMLSEC_X509_NAME_READ_STATE_DELIMETER;
} else if (inCh == '\\') {
/* do not update output, move to next chat */
state = XMLSEC_X509_NAME_READ_STATE_AFTER_SLASH1;
++ii;
} else {
/* copy char and move to next */
out[jj] = inCh;
++ii;
++jj;
/* remember position of last non-spaceChar */
if (ingoreTrailingSpaces && !isspace(inCh)) {
nonSpaceJJ = jj;
}
}
break;
case XMLSEC_X509_NAME_READ_STATE_AFTER_SLASH1:
/* if next char after \\ is a hex then we expect \\XX, otherwise we just remove \\ */
if (xmlSecIsHex(inCh)) {
inFirstHex = inCh;
state = XMLSEC_X509_NAME_READ_STATE_AFTER_SLASH2;
++ii;
} else {
/* just remove \\ */
state = XMLSEC_X509_NAME_READ_STATE_NORMAL;
/* copy char and move to next */
out[jj] = inCh;
++ii;
++jj;
/* remember position of last non-spaceChar */
if (ingoreTrailingSpaces && !isspace(inCh)) {
nonSpaceJJ = jj;
}
}
break;
case XMLSEC_X509_NAME_READ_STATE_AFTER_SLASH2:
/* two XX chars are expected */
if ((xmlSecIsHex(inCh)) && (inFirstHex > 0)) {
state = XMLSEC_X509_NAME_READ_STATE_NORMAL;
inCh = xmlSecFromHex2(inFirstHex, inCh);
inFirstHex = 0;
/* copy char and move to next */
out[jj] = inCh;
++ii;
++jj;
/* remember position of last non-spaceChar */
if (ingoreTrailingSpaces && !isspace(inCh)) {
nonSpaceJJ = jj;
}
} else {
xmlSecInvalidDataError("two hex digits expected", NULL);
return(-1);
}
break;
default:
/* This should not be possible: logical error! */
xmlSecInternalError2("", NULL, "invalid state=%d while parsing x509 name", state);
return(-1);
}
}
/* success */
(*inSize) -= ii;
(*in) += ii;
if (ingoreTrailingSpaces != 0) {
(*outWritten) = nonSpaceJJ;
} else {
(*outWritten) = (jj);
}
return(0);
}
/**
* xmlSecX509AttrValueStringRead:
* @in: the in/out pointer to the parsed string.
* @inSize: the in/out size of the parsed string.
* @out: the pointer to output string.
* @outSize: the size of the output string.
* @outWritten: the number of characters written to the output string.
* @outType: the type of string (UTF8 or octet).
* @delim: the delimiter (stop char).
* @ingoreTrailingSpaces: the flag indicating if trailing spaces should not be copied to output.
*
* Reads X509 attr value string (see https://datatracker.ietf.org/doc/html/rfc4514#section-3) of one of the
* three types:
* - string (eg 'abc')
* - quoted string (eg '"abc"')
* - hexstring (eg '#A0B0')
* The function parses the string in the @in paramter until end of string or @delim is encountered.
* The @in and @inSize parameters are moved to the next character (e.g. delimeter if it was encountered
* during parsing).
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecX509AttrValueStringRead(
const xmlChar **in,
xmlSecSize *inSize,
xmlSecByte *out,
xmlSecSize outSize,
xmlSecSize *outWritten,
int *outType,
xmlSecByte delim,
int ingoreTrailingSpaces
) {