File: qca-wingss.cpp

package info (click to toggle)
qca2 2.3.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,888 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (2112 lines) | stat: -rw-r--r-- 62,875 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
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
/*
 * Copyright (C) 2008  Barracuda Networks, Inc.
 *
 * 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 WITHANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 *
 */

#include <QLibrary>
#include <QMutex>
#include <QTimer>
#include <QtCrypto>
#include <QtPlugin>
#include <qcaprovider.h>

#ifndef FORWARD_ONLY
#include <windows.h>
#define SECURITY_WIN32
#include <Security.h>
#endif

using namespace QCA;

#define PROVIDER_NAME "qca-wingss"

#if !defined(FORWARD_ONLY)

// some defs possibly missing from MinGW

#ifndef SEC_E_MESSAGE_ALTERED
#define SEC_E_MESSAGE_ALTERED 0x8009030F
#endif

#ifndef SEC_E_CONTEXT_EXPIRED
#define SEC_E_CONTEXT_EXPIRED 0x80090317
#endif

#ifndef SEC_E_CRYPTO_SYSTEM_INVALID
#define SEC_E_CRYPTO_SYSTEM_INVALID 0x80090337
#endif

#ifndef SEC_E_OUT_OF_SEQUENCE
#define SEC_E_OUT_OF_SEQUENCE 0x80090310
#endif

#ifndef SEC_E_BUFFER_TOO_SMALL
#define SEC_E_BUFFER_TOO_SMALL 0x80090321
#endif

#ifndef SECURITY_ENTRYPOINTW
#define SECURITY_ENTRYPOINTW TEXT("InitSecurityInterfaceW")
#endif

#ifndef SECURITY_ENTRYPOINT_ANSIA
#define SECURITY_ENTRYPOINT_ANSIA "InitSecurityInterfaceA"
#endif

#ifndef SECPKG_FLAG_GSS_COMPATIBLE
#define SECPKG_FLAG_GSS_COMPATIBLE 0x00001000
#endif

#ifndef SECQOP_WRAP_NO_ENCRYPT
#define SECQOP_WRAP_NO_ENCRYPT 0x80000001
#endif

#ifndef ISC_RET_MUTUAL_AUTH
#define ISC_RET_MUTUAL_AUTH 0x00000002
#endif

#ifndef ISC_RET_SEQUENCE_DETECT
#define ISC_RET_SEQUENCE_DETECT 0x00000008
#endif

#ifndef ISC_RET_CONFIDENTIALITY
#define ISC_RET_CONFIDENTIALITY 0x00000010
#endif

#ifndef ISC_RET_INTEGRITY
#define ISC_RET_INTEGRITY 0x00010000
#endif

#ifdef Q_CC_MINGW

// for some reason, the MinGW definition of the W table has A functions in
//   it, so we define a fixed version to use instead...

typedef struct _FIXED_SECURITY_FUNCTION_TABLEW
{
    unsigned long                     dwVersion;
    ENUMERATE_SECURITY_PACKAGES_FN_W  EnumerateSecurityPackagesW;
    QUERY_CREDENTIALS_ATTRIBUTES_FN_W QueryCredentialsAttributesW;
    ACQUIRE_CREDENTIALS_HANDLE_FN_W   AcquireCredentialsHandleW;
    FREE_CREDENTIALS_HANDLE_FN        FreeCredentialsHandle;
    void SEC_FAR                     *Reserved2;
    INITIALIZE_SECURITY_CONTEXT_FN_W  InitializeSecurityContextW;
    ACCEPT_SECURITY_CONTEXT_FN        AcceptSecurityContext;
    COMPLETE_AUTH_TOKEN_FN            CompleteAuthToken;
    DELETE_SECURITY_CONTEXT_FN        DeleteSecurityContext;
    APPLY_CONTROL_TOKEN_FN_W          ApplyControlTokenW;
    QUERY_CONTEXT_ATTRIBUTES_FN_W     QueryContextAttributesW;
    IMPERSONATE_SECURITY_CONTEXT_FN   ImpersonateSecurityContext;
    REVERT_SECURITY_CONTEXT_FN        RevertSecurityContext;
    MAKE_SIGNATURE_FN                 MakeSignature;
    VERIFY_SIGNATURE_FN               VerifySignature;
    FREE_CONTEXT_BUFFER_FN            FreeContextBuffer;
    QUERY_SECURITY_PACKAGE_INFO_FN_W  QuerySecurityPackageInfoW;
    void SEC_FAR                     *Reserved3;
    void SEC_FAR                     *Reserved4;
    void SEC_FAR                     *Unknown1;
    void SEC_FAR                     *Unknown2;
    void SEC_FAR                     *Unknown3;
    void SEC_FAR                     *Unknown4;
    void SEC_FAR                     *Unknown5;
    ENCRYPT_MESSAGE_FN                EncryptMessage;
    DECRYPT_MESSAGE_FN                DecryptMessage;
} FixedSecurityFunctionTableW, *PFixedSecurityFunctionTableW;

typedef FixedSecurityFunctionTableW  MySecurityFunctionTableW;
typedef PFixedSecurityFunctionTableW PMySecurityFunctionTableW;

#else

typedef SecurityFunctionTableW  MySecurityFunctionTableW;
typedef PSecurityFunctionTableW PMySecurityFunctionTableW;

#endif

#ifdef UNICODE
#define MySecurityFunctionTable MySecurityFunctionTableW
#define PMySecurityFunctionTable PMySecurityFunctionTableW
#else
#define MySecurityFunctionTable MySecurityFunctionTableA
#define PMySecurityFunctionTable PMySecurityFunctionTableA
#endif

#endif // !defined(FORWARD_ONLY)

