| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-sexp.c: Test sexp stuff |
| 3 |
|
| 4 |
Copyright (C) 2007 Stefan Walter |
| 5 |
|
| 6 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include "mock-module.h" |
| 30 |
|
| 31 |
#include "gkm/gkm-crypto.h" |
| 32 |
#include "gkm/gkm-sexp.h" |
| 33 |
#include "gkm/gkm-private-xsa-key.h" |
| 34 |
#include "gkm/gkm-public-xsa-key.h" |
| 35 |
|
| 36 |
#include "egg/egg-secure-memory.h" |
| 37 |
#include "gkm/gkm-transaction.h" |
| 38 |
|
| 39 |
#include <gcrypt.h> |
| 40 |
|
| 41 |
#define TEST_RSA \ |
| 42 |
"(private-key (rsa " \ |
| 43 |
"(n #00B78758D55EBFFAB61D07D0DC49B5309A6F1DA2AE51C275DFC2370959BB81AC0C39093B1C618E396161A0DECEB8768D0FFB14F197B96C3DA14190EE0F20D51315#)" \ |
| 44 |
"(e #010001#)" \ |
| 45 |
"(d #108BCAC5FDD35812981E6EC5957D98E2AB76E4064C47B861D27C2CC322C50792313C852B4164A035B42D261F1A09F9FFE8F477F9F78FF2EABBDA6BA875C671D7#)" \ |
| 46 |
"(p #00C357F11B19A18C66573D25D1E466D9AB8BCDDCDFE0B2E80BD46712C4BEC18EB7#)" \ |
| 47 |
"(q #00F0843B90A60EF7034CA4BE80414ED9497CABCC685143B388013FF989CBB0E093#)" \ |
| 48 |
"(u #12F2555F52EB56329A991CF0404B51C68AC921AD370A797860F550415FF987BD#)" \ |
| 49 |
"))" |
| 50 |
|
| 51 |
#define TEST_DSA \ |
| 52 |
"(private-key (dsa " \ |
| 53 |
" (p #0090EC0B60735839C754EAF8F64BB03FC35398D69772BFAE540079DEA2D3A61FAFFB27630A038A01A3D0CD62A10745A574A27ECB462F4F0885B79C61BBE954A60A29668AD54BBA5C07A72FD8B1105249670B339DF2C59E64A47064EFCF0B7236C5C72CD55CEB32917430BEC9A003D4E484FBAA84D79571B38D6B5AC95BB73E3F7B#)" \ |
| 54 |
" (q #00FA214A1385C21BFEBAADAB240A2430C607D56271#)" \ |
| 55 |
" (g #2DE05751F5DAEE97F3D43C54595A3E94A080728F0C66C98AEBED5762F6AB155802D8359EAD1DE1EC36A459FBEEEA48E59B9E6A8CB4F5295936B3CC881A5D957C7339175E2CFFE0F30D3711E430DB6648C2EB474AA10A4A3297450531FF2C7C6951220C9D446B6B6B0F00262E1EBEB3CC861476AA518CC555C9ABF9E5F39023FC#)" \ |
| 56 |
" (y #54734451DB79D4EEDF0BBCEBD43BB6CBB7B8584603B957080075DD318EB5B0266D4B20DC5EFF376BDFC4EA2983B1F7F02A39ED4C619ED68712729FFF3B7C696ADD1B6D748F56A4B4BEC5C4385E528423A3B88AE65E6D5500F97839E7A486255982189C3B4FA8D94338C76F0E5CAFC9A30A1ED728BB9F2091D594E3250A09EA00#)" \ |
| 57 |
" (x #00876F84F709D51108DFB0CBFA1F1C569C09C413EC#)))" |
| 58 |
|
| 59 |
#define TEST_ECDSA \ |
| 60 |
"(private-key (ecdsa " \ |
| 61 |
" (curve \"NIST P-256\")" \ |
| 62 |
" (q #04D4F6A6738D9B8D3A7075C1E4EE95015FC0C9B7E4272D2BEB6644D3609FC781B71F9A8072F58CB66AE2F89BB12451873ABF7D91F9E1FBF96BF2F70E73AAC9A283#)" \ |
| 63 |
" (d #5A1EF0035118F19F3110FB81813D3547BCE1E5BCE77D1F744715E1D5BBE70378#)))" |
| 64 |
|
| 65 |
/* test data 20 bytes for DSA */ |
| 66 |
#define TEST_DATA "Test data to sign..." |
| 67 |
#define TEST_DATA_SIZE 20 |
| 68 |
|
| 69 |
|
| 70 |
typedef struct { |
| 71 |
gcry_sexp_t rsakey; |
| 72 |
gcry_sexp_t dsakey; |
| 73 |
gcry_sexp_t ecdsakey; |
| 74 |
GkmModule *module; |
| 75 |
GkmTransaction *transaction; |
| 76 |
} Test; |
| 77 |
|
| 78 |
static void |
| 79 |
setup (Test *test, gconstpointer unused) |
| 80 |
{ |
| 81 |
gcry_error_t gcry; |
| 82 |
|
| 83 |
gkm_crypto_initialize (); |
| 84 |
|
| 85 |
gcry = gcry_sexp_new (&test->rsakey, TEST_RSA, strlen (TEST_RSA), 1); |
| 86 |
g_return_if_fail (gcry == 0); |
| 87 |
gcry = gcry_sexp_new (&test->dsakey, TEST_DSA, strlen (TEST_DSA), 1); |
| 88 |
g_return_if_fail (gcry == 0); |
| 89 |
gcry = gcry_sexp_new (&test->ecdsakey, TEST_ECDSA, strlen (TEST_ECDSA), 1); |
| 90 |
g_return_if_fail (gcry == 0); |
| 91 |
|
| 92 |
/* create a bogus module */ |
| 93 |
test->module = mock_module_initialize_and_enter (); |
| 94 |
|
| 95 |
test->transaction = gkm_transaction_new (); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
teardown (Test *test, gconstpointer unused) |
| 100 |
{ |
| 101 |
g_clear_object (&test->transaction); |
| 102 |
g_clear_pointer (&test->rsakey, gcry_sexp_release); |
| 103 |
g_clear_pointer (&test->dsakey, gcry_sexp_release); |
| 104 |
g_clear_pointer (&test->ecdsakey, gcry_sexp_release); |
| 105 |
|
| 106 |
mock_module_leave_and_finalize (); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
test_parse_key (Test *test, gconstpointer unused) |
| 111 |
{ |
| 112 |
gcry_sexp_t sexp = NULL; |
| 113 |
gcry_mpi_t mpi = NULL; |
| 114 |
gboolean ret; |
| 115 |
gboolean is_priv = FALSE; |
| 116 |
int algorithm = 0; |
| 117 |
|
| 118 |
/* RSA */ |
| 119 |
/* Get the private key out */ |
| 120 |
ret = gkm_sexp_parse_key (test->rsakey, &algorithm, &is_priv, &sexp); |
| 121 |
g_assert (ret); |
| 122 |
g_assert (algorithm == GCRY_PK_RSA); |
| 123 |
g_assert (is_priv == TRUE); |
| 124 |
g_assert (sexp != NULL); |
| 125 |
gcry_sexp_release (sexp); |
| 126 |
|
| 127 |
ret = gkm_sexp_extract_mpi (test->rsakey, &mpi, "p", NULL); |
| 128 |
g_assert (ret); |
| 129 |
g_assert (mpi != NULL); |
| 130 |
gcry_mpi_release (mpi); |
| 131 |
|
| 132 |
/* DSA */ |
| 133 |
/* Get the private key out */ |
| 134 |
ret = gkm_sexp_parse_key (test->dsakey, &algorithm, &is_priv, &sexp); |
| 135 |
g_assert (ret); |
| 136 |
g_assert (algorithm == GCRY_PK_DSA); |
| 137 |
g_assert (is_priv == TRUE); |
| 138 |
g_assert (sexp != NULL); |
| 139 |
gcry_sexp_release (sexp); |
| 140 |
|
| 141 |
ret = gkm_sexp_extract_mpi (test->dsakey, &mpi, "p", NULL); |
| 142 |
g_assert (ret); |
| 143 |
g_assert (mpi != NULL); |
| 144 |
gcry_mpi_release (mpi); |
| 145 |
|
| 146 |
/* ECDSA */ |
| 147 |
/* Get the private key out */ |
| 148 |
ret = gkm_sexp_parse_key (test->ecdsakey, &algorithm, &is_priv, &sexp); |
| 149 |
g_assert (ret); |
| 150 |
g_assert (algorithm == GCRY_PK_ECC); |
| 151 |
g_assert (is_priv == TRUE); |
| 152 |
g_assert (sexp != NULL); |
| 153 |
gcry_sexp_release (sexp); |
| 154 |
|
| 155 |
ret = gkm_sexp_extract_mpi (test->ecdsakey, &mpi, "d", NULL); |
| 156 |
g_assert (ret); |
| 157 |
g_assert (mpi != NULL); |
| 158 |
gcry_mpi_release (mpi); |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
test_key_to_public (Test *test, gconstpointer unused) |
| 163 |
{ |
| 164 |
gcry_sexp_t pubkey = NULL; |
| 165 |
guchar id1[20], id2[20]; |
| 166 |
gboolean ret; |
| 167 |
guchar *p; |
| 168 |
|
| 169 |
/* RSA */ |
| 170 |
ret = gkm_sexp_key_to_public (test->rsakey, &pubkey); |
| 171 |
g_assert (ret); |
| 172 |
g_assert (pubkey != NULL); |
| 173 |
|
| 174 |
p = gcry_pk_get_keygrip (test->rsakey, id1); |
| 175 |
g_return_if_fail (p == id1); |
| 176 |
p = gcry_pk_get_keygrip (pubkey, id2); |
| 177 |
g_return_if_fail (p == id2); |
| 178 |
|
| 179 |
g_assert (memcmp (id1, id2, sizeof (id1)) == 0); |
| 180 |
|
| 181 |
gcry_sexp_release (pubkey); |
| 182 |
|
| 183 |
|
| 184 |
/* DSA */ |
| 185 |
ret = gkm_sexp_key_to_public (test->dsakey, &pubkey); |
| 186 |
g_assert (ret); |
| 187 |
g_assert (pubkey != NULL); |
| 188 |
|
| 189 |
p = gcry_pk_get_keygrip (test->dsakey, id1); |
| 190 |
g_return_if_fail (p == id1); |
| 191 |
p = gcry_pk_get_keygrip (pubkey, id2); |
| 192 |
g_return_if_fail (p == id2); |
| 193 |
|
| 194 |
g_assert (memcmp (id1, id2, sizeof (id1)) == 0); |
| 195 |
|
| 196 |
gcry_sexp_release (pubkey); |
| 197 |
|
| 198 |
|
| 199 |
/* ECDSA */ |
| 200 |
ret = gkm_sexp_key_to_public (test->ecdsakey, &pubkey); |
| 201 |
g_assert (ret); |
| 202 |
g_assert (pubkey != NULL); |
| 203 |
|
| 204 |
p = gcry_pk_get_keygrip (test->ecdsakey, id1); |
| 205 |
g_return_if_fail (p == id1); |
| 206 |
p = gcry_pk_get_keygrip (pubkey, id2); |
| 207 |
g_return_if_fail (p == id2); |
| 208 |
|
| 209 |
g_assert (memcmp (id1, id2, sizeof (id1)) == 0); |
| 210 |
|
| 211 |
gcry_sexp_release (pubkey); |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
test_sign_verify (Test *test, gconstpointer unused) |
| 216 |
{ |
| 217 |
gcry_sexp_t pubkey = NULL; |
| 218 |
gboolean ret; |
| 219 |
CK_BYTE data[] = TEST_DATA; |
| 220 |
CK_ULONG data_size = TEST_DATA_SIZE; |
| 221 |
CK_BYTE signature[128]; |
| 222 |
CK_ULONG signature_size = 128; |
| 223 |
|
| 224 |
/* RSA */ |
| 225 |
/* sign some data */ |
| 226 |
ret = gkm_crypto_sign_xsa (test->rsakey, CKM_RSA_PKCS, data, data_size, signature, &signature_size); |
| 227 |
g_assert (ret == CKR_OK); |
| 228 |
g_assert (signature_size != 0); |
| 229 |
g_assert (signature != NULL); |
| 230 |
|
| 231 |
/* create a public key */ |
| 232 |
ret = gkm_sexp_key_to_public (test->rsakey, &pubkey); |
| 233 |
g_assert (ret); |
| 234 |
g_assert (pubkey != NULL); |
| 235 |
|
| 236 |
/* verify the signature */ |
| 237 |
ret = gkm_crypto_verify_xsa (pubkey, CKM_RSA_PKCS, data, data_size, signature, signature_size); |
| 238 |
g_assert (ret == CKR_OK); |
| 239 |
|
| 240 |
/* reset for the next test */ |
| 241 |
gcry_sexp_release (pubkey); |
| 242 |
signature_size = 512; |
| 243 |
|
| 244 |
/* DSA */ |
| 245 |
/* sign some data */ |
| 246 |
ret = gkm_crypto_sign_xsa (test->dsakey, CKM_DSA, data, data_size, signature, &signature_size); |
| 247 |
g_assert (ret == CKR_OK); |
| 248 |
g_assert (signature_size != 0); |
| 249 |
g_assert (signature != NULL); |
| 250 |
|
| 251 |
/* create a public key */ |
| 252 |
ret = gkm_sexp_key_to_public (test->dsakey, &pubkey); |
| 253 |
g_assert (ret); |
| 254 |
g_assert (pubkey != NULL); |
| 255 |
|
| 256 |
/* verify the signature */ |
| 257 |
ret = gkm_crypto_verify_xsa (pubkey, CKM_DSA, data, data_size, signature, signature_size); |
| 258 |
g_assert (ret == CKR_OK); |
| 259 |
|
| 260 |
/* reset for the next test */ |
| 261 |
gcry_sexp_release (pubkey); |
| 262 |
signature_size = 512; |
| 263 |
|
| 264 |
/* ECDSA */ |
| 265 |
/* sign some data */ |
| 266 |
ret = gkm_crypto_sign_xsa (test->ecdsakey, CKM_ECDSA, data, data_size, signature, &signature_size); |
| 267 |
g_assert (ret == CKR_OK); |
| 268 |
g_assert (signature_size != 0); |
| 269 |
g_assert (signature != NULL); |
| 270 |
|
| 271 |
/* create a public key */ |
| 272 |
ret = gkm_sexp_key_to_public (test->ecdsakey, &pubkey); |
| 273 |
g_assert (ret); |
| 274 |
g_assert (pubkey != NULL); |
| 275 |
|
| 276 |
/* verify the signature */ |
| 277 |
ret = gkm_crypto_verify_xsa (pubkey, CKM_ECDSA, data, data_size, signature, signature_size); |
| 278 |
g_assert (ret == CKR_OK); |
| 279 |
|
| 280 |
gcry_sexp_release (pubkey); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
assert_get_attribute_ulong (GkmPrivateXsaKey *key, CK_ATTRIBUTE_TYPE type, CK_ULONG value, |
| 285 |
CK_ATTRIBUTE_PTR attrs, CK_ULONG_PTR n_attrs) |
| 286 |
{ |
| 287 |
CK_ATTRIBUTE_PTR attr; |
| 288 |
CK_RV ret; |
| 289 |
GkmPrivateXsaKeyClass *key_class; |
| 290 |
GkmObject *base; |
| 291 |
|
| 292 |
base = GKM_OBJECT (key); /* cast */ |
| 293 |
key_class = GKM_PRIVATE_XSA_KEY_GET_CLASS(key); |
| 294 |
|
| 295 |
attr = &attrs[(*n_attrs)++]; |
| 296 |
attr->pValue = g_new (CK_ULONG, 1); |
| 297 |
attr->ulValueLen = sizeof (CK_ULONG); |
| 298 |
|
| 299 |
attr->type = type; |
| 300 |
ret = GKM_OBJECT_CLASS (key_class)->get_attribute (base, NULL, attr); |
| 301 |
g_assert (ret == CKR_OK); |
| 302 |
g_assert (attr->ulValueLen == sizeof (CK_ULONG)); |
| 303 |
g_assert (*( (CK_ULONG *) attr->pValue) == value); |
| 304 |
} |
| 305 |
|
| 306 |
static void |
| 307 |
assert_get_attribute_bool (GkmPrivateXsaKey *key, CK_ATTRIBUTE_TYPE type, CK_BBOOL value, |
| 308 |
CK_ATTRIBUTE_PTR attrs, CK_ULONG_PTR n_attrs) |
| 309 |
{ |
| 310 |
CK_ATTRIBUTE_PTR attr; |
| 311 |
CK_RV ret; |
| 312 |
GkmPrivateXsaKeyClass *key_class; |
| 313 |
GkmObject *base; |
| 314 |
|
| 315 |
base = GKM_OBJECT (key); /* cast */ |
| 316 |
key_class = GKM_PRIVATE_XSA_KEY_GET_CLASS(key); |
| 317 |
|
| 318 |
attr = &attrs[(*n_attrs)++]; |
| 319 |
attr->pValue = g_new (CK_BBOOL, 1); |
| 320 |
attr->ulValueLen = sizeof (CK_BBOOL); |
| 321 |
|
| 322 |
attr->type = type; |
| 323 |
ret = GKM_OBJECT_CLASS (key_class)->get_attribute (base, NULL, attr); |
| 324 |
g_assert (ret == CKR_OK); |
| 325 |
g_assert (attr->ulValueLen == sizeof (CK_BBOOL)); |
| 326 |
g_assert (*( (CK_BBOOL *) attr->pValue) == value); |
| 327 |
} |
| 328 |
|
| 329 |
static void |
| 330 |
assert_get_attribute_buffer (GkmPrivateXsaKey *key, CK_ATTRIBUTE_TYPE type, const gchar *exp, gsize exp_len, |
| 331 |
CK_ATTRIBUTE_PTR attrs, CK_ULONG_PTR n_attrs) |
| 332 |
{ |
| 333 |
CK_ATTRIBUTE_PTR attr; |
| 334 |
CK_RV ret; |
| 335 |
GkmPrivateXsaKeyClass *key_class; |
| 336 |
GkmObject *base; |
| 337 |
|
| 338 |
g_assert (exp_len < 512); |
| 339 |
|
| 340 |
base = GKM_OBJECT (key); /* cast */ |
| 341 |
key_class = GKM_PRIVATE_XSA_KEY_GET_CLASS(key); |
| 342 |
|
| 343 |
attr = &attrs[(*n_attrs)++]; |
| 344 |
attr->pValue = g_new (gchar, 512); |
| 345 |
attr->ulValueLen = 512; |
| 346 |
|
| 347 |
attr->type = type; |
| 348 |
ret = GKM_OBJECT_CLASS (key_class)->get_attribute (base, NULL, attr); |
| 349 |
g_assert (ret == CKR_OK); |
| 350 |
g_assert (attr->ulValueLen == exp_len); |
| 351 |
g_assert (memcmp (attr->pValue, exp, exp_len) == 0); |
| 352 |
} |
| 353 |
|
| 354 |
static void |
| 355 |
assert_get_attribute_error (GkmPrivateXsaKey *key, CK_ATTRIBUTE_TYPE type, CK_RV expect) |
| 356 |
{ |
| 357 |
CK_ATTRIBUTE attr; |
| 358 |
CK_RV ret; |
| 359 |
GkmPrivateXsaKeyClass *key_class; |
| 360 |
GkmObject *base; |
| 361 |
|
| 362 |
base = GKM_OBJECT (key); /* cast */ |
| 363 |
key_class = GKM_PRIVATE_XSA_KEY_GET_CLASS(key); |
| 364 |
|
| 365 |
attr.pValue = NULL; |
| 366 |
attr.ulValueLen = 0; |
| 367 |
|
| 368 |
attr.type = type; |
| 369 |
ret = GKM_OBJECT_CLASS (key_class)->get_attribute (base, NULL, &attr); |
| 370 |
g_assert (ret == expect); |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
assert_sexp_compare_mpi (gcry_sexp_t s1, gcry_sexp_t s2, const gchar *field) |
| 375 |
{ |
| 376 |
gcry_mpi_t m1, m2; |
| 377 |
|
| 378 |
g_assert (gkm_sexp_extract_mpi (s1, &m1, field, NULL)); |
| 379 |
g_assert (gkm_sexp_extract_mpi (s2, &m2, field, NULL)); |
| 380 |
|
| 381 |
g_assert (gcry_mpi_cmp (m1, m2) == 0); |
| 382 |
gcry_mpi_release (m1); |
| 383 |
gcry_mpi_release (m2); |
| 384 |
} |
| 385 |
|
| 386 |
static void |
| 387 |
assert_sexp_compare_bytes (gcry_sexp_t s1, gcry_sexp_t s2, const gchar *field) |
| 388 |
{ |
| 389 |
gchar *b1, *b2; |
| 390 |
gsize bs1, bs2; |
| 391 |
|
| 392 |
g_assert (gkm_sexp_extract_buffer (s1, &b1, &bs1, field, NULL)); |
| 393 |
g_assert (gkm_sexp_extract_buffer (s2, &b2, &bs2, field, NULL)); |
| 394 |
|
| 395 |
g_assert (bs1 == bs2); |
| 396 |
g_assert (memcmp (b1, b2, bs1) == 0); |
| 397 |
g_free (b1); |
| 398 |
g_free (b2); |
| 399 |
} |
| 400 |
|
| 401 |
/* Test sexp -> PKCS#11 attributes */ |
| 402 |
static void |
| 403 |
test_rsa_attributes (Test *test, gconstpointer unused) |
| 404 |
{ |
| 405 |
GkmPrivateXsaKey *key; |
| 406 |
CK_ATTRIBUTE attrs[10]; |
| 407 |
CK_ULONG n_attrs = 0; |
| 408 |
g_autoptr(GkmSexp) sexp = NULL; |
| 409 |
g_autoptr(GkmSexp) base_sexp = NULL; |
| 410 |
|
| 411 |
base_sexp = gkm_sexp_new (test->rsakey); |
| 412 |
key = g_object_new (GKM_TYPE_PRIVATE_XSA_KEY, |
| 413 |
"base-sexp", base_sexp, |
| 414 |
"module", test->module, /*"manager", NULL,*/ NULL); |
| 415 |
g_assert (key != NULL); |
| 416 |
|
| 417 |
assert_get_attribute_ulong (key, CKA_KEY_TYPE, CKK_RSA, attrs, &n_attrs); |
| 418 |
assert_get_attribute_ulong (key, CKA_CLASS, CKO_PRIVATE_KEY, attrs, &n_attrs); |
| 419 |
assert_get_attribute_bool (key, CKA_PRIVATE, TRUE, attrs, &n_attrs); |
| 420 |
assert_get_attribute_bool (key, CKA_SIGN, TRUE, attrs, &n_attrs); |
| 421 |
assert_get_attribute_bool (key, CKA_SIGN_RECOVER, FALSE, attrs, &n_attrs); |
| 422 |
assert_get_attribute_buffer (key, CKA_MODULUS, |
| 423 |
"\xb7\x87\x58\xd5\x5e\xbf\xfa\xb6\x1d\x07\xd0\xdc\x49\xb5\x30\x9a" |
| 424 |
"\x6f\x1d\xa2\xae\x51\xc2\x75\xdf\xc2\x37\x09\x59\xbb\x81\xac\x0c" |
| 425 |
"\x39\x09\x3b\x1c\x61\x8e\x39\x61\x61\xa0\xde\xce\xb8\x76\x8d\x0f" |
| 426 |
"\xfb\x14\xf1\x97\xb9\x6c\x3d\xa1\x41\x90\xee\x0f\x20\xd5\x13\x15", |
| 427 |
64, attrs, &n_attrs); |
| 428 |
assert_get_attribute_buffer (key, CKA_PUBLIC_EXPONENT, |
| 429 |
"\x01\x00\x01", 3, attrs, &n_attrs); |
| 430 |
assert_get_attribute_error (key, CKA_PRIVATE_EXPONENT, CKR_ATTRIBUTE_SENSITIVE); |
| 431 |
|
| 432 |
/* TODO to test parser and reader, there might be more tests */ |
| 433 |
|
| 434 |
/* we have an object so lets recreate the sexp public key */ |
| 435 |
sexp = gkm_public_xsa_key_create_sexp (NULL, test->transaction, attrs, n_attrs); |
| 436 |
g_assert (sexp != NULL); |
| 437 |
|
| 438 |
while (n_attrs > 0) |
| 439 |
g_free (attrs[--n_attrs].pValue); |
| 440 |
|
| 441 |
assert_sexp_compare_mpi (test->rsakey, gkm_sexp_get (sexp), "n"); |
| 442 |
assert_sexp_compare_mpi (test->rsakey, gkm_sexp_get (sexp), "e"); |
| 443 |
|
| 444 |
/* gcry_sexp_dump (gkm_sexp_get (sexp)); */ |
| 445 |
|
| 446 |
g_clear_object (&key); |
| 447 |
/* base_sexp takes ownership, so avoid a dobule free in the test teardown */ |
| 448 |
test->rsakey = NULL; |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
test_dsa_attributes (Test *test, gconstpointer unused) |
| 453 |
{ |
| 454 |
GkmPrivateXsaKey *key; |
| 455 |
CK_ATTRIBUTE attrs[10]; |
| 456 |
CK_ULONG n_attrs = 0; |
| 457 |
g_autoptr(GkmSexp) base_sexp = NULL; |
| 458 |
|
| 459 |
base_sexp = gkm_sexp_new (g_steal_pointer (&test->dsakey)); |
| 460 |
key = g_object_new (GKM_TYPE_PRIVATE_XSA_KEY, |
| 461 |
"base-sexp", base_sexp, |
| 462 |
"module", test->module, /*"manager", NULL,*/ NULL); |
| 463 |
g_assert (key != NULL); |
| 464 |
|
| 465 |
assert_get_attribute_ulong (key, CKA_CLASS, CKO_PRIVATE_KEY, attrs, &n_attrs); |
| 466 |
assert_get_attribute_bool (key, CKA_PRIVATE, TRUE, attrs, &n_attrs); |
| 467 |
assert_get_attribute_bool (key, CKA_SIGN, TRUE, attrs, &n_attrs); |
| 468 |
assert_get_attribute_bool (key, CKA_SIGN_RECOVER, FALSE, attrs, &n_attrs); |
| 469 |
assert_get_attribute_ulong (key, CKA_KEY_TYPE, CKK_DSA, attrs, &n_attrs); |
| 470 |
assert_get_attribute_error (key, CKA_VALUE, CKR_ATTRIBUTE_SENSITIVE); |
| 471 |
assert_get_attribute_buffer (key, CKA_PRIME, |
| 472 |
"\x90\xec\x0b\x60\x73\x58\x39\xc7\x54\xea\xf8\xf6\x4b\xb0\x3f\xc3" |
| 473 |
"\x53\x98\xd6\x97\x72\xbf\xae\x54\x00\x79\xde\xa2\xd3\xa6\x1f\xaf" |
| 474 |
"\xfb\x27\x63\x0a\x03\x8a\x01\xa3\xd0\xcd\x62\xa1\x07\x45\xa5\x74" |
| 475 |
"\xa2\x7e\xcb\x46\x2f\x4f\x08\x85\xb7\x9c\x61\xbb\xe9\x54\xa6\x0a" |
| 476 |
"\x29\x66\x8a\xd5\x4b\xba\x5c\x07\xa7\x2f\xd8\xb1\x10\x52\x49\x67" |
| 477 |
"\x0b\x33\x9d\xf2\xc5\x9e\x64\xa4\x70\x64\xef\xcf\x0b\x72\x36\xc5" |
| 478 |
"\xc7\x2c\xd5\x5c\xeb\x32\x91\x74\x30\xbe\xc9\xa0\x03\xd4\xe4\x84" |
| 479 |
"\xfb\xaa\x84\xd7\x95\x71\xb3\x8d\x6b\x5a\xc9\x5b\xb7\x3e\x3f\x7b", |
| 480 |
128, attrs, &n_attrs); |
| 481 |
assert_get_attribute_buffer (key, CKA_SUBPRIME, |
| 482 |
"\xfa\x21\x4a\x13\x85\xc2\x1b\xfe\xba\xad\xab\x24" |
| 483 |
"\x0a\x24\x30\xc6\x07\xd5\x62\x71", |
| 484 |
20, attrs, &n_attrs); |
| 485 |
assert_get_attribute_buffer (key, CKA_BASE, |
| 486 |
"\x2d\xe0\x57\x51\xf5\xda\xee\x97\xf3\xd4\x3c\x54\x59\x5a\x3e\x94" |
| 487 |
"\xa0\x80\x72\x8f\x0c\x66\xc9\x8a\xeb\xed\x57\x62\xf6\xab\x15\x58" |
| 488 |
"\x02\xd8\x35\x9e\xad\x1d\xe1\xec\x36\xa4\x59\xfb\xee\xea\x48\xe5" |
| 489 |
"\x9b\x9e\x6a\x8c\xb4\xf5\x29\x59\x36\xb3\xcc\x88\x1a\x5d\x95\x7c" |
| 490 |
"\x73\x39\x17\x5e\x2c\xff\xe0\xf3\x0d\x37\x11\xe4\x30\xdb\x66\x48" |
| 491 |
"\xc2\xeb\x47\x4a\xa1\x0a\x4a\x32\x97\x45\x05\x31\xff\x2c\x7c\x69" |
| 492 |
"\x51\x22\x0c\x9d\x44\x6b\x6b\x6b\x0f\x00\x26\x2e\x1e\xbe\xb3\xcc" |
| 493 |
"\x86\x14\x76\xaa\x51\x8c\xc5\x55\xc9\xab\xf9\xe5\xf3\x90\x23\xfc", |
| 494 |
128, attrs, &n_attrs); |
| 495 |
|
| 496 |
/* TODO to test parser and reader, there might be more tests */ |
| 497 |
|
| 498 |
while (n_attrs > 0) |
| 499 |
g_free (attrs[--n_attrs].pValue); |
| 500 |
|
| 501 |
/* can't recreate the public key, because CKA_VALUE is sensitive in private key*/ |
| 502 |
g_clear_object (&key); |
| 503 |
} |
| 504 |
|
| 505 |
static void |
| 506 |
test_ecdsa_attributes (Test *test, gconstpointer unused) |
| 507 |
{ |
| 508 |
GkmPrivateXsaKey *key; |
| 509 |
CK_ATTRIBUTE attrs[10]; |
| 510 |
CK_ULONG n_attrs = 0; |
| 511 |
g_autoptr(GkmSexp) sexp = NULL; |
| 512 |
g_autoptr(GkmSexp) base_sexp = NULL; |
| 513 |
|
| 514 |
base_sexp = gkm_sexp_new (test->ecdsakey); |
| 515 |
key = g_object_new (GKM_TYPE_PRIVATE_XSA_KEY, |
| 516 |
"base-sexp", base_sexp, |
| 517 |
"module", test->module, /*"manager", NULL,*/ NULL); |
| 518 |
g_assert (key != NULL); |
| 519 |
|
| 520 |
assert_get_attribute_ulong (key, CKA_CLASS, CKO_PRIVATE_KEY, attrs, &n_attrs); |
| 521 |
assert_get_attribute_bool (key, CKA_PRIVATE, TRUE, attrs, &n_attrs); |
| 522 |
assert_get_attribute_bool (key, CKA_SIGN, TRUE, attrs, &n_attrs); |
| 523 |
assert_get_attribute_bool (key, CKA_SIGN_RECOVER, FALSE, attrs, &n_attrs); |
| 524 |
assert_get_attribute_ulong (key, CKA_KEY_TYPE, CKK_ECDSA, attrs, &n_attrs); |
| 525 |
assert_get_attribute_error (key, CKA_VALUE, CKR_ATTRIBUTE_SENSITIVE); |
| 526 |
assert_get_attribute_buffer (key, CKA_EC_PARAMS, |
| 527 |
"\x06" /* tag (OID) */ |
| 528 |
"\x08" /* length (8 bytes) */ |
| 529 |
"\x2a\x86\x48\xce\x3d\x03\x01\x07", /* DER encoded OID */ |
| 530 |
10, attrs, &n_attrs); |
| 531 |
assert_get_attribute_buffer (key, CKA_EC_POINT, |
| 532 |
"\x04" /* tag (OCTET STRING) */ |
| 533 |
"\x41" /* length (65 bytes) */ |
| 534 |
"\x04\xd4\xf6\xa6\x73\x8d\x9b\x8d\x3a\x70\x75\xc1\xe4\xee\x95\x01\x5f\xc0\xc9\xb7\xe4\x27\x2d\x2b\xeb\x66\x44\xd3\x60\x9f\xc7\x81\xb7\x1f\x9a\x80\x72\xf5\x8c\xb6\x6a\xe2\xf8\x9b\xb1\x24\x51\x87\x3a\xbf\x7d\x91\xf9\xe1\xfb\xf9\x6b\xf2\xf7\x0e\x73\xaa\xc9\xa2\x83", |
| 535 |
67, attrs, &n_attrs); |
| 536 |
|
| 537 |
/* we have an object so lets recreate the sexp public key */ |
| 538 |
sexp = gkm_public_xsa_key_create_sexp (NULL, test->transaction, attrs, n_attrs); |
| 539 |
g_assert (sexp != NULL); |
| 540 |
|
| 541 |
assert_sexp_compare_bytes (test->ecdsakey, gkm_sexp_get (sexp), "curve"); |
| 542 |
assert_sexp_compare_bytes (test->ecdsakey, gkm_sexp_get (sexp), "q"); |
| 543 |
|
| 544 |
/* gcry_sexp_dump (gkm_sexp_get (sexp)); */ |
| 545 |
|
| 546 |
while (n_attrs > 0) |
| 547 |
g_free (attrs[--n_attrs].pValue); |
| 548 |
|
| 549 |
g_clear_object (&key); |
| 550 |
|
| 551 |
/* base_sexp takes ownership, so avoid a dobule free in the test teardown */ |
| 552 |
test->ecdsakey = NULL; |
| 553 |
} |
| 554 |
|
| 555 |
int |
| 556 |
main (int argc, char **argv) |
| 557 |
{ |
| 558 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 559 |
g_type_init (); |
| 560 |
#endif |
| 561 |
g_test_init (&argc, &argv, NULL); |
| 562 |
|
| 563 |
g_test_add ("/gkm/sexp/parse_key", Test, NULL, setup, test_parse_key, teardown); |
| 564 |
g_test_add ("/gkm/sexp/key_to_public", Test, NULL, setup, test_key_to_public, teardown); |
| 565 |
g_test_add ("/gkm/sexp/sign_verify", Test, NULL, setup, test_sign_verify, teardown); |
| 566 |
g_test_add ("/gkm/sexp/rsa_attributes", Test, NULL, setup, test_rsa_attributes, teardown); |
| 567 |
g_test_add ("/gkm/sexp/dsa_attributes", Test, NULL, setup, test_dsa_attributes, teardown); |
| 568 |
g_test_add ("/gkm/sexp/ecdsa_attributes", Test, NULL, setup, test_ecdsa_attributes, teardown); |
| 569 |
|
| 570 |
return g_test_run (); |
| 571 |
} |