diff --git a/tests/t-getattribute.c b/tests/t-getattribute.c index ae319ff..a2be7c0 100644 --- a/tests/t-getattribute.c +++ b/tests/t-getattribute.c @@ -1,602 +1,602 @@ /* t-getattribute.c - Regression test. Copyright (C) 2006, 2007 g10 Code GmbH This file is part of Scute. - + Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include #include #include #include "t-support.h" /* If printable characters should be output "as-is". */ bool printable; CK_RV dump_one (CK_ATTRIBUTE_PTR attr, unsigned char *data, unsigned int max_size) { unsigned int i; int col; if (attr->ulValueLen > max_size) return CKR_GENERAL_ERROR; col = 0; for (i = 0; i < attr->ulValueLen; i++) { if (col == 0) printf (" "); if (printable) { if (isprint (data[i])) { printf ("%c", data[i]); col++; } else { printf ("\\x%02x", data[i]); col += 4; } } else { printf ("%02x", data[i]); col += 2; } if (col >= 64) { printf ("\n"); col = 0; } } if (col) printf ("\n"); return 0; } CK_RV dump_object (CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object) { CK_RV err; CK_OBJECT_CLASS obj_class; CK_ATTRIBUTE attr_class = { CKA_CLASS, &obj_class, sizeof (obj_class) }; err = C_GetAttributeValue (session, object, &attr_class, 1); if (err) return err; printf (" Object Class: %lu = ", obj_class); switch (obj_class) { #define MAX_CERT_LEN 4096 case CKO_CERTIFICATE: { CK_CERTIFICATE_TYPE cert_type; CK_BBOOL cert_token; CK_BBOOL cert_private; CK_BBOOL cert_modifiable; CK_BYTE cert_label[MAX_CERT_LEN]; CK_BBOOL cert_trusted; CK_ULONG cert_cc; CK_BYTE cert_check[3]; CK_DATE cert_sdate; CK_DATE cert_edate; CK_BYTE cert_subject[MAX_CERT_LEN]; CK_BYTE cert_id[MAX_CERT_LEN]; CK_BYTE cert_issuer[MAX_CERT_LEN]; CK_BYTE cert_serial[MAX_CERT_LEN]; CK_BYTE cert_value[MAX_CERT_LEN]; CK_ULONG cert_jm; /* Note that the order is encoded below in the various length checks. */ CK_ATTRIBUTE cert_attr[] = { { CKA_CERTIFICATE_TYPE, &cert_type, sizeof (cert_type) }, { CKA_TOKEN, &cert_token, sizeof (cert_token) }, { CKA_PRIVATE, &cert_private, sizeof (cert_private) }, { CKA_MODIFIABLE, &cert_modifiable, sizeof (cert_modifiable) }, { CKA_LABEL, &cert_label, sizeof (cert_label) }, { CKA_TRUSTED, &cert_trusted, sizeof (cert_trusted) }, { CKA_CERTIFICATE_CATEGORY, &cert_cc, sizeof (cert_cc) }, { CKA_CHECK_VALUE, &cert_check, sizeof (cert_check) }, { CKA_START_DATE, &cert_sdate, sizeof (cert_sdate) }, { CKA_END_DATE, &cert_edate, sizeof (cert_edate) }, { CKA_SUBJECT, &cert_subject, sizeof (cert_subject) }, { CKA_ID, &cert_id, sizeof (cert_id) }, { CKA_ISSUER, &cert_issuer, sizeof (cert_issuer) }, { CKA_SERIAL_NUMBER, &cert_serial, sizeof (cert_serial) }, { CKA_VALUE, cert_value, sizeof (cert_value) }, { CKA_URL, NULL, 0 }, { CKA_HASH_OF_SUBJECT_PUBLIC_KEY, NULL, 0 }, { CKA_HASH_OF_ISSUER_PUBLIC_KEY, NULL, 0 }, { CKA_JAVA_MIDP_SECURITY_DOMAIN, &cert_jm, sizeof (cert_jm) } }; printf ("CKO_CERTIFICATE\n"); err = C_GetAttributeValue (session, object, cert_attr, DIM (cert_attr)); if (err) return err; fail_if_err ((cert_attr[0].ulValueLen != sizeof (cert_type)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Type: %lu = ", cert_type); switch (cert_type) { case CKC_X_509: printf ("CKC_X_509"); break; case CKC_WTLS: printf ("CKC_WTLS"); break; case CKC_X_509_ATTR_CERT: printf ("CKC_X_509_ATTR_CERT"); break; default: printf ("(unknown"); break; } printf ("\n"); fail_if_err ((cert_attr[1].ulValueLen != sizeof (cert_token)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Token: %s\n", cert_token ? "true" : "false"); fail_if_err ((cert_attr[2].ulValueLen != sizeof (cert_private)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Private: %s\n", cert_private ? "true" : "false"); fail_if_err ((cert_attr[3].ulValueLen != sizeof (cert_modifiable)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Modifiable: %s\n", cert_modifiable ? "true" : "false"); printf (" Certificate Label: Length %lu\n", cert_attr[4].ulValueLen); err = dump_one (&cert_attr[4], cert_label, sizeof (cert_label)); fail_if_err (err); fail_if_err ((cert_attr[5].ulValueLen != sizeof (cert_trusted)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Trusted: %s\n", cert_trusted ? "true" : "false"); fail_if_err ((cert_attr[6].ulValueLen != sizeof (cert_cc)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Category: %lu = ", cert_cc); switch (cert_cc) { case 0: printf ("unspecified"); break; case 1: printf ("token user"); break; case 2: printf ("authority"); break; case 3: printf ("other entity"); break; default: printf ("(unknown)"); break; } printf ("\n"); fail_if_err ((cert_attr[7].ulValueLen != sizeof (cert_check)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Check Value: %02x%02x%02x\n", cert_check[0], cert_check[1], cert_check[2]); if (cert_attr[8].ulValueLen && cert_attr[9].ulValueLen) { fail_if_err ((cert_attr[8].ulValueLen != sizeof (cert_sdate)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Start Date: %.4s/%.2s/%.2s\n", cert_sdate.year, cert_sdate.month, cert_sdate.day); fail_if_err ((cert_attr[9].ulValueLen != sizeof (cert_edate)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate End Date: %.4s/%.2s/%.2s\n", cert_edate.year, cert_edate.month, cert_edate.day); } printf (" Certificate Subject: Length %lu\n", cert_attr[10].ulValueLen); err = dump_one (&cert_attr[10], cert_subject, sizeof (cert_subject)); fail_if_err (err); printf (" Certificate ID: Length %lu\n", cert_attr[11].ulValueLen); err = dump_one (&cert_attr[11], cert_id, sizeof (cert_id)); fail_if_err (err); printf (" Certificate Issuer: Length %lu\n", cert_attr[12].ulValueLen); err = dump_one (&cert_attr[12], cert_issuer, sizeof (cert_issuer)); fail_if_err (err); printf (" Certificate Serial Number: Length %lu\n", cert_attr[13].ulValueLen); err = dump_one (&cert_attr[13], cert_serial, sizeof (cert_serial)); fail_if_err (err); printf (" Certificate Value: Length %lu\n", cert_attr[14].ulValueLen); err = dump_one (&cert_attr[14], cert_value, sizeof (cert_value)); fail_if_err (err); fail_if_err ((cert_attr[15].ulValueLen != 0) ? CKR_GENERAL_ERROR : 0); fail_if_err ((cert_attr[16].ulValueLen != 0) ? CKR_GENERAL_ERROR : 0); fail_if_err ((cert_attr[17].ulValueLen != 0) ? CKR_GENERAL_ERROR : 0); fail_if_err ((cert_attr[18].ulValueLen != sizeof (cert_jm)) ? CKR_GENERAL_ERROR : 0); printf (" Certificate Java MIDP Security Domain: %lu = ", cert_jm); switch (cert_jm) { case 0: printf ("unspecified"); break; case 1: printf ("manufacturer"); break; case 2: printf ("operator"); break; case 3: printf ("third party"); break; default: printf ("(unknown)"); break; } printf ("\n"); } break; case CKO_PRIVATE_KEY: { CK_KEY_TYPE key_type; CK_BBOOL key_token; CK_BBOOL key_private; CK_BBOOL key_modifiable; CK_BYTE key_label[MAX_CERT_LEN]; CK_BYTE key_id[MAX_CERT_LEN]; CK_DATE key_sdate; CK_DATE key_edate; CK_BBOOL key_derive; CK_BBOOL key_local; CK_MECHANISM_TYPE key_gen; CK_MECHANISM_TYPE key_mechanisms[1]; /* FIXME, hard-coded constant. */ CK_BYTE key_subject[MAX_CERT_LEN]; CK_BBOOL key_sensitive; CK_BBOOL key_decrypt; CK_BBOOL key_sign; CK_BBOOL key_sign_recover; CK_BBOOL key_unwrap; CK_BBOOL key_extractable; CK_BBOOL key_always_sensitive; CK_BBOOL key_never_extractable; CK_BBOOL key_wrap_with_trusted; CK_BBOOL key_always_authenticate; CK_BYTE key_modulus[MAX_CERT_LEN]; CK_BYTE key_public_exp[MAX_CERT_LEN]; /* Note that the order is encoded below in the various length checks. */ CK_ATTRIBUTE key_attr[] = { { CKA_KEY_TYPE, &key_type, sizeof (key_type) }, { CKA_TOKEN, &key_token, sizeof (key_token) }, { CKA_PRIVATE, &key_private, sizeof (key_private) }, { CKA_MODIFIABLE, &key_modifiable, sizeof (key_modifiable) }, { CKA_LABEL, &key_label, sizeof (key_label) }, { CKA_ID, &key_id, sizeof (key_id) }, { CKA_START_DATE, &key_sdate, sizeof (key_sdate) }, { CKA_END_DATE, &key_edate, sizeof (key_edate) }, { CKA_DERIVE, &key_derive, sizeof (key_derive) }, { CKA_LOCAL, &key_local, sizeof (key_local) }, { CKA_KEY_GEN_MECHANISM, &key_gen, sizeof (key_gen) }, { CKA_ALLOWED_MECHANISMS, &key_mechanisms, sizeof (key_mechanisms) }, { CKA_SUBJECT, &key_subject, sizeof (key_subject) }, { CKA_SENSITIVE, &key_sensitive, sizeof (key_sensitive) }, { CKA_DECRYPT, &key_decrypt, sizeof (key_decrypt) }, { CKA_SIGN, &key_sign, sizeof (key_sign) }, { CKA_SIGN_RECOVER, &key_sign_recover, sizeof (key_sign_recover) }, { CKA_UNWRAP, &key_unwrap, sizeof (key_unwrap) }, { CKA_EXTRACTABLE, &key_extractable, sizeof (key_extractable) }, { CKA_ALWAYS_SENSITIVE, &key_always_sensitive, sizeof (key_always_sensitive) }, { CKA_NEVER_EXTRACTABLE, &key_never_extractable, sizeof (key_never_extractable) }, { CKA_WRAP_WITH_TRUSTED, &key_wrap_with_trusted, sizeof (key_wrap_with_trusted) }, { CKA_UNWRAP_TEMPLATE, NULL, 0 }, { CKA_ALWAYS_AUTHENTICATE, &key_always_authenticate, sizeof (key_always_authenticate) }, { CKA_MODULUS, &key_modulus, sizeof (key_modulus) }, { CKA_PUBLIC_EXPONENT, &key_public_exp, sizeof (key_public_exp) } }; printf ("CKO_PRIVATE_KEY\n"); err = C_GetAttributeValue (session, object, key_attr, DIM (key_attr)); if (err) return err; fail_if_err ((key_attr[0].ulValueLen != sizeof (key_type)) ? CKR_GENERAL_ERROR : 0); printf (" Key Type: %lu = ", key_type); switch (key_type) { case CKK_RSA: printf ("CKK_RSA"); break; case CKK_DSA: printf ("CKK_DSA"); break; default: printf ("(unknown"); break; } printf ("\n"); fail_if_err ((key_attr[1].ulValueLen != sizeof (key_token)) ? CKR_GENERAL_ERROR : 0); printf (" Key Token: %s\n", key_token ? "true" : "false"); fail_if_err ((key_attr[2].ulValueLen != sizeof (key_private)) ? CKR_GENERAL_ERROR : 0); printf (" Key Private: %s\n", key_private ? "true" : "false"); fail_if_err ((key_attr[3].ulValueLen != sizeof (key_modifiable)) ? CKR_GENERAL_ERROR : 0); printf (" Key Modifiable: %s\n", key_modifiable ? "true" : "false"); printf (" Key Label: Length %lu\n", key_attr[4].ulValueLen); err = dump_one (&key_attr[4], key_label, sizeof (key_label)); fail_if_err (err); printf (" Key ID: Length %lu\n", key_attr[5].ulValueLen); err = dump_one (&key_attr[5], key_id, sizeof (key_id)); fail_if_err (err); if (key_attr[6].ulValueLen && key_attr[7].ulValueLen) { fail_if_err ((key_attr[6].ulValueLen != sizeof (key_sdate)) ? CKR_GENERAL_ERROR : 0); printf (" Key Start Date: %.4s/%.2s/%.2s\n", key_sdate.year, key_sdate.month, key_sdate.day); - + fail_if_err ((key_attr[7].ulValueLen != sizeof (key_edate)) ? CKR_GENERAL_ERROR : 0); printf (" Key End Date: %.4s/%.2s/%.2s\n", key_edate.year, key_edate.month, key_edate.day); } fail_if_err ((key_attr[8].ulValueLen != sizeof (key_derive)) ? CKR_GENERAL_ERROR : 0); printf (" Key Derive: %s\n", key_derive ? "true" : "false"); fail_if_err ((key_attr[9].ulValueLen != sizeof (key_local)) ? CKR_GENERAL_ERROR : 0); printf (" Key Local: %s\n", key_local ? "true" : "false"); fail_if_err ((key_attr[10].ulValueLen != sizeof (key_gen)) ? CKR_GENERAL_ERROR : 0); /* FIXME: Print Mechanism. */ printf (" Key Gen Mechanism: %lu\n", key_gen); /* FIXME: Print supported mechanisms. 11 */ printf (" Key Subject: Length %lu\n", key_attr[12].ulValueLen); err = dump_one (&key_attr[12], key_subject, sizeof (key_subject)); fail_if_err (err); fail_if_err ((key_attr[13].ulValueLen != sizeof (key_sensitive)) ? CKR_GENERAL_ERROR : 0); printf (" Key Sensitive: %s\n", key_sensitive ? "true" : "false"); fail_if_err ((key_attr[14].ulValueLen != sizeof (key_decrypt)) ? CKR_GENERAL_ERROR : 0); printf (" Key Decrypt: %s\n", key_decrypt ? "true" : "false"); fail_if_err ((key_attr[15].ulValueLen != sizeof (key_sign)) ? CKR_GENERAL_ERROR : 0); printf (" Key Sign: %s\n", key_sign ? "true" : "false"); fail_if_err ((key_attr[16].ulValueLen != sizeof (key_sign_recover)) ? CKR_GENERAL_ERROR : 0); printf (" Key Sign Recover: %s\n", key_sign_recover ? "true" : "false"); fail_if_err ((key_attr[17].ulValueLen != sizeof (key_unwrap)) ? CKR_GENERAL_ERROR : 0); printf (" Key Unwrap: %s\n", key_unwrap ? "true" : "false"); fail_if_err ((key_attr[18].ulValueLen != sizeof (key_extractable)) ? CKR_GENERAL_ERROR : 0); printf (" Key Extractable: %s\n", key_extractable ? "true" : "false"); fail_if_err ((key_attr[19].ulValueLen != sizeof (key_always_sensitive)) ? CKR_GENERAL_ERROR : 0); printf (" Key Always Sensitive: %s\n", key_always_sensitive ? "true" : "false"); fail_if_err ((key_attr[20].ulValueLen != sizeof (key_never_extractable)) ? CKR_GENERAL_ERROR : 0); printf (" Key Never Extractable: %s\n", key_never_extractable ? "true" : "false"); fail_if_err ((key_attr[21].ulValueLen != sizeof (key_wrap_with_trusted)) ? CKR_GENERAL_ERROR : 0); printf (" Key Wrap With Trusted: %s\n", key_wrap_with_trusted ? "true" : "false"); fail_if_err ((key_attr[22].ulValueLen != 0) ? CKR_GENERAL_ERROR : 0); fail_if_err ((key_attr[23].ulValueLen != sizeof (key_always_authenticate)) ? CKR_GENERAL_ERROR : 0); printf (" Key Always Authenticate: %s\n", key_always_authenticate ? "true" : "false"); printf (" Key Modulus: Length %lu\n", key_attr[24].ulValueLen); err = dump_one (&key_attr[24], key_modulus, sizeof (key_modulus)); fail_if_err (err); printf (" Key Subject: Length %lu\n", key_attr[25].ulValueLen); err = dump_one (&key_attr[25], key_public_exp, sizeof (key_public_exp)); fail_if_err (err); } break; default: printf ("(unknown)\n"); } return 0; } int main (int argc, char *argv[]) { CK_RV err; CK_SLOT_ID_PTR slots; CK_ULONG slots_count; unsigned int i; (void) argc; (void) argv; if (argc > 1 && !strcmp ("--printable", argv[1])) printable = true; - + init_cryptoki (); err = C_GetSlotList (true, NULL, &slots_count); fail_if_err (err); if (slots_count == 0) { printf ("Skipping test because no token is present.\n"); return 77; } printf ("Number of slots with tokens: %lu\n", slots_count); slots = malloc (sizeof (CK_SLOT_ID) * slots_count); if (!slots) fail_if_err (CKR_HOST_MEMORY); err = C_GetSlotList (true, slots, &slots_count); fail_if_err (err); for (i = 0; i < slots_count; i++) { CK_SESSION_HANDLE session; CK_OBJECT_HANDLE object; CK_ULONG count; printf ("%2i. Slot ID %lu\n", i, slots[i]); err = C_OpenSession (slots[i], CKF_SERIAL_SESSION, NULL, NULL, &session); fail_if_err (err); - + printf (" Session ID: %lu\n", session); err = C_FindObjectsInit (session, NULL, 0); fail_if_err (err); do { err = C_FindObjects (session, &object, 1, &count); fail_if_err (err); if (count) { printf (" Object Handle: %lu\n", object); err = dump_object (session, object); fail_if_err (err); } } while (count); err = C_FindObjectsFinal (session); fail_if_err (err); err = C_CloseSession (session); fail_if_err (err); } return 0; } diff --git a/tests/t-getsessioninfo.c b/tests/t-getsessioninfo.c index 1d4d902..92746e5 100644 --- a/tests/t-getsessioninfo.c +++ b/tests/t-getsessioninfo.c @@ -1,126 +1,126 @@ /* t-getsessioninfo.c - Regression test. Copyright (C) 2006 g10 Code GmbH This file is part of Scute. - + Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include #include "t-support.h" int main (int argc, char *argv[]) { CK_RV err; CK_SLOT_ID_PTR slots; CK_SESSION_HANDLE_PTR sessions; CK_ULONG slots_count; unsigned int i; (void) argc; (void) argv; init_cryptoki (); err = C_GetSlotList (true, NULL, &slots_count); fail_if_err (err); if (slots_count == 0) { printf ("Skipping test because no token is present.\n"); return 77; } printf ("Number of slots with tokens: %lu\n", slots_count); slots = malloc (sizeof (CK_SLOT_ID) * slots_count); if (!slots) fail_if_err (CKR_HOST_MEMORY); sessions = malloc (sizeof (CK_SESSION_HANDLE) * slots_count); if (!sessions) fail_if_err (CKR_HOST_MEMORY); err = C_GetSlotList (true, slots, &slots_count); fail_if_err (err); for (i = 0; i < slots_count; i++) { CK_SESSION_INFO info; printf ("%2i. Slot ID %lu\n", i, slots[i]); err = C_OpenSession (slots[i], CKF_SERIAL_SESSION, NULL, NULL, &sessions[i]); fail_if_err (err); printf (" Session ID: %lu\n", sessions[i]); err = C_GetSessionInfo (sessions[i], &info); fail_if_err (err); printf (" Slot ID: %lu\n", info.slotID); printf (" State: %s\n", session_state_str (info.state)); printf (" Flags: %#lx", info.flags); if (info.flags) { bool any = false; CK_FLAGS xflags = 0; printf (" == "); #define DO_FLAG(sym) \ if (info.flags & sym) \ { \ printf ("%s" #sym, any ? " | " : ""); \ any = true; \ xflags |= sym; \ } DO_FLAG (CKF_RW_SESSION); DO_FLAG (CKF_SERIAL_SESSION); - + xflags = info.flags & ~xflags; if (xflags) printf ("%s%#lx", any ? " | " : "", xflags); } printf ("\n"); printf (" Device Error: %lu\n", info.ulDeviceError); fail_if_err (info.slotID != slots[i] ? CKR_GENERAL_ERROR : 0); fail_if_err (info.state != CKS_RO_PUBLIC_SESSION ? CKR_GENERAL_ERROR : 0); fail_if_err (info.flags != CKF_SERIAL_SESSION ? CKR_GENERAL_ERROR : 0); fail_if_err (info.ulDeviceError ? CKR_GENERAL_ERROR : 0); } for (i = 0; i < slots_count; i++) { err = C_CloseSession (sessions[i]); fail_if_err (err); } return 0; } diff --git a/tests/t-support.h b/tests/t-support.h index a2cfcf2..7ae1da7 100644 --- a/tests/t-support.h +++ b/tests/t-support.h @@ -1,281 +1,281 @@ /* t-support.h - Helper routines for regression tests. Copyright (C) 2006 g10 Code GmbH This file is part of Scute. - + Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include #include #include #include #define DIM(x) (sizeof (x) / sizeof (x[0])) /* Check for compiler features. */ #if __GNUC__ #define _GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) #if _GCC_VERSION > 30100 #define UNUSED __attribute__ ((__unused__)) #endif #endif #ifndef UNUSED #define UNUSED #endif const char *msg[] = { "OK", "Cancel", "Host memory", "Slot ID invalid", "Flags invalid", "General error", "Function failed", "Arguments bad", "No event", "Need to create threads", "Can't lock", "0x0000000b", "0x0000000c", "0x0000000d", "0x0000000e", "0x0000000f", "Attribute read only", "Attribute sensitive", "Attribute type invalid", "Attribute value invalid", "0x00000014", "0x00000015", "0x00000016", "0x00000017", "0x00000018", "0x00000019", "0x0000001a", "0x0000001b", "0x0000001c", "0x0000001d", "0x0000001e", "0x0000001f", "Data invalid", "Data length range", "0x00000022", "0x00000023", "0x00000024", "0x00000025", "0x00000026", "0x00000027", "0x00000028", "0x00000029", "0x0000002a", "0x0000002b", "0x0000002c", "0x0000002d", "0x0000002e", "0x0000002f", "Device error", "Device memory", "Devire removed", "0x00000033", "0x00000034", "0x00000035", "0x00000036", "0x00000037", "0x00000038", "0x00000039", "0x0000003a", "0x0000003b", "0x0000003c", "0x0000003d", "0x0000003e", "0x0000003f", "Encrypted data invalid", "Encrypted data length range", "0x00000042", "0x00000043", "0x00000044", "0x00000045", "0x00000046", "0x00000047", "0x00000048", "0x00000049", "0x0000004a", "0x0000004b", "0x0000004c", "0x0000004d", "0x0000004e", "0x0000004f", "Function canceled", "Function not parallel", "0x00000052", "0x00000053", "Function not supported", "0x00000055", "0x00000056", "0x00000057", "0x00000058", "0x00000059", "0x0000005a", "0x0000005b", "0x0000005c", "0x0000005d", "0x0000005e", "0x0000005f", "Key handle invalid", "Key sensitive", "Key size range", "Key type inconsistent", "Key not needed", "Key changed", "Key needed", "Key indigestible", "Key function not permitted", "Key not wrappable", "Key unextractable", "0x0000006b", "0x0000006c", "0x0000006d", "0x0000006e", "0x0000006f", "Mechanism invalid", "Mechanism parameter invalid", "0x00000072", "0x00000073", "0x00000074", "0x00000075", "0x00000076", "0x00000077", "0x00000078", "0x00000079", "0x0000007a", "0x0000007b", "0x0000007c", "0x0000007d", "0x0000007e", "0x0000007f", "Object class inconsistent", "Object class invalid", "Object handle invalid", "0x00000083", "0x00000084", "0x00000085", "0x00000086", "0x00000087", "0x00000088", "0x00000089", "0x0000008a", "0x0000008b", "0x0000008c", "0x0000008d", "0x0000008e", "0x0000008f", "Operation active", "Operation not initialized", "0x00000092", "0x00000093", "0x00000094", "0x00000095", "0x00000096", "0x00000097", "0x00000098", "0x00000099", "0x0000009a", "0x0000009b", "0x0000009c", "0x0000009d", "0x0000009e", "0x0000009f", "PIN incorrect", "PIN invalid", "PIN length range", "PIN expired", "PIN locked", "0x000000a5", "0x000000a6", "0x000000a7", "0x000000a8", "0x000000a9", "0x000000aa", "0x000000ab", "0x000000ac", "0x000000ad", "0x000000ae", "0x000000af", "Session closed", "Session count", "0x000000b2", "Session handle invalid", "Session parallel not supported", "Session read only", "Session exists", "Session read only exists", "Session read write SO exists", "0x000000b9", "0x000000ba", "0x000000bb", "0x000000bc", "0x000000bd", "0x000000be", "0x000000bf", "Signature invalid", "Signature length range", "0x000000c2", "0x000000c3", "0x000000c4", "0x000000c5", "0x000000c6", "0x000000c7", "0x000000c8", "0x000000c9", "0x000000ca", "0x000000cb", "0x000000cc", "0x000000cd", "0x000000ce", "0x000000cf", "Template incomplete", "Template inconsistent", "0x000000d2", "0x000000d3", "0x000000d4", "0x000000d5", "0x000000d6", "0x000000d7", "0x000000d8", "0x000000d9", "0x000000da", "0x000000db", "0x000000dc", "0x000000dd", "0x000000de", "0x000000df", "Token not present", "Token not recognized", "Token write protected", "0x000000e3", "0x000000e4", "0x000000e5", "0x000000e6", "0x000000e7", "0x000000e8", "0x000000e9", "0x000000ea", "0x000000eb", "0x000000ec", "0x000000ed", "0x000000ee", "0x000000ef", "Unwrapping key handle invalid", "Unwrapping key size range", "Unwrapping key type inconsistent", "0x000000f3", "0x000000f4", "0x000000f5", "0x000000f6", "0x000000f7", "0x000000f8", "0x000000f9", "0x000000fa", "0x000000fb", "0x000000fc", "0x000000fd", "0x000000fe", "0x000000ff", "User already logged in", "User not logged in", "User PIN not initialized", "User type invalid", "Another user already logged in", "User too many types", "0x00000106", "0x00000107", "0x00000108", "0x00000109", "0x0000010a", "0x0000010b", "0x0000010c", "0x0000010d", "0x0000010e", "0x0000010f", "Wrapped key invalid", "0x00000110", "Wrapped key length range", "Wrapping key handle invalid", "Wrapping key size range", "Wrapping key type inconsistent", "0x00000116", "0x00000117", "0x00000118", "0x00000119", "0x0000011a", "0x0000011b", "0x0000011c", "0x0000011d", "0x0000011e", "0x0000011f", "Random seed not supported", "No random number generator", "0x00000122", "0x00000123", "0x00000124", "0x00000125", "0x00000126", "0x00000127", "0x00000128", "0x00000129", "0x0000012a", "0x0000012b", "0x0000012c", "0x0000012d", "0x0000012e", "0x0000012f", "Domain parameters invalid", "0x00000131", "0x00000132", "0x00000133", "0x00000134", "0x00000135", "0x00000136", "0x00000137", "0x00000138", "0x00000139", "0x0000013a", "0x0000013b", "0x0000013c", "0x0000013d", "0x0000013e", "0x0000013f", "0x00000140", "0x00000141", "0x00000142", "0x00000143", "0x00000144", "0x00000145", "0x00000146", "0x00000147", "0x00000148", "0x00000149", "0x0000014a", "0x0000014b", "0x0000014c", "0x0000014d", "0x0000014e", "0x0000014f", "Buffer too small", "0x00000151", "0x00000152", "0x00000153", "0x00000154", "0x00000155", "0x00000156", "0x00000157", "0x00000158", "0x00000159", "0x0000015a", "0x0000015b", "0x0000015c", "0x0000015d", "0x0000015e", "0x0000015f", "Saved state invalid", "0x00000161", "0x00000162", "0x00000163", "0x00000164", "0x00000165", "0x00000166", "0x00000167", "0x00000168", "0x00000169", "0x0000016a", "0x0000016b", "0x0000016c", "0x0000016d", "0x0000016e", "0x0000016f", "Information sensitive", "0x00000171", "0x00000172", "0x00000173", "0x00000174", "0x00000175", "0x00000176", "0x00000177", "0x00000178", "0x00000179", "0x0000017a", "0x0000017b", "0x0000017c", "0x0000017d", "0x0000017e", "0x0000017f", "State unsaveable", "0x00000181", "0x00000182", "0x00000183", "0x00000184", "0x00000185", "0x00000186", "0x00000187", "0x00000188", "0x00000189", "0x0000018a", "0x0000018b", "0x0000018c", "0x0000018d", "0x0000018e", "0x0000018f", "Cryptoki not initialized", "Cryptoki already initialized", "0x00000192", "0x00000193", "0x00000194", "0x00000195", "0x00000196", "0x00000197", "0x00000198", "0x00000199", "0x0000019a", "0x0000019b", "0x0000019c", "0x0000019d", "0x0000019e", "0x0000019f", "Mutex bad", "Mutex not locked", "0x000001a2", "0x000001a3", "0x000001a4", "0x000001a5", "0x000001a6", "0x000001a7", "0x000001a8", "0x000001a9", "0x000001aa", "0x000001ab", "0x000001ac", "0x000001ad", "0x000001ae", "0x000001af", "0x000001b0", "0x000001b1", "0x000001b2", "0x000001b3", "0x000001b4", "0x000001b5", "0x000001b6", "0x000001b7", "0x000001b8", "0x000001b9", "0x000001ba", "0x000001bb", "0x000001bc", "0x000001bd", "0x000001be", "0x000001bf", "0x000001c0", "0x000001c1", "0x000001c2", "0x000001c3", "0x000001c4", "0x000001c5", "0x000001c6", "0x000001c7", "0x000001c8", "0x000001c9", "0x000001ca", "0x000001cb", "0x000001cc", "0x000001cd", "0x000001ce", "0x000001cf", "0x000001d0", "0x000001d1", "0x000001d2", "0x000001d3", "0x000001d4", "0x000001d5", "0x000001d6", "0x000001d7", "0x000001d8", "0x000001d9", "0x000001da", "0x000001db", "0x000001dc", "0x000001dd", "0x000001de", "0x000001df", "0x000001e0", "0x000001e1", "0x000001e2", "0x000001e3", "0x000001e4", "0x000001e5", "0x000001e6", "0x000001e7", "0x000001e8", "0x000001e9", "0x000001ea", "0x000001eb", "0x000001ec", "0x000001ed", "0x000001ee", "0x000001ef", "0x000001f0", "0x000001f1", "0x000001f2", "0x000001f3", "0x000001f4", "0x000001f5", "0x000001f6", "0x000001f7", "0x000001f8", "0x000001f9", "0x000001fa", "0x000001fb", "0x000001fc", "0x000001fd", "0x000001fe", "0x000001ff", "Function rejected" }; #define ERRMSG(nr) ((nr) == CKR_VENDOR_DEFINED ? "Vendor defined" : \ ((nr) > sizeof (msg) / sizeof (msg[0]) ? \ "(unknown error code)" : msg[(nr)])) static const char * mechanism_type_str (CK_MECHANISM_TYPE mechanism_type) UNUSED; static const char * mechanism_type_str (CK_MECHANISM_TYPE mechanism_type) { switch (mechanism_type) { #define CKM_ONE(mechanism) \ case mechanism: \ return #mechanism; CKM_ONE (CKM_RSA_PKCS_KEY_PAIR_GEN); CKM_ONE (CKM_RSA_PKCS); default: return NULL; } } static const char *session_state_str (CK_STATE state) UNUSED; static const char * session_state_str (CK_STATE state) { switch (state) { #define CKS_ONE(state) \ case state: \ return #state; CKS_ONE (CKS_RO_PUBLIC_SESSION); CKS_ONE (CKS_RO_USER_FUNCTIONS); CKS_ONE (CKS_RW_PUBLIC_SESSION); CKS_ONE (CKS_RW_USER_FUNCTIONS); CKS_ONE (CKS_RW_SO_FUNCTIONS); default: return NULL; } } #define fail_if_err(err) \ do \ - { \ - if (err) \ + { unsigned int _err = (err); \ + if (_err) \ { \ fprintf (stderr, "%s:%d: %s\n", \ - __FILE__, __LINE__, ERRMSG(err)); \ + __FILE__, __LINE__, ERRMSG(_err)); \ exit (1); \ } \ } \ while (0) #define fail(errmsg) \ do \ { \ if (err) \ { \ fprintf (stderr, "%s:%d: %s\n", \ __FILE__, __LINE__, errmsg); \ exit (1); \ } \ } \ while (0) #ifdef _WIN32 #include #endif void init_cryptoki (void) { CK_RV err; #ifdef _WIN32 WSADATA wsadat; WSAStartup (0x202, &wsadat); #endif err = C_Initialize (NULL); fail_if_err (err); }