namespace wingssQCAPlugin {

//----------------------------------------------------------------------------
// SSPI helper API
//----------------------------------------------------------------------------

typedef void (*sspi_logger_func)(const QString &str);

class SspiPackage
{
public:
    QString name;
    quint32 caps;
    quint32 maxtok;
    quint16 version;
    quint16 rpcid;
    QString comment;
};

// logger can be set even when sspi is not loaded (this is needed so logging
//   during sspi_load/unload can be captured).  pass 0 to disable.
void sspi_set_logger(sspi_logger_func p);

void sspi_log(const QString &str);

bool sspi_load();
void sspi_unload();

// returns the available security packages.  only the first call actually
//   queries the sspi subsystem.  subsequent calls return a cached result.
QList<SspiPackage> sspi_get_packagelist();

// refresh the package list cache.  call sspi_get_packagelist afterwards to
//   get the new list.
void sspi_refresh_packagelist();

// helper functions for logging
QString SECURITY_STATUS_toString(SECURITY_STATUS i);
QString ptr_toString(const void *p);

//----------------------------------------------------------------------------
// SSPI helper implementation
//----------------------------------------------------------------------------

Q_GLOBAL_STATIC(QMutex, sspi_mutex)
Q_GLOBAL_STATIC(QMutex, sspi_logger_mutex)

union SecurityFunctionTableUnion {
    PMySecurityFunctionTableW W;
    PSecurityFunctionTableA   A;
    void                     *ptr;
};

static QLibrary                  *sspi_lib = 0;
static SecurityFunctionTableUnion sspi;
static sspi_logger_func           sspi_logger;
static QList<SspiPackage>        *sspi_packagelist = 0;

void sspi_log(const QString &str)
{
    QMutexLocker locker(sspi_logger_mutex());

    if (sspi_logger)
        sspi_logger(str);
}

void sspi_set_logger(sspi_logger_func p)
{
    QMutexLocker locker(sspi_logger_mutex());

    sspi_logger = p;
}

#define CASE_SS_STRING(s)                                                                                              \
    case s:                                                                                                            \
        return #s;

static const char *SECURITY_STATUS_lookup(SECURITY_STATUS i)
{
    switch (i) {
        CASE_SS_STRING(SEC_E_OK);
        CASE_SS_STRING(SEC_I_COMPLETE_AND_CONTINUE);
        CASE_SS_STRING(SEC_I_COMPLETE_NEEDED);
        CASE_SS_STRING(SEC_I_CONTINUE_NEEDED);
        CASE_SS_STRING(SEC_I_INCOMPLETE_CREDENTIALS);
        CASE_SS_STRING(SEC_E_UNSUPPORTED_FUNCTION);
        CASE_SS_STRING(SEC_E_INVALID_TOKEN);
        CASE_SS_STRING(SEC_E_MESSAGE_ALTERED);
        CASE_SS_STRING(SEC_E_INSUFFICIENT_MEMORY);
        CASE_SS_STRING(SEC_E_INTERNAL_ERROR);
        CASE_SS_STRING(SEC_E_INVALID_HANDLE);
        CASE_SS_STRING(SEC_E_LOGON_DENIED);
        CASE_SS_STRING(SEC_E_NO_AUTHENTICATING_AUTHORITY);
        CASE_SS_STRING(SEC_E_NO_CREDENTIALS);
        CASE_SS_STRING(SEC_E_TARGET_UNKNOWN);
        CASE_SS_STRING(SEC_E_WRONG_PRINCIPAL);
        CASE_SS_STRING(SEC_E_BUFFER_TOO_SMALL);
        CASE_SS_STRING(SEC_E_CONTEXT_EXPIRED);
        CASE_SS_STRING(SEC_E_CRYPTO_SYSTEM_INVALID);
        CASE_SS_STRING(SEC_E_QOP_NOT_SUPPORTED);
        CASE_SS_STRING(SEC_E_INCOMPLETE_MESSAGE);
        CASE_SS_STRING(SEC_E_OUT_OF_SEQUENCE);
    default:
        break;
    }
    return 0;
}

QString SECURITY_STATUS_toString(SECURITY_STATUS i)
{
    const char *str = SECURITY_STATUS_lookup(i);
    if (str)
        return QString(str);
    else
        return QString::number(i);
}

QString ptr_toString(const void *p)
{
    return QString().sprintf("%p", p);
}

bool sspi_load()
{
    QMutexLocker locker(sspi_mutex());
    if (sspi_lib)
        return true;

    sspi_lib = new QLibrary("secur32");
    if (!sspi_lib->load()) {
        delete sspi_lib;
        sspi_lib = 0;
        return false;
    }

    union {
        INIT_SECURITY_INTERFACE_W W;
        INIT_SECURITY_INTERFACE_A A;
        void                     *ptr;
    } pInitSecurityInterface;
    pInitSecurityInterface.ptr = 0;

    QString securityEntrypoint;
    securityEntrypoint       = QString::fromUtf16((const ushort *)SECURITY_ENTRYPOINTW);
    pInitSecurityInterface.W = (INIT_SECURITY_INTERFACE_W)(sspi_lib->resolve(securityEntrypoint.toLatin1().data()));
    if (!pInitSecurityInterface.ptr) {
        sspi_lib->unload();
        delete sspi_lib;
        sspi_lib = 0;
        return false;
    }

    union {
        PMySecurityFunctionTableW W;
        PSecurityFunctionTableA   A;
        void                     *ptr;
    } funcs;
    funcs.ptr = 0;

    funcs.W = (PMySecurityFunctionTableW)pInitSecurityInterface.W();

    sspi_log(QString("%1() = %2\n").arg(securityEntrypoint, ptr_toString(funcs.ptr)));
    if (!funcs.ptr) {
        sspi_lib->unload();
        delete sspi_lib;
        sspi_lib = 0;
        return false;
    }

    sspi.W = funcs.W;

    return true;
}

void sspi_unload()
{
    QMutexLocker locker(sspi_mutex());

    sspi_lib->unload();
    delete sspi_lib;
    sspi_lib = 0;
    sspi.ptr = 0;
}

static QList<SspiPackage> sspi_get_packagelist_direct()
{
    QList<SspiPackage> out;

    ULONG           cPackages;
    SecPkgInfoW    *pPackageInfo;
    SECURITY_STATUS ret = sspi.W->EnumerateSecurityPackagesW(&cPackages, &pPackageInfo);
    sspi_log(QString("EnumerateSecurityPackages() = %1\n").arg(SECURITY_STATUS_toString(ret)));
    if (ret != SEC_E_OK)
        return out;

    for (int n = 0; n < (int)cPackages; ++n) {
        SecPkgInfoW *p = &pPackageInfo[n];
        SspiPackage  i;
        i.name    = QString::fromUtf16((const ushort *)p->Name);
        i.caps    = p->fCapabilities;
        i.version = p->wVersion;
        i.rpcid   = p->wRPCID;
        i.maxtok  = p->cbMaxToken;
        i.comment = QString::fromUtf16((const ushort *)p->Comment);
        out += i;
    }

    ret = sspi.W->FreeContextBuffer(&pPackageInfo);
    sspi_log(QString("FreeContextBuffer() = %1\n").arg(SECURITY_STATUS_toString(ret)));

    return out;
}

static void sspi_refresh_packagelist_internal()
{
    if (sspi_packagelist)
        *sspi_packagelist = sspi_get_packagelist_direct();
    else
        sspi_packagelist = new QList<SspiPackage>(sspi_get_packagelist_direct());
}

QList<SspiPackage> sspi_get_packagelist()
{
    QMutexLocker locker(sspi_mutex());

    if (!sspi_packagelist)
        sspi_refresh_packagelist_internal();
    return *sspi_packagelist;
}

void sspi_refresh_packagelist()
{
    QMutexLocker locker(sspi_mutex());

    sspi_refresh_packagelist_internal();
}

template<typename T> inline T cap_to_int(const T &t)
{
    if (sizeof(int) <= sizeof(T))
        return (int)((t > INT_MAX) ? INT_MAX : t);
    else
        return (int)t;
}

//----------------------------------------------------------------------------
// KerberosSession
//----------------------------------------------------------------------------
// this class thinly wraps SSPI to perform kerberos.
class KerberosSession
{
public:
    enum ReturnCode
    {
        Success,
        NeedMoreData, // for decrypt
        ErrorInvalidSystem,
        ErrorKerberosNotFound,
        ErrorAcquireCredentials,
        ErrorInitialize,
        ErrorQueryContext,
        ErrorEncrypt,
        ErrorDecrypt
    };

    SECURITY_STATUS lastErrorCode;

    quint32 maxtok;

    bool       initialized;
    bool       first_step;
    QByteArray first_out_token;
    bool       authed;

    QString spn;

    CredHandle user_cred;
    TimeStamp  user_cred_expiry;

    CtxtHandle                ctx;
    ULONG                     ctx_attr_req;
    ULONG                     ctx_attr;
    bool                      have_sizes;
    SecPkgContext_Sizes       ctx_sizes;
    SecPkgContext_StreamSizes ctx_streamSizes;

    KerberosSession()
        : initialized(false)
        , have_sizes(false)
    {
    }

    ~KerberosSession()
    {
        if (initialized) {
            SECURITY_STATUS ret = sspi.W->DeleteSecurityContext(&ctx);
            sspi_log(QString("DeleteSecurityContext() = %1\n").arg(SECURITY_STATUS_toString(ret)));

            ret = sspi.W->FreeCredentialsHandle(&user_cred);
            sspi_log(QString("FreeCredentialsHandle() = %1\n").arg(SECURITY_STATUS_toString(ret)));
        }
    }

