summaryrefslogtreecommitdiffstats
path: root/client/smartcard_channel.h
blob: 0423bff0c1e6bc92ab6413dd57678e37c92952f7 (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
/*
   Copyright (C) 2010 Red Hat, 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 WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SMART_CARD_H__
#define __SMART_CARD_H__

#include <map>

#include <vreadert.h>
#include <vscard_common.h>
#include <eventt.h>

#include "red_channel.h"
#include "red_peer.h"

class Application;

struct SmartcardOptions {
    std::vector<std::string> certs;
    std::string dbname;
    bool enable;
    SmartcardOptions();
};

void smartcard_init(const SmartcardOptions* options);

struct ReaderData {
    ReaderData() :
        vreader(NULL),
        reader_id(VSCARD_UNDEFINED_READER_ID),
        name(NULL),
        card_insert_pending(false)
    {}
    VReader *vreader;
    uint32_t reader_id;
    char* name;
    bool card_insert_pending;
};

void virtual_card_remove();
void virtual_card_insert();

class SmartCardChannel;

class VEventEvent : public Event {
public:
    VEventEvent(SmartCardChannel* smartcard_channel, VEvent* vevent);
    ~VEventEvent();
    SmartCardChannel* _smartcard_channel;
    VReader* _vreader;
    VEvent* _vevent;
};

class ReaderAddEvent: public VEventEvent {
public:
    ReaderAddEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class ReaderRemoveEvent: public VEventEvent {
public:
    ReaderRemoveEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class CardInsertEvent: public VEventEvent {
public:
    CardInsertEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class CardRemoveEvent: public VEventEvent {
public:
    CardRemoveEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class VSCMessageEvent: public Event {
public:
    VSCMessageEvent(SmartCardChannel* smartcard_channel,
        VSCMsgHeader* vheader);
    ~VSCMessageEvent();
    SmartCardChannel* _smartcard_channel;
    VSCMsgHeader* _vheader;
    virtual void response(AbstractProcessLoop& events_loop);
};

typedef std::pair<VEventType, Event*> SmartCardEvent;

class SmartCardChannel : public RedChannel {

public:
    SmartCardChannel(RedClient& client, uint32_t id);
    void handle_smartcard_data(RedPeer::InMessage* message);

    void virtual_card_remove();
    void virtual_card_insert();
    static ChannelFactory& Factory();
protected:
    virtual void on_connect();
    virtual void on_disconnect();

private:
    static void* cac_card_events_thread_entry(void* data);
    void cac_card_events_thread_main();
    void send_message(uint32_t reader_id, VSCMsgType type, uint8_t* data, uint32_t len);

    Thread* _event_thread;

    Application* _app;

    VReaderList *_reader_list;
    typedef std::map<uint32_t, ReaderData*> readers_by_id_t;
    readers_by_id_t _readers_by_id;
    typedef std::map<VReader*, ReaderData*> readers_by_vreader_t;
    readers_by_vreader_t _readers_by_vreader;
    readers_by_vreader_t _unallocated_readers_by_vreader;
    VEventType _next_sync_vevent;
    std::list<SmartCardEvent> _sync_events;

    void push_sync_event(VEventType type, Event *event);
    void send_next_sync_event();
    void handle_reader_add_response(VSCMsgHeader *vheader, VSCMsgError *error);
    void handle_error_message(VSCMsgHeader *vheader, VSCMsgError *error);

    ReaderData* reader_data_from_vreader(VReader* vreader);
    ReaderData* reader_data_from_reader_id(uint32_t reader_id);
    void add_unallocated_reader(VReader* vreader, const char* name);
    ReaderData* add_reader(uint32_t reader_id);
    void remove_reader(ReaderData* data);
    void send_reader_added(const char* reader_name);
    void send_reader_removed(uint32_t reader_id);
    void send_atr(VReader* vreader);

    friend class ReaderAddEvent;
    friend class ReaderRemoveEvent;
    friend class CardInsertEvent;
    friend class CardRemoveEvent;
    friend class VSCMessageEvent;
};

#endif // __SMART_CARD_H__