File: DigiDocEncSAXParser.c

package info (click to toggle)
libdigidoc 3.10.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,436 kB
  • sloc: ansic: 29,037; makefile: 15
file content (1173 lines) | stat: -rw-r--r-- 43,142 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
//==================================================
// FILE:	DigiDocEncSAXParser.c
// PROJECT:     Digi Doc Encryption 
// DESCRIPTION: DigiDocEnc XML SAX parsing
// AUTHOR:  Veiko Sinivee, S|E|B IT Partner Estonia
//==================================================
// Copyright (C) AS Sertifitseerimiskeskus
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// GNU Lesser General Public Licence is available at
// http://www.gnu.org/copyleft/lesser.html
//==========< HISTORY >=============================
//      11.10.2004      Veiko Sinivee
//                      Creation
//==================================================

#ifdef WIN32
#include <windows.h>
#endif

#include <libdigidoc/DigiDocDefs.h>
#include <libdigidoc/DigiDocEncSAXParser.h>
#include <libdigidoc/DigiDocError.h>
#include <libdigidoc/DigiDocDebug.h>
#include <libdigidoc/DigiDocStack.h>
#include <libdigidoc/DigiDocConvert.h>
#include <libdigidoc/DigiDocLib.h>
#include <libdigidoc/DigiDocPKCS11.h> 
#include <libdigidoc/DigiDocCert.h>
#include <libdigidoc/DigiDocDfExtract.h>

#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <zlib.h>

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>


#include <libxml/globals.h>
#include <libxml/xmlerror.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */

#if OPENSSL_VERSION_NUMBER < 0x10010000L
static EVP_ENCODE_CTX *EVP_ENCODE_CTX_new()
{
	return (EVP_ENCODE_CTX*)OPENSSL_malloc(sizeof(EVP_ENCODE_CTX));
}

static void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx)
{
	OPENSSL_free(ctx);
}
#endif

//===============< SAX handlers >==============================

/*
* Working area for XML parsing with SAX
*/
typedef struct DEncParse_st {
  DEncEncryptedData* pEncData;	// document file to be filled with data
  int errcode;
  ElementEntry dencStack;  // stack used for keeping the current parsing position
  DigiDocMemBuf mbufContent; // used for collecting element content
  int bCollectMode;          // flag to switch collection of content on and off
  char errmsg[100];
} DEncParse;


//--------------------------------------------------
// Cleans up the memory that might habe been allocated
// during parsing process
// pctx - SAX parser context
//--------------------------------------------------
void dencSaxCleanup(DEncParse* pctx)
{
  ddocMemBuf_free(&(pctx->mbufContent));
  memset(pctx, 0, sizeof(DEncParse));
}

//--------------------------------------------------
// Handles the <EncryptedData> start event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------

int dencSaxHandleStartEncryptedData(DEncParse* pctx, const xmlChar** atts)
{
  int i, err = ERR_OK;
  char *id = NULL, *type = NULL, *mime = NULL, *xmlns = NULL;

  // check the atributes
  for(i = 0; atts && atts[i] && atts[i+1]; i++) {
    if(!strcmp((char*)atts[i], "Id"))
      id = (char*)atts[i+1];
    if(!strcmp((char*)atts[i], "Type"))
      type = (char*)atts[i+1];
    if(!strcmp((char*)atts[i], "MimeType"))
      mime = (char*)atts[i+1];
    if(!strncmp((char*)atts[i], "xmlns", 5))
      xmlns = (char*)atts[i+1];
  }
  // create new EncryptedData object
  err = dencEncryptedData_new(&(pctx->pEncData), xmlns, NULL, id, type, mime);
  // delete automatically generated meta info to read the stuff from file
  err = dencMetaInfo_deleteVersionInfo(pctx->pEncData);
  return err;
}

