summaryrefslogtreecommitdiffstats
blob: 29bb33876b3f33a144919600f5073243192dbfeb (plain)
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
/*
 * Copyright (c) 2012-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <Swiften/TLS/CAPICertificate.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/bind.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Network/TimerFactory.h>
#include <Swiften/StringCodecs/Hexify.h>

// Size of the SHA1 hash
#define SHA1_HASH_LEN 20

#define DEBUG_SCARD_STATUS(function, status) \
{ \
    boost::shared_ptr<boost::system::error_code> errorCode = boost::make_shared<boost::system::error_code>(status, boost::system::system_category()); \
    SWIFT_LOG(debug) << std::hex << function << ": status: 0x" << status << ": " << errorCode->message() << std::endl; \
}

namespace Swift {

CAPICertificate::CAPICertificate(const std::string& capiUri, TimerFactory* timerFactory) :
        valid_(false),
        uri_(capiUri),
        certStoreHandle_(0),
        scardContext_(0),
        cardHandle_(0),
        certStore_(),
        certName_(),
        smartCardReaderName_(),
        timerFactory_(timerFactory),
        lastPollingResult_(true) {
    assert(timerFactory_);

    setUri(capiUri);
}

CAPICertificate::~CAPICertificate() {
    if (smartCardTimer_) {
        smartCardTimer_->stop();
        smartCardTimer_->onTick.disconnect(boost::bind(&CAPICertificate::handleSmartCardTimerTick, this));
        smartCardTimer_.reset();
    }

    if (certStoreHandle_) {
        CertCloseStore(certStoreHandle_, 0);
    }

    if (cardHandle_) {
        LONG result = SCardDisconnect(cardHandle_, SCARD_LEAVE_CARD);
        DEBUG_SCARD_STATUS("SCardDisconnect", result);
    }

    if (scardContext_) {
        LONG result = SCardReleaseContext(scardContext_);
        DEBUG_SCARD_STATUS("SCardReleaseContext", result);
    }
}

bool CAPICertificate::isNull() const {
    return uri_.empty() || !valid_;
}

const std::string& CAPICertificate::getCertStoreName() const {
    return certStore_;
}

const std::string& CAPICertificate::getCertName() const {
    return certName_;
}

const std::string& CAPICertificate::getSmartCardReaderName() const {
    return smartCardReaderName_;
}

PCCERT_CONTEXT findCertificateInStore (HCERTSTORE certStoreHandle, const std::string &certName) {
    if (!boost::iequals(certName.substr(0, 5), "sha1:")) {

        // Find client certificate. Note that this sample just searches for a
        // certificate that contains the user name somewhere in the subject name.
        return CertFindCertificateInStore(certStoreHandle, X509_ASN_ENCODING, /*dwFindFlags*/ 0, CERT_FIND_SUBJECT_STR_A, /* *pvFindPara*/certName.c_str(), /*pPrevCertContext*/ NULL);
    }


    std::string hexstring = certName.substr(5);
    ByteArray byteArray = Hexify::unhexify(hexstring);
    CRYPT_HASH_BLOB HashBlob;

    if (byteArray.size() != SHA1_HASH_LEN) {
        return NULL;
    }
    HashBlob.cbData = SHA1_HASH_LEN;
    HashBlob.pbData = static_cast<BYTE *>(vecptr(byteArray));

    // Find client certificate. Note that this sample just searches for a
    // certificate that contains the user name somewhere in the subject name.
    return CertFindCertificateInStore(certStoreHandle, X509_ASN_ENCODING, /* dwFindFlags */ 0, CERT_FIND_HASH, &HashBlob, /* pPrevCertContext */ NULL);

}


void CAPICertificate::setUri (const std::string& capiUri) {
    valid_ = false;

    /* Syntax: "certstore:" <cert_store> ":" <hash> ":" <hash_of_cert> */

    if (!boost::iequals(capiUri.substr(0, 10), "certstore:")) {
        return;
    }

    /* Substring of subject: uses "storename" */
    std::string capiIdentity = capiUri.substr(10);
    std::string newCertStoreName;
    size_t pos = capiIdentity.find_first_of (':');

    if (pos == std::string::npos) {
        /* Using the default certificate store */
        newCertStoreName = "MY";
        certName_ = capiIdentity;
    }
    else {
        newCertStoreName = capiIdentity.substr(0, pos);
        certName_ = capiIdentity.substr(pos + 1);
    }

    if (certStoreHandle_ != NULL) {
        if (newCertStoreName != certStore_) {
            CertCloseStore(certStoreHandle_, 0);
            certStoreHandle_ = NULL;
        }
    }

    if (certStoreHandle_ == NULL) {
        certStoreHandle_ = CertOpenSystemStore(0, newCertStoreName.c_str());
        if (!certStoreHandle_) {
            return;
        }
    }

    certStore_ = newCertStoreName;

    PCCERT_CONTEXT certContext = findCertificateInStore (certStoreHandle_, certName_);

    if (!certContext) {
        return;
    }


    /* Now verify that we can have access to the corresponding private key */

    DWORD len;
    CRYPT_KEY_PROV_INFO *pinfo;
    HCRYPTPROV hprov;
    HCRYPTKEY key;

    if (!CertGetCertificateContextProperty(certContext,
            CERT_KEY_PROV_INFO_PROP_ID,
            NULL,
            &len)) {
        CertFreeCertificateContext(certContext);
        return;
    }

    pinfo = static_cast<CRYPT_KEY_PROV_INFO *>(malloc(len));
    if (!pinfo) {
        CertFreeCertificateContext(certContext);
        return;
    }

    if (!CertGetCertificateContextProperty(certContext, CERT_KEY_PROV_INFO_PROP_ID, pinfo, &len)) {
        CertFreeCertificateContext(certContext);
        free(pinfo);
        return;
    }

    CertFreeCertificateContext(certContext);

    // Now verify if we have access to the private key
    if (!CryptAcquireContextW(&hprov, pinfo->pwszContainerName, pinfo->pwszProvName, pinfo->dwProvType, 0)) {
        free(pinfo);
        return;
    }


    char smartCardReader[1024];
    DWORD bufferLength = sizeof(smartCardReader);
    if (!CryptGetProvParam(hprov, PP_SMARTCARD_READER, (BYTE *)&smartCardReader, &bufferLength, 0)) {
        DWORD error = GetLastError();
        smartCardReaderName_ = "";
    }
    else {
        smartCardReaderName_ = smartCardReader;

        LONG result = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &scardContext_);
        DEBUG_SCARD_STATUS("SCardEstablishContext", result);
        if (SCARD_S_SUCCESS == result) {
            // Initiate monitoring for smartcard ejection
            smartCardTimer_ = timerFactory_->createTimer(SMARTCARD_EJECTION_CHECK_FREQUENCY_MILLISECONDS);
        }
        else {
            ///Need to handle an error here
        }
    }

    if (!CryptGetUserKey(hprov, pinfo->dwKeySpec, &key)) {
        CryptReleaseContext(hprov, 0);
        free(pinfo);
        return;
    }

    CryptDestroyKey(key);
    CryptReleaseContext(hprov, 0);
    free(pinfo);

    if (smartCardTimer_) {
        smartCardTimer_->onTick.connect(boost::bind(&CAPICertificate::handleSmartCardTimerTick, this));
        smartCardTimer_->start();
    }

    valid_ = true;
}

