| 1 |
/* |
| 2 |
* gnome-keyring |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Stefan Walter |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "pkcs11/pkcs11.h" |
| 24 |
|
| 25 |
#include "gkm-attributes.h" |
| 26 |
#include "gkm-credential.h" |
| 27 |
#define DEBUG_FLAG GKM_DEBUG_OBJECT |
| 28 |
#include "gkm-debug.h" |
| 29 |
#include "gkm-factory.h" |
| 30 |
#include "gkm-private-xsa-key.h" |
| 31 |
#include "gkm-data-der.h" |
| 32 |
#include "gkm-session.h" |
| 33 |
#include "gkm-transaction.h" |
| 34 |
#include "gkm-util.h" |
| 35 |
|
| 36 |
struct _GkmPrivateXsaKeyPrivate { |
| 37 |
GkmSexp *sexp; |
| 38 |
}; |
| 39 |
|
| 40 |
G_DEFINE_TYPE_WITH_PRIVATE (GkmPrivateXsaKey, gkm_private_xsa_key, GKM_TYPE_SEXP_KEY); |
| 41 |
|
| 42 |
/* ----------------------------------------------------------------------------- |
| 43 |
* INTERNAL |
| 44 |
*/ |
| 45 |
|
| 46 |
/* Can't be defined in gkm_attributes, because it does not know anything about |
| 47 |
* DER encoding nor OIDs |
| 48 |
*/ |
| 49 |
gboolean |
| 50 |
gkm_attributes_find_ecc_oid (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, GQuark *value) |
| 51 |
{ |
| 52 |
GBytes *bytes; |
| 53 |
CK_ATTRIBUTE_PTR attr; |
| 54 |
GQuark oid; |
| 55 |
|
| 56 |
g_return_val_if_fail (attrs || !n_attrs, FALSE); |
| 57 |
|
| 58 |
attr = gkm_attributes_find (attrs, n_attrs, CKA_EC_PARAMS); |
| 59 |
if (attr == NULL) |
| 60 |
return FALSE; |
| 61 |
|
| 62 |
bytes = g_bytes_new (attr->pValue, attr->ulValueLen); |
| 63 |
g_return_val_if_fail (bytes != NULL, FALSE); |
| 64 |
|
| 65 |
oid = gkm_data_der_oid_from_ec_params (bytes); |
| 66 |
g_return_val_if_fail (oid != 0, FALSE); |
| 67 |
*value = oid; |
| 68 |
|
| 69 |
g_bytes_unref (bytes); |
| 70 |
return TRUE; |
| 71 |
} |
| 72 |
|
| 73 |
gboolean |
| 74 |
gkm_attributes_find_ecc_q (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, |
| 75 |
CK_ATTRIBUTE_TYPE type, GBytes **value) |
| 76 |
{ |
| 77 |
g_autoptr(GBytes) data = NULL; |
| 78 |
gboolean rv; |
| 79 |
|
| 80 |
rv = gkm_attributes_find_bytes (attrs, n_attrs, type, &data); |
| 81 |
g_return_val_if_fail (rv, FALSE); |
| 82 |
|
| 83 |
rv = gkm_data_der_decode_ecdsa_q (data, value); |
| 84 |
|
| 85 |
return rv; |
| 86 |
} |
| 87 |
|
| 88 |
static CK_RV |
| 89 |
create_rsa_private (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, gcry_sexp_t *skey) |
| 90 |
{ |
| 91 |
gcry_error_t gcry; |
| 92 |
gcry_mpi_t n = NULL; |
| 93 |
gcry_mpi_t e = NULL; |
| 94 |
gcry_mpi_t d = NULL; |
| 95 |
gcry_mpi_t p = NULL; |
| 96 |
gcry_mpi_t q = NULL; |
| 97 |
gcry_mpi_t u = NULL; |
| 98 |
CK_RV ret; |
| 99 |
|
| 100 |
if (!gkm_attributes_find_mpi (attrs, n_attrs, CKA_MODULUS, &n) || |
| 101 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_PUBLIC_EXPONENT, &e) || |
| 102 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_PRIVATE_EXPONENT, &d) || |
| 103 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_PRIME_1, &p) || |
| 104 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_PRIME_2, &q)) { |
| 105 |
ret = CKR_TEMPLATE_INCOMPLETE; |
| 106 |
goto done; |
| 107 |
} |
| 108 |
|
| 109 |
/* Fix up the incoming key so gcrypt likes it */ |
| 110 |
if (gcry_mpi_cmp (p, q) > 0) |
| 111 |
gcry_mpi_swap (p, q); |
| 112 |
|
| 113 |
/* Compute U. */ |
| 114 |
u = gcry_mpi_snew (gcry_mpi_get_nbits (n)); |
| 115 |
gcry_mpi_invm (u, p, q); |
| 116 |
|
| 117 |
gcry = gcry_sexp_build (skey, NULL, |
| 118 |
"(private-key (rsa (n %m) (e %m) (d %m) (p %m) (q %m) (u %m)))", |
| 119 |
n, e, d, p, q, u); |
| 120 |
|
| 121 |
if (gcry != 0) { |
| 122 |
g_message ("couldn't create RSA key from passed attributes: %s", gcry_strerror (gcry)); |
| 123 |
ret = CKR_FUNCTION_FAILED; |
| 124 |
goto done; |
| 125 |
} |
| 126 |
|
| 127 |
gkm_attributes_consume (attrs, n_attrs, CKA_MODULUS, CKA_PUBLIC_EXPONENT, |
| 128 |
CKA_PRIVATE_EXPONENT, CKA_PRIME_1, CKA_PRIME_2, |
| 129 |
CKA_EXPONENT_1, CKA_EXPONENT_2, CKA_COEFFICIENT, G_MAXULONG); |
| 130 |
ret = CKR_OK; |
| 131 |
|
| 132 |
done: |
| 133 |
gcry_mpi_release (n); |
| 134 |
gcry_mpi_release (e); |
| 135 |
gcry_mpi_release (d); |
| 136 |
gcry_mpi_release (p); |
| 137 |
gcry_mpi_release (q); |
| 138 |
gcry_mpi_release (u); |
| 139 |
return ret; |
| 140 |
} |
| 141 |
|
| 142 |
static CK_RV |
| 143 |
create_dsa_private (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, gcry_sexp_t *skey) |
| 144 |
{ |
| 145 |
gcry_error_t gcry; |
| 146 |
gcry_mpi_t p = NULL; |
| 147 |
gcry_mpi_t q = NULL; |
| 148 |
gcry_mpi_t g = NULL; |
| 149 |
gcry_mpi_t y = NULL; |
| 150 |
gcry_mpi_t value = NULL; |
| 151 |
CK_RV ret; |
| 152 |
|
| 153 |
if (!gkm_attributes_find_mpi (attrs, n_attrs, CKA_PRIME, &p) || |
| 154 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_SUBPRIME, &q) || |
| 155 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_BASE, &g) || |
| 156 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_VALUE, &value)) { |
| 157 |
ret = CKR_TEMPLATE_INCOMPLETE; |
| 158 |
goto done; |
| 159 |
} |
| 160 |
|
| 161 |
/* Calculate the public part from the private */ |
| 162 |
y = gcry_mpi_snew (gcry_mpi_get_nbits (value)); |
| 163 |
g_return_val_if_fail (y, CKR_GENERAL_ERROR); |
| 164 |
gcry_mpi_powm (y, g, value, p); |
| 165 |
|
| 166 |
gcry = gcry_sexp_build (skey, NULL, |
| 167 |
"(private-key (dsa (p %m) (q %m) (g %m) (y %m) (x %m)))", |
| 168 |
p, q, g, y, value); |
| 169 |
|
| 170 |
if (gcry != 0) { |
| 171 |
g_message ("couldn't create DSA key from passed attributes: %s", gcry_strerror (gcry)); |
| 172 |
ret = CKR_FUNCTION_FAILED; |
| 173 |
goto done; |
| 174 |
} |
| 175 |
|
| 176 |
gkm_attributes_consume (attrs, n_attrs, CKA_PRIME, CKA_SUBPRIME, |
| 177 |
CKA_BASE, CKA_VALUE, G_MAXULONG); |
| 178 |
ret = CKR_OK; |
| 179 |
|
| 180 |
done: |
| 181 |
gcry_mpi_release (p); |
| 182 |
gcry_mpi_release (q); |
| 183 |
gcry_mpi_release (g); |
| 184 |
gcry_mpi_release (y); |
| 185 |
gcry_mpi_release (value); |
| 186 |
return ret; |
| 187 |
} |
| 188 |
|
| 189 |
static CK_RV |
| 190 |
create_ecdsa_private (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, gcry_sexp_t *skey) |
| 191 |
{ |
| 192 |
gcry_error_t gcry; |
| 193 |
gcry_mpi_t d = NULL; |
| 194 |
const gchar *curve_name, *q_data; |
| 195 |
GBytes *q = NULL; |
| 196 |
gsize q_size; |
| 197 |
GQuark oid; |
| 198 |
CK_RV ret; |
| 199 |
|
| 200 |
if (!gkm_attributes_find_ecc_oid (attrs, n_attrs, &oid) || |
| 201 |
!gkm_attributes_find_ecc_q (attrs, n_attrs, CKA_EC_POINT, &q) || |
| 202 |
!gkm_attributes_find_mpi (attrs, n_attrs, CKA_VALUE, &d)) { |
| 203 |
ret = CKR_TEMPLATE_INCOMPLETE; |
| 204 |
goto done; |
| 205 |
} |
| 206 |
|
| 207 |
curve_name = gkm_data_der_oid_to_curve (oid); |
| 208 |
if (curve_name == NULL) { |
| 209 |
ret = CKR_FUNCTION_FAILED; |
| 210 |
goto done; |
| 211 |
} |
| 212 |
|
| 213 |
q_data = g_bytes_get_data (q, &q_size); |
| 214 |
|
| 215 |
gcry = gcry_sexp_build (skey, NULL, |
| 216 |
"(private-key (ecdsa (curve %s) (q %b) (d %m)))", |
| 217 |
curve_name, q_size, q_data, d); |
| 218 |
|
| 219 |
if (gcry != 0) { |
| 220 |
g_message ("couldn't create ECDSA key from passed attributes: %s", gcry_strerror (gcry)); |
| 221 |
ret = CKR_FUNCTION_FAILED; |
| 222 |
goto done; |
| 223 |
} |
| 224 |
|
| 225 |
gkm_attributes_consume (attrs, n_attrs, CKA_EC_PARAMS, |
| 226 |
CKA_EC_POINT, CKA_VALUE, G_MAXULONG); |
| 227 |
ret = CKR_OK; |
| 228 |
|
| 229 |
done: |
| 230 |
g_bytes_unref (q); |
| 231 |
gcry_mpi_release (d); |
| 232 |
return ret; |
| 233 |
} |
| 234 |
|
| 235 |
static GkmObject* |
| 236 |
factory_create_private_xsa_key (GkmSession *session, GkmTransaction *transaction, |
| 237 |
CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs) |
| 238 |
{ |
| 239 |
GkmPrivateXsaKey *key; |
| 240 |
GkmSexp *sexp; |
| 241 |
|
| 242 |
g_return_val_if_fail (GKM_IS_TRANSACTION (transaction), NULL); |
| 243 |
g_return_val_if_fail (attrs || !n_attrs, NULL); |
| 244 |
|
| 245 |
sexp = gkm_private_xsa_key_create_sexp (session, transaction, attrs, n_attrs); |
| 246 |
if (sexp == NULL) |
| 247 |
return NULL; |
| 248 |
|
| 249 |
key = g_object_new (GKM_TYPE_PRIVATE_XSA_KEY, "base-sexp", sexp, |
| 250 |
"module", gkm_session_get_module (session), |
| 251 |
"manager", gkm_manager_for_template (attrs, n_attrs, session), |
| 252 |
NULL); |
| 253 |
key->pv->sexp = sexp; |
| 254 |
|
| 255 |
gkm_session_complete_object_creation (session, transaction, GKM_OBJECT (key), |
| 256 |
TRUE, attrs, n_attrs); |
| 257 |
return GKM_OBJECT (key); |
| 258 |
} |
| 259 |
|
| 260 |
static gboolean |
| 261 |
acquire_from_credential (GkmCredential *cred, GkmObject *object, gpointer user_data) |
| 262 |
{ |
| 263 |
GkmSexp **result = user_data; |
| 264 |
|
| 265 |
g_assert (result); |
| 266 |
g_assert (!*result); |
| 267 |
|
| 268 |
/* The sexp we stored on the credential */ |
| 269 |
*result = gkm_credential_pop_data (cred, GKM_BOXED_SEXP); |
| 270 |
if (*result != NULL) |
| 271 |
return TRUE; |
| 272 |
|
| 273 |
return FALSE; |
| 274 |
} |
| 275 |
|
| 276 |
static gboolean |
| 277 |
have_from_credential (GkmCredential *cred, GkmObject *object, gpointer unused) |
| 278 |
{ |
| 279 |
/* The sexp we stored on the credential */ |
| 280 |
return gkm_credential_peek_data (cred, GKM_BOXED_SEXP) ? TRUE : FALSE; |
| 281 |
} |
| 282 |
|
| 283 |
/* ----------------------------------------------------------------------------- |
| 284 |
* PRIVATE_XSA_KEY |
| 285 |
*/ |
| 286 |
|
| 287 |
static CK_RV |
| 288 |
gkm_private_xsa_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr) |
| 289 |
{ |
| 290 |
GkmPrivateXsaKey *self = GKM_PRIVATE_XSA_KEY (base); |
| 291 |
gboolean have; |
| 292 |
gint algorithm; |
| 293 |
|
| 294 |
switch (attr->type) { |
| 295 |
case CKA_CLASS: |
| 296 |
return gkm_attribute_set_ulong (attr, CKO_PRIVATE_KEY); |
| 297 |
|
| 298 |
case CKA_PRIVATE: |
| 299 |
return gkm_attribute_set_bool (attr, TRUE); |
| 300 |
|
| 301 |
case CKA_SENSITIVE: |
| 302 |
return gkm_attribute_set_bool (attr, TRUE); |
| 303 |
|
| 304 |
case CKA_DECRYPT: |
| 305 |
algorithm = gkm_sexp_key_get_algorithm (GKM_SEXP_KEY (self)); |
| 306 |
return gkm_attribute_set_bool (attr, algorithm == GCRY_PK_RSA); |
| 307 |
|
| 308 |
case CKA_SIGN: |
| 309 |
return gkm_attribute_set_bool (attr, TRUE); |
| 310 |
|
| 311 |
case CKA_SIGN_RECOVER: |
| 312 |
return gkm_attribute_set_bool (attr, FALSE); |
| 313 |
|
| 314 |
case CKA_UNWRAP: |
| 315 |
return gkm_attribute_set_bool (attr, FALSE); |
| 316 |
|
| 317 |
case CKA_EXTRACTABLE: |
| 318 |
return gkm_attribute_set_bool (attr, FALSE); |
| 319 |
|
| 320 |
case CKA_ALWAYS_SENSITIVE: |
| 321 |
return gkm_attribute_set_bool (attr, FALSE); |
| 322 |
|
| 323 |
case CKA_NEVER_EXTRACTABLE: |
| 324 |
return gkm_attribute_set_bool (attr, FALSE); |
| 325 |
|
| 326 |
case CKA_WRAP_WITH_TRUSTED: |
| 327 |
return gkm_attribute_set_bool (attr, FALSE); |
| 328 |
|
| 329 |
case CKA_UNWRAP_TEMPLATE: |
| 330 |
gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_UNWRAP_TEMPLATE on key"); |
| 331 |
return CKR_ATTRIBUTE_TYPE_INVALID; |
| 332 |
|
| 333 |
case CKA_ALWAYS_AUTHENTICATE: |
| 334 |
have = self->pv->sexp ? TRUE : FALSE; |
| 335 |
if (!have && session) |
| 336 |
have = gkm_credential_for_each (session, base, have_from_credential, NULL); |
| 337 |
return gkm_attribute_set_bool (attr, !have); |
| 338 |
|
| 339 |
case CKA_MODULUS: |
| 340 |
return gkm_sexp_key_set_part (GKM_SEXP_KEY (self), GCRY_PK_RSA, "n", attr); |
| 341 |
|
| 342 |
case CKA_PUBLIC_EXPONENT: |
| 343 |
return gkm_sexp_key_set_part (GKM_SEXP_KEY (self), GCRY_PK_RSA, "e", attr); |
| 344 |
|
| 345 |
/* RSA private key parts */ |
| 346 |
case CKA_PRIVATE_EXPONENT: |
| 347 |
case CKA_PRIME_1: |
| 348 |
case CKA_PRIME_2: |
| 349 |
case CKA_EXPONENT_1: |
| 350 |
case CKA_EXPONENT_2: |
| 351 |
case CKA_COEFFICIENT: |
| 352 |
return CKR_ATTRIBUTE_SENSITIVE; |
| 353 |
|
| 354 |
case CKA_PRIME: |
| 355 |
return gkm_sexp_key_set_part (GKM_SEXP_KEY (self), GCRY_PK_DSA, "p", attr); |
| 356 |
|
| 357 |
case CKA_SUBPRIME: |
| 358 |
return gkm_sexp_key_set_part (GKM_SEXP_KEY (self), GCRY_PK_DSA, "q", attr); |
| 359 |
|
| 360 |
case CKA_BASE: |
| 361 |
return gkm_sexp_key_set_part (GKM_SEXP_KEY (self), GCRY_PK_DSA, "g", attr); |
| 362 |
|
| 363 |
case CKA_EC_POINT: |
| 364 |
return gkm_sexp_key_set_ec_q (GKM_SEXP_KEY (self), GCRY_PK_ECC, attr); |
| 365 |
|
| 366 |
case CKA_EC_PARAMS: |
| 367 |
return gkm_sexp_key_set_ec_params (GKM_SEXP_KEY (self), GCRY_PK_ECC, attr); |
| 368 |
|
| 369 |
/* (EC)DSA private parts */ |
| 370 |
case CKA_VALUE: |
| 371 |
return CKR_ATTRIBUTE_SENSITIVE; |
| 372 |
}; |
| 373 |
|
| 374 |
return GKM_OBJECT_CLASS (gkm_private_xsa_key_parent_class)->get_attribute (base, session, attr); |
| 375 |
} |
| 376 |
|
| 377 |
static GkmSexp* |
| 378 |
gkm_private_xsa_key_real_acquire_crypto_sexp (GkmSexpKey *base, GkmSession *session) |
| 379 |
{ |
| 380 |
GkmPrivateXsaKey *self = GKM_PRIVATE_XSA_KEY (base); |
| 381 |
GkmSexp *sexp = NULL; |
| 382 |
|
| 383 |
/* We have an unlocked private key here */ |
| 384 |
if (self->pv->sexp) |
| 385 |
sexp = gkm_sexp_ref (self->pv->sexp); |
| 386 |
|
| 387 |
/* Find an credential, with an unlocked copy */ |
| 388 |
else |
| 389 |
gkm_credential_for_each (session, GKM_OBJECT (self), |
| 390 |
acquire_from_credential, &sexp); |
| 391 |
|
| 392 |
return sexp; |
| 393 |
} |
| 394 |
|
| 395 |
static void |
| 396 |
gkm_private_xsa_key_init (GkmPrivateXsaKey *self) |
| 397 |
{ |
| 398 |
self->pv = gkm_private_xsa_key_get_instance_private (self); |
| 399 |
} |
| 400 |
|
| 401 |
static void |
| 402 |
gkm_private_xsa_key_dispose (GObject *obj) |
| 403 |
{ |
| 404 |
GkmPrivateXsaKey *self = GKM_PRIVATE_XSA_KEY (obj); |
| 405 |
|
| 406 |
if (self->pv->sexp) |
| 407 |
gkm_sexp_unref (self->pv->sexp); |
| 408 |
self->pv->sexp = NULL; |
| 409 |
|
| 410 |
G_OBJECT_CLASS (gkm_private_xsa_key_parent_class)->dispose (obj); |
| 411 |
} |
| 412 |
|
| 413 |
static void |
| 414 |
gkm_private_xsa_key_finalize (GObject *obj) |
| 415 |
{ |
| 416 |
GkmPrivateXsaKey *self = GKM_PRIVATE_XSA_KEY (obj); |
| 417 |
|
| 418 |
g_assert (self->pv->sexp == NULL); |
| 419 |
|
| 420 |
G_OBJECT_CLASS (gkm_private_xsa_key_parent_class)->finalize (obj); |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
gkm_private_xsa_key_class_init (GkmPrivateXsaKeyClass *klass) |
| 425 |
{ |
| 426 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 427 |
GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass); |
| 428 |
GkmSexpKeyClass *key_class = GKM_SEXP_KEY_CLASS (klass); |
| 429 |
|
| 430 |
gobject_class->dispose = gkm_private_xsa_key_dispose; |
| 431 |
gobject_class->finalize = gkm_private_xsa_key_finalize; |
| 432 |
|
| 433 |
gkm_class->get_attribute = gkm_private_xsa_key_real_get_attribute; |
| 434 |
|
| 435 |
key_class->acquire_crypto_sexp = gkm_private_xsa_key_real_acquire_crypto_sexp; |
| 436 |
} |
| 437 |
|
| 438 |
/* ----------------------------------------------------------------------------- |
| 439 |
* PUBLIC |
| 440 |
*/ |
| 441 |
|
| 442 |
void |
| 443 |
gkm_private_xsa_key_set_unlocked_private (GkmPrivateXsaKey *self, GkmSexp *sexp) |
| 444 |
{ |
| 445 |
g_return_if_fail (GKM_IS_PRIVATE_XSA_KEY (self)); |
| 446 |
g_return_if_fail (sexp); |
| 447 |
|
| 448 |
if (sexp) |
| 449 |
gkm_sexp_ref (sexp); |
| 450 |
if (self->pv->sexp) |
| 451 |
gkm_sexp_unref (self->pv->sexp); |
| 452 |
self->pv->sexp = sexp; |
| 453 |
} |
| 454 |
|
| 455 |
void |
| 456 |
gkm_private_xsa_key_set_locked_private (GkmPrivateXsaKey *self, GkmCredential *cred, |
| 457 |
GkmSexp *sexp) |
| 458 |
{ |
| 459 |
g_return_if_fail (GKM_IS_PRIVATE_XSA_KEY (self)); |
| 460 |
g_return_if_fail (GKM_IS_CREDENTIAL (cred)); |
| 461 |
g_return_if_fail (gkm_credential_get_object (cred) == GKM_OBJECT (self)); |
| 462 |
gkm_credential_set_data (cred, GKM_BOXED_SEXP, sexp); |
| 463 |
} |
| 464 |
|
| 465 |
GkmSexp* |
| 466 |
gkm_private_xsa_key_create_sexp (GkmSession *session, GkmTransaction *transaction, |
| 467 |
CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs) |
| 468 |
{ |
| 469 |
CK_KEY_TYPE type; |
| 470 |
gcry_sexp_t sexp; |
| 471 |
CK_RV ret; |
| 472 |
|
| 473 |
g_return_val_if_fail (GKM_IS_TRANSACTION (transaction), NULL); |
| 474 |
g_return_val_if_fail (attrs || !n_attrs, NULL); |
| 475 |
|
| 476 |
if (!gkm_attributes_find_ulong (attrs, n_attrs, CKA_KEY_TYPE, &type)) { |
| 477 |
gkm_transaction_fail (transaction, CKR_TEMPLATE_INCOMPLETE); |
| 478 |
return NULL; |
| 479 |
} |
| 480 |
|
| 481 |
gkm_attributes_consume (attrs, n_attrs, CKA_KEY_TYPE, CKA_CLASS, G_MAXULONG); |
| 482 |
|
| 483 |
switch (type) { |
| 484 |
case CKK_RSA: |
| 485 |
ret = create_rsa_private (attrs, n_attrs, &sexp); |
| 486 |
break; |
| 487 |
case CKK_DSA: |
| 488 |
ret = create_dsa_private (attrs, n_attrs, &sexp); |
| 489 |
break; |
| 490 |
case CKK_EC: |
| 491 |
ret = create_ecdsa_private (attrs, n_attrs, &sexp); |
| 492 |
break; |
| 493 |
default: |
| 494 |
ret = CKR_ATTRIBUTE_VALUE_INVALID; |
| 495 |
break; |
| 496 |
}; |
| 497 |
|
| 498 |
if (ret != CKR_OK) { |
| 499 |
gkm_transaction_fail (transaction, ret); |
| 500 |
return NULL; |
| 501 |
} |
| 502 |
|
| 503 |
g_return_val_if_fail (sexp, NULL); |
| 504 |
return gkm_sexp_new (sexp); |
| 505 |
} |
| 506 |
|
| 507 |
GkmFactory* |
| 508 |
gkm_private_xsa_key_get_factory (void) |
| 509 |
{ |
| 510 |
static CK_OBJECT_CLASS klass = CKO_PRIVATE_KEY; |
| 511 |
|
| 512 |
static CK_ATTRIBUTE attributes[] = { |
| 513 |
{ CKA_CLASS, &klass, sizeof (klass) } |
| 514 |
}; |
| 515 |
|
| 516 |
static GkmFactory factory = { |
| 517 |
attributes, |
| 518 |
G_N_ELEMENTS (attributes), |
| 519 |
factory_create_private_xsa_key |
| 520 |
}; |
| 521 |
|
| 522 |
return &factory; |
| 523 |
} |