File: xmlenc-dec.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (2096 lines) | stat: -rw-r--r-- 63,650 bytes parent folder | download | duplicates (2)
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
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
/*
 *  xmlenc-dec.c
 *
 *  XML Encryption spec part 2 - decryption
 *
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#ifdef _SSL

#include "wi.h"
#include "sqlnode.h"
#include "sqlbif.h"
#include "xml.h"
#include "xmlgen.h"
#include "xmltree.h"
#include "xmlenc.h"
#include "xml_ecm.h"
#include "soap.h"
#include "security.h"
#include "date.h"
#include "bif_text.h"

#ifdef DEBUG
#include "xmlenc_test.h"
#endif


#define WSSE_UNKNOWN_CODE			"000"
#define WSSE_UNKNOWN_ERR			"unknown error"

#define WSSE_NO_ALGORITHM_CODE			"100"
#define WSSE_NO_ALGORITHM_ERR			"no algorithm specified"

#define WSSE_UNKNOWN_DIGEST_ALGORITHM_CODE	"101"
#define WSSE_UNKNOWN_DIGEST_ALGORITHM_ERR	"unknown digest algorithm %s"

#define WSSE_DIGEST_VALUE_UNINIT_CODE		"102"
#define WSSE_DIGEST_VALUE_UNINIT_ERR		"digest value is not specified"

#define WSSE_EMPTY_XPATH_EXPRESSION_CODE	"104"
#define WSSE_EMPTY_XPATH_EXPRESSION_ERR		"no XPath expression specified"

#define WSSE_EMPTY_SIGNATURE_VALUE_CODE		"105"
#define WSSE_EMPTY_SIGNATURE_VALUE_ERR		"signature value is not specified"

#define WSSE_CALLBACK_NOT_FOUND_CODE		"106"
#define WSSE_CALLBACK_NOT_FOUND_ERR		"tag is unknown %s"

#define WSSE_UNKNOWN_URI_CODE			"107"
#define WSSE_UNKNOWN_URI_ERR			"unknown namespace in security xml"

#define WSSE_UNKNOWN_CANON_ALGORITHM_CODE	"108"
#define WSSE_UNKNOWN_CANON_ALGORITHM_ERR	"unknown canonicalization algorithm %s"

#define WSSE_UNKNOWN_SIGN_ALGORITHM_CODE	"109"
#define WSSE_UNKNOWN_SIGN_ALGORITHM_ERR		"unknown signature algorithm %s"

#define WSSE_NO_TAG_CODE			"110"
#define WSSE_NO_TAG_ERR				"desired tag %s is not presented"

#define WSSE_ALGO_CODE				"111"
#define WSSE_ALGO_ERR				"algorithm %s internal error (key is not private?)"

#define WSSE_WRONG_SIGNVAL_CODE			"112"
#define WSSE_WRONG_SIGNVAL_ERR			"signature has wrong SignValue"

#define WSSE_EMPTY_CVAL_VALUE_CODE		"113"
#define WSSE_EMPTY_CVAL_VALUE_ERR		"no cipher value specified"

#define WSSE_NO_URI_CODE			"114"
#define WSSE_NO_URI_ERR				"no URI specified"

#define WSSE_EMPTYNAME_CODE			"115"
#define WSSE_EMPTYNAME_ERR			"no key name specified"

#define WSSE_NO_ENC_KEY				"116"
#define WSSE_CORRUPTED_ENC_KEY			"117"
#define WSSE_XML_CODE				"118"
#define WSSE_EMPTY_RSA_MODULUS_CODE		"119"
#define WSSE_EMPTY_RSA_EXPONENT_CODE		"120"
#define WSSE_UNKNOWN_KEY_CODE			"121"
#define WSSE_WRONG_KEY_CODE			"122"
#define WSSE_UNENCRYPTED_KEY_CODE		"123"
#define WSSE_ALGO_EMPTY_KEY_CODE		"124"
#define WSSE_UNRESOLVED_REF_URIS_CODE		"125"
#define WSSE_UNRESOLVED_REF_URI_CODE		"126"
#define WSSE_ENCKEY_REF_URI_NO_KEYINFO_CODE	"127"
#define WSSE_ENCKEY_REF_URI_NO_KEYNAME_CODE	"128"
#define WSSE_ENCKEY_REF_URI_EMPTY_KEYNAME_CODE	"129"
#define WSSE_BINARYSECTOKEN_CODE		"130"
#define WSSE_BINARYSECTOKENVALTYPE_CODE		"131"
#define WSSE_BINARYSECTOKENREF_CODE		"132"

struct wsse_error_templ_s
{
  char *		code;
  char *		templ;
} wsse_error_templs[] =
  {
    { WSSE_UNKNOWN_CODE, WSSE_UNKNOWN_ERR},
    { WSSE_NO_ALGORITHM_CODE, WSSE_NO_ALGORITHM_ERR },
    { WSSE_UNKNOWN_DIGEST_ALGORITHM_CODE, WSSE_UNKNOWN_DIGEST_ALGORITHM_ERR },
    { WSSE_DIGEST_VALUE_UNINIT_CODE, WSSE_DIGEST_VALUE_UNINIT_ERR },
    { WSSE_EMPTY_XPATH_EXPRESSION_CODE, WSSE_EMPTY_XPATH_EXPRESSION_ERR},
    { WSSE_EMPTY_SIGNATURE_VALUE_CODE, WSSE_EMPTY_SIGNATURE_VALUE_ERR},
    { WSSE_CALLBACK_NOT_FOUND_CODE, WSSE_CALLBACK_NOT_FOUND_ERR},
    { WSSE_UNKNOWN_URI_CODE, WSSE_UNKNOWN_URI_ERR},
    { WSSE_UNKNOWN_CANON_ALGORITHM_CODE, WSSE_UNKNOWN_CANON_ALGORITHM_ERR },
    { WSSE_UNKNOWN_SIGN_ALGORITHM_CODE, WSSE_UNKNOWN_SIGN_ALGORITHM_ERR },
    { WSSE_NO_TAG_CODE, WSSE_NO_TAG_ERR},
    { WSSE_ALGO_CODE, WSSE_ALGO_ERR},
    { WSSE_WRONG_SIGNVAL_CODE, WSSE_WRONG_SIGNVAL_ERR},
    { WSSE_EMPTY_CVAL_VALUE_CODE, WSSE_EMPTY_CVAL_VALUE_ERR},
    { WSSE_NO_URI_CODE, WSSE_NO_URI_ERR},
    { WSSE_EMPTYNAME_CODE, WSSE_EMPTYNAME_ERR},
    { WSSE_NO_ENC_KEY, "no encrypted key restored" },
    { WSSE_CORRUPTED_ENC_KEY, "encrypted key restored with error at %s" },
    { WSSE_XML_CODE, "could not parser signature xml" },
    { WSSE_EMPTY_RSA_MODULUS_CODE, "RSA modulus is empty" },
    { WSSE_EMPTY_RSA_EXPONENT_CODE, "RSA exponent is empty" },
    { WSSE_UNKNOWN_KEY_CODE, "key %s is unknown"},
    { WSSE_WRONG_KEY_CODE, "key %s could not be used by method %s"},
    { WSSE_UNENCRYPTED_KEY_CODE, "Unencrypted key declaration is not allowed"},
    { WSSE_ALGO_EMPTY_KEY_CODE, "algorithm %s internal error (no key provided?)"},
    { WSSE_UNRESOLVED_REF_URIS_CODE, "document contains unresolved references to encrypted data"},
    { WSSE_UNRESOLVED_REF_URI_CODE, "reference %s to encrypted data is unresolved"},
    { WSSE_ENCKEY_REF_URI_NO_KEYINFO_CODE, "EncryptedData does not contain key info [%s]"},
    { WSSE_ENCKEY_REF_URI_NO_KEYNAME_CODE, "EncryptedData/KeyInfo does not contain key name [%s]"},
    { WSSE_ENCKEY_REF_URI_EMPTY_KEYNAME_CODE, "EncryptedData/KeyInfo/KeyName is empty [%s]"},
    { WSSE_BINARYSECTOKEN_CODE, "error in BinarySecurityToken tag: %s"},
    { WSSE_BINARYSECTOKENVALTYPE_CODE, "error in SecurityTokenReference tag: value type %s is not supported"},
    { WSSE_BINARYSECTOKENREF_CODE, "Unknown binary security token referenced by key identifier %s"}
  };

ptrlong wsse_error_templs_len = sizeof (wsse_error_templs) / sizeof (struct wsse_error_templ_s);


#define XML_ELEMENT_NAME(x) \
  ((char *)( ((x) && DV_TYPE_OF (x) == DV_ARRAY_OF_POINTER && ((caddr_t *)(x))[0] && ((caddr_t **)(x))[0][0]) ? ((caddr_t **)(x))[0][0] : NULL))

#define XML_ELEMENT_ATTR_COUNT(x) \
    ( \
      ( \
	(x) && \
	DV_TYPE_OF (x) == DV_ARRAY_OF_POINTER && \
	((caddr_t *)x)[0] && \
	DV_TYPE_OF (((caddr_t *)x)[0]) == DV_ARRAY_OF_POINTER \
      ) \
      ? \
      (BOX_ELEMENTS (((caddr_t *)x)[0]) - 1) / 2 \
      : \
      0 \
    )

#define XML_ELEMENT_ATTR_NAME(x, n) \
    ( \
      XML_ELEMENT_ATTR_COUNT(x) >= n \
      ? \
      ((caddr_t **)x)[0][n * 2 + 1] \
      : \
      NULL \
    )

#define XML_ELEMENT_ATTR_VALUE(x, n) \
    ( \
      XML_ELEMENT_ATTR_COUNT(x) >= n \
      ? \
      ((caddr_t **)x)[0][n * 2 + 2] \
      : \
      NULL \
    )

#define XML_ELEMENT_CHILD(x, n) \
  ( ((x) && DV_TYPE_OF (x) == DV_ARRAY_OF_POINTER && BOX_ELEMENTS (x) > (n + 1) && \
     ((caddr_t *)(x))[n + 1]) ? \
       ((caddr_t **)(x))[n + 1] : \
       NULL)

/* temp declarations */
/* http.c */
int decode_base64 (char *src, char *end);
/* xmlenc_algos.c */
int xenc_decode_base64 (char *src, char *end);


