[c8d76d]: src / os / linux / PWYubi.cpp Maximize Restore History

Download this file

PWYubi.cpp    250 lines (228 with data), 6.4 kB

  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
/*
* Copyright (c) 2003-2014 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/** \file PWYubi.cpp
*
*/
#include "PWYubi.h"
#include "../debug.h"
#include "../utf8conv.h"
#include <ykcore.h>
#include <ykpers.h>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
bool PWYubi::s_yubiDetected = false;
pthread_mutex_t PWYubi::s_mutex = PTHREAD_MUTEX_INITIALIZER;
PWYubi::PWYubi() : m_isInited(false), m_reqstat(ERROR)
{
pthread_mutex_lock(&s_mutex);
m_isInited = yk_init() != 0;
pthread_mutex_unlock(&s_mutex);
}
PWYubi::~PWYubi()
{
pthread_mutex_lock(&s_mutex);
if (m_isInited)
yk_release();
pthread_mutex_unlock(&s_mutex);
}
bool PWYubi::IsYubiInserted() const
{
bool retval = false;
pthread_mutex_lock(&s_mutex);
if (m_isInited) {
YK_KEY *ykey = yk_open_first_key();
if (ykey != NULL) {
yk_close_key(ykey);
retval = true;
} else {
// reset s.t. we'll init next time
yk_release();
m_isInited = false;
}
} else { // try again
m_isInited = yk_init() != 0;
}
pthread_mutex_unlock(&s_mutex);
if (retval)
s_yubiDetected = true;
return retval;
}
// Following not a member function as we don't want to expose
// YK_KEY in the interface (header file).
static bool check_firmware_version(YK_KEY *yk)
{
YK_STATUS *st = ykds_alloc();
bool retval = false;
if (yk_get_status(yk, st) &&
(ykds_version_major(st) > 2 ||
(ykds_version_major(st) == 2
&& ykds_version_minor(st) >= 2))) {
retval = true;
}
free(st);
return retval;
}
bool PWYubi::GetSerial(unsigned int &serial) const
{
bool retval = false;
YK_KEY *ykey = NULL;
pthread_mutex_lock(&s_mutex);
// if yk isn't init'ed, don't bother
if (m_isInited) {
ykey = yk_open_first_key();
if (ykey != NULL) {
if (!check_firmware_version(ykey)) {
m_ykerrstr = _S("YubiKey firmware version unsupported");
goto done;
}
if (!yk_get_serial(ykey, 0, 0, &serial)) {
m_ykerrstr = _S("Failed to read serial number");
goto done;
}
retval = true;
} else { // NULL ykey, perhaps removed?
report_error();
}
}
done:
if (ykey != NULL)
yk_close_key(ykey);
pthread_mutex_unlock(&s_mutex);
return retval;
}
bool PWYubi::WriteSK(const unsigned char *yubi_sk_bin, size_t sklen)
{
bool retval = false;
YK_KEY *ykey = NULL;
YKP_CONFIG *cfg = ykp_alloc();
YK_STATUS *st = ykds_alloc();
pthread_mutex_lock(&s_mutex);
// if yk isn't init'ed, don't bother
if (m_isInited) {
ykey = yk_open_first_key();
if (ykey == NULL)
goto done;
if (!yk_get_status(ykey, st) ||
!ykp_set_tktflag_CHAL_RESP(cfg,true) ||
!ykp_set_cfgflag_CHAL_HMAC(cfg, true) ||
!ykp_set_cfgflag_HMAC_LT64(cfg, true) ||
!ykp_set_cfgflag_CHAL_BTN_TRIG(cfg, true) ||
!ykp_set_extflag_SERIAL_API_VISIBLE(cfg, true)
) {
m_ykerrstr = _S("Internal error: couldn't set configuration");
goto done;
}
// not sure we need the following, comment in ykpers.h hints it is
ykp_configure_version(cfg, st);
if (!ykp_configure_command(cfg, SLOT_CONFIG2)) { // _UPDATE2?
m_ykerrstr = _S("Internal error: couldn't configure command");
goto done;
}
ostringstream os;
for (size_t i = 0; i < sklen; i++)
os << setfill('0') << setw(2) << hex << int(yubi_sk_bin[i]);
if (!ykp_HMAC_key_from_hex(cfg, os.str().c_str())) {
m_ykerrstr = _S("Internal error: couldn't configure key");
goto done;
}
if (!yk_write_command(ykey,
ykp_core_config(cfg), ykp_command(cfg),
NULL)) {
m_ykerrstr = _S("Internal error: couldn't configure key");
goto done;
}
retval = true;
} // m_isInited
done:
if (ykey != NULL)
yk_close_key(ykey);
pthread_mutex_unlock(&s_mutex);
free(cfg);
free(st);
return retval;
}
bool PWYubi::RequestHMacSHA1(const unsigned char *challenge, unsigned int len)
{
bool retval = false;
m_reqstat = ERROR;
YK_KEY *ykey = NULL;
pthread_mutex_lock(&s_mutex);
// if yk isn't init'ed, don't bother
if (m_isInited) {
ykey = yk_open_first_key();
if (ykey == NULL)
goto done;
if (yk_write_to_key(ykey, SLOT_CHAL_HMAC2, challenge, len)) {
m_reqstat = PENDING;
retval = true;
}
}
done:
if (ykey != NULL)
yk_close_key(ykey);
pthread_mutex_unlock(&s_mutex);
return retval;
}
PWYubi::RequestStatus PWYubi::GetResponse(unsigned char resp[PWYubi::RESPLEN])
{
YK_KEY *ykey = NULL;
pthread_mutex_lock(&s_mutex);
// if yk isn't init'ed, don't bother
if (m_isInited && m_reqstat == PENDING) {
ykey = yk_open_first_key();
if (ykey == NULL) {
m_reqstat = ERROR;
goto done;
}
unsigned char response[64];
unsigned int response_len = 0;
if (yk_read_response_from_key(ykey, 2, YK_FLAG_MAYBLOCK,
response, sizeof(response),
20, &response_len)) {
memcpy(resp, response, RESPLEN);
m_reqstat = DONE;
} else {
if (yk_errno == YK_ETIMEOUT)
m_reqstat = TIMEOUT;
// It's unclear what's returned if the user hasn't
// pressed the button. We'll leave the status untouched (PENDING)
// if read failed but hasn't timed out, so that next time
// it could possibly succeed.
}
}
done:
if (ykey != NULL)
yk_close_key(ykey);
pthread_mutex_unlock(&s_mutex);
return m_reqstat;
}
void PWYubi::report_error() const
{
std::string yk_errstr;
if (ykp_errno) {
pws_os::Trace(_S("Yubikey personalization error(%d)\n"), ykp_errno);
yk_errstr = ykp_strerror(ykp_errno);
}
if (yk_errno) {
if (yk_errno == YK_EUSBERR) {
pws_os::Trace(_S("USB error(%d)\n"), yk_errno);
yk_errstr += yk_usb_strerror();
} else {
pws_os::Trace(_S("Yubikey core error(%d)\n"), yk_errno);
yk_errstr += yk_strerror(yk_errno);
}
}
if (yk_errstr.empty())
m_ykerrstr = L"";
else
m_ykerrstr = pws_os::towc(yk_errstr.c_str());
}