    ReturnCode init(const QString &_spn)
    {
        // ensure kerberos is available
        bool               found    = false;
        quint32            _maxtok  = 0;
        QList<SspiPackage> packages = sspi_get_packagelist();
        sspi_log("SSPI packages:\n");
        foreach (const SspiPackage &p, packages) {
            bool gss = false;
            if (p.caps & SECPKG_FLAG_GSS_COMPATIBLE)
                gss = true;

            if (p.name == "Kerberos" && gss) {
                found   = true;
                _maxtok = p.maxtok;
            }

            QString gssstr = gss ? "yes" : "no";
            sspi_log(QString("  %1 (gss=%2, maxtok=%3)\n").arg(p.name, gssstr, QString::number(p.maxtok)));
        }

        if (!found)
            return ErrorKerberosNotFound;

        // get the logged-in user's credentials
        SECURITY_STATUS ret = sspi.W->AcquireCredentialsHandleW((SEC_WCHAR *)0, // we want creds of logged-in user
                                                                (SEC_WCHAR *)QString("Kerberos").utf16(),
                                                                SECPKG_CRED_OUTBOUND,
                                                                0, // don't need a LUID
                                                                0, // default credentials for kerberos
                                                                0, // not used
                                                                0, // not used
                                                                &user_cred,
                                                                &user_cred_expiry);
        sspi_log(QString("AcquireCredentialsHandle() = %1\n").arg(SECURITY_STATUS_toString(ret)));
        if (ret != SEC_E_OK) {
            lastErrorCode = ret;
            return ErrorAcquireCredentials;
        }

        maxtok = _maxtok;
        authed = false;
        spn    = _spn;

        SecBuffer outbuf;
        outbuf.BufferType = SECBUFFER_TOKEN;
        outbuf.cbBuffer   = 0;
        outbuf.pvBuffer   = NULL;

        SecBufferDesc outbufdesc;
        outbufdesc.ulVersion = SECBUFFER_VERSION;
        outbufdesc.cBuffers  = 1;
        outbufdesc.pBuffers  = &outbuf;

        ctx_attr_req = 0;

        // not strictly required, but some SSPI calls seem to always
        //   allocate memory, so for consistency we'll explicity
        //   request to have it that way all the time
        ctx_attr_req |= ISC_REQ_ALLOCATE_MEMORY;

        // required by SASL GSSAPI RFC
        ctx_attr_req |= ISC_REQ_INTEGRITY;

        // required for security layer
        ctx_attr_req |= ISC_REQ_MUTUAL_AUTH;
        ctx_attr_req |= ISC_REQ_SEQUENCE_DETECT;

        // required for encryption
        ctx_attr_req |= ISC_REQ_CONFIDENTIALITY;

        // other options that may be of use, but we currently aren't
        //   using:
        // ISC_REQ_DELEGATE
        // ISC_REQ_REPLAY_DETECT

        ret = sspi.W->InitializeSecurityContextW(&user_cred,
                                                 0, // NULL for the first call
                                                 (SEC_WCHAR *)spn.utf16(),
                                                 ctx_attr_req,
                                                 0,
                                                 SECURITY_NETWORK_DREP,
                                                 0, // NULL for first call
                                                 0,
                                                 &ctx,
                                                 &outbufdesc,
                                                 &ctx_attr,
                                                 0); // don't care about expiration
        sspi_log(QString("InitializeSecurityContext(*, 0, ...) = %1\n").arg(SECURITY_STATUS_toString(ret)));
        if (ret == SEC_E_OK || ret == SEC_I_CONTINUE_NEEDED) {
            if (outbuf.pvBuffer) {
                first_out_token.resize(outbuf.cbBuffer);
                memcpy(first_out_token.data(), outbuf.pvBuffer, outbuf.cbBuffer);

                SECURITY_STATUS fret = sspi.W->FreeContextBuffer(outbuf.pvBuffer);
                sspi_log(QString("FreeContextBuffer() = %1\n").arg(SECURITY_STATUS_toString(fret)));
            }

            if (ret == SEC_E_OK)
                authed = true;
        } else {
            // ret was an error, or some unexpected value like
            //   SEC_I_COMPLETE_NEEDED or
            //   SEC_I_COMPLETE_AND_CONTINUE, which i believe are
            //   not used for kerberos

            lastErrorCode = ret;

            ret = sspi.W->FreeCredentialsHandle(&user_cred);
            sspi_log(QString("FreeCredentialsHandle() = %1\n").arg(SECURITY_STATUS_toString(ret)));

            return ErrorInitialize;
        }

        initialized = true;
        first_step  = true;

        return Success;
    }

    ReturnCode step(const QByteArray &in, QByteArray *out, bool *authenticated)
    {
        if (authed) {
            out->clear();
            *authenticated = true;
            return Success;
        }

        if (first_step) {
            // ignore 'in'

            *out = first_out_token;
            first_out_token.clear();

            first_step = false;
        } else {
            SecBuffer outbuf;
            outbuf.BufferType = SECBUFFER_TOKEN;
            outbuf.cbBuffer   = 0;
            outbuf.pvBuffer   = NULL;

            SecBufferDesc outbufdesc;
            outbufdesc.ulVersion = SECBUFFER_VERSION;
            outbufdesc.cBuffers  = 1;
            outbufdesc.pBuffers  = &outbuf;

            SecBuffer inbuf;
            inbuf.BufferType = SECBUFFER_TOKEN;
            inbuf.cbBuffer   = in.size();
            inbuf.pvBuffer   = (void *)in.data();

            SecBufferDesc inbufdesc;
            inbufdesc.ulVersion = SECBUFFER_VERSION;
            inbufdesc.cBuffers  = 1;
            inbufdesc.pBuffers  = &inbuf;

            SECURITY_STATUS ret = sspi.W->InitializeSecurityContextW(&user_cred,
                                                                     &ctx,
                                                                     (SEC_WCHAR *)spn.utf16(),
                                                                     ctx_attr_req,
                                                                     0,
                                                                     SECURITY_NETWORK_DREP,
                                                                     &inbufdesc,
                                                                     0,
                                                                     &ctx,
                                                                     &outbufdesc,
                                                                     &ctx_attr,
                                                                     0); // don't care about expiration
            sspi_log(QString("InitializeSecurityContext(*, ctx, ...) = %1\n").arg(SECURITY_STATUS_toString(ret)));
            if (ret == SEC_E_OK || ret == SEC_I_CONTINUE_NEEDED) {
                if (outbuf.pvBuffer) {
                    out->resize(outbuf.cbBuffer);
                    memcpy(out->data(), outbuf.pvBuffer, outbuf.cbBuffer);

                    SECURITY_STATUS fret = sspi.W->FreeContextBuffer(outbuf.pvBuffer);
                    sspi_log(QString("FreeContextBuffer() = %1\n").arg(SECURITY_STATUS_toString(fret)));
                } else
                    out->clear();

                if (ret == SEC_E_OK)
                    authed = true;
            } else {
                // ret was an error, or some unexpected value like
                //   SEC_I_COMPLETE_NEEDED or
                //   SEC_I_COMPLETE_AND_CONTINUE, which i believe are
                //   not used for kerberos

                lastErrorCode = ret;

                ret = sspi.W->DeleteSecurityContext(&ctx);
                sspi_log(QString("DeleteSecurityContext() = %1\n").arg(SECURITY_STATUS_toString(ret)));

                ret = sspi.W->FreeCredentialsHandle(&user_cred);
                sspi_log(QString("FreeCredentialsHandle() = %1\n").arg(SECURITY_STATUS_toString(ret)));

                initialized = false;
                return ErrorInitialize;
            }
        }

        *authenticated = authed;
        return Success;
    }

