︙ | | |
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
|
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
|
+
+
+
|
CK_MECHANISM_TYPE decrypt_mechanism;
CK_VOID_PTR decrypt_mech_parm;
CK_ULONG decrypt_mech_parmlen;
struct cackey_identity *decrypt_identity;
};
struct cackey_slot {
unsigned int id;
int active;
int internal;
char *pcsc_reader;
int pcsc_card_connected;
SCARDHANDLE pcsc_card;
DWORD pcsc_state;
int transaction_depth;
int transaction_need_hw_lock;
int slot_reset;
CK_FLAGS token_flags;
|
︙ | | |
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
|
+
|
#define CACKEY_PIN_COMMAND_DEFAULT_XSTR(str) CACKEY_PIN_COMMAND_DEFAULT_STR(str)
#define CACKEY_PIN_COMMAND_DEFAULT_STR(str) #str
static char *cackey_pin_command = NULL;
static char *cackey_pin_command_xonly = NULL;
/* PCSC Global Handles */
static LPSCARDCONTEXT cackey_pcsc_handle = NULL;
static LPSCARDCONTEXT cackey_waiting_pcsc_handle = NULL;
static unsigned long cackey_getversion(void) {
static unsigned long retval = 255;
unsigned long major = 0;
unsigned long minor = 0;
char *major_str = NULL;
char *minor_str = NULL;
|
︙ | | |
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
|
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
cackey_pcsc_handle = NULL;
cackey_slots_disconnect_all();
return(CACKEY_PCSC_E_GENERIC);
}
}
if (cackey_waiting_pcsc_handle == NULL) {
cackey_waiting_pcsc_handle = malloc(sizeof(*cackey_waiting_pcsc_handle));
if (cackey_waiting_pcsc_handle == NULL) {
CACKEY_DEBUG_PRINTF("Call to malloc() failed, returning in failure");
cackey_slots_disconnect_all();
free(cackey_pcsc_handle);
cackey_pcsc_handle = NULL;
return(CACKEY_PCSC_E_GENERIC);
}
CACKEY_DEBUG_PRINTF("SCardEstablishContext() called");
scard_est_context_ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, cackey_waiting_pcsc_handle);
if (scard_est_context_ret != SCARD_S_SUCCESS) {
CACKEY_DEBUG_PRINTF("Call to SCardEstablishContext failed (returned %s/%li), returning in failure", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_est_context_ret), (long) scard_est_context_ret);
free(cackey_pcsc_handle);
cackey_pcsc_handle = NULL;
free(cackey_waiting_pcsc_handle);
cackey_waiting_pcsc_handle = NULL;
cackey_slots_disconnect_all();
return(CACKEY_PCSC_E_GENERIC);
}
}
#ifdef HAVE_SCARDISVALIDCONTEXT
CACKEY_DEBUG_PRINTF("SCardIsValidContext() called");
scard_isvalid_ret = SCardIsValidContext(*cackey_pcsc_handle);
if (scard_isvalid_ret != SCARD_S_SUCCESS) {
CACKEY_DEBUG_PRINTF("Handle has become invalid (SCardIsValidContext = %s/%li), trying to re-establish...", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_isvalid_ret), (long) scard_isvalid_ret);
|
︙ | | |
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
|
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
cackey_slots_disconnect_all();
return(CACKEY_PCSC_E_GENERIC);
}
CACKEY_DEBUG_PRINTF("Handle has been re-established");
}
CACKEY_DEBUG_PRINTF("SCardIsValidContext() called");
scard_isvalid_ret = SCardIsValidContext(*cackey_waiting_pcsc_handle);
if (scard_isvalid_ret != SCARD_S_SUCCESS) {
CACKEY_DEBUG_PRINTF("Handle has become invalid (SCardIsValidContext = %s/%li), trying to re-establish...", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_isvalid_ret), (long) scard_isvalid_ret);
CACKEY_DEBUG_PRINTF("SCardEstablishContext() called");
scard_est_context_ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, cackey_waiting_pcsc_handle);
if (scard_est_context_ret != SCARD_S_SUCCESS) {
CACKEY_DEBUG_PRINTF("Call to SCardEstablishContext failed (returned %s/%li), returning in failure", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_est_context_ret), (long) scard_est_context_ret);
free(cackey_waiting_pcsc_handle);
cackey_waiting_pcsc_handle = NULL;
cackey_slots_disconnect_all();
return(CACKEY_PCSC_E_GENERIC);
}
CACKEY_DEBUG_PRINTF("Waiting handle has been re-established");
}
#endif
CACKEY_DEBUG_PRINTF("Sucessfully connected to PC/SC, returning in success");
return(CACKEY_PCSC_S_OK);
}
|
︙ | | |
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
|
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
|
+
-
-
-
+
+
+
+
+
-
-
-
+
+
-
-
-
+
+
+
+
+
+
+
+
-
+
|
* NOTES
* This function disconnects from the PC/SC Connection manager and updates
* the global handle.
*
*/
static cackey_ret cackey_pcsc_disconnect(void) {
LONG scard_rel_context_ret;
cackey_ret retval = CACKEY_PCSC_S_OK;
CACKEY_DEBUG_PRINTF("Called.");
if (cackey_pcsc_handle == NULL) {
return(CACKEY_PCSC_S_OK);
}
if (cackey_pcsc_handle != NULL) {
scard_rel_context_ret = SCardReleaseContext(*cackey_pcsc_handle);
if (scard_rel_context_ret != SCARD_S_SUCCESS) {
retval = CACKEY_PCSC_E_GENERIC;
}
scard_rel_context_ret = SCardReleaseContext(*cackey_pcsc_handle);
if (cackey_pcsc_handle) {
free(cackey_pcsc_handle);
cackey_pcsc_handle = NULL;
}
if (cackey_waiting_pcsc_handle != NULL) {
scard_rel_context_ret = SCardReleaseContext(*cackey_waiting_pcsc_handle);
if (scard_rel_context_ret != SCARD_S_SUCCESS) {
return(CACKEY_PCSC_E_GENERIC);
}
if (scard_rel_context_ret != SCARD_S_SUCCESS) {
retval = CACKEY_PCSC_E_GENERIC;
}
free(cackey_waiting_pcsc_handle);
cackey_waiting_pcsc_handle = NULL;
}
return(CACKEY_PCSC_S_OK);
return(retval);
}
/*
* SYNPOSIS
* void cackey_mark_slot_reset(struct cackey_slot *slot);
*
* ARGUMENTS
|
︙ | | |
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
|
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
|
+
|
if (slot->pcsc_card_connected) {
SCardDisconnect(slot->pcsc_card, SCARD_LEAVE_CARD);
}
slot->slot_reset = 1;
slot->pcsc_card_connected = 0;
slot->token_flags = CKF_LOGIN_REQUIRED;
slot->pcsc_state = SCARD_STATE_UNAWARE;
CACKEY_DEBUG_PRINTF("Returning.");
return;
}
/*
|
︙ | | |
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
|
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
|
+
+
|
}
for (idx = 0; idx < (sizeof(cackey_sessions) / sizeof(cackey_sessions[0])); idx++) {
cackey_sessions[idx].active = 0;
}
for (idx = 0; idx < (sizeof(cackey_slots) / sizeof(cackey_slots[0])); idx++) {
cackey_slots[idx].id = idx;
cackey_slots[idx].active = 0;
cackey_slots[idx].pcsc_reader = NULL;
cackey_slots[idx].pcsc_state = SCARD_STATE_UNAWARE;
cackey_slots[idx].transaction_depth = 0;
cackey_slots[idx].transaction_need_hw_lock = 0;
cackey_slots[idx].slot_reset = 0;
cackey_slots[idx].token_flags = 0;
cackey_slots[idx].label = NULL;
cackey_slots[idx].internal = 0;
}
|
︙ | | |
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
|
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
|
+
+
|
for (idx = 0; idx < (sizeof(cackey_sessions) / sizeof(cackey_sessions[0])); idx++) {
if (cackey_sessions[idx].active) {
C_CloseSession(idx);
}
}
cackey_mutex_lock(cackey_biglock);
cackey_slots_disconnect_all();
for (idx = 0; idx < (sizeof(cackey_slots) / sizeof(cackey_slots[0])); idx++) {
if (cackey_slots[idx].internal) {
continue;
}
|
︙ | | |
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
|
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
|
+
+
|
cackey_slots[idx].cached_certs = NULL;
}
}
cackey_pcsc_disconnect();
cackey_initialized = 0;
cackey_mutex_unlock(cackey_biglock);
CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);
return(CKR_OK);
}
CK_DEFINE_FUNCTION(CK_RV, C_GetInfo)(CK_INFO_PTR pInfo) {
|
︙ | | |
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
|
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);
return(CKR_OK);
}
CK_DEFINE_FUNCTION(CK_RV, C_WaitForSlotEvent)(CK_FLAGS flags, CK_SLOT_ID_PTR pSlotID, CK_VOID_PTR pReserved) {
SCARD_READERSTATE reader_states[(sizeof(cackey_slots) / sizeof(cackey_slots[0])) + 1];
LPSCARDCONTEXT handle;
LONG scard_getstatchng_ret;
cackey_ret pcsc_connect_ret;
struct cackey_slot *cackey_slot;
unsigned int currslot, reader_state_slot, reader_state_slot_cnt;
int mutex_retval;
int slot_changed;
CACKEY_DEBUG_PRINTF("Called.");
if (pReserved != NULL) {
CACKEY_DEBUG_PRINTF("Error. pReserved is not NULL.");
return(CKR_ARGUMENTS_BAD);
}
if (pSlotID == NULL) {
CACKEY_DEBUG_PRINTF("Error. pSlotID is NULL.");
return(CKR_ARGUMENTS_BAD);
}
if (!cackey_initialized) {
CACKEY_DEBUG_PRINTF("Error. Not initialized.");
return(CKR_CRYPTOKI_NOT_INITIALIZED);
}
mutex_retval = cackey_mutex_lock(cackey_biglock);
if (mutex_retval != 0) {
CACKEY_DEBUG_PRINTF("Error. Locking failed.");
/* XXX: TODO: Implement this... */
CACKEY_DEBUG_PRINTF("Returning CKR_FUNCTION_NOT_SUPPORTED (%i)", CKR_FUNCTION_NOT_SUPPORTED);
return(CKR_GENERAL_ERROR);
}
pcsc_connect_ret = cackey_pcsc_connect();
if (pcsc_connect_ret != CACKEY_PCSC_S_OK) {
cackey_mutex_unlock(cackey_biglock);
CACKEY_DEBUG_PRINTF("Connection to PC/SC failed, returning in failure");
return(CKR_GENERAL_ERROR);
}
for (reader_state_slot = currslot = 0; currslot < (sizeof(cackey_slots) / sizeof(cackey_slots[0])); currslot++) {
if (cackey_slots[currslot].internal) {
continue;
}
if (cackey_slots[currslot].active == 0) {
continue;
}
reader_states[reader_state_slot].szReader = cackey_slots[currslot].pcsc_reader;
reader_states[reader_state_slot].pvUserData = &cackey_slots[currslot];
if ((flags & CKF_DONT_BLOCK) == CKF_DONT_BLOCK) {
reader_states[reader_state_slot].dwCurrentState = SCARD_STATE_UNAWARE;
} else {
reader_states[reader_state_slot].dwCurrentState = cackey_slots[currslot].pcsc_state;
}
reader_state_slot++;
}
mutex_retval = cackey_mutex_unlock(cackey_biglock);
if (mutex_retval != 0) {
CACKEY_DEBUG_PRINTF("Error. Unlocking failed.");
return(CKR_GENERAL_ERROR);
}
reader_states[reader_state_slot].szReader = "\\\\?PnP?\\Notification";
reader_states[reader_state_slot].pvUserData = NULL;
reader_states[reader_state_slot].dwCurrentState = SCARD_STATE_UNAWARE;
reader_state_slot++;
reader_state_slot_cnt = reader_state_slot;
while (1) {
cackey_mutex_lock(cackey_biglock);
handle = cackey_waiting_pcsc_handle;
cackey_mutex_unlock(cackey_biglock);
if (handle == NULL) {
CACKEY_DEBUG_PRINTF("Waiting handle disappeared, not attempting to wait.");
break;
}
CACKEY_DEBUG_PRINTF("Calling SCardGetStatusChange(), which will block for 1 second");
scard_getstatchng_ret = SCardGetStatusChange(*handle, 1000, reader_states, reader_state_slot_cnt);
if (scard_getstatchng_ret == SCARD_E_TIMEOUT) {
continue;
}
break;
}
if (scard_getstatchng_ret != SCARD_S_SUCCESS) {
CACKEY_DEBUG_PRINTF("Returning CKR_GENERAL_ERROR (%i) because SCardGetStatusChange failed: %lx", CKR_GENERAL_ERROR, scard_getstatchng_ret);
return(CKR_GENERAL_ERROR);
}
mutex_retval = cackey_mutex_lock(cackey_biglock);
if (mutex_retval != 0) {
CACKEY_DEBUG_PRINTF("Error. Locking failed.");
return(CKR_GENERAL_ERROR);
}
for (reader_state_slot = 0; reader_state_slot < reader_state_slot_cnt; reader_state_slot++) {
CACKEY_DEBUG_PRINTF("[pcsc/slot = %u] CurrentState = %lx, EventState = %lx",
reader_state_slot,
reader_states[reader_state_slot].dwCurrentState & 0xffff,
reader_states[reader_state_slot].dwEventState & 0xffff
);
cackey_slot = NULL;
for (currslot = 0; currslot < (sizeof(cackey_slots) / sizeof(cackey_slots[0])); currslot++) {
if (cackey_slots[currslot].internal) {
continue;
}
if (reader_states[reader_state_slot].szReader == NULL) {
continue;
}
if (cackey_slots[currslot].pcsc_reader == NULL) {
continue;
}
CACKEY_DEBUG_PRINTF("Checking to see if pcsc/slot:%u (%s) is cackey/slot:%u (%s)...",
reader_state_slot,
reader_states[reader_state_slot].szReader,
currslot,
cackey_slots[currslot].pcsc_reader
);
if (strcmp(reader_states[reader_state_slot].szReader, cackey_slots[currslot].pcsc_reader) != 0) {
CACKEY_DEBUG_PRINTF(" no match");
continue;
}
cackey_slot = &cackey_slots[currslot];
CACKEY_DEBUG_PRINTF(" match, slot = %p", cackey_slot);
break;
}
if (cackey_slot == NULL) {
/* XXX: TODO: Someone plugged in a new slot or removed a slot */
CACKEY_DEBUG_PRINTF("WARNING: Unhandled code. Slot inserted or removed.");
continue;
}
slot_changed = 0;
if ((flags & CKF_DONT_BLOCK) == CKF_DONT_BLOCK) {
if (cackey_slot->pcsc_state != reader_states[reader_state_slot].dwEventState) {
slot_changed = 1;
}
} else {
if (reader_states[reader_state_slot].dwCurrentState != reader_states[reader_state_slot].dwEventState) {
slot_changed = 1;
}
}
if (slot_changed == 0) {
CACKEY_DEBUG_PRINTF("[pcsc/slot = %u] Slot did not change", (unsigned int) reader_state_slot);
continue;
}
CACKEY_DEBUG_PRINTF("Returning slot changed: %u", (unsigned int) cackey_slot->id);
cackey_slot->pcsc_state = reader_states[reader_state_slot].dwEventState;
*pSlotID = (CK_SLOT_ID) cackey_slot->id;
CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);
mutex_retval = cackey_mutex_unlock(cackey_biglock);
if (mutex_retval != 0) {
CACKEY_DEBUG_PRINTF("Error. Unlocking failed.");
return(CKR_GENERAL_ERROR);
}
return(CKR_FUNCTION_NOT_SUPPORTED);
return(CKR_OK);
}
mutex_retval = cackey_mutex_unlock(cackey_biglock);
if (mutex_retval != 0) {
CACKEY_DEBUG_PRINTF("Error. Unlocking failed.");
return(CKR_GENERAL_ERROR);
}
if ((flags & CKF_DONT_BLOCK) != CKF_DONT_BLOCK) {
CACKEY_DEBUG_PRINTF("Returning CKR_NO_EVENT (%i), but asked to block !? BUG ENCOUNTERED.", CKR_NO_EVENT);
} else {
CACKEY_DEBUG_PRINTF("Returning CKR_NO_EVENT (%i)", CKR_NO_EVENT);
}
return(CKR_NO_EVENT);
}
CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismList)(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pulCount) {
CACKEY_DEBUG_PRINTF("Called.");
if (!cackey_initialized) {
CACKEY_DEBUG_PRINTF("Error. Not initialized.");
|
︙ | | |