/
x509.c
1730 lines (1484 loc) · 59.3 KB
/
x509.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
/**
* XMLSec library
*
* X509 support
*
*
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2010-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
*/
#include "globals.h"
#ifndef XMLSEC_NO_X509
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <libxml/tree.h>
#include <xmlsec/xmlsec.h>
#include <xmlsec/xmltree.h>
#include <xmlsec/keys.h>
#include <xmlsec/keyinfo.h>
#include <xmlsec/keysmngr.h>
#include <xmlsec/x509.h>
#include <xmlsec/base64.h>
#include <xmlsec/errors.h>
#include <xmlsec/private.h>
#include <xmlsec/gnutls/crypto.h>
#include <xmlsec/gnutls/x509.h>
#include "x509utils.h"
/*************************************************************************
*
* X509 utility functions
*
************************************************************************/
static int xmlSecGnuTLSX509DataNodeRead (xmlSecKeyDataPtr data,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509CertificateNodeRead (xmlSecKeyDataPtr data,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509CertificateNodeWrite (gnutls_x509_crt_t cert,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509SubjectNameNodeRead (xmlSecKeyDataPtr data,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509SubjectNameNodeWrite (gnutls_x509_crt_t cert,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509IssuerSerialNodeRead (xmlSecKeyDataPtr data,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509IssuerSerialNodeWrite (gnutls_x509_crt_t cert,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509SKINodeRead (xmlSecKeyDataPtr data,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509SKINodeWrite (gnutls_x509_crt_t cert,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509CRLNodeRead (xmlSecKeyDataPtr data,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSX509CRLNodeWrite (gnutls_x509_crl_t crl,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSKeyDataX509VerifyAndExtractKey(xmlSecKeyDataPtr data,
xmlSecKeyPtr key,
xmlSecKeyInfoCtxPtr keyInfoCtx);
/*************************************************************************
*
* Internal GnuTLS X509 data CTX
*
************************************************************************/
typedef struct _xmlSecGnuTLSX509DataCtx xmlSecGnuTLSX509DataCtx,
*xmlSecGnuTLSX509DataCtxPtr;
struct _xmlSecGnuTLSX509DataCtx {
gnutls_x509_crt_t keyCert;
xmlSecPtrList certsList;
xmlSecPtrList crlsList;
};
/**************************************************************************
*
* <dsig:X509Data> processing
*
*
* 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.
*
* 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) >
*
* -----------------------------------------------------------------------
*
* xmlSecGnuTLSX509DataCtx is located after xmlSecTransform
*
*************************************************************************/
#define xmlSecGnuTLSX509DataSize \
(sizeof(xmlSecKeyData) + sizeof(xmlSecGnuTLSX509DataCtx))
#define xmlSecGnuTLSX509DataGetCtx(data) \
((xmlSecGnuTLSX509DataCtxPtr)(((xmlSecByte*)(data)) + sizeof(xmlSecKeyData)))
static int xmlSecGnuTLSKeyDataX509Initialize (xmlSecKeyDataPtr data);
static int xmlSecGnuTLSKeyDataX509Duplicate (xmlSecKeyDataPtr dst,
xmlSecKeyDataPtr src);
static void xmlSecGnuTLSKeyDataX509Finalize (xmlSecKeyDataPtr data);
static int xmlSecGnuTLSKeyDataX509XmlRead (xmlSecKeyDataId id,
xmlSecKeyPtr key,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static int xmlSecGnuTLSKeyDataX509XmlWrite (xmlSecKeyDataId id,
xmlSecKeyPtr key,
xmlNodePtr node,
xmlSecKeyInfoCtxPtr keyInfoCtx);
static xmlSecKeyDataType xmlSecGnuTLSKeyDataX509GetType (xmlSecKeyDataPtr data);
static const xmlChar* xmlSecGnuTLSKeyDataX509GetIdentifier (xmlSecKeyDataPtr data);
static void xmlSecGnuTLSKeyDataX509DebugDump (xmlSecKeyDataPtr data,
FILE* output);
static void xmlSecGnuTLSKeyDataX509DebugXmlDump (xmlSecKeyDataPtr data,
FILE* output);
static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataX509Klass = {
sizeof(xmlSecKeyDataKlass),
xmlSecGnuTLSX509DataSize,
/* data */
xmlSecNameX509Data,
xmlSecKeyDataUsageKeyInfoNode | xmlSecKeyDataUsageRetrievalMethodNodeXml,
/* xmlSecKeyDataUsage usage; */
xmlSecHrefX509Data, /* const xmlChar* href; */
xmlSecNodeX509Data, /* const xmlChar* dataNodeName; */
xmlSecDSigNs, /* const xmlChar* dataNodeNs; */
/* constructors/destructor */
xmlSecGnuTLSKeyDataX509Initialize, /* xmlSecKeyDataInitializeMethod initialize; */
xmlSecGnuTLSKeyDataX509Duplicate, /* xmlSecKeyDataDuplicateMethod duplicate; */
xmlSecGnuTLSKeyDataX509Finalize, /* xmlSecKeyDataFinalizeMethod finalize; */
NULL, /* xmlSecKeyDataGenerateMethod generate; */
/* get info */
xmlSecGnuTLSKeyDataX509GetType, /* xmlSecKeyDataGetTypeMethod getType; */
NULL, /* xmlSecKeyDataGetSizeMethod getSize; */
xmlSecGnuTLSKeyDataX509GetIdentifier, /* xmlSecKeyDataGetIdentifier getIdentifier; */
/* read/write */
xmlSecGnuTLSKeyDataX509XmlRead, /* xmlSecKeyDataXmlReadMethod xmlRead; */
xmlSecGnuTLSKeyDataX509XmlWrite, /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
NULL, /* xmlSecKeyDataBinReadMethod binRead; */
NULL, /* xmlSecKeyDataBinWriteMethod binWrite; */
/* debug */
xmlSecGnuTLSKeyDataX509DebugDump, /* xmlSecKeyDataDebugDumpMethod debugDump; */
xmlSecGnuTLSKeyDataX509DebugXmlDump, /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
/* reserved for the future */
NULL, /* void* reserved0; */
NULL, /* void* reserved1; */
};
/**
* xmlSecGnuTLSKeyDataX509GetKlass:
*
* The GnuTLS X509 key data klass (http://www.w3.org/TR/xmldsig-core/#sec-X509Data).
*
* Returns: the X509 data klass.
*/
xmlSecKeyDataId
xmlSecGnuTLSKeyDataX509GetKlass(void) {
return(&xmlSecGnuTLSKeyDataX509Klass);
}
/**
* xmlSecGnuTLSKeyDataX509GetKeyCert:
* @data: the pointer to X509 key data.
*
* Gets the certificate from which the key was extracted.
*
* Returns: the key's certificate or NULL if key data was not used for key
* extraction or an error occurs.
*/
gnutls_x509_crt_t
xmlSecGnuTLSKeyDataX509GetKeyCert(xmlSecKeyDataPtr data) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), NULL);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, NULL);
return(ctx->keyCert);
}
/**
* xmlSecGnuTLSKeyDataX509AdoptKeyCert:
* @data: the pointer to X509 key data.
* @cert: the pointer to GnuTLS X509 certificate.
*
* Sets the key's certificate in @data.
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecGnuTLSKeyDataX509AdoptKeyCert(xmlSecKeyDataPtr data, gnutls_x509_crt_t cert) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(cert != NULL, -1);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, -1);
if(ctx->keyCert != NULL) {
gnutls_x509_crt_deinit(ctx->keyCert);
}
ctx->keyCert = cert;
return(0);
}
/**
* xmlSecGnuTLSKeyDataX509AdoptCert:
* @data: the pointer to X509 key data.
* @cert: the pointer to GnuTLS X509 certificate.
*
* Adds certificate to the X509 key data.
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecGnuTLSKeyDataX509AdoptCert(xmlSecKeyDataPtr data, gnutls_x509_crt_t cert) {
xmlSecGnuTLSX509DataCtxPtr ctx;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(cert != NULL, -1);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, -1);
ret = xmlSecPtrListAdd(&(ctx->certsList), cert);
if(ret < 0) {
xmlSecInternalError("xmlSecPtrListAdd",
xmlSecKeyDataGetName(data));
return(-1);
}
return(0);
}
/**
* xmlSecGnuTLSKeyDataX509GetCert:
* @data: the pointer to X509 key data.
* @pos: the desired certificate position.
*
* Gets a certificate from X509 key data.
*
* Returns: the pointer to certificate or NULL if @pos is larger than the
* number of certificates in @data or an error occurs.
*/
gnutls_x509_crt_t
xmlSecGnuTLSKeyDataX509GetCert(xmlSecKeyDataPtr data, xmlSecSize pos) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), NULL);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, NULL);
return(xmlSecPtrListGetItem(&(ctx->certsList), pos));
}
/**
* xmlSecGnuTLSKeyDataX509GetCertsSize:
* @data: the pointer to X509 key data.
*
* Gets the number of certificates in @data.
*
* Returns: te number of certificates in @data.
*/
xmlSecSize
xmlSecGnuTLSKeyDataX509GetCertsSize(xmlSecKeyDataPtr data) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), 0);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, 0);
return(xmlSecPtrListGetSize(&(ctx->certsList)));
}
/**
* xmlSecGnuTLSKeyDataX509AdoptCrl:
* @data: the pointer to X509 key data.
* @crl: the pointer to GnuTLS X509 crl.
*
* Adds crl to the X509 key data.
*
* Returns: 0 on success or a negative value if an error occurs.
*/
int
xmlSecGnuTLSKeyDataX509AdoptCrl(xmlSecKeyDataPtr data, gnutls_x509_crl_t crl) {
xmlSecGnuTLSX509DataCtxPtr ctx;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(crl != NULL, -1);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, -1);
ret = xmlSecPtrListAdd(&(ctx->crlsList), crl);
if(ret < 0) {
xmlSecInternalError("xmlSecPtrListAdd",
xmlSecKeyDataGetName(data));
return(-1);
}
return(0);
}
/**
* xmlSecGnuTLSKeyDataX509GetCrl:
* @data: the pointer to X509 key data.
* @pos: the desired crl position.
*
* Gets a crl from X509 key data.
*
* Returns: the pointer to crl or NULL if @pos is larger than the
* number of crls in @data or an error occurs.
*/
gnutls_x509_crl_t
xmlSecGnuTLSKeyDataX509GetCrl(xmlSecKeyDataPtr data, xmlSecSize pos) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), NULL);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, NULL);
return(xmlSecPtrListGetItem(&(ctx->crlsList), pos));
}
/**
* xmlSecGnuTLSKeyDataX509GetCrlsSize:
* @data: the pointer to X509 key data.
*
* Gets the number of crls in @data.
*
* Returns: te number of crls in @data.
*/
xmlSecSize
xmlSecGnuTLSKeyDataX509GetCrlsSize(xmlSecKeyDataPtr data) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), 0);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, 0);
return(xmlSecPtrListGetSize(&(ctx->crlsList)));
}
static int
xmlSecGnuTLSKeyDataX509Initialize(xmlSecKeyDataPtr data) {
xmlSecGnuTLSX509DataCtxPtr ctx;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert2(ctx != NULL, -1);
memset(ctx, 0, sizeof(xmlSecGnuTLSX509DataCtx));
ret = xmlSecPtrListInitialize(&(ctx->certsList), xmlSecGnuTLSX509CrtListId);
if(ret < 0) {
xmlSecInternalError("xmlSecPtrListInitialize(certsList)",
xmlSecKeyDataGetName(data));
return(-1);
}
ret = xmlSecPtrListInitialize(&(ctx->crlsList), xmlSecGnuTLSX509CrlListId);
if(ret < 0) {
xmlSecInternalError("xmlSecPtrListInitialize(crlsList)",
xmlSecKeyDataGetName(data));
return(-1);
}
return(0);
}
static int
xmlSecGnuTLSKeyDataX509Duplicate(xmlSecKeyDataPtr dst, xmlSecKeyDataPtr src) {
xmlSecGnuTLSX509DataCtxPtr ctxSrc;
xmlSecGnuTLSX509DataCtxPtr ctxDst;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(dst, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(xmlSecKeyDataCheckId(src, xmlSecGnuTLSKeyDataX509Id), -1);
ctxSrc = xmlSecGnuTLSX509DataGetCtx(src);
xmlSecAssert2(ctxSrc != NULL, 0);
ctxDst = xmlSecGnuTLSX509DataGetCtx(dst);
xmlSecAssert2(ctxDst != NULL, 0);
/* copy key cert if exist */
if(ctxDst->keyCert != NULL) {
gnutls_x509_crt_deinit(ctxDst->keyCert);
ctxDst->keyCert = NULL;
}
if(ctxSrc->keyCert != NULL) {
ctxDst->keyCert = xmlSecGnuTLSX509CertDup(ctxSrc->keyCert);
if(ctxDst->keyCert == NULL) {
xmlSecInternalError("xmlSecGnuTLSX509CertDup",
xmlSecKeyDataGetName(src));
return(-1);
}
}
/* copy certsList if exists */
xmlSecPtrListEmpty(&(ctxDst->certsList));
ret = xmlSecPtrListCopy(&(ctxDst->certsList), &(ctxSrc->certsList));
if(ret < 0) {
xmlSecInternalError("xmlSecPtrListCopy(certsList)",
xmlSecKeyDataGetName(src));
return(-1);
}
/* copy crlsList if exists */
xmlSecPtrListEmpty(&(ctxDst->crlsList));
ret = xmlSecPtrListCopy(&(ctxDst->crlsList), &(ctxSrc->crlsList));
if(ret < 0) {
xmlSecInternalError("xmlSecPtrListCopy(crlsList)",
xmlSecKeyDataGetName(src));
return(-1);
}
/* done */
return(0);
}
static void
xmlSecGnuTLSKeyDataX509Finalize(xmlSecKeyDataPtr data) {
xmlSecGnuTLSX509DataCtxPtr ctx;
xmlSecAssert(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id));
ctx = xmlSecGnuTLSX509DataGetCtx(data);
xmlSecAssert(ctx != NULL);
xmlSecPtrListFinalize(&(ctx->crlsList));
xmlSecPtrListFinalize(&(ctx->certsList));
if(ctx->keyCert != NULL) {
gnutls_x509_crt_deinit(ctx->keyCert);
}
memset(ctx, 0, sizeof(xmlSecGnuTLSX509DataCtx));
}
static int
xmlSecGnuTLSKeyDataX509XmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlSecKeyDataPtr data;
int ret;
xmlSecAssert2(id == xmlSecGnuTLSKeyDataX509Id, -1);
xmlSecAssert2(key != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
data = xmlSecKeyEnsureData(key, id);
if(data == NULL) {
xmlSecInternalError("xmlSecKeyEnsureData",
xmlSecKeyDataKlassGetName(id));
return(-1);
}
ret = xmlSecGnuTLSX509DataNodeRead(data, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSX509DataNodeRead",
xmlSecKeyDataKlassGetName(id));
return(-1);
}
ret = xmlSecGnuTLSKeyDataX509VerifyAndExtractKey(data, key, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSKeyDataX509VerifyAndExtractKey",
xmlSecKeyDataKlassGetName(id));
return(-1);
}
return(0);
}
static int
xmlSecGnuTLSKeyDataX509XmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlSecKeyDataPtr data;
gnutls_x509_crt_t cert;
gnutls_x509_crl_t crl;
xmlSecSize size, pos;
int content;
int ret;
xmlSecAssert2(id == xmlSecGnuTLSKeyDataX509Id, -1);
xmlSecAssert2(key != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
content = xmlSecX509DataGetNodeContent (node, keyInfoCtx);
if (content < 0) {
xmlSecInternalError2("xmlSecX509DataGetNodeContent",
xmlSecKeyDataKlassGetName(id),
"content=%d", content);
return(-1);
} else if(content == 0) {
/* by default we are writing certificates and crls */
content = XMLSEC_X509DATA_DEFAULT;
}
/* get x509 data */
data = xmlSecKeyGetData(key, id);
if(data == NULL) {
/* no x509 data in the key */
return(0);
}
/* write certs */
size = xmlSecGnuTLSKeyDataX509GetCertsSize(data);
for(pos = 0; pos < size; ++pos) {
cert = xmlSecGnuTLSKeyDataX509GetCert(data, pos);
if(cert == NULL) {
xmlSecInternalError2("xmlSecGnuTLSKeyDataX509GetCert",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
if((content & XMLSEC_X509DATA_CERTIFICATE_NODE) != 0) {
ret = xmlSecGnuTLSX509CertificateNodeWrite(cert, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError2("xmlSecGnuTLSX509CertificateNodeWrite",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
}
if((content & XMLSEC_X509DATA_SUBJECTNAME_NODE) != 0) {
ret = xmlSecGnuTLSX509SubjectNameNodeWrite(cert, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError2("xmlSecGnuTLSX509SubjectNameNodeWrite",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
}
if((content & XMLSEC_X509DATA_ISSUERSERIAL_NODE) != 0) {
ret = xmlSecGnuTLSX509IssuerSerialNodeWrite(cert, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError2("xmlSecGnuTLSX509IssuerSerialNodeWrite",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
}
if((content & XMLSEC_X509DATA_SKI_NODE) != 0) {
ret = xmlSecGnuTLSX509SKINodeWrite(cert, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError2("xmlSecGnuTLSX509SKINodeWrite",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
}
}
/* write crls if needed */
if((content & XMLSEC_X509DATA_CRL_NODE) != 0) {
size = xmlSecGnuTLSKeyDataX509GetCrlsSize(data);
for(pos = 0; pos < size; ++pos) {
crl = xmlSecGnuTLSKeyDataX509GetCrl(data, pos);
if(crl == NULL) {
xmlSecInternalError2("xmlSecGnuTLSKeyDataX509GetCrl",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
ret = xmlSecGnuTLSX509CRLNodeWrite(crl, node, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError2("xmlSecGnuTLSX509CRLNodeWrite",
xmlSecKeyDataKlassGetName(id),
"pos=%d", pos);
return(-1);
}
}
}
/* done */
return(0);
}
static xmlSecKeyDataType
xmlSecGnuTLSKeyDataX509GetType(xmlSecKeyDataPtr data) {
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), xmlSecKeyDataTypeUnknown);
/* TODO: return verified/not verified status */
return(xmlSecKeyDataTypeUnknown);
}
static const xmlChar*
xmlSecGnuTLSKeyDataX509GetIdentifier(xmlSecKeyDataPtr data) {
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), NULL);
/* TODO */
return(NULL);
}
static void
xmlSecGnuTLSKeyDataX509DebugDump(xmlSecKeyDataPtr data, FILE* output) {
xmlSecSize size, pos;
xmlSecAssert(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id));
xmlSecAssert(output != NULL);
fprintf(output, "=== X509 Data:\n");
/* key cert */
{
gnutls_x509_crt_t cert;
cert = xmlSecGnuTLSKeyDataX509GetKeyCert(data);
if(cert != NULL) {
fprintf(output, "==== Key Certificate:\n");
xmlSecGnuTLSX509CertDebugDump(cert, output);
}
}
/* other certs */
size = xmlSecGnuTLSKeyDataX509GetCertsSize(data);
for(pos = 0; pos < size; ++pos) {
gnutls_x509_crt_t cert;
cert = xmlSecGnuTLSKeyDataX509GetCert(data, pos);
if(cert == NULL) {
xmlSecInternalError2("xmlSecGnuTLSKeyDataX509GetCert",
xmlSecKeyDataGetName(data),
"pos=%d", pos);
return;
}
fprintf(output, "==== Certificate:\n");
xmlSecGnuTLSX509CertDebugDump(cert, output);
}
/* crls */
size = xmlSecGnuTLSKeyDataX509GetCrlsSize(data);
for(pos = 0; pos < size; ++pos) {
gnutls_x509_crl_t crl;
crl = xmlSecGnuTLSKeyDataX509GetCrl(data, pos);
if(crl == NULL) {
xmlSecInternalError2("xmlSecGnuTLSKeyDataX509GetCrl",
xmlSecKeyDataGetName(data),
"pos=%d", pos);
return;
}
fprintf(output, "==== Crl:\n");
xmlSecGnuTLSX509CrlDebugDump(crl, output);
}
}
static void
xmlSecGnuTLSKeyDataX509DebugXmlDump(xmlSecKeyDataPtr data, FILE* output) {
xmlSecSize size, pos;
xmlSecAssert(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id));
xmlSecAssert(output != NULL);
fprintf(output, "<X509Data>\n");
/* key cert */
{
gnutls_x509_crt_t cert;
cert = xmlSecGnuTLSKeyDataX509GetKeyCert(data);
if(cert != NULL) {
fprintf(output, "<KeyCertificate>\n");
xmlSecGnuTLSX509CertDebugXmlDump(cert, output);
fprintf(output, "</KeyCertificate>\n");
}
}
/* other certs */
size = xmlSecGnuTLSKeyDataX509GetCertsSize(data);
for(pos = 0; pos < size; ++pos) {
gnutls_x509_crt_t cert;
cert = xmlSecGnuTLSKeyDataX509GetCert(data, pos);
if(cert == NULL) {
xmlSecInternalError2("xmlSecGnuTLSKeyDataX509GetCert",
xmlSecKeyDataGetName(data),
"pos=%d", pos);
return;
}
fprintf(output, "<Certificate>\n");
xmlSecGnuTLSX509CertDebugXmlDump(cert, output);
fprintf(output, "</Certificate>\n");
}
/* other crls */
size = xmlSecGnuTLSKeyDataX509GetCrlsSize(data);
for(pos = 0; pos < size; ++pos) {
gnutls_x509_crl_t crl;
crl = xmlSecGnuTLSKeyDataX509GetCrl(data, pos);
if(crl == NULL) {
xmlSecInternalError2("xmlSecGnuTLSKeyDataX509GetCrl",
xmlSecKeyDataGetName(data),
"pos=%d", pos);
return;
}
fprintf(output, "<CRL>\n");
xmlSecGnuTLSX509CrlDebugXmlDump(crl, output);
fprintf(output, "</CRL>\n");
}
/* we don't print out crls */
fprintf(output, "</X509Data>\n");
}
static int
xmlSecGnuTLSX509DataNodeRead(xmlSecKeyDataPtr data, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlNodePtr cur;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
for(cur = xmlSecGetNextElementNode(node->children);
cur != NULL;
cur = xmlSecGetNextElementNode(cur->next)) {
ret = 0;
if(xmlSecCheckNodeName(cur, xmlSecNodeX509Certificate, xmlSecDSigNs)) {
ret = xmlSecGnuTLSX509CertificateNodeRead(data, cur, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSX509CertificateNodeRead",
xmlSecKeyDataGetName(data));
return(-1);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509SubjectName, xmlSecDSigNs)) {
ret = xmlSecGnuTLSX509SubjectNameNodeRead(data, cur, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSX509SubjectNameNodeRead",
xmlSecKeyDataGetName(data));
return(-1);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509IssuerSerial, xmlSecDSigNs)) {
ret = xmlSecGnuTLSX509IssuerSerialNodeRead(data, cur, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSX509IssuerSerialNodeRead",
xmlSecKeyDataGetName(data));
return(-1);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509SKI, xmlSecDSigNs)) {
ret = xmlSecGnuTLSX509SKINodeRead(data, cur, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSX509SKINodeRead",
xmlSecKeyDataGetName(data));
return(-1);
}
} else if(xmlSecCheckNodeName(cur, xmlSecNodeX509CRL, xmlSecDSigNs)) {
ret = xmlSecGnuTLSX509CRLNodeRead(data, cur, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSX509CRLNodeRead",
xmlSecKeyDataGetName(data));
return(-1);
}
} else if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_STOP_ON_UNKNOWN_CHILD) != 0) {
/* laxi schema validation: ignore unknown nodes */
xmlSecUnexpectedNodeError(cur, xmlSecKeyDataGetName(data));
return(-1);
}
}
return(0);
}
static int
xmlSecGnuTLSX509CertificateNodeRead(xmlSecKeyDataPtr data, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlChar *content;
gnutls_x509_crt_t cert;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
content = xmlNodeGetContent(node);
if((content == NULL) || (xmlSecIsEmptyString(content) == 1)) {
if(content != NULL) {
xmlFree(content);
}
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_STOP_ON_EMPTY_NODE) != 0) {
xmlSecInvalidNodeContentError(node, xmlSecKeyDataGetName(data), "empty");
return(-1);
}
return(0);
}
cert = xmlSecGnuTLSX509CertBase64DerRead(content);
if(cert == NULL) {
xmlSecInternalError("xmlSecGnuTLSX509CertBase64DerRead",
xmlSecKeyDataGetName(data));
xmlFree(content);
return(-1);
}
ret = xmlSecGnuTLSKeyDataX509AdoptCert(data, cert);
if(ret < 0) {
xmlSecInternalError("xmlSecGnuTLSKeyDataX509AdoptCert",
xmlSecKeyDataGetName(data));
gnutls_x509_crt_deinit(cert);
xmlFree(content);
return(-1);
}
xmlFree(content);
return(0);
}
static int
xmlSecGnuTLSX509CertificateNodeWrite(gnutls_x509_crt_t cert, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlChar* buf;
xmlNodePtr cur;
xmlSecAssert2(cert != NULL, -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
/* set base64 lines size from context */
buf = xmlSecGnuTLSX509CertBase64DerWrite(cert, keyInfoCtx->base64LineSize);
if(buf == NULL) {
xmlSecInternalError("xmlSecGnuTLSX509CertBase64DerWrite", NULL);
return(-1);
}
cur = xmlSecEnsureEmptyChild(node, xmlSecNodeX509Certificate, xmlSecDSigNs);
if(cur == NULL) {
xmlSecInternalError("xmlSecEnsureEmptyChild(NodeX509Certificate)", NULL);
xmlFree(buf);
return(-1);
}
/* todo: add \n around base64 data - from context */
/* todo: add errors check */
xmlNodeSetContent(cur, xmlSecStringCR);
xmlNodeSetContent(cur, buf);
xmlFree(buf);
return(0);
}
#define XMLSEC_GNUTLS_IS_SPACE(ch) \
(((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n'))
static void
xmlSecGnuTLSX509Trim(xmlChar * str) {
xmlChar * p, * q;
xmlSecAssert(str != NULL);
/* skip spaces from the beggining */
p = str;
while(XMLSEC_GNUTLS_IS_SPACE(*p) && ((*p) != '\0')) ++p;
if(p != str) {
for(q = str; ; ++q, ++p) {
(*q) = (*p);
if((*p) == '\0') {
break;
}
}
}
/* skip spaces from the end */
for(p = str; (*p) != '\0'; ++p);
while((p > str) && (XMLSEC_GNUTLS_IS_SPACE(*(p - 1)))) *(--p) = '\0';
}
static int
xmlSecGnuTLSX509SubjectNameNodeRead(xmlSecKeyDataPtr data, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlSecKeyDataStorePtr x509Store;
xmlChar* subject;
gnutls_x509_crt_t cert;
gnutls_x509_crt_t cert2;
int ret;
xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataX509Id), -1);
xmlSecAssert2(node != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
xmlSecAssert2(keyInfoCtx->keysMngr != NULL, -1);
x509Store = xmlSecKeysMngrGetDataStore(keyInfoCtx->keysMngr, xmlSecGnuTLSX509StoreId);
if(x509Store == NULL) {
xmlSecInternalError("xmlSecKeysMngrGetDataStore",