    // private
    bool ensure_sizes_cached()
    {
        if (!have_sizes) {
            SECURITY_STATUS ret = sspi.W->QueryContextAttributesW(&ctx, SECPKG_ATTR_SIZES, &ctx_sizes);
            sspi_log(QString("QueryContextAttributes(ctx, SECPKG_ATTR_SIZES, ...) = %1\n")
                         .arg(SECURITY_STATUS_toString(ret)));
            if (ret != SEC_E_OK) {
                lastErrorCode = ret;
                return false;
            }

            // for some reason, querying the stream sizes returns
            //   SEC_E_UNSUPPORTED_FUNCTION on my system, even
            //   though the docs say it should work and putty
            //   wingss also calls it.

            // all we really need is cbMaximumMessage, and since
            //   we can't query for it, we'll hard code some
            //   value.  according to putty wingss, the max size
            //   is essentially unbounded anyway, so this should
            //   be safe to do.
            ctx_streamSizes.cbMaximumMessage = 8192;

            // ret = sspi.W->QueryContextAttributesW(&ctx, SECPKG_ATTR_STREAM_SIZES, &ctx_streamSizes);
            // sspi_log(QString("QueryContextAttributes(ctx, SECPKG_ATTR_STREAM_SIZES, ...) =
            // %1\n").arg(SECURITY_STATUS_toString(ret))); if(ret != SEC_E_OK)
            //{
            //	lastErrorCode = ret;
            //	return ErrorQueryContext;
            //}

            have_sizes = true;
        }

        return true;
    }

    ReturnCode get_max_encrypt_size(int *max)
    {
        if (!ensure_sizes_cached())
            return ErrorQueryContext;

        *max = cap_to_int<unsigned long>(ctx_streamSizes.cbMaximumMessage);

        return Success;
    }

    ReturnCode encode(const QByteArray &in, QByteArray *out, bool encrypt)
    {
        if (!ensure_sizes_cached())
            return ErrorQueryContext;

        QByteArray tokenbuf(ctx_sizes.cbSecurityTrailer, 0);
        QByteArray padbuf(ctx_sizes.cbBlockSize, 0);

        // we assume here, like putty wingss, that the output size is
        //   less than or equal to the input size.  honestly I don't
        //   see how this is clear from the SSPI documentation, but
        //   the code seems to work so we'll go with it...
        QByteArray databuf = in;

        SecBuffer buf[3];
        buf[0].BufferType = SECBUFFER_TOKEN;
        buf[0].cbBuffer   = tokenbuf.size();
        buf[0].pvBuffer   = tokenbuf.data();
        buf[1].BufferType = SECBUFFER_DATA;
        buf[1].cbBuffer   = databuf.size();
        buf[1].pvBuffer   = databuf.data();
        buf[2].BufferType = SECBUFFER_PADDING;
        buf[2].cbBuffer   = padbuf.size();
        buf[2].pvBuffer   = padbuf.data();

        SecBufferDesc bufdesc;
        bufdesc.ulVersion = SECBUFFER_VERSION;
        bufdesc.cBuffers  = 3;
        bufdesc.pBuffers  = buf;

        SECURITY_STATUS ret = sspi.W->EncryptMessage(&ctx, encrypt ? 0 : SECQOP_WRAP_NO_ENCRYPT, &bufdesc, 0);
        sspi_log(QString("EncryptMessage() = %1\n").arg(SECURITY_STATUS_toString(ret)));
        if (ret != SEC_E_OK) {
            lastErrorCode = ret;
            return ErrorEncrypt;
        }

        QByteArray fullbuf;
        for (int i = 0; i < (int)bufdesc.cBuffers; ++i)
            fullbuf += QByteArray((const char *)bufdesc.pBuffers[i].pvBuffer, bufdesc.pBuffers[i].cbBuffer);

        *out = fullbuf;
        return Success;
    }

    ReturnCode decode(const QByteArray &in, QByteArray *out, bool *encrypted)
    {
        SecBuffer buf[2];
        buf[0].BufferType = SECBUFFER_DATA;
        buf[0].cbBuffer   = 0;
        buf[0].pvBuffer   = NULL;
        buf[1].BufferType = SECBUFFER_STREAM;
        buf[1].cbBuffer   = in.size();
        buf[1].pvBuffer   = (void *)in.data();

        SecBufferDesc bufdesc;
        bufdesc.ulVersion = SECBUFFER_VERSION;
        bufdesc.cBuffers  = 2;
        bufdesc.pBuffers  = buf;

        ULONG           fQOP;
        SECURITY_STATUS ret = sspi.W->DecryptMessage(&ctx, &bufdesc, 0, &fQOP);
        sspi_log(QString("DecryptMessage() = %1\n").arg(SECURITY_STATUS_toString(ret)));
        if (ret == SEC_E_INCOMPLETE_MESSAGE) {
            return NeedMoreData;
        } else if (ret != SEC_E_OK) {
            lastErrorCode = ret;
            return ErrorDecrypt;
        }

        if (buf[0].pvBuffer) {
            out->resize(buf[0].cbBuffer);
            memcpy(out->data(), buf[0].pvBuffer, buf[0].cbBuffer);

            SECURITY_STATUS ret = sspi.W->FreeContextBuffer(buf[0].pvBuffer);
            sspi_log(QString("FreeContextBuffer() = %1\n").arg(SECURITY_STATUS_toString(ret)));
        } else
            out->clear();

        if (fQOP & SECQOP_WRAP_NO_ENCRYPT)
            *encrypted = false;
        else
            *encrypted = true;

        return Success;
    }
};

//----------------------------------------------------------------------------
// SaslGssapiSession
//----------------------------------------------------------------------------
// this class wraps KerberosSession to perform SASL GSSAPI.  it hides away
//   any SSPI details, and is thus very simple to use.
class SaslGssapiSession
{
private:
    int             secflags;
    KerberosSession sess;
    int             mode; // 0 = kerberos tokens, 1 = app packets
    bool            authed;
    QByteArray      inbuf;

    int max_enc_size; // most we can encrypt to them
    int max_dec_size; // most we are expected to decrypt from them

public:
    enum SecurityFlags
    {
        // only one of these should be set
        RequireAtLeastInt = 0x0001,
        RequireConf       = 0x0002
    };

    enum ReturnCode
    {
        Success,
        ErrorInit,
        ErrorKerberosStep,
        ErrorAppTokenDecode,
        ErrorAppTokenIsEncrypted,
        ErrorAppTokenWrongSize,
        ErrorAppTokenInvalid,
        ErrorAppTokenEncode,
        ErrorLayerTooWeak,
        ErrorEncode,
        ErrorDecode,
        ErrorDecodeTooLarge,
        ErrorDecodeNotEncrypted
    };

    // set this before auth, if you want
    QString authzid;

    // read-only
    bool do_layer, do_conf;

    SaslGssapiSession()
    {
    }

    ReturnCode init(const QString &proto, const QString &fqdn, int _secflags)
    {
        secflags = _secflags;
        mode     = 0; // kerberos tokens
        authed   = false;

        do_layer = false;
        do_conf  = false;

        if (sess.init(proto + '/' + fqdn) != KerberosSession::Success)
            return ErrorInit;

        return Success;
    }