//--------------------------------------------------
// Handles the <EncryptionMethod> start event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleStartEncryptionMethod(DEncParse* pctx, const xmlChar** atts)
{
  int i, err = ERR_OK;
  char *alg = NULL;

  // check the atributes
  for(i = 0; atts && atts[i] && atts[i+1]; i++) {
    if(!strcmp((char*)atts[i], "Algorithm"))
      alg = (char*)atts[i+1];
  }
  // check the EncryptionMethod position in xml doc
  if(ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedKey", NULL)) {
    DEncEncryptedKey* pEncKey = dencEncryptedData_GetLastEncryptedKey(pctx->pEncData);
    if(pEncKey)
      err = dencEncryptedKey_SetEncryptionMethod(pEncKey, alg);
  } else
    if(ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedData", NULL))
      err = dencEncryptedData_SetEncryptionMethod(pctx->pEncData, alg);
  return err;
}

//--------------------------------------------------
// Handles the <EncryptedKey> start event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleStartEncryptedKey(DEncParse* pctx, const xmlChar** atts)
{
  int i;
  char *id = NULL, *recipient = NULL;
  DEncEncryptedKey* pEncKey = 0;

  // check the atributes
  for(i = 0; atts && atts[i] && atts[i+1]; i++) {
    if(!strcmp((char*)atts[i], "Id"))
      id = (char*)atts[i+1];
    if(!strcmp((char*)atts[i], "Recipient"))
      recipient = (char*)atts[i+1];
  }
  // create new EncryptedData object
  return dencEncryptedKey_new(pctx->pEncData, &pEncKey, NULL, NULL,
			      id, recipient, NULL, NULL);
}

//--------------------------------------------------
// Handles the <X509Certificate> end event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleEndX509Certificate(DEncParse* pctx)
{
  int err = ERR_OK;
  X509 *pCert = 0;
  DEncEncryptedKey* pEncKey = 0;

  err = ddocDecodeX509PEMData(&pCert, (const char*)pctx->mbufContent.pMem, (int)pctx->mbufContent.nLen);
  if(err) return err;
  pEncKey = dencEncryptedData_GetLastEncryptedKey(pctx->pEncData);
  if(pEncKey)
    err = dencEncryptedKey_SetCertificate(pEncKey, pCert);
  ddocDebug(4, "dencSaxHandleEndX509Certificate", "EncKey: %s, cert: %s rc: %d",
	    (pEncKey ? "OK" : "NULL"), (pCert ? "OK" : "NULL"), err);
  // reset collect mode and cleanup
  pctx->bCollectMode = 0;
  ddocMemBuf_free(&(pctx->mbufContent));
  return err;
}

//--------------------------------------------------
// Handles the <KeyName> end event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleEndKeyName(DEncParse* pctx)
{
  int err = ERR_OK;

  DEncEncryptedKey* pEncKey = dencEncryptedData_GetLastEncryptedKey(pctx->pEncData);
  if(pEncKey)
    err = dencEncryptedKey_SetKeyName(pEncKey, (char*)pctx->mbufContent.pMem);
  ddocDebug(4, "dencSaxHandleEndKeyName", "EncKey: %s, KeyName: %s rc: %d",
    (pEncKey ? "OK" : "NULL"), (pctx->mbufContent.pMem ? pctx->mbufContent.pMem : "NULL"), err);
  // reset collect mode and cleanup
  pctx->bCollectMode = 0;
  ddocMemBuf_free(&(pctx->mbufContent));
  return err;
}

//--------------------------------------------------
// Handles the <CarriedKeyName> end event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleEndCarriedKeyName(DEncParse* pctx)
{
  int err = ERR_OK;

  DEncEncryptedKey* pEncKey = dencEncryptedData_GetLastEncryptedKey(pctx->pEncData);
  if(pEncKey)
    err = dencEncryptedKey_SetCarriedKeyName(pEncKey, (char*)pctx->mbufContent.pMem);
  ddocDebug(4, "dencSaxHandleEndKeyName", "EncKey: %s, CarriedKeyName: %s rc: %d",
    (pEncKey ? "OK" : "NULL"), (pctx->mbufContent.pMem ? pctx->mbufContent.pMem : "NULL"), err);
  // reset collect mode and cleanup
  pctx->bCollectMode = 0;
  ddocMemBuf_free(&(pctx->mbufContent));
  return err;
}

//--------------------------------------------------
// Handles the <CipherValue> end event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleEndCipherValue(DEncParse* pctx)
{
  int err = ERR_OK, l = 0, i;
  char *p = 0;
  EVP_ENCODE_CTX *ectx;

  if(pctx->mbufContent.pMem && pctx->mbufContent.nLen) {
    l = pctx->mbufContent.nLen; // enough since it's shrinking
    p = (char*)malloc(l);
    RETURN_IF_BAD_ALLOC(p)
    //decode((const byte*)pctx->mbufContent.pMem, pctx->mbufContent.nLen, p, &l);
	ectx = EVP_ENCODE_CTX_new();
	EVP_DecodeInit(ectx);
	EVP_DecodeUpdate(ectx, (unsigned char*)p, &l, (unsigned char*)pctx->mbufContent.pMem, pctx->mbufContent.nLen);
    ddocDebug(3, "dencSaxHandleEndCipherValue", "Initial decoding: %d -> %d bytes", pctx->mbufContent.nLen, l);
    i = pctx->mbufContent.nLen - l;
	EVP_DecodeFinal(ectx, (unsigned char*)p+l, &i);
	EVP_ENCODE_CTX_free(ectx);
    l += i;
    ddocDebug(3, "dencSaxHandleEndCipherValue", "Final decoding: %d bytes", i);
    ddocDebug(3, "dencSaxHandleEndCipherValue", "Decoding: %d bytes of base64 data, got: %d bytes", pctx->mbufContent.nLen, l);
    ddocMemBuf_free(&(pctx->mbufContent));
  }
  if(p) {
  // check the EncryptionMethod position in xml doc
    if(ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedKey", NULL)) {
      DEncEncryptedKey* pEncKey = dencEncryptedData_GetLastEncryptedKey(pctx->pEncData);
      if(pEncKey) {
	pEncKey->mbufTransportKey.pMem = p;
	pEncKey->mbufTransportKey.nLen = l;
	ddocDebug(4, "dencSaxHandleEndCipherValue", "Set encrypted tarnsport key: %d bytes", l);
      } else
	free(p);
    } else {
      if(ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedData", NULL)) {
	pctx->pEncData->mbufEncryptedData.pMem = p;
	pctx->pEncData->mbufEncryptedData.nLen = l;
	ddocDebug(4, "dencSaxHandleEndCipherValue", "Set encrypted data: %d bytes", l);
	if(pctx->pEncData->szMimeType && 
	   (!strcmp(pctx->pEncData->szMimeType, DENC_ENCDATA_MIME_ZLIB)))
	  pctx->pEncData->nDataStatus = DENC_DATA_STATUS_ENCRYPTED_AND_COMPRESSED;
	else
	  pctx->pEncData->nDataStatus = DENC_DATA_STATUS_ENCRYPTED_AND_NOT_COMPRESSED;
      } else
	free(p);
    }
  }
  // reset collect mode and cleanup
  pctx->bCollectMode = 0;
  return err;
}

//--------------------------------------------------
// Handles the <EncryptionProperties> start event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleStartEncryptionProperties(DEncParse* pctx, const xmlChar** atts)
{
  int i, err = ERR_OK;
  char *id = NULL;

  // check the atributes
  for(i = 0; atts && atts[i] && atts[i+1]; i++) {
    if(!strcmp((char*)atts[i], "Id"))
      id = (char*)atts[i+1];
  }
  if(id)
    err = dencEncryptedData_SetEncryptionPropertiesId(pctx->pEncData, id);
  return err;
}

//--------------------------------------------------
// Handles the <EncryptionProperty> start event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleStartEncryptionProperty(DEncParse* pctx, const xmlChar** atts)
{
  int i, err = ERR_OK;
  char *id = NULL, *target = NULL, *name = NULL;
  DEncEncryptionProperty* pEncProperty = 0;

  // check the atributes
  for(i = 0; atts && atts[i] && atts[i+1]; i++) {
    if(!strcmp((char*)atts[i], "Id"))
      id = (char*)atts[i+1];
    if(!strcmp((char*)atts[i], "Target"))
      target = (char*)atts[i+1];
    if(!strcmp((char*)atts[i], "Name"))
      name = (char*)atts[i+1];
  }
  err = dencEncryptionProperty_new(pctx->pEncData, &pEncProperty,
				    id, target, name, NULL);
  if(err) return err;
  pctx->bCollectMode = 1;
  ddocMemBuf_free(&(pctx->mbufContent));
  return err;
}

//--------------------------------------------------
// Handles the <EncryptionProperty> end event
// pctx - SAX parser context
// atts - array of atribute names and values
// returns error code or ERR_OK.
//--------------------------------------------------
int dencSaxHandleEndEncryptionProperty(DEncParse* pctx)
{
  int err = ERR_OK;
  DEncEncryptionProperty* pEncProperty = 0;

  if(pctx->mbufContent.pMem && pctx->mbufContent.nLen) {
    pEncProperty = dencEncryptedData_GetLastEncryptionProperty(pctx->pEncData);
    if(pEncProperty)
      err = dencEncryptionProperty_SetContent(pEncProperty, (char*)pctx->mbufContent.pMem);
    ddocMemBuf_free(&(pctx->mbufContent));
  }
  pctx->bCollectMode = 0;
  return err;
}



//===============< SAX handlers >==============================

//--------------------------------------------------
// dencStartElementHandler:
// @ctxt:  An XML parser context
// @name:  The element name
// called when an opening tag has been processed.
//--------------------------------------------------
static void dencStartElementHandler(void *ctx, const xmlChar *name, const xmlChar **atts)
{
  DEncParse* pctx = (DEncParse*)ctx;
  ElementEntry* pCurrElem = 0;

  ddocDebug(5, "dencStartElementHandler", "<%s>, err: %d", (const char*)name, pctx->errcode);
  if(pctx->errcode) return; // if error skip all additional parsing
  pctx->errcode = ddocStackPushElementSAX(&(pctx->dencStack), name, atts, &pCurrElem);
  if(pctx->errcode) return;
  // check the element name
  if(pCurrElem) {
    if(!strcmp((const char*)pCurrElem->tag, "EncryptedData"))
      pctx->errcode = dencSaxHandleStartEncryptedData(pctx, atts);
    if(!strcmp((const char*)pCurrElem->tag, "EncryptionMethod"))
      pctx->errcode = dencSaxHandleStartEncryptionMethod(pctx, atts);
    if(!strcmp((const char*)pCurrElem->tag, "EncryptedKey"))
      pctx->errcode = dencSaxHandleStartEncryptedKey(pctx, atts);
    // start collecting certificate data
    if(!strcmp((const char*)pCurrElem->tag, "X509Certificate") ||
       !strcmp((const char*)pCurrElem->tag, "KeyName") ||
       !strcmp((const char*)pCurrElem->tag, "CipherValue") ||
       !strcmp((const char*)pCurrElem->tag, "CarriedKeyName") ) {
      pctx->bCollectMode = 1;
      ddocMemBuf_free(&(pctx->mbufContent));
    }
    if(!strcmp((const char*)pCurrElem->tag, "EncryptionProperties"))
      pctx->errcode = dencSaxHandleStartEncryptionProperties(pctx, atts);
    if(!strcmp((const char*)pCurrElem->tag, "EncryptionProperty"))
      pctx->errcode = dencSaxHandleStartEncryptionProperty(pctx, atts);
  }
}

//--------------------------------------------------
// dencEndElementHandler:
// @ctxt:  An XML parser context
// @name:  The element name
// called when the end of an element has been detected.
//--------------------------------------------------
static void dencEndElementHandler(void *ctx, const xmlChar *name)
{
  DEncParse* pctx = (DEncParse*)ctx;
  ElementEntry* pCurrElem = 0;

  ddocDebug(5, "dencEndElementHandler", "</%s>, err: %d", (const char*)name, pctx->errcode);
  if(pctx->errcode) return; // if error skip all additional parsing
  // find last element
  pCurrElem = ddocStackFindEnd(&(pctx->dencStack));
  // check the element name
  if(pCurrElem) {
    if(!strcmp((const char*)pCurrElem->tag, "X509Certificate"))
      pctx->errcode = dencSaxHandleEndX509Certificate(pctx);
    if(!strcmp((const char*)pCurrElem->tag, "KeyName"))
      pctx->errcode = dencSaxHandleEndKeyName(pctx);
    if(!strcmp((const char*)pCurrElem->tag, "CarriedKeyName"))
      pctx->errcode = dencSaxHandleEndCarriedKeyName(pctx);
    if(!strcmp((const char*)pCurrElem->tag, "CipherValue"))
      pctx->errcode = dencSaxHandleEndCipherValue(pctx);
    if(!strcmp((const char*)pCurrElem->tag, "EncryptionProperty"))
      pctx->errcode = dencSaxHandleEndEncryptionProperty(pctx);
  }
  // pop stack
  pctx->errcode = ddocStackPopElement(&(pctx->dencStack), 0, NULL);
}

//--------------------------------------------------
// dencCharactersHandler:
// @ctxt:  An XML parser context
// @ch:  a xmlChar string
// @len: the number of xmlChar
// receiving some chars from the parser.
//--------------------------------------------------
static void dencCharactersHandler(void *ctx, const xmlChar *ch, int len)
{
  DEncParse* pctx = (DEncParse*)ctx;
	
  ddocDebug(5, "dencCharactersHandler", "err: %d", pctx->errcode);
  if(pctx->errcode) return; // if error skip all additional parsing

  if(pctx->bCollectMode) {
    pctx->errcode = ddocMemAppendData(&(pctx->mbufContent), (char*)ch, len);
  }

  ddocDebug(5, "dencCharactersHandler: %s", "End");
}


//--------------------------------------------------
// cdataBlockHandler:
// @ctx: the user data (XML parser context)
// @value:  The pcdata content
// @len:  the block length
// called when a pcdata block has been parsed
//--------------------------------------------------
static void dencCdataBlockHandler(void * ctx, const xmlChar *value, int len)
{
  ddocDebug(5, "dencCdataBlockHandler", "SAX.pcdata(%.20s, %d)", (char*)value, len);
}


//--------------------------------------------------
// dencWarningHandler:
// @ctxt:  An XML parser context
// @msg:  the message to display/transmit
// @...:  extra parameters for the message display
// Display and format a warning messages, gives file, line, position and
// extra parameters
//--------------------------------------------------
static void dencWarningHandler(void * ctx, const char *msg, ...)
{
    va_list args;

    va_start(args, msg);
    ddocDebug(2, "dencWarningHandler", msg, args);
    fprintf(stdout, "SAX.warning: ");
    vfprintf(stdout, msg, args);
    va_end(args);
}

//--------------------------------------------------
// dencErrorHandler:
// @ctxt:  An XML parser context
// @msg:  the message to display/transmit
// @...:  extra parameters for the message display
// Display and format a error messages, gives file, line, position and
// extra parameters.
//--------------------------------------------------
static void dencErrorHandler(void *ctx, const char *msg, ...)
{
  va_list args;
  DEncParse* pctx = (DEncParse*)ctx;
	
  va_start(args, msg);	
  pctx->errcode = ERR_DIGIDOC_PARSE;
  ddocDebugVaArgs(1, "dencErrorHandler", msg, args);
  addError(pctx->errcode, __FILE__, __LINE__, "XML parsing error");
  va_end(args);
}

//--------------------------------------------------
// dencFatalErrorHandler:
// @ctxt:  An XML parser context
// @msg:  the message to display/transmit
// @...:  extra parameters for the message display
// Display and format a fatalError messages, gives file, line, position and
// extra parameters.
//--------------------------------------------------
static void dencFatalErrorHandler(void *ctx, const char *msg, ...)
{
  va_list args;
  DEncParse* pctx = (DEncParse*)ctx;

  va_start(args, msg);
  pctx->errcode = ERR_DIGIDOC_PARSE;
  ddocDebugVaArgs(1, "dencFatalErrorHandler", msg, args);
  addError(pctx->errcode, __FILE__, __LINE__, "XML parsing error");
  va_end(args);
}

xmlSAXHandler dencSAXHandlerStruct = {
    NULL, //internalSubsetHandler,
    NULL, //isStandaloneHandler,
    NULL, //hasInternalSubsetHandler,
    NULL, //hasExternalSubsetHandler,
    NULL, //resolveEntityHandler,
    NULL, //getEntityHandler,
    NULL, //entityDeclHandler,
    NULL, //notationDeclHandler,
    NULL, //attributeDeclHandler,
    NULL, //elementDeclHandler,
    NULL, //unparsedEntityDeclHandler,
    NULL, //setDocumentLocatorHandler,
    NULL, //startDocumentHandler,
    NULL, //endDocumentHandler,
    dencStartElementHandler,
    dencEndElementHandler,
    NULL, //referenceHandler,
    dencCharactersHandler,
    NULL, //ignorableWhitespaceHandler,
    NULL, //processingInstructionHandler,
    NULL, //commentHandler,
    dencWarningHandler,
    dencErrorHandler,
    dencFatalErrorHandler,
    NULL, //getParameterEntityHandler,
    dencCdataBlockHandler,
    NULL, //externalSubsetHandler,
    1
};


xmlSAXHandlerPtr dencSAXHandler = &dencSAXHandlerStruct;



//--------------------------------------------------
// Reads in encrypted XML document.
// ppEncData - address for new encrypted data object [REQUIRED]
// szFileName - input file name
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int dencSaxReadEncryptedData(DEncEncryptedData** ppEncData, const char* szFileName)
{
  FILE *f;
  int ret;
  char chars[1025];
  xmlParserCtxtPtr ctxt;
  DEncParse pctx;
#ifdef WIN32
  wchar_t *convFileName = 0; 
  int i= 0;
#endif

  RETURN_IF_NULL_PARAM(szFileName)
  RETURN_IF_NULL_PARAM(ppEncData)
  clearErrors();
  *ppEncData = 0; // mark as not read yet
  memset(&pctx, 0, sizeof(pctx));
  //ddocConvertFileName(chars, sizeof(chars), szFileName );
  ddocDebug(3, "dencSaxReadEncryptedData", "Opening file: %s", szFileName);
#ifdef WIN32
  i = 0;
  ret = utf82unicode((const char*)szFileName, (char**)&convFileName, &i);
  ddocDebug(3, "dencSaxReadEncryptedData", "file: %s, conv-file: %s len: %d, rc: %d", szFileName, convFileName, i, ret);
  if((f = _wfopen(convFileName, L"rb")) != NULL) {
	ddocDebug(3, "dencSaxReadEncryptedData", "Opened w-file: %s", convFileName);
#else
  if((f = fopen(szFileName, "rb")) != NULL) {
	ddocDebug(3, "dencSaxReadEncryptedData", "Opened file: %s", szFileName);
#endif
    ret = fread(chars, 1, 1024, f);
    if (ret > 0) {
	  ddocDebug(3, "dencSaxReadEncryptedData", "Read first %d bytes", ret);
      ctxt = xmlCreatePushParserCtxt(dencSAXHandler, &pctx,
				     chars, ret, szFileName);
      while ((ret = fread(chars, 1, 1024, f)) > 0) {
		ddocDebug(3, "dencSaxReadEncryptedData", "Parsing %d bytes", ret);
		xmlParseChunk(ctxt, chars, ret, 0);
      }
      xmlParseChunk(ctxt, chars, 0, 1);
      xmlFreeParserCtxt(ctxt);
    }
    fclose(f);
  } else {
    ddocDebug(1, "dencSaxReadEncryptedData", "Error reading file: %s", szFileName);
    SET_LAST_ERROR_RETURN_CODE(ERR_FILE_READ);
  }
  // cleanup stack
  ret = pctx.errcode = ddocStackPopElement(&(pctx.dencStack), 1, NULL);
  ddocDebug(3, "dencSaxReadEncryptedData", "End parsing file: %s - RC: %d", szFileName, ret);
  if(ret == 0)
    *ppEncData = pctx.pEncData;
  // cleanup
  dencSaxCleanup(&pctx);
  return ret;
}

//--------------------------------------------------
// Reads in encrypted XML document.
// ppEncData - address for new encrypted data object [REQUIRED]
// pData - input data [REQUIRED]
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int dencSaxReadEncryptedDataFromMemory(DEncEncryptedData** ppEncData, DigiDocMemBuf* pData)
{
    int ret;
    DEncParse pctx;
        
    RETURN_IF_NULL_PARAM(pData)
    RETURN_IF_NULL_PARAM(ppEncData)
    clearErrors();
    *ppEncData = 0; // mark as not read yet
    memset(&pctx, 0, sizeof(pctx));
    ddocDebug(3, "dencSaxReadEncryptedData", "Reading from mem %d bytes", pData->nLen);
    ret = xmlSAXUserParseMemory(dencSAXHandler, &pctx, (const char*)pData->pMem, pData->nLen);
    // cleanup stack
    ret = pctx.errcode = ddocStackPopElement(&(pctx.dencStack), 1, NULL);
    ddocDebug(3, "dencSaxReadEncryptedData", "End parsing mem: %d - RC: %d", pData->nLen, ret);
    if(ret == 0)
        *ppEncData = pctx.pEncData;
    // cleanup
    dencSaxCleanup(&pctx);
    return ret;
}
    

//===============< Large file decryption SAX handlers >==============================

/*
* Working area for XML parsing with SAX
*/
typedef struct DEncDecryptParse_st {
  int errcode;
  ElementEntry dencStack;    // stack used for keeping the current parsing position
  FILE* hOutFile;
  DigiDocMemBuf mbufTransportKey;
  DigiDocMemBuf mbufTemp;
  X509* pCert;
  EVP_PKEY* pkey;
  char* szPin;
  int nSlot;
  long lB64Len, lBinLen, lDecLen;
  EVP_ENCODE_CTX *ectx;
  EVP_CIPHER_CTX *dctx;
  int nB64SkipMode;
  char errmsg[100];
  char szCertSerial[100];
  int nCipherInited;
} DEncDecryptParse;


//--------------------------------------------------
// Cleans up data that might have been allocated
// during the parsing process
// pctx:  An XML parser context
//--------------------------------------------------
void dencDecryptSaxCleanup(DEncDecryptParse* pctx)
{
  ddocMemBuf_free(&(pctx->mbufTransportKey));
  ddocMemBuf_free(&(pctx->mbufTemp));
  if(pctx->pCert)
    X509_free(pctx->pCert);
  if(pctx->hOutFile)
    fclose(pctx->hOutFile);
  memset(pctx, 0, sizeof(DEncDecryptParse));
}

//--------------------------------------------------
// dencDecryptStartElementHandler:
// @ctxt:  An XML parser context
// @name:  The element name
// called when an opening tag has been processed.
//--------------------------------------------------
static void dencDecryptStartElementHandler(void *ctx, const xmlChar *name, const xmlChar **atts)
{
  DEncDecryptParse* pctx = (DEncDecryptParse*)ctx;
  ElementEntry* pCurrElem = 0;
    char *mime = NULL;
    int i;
    
    // check the atributes
    for(i = 0; atts && atts[i] && atts[i+1]; i++) {
        if(!strcmp((char*)atts[i], "MimeType")) {
            mime = (char*)atts[i+1];
        }
    }
  ddocDebug(4, "dencDecryptStartElementHandler", "<%s>, err: %d", (const char*)name, pctx->errcode);
  if(pctx->errcode) return; // if error skip all additional parsing
  pctx->errcode = ddocStackPushElementSAX(&(pctx->dencStack), name, atts, &pCurrElem);
  // initialize decoding and decryption
  if(pCurrElem && !strcmp((const char*)pCurrElem->tag, "CipherValue") &&
     !ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedKey", NULL)) {    
    if(pctx->nB64SkipMode == 0) {
      ddocDebug(4, "dencDecryptStartElementHandler", "Decode init");
	  pctx->ectx = EVP_ENCODE_CTX_new();
	  EVP_DecodeInit(pctx->ectx);
	  EVP_CIPHER_CTX_init(pctx->dctx);
      pctx->lB64Len = pctx->lBinLen = pctx->lDecLen = 0;
    }
    pctx->nB64SkipMode++; // increment skip mode
    ddocDebug(4, "dencDecryptStartElementHandler", "Decode start, skip: %d", pctx->nB64SkipMode);
  }
  // check mime
  if(strstr((char*)name, "EncryptedData")) {
        
  }
  // <X509Certificate>
  if(strstr((char*)name, "X509Certificate")) {
      ddocDebug(4, "dencDecryptStartElementHandler", "Start collecting cert");
      ddocMemBuf_free(&(pctx->mbufTemp));
  }
}

//--------------------------------------------------
// dencDecryptEndElementHandler:
// @ctxt:  An XML parser context
// @name:  The element name
// called when the end of an element has been detected.
//--------------------------------------------------
static void dencDecryptEndElementHandler(void *ctx, const xmlChar *name)
{
  DEncDecryptParse* pctx = (DEncDecryptParse*)ctx;
  char buf1[4096], buf2[4096];
  int l1, l2, l3;
    DigiDocMemBuf mbuf1;
    
    mbuf1.pMem = NULL;
    mbuf1.nLen = 0;
  ddocDebug(4, "dencDecryptEndElementHandler", "</%s>, err: %d", (const char*)name, pctx->errcode);
  if(pctx->errcode) return; // if error skip all additional parsing
  // decode the certificate data
  if(strstr((char*)name, "X509Certificate")) {
    pctx->errcode = ddocDecodeX509PEMData(&(pctx->pCert), (const char*)pctx->mbufTemp.pMem, (int)pctx->mbufTemp.nLen);
    ddocDebug(4, "dencDecryptEndElementHandler", "Decoding pem: %d cert: %s, rc: %d", 
	      pctx->mbufTemp.nLen, (pctx->pCert ? "OK" : "NULL"), pctx->errcode);
    ddocMemBuf_free(&(pctx->mbufTemp));
    //EVP_DecodeFinal(&(pctx->ectx),out,outl)
    pctx->nB64SkipMode = 0;
  }
  // check if it was the right key and decrypt transport key 
  if(strstr((char*)name, "EncryptedKey")) {
    memset(buf1, 0, sizeof(buf1));
    pctx->errcode = ReadCertSerialNumber(buf1, sizeof(buf1), pctx->pCert);
      ddocCertGetDN(pctx->pCert, &mbuf1, 0);
    ddocDebug(4, "dencDecryptEndElementHandler", "Looking for cert: %s, found: %s - %s, rc: %d", 
	      pctx->szCertSerial, buf1, (char*)mbuf1.pMem, pctx->errcode);
      ddocMemBuf_free(&mbuf1);
    if(!strcmp(pctx->szCertSerial, buf1)) {
      l1 = sizeof(buf1);
      memset(buf1, 0, l1);
      decode((const byte*)pctx->mbufTemp.pMem, pctx->mbufTemp.nLen, (byte*)buf1, &l1);
      ddocDebug(4, "dencDecryptEndElementHandler", "Decoded key-len: %d got: %d", 
		pctx->mbufTemp.nLen, l1);
      // cleanup temp buffer
      ddocMemBuf_free(&(pctx->mbufTemp));
      if(l1 < DENC_ENCRYPTED_KEY_LEN) {
	SET_LAST_ERROR(ERR_DENC_DECRYPT);
	pctx->errcode = ERR_DENC_DECRYPT;
	return;
      }
      // decrypt the transport key
      pctx->mbufTransportKey.nLen = l1; 
      pctx->mbufTransportKey.pMem = (char*)malloc(l1);
      if(!pctx->mbufTransportKey.pMem) {
	SET_LAST_ERROR(ERR_BAD_ALLOC);
	pctx->errcode = ERR_BAD_ALLOC;
	return;
      }
      memset(pctx->mbufTransportKey.pMem, 0, l1);
      l3 = pctx->mbufTransportKey.nLen;
      if(pctx->pkey) {
#if OPENSSL_VERSION_NUMBER > 0x10000000
          l3 = EVP_PKEY_decrypt_old((unsigned char *)pctx->mbufTransportKey.pMem, 
                                 (const unsigned char*)buf1, l1, pctx->pkey);
#else
        l3 = EVP_PKEY_decrypt((unsigned char *)pctx->mbufTransportKey.pMem, 
                                 (const unsigned char*)buf1, l1, pctx->pkey);
#endif
        pctx->mbufTransportKey.nLen = l3;
        if(l3 != 16)
            pctx->errcode = ERR_DENC_DECRYPT;
      } else {
        pctx->errcode = decryptWithEstID(pctx->nSlot, pctx->szPin, (char *)buf1, l1,
				       (char*)pctx->mbufTransportKey.pMem, &l3);
        pctx->mbufTransportKey.nLen = l3;
      }
      ddocDebug(4, "dencDecryptEndElementHandler", "Decrypted key-len: %d rc: %d", 
		pctx->mbufTransportKey.nLen, pctx->errcode);
      if(pctx->mbufTransportKey.nLen != DENC_DECRYPTED_KEY_LEN) {
	SET_LAST_ERROR(ERR_DENC_DECRYPT);
	pctx->errcode = ERR_DENC_DECRYPT;
	return;
      }
    }
  }
  // last block of encrypted data
  if(strstr((char*)name, "CipherValue") &&
     !ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedKey", NULL)) {
    ddocDebug(4, "dencDecryptEndElementHandler", "Decode end, skip: %d", pctx->nB64SkipMode);
    if(pctx->nB64SkipMode > 0)
      pctx->nB64SkipMode--;
    if(pctx->nB64SkipMode == 0) {
      l1 = sizeof(buf1);
      memset(buf1, 0, l1);
      ddocDebug(4, "dencDecryptEndElementHandler", "Decoding: final into: %d", l1);
	  EVP_DecodeFinal(pctx->ectx, (unsigned char*)buf1, &l1);
	  EVP_ENCODE_CTX_free(pctx->ectx);
      pctx->lBinLen += l1;
      ddocDebug(4, "dencDecryptEndElementHandler", "Decoded: final got: %d, total %d -> %d", l1, pctx->lB64Len, pctx->lBinLen);
      // decrypt decoded data
      l2 = sizeof(buf2);
      memset(buf2, 0, l2);
      ddocDebug(3, "dencDecryptEndElementHandler", "Decrypting: final into: %d", l2);
	  EVP_CipherFinal_ex(pctx->dctx, (unsigned char*)buf2, &l2);
	  EVP_CIPHER_CTX_free(pctx->dctx);
      ddocDebug(4, "dencDecryptEndElementHandler", "Decrypted: final got: %d", l2);
      // write to file
      if(pctx->hOutFile) {
	if(l2)
	  pctx->lDecLen += fwrite(buf2, 1, l2, pctx->hOutFile);
	fclose(pctx->hOutFile);
	pctx->hOutFile = 0;
      }
      ddocDebug(3, "dencDecryptEndElementHandler", "Total base64: %d decoded: %d decrypted: %d RC: %d", 
	      pctx->lB64Len, pctx->lBinLen, pctx->lDecLen, pctx->errcode);
      pctx->nB64SkipMode = 0;
    }
  }
  // pop stack
  pctx->errcode = ddocStackPopElement(&(pctx->dencStack), 0, NULL);
}

//--------------------------------------------------
// dencDecryptCharactersHandler:
// @ctxt:  An XML parser context
// @ch:  a xmlChar string
// @len: the number of xmlChar
// receiving some chars from the parser.
//--------------------------------------------------
static void dencDecryptCharactersHandler(void *ctx, const xmlChar *ch, int len)
{
  DEncDecryptParse* pctx = (DEncDecryptParse*)ctx;
  ElementEntry* pCurrElem = 0;
  char *buf1=0, *buf2=0, *p1=0;
  int l1, l2, i, l;

  ddocDebug(4, "dencDecryptCharactersHandler", "Parsing: %d chars err: %d, skip: %d", len, pctx->errcode, pctx->nB64SkipMode);
  if(pctx->errcode) return; // if error skip all additional parsing
  // find last element
  pCurrElem = ddocStackFindEnd(&(pctx->dencStack));
  // if this is data belonging to <CipherValue> tag and
  // the latter is not a child of <EncryptedKey>, thus
  // it must be the main content, then decrypt it
  if(pCurrElem && !strcmp((const char*)pCurrElem->tag, "X509Certificate")) {
    // collect the certificate data
    pctx->errcode = ddocMemAppendData(&(pctx->mbufTemp), (char*)ch, len);
  }
  if(pCurrElem || pctx->nB64SkipMode > 0) {
    // handle encrypted data
    if((pCurrElem && !strcmp((const char*)pCurrElem->tag, "CipherValue")) || pctx->nB64SkipMode > 0) {
      // collect encrypted transport key data
      if(pCurrElem && 
	 ddocStackHasParentWithName(&(pctx->dencStack), (xmlChar*)"EncryptedKey", NULL)) {
	pctx->errcode = ddocMemAppendData(&(pctx->mbufTemp), (char*)ch, len);
      }
      // if this is the real encrypted content not a key then decrypt it
      else {
	// check if the transport key is ready for decryption
	if(pctx->mbufTransportKey.nLen != DENC_DECRYPTED_KEY_LEN) {
	  ddocDebug(1, "dencDecryptCharactersHandler", "Transport key len: %d", pctx->mbufTransportKey.nLen);
	  SET_LAST_ERROR(ERR_DENC_NO_KEY_FOUND); // not encrypted for this user!
	  pctx->errcode = ERR_DENC_NO_KEY_FOUND;
	  return;
	}
	// decode base64 encrypted data
	pctx->lB64Len += len;
	l1 = len * 2;
	buf1 = (char*)malloc(l1);
	if(!buf1) {
	  SET_LAST_ERROR(ERR_BAD_ALLOC);
	  return;
	}
	memset(buf1, 0, l1);
	ddocDebug(4, "dencDecryptCharactersHandler", "Decoding: %d into: %d, skip: %d", len, l1, pctx->nB64SkipMode);
	EVP_DecodeUpdate(pctx->ectx, (unsigned char*)buf1, &l1, (unsigned char*)ch, len);
    ddocDebug(4, "dencDecryptCharactersHandler", "Decoded: %d got: %d, skip: %d", len, l1, pctx->nB64SkipMode);
	// if this was the first block of decoded base64 data 
	// then use the first 16 bytes as the IV value
	p1 = buf1;
	if(pctx->lBinLen == 0) {
	  ddocDebug(4, "dencDecryptCharactersHandler", "Using 16 bytes for IV. Initing cipher");
	  p1 += 16; // don't decrypt the IV data
	  l1 -= 16;
	  pctx->dctx = EVP_CIPHER_CTX_new();
	  EVP_CipherInit_ex(pctx->dctx, EVP_aes_128_cbc(), NULL,
			(const unsigned char*)pctx->mbufTransportKey.pMem, (const unsigned char*)buf1, DECRYPT);
	}
	pctx->lBinLen += l1;
	ddocDebug(4, "dencDecryptCharactersHandler", "Decoded: %d got: %d, skip: %d", len, l1, pctx->nB64SkipMode);
	// decrypt decoded data
	l = l2 = l1 * 2;
	buf2 = (char*)malloc(l2);
	if(!buf2) {
	  SET_LAST_ERROR(ERR_BAD_ALLOC);
	  return;
	}
	memset(buf2, 0, l2);
	//if(pctx->nB64SkipMode == 4)
	//  l1 += 16; // ???
	ddocDebug(4, "dencDecryptCharactersHandler", "Decrypting: %d into: %d", l1, l2);
	EVP_CipherUpdate(pctx->dctx, (unsigned char*)buf2, &l, (const unsigned char*)p1, l1);
	ddocDebug(4, "dencDecryptCharactersHandler", "Decrypted: %d got: %d, skip: %d", l1, l, pctx->nB64SkipMode);
    if(buf1)
	free(buf1); 

	// no padding until the final chunk
	if((pctx->nB64SkipMode == 0 || pctx->nB64SkipMode == 4)) {
	  // on the last block check for a block with all 0x0F
	  l1 = (int)(unsigned char)buf2[l-1];
	  if(l1 == 16) {
	    ddocDebug(4, "dencDecryptCharactersHandler", "Check 0x0F padding 1: %d", l1);
	    for(i = l - l1; i < l - 1; i++) {
	      ddocDebug(4, "dencDecryptCharactersHandler", "Pad pos: %d = %d", i, buf2[i]);
	      if(buf2[i] != 16) {
		l1 = 0; // set not matched flag
		break;
	      }
	    }
	    if(l1) {
	      ddocDebug(4, "dencDecryptCharactersHandler", "Decrypted len: %d reduce by: %d -> %d", l2, l1, (l-l1));
	      l -= l1;
	    }
	  }
	  // remove padding
	  l1 = (int)(unsigned char)buf2[l-1];
	  if(l1 > 0 && l1 <= 16) {
	    ddocDebug(4, "dencDecryptCharactersHandler", "Check padding: %d", l1);
	    for(i = l - l1; i < l - 1; i++) {
	      ddocDebug(4, "dencDecryptCharactersHandler", "Pad pos: %d = %d", i, buf2[i]);
	      if(buf2[i]) {
		l1 = 0; // set not matched flag
		break;
	      }
	    }
	    if(l1) {
	      ddocDebug(4, "dencDecryptCharactersHandler", "Decrypted len: %d reduce by: %d -> %d", l2, l1, (l-l1));
	      l -= l1;
	    }
	  }
	  else
	    ddocDebug(4, "dencDecryptCharactersHandler", "Impossible padding: %d", l1);
	  if(pctx->nB64SkipMode == 4)
	    pctx->nB64SkipMode = 1; // reset flag - look for padding
	}
	// write to file
	if(pctx->hOutFile && buf2) 
	  pctx->lDecLen += fwrite(buf2, 1, l, pctx->hOutFile);
	free(buf2);
      }
      
    }

  }

  ddocDebug(5, "dencDecryptCharactersHandler", "End");
}


xmlSAXHandler dencDecryptSAXHandlerStruct = {
    NULL, //internalSubsetHandler,
    NULL, //isStandaloneHandler,
    NULL, //hasInternalSubsetHandler,
    NULL, //hasExternalSubsetHandler,
    NULL, //resolveEntityHandler,
    NULL, //getEntityHandler,
    NULL, //entityDeclHandler,
    NULL, //notationDeclHandler,
    NULL, //attributeDeclHandler,
    NULL, //elementDeclHandler,
    NULL, //unparsedEntityDeclHandler,
    NULL, //setDocumentLocatorHandler,
    NULL, //startDocumentHandler,
    NULL, //endDocumentHandler,
    dencDecryptStartElementHandler,
    dencDecryptEndElementHandler,
    NULL, //referenceHandler,
    dencDecryptCharactersHandler,
    NULL, //ignorableWhitespaceHandler,
    NULL, //processingInstructionHandler,
    NULL, //commentHandler,
    dencWarningHandler,
    dencErrorHandler,
    dencFatalErrorHandler,
    NULL, //getParameterEntityHandler,
    dencCdataBlockHandler,
    NULL, //externalSubsetHandler,
    1
};


xmlSAXHandlerPtr dencDecryptSAXHandler = &dencDecryptSAXHandlerStruct;

// string used to force parser to flush it's buffers
static char g_szCipherValueFlush1[] = "</denc:CipherValue>";
static char g_szCipherValueFlush2[] = "<denc:CipherValue>";

//--------------------------------------------------
// Decrypts an encrypted XML document and stores the
// cleartext data in another document.
// szInputFileName - input file name [REQUIRED]
// szOutputFileName - output file name [REQUIRED]
// szPin - PIN1 of the id-card to decrypt the transport key [REQUIRED]
// szPkcs12File - pkcs12 key container filename [OPTIONAL]
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int dencSaxReadDecryptFile(const char* szInputFileName, 
				      const char* szOutputFileName, 
				      const char* szPin, const char* szPkcs12File)
{
  FILE *f;
  int ret;
  char chars[1025], convInFileName[256], convOutFileName[256], *p;
  xmlParserCtxtPtr ctxt;
  DEncDecryptParse pctx;
  X509* pCert = 0;
  EVP_PKEY *pkey = NULL;
    DigiDocMemBuf mbuf1;
    
    mbuf1.pMem = NULL;
    mbuf1.nLen = 0;
  RETURN_IF_NULL_PARAM(szInputFileName)
  RETURN_IF_NULL_PARAM(szOutputFileName)
  RETURN_IF_NULL_PARAM(szPin)
  clearErrors();
  ddocDebug(3, "dencSaxReadDecryptFile", "input-file: %s, output-file: %s", 
	    szInputFileName, szOutputFileName);
  memset(&pctx, 0, sizeof(pctx));

  ddocConvertFileName(convInFileName, sizeof(convInFileName), szInputFileName);
  ddocConvertFileName(convOutFileName, sizeof(convOutFileName), szOutputFileName);

  //store decryption params
  pctx.nSlot = ConfigItem_lookup_int("DIGIDOC_AUTH_KEY_SLOT", 0);
  pctx.szPin = (char*)szPin;
  pctx.hOutFile = fopen(convOutFileName, "wb");
  pctx.nB64SkipMode = 0;
  if(!pctx.hOutFile) {
    ddocDebug(1, "dencSaxReadDecryptFile", "Error writing to file: %s", szOutputFileName);
    SET_LAST_ERROR_RETURN(ERR_FILE_WRITE, ERR_FILE_WRITE)
  }
  // if using pkcs12 file for decryption
  if(szPkcs12File) {
    pctx.errcode = ReadCertificateByPKCS12(&pCert, szPkcs12File, szPin, &pkey);
    pctx.pkey = pkey;
  } else {
    pctx.errcode = findUsersCertificate(pctx.nSlot, &pCert);
  }
  if(pCert) {
    pctx.errcode = ReadCertSerialNumber(pctx.szCertSerial, sizeof(pctx.szCertSerial), pCert);
      ddocCertGetDN(pCert, &mbuf1, 0);
    ddocDebug(3, "dencSaxReadDecryptFile", "Decrypting using certificate: %s - %s", pctx.szCertSerial, (char*)mbuf1.pMem);
      ddocMemBuf_free(&mbuf1);
  } else {
    ddocDebug(1, "dencSaxReadDecryptFile", "Users cert for decryption could not be read from card!");
    return pctx.errcode;
  }
      
  // parse and decrypt data in file
  if((f = fopen(convInFileName, "r")) != NULL && !pctx.errcode) {
    ret = fread(chars, 1, 1024, f);
    if (ret > 0) {
      ctxt = xmlCreatePushParserCtxt(dencDecryptSAXHandler, &pctx,
				     chars, ret, convInFileName);
      do {
	memset(chars, 0, sizeof(chars));
	ret = fread(chars, 1, 1024, f);
	if(ret == 0)
	  ret = strlen(chars);
	ddocDebug(4, "dencSaxReadDecryptFile", "In: %d Parsed: %d, skip: %d", ret, ctxt->nbChars, pctx.nB64SkipMode);
	// this horrible chemistry is done to prevent
	// libxml2 to start colecting huge memory structures
	// Since we cannot disable it we'll just bypass parser
	// with a large amount of base64 data
	if(pctx.nB64SkipMode > 0) {
	  p = strchr(chars, '<');
	  // if <CipherValue> was found and no "<" (beginn of new element)
	  // is found then send the "flush command" and enter bypass mode
	  if(pctx.nB64SkipMode == 1 && !p) {
	    pctx.nB64SkipMode += 2;
	    ddocDebug(4, "dencSaxReadDecryptFile", "Starting bypass mode, skip: %d", pctx.nB64SkipMode);
	    // force the parser to release element content 
	    // before entering into bypass mode
	    xmlParseChunk(ctxt, g_szCipherValueFlush1, strlen(g_szCipherValueFlush1), 0);
	    ddocDebug(4, "dencSaxReadDecryptFile", "Entering bypass mode, skip: %d", pctx.nB64SkipMode);
	  }
	  if(pctx.nB64SkipMode >= 2 && !p) {
	    ddocDebug(4, "dencSaxReadDecryptFile", "Parsing in bypass mode, skip: %d", pctx.nB64SkipMode);
	    dencDecryptCharactersHandler(&pctx, (const xmlChar *)chars, ret);
	  }
	  // if we reached the first block that contains a start of xml element
	  // then stop bypass mode
	  if(strchr(chars, '<')) {
	    pctx.nB64SkipMode = 2;
	    xmlParseChunk(ctxt, g_szCipherValueFlush2, strlen(g_szCipherValueFlush2), 0);
	    ddocDebug(4, "dencSaxReadDecryptFile", "Ending bypass mode len: %d, skip: %d", ret, pctx.nB64SkipMode);
	  }
	} // if bypass mode
	// if in normal mode or finished bypass mode
	if(pctx.nB64SkipMode == 0 || pctx.nB64SkipMode == 3) {
	  if(pctx.nB64SkipMode == 3)
	    pctx.nB64SkipMode = 4; // used as flag: last block - look for padding
	  ddocDebug(4, "dencSaxReadDecryptFile", "parsing normal chunk");
	  xmlParseChunk(ctxt, chars, ret, 0);	  
	}
	//memset(chars, 0, sizeof(chars));
      } while(ret == 1024);  // do-while
      // last block of data
      ddocDebug(4, "dencSaxReadDecryptFile", "parsing final chunk: %d", strlen(chars));
        if(!pctx.errcode)
      xmlParseChunk(ctxt, NULL, 0, 1);
      xmlFreeParserCtxt(ctxt);
    } // if(ret > 0)
    fclose(f);
  } else {
    ddocDebug(1, "dencSaxReadDecryptFile", "Error reading file: %s", szInputFileName);
    SET_LAST_ERROR_RETURN_CODE(ERR_FILE_READ);
  }
  // cleanup stack
  ret = pctx.errcode = ddocStackPopElement(&(pctx.dencStack), 1, NULL);
  // cleanup
  dencDecryptSaxCleanup(&pctx);
  ddocDebug(1, "dencSaxReadDecryptFile", 
	    "End parsing file: %s - RC: %d", szInputFileName, ret);
  return ret;
}