summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/Makefile.am1
-rw-r--r--common/attrs.c242
-rw-r--r--common/constants.c363
-rw-r--r--common/constants.h74
-rw-r--r--common/tests/Makefile.am1
-rw-r--r--common/tests/test-constants.c117
6 files changed, 566 insertions, 232 deletions
diff --git a/common/Makefile.am b/common/Makefile.am
index 9e459b5..d914fa8 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -22,6 +22,7 @@ libp11_library_la_SOURCES = \
attrs.c attrs.h \
array.c array.h \
buffer.c buffer.h \
+ constants.c constants.h \
debug.c debug.h \
dict.c dict.h \
library.c library.h \
diff --git a/common/attrs.c b/common/attrs.c
index 0ccf832..62b35cd 100644
--- a/common/attrs.c
+++ b/common/attrs.c
@@ -38,6 +38,7 @@
#include "attrs.h"
#include "buffer.h"
#include "compat.h"
+#include "constants.h"
#include "debug.h"
#include "pkcs11.h"
#include "pkcs11x.h"
@@ -48,6 +49,8 @@
#include <stdlib.h>
#include <string.h>
+#define ELEMS(x) (sizeof (x) / sizeof (x[0]))
+
bool
p11_attrs_is_empty (const CK_ATTRIBUTE *attrs)
{
@@ -662,29 +665,7 @@ static void
format_class (p11_buffer *buffer,
CK_OBJECT_CLASS klass)
{
- const char *string = NULL;
-
- switch (klass) {
- #define X(x) case x: string = #x; break;
- X (CKO_DATA)
- X (CKO_CERTIFICATE)
- X (CKO_PUBLIC_KEY)
- X (CKO_PRIVATE_KEY)
- X (CKO_SECRET_KEY)
- X (CKO_HW_FEATURE)
- X (CKO_DOMAIN_PARAMETERS)
- X (CKO_MECHANISM)
- X (CKO_X_TRUST_ASSERTION)
- X (CKO_X_CERTIFICATE_EXTENSION)
- X (CKO_NSS_CRL)
- X (CKO_NSS_SMIME)
- X (CKO_NSS_TRUST)
- X (CKO_NSS_BUILTIN_ROOT_LIST)
- X (CKO_NSS_NEWSLOT)
- X (CKO_NSS_DELSLOT)
- #undef X
- }
-
+ const char *string = p11_constant_name (p11_constant_classes, klass);
if (string != NULL)
p11_buffer_add (buffer, string, -1);
else
@@ -695,16 +676,7 @@ static void
format_assertion_type (p11_buffer *buffer,
CK_X_ASSERTION_TYPE type)
{
- const char *string = NULL;
-
- switch (type) {
- #define X(x) case x: string = #x; break;
- X (CKT_X_DISTRUSTED_CERTIFICATE)
- X (CKT_X_PINNED_CERTIFICATE)
- X (CKT_X_ANCHORED_CERTIFICATE)
- #undef X
- }
-
+ const char *string = p11_constant_name (p11_constant_asserts, type);
if (string != NULL)
p11_buffer_add (buffer, string, -1);
else
@@ -715,39 +687,7 @@ static void
format_key_type (p11_buffer *buffer,
CK_KEY_TYPE type)
{
- const char *string = NULL;
-
- switch (type) {
- #define X(x) case x: string = #x; break;
- X (CKK_RSA)
- X (CKK_DSA)
- X (CKK_DH)
- /* X (CKK_ECDSA) */
- X (CKK_EC)
- X (CKK_X9_42_DH)
- X (CKK_KEA)
- X (CKK_GENERIC_SECRET)
- X (CKK_RC2)
- X (CKK_RC4)
- X (CKK_DES)
- X (CKK_DES2)
- X (CKK_DES3)
- X (CKK_CAST)
- X (CKK_CAST3)
- X (CKK_CAST128)
- X (CKK_RC5)
- X (CKK_IDEA)
- X (CKK_SKIPJACK)
- X (CKK_BATON)
- X (CKK_JUNIPER)
- X (CKK_CDMF)
- X (CKK_AES)
- X (CKK_BLOWFISH)
- X (CKK_TWOFISH)
- X (CKK_NSS_PKCS8)
- #undef X
- }
-
+ const char *string = p11_constant_name (p11_constant_keys, type);
if (string != NULL)
p11_buffer_add (buffer, string, -1);
else
@@ -758,15 +698,7 @@ static void
format_certificate_type (p11_buffer *buffer,
CK_CERTIFICATE_TYPE type)
{
- const char *string = NULL;
-
- switch (type) {
- #define X(x) case x: string = #x; break;
- X (CKC_X_509)
- X (CKC_X_509_ATTR_CERT)
- X (CKC_WTLS)
- }
-
+ const char *string = p11_constant_name (p11_constant_certs, type);
if (string != NULL)
p11_buffer_add (buffer, string, -1);
else
@@ -777,17 +709,7 @@ static void
format_trust_value (p11_buffer *buffer,
CK_TRUST trust)
{
- const char *string = NULL;
-
- switch (trust) {
- #define X(x) case x: string = #x; break;
- X (CKT_NSS_TRUSTED)
- X (CKT_NSS_TRUSTED_DELEGATOR)
- X (CKT_NSS_NOT_TRUSTED)
- X (CKT_NSS_MUST_VERIFY_TRUST)
- X (CKT_NSS_TRUST_UNKNOWN)
- }
-
+ const char *string = p11_constant_name (p11_constant_trusts, trust);
if (string != NULL)
p11_buffer_add (buffer, string, -1);
else
@@ -798,23 +720,7 @@ static void
format_certificate_category (p11_buffer *buffer,
CK_ULONG category)
{
- const char *string = NULL;
-
- switch (category) {
- case 0:
- string = "unspecified";
- break;
- case 1:
- string = "token-user";
- break;
- case 2:
- string = "authority";
- break;
- case 3:
- string = "other-entry";
- break;
- }
-
+ const char *string = p11_constant_name (p11_constant_categories, category);
if (string != NULL)
buffer_append_printf (buffer, "%lu (%s)", category, string);
else
@@ -825,135 +731,7 @@ static void
format_attribute_type (p11_buffer *buffer,
CK_ULONG type)
{
- const char *string = NULL;
-
- switch (type) {
- #define X(x) case x: string = #x; break;
- X (CKA_CLASS)
- X (CKA_TOKEN)
- X (CKA_PRIVATE)
- X (CKA_LABEL)
- X (CKA_APPLICATION)
- X (CKA_VALUE)
- X (CKA_OBJECT_ID)
- X (CKA_CERTIFICATE_TYPE)
- X (CKA_ISSUER)
- X (CKA_SERIAL_NUMBER)
- X (CKA_AC_ISSUER)
- X (CKA_OWNER)
- X (CKA_ATTR_TYPES)
- X (CKA_TRUSTED)
- X (CKA_CERTIFICATE_CATEGORY)
- X (CKA_JAVA_MIDP_SECURITY_DOMAIN)
- X (CKA_URL)
- X (CKA_HASH_OF_SUBJECT_PUBLIC_KEY)
- X (CKA_HASH_OF_ISSUER_PUBLIC_KEY)
- X (CKA_CHECK_VALUE)
- X (CKA_KEY_TYPE)
- X (CKA_SUBJECT)
- X (CKA_ID)
- X (CKA_SENSITIVE)
- X (CKA_ENCRYPT)
- X (CKA_DECRYPT)
- X (CKA_WRAP)
- X (CKA_UNWRAP)
- X (CKA_SIGN)
- X (CKA_SIGN_RECOVER)
- X (CKA_VERIFY)
- X (CKA_VERIFY_RECOVER)
- X (CKA_DERIVE)
- X (CKA_START_DATE)
- X (CKA_END_DATE)
- X (CKA_MODULUS)
- X (CKA_MODULUS_BITS)
- X (CKA_PUBLIC_EXPONENT)
- X (CKA_PRIVATE_EXPONENT)
- X (CKA_PRIME_1)
- X (CKA_PRIME_2)
- X (CKA_EXPONENT_1)
- X (CKA_EXPONENT_2)
- X (CKA_COEFFICIENT)
- X (CKA_PRIME)
- X (CKA_SUBPRIME)
- X (CKA_BASE)
- X (CKA_PRIME_BITS)
- /* X (CKA_SUBPRIME_BITS) */
- X (CKA_SUB_PRIME_BITS)
- X (CKA_VALUE_BITS)
- X (CKA_VALUE_LEN)
- X (CKA_EXTRACTABLE)
- X (CKA_LOCAL)
- X (CKA_NEVER_EXTRACTABLE)
- X (CKA_ALWAYS_SENSITIVE)
- X (CKA_KEY_GEN_MECHANISM)
- X (CKA_MODIFIABLE)
- X (CKA_ECDSA_PARAMS)
- /* X (CKA_EC_PARAMS) */
- X (CKA_EC_POINT)
- X (CKA_SECONDARY_AUTH)
- X (CKA_AUTH_PIN_FLAGS)
- X (CKA_ALWAYS_AUTHENTICATE)
- X (CKA_WRAP_WITH_TRUSTED)
- X (CKA_WRAP_TEMPLATE)
- X (CKA_UNWRAP_TEMPLATE)
- X (CKA_HW_FEATURE_TYPE)
- X (CKA_RESET_ON_INIT)
- X (CKA_HAS_RESET)
- X (CKA_PIXEL_X)
- X (CKA_PIXEL_Y)
- X (CKA_RESOLUTION)
- X (CKA_CHAR_ROWS)
- X (CKA_CHAR_COLUMNS)
- X (CKA_COLOR)
- X (CKA_BITS_PER_PIXEL)
- X (CKA_CHAR_SETS)
- X (CKA_ENCODING_METHODS)
- X (CKA_MIME_TYPES)
- X (CKA_MECHANISM_TYPE)
- X (CKA_REQUIRED_CMS_ATTRIBUTES)
- X (CKA_DEFAULT_CMS_ATTRIBUTES)
- X (CKA_SUPPORTED_CMS_ATTRIBUTES)
- X (CKA_ALLOWED_MECHANISMS)
- X (CKA_X_ASSERTION_TYPE)
- X (CKA_X_CERTIFICATE_VALUE)
- X (CKA_X_PURPOSE)
- X (CKA_X_PEER)
- X (CKA_X_DISTRUSTED)
- X (CKA_X_CRITICAL)
- X (CKA_NSS_URL)
- X (CKA_NSS_EMAIL)
- X (CKA_NSS_SMIME_INFO)
- X (CKA_NSS_SMIME_TIMESTAMP)
- X (CKA_NSS_PKCS8_SALT)
- X (CKA_NSS_PASSWORD_CHECK)
- X (CKA_NSS_EXPIRES)
- X (CKA_NSS_KRL)
- X (CKA_NSS_PQG_COUNTER)
- X (CKA_NSS_PQG_SEED)
- X (CKA_NSS_PQG_H)
- X (CKA_NSS_PQG_SEED_BITS)
- X (CKA_NSS_MODULE_SPEC)
- X (CKA_TRUST_DIGITAL_SIGNATURE)
- X (CKA_TRUST_NON_REPUDIATION)
- X (CKA_TRUST_KEY_ENCIPHERMENT)
- X (CKA_TRUST_DATA_ENCIPHERMENT)
- X (CKA_TRUST_KEY_AGREEMENT)
- X (CKA_TRUST_KEY_CERT_SIGN)
- X (CKA_TRUST_CRL_SIGN)
- X (CKA_TRUST_SERVER_AUTH)
- X (CKA_TRUST_CLIENT_AUTH)
- X (CKA_TRUST_CODE_SIGNING)
- X (CKA_TRUST_EMAIL_PROTECTION)
- X (CKA_TRUST_IPSEC_END_SYSTEM)
- X (CKA_TRUST_IPSEC_TUNNEL)
- X (CKA_TRUST_IPSEC_USER)
- X (CKA_TRUST_TIME_STAMPING)
- X (CKA_TRUST_STEP_UP_APPROVED)
- X (CKA_CERT_SHA1_HASH)
- X (CKA_CERT_MD5_HASH)
- #undef X
- }
-
+ const char *string = p11_constant_name (p11_constant_types, type);
if (string != NULL)
p11_buffer_add (buffer, string, -1);
else
diff --git a/common/constants.c b/common/constants.c
new file mode 100644
index 0000000..82bdb20
--- /dev/null
+++ b/common/constants.c
@@ -0,0 +1,363 @@
+/*
+ * Copyright (C) 2013, Redhat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ * * Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ * * The names of contributors to this software may not be
+ * used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * Author: Stef Walter <stefw@redhat.com>
+ */
+
+#include "config.h"
+
+#include "attrs.h"
+#include "constants.h"
+#include "debug.h"
+#include "pkcs11.h"
+#include "pkcs11x.h"
+
+#include <stdlib.h>
+
+#define ELEMS(x) (sizeof (x) / sizeof (x[0]))
+
+/*
+ * These are in numeric order of their type for easy lookup
+ * After changing something make sure to run the test-attrs
+ * test to verify everything is in order.
+ */
+
+#define CT(x, n) { x, #x, n },
+
+const p11_constant p11_constant_types[] = {
+ CT (CKA_CLASS, "class")
+ CT (CKA_TOKEN, "token")
+ CT (CKA_PRIVATE, "private")
+ CT (CKA_LABEL, "label")
+ CT (CKA_APPLICATION, "application")
+ CT (CKA_VALUE, "value")
+ CT (CKA_OBJECT_ID, "object-id")
+ CT (CKA_CERTIFICATE_TYPE, "certificate-type")
+ CT (CKA_ISSUER, "issuer")
+ CT (CKA_SERIAL_NUMBER, "serial-number")
+ CT (CKA_AC_ISSUER, "ac-issuer")
+ CT (CKA_OWNER, "owner")
+ CT (CKA_ATTR_TYPES, "attr-types")
+ CT (CKA_TRUSTED, "trusted")
+ CT (CKA_CERTIFICATE_CATEGORY, "certificate-category")
+ CT (CKA_JAVA_MIDP_SECURITY_DOMAIN, "java-midp-security-domain")
+ CT (CKA_URL, "url")
+ CT (CKA_HASH_OF_SUBJECT_PUBLIC_KEY, "hash-of-subject-public-key")
+ CT (CKA_HASH_OF_ISSUER_PUBLIC_KEY, "hash-of-issuer-public-key")
+ CT (CKA_CHECK_VALUE, "check-value")
+ CT (CKA_KEY_TYPE, "key-type")
+ CT (CKA_SUBJECT, "subject")
+ CT (CKA_ID, "id")
+ CT (CKA_SENSITIVE, "sensitive")
+ CT (CKA_ENCRYPT, "encrypt")
+ CT (CKA_DECRYPT, "decrypt")
+ CT (CKA_WRAP, "wrap")
+ CT (CKA_UNWRAP, "unwrap")
+ CT (CKA_SIGN, "sign")
+ CT (CKA_SIGN_RECOVER, "sign-recover")
+ CT (CKA_VERIFY, "verify")
+ CT (CKA_VERIFY_RECOVER, "recover")
+ CT (CKA_DERIVE, "derive")
+ CT (CKA_START_DATE, "start-date")
+ CT (CKA_END_DATE, "end-date")
+ CT (CKA_MODULUS, "modulus")
+ CT (CKA_MODULUS_BITS, "modulus-bits")
+ CT (CKA_PUBLIC_EXPONENT, "public-exponent")
+ CT (CKA_PRIVATE_EXPONENT, "private-exponent")
+ CT (CKA_PRIME_1, "prime-1")
+ CT (CKA_PRIME_2, "prime-2")
+ CT (CKA_EXPONENT_1, "exponent-1")
+ CT (CKA_EXPONENT_2, "exponent-2")
+ CT (CKA_COEFFICIENT, "coefficient")
+ CT (CKA_PRIME, "prime")
+ CT (CKA_SUBPRIME, "subprime")
+ CT (CKA_BASE, "base")
+ CT (CKA_PRIME_BITS, "prime-bits")
+ /* CT (CKA_SUBPRIME_BITS) */
+ CT (CKA_SUB_PRIME_BITS, "subprime-bits")
+ CT (CKA_VALUE_BITS, "value-bits")
+ CT (CKA_VALUE_LEN, "vaule-len")
+ CT (CKA_EXTRACTABLE, "extractable")
+ CT (CKA_LOCAL, "local")
+ CT (CKA_NEVER_EXTRACTABLE, "never-extractable")
+ CT (CKA_ALWAYS_SENSITIVE, "always-sensitive")
+ CT (CKA_KEY_GEN_MECHANISM, "key-gen-mechanism")
+ CT (CKA_MODIFIABLE, "modifiable")
+ CT (CKA_ECDSA_PARAMS, "ecdsa-params")
+ /* CT (CKA_EC_PARAMS) */
+ CT (CKA_EC_POINT, "ec-point")
+ CT (CKA_SECONDARY_AUTH, "secondary-auth")
+ CT (CKA_AUTH_PIN_FLAGS, "auth-pin-flags")
+ CT (CKA_ALWAYS_AUTHENTICATE, "always-authenticate")
+ CT (CKA_WRAP_WITH_TRUSTED, "wrap-with-trusted")
+ CT (CKA_HW_FEATURE_TYPE, "hw-feature-type")
+ CT (CKA_RESET_ON_INIT, "reset-on-init")
+ CT (CKA_HAS_RESET, "has-reset")
+ CT (CKA_PIXEL_X, "pixel-x")
+ CT (CKA_PIXEL_Y, "pixel-y")
+ CT (CKA_RESOLUTION, "resolution")
+ CT (CKA_CHAR_ROWS, "char-rows")
+ CT (CKA_CHAR_COLUMNS, "char-columns")
+ CT (CKA_COLOR, "color")
+ CT (CKA_BITS_PER_PIXEL, "bits-per-pixel")
+ CT (CKA_CHAR_SETS, "char-sets")
+ CT (CKA_ENCODING_METHODS, "encoding-methods")
+ CT (CKA_MIME_TYPES, "mime-types")
+ CT (CKA_MECHANISM_TYPE, "mechanism-type")
+ CT (CKA_REQUIRED_CMS_ATTRIBUTES, "required-cms-attributes")
+ CT (CKA_DEFAULT_CMS_ATTRIBUTES, "default-cms-attributes")
+ CT (CKA_SUPPORTED_CMS_ATTRIBUTES, "supported-cms-attributes")
+ CT (CKA_WRAP_TEMPLATE, "wrap-template")
+ CT (CKA_UNWRAP_TEMPLATE, "unwrap-template")
+ CT (CKA_ALLOWED_MECHANISMS, "allowed-mechanisms")
+ CT (CKA_NSS_URL, "nss-url")
+ CT (CKA_NSS_EMAIL, "nss-email")
+ CT (CKA_NSS_SMIME_INFO, "nss-smime-constant")
+ CT (CKA_NSS_SMIME_TIMESTAMP, "nss-smime-timestamp")
+ CT (CKA_NSS_PKCS8_SALT, "nss-pkcs8-salt")
+ CT (CKA_NSS_PASSWORD_CHECK, "nss-password-check")
+ CT (CKA_NSS_EXPIRES, "nss-expires")
+ CT (CKA_NSS_KRL, "nss-krl")
+ CT (CKA_NSS_PQG_COUNTER, "nss-pqg-counter")
+ CT (CKA_NSS_PQG_SEED, "nss-pqg-seed")
+ CT (CKA_NSS_PQG_H, "nss-pqg-h")
+ CT (CKA_NSS_PQG_SEED_BITS, "nss-pqg-seed-bits")
+ CT (CKA_NSS_MODULE_SPEC, "nss-module-spec")
+ CT (CKA_TRUST_DIGITAL_SIGNATURE, "trust-digital-signature")
+ CT (CKA_TRUST_NON_REPUDIATION, "trust-non-repudiation")
+ CT (CKA_TRUST_KEY_ENCIPHERMENT, "trust-key-encipherment")
+ CT (CKA_TRUST_DATA_ENCIPHERMENT, "trust-data-encipherment")
+ CT (CKA_TRUST_KEY_AGREEMENT, "trust-key-agreement")
+ CT (CKA_TRUST_KEY_CERT_SIGN, "trust-key-cert-sign")
+ CT (CKA_TRUST_CRL_SIGN, "trust-crl-sign")
+ CT (CKA_TRUST_SERVER_AUTH, "trust-server-auth")
+ CT (CKA_TRUST_CLIENT_AUTH, "trust-client-auth")
+ CT (CKA_TRUST_CODE_SIGNING, "trust-code-signing")
+ CT (CKA_TRUST_EMAIL_PROTECTION, "trust-email-protection")
+ CT (CKA_TRUST_IPSEC_END_SYSTEM, "trust-ipsec-end-system")
+ CT (CKA_TRUST_IPSEC_TUNNEL, "trust-ipsec-tunnel")
+ CT (CKA_TRUST_IPSEC_USER, "trust-ipsec-user")
+ CT (CKA_TRUST_TIME_STAMPING, "trust-time-stamping")
+ CT (CKA_TRUST_STEP_UP_APPROVED, "trust-step-up-approved")
+ CT (CKA_CERT_SHA1_HASH, "cert-sha1-hash")
+ CT (CKA_CERT_MD5_HASH, "cert-md5-hash")
+ CT (CKA_X_ASSERTION_TYPE, "x-assertion-type")
+ CT (CKA_X_CERTIFICATE_VALUE, "x-cetrificate-value")
+ CT (CKA_X_PURPOSE, "x-purpose")
+ CT (CKA_X_PEER, "x-peer")
+ CT (CKA_X_DISTRUSTED, "distrusted")
+ CT (CKA_X_CRITICAL, "x-critical")
+ { CKA_INVALID },
+};
+
+const p11_constant p11_constant_classes[] = {
+ CT (CKO_DATA, "data")
+ CT (CKO_CERTIFICATE, "certificate")
+ CT (CKO_PUBLIC_KEY, "public-key")
+ CT (CKO_PRIVATE_KEY, "private-key")
+ CT (CKO_SECRET_KEY, "secret-key")
+ CT (CKO_HW_FEATURE, "hw-feature")
+ CT (CKO_DOMAIN_PARAMETERS, "domain-parameters")
+ CT (CKO_MECHANISM, "mechanism")
+ CT (CKO_NSS_CRL, "nss-crl")
+ CT (CKO_NSS_SMIME, "nss-smime")
+ CT (CKO_NSS_TRUST, "nss-trust")
+ CT (CKO_NSS_BUILTIN_ROOT_LIST, "nss-builtin-root-list")
+ CT (CKO_NSS_NEWSLOT, "nss-newslot")
+ CT (CKO_NSS_DELSLOT, "nss-delslot")
+ CT (CKO_X_TRUST_ASSERTION, "x-trust-assertion")
+ CT (CKO_X_CERTIFICATE_EXTENSION, "x-certificate-extension")
+ { CKA_INVALID },
+};
+
+const p11_constant p11_constant_trusts[] = {
+ CT (CKT_NSS_TRUSTED, "nss-trusted")
+ CT (CKT_NSS_TRUSTED_DELEGATOR, "nss-trusted-delegator")
+ CT (CKT_NSS_MUST_VERIFY_TRUST, "nss-must-verify-trust")
+ CT (CKT_NSS_TRUST_UNKNOWN, "nss-trust-unknown")
+ CT (CKT_NSS_NOT_TRUSTED, "nss-not-trusted")
+ CT (CKT_NSS_VALID_DELEGATOR, "nss-valid-delegator")
+ { CKA_INVALID },
+};
+
+const p11_constant p11_constant_certs[] = {
+ CT (CKC_X_509, "x-509")
+ CT (CKC_X_509_ATTR_CERT, "x-509-attr-cert")
+ CT (CKC_WTLS, "wtls")
+ { CKA_INVALID },
+};
+
+const p11_constant p11_constant_keys[] = {
+ CT (CKK_RSA, "rsa")
+ CT (CKK_DSA, "dsa")
+ CT (CKK_DH, "dh")
+ /* CT (CKK_ECDSA) */
+ CT (CKK_EC, "ec")
+ CT (CKK_X9_42_DH, "x9-42-dh")
+ CT (CKK_KEA, "kea")
+ CT (CKK_GENERIC_SECRET, "generic-secret")
+ CT (CKK_RC2, "rc2")
+ CT (CKK_RC4, "rc4")
+ CT (CKK_DES, "des")
+ CT (CKK_DES2, "des2")
+ CT (CKK_DES3, "des3")
+ CT (CKK_CAST, "cast")
+ CT (CKK_CAST3, "cast3")
+ CT (CKK_CAST128, "cast128")
+ CT (CKK_RC5, "rc5")
+ CT (CKK_IDEA, "idea")
+ CT (CKK_SKIPJACK, "skipjack")
+ CT (CKK_BATON, "baton")
+ CT (CKK_JUNIPER, "juniper")
+ CT (CKK_CDMF, "cdmf")
+ CT (CKK_AES, "aes")
+ CT (CKK_BLOWFISH, "blowfish")
+ CT (CKK_TWOFISH, "twofish")
+ CT (CKK_NSS_PKCS8, "nss-pkcs8")
+ { CKA_INVALID },
+};
+
+const p11_constant p11_constant_asserts[] = {
+ CT (CKT_X_DISTRUSTED_CERTIFICATE, "x-distrusted-certificate")
+ CT (CKT_X_PINNED_CERTIFICATE, "x-pinned-certificate")
+ CT (CKT_X_ANCHORED_CERTIFICATE, "x-anchored-certificate")
+ { CKA_INVALID },
+};
+
+const p11_constant p11_constant_categories[] = {
+ { 0, "unspecified", "unspecified" },
+ { 1, "token-user", "token-user" },
+ { 2, "authority", "authority" },
+ { 3, "other-entry", "other-entry" },
+ { CKA_INVALID },
+};
+
+#undef CT
+
+struct {
+ const p11_constant *table;
+ int length;
+} tables[] = {
+ { p11_constant_types, ELEMS (p11_constant_types) - 1 },
+ { p11_constant_classes, ELEMS (p11_constant_classes) - 1 },
+ { p11_constant_trusts, ELEMS (p11_constant_trusts) - 1 },
+ { p11_constant_certs, ELEMS (p11_constant_certs) - 1 },
+ { p11_constant_keys, ELEMS (p11_constant_keys) - 1 },
+ { p11_constant_asserts, ELEMS (p11_constant_asserts) - 1 },
+ { p11_constant_categories, ELEMS (p11_constant_categories) - 1 }
+};
+
+static int
+compar_attr_info (const void *one,
+ const void *two)
+{
+ const p11_constant *a1 = one;
+ const p11_constant *a2 = two;
+ if (a1->value == a2->value)
+ return 0;
+ if (a1->value < a2->value)
+ return -1;
+ return 1;
+}
+
+static const p11_constant *
+lookup_info (const p11_constant *table,
+ CK_ATTRIBUTE_TYPE type)
+{
+ p11_constant match = { type, NULL, NULL };
+ int length = -1;
+ int i;
+
+ for (i = 0; i < ELEMS (tables); i++) {
+ if (table == tables[i].table) {
+ length = tables[i].length;
+ break;
+ }
+ }
+
+ return_val_if_fail (length != -1, NULL);
+ return bsearch (&match, table, length, sizeof (p11_constant), compar_attr_info);
+
+}
+const char *
+p11_constant_name (const p11_constant *constants,
+ CK_ULONG type)
+{
+ const p11_constant *constant = lookup_info (constants, type);
+ return constant ? constant->name : NULL;
+}
+
+const char *
+p11_constant_nick (const p11_constant *constants,
+ CK_ULONG type)
+{
+ const p11_constant *constant = lookup_info (constants, type);
+ return constant ? constant->nick : NULL;
+}
+
+p11_dict *
+p11_constant_reverse (bool nick)
+{
+ const p11_constant *table;
+ p11_dict *lookups;
+ int length = -1;
+ int i, j;
+
+ lookups = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL);
+ return_val_if_fail (lookups != NULL, NULL);
+
+ for (i = 0; i < ELEMS (tables); i++) {
+ table = tables[i].table;
+ length = tables[i].length;
+
+ for (j = 0; j < length; j++) {
+ if (!p11_dict_set (lookups,
+ nick ? (void *)table[j].nick : (void *)table[j].name,
+ (void *)&table[j].value))
+ return_val_if_reached (NULL);
+ }
+ }
+
+ return lookups;
+}
+
+CK_ULONG
+p11_constant_resolve (p11_dict *reversed,
+ const char *string)
+{
+ CK_ULONG *ptr;
+
+ return_val_if_fail (reversed != NULL, CKA_INVALID);
+ return_val_if_fail (string != NULL, CKA_INVALID);
+
+ ptr = p11_dict_get (reversed, string);
+ return ptr ? *ptr : CKA_INVALID;
+}
diff --git a/common/constants.h b/common/constants.h
new file mode 100644
index 0000000..82a0879
--- /dev/null
+++ b/common/constants.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2012, Redhat Inc.
+ * Copyright (c) 2011, Collabora Ltd.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ * * Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ * * The names of contributors to this software may not be
+ * used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * Author: Stef Walter <stefw@collabora.co.uk>
+ */
+
+#ifndef P11_CONSTANTS_H_
+#define P11_CONSTANTS_H_
+
+#include "compat.h"
+#include "dict.h"
+#include "pkcs11.h"
+
+typedef struct {
+ CK_ULONG value;
+ const char *name;
+ const char *nick;
+} p11_constant;
+
+const char * p11_constant_name (const p11_constant *constants,
+ CK_ULONG value);
+
+const char * p11_constant_nick (const p11_constant *constants,
+ CK_ULONG type);
+
+p11_dict * p11_constant_reverse (bool nick);
+
+CK_ULONG p11_constant_resolve (p11_dict *table,
+ const char *string);
+
+extern const p11_constant p11_constant_types[];
+
+extern const p11_constant p11_constant_classes[];
+
+extern const p11_constant p11_constant_trusts[];
+
+extern const p11_constant p11_constant_certs[];
+
+extern const p11_constant p11_constant_keys[];
+
+extern const p11_constant p11_constant_asserts[];
+
+extern const p11_constant p11_constant_categories[];
+
+#endif /* P11_CONSTANTS_H_ */
diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am
index 2d28b86..582c1fb 100644
--- a/common/tests/Makefile.am
+++ b/common/tests/Makefile.am
@@ -17,6 +17,7 @@ LDADD = \
CHECK_PROGS = \
test-dict \
test-array \
+ test-constants \
test-attrs \
test-buffer \
$(NULL)
diff --git a/common/tests/test-constants.c b/common/tests/test-constants.c
new file mode 100644
index 0000000..4cd4472
--- /dev/null
+++ b/common/tests/test-constants.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2012 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ * * Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ * * The names of contributors to this software may not be
+ * used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * Author: Stef Walter <stefw@gnome.org>
+ */
+
+#include "config.h"
+#include "CuTest.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "attrs.h"
+#include "constants.h"
+#include "debug.h"
+
+static void
+test_constants (CuTest *tc)
+{
+ const p11_constant *constant;
+ p11_dict *nicks, *names;
+ CK_ULONG check;
+ int i, j;
+
+ static const p11_constant *constants[] = {
+ p11_constant_types,
+ p11_constant_classes,
+ p11_constant_trusts,
+ p11_constant_certs,
+ p11_constant_keys,
+ p11_constant_asserts,
+ p11_constant_categories,
+ NULL
+ };
+
+ nicks = p11_constant_reverse (true);
+ names = p11_constant_reverse (false);
+
+ for (j = 0; constants[j] != NULL; j++) {
+ constant = constants[j];
+ for (i = 1; constant[i].value != CKA_INVALID; i++) {
+ if (constant[i].value < constant[i - 1].value) {
+ CuFail_Line (tc, __FILE__, __LINE__,
+ "attr constant out of order", constant[i].name);
+ }
+ }
+ for (i = 0; constant[i].value != CKA_INVALID; i++) {
+ CuAssertPtrNotNull (tc, constant[i].nick);
+ CuAssertPtrNotNull (tc, constant[i].name);
+
+ CuAssertStrEquals (tc, constant[i].nick,
+ p11_constant_nick (constant, constant[i].value));
+ CuAssertStrEquals (tc, constant[i].name,
+ p11_constant_name (constant, constant[i].value));
+
+ check = p11_constant_resolve (nicks, constant[i].nick);
+ CuAssertIntEquals (tc, constant[i].value, check);
+
+ check = p11_constant_resolve (names, constant[i].name);
+ CuAssertIntEquals (tc, constant[i].value, check);
+ }
+ }
+
+ p11_dict_free (names);
+ p11_dict_free (nicks);
+}
+
+int
+main (void)
+{
+ CuString *output = CuStringNew ();
+ CuSuite* suite = CuSuiteNew ();
+ int ret;
+
+ putenv ("P11_KIT_STRICT=1");
+ p11_debug_init ();
+ SUITE_ADD_TEST (suite, test_constants);
+
+ CuSuiteRun (suite);
+ CuSuiteSummary (suite, output);
+ CuSuiteDetails (suite, output);
+ printf ("%s\n", output->buffer);
+ ret = suite->failCount;
+ CuSuiteDelete (suite);
+ CuStringDelete (output);
+
+ return ret;
+}