    ReturnCode step(const QByteArray &in, QByteArray *out, bool *authenticated)
    {
        if (authed) {
            out->clear();
            *authenticated = true;
            return Success;
        }

        if (mode == 0) // kerberos tokens
        {
            bool kerb_authed;
            if (sess.step(in, out, &kerb_authed) != KerberosSession::Success)
                return ErrorKerberosStep;

            if (kerb_authed)
                mode = 1; // switch to app packets

            *authenticated = false;
        } else if (mode == 1) {
            bool layerPossible      = false;
            bool encryptionPossible = false;
            if (sess.ctx_attr & ISC_RET_INTEGRITY && sess.ctx_attr & ISC_RET_MUTUAL_AUTH &&
                sess.ctx_attr & ISC_RET_SEQUENCE_DETECT) {
                layerPossible = true;

                if (sess.ctx_attr & ISC_RET_CONFIDENTIALITY)
                    encryptionPossible = true;
            }

            if (layerPossible) {
                if (encryptionPossible)
                    sspi_log("Kerberos application data protection supported (with encryption)\n");
                else
                    sspi_log("Kerberos application data protection supported (without encryption)\n");
            } else
                sspi_log("No Kerberos application data protection supported\n");

            QByteArray decbuf;
            bool       encrypted;
            if (sess.decode(in, &decbuf, &encrypted) != KerberosSession::Success) {
                sspi_log("Error decoding application token\n");
                return ErrorAppTokenDecode;
            }

            // this packet is supposed to be not encrypted
            if (encrypted) {
                sspi_log("Error, application token is encrypted\n");
                return ErrorAppTokenIsEncrypted;
            }

            // packet must be exactly 4 bytes
            if (decbuf.size() != 4) {
                sspi_log("Error, application token is the wrong size\n");
                return ErrorAppTokenWrongSize;
            }

            QString str;
            str.sprintf("%02x%02x%02x%02x",
                        (unsigned int)decbuf[0],
                        (unsigned int)decbuf[1],
                        (unsigned int)decbuf[2],
                        (unsigned int)decbuf[3]);
            sspi_log(QString("Received application token: [%1]\n").arg(str));

            unsigned char layermask = decbuf[0];
            quint32       maxsize   = 0;
            maxsize += (unsigned char)decbuf[1];
            maxsize <<= 8;
            maxsize += (unsigned char)decbuf[2];
            maxsize <<= 8;
            maxsize += (unsigned char)decbuf[3];

            // if 'None' is all that is supported, then maxsize
            //   must be zero
            if (layermask == 1 && maxsize > 0) {
                sspi_log("Error, supports no security layer but the max buffer size is not zero\n");
                return ErrorAppTokenInvalid;
            }

            // convert maxsize to a signed int, by capping it
            int _max_enc_size = cap_to_int<quint32>(maxsize);

            // parse out layermask
            bool        saslLayerNone = false;
            bool        saslLayerInt  = false;
            bool        saslLayerConf = false;
            QStringList saslLayerModes;
            if (layermask & 1) {
                saslLayerNone = true;
                saslLayerModes += "None";
            }
            if (layermask & 2) {
                saslLayerInt = true;
                saslLayerModes += "Int";
            }
            if (layermask & 4) {
                saslLayerConf = true;
                saslLayerModes += "Conf";
            }

            sspi_log(QString("Security layer modes supported: %1\n").arg(saslLayerModes.join(", ")));
            sspi_log(QString("Security layer max packet size: %1\n").arg(maxsize));

            // create outbound application token
            QByteArray obuf(4, 0); // initially 4 bytes

            // set one of use_conf or use_int, but not both
            bool use_conf = false;
            bool use_int  = false;
            if (encryptionPossible && saslLayerConf)
                use_conf = true;
            else if (layerPossible && saslLayerInt)
                use_int = true;
            else if (!saslLayerNone) {
                sspi_log("Error, no compatible layer mode supported, not even 'None'\n");
                return ErrorLayerTooWeak;
            }

            if ((secflags & RequireConf) && !use_conf) {
                sspi_log("Error, 'Conf' required but not supported\n");
                return ErrorLayerTooWeak;
            }

            if ((secflags & RequireAtLeastInt) && !use_conf && !use_int) {
                sspi_log("Error, 'Conf' or 'Int' required but not supported\n");
                return ErrorLayerTooWeak;
            }

            if (use_conf) {
                sspi_log("Using 'Conf' layer\n");
                obuf[0] = 4;
            } else if (use_int) {
                sspi_log("Using 'Int' layer\n");
                obuf[0] = 2;
            } else {
                sspi_log("Using 'None' layer\n");
                obuf[0] = 1;
            }

            // as far as i can tell, there is no max decrypt size
            //   with sspi.  so we'll just pick some number.
            //   a small one is good, to prevent excessive input
            //   buffering.
            // in other parts of the code, it is assumed this
            //   value is less than INT_MAX
            int _max_dec_size = 8192; // same as cyrus

            // max size must be zero if no security layer is used
            if (!use_conf && !use_int)
                _max_dec_size = 0;

            obuf[1] = (unsigned char)((_max_dec_size >> 16) & 0xff);
            obuf[2] = (unsigned char)((_max_dec_size >> 8) & 0xff);
            obuf[3] = (unsigned char)((_max_dec_size) & 0xff);

            if (!authzid.isEmpty())
                obuf += authzid.toUtf8();

            str.clear();
            for (int n = 0; n < obuf.size(); ++n)
                str += QString().sprintf("%02x", (unsigned int)obuf[n]);
            sspi_log(QString("Sending application token: [%1]\n").arg(str));

            if (sess.encode(obuf, out, false) != KerberosSession::Success) {
                sspi_log("Error encoding application token\n");
                return ErrorAppTokenEncode;
            }

            if (use_conf || use_int)
                do_layer = true;
            if (use_conf)
                do_conf = true;

            max_enc_size = _max_enc_size;
            max_dec_size = _max_dec_size;

            *authenticated = true;
        }

        return Success;
    }

    ReturnCode encode(const QByteArray &in, QByteArray *out)
    {
        if (!do_layer) {
            *out = in;
            return Success;
        }

        int local_encrypt_max;
        if (sess.get_max_encrypt_size(&local_encrypt_max) != KerberosSession::Success)
            return ErrorEncode;

        // send no more per-packet than what our local system will
        //   encrypt AND no more than what the peer will accept.
        int chunk_max = qMin(local_encrypt_max, max_enc_size);
        if (chunk_max < 8) {
            sspi_log("Error, chunk_max is ridiculously small\n");
            return ErrorEncode;
        }

        QByteArray total_out;

        // break up into packets, if input exceeds max size
        int encoded = 0;
        while (encoded < in.size()) {
            int        left       = in.size() - encoded;
            int        chunk_size = qMin(left, chunk_max);
            QByteArray kerb_in    = QByteArray::fromRawData(in.data() + encoded, chunk_size);
            QByteArray kerb_out;
            if (sess.encode(kerb_in, &kerb_out, do_conf) != KerberosSession::Success)
                return ErrorEncode;

            QByteArray sasl_out(kerb_out.size() + 4, 0);

            // SASL (not GSS!) uses a 4 byte length prefix
            quint32 len = kerb_out.size();
            sasl_out[0] = (unsigned char)((len >> 24) & 0xff);
            sasl_out[1] = (unsigned char)((len >> 16) & 0xff);
            sasl_out[2] = (unsigned char)((len >> 8) & 0xff);
            sasl_out[3] = (unsigned char)((len) & 0xff);

            memcpy(sasl_out.data() + 4, kerb_out.data(), kerb_out.size());

            encoded += kerb_in.size();
            total_out += sasl_out;
        }

        *out = total_out;
        return Success;
    }

