| 1 |
#include <limits.h> |
| 2 |
#include <errno.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <dlfcn.h> |
| 7 |
#include <stdint.h> |
| 8 |
|
| 9 |
#include "platform.h" |
| 10 |
#include "pkcs11types.h" |
| 11 |
#include "ec_curves.h" |
| 12 |
|
| 13 |
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) |
| 14 |
|
| 15 |
#define PKCS11_MAX_PIN_LEN 128 |
| 16 |
#define PKCS11_SO_PIN_ENV_VAR "PKCS11_SO_PIN" |
| 17 |
#define PKCS11_USER_PIN_ENV_VAR "PKCS11_USER_PIN" |
| 18 |
|
| 19 |
#define POLICY_TEST_RSA_SIZE 4096 |
| 20 |
#define POLICY_TEST_EC_SIZE 384 |
| 21 |
#define POLICY_TEST_AES_SIZE 256 |
| 22 |
|
| 23 |
/* Tests implemented (Bit indices) */ |
| 24 |
#define TEST_RSA 1u |
| 25 |
#define TEST_EC 2u |
| 26 |
#define TEST_AESWRAP 3u |
| 27 |
#define TEST_DIGEST 4u |
| 28 |
#define TEST_DH 5u |
| 29 |
|
| 30 |
static CK_FUNCTION_LIST *funcs; |
| 31 |
static void *pkcs11lib; |
| 32 |
static CK_SESSION_HANDLE session; |
| 33 |
|
| 34 |
static const char rsamessage[POLICY_TEST_RSA_SIZE / 8 - 11] = {0}; |
| 35 |
static const char ecmessage[POLICY_TEST_EC_SIZE / 8] = {0}; |
| 36 |
|
| 37 |
static const CK_UTF8CHAR poltestlabel[] = "policytest key"; |
| 38 |
static const CK_UTF8CHAR poltestdhlabelA[] = "policytest key A"; |
| 39 |
static const CK_UTF8CHAR poltestdhlabelB[] = "policytest key B"; |
| 40 |
|
| 41 |
static CK_BBOOL tokenkeys = CK_FALSE; |
| 42 |
|
| 43 |
static struct { |
| 44 |
CK_OBJECT_HANDLE rsapub; |
| 45 |
CK_OBJECT_HANDLE rsapriv; |
| 46 |
CK_OBJECT_HANDLE ecpub; |
| 47 |
CK_OBJECT_HANDLE ecpriv; |
| 48 |
CK_OBJECT_HANDLE aes; |
| 49 |
CK_OBJECT_HANDLE dhpubA; |
| 50 |
CK_OBJECT_HANDLE dhprivA; |
| 51 |
CK_OBJECT_HANDLE dhpubB; |
| 52 |
CK_OBJECT_HANDLE dhprivB; |
| 53 |
} keys; |
| 54 |
|
| 55 |
/* copied from testcases/crypto/dh_func.c */ |
| 56 |
CK_BYTE DH_PUBL_PRIME[128] = { |
| 57 |
0xd5, 0xb1, 0xaa, 0x6a, 0x3b, 0x85, 0x50, 0xf0, 0xe2, |
| 58 |
0xea, 0x6b, 0xec, 0x26, 0x3b, 0xe0, 0xbf, 0x7a, 0x82, |
| 59 |
0x45, 0x1b, 0xa8, 0x0a, 0x54, 0x2e, 0x14, 0x2c, 0xc2, |
| 60 |
0x58, 0xb1, 0xf5, 0x59, 0xec, 0x7d, 0x16, 0x9e, 0x00, |
| 61 |
0x62, 0xb3, 0xa7, 0xdc, 0x38, 0x6f, 0x64, 0x40, 0xfc, |
| 62 |
0x0d, 0x3e, 0x0b, 0x66, 0x13, 0x5e, 0xa5, 0x84, 0x90, |
| 63 |
0x26, 0x62, 0xcf, 0x5a, 0x14, 0x72, 0x2d, 0x1b, 0x37, |
| 64 |
0x7e, 0x8a, 0x4b, 0xc0, 0xb7, 0xf2, 0x63, 0xd1, 0xaa, |
| 65 |
0x51, 0x92, 0x96, 0x18, 0xae, 0xb9, 0xfd, 0x5f, 0x9d, |
| 66 |
0x5d, 0xdf, 0x75, 0xa9, 0x80, 0x3d, 0xaa, 0xc2, 0x54, |
| 67 |
0x00, 0xcc, 0xc1, 0x9e, 0x31, 0x4d, 0x22, 0x31, 0x44, |
| 68 |
0xe9, 0x69, 0x34, 0xae, 0xcf, 0xcd, 0x6d, 0xf6, 0xe9, |
| 69 |
0x37, 0x20, 0xa4, 0xd3, 0x85, 0x24, 0xff, 0x9f, 0x39, |
| 70 |
0xeb, 0x78, 0xf2, 0xd1, 0xc3, 0xf9, 0x66, 0xab, 0xbd, |
| 71 |
0x2d, 0xd3 |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
CK_BYTE DH_PUBL_BASE[128] = { |
| 76 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 77 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 79 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 80 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 81 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 82 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 84 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 86 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 87 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 88 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 89 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 90 |
0x00, 0x02 |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
static void unloadLib(void) |
| 95 |
{ |
| 96 |
#ifndef WITH_SANITIZER |
| 97 |
if (pkcs11lib) |
| 98 |
dlclose(pkcs11lib); |
| 99 |
#endif |
| 100 |
} |
| 101 |
|
| 102 |
static void finalizeLib(void) |
| 103 |
{ |
| 104 |
funcs->C_Finalize(NULL); |
| 105 |
} |
| 106 |
|
| 107 |
static void closeSession(void) |
| 108 |
{ |
| 109 |
funcs->C_CloseSession(session); |
| 110 |
} |
| 111 |
|
| 112 |
static void logoutUser(void) |
| 113 |
{ |
| 114 |
funcs->C_Logout(session); |
| 115 |
} |
| 116 |
|
| 117 |
static int doLoadLib(void) |
| 118 |
{ |
| 119 |
const char *lib = OCK_API_LIBNAME; |
| 120 |
const char *envlib; |
| 121 |
CK_RV (*pGetFuncList)(CK_FUNCTION_LIST **); |
| 122 |
CK_RV rc; |
| 123 |
|
| 124 |
if ((envlib = getenv("PKCSLIB")) != NULL) |
| 125 |
lib = envlib; |
| 126 |
pkcs11lib = dlopen(lib, DYNLIB_LDFLAGS); |
| 127 |
if (pkcs11lib == NULL) { |
| 128 |
fprintf(stderr, "Failed to open %s: %s\n", lib, dlerror()); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
atexit(unloadLib); |
| 132 |
*(void**)(&pGetFuncList) = dlsym(pkcs11lib, "C_GetFunctionList"); |
| 133 |
if (pGetFuncList == NULL) { |
| 134 |
fprintf(stderr, "Failed to find C_GetFunctionList symbol in %s\n", lib); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
rc = pGetFuncList(&funcs); |
| 138 |
if (rc != CKR_OK) { |
| 139 |
fprintf(stderr, "C_GetFunctionList failed with 0x%lx\n", rc); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int doInitLib(void) |
| 146 |
{ |
| 147 |
CK_C_INITIALIZE_ARGS cinit_args; |
| 148 |
CK_RV rc; |
| 149 |
|
| 150 |
memset(&cinit_args, 0x0, sizeof(cinit_args)); |
| 151 |
cinit_args.flags = CKF_OS_LOCKING_OK; |
| 152 |
rc = funcs->C_Initialize(&cinit_args); |
| 153 |
if (rc != CKR_OK) { |
| 154 |
fprintf(stderr, "C_Initialize failed with 0x%lx\n", rc); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
atexit(finalizeLib); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static int doOpenSession(CK_SLOT_ID slot) |
| 162 |
{ |
| 163 |
CK_RV rc; |
| 164 |
CK_FLAGS flags = CKF_SERIAL_SESSION | CKF_RW_SESSION; |
| 165 |
|
| 166 |
rc = funcs->C_OpenSession(slot, flags, NULL, NULL, &session); |
| 167 |
if (rc != CKR_OK) { |
| 168 |
fprintf(stderr, "Failed to open r/w session (rc = 0x%lx)\n", rc); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
atexit(closeSession); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
static int doLoginUser(unsigned char *pin) |
| 176 |
{ |
| 177 |
CK_RV rc; |
| 178 |
|
| 179 |
rc = funcs->C_Login(session, CKU_USER, pin, strlen((char *)pin)); |
| 180 |
if (rc != CKR_OK) { |
| 181 |
fprintf(stderr, "Failed to log in to token (rc = 0x%lx)\n", rc); |
| 182 |
return -1; |
| 183 |
} |
| 184 |
atexit(logoutUser); |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
static int is_cca_token(CK_SLOT_ID slot_id) |
| 189 |
{ |
| 190 |
CK_RV rc; |
| 191 |
CK_TOKEN_INFO tokinfo; |
| 192 |
|
| 193 |
rc = funcs->C_GetTokenInfo(slot_id, &tokinfo); |
| 194 |
if (rc != CKR_OK) |
| 195 |
return FALSE; |
| 196 |
|
| 197 |
return strstr((const char *) tokinfo.model, "CCA") != NULL; |
| 198 |
} |
| 199 |
|
| 200 |
static CK_RV generateRSAKey(void) |
| 201 |
{ |
| 202 |
CK_OBJECT_HANDLE pubkey, privkey; |
| 203 |
CK_MECHANISM mech = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL, 0 }; |
| 204 |
CK_BYTE subject[] = {0}; |
| 205 |
CK_BYTE id[] = {123}; |
| 206 |
CK_BBOOL cktrue = TRUE; |
| 207 |
CK_ULONG modulusBits = POLICY_TEST_RSA_SIZE; |
| 208 |
CK_BYTE publicExponent[] = {0x01, 0x00, 0x01}; |
| 209 |
CK_ATTRIBUTE pubkeyTemplate[] = { |
| 210 |
{CKA_ENCRYPT, &cktrue, sizeof(cktrue)}, |
| 211 |
{CKA_VERIFY, &cktrue, sizeof(cktrue)}, |
| 212 |
{CKA_WRAP, &cktrue, sizeof(cktrue)}, |
| 213 |
{CKA_MODULUS_BITS, &modulusBits, sizeof(modulusBits)}, |
| 214 |
{CKA_PUBLIC_EXPONENT, publicExponent, sizeof(publicExponent)}, |
| 215 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 216 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 217 |
}; |
| 218 |
CK_ATTRIBUTE privkeyTemplate[] = { |
| 219 |
{CKA_TOKEN, &cktrue, sizeof(cktrue)}, |
| 220 |
{CKA_PRIVATE, &cktrue, sizeof(cktrue)}, |
| 221 |
{CKA_SUBJECT, subject, 0}, |
| 222 |
{CKA_ID, id, sizeof(id)}, |
| 223 |
{CKA_DECRYPT, &cktrue, sizeof(cktrue)}, |
| 224 |
{CKA_SIGN, &cktrue, sizeof(cktrue)}, |
| 225 |
{CKA_UNWRAP, &cktrue, sizeof(cktrue)}, |
| 226 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 227 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 228 |
}; |
| 229 |
|
| 230 |
return funcs->C_GenerateKeyPair(session, &mech, |
| 231 |
pubkeyTemplate, ARRAY_SIZE(pubkeyTemplate), |
| 232 |
privkeyTemplate, ARRAY_SIZE(privkeyTemplate), |
| 233 |
&pubkey, &privkey); |
| 234 |
} |
| 235 |
|
| 236 |
static CK_RV generateECKey(void) |
| 237 |
{ |
| 238 |
CK_OBJECT_HANDLE pubkey, privkey; |
| 239 |
CK_BYTE secp384r1[] = OCK_SECP384R1; |
| 240 |
CK_MECHANISM mech = { CKM_EC_KEY_PAIR_GEN, NULL, 0 }; |
| 241 |
CK_BYTE subject[] = {0}; |
| 242 |
CK_BYTE id[] = { 123 }; |
| 243 |
CK_BBOOL true = TRUE; |
| 244 |
CK_ATTRIBUTE publicKeyTemplate[] = { |
| 245 |
{CKA_VERIFY, &true, sizeof(true)}, |
| 246 |
{CKA_EC_PARAMS, &secp384r1, sizeof(secp384r1)}, |
| 247 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 248 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 249 |
}; |
| 250 |
CK_ATTRIBUTE privateKeyTemplate[] = { |
| 251 |
{CKA_PRIVATE, &true, sizeof(true)}, |
| 252 |
{CKA_SUBJECT, subject, 0}, |
| 253 |
{CKA_ID, id, sizeof(id)}, |
| 254 |
{CKA_SIGN, &true, sizeof(true)}, |
| 255 |
{CKA_DERIVE, &true, sizeof(true)}, |
| 256 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 257 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 258 |
}; |
| 259 |
|
| 260 |
return funcs->C_GenerateKeyPair(session, &mech, |
| 261 |
publicKeyTemplate, ARRAY_SIZE(publicKeyTemplate), |
| 262 |
privateKeyTemplate, ARRAY_SIZE(privateKeyTemplate), |
| 263 |
&pubkey, &privkey); |
| 264 |
} |
| 265 |
|
| 266 |
static CK_RV generateAESKey(void) |
| 267 |
{ |
| 268 |
CK_OBJECT_HANDLE key; |
| 269 |
CK_ULONG keylen = POLICY_TEST_AES_SIZE / 8; |
| 270 |
CK_ATTRIBUTE key_gen_tmpl[] = { |
| 271 |
{CKA_VALUE_LEN, &keylen, sizeof(CK_ULONG)}, |
| 272 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 273 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 274 |
}; |
| 275 |
CK_MECHANISM mech = { |
| 276 |
.mechanism = CKM_AES_KEY_GEN, |
| 277 |
.ulParameterLen = 0, |
| 278 |
.pParameter = NULL, |
| 279 |
}; |
| 280 |
|
| 281 |
return funcs->C_GenerateKey(session, &mech, key_gen_tmpl, |
| 282 |
ARRAY_SIZE(key_gen_tmpl), &key); |
| 283 |
} |
| 284 |
|
| 285 |
static CK_RV generateDHKeys(void) |
| 286 |
{ |
| 287 |
CK_OBJECT_HANDLE pubkeyA, privkeyA, pubkeyB, privkeyB; |
| 288 |
CK_OBJECT_CLASS pub_key_class = CKO_PUBLIC_KEY; |
| 289 |
CK_KEY_TYPE key_type = CKK_DH; |
| 290 |
CK_OBJECT_CLASS priv_key_class = CKO_PRIVATE_KEY; |
| 291 |
CK_BBOOL ltrue = CK_TRUE; |
| 292 |
CK_ATTRIBUTE publA_tmpl[] = { |
| 293 |
{CKA_CLASS, &pub_key_class, sizeof(pub_key_class)}, |
| 294 |
{CKA_KEY_TYPE, &key_type, sizeof(key_type)}, |
| 295 |
{CKA_PRIME, DH_PUBL_PRIME, sizeof(DH_PUBL_PRIME)}, |
| 296 |
{CKA_BASE, DH_PUBL_BASE, sizeof(DH_PUBL_BASE)}, |
| 297 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 298 |
{CKA_LABEL, (void *)poltestdhlabelA, sizeof(poltestdhlabelA)} |
| 299 |
}; |
| 300 |
CK_ATTRIBUTE privA_tmpl[] = { |
| 301 |
{CKA_CLASS, &priv_key_class, sizeof(priv_key_class)}, |
| 302 |
{CKA_KEY_TYPE, &key_type, sizeof(key_type)}, |
| 303 |
{CKA_DERIVE, <rue, sizeof(ltrue)}, |
| 304 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 305 |
{CKA_LABEL, (void *)poltestdhlabelA, sizeof(poltestdhlabelA)} |
| 306 |
}; |
| 307 |
CK_ATTRIBUTE publB_tmpl[] = { |
| 308 |
{CKA_CLASS, &pub_key_class, sizeof(pub_key_class)}, |
| 309 |
{CKA_KEY_TYPE, &key_type, sizeof(key_type)}, |
| 310 |
{CKA_PRIME, DH_PUBL_PRIME, sizeof(DH_PUBL_PRIME)}, |
| 311 |
{CKA_BASE, DH_PUBL_BASE, sizeof(DH_PUBL_BASE)}, |
| 312 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 313 |
{CKA_LABEL, (void *)poltestdhlabelB, sizeof(poltestdhlabelB)} |
| 314 |
}; |
| 315 |
CK_ATTRIBUTE privB_tmpl[] = { |
| 316 |
{CKA_CLASS, &priv_key_class, sizeof(priv_key_class)}, |
| 317 |
{CKA_KEY_TYPE, &key_type, sizeof(key_type)}, |
| 318 |
{CKA_DERIVE, <rue, sizeof(ltrue)}, |
| 319 |
{CKA_TOKEN, &tokenkeys, sizeof(tokenkeys)}, |
| 320 |
{CKA_LABEL, (void *)poltestdhlabelB, sizeof(poltestdhlabelB)} |
| 321 |
}; |
| 322 |
CK_MECHANISM mech = { CKM_DH_PKCS_KEY_PAIR_GEN, NULL, 0 }; |
| 323 |
CK_RV rc; |
| 324 |
|
| 325 |
rc = funcs->C_GenerateKeyPair(session, &mech, |
| 326 |
publA_tmpl, ARRAY_SIZE(publA_tmpl), |
| 327 |
privA_tmpl, ARRAY_SIZE(privA_tmpl), |
| 328 |
&pubkeyA, &privkeyA); |
| 329 |
if (rc != CKR_OK) |
| 330 |
return rc; |
| 331 |
rc = funcs->C_GenerateKeyPair(session, &mech, |
| 332 |
publB_tmpl, ARRAY_SIZE(publB_tmpl), |
| 333 |
privB_tmpl, ARRAY_SIZE(privB_tmpl), |
| 334 |
&pubkeyB, &privkeyB); |
| 335 |
if (rc != CKR_OK) { |
| 336 |
funcs->C_DestroyObject(session, pubkeyA); |
| 337 |
funcs->C_DestroyObject(session, privkeyA); |
| 338 |
} |
| 339 |
return rc; |
| 340 |
} |
| 341 |
|
| 342 |
static CK_RV encryptRSA(CK_OBJECT_HANDLE key, CK_BYTE *res, CK_ULONG *reslen) |
| 343 |
{ |
| 344 |
CK_MECHANISM mech = { .mechanism = CKM_RSA_PKCS, |
| 345 |
.pParameter = NULL, |
| 346 |
.ulParameterLen = 0 }; |
| 347 |
CK_RV rc; |
| 348 |
|
| 349 |
rc = funcs->C_EncryptInit(session, &mech, key); |
| 350 |
if (rc != CKR_OK) |
| 351 |
return rc; |
| 352 |
rc = funcs->C_Encrypt(session, (CK_BYTE *)rsamessage, sizeof(rsamessage), |
| 353 |
res, reslen); |
| 354 |
return rc; |
| 355 |
} |
| 356 |
|
| 357 |
static CK_RV decryptRSA(CK_OBJECT_HANDLE key, CK_BYTE *enc, CK_ULONG len) |
| 358 |
{ |
| 359 |
CK_MECHANISM mech = { .mechanism = CKM_RSA_PKCS, |
| 360 |
.pParameter = NULL, |
| 361 |
.ulParameterLen = 0 }; |
| 362 |
CK_BYTE dec[POLICY_TEST_RSA_SIZE / 8]; |
| 363 |
CK_ULONG declen = sizeof(dec); |
| 364 |
CK_RV rc; |
| 365 |
|
| 366 |
rc = funcs->C_DecryptInit(session, &mech, key); |
| 367 |
if (rc != CKR_OK) |
| 368 |
return rc; |
| 369 |
rc = funcs->C_Decrypt(session, enc, len, dec, &declen); |
| 370 |
if (rc != CKR_OK) |
| 371 |
return rc; |
| 372 |
if (memcmp(rsamessage, dec, declen)) |
| 373 |
return CKR_GENERAL_ERROR; |
| 374 |
return rc; |
| 375 |
} |
| 376 |
|
| 377 |
static int runRSATest(void) |
| 378 |
{ |
| 379 |
CK_BYTE enc[POLICY_TEST_RSA_SIZE / 8]; |
| 380 |
CK_ULONG enclen = sizeof(enc); |
| 381 |
CK_RV rc; |
| 382 |
|
| 383 |
rc = encryptRSA(keys.rsapub, enc, &enclen); |
| 384 |
if (rc != CKR_OK) { |
| 385 |
fprintf(stderr, "WARN: RSA public key usage failed with 0x%lx\n", rc); |
| 386 |
return -1; |
| 387 |
} |
| 388 |
rc = decryptRSA(keys.rsapriv, enc, enclen); |
| 389 |
if (rc != CKR_OK) { |
| 390 |
fprintf(stderr, "WARN: RSA private key usage failed with 0x%lx\n", rc); |
| 391 |
return -1; |
| 392 |
} |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
static CK_RV signEC(CK_OBJECT_HANDLE key, CK_BYTE *sig, CK_ULONG *siglen) |
| 398 |
{ |
| 399 |
CK_MECHANISM mech = { CKM_ECDSA, NULL, 0 }; |
| 400 |
CK_RV rc; |
| 401 |
|
| 402 |
rc = funcs->C_SignInit(session, &mech, key); |
| 403 |
if (rc != CKR_OK) |
| 404 |
return rc; |
| 405 |
rc = funcs->C_Sign(session, (CK_BYTE *)ecmessage, sizeof(ecmessage), |
| 406 |
sig, siglen); |
| 407 |
return rc; |
| 408 |
} |
| 409 |
|
| 410 |
static CK_RV verifyEC(CK_OBJECT_HANDLE key, CK_BYTE *sig, CK_ULONG siglen) |
| 411 |
{ |
| 412 |
CK_MECHANISM mech = { CKM_ECDSA, NULL, 0 }; |
| 413 |
CK_RV rc; |
| 414 |
|
| 415 |
rc = funcs->C_VerifyInit(session, &mech, key); |
| 416 |
if (rc != CKR_OK) |
| 417 |
return rc; |
| 418 |
rc = funcs->C_Verify(session, (CK_BYTE *)ecmessage, sizeof(ecmessage), |
| 419 |
sig, siglen); |
| 420 |
return rc; |
| 421 |
} |
| 422 |
|
| 423 |
static int runECTest(void) |
| 424 |
{ |
| 425 |
CK_BYTE sig[2 * POLICY_TEST_EC_SIZE / 8]; |
| 426 |
CK_ULONG siglen = sizeof(sig); |
| 427 |
CK_RV rc; |
| 428 |
|
| 429 |
rc = signEC(keys.ecpriv, sig, &siglen); |
| 430 |
if (rc != CKR_OK) { |
| 431 |
fprintf(stderr, "WARN: EC private key usage failed with 0x%lx\n", rc); |
| 432 |
return -1; |
| 433 |
} |
| 434 |
rc = verifyEC(keys.ecpub, sig, siglen); |
| 435 |
if (rc != CKR_OK) { |
| 436 |
fprintf(stderr, "WARN: EC public key usage failed with 0x%lx\n", rc); |
| 437 |
return -1; |
| 438 |
} |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
static CK_RV wrapAESKey(CK_OBJECT_HANDLE key, CK_BYTE **wrapped, CK_ULONG *len) |
| 443 |
{ |
| 444 |
CK_BYTE iv[16] = {0}; |
| 445 |
CK_MECHANISM mech = { CKM_AES_CBC_PAD, iv, sizeof(iv) }; |
| 446 |
CK_RV rc; |
| 447 |
CK_BYTE *res; |
| 448 |
|
| 449 |
rc = funcs->C_WrapKey(session, &mech, key, key, NULL, len); |
| 450 |
if (rc != CKR_OK) |
| 451 |
return rc; |
| 452 |
res = calloc(1, *len); |
| 453 |
if (!res) |
| 454 |
return CKR_HOST_MEMORY; |
| 455 |
rc = funcs->C_WrapKey(session, &mech, key, key, res, len); |
| 456 |
if (rc != CKR_OK) { |
| 457 |
free(res); |
| 458 |
return rc; |
| 459 |
} |
| 460 |
*wrapped = res; |
| 461 |
return rc; |
| 462 |
} |
| 463 |
|
| 464 |
static CK_RV unwrapAESKey(CK_OBJECT_HANDLE key, CK_BYTE *wrapped, CK_ULONG len, |
| 465 |
CK_OBJECT_HANDLE_PTR unwrapped, int iscca) |
| 466 |
{ |
| 467 |
CK_BYTE iv[16] = {0}; |
| 468 |
CK_MECHANISM mech = { CKM_AES_CBC_PAD, iv, sizeof(iv) }; |
| 469 |
CK_RV rc; |
| 470 |
CK_OBJECT_CLASS key_class = CKO_SECRET_KEY; |
| 471 |
CK_KEY_TYPE key_type = CKK_AES; |
| 472 |
CK_ULONG key_size = POLICY_TEST_AES_SIZE / 8; |
| 473 |
CK_ATTRIBUTE template[] = { |
| 474 |
{CKA_CLASS, &key_class, sizeof(key_class)}, |
| 475 |
{CKA_KEY_TYPE, &key_type, sizeof(key_type)}, |
| 476 |
{CKA_VALUE_LEN, &key_size, sizeof(key_size)} /* For CCA only */ |
| 477 |
}; |
| 478 |
CK_ULONG templatecount = 2 + iscca; |
| 479 |
|
| 480 |
rc = funcs->C_UnwrapKey(session, &mech, key, wrapped, len, |
| 481 |
template, templatecount, unwrapped); |
| 482 |
free(wrapped); |
| 483 |
return rc; |
| 484 |
} |
| 485 |
|
| 486 |
static int runAESWrapTest(CK_SLOT_ID slot) |
| 487 |
{ |
| 488 |
CK_OBJECT_HANDLE unwrapped = CK_INVALID_HANDLE; |
| 489 |
CK_BYTE *wrapped; |
| 490 |
CK_ULONG wrappedlen; |
| 491 |
CK_RV rc; |
| 492 |
|
| 493 |
rc = wrapAESKey(keys.aes, &wrapped, &wrappedlen); |
| 494 |
if (rc != CKR_OK) { |
| 495 |
fprintf(stderr, "WARN: AES wrapping failed with 0x%lx\n", rc); |
| 496 |
return -1; |
| 497 |
} |
| 498 |
rc = unwrapAESKey(keys.aes, wrapped, wrappedlen, &unwrapped, |
| 499 |
is_cca_token(slot)); |
| 500 |
funcs->C_DestroyObject(session, unwrapped); |
| 501 |
if (rc != CKR_OK) { |
| 502 |
fprintf(stderr, "WARN: AES unwrapping failed with 0x%lx\n", rc); |
| 503 |
return -1; |
| 504 |
} |
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
static int runDIGESTTest(void) |
| 509 |
{ |
| 510 |
CK_BYTE digest[64]; |
| 511 |
CK_MECHANISM mech = { CKM_SHA512, NULL, 0 }; |
| 512 |
CK_RV rc; |
| 513 |
CK_ULONG len = sizeof(digest); |
| 514 |
|
| 515 |
rc = funcs->C_DigestInit(session, &mech); |
| 516 |
if (rc != CKR_OK) { |
| 517 |
fprintf(stderr, "WARN: Digest initialization failed with 0x%lx\n", rc); |
| 518 |
return -1; |
| 519 |
} |
| 520 |
rc = funcs->C_Digest(session, (CK_BYTE *)rsamessage, sizeof(rsamessage), |
| 521 |
digest, &len); |
| 522 |
if (rc != CKR_OK) { |
| 523 |
/* No policy checking done in C_Digest... */ |
| 524 |
fprintf(stderr, "WARN: Digest failed with 0x%lx\n", rc); |
| 525 |
return -1; |
| 526 |
} |
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
static int runDHTest(void) |
| 532 |
{ |
| 533 |
CK_BYTE key_value[sizeof(DH_PUBL_PRIME) * 2]; |
| 534 |
CK_ATTRIBUTE extr_tmpl = {CKA_VALUE, key_value, sizeof(key_value)}; |
| 535 |
CK_ULONG secret_key_size = sizeof(DH_PUBL_PRIME); |
| 536 |
CK_OBJECT_CLASS secret_key_class = CKO_SECRET_KEY; |
| 537 |
CK_KEY_TYPE secret_key_type = CKK_GENERIC_SECRET; |
| 538 |
CK_ATTRIBUTE secret_tmpl[] = { |
| 539 |
{CKA_CLASS, &secret_key_class, sizeof(secret_key_class)}, |
| 540 |
{CKA_KEY_TYPE, &secret_key_type, sizeof(secret_key_type)}, |
| 541 |
{CKA_VALUE_LEN, &secret_key_size, sizeof(secret_key_size)} |
| 542 |
}; |
| 543 |
CK_MECHANISM mech = { CKM_DH_PKCS_DERIVE, key_value, 0 }; |
| 544 |
CK_OBJECT_HANDLE derived = CK_INVALID_HANDLE; |
| 545 |
CK_RV rc; |
| 546 |
|
| 547 |
// Extract the peer's public key |
| 548 |
rc = funcs->C_GetAttributeValue(session, keys.dhpubB, &extr_tmpl, 1); |
| 549 |
if (rc != CKR_OK) { |
| 550 |
fprintf(stderr, |
| 551 |
"WARN: Getting CKA_VALUE for DH peer pub key failed with 0x%lx!\n", |
| 552 |
rc); |
| 553 |
return -1; |
| 554 |
} |
| 555 |
mech.ulParameterLen = extr_tmpl.ulValueLen; |
| 556 |
rc = funcs->C_DeriveKey(session, &mech, keys.dhprivA, secret_tmpl, ARRAY_SIZE(secret_tmpl), &derived); |
| 557 |
funcs->C_DestroyObject(session, derived); |
| 558 |
if (rc != CKR_OK) { |
| 559 |
fprintf(stderr, "WARN: DH key derivation failed with 0x%lx\n", rc); |
| 560 |
return -1; |
| 561 |
} |
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
static int generateKeys(void) |
| 566 |
{ |
| 567 |
int res = 0; |
| 568 |
|
| 569 |
if (generateRSAKey() != CKR_OK) |
| 570 |
res = -1; |
| 571 |
if (generateECKey() != CKR_OK) |
| 572 |
res = -1; |
| 573 |
if (generateAESKey() != CKR_OK) |
| 574 |
res = -1; |
| 575 |
if (generateDHKeys() != CKR_OK) |
| 576 |
res = -1; |
| 577 |
return res; |
| 578 |
} |
| 579 |
|
| 580 |
static int loadKeys(void) |
| 581 |
{ |
| 582 |
static const CK_KEY_TYPE rsakt = CKK_RSA; |
| 583 |
static const CK_KEY_TYPE eckt = CKK_EC; |
| 584 |
static const CK_KEY_TYPE aeskt = CKK_AES; |
| 585 |
static const CK_KEY_TYPE dhkt = CKK_DH; |
| 586 |
static const CK_OBJECT_CLASS publickey = CKO_PUBLIC_KEY; |
| 587 |
static const CK_OBJECT_CLASS privatekey = CKO_PRIVATE_KEY; |
| 588 |
static const CK_ATTRIBUTE rsapubtmpl[] = { |
| 589 |
{CKA_KEY_TYPE, (void *)&rsakt, sizeof(rsakt)}, |
| 590 |
{CKA_CLASS, (void *)&publickey, sizeof(publickey)}, |
| 591 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 592 |
}; |
| 593 |
static const CK_ATTRIBUTE rsaprivtmpl[] = { |
| 594 |
{CKA_KEY_TYPE, (void *)&rsakt, sizeof(rsakt)}, |
| 595 |
{CKA_CLASS, (void *)&privatekey, sizeof(privatekey)}, |
| 596 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 597 |
}; |
| 598 |
static const CK_ATTRIBUTE ecpubtmpl[] = { |
| 599 |
{CKA_KEY_TYPE, (void *)&eckt, sizeof(eckt)}, |
| 600 |
{CKA_CLASS, (void *)&publickey, sizeof(publickey)}, |
| 601 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 602 |
}; |
| 603 |
static const CK_ATTRIBUTE ecprivtmpl[] = { |
| 604 |
{CKA_KEY_TYPE, (void *)&eckt, sizeof(eckt)}, |
| 605 |
{CKA_CLASS, (void *)&privatekey, sizeof(privatekey)}, |
| 606 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 607 |
}; |
| 608 |
static const CK_ATTRIBUTE aestmpl[] = { |
| 609 |
{CKA_KEY_TYPE, (void *)&aeskt, sizeof(aeskt)}, |
| 610 |
{CKA_LABEL, (void *)poltestlabel, sizeof(poltestlabel)} |
| 611 |
}; |
| 612 |
static const CK_ATTRIBUTE dhApubtmpl[] = { |
| 613 |
{CKA_KEY_TYPE, (void *)&dhkt, sizeof(dhkt)}, |
| 614 |
{CKA_CLASS, (void *)&publickey, sizeof(publickey)}, |
| 615 |
{CKA_LABEL, (void *)poltestdhlabelA, sizeof(poltestdhlabelA)} |
| 616 |
}; |
| 617 |
static const CK_ATTRIBUTE dhAprivtmpl[] = { |
| 618 |
{CKA_KEY_TYPE, (void *)&dhkt, sizeof(dhkt)}, |
| 619 |
{CKA_CLASS, (void *)&privatekey, sizeof(privatekey)}, |
| 620 |
{CKA_LABEL, (void *)poltestdhlabelA, sizeof(poltestdhlabelA)} |
| 621 |
}; |
| 622 |
static const CK_ATTRIBUTE dhBpubtmpl[] = { |
| 623 |
{CKA_KEY_TYPE, (void *)&dhkt, sizeof(dhkt)}, |
| 624 |
{CKA_CLASS, (void *)&publickey, sizeof(publickey)}, |
| 625 |
{CKA_LABEL, (void *)poltestdhlabelB, sizeof(poltestdhlabelB)} |
| 626 |
}; |
| 627 |
static const CK_ATTRIBUTE dhBprivtmpl[] = { |
| 628 |
{CKA_KEY_TYPE, (void *)&dhkt, sizeof(dhkt)}, |
| 629 |
{CKA_CLASS, (void *)&privatekey, sizeof(privatekey)}, |
| 630 |
{CKA_LABEL, (void *)poltestdhlabelB, sizeof(poltestdhlabelB)} |
| 631 |
}; |
| 632 |
CK_OBJECT_HANDLE handle; |
| 633 |
CK_ULONG count; |
| 634 |
CK_RV rc; |
| 635 |
int res = 0; |
| 636 |
|
| 637 |
// Find RSA pubkey |
| 638 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)rsapubtmpl, |
| 639 |
ARRAY_SIZE(rsapubtmpl)); |
| 640 |
if (rc != CKR_OK) { |
| 641 |
res = -1; |
| 642 |
} else { |
| 643 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 644 |
funcs->C_FindObjectsFinal(session); |
| 645 |
if (rc != CKR_OK || count != 1u) |
| 646 |
res = -1; |
| 647 |
else |
| 648 |
keys.rsapub = handle; |
| 649 |
} |
| 650 |
// Find RSA privkey |
| 651 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)rsaprivtmpl, |
| 652 |
ARRAY_SIZE(rsaprivtmpl)); |
| 653 |
if (rc != CKR_OK) { |
| 654 |
res = -1; |
| 655 |
} else { |
| 656 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 657 |
funcs->C_FindObjectsFinal(session); |
| 658 |
if (rc != CKR_OK || count != 1u) |
| 659 |
res = -1; |
| 660 |
else |
| 661 |
keys.rsapriv = handle; |
| 662 |
} |
| 663 |
// Find EC pubkey |
| 664 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)ecpubtmpl, |
| 665 |
ARRAY_SIZE(ecpubtmpl)); |
| 666 |
if (rc != CKR_OK) { |
| 667 |
res = -1; |
| 668 |
} else { |
| 669 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 670 |
funcs->C_FindObjectsFinal(session); |
| 671 |
if (rc != CKR_OK || count != 1u) |
| 672 |
res = -1; |
| 673 |
else |
| 674 |
keys.ecpub = handle; |
| 675 |
} |
| 676 |
// Find EC privkey |
| 677 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)ecprivtmpl, |
| 678 |
ARRAY_SIZE(ecprivtmpl)); |
| 679 |
if (rc != CKR_OK) { |
| 680 |
res = -1; |
| 681 |
} else { |
| 682 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 683 |
funcs->C_FindObjectsFinal(session); |
| 684 |
if (rc != CKR_OK || count != 1u) |
| 685 |
res = -1; |
| 686 |
else |
| 687 |
keys.ecpriv = handle; |
| 688 |
} |
| 689 |
// Find AES pubkey |
| 690 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)aestmpl, |
| 691 |
ARRAY_SIZE(aestmpl)); |
| 692 |
if (rc != CKR_OK) { |
| 693 |
res = -1; |
| 694 |
} else { |
| 695 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 696 |
funcs->C_FindObjectsFinal(session); |
| 697 |
if (rc != CKR_OK || count != 1u) |
| 698 |
res = -1; |
| 699 |
else |
| 700 |
keys.aes = handle; |
| 701 |
} |
| 702 |
// Find DH pubkey A |
| 703 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)dhApubtmpl, |
| 704 |
ARRAY_SIZE(dhApubtmpl)); |
| 705 |
if (rc != CKR_OK) { |
| 706 |
res = -1; |
| 707 |
} else { |
| 708 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 709 |
funcs->C_FindObjectsFinal(session); |
| 710 |
if (rc != CKR_OK || count != 1u) |
| 711 |
res = -1; |
| 712 |
else |
| 713 |
keys.dhpubA = handle; |
| 714 |
} |
| 715 |
// Find DH privkey A |
| 716 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)dhAprivtmpl, |
| 717 |
ARRAY_SIZE(dhAprivtmpl)); |
| 718 |
if (rc != CKR_OK) { |
| 719 |
res = -1; |
| 720 |
} else { |
| 721 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 722 |
funcs->C_FindObjectsFinal(session); |
| 723 |
if (rc != CKR_OK || count != 1u) |
| 724 |
res = -1; |
| 725 |
else |
| 726 |
keys.dhprivA = handle; |
| 727 |
} |
| 728 |
// Find DH pubkey B |
| 729 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)dhBpubtmpl, |
| 730 |
ARRAY_SIZE(dhBpubtmpl)); |
| 731 |
if (rc != CKR_OK) { |
| 732 |
res = -1; |
| 733 |
} else { |
| 734 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 735 |
funcs->C_FindObjectsFinal(session); |
| 736 |
if (rc != CKR_OK || count != 1u) |
| 737 |
res = -1; |
| 738 |
else |
| 739 |
keys.dhpubB = handle; |
| 740 |
} |
| 741 |
// Find DH privkey B |
| 742 |
rc = funcs->C_FindObjectsInit(session, (CK_ATTRIBUTE *)dhBprivtmpl, |
| 743 |
ARRAY_SIZE(dhBprivtmpl)); |
| 744 |
if (rc != CKR_OK) { |
| 745 |
res = -1; |
| 746 |
} else { |
| 747 |
rc = funcs->C_FindObjects(session, &handle, 1, &count); |
| 748 |
funcs->C_FindObjectsFinal(session); |
| 749 |
if (rc != CKR_OK || count != 1u) |
| 750 |
res = -1; |
| 751 |
else |
| 752 |
keys.dhprivB = handle; |
| 753 |
} |
| 754 |
return res; |
| 755 |
} |
| 756 |
|
| 757 |
static void deleteKeys(void) |
| 758 |
{ |
| 759 |
funcs->C_DestroyObject(session, keys.rsapub); |
| 760 |
funcs->C_DestroyObject(session, keys.rsapriv); |
| 761 |
funcs->C_DestroyObject(session, keys.ecpub); |
| 762 |
funcs->C_DestroyObject(session, keys.ecpriv); |
| 763 |
funcs->C_DestroyObject(session, keys.aes); |
| 764 |
funcs->C_DestroyObject(session, keys.dhpubA); |
| 765 |
funcs->C_DestroyObject(session, keys.dhprivA); |
| 766 |
funcs->C_DestroyObject(session, keys.dhpubB); |
| 767 |
funcs->C_DestroyObject(session, keys.dhprivB); |
| 768 |
} |
| 769 |
|
| 770 |
static void dumpKeyInfo(void) |
| 771 |
{ |
| 772 |
#define KEY_TO_STR(X) ((X) == CK_INVALID_HANDLE ? "UNAVAILABLE" : "ok") |
| 773 |
fprintf(stderr, "RSA public key: %s\n", KEY_TO_STR(keys.rsapub)); |
| 774 |
fprintf(stderr, "RSA private key: %s\n", KEY_TO_STR(keys.rsapriv)); |
| 775 |
fprintf(stderr, "EC public key: %s\n", KEY_TO_STR(keys.ecpub)); |
| 776 |
fprintf(stderr, "EC private key: %s\n", KEY_TO_STR(keys.ecpriv)); |
| 777 |
fprintf(stderr, "AES key: %s\n", KEY_TO_STR(keys.aes)); |
| 778 |
fprintf(stderr, "DH public key A: %s\n", KEY_TO_STR(keys.dhpubA)); |
| 779 |
fprintf(stderr, "DH private key A: %s\n", KEY_TO_STR(keys.dhprivA)); |
| 780 |
fprintf(stderr, "DH public key B: %s\n", KEY_TO_STR(keys.dhpubB)); |
| 781 |
fprintf(stderr, "DH private key B: %s\n", KEY_TO_STR(keys.dhprivB)); |
| 782 |
#undef KEY_TO_STR |
| 783 |
} |
| 784 |
|
| 785 |
static inline void initializeKeys(void) |
| 786 |
{ |
| 787 |
keys.rsapub = keys.rsapriv = |
| 788 |
keys.ecpub = keys.ecpriv = |
| 789 |
keys.aes = |
| 790 |
keys.dhpubA = keys.dhprivA = |
| 791 |
keys.dhpubB = keys.dhprivB = CK_INVALID_HANDLE; |
| 792 |
} |
| 793 |
|
| 794 |
static int runTests(CK_SLOT_ID slot, uint32_t tests, uint32_t expfail) |
| 795 |
{ |
| 796 |
int res = 0, tmpres, shouldfail; |
| 797 |
|
| 798 |
tmpres = 0; |
| 799 |
if (tests & (1u << TEST_RSA)) { |
| 800 |
shouldfail = expfail & (1u << TEST_RSA); |
| 801 |
if (keys.rsapub == CK_INVALID_HANDLE || |
| 802 |
keys.rsapriv == CK_INVALID_HANDLE) { |
| 803 |
fprintf(stderr, "WARN: RSA test requested, but RSA keys unavailable!\n"); |
| 804 |
tmpres = -1; |
| 805 |
} else { |
| 806 |
tmpres = runRSATest(); |
| 807 |
} |
| 808 |
if (tmpres && shouldfail == 0) { |
| 809 |
fprintf(stderr, "ERROR: RSA test failed unexpectedly!\n"); |
| 810 |
res = -1; |
| 811 |
} else if (tmpres == 0 && shouldfail) { |
| 812 |
fprintf(stderr, "ERROR: RSA test did not fail as expected!\n"); |
| 813 |
res = -1; |
| 814 |
} |
| 815 |
} |
| 816 |
tmpres = 0; |
| 817 |
if (tests & (1u << TEST_EC)) { |
| 818 |
shouldfail = expfail & (1u << TEST_EC); |
| 819 |
if (keys.ecpub == CK_INVALID_HANDLE || |
| 820 |
keys.ecpriv == CK_INVALID_HANDLE) { |
| 821 |
fprintf(stderr, "WARN: EC test requested, but EC keys unavailable!\n"); |
| 822 |
tmpres = -1; |
| 823 |
} else { |
| 824 |
tmpres = runECTest(); |
| 825 |
} |
| 826 |
if (tmpres && shouldfail == 0) { |
| 827 |
fprintf(stderr, "ERROR: EC test failed unexpectedly!\n"); |
| 828 |
res = -1; |
| 829 |
} else if (tmpres == 0 && shouldfail) { |
| 830 |
fprintf(stderr, "ERROR: EC test did not fail as expected!\n"); |
| 831 |
res = -1; |
| 832 |
} |
| 833 |
} |
| 834 |
tmpres = 0; |
| 835 |
if (tests & (1u << TEST_AESWRAP)) { |
| 836 |
shouldfail = expfail & (1u << TEST_AESWRAP); |
| 837 |
if (keys.aes == CK_INVALID_HANDLE) { |
| 838 |
fprintf(stderr, "WARN: AES test requested, but AES key unavailable!\n"); |
| 839 |
tmpres = -1; |
| 840 |
} else { |
| 841 |
tmpres = runAESWrapTest(slot); |
| 842 |
} |
| 843 |
if (tmpres && shouldfail == 0) { |
| 844 |
fprintf(stderr, "ERROR: AES Wrap test failed unexpectedly!\n"); |
| 845 |
res = -1; |
| 846 |
} else if (tmpres == 0 && shouldfail) { |
| 847 |
fprintf(stderr, "ERROR: AES Wrap test did not fail as expected!\n"); |
| 848 |
res = -1; |
| 849 |
} |
| 850 |
} |
| 851 |
if (tests & (1u << TEST_DIGEST)) { |
| 852 |
shouldfail = expfail & (1u << TEST_DIGEST); |
| 853 |
tmpres = runDIGESTTest(); |
| 854 |
if (tmpres && shouldfail == 0) { |
| 855 |
fprintf(stderr, "ERROR: Digest test failed unexpectedly!\n"); |
| 856 |
res = -1; |
| 857 |
} else if (tmpres == 0 && shouldfail) { |
| 858 |
fprintf(stderr, "ERROR: Digest test did not fail as expected!\n"); |
| 859 |
res = -1; |
| 860 |
} |
| 861 |
} |
| 862 |
tmpres = 0; |
| 863 |
if (tests & (1u << TEST_DH)) { |
| 864 |
shouldfail = expfail & (1u << TEST_DH); |
| 865 |
if (keys.dhpubA == CK_INVALID_HANDLE || |
| 866 |
keys.dhprivA == CK_INVALID_HANDLE || |
| 867 |
keys.dhpubB == CK_INVALID_HANDLE || |
| 868 |
keys.dhprivB == CK_INVALID_HANDLE) { |
| 869 |
fprintf(stderr, "WARN: DH test requested, but DH key unavailable!\n"); |
| 870 |
tmpres = -1; |
| 871 |
} else { |
| 872 |
tmpres = runDHTest(); |
| 873 |
} |
| 874 |
if (tmpres && shouldfail == 0) { |
| 875 |
fprintf(stderr, "ERROR: DH test failed unexpectedly!\n"); |
| 876 |
res = -1; |
| 877 |
} else if (tmpres == 0 && shouldfail) { |
| 878 |
fprintf(stderr, "ERROR: DH test did not fail as expected!\n"); |
| 879 |
res = -1; |
| 880 |
} |
| 881 |
} |
| 882 |
return res; |
| 883 |
} |
| 884 |
|
| 885 |
static void usage(char *prgname) |
| 886 |
{ |
| 887 |
printf("USAGE: %s -s|--slot <num> [-g|--generate] [-t|--tests] [-d|--delete] [-k|--token] [-r|--restrict <tests>] [-f|--fail <tests>]\n", |
| 888 |
prgname); |
| 889 |
printf("where:\n"); |
| 890 |
printf("\t-s or --slot specifies the PKCS #11 slot to use\n"); |
| 891 |
printf("\t-g or --generate specifies to generate keys for testing\n"); |
| 892 |
printf("\t-t or --tests specifies to run tests\n"); |
| 893 |
printf("\t-d or --delete specifies to delete the keys at the end\n"); |
| 894 |
printf("\t-k or --token specifies to use token keys (only useful with key generation)\n"); |
| 895 |
printf("\t-r or --restrict specifies which tests to run\n"); |
| 896 |
printf("\t-f or --fail specifies which tests are expected to fail\n"); |
| 897 |
printf("\nvalid tests are:\n"); |
| 898 |
printf("RSA - Encrypt/Decrypt test with 4k RSA key and CKM_RSA_PKCS\n"); |
| 899 |
printf("EC - CKM_ECDSA sign/verify with secp384r1 key\n"); |
| 900 |
printf("AESWRAP - CKM_AES_CBC_PAD key wrapping with 256bit key\n"); |
| 901 |
printf("DIGEST - CKM_SHA512\n"); |
| 902 |
printf("DH - CKM_DH_PKCS_DERIVE with 128 byte prime\n"); |
| 903 |
printf("<tests> is a CSV of the keys above.\n"); |
| 904 |
} |
| 905 |
|
| 906 |
static int getslotid(CK_SLOT_ID *slotid, char *arg) |
| 907 |
{ |
| 908 |
char *endptr; |
| 909 |
unsigned long res; |
| 910 |
|
| 911 |
errno = 0; |
| 912 |
res = strtoul(arg, &endptr, 0); |
| 913 |
if (*endptr || endptr == arg || (res == ULONG_MAX && errno == ERANGE)) { |
| 914 |
fprintf(stderr, "Failed to parse slot id %s\n", arg); |
| 915 |
return -1; |
| 916 |
} |
| 917 |
*slotid = res; |
| 918 |
return 0; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
static int get_user_pin(CK_BYTE * dest) |
| 923 |
{ |
| 924 |
char *val; |
| 925 |
|
| 926 |
val = getenv(PKCS11_USER_PIN_ENV_VAR); |
| 927 |
if (val == NULL) { |
| 928 |
fprintf(stderr, "The environment variable %s must be set " |
| 929 |
"before this testcase is run.\n", PKCS11_USER_PIN_ENV_VAR); |
| 930 |
return -1; |
| 931 |
} |
| 932 |
|
| 933 |
if ((strlen(val) + 1) > PKCS11_MAX_PIN_LEN) { |
| 934 |
fprintf(stderr, "The environment variable %s must hold a " |
| 935 |
"value less than %d chars in length.\n", |
| 936 |
PKCS11_SO_PIN_ENV_VAR, (int) PKCS11_MAX_PIN_LEN); |
| 937 |
return -1; |
| 938 |
} |
| 939 |
|
| 940 |
memcpy(dest, val, strlen(val) + 1); |
| 941 |
|
| 942 |
return 0; |
| 943 |
} |
| 944 |
|
| 945 |
static int parsecsv(uint32_t *mask, const char *carg) |
| 946 |
{ |
| 947 |
char *arg = strdup(carg); |
| 948 |
char *s, *tok; |
| 949 |
int res = 0; |
| 950 |
|
| 951 |
if (!arg) |
| 952 |
return -1; |
| 953 |
s = arg; |
| 954 |
while ((tok = strtok(s, ",")) != NULL) { |
| 955 |
if (strcmp(tok, "RSA") == 0) |
| 956 |
*mask |= (1u << TEST_RSA); |
| 957 |
else if (strcmp(tok, "EC") == 0) |
| 958 |
*mask |= (1u << TEST_EC); |
| 959 |
else if (strcmp(tok, "AESWRAP") == 0) |
| 960 |
*mask |= (1u << TEST_AESWRAP); |
| 961 |
else if (strcmp(tok, "DIGEST") == 0) |
| 962 |
*mask |= (1u << TEST_DIGEST); |
| 963 |
else if (strcmp(tok, "DH") == 0) |
| 964 |
*mask |= (1u << TEST_DH); |
| 965 |
else |
| 966 |
res = -1; |
| 967 |
s = NULL; |
| 968 |
} |
| 969 |
free(arg); |
| 970 |
return res; |
| 971 |
} |
| 972 |
|
| 973 |
static int parseArgs(int argc, char **argv, CK_SLOT_ID *slot, |
| 974 |
int *keygen, int *test, int *delete, |
| 975 |
uint32_t *tests, uint32_t *fail) |
| 976 |
{ |
| 977 |
static struct option long_options[] = |
| 978 |
{ |
| 979 |
{"slot", required_argument, 0, 's'}, |
| 980 |
{"generate", no_argument, 0, 'g'}, |
| 981 |
{"tests", no_argument, 0, 't'}, |
| 982 |
{"delete", no_argument, 0, 'd'}, |
| 983 |
{"restrict", required_argument, 0, 'r'}, |
| 984 |
{"fail", required_argument, 0, 'f'}, |
| 985 |
{"token", no_argument, 0, 'k'}, |
| 986 |
{"help", no_argument, 0, 'h'}, |
| 987 |
{0, 0, 0, 0 } |
| 988 |
}; |
| 989 |
int c, slotunspecified = -1; |
| 990 |
|
| 991 |
*keygen = *test = *delete = 0; |
| 992 |
*tests = ~0u; |
| 993 |
*fail = 0; |
| 994 |
while (1) { |
| 995 |
c = getopt_long(argc, argv, "s:gtdr:f:kh", long_options, 0); |
| 996 |
if (c == -1) |
| 997 |
break; |
| 998 |
switch (c) { |
| 999 |
case 's': |
| 1000 |
if (getslotid(slot, optarg)) |
| 1001 |
return -1; |
| 1002 |
slotunspecified = 0; |
| 1003 |
break; |
| 1004 |
case 'g': |
| 1005 |
*keygen = 1; |
| 1006 |
break; |
| 1007 |
case 't': |
| 1008 |
*test = 1; |
| 1009 |
break; |
| 1010 |
case 'd': |
| 1011 |
*delete = 1; |
| 1012 |
break; |
| 1013 |
case 'r': |
| 1014 |
*tests = 0; |
| 1015 |
if (parsecsv(tests, optarg)) |
| 1016 |
return -1; |
| 1017 |
break; |
| 1018 |
case 'f': |
| 1019 |
if (parsecsv(fail, optarg)) |
| 1020 |
return -1; |
| 1021 |
break; |
| 1022 |
case 'k': |
| 1023 |
tokenkeys = CK_TRUE; |
| 1024 |
break; |
| 1025 |
case 'h': |
| 1026 |
usage(argv[0]); |
| 1027 |
return 1; |
| 1028 |
default: |
| 1029 |
usage(argv[0]); |
| 1030 |
return -1; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
if (slotunspecified) |
| 1034 |
usage(argv[0]); |
| 1035 |
return slotunspecified; |
| 1036 |
} |
| 1037 |
|
| 1038 |
int main(int argc, char **argv) |
| 1039 |
{ |
| 1040 |
|
| 1041 |
CK_BYTE userpin[PKCS11_MAX_PIN_LEN]; |
| 1042 |
int keygen, test, delete; |
| 1043 |
uint32_t tests, fail; |
| 1044 |
CK_SLOT_ID slot = 0; |
| 1045 |
int res = 0; |
| 1046 |
|
| 1047 |
initializeKeys(); |
| 1048 |
if (parseArgs(argc, argv, &slot, &keygen, &test, &delete, &tests, &fail)) |
| 1049 |
return 1; |
| 1050 |
if (get_user_pin(userpin)) |
| 1051 |
return 2; |
| 1052 |
if (doLoadLib()) |
| 1053 |
return 3; |
| 1054 |
if (doInitLib()) |
| 1055 |
return 4; |
| 1056 |
if (doOpenSession(slot)) |
| 1057 |
return 5; |
| 1058 |
if (doLoginUser(userpin)) |
| 1059 |
return 6; |
| 1060 |
if (keygen && generateKeys()) |
| 1061 |
fprintf(stderr, "WARN: Could not generate all keys\n"); |
| 1062 |
if (loadKeys()) { |
| 1063 |
fprintf(stderr, "WARN: Could not load all keys\n"); |
| 1064 |
dumpKeyInfo(); |
| 1065 |
} |
| 1066 |
if (test && runTests(slot, tests, fail)) |
| 1067 |
res = 7; |
| 1068 |
if (delete) |
| 1069 |
deleteKeys(); |
| 1070 |
return res; |
| 1071 |
} |