| 1 |
/* |
| 2 |
* Copyright (c) 2016 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
findslot.cpp |
| 29 |
|
| 30 |
Helper function to find the slot |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#include "findslot.h" |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
extern CK_FUNCTION_LIST_PTR p11; |
| 41 |
|
| 42 |
// Find the slot/token |
| 43 |
int findSlot(char* slot, char* serial, char* token, bool freeToken, CK_SLOT_ID& slotID) |
| 44 |
{ |
| 45 |
if (slot != NULL) |
| 46 |
{ |
| 47 |
int slotNumber = atoi(slot); |
| 48 |
if (slotNumber < 0) |
| 49 |
{ |
| 50 |
fprintf(stderr, "ERROR: The slot number is negative.\n"); |
| 51 |
return 1; |
| 52 |
} |
| 53 |
|
| 54 |
slotID = slotNumber; |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
if (serial == NULL && token == NULL && freeToken == false) |
| 59 |
{ |
| 60 |
fprintf(stderr, "ERROR: A slot/token must be supplied. " |
| 61 |
"Use --slot <number>, --serial <serial>, " |
| 62 |
"--token <label>, or --free\n"); |
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
// Load the variables |
| 67 |
CK_UTF8CHAR paddedSerial[16]; |
| 68 |
CK_UTF8CHAR paddedToken[32]; |
| 69 |
if (serial != NULL) |
| 70 |
{ |
| 71 |
size_t inSize = strlen(serial); |
| 72 |
size_t outSize = sizeof(paddedSerial); |
| 73 |
if (inSize > outSize) |
| 74 |
{ |
| 75 |
fprintf(stderr, "ERROR: --serial is too long.\n"); |
| 76 |
return 1; |
| 77 |
} |
| 78 |
memset(paddedSerial, ' ', outSize); |
| 79 |
memcpy(paddedSerial, serial, inSize); |
| 80 |
} |
| 81 |
if (token != NULL) |
| 82 |
{ |
| 83 |
size_t inSize = strlen(token); |
| 84 |
size_t outSize = sizeof(paddedToken); |
| 85 |
if (inSize > outSize) |
| 86 |
{ |
| 87 |
fprintf(stderr, "ERROR: --token is too long.\n"); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
memset(paddedToken, ' ', outSize); |
| 91 |
memcpy(paddedToken, token, inSize); |
| 92 |
} |
| 93 |
|
| 94 |
CK_ULONG ulSlotCount; |
| 95 |
CK_RV rv = p11->C_GetSlotList(CK_TRUE, NULL_PTR, &ulSlotCount); |
| 96 |
if (rv != CKR_OK) |
| 97 |
{ |
| 98 |
fprintf(stderr, "ERROR: Could not get the number of slots.\n"); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
CK_SLOT_ID_PTR pSlotList = (CK_SLOT_ID_PTR) malloc(ulSlotCount*sizeof(CK_SLOT_ID)); |
| 103 |
if (pSlotList == NULL) |
| 104 |
{ |
| 105 |
fprintf(stderr, "ERROR: Could not allocate memory.\n"); |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
rv = p11->C_GetSlotList(CK_FALSE, pSlotList, &ulSlotCount); |
| 110 |
if (rv != CKR_OK) |
| 111 |
{ |
| 112 |
fprintf(stderr, "ERROR: Could not get the slot list.\n"); |
| 113 |
free(pSlotList); |
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
size_t counter = 0; |
| 118 |
for (CK_ULONG i = 0; i < ulSlotCount; i++) |
| 119 |
{ |
| 120 |
CK_TOKEN_INFO tokenInfo; |
| 121 |
|
| 122 |
rv = p11->C_GetTokenInfo(pSlotList[i], &tokenInfo); |
| 123 |
if (rv != CKR_OK) |
| 124 |
{ |
| 125 |
fprintf(stderr, "ERROR: Could not get info about the token in slot %lu.\n", |
| 126 |
pSlotList[i]); |
| 127 |
free(pSlotList); |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
if (freeToken) |
| 132 |
{ |
| 133 |
if ((tokenInfo.flags & CKF_TOKEN_INITIALIZED) == 0) |
| 134 |
{ |
| 135 |
printf("Slot %lu has a free/uninitialized token.\n", pSlotList[i]); |
| 136 |
slotID = pSlotList[i]; |
| 137 |
free(pSlotList); |
| 138 |
return 0; |
| 139 |
} |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
if (serial != NULL && token == NULL && |
| 144 |
memcmp(tokenInfo.serialNumber, paddedSerial, sizeof(paddedSerial)) == 0) |
| 145 |
{ |
| 146 |
printf("Found slot %lu with matching serial.\n", |
| 147 |
pSlotList[i]); |
| 148 |
slotID = pSlotList[i]; |
| 149 |
counter++; |
| 150 |
} |
| 151 |
if (serial == NULL && token != NULL && |
| 152 |
memcmp(tokenInfo.label, paddedToken, sizeof(paddedToken)) == 0) |
| 153 |
{ |
| 154 |
printf("Found slot %lu with matching token label.\n", |
| 155 |
pSlotList[i]); |
| 156 |
slotID = pSlotList[i]; |
| 157 |
counter++; |
| 158 |
} |
| 159 |
if (serial != NULL && token != NULL && |
| 160 |
memcmp(tokenInfo.serialNumber, paddedSerial, sizeof(paddedSerial)) == 0 && |
| 161 |
memcmp(tokenInfo.label, paddedToken, sizeof(paddedToken)) == 0) |
| 162 |
{ |
| 163 |
printf("Found slot %lu with matching serial and token label.\n", |
| 164 |
pSlotList[i]); |
| 165 |
slotID = pSlotList[i]; |
| 166 |
counter++; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
free(pSlotList); |
| 172 |
|
| 173 |
if (counter == 1) return 0; |
| 174 |
if (counter > 1) |
| 175 |
{ |
| 176 |
fprintf(stderr, "ERROR: Found multiple matching slots/tokens.\n"); |
| 177 |
return 1; |
| 178 |
} |
| 179 |
|
| 180 |
fprintf(stderr, "ERROR: Could not find a slot/token using --serial, --token, or --free.\n"); |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
// Find the slot/token |
| 185 |
int findSlot(char* slot, char* serial, char* token, CK_SLOT_ID& slotID) |
| 186 |
{ |
| 187 |
if (slot != NULL) |
| 188 |
{ |
| 189 |
int slotNumber = atoi(slot); |
| 190 |
if (slotNumber < 0) |
| 191 |
{ |
| 192 |
fprintf(stderr, "ERROR: The slot number is negative.\n"); |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
slotID = slotNumber; |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
if (serial == NULL && token == NULL) |
| 201 |
{ |
| 202 |
fprintf(stderr, "ERROR: A slot/token must be supplied. " |
| 203 |
"Use --slot <number>, --serial <serial>, " |
| 204 |
"or --token <label>\n"); |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
// Load the variables |
| 209 |
CK_UTF8CHAR paddedSerial[16]; |
| 210 |
CK_UTF8CHAR paddedToken[32]; |
| 211 |
if (serial != NULL) |
| 212 |
{ |
| 213 |
size_t inSize = strlen(serial); |
| 214 |
size_t outSize = sizeof(paddedSerial); |
| 215 |
if (inSize > outSize) |
| 216 |
{ |
| 217 |
fprintf(stderr, "ERROR: --serial is too long.\n"); |
| 218 |
return 1; |
| 219 |
} |
| 220 |
memset(paddedSerial, ' ', outSize); |
| 221 |
memcpy(paddedSerial, serial, inSize); |
| 222 |
} |
| 223 |
if (token != NULL) |
| 224 |
{ |
| 225 |
size_t inSize = strlen(token); |
| 226 |
size_t outSize = sizeof(paddedToken); |
| 227 |
if (inSize > outSize) |
| 228 |
{ |
| 229 |
fprintf(stderr, "ERROR: --token is too long.\n"); |
| 230 |
return 1; |
| 231 |
} |
| 232 |
memset(paddedToken, ' ', outSize); |
| 233 |
memcpy(paddedToken, token, inSize); |
| 234 |
} |
| 235 |
|
| 236 |
CK_ULONG ulSlotCount; |
| 237 |
CK_RV rv = p11->C_GetSlotList(CK_TRUE, NULL_PTR, &ulSlotCount); |
| 238 |
if (rv != CKR_OK) |
| 239 |
{ |
| 240 |
fprintf(stderr, "ERROR: Could not get the number of slots.\n"); |
| 241 |
return 1; |
| 242 |
} |
| 243 |
|
| 244 |
CK_SLOT_ID_PTR pSlotList = (CK_SLOT_ID_PTR) malloc(ulSlotCount*sizeof(CK_SLOT_ID)); |
| 245 |
if (pSlotList == NULL) |
| 246 |
{ |
| 247 |
fprintf(stderr, "ERROR: Could not allocate memory.\n"); |
| 248 |
return 1; |
| 249 |
} |
| 250 |
|
| 251 |
rv = p11->C_GetSlotList(CK_FALSE, pSlotList, &ulSlotCount); |
| 252 |
if (rv != CKR_OK) |
| 253 |
{ |
| 254 |
fprintf(stderr, "ERROR: Could not get the slot list.\n"); |
| 255 |
free(pSlotList); |
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
size_t counter = 0; |
| 260 |
for (CK_ULONG i = 0; i < ulSlotCount; i++) |
| 261 |
{ |
| 262 |
CK_TOKEN_INFO tokenInfo; |
| 263 |
|
| 264 |
rv = p11->C_GetTokenInfo(pSlotList[i], &tokenInfo); |
| 265 |
if (rv != CKR_OK) |
| 266 |
{ |
| 267 |
fprintf(stderr, "ERROR: Could not get info about the token in slot %lu.\n", |
| 268 |
pSlotList[i]); |
| 269 |
free(pSlotList); |
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
if (serial != NULL && token == NULL && |
| 274 |
memcmp(tokenInfo.serialNumber, paddedSerial, sizeof(paddedSerial)) == 0) |
| 275 |
{ |
| 276 |
printf("Found slot %lu with matching serial.\n", |
| 277 |
pSlotList[i]); |
| 278 |
slotID = pSlotList[i]; |
| 279 |
counter++; |
| 280 |
} |
| 281 |
if (serial == NULL && token != NULL && |
| 282 |
memcmp(tokenInfo.label, paddedToken, sizeof(paddedToken)) == 0) |
| 283 |
{ |
| 284 |
printf("Found slot %lu with matching token label.\n", |
| 285 |
pSlotList[i]); |
| 286 |
slotID = pSlotList[i]; |
| 287 |
counter++; |
| 288 |
} |
| 289 |
if (serial != NULL && token != NULL && |
| 290 |
memcmp(tokenInfo.serialNumber, paddedSerial, sizeof(paddedSerial)) == 0 && |
| 291 |
memcmp(tokenInfo.label, paddedToken, sizeof(paddedToken)) == 0) |
| 292 |
{ |
| 293 |
printf("Found slot %lu with matching serial and token label.\n", |
| 294 |
pSlotList[i]); |
| 295 |
slotID = pSlotList[i]; |
| 296 |
counter++; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
free(pSlotList); |
| 301 |
|
| 302 |
if (counter == 1) return 0; |
| 303 |
if (counter > 1) |
| 304 |
{ |
| 305 |
fprintf(stderr, "ERROR: Found multiple matching slots/tokens.\n"); |
| 306 |
return 1; |
| 307 |
} |
| 308 |
|
| 309 |
fprintf(stderr, "ERROR: Could not find a slot/token using --serial, or --token\n"); |
| 310 |
return 1; |
| 311 |
} |
| 312 |
|
| 313 |
// Find the slot/token |
| 314 |
int findSlot(CK_TOKEN_INFO tokenInfo, CK_SLOT_ID& slotID) |
| 315 |
{ |
| 316 |
CK_ULONG ulSlotCount; |
| 317 |
CK_RV rv = p11->C_GetSlotList(CK_TRUE, NULL_PTR, &ulSlotCount); |
| 318 |
if (rv != CKR_OK) |
| 319 |
{ |
| 320 |
fprintf(stderr, "ERROR: Could not get the number of slots.\n"); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
CK_SLOT_ID_PTR pSlotList = (CK_SLOT_ID_PTR) malloc(ulSlotCount*sizeof(CK_SLOT_ID)); |
| 325 |
if (pSlotList == NULL) |
| 326 |
{ |
| 327 |
fprintf(stderr, "ERROR: Could not allocate memory.\n"); |
| 328 |
return 1; |
| 329 |
} |
| 330 |
|
| 331 |
rv = p11->C_GetSlotList(CK_FALSE, pSlotList, &ulSlotCount); |
| 332 |
if (rv != CKR_OK) |
| 333 |
{ |
| 334 |
fprintf(stderr, "ERROR: Could not get the slot list.\n"); |
| 335 |
free(pSlotList); |
| 336 |
return 1; |
| 337 |
} |
| 338 |
|
| 339 |
size_t counter = 0; |
| 340 |
for (CK_ULONG i = 0; i < ulSlotCount; i++) |
| 341 |
{ |
| 342 |
CK_TOKEN_INFO currentTokenInfo; |
| 343 |
|
| 344 |
rv = p11->C_GetTokenInfo(pSlotList[i], ¤tTokenInfo); |
| 345 |
if (rv != CKR_OK) |
| 346 |
{ |
| 347 |
fprintf(stderr, "ERROR: Could not get info about the token in slot %lu.\n", |
| 348 |
pSlotList[i]); |
| 349 |
free(pSlotList); |
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
if (memcmp(currentTokenInfo.serialNumber, tokenInfo.serialNumber, sizeof(tokenInfo.serialNumber)) == 0 && |
| 354 |
memcmp(currentTokenInfo.label, tokenInfo.label, sizeof(tokenInfo.label)) == 0) |
| 355 |
{ |
| 356 |
slotID = pSlotList[i]; |
| 357 |
counter++; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
free(pSlotList); |
| 362 |
|
| 363 |
if (counter == 1) return 0; |
| 364 |
if (counter > 1) |
| 365 |
{ |
| 366 |
fprintf(stderr, "ERROR: Found multiple matching slots/tokens.\n"); |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
fprintf(stderr, "ERROR: Could not find a slot/token using --serial, or --token\n"); |
| 371 |
return 1; |
| 372 |
} |