| 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 "gkm-attributes.h" |
| 24 |
#include "gkm-data-der.h" |
| 25 |
#include "gkm-util.h" |
| 26 |
|
| 27 |
#include "egg/egg-timegm.h" |
| 28 |
|
| 29 |
#include <stdarg.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
|
| 35 |
CK_RV |
| 36 |
gkm_attribute_get_bool (CK_ATTRIBUTE_PTR attr, gboolean *value) |
| 37 |
{ |
| 38 |
CK_BBOOL* booly; |
| 39 |
|
| 40 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 41 |
g_return_val_if_fail (value, CKR_GENERAL_ERROR); |
| 42 |
|
| 43 |
if (attr->ulValueLen != sizeof (CK_BBOOL) || attr->pValue == NULL) |
| 44 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 45 |
|
| 46 |
booly = attr->pValue; |
| 47 |
*value = *booly ? TRUE : FALSE; |
| 48 |
return CKR_OK; |
| 49 |
} |
| 50 |
|
| 51 |
CK_RV |
| 52 |
gkm_attribute_get_ulong (CK_ATTRIBUTE_PTR attr, CK_ULONG *value) |
| 53 |
{ |
| 54 |
CK_ULONG* ulong; |
| 55 |
|
| 56 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 57 |
g_return_val_if_fail (value, CKR_GENERAL_ERROR); |
| 58 |
|
| 59 |
if (attr->ulValueLen != sizeof (CK_ULONG) || attr->pValue == NULL) |
| 60 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 61 |
|
| 62 |
ulong = attr->pValue; |
| 63 |
*value = *ulong; |
| 64 |
return CKR_OK; |
| 65 |
} |
| 66 |
|
| 67 |
CK_RV |
| 68 |
gkm_attribute_get_time (CK_ATTRIBUTE_PTR attr, glong *when) |
| 69 |
{ |
| 70 |
struct tm tm; |
| 71 |
gchar buf[15]; |
| 72 |
time_t time; |
| 73 |
|
| 74 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 75 |
g_return_val_if_fail (when, CKR_GENERAL_ERROR); |
| 76 |
|
| 77 |
if (attr->ulValueLen == 0) { |
| 78 |
*when = (glong)-1; |
| 79 |
return CKR_OK; |
| 80 |
} |
| 81 |
|
| 82 |
if (!attr->pValue || attr->ulValueLen != 16) |
| 83 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 84 |
|
| 85 |
memset (&tm, 0, sizeof (tm)); |
| 86 |
memcpy (buf, attr->pValue, 14); |
| 87 |
buf[14] = 0; |
| 88 |
|
| 89 |
if (!strptime(buf, "%Y%m%d%H%M%S", &tm)) |
| 90 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 91 |
|
| 92 |
/* Convert to seconds since epoch */ |
| 93 |
time = timegm (&tm); |
| 94 |
if (time < 0) |
| 95 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 96 |
|
| 97 |
*when = time; |
| 98 |
return CKR_OK; |
| 99 |
} |
| 100 |
|
| 101 |
CK_RV |
| 102 |
gkm_attribute_get_string (CK_ATTRIBUTE_PTR attr, gchar **value) |
| 103 |
{ |
| 104 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 105 |
g_return_val_if_fail (value, CKR_GENERAL_ERROR); |
| 106 |
|
| 107 |
if (attr->ulValueLen == 0) { |
| 108 |
*value = NULL; |
| 109 |
return CKR_OK; |
| 110 |
} |
| 111 |
|
| 112 |
if (!attr->pValue) |
| 113 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 114 |
|
| 115 |
if (!g_utf8_validate (attr->pValue, attr->ulValueLen, NULL)) |
| 116 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 117 |
|
| 118 |
*value = g_strndup (attr->pValue, attr->ulValueLen); |
| 119 |
return CKR_OK; |
| 120 |
} |
| 121 |
|
| 122 |
CK_RV |
| 123 |
gkm_attribute_get_bytes (CK_ATTRIBUTE_PTR attr, GBytes **value) |
| 124 |
{ |
| 125 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 126 |
g_return_val_if_fail (value, CKR_GENERAL_ERROR); |
| 127 |
|
| 128 |
if (attr->ulValueLen == 0) { |
| 129 |
*value = NULL; |
| 130 |
return CKR_OK; |
| 131 |
} |
| 132 |
|
| 133 |
if (!attr->pValue) |
| 134 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 135 |
|
| 136 |
*value = g_bytes_new (attr->pValue, attr->ulValueLen); |
| 137 |
return CKR_OK; |
| 138 |
} |
| 139 |
|
| 140 |
CK_RV |
| 141 |
gkm_attribute_get_mpi (CK_ATTRIBUTE_PTR attr, gcry_mpi_t *value) |
| 142 |
{ |
| 143 |
gcry_error_t gcry; |
| 144 |
|
| 145 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 146 |
g_return_val_if_fail (value, CKR_GENERAL_ERROR); |
| 147 |
|
| 148 |
gcry = gcry_mpi_scan (value, GCRYMPI_FMT_USG, attr->pValue, attr->ulValueLen, NULL); |
| 149 |
if (gcry != 0) |
| 150 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 151 |
|
| 152 |
return CKR_OK; |
| 153 |
} |
| 154 |
|
| 155 |
CK_RV |
| 156 |
gkm_attribute_get_template (CK_ATTRIBUTE_PTR attr, GArray **template) |
| 157 |
{ |
| 158 |
CK_ULONG n_attrs; |
| 159 |
|
| 160 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 161 |
g_return_val_if_fail (attr, CKR_GENERAL_ERROR); |
| 162 |
|
| 163 |
/* Validate everything first */ |
| 164 |
if (attr->ulValueLen % sizeof (CK_ATTRIBUTE) != 0) |
| 165 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 166 |
|
| 167 |
n_attrs = attr->ulValueLen / sizeof (CK_ATTRIBUTE); |
| 168 |
if (n_attrs != 0 && !attr->pValue) |
| 169 |
return CKR_ATTRIBUTE_VALUE_INVALID; |
| 170 |
|
| 171 |
*template = gkm_template_new (attr->pValue, n_attrs); |
| 172 |
return CKR_OK; |
| 173 |
} |
| 174 |
|
| 175 |
CK_RV |
| 176 |
gkm_attribute_set_empty (CK_ATTRIBUTE_PTR attr) |
| 177 |
{ |
| 178 |
return gkm_attribute_set_data (attr, "", 0); |
| 179 |
} |
| 180 |
|
| 181 |
CK_RV |
| 182 |
gkm_attribute_set_bool (CK_ATTRIBUTE_PTR attr, CK_BBOOL value) |
| 183 |
{ |
| 184 |
return gkm_attribute_set_data (attr, &value, sizeof (CK_BBOOL)); |
| 185 |
} |
| 186 |
|
| 187 |
CK_RV |
| 188 |
gkm_attribute_set_ulong (CK_ATTRIBUTE_PTR attr, CK_ULONG value) |
| 189 |
{ |
| 190 |
return gkm_attribute_set_data (attr, &value, sizeof (CK_ULONG)); |
| 191 |
} |
| 192 |
|
| 193 |
CK_RV |
| 194 |
gkm_attribute_set_string (CK_ATTRIBUTE_PTR attr, const gchar* string) |
| 195 |
{ |
| 196 |
return gkm_attribute_set_data (attr, (CK_VOID_PTR)string, |
| 197 |
string ? strlen (string) : 0); |
| 198 |
} |
| 199 |
|
| 200 |
CK_RV |
| 201 |
gkm_attribute_set_date (CK_ATTRIBUTE_PTR attr, time_t time) |
| 202 |
{ |
| 203 |
CK_DATE date; |
| 204 |
struct tm tm; |
| 205 |
gchar buf[16]; |
| 206 |
|
| 207 |
/* 'Empty' date as defined in PKCS#11 */ |
| 208 |
if (time == (time_t)-1) |
| 209 |
return gkm_attribute_set_data (attr, NULL, 0); |
| 210 |
|
| 211 |
if (!attr->pValue) { |
| 212 |
attr->ulValueLen = sizeof (CK_DATE); |
| 213 |
return CKR_OK; |
| 214 |
} |
| 215 |
|
| 216 |
if (!gmtime_r (&time, &tm)) |
| 217 |
g_return_val_if_reached (CKR_GENERAL_ERROR); |
| 218 |
|
| 219 |
g_assert (sizeof (date.year) == 4); |
| 220 |
if (G_UNLIKELY (snprintf ((char*)buf, 5, "%04d", 1900 + tm.tm_year) < 0)) |
| 221 |
abort (); |
| 222 |
memcpy (date.year, buf, 4); |
| 223 |
|
| 224 |
g_assert (sizeof (date.month) == 2); |
| 225 |
if (G_UNLIKELY (snprintf ((char*)buf, 3, "%02d", tm.tm_mon + 1) < 0)) |
| 226 |
abort (); |
| 227 |
memcpy (date.month, buf, 2); |
| 228 |
|
| 229 |
g_assert (sizeof (date.day) == 2); |
| 230 |
if (G_UNLIKELY (snprintf ((char*)buf, 3, "%02d", tm.tm_mday) < 0)) |
| 231 |
abort (); |
| 232 |
memcpy (date.day, buf, 2); |
| 233 |
|
| 234 |
return gkm_attribute_set_data (attr, &date, sizeof (date)); |
| 235 |
} |
| 236 |
|
| 237 |
CK_RV |
| 238 |
gkm_attribute_set_time (CK_ATTRIBUTE_PTR attr, glong when) |
| 239 |
{ |
| 240 |
struct tm tm; |
| 241 |
gchar buf[20]; |
| 242 |
|
| 243 |
/* 'Empty' time as defined in PKCS#11 */ |
| 244 |
if (when == (glong)-1) |
| 245 |
return gkm_attribute_set_data (attr, NULL, 0); |
| 246 |
|
| 247 |
if (!attr->pValue) { |
| 248 |
attr->ulValueLen = 16; |
| 249 |
return CKR_OK; |
| 250 |
} |
| 251 |
|
| 252 |
time_t time = when; |
| 253 |
if (!gmtime_r (&time, &tm)) |
| 254 |
g_return_val_if_reached (CKR_GENERAL_ERROR); |
| 255 |
|
| 256 |
if (!strftime(buf, sizeof (buf), "%Y%m%d%H%M%S00", &tm)) |
| 257 |
g_return_val_if_reached (CKR_GENERAL_ERROR); |
| 258 |
|
| 259 |
return gkm_attribute_set_data (attr, buf, 16); |
| 260 |
} |
| 261 |
|
| 262 |
CK_RV |
| 263 |
gkm_attribute_set_data (CK_ATTRIBUTE_PTR attr, gconstpointer value, gsize n_value) |
| 264 |
{ |
| 265 |
CK_RV rv = gkm_util_return_data (attr->pValue, &(attr->ulValueLen), value, n_value); |
| 266 |
if (rv == CKR_BUFFER_TOO_SMALL) |
| 267 |
attr->ulValueLen = (CK_ULONG)-1; |
| 268 |
return rv; |
| 269 |
} |
| 270 |
|
| 271 |
CK_RV |
| 272 |
gkm_attribute_set_bytes (CK_ATTRIBUTE_PTR attr, |
| 273 |
GBytes *value) |
| 274 |
{ |
| 275 |
return gkm_attribute_set_data (attr, g_bytes_get_data (value, NULL), |
| 276 |
g_bytes_get_size (value)); |
| 277 |
} |
| 278 |
|
| 279 |
CK_RV |
| 280 |
gkm_attribute_set_mpi (CK_ATTRIBUTE_PTR attr, gcry_mpi_t mpi) |
| 281 |
{ |
| 282 |
gsize len; |
| 283 |
gcry_error_t gcry; |
| 284 |
|
| 285 |
g_assert (attr); |
| 286 |
g_assert (mpi); |
| 287 |
|
| 288 |
/* Get the size */ |
| 289 |
gcry = gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &len, mpi); |
| 290 |
g_return_val_if_fail (gcry == 0, CKR_GENERAL_ERROR); |
| 291 |
|
| 292 |
if (!attr->pValue) { |
| 293 |
attr->ulValueLen = len; |
| 294 |
return CKR_OK; |
| 295 |
} |
| 296 |
|
| 297 |
if (len > attr->ulValueLen) { |
| 298 |
attr->ulValueLen = (CK_ULONG)-1; |
| 299 |
return CKR_BUFFER_TOO_SMALL; |
| 300 |
} |
| 301 |
|
| 302 |
/* Write in directly to attribute */ |
| 303 |
gcry = gcry_mpi_print (GCRYMPI_FMT_USG, attr->pValue, len, &len, mpi); |
| 304 |
g_return_val_if_fail (gcry == 0, CKR_GENERAL_ERROR); |
| 305 |
|
| 306 |
attr->ulValueLen = len; |
| 307 |
return CKR_OK; |
| 308 |
} |
| 309 |
|
| 310 |
CK_RV |
| 311 |
gkm_attribute_set_template (CK_ATTRIBUTE_PTR attr, GArray *template) |
| 312 |
{ |
| 313 |
CK_ATTRIBUTE_PTR array; |
| 314 |
CK_ATTRIBUTE_PTR at; |
| 315 |
CK_RV rv; |
| 316 |
gulong len; |
| 317 |
gulong i; |
| 318 |
|
| 319 |
g_assert (attr); |
| 320 |
g_warn_if_fail ((attr->type & CKF_ARRAY_ATTRIBUTE) != 0); |
| 321 |
|
| 322 |
len = sizeof (CK_ATTRIBUTE) * template->len; |
| 323 |
if (!attr->pValue) { |
| 324 |
attr->ulValueLen = len; |
| 325 |
return CKR_OK; |
| 326 |
} else if (len > attr->ulValueLen) { |
| 327 |
attr->ulValueLen = (CK_ULONG)-1; |
| 328 |
return CKR_BUFFER_TOO_SMALL; |
| 329 |
} |
| 330 |
|
| 331 |
attr->ulValueLen = len; |
| 332 |
array = attr->pValue; |
| 333 |
rv = CKR_OK; |
| 334 |
|
| 335 |
/* Start working with individual elements */ |
| 336 |
for (i = 0; i < template->len; ++i) { |
| 337 |
at = &g_array_index (template, CK_ATTRIBUTE, i); |
| 338 |
array[i].type = at->type; |
| 339 |
if (!array[i].pValue) { |
| 340 |
array[i].ulValueLen = at->ulValueLen; |
| 341 |
} else if (array[i].ulValueLen < at->ulValueLen) { |
| 342 |
array[i].ulValueLen = (CK_ULONG)-1; |
| 343 |
rv = CKR_BUFFER_TOO_SMALL; |
| 344 |
} else { |
| 345 |
memcpy(array[i].pValue, at->pValue, at->ulValueLen); |
| 346 |
array[i].ulValueLen = at->ulValueLen; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return rv; |
| 351 |
} |
| 352 |
|
| 353 |
CK_RV |
| 354 |
gkm_attribute_set_checksum (CK_ATTRIBUTE_PTR attr, GChecksumType ctype, |
| 355 |
gconstpointer data, gsize n_data) |
| 356 |
{ |
| 357 |
GChecksum *checksum; |
| 358 |
gssize length; |
| 359 |
gsize result; |
| 360 |
|
| 361 |
g_assert (attr); |
| 362 |
|
| 363 |
g_return_val_if_fail (data, CKR_GENERAL_ERROR); |
| 364 |
g_return_val_if_fail (n_data, CKR_GENERAL_ERROR); |
| 365 |
|
| 366 |
length = g_checksum_type_get_length (ctype); |
| 367 |
g_return_val_if_fail (length > 0, CKR_GENERAL_ERROR); |
| 368 |
|
| 369 |
/* Just asking for the length */ |
| 370 |
if (!attr->pValue) { |
| 371 |
attr->ulValueLen = length; |
| 372 |
return CKR_OK; |
| 373 |
} |
| 374 |
|
| 375 |
/* Buffer is too short */ |
| 376 |
if (length > attr->ulValueLen) { |
| 377 |
attr->ulValueLen = length; |
| 378 |
return CKR_BUFFER_TOO_SMALL; |
| 379 |
} |
| 380 |
|
| 381 |
checksum = g_checksum_new (ctype); |
| 382 |
g_checksum_update (checksum, data, n_data); |
| 383 |
result = attr->ulValueLen; |
| 384 |
g_checksum_get_digest (checksum, attr->pValue, &result); |
| 385 |
g_checksum_free (checksum); |
| 386 |
attr->ulValueLen = result; |
| 387 |
return CKR_OK; |
| 388 |
} |
| 389 |
|
| 390 |
gboolean |
| 391 |
gkm_attribute_equal (gconstpointer v1, gconstpointer v2) |
| 392 |
{ |
| 393 |
const CK_ATTRIBUTE *a1 = v1; |
| 394 |
const CK_ATTRIBUTE *a2 = v2; |
| 395 |
|
| 396 |
g_assert (a1); |
| 397 |
g_assert (a2); |
| 398 |
|
| 399 |
if (a1 == a2) |
| 400 |
return TRUE; |
| 401 |
if (a1->type != a2->type) |
| 402 |
return FALSE; |
| 403 |
if (a1->ulValueLen != a2->ulValueLen) |
| 404 |
return FALSE; |
| 405 |
if (a1->pValue == a2->pValue) |
| 406 |
return TRUE; |
| 407 |
if (a1->ulValueLen == 0) |
| 408 |
return TRUE; |
| 409 |
|
| 410 |
g_assert (a1->pValue); |
| 411 |
g_assert (a2->pValue); |
| 412 |
|
| 413 |
return memcmp (a1->pValue, a2->pValue, a1->ulValueLen) == 0; |
| 414 |
} |
| 415 |
|
| 416 |
guint |
| 417 |
gkm_attribute_hash (gconstpointer v) |
| 418 |
{ |
| 419 |
const CK_ATTRIBUTE *a = v; |
| 420 |
const signed char *p; |
| 421 |
guint i, h; |
| 422 |
|
| 423 |
g_assert (a); |
| 424 |
|
| 425 |
p = (const signed char*)&(a->type); |
| 426 |
h = *p; |
| 427 |
for(i = 0; i < sizeof (CK_ATTRIBUTE_PTR); ++i) |
| 428 |
h = (h << 5) - h + *(p++); |
| 429 |
|
| 430 |
p = a->pValue; |
| 431 |
for(i = 0; i < a->ulValueLen; ++i) |
| 432 |
h = (h << 5) - h + *(p++); |
| 433 |
|
| 434 |
return h; |
| 435 |
} |
| 436 |
|
| 437 |
gboolean |
| 438 |
gkm_attribute_consumed (CK_ATTRIBUTE_PTR attr) |
| 439 |
{ |
| 440 |
g_return_val_if_fail (attr, FALSE); |
| 441 |
return attr->type == (CK_ULONG)-1; |
| 442 |
} |
| 443 |
|
| 444 |
void |
| 445 |
gkm_attribute_consume (CK_ATTRIBUTE_PTR attr) |
| 446 |
{ |
| 447 |
attr->type = (CK_ULONG)-1; |
| 448 |
} |
| 449 |
|
| 450 |
void |
| 451 |
gkm_attributes_consume (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, ...) |
| 452 |
{ |
| 453 |
CK_ATTRIBUTE_TYPE type; |
| 454 |
GArray *types; |
| 455 |
guint i, j; |
| 456 |
va_list va; |
| 457 |
|
| 458 |
/* Convert the var args into an array */ |
| 459 |
types = g_array_new (FALSE, TRUE, sizeof (CK_ATTRIBUTE_TYPE)); |
| 460 |
va_start (va, n_attrs); |
| 461 |
while ((type = va_arg (va, CK_ATTRIBUTE_TYPE)) != G_MAXULONG) |
| 462 |
g_array_append_val (types, type); |
| 463 |
va_end (va); |
| 464 |
|
| 465 |
/* Consume each attribute whose type was in the var args */ |
| 466 |
for (i = 0; i < n_attrs; ++i) { |
| 467 |
if (gkm_attribute_consumed (&attrs[i])) |
| 468 |
continue; |
| 469 |
for (j = 0; j < types->len; ++j) { |
| 470 |
if (attrs[i].type == g_array_index (types, CK_ATTRIBUTE_TYPE, j)) { |
| 471 |
attrs[i].type = (CK_ULONG)-1; |
| 472 |
break; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
g_array_free (types, TRUE); |
| 478 |
} |
| 479 |
|
| 480 |
gboolean |
| 481 |
gkm_attributes_contains (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, CK_ATTRIBUTE_PTR attr) |
| 482 |
{ |
| 483 |
CK_ULONG i; |
| 484 |
|
| 485 |
g_assert (attrs || !n_attrs); |
| 486 |
g_assert (attr); |
| 487 |
|
| 488 |
for (i = 0; i < n_attrs; ++i) { |
| 489 |
if (gkm_attribute_equal (attr, &attrs[i])) |
| 490 |
return TRUE; |
| 491 |
} |
| 492 |
|
| 493 |
return FALSE; |
| 494 |
} |
| 495 |
|
| 496 |
CK_ATTRIBUTE_PTR |
| 497 |
gkm_attributes_find (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, CK_ATTRIBUTE_TYPE type) |
| 498 |
{ |
| 499 |
CK_ULONG i; |
| 500 |
|
| 501 |
g_assert (attrs || !n_attrs); |
| 502 |
|
| 503 |
for (i = 0; i < n_attrs; ++i) { |
| 504 |
if(attrs[i].type == type && attrs[i].ulValueLen != (CK_ULONG)-1) |
| 505 |
return &attrs[i]; |
| 506 |
} |
| 507 |
|
| 508 |
return NULL; |
| 509 |
} |
| 510 |
|
| 511 |
gboolean |
| 512 |
gkm_attributes_find_boolean (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, CK_ATTRIBUTE_TYPE type, gboolean *value) |
| 513 |
{ |
| 514 |
CK_ATTRIBUTE_PTR attr; |
| 515 |
|
| 516 |
g_assert (attrs || !n_attrs); |
| 517 |
|
| 518 |
attr = gkm_attributes_find (attrs, n_attrs, type); |
| 519 |
if (attr == NULL) |
| 520 |
return FALSE; |
| 521 |
|
| 522 |
if (attr->ulValueLen != sizeof (CK_BBOOL)) |
| 523 |
return FALSE; |
| 524 |
|
| 525 |
if (value != NULL) { |
| 526 |
CK_BBOOL bbool; |
| 527 |
memcpy (&bbool, attr->pValue, sizeof (CK_BBOOL)); |
| 528 |
*value = bbool == CK_TRUE ? TRUE : FALSE; |
| 529 |
} |
| 530 |
|
| 531 |
return TRUE; |
| 532 |
} |
| 533 |
|
| 534 |
gboolean |
| 535 |
gkm_attributes_find_ulong (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, CK_ATTRIBUTE_TYPE type, gulong *value) |
| 536 |
{ |
| 537 |
CK_ATTRIBUTE_PTR attr; |
| 538 |
|
| 539 |
g_assert (attrs || !n_attrs); |
| 540 |
|
| 541 |
attr = gkm_attributes_find (attrs, n_attrs, type); |
| 542 |
if (attr == NULL) |
| 543 |
return FALSE; |
| 544 |
|
| 545 |
if (attr->ulValueLen != sizeof (CK_ULONG)) |
| 546 |
return FALSE; |
| 547 |
|
| 548 |
if (value != NULL) { |
| 549 |
CK_ULONG ulong; |
| 550 |
memcpy (&ulong, attr->pValue, sizeof (CK_ULONG)); |
| 551 |
*value = ulong; |
| 552 |
} |
| 553 |
|
| 554 |
return TRUE; |
| 555 |
} |
| 556 |
|
| 557 |
gboolean |
| 558 |
gkm_attributes_find_mpi (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, |
| 559 |
CK_ATTRIBUTE_TYPE type, gcry_mpi_t *value) |
| 560 |
{ |
| 561 |
CK_ATTRIBUTE_PTR attr; |
| 562 |
|
| 563 |
g_assert (attrs || !n_attrs); |
| 564 |
|
| 565 |
attr = gkm_attributes_find (attrs, n_attrs, type); |
| 566 |
if (attr == NULL) |
| 567 |
return FALSE; |
| 568 |
|
| 569 |
return gkm_attribute_get_mpi (attr, value) == CKR_OK; |
| 570 |
} |
| 571 |
|
| 572 |
gboolean |
| 573 |
gkm_attributes_find_string (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, |
| 574 |
CK_ATTRIBUTE_TYPE type, gchar **value) |
| 575 |
{ |
| 576 |
CK_ATTRIBUTE_PTR attr; |
| 577 |
|
| 578 |
g_return_val_if_fail (attrs || !n_attrs, FALSE); |
| 579 |
|
| 580 |
attr = gkm_attributes_find (attrs, n_attrs, type); |
| 581 |
if (attr == NULL) |
| 582 |
return FALSE; |
| 583 |
|
| 584 |
return gkm_attribute_get_string (attr, value) == CKR_OK; |
| 585 |
} |
| 586 |
|
| 587 |
/* Need to get DER encoded EC parameters and point */ |
| 588 |
gboolean |
| 589 |
gkm_attributes_find_bytes (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs, |
| 590 |
CK_ATTRIBUTE_TYPE type, GBytes **value) |
| 591 |
{ |
| 592 |
CK_ATTRIBUTE_PTR attr; |
| 593 |
|
| 594 |
g_return_val_if_fail (attrs || !n_attrs, FALSE); |
| 595 |
|
| 596 |
attr = gkm_attributes_find (attrs, n_attrs, type); |
| 597 |
if (attr == NULL) |
| 598 |
return FALSE; |
| 599 |
|
| 600 |
return gkm_attribute_get_bytes (attr, value) == CKR_OK; |
| 601 |
} |
| 602 |
|
| 603 |
GArray* |
| 604 |
gkm_template_new (CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs) |
| 605 |
{ |
| 606 |
GArray *template = g_array_new (FALSE, FALSE, sizeof (CK_ATTRIBUTE)); |
| 607 |
CK_ATTRIBUTE_PTR pat; |
| 608 |
gulong i; |
| 609 |
|
| 610 |
g_return_val_if_fail (attrs || !n_attrs, NULL); |
| 611 |
|
| 612 |
g_array_append_vals (template, attrs, n_attrs); |
| 613 |
for (i = 0; i < n_attrs; ++i) { |
| 614 |
pat = &g_array_index (template, CK_ATTRIBUTE, i); |
| 615 |
if (pat->pValue) { |
| 616 |
g_return_val_if_fail (pat->ulValueLen != (CK_ULONG)-1, NULL); |
| 617 |
pat->pValue = g_memdup2 (pat->pValue, pat->ulValueLen ? pat->ulValueLen : 1); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
return template; |
| 622 |
} |
| 623 |
|
| 624 |
void |
| 625 |
gkm_template_set (GArray *template, CK_ATTRIBUTE_PTR attr) |
| 626 |
{ |
| 627 |
CK_ATTRIBUTE at; |
| 628 |
guint i; |
| 629 |
|
| 630 |
g_return_if_fail (template); |
| 631 |
g_return_if_fail (attr); |
| 632 |
g_return_if_fail (attr->ulValueLen != (CK_ULONG)-1); |
| 633 |
|
| 634 |
/* Remove any previous value */ |
| 635 |
for (i = 0; i < template->len; ++i) { |
| 636 |
if (g_array_index (template, CK_ATTRIBUTE, i).type == attr->type) { |
| 637 |
g_free (g_array_index (template, CK_ATTRIBUTE, i).pValue); |
| 638 |
g_array_remove_index_fast (template, i); |
| 639 |
break; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
/* Add a new attribute */ |
| 644 |
memcpy (&at, attr, sizeof (at)); |
| 645 |
if (at.pValue) |
| 646 |
at.pValue = g_memdup2 (at.pValue, at.ulValueLen ? at.ulValueLen : 1); |
| 647 |
g_array_append_vals (template, &at, 1); |
| 648 |
} |
| 649 |
|
| 650 |
void |
| 651 |
gkm_template_set_value (GArray *template, CK_ATTRIBUTE_TYPE type, |
| 652 |
CK_VOID_PTR value, CK_ULONG length) |
| 653 |
{ |
| 654 |
CK_ATTRIBUTE attr; |
| 655 |
g_return_if_fail (template); |
| 656 |
|
| 657 |
attr.type = type; |
| 658 |
attr.pValue = value; |
| 659 |
attr.ulValueLen = length; |
| 660 |
gkm_template_set (template, &attr); |
| 661 |
} |
| 662 |
|
| 663 |
void |
| 664 |
gkm_template_set_string (GArray *template, CK_ATTRIBUTE_TYPE type, const gchar *value) |
| 665 |
{ |
| 666 |
CK_ATTRIBUTE attr; |
| 667 |
g_return_if_fail (template); |
| 668 |
g_return_if_fail (value); |
| 669 |
|
| 670 |
attr.type = type; |
| 671 |
attr.pValue = (CK_VOID_PTR)value; |
| 672 |
attr.ulValueLen = strlen (value); |
| 673 |
gkm_template_set (template, &attr); |
| 674 |
|
| 675 |
} |
| 676 |
void |
| 677 |
gkm_template_set_ulong (GArray *template, CK_ATTRIBUTE_TYPE type, CK_ULONG value) |
| 678 |
{ |
| 679 |
CK_ATTRIBUTE attr; |
| 680 |
g_return_if_fail (template); |
| 681 |
|
| 682 |
attr.type = type; |
| 683 |
attr.pValue = &value; |
| 684 |
attr.ulValueLen = sizeof (value); |
| 685 |
gkm_template_set (template, &attr); |
| 686 |
|
| 687 |
} |
| 688 |
|
| 689 |
void |
| 690 |
gkm_template_set_boolean (GArray *template, CK_ATTRIBUTE_TYPE type, CK_BBOOL value) |
| 691 |
{ |
| 692 |
CK_ATTRIBUTE attr; |
| 693 |
g_return_if_fail (template); |
| 694 |
|
| 695 |
attr.type = type; |
| 696 |
attr.pValue = &value; |
| 697 |
attr.ulValueLen = sizeof (value); |
| 698 |
gkm_template_set (template, &attr); |
| 699 |
} |
| 700 |
|
| 701 |
void |
| 702 |
gkm_template_free (GArray *template) |
| 703 |
{ |
| 704 |
guint i; |
| 705 |
|
| 706 |
if (!template) |
| 707 |
return; |
| 708 |
|
| 709 |
for (i = 0; i < template->len; ++i) |
| 710 |
g_free (g_array_index (template, CK_ATTRIBUTE, i).pValue); |
| 711 |
g_array_free (template, TRUE); |
| 712 |
} |
| 713 |
|
| 714 |
CK_ATTRIBUTE_PTR |
| 715 |
gkm_template_find (GArray *template, CK_ATTRIBUTE_TYPE type) |
| 716 |
{ |
| 717 |
g_return_val_if_fail (template, NULL); |
| 718 |
return gkm_attributes_find ((CK_ATTRIBUTE_PTR)template->data, template->len, type); |
| 719 |
} |
| 720 |
|
| 721 |
gboolean |
| 722 |
gkm_template_find_boolean (GArray *template, CK_ATTRIBUTE_TYPE type, gboolean *value) |
| 723 |
{ |
| 724 |
g_return_val_if_fail (template, FALSE); |
| 725 |
return gkm_attributes_find_boolean ((CK_ATTRIBUTE_PTR)template->data, template->len, type, value); |
| 726 |
} |
| 727 |
|
| 728 |
gboolean |
| 729 |
gkm_template_find_ulong (GArray *template, CK_ATTRIBUTE_TYPE type, gulong *value) |
| 730 |
{ |
| 731 |
g_return_val_if_fail (template, FALSE); |
| 732 |
return gkm_attributes_find_ulong ((CK_ATTRIBUTE_PTR)template->data, template->len, type, value); |
| 733 |
} |