    ReturnCode decode(const QByteArray &in, QByteArray *out)
    {
        if (!do_layer) {
            *out = in;
            return Success;
        }

        // buffer the input
        inbuf += in;

        QByteArray total_out;

        // the buffer might contain many packets.  decode as many
        //   as possible
        while (1) {
            if (inbuf.size() < 4) {
                // need more data
                break;
            }

            // SASL (not GSS!) uses a 4 byte length prefix
            quint32 ulen = 0;
            ulen += (unsigned char)inbuf[0];
            ulen <<= 8;
            ulen += (unsigned char)inbuf[1];
            ulen <<= 8;
            ulen += (unsigned char)inbuf[2];
            ulen <<= 8;
            ulen += (unsigned char)inbuf[3];

            // this capping is safe, because we throw error if the value
            //   is too large, and an acceptable value will always be
            //   lower than the maximum integer size.
            int len = cap_to_int<quint32>(ulen);
            if (len > max_dec_size) {
                // this means the peer ignored our max buffer size.
                //   very evil, or we're under attack.
                sspi_log("Error, decode size too large\n");
                return ErrorDecodeTooLarge;
            }

            if (inbuf.size() - 4 < len) {
                // need more data
                break;
            }

            // take the packet from the inbuf
            QByteArray kerb_in = inbuf.mid(4, len);
            memmove(inbuf.data(), inbuf.data() + len + 4, inbuf.size() - len - 4);
            inbuf.resize(inbuf.size() - len - 4);

            // count incomplete packets as errors, since they are sasl framed
            QByteArray kerb_out;
            bool       encrypted;
            if (sess.decode(kerb_in, &kerb_out, &encrypted) != KerberosSession::Success)
                return ErrorDecode;

            if (do_conf && !encrypted) {
                sspi_log("Error, received unencrypted packet in 'Conf' mode\n");
                return ErrorDecodeNotEncrypted;
            }

            total_out += kerb_out;
        }

        *out = total_out;
        return Success;
    }
};

//----------------------------------------------------------------------------
// SaslWinGss
//----------------------------------------------------------------------------
class SaslWinGss : public SASLContext
{
    Q_OBJECT

public:
    SaslGssapiSession  *sess;
    bool                authed;
    Result              _result;
    SASL::AuthCondition _authCondition;
    QByteArray          _step_to_net;
    QByteArray          _to_net, _to_app;
    int                 enc;
    SafeTimer           resultsReadyTrigger;

    QString opt_service, opt_host, opt_ext_id;
    int     opt_ext_ssf;
    int     opt_flags;
    int     opt_minssf, opt_maxssf;

    QString opt_authzid;

    SaslWinGss(Provider *p)
        : SASLContext(p)
        , sess(0)
        , resultsReadyTrigger(this)
    {
        connect(&resultsReadyTrigger, SIGNAL(timeout()), SIGNAL(resultsReady()));
        resultsReadyTrigger.setSingleShot(true);
    }

    Provider::Context *clone() const
    {
        return 0;
    }

    virtual void reset()
    {
        delete sess;
        sess   = 0;
        authed = false;
        _step_to_net.clear();
        _to_net.clear();
        _to_app.clear();
        resultsReadyTrigger.stop();

        opt_service.clear();
        opt_host.clear();
        opt_ext_id.clear();
        opt_authzid.clear();
    }

    virtual void setup(const QString  &service,
                       const QString  &host,
                       const HostPort *local,
                       const HostPort *remote,
                       const QString  &ext_id,
                       int             ext_ssf)
    {
        // unused by this provider
        Q_UNUSED(local);
        Q_UNUSED(remote);

        opt_service = service;
        opt_host    = host;
        opt_ext_id  = ext_id;
        opt_ext_ssf = ext_ssf;
    }

    virtual void setConstraints(SASL::AuthFlags f, int minSSF, int maxSSF)
    {
        opt_flags  = (int)f;
        opt_minssf = minSSF;
        opt_maxssf = maxSSF;
    }

    virtual void startClient(const QStringList &mechlist, bool allowClientSendFirst)
    {
        // we only support GSSAPI
        if (!mechlist.contains("GSSAPI")) {
            _result        = Error;
            _authCondition = SASL::NoMechanism;
            resultsReadyTrigger.start();
            return;
        }

        // GSSAPI (or this provider) doesn't meet these requirements
        if (opt_flags & SASL::RequireForwardSecrecy || opt_flags & SASL::RequirePassCredentials ||
            !allowClientSendFirst) {
            _result        = Error;
            _authCondition = SASL::NoMechanism;
            resultsReadyTrigger.start();
            return;
        }

        sess          = new SaslGssapiSession;
        sess->authzid = opt_authzid;

        int secflags = 0;
        if (opt_minssf > 1)
            secflags |= SaslGssapiSession::RequireConf;
        else if (opt_minssf == 1)
            secflags |= SaslGssapiSession::RequireAtLeastInt;

        SaslGssapiSession::ReturnCode ret = sess->init(opt_service, opt_host, secflags);
        if (ret != SaslGssapiSession::Success) {
            _result        = Error;
            _authCondition = SASL::AuthFail;
            resultsReadyTrigger.start();
            return;
        }

        ret = sess->step(QByteArray(), &_step_to_net, &authed);
        if (ret != SaslGssapiSession::Success) {
            _result        = Error;
            _authCondition = SASL::AuthFail;
            resultsReadyTrigger.start();
            return;
        }

        if (authed)
            _result = Success;
        else
            _result = Continue;

        resultsReadyTrigger.start();
    }

    virtual void startServer(const QString &realm, bool disableServerSendLast)
    {
        // server mode unsupported at this time
        Q_UNUSED(realm);
        Q_UNUSED(disableServerSendLast);

        _result        = Error;
        _authCondition = SASL::AuthFail;
        resultsReadyTrigger.start();
    }

    virtual void serverFirstStep(const QString &mech, const QByteArray *clientInit)
    {
        // server mode unsupported at this time
        Q_UNUSED(mech);
        Q_UNUSED(clientInit);
    }

    virtual void nextStep(const QByteArray &from_net)
    {
        SaslGssapiSession::ReturnCode ret = sess->step(from_net, &_step_to_net, &authed);
        if (ret != SaslGssapiSession::Success) {
            _result        = Error;
            _authCondition = SASL::AuthFail;
            resultsReadyTrigger.start();
            return;
        }

        if (authed)
            _result = Success;
        else
            _result = Continue;

        resultsReadyTrigger.start();
    }

    virtual void tryAgain()
    {
        // we never ask for params, so this function should never be
        //   called
    }

    virtual void update(const QByteArray &from_net, const QByteArray &from_app)
    {
        SaslGssapiSession::ReturnCode ret;
        QByteArray                    a;

        if (!from_net.isEmpty()) {
            ret = sess->decode(from_net, &a);
            if (ret != SaslGssapiSession::Success) {
                _result = Error;
                resultsReadyTrigger.start();
                return;
            }

            _to_app += a;
        }

        if (!from_app.isEmpty()) {
            ret = sess->encode(from_app, &a);
            if (ret != SaslGssapiSession::Success) {
                _result = Error;
                resultsReadyTrigger.start();
                return;
            }

            _to_net += a;
            enc += from_app.size();
        }

        _result = Success;
        resultsReadyTrigger.start();
    }

    virtual bool waitForResultsReady(int msecs)
    {
        // all results are ready instantly
        Q_UNUSED(msecs);
        resultsReadyTrigger.stop();
        return true;
    }

    virtual Result result() const
    {
        return _result;
    }

    virtual QStringList mechlist() const
    {
        // server mode unsupported at this time
        return QStringList();
    }

    virtual QString mech() const
    {
        // only mech we support :)
        return "GSSAPI";
    }

    virtual bool haveClientInit() const
    {
        // GSSAPI always has a client init response
        return true;
    }

    virtual QByteArray stepData() const
    {
        return _step_to_net;
    }

    virtual QByteArray to_net()
    {
        QByteArray a = _to_net;
        _to_net.clear();
        enc = 0;
        return a;
    }

    virtual int encoded() const
    {
        return enc;
    }

    virtual QByteArray to_app()
    {
        QByteArray a = _to_app;
        _to_app.clear();
        return a;
    }

    virtual int ssf() const
    {
        if (!sess->do_layer)
            return 0;

        if (sess->do_conf) {
            // TODO: calculate this value somehow?  for now we'll
            //   just hard code it to 56, which is basically what
            //   cyrus does.
            return 56;
        } else
            return 1;
    }

    virtual SASL::AuthCondition authCondition() const
    {
        return _authCondition;
    }

