summaryrefslogtreecommitdiffstats
blob: 5322a039c91079b761be8b639d5c2f5c9668a70a (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
/*
 * Copyright (c) 2012 Isode Limited, London, England.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <Swiften/Base/API.h>
#include <Swiften/Base/boost_bsignals.h>
#include <Swiften/Base/SafeByteArray.h>
#include <Swiften/TLS/CertificateWithKey.h>
#include <Swiften/Network/Timer.h>

#define SECURITY_WIN32
#include <Windows.h>
#include <WinCrypt.h>
#include <Winscard.h>

#define SMARTCARD_EJECTION_CHECK_FREQUENCY_MILLISECONDS    1000

namespace Swift {
    class TimerFactory;

    class SWIFTEN_API CAPICertificate : public Swift::CertificateWithKey {
        public:
            CAPICertificate(const std::string& capiUri, TimerFactory* timerFactory);

            virtual ~CAPICertificate();

            virtual bool isNull() const;

            const std::string& getCertStoreName() const;

            const std::string& getCertName() const;

            const std::string& getSmartCardReaderName() const;

        public:
            boost::signal<void ()> onCertificateCardRemoved;

        private:
            void setUri (const std::string& capiUri);

            void handleSmartCardTimerTick();

            bool checkIfSmartCardPresent();

        private:
            bool valid_;
            std::string uri_;

            HCERTSTORE certStoreHandle_;
            SCARDCONTEXT  scardContext_;
            SCARDHANDLE   cardHandle_;

            /* Parsed components of the uri_ */
            std::string certStore_;
            std::string certName_;
            std::string smartCardReaderName_;
            std::shared_ptr<Timer> smartCardTimer_;
            TimerFactory* timerFactory_;

            bool lastPollingResult_;
    };

PCCERT_CONTEXT findCertificateInStore (HCERTSTORE certStoreHandle, const std::string &certName);

}