-
Notifications
You must be signed in to change notification settings - Fork 1
/
CardService.h
207 lines (182 loc) · 6.08 KB
/
CardService.h
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
/*
* esteid-browser-plugin - a browser plugin for Estonian EID card
*
* Copyright (C) 2010 Estonian Informatics Centre
* Copyright (C) 2010 Smartlink OÜ
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This class helps to implement Card Services.
* It encapsulates platform specific locking, card event polling
* thread and synchronized access to cards via a singleton instance.
*
* Typical usage:
*
* boost::shared_ptr<CardService> service = CardService::getInstance();
* service->addObserver(this);
*
*/
#ifndef CARDSERVICE_H_
#define CARDSERVICE_H_
#include <smartcardpp/smartcardpp.h>
#include <boost/scoped_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/function.hpp>
#define PDATA_MIN EstEidCard::SURNAME
#define PDATA_MAX EstEidCard::COMMENT4
typedef unsigned int ReaderID;
enum SignError {
SIGN_ERROR_BLOCKED,
SIGN_ERROR_WRONG_PIN,
SIGN_ERROR_ABORTED,
SIGN_ERROR_CARD_ERROR
};
class CardService
{
public:
// Constructor is protected
virtual ~CardService();
/**
* Get service instance
*/
static boost::shared_ptr<CardService> getInstance();
/**
* Find readers with valid card
*/
void findEstEid(vector<ReaderID>& readers);
/**
* Find the reader with first valid card
*/
ReaderID findFirstEstEid();
/**
* Read personal data file off the first card
*/
void readPersonalData(vector<std::string>& data);
/**
* Read personal data file off the card in specified reader
*/
void readPersonalData(vector<std::string>& data, ReaderID);
/**
* Read authentication certificate off the first card
*/
ByteVec getAuthCert();
/**
* Read authentication certificate off the card in specified reader
*/
ByteVec getAuthCert(ReaderID);
/**
* Read signature certificate off the first card
*/
ByteVec getSignCert();
/**
* Read signature certificate off the card in specified reader
*/
ByteVec getSignCert(ReaderID);
/**
* Retrieve retry counts from the first card
*/
bool getRetryCounts(byte& puk, byte& pinAuth, byte& pinSign);
/**
* Retrieve retry counts from the card in specified reader
*/
bool getRetryCounts(byte& puk, byte& pinAuth, byte& pinSign, ReaderID);
/**
* Check if first card is in a reader with a PinPAD
*/
bool hasSecurePinEntry();
/**
* Check if the specified card is in a reader with a PinPAD
*/
bool hasSecurePinEntry(ReaderID);
/**
* Sign a SHA1 hash with the first card
*/
std::string signSHA1(const std::string& hash,
EstEidCard::KeyType keyId,
const std::string& pin);
/**
* Sign a SHA1 hash with the specified card
*/
std::string signSHA1(const std::string& hash,
EstEidCard::KeyType keyId,
const std::string& pin,
ReaderID);
typedef boost::function<void (const std::string&)> SignCompletedFunc;
typedef boost::function<void (SignError, const std::string&)> SignFailedFunc;
void setSignCompletedCallback(SignCompletedFunc f);
void setSignFailedCallback(SignFailedFunc f);
void signSHA1Async(const std::string& hash,
EstEidCard::KeyType keyId,
const std::string& pin,
ReaderID reader);
void signSHA1Async(const std::string& hash,
EstEidCard::KeyType keyId,
const std::string& pin);
/* Message observer interface */
enum MsgType {
CARD_INSERTED,
CARD_REMOVED,
READERS_CHANGED,
CARD_ERROR
};
class MessageObserver {
friend class CardService;
virtual void onMessage(MsgType e, ReaderID i) = 0;
};
virtual void addObserver(MessageObserver *obs);
virtual void removeObserver(MessageObserver *obs);
protected:
CardService();
/* Get access to smartcard manager instance */
ManagerInterface& cardManager();
/* Singleton instance variable */
static boost::weak_ptr<CardService> sCardService;
class IdCardCacheEntry {
public:
bool cardPresent;
vector<std::string> m_pData;
ByteVec m_authCert;
ByteVec m_signCert;
void purge() { cardPresent = false; m_pData.clear(); m_authCert.clear(); m_signCert.clear(); }
IdCardCacheEntry() : cardPresent(false) {}
};
typedef vector<IdCardCacheEntry> IdCardCache;
IdCardCache m_cache;
vector<MessageObserver *> m_observers;
virtual void postMessage(MsgType e, ReaderID i);
private:
// Declarations only for copy constructor and assignment operator
CardService(const CardService&);
CardService& operator=(const CardService&);
void findEstEid();
void monitor();
void poll();
bool readerHasCard(EstEidCard& card, ReaderID i);
void runSignSHA1(const std::string& hash,
EstEidCard::KeyType keyId,
const std::string& pin,
ReaderID reader);
static SignError decodeAuthError(const AuthError& e);
boost::scoped_ptr<ManagerInterface> m_manager;
SignCompletedFunc signCompletedFunc;
SignFailedFunc signFailedFunc;
boost::mutex m_cardMutex;
boost::mutex m_messageMutex;
boost::thread m_thread;
boost::thread m_signThread;
};
#endif /* CARDSERVICE_H_ */