static void __dbg_wsse_assert (char * file, long line)
{
  char buf[1024];
  snprintf (buf, sizeof (buf), "WSSE Assert failed at %s:%ld", file, line);
  GPF_T1 (buf);
}

#define WSSE_ASSERT(check) if (!check) __dbg_wsse_assert (__FILE__, __LINE__)

wsse_ctx_t * wsse_ctx_allocate ()
{
  NEW_VARZ (wsse_ctx_t, ctx);
  ctx->wc_id_cache = id_hash_allocate (31, sizeof (caddr_t), sizeof (caddr_t*),
					strhash, strhashcmp);
  return ctx;
}

void wsse_ctx_free (wsse_ctx_t * ctx)
{
  if (ctx->wc_dsig)
    dsig_free (ctx->wc_dsig);
  if (ctx->wc_dec)
    {
      DO_SET (xenc_enc_key_t*, ek, &ctx->wc_dec->xed_keys)
	{
	  caddr_t *tree;
	  dk_free_box (ek->xeke_name);
	  dk_free_box (ek->xeke_enc_method);
	  dk_free_box (ek->xeke_super_key);
	  dk_free_box (ek->xeke_carried_key_name);
	  dk_free_box (ek->xeke_cipher_value);
#if 0
	  dk_set_t		xeke_refs; /* xenc_reference_t */
	  xenc_key_t *		xeke_encrypted_key;
#endif

	  tree = (caddr_t *) dk_set_to_array (ek->xeke_refs);
	  dk_free_tree ((box_t) tree);
	  dk_set_free (ek->xeke_refs);
	  dk_free (ek, sizeof (xenc_enc_key_t));
	}
      END_DO_SET();
      dk_set_free (ctx->wc_dec->xed_keys);
      dk_free (ctx->wc_dec, sizeof (xenc_dec_t));
    }
  if (ctx->wc_id_cache)
    {
      id_hash_iterator_t hit;
      caddr_t * id;
      caddr_t * keyname;
      for (id_hash_iterator (&hit, ctx->wc_id_cache);
	   hit_next (&hit, (caddr_t*) &id, (caddr_t*) &keyname);
	   /* nop */)
	{
	  if (id) dk_free_box (id[0]);
	  if (keyname) dk_free_box (keyname[0]);
	}
      id_hash_free (ctx->wc_id_cache);
    }

  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  dk_free (ctx, sizeof (wsse_ctx_t));
}

static
xenc_key_t * xenc_get_key_by_keyid (wsse_ctx_t * ctx, char * ref)
{
  id_hash_t * id_hash = ctx->wc_id_cache;
  caddr_t * keyname = (caddr_t *) id_hash_get (id_hash, (caddr_t) &ref);
  if (!keyname)
    {
      /* id */
      xenc_key_t ** k;
      mutex_enter (xenc_keys_mtx);
      k =  (xenc_key_t **) id_hash_get (xenc_certificates, (caddr_t) & ref);
      mutex_leave (xenc_keys_mtx);
      if (k)
	return k[0];
      return 0;
    }
  return xenc_get_key_by_name (keyname[0], 1);
}

typedef void (*wsse_callback_f) (char* uri, char* name, caddr_t * curr, wsse_ctx_t * ctx);

typedef struct wsse_callback_item_s
{
  char *		wsse_c_name;
  wsse_callback_f	wsse_c_callback;
} wsse_callback_item_t;


void wsse_canonmethod_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_digestmethod_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_digestvalue_callback  (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_keyinfo_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_keyname_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_keyvalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_reference_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_signature_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_signaturemethod_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_signaturevalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_signedinfo_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_transform_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_transforms_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_xpath_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_dsakeyvalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_p_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_q_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_g_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_y_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_rsakeyvalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_modulus_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_exponent_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_x509data_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_x509certificate_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);

/* MUST keep this list in alphabetical order */
static
wsse_callback_item_t wsse_dsig_callbacks [] =
{
  {"CanonicalizationMethod", wsse_canonmethod_callback},
  {"DSAKeyValue", wsse_dsakeyvalue_callback},
  {"DigestMethod", wsse_digestmethod_callback},
  {"DigestValue", wsse_digestvalue_callback},
  {"Exponent", wsse_exponent_callback},
  {"G", wsse_p_callback},
  {"KeyInfo", wsse_keyinfo_callback},
  {"KeyName", wsse_keyname_callback},
  {"KeyValue", wsse_keyvalue_callback},
  {"Modulus", wsse_modulus_callback},
  {"P", wsse_p_callback},
  {"Q", wsse_q_callback},
  {"RSAKeyValue", wsse_rsakeyvalue_callback},
  {"Reference", wsse_reference_callback},
  {"Signature", wsse_signature_callback},
  {"SignatureMethod", wsse_signaturemethod_callback},
  {"SignatureValue", wsse_signaturevalue_callback},
  {"SignedInfo", wsse_signedinfo_callback},
  {"Transform", wsse_transform_callback},
  {"Transforms", wsse_transforms_callback},
  {"X509Certificate", wsse_x509certificate_callback},
  {"X509Data", wsse_x509data_callback},
  {"XPath", wsse_xpath_callback},
  {"Y", wsse_p_callback}
};

static
ptrlong wsse_dsig_callbacks_len = sizeof (wsse_dsig_callbacks) / sizeof (wsse_callback_item_t);

void wsse_cipherdata_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_ciphervalue_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_datareference_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_encryptedkey_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_encryptionmethod_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wsse_referencelist_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);


static
wsse_callback_item_t wsse_xenc_callbacks [] =
{
  /*
     EncryptedKey,
     EncryptionMethod
     CipherData
     CipherValue
     ReferenceList
     DataReference
   */
  { "CipherData", wsse_cipherdata_c },
  { "CipherValue", wsse_ciphervalue_c },
  { "DataReference", wsse_datareference_c },
  { "EncryptedKey", wsse_encryptedkey_c },
  { "EncryptionMethod", wsse_encryptionmethod_c },
  { "ReferenceList", wsse_referencelist_c }
};

static
ptrlong wsse_xenc_callbacks_len = sizeof (wsse_xenc_callbacks) / sizeof (wsse_callback_item_t);

void wss_binarysectoken_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wss_reference_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wss_keyidentifier_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wss_security_c(char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wss_securitytokenref_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wss_usernametoken_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);
void wss_dummy_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx);

static
wsse_callback_item_t wsse_callbacks [] =
{
  { "BinarySecurityToken", wss_binarysectoken_c},
  { "KeyIdentifier", wss_keyidentifier_c},
  { "Nonce", wss_dummy_c },
  { "Password", wss_dummy_c },
  { "Reference", wss_reference_c },
  { "Security", wss_security_c },
  { "SecurityTokenReference", wss_securitytokenref_c },
  { "Username", wss_dummy_c },
  { "UsernameToken", wss_usernametoken_c },
};

static
ptrlong wsse_callbacks_len = sizeof (wsse_callbacks) / sizeof (wsse_callback_item_t);


caddr_t DBG_NAME(wsse_get_content_val) (DBG_PARAMS caddr_t * curr)
{
  if (BOX_ELEMENTS (curr) > 1)
    {
      caddr_t value = curr[1];
      if (DV_STRINGP (value))
	{
	  return DBG_NAME(box_dv_short_string) (DBG_ARGS value);
	}
    }
  return NULL;
}



void wsse_canonmethod_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * algoname = xml_find_attribute (curr, "Algorithm", 0);
  if (!algoname)
    wsse_report_error (ctx, WSSE_NO_ALGORITHM_CODE, 0);
  WSSE_ASSERT (ctx->wc_dsig);

  dsig_canon_f_get (algoname, &ctx->wc_tb);

  ctx->wc_dsig->dss_canon_method = box_dv_short_string (algoname);
}

void wsse_signaturemethod_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * algoname = xml_find_attribute (curr, "Algorithm", 0);
  if (!algoname)
    wsse_report_error (ctx, WSSE_NO_ALGORITHM_CODE, 0);
  WSSE_ASSERT (ctx->wc_dsig);

  dsig_sign_f_get (algoname, &ctx->wc_tb);

  ctx->wc_dsig->dss_signature_method = box_dv_short_string (algoname);
}

void wsse_digestmethod_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * algoname = xml_find_attribute (curr, "Algorithm", 0);
  if (!algoname)
    wsse_report_error (ctx, WSSE_NO_ALGORITHM_CODE, 0);
  WSSE_ASSERT (ctx->wc_curr_ref);

  dsig_digest_f_get (algoname, &ctx->wc_tb);

  ctx->wc_curr_ref->dsr_digest_method = box_dv_short_string (algoname);
}

void wsse_digestvalue_callback  (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  caddr_t res;
  WSSE_ASSERT (ctx->wc_curr_ref);

  res = wsse_get_content_val (curr);

  if (res)
    ctx->wc_curr_ref->dsr_digest = res;
  else if (!ctx->wc_allow_empty_vals)
    wsse_report_error (ctx, WSSE_DIGEST_VALUE_UNINIT_CODE, 0);

  /* after all transforms in reference */
  ctx->wc_curr_ref->dsr_transforms = dk_set_nreverse (ctx->wc_curr_ref->dsr_transforms);
}