    virtual SASL::Params clientParams() const
    {
        // we never ask for params
        return SASL::Params();
    }

    virtual void
    setClientParams(const QString *user, const QString *authzid, const SecureArray *pass, const QString *realm)
    {
        // unused by this provider
        Q_UNUSED(user);
        Q_UNUSED(pass);
        Q_UNUSED(realm);

        if (authzid) {
            opt_authzid = *authzid;
            if (sess)
                sess->authzid = opt_authzid;
        } else {
            opt_authzid.clear();
            if (sess)
                sess->authzid.clear();
        }
    }

    virtual QStringList realmlist() const
    {
        // unused by this provider
        return QStringList();
    }

    virtual QString username() const
    {
        // server mode unsupported at this time
        return QString();
    }

    virtual QString authzid() const
    {
        // server mode unsupported at this time
        return QString();
    }
};

#endif // !defined(FORWARD_ONLY)

//----------------------------------------------------------------------------
// MetaSasl
//----------------------------------------------------------------------------
#ifndef FORWARD_ONLY
class wingssProvider;
static bool wingssProvider_have_sspi(wingssProvider *provider);
#endif

class MetaSasl : public SASLContext
{
    Q_OBJECT

public:
    SASLContext *s;

    Result              _result;
    SASL::AuthCondition _authCondition;
    SafeTimer           resultsReadyTrigger;
    Synchronizer        sync;
    bool                waiting;

    QString         opt_service, opt_host;
    bool            have_opt_local, have_opt_remote;
    HostPort        opt_local, opt_remote;
    QString         opt_ext_id;
    int             opt_ext_ssf;
    SASL::AuthFlags opt_flags;
    int             opt_minssf, opt_maxssf;

    bool        have_opt_user, have_opt_authzid, have_opt_pass, have_opt_realm;
    QString     opt_user, opt_authzid, opt_realm;
    SecureArray opt_pass;

    class SaslProvider
    {
    public:
        SASLContext *sasl;
        bool         ready;
        QStringList  mechlist;

        SaslProvider()
            : sasl(0)
            , ready(false)
        {
        }
    };

    QList<SaslProvider> saslProviders;
    bool                serverInit_active;
    Result              serverInit_result;
    QStringList         serverInit_mechlist;

    MetaSasl(Provider *p)
        : SASLContext(p)
        , resultsReadyTrigger(this)
        , sync(this)
        , waiting(false)
        , serverInit_active(false)
    {
        s = 0;

        have_opt_user    = false;
        have_opt_authzid = false;
        have_opt_pass    = false;
        have_opt_realm   = false;

        connect(&resultsReadyTrigger, SIGNAL(timeout()), SIGNAL(resultsReady()));
        resultsReadyTrigger.setSingleShot(true);
    }

    ~MetaSasl()
    {
        delete s;
    }

    virtual Provider::Context *clone() const
    {
        return 0;
    }

    void clearSaslProviders()
    {
        foreach (const SaslProvider &sp, saslProviders)
            delete sp.sasl;

        saslProviders.clear();
    }

    virtual void reset()
    {
        delete s;
        s = 0;

        resultsReadyTrigger.stop();

        opt_service.clear();
        opt_host.clear();
        opt_ext_id.clear();
        opt_user.clear();
        opt_authzid.clear();
        opt_realm.clear();
        opt_pass.clear();

        have_opt_user    = false;
        have_opt_authzid = false;
        have_opt_pass    = false;
        have_opt_realm   = false;

        clearSaslProviders();
        serverInit_active = false;
        serverInit_mechlist.clear();
    }

    virtual void setup(const QString  &service,
                       const QString  &host,
                       const HostPort *local,
                       const HostPort *remote,
                       const QString  &ext_id,
                       int             ext_ssf)
    {
        opt_service     = service;
        opt_host        = host;
        have_opt_local  = false;
        have_opt_remote = false;
        if (local) {
            have_opt_local = true;
            opt_local      = *local;
        }
        if (remote) {
            have_opt_remote = true;
            opt_remote      = *remote;
        }
        opt_ext_id  = ext_id;
        opt_ext_ssf = ext_ssf;
    }

    virtual void setConstraints(SASL::AuthFlags f, int minSSF, int maxSSF)
    {
        opt_flags  = f;
        opt_minssf = minSSF;
        opt_maxssf = maxSSF;
    }

    virtual void startClient(const QStringList &mechlist, bool allowClientSendFirst)
    {
#ifndef FORWARD_ONLY
        if (mechlist.contains("GSSAPI") && wingssProvider_have_sspi((wingssProvider *)provider())) {
            s = new SaslWinGss(provider());
        } else {
#endif
            // collect providers supporting sasl, in priority order.
            //   (note: providers() is in priority order already)
            ProviderList list;
            foreach (Provider *p, providers()) {
                QString name = p->name();

                // skip ourself
                if (name == PROVIDER_NAME)
                    continue;

                if (p->features().contains("sasl")) {
                    // FIXME: improve qca so this isn't needed
                    SASL tmp_object_to_cause_plugin_init(0, name);

                    // add to the list
                    list += p;
                }
            }

            if (!list.isEmpty()) {
                // use the first
                s = static_cast<SASLContext *>(list.first()->createContext("sasl"));
            }
#ifndef FORWARD_ONLY
        }
#endif

        if (!s) {
            // no usable provider?  throw error
            _result        = Error;
            _authCondition = SASL::NoMechanism;
            resultsReadyTrigger.start();
            return;
        }

        // proper parenting
        s->setParent(this);

        const HostPort *pLocal  = 0;
        const HostPort *pRemote = 0;
        if (have_opt_local)
            pLocal = &opt_local;
        if (have_opt_remote)
            pRemote = &opt_remote;
        s->setup(opt_service, opt_host, pLocal, pRemote, opt_ext_id, opt_ext_ssf);
        s->setConstraints(opt_flags, opt_minssf, opt_maxssf);

        const QString     *pUser    = 0;
        const QString     *pAuthzid = 0;
        const SecureArray *pPass    = 0;
        const QString     *pRealm   = 0;
        if (have_opt_user)
            pUser = &opt_user;
        if (have_opt_authzid)
            pAuthzid = &opt_authzid;
        if (have_opt_pass)
            pPass = &opt_pass;
        if (have_opt_realm)
            pRealm = &opt_realm;
        s->setClientParams(pUser, pAuthzid, pPass, pRealm);
        connect(s, SIGNAL(resultsReady()), SLOT(s_resultsReady()));

        QString str = QString("MetaSasl: client using %1 with %2 mechs")
                          .arg(s->provider()->name(), QString::number(mechlist.count()));
        QCA_logTextMessage(str, Logger::Debug);
        s->startClient(mechlist, allowClientSendFirst);
    }

    virtual void startServer(const QString &realm, bool disableServerSendLast)
    {
        // collect mechs of all providers, by starting all of them
        serverInit_active = true;

        ProviderList list;
        foreach (Provider *p, providers()) {
            QString name = p->name();

            // skip ourself
            if (name == PROVIDER_NAME)
                continue;

            if (p->features().contains("sasl")) {
                // FIXME: improve qca so this isn't needed
                SASL tmp_object_to_cause_plugin_init(0, name);

                // add to the list
                list += p;
            }
        }

        foreach (Provider *p, list) {
            SaslProvider sp;

            sp.sasl = static_cast<SASLContext *>(p->createContext("sasl"));

            // proper parenting
            sp.sasl->setParent(this);

            const HostPort *pLocal  = 0;
            const HostPort *pRemote = 0;
            if (have_opt_local)
                pLocal = &opt_local;
            if (have_opt_remote)
                pRemote = &opt_remote;
            sp.sasl->setup(opt_service, opt_host, pLocal, pRemote, opt_ext_id, opt_ext_ssf);
            sp.sasl->setConstraints(opt_flags, opt_minssf, opt_maxssf);
            connect(sp.sasl, SIGNAL(resultsReady()), SLOT(serverInit_resultsReady()));

            saslProviders += sp;

            sp.sasl->startServer(realm, disableServerSendLast);
        }
    }