static void smartcard_check_status (SCARDCONTEXT hContext,
        const char* pReader,
        SCARDHANDLE hCardHandle, /* Can be 0 on the first call */
        SCARDHANDLE* newCardHandle, /* The handle returned */
        DWORD* pdwState) {
    DWORD shareMode = SCARD_SHARE_SHARED;
    DWORD preferredProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
    DWORD dwAP;
    LONG result;

    if (hCardHandle == 0) {
        result = SCardConnect(hContext, pReader, shareMode, preferredProtocols, &hCardHandle, &dwAP);
        DEBUG_SCARD_STATUS("SCardConnect", result);
        if (SCARD_S_SUCCESS != result) {
            hCardHandle = 0;
        }
    }

    char szReader[200];
    DWORD cch = sizeof(szReader);
    BYTE bAttr[32];
    DWORD cByte = 32;
    size_t countStatusAttempts = 0;

    while (hCardHandle && (countStatusAttempts < 2)) {
        *pdwState = SCARD_UNKNOWN;

        result = SCardStatus(hCardHandle, /* Unfortunately we can't use NULL here */ szReader, &cch, pdwState, NULL, (LPBYTE)&bAttr, &cByte);
        DEBUG_SCARD_STATUS("SCardStatus", result);
        countStatusAttempts++;

        if ((SCARD_W_RESET_CARD == result) && (countStatusAttempts < 2)) {
            result = SCardReconnect(hCardHandle, shareMode, preferredProtocols, SCARD_RESET_CARD, &dwAP);
            DEBUG_SCARD_STATUS("SCardReconnect", result);
            if (SCARD_S_SUCCESS != result) {
                break;
            }
        }
        else {
            break;
        }
    }

    if (SCARD_S_SUCCESS != result) {
        if (SCARD_E_NO_SMARTCARD == result || SCARD_W_REMOVED_CARD == result) {
            *pdwState = SCARD_ABSENT;
        }
        else {
            *pdwState = SCARD_UNKNOWN;
        }
    }

    if (newCardHandle == NULL) {
        result = SCardDisconnect(hCardHandle, SCARD_LEAVE_CARD);
        DEBUG_SCARD_STATUS("SCardDisconnect", result);
    }
    else {
        *newCardHandle = hCardHandle;
    }
}

bool CAPICertificate::checkIfSmartCardPresent () {
    if (!smartCardReaderName_.empty()) {
        DWORD dwState;
        smartcard_check_status(scardContext_, smartCardReaderName_.c_str(), cardHandle_, &cardHandle_, &dwState);

        switch (dwState) {
            case SCARD_ABSENT:
                SWIFT_LOG(debug) << "Card absent." << std::endl;
                break;
            case SCARD_PRESENT:
                SWIFT_LOG(debug) << "Card present." << std::endl;
                break;
            case SCARD_SWALLOWED:
                SWIFT_LOG(debug) << "Card swallowed." << std::endl;
                break;
            case SCARD_POWERED:
                SWIFT_LOG(debug) << "Card has power." << std::endl;
                break;
            case SCARD_NEGOTIABLE:
                SWIFT_LOG(debug) << "Card reset and waiting PTS negotiation." << std::endl;
                break;
            case SCARD_SPECIFIC:
                SWIFT_LOG(debug) << "Card has specific communication protocols set." << std::endl;
                break;
            default:
                SWIFT_LOG(debug) << "Unknown or unexpected card state." << std::endl;
                break;
        }



        switch (dwState) {
            case SCARD_ABSENT:
                return false;

            case SCARD_PRESENT:
            case SCARD_SWALLOWED:
            case SCARD_POWERED:
            case SCARD_NEGOTIABLE:
            case SCARD_SPECIFIC:
                return true;

            default:
                return false;
        }
    }
    else {
        return false;
    }
}

void CAPICertificate::handleSmartCardTimerTick() {
    bool poll = checkIfSmartCardPresent();
    if (lastPollingResult_ && !poll) {
        onCertificateCardRemoved();
    }
    lastPollingResult_ = poll;
    smartCardTimer_->start();
}

}