void wsse_reference_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  dsig_reference_t * ref;
  char * URI;
  char * Id;
  char * Type;

  WSSE_ASSERT (ctx->wc_dsig);

  ref = (dsig_reference_t *) dk_alloc (sizeof (dsig_reference_t));
  memset (ref,0,sizeof (dsig_reference_t));

  ctx->wc_curr_ref = ref;

  URI = xml_find_attribute (curr, "URI", 0);
  Id = xml_find_attribute (curr, "Id", 0);
  Type = xml_find_attribute (curr, "Type", 0);

  ref->dsr_uri = box_dv_short_string (URI);
  ref->dsr_id = box_dv_short_string (Id);
  ref->dsr_type = box_dv_short_string (Type);

  if (URI && URI[0]) /* non empty URI */
    {
      NEW_VARZ (dsig_transform_t, tr);
      tr->dst_name = box_dv_short_string (DSIG_FAKE_URI_TRANSFORM_ALGO);
      tr->dst_data = box_dv_short_string (URI);
      dk_set_push (&ref->dsr_transforms, tr);
    }
  dk_set_push (&ctx->wc_dsig->dss_refs, (void*) ref);
}

void wsse_signedinfo_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
}

void wsse_transform_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * algoname = xml_find_attribute (curr, "Algorithm", 0);

  WSSE_ASSERT (ctx->wc_curr_ref);

  if (algoname)
    {
      NEW_VARZ (dsig_transform_t, tr);
      tr->dst_name = box_dv_short_string (algoname);
      ctx->wc_curr_transform = tr;
      dk_set_push (&ctx->wc_curr_ref->dsr_transforms, tr);
    }
}

void wsse_transforms_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  WSSE_ASSERT (ctx->wc_curr_ref);
}

void wsse_xpath_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  dsig_transform_t * tr = ctx->wc_curr_transform;
  WSSE_ASSERT (tr);

  tr->dst_data = wsse_get_content_val (curr);

  if (!tr->dst_data)
    wsse_report_error (ctx, WSSE_EMPTY_XPATH_EXPRESSION_CODE, 0);
}

void wsse_dsakeyvalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
}
void wsse_p_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{}
void wsse_q_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{}
void wsse_g_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{}
void wsse_y_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{}

void wsse_rsakeyvalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
#if 1
  return;
#else
  xenc_key_t * k = ctx->wc_key;
  WSSE_ASSERT (ctx->wc_dsig);
  WSSE_ASSERT (ctx->wc_key);

  k->xek_type = DSIG_KEY_RSA;
  k->xek_enc_algo =  xenc_algorithms_get (XENC_RSA_ALGO);
  k->xek_sign_algo =  xenc_algorithms_get (DSIG_RSA_SHA1_ALGO);
#endif
}
void wsse_modulus_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
#if 1
  return;
#else
  xenc_key_t * k = ctx->wc_key;
  char * mod;
  WSSE_ASSERT (ctx->wc_dsig);
  WSSE_ASSERT (ctx->wc_key);

  mod = wsse_get_content_val (curr);
  if (!mod)
    wsse_report_error (ctx, WSSE_EMPTY_RSA_MODULUS_CODE, 0);
  dk_free_box (mod);
#endif
}
void wsse_exponent_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
#if 1
  return;
#else
  xenc_key_t * k = ctx->wc_key;
  char * exp;
  WSSE_ASSERT (ctx->wc_dsig);
  WSSE_ASSERT (ctx->wc_key);

  exp = wsse_get_content_val (curr);
  if (!exp)
    wsse_report_error (ctx, WSSE_EMPTY_RSA_EXPONENT_CODE, 0);
  dk_free_box (exp);
#endif
}

void wsse_signature_callback (char * uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  NEW_VARZ (dsig_signature_t, d);
  ctx->wc_dsig = d;
  ctx->wc_object_type = XENC_T_DSIG;
}

void wsse_signaturevalue_callback (char * uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  dsig_signature_t * dsig = ctx->wc_dsig;
  WSSE_ASSERT (dsig);

  dsig->dss_signature = wsse_get_content_val (curr);
  if (!ctx->wc_allow_empty_vals && !dsig->dss_signature)
    wsse_report_error (ctx, WSSE_EMPTY_SIGNATURE_VALUE_CODE, 0);

  /* after all references */
  dsig->dss_refs = dk_set_nreverse (dsig->dss_refs);
}

void wsse_keyinfo_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  switch (ctx->wc_object_type)
    {
    case XENC_T_DSIG:
      {
	WSSE_ASSERT (ctx->wc_dsig);
	break;
      }
    case XENC_T_ENCKEY:
      {
	WSSE_ASSERT (ctx->wc_curr_enckey);
	break;
      }
    case XENC_T_ENCCTX:
      {
	ctx->wc_key = (xenc_key_t *) dk_alloc (sizeof (xenc_key_t));
	memset (ctx->wc_key, 0, sizeof (xenc_key_t));
	break;
      }
#if 1
    case XENC_T_SECURITY: /* session key */
      break;
#endif
    default:
      wsse_report_error (ctx, WSSE_UNENCRYPTED_KEY_CODE, 0);
    }
}

void wsse_keyname_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  switch (ctx->wc_object_type)
    {
    case XENC_T_DSIG:
      {
	caddr_t keyname = wsse_get_content_val (curr);
	xenc_key_t * key;
	if (!keyname)
	  wsse_report_error (ctx, WSSE_EMPTYNAME_CODE, 0);


	key = xenc_get_key_by_name (keyname, 1);
	if (!key)
	  {
	    char tmpbuf[128];
	    strncpy (tmpbuf, keyname, 128);
	    tmpbuf[127] = 0; /* if strlen (keyname) >= 128 */
	    dk_free_box (keyname);
	    wsse_report_error (ctx, WSSE_UNKNOWN_KEY_CODE, strlen (tmpbuf), tmpbuf);
	  }
	dk_free_box (keyname);
	if (strcmp (key->xek_sign_algo->xea_ns, ctx->wc_dsig->dss_signature_method))
	  {
	    wsse_report_error (ctx, WSSE_WRONG_KEY_CODE, strlen (key->xek_name)
			       + strlen (ctx->wc_dsig->dss_signature_method), key->xek_name,
			       ctx->wc_dsig->dss_signature_method);
	  }
	ctx->wc_dsig->dss_key = key;
      }
      break;
    case XENC_T_ENCKEY:
      {
	caddr_t keyname = wsse_get_content_val (curr);
	if (!keyname)
	  wsse_report_error (ctx, WSSE_EMPTYNAME_CODE, 0);

	WSSE_ASSERT (ctx->wc_curr_enckey);

	ctx->wc_curr_enckey->xeke_super_key = keyname;
      }
      break;
    case XENC_T_ENCCTX:
      {
	char * keyname = wsse_get_content_val (curr);
	GPF_T;
	WSSE_ASSERT (ctx->wc_key);

	if (!keyname)
	  wsse_report_error (ctx, WSSE_EMPTYNAME_CODE, 0);

	ctx->wc_key->xek_name = keyname;
      }
      break;
#if 1
    case XENC_T_SECURITY: /* session key */
      {
	char * keyname = wsse_get_content_val (curr);
	WSSE_ASSERT (ctx->wc_dec);

	if (!keyname)
	  wsse_report_error (ctx, WSSE_EMPTYNAME_CODE, 0);
	else if (!xenc_get_key_by_name (keyname, 1))
	  {
	    char keyn[128];
	    strncpy (keyn, keyname, 127);
	    keyn[127] = 0;
	    dk_free_box (keyname);
	    wsse_report_error (ctx, WSSE_UNKNOWN_KEY_CODE, 128, keyn);
	  }
	else
	  {
	    NEW_VARZ (xenc_enc_key_t , ek);

	    ctx->wc_curr_enckey = ek;
	    dk_set_push (&ctx->wc_dec->xed_keys, ek);
	    ek->xeke_name = keyname;
	  }
      }
      break;
#endif
    default:
      GPF_T;
    }
}

void wsse_keyvalue_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
}

void wsse_x509data_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  dsig_signature_t * dsig = ctx->wc_dsig;
  WSSE_ASSERT (ctx->wc_dsig);
  dsig->dss_key_value_type = XENC_T_X509_CERT;
}

void wsse_x509certificate_callback (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  caddr_t content = wsse_get_content_val (curr);
  xenc_key_t * key;
  if (!content)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "tag is empty");
  key = certificate_decode (content, WSSE_OASIS_X509_VALUE_TYPE, WSSE_OASIS_BASE64_ENCODING_TYPE);
  dk_free_box (content);
  if (!key)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "could not decode certificate");
  if (ctx->wc_object_type == XENC_T_DSIG)
    {
      WSSE_ASSERT (ctx->wc_dsig);
      ctx->wc_dsig->dss_key = key;
    }
  else if (ctx->wc_object_type == XENC_T_ENCKEY)
    {
      WSSE_ASSERT (ctx->wc_curr_enckey);
      ctx->wc_curr_enckey->xeke_super_key = box_dv_short_string (key->xek_name);
    }
}

void wsse_cipherdata_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  WSSE_ASSERT (ctx->wc_curr_enckey);
}

void wsse_ciphervalue_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  xenc_enc_key_t * ek = ctx->wc_curr_enckey;
  WSSE_ASSERT (ek);

  ek->xeke_cipher_value = wsse_get_content_val (curr);
  if (!ek->xeke_cipher_value)
    wsse_report_error (ctx, WSSE_EMPTY_CVAL_VALUE_CODE, 0);
}