    virtual void serverFirstStep(const QString &mech, const QByteArray *clientInit)
    {
        // choose a provider based on the mech
        int at = choose_provider(mech);

        // extract it and clean up the rest
        SASLContext *sasl = saslProviders[at].sasl;
        sasl->disconnect(this);
        saslProviders.removeAt(at);
        clearSaslProviders();
        serverInit_active = false;

        // use the chosen provider
        s = sasl;
        connect(s, SIGNAL(resultsReady()), SLOT(s_resultsReady()));
        s->serverFirstStep(mech, clientInit);
    }

    virtual void nextStep(const QByteArray &from_net)
    {
        s->nextStep(from_net);
    }

    virtual void tryAgain()
    {
        s->tryAgain();
    }

    virtual void update(const QByteArray &from_net, const QByteArray &from_app)
    {
        s->update(from_net, from_app);
    }

    virtual bool waitForResultsReady(int msecs)
    {
        if (serverInit_active) {
            waiting  = true;
            bool ret = sync.waitForCondition(msecs);
            waiting  = false;
            return ret;
        } else if (s)
            return s->waitForResultsReady(msecs);
        else
            return true;
    }

    virtual Result result() const
    {
        if (serverInit_active)
            return serverInit_result;
        else if (s)
            return s->result();
        else
            return _result;
    }

    virtual QStringList mechlist() const
    {
        return serverInit_mechlist;
    }

    virtual QString mech() const
    {
        if (s)
            return s->mech();
        else
            return QString();
    }

    virtual bool haveClientInit() const
    {
        return s->haveClientInit();
    }

    virtual QByteArray stepData() const
    {
        return s->stepData();
    }

    virtual QByteArray to_net()
    {
        return s->to_net();
    }

    virtual int encoded() const
    {
        return s->encoded();
    }

    virtual QByteArray to_app()
    {
        return s->to_app();
    }

    virtual int ssf() const
    {
        return s->ssf();
    }

    virtual SASL::AuthCondition authCondition() const
    {
        if (s)
            return s->authCondition();
        else
            return _authCondition;
    }

    virtual SASL::Params clientParams() const
    {
        return s->clientParams();
    }

    virtual void
    setClientParams(const QString *user, const QString *authzid, const SecureArray *pass, const QString *realm)
    {
        if (!s) {
            if (user) {
                have_opt_user = true;
                opt_user      = *user;
            }
            if (authzid) {
                have_opt_authzid = true;
                opt_authzid      = *authzid;
            }
            if (pass) {
                have_opt_pass = true;
                opt_pass      = *pass;
            }
            if (realm) {
                have_opt_realm = true;
                opt_realm      = *realm;
            }
        } else {
            s->setClientParams(user, authzid, pass, realm);
        }
    }

    virtual QStringList realmlist() const
    {
        return s->realmlist();
    }

    virtual QString username() const
    {
        return s->username();
    }

    virtual QString authzid() const
    {
        return s->authzid();
    }

private Q_SLOTS:
    void s_resultsReady()
    {
        emit resultsReady();
    }

    void serverInit_resultsReady()
    {
        SASLContext *sasl = (SASLContext *)sender();

        int at = -1;
        for (int n = 0; n < saslProviders.count(); ++n) {
            if (saslProviders[n].sasl == sasl) {
                at = n;
                break;
            }
        }
        if (at == -1)
            return;

        if (sasl->result() == Success) {
            saslProviders[at].ready    = true;
            saslProviders[at].mechlist = sasl->mechlist();

            bool allReady = true;
            for (int n = 0; n < saslProviders.count(); ++n) {
                if (!saslProviders[n].ready) {
                    allReady = false;
                    break;
                }
            }

            if (allReady) {
                // indicate success
                serverInit_result   = Success;
                serverInit_mechlist = combine_mechlists();

                if (waiting)
                    sync.conditionMet();
                else
                    emit resultsReady();
            }
        } else {
            delete sasl;
            saslProviders.removeAt(at);

            if (saslProviders.isEmpty()) {
                // indicate error
                serverInit_result = Error;
                _authCondition    = SASL::NoMechanism;

                if (waiting)
                    sync.conditionMet();
                else
                    emit resultsReady();
            }
        }
    }

private:
    QStringList combine_mechlists()
    {
        QStringList out;

        // FIXME: consider prioritizing certain mechs?
        foreach (const SaslProvider &sp, saslProviders) {
            foreach (const QString &mech, sp.mechlist) {
                if (!out.contains(mech))
                    out += mech;
            }
        }

        return out;
    }

    int choose_provider(const QString &mech)
    {
        int at = -1;

        // find a provider for this mech
        for (int n = 0; n < saslProviders.count(); ++n) {
            const SaslProvider &sp = saslProviders[n];
            if (sp.mechlist.contains(mech)) {
                at = n;
                break;
            }
        }

        // no provider offered this mech?  then just go with the
        //   first provider
        if (at == -1)
            at = 0;

        return at;
    }
};

class wingssProvider : public Provider
{
public:
    mutable QMutex m;
    mutable bool   forced_priority;
    bool           have_sspi;

    wingssProvider()
        : forced_priority(false)
        , have_sspi(false)
    {
    }

    virtual void init()
    {
#ifndef FORWARD_ONLY
        sspi_set_logger(do_log);
        have_sspi = sspi_load();
#endif
    }

    ~wingssProvider()
    {
#ifndef FORWARD_ONLY
        if (have_sspi)
            sspi_unload();
#endif
    }

    virtual int qcaVersion() const
    {
        return QCA_VERSION;
    }

    virtual QString name() const
    {
        return PROVIDER_NAME;
    }

    virtual QStringList features() const
    {
        // due to context manipulation, this plugin is only designed
        //   for qca 2.0 at this time, and not a possible 2.1, etc.
        if ((qcaVersion() & 0xffff00) > 0x020000)
            return QStringList();

        m.lock();
        // FIXME: we need to prioritize this plugin to be higher
        //   than other plugins by default.  unfortunately there's
        //   no clean way to do this.  we can't change our priority
        //   until we are slotted into the qca provider system.  the
        //   constructor, qcaVersion, and name functions are all
        //   guaranteed to be called, but unfortunately they are
        //   only guaranteed to be called before the slotting.  the
        //   features function is essentially guaranteed to be called
        //   after the slotting though, since QCA::isSupported()
        //   trips it, and any proper QCA app will call isSupported.
        if (!forced_priority) {
            forced_priority = true;
            setProviderPriority(PROVIDER_NAME, 0);
        }
        m.unlock();

        QStringList list;
        list += "sasl";
        return list;
    }

    virtual Context *createContext(const QString &type)
    {
        if (type == "sasl")
            return new MetaSasl(this);
        else
            return 0;
    }

#ifndef FORWARD_ONLY
    static void do_log(const QString &str)
    {
        QCA_logTextMessage(str, Logger::Debug);
    }
#endif
};

#ifndef FORWARD_ONLY
bool wingssProvider_have_sspi(wingssProvider *provider)
{
    return provider->have_sspi;
}
#endif

}

using namespace wingssQCAPlugin;

//----------------------------------------------------------------------------
// wingssPlugin
//----------------------------------------------------------------------------

class wingssPlugin : public QObject, public QCAPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "com.affinix.qca.Plugin/1.0")
    Q_INTERFACES(QCAPlugin)

public:
    virtual Provider *createProvider()
    {
        return new wingssProvider;
    }
};

#include "qca-wingss.moc"