| 1 |
/* |
| 2 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 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 |
softhsm2-util-ossl.cpp |
| 29 |
|
| 30 |
Code specific for OpenSSL |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#define UTIL_OSSL |
| 35 |
#include "softhsm2-util.h" |
| 36 |
#include "softhsm2-util-ossl.h" |
| 37 |
#include "OSSLComp.h" |
| 38 |
|
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#include <iostream> |
| 43 |
#include <fstream> |
| 44 |
|
| 45 |
#include <openssl/pem.h> |
| 46 |
#include <openssl/evp.h> |
| 47 |
#include <openssl/err.h> |
| 48 |
#include <openssl/pkcs12.h> |
| 49 |
#include <openssl/objects.h> |
| 50 |
|
| 51 |
// Init OpenSSL |
| 52 |
void crypto_init() |
| 53 |
{ |
| 54 |
// We do not need to do this one |
| 55 |
// OpenSSL_add_all_algorithms(); |
| 56 |
#ifdef WITH_FIPS |
| 57 |
// The PKCS#11 library might be using a FIPS capable OpenSSL |
| 58 |
if (FIPS_mode()) |
| 59 |
return; |
| 60 |
if (!FIPS_mode_set(1)) |
| 61 |
{ |
| 62 |
fprintf(stderr, "ERROR: can't enter into FIPS mode.\n"); |
| 63 |
exit(0); |
| 64 |
} |
| 65 |
#endif |
| 66 |
} |
| 67 |
|
| 68 |
// Final OpenSSL |
| 69 |
void crypto_final() |
| 70 |
{ |
| 71 |
// EVP_cleanup(); |
| 72 |
CRYPTO_cleanup_all_ex_data(); |
| 73 |
} |
| 74 |
|
| 75 |
int crypto_import_aes_key |
| 76 |
( |
| 77 |
CK_SESSION_HANDLE hSession, |
| 78 |
char* filePath, |
| 79 |
char* label, |
| 80 |
char* objID, |
| 81 |
size_t objIDLen |
| 82 |
) |
| 83 |
{ |
| 84 |
const size_t cMaxAesKeySize = 1024 + 1; // including null-character |
| 85 |
char aesKeyValue[cMaxAesKeySize]; |
| 86 |
FILE* fp = fopen(filePath, "rb"); |
| 87 |
if (fp == NULL) |
| 88 |
{ |
| 89 |
fprintf(stderr, "ERROR: Could not open the secret key file.\n"); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
if (fgets(aesKeyValue, cMaxAesKeySize, fp) == NULL) |
| 93 |
{ |
| 94 |
fprintf(stderr, "ERROR: Could not read the secret key file.\n"); |
| 95 |
fclose(fp); |
| 96 |
return 1; |
| 97 |
} |
| 98 |
fclose(fp); |
| 99 |
|
| 100 |
CK_BBOOL ckTrue = CK_TRUE; |
| 101 |
CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; |
| 102 |
CK_KEY_TYPE keyType = CKK_AES; |
| 103 |
CK_ATTRIBUTE keyTemplate[] = { |
| 104 |
{ CKA_CLASS, &keyClass, sizeof(keyClass) }, |
| 105 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 106 |
{ CKA_LABEL, label, strlen(label) }, |
| 107 |
{ CKA_ID, objID, objIDLen }, |
| 108 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 109 |
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) }, |
| 110 |
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) }, |
| 111 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 112 |
{ CKA_VALUE, &aesKeyValue, strlen(aesKeyValue) } |
| 113 |
}; |
| 114 |
|
| 115 |
CK_OBJECT_HANDLE hKey; |
| 116 |
CK_RV rv = p11->C_CreateObject(hSession, keyTemplate, 9, &hKey); |
| 117 |
if (rv != CKR_OK) |
| 118 |
{ |
| 119 |
fprintf(stderr, "ERROR: Could not save the secret key in the token. " |
| 120 |
"Maybe the algorithm is not supported.\n"); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
// Import a key pair from given path |
| 128 |
int crypto_import_key_pair |
| 129 |
( |
| 130 |
CK_SESSION_HANDLE hSession, |
| 131 |
char* filePath, |
| 132 |
char* filePIN, |
| 133 |
char* label, |
| 134 |
char* objID, |
| 135 |
size_t objIDLen, |
| 136 |
int noPublicKey |
| 137 |
) |
| 138 |
{ |
| 139 |
EVP_PKEY* pkey = crypto_read_file(filePath, filePIN); |
| 140 |
if (pkey == NULL) |
| 141 |
{ |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
RSA* rsa = NULL; |
| 146 |
DSA* dsa = NULL; |
| 147 |
#ifdef WITH_ECC |
| 148 |
EC_KEY* ecdsa = NULL; |
| 149 |
#endif |
| 150 |
#ifdef WITH_EDDSA |
| 151 |
EVP_PKEY* eddsa = NULL; |
| 152 |
#endif |
| 153 |
|
| 154 |
switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) |
| 155 |
{ |
| 156 |
case EVP_PKEY_RSA: |
| 157 |
rsa = EVP_PKEY_get1_RSA(pkey); |
| 158 |
break; |
| 159 |
case EVP_PKEY_DSA: |
| 160 |
dsa = EVP_PKEY_get1_DSA(pkey); |
| 161 |
break; |
| 162 |
#ifdef WITH_ECC |
| 163 |
case EVP_PKEY_EC: |
| 164 |
ecdsa = EVP_PKEY_get1_EC_KEY(pkey); |
| 165 |
break; |
| 166 |
#endif |
| 167 |
#ifdef WITH_EDDSA |
| 168 |
case NID_X25519: |
| 169 |
case NID_ED25519: |
| 170 |
case NID_X448: |
| 171 |
case NID_ED448: |
| 172 |
EVP_PKEY_up_ref(pkey); |
| 173 |
eddsa = pkey; |
| 174 |
break; |
| 175 |
#endif |
| 176 |
default: |
| 177 |
fprintf(stderr, "ERROR: Cannot handle this algorithm.\n"); |
| 178 |
EVP_PKEY_free(pkey); |
| 179 |
return 1; |
| 180 |
break; |
| 181 |
} |
| 182 |
EVP_PKEY_free(pkey); |
| 183 |
|
| 184 |
int result = 0; |
| 185 |
|
| 186 |
if (rsa) |
| 187 |
{ |
| 188 |
result = crypto_save_rsa(hSession, label, objID, objIDLen, noPublicKey, rsa); |
| 189 |
RSA_free(rsa); |
| 190 |
} |
| 191 |
else if (dsa) |
| 192 |
{ |
| 193 |
result = crypto_save_dsa(hSession, label, objID, objIDLen, noPublicKey, dsa); |
| 194 |
DSA_free(dsa); |
| 195 |
} |
| 196 |
#ifdef WITH_ECC |
| 197 |
else if (ecdsa) |
| 198 |
{ |
| 199 |
result = crypto_save_ecdsa(hSession, label, objID, objIDLen, noPublicKey, ecdsa); |
| 200 |
EC_KEY_free(ecdsa); |
| 201 |
} |
| 202 |
#endif |
| 203 |
#ifdef WITH_EDDSA |
| 204 |
else if (eddsa) |
| 205 |
{ |
| 206 |
result = crypto_save_eddsa(hSession, label, objID, objIDLen, noPublicKey, eddsa); |
| 207 |
EVP_PKEY_free(eddsa); |
| 208 |
} |
| 209 |
#endif |
| 210 |
else |
| 211 |
{ |
| 212 |
fprintf(stderr, "ERROR: Could not get the key material.\n"); |
| 213 |
result = 1; |
| 214 |
} |
| 215 |
|
| 216 |
return result; |
| 217 |
} |
| 218 |
|
| 219 |
// Read the key from file |
| 220 |
EVP_PKEY* crypto_read_file(char* filePath, char* filePIN) |
| 221 |
{ |
| 222 |
BIO* in = NULL; |
| 223 |
PKCS8_PRIV_KEY_INFO* p8inf = NULL; |
| 224 |
EVP_PKEY* pkey = NULL; |
| 225 |
X509_SIG* p8 = NULL; |
| 226 |
|
| 227 |
if (!(in = BIO_new_file(filePath, "rb"))) |
| 228 |
{ |
| 229 |
fprintf(stderr, "ERROR: Could open the PKCS#8 file: %s\n", filePath); |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
|
| 233 |
// The PKCS#8 file is encrypted |
| 234 |
if (filePIN) |
| 235 |
{ |
| 236 |
p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); |
| 237 |
BIO_free(in); |
| 238 |
|
| 239 |
if (!p8) |
| 240 |
{ |
| 241 |
fprintf(stderr, "ERROR: Could not read the PKCS#8 file. " |
| 242 |
"Maybe the file is not encrypted.\n"); |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
|
| 246 |
p8inf = PKCS8_decrypt(p8, filePIN, strlen(filePIN)); |
| 247 |
X509_SIG_free(p8); |
| 248 |
|
| 249 |
if (!p8inf) |
| 250 |
{ |
| 251 |
fprintf(stderr, "ERROR: Could not decrypt the PKCS#8 file. " |
| 252 |
"Maybe wrong PIN to file (--file-pin <PIN>)\n"); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL); |
| 259 |
BIO_free(in); |
| 260 |
|
| 261 |
if (!p8inf) |
| 262 |
{ |
| 263 |
fprintf(stderr, "ERROR: Could not read the PKCS#8 file. " |
| 264 |
"Maybe it is encypted (--file-pin <PIN>)\n"); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// Convert the PKCS#8 to OpenSSL |
| 270 |
pkey = EVP_PKCS82PKEY(p8inf); |
| 271 |
PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 272 |
if (!pkey) |
| 273 |
{ |
| 274 |
fprintf(stderr, "ERROR: Could not convert the key.\n"); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
return pkey; |
| 279 |
} |
| 280 |
|
| 281 |
// Save the key data in PKCS#11 |
| 282 |
int crypto_save_rsa |
| 283 |
( |
| 284 |
CK_SESSION_HANDLE hSession, |
| 285 |
char* label, |
| 286 |
char* objID, |
| 287 |
size_t objIDLen, |
| 288 |
int noPublicKey, |
| 289 |
RSA* rsa |
| 290 |
) |
| 291 |
{ |
| 292 |
rsa_key_material_t* keyMat = crypto_malloc_rsa(rsa); |
| 293 |
if (!keyMat) |
| 294 |
{ |
| 295 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 296 |
return 1; |
| 297 |
} |
| 298 |
|
| 299 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 300 |
CK_KEY_TYPE keyType = CKK_RSA; |
| 301 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 302 |
if (noPublicKey) |
| 303 |
{ |
| 304 |
ckToken = CK_FALSE; |
| 305 |
} |
| 306 |
CK_ATTRIBUTE pubTemplate[] = { |
| 307 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 308 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 309 |
{ CKA_LABEL, label, strlen(label) }, |
| 310 |
{ CKA_ID, objID, objIDLen }, |
| 311 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 312 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 313 |
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) }, |
| 314 |
{ CKA_WRAP, &ckTrue, sizeof(ckTrue) }, |
| 315 |
{ CKA_PUBLIC_EXPONENT, keyMat->bigE, keyMat->sizeE }, |
| 316 |
{ CKA_MODULUS, keyMat->bigN, keyMat->sizeN } |
| 317 |
}; |
| 318 |
CK_ATTRIBUTE privTemplate[] = { |
| 319 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 320 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 321 |
{ CKA_LABEL, label, strlen(label) }, |
| 322 |
{ CKA_ID, objID, objIDLen }, |
| 323 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 324 |
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) }, |
| 325 |
{ CKA_UNWRAP, &ckTrue, sizeof(ckTrue) }, |
| 326 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 327 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 328 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 329 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 330 |
{ CKA_PUBLIC_EXPONENT, keyMat->bigE, keyMat->sizeE }, |
| 331 |
{ CKA_MODULUS, keyMat->bigN, keyMat->sizeN }, |
| 332 |
{ CKA_PRIVATE_EXPONENT, keyMat->bigD, keyMat->sizeD }, |
| 333 |
{ CKA_PRIME_1, keyMat->bigP, keyMat->sizeP }, |
| 334 |
{ CKA_PRIME_2, keyMat->bigQ, keyMat->sizeQ }, |
| 335 |
{ CKA_EXPONENT_1, keyMat->bigDMP1, keyMat->sizeDMP1 }, |
| 336 |
{ CKA_EXPONENT_2, keyMat->bigDMQ1, keyMat->sizeDMQ1 }, |
| 337 |
{ CKA_COEFFICIENT, keyMat->bigIQMP, keyMat->sizeIQMP } |
| 338 |
}; |
| 339 |
|
| 340 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 341 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 19, &hKey1); |
| 342 |
if (rv != CKR_OK) |
| 343 |
{ |
| 344 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 345 |
"Maybe the algorithm is not supported.\n"); |
| 346 |
crypto_free_rsa(keyMat); |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
rv = p11->C_CreateObject(hSession, pubTemplate, 10, &hKey2); |
| 351 |
crypto_free_rsa(keyMat); |
| 352 |
|
| 353 |
if (rv != CKR_OK) |
| 354 |
{ |
| 355 |
p11->C_DestroyObject(hSession, hKey1); |
| 356 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 357 |
return 1; |
| 358 |
} |
| 359 |
|
| 360 |
printf("The key pair has been imported.\n"); |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
// Convert the OpenSSL key to binary |
| 366 |
rsa_key_material_t* crypto_malloc_rsa(RSA* rsa) |
| 367 |
{ |
| 368 |
if (rsa == NULL) |
| 369 |
{ |
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
rsa_key_material_t* keyMat = (rsa_key_material_t*)malloc(sizeof(rsa_key_material_t)); |
| 374 |
if (keyMat == NULL) |
| 375 |
{ |
| 376 |
return NULL; |
| 377 |
} |
| 378 |
|
| 379 |
const BIGNUM* bn_e = NULL; |
| 380 |
const BIGNUM* bn_n = NULL; |
| 381 |
const BIGNUM* bn_d = NULL; |
| 382 |
const BIGNUM* bn_p = NULL; |
| 383 |
const BIGNUM* bn_q = NULL; |
| 384 |
const BIGNUM* bn_dmp1 = NULL; |
| 385 |
const BIGNUM* bn_dmq1 = NULL; |
| 386 |
const BIGNUM* bn_iqmp = NULL; |
| 387 |
RSA_get0_factors(rsa, &bn_p, &bn_q); |
| 388 |
RSA_get0_crt_params(rsa, &bn_dmp1, &bn_dmq1, &bn_iqmp); |
| 389 |
RSA_get0_key(rsa, &bn_n, &bn_e, &bn_d); |
| 390 |
|
| 391 |
keyMat->sizeE = BN_num_bytes(bn_e); |
| 392 |
keyMat->sizeN = BN_num_bytes(bn_n); |
| 393 |
keyMat->sizeD = BN_num_bytes(bn_d); |
| 394 |
keyMat->sizeP = BN_num_bytes(bn_p); |
| 395 |
keyMat->sizeQ = BN_num_bytes(bn_q); |
| 396 |
keyMat->sizeDMP1 = BN_num_bytes(bn_dmp1); |
| 397 |
keyMat->sizeDMQ1 = BN_num_bytes(bn_dmq1); |
| 398 |
keyMat->sizeIQMP = BN_num_bytes(bn_iqmp); |
| 399 |
|
| 400 |
keyMat->bigE = (CK_VOID_PTR)malloc(keyMat->sizeE); |
| 401 |
keyMat->bigN = (CK_VOID_PTR)malloc(keyMat->sizeN); |
| 402 |
keyMat->bigD = (CK_VOID_PTR)malloc(keyMat->sizeD); |
| 403 |
keyMat->bigP = (CK_VOID_PTR)malloc(keyMat->sizeP); |
| 404 |
keyMat->bigQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 405 |
keyMat->bigDMP1 = (CK_VOID_PTR)malloc(keyMat->sizeDMP1); |
| 406 |
keyMat->bigDMQ1 = (CK_VOID_PTR)malloc(keyMat->sizeDMQ1); |
| 407 |
keyMat->bigIQMP = (CK_VOID_PTR)malloc(keyMat->sizeIQMP); |
| 408 |
|
| 409 |
if |
| 410 |
( |
| 411 |
!keyMat->bigE || |
| 412 |
!keyMat->bigN || |
| 413 |
!keyMat->bigD || |
| 414 |
!keyMat->bigP || |
| 415 |
!keyMat->bigQ || |
| 416 |
!keyMat->bigDMP1 || |
| 417 |
!keyMat->bigDMQ1 || |
| 418 |
!keyMat->bigIQMP |
| 419 |
) |
| 420 |
{ |
| 421 |
crypto_free_rsa(keyMat); |
| 422 |
return NULL; |
| 423 |
} |
| 424 |
|
| 425 |
BN_bn2bin(bn_e, (unsigned char*)keyMat->bigE); |
| 426 |
BN_bn2bin(bn_n, (unsigned char*)keyMat->bigN); |
| 427 |
BN_bn2bin(bn_d, (unsigned char*)keyMat->bigD); |
| 428 |
BN_bn2bin(bn_p, (unsigned char*)keyMat->bigP); |
| 429 |
BN_bn2bin(bn_q, (unsigned char*)keyMat->bigQ); |
| 430 |
BN_bn2bin(bn_dmp1, (unsigned char*)keyMat->bigDMP1); |
| 431 |
BN_bn2bin(bn_dmq1, (unsigned char*)keyMat->bigDMQ1); |
| 432 |
BN_bn2bin(bn_iqmp, (unsigned char*)keyMat->bigIQMP); |
| 433 |
|
| 434 |
return keyMat; |
| 435 |
} |
| 436 |
|
| 437 |
// Free the memory of the key |
| 438 |
void crypto_free_rsa(rsa_key_material_t* keyMat) |
| 439 |
{ |
| 440 |
if (keyMat == NULL) return; |
| 441 |
if (keyMat->bigE) free(keyMat->bigE); |
| 442 |
if (keyMat->bigN) free(keyMat->bigN); |
| 443 |
if (keyMat->bigD) free(keyMat->bigD); |
| 444 |
if (keyMat->bigP) free(keyMat->bigP); |
| 445 |
if (keyMat->bigQ) free(keyMat->bigQ); |
| 446 |
if (keyMat->bigDMP1) free(keyMat->bigDMP1); |
| 447 |
if (keyMat->bigDMQ1) free(keyMat->bigDMQ1); |
| 448 |
if (keyMat->bigIQMP) free(keyMat->bigIQMP); |
| 449 |
free(keyMat); |
| 450 |
} |
| 451 |
|
| 452 |
// Save the key data in PKCS#11 |
| 453 |
int crypto_save_dsa |
| 454 |
( |
| 455 |
CK_SESSION_HANDLE hSession, |
| 456 |
char* label, |
| 457 |
char* objID, |
| 458 |
size_t objIDLen, |
| 459 |
int noPublicKey, |
| 460 |
DSA* dsa |
| 461 |
) |
| 462 |
{ |
| 463 |
dsa_key_material_t* keyMat = crypto_malloc_dsa(dsa); |
| 464 |
if (keyMat == NULL) |
| 465 |
{ |
| 466 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 467 |
return 1; |
| 468 |
} |
| 469 |
|
| 470 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 471 |
CK_KEY_TYPE keyType = CKK_DSA; |
| 472 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 473 |
if (noPublicKey) |
| 474 |
{ |
| 475 |
ckToken = CK_FALSE; |
| 476 |
} |
| 477 |
CK_ATTRIBUTE pubTemplate[] = { |
| 478 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 479 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 480 |
{ CKA_LABEL, label, strlen(label) }, |
| 481 |
{ CKA_ID, objID, objIDLen }, |
| 482 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 483 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 484 |
{ CKA_ENCRYPT, &ckFalse, sizeof(ckFalse) }, |
| 485 |
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) }, |
| 486 |
{ CKA_PRIME, keyMat->bigP, keyMat->sizeP }, |
| 487 |
{ CKA_SUBPRIME, keyMat->bigQ, keyMat->sizeQ }, |
| 488 |
{ CKA_BASE, keyMat->bigG, keyMat->sizeG }, |
| 489 |
{ CKA_VALUE, keyMat->bigY, keyMat->sizeY } |
| 490 |
}; |
| 491 |
CK_ATTRIBUTE privTemplate[] = { |
| 492 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 493 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 494 |
{ CKA_LABEL, label, strlen(label) }, |
| 495 |
{ CKA_ID, objID, objIDLen }, |
| 496 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 497 |
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) }, |
| 498 |
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) }, |
| 499 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 500 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 501 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 502 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 503 |
{ CKA_PRIME, keyMat->bigP, keyMat->sizeP }, |
| 504 |
{ CKA_SUBPRIME, keyMat->bigQ, keyMat->sizeQ }, |
| 505 |
{ CKA_BASE, keyMat->bigG, keyMat->sizeG }, |
| 506 |
{ CKA_VALUE, keyMat->bigX, keyMat->sizeX } |
| 507 |
}; |
| 508 |
|
| 509 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 510 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 15, &hKey1); |
| 511 |
if (rv != CKR_OK) |
| 512 |
{ |
| 513 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 514 |
"Maybe the algorithm is not supported.\n"); |
| 515 |
crypto_free_dsa(keyMat); |
| 516 |
return 1; |
| 517 |
} |
| 518 |
|
| 519 |
rv = p11->C_CreateObject(hSession, pubTemplate, 12, &hKey2); |
| 520 |
crypto_free_dsa(keyMat); |
| 521 |
|
| 522 |
if (rv != CKR_OK) |
| 523 |
{ |
| 524 |
p11->C_DestroyObject(hSession, hKey1); |
| 525 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 526 |
return 1; |
| 527 |
} |
| 528 |
|
| 529 |
printf("The key pair has been imported.\n"); |
| 530 |
|
| 531 |
return 0; |
| 532 |
} |
| 533 |
|
| 534 |
// Convert the OpenSSL key to binary |
| 535 |
dsa_key_material_t* crypto_malloc_dsa(DSA* dsa) |
| 536 |
{ |
| 537 |
if (dsa == NULL) |
| 538 |
{ |
| 539 |
return NULL; |
| 540 |
} |
| 541 |
|
| 542 |
dsa_key_material_t* keyMat = (dsa_key_material_t*)malloc(sizeof(dsa_key_material_t)); |
| 543 |
if (keyMat == NULL) |
| 544 |
{ |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
|
| 548 |
const BIGNUM* bn_p = NULL; |
| 549 |
const BIGNUM* bn_q = NULL; |
| 550 |
const BIGNUM* bn_g = NULL; |
| 551 |
const BIGNUM* bn_priv_key = NULL; |
| 552 |
const BIGNUM* bn_pub_key = NULL; |
| 553 |
DSA_get0_pqg(dsa, &bn_p, &bn_q, &bn_g); |
| 554 |
DSA_get0_key(dsa, &bn_pub_key, &bn_priv_key); |
| 555 |
|
| 556 |
keyMat->sizeP = BN_num_bytes(bn_p); |
| 557 |
keyMat->sizeQ = BN_num_bytes(bn_q); |
| 558 |
keyMat->sizeG = BN_num_bytes(bn_g); |
| 559 |
keyMat->sizeX = BN_num_bytes(bn_priv_key); |
| 560 |
keyMat->sizeY = BN_num_bytes(bn_pub_key); |
| 561 |
|
| 562 |
keyMat->bigP = (CK_VOID_PTR)malloc(keyMat->sizeP); |
| 563 |
keyMat->bigQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 564 |
keyMat->bigG = (CK_VOID_PTR)malloc(keyMat->sizeG); |
| 565 |
keyMat->bigX = (CK_VOID_PTR)malloc(keyMat->sizeX); |
| 566 |
keyMat->bigY = (CK_VOID_PTR)malloc(keyMat->sizeY); |
| 567 |
|
| 568 |
if (!keyMat->bigP || !keyMat->bigQ || !keyMat->bigG || !keyMat->bigX || !keyMat->bigY) |
| 569 |
{ |
| 570 |
crypto_free_dsa(keyMat); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
|
| 574 |
BN_bn2bin(bn_p, (unsigned char*)keyMat->bigP); |
| 575 |
BN_bn2bin(bn_q, (unsigned char*)keyMat->bigQ); |
| 576 |
BN_bn2bin(bn_g, (unsigned char*)keyMat->bigG); |
| 577 |
BN_bn2bin(bn_priv_key, (unsigned char*)keyMat->bigX); |
| 578 |
BN_bn2bin(bn_pub_key, (unsigned char*)keyMat->bigY); |
| 579 |
|
| 580 |
return keyMat; |
| 581 |
} |
| 582 |
|
| 583 |
// Free the memory of the key |
| 584 |
void crypto_free_dsa(dsa_key_material_t* keyMat) |
| 585 |
{ |
| 586 |
if (keyMat == NULL) return; |
| 587 |
if (keyMat->bigP) free(keyMat->bigP); |
| 588 |
if (keyMat->bigQ) free(keyMat->bigQ); |
| 589 |
if (keyMat->bigG) free(keyMat->bigG); |
| 590 |
if (keyMat->bigX) free(keyMat->bigX); |
| 591 |
if (keyMat->bigY) free(keyMat->bigY); |
| 592 |
free(keyMat); |
| 593 |
} |
| 594 |
|
| 595 |
#ifdef WITH_ECC |
| 596 |
|
| 597 |
// Save the key data in PKCS#11 |
| 598 |
int crypto_save_ecdsa |
| 599 |
( |
| 600 |
CK_SESSION_HANDLE hSession, |
| 601 |
char* label, |
| 602 |
char* objID, |
| 603 |
size_t objIDLen, |
| 604 |
int noPublicKey, |
| 605 |
EC_KEY* ecdsa |
| 606 |
) |
| 607 |
{ |
| 608 |
ecdsa_key_material_t* keyMat = crypto_malloc_ecdsa(ecdsa); |
| 609 |
if (keyMat == NULL) |
| 610 |
{ |
| 611 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 612 |
return 1; |
| 613 |
} |
| 614 |
|
| 615 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 616 |
CK_KEY_TYPE keyType = CKK_EC; |
| 617 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 618 |
if (noPublicKey) |
| 619 |
{ |
| 620 |
ckToken = CK_FALSE; |
| 621 |
} |
| 622 |
CK_ATTRIBUTE pubTemplate[] = { |
| 623 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 624 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 625 |
{ CKA_LABEL, label, strlen(label) }, |
| 626 |
{ CKA_ID, objID, objIDLen }, |
| 627 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 628 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 629 |
{ CKA_ENCRYPT, &ckFalse, sizeof(ckFalse) }, |
| 630 |
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) }, |
| 631 |
{ CKA_EC_PARAMS, keyMat->derParams, keyMat->sizeParams }, |
| 632 |
{ CKA_EC_POINT, keyMat->derQ, keyMat->sizeQ }, |
| 633 |
}; |
| 634 |
CK_ATTRIBUTE privTemplate[] = { |
| 635 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 636 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 637 |
{ CKA_LABEL, label, strlen(label) }, |
| 638 |
{ CKA_ID, objID, objIDLen }, |
| 639 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 640 |
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) }, |
| 641 |
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) }, |
| 642 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 643 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 644 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 645 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 646 |
{ CKA_EC_PARAMS, keyMat->derParams, keyMat->sizeParams }, |
| 647 |
{ CKA_VALUE, keyMat->bigD, keyMat->sizeD } |
| 648 |
}; |
| 649 |
|
| 650 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 651 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 13, &hKey1); |
| 652 |
if (rv != CKR_OK) |
| 653 |
{ |
| 654 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 655 |
"Maybe the algorithm is not supported.\n"); |
| 656 |
crypto_free_ecdsa(keyMat); |
| 657 |
return 1; |
| 658 |
} |
| 659 |
|
| 660 |
rv = p11->C_CreateObject(hSession, pubTemplate, 10, &hKey2); |
| 661 |
crypto_free_ecdsa(keyMat); |
| 662 |
|
| 663 |
if (rv != CKR_OK) |
| 664 |
{ |
| 665 |
p11->C_DestroyObject(hSession, hKey1); |
| 666 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 667 |
return 1; |
| 668 |
} |
| 669 |
|
| 670 |
printf("The key pair has been imported.\n"); |
| 671 |
|
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
// Convert the OpenSSL key to binary |
| 676 |
ecdsa_key_material_t* crypto_malloc_ecdsa(EC_KEY* ec_key) |
| 677 |
{ |
| 678 |
int result; |
| 679 |
|
| 680 |
if (ec_key == NULL) |
| 681 |
{ |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
|
| 685 |
ecdsa_key_material_t* keyMat = (ecdsa_key_material_t*)malloc(sizeof(ecdsa_key_material_t)); |
| 686 |
if (keyMat == NULL) |
| 687 |
{ |
| 688 |
return NULL; |
| 689 |
} |
| 690 |
|
| 691 |
const BIGNUM *d = EC_KEY_get0_private_key(ec_key); |
| 692 |
const EC_GROUP *group = EC_KEY_get0_group(ec_key); |
| 693 |
const EC_POINT *point = EC_KEY_get0_public_key(ec_key); |
| 694 |
|
| 695 |
keyMat->sizeParams = i2d_ECPKParameters(group, NULL); |
| 696 |
keyMat->sizeD = BN_num_bytes(d); |
| 697 |
|
| 698 |
keyMat->derParams = (CK_VOID_PTR)malloc(keyMat->sizeParams); |
| 699 |
keyMat->bigD = (CK_VOID_PTR)malloc(keyMat->sizeD); |
| 700 |
keyMat->derQ = NULL; |
| 701 |
|
| 702 |
if (!keyMat->derParams || !keyMat->bigD) |
| 703 |
{ |
| 704 |
crypto_free_ecdsa(keyMat); |
| 705 |
return NULL; |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
* i2d functions increment the pointer, so we have to use a |
| 710 |
* sacrificial pointer |
| 711 |
*/ |
| 712 |
unsigned char *derParams = (unsigned char*) keyMat->derParams; |
| 713 |
result = i2d_ECPKParameters(group, &derParams); |
| 714 |
if (result == 0) |
| 715 |
{ |
| 716 |
crypto_free_ecdsa(keyMat); |
| 717 |
return NULL; |
| 718 |
} |
| 719 |
BN_bn2bin(d, (unsigned char*)keyMat->bigD); |
| 720 |
|
| 721 |
size_t point_length = EC_POINT_point2oct(group, |
| 722 |
point, |
| 723 |
POINT_CONVERSION_UNCOMPRESSED, |
| 724 |
NULL, |
| 725 |
0, |
| 726 |
NULL); |
| 727 |
|
| 728 |
// Definite, short |
| 729 |
if (point_length <= 0x7f) |
| 730 |
{ |
| 731 |
keyMat->sizeQ = 2 + point_length; |
| 732 |
keyMat->derQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 733 |
if (!keyMat->derQ) |
| 734 |
{ |
| 735 |
crypto_free_ecdsa(keyMat); |
| 736 |
return NULL; |
| 737 |
} |
| 738 |
|
| 739 |
unsigned char *derQ = (unsigned char *)keyMat->derQ; |
| 740 |
derQ[0] = V_ASN1_OCTET_STRING; |
| 741 |
derQ[1] = point_length & 0x7f; |
| 742 |
result = EC_POINT_point2oct(group, |
| 743 |
point, |
| 744 |
POINT_CONVERSION_UNCOMPRESSED, |
| 745 |
&derQ[2], |
| 746 |
point_length, |
| 747 |
NULL); |
| 748 |
if (result == 0) |
| 749 |
{ |
| 750 |
crypto_free_ecdsa(keyMat); |
| 751 |
return NULL; |
| 752 |
} |
| 753 |
} |
| 754 |
// Definite, long |
| 755 |
else |
| 756 |
{ |
| 757 |
// Count significate bytes |
| 758 |
size_t bytes = sizeof(size_t); |
| 759 |
for(; bytes > 0; bytes--) |
| 760 |
{ |
| 761 |
size_t value = point_length >> ((bytes - 1) * 8); |
| 762 |
if (value & 0xFF) break; |
| 763 |
} |
| 764 |
|
| 765 |
keyMat->sizeQ = 2 + bytes + point_length; |
| 766 |
keyMat->derQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 767 |
if (!keyMat->derQ) |
| 768 |
{ |
| 769 |
crypto_free_ecdsa(keyMat); |
| 770 |
return NULL; |
| 771 |
} |
| 772 |
|
| 773 |
unsigned char *derQ = (unsigned char *)keyMat->derQ; |
| 774 |
derQ[0] = V_ASN1_OCTET_STRING; |
| 775 |
derQ[1] = 0x80 | bytes; |
| 776 |
|
| 777 |
size_t len = point_length; |
| 778 |
for (size_t i = 1; i <= bytes; i++) |
| 779 |
{ |
| 780 |
derQ[2+bytes-i] = (unsigned char) (len & 0xFF); |
| 781 |
len >>= 8; |
| 782 |
} |
| 783 |
|
| 784 |
result = EC_POINT_point2oct(group, |
| 785 |
point, |
| 786 |
POINT_CONVERSION_UNCOMPRESSED, |
| 787 |
&derQ[2+bytes], |
| 788 |
point_length, |
| 789 |
NULL); |
| 790 |
if (result == 0) |
| 791 |
{ |
| 792 |
crypto_free_ecdsa(keyMat); |
| 793 |
return NULL; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
return keyMat; |
| 798 |
} |
| 799 |
|
| 800 |
// Free the memory of the key |
| 801 |
void crypto_free_ecdsa(ecdsa_key_material_t* keyMat) |
| 802 |
{ |
| 803 |
if (keyMat == NULL) return; |
| 804 |
if (keyMat->derParams) free(keyMat->derParams); |
| 805 |
if (keyMat->bigD) free(keyMat->bigD); |
| 806 |
if (keyMat->derQ) free(keyMat->derQ); |
| 807 |
free(keyMat); |
| 808 |
} |
| 809 |
|
| 810 |
#endif |
| 811 |
|
| 812 |
#ifdef WITH_EDDSA |
| 813 |
|
| 814 |
// Save the key data in PKCS#11 |
| 815 |
int crypto_save_eddsa |
| 816 |
( |
| 817 |
CK_SESSION_HANDLE hSession, |
| 818 |
char* label, |
| 819 |
char* objID, |
| 820 |
size_t objIDLen, |
| 821 |
int noPublicKey, |
| 822 |
EVP_PKEY* eddsa |
| 823 |
) |
| 824 |
{ |
| 825 |
eddsa_key_material_t* keyMat = crypto_malloc_eddsa(eddsa); |
| 826 |
if (keyMat == NULL) |
| 827 |
{ |
| 828 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 829 |
return 1; |
| 830 |
} |
| 831 |
|
| 832 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 833 |
CK_KEY_TYPE keyType = CKK_EC_EDWARDS; |
| 834 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 835 |
if (noPublicKey) |
| 836 |
{ |
| 837 |
ckToken = CK_FALSE; |
| 838 |
} |
| 839 |
CK_ATTRIBUTE pubTemplate[] = { |
| 840 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 841 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 842 |
{ CKA_LABEL, label, strlen(label) }, |
| 843 |
{ CKA_ID, objID, objIDLen }, |
| 844 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 845 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 846 |
{ CKA_ENCRYPT, &ckFalse, sizeof(ckFalse) }, |
| 847 |
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) }, |
| 848 |
{ CKA_EC_PARAMS, keyMat->derOID, keyMat->sizeOID }, |
| 849 |
{ CKA_EC_POINT, keyMat->bigA, keyMat->sizeA }, |
| 850 |
}; |
| 851 |
CK_ATTRIBUTE privTemplate[] = { |
| 852 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 853 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 854 |
{ CKA_LABEL, label, strlen(label) }, |
| 855 |
{ CKA_ID, objID, objIDLen }, |
| 856 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 857 |
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) }, |
| 858 |
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) }, |
| 859 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 860 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 861 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 862 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 863 |
{ CKA_EC_PARAMS, keyMat->derOID, keyMat->sizeOID }, |
| 864 |
{ CKA_VALUE, keyMat->bigK, keyMat->sizeK } |
| 865 |
}; |
| 866 |
|
| 867 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 868 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 13, &hKey1); |
| 869 |
if (rv != CKR_OK) |
| 870 |
{ |
| 871 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 872 |
"Maybe the algorithm is not supported.\n"); |
| 873 |
crypto_free_eddsa(keyMat); |
| 874 |
return 1; |
| 875 |
} |
| 876 |
|
| 877 |
rv = p11->C_CreateObject(hSession, pubTemplate, 10, &hKey2); |
| 878 |
crypto_free_eddsa(keyMat); |
| 879 |
|
| 880 |
if (rv != CKR_OK) |
| 881 |
{ |
| 882 |
p11->C_DestroyObject(hSession, hKey1); |
| 883 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 884 |
return 1; |
| 885 |
} |
| 886 |
|
| 887 |
printf("The key pair has been imported.\n"); |
| 888 |
|
| 889 |
return 0; |
| 890 |
} |
| 891 |
|
| 892 |
// Convert the OpenSSL key to binary |
| 893 |
|
| 894 |
#define X25519_KEYLEN 32 |
| 895 |
#define X448_KEYLEN 56 |
| 896 |
#define ED448_KEYLEN 57 |
| 897 |
|
| 898 |
#define PUBPREFIXLEN 12 |
| 899 |
#define PRIVPREFIXLEN 16 |
| 900 |
|
| 901 |
eddsa_key_material_t* crypto_malloc_eddsa(EVP_PKEY* pkey) |
| 902 |
{ |
| 903 |
int result; |
| 904 |
int len; |
| 905 |
unsigned char *buf; |
| 906 |
|
| 907 |
if (pkey == NULL) |
| 908 |
{ |
| 909 |
return NULL; |
| 910 |
} |
| 911 |
|
| 912 |
eddsa_key_material_t* keyMat = (eddsa_key_material_t*)calloc(1, sizeof(eddsa_key_material_t)); |
| 913 |
if (keyMat == NULL) |
| 914 |
{ |
| 915 |
return NULL; |
| 916 |
} |
| 917 |
|
| 918 |
int nid = EVP_PKEY_id(pkey); |
| 919 |
keyMat->sizeOID = i2d_ASN1_OBJECT(OBJ_nid2obj(nid), NULL); |
| 920 |
keyMat->derOID = (CK_VOID_PTR)malloc(keyMat->sizeOID); |
| 921 |
|
| 922 |
switch (nid) { |
| 923 |
case NID_X25519: |
| 924 |
case NID_ED25519: |
| 925 |
keyMat->sizeK = X25519_KEYLEN; |
| 926 |
keyMat->sizeA = X25519_KEYLEN; |
| 927 |
break; |
| 928 |
case NID_X448: |
| 929 |
keyMat->sizeK = X448_KEYLEN; |
| 930 |
keyMat->sizeA = X448_KEYLEN; |
| 931 |
break; |
| 932 |
case NID_ED448: |
| 933 |
keyMat->sizeK = ED448_KEYLEN; |
| 934 |
keyMat->sizeA = ED448_KEYLEN; |
| 935 |
break; |
| 936 |
default: |
| 937 |
crypto_free_eddsa(keyMat); |
| 938 |
return NULL; |
| 939 |
} |
| 940 |
keyMat->bigK = (CK_VOID_PTR)malloc(keyMat->sizeK); |
| 941 |
keyMat->bigA = (CK_VOID_PTR)malloc(keyMat->sizeA); |
| 942 |
if (!keyMat->derOID || !keyMat->bigK || !keyMat->bigA) |
| 943 |
{ |
| 944 |
crypto_free_eddsa(keyMat); |
| 945 |
return NULL; |
| 946 |
} |
| 947 |
|
| 948 |
unsigned char *p = (unsigned char*) keyMat->derOID; |
| 949 |
result = i2d_ASN1_OBJECT(OBJ_nid2obj(nid), &p); |
| 950 |
if (result <= 0) |
| 951 |
{ |
| 952 |
crypto_free_eddsa(keyMat); |
| 953 |
return NULL; |
| 954 |
} |
| 955 |
|
| 956 |
len = i2d_PUBKEY(pkey, NULL); |
| 957 |
if (((CK_ULONG) len != PUBPREFIXLEN + keyMat->sizeA) || |
| 958 |
((buf = (unsigned char*) malloc(len)) == NULL)) |
| 959 |
{ |
| 960 |
crypto_free_eddsa(keyMat); |
| 961 |
return NULL; |
| 962 |
} |
| 963 |
p = buf; |
| 964 |
i2d_PUBKEY(pkey, &p); |
| 965 |
memcpy(keyMat->bigA, buf + PUBPREFIXLEN, keyMat->sizeA); |
| 966 |
free(buf); |
| 967 |
|
| 968 |
len = i2d_PrivateKey(pkey, NULL); |
| 969 |
if (((CK_ULONG) len != PRIVPREFIXLEN + keyMat->sizeK) || |
| 970 |
((buf = (unsigned char*) malloc(len)) == NULL)) |
| 971 |
{ |
| 972 |
crypto_free_eddsa(keyMat); |
| 973 |
return NULL; |
| 974 |
} |
| 975 |
p = buf; |
| 976 |
i2d_PrivateKey(pkey, &p); |
| 977 |
memcpy(keyMat->bigK, buf + PRIVPREFIXLEN, keyMat->sizeK); |
| 978 |
free(buf); |
| 979 |
|
| 980 |
return keyMat; |
| 981 |
} |
| 982 |
|
| 983 |
// Free the memory of the key |
| 984 |
void crypto_free_eddsa(eddsa_key_material_t* keyMat) |
| 985 |
{ |
| 986 |
if (keyMat == NULL) return; |
| 987 |
if (keyMat->derOID) free(keyMat->derOID); |
| 988 |
if (keyMat->bigK) free(keyMat->bigK); |
| 989 |
if (keyMat->bigA) free(keyMat->bigA); |
| 990 |
free(keyMat); |
| 991 |
} |
| 992 |
|
| 993 |
#endif |