void wsse_datareference_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  xenc_enc_key_t * ek = ctx->wc_curr_enckey;
  char * URI = xml_find_attribute (curr, "URI", 0);

  if (!URI)
    wsse_report_error (ctx, WSSE_NO_URI_CODE, 0);

  if (!ek) /* is not within EncryptedKey */
    {
      ek = (xenc_enc_key_t *) dk_alloc (sizeof (xenc_enc_key_t));
      memset (ek, 0, sizeof (xenc_enc_key_t));
      ek->xeke_is_raw = 1;

      WSSE_ASSERT (ctx->wc_dec);

      ctx->wc_curr_enckey = ek;
      dk_set_push (&ctx->wc_dec->xed_keys, ek);
      ctx->wc_object_type = XENC_T_ENCKEY;
    }
  dk_set_push (&ek->xeke_refs, box_dv_short_string (URI));
}


void wsse_encryptedkey_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  NEW_VARZ (xenc_enc_key_t , ek);

  WSSE_ASSERT (ctx->wc_dec);

  ctx->wc_curr_enckey = ek;
  dk_set_push (&ctx->wc_dec->xed_keys, ek);
  ctx->wc_object_type = XENC_T_ENCKEY;
}

void wsse_encryptionmethod_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * algo = xml_find_attribute (curr, "Algorithm", 0);

  WSSE_ASSERT (ctx->wc_curr_enckey);

  if (!algo)
    wsse_report_error (ctx, WSSE_NO_ALGORITHM_CODE, 0);

  ctx->wc_curr_enckey->xeke_enc_method = box_dv_short_string (algo);
}

void wsse_referencelist_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{

}

void wss_security_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  NEW_VARZ (xenc_dec_t, dec);

  ctx->wc_dec = dec;
#if 1
  ctx->wc_object_type = XENC_T_SECURITY;
#endif
}

void wss_binarysectoken_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * value_type = xml_find_attribute (curr, "ValueType", 0);
  char * encoding_type = xml_find_attribute (curr, "EncodingType", 0);
  char * id = xml_find_attribute (curr, "Id", 0);
  char * content;
  xenc_key_t * key;

  if (!value_type)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "no ValueType attribute");

  if (!encoding_type)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "no EncodingType attribute");

#if 0
  /* WSS spec: An optional string label for this security token */
  if (!id)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "no {wsse}:Id attribute");
#endif

  content = wsse_get_content_val (curr);
  if (!content)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "tag is empty");

  key = certificate_decode (content, value_type, encoding_type);
  dk_free_box (content);
  if (!key)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "could not decode certificate");
  if (id[0])
    {
      char * id_1 = dk_alloc_box (box_length (id) + 1, DV_STRING);
      char * kname = box_dv_short_string (key->xek_name);
      id_1[0] = '#';
      memcpy (id_1 + 1, id, box_length (id));
      id_hash_set (ctx->wc_id_cache, (caddr_t) &id_1, (caddr_t)&kname);
    }
}
void wss_reference_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * ref = xml_find_attribute (curr, "URI", 0);
  if (ref)
    {
      xenc_key_t * k = xenc_get_key_by_keyid (ctx, ref);
      if (!k)
	wsse_report_error (ctx, WSSE_BINARYSECTOKENREF_CODE, strlen (ref), ref);
      if (ctx->wc_object_type == XENC_T_DSIG)
	{
	  ctx->wc_dsig->dss_key = k;
	}
      else if (ctx->wc_object_type == XENC_T_ENCKEY)
	{
	  WSSE_ASSERT (ctx->wc_curr_enckey);
	  ctx->wc_curr_enckey->xeke_super_key = box_dv_short_string (k->xek_name);
	}
    }
}
void wss_keyidentifier_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  caddr_t value_type = xml_find_attribute (curr, "ValueType", 0);
  caddr_t keyident;

  if (!value_type)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100, "Unknown type of key identifier");

  if (strcmp (value_type, WSSE_X509_VALUE_TYPE) && strcmp (value_type, WSSE_OASIS_X509_SUBJECT_KEYIDENTIFIER))
    wsse_report_error (ctx, WSSE_BINARYSECTOKENVALTYPE_CODE, strlen (value_type), value_type);
  keyident = wsse_get_content_val (curr);
  if (!keyident)
    wsse_report_error (ctx, WSSE_BINARYSECTOKEN_CODE, 100,
		       "Key identifier must be base64 encoded string");

  if (ctx->wc_object_type == XENC_T_DSIG ||
      ctx->wc_object_type == XENC_T_ENCKEY)
    {
      xenc_key_t * k = xenc_get_key_by_keyidentifier (keyident, 1);
      if (!k)
	{
	  char buf[201];
	  memset (buf, 0 , sizeof (buf));
	  strncpy (buf, keyident, 200);
	  dk_free_box (keyident);
	  wsse_report_error (ctx, WSSE_BINARYSECTOKENREF_CODE, strlen (buf), buf);
	}
      if (ctx->wc_object_type == XENC_T_DSIG)
	{
	  ctx->wc_dsig->dss_key = k;
	}
      else
	{
	  WSSE_ASSERT (ctx->wc_curr_enckey);
	  ctx->wc_curr_enckey->xeke_super_key = box_dv_short_string (k->xek_name);
	}
    }
  dk_free_box (keyident);
}


void wss_securitytokenref_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
}

void wss_usernametoken_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
  char * id = xml_find_attribute (curr, "Id", 0);
  xenc_key_t * key;
  caddr_t *tag_uname, *tag_pass, *tag_nonce, *tag_stamp, seed, pass_type = NULL;
  char *uname = NULL, *pass = NULL, *nonce = NULL, *stamp = NULL;
  dk_session_t *ses = strses_allocate ();
  dk_session_t *dgs = strses_allocate ();
  user_t * user;
  u_tok_t * utok;
  caddr_t * utok_opts = (caddr_t *) xenc_get_option (ctx->wc_opts, "UsernameToken", NULL);
  caddr_t wss_label = xenc_get_option (utok_opts, "label", "WS-Security");

  session_buffered_write (ses, wss_label, strlen (wss_label)); /* WSE2.0 silently defines WS-Security as label */

  if (NULL != (tag_nonce = (caddr_t *) xml_find_one_child (curr, "Nonce", wsse_uris, 0, 0)))
    {
      int len;
      caddr_t nonce1;
      nonce = wsse_get_content_val (tag_nonce);
      nonce1 = box_copy (nonce);
      len = xenc_decode_base64 (nonce1, nonce1 + box_length (nonce1));
      session_buffered_write (ses, nonce1, len);
      session_buffered_write (dgs, nonce1, len);
      dk_free_box (nonce1);
    }
  else if (ctx->wc_object_type == XENC_T_DSIG)
    {
      char nonce1[16], nonce2 [40];
      int len;
      RAND_pseudo_bytes ((unsigned char *)nonce1, 16);
      session_buffered_write (ses, nonce1, 16);
      len = xenc_encode_base64 (nonce1, nonce2, 16);
      nonce2[len] = 0;
      nonce = box_dv_short_string (nonce2);
    }
  else
    {
      wsse_report_error (ctx, WSSE_UNKNOWN_CODE, 100, "Mandatory wss:Nonce element is missing");
    }

  if (NULL != (tag_uname = (caddr_t *) xml_find_one_child (curr, "Username", wsse_uris, 0, 0)))
    {
      uname = wsse_get_content_val (tag_uname);
    }

  if (NULL != (tag_stamp = (caddr_t *) xml_find_one_child (curr, "Created", wsu_uris, 0, 0)))
    {
      stamp = wsse_get_content_val (tag_stamp);
      session_buffered_write (ses, stamp, box_length (stamp) - 1);
      session_buffered_write (dgs, stamp, box_length (stamp) - 1);
    }
  else if (ctx->wc_object_type == XENC_T_DSIG)
    {
      time_t now;
      char dt [DT_LENGTH], buf [100];

      time (&now);
      time_t_to_dt (now, 0, dt);
      DT_SET_TZ (dt, 0);
      dt_to_iso8601_string (dt, buf, sizeof (buf));
      stamp = box_dv_short_string (buf);
      session_buffered_write (ses, stamp, box_length (stamp) - 1);
    }

  if (NULL != (tag_pass  = (caddr_t *) xml_find_one_child (curr, "Password", wsse_uris, 0, 0)))
    {
      pass = wsse_get_content_val (tag_pass);
      pass_type = xml_find_attribute (tag_pass, "Type", 0);
      if (pass_type && !strncmp (pass_type, WSSE_OASIS_UTOKEN_PROFILE, strlen (WSSE_OASIS_UTOKEN_PROFILE)))
	{
	  pass_type = strrchr (pass_type, '#');
	  pass_type++;
	}
      else if (pass_type && strrchr (pass_type, ':'))
	{
	  pass_type = strrchr (pass_type, ':');
	  pass_type++;
	}
    }

  seed = strses_string (ses);
  strses_free (ses);

  if (ctx->wc_object_type != XENC_T_DSIG)
    {
      if (NULL == (user = sec_name_to_user (uname)))
	{
	  dk_free_box (pass); dk_free_box (uname); dk_free_box (stamp); dk_free_box (nonce);
	  dk_free_box (seed);
	  strses_free (dgs);
	  wsse_report_error (ctx, WSSE_UNKNOWN_CODE, 100, "Unknown user in wss:Username");
	}

      if (pass_type && !strcmp (pass_type, "PasswordDigest"))
	{
	  unsigned char md [SHA_DIGEST_LENGTH + 1], md64 [SHA_DIGEST_LENGTH * 2 + 1];
	  SHA_CTX ctx1;
	  int len;
	  caddr_t tmp;

	  session_buffered_write (dgs, user->usr_pass, box_length (user->usr_pass) - 1);
	  tmp = strses_string (dgs);
	  SHA1_Init(&ctx1);
	  SHA1_Update(&ctx1, tmp, box_length (tmp) - 1);
	  SHA1_Final(&(md[0]), &ctx1);
	  dk_free_box (tmp);
	  len = xenc_encode_base64 ((char *)md, (char *)md64, SHA_DIGEST_LENGTH);
	  md64 [len] = 0;
	  if (0 != strcmp ((char *)md64, pass))
	    {
	      dk_free_box (pass); dk_free_box (uname); dk_free_box (stamp); dk_free_box (nonce);
	      dk_free_box (seed);
	      strses_free (dgs);
	      wsse_report_error (ctx, WSSE_UNKNOWN_CODE, 100, "Invalid credentials");
	    }
	  else
	    {
	      dk_free_box (pass);
	      pass = box_copy (user->usr_pass);
	    }
	}
      else if (!pass || 0 != strcmp (user->usr_pass, pass))
	{
	  dk_free_box (pass); dk_free_box (uname); dk_free_box (stamp); dk_free_box (nonce);
	  dk_free_box (seed);
	  strses_free (dgs);
	  wsse_report_error (ctx, WSSE_UNKNOWN_CODE, 100, "Invalid credentials");
	}
    }
  strses_free (dgs);


  /*
     The secret is the password, the label is the client label (optionally specified in policies),
     and the seed is the nonce value specified by the client (a <Nonce> element is required).
     If such an element is specified in the <Username> element it is used,
     otherwise the value from the <Security> element is used.
     If a <wsu:Created> time element is specified in the <UsernameToken>, then it is also used.
     If this message is part of a shared context with another party,
     then the label is the concatenation of the client and server labels
     and the seed is the concatenation of the client and server nonces.
     The nonce is processed as a binary octet stream and the timestamp as a UTF-8 encoded string.
     P_SHA1 (password, label + nonce + timestamp)
  */

  utok = (u_tok_t *) dk_alloc (sizeof (u_tok_t));
  memset (utok, 0, sizeof (u_tok_t));
  utok->uname = uname;
  utok->pass = pass;
  utok->ts = stamp;
  utok->nonce = nonce;

  key = xenc_key_create_from_utok (utok, seed, ctx);
  dk_free_box (seed);

  if (!key)
    wsse_report_error (ctx, WSSE_UNKNOWN_CODE, 100, "could not decode username token");


  if (ctx->wc_object_type == XENC_T_DSIG)
    {
      ctx->wc_dsig->dss_key = key;
    }

  if (id && id[0])
    {
      char * id_1 = dk_alloc_box (box_length (id) + 1, DV_STRING);
      char * kname = box_dv_short_string (key->xek_name);
      id_1[0] = '#';
      memcpy (id_1 + 1, id, box_length (id));
      id_hash_set (ctx->wc_id_cache, (caddr_t) &id_1, (caddr_t)&kname);
    }

}


