| 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-botan.cpp |
| 29 |
|
| 30 |
Code specific for Botan |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#define UTIL_BOTAN |
| 35 |
#include "softhsm2-util.h" |
| 36 |
#include "softhsm2-util-botan.h" |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <iostream> |
| 42 |
#include <fstream> |
| 43 |
|
| 44 |
#include <botan/version.h> |
| 45 |
#include <botan/auto_rng.h> |
| 46 |
#include <botan/pkcs8.h> |
| 47 |
#include <botan/bigint.h> |
| 48 |
#include <botan/der_enc.h> |
| 49 |
#include <botan/oids.h> |
| 50 |
|
| 51 |
// Init Botan |
| 52 |
void crypto_init() |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
// Final Botan |
| 57 |
void crypto_final() |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
// Import a aes secret key from given path |
| 62 |
int crypto_import_aes_key |
| 63 |
( |
| 64 |
CK_SESSION_HANDLE hSession, |
| 65 |
char* filePath, |
| 66 |
char* label, |
| 67 |
char* objID, |
| 68 |
size_t objIDLen |
| 69 |
) |
| 70 |
{ |
| 71 |
const size_t cMaxAesKeySize = 1024 + 1; // including null-character |
| 72 |
char aesKeyValue[cMaxAesKeySize]; |
| 73 |
FILE* fp = fopen(filePath, "rb"); |
| 74 |
if (fp == NULL) |
| 75 |
{ |
| 76 |
fprintf(stderr, "ERROR: Could not open the secret key file.\n"); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
if (fgets(aesKeyValue, cMaxAesKeySize, fp) == NULL) |
| 80 |
{ |
| 81 |
fprintf(stderr, "ERROR: Could not read the secret key file.\n"); |
| 82 |
fclose(fp); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
fclose(fp); |
| 86 |
|
| 87 |
CK_BBOOL ckTrue = CK_TRUE; |
| 88 |
CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; |
| 89 |
CK_KEY_TYPE keyType = CKK_AES; |
| 90 |
CK_ATTRIBUTE keyTemplate[] = { |
| 91 |
{ CKA_CLASS, &keyClass, sizeof(keyClass) }, |
| 92 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 93 |
{ CKA_LABEL, label, strlen(label) }, |
| 94 |
{ CKA_ID, objID, objIDLen }, |
| 95 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 96 |
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) }, |
| 97 |
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) }, |
| 98 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 99 |
{ CKA_VALUE, &aesKeyValue, strlen(aesKeyValue) } |
| 100 |
}; |
| 101 |
|
| 102 |
CK_OBJECT_HANDLE hKey; |
| 103 |
CK_RV rv = p11->C_CreateObject(hSession, keyTemplate, 9, &hKey); |
| 104 |
if (rv != CKR_OK) |
| 105 |
{ |
| 106 |
fprintf(stderr, "ERROR: Could not save the secret key in the token. " |
| 107 |
"Maybe the algorithm is not supported.\n"); |
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
// Import a key pair from given path |
| 115 |
int crypto_import_key_pair |
| 116 |
( |
| 117 |
CK_SESSION_HANDLE hSession, |
| 118 |
char* filePath, |
| 119 |
char* filePIN, |
| 120 |
char* label, |
| 121 |
char* objID, |
| 122 |
size_t objIDLen, |
| 123 |
int noPublicKey |
| 124 |
) |
| 125 |
{ |
| 126 |
Botan::Private_Key* pkey = crypto_read_file(filePath, filePIN); |
| 127 |
if (pkey == NULL) |
| 128 |
{ |
| 129 |
return 1; |
| 130 |
} |
| 131 |
|
| 132 |
Botan::RSA_PrivateKey* rsa = NULL; |
| 133 |
Botan::DSA_PrivateKey* dsa = NULL; |
| 134 |
#ifdef WITH_ECC |
| 135 |
Botan::ECDSA_PrivateKey* ecdsa = NULL; |
| 136 |
#endif |
| 137 |
#ifdef WITH_EDDSA |
| 138 |
Botan::Curve25519_PrivateKey* x25519 = NULL; |
| 139 |
Botan::Ed25519_PrivateKey* ed25519 = NULL; |
| 140 |
#endif |
| 141 |
|
| 142 |
if (pkey->algo_name().compare("RSA") == 0) |
| 143 |
{ |
| 144 |
rsa = dynamic_cast<Botan::RSA_PrivateKey*>(pkey); |
| 145 |
} |
| 146 |
else if (pkey->algo_name().compare("DSA") == 0) |
| 147 |
{ |
| 148 |
dsa = dynamic_cast<Botan::DSA_PrivateKey*>(pkey); |
| 149 |
} |
| 150 |
#ifdef WITH_ECC |
| 151 |
else if (pkey->algo_name().compare("ECDSA") == 0) |
| 152 |
{ |
| 153 |
ecdsa = dynamic_cast<Botan::ECDSA_PrivateKey*>(pkey); |
| 154 |
} |
| 155 |
#endif |
| 156 |
#ifdef WITH_EDDSA |
| 157 |
else if (pkey->algo_name().compare("Curve25519") == 0) |
| 158 |
{ |
| 159 |
x25519 = dynamic_cast<Botan::Curve25519_PrivateKey*>(pkey); |
| 160 |
} |
| 161 |
else if (pkey->algo_name().compare("Ed25519") == 0) |
| 162 |
{ |
| 163 |
ed25519 = dynamic_cast<Botan::Ed25519_PrivateKey*>(pkey); |
| 164 |
} |
| 165 |
#endif |
| 166 |
else |
| 167 |
{ |
| 168 |
fprintf(stderr, "ERROR: %s is not a supported algorithm.\n", |
| 169 |
pkey->algo_name().c_str()); |
| 170 |
delete pkey; |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
int result = 0; |
| 175 |
|
| 176 |
if (rsa) |
| 177 |
{ |
| 178 |
result = crypto_save_rsa(hSession, label, objID, objIDLen, noPublicKey, rsa); |
| 179 |
} |
| 180 |
else if (dsa) |
| 181 |
{ |
| 182 |
result = crypto_save_dsa(hSession, label, objID, objIDLen, noPublicKey, dsa); |
| 183 |
} |
| 184 |
#ifdef WITH_ECC |
| 185 |
else if (ecdsa) |
| 186 |
{ |
| 187 |
result = crypto_save_ecdsa(hSession, label, objID, objIDLen, noPublicKey, ecdsa); |
| 188 |
} |
| 189 |
#endif |
| 190 |
#ifdef WITH_EDDSA |
| 191 |
else if (x25519) |
| 192 |
{ |
| 193 |
result = crypto_save_eddsa(hSession, label, objID, objIDLen, noPublicKey, x25519, 0); |
| 194 |
} |
| 195 |
else if (ed25519) |
| 196 |
{ |
| 197 |
result = crypto_save_eddsa(hSession, label, objID, objIDLen, noPublicKey, 0, ed25519); |
| 198 |
} |
| 199 |
#endif |
| 200 |
else |
| 201 |
{ |
| 202 |
fprintf(stderr, "ERROR: Could not get the key material.\n"); |
| 203 |
result = 1; |
| 204 |
} |
| 205 |
|
| 206 |
delete pkey; |
| 207 |
return result; |
| 208 |
} |
| 209 |
|
| 210 |
// Read the key from file |
| 211 |
Botan::Private_Key* crypto_read_file(char* filePath, char* filePIN) |
| 212 |
{ |
| 213 |
if (filePath == NULL) |
| 214 |
{ |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
Botan::AutoSeeded_RNG* rng = new Botan::AutoSeeded_RNG(); |
| 219 |
Botan::Private_Key* pkey = NULL; |
| 220 |
|
| 221 |
try |
| 222 |
{ |
| 223 |
if (filePIN == NULL) |
| 224 |
{ |
| 225 |
pkey = Botan::PKCS8::load_key(std::string(filePath), *rng); |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
pkey = Botan::PKCS8::load_key(std::string(filePath), *rng, std::string(filePIN)); |
| 230 |
} |
| 231 |
} |
| 232 |
catch (std::exception& e) |
| 233 |
{ |
| 234 |
fprintf(stderr, "%s\n", e.what()); |
| 235 |
fprintf(stderr, "ERROR: Perhaps wrong path to file, wrong file format, " |
| 236 |
"or wrong PIN to file (--file-pin <PIN>).\n"); |
| 237 |
delete rng; |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
delete rng; |
| 241 |
|
| 242 |
return pkey; |
| 243 |
} |
| 244 |
|
| 245 |
// Save the key data in PKCS#11 |
| 246 |
int crypto_save_rsa |
| 247 |
( |
| 248 |
CK_SESSION_HANDLE hSession, |
| 249 |
char* label, |
| 250 |
char* objID, |
| 251 |
size_t objIDLen, |
| 252 |
int noPublicKey, |
| 253 |
Botan::RSA_PrivateKey* rsa |
| 254 |
) |
| 255 |
{ |
| 256 |
rsa_key_material_t* keyMat = crypto_malloc_rsa(rsa); |
| 257 |
if (!keyMat) |
| 258 |
{ |
| 259 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 260 |
return 1; |
| 261 |
} |
| 262 |
|
| 263 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 264 |
CK_KEY_TYPE keyType = CKK_RSA; |
| 265 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 266 |
if (noPublicKey) |
| 267 |
{ |
| 268 |
ckToken = CK_FALSE; |
| 269 |
} |
| 270 |
CK_ATTRIBUTE pubTemplate[] = { |
| 271 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 272 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 273 |
{ CKA_LABEL, label, strlen(label) }, |
| 274 |
{ CKA_ID, objID, objIDLen }, |
| 275 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 276 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 277 |
{ CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) }, |
| 278 |
{ CKA_WRAP, &ckTrue, sizeof(ckTrue) }, |
| 279 |
{ CKA_PUBLIC_EXPONENT, keyMat->bigE, keyMat->sizeE }, |
| 280 |
{ CKA_MODULUS, keyMat->bigN, keyMat->sizeN } |
| 281 |
}; |
| 282 |
CK_ATTRIBUTE privTemplate[] = { |
| 283 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 284 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 285 |
{ CKA_LABEL, label, strlen(label) }, |
| 286 |
{ CKA_ID, objID, objIDLen }, |
| 287 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 288 |
{ CKA_DECRYPT, &ckTrue, sizeof(ckTrue) }, |
| 289 |
{ CKA_UNWRAP, &ckTrue, sizeof(ckTrue) }, |
| 290 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 291 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 292 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 293 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 294 |
{ CKA_PUBLIC_EXPONENT, keyMat->bigE, keyMat->sizeE }, |
| 295 |
{ CKA_MODULUS, keyMat->bigN, keyMat->sizeN }, |
| 296 |
{ CKA_PRIVATE_EXPONENT, keyMat->bigD, keyMat->sizeD }, |
| 297 |
{ CKA_PRIME_1, keyMat->bigP, keyMat->sizeP }, |
| 298 |
{ CKA_PRIME_2, keyMat->bigQ, keyMat->sizeQ }, |
| 299 |
{ CKA_EXPONENT_1, keyMat->bigDMP1, keyMat->sizeDMP1 }, |
| 300 |
{ CKA_EXPONENT_2, keyMat->bigDMQ1, keyMat->sizeDMQ1 }, |
| 301 |
{ CKA_COEFFICIENT, keyMat->bigIQMP, keyMat->sizeIQMP } |
| 302 |
}; |
| 303 |
|
| 304 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 305 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 19, &hKey1); |
| 306 |
if (rv != CKR_OK) |
| 307 |
{ |
| 308 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 309 |
"Maybe the algorithm is not supported.\n"); |
| 310 |
crypto_free_rsa(keyMat); |
| 311 |
return 1; |
| 312 |
} |
| 313 |
|
| 314 |
rv = p11->C_CreateObject(hSession, pubTemplate, 10, &hKey2); |
| 315 |
crypto_free_rsa(keyMat); |
| 316 |
|
| 317 |
if (rv != CKR_OK) |
| 318 |
{ |
| 319 |
p11->C_DestroyObject(hSession, hKey1); |
| 320 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
printf("The key pair has been imported.\n"); |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
// Convert the Botan key to binary |
| 330 |
rsa_key_material_t* crypto_malloc_rsa(Botan::RSA_PrivateKey* rsa) |
| 331 |
{ |
| 332 |
if (rsa == NULL) |
| 333 |
{ |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
|
| 337 |
rsa_key_material_t* keyMat = (rsa_key_material_t*)malloc(sizeof(rsa_key_material_t)); |
| 338 |
if (keyMat == NULL) |
| 339 |
{ |
| 340 |
return NULL; |
| 341 |
} |
| 342 |
|
| 343 |
keyMat->sizeE = rsa->get_e().bytes(); |
| 344 |
keyMat->sizeN = rsa->get_n().bytes(); |
| 345 |
keyMat->sizeD = rsa->get_d().bytes(); |
| 346 |
keyMat->sizeP = rsa->get_p().bytes(); |
| 347 |
keyMat->sizeQ = rsa->get_q().bytes(); |
| 348 |
keyMat->sizeDMP1 = rsa->get_d1().bytes(); |
| 349 |
keyMat->sizeDMQ1 = rsa->get_d2().bytes(); |
| 350 |
keyMat->sizeIQMP = rsa->get_c().bytes(); |
| 351 |
|
| 352 |
keyMat->bigE = (CK_VOID_PTR)malloc(keyMat->sizeE); |
| 353 |
keyMat->bigN = (CK_VOID_PTR)malloc(keyMat->sizeN); |
| 354 |
keyMat->bigD = (CK_VOID_PTR)malloc(keyMat->sizeD); |
| 355 |
keyMat->bigP = (CK_VOID_PTR)malloc(keyMat->sizeP); |
| 356 |
keyMat->bigQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 357 |
keyMat->bigDMP1 = (CK_VOID_PTR)malloc(keyMat->sizeDMP1); |
| 358 |
keyMat->bigDMQ1 = (CK_VOID_PTR)malloc(keyMat->sizeDMQ1); |
| 359 |
keyMat->bigIQMP = (CK_VOID_PTR)malloc(keyMat->sizeIQMP); |
| 360 |
|
| 361 |
if |
| 362 |
( |
| 363 |
!keyMat->bigE || |
| 364 |
!keyMat->bigN || |
| 365 |
!keyMat->bigD || |
| 366 |
!keyMat->bigP || |
| 367 |
!keyMat->bigQ || |
| 368 |
!keyMat->bigDMP1 || |
| 369 |
!keyMat->bigDMQ1 || |
| 370 |
!keyMat->bigIQMP |
| 371 |
) |
| 372 |
{ |
| 373 |
crypto_free_rsa(keyMat); |
| 374 |
return NULL; |
| 375 |
} |
| 376 |
|
| 377 |
rsa->get_e().binary_encode((Botan::byte*)keyMat->bigE); |
| 378 |
rsa->get_n().binary_encode((Botan::byte*)keyMat->bigN); |
| 379 |
rsa->get_d().binary_encode((Botan::byte*)keyMat->bigD); |
| 380 |
rsa->get_p().binary_encode((Botan::byte*)keyMat->bigP); |
| 381 |
rsa->get_q().binary_encode((Botan::byte*)keyMat->bigQ); |
| 382 |
rsa->get_d1().binary_encode((Botan::byte*)keyMat->bigDMP1); |
| 383 |
rsa->get_d2().binary_encode((Botan::byte*)keyMat->bigDMQ1); |
| 384 |
rsa->get_c().binary_encode((Botan::byte*)keyMat->bigIQMP); |
| 385 |
|
| 386 |
return keyMat; |
| 387 |
} |
| 388 |
|
| 389 |
// Free the memory of the key |
| 390 |
void crypto_free_rsa(rsa_key_material_t* keyMat) |
| 391 |
{ |
| 392 |
if (keyMat == NULL) return; |
| 393 |
if (keyMat->bigE) free(keyMat->bigE); |
| 394 |
if (keyMat->bigN) free(keyMat->bigN); |
| 395 |
if (keyMat->bigD) free(keyMat->bigD); |
| 396 |
if (keyMat->bigP) free(keyMat->bigP); |
| 397 |
if (keyMat->bigQ) free(keyMat->bigQ); |
| 398 |
if (keyMat->bigDMP1) free(keyMat->bigDMP1); |
| 399 |
if (keyMat->bigDMQ1) free(keyMat->bigDMQ1); |
| 400 |
if (keyMat->bigIQMP) free(keyMat->bigIQMP); |
| 401 |
free(keyMat); |
| 402 |
} |
| 403 |
|
| 404 |
// Save the key data in PKCS#11 |
| 405 |
int crypto_save_dsa |
| 406 |
( |
| 407 |
CK_SESSION_HANDLE hSession, |
| 408 |
char* label, |
| 409 |
char* objID, |
| 410 |
size_t objIDLen, |
| 411 |
int noPublicKey, |
| 412 |
Botan::DSA_PrivateKey* dsa |
| 413 |
) |
| 414 |
{ |
| 415 |
dsa_key_material_t* keyMat = crypto_malloc_dsa(dsa); |
| 416 |
if (keyMat == NULL) |
| 417 |
{ |
| 418 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 423 |
CK_KEY_TYPE keyType = CKK_DSA; |
| 424 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 425 |
if (noPublicKey) |
| 426 |
{ |
| 427 |
ckToken = CK_FALSE; |
| 428 |
} |
| 429 |
CK_ATTRIBUTE pubTemplate[] = { |
| 430 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 431 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 432 |
{ CKA_LABEL, label, strlen(label) }, |
| 433 |
{ CKA_ID, objID, objIDLen }, |
| 434 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 435 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 436 |
{ CKA_ENCRYPT, &ckFalse, sizeof(ckFalse) }, |
| 437 |
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) }, |
| 438 |
{ CKA_PRIME, keyMat->bigP, keyMat->sizeP }, |
| 439 |
{ CKA_SUBPRIME, keyMat->bigQ, keyMat->sizeQ }, |
| 440 |
{ CKA_BASE, keyMat->bigG, keyMat->sizeG }, |
| 441 |
{ CKA_VALUE, keyMat->bigY, keyMat->sizeY } |
| 442 |
}; |
| 443 |
CK_ATTRIBUTE privTemplate[] = { |
| 444 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 445 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 446 |
{ CKA_LABEL, label, strlen(label) }, |
| 447 |
{ CKA_ID, objID, objIDLen }, |
| 448 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 449 |
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) }, |
| 450 |
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) }, |
| 451 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 452 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 453 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 454 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 455 |
{ CKA_PRIME, keyMat->bigP, keyMat->sizeP }, |
| 456 |
{ CKA_SUBPRIME, keyMat->bigQ, keyMat->sizeQ }, |
| 457 |
{ CKA_BASE, keyMat->bigG, keyMat->sizeG }, |
| 458 |
{ CKA_VALUE, keyMat->bigX, keyMat->sizeX } |
| 459 |
}; |
| 460 |
|
| 461 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 462 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 15, &hKey1); |
| 463 |
if (rv != CKR_OK) |
| 464 |
{ |
| 465 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 466 |
"Maybe the algorithm is not supported.\n"); |
| 467 |
crypto_free_dsa(keyMat); |
| 468 |
return 1; |
| 469 |
} |
| 470 |
|
| 471 |
rv = p11->C_CreateObject(hSession, pubTemplate, 12, &hKey2); |
| 472 |
crypto_free_dsa(keyMat); |
| 473 |
|
| 474 |
if (rv != CKR_OK) |
| 475 |
{ |
| 476 |
p11->C_DestroyObject(hSession, hKey1); |
| 477 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 478 |
return 1; |
| 479 |
} |
| 480 |
|
| 481 |
printf("The key pair has been imported.\n"); |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
// Convert the Botan key to binary |
| 487 |
dsa_key_material_t* crypto_malloc_dsa(Botan::DSA_PrivateKey* dsa) |
| 488 |
{ |
| 489 |
if (dsa == NULL) |
| 490 |
{ |
| 491 |
return NULL; |
| 492 |
} |
| 493 |
|
| 494 |
dsa_key_material_t *keyMat = (dsa_key_material_t *)malloc(sizeof(dsa_key_material_t)); |
| 495 |
if (keyMat == NULL) |
| 496 |
{ |
| 497 |
return NULL; |
| 498 |
} |
| 499 |
|
| 500 |
keyMat->sizeP = dsa->group_p().bytes(); |
| 501 |
keyMat->sizeQ = dsa->group_q().bytes(); |
| 502 |
keyMat->sizeG = dsa->group_g().bytes(); |
| 503 |
keyMat->sizeX = dsa->get_x().bytes(); |
| 504 |
keyMat->sizeY = dsa->get_y().bytes(); |
| 505 |
|
| 506 |
keyMat->bigP = (CK_VOID_PTR)malloc(keyMat->sizeP); |
| 507 |
keyMat->bigQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 508 |
keyMat->bigG = (CK_VOID_PTR)malloc(keyMat->sizeG); |
| 509 |
keyMat->bigX = (CK_VOID_PTR)malloc(keyMat->sizeX); |
| 510 |
keyMat->bigY = (CK_VOID_PTR)malloc(keyMat->sizeY); |
| 511 |
|
| 512 |
if (!keyMat->bigP || !keyMat->bigQ || !keyMat->bigG || !keyMat->bigX || !keyMat->bigY) |
| 513 |
{ |
| 514 |
crypto_free_dsa(keyMat); |
| 515 |
return NULL; |
| 516 |
} |
| 517 |
|
| 518 |
dsa->group_p().binary_encode((Botan::byte*)keyMat->bigP); |
| 519 |
dsa->group_q().binary_encode((Botan::byte*)keyMat->bigQ); |
| 520 |
dsa->group_g().binary_encode((Botan::byte*)keyMat->bigG); |
| 521 |
dsa->get_x().binary_encode((Botan::byte*)keyMat->bigX); |
| 522 |
dsa->get_y().binary_encode((Botan::byte*)keyMat->bigY); |
| 523 |
|
| 524 |
return keyMat; |
| 525 |
} |
| 526 |
|
| 527 |
// Free the memory of the key |
| 528 |
void crypto_free_dsa(dsa_key_material_t* keyMat) |
| 529 |
{ |
| 530 |
if (keyMat == NULL) return; |
| 531 |
if (keyMat->bigP) free(keyMat->bigP); |
| 532 |
if (keyMat->bigQ) free(keyMat->bigQ); |
| 533 |
if (keyMat->bigG) free(keyMat->bigG); |
| 534 |
if (keyMat->bigX) free(keyMat->bigX); |
| 535 |
if (keyMat->bigY) free(keyMat->bigY); |
| 536 |
free(keyMat); |
| 537 |
} |
| 538 |
|
| 539 |
#ifdef WITH_ECC |
| 540 |
// Save the key data in PKCS#11 |
| 541 |
int crypto_save_ecdsa |
| 542 |
( |
| 543 |
CK_SESSION_HANDLE hSession, |
| 544 |
char* label, |
| 545 |
char* objID, |
| 546 |
size_t objIDLen, |
| 547 |
int noPublicKey, |
| 548 |
Botan::ECDSA_PrivateKey* ecdsa |
| 549 |
) |
| 550 |
{ |
| 551 |
ecdsa_key_material_t* keyMat = crypto_malloc_ecdsa(ecdsa); |
| 552 |
if (keyMat == NULL) |
| 553 |
{ |
| 554 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 555 |
return 1; |
| 556 |
} |
| 557 |
|
| 558 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 559 |
CK_KEY_TYPE keyType = CKK_ECDSA; |
| 560 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 561 |
if (noPublicKey) |
| 562 |
{ |
| 563 |
ckToken = CK_FALSE; |
| 564 |
} |
| 565 |
CK_ATTRIBUTE pubTemplate[] = { |
| 566 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 567 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 568 |
{ CKA_LABEL, label, strlen(label) }, |
| 569 |
{ CKA_ID, objID, objIDLen }, |
| 570 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 571 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 572 |
{ CKA_ENCRYPT, &ckFalse, sizeof(ckFalse) }, |
| 573 |
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) }, |
| 574 |
{ CKA_EC_PARAMS, keyMat->derParams, keyMat->sizeParams }, |
| 575 |
{ CKA_EC_POINT, keyMat->derQ, keyMat->sizeQ } |
| 576 |
}; |
| 577 |
CK_ATTRIBUTE privTemplate[] = { |
| 578 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 579 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 580 |
{ CKA_LABEL, label, strlen(label) }, |
| 581 |
{ CKA_ID, objID, objIDLen }, |
| 582 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 583 |
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) }, |
| 584 |
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) }, |
| 585 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 586 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 587 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 588 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 589 |
{ CKA_EC_PARAMS, keyMat->derParams, keyMat->sizeParams }, |
| 590 |
{ CKA_VALUE, keyMat->bigD, keyMat->sizeD } |
| 591 |
}; |
| 592 |
|
| 593 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 594 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 13, &hKey1); |
| 595 |
if (rv != CKR_OK) |
| 596 |
{ |
| 597 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 598 |
"Maybe the algorithm is not supported.\n"); |
| 599 |
crypto_free_ecdsa(keyMat); |
| 600 |
return 1; |
| 601 |
} |
| 602 |
|
| 603 |
rv = p11->C_CreateObject(hSession, pubTemplate, 10, &hKey2); |
| 604 |
crypto_free_ecdsa(keyMat); |
| 605 |
|
| 606 |
if (rv != CKR_OK) |
| 607 |
{ |
| 608 |
p11->C_DestroyObject(hSession, hKey1); |
| 609 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 610 |
return 1; |
| 611 |
} |
| 612 |
|
| 613 |
printf("The key pair has been imported.\n"); |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
// Convert the Botan key to binary |
| 619 |
ecdsa_key_material_t* crypto_malloc_ecdsa(Botan::ECDSA_PrivateKey* ecdsa) |
| 620 |
{ |
| 621 |
if (ecdsa == NULL) |
| 622 |
{ |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
|
| 626 |
ecdsa_key_material_t *keyMat = (ecdsa_key_material_t *)malloc(sizeof(ecdsa_key_material_t)); |
| 627 |
if (keyMat == NULL) |
| 628 |
{ |
| 629 |
return NULL; |
| 630 |
} |
| 631 |
|
| 632 |
std::vector<Botan::byte> derEC = ecdsa->domain().DER_encode(Botan::EC_DOMPAR_ENC_OID); |
| 633 |
Botan::secure_vector<Botan::byte> derPoint; |
| 634 |
|
| 635 |
try |
| 636 |
{ |
| 637 |
#if BOTAN_VERSION_CODE >= BOTAN_VERSION_CODE_FOR(2,5,0) |
| 638 |
std::vector<uint8_t> repr = ecdsa->public_point().encode(Botan::PointGFp::UNCOMPRESSED); |
| 639 |
#else |
| 640 |
Botan::secure_vector<Botan::byte> repr = Botan::EC2OSP(ecdsa->public_point(), |
| 641 |
Botan::PointGFp::UNCOMPRESSED); |
| 642 |
#endif |
| 643 |
|
| 644 |
derPoint = Botan::DER_Encoder() |
| 645 |
.encode(repr, Botan::OCTET_STRING) |
| 646 |
.get_contents(); |
| 647 |
} |
| 648 |
catch (...) |
| 649 |
{ |
| 650 |
return NULL; |
| 651 |
} |
| 652 |
|
| 653 |
keyMat->sizeParams = derEC.size(); |
| 654 |
keyMat->sizeD = ecdsa->private_value().bytes(); |
| 655 |
keyMat->sizeQ = derPoint.size(); |
| 656 |
|
| 657 |
keyMat->derParams = (CK_VOID_PTR)malloc(keyMat->sizeParams); |
| 658 |
keyMat->bigD = (CK_VOID_PTR)malloc(keyMat->sizeD); |
| 659 |
keyMat->derQ = (CK_VOID_PTR)malloc(keyMat->sizeQ); |
| 660 |
|
| 661 |
if (!keyMat->derParams || !keyMat->bigD || !keyMat->derQ) |
| 662 |
{ |
| 663 |
crypto_free_ecdsa(keyMat); |
| 664 |
return NULL; |
| 665 |
} |
| 666 |
|
| 667 |
memcpy(keyMat->derParams, &derEC[0], derEC.size()); |
| 668 |
ecdsa->private_value().binary_encode((Botan::byte*)keyMat->bigD); |
| 669 |
memcpy(keyMat->derQ, &derPoint[0], derPoint.size()); |
| 670 |
|
| 671 |
return keyMat; |
| 672 |
} |
| 673 |
|
| 674 |
// Free the memory of the key |
| 675 |
void crypto_free_ecdsa(ecdsa_key_material_t* keyMat) |
| 676 |
{ |
| 677 |
if (keyMat == NULL) return; |
| 678 |
if (keyMat->derParams) free(keyMat->derParams); |
| 679 |
if (keyMat->bigD) free(keyMat->bigD); |
| 680 |
if (keyMat->derQ) free(keyMat->derQ); |
| 681 |
free(keyMat); |
| 682 |
} |
| 683 |
#endif |
| 684 |
|
| 685 |
#ifdef WITH_EDDSA |
| 686 |
// Save the key data in PKCS#11 |
| 687 |
int crypto_save_eddsa |
| 688 |
( |
| 689 |
CK_SESSION_HANDLE hSession, |
| 690 |
char* label, |
| 691 |
char* objID, |
| 692 |
size_t objIDLen, |
| 693 |
int noPublicKey, |
| 694 |
Botan::Curve25519_PrivateKey* x25519, |
| 695 |
Botan::Ed25519_PrivateKey* ed25519 |
| 696 |
) |
| 697 |
{ |
| 698 |
eddsa_key_material_t* keyMat = crypto_malloc_eddsa(x25519, ed25519); |
| 699 |
if (keyMat == NULL) |
| 700 |
{ |
| 701 |
fprintf(stderr, "ERROR: Could not convert the key material to binary information.\n"); |
| 702 |
return 1; |
| 703 |
} |
| 704 |
|
| 705 |
CK_OBJECT_CLASS pubClass = CKO_PUBLIC_KEY, privClass = CKO_PRIVATE_KEY; |
| 706 |
CK_KEY_TYPE keyType = CKK_EC_EDWARDS; |
| 707 |
CK_BBOOL ckTrue = CK_TRUE, ckFalse = CK_FALSE, ckToken = CK_TRUE; |
| 708 |
if (noPublicKey) |
| 709 |
{ |
| 710 |
ckToken = CK_FALSE; |
| 711 |
} |
| 712 |
CK_ATTRIBUTE pubTemplate[] = { |
| 713 |
{ CKA_CLASS, &pubClass, sizeof(pubClass) }, |
| 714 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 715 |
{ CKA_LABEL, label, strlen(label) }, |
| 716 |
{ CKA_ID, objID, objIDLen }, |
| 717 |
{ CKA_TOKEN, &ckToken, sizeof(ckToken) }, |
| 718 |
{ CKA_VERIFY, &ckTrue, sizeof(ckTrue) }, |
| 719 |
{ CKA_ENCRYPT, &ckFalse, sizeof(ckFalse) }, |
| 720 |
{ CKA_WRAP, &ckFalse, sizeof(ckFalse) }, |
| 721 |
{ CKA_EC_PARAMS, keyMat->derOID, keyMat->sizeOID }, |
| 722 |
{ CKA_EC_POINT, keyMat->bigA, keyMat->sizeA }, |
| 723 |
}; |
| 724 |
CK_ATTRIBUTE privTemplate[] = { |
| 725 |
{ CKA_CLASS, &privClass, sizeof(privClass) }, |
| 726 |
{ CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
| 727 |
{ CKA_LABEL, label, strlen(label) }, |
| 728 |
{ CKA_ID, objID, objIDLen }, |
| 729 |
{ CKA_SIGN, &ckTrue, sizeof(ckTrue) }, |
| 730 |
{ CKA_DECRYPT, &ckFalse, sizeof(ckFalse) }, |
| 731 |
{ CKA_UNWRAP, &ckFalse, sizeof(ckFalse) }, |
| 732 |
{ CKA_SENSITIVE, &ckTrue, sizeof(ckTrue) }, |
| 733 |
{ CKA_TOKEN, &ckTrue, sizeof(ckTrue) }, |
| 734 |
{ CKA_PRIVATE, &ckTrue, sizeof(ckTrue) }, |
| 735 |
{ CKA_EXTRACTABLE, &ckFalse, sizeof(ckFalse) }, |
| 736 |
{ CKA_EC_PARAMS, keyMat->derOID, keyMat->sizeOID }, |
| 737 |
{ CKA_VALUE, keyMat->bigK, keyMat->sizeK } |
| 738 |
}; |
| 739 |
|
| 740 |
CK_OBJECT_HANDLE hKey1, hKey2; |
| 741 |
CK_RV rv = p11->C_CreateObject(hSession, privTemplate, 13, &hKey1); |
| 742 |
if (rv != CKR_OK) |
| 743 |
{ |
| 744 |
fprintf(stderr, "ERROR: Could not save the private key in the token. " |
| 745 |
"Maybe the algorithm is not supported.\n"); |
| 746 |
crypto_free_eddsa(keyMat); |
| 747 |
return 1; |
| 748 |
} |
| 749 |
|
| 750 |
rv = p11->C_CreateObject(hSession, pubTemplate, 10, &hKey2); |
| 751 |
crypto_free_eddsa(keyMat); |
| 752 |
|
| 753 |
if (rv != CKR_OK) |
| 754 |
{ |
| 755 |
p11->C_DestroyObject(hSession, hKey1); |
| 756 |
fprintf(stderr, "ERROR: Could not save the public key in the token.\n"); |
| 757 |
return 1; |
| 758 |
} |
| 759 |
|
| 760 |
printf("The key pair has been imported.\n"); |
| 761 |
|
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
// Convert the OpenSSL key to binary |
| 766 |
eddsa_key_material_t* crypto_malloc_eddsa |
| 767 |
( |
| 768 |
Botan::Curve25519_PrivateKey* x25519, |
| 769 |
Botan::Ed25519_PrivateKey* ed25519 |
| 770 |
) |
| 771 |
{ |
| 772 |
if ((x25519 == NULL) && (ed25519 == NULL)) |
| 773 |
{ |
| 774 |
return NULL; |
| 775 |
} |
| 776 |
|
| 777 |
eddsa_key_material_t* keyMat = (eddsa_key_material_t*)malloc(sizeof(eddsa_key_material_t)); |
| 778 |
if (keyMat == NULL) |
| 779 |
{ |
| 780 |
return NULL; |
| 781 |
} |
| 782 |
|
| 783 |
Botan::OID oid; |
| 784 |
if (x25519) oid = Botan::OIDS::lookup("Curve25519"); |
| 785 |
if (ed25519) oid = Botan::OIDS::lookup("Ed25519"); |
| 786 |
if (oid.empty()) |
| 787 |
{ |
| 788 |
return NULL; |
| 789 |
} |
| 790 |
|
| 791 |
Botan::secure_vector<Botan::byte> derOID; |
| 792 |
derOID = Botan::DER_Encoder().encode(oid).get_contents(); |
| 793 |
|
| 794 |
memset(keyMat, 0, sizeof(*keyMat)); |
| 795 |
keyMat->sizeOID = derOID.size(); |
| 796 |
keyMat->derOID = (CK_VOID_PTR)malloc(keyMat->sizeOID); |
| 797 |
|
| 798 |
std::vector<Botan::byte> pub; |
| 799 |
if (x25519) pub = x25519->public_value(); |
| 800 |
if (ed25519) pub = ed25519->get_public_key(); |
| 801 |
keyMat->sizeA = pub.size(); |
| 802 |
keyMat->bigA = (CK_VOID_PTR)malloc(keyMat->sizeA); |
| 803 |
|
| 804 |
Botan::secure_vector<Botan::byte> priv; |
| 805 |
if (x25519) priv = x25519->get_x(); |
| 806 |
if (ed25519) |
| 807 |
{ |
| 808 |
priv = ed25519->get_private_key(); |
| 809 |
priv.resize(32); |
| 810 |
} |
| 811 |
keyMat->sizeK = priv.size(); |
| 812 |
keyMat->bigK = (CK_VOID_PTR)malloc(keyMat->sizeK); |
| 813 |
|
| 814 |
if (!keyMat->derOID || !keyMat->bigK || !keyMat->bigA) |
| 815 |
{ |
| 816 |
crypto_free_eddsa(keyMat); |
| 817 |
return NULL; |
| 818 |
} |
| 819 |
|
| 820 |
memcpy(keyMat->derOID, derOID.data(), keyMat->sizeOID); |
| 821 |
memcpy(keyMat->bigA, pub.data(), keyMat->sizeA); |
| 822 |
memcpy(keyMat->bigK, priv.data(), keyMat->sizeK); |
| 823 |
|
| 824 |
return keyMat; |
| 825 |
} |
| 826 |
|
| 827 |
// Free the memory of the key |
| 828 |
void crypto_free_eddsa(eddsa_key_material_t* keyMat) |
| 829 |
{ |
| 830 |
if (keyMat == NULL) return; |
| 831 |
if (keyMat->derOID) free(keyMat->derOID); |
| 832 |
if (keyMat->bigK) free(keyMat->bigK); |
| 833 |
if (keyMat->bigA) free(keyMat->bigA); |
| 834 |
free(keyMat); |
| 835 |
} |
| 836 |
#endif |