-
Notifications
You must be signed in to change notification settings - Fork 1
/
ep_pkcs15.cpp
196 lines (144 loc) · 5.04 KB
/
ep_pkcs15.cpp
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
#include <string>
#include "inc/ep_pkcs15.h"
#include "inc/token.h"
#include "libopensc/pkcs15.h"
#include "pkcs15init/pkcs15-init.h"
#include "pkcs15init/profile.h"
#include "inc/util_pkcs15.h"
#include <windows.h>
namespace epsilon {
CefRefPtr<TokenContext> TokenContext::instance_ = NULL;
sc_pkcs15_card *CreateP15Card(CefRefPtr<TokenContext> ctx) {
sc_pkcs15_card *p15card = sc_pkcs15_card_new();
p15card->card = ctx->GetCard();
return p15card;
}
void DestroyP15Card(sc_pkcs15_card *p15card) {
sc_pkcs15_card_free(p15card);
}
sc_pkcs15_card *BindP15Card(CefRefPtr<TokenContext> ctx) {
sc_pkcs15_card *p15card;
if(::util::ep_pkcs15_bind_card(ctx->GetCard(), &p15card, ctx->GetProfile()) == SC_SUCCESS) {
sc_pkcs15init_set_p15card(ctx->GetProfile(), p15card);
return p15card;
}
return NULL;
}
void ReleaseP15Card(sc_pkcs15_card *p15card) {
sc_pkcs15_unbind(p15card);
}
void GetTokenInfo(CefRefPtr<TokenContext> ctx, ep_token_info *info) {
sc_pkcs15_card *p15card;
sc_pkcs15_object *pin_obj;
sc_pkcs15_auth_info *pinfo = NULL;
/*for(unsigned int i = 0, len = sc_ctx_get_reader_count(ctx->GetSCContext()); i < len; i++) {
sc_reader *reader = sc_ctx_get_reader_by_id(ctx->GetSCContext(), 0);
}*/
memset(info, 0, sizeof(*info));
memset(info->manufacturerID, 0, sizeof(info->manufacturerID));
memset(info->label, 0, sizeof(info->label));
memset(info->reader, 0, sizeof(info->reader));
strncpy(info->reader, ctx->GetCard()->reader->name, sizeof(info->reader));
info->onepin = ::util::ep_has_onepin(ctx->GetProfile());
info->minlen = ctx->GetProfile()->pin_minlen;
info->maxlen = ctx->GetProfile()->pin_maxlen;
if ((p15card = BindP15Card(ctx)) == NULL) {
info->token_initialized = 0;
return;
}
strncpy(info->label, p15card->tokeninfo->label, sizeof(info->label));
info->readonly = (p15card->tokeninfo->flags >> 0) & 1; /* see pkcs15-tool `dump` for details */
info->login_required = (p15card->tokeninfo->flags >> 1) & 1 ;
strncpy(info->manufacturerID, p15card->tokeninfo->manufacturer_id, sizeof(info->manufacturerID));
if ((pin_obj = ::util::get_pin_info(p15card)) == NULL)
goto out;
pinfo = (sc_pkcs15_auth_info_t *) pin_obj->data;
::util::ep_refresh_pin_info(p15card->card, pinfo);
info->token_initialized = 1;
info->inuse = ctx->GetProfile()->card->reader->flags & SC_READER_CARD_INUSE ? 1 : 0;
info->pin_initialized = pinfo->attrs.pin.flags & SC_PKCS15_PIN_FLAG_INITIALIZED ? 1 : 0;
info->minlen = pinfo->attrs.pin.min_length;
info->maxlen = pinfo->attrs.pin.max_length;
info->max_tries = pinfo->max_tries;
info->tries_left = pinfo->tries_left;
out: ReleaseP15Card(p15card);
}
bool TokenContext::isTokenPresent(sc_context **ctx) {
sc_context_param_t ctx_param;
memset(&ctx_param, 0, sizeof(ctx_param));
ctx_param.ver = 0;
ctx_param.app_name = E_TOKEN_APPLICATION_NAME;
ctx_param.thread_ctx = &sc_thread_ctx;
if (sc_context_create(ctx, &ctx_param))
return false;
if (sc_ctx_get_reader_count(*ctx) > 0)
/* Automatically try to skip to a reader with a card if reader not specified */
for (unsigned int i = 0; i < sc_ctx_get_reader_count(*ctx); i++) {
sc_reader *reader = sc_ctx_get_reader(*ctx, i);
if (sc_detect_card_presence(reader) & SC_READER_CARD_PRESENT)
return true;
}
sc_release_context(*ctx);
*ctx = NULL;
return false;
}
bool TokenContext::Bind(const char *reader) {
if(in_context)
return true;
/* Connect to the card */
if (!::util::ep_open_reader_and_card(&ctx, reader, &card))
return false;
/* Bind the card-specific operations and load the profile */
if (sc_pkcs15init_bind(card, kProfileName.c_str(),
NULL, &profile) < 0) {
if (card) {
sc_unlock(card);
sc_disconnect_card(card);
}
if(ctx)
sc_release_context(ctx);
return false;
}
in_context = true;
return true;
}
void TokenContext::Destroy() {
if(!InContext()) return;
if (profile) {
if (profile->dirty != 0 /*&& profile->p15_data != NULL */&& profile->pkcs15.do_last_update) {
if((/*profile->p15_data = */BindP15Card(this)) != NULL) {
sc_pkcs15init_unbind(profile);
//if(profile->p15_data) ReleaseP15Card(profile->p15_data);
}
} else {
sc_pkcs15init_unbind(profile);
}
}
if (card) {
sc_unlock(card);
sc_disconnect_card(card);
}
if(ctx)
sc_release_context(ctx);
/* NULLify variables */
profile = NULL;
card = NULL;
ctx = NULL;
in_context = false;
}
sc_profile *TokenContext::GetProfile() const {
return profile;
}
std::string TokenContext::GetProfileName() {
return kProfileName;
}
sc_context *TokenContext::GetSCContext() const {
return ctx;
}
sc_card *TokenContext::GetCard() const {
return card;
}
bool TokenContext::InContext() const {
return in_context;
}
}