void wss_dummy_c (char* uri, char * name, caddr_t * curr, wsse_ctx_t * ctx)
{
}

wsse_callback_f wsse_get_callback (wsse_ctx_t * ctx, const char * uri, const char * name)
{
  ptrlong idx;
  wsse_callback_item_t * callbacks = NULL;
  ptrlong callbacks_len = 0;

  if (!strcmp (uri, XENC_URI))
    {
      callbacks = (wsse_callback_item_t *) wsse_xenc_callbacks;
      callbacks_len = wsse_xenc_callbacks_len;
    }
  else if (!strcmp (uri, DSIG_URI))
    {
      callbacks = (wsse_callback_item_t *) wsse_dsig_callbacks;
      callbacks_len = wsse_dsig_callbacks_len;
    }
  else if (is_in_urls (wsse_uris, uri, NULL))
    {
      callbacks = (wsse_callback_item_t *) wsse_callbacks;
      callbacks_len = wsse_callbacks_len;
    }
  else if (is_in_urls (wsu_uris, uri, NULL))
    {
      /* no callbacks at present */
      return 0;
    }
  else
    wsse_report_error (ctx, WSSE_UNKNOWN_URI_CODE, 0);

  idx = ecm_find_name (name, (void *)callbacks, callbacks_len, sizeof (wsse_callback_item_t));

  if (idx != -1)
    {
      wsse_callback_item_t * item = callbacks + idx;
      return item->wsse_c_callback;
    }
  wsse_report_error (ctx, WSSE_CALLBACK_NOT_FOUND_CODE, strlen (name), name);
  return 0;
}

int wsse_build_wsse_objects (caddr_t * curr, wsse_callback_f wsse_serialize, wsse_ctx_t * ctx)
{
  if (!wsse_serialize)
    return 0;
  else
    {
      char *szName = XML_ELEMENT_NAME (curr);
      char *szColon = strrchr (szName, ':');
      char *name;
      int inx;

      if (!szColon)
	return 0;

      name = szColon + 1;
      /* error handling must be here */
      ( wsse_serialize ) (0, name, curr, ctx);
      DO_BOX (caddr_t *, child, inx, curr)
	{
	  if (inx && DV_TYPE_OF (child) == DV_ARRAY_OF_POINTER)
	    {
	      char *szName = XML_ELEMENT_NAME (child);
	      char *szColon = strrchr (szName, ':');
	      char *name;
	      int colon_len = (int) (szColon - szName);
	      char uri[100];
	      wsse_callback_f new_serilize;

	      if (!szColon)
		continue;

	      strncpy (uri, szName, colon_len);
	      uri[colon_len] = 0;
	      name = szColon + 1;
	      new_serilize = wsse_get_callback (ctx, uri, name);

	      wsse_build_wsse_objects (child, new_serilize, ctx);
	    }
	}
      END_DO_BOX;
      return 1;
    }
}

void wsse_resolve_encrypted_keys (caddr_t * envelope, wsse_ctx_t * ctx)
{
  id_hash_t * id_cache = 0;
  dk_set_t unresolved = 0;
  xenc_dec_t * dec = ctx->wc_dec;

  if (!dec)
    return;

  DO_SET (xenc_enc_key_t*, enc_key, &dec->xed_keys)
    {
      if (enc_key->xeke_is_raw)
	{
	  dk_set_push (&unresolved, enc_key);
	}
    }
  END_DO_SET ();

  if (!unresolved)
    return;

  xenc_build_ids_hash (envelope, &id_cache, 1);
  if (!id_cache)
    {
      dk_set_free (unresolved);
      wsse_report_error (ctx, WSSE_UNRESOLVED_REF_URIS_CODE, 0);
    }

  DO_SET (xenc_enc_key_t*, enc_key, &unresolved)
    {
      caddr_t ** curr;
      caddr_t uri = (caddr_t) enc_key->xeke_refs->data;
      caddr_t * keyinfo;
      caddr_t * keyname;
      char * key_name;

      curr = (caddr_t **) id_hash_get (id_cache, (caddr_t) & uri);
      if (!curr)
	{
	  xenc_ids_hash_free (id_cache);
	  dk_set_free (unresolved);
	  wsse_report_error (ctx, WSSE_UNRESOLVED_REF_URI_CODE, strlen (uri), uri);
	}
      keyinfo = (caddr_t *) xml_find_child (curr[0], "KeyInfo", DSIG_URI, 0,0);
      if (!keyinfo)
	{
	  xenc_ids_hash_free (id_cache);
	  dk_set_free (unresolved);
	  wsse_report_error (ctx, WSSE_ENCKEY_REF_URI_NO_KEYINFO_CODE, strlen (uri), uri);
	}
      keyname = (caddr_t *) xml_find_child (keyinfo, "KeyName", DSIG_URI, 0, 0);
      if (!keyname)
	{
	  xenc_ids_hash_free (id_cache);
	  dk_set_free (unresolved);
	  wsse_report_error (ctx, WSSE_ENCKEY_REF_URI_NO_KEYNAME_CODE, strlen (uri), uri);
	}
      key_name = wsse_get_content_val (keyname);
      if (!key_name)
	{
	  xenc_ids_hash_free (id_cache);
	  dk_set_free (unresolved);
	  wsse_report_error (ctx, WSSE_ENCKEY_REF_URI_EMPTY_KEYNAME_CODE, strlen (uri), uri);
	}
      enc_key->xeke_name = key_name;
     }
  END_DO_SET ();
  xenc_ids_hash_free (id_cache);
  dk_set_free (unresolved);
}

/* returns 0 if failed */
int dsig_verify_signature (dsig_signature_t * d, xml_tree_ent_t * xte, id_hash_t * nss)
{
  caddr_t * signature_tree = xte->xte_current;
  caddr_t * doc_tree =  xte->xe_doc.xtd->xtd_tree;
  query_instance_t * qi = xte->xe_doc.xtd->xd_qi;
  dk_session_t * ses_out;
  dsig_verify_f verify = dsig_verify_f_get (d->dss_signature_method, 0);

  ses_out = strses_allocate ();

  if (!xml_canonicalize (qi, doc_tree, signature_tree, nss, ses_out))
    {
      strses_free (ses_out);
      return 0;
    }

  if (!verify(ses_out, strses_length (ses_out), d->dss_key, d->dss_signature))
    {
      strses_free (ses_out);
      return 0;
    }
  strses_free (ses_out);
  return 1;
}

xenc_err_code_t dsig_check_xml (query_instance_t * qi, dsig_signature_t * d, char * xml_text,
	 long len, xenc_err_code_t * c, char ** err, int is_wsse)
{
  dsig_signature_t * d_copy = dsig_copy_draft (d);
  xenc_err_code_t cc;
  dk_session_t * xml_doc;
  dsig_compare_t cmp;
  memset (&cmp, 0, sizeof (dsig_compare_t));
  if (err) err[0] = 0;

  xml_doc = strses_allocate ();
  xml_doc->dks_in_buffer = xml_text;
  xml_doc->dks_in_fill = len;
  xml_doc->dks_in_read = 0;

  SET_THR_ATTR (THREAD_CURRENT_THREAD, TA_XENC_ERR_BUFFER, 0);
  SET_THR_ATTR (THREAD_CURRENT_THREAD, TA_XENC_ERR_CODE, 0);

  if ((cc = dsig_initialize (qi, xml_doc, len, d_copy, c, err)))
    {
      dsig_free (d_copy);
      xml_doc->dks_in_buffer = NULL;
      strses_free (xml_doc);
      return cc;
    }
  xml_doc->dks_in_buffer = NULL;
  strses_free (xml_doc);
  xml_doc = NULL;

  if (dsig_compare (d, d_copy, &cmp))
    {
      if (c)
	cc = c[0] = DSIG_CHECK_ERR;

      if (err)
	{
	  char buf[1024];
	  memset (buf, 0, 1024);
	  snprintf (buf, 1023, "XML document failed signature check at %s with value %s [must be %s]",
		    cmp.dsc_obj, cmp.dsc_value1, cmp.dsc_value2);
	  err[0] = box_dv_short_string (buf);
	}
      dsig_free (d_copy);
      return cc;
    }

  dsig_free (d_copy);

  {
    id_hash_t * nss = 0;
    xml_tree_ent_t * xte = (xml_tree_ent_t *) xml_make_tree_with_ns (qi, xml_text,
	0, "UTF-8", lh_get_handler ("x-any"), &nss, 0);
    caddr_t * signature = xte ? xml_find_signedinfo (xte->xte_current, is_wsse) : 0;

    if (!signature)
      {
	dk_free_box ((box_t) xte);
	nss_free (nss);
	if (c)
	  c[0] = DSIG_CHECK_ERR;
	if (err)
	  err[0] = box_dv_short_string ("XML Document contains no Signature");
	return DSIG_CHECK_ERR;
      }
    xte->xte_current = signature;
    if (!dsig_verify_signature (d, xte, nss))
      {
	dk_free_box ((box_t) xte);
	nss_free (nss);
	if (c)
	  c[0] = DSIG_CHECK_ERR;
	if (err)
	  err[0] = box_dv_short_string ("Signature verify check error");
	return DSIG_CHECK_ERR;
      }
    dk_free_box ((box_t) xte);
    nss_free (nss);
  }
  return 0;
}

/* uri must not be zero! */
int xml_is (char * name, char * uri, char * tagname)
{
  char *col;
  int len;

  if (!name)
    return 0;

  col = strrchr (name, ':');
  len = (int) (col - name);

  if (!col)
    return 0;

  if (len != strlen (uri))
    return 0;

  if (strncmp (name, uri, len))
    return 0;

  if (strcmp (col + 1, tagname))
    return 0;
  return 1;
}

caddr_t dsig_sign_signature (dsig_signature_t * dsig, xml_tree_ent_t * xte, id_hash_t * nss, wsse_ctx_t * ctx)
{
  caddr_t * signature_tree = xte->xte_current;
  caddr_t * doc_tree =  xte->xe_doc.xtd->xtd_tree;
  query_instance_t * qi = xte->xe_doc.xtd->xd_qi;
  caddr_t * siginfo_tree = xml_find_child (signature_tree, "SignedInfo", DSIG_URI, 0, 0);
  dsig_canon_2_f canon_f;
  dsig_sign_f sign_f;
  dk_session_t * ses;
  caddr_t signval;

  if (!siginfo_tree)
    wsse_report_error (ctx, WSSE_NO_TAG_CODE, strlen ("SingInfo"), "SignInfo");

  canon_f = dsig_canon_2_f_get (dsig->dss_canon_method, &ctx->wc_tb);
  sign_f = dsig_sign_f_get (dsig->dss_signature_method, &ctx->wc_tb);

  ses = strses_allocate ();

  if (!(canon_f) (qi, doc_tree, siginfo_tree, nss, ses))
    {
      strses_free (ses);
      wsse_report_error (ctx, WSSE_ALGO_CODE, sizeof (dsig->dss_canon_method),
			 dsig->dss_canon_method);
    }

  if (!(sign_f) (ses, strses_length(ses), dsig->dss_key, &signval))
    {
      strses_free (ses);
      if (dsig->dss_key)
	wsse_report_error (ctx, WSSE_ALGO_CODE, sizeof (dsig->dss_signature_method),
			 dsig->dss_signature_method);
      else
	wsse_report_error (ctx, WSSE_ALGO_EMPTY_KEY_CODE, sizeof (dsig->dss_signature_method),
			 dsig->dss_signature_method);
    }
  strses_free (ses);

  return signval;
}
caddr_t dsig_sign_signature_ (query_instance_t * qi, dsig_signature_t * dsig, caddr_t dsig_template_str, xenc_err_code_t * c, char ** err)
{
  wsse_ctx_t * ctx;
  caddr_t signval = 0;
  caddr_t * signature;
  id_hash_t * nss = 0;
  xml_tree_ent_t * doc = NULL;

  ctx = wsse_ctx_allocate ();

  XENC_TRY (&ctx->wc_tb)
    {
      doc =  (xml_tree_ent_t *) xml_make_tree_with_ns (qi,
	dsig_template_str, err, "UTF-8", lh_get_handler ("x-any"), &nss, 0);

      if (!doc)
	wsse_report_error (ctx, WSSE_XML_CODE, 0);

      signature = xml_find_signature (doc->xte_current, 1);

      if (!signature)
	wsse_report_error (ctx, WSSE_NO_TAG_CODE, strlen ("Signature"), "Signature");

      doc->xte_current = signature;

      signval = dsig_sign_signature (dsig, doc, nss, ctx);
      dk_free_box ((box_t) doc);
      nss_free (nss);
    }
  XENC_CATCH
    {
      dk_free_box ((box_t) doc);
      nss_free (nss);
      if (c) c[0] = ctx->wc_tb.xtb_err_code;
      if (err) err[0] = box_dv_short_string (ctx->wc_tb.xtb_err_buffer);
      wsse_ctx_free (ctx);
      return 0;
    }
  XENC_TRY_END (&ctx->wc_tb);
  wsse_ctx_free (ctx);

  return signval;
}


/* procedure for validating xml against detached signature */
caddr_t bif_dsig_validate (caddr_t *qst, caddr_t *err, state_slot_t ** args)
{
  caddr_t xml_text = bif_string_arg (qst, args, 0, "dsig_validate");
  caddr_t signature_xml_text = BOX_ELEMENTS (args) > 1 ? bif_string_or_null_arg (qst, args, 1, "dsig_validate") : NULL;
  caddr_t enc = BOX_ELEMENTS (args) > 2 ? bif_string_arg (qst, args, 2, "dsig_validate") : "UTF-8";
  lang_handler_t *lh = BOX_ELEMENTS (args) > 3 ? lh_get_handler (bif_string_arg (qst, args, 3, "dsig_validate")) : server_default_lh;
  wsse_ctx_t * ctx;
  dsig_signature_t * dsig;
  caddr_t * signature_tag, err_ret = NULL;
  caddr_t * xml_doc;
  xenc_err_code_t c;
  caddr_t errm = NULL;
  id_hash_t * nss = 0;
  xml_tree_ent_t * xte;

  if (!signature_xml_text)
    signature_xml_text = xml_text;

  xte = (xml_tree_ent_t *) xml_make_tree_with_ns ((query_instance_t*) qst, signature_xml_text, &err_ret, enc, lh, &nss, 0);

  if (!xte)
    {
      nss_free (nss);
      if (!err_ret)
	sqlr_new_error ("42000", "XENC25", "Could not parse signature XML document");
      else
	{
	  *err = err_ret;
	  return NULL;
	}
    }

  xml_doc = xte->xte_current;
  signature_tag = xml_find_any_child (xml_doc, "Signature", DSIG_URI);
  if (!signature_tag)
    {
      dk_free_box ((box_t) xte);
      nss_free (nss);
      sqlr_new_error ("42000", "XENC26", "XML document is not XML Signature");
    }

  xte->xte_current = signature_tag;
  ctx = wsse_ctx_allocate();
  XENC_TRY (&ctx->wc_tb)
    {
      wsse_build_wsse_objects (signature_tag, wsse_get_callback (ctx, DSIG_URI , "Signature"), ctx);
    }
  XENC_CATCH
    {
      char errbuf[1024];
      dk_free_box ((box_t) xte);
      nss_free (nss);
      xenc_make_error (errbuf, 1024, ctx->wc_tb.xtb_err_code, ctx->wc_tb.xtb_err_buffer);
      wsse_ctx_free (ctx);
      sqlr_new_error ("42000", "XENC27", "dsig_validate function reports an error: %s", errbuf);
    }
  XENC_TRY_END(&ctx->wc_tb);

  dsig = ctx->wc_dsig;
  dk_free_box ((box_t) xte);
  nss_free (nss);

  if (dsig_check_xml ((query_instance_t*) qst, dsig, xml_text, box_length (xml_text) - 1, &c, &errm, 0))
    {
      char buf[1024];
      xenc_make_error (buf, 1024, c, errm);
      dk_free_tree (errm);
      wsse_ctx_free (ctx);
      sqlr_new_error ("42000", "XENC28", "error occurred when xml document was checked: %s", buf);
    }
  wsse_ctx_free (ctx);
  return NEW_DB_NULL;
}

caddr_t * xml_find_signature (caddr_t * doc, int is_wsse)
{
  caddr_t * envelope;
  if (!is_wsse)
    return xml_find_any_child (doc, "Signature", DSIG_URI);
  envelope = xml_find_child (doc, "Envelope", WSS_SOAP_URI, 0, NULL);
  if (envelope)
    {
      caddr_t * header = xml_find_child (envelope, "Header", WSS_SOAP_URI, 0,NULL);
      if (header)
	{
	  caddr_t * security = xml_find_one_child (header, "Security", wsse_uris, 0, NULL);
	  if (!security)
		return 0;
	  return xml_find_child (security, "Signature", DSIG_URI, 0, NULL);
	}
    }
  return 0;
}

caddr_t * xml_find_signedinfo (caddr_t * doc, int is_wsse)
{
  caddr_t * signature = xml_find_signature (doc, is_wsse);
  if (signature)
    {
      return xml_find_child (signature, "SignedInfo", DSIG_URI, 0, NULL);
    }
  return 0;
}


#define XENC_VALIDATE_NONE	0
#define XENC_VALIDATE_EXPLICIT	1
#define XENC_VALIDATE_IF	2
#define XENC_TRY_DECODE		4

#define xenc_signal_error(c,v,m) \
	if (err_ret) { \
	  *err_ret = srv_make_new_error (c, v, "%s", m); \
          return NULL; \
	} else { \
	  sqlr_new_error (c, v, "%s", m); \
	}

caddr_t *
xmlenc_get_keys (wsse_ctx_t * ctx)
{
  dk_set_t set = NULL;
  caddr_t * ret = (caddr_t *) list (2, 0, 0);
  xenc_dec_t * enc = ctx->wc_dec;
  dsig_signature_t * dsig = ctx->wc_dsig;

  DO_SET (xenc_enc_key_t *, enc_key, &enc->xed_keys)
    {
      if (enc_key->xeke_super_key)
	dk_set_push (&set, (void *) box_dv_short_string (enc_key->xeke_super_key));
    }
  END_DO_SET ();

  /* array of names of the data encryption keys */
  ret[0] = list_to_array (dk_set_nreverse (set));

  if (dsig && dsig->dss_key && dsig->dss_key->xek_name)
    {
      xenc_key_t * skey = dsig->dss_key;
      caddr_t * ksig = (caddr_t *) list (2, 0, 0);
      ksig[0] = box_dv_short_string (skey->xek_name);
      if (skey->xek_x509_KI)
        {
	  xenc_key_t * rkey = xenc_get_key_by_keyidentifier (skey->xek_x509_KI, 1);
	  if (rkey != skey && !rkey->xek_is_temp)
	    ksig[1] = box_dv_short_string (rkey->xek_name);
        }
      if (0 == ksig[1])
	ksig[1] = NEW_DB_NULL;
      /* name and matching key from local key store for xml signature */
      ret[1] = (caddr_t) ksig;
    }
  else
    ret[1] = NEW_DB_NULL;

  return ret;
}

/* xenc_decrypt internal function */
caddr_t
xmlenc_decrypt_soap (caddr_t * qst, char * xml_text, long soap_version, long validate_sign,
                     const char * enc, lang_handler_t *lh, caddr_t * err_ret, caddr_t * opts,
		     caddr_t *rkeys)
{
  wsse_ctx_t * ctx;
  xenc_err_code_t c = 0;
  char * errm = 0;
  caddr_t * doc ;
  caddr_t * envelope;
  caddr_t * header;
  caddr_t * security;
  caddr_t * signature;
  int is_valid_secxml = 0, try_decode = (validate_sign & 4);
  char * decrypted_xml_text = NULL;
  dk_session_t *decrypted_xml;
  wsse_ser_ctx_t sctx, *pctx = &sctx;

  memset (&sctx, 0, sizeof (wsse_ser_ctx_t));
  doc = (caddr_t *) xml_make_tree ((query_instance_t*) qst, xml_text, err_ret, (char *) enc, lh, 0);

  if (!doc)
    {
      xenc_signal_error ("42000", "XENC29", "Could not parse XML document, call xml_validate_dtd() for details");
    }

  validate_sign = validate_sign & 3;

  envelope = xml_find_child (doc, "Envelope", WSS_SOAP_URI, 0, NULL);
  if (!envelope)
    goto ret;

  header = xml_find_child (envelope, "Header", WSS_SOAP_URI, 0,NULL);
  if (!header)
    {
      if (try_decode) /* no security info, no signal */
	{
	  dk_free_tree ((box_t) doc);
	  return NULL;
	}
      else
	goto ret;
    }

  if (NULL == (security = xml_find_one_child (header, "Security", wsse_uris, 0, NULL)))
    {
      if (try_decode) /* no security info, no signal */
	{
	  dk_free_tree ((box_t) doc);
	  return NULL;
	}
      else
	goto ret;
    }

  signature = xml_find_child (security, "Signature", DSIG_URI, 0, NULL);
  if (validate_sign == XENC_VALIDATE_EXPLICIT && !signature)
    goto ret;

  is_valid_secxml = 1;

  ctx = wsse_ctx_allocate();
  ctx->wc_opts = opts;

  XENC_TRY (&ctx->wc_tb)
    {
      wsse_build_wsse_objects (security, wsse_get_callback (ctx, WSSE_URI(pctx) , "Security"), ctx);
      wsse_resolve_encrypted_keys (envelope, ctx);
    }
  XENC_CATCH
    {
      char errbuf[1024];
      xenc_make_error (errbuf, 1024, ctx->wc_tb.xtb_err_code, ctx->wc_tb.xtb_err_buffer);
      wsse_ctx_free (ctx);
      xenc_signal_error ("42000", "XENC30", errbuf);
    }
  XENC_TRY_END (&ctx->wc_tb);

  decrypted_xml = strses_allocate();

  if (xenc_decrypt_xml ((query_instance_t*) qst, ctx->wc_dec, xml_text, (caddr_t) enc,
	lh, decrypted_xml, &c, &errm))
    {
      char buf[1024];
      xenc_make_error (buf, 1024, c, errm);
      dk_free_box (errm);
      wsse_ctx_free (ctx);
      strses_free (decrypted_xml);
      xenc_signal_error ("42000", "XENC31", buf);
    }

  decrypted_xml_text = strses_string (decrypted_xml);

  if (validate_sign != XENC_VALIDATE_NONE && signature)
    if (dsig_check_xml ((query_instance_t*) qst, ctx->wc_dsig, xml_text, strlen (xml_text), &c, &errm, 1))
      {
	char buf[1024];
	wsse_ctx_free (ctx);
	xenc_make_error (buf, 1024, c, errm);
	dk_free_box (errm);
	dk_free_box (decrypted_xml_text);
	xenc_signal_error ("42000", "XENC32", buf);
      }
  if (rkeys)
    {
      *rkeys = (caddr_t) xmlenc_get_keys (ctx);
    }
  wsse_ctx_free (ctx);
  strses_free (decrypted_xml);
 ret:
  dk_free_tree ((box_t) doc);
  if (!is_valid_secxml)
    {
      xenc_signal_error ("42000", "XENC33", "XML document is not encrypted SOAP message");
    }

  return decrypted_xml_text;
}

/* this procedure takes xml text as first parameter, security entity as second
   decrypts and verifies it. Returns decrypted xml document as result */

/* bif_xmlenc_decrypt_soap
   @xml text
   @soap version
   @check signature
   @encoding
   @language
   @options
   @keys used
*/
caddr_t bif_xmlenc_decrypt_soap (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
  char * xml_text = bif_string_arg (qst, args, 0, "xenc_decrypt");
  long soap_version = bif_long_arg (qst, args, 1, "xenc_decrypt");
  long validate_sign = bif_long_arg (qst, args, 2, "xenc_decrypt");
  caddr_t enc = bif_string_arg (qst, args, 3, "xenc_decrypt");
  lang_handler_t *lh = lh_get_handler (bif_string_arg (qst, args, 4, "xenc_decrypt"));
  caddr_t opts = BOX_ELEMENTS(args) > 5 ? bif_strict_array_or_null_arg (qst, args, 5, "xenc_decrypt") : NULL;
  caddr_t ret, rkeys = NULL;

  ret = xmlenc_decrypt_soap (qst, xml_text, soap_version, validate_sign, enc, lh, err_ret,
      (caddr_t *)opts, &rkeys);

  if (BOX_ELEMENTS(args) > 6 && ssl_is_settable (args[6]))
    {
      if (!rkeys)
	rkeys = NEW_DB_NULL;
      qst_set (qst, args[6], rkeys);
    }
  else
    {
      dk_free_tree (rkeys);
    }

  return ret;
}

void wsse_report_error (struct wsse_ctx_s * ctx, char * code, int buflen, ...)
{
  ptrlong erridx;
  buflen += 512;
  /* WSSE_ASSERT (ctx->wc_is_try_block); */

 again:
  erridx = ecm_find_name (code, (void*) wsse_error_templs,
      wsse_error_templs_len, sizeof (struct wsse_error_templ_s));

  if (erridx != -1)
    {
      struct wsse_error_templ_s * wsse_err = wsse_error_templs + erridx;
      va_list tail;
      char * tmpbuf = (char *) dk_alloc (buflen);
      char * tmphead = tmpbuf;
      int rc;
      memset (tmpbuf, 0, buflen);

      memcpy (tmphead, code, strlen (code));
      tmphead += strlen (tmphead);

      tmphead[0] = '\t';
      tmphead++;

      va_start (tail, buflen);
      rc = vsnprintf (tmphead, buflen - strlen (code) - 1, wsse_err->templ, tail);
      if (rc > buflen)
	GPF_T1("Not enough buffer length for writing");
      va_end (tail);

      ctx->wc_tb.xtb_err_code = WSSE_ERR;
      ctx->wc_tb.xtb_err_buffer = box_dv_short_string (tmpbuf);
      dk_free (tmpbuf, buflen);
    }
  else
    {
      code = WSSE_UNKNOWN_CODE;
      goto again;
    }
#if 0
  if (xenc_test_processing())
    return;
#endif
  XENC_SIGNAL_FATAL (&ctx->wc_tb);
}

void wsse_check_built_encrypted_key (xenc_key_t * key, wsse_ctx_t * ctx)
{
  if (!key)
    wsse_report_error (ctx, WSSE_NO_ENC_KEY, 0);
  if (!key->xek_name)
    wsse_report_error (ctx, WSSE_CORRUPTED_ENC_KEY, strlen ("Key Name"), "Key Name");
  if (!key->xek_enc_algo)
    wsse_report_error (ctx, WSSE_CORRUPTED_ENC_KEY, strlen ("Algorithm"), "Algorithm");
  if (!key->xek_sign_algo)
    wsse_report_error (ctx, WSSE_CORRUPTED_ENC_KEY, strlen ("Algorithm"), "Algorithm");
}

/* returns encrypted key instance */
xenc_key_t * xenc_build_encrypted_key (const char * carried_name, dk_session_t * in,
				       const char * algo_name,  xenc_try_block_t * t)
{
  unsigned char * key_raw_data;
  xenc_key_t * key;
  int len;

  key_raw_data = (unsigned char *)strses_string (in);
#ifdef DEBUG
  printf ("\n%s\n", key_raw_data);
#endif
  mutex_enter (xenc_keys_mtx);

  if (carried_name)
    key = xenc_key_create (carried_name , algo_name, algo_name, 0);
  else
    key = xenc_key_create (0, algo_name, algo_name, 0);

  if (!key)
    goto error_end;

  switch (key->xek_type)
    {
    case DSIG_KEY_3DES:
      /* len = decode_base64 (key_raw_data, key_raw_data + box_length (key_raw_data) - 1); */
      len = strses_length (in);
      if (len != 192 / 8) /* 192 = 64 * 3 */
	{
	  xenc_key_remove (key, 0);
	  dk_free_box ((box_t) key_raw_data);
	  mutex_leave (xenc_keys_mtx);
	  xenc_report_error (t, 500 + strlen (algo_name), XENC_ENCKEY_ERR, "Serialized triple_des key %s has wrong size %ld", carried_name ? carried_name : "", len * 8);
	}
      xenc_key_3des_init (key, key_raw_data, key_raw_data + 8, key_raw_data + 16);
      break;
#ifdef AES_ENC_ENABLE
    case DSIG_KEY_AES:
      /* len = decode_base64 (key_raw_data, key_raw_data + box_length (key_raw_data) - 1); */
      len = strses_length (in);
      if (!carried_name && 0 == key->ki.aes.bits && algo_name)
	{
	  if (!strcmp (algo_name, XENC_AES128_ALGO))
	    key->ki.aes.bits = 128;
	  else if (!strcmp (algo_name, XENC_AES192_ALGO))
	    key->ki.aes.bits = 192;
	  else if (!strcmp (algo_name, XENC_AES256_ALGO))
	    key->ki.aes.bits = 256;
	}
      if (len != key->ki.aes.bits / 8)
	{
	  xenc_key_remove (key, 0);
	  dk_free_box ((box_t) key_raw_data);
	  mutex_leave (xenc_keys_mtx);
	  xenc_report_error (t, 500 + strlen (algo_name), XENC_ENCKEY_ERR, "Serialized AES key %s has wrong size", carried_name ? carried_name : "");
	}
      key->ki.aes.k = (unsigned char *) dk_alloc (key->ki.aes.bits / 8 /* number of bits in a byte */);
      memset (key->ki.aes.iv, 0, sizeof (key->ki.aes.iv));
      memcpy (key->ki.aes.k, key_raw_data, key->ki.aes.bits / 8 /* bits in one byte */);
      break;
#endif
    default:;
      /* do nothing */
    }
error_end:
  dk_free_box ((box_t) key_raw_data);

  mutex_leave (xenc_keys_mtx);

  if (!key)
    xenc_report_error (t, 500 + strlen (algo_name), XENC_ALGO_ERR, "Could not create key with algorithm %s",
		       algo_name);
  WSSE_ASSERT (key);
  return key;
}

/* builds uninitialized signature from xml template */
dsig_signature_t * dsig_template_ (query_instance_t * qi, caddr_t signature_xml_text, xenc_try_block_t * t, caddr_t * opts)
{
  id_hash_t * nss = 0;
  xml_tree_ent_t * xte = (xml_tree_ent_t *) xml_make_tree_with_ns (qi, signature_xml_text,
	0, "UTF-8", lh_get_handler ("x-any"), &nss, 0);
  wsse_ctx_t * ctx;
  caddr_t * signature;
  dsig_signature_t * dsig;

  if (!xte)
    xenc_report_error (t, 500, DSIG_TEMPL_ERR, "Could not parse signature template xml");

  signature = xml_find_child (xte->xte_current, "Signature", DSIG_URI, 0, 0);
  if (!signature)
    {
      dk_free_box ((box_t) xte); nss_free (nss);
      xenc_report_error (t, 500, DSIG_TEMPL_ERR, "xml doc is not signature template xml");
    }

  ctx = wsse_ctx_allocate();
  ctx->wc_allow_empty_vals = 1;
  ctx->wc_opts = opts;

  XENC_TRY (&ctx->wc_tb)
    {
      wsse_build_wsse_objects (signature, wsse_get_callback (ctx, DSIG_URI , "Signature"), ctx);
    }
  XENC_CATCH
    {
      char errbuf[1024];
      dk_free_box ((box_t) xte);
      nss_free (nss);
      xenc_make_error (errbuf, 1024, ctx->wc_tb.xtb_err_code, ctx->wc_tb.xtb_err_buffer);
      wsse_ctx_free (ctx);

      xenc_report_error (t, 500 + strlen (errbuf), DSIG_TEMPL_ERR, errbuf);
    }
  XENC_TRY_END(&ctx->wc_tb);

  dsig = ctx->wc_dsig;
  ctx->wc_dsig = 0;

  wsse_ctx_free (ctx);

  dk_free_box ((box_t) xte);
  nss_free (nss);
  return dsig;
}

/* test of wsse_err */
#ifdef DEBUG



void xmlenc_check_ecm_array (void * array, ptrlong len, size_t elem_size)
{
  ptrlong i = 0;
  for (i=0;i<len;i++)
    {
      char * name = ((char **) ((char*) array + i * elem_size))[0];
      ptrlong idx = ecm_find_name ( (const utf8char*) name, array, len, elem_size);
      if (idx != i)
	{
	  xenc_assert (0);
	  rep_printf ("--> %s <--\n", name);
	}
    }
}

void xmlenc_check_ecm_arrays ()
{
  xmlenc_check_ecm_array ((void *) wsse_error_templs, wsse_error_templs_len, sizeof (struct wsse_error_templ_s));
  xmlenc_check_ecm_array ((void *) wsse_dsig_callbacks, wsse_dsig_callbacks_len, sizeof (wsse_callback_item_t));
  xmlenc_check_ecm_array ((void*) wsse_xenc_callbacks, wsse_xenc_callbacks_len, sizeof (wsse_callback_item_t));
  xmlenc_check_ecm_array ((void*) wsse_callbacks, wsse_callbacks_len, sizeof (wsse_callback_item_t));
}

void xmlenc_test_wsse_error ()
{
  wsse_ctx_t * ctx = dk_alloc (sizeof (wsse_ctx_t));
  ctx->wc_is_try_block = 1;

  wsse_report_error (ctx, "100", 1024);
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "100	no algorithm specified"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);

  wsse_report_error (ctx, "101", 1024, "helo");
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "101	unknown digest algorithm helo"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  wsse_report_error (ctx, "102", 1024);
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "102	digest value is not specified"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  wsse_report_error (ctx, "103", 1024);
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "000	unknown error"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  wsse_report_error (ctx, "104", 1024);
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "104	no XPath expression specified"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  wsse_report_error (ctx, "105", 1024);
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "105	signature value is not specified"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  wsse_report_error (ctx, "106", 1024, "helo");
  printf ("%s\n",ctx->wc_tb.xtb_err_buffer);
  xenc_assert (!strcmp (ctx->wc_tb.xtb_err_buffer, "106	tag is unknown helo"));
  dk_free_box (ctx->wc_tb.xtb_err_buffer);
  fflush (stdout);
  dk_free (ctx, sizeof (wsse_ctx_t));

  /* dsig_is_signature_root test */
  xenc_assert (xml_is (DSIG_URI ":Signature", DSIG_URI, "Signature"));
  xenc_assert (!xml_is (DSIG_URI ":Signature", DSIG_URI, "Signatureeeeee"));
  xenc_assert (!xml_is (DSIG_URI ":Barbarrossa", DSIG_URI, "Signature"));
  xenc_assert (!xml_is (XENC_URI "#:Signature", DSIG_URI, "Signature"));
  xenc_assert (!xml_is (":Signature", DSIG_URI, "Signature"));
  xenc_assert (!xml_is (":Sign", DSIG_URI, "Signature"));
  xenc_assert (!xml_is ("", DSIG_URI, "Signature"));
  xenc_assert (!xml_is (":", DSIG_URI, "Signature"));

  xenc_asserts_print_report (stderr);
}

#endif /* DEBUG */


#endif /* _SSL */